An open API service indexing awesome lists of open source software.

https://github.com/cpscript/simple-jit-compiler

A Simple JIT Compiler: For arithmetic expressions, emitting native x86-64 code.
https://github.com/cpscript/simple-jit-compiler

Last synced: about 2 months ago
JSON representation

A Simple JIT Compiler: For arithmetic expressions, emitting native x86-64 code.

Awesome Lists containing this project

README

          

### This JIT compiler implements a complete pipeline from arithmetic expression parsing to native x86-64 code execution.
>Bleh :P
---

**Architecture:** The compiler uses a recursive descent parser to build an AST from infix expressions, then traverses the AST to emit x86-64 machine code directly into executable memory allocated via mmap with PROT_EXEC.

**Parser Implementation:** Tokenizes input into numbers and operators, builds AST using standard precedence rules (multiplication/division before addition/subtraction), handles parentheses for grouping.

**Code Generation:** Emits raw x86-64 opcodes using RAX as primary accumulator and RBX for binary operations. Uses stack for intermediate values during expression evaluation. Key instruction sequences: MOV RAX immediate for loading constants, PUSH/POP for stack management, ADD/SUB/IMUL for arithmetic, XOR RDX + IDIV for division.

**Memory Management:** Uses mmap to allocate executable pages, automatically handles machine code layout, returns function pointer for direct execution.

**Execution Model:** Generated code follows x86-64 calling convention, returns result in RAX register, can be called directly as C function pointer.

Compile with: `gcc -o jit jit.c`
Usage: `./jit "2 + 3 * 4"`

Supports nested expressions, proper operator precedence, and generates efficient native code without intermediate interpretation layers.