简体   繁体   English

如何从C ++程序调用LLVM代码并将数据传递给它

[英]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. 我有一个c ++程序,可以在运行时生成LLVM IR。 Now I want to compile this IR into a binary from my c++ program and invoke the binary. 现在,我想从我的c ++程序将此IR编译为二进制文件并调用该二进制文件。 Also before invoking I want to pass data from my c++ program to the generated binary. 同样在调用之前,我想将数据从我的c ++程序传递到生成的二进制文件。 I am new to LLVM and I couldnt find any documentation regarding this. 我是LLVM的新手,我找不到与此相关的任何文档。 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) 生成LLVM IR(请参见下文)
  2. Invoke, using a function like system , clang that will generate the new executable program from the LLVM IR. 使用诸如system ,clang之类的函数调用,该函数将从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. 如果编译成功,则使用诸如system类的函数,调用新生成的可执行文件并将数据作为命令行参数传递。

LLVM IR Generated 生成LLVM IR

  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. 在生成的IR中,您需要发出一个main函数定义,该函数定义使用argc and argv因为这将消耗正在发送的已传递数据。
  2. In the main body you would need to handle the command line input appropriately and call the code you originally wanted exercised. main身体,你将需要妥善处理在命令行中输入并调用你原本想行使的代码。

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. 例如,如果要生成动态库,则可以省略main函数的生成,而是对刚创建的动态库进行运行时加载并解析函数入口点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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