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.
Related
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.
I was working on some project to convert a C+ACSL language into another language by converting first into LLVM IR and then convert it into another language. I cannot tell about that because it is related to a group of some people who do not want to disclose it, so we will not talk about it, but it is very close to C. I have completed the work till LLVM IR and also have written to convert it back, with the help of CBackend, but I really don't know how to use it, means how should I run that into my LLVM IR. Is there a command in LLVM for doing it, or something which can help me.
UPDATE 1
My input is LLVM IR and ouput is going to be a C like code, not C as it will be following syntax different from C.
Is there some nice feature of the format or library for going from some part of the bytecode to the line of code it originally came from? This would obviously be useful for debugging and error messages.
In particular, I'm looking at how hard it would be to add support for source maps to js_of_ocaml.
When compiled with debug information enabled (option -g), the bytecode carries so-called "event" structures marking for example the function entry and return point, which provide source location and typing information.
As a proof of concept of how to inspect this information, I have created a small branch of the ocamlpp tool (a small utility by Benoît Vaugon to inspect bytecode files) that prints this debug information alongside the bytecode instructions.
I have no idea whether js_of_ocaml takes the necessary steps to preserve this location information throughout the compilation process. You should probably contact the maintainer, Jérôme Vouillon, to ask for more information.
js_of_ocaml -debuginfo uses debug_event in a bytecode to write the line of code in comment.
Is there an easy way of going from llvm ir to working source code?
Specifically, I'd like to start with some simple C++ code that merely modifies PODs (mainly arrays of ints, floats, etc), convert it to llvm ir, perform some simple analysis and translation on it and then convert it back into C++ code?
It don't really mind about any of the names getting mangled, I'd just like to be able to hack about with the source before doing the machine-dependent optimisations.
There are number of options actually. The 2 that you'll probably be interested in are -march=c and -march=cpp, which are options to llc.
Run:
llc -march=c -o code.c code.ll
This will convert the LLVM bitcode in code.ll back to C and put it in code.c.
Also:
llc -march=cpp -o code.cpp code.ll
This is different than the C output engine. It actually will write out C++ code that can be run to reconstruct the IR. I use this personal to embed LLVM IR in a program without having to deal with parsing bitcode files or anything.
-march=cpp has more options you can see with llc --help, such as -cppgen= which controls how much of the IR the output C++ reconstructs.
CppBackend was removed. We have no -march=cpp and -march=c option since 2016-05-05, r268631.
There is an issue here... it might not be possible to easily represent the IR back into the language.
I mean, you'll probably be able to get some representation, but it might be less readable.
The issue is that the IR is not concerned with high-level semantic, and without it...
I'd rather advise you to learn to read the IR. I can read a bit of it without that much effort, and I am far from being a llvm expert.
Otherwise, you can C code from the IR. It won't be much more similar to your C++ code, but you'll perhaps feel better without ssa and phi nodes.
I was reading here and there about llvm that can be used to ease the pain of cross platform compilations in c++ , i was trying to read the documents but i didn't understand how can i
use it in real life development problems can someone please explain me in simple words how can i use it ?
The key concept of LLVM is a low-level "intermediate" representation (IR) of your program.
This IR is at about the level of assembler code, but it contains more information to facilitate optimization.
The power of LLVM comes from its ability to defer compilation of this intermediate representation to a specific target machine until just before the code needs to run. A just-in-time (JIT) compilation approach can be used for an application to produce the code it needs just before it needs it.
In many cases, you have more information at the time the program is running that you do back at head office, so the program can be much optimized.
To get started, you could compile a C++ program to a single intermediate representation, then compile it to multiple platforms from that IR.
You can also try the Kaleidoscope demo, which walks you through creating a new language without having to actually write a compiler, just write the IR.
In performance-critical applications, the application can essentially write its own code that it needs to run, just before it needs to run it.
Why don't you go to the LLVM website and check out all the documentation there. They explain in great detail what LLVM is and how to use it. For example they have a Getting Started page.
LLVM is, as its name says a low level virtual machine which have code generator. If you want to compile to it, you can use either gcc front end or clang, which is c/c++ compiler for LLVM which is still work in progress.
It's important to note that a bunch of information about the target comes from the system header files that you use when compiling. LLVM does not defer resolving things like "size of pointer" or "byte layout" so if you compile with 64-bit headers for a little-endian platform, you cannot use that LLVM source code to target a 32-bit big-endian assembly output pater.
There is a good chapter in a book explaining everything nicely here: www.aosabook.org/en/llvm.html