diff --git a/src/SysYIRGenerator.cpp b/src/SysYIRGenerator.cpp index ba25144..eb21a1a 100644 --- a/src/SysYIRGenerator.cpp +++ b/src/SysYIRGenerator.cpp @@ -146,7 +146,11 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c delete root; } // 创建全局变量,并更新符号表 - module->createGlobalValue(name, Type::getPointerType(type), dims, values); + Type* variableType = type; + if (!dims.empty()) { // 如果有维度,说明是数组 + variableType = buildArrayType(type, dims); // 构建完整的 ArrayType + } + module->createGlobalValue(name, Type::getPointerType(variableType), dims, values); } return std::any(); } @@ -193,7 +197,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) { // 对于数组,alloca 的类型将是指针指向数组类型,例如 `int[2][3]*` // 对于标量,alloca 的类型将是指针指向标量类型,例如 `int*` AllocaInst* alloca = - builder.createAllocaInst(Type::getPointerType(type), dims, name); + builder.createAllocaInst(Type::getPointerType(variableType), dims, name); if (varDef->initVal() != nullptr) { ValueCounter values; @@ -1232,6 +1236,7 @@ auto SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext *ctx) -> std::any { return std::any(); } +// attention : 这里的type是数组元素的type void Utils::tree2Array(Type *type, ArrayValueTree *root, const std::vector &dims, unsigned numDims, ValueCounter &result, IRBuilder *builder) {