Reputation: 201
I have created a compiler that produces an intermediate code. I do not have the time to write the backend for my project.
Is there any software I could use to evaluate the intermediate code produced? Where could I download this software?
The output looks something like this :
t1 = 0.67596e-7
sum = t1
t1 = 2
t2 = 3
t3 = t2 + t1
i = t3
L0:
t1 = sum
t2 = 20
t3 = compare(t1 <= t2)
t4 = sum
t5 = 12
t6 = compare(t4 ~= t5)
t7 = t3 | t6
t8 = sum
t9 = 20
t10 = compare(t8 > t9)
t11 = t7 & t10
if t11 true then goto L1 else goto L2
L1:
t1 = 2
t2 = sum
t3 = t2 + t1
sum = t3
t1 = 1
t2 = i
t3 = t2 + t1
i = t3
goto L0
L2:
Thanks for reading.
Upvotes: 9
Views: 4863
Reputation: 1147
Another alternative nobody has mentioned is QBE.
QBE aims to be a pure C embeddable backend that provides 70% of the performance of advanced compilers in 10% of the code.
It is like LLVM, but it's a lot lighter as a dependency. From the comparison to LLVM:
QBE is for amateur language designers.
It does not address all the problems faced when conceiving an industry-grade language. If you are toying with some language ideas, using LLVM will be like hauling your backpack with a truck, but using QBE will feel more like riding a bicycle.
Upvotes: 3
Reputation: 494
highly recommend llvm, a platform-cross infrastructure, support machine-code generation and many passes of optimization. The tutorial on llvm.org is very simple.
Upvotes: 0
Reputation: 6295
I'd recommend spitting out Pascal code and using Free pascal.
It's natively compiled, compilation is blazing fast and you can target a plethora of platforms.
Upvotes: 3
Reputation: 202495
Code generation is my business :-)
Comments on a few options:
CLR:
LLVM:
C as target language
Summary: anything except C is a reasonable choice. For the best combination of flexibility, quality, and expected longevity, I'd probably recommend LLVM. But your example code is very close to C--, so that may be an advantage.
Full disclosure: I am affiliated with the C-- project.
Upvotes: 15
Reputation: 40224
Since your code is C
-ish, you could try C--
. It a C
like language for compiler backends. IIRC, it's used by one of the major Haskell compilers.
Upvotes: 0
Reputation: 49311
If you can tolerate a dependency on the JVM, then it is pretty simple to generate byte-code using ASM.
If you can't, then gnu lightning can be used for on-the-fly code generation (smaller and simpler than LLVM, but without the optimisations).
Upvotes: 1
Reputation: 1503
NekoVM is designed to accept a high level IL similar to yours and then run it. Neil Butterworth's suggestion to use C is also a good choice.
Upvotes: 1
Reputation: 15198
You may have better luck selecting a compiler stack and using the intermediate code supported by that stack. For example, gcc uses RTL, register transfer language as a common denominator for all languages supported by gcc. I suspect that .net has something similar that's used for things like IronPython.
If you want to use something that supplies a virtual machine for evaluating code, the SPIM MIPS vm supports the Motorola 68000 architecture and is really easy to work with.
Upvotes: 2
Reputation: 47452
Why not target another high level language as the intermediate output, for example you could generate C code, and use any C compiler to compile that. This has been used by a number of high level languages in the past.
Another slightly lower level option would be to generate IL code and use the .NET runtime. If the compiler is written in .NET you can use the Reflection.Emit namespace to generate a .NET assembly which can then be run in any .NET environment.
Upvotes: 2
Reputation:
Have a look at the llvm compiler infrastructure project. It is used in a lot of real world projects. The LLVM is a low level virtual machine, easy to create code for and easy to translate to native code.
Upvotes: 12
Reputation:
Your emitted code looks pretty close to C - whay not have your compiler emit C and use a C compiler as the backend - that's the way the original C++ cfront compiler worked.
Upvotes: 8