如何使用llvm生成机器代码 [英] How to generate machine code with llvm

查看:212
本文介绍了如何使用llvm生成机器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用llvm进行编译器项目.我已经按照各种教程进行了讲解,以至于我有一个解析器来创建语法树,然后使用提供的IRBuilder将树转换为llvm模块.

I'm currently working on a compiler project using llvm. I have followed various tutorials to the point where I have a parser to create a syntax tree and then the tree is converted into an llvm Module using the provided IRBuilder.

我的目标是创建一个可执行文件,我对下一步的工作感到困惑.我发现的所有教程都只是创建llvm模块,并使用Module.dump()打印出程序集.另外,我只能找到针对llvm开发人员的文档,而不是该项目的最终用户的文档.

My goal is to create an executable, and I am confused as what to do next. All the tutorials I've found just create the llvm module and print out the assembly using Module.dump(). Additionally, the only documentation I can find is for llvm developers, and not end users of the project.

如果我想生成机器代码,下一步是什么? llvm-mc项目看起来像它可以完成我想要的,但是我找不到关于它的任何文档.

If I want to generate machine code, what are the next steps? The llvm-mc project looks like it may do what I want, but I can't find any sort of documentation on it.

也许我期望llvm做一些它不做的事情.我的期望是,我可以构建一个模块,然后会有一个可以与该模块一起调用的API,然后将创建一个目标三元组并生成一个目标文件.我发现了有关生成JIT的文档和示例,对此我不感兴趣.我正在寻找如何生成已编译的二进制文件.

Perhaps I'm expecting llvm to do something that it doesn't. My expectation is that I can build a Module, then there would be an API that I can call with the Module and a target triple and an object file will be produced. I have found documentation and examples on producing a JIT, and I am not interested in that. I am looking for how to produce compiled binaries.

我正在使用OS X,如果有影响的话.

I am working on OS X, if that has any impact.

推荐答案

使用llc -filetype=obj从IR发出可链接的目标文件.您可以查看llc的代码,以查看它发出的LLVM API调用以发出此类代码.至少对于Mac OS X和Linux,以这种方式发出的对象应该是相当不错的(即,到目前为止,这还不是"alpha quality"选项).

Use llc -filetype=obj to emit a linkable object file from your IR. You can look at the code of llc to see the LLVM API calls it makes to emit such code. At least for Mac OS X and Linux, the objects emitted in such a manner should be pretty good (i.e. this is not a "alpha quality" option by now).

LLVM不包含链接器(尚未!).因此,要将这个目标文件实际链接到某些可执行文件或共享库中,您将需要使用系统链接器.请注意,即使您有一个包含单个目标文件的可执行文件,也无论如何都必须链接后者. LLVM社区中的开发人员正在开发一个名为lld的LLVM真实链接器.您可以访问其页面或搜索邮件列表档案以跟踪其进度.

LLVM does not contain a linker (yet!), however. So to actually link this object file into some executable or shared library, you will need to use the system linker. Note that even if you have an executable consisting of a single object file, the latter has to be linked anyway. Developers in the LLVM community are working on a real linker for LLVM, called lld. You can visit its page or search the mailing list archives to follow its progress.

这篇关于如何使用llvm生成机器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