• Keine Ergebnisse gefunden

Translation to Target Language - Imperative Language Constructs -

N/A
N/A
Protected

Academic year: 2022

Aktie "Translation to Target Language - Imperative Language Constructs -"

Copied!
56
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

- Imperative Language Constructs - Lecture Compilers SS 2009

Dr.-Ing. Ina Schaefer

Software Technology Group TU Kaiserslautern

Ina Schaefer Translation to Target Language 1

Content of Lecture

1. Introduction: Overview and Motivation 2. Syntax- and Type Analysis

2.1 Lexical Analysis

2.2 Context-Free Syntax Analysis

2.3 Context-Dependent Syntax Analysis 3. Translation to Target Language

3.1 Translation of Imperative Language Constructs 3.2 Translation of Object-Oriented Language Constructs 4. Selected Aspects of Compilers

4.1 Intermediate Languages 4.2 Optimization

4.3 Command Selection 4.4 Register Allocation 4.5 Code Generation 5. Garbage Collection

6. XML Processing (DOM, SAX, XSLT)

(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)

(3)

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

(4)

Language Constructs of Procedural Languages

Procedural Languages

From a conceptional and semantical view point, procedural languages have 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 only

relevant for translation in name analysis and for binding and loading.

(5)

Nested Procedures

Example from [Wilhelm, Maurer; Fig. 2.9]

Übersetzung geschachtelter Prozeduren

Geschachtelte/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/Maurer,

var b begin ... b ...

... a ...

c

mt aus Wilhe

... c ...

end begin ... a ...

... b ...

spiel stamm

... call Q ...

end proc S

var a begin

(das Beis

begin ... a ...

... call Q ...

end begin

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 237

... 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

(6)

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 Simulator

http://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

(7)

MIPS Commands

Arithemic

add add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 add immediate addi $s1, $s2, c $s1 = $s2 + c multiply 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] = $s1 load immediate li, $s1, c $s1 = c

load half lh $s1, c($s2) $s1 = Memory[$s2 + c]

store half sh $s1, c($s2) Memory[$s2 + c] = $s1 load byte lb $s1, c($s2) $s1 = Memory[$s2 + c]

store byte sb $s1, c($s2) Memory[$s2 + c] = $s1

(8)

Assembly and Machine Languages

MIPS Commands (3)

Logical

and and $s1, $s2, $s3 $s1 = $s2 & $s3 or or $s1, $s2, $s3 $s1 = $s2 | $s3 nor nor $s1, $s2, $s3 $s1 = ¬ ( $s2 | $s3 ) and immediate andi $s1, $s2, c $s1 = $s2 & c

or immediate ori $s1, $s2, c $s1 = $s2 | c shift left logical sll $s1, $s2, c $s1 = $s2 « c shift 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 label jump register jr $ra goto $ra

jump and link jal label $ra = PC + 4; goto label

(9)

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 memory location 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 the instruction 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

(10)

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 integer

add $a0, $t0, $zero # load value into $a0 syscall

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 print

print string 4 $a0 = address of

null-terminated string to print exit (terminate execution) 10

print character 11 $a0 = character to print

exit2 (terminate with value) 17 $a0 = termination result

(11)

Example: Translation to MIPS

The example illustrates the MIPS assembler and typical translation tasks.

Code quality is not considered.

Source Code in C:

Das Beispiel soll zum einen die MI-Assemblersprachep p demonstrieren, 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 © A. Poetzsch-Heffter, TU Kaiserslautern 202

Den Prozeduraufruf von main vernachlässigen wir 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 variables li $t1, 2

sw $t1, 0($sp) # set i to 2 li $t1, 1

sb $t1, 4($sp) # set res at sp +4 loop:

lw $t2, 0($sp) # load i into $t2 li $t3, -1 # load -1 into $t3 slt $t0, $t3, $t2 # $t3 < $t2 ?

