简体   繁体   中英

How to invoke LLVM code from a c++ program and pass data to it

I have a c++ program which can generate LLVM IR at runtime. Now I want to compile this IR into a binary from my c++ program and invoke the binary. Also before invoking I want to pass data from my c++ program to the generated binary. I am new to LLVM and I couldnt find any documentation regarding this. Could someone point to a documentation about this or let me know how this can be done.

Here are the general block and tackle steps that come to mind even though you didn't state how you want to invoke the emitted code so I will assume it is as though you are invoking another executable:

In your executable

  1. Generate the LLVM IR (see below)
  2. Invoke, using a function like system , clang that will generate the new executable program from the LLVM IR.
  3. If the compile is successful, then, using a function like system , call the newly generated executable and pass the data as command line arguments.

LLVM IR Generated

  1. In the IR you are generating, you would need to emit a main function definition that takes argc and argv as that will be what consumes the passed data that you are sending.
  2. In the main body you would need to handle the command line input appropriately and call the code you originally wanted exercised.

However: there are variations of this. For example, if you wanted to generate a dynamic library you could omit the generation of the main function and instead do a run-time load of the dynamic library you just created and resolve the function entry point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM