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的更多相关文章

  1. [转]LLVM MC Project

    Intro to the LLVM MC Project The LLVM Machine Code (aka MC) sub-project of LLVM was created to solve ...

  2. LLVM的总结

    LLVM 写在前面的话:无意中看到的LLVM的作者Chris Lattner相关的介绍和故事,觉得很有意思就贴上来,如果不感兴趣,可以直接跳入下一章. 关于LLVM 如果你对LLVM的由来陌生,那么我 ...

  3. LLVM 编码规范 - 中文翻译

    LLVM 编码规范 导论 语言.库和标准 C++ 标准版本 C++ 标准库 Go 代码准则 机械的代码问题 代码格式化 注释 头文件 类概述 method information 注释格式化 使用Do ...

  4. LLVM编译器架构

    LLVM编译器架构 LLVM概述 LLVM项目是模块化和可重用的编译器及工具链技术的集合.尽管名称如此,LLVM与传统虚拟机关系不大.名称" LLVM"本身不是缩写.它是项目的全名 ...

  5. 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 ...

  6. 认识 LLVM

    简介 LLVM是一套提供编译器基础设施的开源项目,是用 C++ 编写,包含一系列模块化的编译器组件和工具链,用来开发编译器前端和后端.它是为了任意一种编程语言而写成的程序,利用虚拟技术创造出编译时期. ...

  7. LLVM 初探<一>

    一.安装LLVM LLVM是一个低级虚拟机,全称为Low Level Virtual Machine.LLVM也是一个新型的编译器框架,相关的介绍Wikipedia. 现在LLVM的版本已经有很多,根 ...

  8. llvm學習(二)————llvm編譯與環境構建

    本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17339027) 在2011十月份的时候, ...

  9. LLVM和clang

    LLVM编译器架构 LLVM项目是一套工具的集合,它包括模块化.可复用的编译器及一些列工具链技术. LLVM最开始是Low Level Virtual Machine的简称,但现在它并不是传统意义上的 ...

随机推荐

  1. what is SVD and how to calculate it

    http://web.mit.edu/be.400/www/SVD/Singular_Value_Decomposition.htm SVD是研究地震波运动极性化的一个方法.

  2. vs2008下面wince错误MSDiscoCodeGenerator

    引用webservice时候错误.网上也有很多解决方法,各不相同. 不多说了. 打开vs2008安装包 找到wcu目录下面有个NetCF 目录: 下面的NetCFSetupv2.msi,NetCFSe ...

  3. css中文乱码与替换字符

    有时候,我们的css样式表中字体乱码,很诧异.百度谷歌是两个老师,有时jquery博客还上淘宝,那边有现成的代码,看看,发现里面定义全局字体是这样的font:12px/1.5 tahoma,arial ...

  4. HTML5+CSS3学习小记

    1.用网络图片作为背景图片: body{ background-image: url(http://b.hiphotos.baidu.com/album/h%3D900%3Bcrop%3D0%2C0% ...

  5. 【转】Native JavaScript Data-Binding

    原文转自:http://www.sellarafaeli.com/blog/native_javascript_data_binding Two-way data-binding is such an ...

  6. iOS UIPickerView 显示全国省市

    效果图 #import "ViewController.h" @interface ViewController () @property(strong,nonatomic)UIP ...

  7. 第七章 LED 将为我闪烁:控制发光二级管

    在上一章中了解到驱动程序的开发步骤,并一个实列来演示如何开发一个完整的驱动.但这个驱动只是简单的演示了实现步骤.真正的驱动需要与硬件直接进行相互交互.这节完整的演示驱动程序,控制开发板上的4个led灯 ...

  8. 命令行模式 svn版本管理

    linux 下svn 在命令行模式下的操作安装完svn服务并配置了环境变量之后,要创建一个存放工厂(项目)的仓库repositories用于版本控制(比如我的repositories的路径为 path ...

  9. Mercurial笔记(hg命令)

    两个站点: http://z42.readthedocs.org/zh/latest/devtools/hg.html http://bucunzai.net/hginit/ 添加用户名 在.hg目录 ...

  10. php读取json时无数据(为空)的解决方法

    在使用PHP调用一些json接口文件时 如果使用 file_get_contents 获取页面json数据后 再使用json_decode()解析后 数据无法正常输出 这是的返回值为null 这是由于 ...