[llvm] Call the LLVM Jit from c program
stackoverflow: http://stackoverflow.com/questions/1838304/call-the-llvm-jit-from-c-program
Another trial under llvm 3.2;
In prepared IR "tst.ll", code:
; ModuleID = 'tst.bc'
define i32 @add1(i32 %AnArg) {
EntryBlock:
%0 = add i32 1, %AnArg
ret i32 %0
}
define i32 @foo() {
EntryBlock:
%0 = tail call i32 @add1(i32 10)
ret i32 %0
}
run " llvm-as tst.ll " to create the asm file tst.bc
then the caller programe:
#include <string>
#include <memory>
#include <iostream> #include <llvm/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Module.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/Support/system_error.h> using namespace std;
using namespace llvm; int main()
{
InitializeNativeTarget();
llvm_start_multithreaded();
LLVMContext context;
string error; OwningPtr<MemoryBuffer> file_buffer;
MemoryBuffer::getFile("tst.bc", file_buffer);
Module *m = ParseBitcodeFile(file_buffer.get(), context, &error); ExecutionEngine *ee = ExecutionEngine::create(m); Function* func = ee->FindFunctionNamed("foo"); typedef int (*PFN)();
PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
int x = pfn();
cout << "~~~ " << x << "\n";
delete ee; }
In the above example, IR codes are load from a file. In more dymanic usage, we could construct the IR code as a string according to input "query" type. Then compile the IR to bitcode on the fly.
[llvm] Call the LLVM Jit from c program的更多相关文章
- [转]LLVM MC Project
Intro to the LLVM MC Project The LLVM Machine Code (aka MC) sub-project of LLVM was created to solve ...
- LLVM的总结
LLVM 写在前面的话:无意中看到的LLVM的作者Chris Lattner相关的介绍和故事,觉得很有意思就贴上来,如果不感兴趣,可以直接跳入下一章. 关于LLVM 如果你对LLVM的由来陌生,那么我 ...
- LLVM 编码规范 - 中文翻译
LLVM 编码规范 导论 语言.库和标准 C++ 标准版本 C++ 标准库 Go 代码准则 机械的代码问题 代码格式化 注释 头文件 类概述 method information 注释格式化 使用Do ...
- LLVM编译器架构
LLVM编译器架构 LLVM概述 LLVM项目是模块化和可重用的编译器及工具链技术的集合.尽管名称如此,LLVM与传统虚拟机关系不大.名称" LLVM"本身不是缩写.它是项目的全名 ...
- ubuntu下载源码clang + llvm+lldb 编译+安装
[本文可能涉及到Ubuntu安装以下工具:] A.g++ B.gcc C.make D.cmake E.clang(10.0.1)(必须) F.llvm(10.0.1)(必须) G.lldb(10.0 ...
- 认识 LLVM
简介 LLVM是一套提供编译器基础设施的开源项目,是用 C++ 编写,包含一系列模块化的编译器组件和工具链,用来开发编译器前端和后端.它是为了任意一种编程语言而写成的程序,利用虚拟技术创造出编译时期. ...
- LLVM 初探<一>
一.安装LLVM LLVM是一个低级虚拟机,全称为Low Level Virtual Machine.LLVM也是一个新型的编译器框架,相关的介绍Wikipedia. 现在LLVM的版本已经有很多,根 ...
- llvm學習(二)————llvm編譯與環境構建
本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17339027) 在2011十月份的时候, ...
- LLVM和clang
LLVM编译器架构 LLVM项目是一套工具的集合,它包括模块化.可复用的编译器及一些列工具链技术. LLVM最开始是Low Level Virtual Machine的简称,但现在它并不是传统意义上的 ...
随机推荐
- AngularJs的UI组件ui-Bootstrap分享(十二)——Rating
Rating是一个用于打分或排名的控件.看一个最简单的例子: <!DOCTYPE html> <html ng-app="ui.bootstrap.demo" x ...
- ASP.NET后台调用前台JS函数的三种常见方法
第一种:使用普通的添加控件中的Attributes属性进行调用 例如,像一般的普通的按钮:Button1.Attributes.Add("onclick","MyFun( ...
- DevExpress gridControl 设置分组
有些代码非常有用,但是用的时候就记不清怎么写,所以就在这里打个草稿. //设置组汇总 private void SetSummation() { this.gridViewShipment.Group ...
- DotSpatial 创建面状要素——含空洞
private void toolStripButton23_Click(object sender, EventArgs e) { //选择图层 FeatureSet fs = null; fs = ...
- PyAutoGUI-python版的autoit/AHK
简单介绍各个图形界面自动操作的python库,类似按键精灵\autoit\ahk(autohotkey)等等这些自动化工具.这类python库不是只是用来实现自动游戏之类的程序,业界也用这些库来做GU ...
- ASP.NET 中HTML和Form辅助方法
Form辅助方法 Form最重要的属性就是action和method,action指明form中的数据被提交到哪里,method指明用什么方法,默认为GET,下面是一个简单的例子: <form ...
- qt做触摸屏演示程序
界面效果图: 参考资料: http://blog.csdn.net/orz415678659/article/details/9136575 这个最重要.. https://www.oschi ...
- 【Python】个人所得税
以月收入1w,举例计算个税: #!/usr/bin/python #-*- encoding:UTF-8 -*- #========================================== ...
- 多线程随笔知识点总结-NSThread4.1
线程的状态 状态说明 a.新建 实例化线程对象 b.就绪 向线程对象发送start消息,线程对象被加入可调度线程池等待CPU调度;detach方法和performSelectorInBackGroun ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...