From c68b031c01d4b08ed5e962ea1b04a23fc730dd5f Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Thu, 24 Jul 2025 15:22:38 +0800 Subject: [PATCH] =?UTF-8?q?[midend]=E4=BF=AE=E5=A4=8D=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=B1=BB=E5=9E=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SysYIRGenerator.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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) {