From 8dd9db7a74334f13818cc026937191d29cee70a1 Mon Sep 17 00:00:00 2001 From: mayiyang <1> Date: Mon, 20 Apr 2026 15:16:49 +0800 Subject: [PATCH] =?UTF-8?q?lab2=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ir/IRBuilder.cpp | 5 +++++ src/irgen/IRGenExp.cpp | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ir/IRBuilder.cpp b/src/ir/IRBuilder.cpp index 4987681..f7f2c30 100644 --- a/src/ir/IRBuilder.cpp +++ b/src/ir/IRBuilder.cpp @@ -218,6 +218,11 @@ static std::shared_ptr ResolveGepResultType(const std::shared_ptr& b } auto cur = base_ptr_ty->GetElementType(); for (size_t i = 0; i < index_count; ++i) { + // LLVM GEP 的第一个索引只是在当前 pointee 对象上做寻址, + // 不会立刻深入到数组元素类型;真正进入聚合类型从第二个索引开始。 + if (i == 0) { + continue; + } if (cur->IsArray()) { cur = cur->GetElementType(); continue; diff --git a/src/irgen/IRGenExp.cpp b/src/irgen/IRGenExp.cpp index 9a67446..27f2498 100644 --- a/src/irgen/IRGenExp.cpp +++ b/src/irgen/IRGenExp.cpp @@ -137,7 +137,8 @@ std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) { auto* arg_elem = arg->GetType()->GetElementType().get(); if (param_elem && arg_elem && arg_elem->IsArray() && arg_elem->GetElementType()->Equals(*param_elem)) { - std::vector idx = {builder_.CreateConstInt(0)}; + std::vector idx = {builder_.CreateConstInt(0), + builder_.CreateConstInt(0)}; args[i] = builder_.CreateGep(arg, std::move(idx), module_.GetContext().NextTemp()); arg = args[i];