zoukankan      html  css  js  c++  java
  • [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 << "
    ";                                                         
        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.

  • 相关阅读:
    自己动手编译apache-tomcat-6.0.41-src源码
    搭建Tomcat6源代码阅读环境
    Windows CMD下一些有用的命令
    windows server域的概念以及wmic(centos上命令)
    C++虚函数的实现机制示例
    通过j-interop访问WMI实例代码
    C++虚函数示例
    创建一个Windows窗体
    UNICODE字符集(20140520)
    windows程序设计笔记
  • 原文地址:https://www.cnblogs.com/luweiseu/p/3161146.html
Copyright © 2011-2022 走看看