Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Translation to Target Language- Imperative Language Constructs -
Lecture Compilers SS 2009
Dr.-Ing. Ina Schaefer
Software Technology GroupTU Kaiserslautern
Ina Schaefer Translation to Target Language 1
Content of Lecture
1. Introduction: Overview and Motivation2. Syntax- and Type Analysis
2.1 Lexical Analysis2.2 Context-Free Syntax Analysis2.3 Context-Dependent Syntax Analysis
3. Translation to Target Language3.1 Translation of Imperative Language Constructs3.2 Translation of Object-Oriented Language Constructs
4. Selected Aspects of Compilers4.1 Intermediate Languages4.2 Optimization4.3 Command Selection4.4 Register Allocation4.5 Code Generation
5. Garbage Collection6. XML Processing (DOM, SAX, XSLT)
Ina Schaefer Translation to Target Language 2
Outline
1. Language Constructs of Procedural Languages
2. Assembly and Machine Languages
3. Translation of Variables and Data Types
4. Translation of Expressions
5. Translation of Statements
6. Translation of Procedures and Local Objects
Ina Schaefer Translation to Target Language 3
Translation to Target Language
Focus:• Differences between source languages and target
languages/target machines• Most important translation techniques for different programing
paradigms (procedural/object-oriented)
Ina Schaefer Translation to Target Language 4
Translation to Target Language (2)
Educational Objectives:• Overview of imperative and procedural language constructs• Typical language constructs of assembler languages• Translation techniques for procedural language constructs• Translation of object-oriented language constructs
Ina Schaefer Translation to Target Language 5
Translation of Procedural Languages
• Language constructs of procedural programing languages• Language constructs of assembly languages• Translation of variables and data types• Translation of expressions• Translation of statements (control structures)• Translation of procedures
Ina Schaefer Translation to Target Language 6
Language Constructs of Procedural Languages
Procedural Languages
From a conceptional and semantical view point, procedural languageshave the following constructs:
• Domains with operations (often typed)! pre-defined: int, boolean, ...! user-defined: records, classes, ...! implicitly defined: field types, address types, function types
• Variables! simple and compound types! global, local, statically/dynamically allocated! define memory state
• Expressions! computation of values with implicit intermediate results! possibly in combination with execution control and state
modification
Ina Schaefer Translation to Target Language 7
Language Constructs of Procedural Languages
Procedural Languages (2)
• Statements! simple and combined statements! define execution control and state modification
• Procedures! abstraction of parametrized statements! may be recursive! may be nested
Modules usually do not have a semantic meaning and are onlyrelevant for translation in name analysis and for binding and loading.
Ina Schaefer Translation to Target Language 8
Language Constructs of Procedural Languages
Nested ProceduresExample from [Wilhelm, Maurer; Fig. 2.9]
Übersetzung geschachtelter ProzedurenGeschachtelte/lokale Prozeduren werden z.B.
von Pascal und Ada unterstützt
Beispiel: (geschachtelte Prozeduren)
von Pascal und Ada unterstützt.
proc P(a)
var b
Abb. 2.9
)
var b
var c
proc Q
var a
proc R
elm
/Maure
r,var b
begin
... b ...
... a ...
c
mt aus W
ilhe... c ...
end
begin
... a ...
... b ...
spie
l sta
mm... call Q ...
end
proc S
var a
begin
(das B
eisbegin
... a ...
... call Q ...
end
begin
12.06.2007 237© A. Poetzsch-Heffter, TU Kaiserslautern
... a ...
... call Q ...
end
Ina Schaefer Translation to Target Language 9
Assembly and Machine Languages
Assembly and Machine Languages
Assembly languages have the following language constructs:• Finite sequences of bits of various length: byte, word, halfword, ...• Global memory
! register, flags (addressing by name)! indexed, mostly word addressed main memory
• Instructions! load, store! arithmetic and boolean operations! execution control (jumps, procedures)! simple, not combined statements! possibly complex addressing of operands
• Initialization instructions
Ina Schaefer Translation to Target Language 10
Assembly and Machine Languages
The MIPS Assembler
MIPS - Microprocessor without interlocked pipeline stages
• RISC Architecture, originally 32 bit (since 1991 64bit)• developed by John Hennessy (Stanford) starting 1981• MARS Simulatorhttp://courses.missouristate.edu/KenVollmar/MARS/
Ina Schaefer Translation to Target Language 11
Assembly and Machine Languages
MIPS Architecture
• Arithmetic-Logic Unit (ALU)• Floating-Point Unit (FPU)• 32 Registers (inkl. stack pointer, frame pointer, global pointer,
return address)• Main memory, 230 memory words (4 byte)• 5-stage pipeline
Ina Schaefer Translation to Target Language 12
Assembly and Machine Languages
MIPS Commands
• Arithemic
add add $s1, $s2, $s3 $s1 = $s2 + $s3subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3add immediate addi $s1, $s2, c $s1 = $s2 + cmultiply mul $s1, $s2, $s3 $s1 = $s2 * $s3
(lower 32 bits in $s1)
Ina Schaefer Translation to Target Language 13
Assembly and Machine Languages
MIPS Commands (2)
• Data Transfer
load word lw $s1, c($s2) $s1 = Memory[$s2 + c]store word sw $s1, c($s2) Memory[$s2 + c] = $s1load immediate li, $s1, c $s1 = cload half lh $s1, c($s2) $s1 = Memory[$s2 + c]store half sh $s1, c($s2) Memory[$s2 + c] = $s1load byte lb $s1, c($s2) $s1 = Memory[$s2 + c]store byte sb $s1, c($s2) Memory[$s2 + c] = $s1
Ina Schaefer Translation to Target Language 14
Assembly and Machine Languages
MIPS Commands (3)
• Logical
and and $s1, $s2, $s3 $s1 = $s2 & $s3or or $s1, $s2, $s3 $s1 = $s2 | $s3nor nor $s1, $s2, $s3 $s1 = ¬ ( $s2 | $s3 )and immediate andi $s1, $s2, c $s1 = $s2 & cor immediate ori $s1, $s2, c $s1 = $s2 | cshift left logical sll $s1, $s2, c $s1 = $s2 « cshift right logical sll $s1, $s2, c $s1 = $s2 » c
Ina Schaefer Translation to Target Language 15
Assembly and Machine Languages
MIPS Commands (4)
• Conditional Branches
branch on equal beq $s1, $s2, label if ($s1 == $s2)goto label
branch on not equal bne $s1, $s2, label if ($s1 != $s2)goto label
set on less than slt $s1, $s2, $s3 if ($s2 < $s3)$s1 := 1 else $s1 := 0
set o.l.t. immediate slti $s1, $s2, c if ($s2 < c)$s1 := 1 else $s1 := 0
• Unconditional Branches
jump j label goto labeljump register jr $ra goto $rajump and link jal label $ra = PC + 4; goto label
Ina Schaefer Translation to Target Language 16
Assembly and Machine Languages
Adressing in MIPS
• Immediate: Operand is a constant, e.g. 25
• Register: Operand is a register, e.g. $s2
• Base or Displacement Addressing: Operand is a memorylocation whose address is the sum of the register and a constant,e.g. 8($sp)
• PC relative: Address is the sum of PC and a constant
• Pseudodirect Addressing: Jump address is the 26 bit of theinstruction with the upper bits of the PC
Ina Schaefer Translation to Target Language 17
Assembly and Machine Languages
MIPS Registers
• $zero: the constant 0
• $at: assembler temporary
• $v0, $v1: values for function results and expression evaluation
• $a0 - $a3: arguments
• $t0 - $t9: temporaries
• $s0 - $s7: saved temporaries
• $k0, $k1: reserved for OS kernel
• $gp: global pointer
• $sp: stack pointer
• $fp: frame pointer
• $ra: return address
Ina Schaefer Translation to Target Language 18
Assembly and Machine Languages
Syscalls for MARS/SPIM Simulators
How to use System Calls:• load service number into register $v0• load argument values, if any into $a0, $a1, $a2• issue call instruction syscall• retrieve return values, if any
Example:
li $v0, 1 # print integeradd $a0, $t0, $zero # load value into $a0syscall
Ina Schaefer Translation to Target Language 19
Assembly and Machine Languages
List of System Services
Service Code in $v0 Arguments
print integer 1 $a0 = integer to printprint string 4 $a0 = address of
null-terminated string to printexit (terminate execution) 10print character 11 $a0 = character to printexit2 (terminate with value) 17 $a0 = termination result
Ina Schaefer Translation to Target Language 20
Assembly and Machine Languages
Example: Translation to MIPSThe example illustrates the MIPS assembler and typical translation tasks.Code quality is not considered.
Source Code in C:
Beispiel: (einer Übersetzung nach MI)
Das Beispiel soll zum einen die MI-Assemblersprachep pdemonstrieren, zum anderen aber auch Übersetzungs-probleme veranschaulichen. Auf die Qualität des Zielprogramms wurde kein Wert gelegt.
char a[3], b[3];
int i;
char res;
Quellprogramm in C:
char res;
void main() {
i:= 2;
res := 1;res : 1;
while( -1 < i ) {
if( res ) {
res = (a[i]==b[i]);res (a[i] b[i]);
i = i-1;
} else {
i = i-1;
}}
}
}
Den Prozeduraufruf von main vernachlässigen
12.06.2007 202© A. Poetzsch-Heffter, TU Kaiserslautern
Den Prozeduraufruf von main vernachlässigenwir bei diesem einführenden Beispiel.
Ina Schaefer Translation to Target Language 21
Assembly and Machine Languages
MIPS Program
# sp + 0 : 1# sp + 4 : res# sp + 5 : base address of a# sp + 8 : base address of b
addi $sp, $sp, -12 # make space for the variablesli $t1, 2sw $t1, 0($sp) # set i to 2li $t1, 1sb $t1, 4($sp) # set res at sp +4
loop:lw $t2, 0($sp) # load i into $t2li $t3, -1 # load -1 into $t3slt $t0, $t3, $t2 # $t3 < $t2 ?beq $t0, $zero, exit # if i < -1 goto exitlw $t1, 4($sp) # load res from stackbeq $t1, $zero, elseif # if res == 0 goto else if
Ina Schaefer Translation to Target Language 22
Assembly and Machine Languages
MIPS Program (2)
add $t4, $sp, 5 # base address of array aadd $t4, $t4, $t2 # add offset/ array indexlb $t0, 0($t4) # load a[i]add $t4, $sp, 8 # base address of array badd $t4, $t4, $t2 # add offset/ array indexlb $t1, 0($t4) # load b[i]beq $t0, $t1, equal # if a[i] == b[i]sb $zero, 4($sp) # set res to 0j after
equal:add $t3, $zero, 1sb $t3, 4($sp) # set res
after:subi $t2, $t2, 1 # i = i-1sw $t2, 0($sp) # store i to $sp +4j afterif # goto end of if statement
Ina Schaefer Translation to Target Language 23
Assembly and Machine Languages
MIPS Program (3)
elseif:subi $t2, $t2, 1 # i = i-1sw $t2, 0($sp) # store i to $sp +4
afterif:j loop # return to loop
exit:addi $sp, $sp, 12 # reset stack pointerli $a0, 1 # terminated successfullyli $v0, 17syscall
Ina Schaefer Translation to Target Language 24
Assembly and Machine Languages
Translation to MIPS
Remarks:The example illustrates typical translation tasks:
• Translation of data types, memory management, addressing• Translation of Expressions, Management of intermediate results,
mapping of operations of the source language to operations of thetarget language
• Translation of statements by implementation with jumps• Bad code quality with simple systematic approach
Ina Schaefer Translation to Target Language 25
Assembly and Machine Languages
Translation Process
Concrete Syntax
SL
Concrete SyntaxMIPS
AST SL
AST MIPS
Lexical and Context-Free
Analysis
Context-Dependent
Analysis
Translator Code Generator
Ina Schaefer Translation to Target Language 26
Assembly and Machine Languages
MIPS Abstract Syntax
Prog * Code
Code =ADD (reg reg reg) | ADDI (reg reg const) | SUB (reg reg reg) |MUL (reg, reg, reg) |AND (reg reg reg) | OR (reg reg reg) | NOR (reg reg reg) |ANDI (reg reg const) | ORI (reg reg const) |SLL (reg reg const) | SRL (reg reg const) |BEQ (reg reg label) | BNE (reg reg label) |SLT (reg reg reg) | SLTI (reg reg const) |JUMP (label) | JR (reg) | JAL (label) |STORE (reg const reg) | LOAD (reg const reg) | LOADI (reg const) |STOREB (reg const reg) | LOADB (reg const reg) |LABEL (name)
Ina Schaefer Translation to Target Language 27
Translation of Variables and Data Types
Translation of Variables and Data Types
Compiler
Programing Language
Assembly Language
named variablescomplex types
addresses of memory regionsindex and offset computation
Ina Schaefer Translation to Target Language 28
Translation of Variables and Data Types
Translation of Variables and Data Types (2)
The translation of variables and data types comprises:
• handling of basis data types• conversion of data types (e.g. int ! float)• memory organisation• translation of arrays• translation of records and classes• implementation of dynamic objects
Ina Schaefer Translation to Target Language 29
Translation of Variables and Data Types
Basis Data Types
Often, there is good support of basis data types of source language onthe target machine:
• int, long ! 4 byte word with integer arithmetic• float, double ! accordingly
Potentially, data types have to be encoded:• boolean ! 1 byte or 4 byte words
Problem: If target machine does not comply to requirements of sourcelanguage, e.g.
• floating point arithmetic is not handled according to IEEE standard• overflows are not dealt with correctly
(cmp. Java FP-strict expressions)• operations for conversion are missing on target machine
Ina Schaefer Translation to Target Language 30
Translation of Variables and Data Types
Memory LayoutThe conceptional memory layout of most imperative programing languagesand target machines is similar. (Details depend on OS and machine)
dynamic variables, objects, ...
intermediate results, procedure-local values,objects with restricted scope
OS kernel
global values
low addresses
highaddresses
global, static variables, constants, ...
heap
stack
program
Ina Schaefer Translation to Target Language 31
Translation of Variables and Data Types
Translation of Arrays
Efficient translation of Arrays is important for many tasks.
One-dimensional static arrays
• Allocate memory in the region for global values (starting at $gp)• Address computation with base address of array, index of array
element and size of element type
Consider the array declaration T tarray[57]:
• $gp contains the base adress for the global memory region• Let Rrel contain the relative address of the array tarray in the
global memory region• Let Ri contain the index i of the array component
If k = sizeof (T ), then the address of tarray[i] is $gp + Rrel + k " Ri .
Ina Schaefer Translation to Target Language 32
Translation of Variables and Data Types
Translation of Arrays (2)
Computation in MIPS
LI $ti, kMUL $ti, Ri, $tiADD $ti, R_rel, $tiADD $ti, $gp, $tiLOAD $ti, 0, $ti
Ina Schaefer Translation to Target Language 33
Translation of Variables and Data Types
More Translation of Arrays
Multi-dimensional static arrays
Consider as example the Pascal declaration
var a:array[-5..5][1..9] of integer;
which corresponds to 99 integer variables:
a[-5, 1] ... a[-5,9]...a[5,1] ... a[5,9]
Matrix is stored in rows in memory. Storing rows is more efficient thanstoring columns as second index is often incremented in inner loops.
Ina Schaefer Translation to Target Language 34
Translation of Variables and Data Types
Further Translation of Arrays(2)
Translation of Access to a[E1,E2]:
Assume results of evaluating E1 and E2 are stored in $t1 and $t2.
As a is a static array, we know the dimensions at compile time.
a[$t1,$t2] is the r-th component of a linear array with
r = ($t1# (#5) " ((9# 1) + 1) + ($t2# 1)= 9 " $t1 + 45 + $t2# 1= 9 " $t1 + $t2 + 44
Result: Store the address of the 44-th component as base address ofthe array in symbol table. Then it suffices to add 9 " $t1 + $t2 to baseaddress.
Ina Schaefer Translation to Target Language 35
Translation of Variables and Data Types
Further Translation of Arrays(2)
Code example for Access to a[E1,E2]:
[Code for E1 -> $t1][Code for E2 -> $t2]LOADI ($t3, 9)MUL ($t1, $t1, $t3)ADD ($t1, $t1, $t2)LOADI ($t2, 4)MUL ($t1, $t1, $t2)ADDI ($t1, $t1, relA)ADD ($t1, $t1, $gp)LOAD ($t1, 0, $t1)
where relA = offset(a) + 44
Ina Schaefer Translation to Target Language 36
Translation of Variables and Data Types
General Translation of Arrays
General Array Declaration of Dimension k
var a: array [u1..o1], ...., [uk..uk] of T;
Storing Rows yields the following adress for accessing a[R1, ..., Rk]:
r = (R1# u1) " size(array [u2..o2, ..., uk ..ok ] of T )+ (R2# u2) " size(array [u3..o3, ..., uk ..ok ] of T )+ . . .+ (Rk # uk) " size(T )
Ina Schaefer Translation to Target Language 37
Translation of Variables and Data Types
General Translation of Arrays (2)For i = 1, . . . , k # 1, it holds that
size(i) := size(array [u{i + 1}..o{i + 1}, ..., uk ..ok ] of T )
size(k) = size(t)
This impliessize(i # 1) = size(i) " (oi # ui + 1)
Simplification yields:
r =k!
i=1
Ri " size(i)#k!
i=1
ui " size(i)
At runtime, only first summand has to be computed for which code hasto be generated.
Ina Schaefer Translation to Target Language 38
Translation of Variables and Data Types
Code Generation for Array Access
Abstract Syntax of Source Language:Einfache Codeerzeugung für Feldzugriff:
Beispiel:
ArrayAccess ( UsedId uid, IndexExps ies )
UsedId ( Ident id )
IndexExps = IndexExpElem | IndexExp
IndexExpElem ( IndexExp ie, IndexExps ies )p ( p , p )
IndexExp ( ... )
Symboltabelle
Register, in dem Ergebnis steht ( Reg(Ri) )
Adressierung des Feldelements
Code für den Unterbaum
Liste der Größen zu jeder Felddimension
Relativadresse zur Adressierung eines Feldes a:
relA = offset(a) - !"ui * size(i) k
I=1
lkupRA: Ident x SymTab ! Adresse
lk SZL Id t S T b ! I tLi t
I=1
lkupSZL: Ident x SymTab ! IntList
Zur Konkatenation von Codelisten benutzen wir “+“,
die Erzeugung einer einelementigen Liste aus einem
El t h ib i l [ ]
12.06.2007 211© A. Poetzsch-Heffter, TU Kaiserslautern
Element e schreiben wir als [e] .
Ina Schaefer Translation to Target Language 39
Translation of Variables and Data Types
Code Generation for Array Access (2)
Attribution:
Einfache Codeerzeugung für Feldzugriff:
Beispiel:
ArrayAccess ( UsedId uid, IndexExps ies )
UsedId ( Ident id )
IndexExps = IndexExpElem | IndexExp
IndexExpElem ( IndexExp ie, IndexExps ies )p ( p , p )
IndexExp ( ... )
Symboltabelle
Register, in dem Ergebnis steht ( Reg(Ri) )
Adressierung des Feldelements
Code für den Unterbaum
Liste der Größen zu jeder Felddimension
Relativadresse zur Adressierung eines Feldes a:
relA = offset(a) - !"ui * size(i) k
I=1
lkupRA: Ident x SymTab ! Adresse
lk SZL Id t S T b ! I tLi t
I=1
lkupSZL: Ident x SymTab ! IntList
Zur Konkatenation von Codelisten benutzen wir “+“,
die Erzeugung einer einelementigen Liste aus einem
El t h ib i l [ ]
12.06.2007 211© A. Poetzsch-Heffter, TU Kaiserslautern
Element e schreiben wir als [e] .
Symbol Table
Result Register Ri
Address of Array Element
Code for Subtree
List of Sizes for each Array Dimension
Relative Address for Array a
Ina Schaefer Translation to Target Language 40
Translation of Variables and Data Types
Code Generation for Array Access (3)
Operations for Attribution:• lkupRA: Ident $ SymTab ! Address• lkupSZL: Ident $ SymTab ! IntList• + : List Concatenation, for an element e, [e] is the list containing
only e.
In the following, the SymTab attribute is only explicitly given where it isrequired.
Ina Schaefer Translation to Target Language 41
Translation of Variables and Data Types
Code Generation for Array Access (4)
Das Symboltabellenattribut ist nur angegeben, wo es
gebraucht wird. R0 enthält die Basisadresse des
Speicherbereichs, in dem das Feld gespeichert ist.
ArrayAccess
UsedId IndexExps
Bdispx(Reg(R0),_,_)
UsedId IndexExps
lkupRA(_,_) lkupSZL(_,_)
IndexExpElem
Ident
IndexExpElem
_ +
rest(_) first(_)
_ +
[ Mult2(W,Imm(_),_) ] +
[ Add2(W,_,_) ]
12.06.2007 212© A. Poetzsch-Heffter, TU Kaiserslautern
IndexExps
IndexExp
IndexExp
ADD(Ri,Ri, $gp)ADD(Ri, Ri,RA)
RiRA
Ina Schaefer Translation to Target Language 42
Translation of Variables and Data Types
Code Generation for Array Access (5)Um die Attributierungsbilder übersichtlicher zu gestalten, können Bezeichner für Attributwertebenutzt werden:
IndexExpElem
rest(_) first(_)
CL + CR +[ Mult2(W,Imm(_),RL) ] +[ Add2(W,RL,RR) ]
IndexExpsIndexExpRL CL RR CR
Zur Laufzeit braucht wieder nur der erste Summandberechnet werden. Dafür muss also Code generiertwerden. Bei der schrittweisen Berechung kann aucheine Bereichsprüfung für das Feld vorgenommen werden.
Bemerkungen:
• Bei der Berechnung von Feldindizes gibt es häufigeine großes Potential für Optimierungen.
• Für die Übersetzung dynamischer Felder muss
die Adressierung geeignet verallgemeinert werden
12.06.2007 213© A. Poetzsch-Heffter, TU Kaiserslautern
die Adressierung geeignet verallgemeinert werden.(siehe z.B. Wilhelm/Maurer, Abschnitt 2.6.2).
CL +CR +[LOADI (RT, FI)] +[MUL (RL, RL, RT) ] +[ADD (RR, RR, RL) ]
FI
During stepwise computation also array bounds can be checked.
Ina Schaefer Translation to Target Language 43
Translation of Variables and Data Types
Array Access
Remarks:• Computation of array indices offers great potential for
optimizations.• For translation of dynamic arrays, addressing has to be
generalized appropriately. (cf. Wilhelm/Maurer, Sect. 2.6.2)
Ina Schaefer Translation to Target Language 44
Translation of Variables and Data Types
Translation of Records
Translation of Records is similar to translation of arrays:• Determine size and memory layout• Compute adresses for selection of record components and pointer
dereferencing• Translation of record operations, e.g. assignments to record
components
Recommended Reading: Wilhelm, Maurer, Section 2.6.2
Ina Schaefer Translation to Target Language 45
Translation of Variables and Data Types
Implementation of Dynamic Objects
Dynamic objects = dynamically allocated variables and objects insense of OO programing
Dynamic objects are stored on the heap:• number of dynamic objects is not know at compile time, objects
are created at runtime• dynamic objects have a designated lifetime which disallows
handling with stackMemory representation and addressing of components is similar tostatic records.
Ina Schaefer Translation to Target Language 46
Translation of Variables and Data Types
Implementation of Dynamic Objects (2)
Example:
Implementierung dynamischer Objekte
Dynamische Objekte werden hier als Sammelbegriff fürDynamische Objekte werden hier als Sammelbegriff fürdynamisch allozierte Variable und Objekte im Sinne der OO-Programmierung verwendet.
Dynamische Objekte werden auf der Halde verwaltet:Dynamische Objekte werden auf der Halde verwaltet:
• Ihre Anzahl ist im Allg. zur Übersetzungszeit nicht
bekannt. Deshalb werden sie erst zur Laufzeit erzeugt.
• Sie haben eine Lebensdauer die eine kellerartigeSie haben eine Lebensdauer, die eine kellerartige
Behandlung im Allg. nicht zulässt.
Beispiel: (dynamische Objekte)Beispiel: (dynamische Objekte)
typedef struct listelem {
int head;
struct listelem* tail; }* list;
# define listelemSIZE sizeof(struct listelem{
int h; struct listelem* t;})
list append( int i list l ) {list append( int i, list l ) {
list lvar = (list) calloc(1,listelemSIZE);
lvar->head = i;
lvar->tail = l;
return lvar;
12.06.2007 215© A. Poetzsch-Heffter, TU Kaiserslautern
}
...
Ina Schaefer Translation to Target Language 47
Translation of Variables and Data Types
Dynamic Memory Management
Dynamic memory management• is handled by runtime environment• can be supported by compiler• can partially be handled by user program
Runtime environment provides operations for dynamic memorymanagement:
• for the programmer, e.g. in C malloc, calloc, realloc, free• for the compiler as in Pascal, Java, Ada• no memory deallocation by programer possible, but garbage
collection by runtime environment e.g. in Java
Ina Schaefer Translation to Target Language 48
Translation of Variables and Data Types
Dynamic Memory Management (2)
General Problem: Provide memory blocks of different sizes from alinear memory and reuse memory after it has been freed
Simple memory management by linear list of free memory areas
Structure of free memory area of variable length:
user datasize
header
free usedused free used
freelist
Ina Schaefer Translation to Target Language 49
Translation of Variables and Data Types
Dynamic Memory Management (3)
List of free memory areas:user datasize
header
free usedused free used
freelist
Procedure to allocate and deallocate memory:• Allocate memory
! Search memory area B of appropriate size! Update references:
• If area has exactly required size, remove it from list.• Else update header of area, create header for rest of free memory
and add this area instead of the old area to list.
Ina Schaefer Translation to Target Language 50
Translation of Variables and Data Types
Dynamic Memory Management (4)
! Return pointer to memory cell after header (size information has tobe kept.)
! If no memory area of required size is found, new memory has to berequested from the OS
• Free memory! Find header for memory area to be freed by pointer to this area! If previous or next memory areas are free, join the areas! Add resulting memory area to list
Ina Schaefer Translation to Target Language 51
Translation of Variables and Data Types
Dynamic Memory Management (5)
Remarks:
• If program writes over assigned memory area, references or sizeinformation can be destroyed with bad consequences.
• If memory cannot be allocated in bytes, alignment restrictionshave to be obeyed.
• For practical use the above principle can be improved by! non linear search! search for exact memory areas, avoiding defragmentation! support for joining memory areas after deallocation
Ina Schaefer Translation to Target Language 52
Translation of Expressions
Translation of Expressions
Difficulties for translation of expressions• Management of intermediate results on stack or in registers• Translation of source language operations
! no counterpart in target language! addressing! context-dependent (Boolean expression as condition is handled
differently as Boolean expression in an assignment.)
Ina Schaefer Translation to Target Language 53
Translation of Expressions
Translation of Expressions (2)
Abstract Syntax of Expressions:
Hier demonstrieren wir die generellen Problemeanhand eines kleinen Beispiels, das die direkte Übersetzung von Ausdrücken demonstriert.
Fortgeschrittene Techniken werden in Kapitel 3
behandelt.
B i i l ( i f h A d k üb t )Beispiel: (einfache Ausdrucksübersetzung)
Wir betrachten die Ausdruckssyntax aus dem MI-Übersetzungsbeispiel in Abschnitt 3.1.2:
Exp = ArtihmExp | Relation | IntConst
| CharConst | ArrayAccess | Var
ArithmExp = Add | Sub
Add, Sub ( Exp left, Exp right )
Relation = Lt | EqRelation Lt | Eq
Lt, Eq ( Exp left, Exp right )
IntConst ( Int i )
CharConst ( Char c )
ArrayAccess ( UsedId uid, Exp e )
iVar ( UsedId uid )
UsedId ( Ident id )
Wir treffen folgende Entwurfsentscheidungen:
Zwischenergebnisse werden auf dem Keller verwaltet• Zwischenergebnisse werden auf dem Keller verwaltet.
• Vergleiche werden durch Sprünge implementiert:
- Subtrahiere die beiden Werte auf dem Keller.- In Abhängigkeit des Ergebnisses springe einen
12.06.2007 220© A. Poetzsch-Heffter, TU Kaiserslautern
In Abhängigkeit des Ergebnisses springe einenBefehl an der 1 kellert bzw. der 0 kellert.Dazu sind entsprechende Marken zu generieren.
Ina Schaefer Translation to Target Language 54
Translation of Expressions
Translation of Expressions (3)
Design Decisions:
• Intermediate results are stored on stack.• Comparisons are implemented by jumps:
! compare values on stack! dependent on result, jump to command pushing 1 or pushing 0! generate associated labels
Ina Schaefer Translation to Target Language 55
Translation of Expressions
Translation of Expressions (4)
Attribution:Attributdeklarationen:
Relativadresse einer Variable oder eines Feldes
Typ eines Ausdrucks ( int, char, int[ ], char[ ] )
Code für den Unterbaum vom Typ CodeList
eindeutige Marke für Ausdruck vom Typ String
Attributierung für das Code-Attribut:
Add
CL + CR +
[ Add2(W Postinc(SP) Regdef(SP) ]
tt but e u g ü das Code tt but
Exp
[ Add2(W,Postinc(SP),Regdef(SP) ]
CL CRExp
Lt
CL + CR +
M
[ Sub2( W, Postinc(SP), Regdef(SP) ] +
[ Jlt( Label( “PUSH1_“ + M ) ) ] +
[ Move( W, Imm(0), Regdef(SP) ) ] +
[ Jump( Label( “ENDREL_“ + M )) ] +
[ Label( “PUSH1 “ + M ) ] +
Exp
[ Label( PUSH1_ + M ) ] +
[ Move( W, Imm(1), Regdef(SP) ) ] +
[ Label( “ENDREL_“ + M ) ]
CL CRExp
© A. Poetzsch-Heffter, TU Kaiserslautern
Exp Exp
( Die Attributierungen für Sub und Eq sind entsprechend. )
Relative Address of Variable or Array
Type of Expression (int, char, int[], char[])
Code for Subtree of Type CodeList
Unique Label for Expression of Type String
Ina Schaefer Translation to Target Language 56
Translation of Expressions
Translation of Expressions (5)
Attributdeklarationen:
Relativadresse einer Variable oder eines Feldes
Typ eines Ausdrucks ( int, char, int[ ], char[ ] )
Code für den Unterbaum vom Typ CodeList
eindeutige Marke für Ausdruck vom Typ String
Attributierung für das Code-Attribut:
Add
CL + CR +
[ Add2(W Postinc(SP) Regdef(SP) ]
tt but e u g ü das Code tt but
Exp
[ Add2(W,Postinc(SP),Regdef(SP) ]
CL CRExp
Lt
CL + CR +
M
[ Sub2( W, Postinc(SP), Regdef(SP) ] +
[ Jlt( Label( “PUSH1_“ + M ) ) ] +
[ Move( W, Imm(0), Regdef(SP) ) ] +
[ Jump( Label( “ENDREL_“ + M )) ] +
[ Label( “PUSH1 “ + M ) ] +
Exp
[ Label( PUSH1_ + M ) ] +
[ Move( W, Imm(1), Regdef(SP) ) ] +
[ Label( “ENDREL_“ + M ) ]
CL CRExp
12.06.2007 221© A. Poetzsch-Heffter, TU Kaiserslautern
Exp Exp
( Die Attributierungen für Sub und Eq sind entsprechend. )
CL +CR + [LOAD (R2, 0, $sp)ADD ($sp, $sp, 4)LOAD (R1, 0, $sp)ADD (R1, R1, R2)STORE (R1, 0, $sp)]
Ina Schaefer Translation to Target Language 57
Translation of Expressions
Translation of Expressions (6)
Attributdeklarationen:
Relativadresse einer Variable oder eines Feldes
Typ eines Ausdrucks ( int, char, int[ ], char[ ] )
Code für den Unterbaum vom Typ CodeList
eindeutige Marke für Ausdruck vom Typ String
Attributierung für das Code-Attribut:
Add
CL + CR +
[ Add2(W Postinc(SP) Regdef(SP) ]
tt but e u g ü das Code tt but
Exp
[ Add2(W,Postinc(SP),Regdef(SP) ]
CL CRExp
Lt
CL + CR +
M
[ Sub2( W, Postinc(SP), Regdef(SP) ] +
[ Jlt( Label( “PUSH1_“ + M ) ) ] +
[ Move( W, Imm(0), Regdef(SP) ) ] +
[ Jump( Label( “ENDREL_“ + M )) ] +
[ Label( “PUSH1 “ + M ) ] +
Exp
[ Label( PUSH1_ + M ) ] +
[ Move( W, Imm(1), Regdef(SP) ) ] +
[ Label( “ENDREL_“ + M ) ]
CL CRExp
12.06.2007 221© A. Poetzsch-Heffter, TU Kaiserslautern
Exp Exp
( Die Attributierungen für Sub und Eq sind entsprechend. )
CL + CR + [LOAD (R2, 0, $sp)ADD($sp, $sp, 4)LOAD (R1, 0, $sp) SLT (R1, R1, R2) BEQ (R1, $zero, “PUSH_0_”+M)LOADI (R1, 1)STORE (R1, 0, $sp)JUMP (“ENDREL_”+M)LABEL(“PUSH_0_”+M)LOADI (R1, 0)STORE (R1, 0, $sp)LABEL (“ENDREL_”+M)]
Ina Schaefer Translation to Target Language 58
Translation of Expressions
Translation of Expressions (7)
IntConst
[ Move( W, Imm( ), Predec(SP) ][ Move( W, Imm(_), Predec(SP) ]
Int
VarTV
if TV = int then
[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ]
else // TV = charelse // TV char
[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId
RA
ArrayAccessTV
ArrayAccess
CR + [ Move( W, Regdef(SP), Reg(R1) ] +
if TV = int then
[ Move(W, Bdispx( Reg(R0), Reg(R1), RA),[ ( p ( g( ) g( ) )
Regdef(SP) ]
else // TV = char
[ Conv( Bdispx( Reg(R0), Reg(R1), RA),
Regdef( SP ) ]
Beachte: Die Attributierung von Var und ArrayAccess
UsedIdRA CR
Exp
12.06.2007 222© A. Poetzsch-Heffter, TU Kaiserslautern
Beachte: Die Attributierung von Var und ArrayAccess
erzeugt Code zum Kellern des Werts vom Ausdruck,
nicht für die Adressierung des Zugriffs.
[LOADI (Ri, int) ] +[SUB ($sp, $sp, 4)] +[STORE (Ri, 0, $sp)]
if TV = int then[SUB ($sp, $sp, 4) LOADI(R1,RA)ADD (RI, RI, $gp)LOAD(R2, 0, RI)STORE (R2, 0, $sp) ] else // TV = char[SUB ($sp,$sp,1)LOADI(R1,RA)ADD (RI, RI, $gp)LOAD(R2, 0, RI)STOREB (R2, 0, $sp) ]
Ina Schaefer Translation to Target Language 59
Translation of Expressions
Translation of Expressions (8)
IntConst
[ Move( W, Imm( ), Predec(SP) ][ Move( W, Imm(_), Predec(SP) ]
Int
VarTV
if TV = int then
[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ]
else // TV = charelse // TV char
[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId
RA
ArrayAccessTV
ArrayAccess
CR + [ Move( W, Regdef(SP), Reg(R1) ] +
if TV = int then
[ Move(W, Bdispx( Reg(R0), Reg(R1), RA),[ ( p ( g( ) g( ) )
Regdef(SP) ]
else // TV = char
[ Conv( Bdispx( Reg(R0), Reg(R1), RA),
Regdef( SP ) ]
Beachte: Die Attributierung von Var und ArrayAccess
UsedIdRA CR
Exp
12.06.2007 222© A. Poetzsch-Heffter, TU Kaiserslautern
Beachte: Die Attributierung von Var und ArrayAccess
erzeugt Code zum Kellern des Werts vom Ausdruck,
nicht für die Adressierung des Zugriffs.
[LOADI (Ri, int) ] +[SUB ($sp, $sp, 4)] +[STORE (Ri, 0, $sp)]
if TV = int then[SUB ($sp, $sp, 4) LOADI(R1,RA)ADD (RI, RI, $gp)LOAD(R2, 0, RI)STORE (R2, 0, $sp) ] else // TV = char[SUB ($sp,$sp,1)LOADI(R1,RA)ADD (RI, RI, $gp)LOAD(R2, 0, RI)STOREB (R2, 0, $sp) ]
Ina Schaefer Translation to Target Language 60
Translation of Expressions
Translation of Expressions (9)
IntConst
[ Move( W, Imm( ), Predec(SP) ][ Move( W, Imm(_), Predec(SP) ]
Int
VarTV
if TV = int then
[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ]
else // TV = charelse // TV char
[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId
RA
ArrayAccessTV
ArrayAccess
CR + [ Move( W, Regdef(SP), Reg(R1) ] +
if TV = int then
[ Move(W, Bdispx( Reg(R0), Reg(R1), RA),[ ( p ( g( ) g( ) )
Regdef(SP) ]
else // TV = char
[ Conv( Bdispx( Reg(R0), Reg(R1), RA),
Regdef( SP ) ]
Beachte: Die Attributierung von Var und ArrayAccess
UsedIdRA CR
Exp
12.06.2007 222© A. Poetzsch-Heffter, TU Kaiserslautern
Beachte: Die Attributierung von Var und ArrayAccess
erzeugt Code zum Kellern des Werts vom Ausdruck,
nicht für die Adressierung des Zugriffs.
CR +[LOAD (R1, 0, $sp)LOADI (R2, RA)ADD (R1, R1, R2)ADD (R1, R1, $gp)] +if TV = int then
[LOAD (R2, 0, RI)STORE (R2, 0, $sp)]
else // TV = char[LOADB (R2 0, RI)STOREB (R2, 0, $sp)]
Ina Schaefer Translation to Target Language 61
Translation of Expressions
Improvements
• Improvement of generated code by! Storage of intermediate results in registers! Context-dependent optimizing instruction selection! Avoiding redundant computations by evaluating common
subexpressions only once• Improvement of translation technique by usage of intermediate
language
Ina Schaefer Translation to Target Language 62
Translation of Statements
Translation of Statements
Most statements can be translated by translation schemes with jumps:
Verbesserungen:
• des erzeugten Codes durch
Verwaltung von Zwischenergebnissen in Registern- Verwaltung von Zwischenergebnissen in Registern- kontextabhängige, optimierende Befehlsauswahl- Vermeidung redundanter Berechnungen durch
einmalige Auswertung gemeinsamer Teilausdrücke
Ü
3 1 5 Übersetzung von Anweisungen
• der Übersetzungstechnik durch Benutzung einer
Zwischensprache
Für die meisten Anweisungen lassen sich relativ leicht Übersetzungsschemata mittels Sprüngen angeben:
3.1.5 Übersetzung von Anweisungen
While
[ Label( “BEGWHILE_“ + M ) ] +CE + [ Cmp( W Imm(0) Postinc(SP) ) ] +
M
[ Cmp( W, Imm(0), Postinc(SP) ) ] +[ Jeq( Label( “ENDWHILE_“+M) ) ] +CS +[ Jump(Label( “BEGWHILE_“+M)) ] +[ Label( “ENDWHILE_“ + M ) ]
Schwieriger ist die gute Übersetzungen von switch-
Exp
( )
CE CSStat
© A. Poetzsch-Heffter, TU Kaiserslautern
g g gAnweisungen und die effiziente Berücksichtigungvon nicht-strikten Ausdrücken.
[LABEL (“BEGWHILE_”+M)] +CE +[LOAD (R1, 0, $sp)ADD ($sp, $sp, 4)BEQ (R1, $zero, “ENDWHILE_”+M)] +CS +[JUMP (“BEGWHILE_”+ M)] +[LABEL (“ENDWHILE_”+M)]
Ina Schaefer Translation to Target Language 63
Translation of Statements
More Complex Translation of Statements
More complex is a good translation of switch-statements and efficienthandling of non-strict expressions.
We consider the translation of non-strict Boolean expressions as anexample of an optimizing translation and for the usage of contextinformation.
Example: Abstract Syntax
Wir demonstrieren hier die Übersetzungnicht-strikter boolescher Ausdrücke:
• als Beispiel für eine optimierende Übersetzung
• um die Verwendung von Kontextinformation zu
illustrieren.
Beispiel: (Verwendung ererbter Information)
Stat = While | IfThenElse | ...
BExp = And | Or | Not | StrictExp
Beispiel: (Verwendung ererbter Information)
Wir betrachten folgendes Sprachfragment:
BExp And | Or | Not | StrictExp
While ( BExp c, Stat b )
IfThenElse ( BExp c, Stat then, Stat else )
And, Or ( BExp left, BExp right )
Not ( Bexp e )
StrictExp ( Exp e )
Ein Programmfragment dazu:
if( (B1 || B2) && ! B3 ) {
while( !(B4 || B5) ) A1
Wobei A1 und A2 Anweisungen sind und B1 bis B5
while( !(B4 || B5) ) A1
} else {
A2
}
Wobei A1 und A2 Anweisungen sind und B1 bis B5strikte Ausdrücke. Wie in C und Java sind die booleschen Ausdrücke || und && nicht-strikt, d.h. z.B.dass bei Auswertung von B1 und B2 zu false, B3 nicht mehr ausgewertet werden braucht und darf!
12.06.2007 224© A. Poetzsch-Heffter, TU Kaiserslautern
nicht mehr ausgewertet werden braucht und darf!
Außerdem sollen Sprungketten vermieden werden,
d.h. Sprünge zu unbedingten Sprungbefehlen.
Ina Schaefer Translation to Target Language 64
Translation of Statements
More Complex Translation of Statements (2)
A program fragment:
Wir demonstrieren hier die Übersetzungnicht-strikter boolescher Ausdrücke:
• als Beispiel für eine optimierende Übersetzung
• um die Verwendung von Kontextinformation zu
illustrieren.
Beispiel: (Verwendung ererbter Information)
Stat = While | IfThenElse | ...
BExp = And | Or | Not | StrictExp
Beispiel: (Verwendung ererbter Information)
Wir betrachten folgendes Sprachfragment:
BExp And | Or | Not | StrictExp
While ( BExp c, Stat b )
IfThenElse ( BExp c, Stat then, Stat else )
And, Or ( BExp left, BExp right )
Not ( Bexp e )
StrictExp ( Exp e )
Ein Programmfragment dazu:
if( (B1 || B2) && ! B3 ) {
while( !(B4 || B5) ) A1
Wobei A1 und A2 Anweisungen sind und B1 bis B5
while( !(B4 || B5) ) A1
} else {
A2
}
Wobei A1 und A2 Anweisungen sind und B1 bis B5strikte Ausdrücke. Wie in C und Java sind die booleschen Ausdrücke || und && nicht-strikt, d.h. z.B.dass bei Auswertung von B1 und B2 zu false, B3 nicht mehr ausgewertet werden braucht und darf!
12.06.2007 224© A. Poetzsch-Heffter, TU Kaiserslautern
nicht mehr ausgewertet werden braucht und darf!
Außerdem sollen Sprungketten vermieden werden,
d.h. Sprünge zu unbedingten Sprungbefehlen.
where• A1, A2 are statements• B1 – B5 are strict expressions
Ina Schaefer Translation to Target Language 65
Translation of Statements
More Complex Translation of Statements (3)
In C and Java, we have that || and && are non-strict, i.e. if B1 and B2evaluate to false, B3 may not be evaluated.
Further, jump cascades should be avoided, i.e. jumps to otherunconditional jumps.
Idea for Attribution:For each boolean expression, compute
• Label for true case (Attribute: %)• Label for false case (Attribute: &)• Information of type bool in which case to jump (Attribute: ')
Ina Schaefer Translation to Target Language 66
Translation of Statements
More Complex Translation of Statements (4)
Further Attributes:
Idee der Attributierung:
Ermittele zu jedem booleschen Ausdruck:
• das Sprungziel für den true-Fall (Attribut ),
• das Sprungziel für den false-Fall (Attribut ),
• die Information vom Typ bool, in welchem Fall
Weitere Attributdeklarationen:
yp ,
zu springen ist (Attribut ).
Code für den Unterbaum vom Typ CodeList
Weitere Attributdeklarationen:
eindeutige Marke für jede Anweisung und jeden
Booleschen Ausdruck vom Typ String
IfThenElseM
“THEN“ + MCB +
[ Label( “THEN“ + M ) ] +
CT +
[ Jump( Label( “END“+M))] +
[ Label( “ELSE“ + M ) ] +false
THEN + M
“ELSE“ + M
[ Label( ELSE + M ) ] +
CE +
[ Label( “END“ + M ) ]
CB C C
false
12.06.2007 225© A. Poetzsch-Heffter, TU Kaiserslautern
BExpCB CT
StatCE
Stat
Code for subtree of type CodeList
Unique label for each statement and for each boolean expression of type String
Ina Schaefer Translation to Target Language 67
Translation of Statements
More Complex Translation of Statements (5)
Idee der Attributierung:
Ermittele zu jedem booleschen Ausdruck:
• das Sprungziel für den true-Fall (Attribut ),
• das Sprungziel für den false-Fall (Attribut ),
• die Information vom Typ bool, in welchem Fall
Weitere Attributdeklarationen:
yp ,
zu springen ist (Attribut ).
Code für den Unterbaum vom Typ CodeList
Weitere Attributdeklarationen:
eindeutige Marke für jede Anweisung und jeden
Booleschen Ausdruck vom Typ String
IfThenElseM
“THEN“ + MCB +
[ Label( “THEN“ + M ) ] +
CT +
[ Jump( Label( “END“+M))] +
[ Label( “ELSE“ + M ) ] +false
THEN + M
“ELSE“ + M
[ Label( ELSE + M ) ] +
CE +
[ Label( “END“ + M ) ]
CB C C
false
12.06.2007 225© A. Poetzsch-Heffter, TU Kaiserslautern
BExpCB CT
StatCE
Stat
Ina Schaefer Translation to Target Language 68
Translation of Statements
More Complex Translation of Statements (6)
WhileM
[ Label( “BEGW“ + M ) ] +
CB +
[ Label( “BODY“ + M ) ] +
CS +
[ Jump( Label( “BEGW“+M))] +
“BODY“ + M
“ENDW“ + M
BExp
[ Jump( Label( BEGW +M))] +
[ Label( “ENDW“ + M ) ]
CB CSStat
false
p
Not
BExp
not(_)
And
M
“BER“ + M CL +
false
CL +
[ Label( “BER“ + M ) ] +
CR
12.06.2007 226© A. Poetzsch-Heffter, TU Kaiserslautern
BExp BExpCL CR
Ina Schaefer Translation to Target Language 69
Translation of Statements
More Complex Translation of Statements (7)
WhileM
[ Label( “BEGW“ + M ) ] +
CB +
[ Label( “BODY“ + M ) ] +
CS +
[ Jump( Label( “BEGW“+M))] +
“BODY“ + M
“ENDW“ + M
BExp
[ Jump( Label( BEGW +M))] +
[ Label( “ENDW“ + M ) ]
CB CSStat
false
p
Not
BExp
not(_)
And
M
“BER“ + M CL +
false
CL +
[ Label( “BER“ + M ) ] +
CR
12.06.2007 226© A. Poetzsch-Heffter, TU Kaiserslautern
BExp BExpCL CR
Ina Schaefer Translation to Target Language 70
Translation of Statements
More Complex Translation of Statements (8)
WhileM
[ Label( “BEGW“ + M ) ] +
CB +
[ Label( “BODY“ + M ) ] +
CS +
[ Jump( Label( “BEGW“+M))] +
“BODY“ + M
“ENDW“ + M
BExp
[ Jump( Label( BEGW +M))] +
[ Label( “ENDW“ + M ) ]
CB CSStat
false
p
Not
BExp
not(_)
And
M
“BER“ + M CL +
false
CL +
[ Label( “BER“ + M ) ] +
CR
12.06.2007 226© A. Poetzsch-Heffter, TU Kaiserslautern
BExp BExpCL CR
Ina Schaefer Translation to Target Language 71
Translation of Statements
More Complex Translation of Statements (9)
OrM
“BER“ + M CL +
true
BER M CL +
[ Label( “BER“ + M ) ] +
CR
BExp BExpCL CR
StrictExp
CE +
[ Cmp( W, Imm(1), Postinc(SP) ) ] +
TT FT JI
[ p( ( ) ( ) ) ]
( if JI then
[ Jeq( Label( TT) ) ]
else
[ Jne( Label( FT) ) ] )
ExpCE
Bemerkung:
Falls nicht-strikte und strikte boolesche Ausdrücke
gemischt sind, wird die Codegenerierung komplexer.
12.06.2007 227© A. Poetzsch-Heffter, TU Kaiserslautern
Beispiel: a = ( b && f(c) ) + g;
Ina Schaefer Translation to Target Language 72
Translation of Statements
More Complex Translation of Statements (10)
OrM
“BER“ + M CL +
true
BER M CL +
[ Label( “BER“ + M ) ] +
CR
BExp BExpCL CR
StrictExp
CE +
[ Cmp( W, Imm(1), Postinc(SP) ) ] +
TT FT JI
[ p( ( ) ( ) ) ]
( if JI then
[ Jeq( Label( TT) ) ]
else
[ Jne( Label( FT) ) ] )
ExpCE
Bemerkung:
Falls nicht-strikte und strikte boolesche Ausdrücke
gemischt sind, wird die Codegenerierung komplexer.
12.06.2007 227© A. Poetzsch-Heffter, TU Kaiserslautern
Beispiel: a = ( b && f(c) ) + g;
CE +[LOAD (R1, 0, $sp)ADD ($sp, $sp, 4)] +if JL then
[BNE (R1, $zero, LABEL(TT))]else
[BEQ (R1, $zero, LABEL(FT)]
Ina Schaefer Translation to Target Language 73
Translation of Statements
More Complex Translation of Statements (11)
Remarks:
If non-strict and strict Boolean expressions are mixed, code generationbecomes more complex.
Example: a = ( b && f(c)) + g ;
Recommended Reading:• Wilhelm, Maurer: Sec. 2.4, pp. 12 –16
Ina Schaefer Translation to Target Language 74
Translation of Procedures and Local Objects
Translation of Procedures and Local Objects
Most procedural languages support recursion, procedure-localvariables and nested procedures. In the following, we consider
• Translation of recursive procedures• Translation of local variables• Translation of nested procedures
We do not consider the translation of procedures as parameters.
Ina Schaefer Translation to Target Language 75
Translation of Procedures and Local Objects
Procedures
The declaration of a procedure consists of• the name of the procedure• the declaration of the formal parameters• the declaration of local variables• the body of the procedure
Each dynamic call of a procedure corresponds to a procedureincarnation.
Analogy:• Procedure declaration ! procedure incarnation• Class declaration ! object/class instance
Ina Schaefer Translation to Target Language 76
Translation of Procedures and Local Objects
Procedure Call Tree
The runtime behaviour of a procedural program can be described by aprocedure call tree.
Example (C-Program):
Das Laufzeitverhalten eines prozeduralen Programms
lässt sich durch den Prozeduraufrufbaum beschreiben.
Beispiel: (Prozeduraufrufbaum)
Wir betrachten folgendes C-Programm:
int even(int n){return n==0?1:odd(n-1);}
int odd (int n){return n==0?0:even(n-1);}
i i (){ (2)? (1) dd(1) }int main(){return even(2)?even(1):odd(1);}
main
even
odd
even
odd
even
Bemerkung:Bemerkung:
• Der Prozeduraufrufbaum ist eine abstrakte
Beschreibung des Laufzeitverhaltens und damit
abhängig von den Eingabewerten des Programms.
12.06.2007 229© A. Poetzsch-Heffter, TU Kaiserslautern
• Zu jedem Ausführungszeitpunkt gibt es einen aktiven
Pfad in dem Baum.
Ina Schaefer Translation to Target Language 77
Translation of Procedures and Local Objects
Procedure Call Tree (2)
Remarks:• The procedure call tree is an abstract description of the runtime
behaviour and depends on the inputs of the program.• For each execution point, there is an active path in the tree.
Ina Schaefer Translation to Target Language 78
Translation of Procedures and Local Objects
Translation of Recursive Procedures
Main Tasks:• Parameter passing on entry, return of result at exit of procedure• Addressing of parameters• Handling of recursion
Main Idea:For each procedure incarnation, a stack frame is allocated. The stackframe contains:
• the current parameters• the return address• the register contents of the caller• further information
Ina Schaefer Translation to Target Language 79
Translation of Procedures and Local Objects
Stack FrameStructure of stack frame
For procedure with result, also memory has to be allocated. (Where?)Ina Schaefer Translation to Target Language 80
Translation of Procedures and Local Objects
Code Generation for Procedures
Code has to be generated• at the call site
! to pass current parameters to procedure incarnation! to jump to the code of the procedure body! to make the procedure’s result available for further processing
• at the beginning of the procedure (prolog)! saving registers! set argument pointer
• at the end of the procedure (epilog)! restore registers
Note: Many tasks can be moved from the call site to the prolog andvice versa. Because a procedure has only one prolog, but potentiallymany call sites, it is more efficient to move the code to the prolog (andto the epilog).
Ina Schaefer Translation to Target Language 81
Translation of Procedures and Local Objects
Translation Scheme for Procedure Declaration
Übersetzungsschema für Prozedurdeklaration:
P D lM
ProcDecl
[ Label( “PROCBEG_“ + M) ] +< Prolog > +CSL +CSL +< Epilog > +[ Ret( ) ]
CSLIdent StatListParamList
Übersetzungsschema für Prozeduraufruf wobeiÜbersetzungsschema für Prozeduraufruf, wobei vorausgesetzt ist, dass der Code für die Liste der Parameterausdrücke (ExpList) das Kellernder aktuellen Parameter besorgt:
Call
CPL +[ Jump PLAB ] +< entfernen der Parameter vom Keller >
UsedIdPLAB CPL
ExpList
12.06.2007 233© A. Poetzsch-Heffter, TU Kaiserslautern
wobei PLAB die Ansprungmarke der Prozedur ist.
[LABEL (“PROCBEG_”+M)] +<Prolog> +CSL +<Epilog> +[JR $ra]
Ina Schaefer Translation to Target Language 82
Translation of Procedures and Local Objects
Translation Scheme for Procedure Call
Assume that code for list of parameter expressions ExpList pushescurrent parameters on stack.
Übersetzungsschema für Prozedurdeklaration:
P D lM
ProcDecl
[ Label( “PROCBEG_“ + M) ] +< Prolog > +CSL +CSL +< Epilog > +[ Ret( ) ]
CSLIdent StatListParamList
Übersetzungsschema für Prozeduraufruf wobeiÜbersetzungsschema für Prozeduraufruf, wobei vorausgesetzt ist, dass der Code für die Liste der Parameterausdrücke (ExpList) das Kellernder aktuellen Parameter besorgt:
Call
CPL +[ Jump PLAB ] +< entfernen der Parameter vom Keller >
UsedIdPLAB CPL
ExpList
12.06.2007 233© A. Poetzsch-Heffter, TU Kaiserslautern
wobei PLAB die Ansprungmarke der Prozedur ist.
CPL +[JAL PLAB] +<Code to remove parameters from stack>
Some machines have special commands for procedure call return.(MIPS: JAL, JR)
Ina Schaefer Translation to Target Language 83
Translation of Procedures and Local Objects
Translation of Procedure-Local VariablesAnaloge to parameters, also procedure-local variables have to bestored in the stack frame, because there is one instance of the localvariables for each procedure incarnation.
Ina Schaefer Translation to Target Language 84
Translation of Procedures and Local Objects
Dynamic and Static Local VariablesLocal Variables are static, if their size is known at compile time, elsethey are dynamic.
Example:
Lokale Variablen heißen statisch, wenn ihreGröße zur Übersetzungszeit bekannt ist, andernfallsdynamisch.
Beispiel: (statische/dynamische Variable)
Im folgenden C-Fragment sind i,j,k statische lokaleVariable; f und g sind dynamische Variable/Felder
void foo( int hsize ) {
int i, j;
Variable; f und g sind dynamische Variable/Felder,da ihre Größe vom Parameter size abhängt.
char f[ 2*hsize ];
int g[ hsize ];
int k;
...
}}
Speicherallokation geschieht im Prolog, bei dynamischen Variablen in Abhängigkeit von denaktuellen Parametern Übersetzer erzeugt dafür Codeaktuellen Parametern. Übersetzer erzeugt dafür Code.
Adressierung:
Prozedurlokale Variable werden relativ zu einem
Bezugspunkt im Kellerrahmen adressiert, z.B. relativ zum Argumentzeiger.
Bei der Adressierung dynamischer Variablen ist
im Allg ein zusätzlicher Indirektionsschritt notwendig
12.06.2007 235© A. Poetzsch-Heffter, TU Kaiserslautern
im Allg. ein zusätzlicher Indirektionsschritt notwendig,um statisch Relativadressen für alle lokalen Variablenfestlegen zu können.
where• i,j,k are static local variables• f, g are dynamic variables (arrays), because their size depends on
the parameter hsize.Ina Schaefer Translation to Target Language 85
Translation of Procedures and Local Objects
Memory Allocation for Local Variables
Memory allocation is done in the prolog of a procedure, for dynamicvariables dependent on the current parameters, thus code isgenerated.
Addressing: Local variables are addressed relative to a referencepoint in the stack frame, e.g. argument pointer/frame pointer.
For dynamic variables, an additional step is necessary to find staticallyrelative addresses for all local variables.
Ina Schaefer Translation to Target Language 86
Translation of Procedures and Local Objects
Stack Frame (Example)
Stack frame for procedure foo:
Ina Schaefer Translation to Target Language 87
Translation of Procedures and Local Objects
Stack Frame (Example) (2)
Addresses of local variables in the example:• i: AP - 64• k: AP - 80• f[Ri] – MIPS Code:
LOADI (R1, AP)SUBI (R1, R1, 72)LOAD (R1, 0, R1)LOADI (R2, 4)MUL (Ri, Ri, R2)ADD (R1, R1, Ri)LOAD (R1, 0, R1)
Ina Schaefer Translation to Target Language 88
Translation of Procedures and Local Objects
Translation of Nested Procedures
For each procedure incarnation, there exist instances of the localvariables and of the parameters.
Problems:• How are non-local variables (neither local nor global) addressed?• Which instance of a non-local variable should be accessed?
These problems are also important for many functional languages.
Ina Schaefer Translation to Target Language 89
Translation of Procedures and Local Objects
Static and Dynamic Successors
• The direct static predecessor of a procedure declaration P is theprocedure declaration encolsing P in the source text.
• The direct static predecessor of a procedure incarnation P is thecurrent youngest procedure incarnation of the direct staticpredecessor of P.
• The direct dynamic predecessor of a procedure incarnation P isthe calling procedure incarnation.
• The static and dynamic predecessors are contained in thetransitive closure.
Ina Schaefer Translation to Target Language 90
Translation of Procedures and Local Objects
Nested Procedures (Example)Beispiel: (geschachtelte Prozeduren, die 2.)
proc P
var vp
proc Q
var vq
proc R
var vr
begin
(* hier vp, vq, vr adressierbar *)( p, q, )
call P
end
begin
(* hier vp und vq adressierbar *)(* hier vp und vq adressierbar *)
call R
end
proc S
begin
(* hier vp adressierbar *)
if ... then call S
if ... then call Q
end
begin
(* hier vp adressierbar *)
call S
12.06.2007 239© A. Poetzsch-Heffter, TU Kaiserslautern
end
here vp, vq, vr addressable
here vp, vq addressable
here vp addressable
here vp addressable
Ina Schaefer Translation to Target Language 91
Translation of Procedures and Local Objects
Nested Procedures (Example) (2)
Procedure Call Tree:Möglicher Prozeduraufrufbaum für das Beispiel:
P ! vp
S
QS
+1
+0 +0! vp
! vp ! vp, vqQ
R
S
+1
-2
! vp ! vp, vq
! vp, vq, vr
P
S
+1
! bedeutet “zugreifbar“ ! vp
! vp
Die Prozedurschachtelungstiefe (PST) ist einwichtiges Merkmal für die Übersetzung geschachtelterProzeduren. Für das obige Beispiel:g p
Prozedur PST aufrufbar
P 0 P, Q, S
Q 1 P Q RQ 1 P, Q, RR 2 P, Q, RS 1 P, Q, S
Ist PG eine von PA aufrufbare Prozedur dann gilt:
12.06.2007 240© A. Poetzsch-Heffter, TU Kaiserslautern
Ist PG eine von PA aufrufbare Prozedur, dann gilt:
PST(PG) ! PST(PA) + 1
denotes accessible
variables
Ina Schaefer Translation to Target Language 92
Translation of Procedures and Local Objects
Nested Procedures (Example) (3)
The procedure nesting depth (PND) is an important characteristic forthe translation of nested procedures.
If PG is a procedure that is callable from PA, then it holds that
PND(PG) ( PND(PA) + 1
In the example:
procedure PND callableP 0 P, Q, SQ 1 P, Q, RR 2 P, Q, RS 1 P, Q, S
Ina Schaefer Translation to Target Language 93
Translation of Procedures and Local Objects
Translation of Nested Procedures (Solutions)
• How are non-local variables (neither local nor global) addressed?(Program Semantics)
If PI is a procedure incarnation accessing the non-local variable vof a procedure declaration P, chose the variable instance in thestatic predecessor P of PI.
• Which instance of a non-local variable should be accessed?(Translation Technique)
1. Store all static predecessors of a procedure incarnation.2. Access stack frame of the respective static predecessor via the
difference of the PND of the current procedure incarnation and theprocedure incarnation of the corresponding static predecessor.
Ina Schaefer Translation to Target Language 94
Translation of Procedures and Local Objects
Reference Chain for Static Predecessors
• Each procedure incarnation has a reference to the procedureincarnation of its direct static predecessor (SPR).
• An incarnation is represented by the address of its argumentpointer AP.
• The static predecessor reference (SPR) is stored in the stackframe.
Ina Schaefer Translation to Target Language 95
Translation of Procedures and Local Objects
Reference Chain for Static Predecessors (2)
Stack Frame with Static Predecessor Reference (SPR):
Ina Schaefer Translation to Target Language 96
Translation of Procedures and Local Objects
Reference Chain for Static Predecessors (3)
Snapshot of Stack for Example
The procedure P has no static predecessors.
Ina Schaefer Translation to Target Language 97
Translation of Procedures and Local Objects
Relevant Aspects for Code Generation
1. Addressing with Static Predecessor Reference Chain:
Let V be a variable with PND(V) = n, i.e. V is declared as a localvariable of a procedure P with PND(P) = n. Let RA(V) be the addressof V relative to the the argument pointer.
Let VA be an application position of V in a procedure Q ()= P) withPND(Q) = m and m > n.
The address of VA is obtained by m # n times dereferencing of thestatic predecessor references:
M[M[. . . M[AP] . . .]]" #$ %m!n times
+RA(V )
Ina Schaefer Translation to Target Language 98
Translation of Procedures and Local Objects
Relevant Aspects for Code Generation (2)
Note:• The difference m-n is known at compile time for each application
position of a variable.• The address of VA can in general not be handled directly by the
addressing techniques of the target machine. Instead, separatecommands have to be used that are executed each time thevariable is accessed.
Ina Schaefer Translation to Target Language 99
Translation of Procedures and Local Objects
Relevant Aspects for Code Generation (3)
2: Management of Static Predecessor Reference Chain:
Let ! PND =def PND(caller) - PND (callee). We distinguish two cases:
• ! PND = -1: Argument pointer of caller is stored as SPR of callee.• ! PND > -1: Follow SPR chain of the caller for ! PND steps. The
resulting SPR is the SPR of the callee.
Ina Schaefer Translation to Target Language 100
Translation of Procedures and Local Objects
Relevant Aspects for Code Generation (4)
The SPR can be handled by the caller procedure before the call; e.g:
SUB $sp, $sp, 4LI $Ri, APcallerSTORE $Ri, 0, $sp[ LOAD $Ri, 0, $spADD $sp, $Ri, $zero...(! PND +1)- times in total ]
First, the AP of the caller is pushed onto the stack, then the SPR chainis followed.
Ina Schaefer Translation to Target Language 101
Translation of Procedures and Local Objects
Relevant Aspects for Code Generation (5)
Remarks:• The SPR chain can relatively easily be realized.• Addressing of non-local variables can be inefficient.
Ina Schaefer Translation to Target Language 102
Translation of Procedures and Local Objects
Static Predecessors in Stack Frames
Observation: The number of static predecessors of a procedure(incarnation) P is known at compile time: PND (P).
Thus: All static predecessors of a procedure incarnation can bedirectly stored in the stack frame (instead of SPR chain).
The stack area to store the static predecessors is called local display.Instead of one word for the SPR, we store PND(P) words.
Ina Schaefer Translation to Target Language 103
Translation of Procedures and Local Objects
Static Predecessors in Stack Frames (2)
Stack Frame with Local Display:
Ina Schaefer Translation to Target Language 104
Translation of Procedures and Local Objects
Static Predecessors in Stack Frames (3)
Snapshot of Stack for Example with Local Display
Ina Schaefer Translation to Target Language 105
Translation of Procedures and Local Objects
Local Display
1. Addressing with Local Display
Let V, n, RA(V), VA and m defined as above, and m >n . The addressof VA is obtained by:
M[AP # 4 " (m # n)] + RA(V )
2. Management of the Local Display:
Let ! PND =def PND(caller) -PND (callee). We distinguish two cases:1. ! PND = -1: Display of caller + AP of caller2. ! PND > -1: Display of caller - ! PND Entries
Remarks:• Addressing of local variables is more efficient with local display.• In general, more memory space on stack is required.
Ina Schaefer Translation to Target Language 106
Translation of Procedures and Local Objects
Static Predecessors in Global Display
Observation: Many entries in the local display are identical.
Goal: Store display in global memory region. This memory area iscalled global display.
Ina Schaefer Translation to Target Language 107
Translation of Procedures and Local Objects
Static Predecessors in Global Display (2)
Snapshot of Stack for Example with Global Display
Ina Schaefer Translation to Target Language 108
Translation of Procedures and Local Objects
Global Display
1. Addressing with Global Display
Addressing with global display is like addressing with local display, butinstead of AP the address of global display is used.
2. Management of the Global Display:
Problem: Global display is changed on a procedure call if procedureswith lower PND are executed that are later called by procedures withhigher PND.
Observation: Each procedure incarnation changes maximally onecomponent of the global display, i.e. if PND(caller) - PND (callee) = -1.
Solution: It suffices to saved the changed component and to restore itin the epilog of a procedure. For saving the component, a memoryword in the stack frame has to be reserved.
Ina Schaefer Translation to Target Language 109
Translation of Procedures and Local Objects
Global Display (2)
Remarks:
• If there are enough registers, the global display (or parts) shouldbe stored in registers.
• For languages that use procedures as parameters, the displaytechnique has to be adapted.
• The different variants for handling nested procedures show typicalvariation points in compiler design.
The introduced memory management can be seen as a schema thatcan be adapted for given source and target languages (consideringproperties of the target machines, e.g. caches).
Ina Schaefer Translation to Target Language 110
Translation of Procedures and Local Objects
Summary: Memory Management
Ina Schaefer Translation to Target Language 111
Translation of Procedures and Local Objects
Literature
Recommended Reading:
• Wilhelm, Maurer: Sect. 2.9, pp. 31 – 53
Ina Schaefer Translation to Target Language 112