34
1 Programming Languages (CS 550) Mini Language Compiler Jeremy R. Johnson

No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

1

Programming Languages (CS 550)

Mini Language Compiler

Jeremy R. Johnson

Page 2: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

2

Introduction

• Objective: To illustrate how to map Mini Language instructions to RAL instructions. To do this in a systematic way that illustrates how to write a compiler to translate Mini Language programs to RAL programs. Show simple optimizations that can be used to reduce the number of instructions.

• Algorithm– Construct code for expressions, assignments, if, and while.– Concatenate statements in stmt-list– Allocate temporaries as needed– Keep track of variables, constants, and temporaries in Symbol Table– Use symbolic instructions and fill in absolute addresses (linking) when

complete code has been constructed

Page 3: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

3

A Random Access Machine

Control Unit

AC

Program

1

2

3

4

5

6...

...

Memory

AC = accumulatorregister

Page 4: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

4

Instruction Set

• LDA X; Load the AC with the contents of memory address X• LDI X; Load the AC indirectly with the contents of address X• STA X; Store the contents of the AC at memory address X• STI X; Store the contents of the AC indirectly at address X• ADD X; Add the contents of address X to the contents of the AC• SUB X; Subtract the contents of address X from the AC• MUL X; Multiply the contents of address X to the contents of the AC• JMP X; Jump to the instruction labeled X• JMZ X; Jump to the instruction labeled X if the AC contains 0• JMN X; Jump to the instruction labeled X if the contents of the AC

; is negative• HLT ; Halt execution

Page 5: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

5

Memory Organization

Constants

Prog. Variables

Temp. Variables

• Num_Consts• Num_Vars• get_temp()• Num_Temps

Page 6: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

6

Symbolic Instructions

• Addresses and labels can be symbolic names• Symbolic names are mapped to actual addresses during linking

• Example:– LD x– ST z– ADD y– JMP L

• Linked code with (x=100, y =110, z = 105, L = 20)– LD 100– ST 105– ADD 110– JMP 20

Page 7: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

7

Symbol Table

• Map from identifiers → Symbol table entries

• Symbol table entries contain: address [may be unknown]

• Indicate whether entry is an constant, variable, temporary or label

Page 8: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

8

Expressions

expr → expr1 op expr2

Code1 ; result stored in t1

Code2 ; result stored in t2

LD t1 ; load result of exp1

OP t2 ; apply op to result of exp2 and result of exp1

ST t3 ; store result of exp1 op exp2

Page 9: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

9

Expressions

expr → NUMBER

; check to see if NUMBER in symbol table,; otherwise add to symbol table

LD NUMBER ; load constant from constant tableST tn ; next available temporary

Page 10: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

10

Expressions

expr → IDENT

; check to see if IDENT in symbol table; otherwise add to symbol table

LD IDENT ; load constant from constant tableST tn ; next available temporary

Page 11: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

11

Assignment

assign_stmt → IDENT = expr

; check to see if IDENT in symbol table; otherwise add to symbol table

CodeLD tST IDENT

Page 12: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

12

Conditional Statements

if_stmt → if expr then S1 else S2 fi

Codee ; result stored in tLD t ;JMN L1Code1

JMP L2L1: Code2L2:

Page 13: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

13

While Statements

while_stmt → while expr do S od

L1: Codee ; result stored in tLD t ;JMN L2CodeS

JMP L1L2:

Page 14: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

14

Statement List

stmt-list → stmt; stmt-list | stmt

code1

code2

…coden

Page 15: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

15

Example

n := 0 - 5; if n then i := n else i := 0 - n fi; fact := 1; while i do fact := fact * i; i := i - 1 od

Page 16: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

16

Example

n := 0 - 5;

LD ZEROST T1LD FIVEST T2LD T1SUB T2ST T3LD T3ST n

Page 17: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

17

Example

if n then i := n else i := 0 - n fi;

LD nST T4LD T4JMN L1LD nST T5LD T5ST iJMP L2

L1: LD ZEROST T6LD nST T7LD T6SUB T7ST T8LD T8ST i

L2:

Page 18: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

18

Example

fact := 1;

LD ONEST T9LD T9ST fact

Page 19: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

19

Example

while i do fact := fact * i; i := i - 1

od

L3: LD iST T10JMN L4LD factST T11LD iST T12LD T11MUL T12ST T13

LD T13ST factLD iST T14LD ONEST T15LD T14SUB T15ST T16LD T16ST iJMP L3

L4:

Page 20: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

20

Complete Example(concatenate and append HLT)

LD ZEROST T1LD FIVEST T2LD T1SUB T2ST T3LD T3ST nLD nST T4LD T4JMN L1LD nST T5LD T5ST iJMP L2

L3: LD iST T10JMN L4LD factST T11LD iST T12LD T11MUL T12ST T13

LD T13ST fact

LD iST T14LD ONEST T15LD T14SUB T15ST T16LD T16ST iJMP L3

L4: HLT

L1: LD ZEROST T6LD nST T7LD T6SUB T7ST T8LD T8ST i

L2: LD ONEST T9LD T9ST fact

Page 21: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

21

Symbol TableName Value Type addrZERO 0 const ?FIVE 5 const ?n u var ?T1 u temp ?T2 u temp ?T3 u temp ?T4 u temp ?T5 u temp ?i u var ?T6 u temp ?T7 u temp ?

