Compiler Development (CMPSC 401) Three Address Code Janyl - - PowerPoint PPT Presentation

compiler development cmpsc 401
SMART_READER_LITE
LIVE PREVIEW

Compiler Development (CMPSC 401) Three Address Code Janyl - - PowerPoint PPT Presentation

Compiler Development (CMPSC 401) Three Address Code Janyl Jumadinova April 2, 2019 Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 1 / 30 Three Address Code (TAC) High-level assembly where each operation has at most three


slide-1
SLIDE 1

Compiler Development (CMPSC 401)

Three Address Code Janyl Jumadinova April 2, 2019

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 1 / 30

slide-2
SLIDE 2

Three Address Code (TAC)

High-level assembly where each operation has at most three operands (e.g., an add instruction has three operands: two for each argument and one for the result).

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 2 / 30

slide-3
SLIDE 3

Three Address Code (TAC)

High-level assembly where each operation has at most three operands (e.g., an add instruction has three operands: two for each argument and one for the result). The operands are called addresses and can be held in a virtual register. The compiler back-end will convert TAC instructions and registers into specific machine instructions and hardware registers.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 2 / 30

slide-4
SLIDE 4

Temporary Variables

The “three” in “three-address code” refers to the number of operands in any instruction. Evaluating an expression with more than three subexpressions requires the introduction of temporary variables.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 3 / 30

slide-5
SLIDE 5

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 4 / 30

slide-6
SLIDE 6

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 5 / 30

slide-7
SLIDE 7

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 6 / 30

slide-8
SLIDE 8

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 7 / 30

slide-9
SLIDE 9

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 8 / 30

slide-10
SLIDE 10

TAC Sample

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 9 / 30

slide-11
SLIDE 11

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-12
SLIDE 12

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-13
SLIDE 13

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-14
SLIDE 14

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3; var1 = constant op var2;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-15
SLIDE 15

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3; var1 = constant op var2; var1 = var2 op constant;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-16
SLIDE 16

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3; var1 = constant op var2; var1 = var2 op constant; var = constant1 op constant2; Permitted operators are + , - , * , / , % .

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-17
SLIDE 17

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3; var1 = constant op var2; var1 = var2 op constant; var = constant1 op constant2; Permitted operators are + , - , * , / , % . How would you compile y = -x; ?

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-18
SLIDE 18

Simple TAC Instructions

Variable assignment allows assignments of the form : var = constant; var1 = var2; var1 = var2 op var3; var1 = constant op var2; var1 = var2 op constant; var = constant1 op constant2; Permitted operators are + , - , * , / , % . How would you compile y = -x; ? y = 0 - x; y = -1 * x;

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

slide-19
SLIDE 19

TAC with bools

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 11 / 30

slide-20
SLIDE 20

TAC with bools

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 12 / 30

slide-21
SLIDE 21

TAC with bools

Boolean variables are represented as integers that have zero or nonzero values. In addition to the arithmetic operator, TAC supports: <, ==, ||, and &&.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 13 / 30

slide-22
SLIDE 22

TAC with bools

Boolean variables are represented as integers that have zero or nonzero values. In addition to the arithmetic operator, TAC supports: <, ==, ||, and &&. How might you compile b = (x ≤ y)?

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 13 / 30

slide-23
SLIDE 23

Control Flow Statements

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 14 / 30

slide-24
SLIDE 24

Control Flow Statements

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 15 / 30

slide-25
SLIDE 25

Labels

TAC allows for named labels indicating particular points in the code that can be jumped to. There are two control flow instructions:

1

Goto label;

2

IfZ value Goto label;

Note that IfZ is always paired with Goto.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 16 / 30

slide-26
SLIDE 26

Control Flow Statements

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 17 / 30

slide-27
SLIDE 27

Control Flow Statements

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 18 / 30

slide-28
SLIDE 28

Compiling Functions

Decaf functions consist of four pieces:

1 A label identifying the start of the function. 2 A BeginFunc N; instruction reserving N bytes of space for locals and

temporaries.

3 The body of the function. 4 An EndFunc; instruction marking the end of the function.

– When reached, cleans up stack frame and returns.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 19 / 30

slide-29
SLIDE 29

A Complete Decaf Program

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 20 / 30

slide-30
SLIDE 30

A Logical Decaf Stack Frame

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 21 / 30

slide-31
SLIDE 31

A Logical Decaf Stack Frame

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 22 / 30

slide-32
SLIDE 32

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 23 / 30

slide-33
SLIDE 33

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 24 / 30

slide-34
SLIDE 34

Stack Management in TAC

The BeginFunc N; instruction only needs to reserve room for local variables and temporaries. The EndFunc; instruction reclaims the room allocated with BeginFunc N; A single parameter is pushed onto the stack by the caller using the PushParam var instruction. Space for parameters is reclaimed by the caller using the PopParams N; instruction. N is measured in bytes, not number of arguments.

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 25 / 30

slide-35
SLIDE 35

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 26 / 30

slide-36
SLIDE 36

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 27 / 30

slide-37
SLIDE 37

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 28 / 30

slide-38
SLIDE 38

Compiling Function Calls

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 29 / 30

slide-39
SLIDE 39

Instruction Formats

Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 30 / 30