Support code for students (Python version).
The runtime.c file needs to be compiled by doing the following
gcc -c -g -std=c99 runtime.c
This will produce a file named runtime.o. The -g flag is to tell the
compiler to produce debug information that you may need to use
the gdb (or lldb) debugger.
On a Mac with an M1 (ARM) processor, use the -arch x86_64 flag to
compile the runtime:
gcc -c -g -std=c99 -arch x86_64 runtime.c
The test harness in utils.py reads the variable arch in your
compiler.py. If it is missing, the harness uses 'x86'. The values
'riscv' and 'arm' are reserved for the RISC-V and ARM back ends that
are in progress.
The assembly syntax tree is shared by all architectures (asm_ast.py;
x86_ast.py adds the x86-only ByteReg, and is still the module to import).
Each architecture has a printer (x86_printer.py), and str() of a node
uses the printer of the architecture chosen by arch.
The x86 interpreter interp_x86, defined in the interp_x86
directory, runs the x86 programs produced by every chapter's compiler, both
the final x86 and the x86-like languages produced by the select instructions,
assign homes and patch instructions passes. Its architecture-independent
core (memory, registers, calls and the runtime functions) is in
emulator.py; the directory interp_x86 holds the x86 instruction table,
parser and converter. Add interp_x86 to your interp_dict for those
passes:
from eval_x86 import interp_x86
interp_dict = {
...
'select_instructions': interp_x86,
'assign_homes': interp_x86,
'patch_instructions': interp_x86,
}
Starting from the chapter on tuples, interp_x86 needs to know how many
variables each program or function spills to the root stack:
- Up to Ltup, set the field
num_root_spillsof theX86Programto the number of tuple variables that have been spilled into the root stack. - Beginning with Lfun, set the field
num_root_spillsof everyFunctionDefin the same way.
The interpreter's collect only enlarges the heap and never copies, so it
won't reveal mistakes in how your compiler uses the root stack. The tests
that run your compiled program with runtime.c will.