Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate binary code (shared library) from embedded LLVM in C++

I am working on a high performance system written in C++. The process needs to be able to understand some complex logic (rules) at runtime written in a simple language developed for this application. We have two options:

  1. Interpret the logic - run a embedded interpreter and generate a dynamic function call, which when receives data, based on the interpreted logic works on the data

  2. Compile the logic into a plugin.so dynamic shared file, use dlopen, dlsym to load the plugin and call logic function at runtime

Option 2 looks to be really attractive as it will be optimized machine code, would run much faster than embedded interpreter in the process.

The options I am exploring are:

    • write a compile method string compile( string logic, list & errors, list & warnings )
    • here input logic is a string containing logic coded in our custom language
    • it generates llvm ir, return value of the compile method returns ir string
    • write link method bool link(string ir, string filename, list & errors, list & warnings)
    • for the link method i searched llvm documentation but I have not been able to find out if there is a possibility to write such a method

    If i am correct, LLVM IR is converted to LLVM Byte Code or Assembly code. Then either LLVM JIT is used to run in JIT mode or use GNU Assembler is used to generate native code.

    Is it possible to find a function in LLVM which does that ? It would be much nicer if it is all done from within the code rather than using a system command from C++ to invoke "as" to generate the plugin.so file for my requirement.

    Please let me know if you know of any ways i can generate a shared library native binary code from my process at runtime.

    like image 971
    Sanjit Avatar asked Apr 09 '14 08:04

    Sanjit


    1 Answers

    llc which is a llvm tool that does LLVM-IR to binary code translation. I think that is all you need.

    Basically you can produce your LLVM IR the way you want and then call llc over your IR.

    You can call it from the command line or you can go to the implementation of llc and find out how it works to do that in your own programs.

    Here is a usefull link:

    http://llvm.org/docs/CommandGuide/llc.html

    I hope it helps.

    like image 114
    AngelBaltar Avatar answered Nov 18 '22 21:11

    AngelBaltar