LLVM IR getelementptr无效索引

LLVM IR getelementptr无效索引,llvm,llvm-ir,Llvm,Llvm Ir,我目前正在学习如何使用LLVM。我正试图通过llc struct-method.ll-o struct-method编译以下文件 struct method.ll ; ModuleID = 'struct-method.ll' @.formatstring = private unnamed_addr constant [13 x i8] c"%c\0A%ld\0A%lld\0A\00" %box = type { i8, i32, i64 } declare i32 @printf(i8

我目前正在学习如何使用LLVM。我正试图通过
llc struct-method.ll-o struct-method
编译以下文件

struct method.ll

; ModuleID = 'struct-method.ll'

@.formatstring = private unnamed_addr constant [13 x i8] c"%c\0A%ld\0A%lld\0A\00"

%box = type { i8, i32, i64 }

declare i32 @printf(i8* noalias nocapture, ...)

define i32 @set_prop_32(%box* %object, i32 %value) {
entry:
  %0 = getelementptr inbounds %box, %box* %object, i64 0, i64 1
  %1 = load i32, i32* %0
  ret i32 %1
}

define i32 @main() {
alloca:
  %mybox = alloca %box
  br label %entry

entry:
  %format = getelementptr [13 x i8], [13 x i8]* @.formatstring, i64 0, i64 0
  %0 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 0
  %1 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 1
  %2 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 2

  store i8  65,       i8*  %0
  store i32 200,      i32* %1
  store i64 9999999,  i64* %2

  %f8 = load i8, i8* %0
  %f32 = load i32, i32* %1
  %f64 = load i64, i64* %2

  call i32 (i8*, ...) @printf(i8* %format, i8 %f8, i32 %f32, i64 %f64)

  call i32 (%box*, i32) @set_prop_32(%box* %mybox, i32 300)

  call i32 (i8*, ...) @printf(i8* %format, i8 %f8, i32 %f32, i64 %f64)

  ret i32 0
}
然而,我在第11行得到了无效的getelementptr索引

有人知道为什么会这样吗?我会写些什么来解决这个问题

编辑: 2013年末,我在Macbook Pro上使用macOS Sierra 10.12。

根据

每个索引参数的类型取决于它要索引到的类型。当索引到(可选压缩)结构中时,只允许使用i32整数常量(当使用索引向量时,它们必须都是相同的i32整数常量)。当索引到数组、指针或向量时,允许使用任意宽度的整数,且不要求它们为常量。这些整数在相关情况下被视为有符号值“

在您的案例中,类型{i8,i32,i64}是结构类型,所以请尝试使用i32类型索引

而不是

%0 = getelementptr inbounds %box, %box* %object, i64 0, i64 1
试一试

依照

每个索引参数的类型取决于它要索引到的类型。当索引到(可选压缩)结构中时,只允许使用i32整数常量(当使用索引向量时,它们必须都是相同的i32整数常量)。当索引到数组、指针或向量时,允许使用任意宽度的整数,且不要求它们为常量。这些整数在相关情况下被视为有符号值“

在您的案例中,类型{i8,i32,i64}是结构类型,所以请尝试使用i32类型索引

而不是

%0 = getelementptr inbounds %box, %box* %object, i64 0, i64 1
试一试


谢谢成功了。但是为什么它在入口块的前4条指令中起作用呢?顺便说一句:这是我试图实现的最终版本:例如,这个文件编译得很好:gep的第一个操作数不是struct,而是struct ptr,所以第一个索引不是索引struct元素,而是结构本身,所以它可以有任何类型的整数索引作为第一个索引。(或者如果它包含ptr,那么它也可以)所以第一个索引将获得(&box[i64 0 index])值。更多信息请参见,谢谢!成功了。但是为什么它在入口块的前4条指令中起作用呢?顺便说一句:这是我试图实现的最终版本:例如,这个文件编译得很好:gep的第一个操作数不是struct,而是struct ptr,所以第一个索引不是索引struct元素,而是结构本身,所以它可以有任何类型的整数索引作为第一个索引。(或者如果它包含ptr,那么它也可以)所以第一个索引将获得(&box[i64 0 index])值。有关更多信息,请参阅