Docsity

Mini Language Compiler: Mapping Instructions to RAL and Creating Symbol Table - Prof. Jere, Assignments of Programming Languages

Partial preview of the text

Download Mini Language Compiler: Mapping Instructions to RAL and Creating Symbol Table - Prof. Jere and more Assignments Programming Languages in PDF only on Docsity!

Programming Languages

(CS 550)

Mini Language Compiler

Jeremy R. Johnson

Introduction

Objective: To illustrate how to map Mini Languageinstructions to RAL instructions. To do this in a systematicway that illustrates how to write a compiler to translate MiniLanguage programs to RAL programs. Show simpleoptimizations that can be used to reduce the number ofinstructions.

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

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 isnegative ™

HLT

; Halt execution

Memory Organization

Constants

Prog. Variables Temp. Variables

Num_Consts

Num_Vars

get_temp()

Num_Temps

Symbol Table

Map from identifiers

Symbol table

entries

Symbol table entries contain: address [maybe unknown]

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

8

Expressions

expr

expr

1

op expr

2

Code

1

; result stored in t

1

Code

2

; result stored in t

2

LD t

1

; load result of exp

1

OP t

2

; apply op to result of exp

2

and result of exp

ST t

3

; store result of exp

1

op exp

2

Expressions

expr

IDENT

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

; load constant from constant table

ST t

n

; next available temporary

Assignment

assign_stmt

IDENT = expr

; check to see if IDENT in symbol table ; otherwise add to symbol table Code LD t ST IDENT

While Statements

while_stmt

while expr

do S

od

while

expr

> 0 then S

od

L1: Code

e

; result stored in t

LD t

JMN L

; jump if t

JMZ L2 Code

S

JMP L

L2:

Statement List

stmt-list

stmt; stmt-list | stmt

code

code

… code

n

Example

LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 LD T3 ST n

n := 0 - 5

Example

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

LD n ST T4 LD T4 JMN L1 JMZ L1 LD n ST T5 LD T5 ST i JMP L

L1: LD ZERO

ST T6 LD n ST T7 LD T6 SUB T7 ST T8 LD T8 ST i

L2:

Example

while i do

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

od L3: LD i

ST T10 LD T10 JMN L4 JMZ L4 LD fact ST T11 LD i ST T12 LD T11 MUL T12 ST T

LD T13 ST fact LD i ST T14 LD ONE ST T15 LD T14 SUB T15 ST T16 LD T16 ST i JMP L

L4:

Complete Example

LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 LD T3 ST n LD n ST T4 LD T4 JMN L1 JMZ L1 LD n ST T5 LD T5 ST i JMP L L3: LD i ST T10 JMN L4 JMZ L4 LD fact ST T11 LD i ST T12 LD T11 MUL T12 ST T13 LD T13 ST fact LD i ST T14 LD ONE ST T15 LD T14 SUB T15 ST T16 LD T16 ST i JMP L L4: HLT L1: LD ZERO ST T6 LD n ST T7 LD T6 SUB T7 ST T8 LD T8 ST i L2: LD ONE ST T9 LD T9 ST fact