How to generate LLVM IR with ANTLR and C target - llvm

im currently trying to generate LLVM IR with ANTLR3.
But the problem ist, that i need the C target (C++ would be better but is not working yet, or is it?) but from C i can't call the LLVM C++ API for Building the IR.
The Tutorial from Terence Parr uses Java and the StringTemplate lib. But as i know the StringTemplate lib ist not available for C.
Does anyone have some good ideas how this can be done? :)
Thx

there is a very good entry in the LLVM faq, discussing what you want to do:
http://llvm.org/docs/FAQ.html#langirgen
It points to llvm-c, which is a C binding to llvm, allowing to generate IR directly from C, and is integrated to llvm since at lease llvm-2.6
The approach Terence takes in its example with antlr-java and stringtemplates is to generate llvm assembly as text (and generating text is really what stringtemplates is about).

Related

How to cross compile Flash SWF file(s) into Keil Compilable C/C++ file(s)

I have been doing research in cross compile flash SWF files into C/C++ source files
There are plenty of tools in decompiling SWF files into plain-text files formats.
The Free SWF Decompiler provides plenty opensource solution on this.
To decompiler SWF into C/C++ source files,
I had tried the following solutions
1) Haxe: The Haxe Compiler is responsible for translating the Haxe programming language to the target platform native source code or binary
To do this, I need to
a) Decompile SWF into actionscripts files
b) Convert actionscripts to Haxe language script
c) Compiler haxe into C++ source file
d) Recompiler C++ source files with Keil MDK-ARM
Drawback: The output C++ file is huge & contains many Flex SDK like resources in C source format, which is hard to re-compiler in Keil MDK-ARM. And it seem quite an inefficient job to get the Keil compilable binary code.
Recently, I had found another possible solution to do this by Adobe Flash C++ Compiler, ie FlasCC (
A complete BSD-like C/C++ development environment with a GCC based cross-compiler capable of targeting the Adobe Flash Runtime)
But I am not sure if it would work as I expected.
Since FlasCC can compile c/c++ code into ActionScript bytecode (ABC) as well as LLVM bytecode.
My thought is
a) Parse SWF ActionScript bytecode (ABC)
b) Read ActionScript bytecode (ABC) in FlahCC (not sure if this can be done?)
c) By the equivalent ActionScript bytecode (ABC) in FlasCC, output its LLVM bytecode (not sure if this can be done?)
d) Convert LLVM bytecode to C++ code by LLC
In this way, the ActionScript bytecode can be optimized through LLVM LTO(Link Time Optimized) Build
Since I am not llvm expert, I need some advise on this.
Is this workable? Or is there any other way to do this?
Generally speaking, no. Auto-compilation of flash to binary will be quite complex thing to do and ineffective one too.
I'm surprised the first approach you used actually worked, I'd expect the resulting code to fail here and there randomly. Anyhow, there is not a single chance as3->haxe compilation can be effective in tems of resulting code performance.
As for the second approach, compilers are one-way thing, you can't use FlasCC to decompile ABC bytecode nor to compile as3, it can only be used to compile c/c++ to ABC.
Compilation of ABC to LLVM and then to binary seems much more feasible and doable, but will require quite a lot of effort and yet again I doubt the result will be any fast. And I'm almost sure it will be far slower than just an swf running in flash player on target platform. (Yet, considering the things you've already doneI'd estimate that you will be able to make a prototype in 2 or 3 month of hard work, so it may be worth a shot.)
It seems much more clear to me now to decompose the whole job.
The todo job is, decompose SWF files to Keil compilable C/C++ source files.
I found an open source solution that was design to convert
ABC to TESSA (Type Enriched SSA) Tamarin on LLVM
TESSA to LLVM IR
LLVM IR to C (By llc)
I will give it a try!

Alternative to LLVM for C++ (bytecode generation)

I have written my lexer and parser in flex and bison. My project is based on C++ and I would love to stick to it. Currently my Interpreter is written in C++ but I want to achieve faster execution time by converting to bytecode (some form of a VM-level bytecode) when my interpreter works. I know this can be achieved through LLVM. I had problems using it from a x64 OS and developing on a Visual Studio 2012 (32-bit). Some of which can be found # LLVM linker errors on VS. The other tool I came across is ANTLR and if I understand correctly then the latest release does not easily integrate into C++ yet. Many references were found for the same but a quick one can be # ANTLR integration with C++ issue. Also I do not want to dispose off my lexer and parser written in flex and bison. What are my options if I want to generate bytecode from my AST?
EDIT: My aim is to generate bytecode from my AST (for the target architecture) so the code can be executed at a Virtual Machine level. Currently I have an Interpretor which interpretes (executes the AST) based on C++ library and generates bytecode. I want to generate Bytecode straight from my AST and execute the AST in its bytecode.
Would be appreciated.
Generating native bytecode directly from your AST is not possible (well actually it is, but that would be extremely difficult). You need some kind of intermediary step like emitting LLVM bytecode or code in some programming language of your choice. Please note that LLVM bytecode is not the same as native target machine bytecode. The LLVM bytecode has to be compiled to native binaries for target machines which is done by the respective frontend. So you could as well just generate C++ code from your AST using a handwritten code emitter which traverses your syntax tree. Then you use a C++ compiler for the target platform to compile it to the desired native binary.

LLVM for parsing math expressions

I have some troubles wrapping my head around what LLVM actually does...
Am I right to assume that it could be used to parse mathematical expressions at runtime in a C++ program?
Right now at runtime, I'm getting the math expressions and build a C program out of it, compile it on the fly by doing system call to gcc. Then I dynamically load the .so produced by gcc and extract my eval function...
I'd like to replace this workflow by something simpler, maybe even faster...
Can LLVM help me out? Any resources out there to get me started?
You're describing using LLVM as a JIT compiler, which is absolutely possible. If you generate LLVM IR code (in memory) and hand it off to the library, it will generate machine code for you (still in memory). You can then run that code however you like.
If you want to generate LLVM IR from C code, you can also link clang as a library.
Here is a PDF I found at this answer, which has some examples of how to use LLVM as a JIT.

How do I parse LLVM IR

I have LLVM IR code in text format. What I wanna do is to be able to parse it and modify that code. Is there an API which can help in parsing the LLVM IR code? What libraries should I have in my system? At this moment I have clang compiler installed as well LLVM, as I can use commands such as llc, opt and llvm-link.
LLVM is primarily a C++ library. It has all the tools you can imagine to parse, manipulate and produce IR in both textual and bitcode (binary) formats.
To get started, take a look at the llvm::ParseIRFile function, defined in header include/llvm/Support/IRReader.h.
The best way to proceed would be to download the LLVM source code and build it, following these instructions. It's then easy to write your own code that uses the LLVM libraries.

How to embed LLVM?

The LLVM Core project consists of:
Compiler - converts source code to LLVM IR
VM - executes compiled IR code
How can I embed the VM to a C++ application?
The LLVM is really a collection of libraries that you can link to, so it's pretty easy to embed. More often the LLVM takes IR that you generate and compiles it directly to machine code. There is also a library available to interpret and execute IR for platforms that do not support JIT compilation.
There's a pretty good tutorial available on the LLVM website here: http://llvm.org/docs/tutorial/. I suggest that you go through that and then ask more specific questions if you have them.
Take a look at the HowToUseJIT example in LLVM.