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.
- Host: GitHub
- URL: https://github.com/cpscript/simple-jit-compiler
- Owner: CPScript
- Created: 2025-07-04T06:12:09.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-07-04T06:14:23.000Z (6 months ago)
- Last Synced: 2025-08-30T05:57:44.309Z (4 months ago)
- Language: C
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.