@ -586,7 +586,18 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
// 显式地为局部常量在栈上分配空间
// alloca 的类型将是指针指向常量类型,例如 `int*` 或 `int[2][3]*`
// 将 alloca 全部集中到entry函数中
// 记录当前位置
BasicBlock * curBB = builder . getBasicBlock ( ) ;
auto curPos = builder . getPosition ( ) ;
Function * currentFunction = builder . getBasicBlock ( ) - > getParent ( ) ;
BasicBlock * entryBB = currentFunction - > getEntryBlock ( ) ;
// 在terminator前插入
auto entryPos = entryBB - > terminator ( ) ;
builder . setPosition ( entryBB , entryPos ) ;
AllocaInst * alloca = builder . createAllocaInst ( Type : : getPointerType ( variableType ) , name ) ;
// 恢复当前位置
builder . setPosition ( curBB , curPos ) ;
ArrayValueTree * root = std : : any_cast < ArrayValueTree * > ( constDef - > constInitVal ( ) - > accept ( this ) ) ;
ValueCounter values ;
@ -743,8 +754,20 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
// 对于数组, alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
// 对于标量, alloca 的类型将是指针指向标量类型,例如 `int*`
AllocaInst * alloca =
builder . createAllocaInst ( Type : : getPointerType ( variableType ) , name ) ;
BasicBlock * curBB = builder . getBasicBlock ( ) ;
auto curPos = builder . getPosition ( ) ;
Function * currentFunction = builder . getBasicBlock ( ) - > getParent ( ) ;
BasicBlock * entryBB = currentFunction - > getEntryBlock ( ) ;
// 在terminator前插入
auto entryPos = entryBB - > terminator ( ) ;
builder . setPosition ( entryBB , entryPos ) ;
AllocaInst * alloca = builder . createAllocaInst ( Type : : getPointerType ( variableType ) , name ) ;
// 恢复当前位置
builder . setPosition ( curBB , curPos ) ;
// AllocaInst* alloca =
// builder.createAllocaInst(Type::getPointerType(variableType), name);
if ( varDef - > initVal ( ) ! = nullptr ) {
ValueCounter values ;