Mahdi Mohammadi via llvm-dev
2017-Dec-08 15:53 UTC
[llvm-dev] Parse LLVM IR using LLVM C API
Hi, I'm writing a compiler and I am thinking of mixing LLVM IR inside the source code I'm compiling. So user can write normal code and write some functions using LLVM IR. Is it possible to use LLVM C API to parse those sections of the source code and convert them into `LLVMValueRef`s pointing to parsed functions? Thanks, Mahdi -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171208/bb837e18/attachment.html>
I assume you mean "use your own code to parse some text representation of LLVM IR and then use the LLVM API to create the appropriate data stuctures". Certainly you can do that with C++ code. With the C API, I don't know. But it's probably easier to use something like the parseAssemblyInto() function in include/llvm/AsmParser/Parser.h. You pass it a memory buffer containing IR text to be parsed, and a Module (possibly empty, possibly one your compiler has already added things to) to insert the parsed items into. That's in C++, of course, but you can make a wrapper and export your own pure C interface to it if you want. On Fri, Dec 8, 2017 at 6:53 PM, Mahdi Mohammadi via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I'm writing a compiler and I am thinking of mixing LLVM IR inside the > source code I'm compiling. So user can write normal code and write some > functions using LLVM IR. > > Is it possible to use LLVM C API to parse those sections of the source > code and convert them into `LLVMValueRef`s pointing to parsed functions? > > Thanks, > Mahdi > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171208/110b488c/attachment.html>
David Chisnall via llvm-dev
2017-Dec-08 17:44 UTC
[llvm-dev] Parse LLVM IR using LLVM C API
On 8 Dec 2017, at 15:53, Mahdi Mohammadi via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > I'm writing a compiler and I am thinking of mixing LLVM IR inside the source code I'm compiling. So user can write normal code and write some functions using LLVM IR.Note that the text representation of LLVM IR is currently intended for debugging and is not guaranteed to be stable. If you’re using the C API, then I’d guess that you want your compiler to work with multiple versions of LLVM, but it’s likely that any code that embeds the text form of LLVM IR will be tied to a specific compiler version (the auto-updater should work with the bitcode representation, but is best-effort when it comes to the text version). If you have some parts of your language’s runtime library that you want to write directly in LLVM IR and don’t mind having to modify them them when you update LLVM, then this might be a good idea, but exposing it to users is likely to cause problems. David
Nicholas Wilson via llvm-dev
2017-Dec-08 21:09 UTC
[llvm-dev] Parse LLVM IR using LLVM C API
We have this feature for LDC the LLVM D Compiler[1] (although we use the C++ API). This is the relevant code: https://github.com/ldc-developers/ldc/blob/master/gen/inlineir.cpp Nic [1]:https://github.com/ldc-developers/ldc/ On 8 Dec 2017, at 11:53 pm, Mahdi Mohammadi via llvm-dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>> wrote: Hi, I'm writing a compiler and I am thinking of mixing LLVM IR inside the source code I'm compiling. So user can write normal code and write some functions using LLVM IR. Is it possible to use LLVM C API to parse those sections of the source code and convert them into `LLVMValueRef`s pointing to parsed functions? Thanks, Mahdi _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171208/0f238148/attachment.html>