In computing, code generation is part of the process chain of a compiler and converts intermediate representation of source code into a form (e.g., machine code) that can be readily executed by the target system.
11/10
Warm-up Problem: Define using our shorthand, code(term1 → term2 STAR factor)
Code reuse
A compiler mixes the code it outputs with code from runtime environment.
MERL for linking purposes.
gcc is only a compiler drivers.
Using print
.import print in prologue to use it.
Think of it as a label. Just like any other procedure, call print using the jalr instruction which overwrites register 31.
Convention: Store 1 inside $11 (For true and false purposes)
Now prologue is even longer…
; Prologue
.import print
lis $4
.word 4
lis $10
.word print
list $11
.word 1
sub $29, $30, $4
; end Prologue and begin body
; space for variables
; translated WLP4 code
; end body and begin epilogue
add $30, $29, $4
jr $31
If Statements
Rule: statement → IF (test) (stmts1) ELSE (stmts2)
Use labels
11/15 Lecture 18
Warm up Problem
Informally how would we generate code for a switch statement?
code(switch (expr){…})
Code generation:
Could optimize when we sw what’s in 3into−12(29) and lw from -12(29)to3.
Print which production rule we are in when debugging.
Procedures
Warm up Problem
11/17/22
Given that NULL is 1 in WLP4, how can we using only WLP4 code cheat and get a pointer to 0 anyways?