moonshot

technical

jit

the jit is a small x86-64 code generator for skalman's REPL. it walks the same syntax tree the REPL's interpreter walks, writes machine code into one page of memory and calls it. the value left in rax is the result.

what it compiles

integer and string literals, arithmetic, the six comparisons, variables, blocks, if and else, while and for loops with break and continue, print(), assignment, and load8, load64, store8 and store64. printing and assignment call back into kernel helpers, the rest is emitted inline.

using it

eval -c <expr> in the shell compiles and runs a program through the jit instead of the interpreter. its output is prefixed [jit] rather than [repl], so the two are told apart.

w^x

the jit page is never writable and executable at once. it is made writable and non-executable while code is written into it, then executable and read-only before it is called.