beq $t0, $zero, exit # if i < -1 goto exit lw $t1, 4($sp) # load res from stack

(12)

Assembly and Machine Languages

MIPS Program (2)

add $t4, $sp, 5 # base address of array a add $t4, $t4, $t2 # add offset/ array index lb $t0, 0($t4) # load a[i]

add $t4, $sp, 8 # base address of array b add $t4, $t4, $t2 # add offset/ array index lb $t1, 0($t4) # load b[i]

beq $t0, $t1, equal # if a[i] == b[i]

sb $zero, 4($sp) # set res to 0

j after

equal:

add $t3, $zero, 1

sb $t3, 4($sp) # set res after:

subi $t2, $t2, 1 # i = i-1

sw $t2, 0($sp) # store i to $sp +4

j 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-1

sw $t2, 0($sp) # store i to $sp +4 afterif:

j loop # return to loop

exit:

addi $sp, $sp, 12 # reset stack pointer li $a0, 1 # terminated successfully li $v0, 17

syscall

(13)

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 the target 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 Syntax

MIPS

AST SL AST

MIPS Lexical and

Context-Free Analysis

Context- Dependent

Analysis

Translator Code Generator

(14)

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 variables complex types

addresses of memory regions index and offset computation

(15)

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 on the 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 source language, 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

(16)

Translation of Variables and Data Types

Memory Layout

The conceptional memory layout of most imperative programing languages and 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

high addresses

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.

(17)

Translation of Arrays (2)

Computation in MIPS LI $ti, k

MUL $ti, Ri, $ti ADD $ti, R_rel, $ti ADD $ti, $gp, $ti LOAD $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 than storing columns as second index is often incremented in inner loops.

(18)

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 ais 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 of the array in symbol table. Then it suffices to add 9$t1+ $t2 to base address.

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

(19)

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 implies

size(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 has to be generated.

(20)

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) - !"kui * size(i)

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 © A. Poetzsch-Heffter, TU Kaiserslautern 211

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) - !"

k

ui * size(i)

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 [ ]

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

(21)

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 is required.

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,_,_) ]

IndexExps IndexExp IndexExp

ADD(Ri,Ri, $gp) ADD(Ri, Ri,RA)

RA Ri

Ina Schaefer Translation to Target Language 42

(22)

Translation of Variables and Data Types

Code Generation for Array Access (5)

Um die Attributierungsbilder übersichtlicher zu gestalten, können Bezeichner für Attributwerte benutzt werden:

IndexExpElem rest(_) first(_)

CL + CR +

[ Mult2(W,Imm(_),RL) ] + [ Add2(W,RL,RR) ]

IndexExps

IndexExp RL CL RR CR

Zur Laufzeit braucht wieder nur der erste Summand berechnet werden. Dafür muss also Code generiert werden. Bei der schrittweisen Berechung kann auch eine Bereichsprüfung für das Feld vorgenommen werden.

Bemerkungen:

• Bei der Berechnung von Feldindizes gibt es häufig eine großes Potential für Optimierungen.

• Für die Übersetzung dynamischer Felder muss die Adressierung geeignet verallgemeinert werden

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 213

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)

(23)

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 in sense 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 stack

Memory representation and addressing of components is similar to static records.

(24)

Translation of Variables and Data Types

Implementation of Dynamic Objects (2)

Example:

Implementierung dynamischer Objekte

Dynamische Objekte werden hier als Sammelbegriff für Dynamische Objekte werden hier als Sammelbegriff für dynamisch 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 © A. Poetzsch-Heffter, TU Kaiserslautern 215

} ...

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 memory management:

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

(25)

Dynamic Memory Management (2)

General Problem: Provide memory blocks of different sizes from a linear 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 data size

header

free used

used 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 data size

header

free used

used 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.

(26)

Translation of Variables and Data Types

Dynamic Memory Management (4)

! Return pointer to memory cell after header (size information has to be kept.)

