How can I compile meta node into dwarf binary with LLVM? - llvm

I'd like to know how LLVM does it. For example, I have some Meta Nodes, and use some code to compile it into binary then dump it.
I can't find the part in LLC's source. Thanks in advance.

Related

Transforming the Clang AST into an AST in other languages

I want to build a tool that can automatically transform the Clang AST into an equivalent AST in other languages (MLIR). I have some experiences with LLVM passes, but never directly played with Clang.
I wonder what is the best way to do that. One thing I can think of is to dump the Clang AST into a file and read back in a pass to construct another AST.
Can this be done within Clang itself and output an AST in the transformed language? If so, is there any tutorial I can start with?
Thank you very much.

Where can I find the opcode numbers for the LLVM bitcode?

Where can I find the LLVM bytecode representation of the LLVM IR language?
Like this <result> = add <ty> <op1>, <op2>, but in binary form like this incept for LLVM instead of JVM. More specifically I want the opcode numbers so I can study the bitcode on a binary level.
I think the canonical source for LLVM Bit Codes is this file :
llvm-src/include/llvm/Bitcode/LLVMBitCodes.h
from the llvm source which can be found here: http://llvm.org/releases/
You may also want to look at the code in llvm-src/lib/Bitcode/Reader, which reads bitcode.
you can find opcode numbers in include/llvm/IR/Instruction.def
https://llvm.org/svn/llvm-project/llvm/tags/RELEASE_14/include/llvm/Instruction.def

debugging LLVM IR with LLDB

I've developed an LLVM front-end generating LLVM IR as the target code from some source-language X. If I extend this front-end to embed debug information within the generated IR, is it possible to use LLDB for debugging my source-language? I mean does lldb support any source-language targeting LLVM IR?
You'll have to get a DWARF language code and get lldb to recognize it. If we get some DWARF for an unknown language, we'll just ignore it...
Then with no more support, some things will work, others won't.
If you emit correct line table information, you should be able to map back to your source, and that should get stepping working as well. Other things start getting hard.
The next hard part is how you are going to tell lldb about your type information. lldb uses Clang's AST's as internal storage for type information in the debugger. lldb translates DWARF type information into Clang AST's both for printing local variables (with the frame variable command) and for use with the expression parser.
If your language has a type system that looks kinda like C lldb should be able to parse the DWARF for your types. You'll that plus correct variable information should get frame variable working.
The expression parser (i.e. the expression, print or po commands) requires that lldb have a parser for your language. That can be a pretty big chunk of work.

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:
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
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.
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.

LLVM instrumentation

Recently, I am doing some research with LLVM.
At first, I want to write a pass to instrument .bc file.
Thus, it will record the execution path of the basic block of my .bc file.
Then, I want to term this .bc file into .exe file. Please give me your suggestions and if you have some examples for instrumentation of LLVM, please show me.
LLVM already comes with a number of instrumentation tools built-in. Take a look in the lib/Transforms/Instrumentation directory in the source tree.
One of the best known passes is Address Sanitizer, an instrumentation-based memory error detector (kinda like Valgrind, but significantly faster). Address Sanitizer has a runtime component + a LLVM pass that inserts instrumentation; the pass lives in lib/Transforms/Instrumentation/AddressSanitizer.cpp. There's some description of how it works on this page.