jitasm
A Python x86-64 JIT assembler
Example
The following example creates an int max(void) function equivalent to:
int64_t max(int64_t a, int64_t b) {
if (a >= b) return a;
return b;
}
The generated instructions are:
max:
mov rax, rdi
cmp rdi, rsi
jge .done
mov rax, rsi
.done:
ret
And we provide a peudo-instruction branch.cc to do CMP and Jcc:
max:
mov rax, rdi
branch.ge rdi, rsi, .done
mov rax, rsi
.done:
ret
And we implement it in Python using jitasm:
from jitasm.x86_64 import *
from jitasm.utils import ccall
e = Emitter()
(e.label("max"),
e.mov(RAX, RDI),
e.branch(GE, RDI, RSI, ".done"),
e.mov(RAX, RSI),
e.label(".done"),
e.ret())
e.finalize()
max_fn_ptr = e.symbol("max")
assert ccall(max_fn_ptr, 3, 7) == 7
assert ccall(max_fn_ptr, 42, -1) == 42
finalize() allocates executable memory and resolves labels. symbol() returns
the address of a public label, and ctypes.CFUNCTYPE converts that address into
a callable Python object. Call unmap() when the generated code is no longer
needed. Functions created from symbol() must not be called after unmapping.
The emitter can be finalized again to create a fresh mapping.
Assembly Spec
`m8/m16/m32/m64/m*`: `[r64]`
| `[r64 + r64 * scale +/- simm32]` (scale = 1/2/4/8)
| `[r64 * scale +/- simm32]` (scale = 1/2/4/8)
| `[r64 +/- simm32]`
| `[rip + rel32]`
`rel32`: label
`cond`: EQ | NE | LT | GT | LE | GE | LTU | GTU | GEU | LEU
Implemented Instructions
Only an essential subset of x86-64 instruction set with several pseodo-intructions are implemented. No SIMD support yet.
MOV:mov r64, r64MOV:mov r64, imm64// zero usesXOR r64, r64MOV:mov r64, m64MOV:mov m64, r64MOV:mov m32, r64// low bitsMOV:mov m16, r64// low bitsMOV:mov m8, r64// low bitsMOVZX:movzx r64, r8MOVZX:movzx r64, r16MOVZX:movzx r64, r32MOVZX:movzx r64, m8MOVZX:movzx r64, m16MOVZX:movzx r64, m32MOVSX:movsx r64, r8MOVSX:movsx r64, r16MOVSX:movsx r64, r32MOVSX:movsx r64, m8MOVSX:movsx r64, m16MOVSX:movsx r64, m32LEA:lea r64, m*MOVSD:movsd xmm, xmmMOVSD:movsd xmm, m64MOVSD:movsd m64, xmmADD:add r64, r64ADD:add r64, simm32SUB:sub r64, r64SUB:sub r64, simm32BITAND:bitand r64, r64BITAND:bitand r64, simm32BITOR:bitor r64, r64BITOR:bitor r64, simm32XOR:xor r64, r64XOR:xor r64, simm32BITNOT:bitnot r64//XOR r64, -1NEG:neg r64IMUL:imul r64, r64IMUL:imul r64, simm32IDIV:idiv r64, r64// op1 = quotient, op2 = remainder; clobbers RAX and RDXDIV:div r64, r64// op1 = quotient, op2 = remainder; clobbers RAX and RDXADDSD:addsd xmm, xmmSUBSD:subsd xmm, xmmMULSD:mulsd xmm, xmmDIVSD:divsd xmm, xmmCVTSI2SD:cvtsi2sd xmm, r64CVTTSD2SI:cvttsd2si r64, xmmROUND:round xmm, xmm// round to nearest, ties to evenCEIL:ceil xmm, xmmFLOOR:floor xmm, xmmTRUNC:trunc xmm, xmmSHL:shl r64, r64// pseudo-instruction, clobbers RCXSHL:shl r64, uimm8SAR:sar r64, r64// pseudo-instruction, clobbers RCXSAR:sar r64, uimm8SHR:shr r64, r64// pseudo-instruction, clobbers RCXSHR:shr r64, uimm8ROR:ror r64, r64// pseudo-instruction, clobbers RCXROR:ror r64, uimm8ROL:rol r64, r64// pseudo-instruction, clobbers RCXROL:rol r64, uimm8PUSH:push r64// pseudo-instructionPOP:pop r64// pseudo-instructionBEGIN:begin//PUSH RBP+MOV RBP, RSPEND:end//MOV RSP, RBP+POP RBP+RETCALL:call rel32// SysV ABICALL:call r64// SysV ABIJMP:jmp rel32JMP:jmp r64BRANCH:branch cond, r64, r64, rel32//CMP+JccBRANCH:branch cond, r64, simm32, rel32//CMP+JccBRANCH:branch cond, xmm, xmm, rel32//UCOMISD+JccCSET:cset cond, r64, r64, r8//CMP+SETccCSET:cset cond, r64, simm32, r8//CMP+SETccCSET:cset cond, xmm, xmm, r8//UCOMISD+SETccRET:retALIGN:align bytes// DATA section zero-padding
Release files for jitasm 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jitasm-0.1.1.tar.gz | 33.6 kB | Details |
Release files / jitasm-0.1.1.tar.gz
| Download URL | jitasm-0.1.1.tar.gz |
|---|---|
| Size | 33.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c61a248639f82a8d981fcf7d4804c5e63c536435488572b78b3731830975a5a
|
|
BLAKE2b-256 checksum How to use checksums |
1ecf164df5ef36f87c2ce2de4b9d7961ddcd399d5bde57999dc15f3d1015ec36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|