简体   繁体   English

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

[英]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: 我在阅读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
}

I wanted to try running this by using llvm and nasm but failed: 我想尝试使用llvmnasm来运行它,但是失败了:

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 llc生成的代码似乎不是本机的OS X汇编代码,请参见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. nasm不支持AT&T汇编语法,因此会出现错误。 On OS X you need to use "as" to assemble (and forget about nasm in 99% cases except when explicitly requested) 在OS X上,您需要使用“ as”进行组装(除非明确要求,否则在99%的情况下请忽略nasm)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉Xcode为Swift发出IR(LLVM Bitcode)? - How to tell Xcode to emit IR (LLVM Bitcode) for Swift? 如何从OSX命令行获取gcc(或llvm)以生成ARM代码? - how do you get gcc (or llvm) to generate ARM code from the OSX command-line? 如何从内存启动可执行文件? - How to launch an executable from memory? 如何从 Mac 上的另一个可执行文件/Python 脚本调用可执行文件? - How to make a call to an executable from another executable/Python script on Mac? 将* .hs转换为llvm ir(* .ll)-—错误:ghc:无法执行:opt-在macOS Sierra上 - Converting *.hs to llvm ir (*.ll) -— Error: ghc: could not execute: opt — on macOS Sierra 如何从Flash AIR 4导出MAC可执行文件 - how to export MAC executable from Flash AIR 4 如何从可执行文件更改Mac应用程序名称? - How to change mac app name from executable? Mac:如何从可执行文件中导出符号? - Mac: How to export symbols from an executable? 如何使用Cocoa访问IR(红外接收器)? - How to access the IR (Infrared receiver) with Cocoa? 如何在可执行文件附近生成 dsym 文件,而不是在包之外,最好没有安装后脚本? - How to generate dsym files near the executable, not outside the bundle, preferably without a post install script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM