INTEGRATIVE QUESTION How would you organize the code for an assembler? To write an assembler... 1. tokenize using lexical analyzer 2. parser that builds ASTs 3. Build symbol table for identifiers (labels, names of data) attributes: type address 4. translate statements into instructions also mnemonics to op codes literals (numbers) into binary COMPILER CONCEPTS What is a scope? an area of program (its text) where a declaration has effect and duplicate declarations are forbidden What syntactic features start scopes in C? Curly braces ('{' and '}'), function declarations, files What starts a scope in PL/0? procedures How is a symbol table used? Checking for duplicate declarations or for undeclared identifier uses Is there just one symbol table? no (it changes over time) CODE GENERATION What did we use for an IR in HW4? enhanced ASTs - with labels (to proc_decl) identifier ASTs for uses of identifiers with id_use structs and attributes inside (with lexical address info for locations) What information from a name's use is needed to generate code in PL/0? - lexical address offset and number of static links to follow In PL/0, why not have the parser determine the lexical address of each variable? when parsing don't know what declaration an identifer refers to parser isn't tracking scopes so would be hard for it to determine # of static links to follow CODE GENERATION FOR STATEMENTS How is the code generator written in C? recursive over the ASTs, follow the grammar style What does the C code look like for generating code for PL/0 statements? switch based on AST type, calling more specific statement functions for each kind of statement (AST) What does the C code look like for generating code for a PL/0 while statement? generate code for the condition; jump conditionally 2 (around a next jump) jump around the body (to exit) generate code for the body of the loop jump back to beginning of condition GENERATING CODE FOR IF STATEMENTS In C there are 2 kinds of if-statements with syntax if (Exp) Stmt and if (Exp) Stmt1 else Stmt2 How would these be compiled? could have 2 different kinds of ASTs code to evaluate expression conditional jump 2 (around next jump) jump to else part (if any) or around body (if no else) code for the then part body (jump to after the else part, if there is one) code for the else part (if any) What would the generated code look like for a C switch statement? evaluate expression, put result in register (or on top of stack) code like if-then-else or build a jump table, addresses of each part evaluate expression check that it's in range of jump table, if not, jump code for default case use the expression as index into table jump to the address found there indirect jumps on OO method calls: o.m() LABELS Would labels (as in HW4) be useful if the VM's ISA required absolute addresses for jumps? How would that work? in a first pass determine address use a second pass to fix the instruction addresses with information from the label