Code generation
Most compilers lower their parse tree into an intermediate representation, optimise that, and then lower again into target code. Our compiler skips the middle step entirely. Every statement of INTERCAL is turned directly into a block of ARM64 or x86-64 assembly, one function-shaped fragment per statement, glued together inside _main.
Why the direct approach works for INTERCAL, what the emitted code looks like, and where each piece lives in src/bootstrap/intercalc.sh.
Why no intermediate representation
An IR pays off when you want to share optimisations across source languages or backends, or when you want to apply classical optimisations (constant folding, dead-code elimination, register allocation). We have none of those needs:
- One source language, INTERCAL, whose programs are seldom hot paths and for which optimisation is, by design, a non-goal.
- Two backends. ARM64 and x86-64, handled by two codegen modules. Three if you count the macOS/Linux ARM64 split, but that is just a
sedpass at the end. - A runtime that already encapsulates the complicated bits (Roman numerals, the Turing tape, mingle/select, error exits) as callable routines. Each INTERCAL statement degenerates to a handful of moves plus a call.
Without an IR, every codegen function is a string template that interpolates the variable numbers and the expression tree's root node. That keeps the compiler well under 2000 lines of zsh and makes each emitted fragment trivially inspectable with INTERCAL_ASM_ONLY=1.
The emitted program layout
After codegen finishes, the generated assembly looks like this, regardless of the input:
.section __TEXT,__text
.align 2
.global _main
_main:
; save argc / argv for Label 666 syscalls 5 and 6
; install a nonzero value at _stmt_flags[i] for every statement whose
; stmt_negated was 1 at compile time
b _stmt_0_start
_stmt_0_start:
; check _stmt_flags[0]; if set, jump to _stmt_0_end
; optionally roll a probability
; ... emitted body for statement 0 ...
; if a COME FROM targets statement 0's label, jump to its body here
_stmt_0_end:
_stmt_1_start:
; same shape
_stmt_1_end:
...
_stmt_N_start:
; statement N body
_stmt_N_end:
; fallthrough past the last statement → ICL633I
bl _rt_error_E633
.section __DATA,__data
; per-program data: roman numeral table pointers etc.
.section __DATA,__bss
_stmt_flags: .space N_STATEMENTS
_spot_1: .space 4
_spot_1_ign: .space 1
_twospot_1: .space 4
; ... one pair (value, ignore-flag) per used variable ...
_tail_1_ptr: .space 8
_tail_1_dims: .space 64 ; up to 16 dimensions
; ... one per used array ...
The key invariants:
- Each statement has a contiguous
_stmt_i_start→_stmt_i_endblock. Jumps fromNEXT,ABSTAIN,COME FROMtarget the_startlabels. - Scalar variables get a 4-byte slot and a 1-byte "ignored" flag. The codegen emits a check of the ignored flag before every write, silently discarding stores if set.
- Arrays get a pointer slot (set by the dimensioning statement via
_rt_mmap) and a fixed-size dimensions table. _stmt_flagsis a byte per statement: zero means "live", nonzero means "abstained".
The dispatch: codegen_statement
The statement dispatcher sits at codegen_statement, around line 791 of intercalc.sh. Its skeleton:
codegen_statement() {
local i=$1
emit "_stmt_${i}_start:"
# abstain flag check
emit " adrp x0, _stmt_flags@PAGE"
emit " add x0, x0, _stmt_flags@PAGEOFF"
emit " ldrb w1, [x0, #${i}]"
emit " cbnz w1, _stmt_${i}_end"
# probability roll if stmt_prob < 100
(( stmt_prob[i] < 100 )) && codegen_probability $i
# dispatch on stmt_type
case "${stmt_type[$i]}" in
ASSIGN) codegen_assign $i ;;
READ_OUT) codegen_read_out $i ;;
WRITE_IN) codegen_write_in $i ;;
NEXT) codegen_next $i ;;
RESUME) codegen_resume $i ;;
FORGET) codegen_forget $i ;;
ABSTAIN) codegen_abstain $i ;;
...
GIVE_UP) codegen_give_up ;;
UNKNOWN) emit " bl _rt_error_E000" ;;
esac
# if any COME FROM targets this statement's label, jump to its body
if [[ -n "${come_from_target[${stmt_label[$i]}]}" ]]; then
local tgt="${come_from_target[${stmt_label[$i]}]}"
emit " b _stmt_${tgt}_start"
fi
emit "_stmt_${i}_end:"
}
The abstain check is uniform across every statement, so it sits in the dispatcher, not inside each codegen_<type>. Same for the probability roll.
A small example end to end
Take the three-line program:
DO .1 <- #5
PLEASE READ OUT .1
DO GIVE UP
After lexing, stmt_count=3 and:
stmt_type = (ASSIGN READ_OUT GIVE_UP)
stmt_body = (".1 <- #5" "READ OUT .1" "GIVE UP")
stmt_polite= (0 1 0)
check_politeness sees 1 polite out of 3 total: 1*5=5 >= 3 ✓, 1*3=3 <= 3 ✓, so we pass. There are no labels or COME FROMs. No syslib range labels are targeted, so needs_syslib=0.
codegen_program emits the prologue. codegen_statement(0) dispatches to codegen_assign, which parses #5 into a single constant node, calls codegen_expr on that node to emit:
mov w0, #5
and then emits the 32→16-bit overflow check, the ignore-flag check, and the store:
mov w1, #65535
cmp w0, w1
b.hi _rt_error_E275
adrp x1, _spot_1_ign@PAGE
add x1, x1, _spot_1_ign@PAGEOFF
ldrb w2, [x1]
cbnz w2, _stmt_0_end
adrp x1, _spot_1@PAGE
add x1, x1, _spot_1@PAGEOFF
str w0, [x1]
codegen_statement(1) dispatches to codegen_read_out, which recognises .1 as a scalar expression, parses it, emits a load of _spot_1 into w0, and then emits:
bl _rt_write_roman
codegen_statement(2) dispatches to codegen_give_up, which on macOS ARM64 emits:
mov x0, #0
mov x16, #1
svc #0x80
On Linux ARM64 the sed conversion later rewrites mov x16, #1 into mov x8, #93 and svc #0x80 into svc #0. On Linux x86-64 the equivalent function in codegen_x86_64.sh emits:
mov rdi, 0
mov rax, 60
syscall
That is all three statements compiled. The only thing left is emit_data, which appends the BSS section with _stmt_flags: .space 3, _spot_1: .space 4, _spot_1_ign: .space 1, and a handful of data symbols used by the runtime (the roman numeral table pointer, etc.).
Expressions: codegen_expr
Expressions are tree-walked by codegen_expr, which takes the index of a tree node and emits code that leaves the value in w0 (or x0 for 32-bit-wide values). The dispatch:
CONST→mov w0, #VAL. If the value is wider than 16 bits, twomovz/movkinstructions assemble the 32-bit word.VAR_SPOTandVAR_TWOSPOT→ load the variable intow0. The address is reached throughadrp+add+ldr.OP_MINGLE→ codegen left intow0, pushw0, codegen right intow0, pop intow1, swap, call_rt_mingle. Result inx0(32-bit).OP_SELECT→ similar, call_rt_selectwith the operand width inw2.OP_AND/OP_OR/OP_XOR→ codegen child, call_rt_unary_and_16(or 32-bit variant) etc., dispatched on the node'sexpr_width.ARRAY_TAIL_REFandARRAY_HYBRID_REF→ codegen each subscript, compute the linear index (with a per-axis bounds check that firesICL241I), load the element withldrh(16-bit) orldr(32-bit).ARRAY_TAILandARRAY_HYBRID(whole-array reference, no SUB) → not legal in expression context; placeholder code emitsmov w0, #0. These node types appear only inREAD OUTandWRITE INstatements, where the codegen handles them directly without going throughcodegen_expr.
Constant folding intercepts the operator dispatch: when eval_const reduces an OP_* subtree to a literal, the codegen emits a single mov (or movz/movk for 32-bit) instead of recursing into runtime calls.
All operator evaluation is delegated to the runtime. The codegen doesn't try to inline mingle or select. This keeps the emitted code short and means that bug-fixing or optimising _rt_mingle affects every compiled program without re-running the compiler.
Tree shape matters
Because there is no precedence in INTERCAL, the shape of the expression tree is fully determined by the explicit grouping in the source. That means the tree that comes out of parse_expr is already in the order we want to evaluate it in. No dependency-ordering, no reassociation.
Per-statement codegen, in detail
Here is what each codegen_<type> emits, in condensed form:
| Statement | Emitted behaviour |
|---|---|
ASSIGN scalar | Eval RHS into w0, check ≤ 65535 (ICL275I if 16-bit target), check ignore flag, store. |
ASSIGN array elem | Eval subscripts, compute offset, eval RHS, store via the array pointer. |
ASSIGN array dim | Eval dimensions, call _rt_mmap for new storage, store pointer + dims table. |
READ_OUT var | If scalar, load + bl _rt_write_roman. If array, bl _rt_read_out_array. |
WRITE_IN var | If scalar, bl _rt_write_in_scalar, store result. If array, bl _rt_write_in_array. |
NEXT (N) | Push return address onto the NEXT stack, check overflow (ICL123I), branch to _stmt_<target>_start. |
RESUME expr | Eval expr into w0, call _rt_resume_1 which pops K entries and returns. Zero fires ICL621I. |
FORGET expr | Eval expr, subtract from the NEXT stack pointer, saturate at zero. |
ABSTAIN FROM (N) | _stmt_flags[<idx>] ← 1. |
ABSTAIN FROM gerund-list | For each gerund, loop over all statements of that type at compile-time and emit one store each. |
REINSTATE ... | Same but store zero. |
IGNORE var-list | _<type>_N_ign ← 1 for each listed variable. |
REMEMBER var-list | Same but store zero. |
STASH var-list | For each var, push current value onto the per-variable stash stack. Overflow guarded. |
RETRIEVE var-list | Pop from the per-variable stash stack; underflow fires ICL436I. |
COME FROM (N) | No body is emitted, the previous statement's body already jumps here. |
GIVE UP | Exit 0. |
UNKNOWN | bl _rt_error_E000. |
Each one lives in its own codegen_<type> function, lines 853–1621.
Ignore flags: a small trick
A statement is abstained at the statement level; a variable is ignored at the variable level. Both are per-entity bitmaps in BSS:
_stmt_flags[i]: one byte per statement._<type>_<n>_ign: one byte per used variable.
The codegen checks both. The statement-level flag is checked once at the top of the block (by the dispatcher). The variable-level flag is checked inside the store sequence for that variable, so a write is discarded silently but an expression that reads an ignored variable still reads the last value written.
This dual-level flag pattern is one of the small non-obvious pieces of the compiler. When we get to stage3.i, the same layout is reproduced in INTERCAL arrays, because it compiles to exactly the same underlying BSS layout.
The x86-64 backend
src/bootstrap/codegen_x86_64.sh is sourced by intercalc.sh when the platform is linux_x86_64. It redefines the codegen_* functions with x86-64 equivalents, using Intel syntax (.intel_syntax noprefix), RIP-relative addressing, and the System V AMD64 calling convention. The ARM64 codegen in the base file is untouched; zsh's last-function-wins semantics is what makes the override work.
Two platform-specific pitfalls worth surfacing here:
- x86-64 GNU-as does not accept
//as a comment delimiter; use#. The codegen override is careful about this. - x86-64 supports at most
[base + index*scale + disp], so three-register expressions like[r12 + r14 + rcx]are illegal. The generated code usesleafirst to pre-compute a two-register base.
The full catalogue of platform pitfalls is in AGENTS.md, "Assembly pitfalls by platform".
Observability
The two ways to see what codegen produced:
INTERCAL_ASM_ONLY=1 zsh src/bootstrap/intercalc.sh < program.i > program.s
tools/pipeline_dump.sh program.i
The first gives you the generated assembly after the Linux ARM64 sed conversion (if applicable). The second is a higher-level dump that shows each phase. Use them when you are adding a new statement type or chasing a codegen bug; do not use them as a verification tool, because the assembly output can be legitimately different across platforms.
Exercises
- The dispatcher emits the same abstain-flag check for every statement. Estimate the per-statement instruction overhead. On a 1000-statement program, what fraction of the emitted instructions is dedicated to abstain checks?
- Compile
DO .1 <- '#5 $ #3'withINTERCAL_ASM_ONLY=1and read the generated code. How manyblcalls into the runtime does the single statement produce? How many of those would a constant-folding pass eliminate? - The codegen for scalar assignment emits an ignore-flag check before every store. The codegen for array element assignment does not. Find the discrepancy in
intercalc.shand explain whether it is a bug. codegen_array_dimcalls_rt_mmapto allocate the array. What happens if the same array is dimensioned twice in a program, once to size N, once to size M? Where does the previous M-sized allocation go?- The ARM64 codegen and the x86-64 codegen share zero source code (the latter overrides every emit function). What would it take to refactor a small overlap (say, the abstain-flag check) into a platform-neutral helper?
Next reading
- runtime.md: every
_rt_*routine the emitted code calls. - syslib.md: how NEXT to labels 1000–1999 fits into the codegen model.
- platforms.md: the full list of emitted-assembly differences per target.