sicp.io
5.5.1 · Structure of the compiler

Dispatch by syntax, honor target and linkage, compose instruction contracts.

Build one compiler dispatcher whose specialized procedures emit instruction-sequence records with explicit needs, modifications, statements, and targets.

Guiding question

Which compiler decisions depend on source syntax, and which depend on the requested target register and linkage?

  • Classify every supported source form before compilation
  • Pass a target register and linkage through specialized compilers
  • Represent instruction sequences with needs, modifies, and statements
  • Compose sequences while retaining conservative register contracts
  • Generate labels for conditional control
  • Compile a lambda body into stored procedure code rather than leaving raw body source

compile owns only syntax dispatch. Specialized compilers decide how a self-evaluating value, variable, quotation, conditional, lambda, sequence, or application reaches a target. end-with-linkage then adds no control transfer, a return through continue, or a direct label goto. This keeps expression meaning separate from where its value must land and what should happen next.

Instruction sequences carry statements plus conservative needs and modifies sets. append-sequences combines those contracts while preserving order. The conditional example shows labels and linkage around recursively compiled predicate and branches. The lambda compiler recursively compiles its body and stores the resulting instruction data in a represented compiled procedure. This educational instruction set models the displayed SICP compiler structure with a generic apply-procedure operation.

SICP code7,888 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The dispatch program returns (self variable quote if lambda begin application). The conditional compiler returns ((env continue) (val proc argl continue) 15 (assign test branch assign assign assign assign assign assign assign goto label assign label goto) 2).

    Trace focus

    In the second program, separate predicate compilation from the generated test and branch. Then follow operator compilation into proc, operand compilation into val and argl, the alternative label, the after-if label, and the final return through continue. The needs and modifies sets provide conservative register-traffic contracts.

    Try it yourself

    Change the program and compare the result.

    Compile a lambda containing a begin and an if with target proc and a named linkage. Inspect the stored body instructions separately from the outer closure-construction sequence.

    Show hint

    The lambda body always compiles toward val with return linkage. The outer target and linkage describe the expression that creates the compiled procedure.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%