博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[llvm] Call the LLVM Jit from c program
阅读量:4982 次
发布时间:2019-06-12

本文共 2055 字,大约阅读时间需要 6 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; using namespace llvm; int main() { InitializeNativeTarget(); llvm_start_multithreaded(); LLVMContext context; string error; OwningPtr
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
(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.

 

转载于:https://www.cnblogs.com/luweiseu/p/3161146.html

你可能感兴趣的文章
[GraphQL] Set variable and default value & alias
查看>>
[Dart] final vs const
查看>>
[GraphQL] Reuse GraphQL Selection Sets with Fragments
查看>>
[GraphQL] Query GraphQL Interface Types in GraphQL Playground
查看>>
[Javascript] Create an Image with JavaScript Using Fetch and URL.createObjectURL
查看>>
[Functional Programming] liftA2 and converge
查看>>
Define Interfaces and Share Class Members through Mixins in Dart
查看>>
[SCSS] SASS dynamic class properties
查看>>
[Angular 8] Lazy loading with dynamic loading syntax
查看>>
[Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
查看>>
[Angular 8] Implement a Custom Preloading Strategy with Angular
查看>>
[Dart] Understand Classes and Inheritance in Dart
查看>>
[Angular] Using Pipe for function memoization
查看>>
[Angular 8] Custom Route Preloading with ngx-quicklink and Angular
查看>>
[Angular 8] Calculate and Measure Performance budgets with the Angular CLI
查看>>
[CSS] Conditionally Assign Style to a Parent Element with Focus-Within Pseudo-class
查看>>
[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
查看>>
[Functional Programming] Rewrite a reducer with functional state ADT
查看>>
[Dart] Manipulate Lists/Arrays in Dart
查看>>
[AngularJS] Extend Controller
查看>>