|
|
|
|
@ -25,51 +25,20 @@ ir::Value* IRGenImpl::EvalExpr(SysYParser::ExpContext& expr) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法基本表达式"));
|
|
|
|
|
std::any IRGenImpl::visitParenExp(SysYParser::ParenExpContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->exp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法括号表达式"));
|
|
|
|
|
}
|
|
|
|
|
// 处理括号表达式:LPAREN exp RPAREN
|
|
|
|
|
if (ctx->exp()) {
|
|
|
|
|
return EvalExpr(*ctx->exp());
|
|
|
|
|
}
|
|
|
|
|
// 处理 lVal(变量使用)- 交给 visitLVal 处理
|
|
|
|
|
if (ctx->lVal()) {
|
|
|
|
|
// 直接在这里处理变量读取,避免 accept 调用可能导致的问题
|
|
|
|
|
auto* lval_ctx = ctx->lVal();
|
|
|
|
|
if (!lval_ctx || !lval_ctx->ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "当前仅支持普通整型变量"));
|
|
|
|
|
}
|
|
|
|
|
const auto* decl = sema_.ResolveObjectUse(lval_ctx);
|
|
|
|
|
if (!decl) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen",
|
|
|
|
|
"变量使用缺少语义绑定:" + lval_ctx->ID()->getText()));
|
|
|
|
|
}
|
|
|
|
|
std::string var_name = lval_ctx->ID()->getText();
|
|
|
|
|
auto it = storage_map_.find(var_name);
|
|
|
|
|
if (it == storage_map_.end()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen",
|
|
|
|
|
"变量声明缺少存储槽位:" + var_name));
|
|
|
|
|
}
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateLoad(it->second, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
// 处理 number
|
|
|
|
|
if (ctx->number()) {
|
|
|
|
|
return ctx->number()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "不支持的基本表达式类型"));
|
|
|
|
|
return EvalExpr(*ctx->exp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitNumber(SysYParser::NumberContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->intConst()) {
|
|
|
|
|
std::any IRGenImpl::visitNumberExp(SysYParser::NumberExpContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->number() || !ctx->number()->ILITERAL()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "当前仅支持整数字面量"));
|
|
|
|
|
}
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateConstInt(std::stoi(ctx->intConst()->getText())));
|
|
|
|
|
builder_.CreateConstInt(std::stoi(ctx->number()->getText())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 变量使用的处理流程:
|
|
|
|
|
@ -78,252 +47,34 @@ std::any IRGenImpl::visitNumber(SysYParser::NumberContext* ctx) {
|
|
|
|
|
// 3. 最后生成 load,把内存中的值读出来。
|
|
|
|
|
//
|
|
|
|
|
// 因此当前 IRGen 自己不再做名字查找,而是直接消费 Sema 的绑定结果。
|
|
|
|
|
std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->ID()) {
|
|
|
|
|
std::any IRGenImpl::visitVarExp(SysYParser::VarExpContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->var() || !ctx->var()->ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "当前仅支持普通整型变量"));
|
|
|
|
|
}
|
|
|
|
|
const auto* decl = sema_.ResolveObjectUse(ctx);
|
|
|
|
|
auto* decl = sema_.ResolveVarUse(ctx->var());
|
|
|
|
|
if (!decl) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen",
|
|
|
|
|
"变量使用缺少语义绑定:" + ctx->ID()->getText()));
|
|
|
|
|
"变量使用缺少语义绑定: " + ctx->var()->ID()->getText()));
|
|
|
|
|
}
|
|
|
|
|
// 使用变量名查找存储槽位
|
|
|
|
|
std::string var_name = ctx->ID()->getText();
|
|
|
|
|
auto it = storage_map_.find(var_name);
|
|
|
|
|
auto it = storage_map_.find(decl);
|
|
|
|
|
if (it == storage_map_.end()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen",
|
|
|
|
|
"变量声明缺少存储槽位:" + var_name));
|
|
|
|
|
"变量声明缺少存储槽位: " + ctx->var()->ID()->getText()));
|
|
|
|
|
}
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateLoad(it->second, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitAddExp(SysYParser::AddExpContext* ctx) {
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法加减法表达式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是 mulExp 直接返回(addExp : mulExp)
|
|
|
|
|
if (ctx->mulExp() && ctx->addExp() == nullptr) {
|
|
|
|
|
return ctx->mulExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理 addExp op mulExp 的递归形式
|
|
|
|
|
if (!ctx->addExp() || !ctx->mulExp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法加减法表达式结构"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->addExp()->accept(this));
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->mulExp()->accept(this));
|
|
|
|
|
|
|
|
|
|
ir::Opcode op = ir::Opcode::Add;
|
|
|
|
|
if (ctx->ADD()) {
|
|
|
|
|
op = ir::Opcode::Add;
|
|
|
|
|
} else if (ctx->SUB()) {
|
|
|
|
|
op = ir::Opcode::Sub;
|
|
|
|
|
} else {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的加减运算符"));
|
|
|
|
|
std::any IRGenImpl::visitAdditiveExp(SysYParser::AdditiveExpContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->exp(0) || !ctx->exp(1)) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法加法表达式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir::Value* lhs = EvalExpr(*ctx->exp(0));
|
|
|
|
|
ir::Value* rhs = EvalExpr(*ctx->exp(1));
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateBinary(op, lhs, rhs, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) {
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法一元表达式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是 primaryExp 直接返回(unaryExp : primaryExp)
|
|
|
|
|
if (ctx->primaryExp()) {
|
|
|
|
|
return ctx->primaryExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理函数调用(unaryExp : ID LPAREN funcRParams? RPAREN)
|
|
|
|
|
// 当前暂不支持,留给后续扩展
|
|
|
|
|
if (ctx->ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "暂不支持函数调用"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理一元运算符(unaryExp : addUnaryOp unaryExp)
|
|
|
|
|
if (ctx->addUnaryOp() && ctx->unaryExp()) {
|
|
|
|
|
ir::Value* operand = std::any_cast<ir::Value*>(ctx->unaryExp()->accept(this));
|
|
|
|
|
|
|
|
|
|
// 判断是正号还是负号
|
|
|
|
|
if (ctx->addUnaryOp()->SUB()) {
|
|
|
|
|
// 负号:生成 sub 0, operand(LLVM IR 中没有 neg 指令)
|
|
|
|
|
ir::Value* zero = builder_.CreateConstInt(0);
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateSub(zero, operand, module_.GetContext().NextTemp()));
|
|
|
|
|
} else if (ctx->addUnaryOp()->ADD()) {
|
|
|
|
|
// 正号:直接返回操作数(+x 等价于 x)
|
|
|
|
|
return operand;
|
|
|
|
|
} else {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的一元运算符"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "不支持的一元表达式类型"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitMulExp(SysYParser::MulExpContext* ctx) {
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法乘除法表达式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是 unaryExp 直接返回(mulExp : unaryExp)
|
|
|
|
|
if (ctx->unaryExp() && ctx->mulExp() == nullptr) {
|
|
|
|
|
return ctx->unaryExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理 mulExp op unaryExp 的递归形式
|
|
|
|
|
if (!ctx->mulExp() || !ctx->unaryExp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法乘除法表达式结构"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->mulExp()->accept(this));
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->unaryExp()->accept(this));
|
|
|
|
|
|
|
|
|
|
ir::Opcode op = ir::Opcode::Mul;
|
|
|
|
|
if (ctx->MUL()) {
|
|
|
|
|
op = ir::Opcode::Mul;
|
|
|
|
|
} else if (ctx->DIV()) {
|
|
|
|
|
op = ir::Opcode::Div;
|
|
|
|
|
} else if (ctx->MOD()) {
|
|
|
|
|
op = ir::Opcode::Mod;
|
|
|
|
|
} else {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的乘除运算符"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return static_cast<ir::Value*>(
|
|
|
|
|
builder_.CreateBinary(op, lhs, rhs, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitRelExp(SysYParser::RelExpContext* ctx) {
|
|
|
|
|
if (ctx->addExp() && ctx->relExp() == nullptr) {
|
|
|
|
|
return ctx->addExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->relExp()->accept(this));
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->addExp()->accept(this));
|
|
|
|
|
if (lhs->GetType()->IsInt1()) lhs = builder_.CreateZext(lhs, module_.GetContext().NextTemp());
|
|
|
|
|
if (rhs->GetType()->IsInt1()) rhs = builder_.CreateZext(rhs, module_.GetContext().NextTemp());
|
|
|
|
|
|
|
|
|
|
ir::CmpOp op;
|
|
|
|
|
if (ctx->LT()) op = ir::CmpOp::Lt;
|
|
|
|
|
else if (ctx->GT()) op = ir::CmpOp::Gt;
|
|
|
|
|
else if (ctx->LE()) op = ir::CmpOp::Le;
|
|
|
|
|
else if (ctx->GE()) op = ir::CmpOp::Ge;
|
|
|
|
|
else throw std::runtime_error(FormatError("irgen", "未知的关系运算符"));
|
|
|
|
|
|
|
|
|
|
return static_cast<ir::Value*>(builder_.CreateCmp(op, lhs, rhs, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitEqExp(SysYParser::EqExpContext* ctx) {
|
|
|
|
|
if (ctx->relExp() && ctx->eqExp() == nullptr) {
|
|
|
|
|
return ctx->relExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->eqExp()->accept(this));
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->relExp()->accept(this));
|
|
|
|
|
if (lhs->GetType()->IsInt1()) lhs = builder_.CreateZext(lhs, module_.GetContext().NextTemp());
|
|
|
|
|
if (rhs->GetType()->IsInt1()) rhs = builder_.CreateZext(rhs, module_.GetContext().NextTemp());
|
|
|
|
|
|
|
|
|
|
ir::CmpOp op;
|
|
|
|
|
if (ctx->EQ()) op = ir::CmpOp::Eq;
|
|
|
|
|
else if (ctx->NE()) op = ir::CmpOp::Ne;
|
|
|
|
|
else throw std::runtime_error(FormatError("irgen", "未知的相等运算符"));
|
|
|
|
|
|
|
|
|
|
return static_cast<ir::Value*>(builder_.CreateCmp(op, lhs, rhs, module_.GetContext().NextTemp()));
|
|
|
|
|
builder_.CreateBinary(ir::Opcode::Add, lhs, rhs,
|
|
|
|
|
module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitCondUnaryExp(SysYParser::CondUnaryExpContext* ctx) {
|
|
|
|
|
if (ctx->eqExp()) {
|
|
|
|
|
return ctx->eqExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
if (ctx->NOT()) {
|
|
|
|
|
ir::Value* operand = std::any_cast<ir::Value*>(ctx->condUnaryExp()->accept(this));
|
|
|
|
|
if (operand->GetType()->IsInt1()) {
|
|
|
|
|
operand = builder_.CreateZext(operand, module_.GetContext().NextTemp());
|
|
|
|
|
}
|
|
|
|
|
ir::Value* zero = builder_.CreateConstInt(0);
|
|
|
|
|
return static_cast<ir::Value*>(builder_.CreateCmp(ir::CmpOp::Eq, operand, zero, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法条件一元表达式"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitLAndExp(SysYParser::LAndExpContext* ctx) {
|
|
|
|
|
if (ctx->condUnaryExp() && ctx->lAndExp() == nullptr) {
|
|
|
|
|
return ctx->condUnaryExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir::AllocaInst* res_ptr = builder_.CreateAllocaI32(module_.GetContext().NextTemp());
|
|
|
|
|
ir::Value* zero = builder_.CreateConstInt(0);
|
|
|
|
|
builder_.CreateStore(zero, res_ptr);
|
|
|
|
|
|
|
|
|
|
ir::BasicBlock* rhs_bb = func_->CreateBlock(NextBlockName("land_rhs"));
|
|
|
|
|
ir::BasicBlock* end_bb = func_->CreateBlock(NextBlockName("land_end"));
|
|
|
|
|
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->lAndExp()->accept(this));
|
|
|
|
|
if (lhs->GetType()->IsInt1()) {
|
|
|
|
|
lhs = builder_.CreateZext(lhs, module_.GetContext().NextTemp());
|
|
|
|
|
}
|
|
|
|
|
ir::Value* lhs_cond = builder_.CreateCmp(ir::CmpOp::Ne, lhs, zero, module_.GetContext().NextTemp());
|
|
|
|
|
builder_.CreateCondBr(lhs_cond, rhs_bb, end_bb);
|
|
|
|
|
|
|
|
|
|
builder_.SetInsertPoint(rhs_bb);
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->condUnaryExp()->accept(this));
|
|
|
|
|
if (rhs->GetType()->IsInt1()) {
|
|
|
|
|
rhs = builder_.CreateZext(rhs, module_.GetContext().NextTemp());
|
|
|
|
|
}
|
|
|
|
|
ir::Value* rhs_cond = builder_.CreateCmp(ir::CmpOp::Ne, rhs, zero, module_.GetContext().NextTemp());
|
|
|
|
|
ir::Value* rhs_res = builder_.CreateZext(rhs_cond, module_.GetContext().NextTemp());
|
|
|
|
|
builder_.CreateStore(rhs_res, res_ptr);
|
|
|
|
|
builder_.CreateBr(end_bb);
|
|
|
|
|
|
|
|
|
|
builder_.SetInsertPoint(end_bb);
|
|
|
|
|
return static_cast<ir::Value*>(builder_.CreateLoad(res_ptr, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitLOrExp(SysYParser::LOrExpContext* ctx) {
|
|
|
|
|
if (ctx->lAndExp() && ctx->lOrExp() == nullptr) {
|
|
|
|
|
return ctx->lAndExp()->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir::AllocaInst* res_ptr = builder_.CreateAllocaI32(module_.GetContext().NextTemp());
|
|
|
|
|
ir::Value* one = builder_.CreateConstInt(1);
|
|
|
|
|
builder_.CreateStore(one, res_ptr);
|
|
|
|
|
|
|
|
|
|
ir::BasicBlock* rhs_bb = func_->CreateBlock(NextBlockName("lor_rhs"));
|
|
|
|
|
ir::BasicBlock* end_bb = func_->CreateBlock(NextBlockName("lor_end"));
|
|
|
|
|
|
|
|
|
|
ir::Value* lhs = std::any_cast<ir::Value*>(ctx->lOrExp()->accept(this));
|
|
|
|
|
ir::Value* zero = builder_.CreateConstInt(0);
|
|
|
|
|
if (lhs->GetType()->IsInt1()) {
|
|
|
|
|
lhs = builder_.CreateZext(lhs, module_.GetContext().NextTemp());
|
|
|
|
|
}
|
|
|
|
|
ir::Value* lhs_cond = builder_.CreateCmp(ir::CmpOp::Eq, lhs, zero, module_.GetContext().NextTemp());
|
|
|
|
|
builder_.CreateCondBr(lhs_cond, rhs_bb, end_bb);
|
|
|
|
|
|
|
|
|
|
builder_.SetInsertPoint(rhs_bb);
|
|
|
|
|
ir::Value* rhs = std::any_cast<ir::Value*>(ctx->lAndExp()->accept(this));
|
|
|
|
|
if (rhs->GetType()->IsInt1()) {
|
|
|
|
|
rhs = builder_.CreateZext(rhs, module_.GetContext().NextTemp());
|
|
|
|
|
}
|
|
|
|
|
ir::Value* rhs_cond = builder_.CreateCmp(ir::CmpOp::Ne, rhs, zero, module_.GetContext().NextTemp());
|
|
|
|
|
ir::Value* rhs_res = builder_.CreateZext(rhs_cond, module_.GetContext().NextTemp());
|
|
|
|
|
builder_.CreateStore(rhs_res, res_ptr);
|
|
|
|
|
builder_.CreateBr(end_bb);
|
|
|
|
|
|
|
|
|
|
builder_.SetInsertPoint(end_bb);
|
|
|
|
|
return static_cast<ir::Value*>(builder_.CreateLoad(res_ptr, module_.GetContext().NextTemp()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitCond(SysYParser::CondContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->lOrExp()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法条件表达式"));
|
|
|
|
|
}
|
|
|
|
|
return ctx->lOrExp()->accept(this);
|
|
|
|
|
}
|