如何从LLVM IR生成可执行文件

时间:2015-03-21 08:31:52

标签: macos assembly llvm

我正在阅读http://www.stephendiehl.com/llvm/#llvm-introduction有一条像这样的LLVM IR:

declare i32 @putchar(i32)

define i32 @add(i32 %a, i32 %b) {
  %1 = add i32 %a, %b
  ret i32 %1
}

define void @main() {
  %1 = call i32 @add(i32 0, i32 97)
  call i32 @putchar(i32 %1)
  ret void
}

我想尝试使用llvmnasm来运行此功能,但失败了:

llc -march=x86-64 h1.bc -o h1.s
nasm -f macho -o h1.o h1.s
# failed here

错误的第一行是:

h1.s:1: error: attempt to define a local label before any non-local labels
h1.s:1: error: parser: instruction expected
h1.s:2: error: attempt to define a local label before any non-local labels
h1.s:2: error: parser: instruction expected
h1.s:3: error: attempt to define a local label before any non-local labels
h1.s:3: error: parser: instruction expected
h1.s:4: error: attempt to define a local label before any non-local labels

llc生成的代码似乎不是http://peter.michaux.ca/articles/assembly-hello-world-for-os-x中描述的本机OS X汇编代码

生成可执行文件的正确命令是什么?

1 个答案:

答案 0 :(得分:2)

nasm不支持AT& T汇编语法,因此错误。在OS X上,您需要使用“as”进行汇编(并且在99%的情况下忘记nasm,除非明确要求)