Kodgenerering. Maskinberoende optimering.

The course Compilers and interpreters | Lectures: 1 2 3 4 5 6 7 8 9 10 11 12


These lecture notes are my own notes that I made in order to use during the lecture, and it is approximately what I will be saying in the lecture. These notes may be brief, incomplete and hard to understand, not to mention in the wrong language, and they do not replace the lecture or the book, but there is no reason to keep them secret if someone wants to look at them.

Idag: Kodgenerering. Maskinberoende optimering.

Kodgenerering

Många av de tekniker som behandlas i kursen, t ex för syntaxspecifikation och parsning, är användbara även utanför rena kompilatorer. Just kodgenerering är dock något som man förmodligen inte kommer i kontakt med, annat än som kompilatorutvecklare.

Kodgenerering är kanske inte heller en del av kompilatorn som man har så stor nytta av att förstå i sitt arbete som programmerare.

Därför tar vi bara med så mycket om kodgenerering som vi hinner.

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

8.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

8.2 The Target Language

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

About register machines

8.3 Addresses in the Target Code

(ASU-86 9.3 Run-time storage management)

Activation records, as in föreläsning 8.
Here they show the actual target-machine instructions to:

8.4-8.5 Basic blocks, flow graphs, optimization

Tas upp i optimeringsföreläsningen

8.6-8.13

Skip the rest of chapter 8.

The course Compilers and interpreters | Lectures: 1 2 3 4 5 6 7 8 9 10 11 12


Thomas Padron-McCarthy (thomas.padron-mccarthy@oru.se), August 29, 2022