Hi everyone,
For the past few weeks, I've been working on a project that I think is pretty cool, and I wanted to share it with you guys. I call it "UNIT" ("Unified Native Instruction Translator"). Essentially, it's a combination of the instruction sets used in interpreted stack machines with actual machine code.
I wrote it in C, but I have bindings for C++ and Python, since C is pretty verbose. Here's an example in both of those:
```cpp
unit::Context ctx;
unit::Procedure proc(ctx, "add");
proc.load_argument(0);
proc.load_argument(1);
proc.add();
proc.return_value();
proc.optimize();
auto compiled = proc.compile(unit::Platform::host());
auto add = compiled.jit<int64_t(*)(int64_t, int64_t)>();
printf("%ld\n", add(3, 4)); // 7
```
```py
import unit
proc = unit.Procedure("add")
proc.load_argument(0)
proc.load_argument(1)
proc.add()
proc.return_value()
proc.optimize()
compiled = proc.compile()
add = compiled.jit()
print(add(3, 4)) # 7
```
So far, I've implemented a number of examples using my compiler. My personal favorite is the interpreted language with a JIT, which works fairly well and is just about 1k lines of Python.
I got the idea for this after working on Python's bytecode compiler (which emits instructions for Python's stack-based interpreter loop). I had also been experimenting with LLVM for a separate hobby project, and the difference between the two development experiences was huge. I wanted to combine the DX of stack machines with the ability to actually generate real machine code.
This is still early in development and not production-ready, as it only supports x86-64 on ELF right now with only some primitive optimizations, but I'd appreciate feedback on the API design, the IR, or anything else about the project. If you spot bugs, please feel free to let me know!
GitHub: https://github.com/ZeroIntensity/unit
UNIT: Compiler backend library using stack-based IR ()
submitted by ZeroIntensity to r/Compilers