Kompilatorer och interpretatorer: Lecture 12

Note: This is an outline of what I intend to say on the lecture. It is not a definition of the course content, and it does not replace the textbook.

Today:
Some rests from lecture 11 about intermediate code generation (ASU 8.2).
Code generation.
ASU 9.1-9.4, 9.12. (KP chapter 7.)

8. Intermediate code generation

...

8.2 Declarations

Declarations in a procedure

When you find a declaration of a variable, put it in the symbol table.
Symbol table interface: enter_variable(name, type, offset), maybe width
Keep track of offset!

Keeping track of scope information

New symbol table interface: ASU Fig 8.12. Symbol tables during compilation of the program in ASU Fig 7.22:

Symbol tables for nested procedures

During processing, keep a stack of symbol tables:

Fields names in records (or members in a class!)

Use a separate symbol table for each record type:
stp = make_table(nil); // when entering the declaration

Then use it just as with variables declared in a procedure:
enter_variable(stp, name, type, offset)

8.3 - 8.7

Skip the rest of chapter 8.

9. Code generation

Input: intermediate code, often three-address code, but can also be e. g. syntax trees.

Several types of target code:

What the code generator does:

From KP page 146, about code generation:

About code generation

9.1 Issues in the design of a code generator

Instruction selection

Example 1, a three-address instruction:
x = y + z;

Use a template:

MOV y,R0 -- Load y into register R0
ADD z,R0 -- Add z to register R0
MOV R0,x -- Store R0 into x

Example 2, more three-address instructions:

a = b + c;
d = a + e;

Using the same template:

MOV b,R0
ADD c,R0
MOV R0,a
MOV a,R0
ADD e,R0
MOV R0,d
Unneccessary instruction(s).

Example 3, a three-address instruction:

a = a + 1;

Use a template:

MOV a,R0
ADD #1,R0 -- Add "immediate" constant 1 to R0
MOV R0,a

Better:

INC a

9.2 The target machine

Several types of target machines: From KP page 143, about register machines:

About register machines

9.3 Run-time storage management

Activation records, as in lecture 8.
Here they show the actual target-machine instructions to:

9.4 Basic blocks and flow graphs

Basic blocks are used for optimization. Example: Finding "heavily trafficed" parts of the program, and use registers for variables used there.

Terms:

From KP page 119:

Basic blocks

Terms:

Transformations on basic blocks:

Terms:

9.5 - 9.12

Skip the rest of chapter 9.


Thomas Padron-McCarthy (Thomas.Padron-McCarthy@tech.oru.se) February 26, 2003