parent
d4f4b77b1e
commit
03bd6d88e3
@ -1,29 +0,0 @@
|
||||
|
||||
// AST 节点定义与实现:
|
||||
// - 表达式、语句、声明、函数、类型等节点
|
||||
// - 支持后续阶段在节点上附加信息(类型、符号绑定、常量值等)
|
||||
#include "ast/AstNodes.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace ast {
|
||||
|
||||
NumberExpr::NumberExpr(int v) : value(v) {}
|
||||
|
||||
VarExpr::VarExpr(std::string n) : name(std::move(n)) {}
|
||||
|
||||
BinaryExpr::BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs,
|
||||
std::shared_ptr<Expr> rhs)
|
||||
: op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}
|
||||
|
||||
ReturnStmt::ReturnStmt(std::shared_ptr<Expr> v) : value(std::move(v)) {}
|
||||
|
||||
VarDecl::VarDecl(std::string n, std::shared_ptr<Expr> i)
|
||||
: name(std::move(n)), init(std::move(i)) {}
|
||||
|
||||
FuncDef::FuncDef(std::string n, std::shared_ptr<Block> b)
|
||||
: name(std::move(n)), body(std::move(b)) {}
|
||||
|
||||
CompUnit::CompUnit(std::shared_ptr<FuncDef> f) : func(std::move(f)) {}
|
||||
|
||||
} // namespace ast
|
||||
@ -1,70 +0,0 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ast {
|
||||
|
||||
enum class BinaryOp { Add, Sub, Mul, Div };
|
||||
|
||||
struct Expr {
|
||||
virtual ~Expr() = default;
|
||||
};
|
||||
|
||||
struct NumberExpr : Expr {
|
||||
int value{};
|
||||
explicit NumberExpr(int v);
|
||||
};
|
||||
|
||||
struct VarExpr : Expr {
|
||||
std::string name;
|
||||
explicit VarExpr(std::string n);
|
||||
};
|
||||
|
||||
struct BinaryExpr : Expr {
|
||||
BinaryOp op;
|
||||
std::shared_ptr<Expr> lhs;
|
||||
std::shared_ptr<Expr> rhs;
|
||||
BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs, std::shared_ptr<Expr> rhs);
|
||||
};
|
||||
|
||||
struct Stmt {
|
||||
virtual ~Stmt() = default;
|
||||
};
|
||||
|
||||
struct ReturnStmt : Stmt {
|
||||
std::shared_ptr<Expr> value;
|
||||
explicit ReturnStmt(std::shared_ptr<Expr> v);
|
||||
};
|
||||
|
||||
struct VarDecl {
|
||||
std::string name;
|
||||
std::shared_ptr<Expr> init; // nullptr if no initializer
|
||||
VarDecl(std::string n, std::shared_ptr<Expr> i);
|
||||
};
|
||||
|
||||
struct Block {
|
||||
std::vector<std::shared_ptr<VarDecl>> varDecls;
|
||||
std::vector<std::shared_ptr<Stmt>> stmts;
|
||||
};
|
||||
|
||||
struct FuncDef {
|
||||
std::string name;
|
||||
std::shared_ptr<Block> body;
|
||||
FuncDef(std::string n, std::shared_ptr<Block> b);
|
||||
};
|
||||
|
||||
struct CompUnit {
|
||||
std::shared_ptr<FuncDef> func;
|
||||
explicit CompUnit(std::shared_ptr<FuncDef> f);
|
||||
};
|
||||
|
||||
// 调试打印
|
||||
void PrintAST(const CompUnit& cu);
|
||||
// 导出 AST 为 Graphviz DOT。
|
||||
void PrintASTDot(const CompUnit& cu, std::ostream& os);
|
||||
|
||||
} // namespace ast
|
||||
@ -1,198 +0,0 @@
|
||||
// 简单 AST 调试打印,便于前端验证。
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace ast {
|
||||
|
||||
static void PrintIndent(int depth) {
|
||||
for (int i = 0; i < depth; ++i) std::cout << " ";
|
||||
}
|
||||
|
||||
static void PrintLine(int depth, const std::string& text) {
|
||||
PrintIndent(depth);
|
||||
std::cout << text << "\n";
|
||||
}
|
||||
|
||||
static void DumpExpr(const Expr* expr, int depth) {
|
||||
if (!expr) {
|
||||
PrintLine(depth, "NullExpr");
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
|
||||
PrintLine(depth, "NumberExpr value=" + std::to_string(num->value));
|
||||
return;
|
||||
}
|
||||
if (auto var = dynamic_cast<const VarExpr*>(expr)) {
|
||||
PrintLine(depth, "VarExpr name=" + var->name);
|
||||
return;
|
||||
}
|
||||
if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
|
||||
const char* op = "?";
|
||||
switch (bin->op) {
|
||||
case BinaryOp::Add:
|
||||
op = "Add";
|
||||
break;
|
||||
case BinaryOp::Sub:
|
||||
op = "Sub";
|
||||
break;
|
||||
case BinaryOp::Mul:
|
||||
op = "Mul";
|
||||
break;
|
||||
case BinaryOp::Div:
|
||||
op = "Div";
|
||||
break;
|
||||
}
|
||||
PrintLine(depth, std::string("BinaryExpr op=") + op);
|
||||
DumpExpr(bin->lhs.get(), depth + 1);
|
||||
DumpExpr(bin->rhs.get(), depth + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
PrintLine(depth, "UnknownExpr");
|
||||
}
|
||||
|
||||
void PrintAST(const CompUnit& cu) {
|
||||
PrintLine(0, "CompUnit");
|
||||
if (!cu.func) {
|
||||
PrintLine(1, "<empty>");
|
||||
std::cout << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
PrintLine(1, "FuncDef name=" + cu.func->name);
|
||||
const auto& body = cu.func->body;
|
||||
if (!body) {
|
||||
PrintLine(2, "Block <null>");
|
||||
return;
|
||||
}
|
||||
|
||||
PrintLine(2, "Block");
|
||||
for (const auto& decl : body->varDecls) {
|
||||
PrintLine(3, "VarDecl name=" + decl->name);
|
||||
if (decl->init) {
|
||||
PrintLine(4, "Init");
|
||||
DumpExpr(decl->init.get(), 5);
|
||||
}
|
||||
}
|
||||
for (const auto& stmt : body->stmts) {
|
||||
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
|
||||
PrintLine(3, "ReturnStmt");
|
||||
DumpExpr(ret->value.get(), 4);
|
||||
} else {
|
||||
PrintLine(3, "UnknownStmt");
|
||||
}
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string EscapeDotLabel(const std::string& s) {
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (char c : s) {
|
||||
if (c == '\\' || c == '"') out.push_back('\\');
|
||||
out.push_back(c);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
class DotWriter {
|
||||
public:
|
||||
explicit DotWriter(std::ostream& os) : os_(os) {}
|
||||
|
||||
int AddNode(const std::string& label) {
|
||||
int id = next_id_++;
|
||||
os_ << " n" << id << " [label=\"" << EscapeDotLabel(label) << "\"];\n";
|
||||
return id;
|
||||
}
|
||||
|
||||
void AddEdge(int from, int to) { os_ << " n" << from << " -> n" << to << ";\n"; }
|
||||
|
||||
private:
|
||||
std::ostream& os_;
|
||||
int next_id_ = 0;
|
||||
};
|
||||
|
||||
int EmitExpr(const Expr* expr, DotWriter& dot) {
|
||||
if (!expr) return dot.AddNode("NullExpr");
|
||||
|
||||
if (auto num = dynamic_cast<const NumberExpr*>(expr)) {
|
||||
return dot.AddNode("Number(" + std::to_string(num->value) + ")");
|
||||
}
|
||||
if (auto var = dynamic_cast<const VarExpr*>(expr)) {
|
||||
return dot.AddNode("Var(" + var->name + ")");
|
||||
}
|
||||
if (auto bin = dynamic_cast<const BinaryExpr*>(expr)) {
|
||||
const char* op = "?";
|
||||
switch (bin->op) {
|
||||
case BinaryOp::Add:
|
||||
op = "Add";
|
||||
break;
|
||||
case BinaryOp::Sub:
|
||||
op = "Sub";
|
||||
break;
|
||||
case BinaryOp::Mul:
|
||||
op = "Mul";
|
||||
break;
|
||||
case BinaryOp::Div:
|
||||
op = "Div";
|
||||
break;
|
||||
}
|
||||
int id = dot.AddNode(std::string("Binary(") + op + ")");
|
||||
int lhs = EmitExpr(bin->lhs.get(), dot);
|
||||
int rhs = EmitExpr(bin->rhs.get(), dot);
|
||||
dot.AddEdge(id, lhs);
|
||||
dot.AddEdge(id, rhs);
|
||||
return id;
|
||||
}
|
||||
return dot.AddNode("UnknownExpr");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void PrintASTDot(const CompUnit& cu, std::ostream& os) {
|
||||
os << "digraph AST {\n";
|
||||
os << " rankdir=TB;\n";
|
||||
os << " node [shape=box, fontname=\"Courier\"];\n";
|
||||
|
||||
DotWriter dot(os);
|
||||
int cu_id = dot.AddNode("CompUnit");
|
||||
if (cu.func) {
|
||||
int func_id = dot.AddNode("FuncDef(" + cu.func->name + ")");
|
||||
dot.AddEdge(cu_id, func_id);
|
||||
if (cu.func->body) {
|
||||
int block_id = dot.AddNode("Block");
|
||||
dot.AddEdge(func_id, block_id);
|
||||
|
||||
for (const auto& decl : cu.func->body->varDecls) {
|
||||
int decl_id = dot.AddNode("VarDecl(" + decl->name + ")");
|
||||
dot.AddEdge(block_id, decl_id);
|
||||
if (decl->init) {
|
||||
int init_id = EmitExpr(decl->init.get(), dot);
|
||||
dot.AddEdge(decl_id, init_id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& stmt : cu.func->body->stmts) {
|
||||
if (auto ret = dynamic_cast<ReturnStmt*>(stmt.get())) {
|
||||
int ret_id = dot.AddNode("Return");
|
||||
dot.AddEdge(block_id, ret_id);
|
||||
int value_id = EmitExpr(ret->value.get(), dot);
|
||||
dot.AddEdge(ret_id, value_id);
|
||||
} else {
|
||||
int stmt_id = dot.AddNode("Stmt");
|
||||
dot.AddEdge(block_id, stmt_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
os << "}\n";
|
||||
}
|
||||
|
||||
} // namespace ast
|
||||
@ -1,8 +0,0 @@
|
||||
add_library(ast STATIC
|
||||
AstNodes.cpp
|
||||
AstPrinter.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(ast PUBLIC
|
||||
build_options
|
||||
)
|
||||
@ -1,114 +0,0 @@
|
||||
// 将 parse tree 转换为 AST。
|
||||
#include "frontend/AstBuilder.h"
|
||||
|
||||
#include <any>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "SysYBaseVisitor.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ast/AstNodes.h"
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using ast::BinaryExpr;
|
||||
using ast::BinaryOp;
|
||||
using ast::Block;
|
||||
using ast::CompUnit;
|
||||
using ast::FuncDef;
|
||||
using ast::NumberExpr;
|
||||
using ast::ReturnStmt;
|
||||
using ast::VarDecl;
|
||||
using ast::VarExpr;
|
||||
|
||||
template <typename T>
|
||||
T Take(std::any&& value) {
|
||||
if (auto* ptr = std::any_cast<T>(&value)) {
|
||||
return std::move(*ptr);
|
||||
}
|
||||
throw std::runtime_error("AST 构建失败:类型不匹配");
|
||||
}
|
||||
|
||||
class Builder : public SysYBaseVisitor {
|
||||
public:
|
||||
std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override {
|
||||
auto func = Take<std::shared_ptr<FuncDef>>(visit(ctx->funcDef()));
|
||||
return std::make_shared<CompUnit>(std::move(func));
|
||||
}
|
||||
|
||||
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override {
|
||||
auto body = Take<std::shared_ptr<Block>>(visit(ctx->block()));
|
||||
return std::make_shared<FuncDef>("main", std::move(body));
|
||||
}
|
||||
|
||||
std::any visitBlock(SysYParser::BlockContext* ctx) override {
|
||||
auto block = std::make_shared<Block>();
|
||||
for (auto stmtCtx : ctx->stmt()) {
|
||||
if (stmtCtx->varDecl()) {
|
||||
block->varDecls.emplace_back(
|
||||
Take<std::shared_ptr<VarDecl>>(visit(stmtCtx->varDecl())));
|
||||
} else if (stmtCtx->returnStmt()) {
|
||||
block->stmts.emplace_back(
|
||||
Take<std::shared_ptr<ReturnStmt>>(visit(stmtCtx->returnStmt())));
|
||||
}
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override {
|
||||
std::shared_ptr<ast::Expr> init;
|
||||
if (ctx->exp()) {
|
||||
init = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
||||
}
|
||||
return std::make_shared<VarDecl>(ctx->Ident()->getText(), std::move(init));
|
||||
}
|
||||
|
||||
std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override {
|
||||
auto expr = Take<std::shared_ptr<ast::Expr>>(visit(ctx->exp()));
|
||||
return std::make_shared<ReturnStmt>(std::move(expr));
|
||||
}
|
||||
|
||||
std::any visitExp(SysYParser::ExpContext* ctx) override {
|
||||
return visit(ctx->addExp());
|
||||
}
|
||||
|
||||
std::any visitAddExp(SysYParser::AddExpContext* ctx) override {
|
||||
auto node = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(0)));
|
||||
for (size_t i = 1; i < ctx->primary().size(); ++i) {
|
||||
auto rhs = Take<std::shared_ptr<ast::Expr>>(visit(ctx->primary(i)));
|
||||
auto opToken = ctx->AddOp(i - 1);
|
||||
BinaryOp op = BinaryOp::Add;
|
||||
if (opToken->getText() == "-") op = BinaryOp::Sub;
|
||||
node = std::make_shared<BinaryExpr>(op, std::move(node), std::move(rhs));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
std::any visitPrimary(SysYParser::PrimaryContext* ctx) override {
|
||||
if (ctx->Number()) {
|
||||
std::shared_ptr<ast::Expr> expr =
|
||||
std::make_shared<NumberExpr>(std::stoi(ctx->Number()->getText()));
|
||||
return expr;
|
||||
}
|
||||
if (ctx->Ident()) {
|
||||
std::shared_ptr<ast::Expr> expr =
|
||||
std::make_shared<VarExpr>(ctx->Ident()->getText());
|
||||
return expr;
|
||||
}
|
||||
return visit(ctx->exp());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree) {
|
||||
if (!tree) {
|
||||
throw std::runtime_error("parse tree 为空");
|
||||
}
|
||||
Builder visitor;
|
||||
auto result = visitor.visit(tree);
|
||||
return Take<std::shared_ptr<ast::CompUnit>>(std::move(result));
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
// 将 ANTLR parse tree 转换为内部 AST。
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace antlr4 {
|
||||
namespace tree {
|
||||
class ParseTree;
|
||||
}
|
||||
} // namespace antlr4
|
||||
|
||||
namespace ast {
|
||||
struct CompUnit;
|
||||
}
|
||||
|
||||
std::shared_ptr<ast::CompUnit> BuildAst(antlr4::tree::ParseTree* tree);
|
||||
@ -1,37 +1,46 @@
|
||||
// 表达式翻译模块:
|
||||
// - 处理算术运算、比较、逻辑运算、函数调用等表达式
|
||||
// - 生成对应的 IR 指令并返回 SSA 值
|
||||
|
||||
#include "irgen/IRGen.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ir/IR.h"
|
||||
|
||||
ir::Value* IRGenImpl::GenExpr(const ast::Expr& expr) {
|
||||
if (auto num = dynamic_cast<const ast::NumberExpr*>(&expr)) {
|
||||
return ir::DefaultContext().GetConstInt(num->value);
|
||||
ir::Value* IRGenImpl::GenExpr(SysYParser::ExpContext& expr) {
|
||||
if (!expr.addExp()) {
|
||||
throw std::runtime_error("[irgen] 非法表达式");
|
||||
}
|
||||
if (auto var = dynamic_cast<const ast::VarExpr*>(&expr)) {
|
||||
auto it = locals_.find(var->name);
|
||||
if (it == locals_.end()) {
|
||||
throw std::runtime_error("[irgen] 变量未找到: " + var->name);
|
||||
}
|
||||
std::string name = ir::DefaultContext().NextTemp();
|
||||
return builder_.CreateLoad(it->second, name);
|
||||
return GenAddExpr(*expr.addExp());
|
||||
}
|
||||
|
||||
ir::Value* IRGenImpl::GenAddExpr(SysYParser::AddExpContext& add) {
|
||||
const auto& terms = add.primary();
|
||||
if (terms.empty()) {
|
||||
throw std::runtime_error("[irgen] 空加法表达式");
|
||||
}
|
||||
if (auto bin = dynamic_cast<const ast::BinaryExpr*>(&expr)) {
|
||||
auto* lhs = GenExpr(*bin->lhs);
|
||||
auto* rhs = GenExpr(*bin->rhs);
|
||||
|
||||
ir::Value* acc = GenPrimary(*terms[0]);
|
||||
for (size_t i = 1; i < terms.size(); ++i) {
|
||||
ir::Value* rhs = GenPrimary(*terms[i]);
|
||||
std::string name = ir::DefaultContext().NextTemp();
|
||||
if (bin->op == ast::BinaryOp::Add) {
|
||||
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
|
||||
}
|
||||
if (bin->op == ast::BinaryOp::Sub) {
|
||||
// 当前子集只需要加法,减法复用 add 但保留分支,便于扩展
|
||||
return builder_.CreateBinary(ir::Opcode::Add, lhs, rhs, name);
|
||||
acc = builder_.CreateBinary(ir::Opcode::Add, acc, rhs, name);
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
ir::Value* IRGenImpl::GenPrimary(SysYParser::PrimaryContext& primary) {
|
||||
if (primary.Number()) {
|
||||
return ir::DefaultContext().GetConstInt(std::stoi(primary.Number()->getText()));
|
||||
}
|
||||
if (primary.Ident()) {
|
||||
const std::string name = primary.Ident()->getText();
|
||||
auto it = locals_.find(name);
|
||||
if (it == locals_.end()) {
|
||||
throw std::runtime_error("[irgen] 变量未找到: " + name);
|
||||
}
|
||||
return builder_.CreateLoad(it->second, ir::DefaultContext().NextTemp());
|
||||
}
|
||||
if (primary.exp()) {
|
||||
return GenExpr(*primary.exp());
|
||||
}
|
||||
throw std::runtime_error("[irgen] 暂不支持的表达式类型");
|
||||
throw std::runtime_error("[irgen] 暂不支持的 primary 形式");
|
||||
}
|
||||
|
||||
@ -1,26 +1,28 @@
|
||||
// 函数翻译模块:
|
||||
// - 处理函数定义、参数列表与返回值翻译
|
||||
// - 创建并填充对应的 IR Function 对象
|
||||
|
||||
#include "irgen/IRGen.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ir/IR.h"
|
||||
|
||||
IRGenImpl::IRGenImpl(ir::Module& module)
|
||||
: module_(module),
|
||||
func_(module_.CreateFunction("main", ir::Type::Int32())),
|
||||
builder_(func_->entry()) {}
|
||||
: module_(module), func_(nullptr), builder_(nullptr) {}
|
||||
|
||||
void IRGenImpl::Gen(const ast::CompUnit& ast) {
|
||||
if (!ast.func || !ast.func->body) {
|
||||
throw std::runtime_error("[irgen] AST 不完整:缺少 main 定义");
|
||||
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
|
||||
if (!cu.funcDef()) {
|
||||
throw std::runtime_error("[irgen] 缺少 main 定义");
|
||||
}
|
||||
GenBlock(*ast.func->body);
|
||||
GenFuncDef(*cu.funcDef());
|
||||
}
|
||||
|
||||
std::unique_ptr<ir::Module> IRGenImpl::TakeModule() {
|
||||
return std::make_unique<ir::Module>(std::move(module_));
|
||||
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
|
||||
if (!func.block()) {
|
||||
throw std::runtime_error("[irgen] 函数体为空");
|
||||
}
|
||||
|
||||
func_ = module_.CreateFunction("main", ir::Type::Int32());
|
||||
builder_.SetInsertPoint(func_->entry());
|
||||
locals_.clear();
|
||||
|
||||
GenBlock(*func.block());
|
||||
}
|
||||
|
||||
@ -1,19 +1,26 @@
|
||||
// 语句翻译模块:
|
||||
// - 处理 if/while/return 等控制流构造
|
||||
// - 负责基本块创建、分支跳转与控制流收束
|
||||
|
||||
#include "irgen/IRGen.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ast/AstNodes.h"
|
||||
#include "SysYParser.h"
|
||||
#include "ir/IR.h"
|
||||
|
||||
void IRGenImpl::GenStmt(const ast::Stmt& stmt) {
|
||||
if (auto ret = dynamic_cast<const ast::ReturnStmt*>(&stmt)) {
|
||||
ir::Value* v = GenExpr(*ret->value);
|
||||
builder_.CreateRet(v);
|
||||
void IRGenImpl::GenStmt(SysYParser::StmtContext& stmt) {
|
||||
if (stmt.varDecl()) {
|
||||
GenVarDecl(*stmt.varDecl());
|
||||
return;
|
||||
}
|
||||
if (stmt.returnStmt()) {
|
||||
GenReturnStmt(*stmt.returnStmt());
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("[irgen] 暂不支持的语句类型");
|
||||
}
|
||||
|
||||
void IRGenImpl::GenReturnStmt(SysYParser::ReturnStmtContext& ret) {
|
||||
if (!ret.exp()) {
|
||||
throw std::runtime_error("[irgen] return 缺少表达式");
|
||||
}
|
||||
ir::Value* v = GenExpr(*ret.exp());
|
||||
builder_.CreateRet(v);
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue