forked from NUDT-compiler/nudt-compiler-cpp
Compare commits
No commits in common. 'lab2' and 'master' have entirely different histories.
@ -1 +0,0 @@
|
|||||||
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
||||||
cd "$ROOT"
|
|
||||||
|
|
||||||
# 生成ANTLR解析器代码
|
|
||||||
echo "生成ANTLR解析器代码..."
|
|
||||||
java -jar "$ROOT/third_party/antlr-4.13.2-complete.jar" -Dlanguage=Cpp -visitor -o "$ROOT/build/generated/antlr4" "$ROOT/src/antlr4/SysY.g4"
|
|
||||||
|
|
||||||
|
|
||||||
# 构建项目
|
|
||||||
echo "构建项目..."
|
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
|
||||||
cmake --build build --parallel
|
|
||||||
|
|
||||||
echo "恭喜通关"
|
|
||||||
@ -1,50 +1,39 @@
|
|||||||
#include "irgen/IRGen.h"
|
#include "irgen/IRGen.h"
|
||||||
|
|
||||||
#include <any>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "RuleContext.h"
|
|
||||||
#include "SysYParser.h"
|
#include "SysYParser.h"
|
||||||
#include "ir/IR.h"
|
#include "ir/IR.h"
|
||||||
#include "utils/Log.h"
|
#include "utils/Log.h"
|
||||||
|
|
||||||
|
// 语句生成当前只实现了最小子集。
|
||||||
|
// 目前支持:
|
||||||
|
// - return <exp>;
|
||||||
|
//
|
||||||
|
// 还未支持:
|
||||||
|
// - 赋值语句
|
||||||
|
// - if / while 等控制流
|
||||||
|
// - 空语句、块语句嵌套分发之外的更多语句形态
|
||||||
|
|
||||||
std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
|
std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw std::runtime_error(FormatError("irgen", "缺少语句"));
|
throw std::runtime_error(FormatError("irgen", "缺少语句"));
|
||||||
}
|
}
|
||||||
|
if (ctx->returnStmt()) {
|
||||||
if (auto* r = dynamic_cast<SysYParser::ReturnStmtContext*>(ctx->returnStmt())) {
|
return ctx->returnStmt()->accept(this);
|
||||||
return visitReturnStmt(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 兼容不同 ANTLR 版本的遍历兜底
|
|
||||||
for (auto* ch : ctx->children) {
|
|
||||||
if (auto* rs = dynamic_cast<SysYParser::ReturnStmtContext*>(
|
|
||||||
dynamic_cast<antlr4::RuleContext*>(ch))) {
|
|
||||||
return visitReturnStmt(rs);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw std::runtime_error(FormatError("irgen", "暂不支持的语句类型"));
|
||||||
throw std::runtime_error(FormatError("irgen", "暂不支持的语句"));
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
|
std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
throw std::runtime_error(FormatError("irgen", "缺少 return"));
|
throw std::runtime_error(FormatError("irgen", "缺少 return 语句"));
|
||||||
}
|
}
|
||||||
if (current_func_void_) {
|
if (!ctx->exp()) {
|
||||||
if (ctx->exp()) {
|
throw std::runtime_error(FormatError("irgen", "return 缺少表达式"));
|
||||||
throw std::runtime_error(FormatError("irgen", "void return 带值"));
|
|
||||||
}
|
|
||||||
// 修复点:原版的 CreateRetVoid() 名字错了,正确的 API 是 CreateRet()
|
|
||||||
builder_.CreateRet(nullptr);
|
|
||||||
} else {
|
|
||||||
if (!ctx->exp()) {
|
|
||||||
throw std::runtime_error(FormatError("irgen", "return 缺表达式"));
|
|
||||||
}
|
|
||||||
ir::Value* v = EvalExpr(*ctx->exp());
|
|
||||||
builder_.CreateRet(v);
|
|
||||||
}
|
}
|
||||||
return std::any(BlockFlow::Terminated);
|
ir::Value* v = EvalExpr(*ctx->exp());
|
||||||
}
|
builder_.CreateRet(v);
|
||||||
|
return BlockFlow::Terminated;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,110 +1,110 @@
|
|||||||
int A[1024][1024];
|
int A[1024][1024];
|
||||||
int B[1024][1024];
|
int B[1024][1024];
|
||||||
int C[1024][1024];
|
int C[1024][1024];
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int T = getint(); // 矩阵规模
|
int T = getint(); // 矩阵规模
|
||||||
int R = getint(); // 重复次数
|
int R = getint(); // 重复次数
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
if (i < T / 2) {
|
if (i < T / 2) {
|
||||||
getarray(A[i]);
|
getarray(A[i]);
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
if (i >= T / 2) {
|
if (i >= T / 2) {
|
||||||
getarray(B[i]);
|
getarray(B[i]);
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
starttime();
|
starttime();
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
if (i >= T / 2) {
|
if (i >= T / 2) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
A[i][j] = -1;
|
A[i][j] = -1;
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
if (i < T / 2) {
|
if (i < T / 2) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
B[i][j] = -1;
|
B[i][j] = -1;
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
C[i][j] = A[i][j] * 2 + B[i][j] * 3;
|
C[i][j] = A[i][j] * 2 + B[i][j] * 3;
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
int val = C[i][j];
|
int val = C[i][j];
|
||||||
val = val * val + 7;
|
val = val * val + 7;
|
||||||
val = val / 3;
|
val = val / 3;
|
||||||
C[i][j] = val;
|
C[i][j] = val;
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
int k = 0;
|
int k = 0;
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
while (k < T) {
|
while (k < T) {
|
||||||
sum = sum + C[i][k] * A[k][j];
|
sum = sum + C[i][k] * A[k][j];
|
||||||
k = k + 1;
|
k = k + 1;
|
||||||
}
|
}
|
||||||
A[i][j] = sum;
|
A[i][j] = sum;
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
int r = 0;
|
int r = 0;
|
||||||
while (r < R) {
|
while (r < R) {
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < T) {
|
while (i < T) {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (j < T) {
|
while (j < T) {
|
||||||
total = total + A[i][j] * A[i][j];
|
total = total + A[i][j] * A[i][j];
|
||||||
j = j + 1;
|
j = j + 1;
|
||||||
}
|
}
|
||||||
i = i + 1;
|
i = i + 1;
|
||||||
}
|
}
|
||||||
r = r + 1;
|
r = r + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
stoptime();
|
stoptime();
|
||||||
|
|
||||||
putint(total);
|
putint(total);
|
||||||
putch(10);
|
putch(10);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
1
|
1
|
||||||
0
|
0
|
||||||
Loading…
Reference in new issue