T8 u temp ?ONE 1 const ?T9 u temp ?fact u var ?T10 u temp ?T11 u temp ?T12 u temp ?T13 u temp ?T14 u temp ?T15 u temp ?T16 u temp ?

Page 22: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

22

Symbol Table and Label Summary

Num_Vars = 3Num_Consts = 3Num_Temps = 16

ConstantsZERO -> addr 1FIVE -> addr 2One -> addr 3

Variablesn -> addr 4i -> addr 5fact -> addr 6

TemporariesT1 -> addr 7T2 -> addr 8

…T16 -> addr 22

L1 = 19L2 = 28L3 = 32L4 = 54

Page 23: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

23

Linked Example

LD 1ST 7LD 2ST 8LD 7SUB 8ST 9LD 9ST 4LD 4ST 10LD 10JMN 19LD 4ST 11LD 11ST 5JMP 28

L3: LD 5ST 16JMN 53LD 6ST 17LD 5ST 18LD 17MUL 18ST 19

LD 19ST 6

LD 5ST 20LD 3ST 21LD 20SUB 21ST 22LD 22ST 5JMP 32

L4: HLT

L1: LD 1ST 12LD 4ST 13LD 12SUB 13ST 14LD 14ST 5

L2: LD 3ST 15LD 15ST 6

Page 24: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

24

Optimizations

• Peephole optimization – Remove ST immediately followed by LD

• Commute (expr1,expr2) in expr → expr1 op expr2 to allow additional peephole optimizations

• Constant folding

• Common subexpression elimination

Page 25: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

25

Complete Example(after peephole optimization)

LD ZEROST T1LD FIVEST T2LD T1SUB T2ST T3LD T3ST nLD nST T4LD T4JMN L1LD nST T5LD T5ST iJMP L2

L3: LD iST T10JMN L4LD factST T11LD iST T12LD T11MUL T12ST T13

LD T13ST fact

LD iST T14LD ONEST T15LD T14SUB T15ST T16LD T16ST iJMP L3

L4: HLT

L1: LD ZEROST T6LD nST T7LD T6SUB T7ST T8LD T8ST i

L2: LD ONEST T9LD T9ST fact

Page 26: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

26

Complete Example(after peephole optimization)

LD ZEROST T1LD FIVEST T2LD T1SUB T2JMN L1LD nST iJMP L2

L3: LD iST T10JMN L4LD factST T11LD iST T12LD T11MUL T12ST fact

LD iST T14LD ONEST T15LD T14SUB T15ST i

JMP L3L4: HLT

L1: LD ZEROST T6LD nST T7LD T6SUB T7ST i

L2: LD ONEST fact

38 vs. 54 instructions

Page 27: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

27

Supporting Procedures

• Fully static environment – No recursion– Activation record

• Parameters• Local variables (keep count)• Return address (indirect jump needed)• Can be statically allocated

• Dynamic environment– Allow recursion– Call stack (dynamic allocation)– Indirect load and store needed

Page 28: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

28

Memory Organization

Constants

GlobalProg. Variables

GlobalTemp. Variables

Activation Records

Constants

GlobalProg. Variables

GlobalTemp. Variables

Call Stack

Page 29: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

29

Program Memory Organization

Procedures

P1

P2

P3

Main Program

Procedure Entry in Function Table

• Number of parameters• Number of local/temp variables• Starting address• Number of instructions

• Need to know starting address of main program

Page 30: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

30

Activation Record

Parameters

Local Variables

Temp. Variables

Return Address

Frame Pointer

Stack Pointer

For call stack

Page 31: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

31

Example: fact(n)

defineproc(n)i := n;fact := 1;

while i do fact := fact * i; i := i - 1

od;return := fact

end

Page 32: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

32

fact(n)

LD iST T7LD ONEST T8LD T7SUB T8ST T9LD T9ST iJMP L1

L2: LD factST T10LD T10ST return

L1: LD iST T3JMN L2LD factST T4LD iST T5LD T4MUL T5ST T6LD T6ST fact

LD nST T1LD T1ST iLD ONEST T2LD T2ST fact

Page 33: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

33

Activation Recordni

factT1T2T3T4T5T6T7T8T9

T10return

ret. addr.

FP

SP

Accessing ARLD n ⇔ LDI FPST n ⇔ STI FP

LD i ⇔ LD FPADD ONEST FPBLDI FPB

⇔ LDO FP[1]ST i ⇔ STO FP[1]LD Tj ⇔LDO FP[j+Num_Param+Num_Vars]

Page 34: No Slide Titlejjohnson/2006-07/spring/cs550/lectures/compiler.pdf · 4 Instruction Set • LDA X; Load the AC with the contents of memory address X • LDI X; Load the AC indirectly

34

Calling SequenceInitiate call1. Create activation record

1. Update FP and SP

2. Store parameters in activation record3. Store return address (RA)4. Jump to starting address of procedure code

1. Introduce call instruction (can place RA relative to SP)

Return from call1. Store return value in activation record (when return is assigned)2. Jump to RA

1. Introduce ret instruction (jmp indirect)

3. Retrieve return value from activation record4. Update FP and SP