LLVM IR getelementptr 无效索引

LLVM IR getelementptr invalid indices

我目前正处于学习如何使用 LLVM 的阶段。我正在尝试通过 llc struct-method.ll -o struct-method.

编译以下文件

结构-method.ll

; ModuleID = 'struct-method.ll'

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

%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 行得到 invalid getelementptr indices

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

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

根据http://llvm.org/docs/LangRef.html#getelementptr-instruction

"The type of each index argument depends on the type it is indexing into. When indexing into a (optionally packed) structure, only i32 integer constants are allowed (when using a vector of indices they must all be the same i32 integer constant). When indexing into an array, pointer or vector, integers of any width are allowed, and they are not required to be constant. These integers are treated as signed values where relevant"

在你的例子中类型 { i8, i32, i64 } 是结构类型所以尝试使用 i32 类型索引。

而不是

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

尝试

%0 = getelementptr inbounds %box, %box* %object, i32 0, i32 1