All Projects → cslarsen → Minijit

cslarsen / Minijit

A basic x86-64 JIT compiler written from scratch in stock Python

Programming Languages

python
139335 projects - #7 most used programming language
assembly
5116 projects

Projects that are alternatives of or similar to Minijit

Jit Compiler
JIT compiler in Go
Stars: ✭ 70 (-62.16%)
Mutual labels:  compiler, jit, jit-compiler, x86-64
kcs
Scripting in C with JIT(x64)/VM.
Stars: ✭ 25 (-86.49%)
Mutual labels:  x86-64, jit, jit-compiler
Mir
A light-weight JIT compiler based on MIR (Medium Internal Representation)
Stars: ✭ 1,075 (+481.08%)
Mutual labels:  compiler, jit-compiler, x86-64
Dora
Dora VM
Stars: ✭ 371 (+100.54%)
Mutual labels:  compiler, jit, x86-64
Asmjit
Machine code generation for C++
Stars: ✭ 2,874 (+1453.51%)
Mutual labels:  compiler, jit, x86-64
Dynarmic
An ARM dynamic recompiler.
Stars: ✭ 475 (+156.76%)
Mutual labels:  compiler, jit, x86-64
Nativejit
A C++ expression -> x64 JIT
Stars: ✭ 999 (+440%)
Mutual labels:  compiler, jit
Mlml
self-hosted compiler for a subset of OCaml
Stars: ✭ 41 (-77.84%)
Mutual labels:  compiler, x86-64
B2dpipe
2D Pipeline Compiler.
Stars: ✭ 51 (-72.43%)
Mutual labels:  compiler, jit
Rustc codegen cranelift
Cranelift based backend for rustc
Stars: ✭ 675 (+264.86%)
Mutual labels:  compiler, jit
Fake
嵌入式脚本语言 Lightweight embedded scripting language
Stars: ✭ 172 (-7.03%)
Mutual labels:  jit, jit-compiler
Evoasm.rb
An AIMGP (Automatic Induction of Machine code by Genetic Programming) engine
Stars: ✭ 91 (-50.81%)
Mutual labels:  jit, x86-64
Wag
WebAssembly compiler implemented in Go
Stars: ✭ 177 (-4.32%)
Mutual labels:  compiler, x86-64
Llvm Tutorial Standalone
DEPRECATED (Use: https://github.com/llvm-hs/llvm-hs-kaleidoscope )
Stars: ✭ 38 (-79.46%)
Mutual labels:  compiler, jit
Leekscript V2
A dynamically typed, compiled just-in-time programming language used in Leek Wars' AIs
Stars: ✭ 46 (-75.14%)
Mutual labels:  compiler, jit
Tinycc
Unofficial mirror of mob development branch
Stars: ✭ 784 (+323.78%)
Mutual labels:  compiler, jit
Peachpy
x86-64 assembler embedded in Python
Stars: ✭ 1,592 (+760.54%)
Mutual labels:  compiler, x86-64
Nanojit
NanoJIT is a small, cross-platform C++ library that emits machine code.
Stars: ✭ 101 (-45.41%)
Mutual labels:  compiler, jit
Jphp
JPHP - an implementation of PHP on Java VM
Stars: ✭ 1,665 (+800%)
Mutual labels:  compiler, jit
Crust
C compiler toolchain in Rust. [WIP, early development stage]
Stars: ✭ 150 (-18.92%)
Mutual labels:  compiler, x86-64

MiniJIT

Contains code for the posts

You need a UNIX/POSIX system and an x86-64 compatible CPU. I've tested this on Linux and macOS, using Python 2.7 and 3+

The ~500 lines of code relies only on standard Python libraries and contains a Python bytecode converter, peephole optimizer and x86-64 machine code assembler. The code is meant to be simple to understand and pedagogical.

Finally, there is a decorator that automatically swaps out Python functions with native code:

>>> from jitcompiler import jit, disassemble
>>> @jit
... def foo(a, b): return a + b
... 
--- Installing JIT for <function foo at 0x10bc48c08>
>>> foo(10, -2)
--- JIT-compiling <function foo at 0x10bc48c08>
8
>>> print(disassemble(foo))
0x10bb3c000 48 89 fb       mov rbx, rdi
0x10bb3c003 48 89 f0       mov rax, rsi
0x10bb3c006 48 01 d8       add rax, rbx
0x10bb3c009 c3             ret 

How to run tests

The first one patches up some machine code and runs it at runtime

$ python mj.py

The second one JIT compiles Python bytecode to machine code at runtime

$ python tests.py

If you have the capstone module installed, it will display an in-memory disassembly as well.

You can also run the decorator test. It defines a function like this

import jitcompiler

#...

@jitcompiler.jit
def foo(a, b):
    return a*a - b*b

On the first call to foo, it will be compiled to native code and swap out the original Python function. It treats all arguments as signed 64-bit integers. If you have the Capstone module installed, it will also print a disassembly. To run:

$ python test-decorator.py
Definition point of foo

Installing JIT for <function foo at 0x1f855f0>

Calling foo

JIT-compiling <function foo at 0x1f855f0>
Installed native code for <function foo at 0x1f855f0>
Calling function <CFunctionType object at 0x7f867642b600>
foo(1, 2) => -3
Calling function <CFunctionType object at 0x7f867642b600>
foo(2, 3) => -5

Disassembly of foo

0x7f86765f9000 48 89 fb       mov rbx, rdi
0x7f86765f9003 48 89 f8       mov rax, rdi
0x7f86765f9006 48 0f af c3    imul rax, rbx
0x7f86765f900a 50             push rax
0x7f86765f900b 48 89 f3       mov rbx, rsi
0x7f86765f900e 48 89 f0       mov rax, rsi
0x7f86765f9011 48 0f af c3    imul rax, rbx
0x7f86765f9015 48 89 c3       mov rbx, rax
0x7f86765f9018 58             pop rax
0x7f86765f9019 48 29 d8       sub rax, rbx
0x7f86765f901c c3             ret

If you want to get serious about this

References

License

Put in the public domain in 2017 by the author Christian Stigen Larsen

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].