! If no memory area of required size is found, new memory has to be requested 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 size information can be destroyed with bad consequences.

If memory cannot be allocated in bytes, alignment restrictions have 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

(27)

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 Probleme anhand 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 | Eq

Relation Lt | Eq

Lt, Eq ( Exp left, Exp right ) IntConst ( Int i )

CharConst ( Char c )

ArrayAccess ( UsedId uid, Exp e ) i

Var ( 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 In Abhängigkeit des Ergebnisses springe einen Befehl an der 1 kellert bzw. der 0 kellert.

Dazu sind entsprechende Marken zu generieren.

Ina Schaefer Translation to Target Language 54

(28)

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 CR

Exp

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 CR

Exp

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

(29)

Translation of Expressions

Translation of Expressions (5)

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 CR

Exp

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 CR

Exp

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 221

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 CR

Exp

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 CR

Exp

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 221

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)]

(30)

Translation of Expressions

Translation of Expressions (7)

IntConst

[ Move( W, Imm( ), Predec(SP) ] [ Move( W, Imm(_), Predec(SP) ] Int

Var TV

if TV = int then

[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ] else // TV = char

else // TV char

[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId RA

ArrayAccess TV

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

UsedId RA CR

Exp

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 222

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

Var TV

if TV = int then

[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ] else // TV = char

else // TV char

[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId RA

ArrayAccess TV

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

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

(31)

Translation of Expressions

Translation of Expressions (9)

[ Move( W, Imm( ), Predec(SP) ] [ Move( W, Imm(_), Predec(SP) ] Int

Var TV

if TV = int then

[ Move( W, Bdisp(Reg(R0), RA), Predec(SP) ] else // TV = char

else // TV char

[ Conv( Bdisp(Reg(R0), RA), Predec(SP) ] UsedId RA

ArrayAccess TV

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 © A. Poetzsch-Heffter, TU Kaiserslautern 222

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

(32)

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 CS

Stat

© A. Poetzsch-Heffter, TU Kaiserslautern

g g g

Anweisungen und die effiziente Berücksichtigung von 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 efficient handling of non-strict expressions.

We consider the translation of non-strict Boolean expressions as an example of an optimizing translation and for the usage of context information.

Example: Abstract Syntax

Wir demonstrieren hier die Übersetzung nicht-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 B5 strikte 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!

nicht mehr ausgewertet werden braucht und darf!

Außerdem sollen Sprungkettenvermieden werden,

Ina Schaefer Translation to Target Language 64

(33)

Translation of Statements

More Complex Translation of Statements (2)

A program fragment:

nicht-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 B5 strikte 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

© A. Poetzsch-Heffter, TU Kaiserslautern

224

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 B2 evaluate to false, B3 may not be evaluated.

Further, jump cascades should be avoided, i.e. jumps to other unconditional 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: ')

(34)

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

IfThenElse M

“THEN“ + M

CB +

[ 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 © A. Poetzsch-Heffter, TU Kaiserslautern 225

BExp CB CT

Stat CE

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

IfThenElse M

“THEN“ + M

CB +

[ 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 © A. Poetzsch-Heffter, TU Kaiserslautern 225

BExp CB CT

Stat CE

Stat

(35)

More Complex Translation of Statements (6)

While M

[ 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 CS

Stat false

p

Not

BExp not(_)

And M

“BER“ + M CL +

false

CL +

[ Label( “BER“ + M ) ] + CR

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 226

BExp CL BExp CR

Ina Schaefer Translation to Target Language 69

Translation of Statements

More Complex Translation of Statements (7)

While M

[ 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 CS

Stat false

p

Not

BExp not(_)

And M

“BER“ + M CL +

false

CL +

[ Label( “BER“ + M ) ] + CR

BExp CL BExp CR

Ina Schaefer Translation to Target Language 70

(36)

Translation of Statements

More Complex Translation of Statements (8)

While M

[ 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 CS

Stat false

p

Not

BExp not(_)

And M

“BER“ + M CL +

false

CL +

[ Label( “BER“ + M ) ] + CR

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 226

BExp CL BExp CR

Ina Schaefer Translation to Target Language 71

Translation of Statements

More Complex Translation of Statements (9)

Or M

“BER“ + M CL +

true

BER M CL +

[ Label( “BER“ + M ) ] + CR

BExp CL BExp CR

StrictExp

CE +

[ Cmp( W, Imm(1), Postinc(SP) ) ] + TT FT JI

[ p( ( ) ( ) ) ]

( if JI then

[ Jeq( Label( TT) ) ] else

[ Jne( Label( FT) ) ] )

Exp CE

Bemerkung:

Falls nicht-strikte und strikte boolesche Ausdrücke gemischt sind, wird die Codegenerierung komplexer.

Beispiel: a = ( b && f(c) ) + g;

Ina Schaefer Translation to Target Language 72

(37)

Translation of Statements

More Complex Translation of Statements (10)

Or

“BER“ + M CL +

true

BER M CL +

[ Label( “BER“ + M ) ] + CR

BExp CL BExp CR

StrictExp

CE +

[ Cmp( W, Imm(1), Postinc(SP) ) ] + TT FT JI

[ p( ( ) ( ) ) ]

( if JI then

[ Jeq( Label( TT) ) ] else

[ Jne( Label( FT) ) ] )

Exp CE

Bemerkung:

Falls nicht-strikte und strikte boolesche Ausdrücke gemischt sind, wird die Codegenerierung komplexer.

12.06.2007 © A. Poetzsch-Heffter, TU Kaiserslautern 227

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 generation becomes more complex.

Example: a = ( b && f(c)) + g ;

Recommended Reading:

Wilhelm, Maurer: Sec. 2.4, pp. 12 –16

(38)

Translation of Procedures and Local Objects

Translation of Procedures and Local Objects

Most procedural languages support recursion, procedure-local variables 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 procedure incarnation.

Analogy:

Procedure declaration procedure incarnation

Class declaration object/class instance

(39)

Procedure Call Tree

The runtime behaviour of a procedural program can be described by a procedure call tree.

Example (C-Program):

Das Laufzeitverhalten eines prozeduralen Programms lässt sich durch den Prozeduraufrufbaumbeschreiben.

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 © A. Poetzsch-Heffter, TU Kaiserslautern 229

• 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.

(40)

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 stack frame 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 Frame

Structure of stack frame

For procedure with result, also memory has to be allocated. (Where?)

Referenzen

ÄHNLICHE DOKUMENTE

Analogue to parameters, also procedure-local variables have to be stored in the stack frame, because there is one instance of the local variables for each procedure

Analogue to parameters, also procedure-local variables have to be stored in the stack frame, because there is one instance of the local variables for each procedure

3.1 Translation of Imperative Language Constructs 3.1.1 Language Constructs of Procedural Language 3.1.2 Assembly and Machine Languages.. 3.1.3 Translation of Variables and Data

3.1 Translation of Imperative Language Constructs 3.1.1 Language Constructs of Procedural Language 3.1.2 Assembly and Machine Languages 3.1.3 Translation of Variables and Data

3.2 Translation of Object-Oriented Language Constructs 3.2.1 Concepts of Object-Oriented Programming Languages 3.2.2 Translation into Procedural Languages.. 3.2.3 Translation

The example demonstrates classes, object creation, inheritance (with subtyping and specialization) and dynamic method binding.. Ina Schaefer Translation to Target

Although macro constants are constrained to represent a valid constant value of a MAINSAIL data type, bracketed text macro bodies may represent almost any string of characters.

The crisis in eastern Ukraine has not changed the Czech Republic, Hungary and Slovakia’s perception of their relations with Russia, which continues to be defined primarily