简体   繁体   中英

How to generate executable from LLVM IR

I was reading http://www.stephendiehl.com/llvm/#llvm-introduction there is a piece of LLVM IR like this:

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
}

I wanted to try running this by using llvm and nasm but failed:

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

first lines of the errors are:

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

the code generated from llc does not seem to be native OS X Assembly code described at http://peter.michaux.ca/articles/assembly-hello-world-for-os-x

What's the right commands to generate an executable?

nasm does not support AT&T assembly syntax hence the error. On OS X you need to use "as" to assemble (and forget about nasm in 99% cases except when explicitly requested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM