Download Mini Language Compiler - Programming Language Concepts | CS 360 and more Assignments Programming Languages in PDF only on Docsity!
Programming Languages
(CS 360)
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 T6LD nST T7LD T6SUB T7ST T8LD T8ST 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 T13ST factLD iST T14LD ONEST T15LD T14SUB T15ST T16LD T16ST iJMP 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 T10JMN L4JMZ L4LD factST T11LD iST T12LD T11MUL T12ST T LD T13ST fact
LD iST T14LD ONEST T15LD T14SUB T15ST T16LD T16ST iJMP L
L4: HLT
L1: LD ZERO
ST T6LD nST T7LD T6SUB T7ST T8LD T8ST i
L2: LD ONE
ST T9 LD T
ST fact