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];