Top Banner
1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University nowledgement to Norm Hutchinson whose slides this lecture is based on.
88

1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

1

Languages and Compilers(SProg og Oversættere)

Lecture 10

Bent Thomsen

Department of Computer Science

Aalborg University

With acknowledgement to Norm Hutchinson whose slides this lecture is based on.

Page 2: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

2

Where are we (going)?

Compiler Driver

Syntactic Analyzer

callscalls

Contextual Analyzer Code Generator

calls

Dependency diagram of a typical Multi Pass Compiler:

A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases.

input

Source Text

output

AST

input output

Decorated AST

input output

Object Code

Page 3: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

3

Code Generation

A compiler translates a program from a high-level language into an equivalent program in a low-level language.

A compiler translates a program from a high-level language into an equivalent program in a low-level language.

TAM Program

Triangle Program

Compile

Run

Result

JVM Program

Java Program

Compile

Run

Result

x86 Program

C Program

Compile

Run

Result

We shall look at this in more detail the next couple of lectures

Page 4: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

4

Triangle Abstract Machine Architecture

• TAM is a stack machine– There are no data registers as in register machines.– The temporary data are stored in the stack.

• But, there are special registers (Table C.1 of page 407)

• TAM Instruction Set– Instruction Format (Figure C.5 of page 408)– op: opcode (4 bits)

• r: special register number (4 bits)• n: size of the operand (8 bits)• d: displacement (16 bits)

• Instruction Set– Table C.2 of page 409

Page 5: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

5

TAM Registers

Page 6: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

6

TAM Code

• Machine code are 32 bits instructions in the code store– op (4 bits), type of instruction

– r (4 bits), register

– n (8 bits), size

– d (16 bits), displacement

• Example: LOAD (1) 3[LB]:– op = 0 (0000)

– r = 8 (1000)

– n = 1 (00000001)

– d = 3 (0000000000000011)

• 0000 1000 0000 0001 0000 0000 0000 0011

Page 7: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

7

TAM Instruction set

Page 8: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

8

TAM Architecture

• Two Storage Areas– Code Store (32 bit words)

• Code Segment: to store the code of the program to run– Pointed to by CB and CT

• Primitive Segment: to store the code for primitive operations– Pointed to by PB and PT

– Data Store (16 bit words)• Stack

– global segment at the base of the stack» Pointed to by SB

– stack area for stack frames of procedure and function calls» Pointed to by LB and ST

• Heap– heap area for the dynamic allocation of variables

» Pointed to by HB and HT

Page 9: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

9

TAM Architecture

Page 10: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

10

Expression Evaluation on a Stack Machine (SM)

On a stack machine, the intermediate results are stored on a stack. Operations take their arguments from the top of the stack and put the result back on the stack.

Stack machine:

Typical Instructions:

STORE aLOAD xMULTSUBADD

Stack machine: • Very natural for expression evaluation

(see examples on next two pages).• Requires more instructions for the

same expression, but the instructions are simpler.

Page 11: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

11

Expression Evaluation on a Stack Machine

Example 1: Computing (a * b) + (1 - (c * 2))on a stack machine.

LOAD a //stack: aLOAD b //stack: a bMULT //stack: (a*b)LOAD #1 //stack: (a*b) 1LOAD c //stack: (a*b) 1 cLOAD #2 //stack: (a*b) 1 c 2MULT //stack: (a*b) 1 (c*2)SUB //stack: (a*b) (1-(c*2))ADD //stack: (a*b)+(1-(c*2))

LOAD a //stack: aLOAD b //stack: a bMULT //stack: (a*b)LOAD #1 //stack: (a*b) 1LOAD c //stack: (a*b) 1 cLOAD #2 //stack: (a*b) 1 c 2MULT //stack: (a*b) 1 (c*2)SUB //stack: (a*b) (1-(c*2))ADD //stack: (a*b)+(1-(c*2))

Note the correspondence between the instructions and the expression written in postfix notation: a b * 1 c 2 * - +

Page 12: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

12

Expression Evaluation on a Stack Machine

Example 2: Computing (0 < n) && odd(n)on a stack machine.

LOAD #0 //stack: 0LOAD n //stack: 0 nLT //stack: (0<n)LOAD n //stack: (0<n) nCALL odd //stack: (0<n) odd(n)AND //stack: (0<n)&&odd(n)

LOAD #0 //stack: 0LOAD n //stack: 0 nLT //stack: (0<n)LOAD n //stack: (0<n) nCALL odd //stack: (0<n) odd(n)AND //stack: (0<n)&&odd(n)

This example illustrates that calling functions/procedures fits in just as naturally with the stack machine evaluation model as operations that correspond to machine instructions.

In register machines this is much more complicated, because a stack must be created in memory for managing subroutine calls/returns.

Page 13: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

14

The “Phases” of a Compiler

Syntax Analysis

Contextual Analysis

Code Generation

Source Program

Abstract Syntax Tree

Decorated Abstract Syntax Tree

Object Code

Error Reports

Error Reports

Next lecture

Page 14: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

15

Storage Allocation

A compiler translates a program from a high-level language into an equivalent program in a low-level language.

A compiler translates a program from a high-level language into an equivalent program in a low-level language.

The low level program must be equivalent to the high-level program.=> High-level concepts must be modeled in terms of the low-level machine.

This lecture is not about the code generation phase itself, but about the way we represent high-level structures in terms of a typical low-level machine’s memory architecture and machine instructions.

=> We need to know this before we can talk about code generation.

Page 15: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

16

What This Lecture is About

High Level Program

Low-level Language Processor

How to model high-level computational structures and data structures in terms of low-level memory and machine instructions.

Procedures

Expressions

VariablesArrays

Records

Objects

Methods

Registers

Machine Instructions

Bits and BytesMachine Stack

How to model ?

Page 16: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

17

Data Representation

• Data Representation: how to represent values of the source language on the target machine.

Records

ArraysStrings

Integer

Char

?

00..10

01..00

...

High level data-structures

0:1:2:3:

Low level memory model

wordword

Note: addressing schema and size of “memory units” may vary

Page 17: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

18

Data Representation

Important properties of a representation schema:• non-confusion: different values of a given type should have

different representations• uniqueness: Each value should always have the same

representation.

These properties are very desirable, but in practice they are not always satisfied:Example: • confusion: approximated floating point numbers.• non-uniqueness: one’s complement representation of integers

+0 and -0

Page 18: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

19

Data Representation

Important issues in data representation:

• constant-size representation: The representation of all values of a given type should occupy the same amount of space.

• direct versus indirect representation

x bit pattern x bit pattern•

handle

Direct representationof a value x

Indirect representationof a value x

Page 19: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

20

Indirect Representation

small x bit pattern

Q: What reasons could there be for choosing indirect representations?

To make the representation “constant size” even if representation requires different amounts of memory for different values.

big x bit pattern

Both are represented by pointers

=>Same size

Page 20: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

21

Indirect versus Direct

The choice between indirect and direct representation is a key decision for a language designer/implementer.

• Direct representations are often preferable for efficiency:• More efficient access (no need to follow pointers)• More efficient “storage class” (e.g stack rather than heap

allocation)• For types with widely varying size of representation it is almost

a must to use indirect representation (see previous slide)

Languages like Pascal, C, C++ try to use direct representation wherever possible.

Languages like Scheme, ML use mostly indirect representation everywhere (because of polymorphic higher order functions)

Java: primitive types direct, “reference types” indirect, e.g. objects and arrays.

Page 21: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

22

Data Representation

We now survey representation of the data types found in Triangle, assuming direct representations wherever possible.

We will discuss representation of values of:• Primitive Types• Record Types• Static Array Types

We will use the following notations (if T is a type): #[T] The cardinality of the type (i.e. the number of possible values)size[T] The size of the representation (in number of bits/bytes)

Page 22: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

23

Data Representation: Primitive Types

What is a primitive type?The primitive types of a programming language are those types that cannot be decomposed into simpler types. For example integer, boolean, char, etc.

Type: booleanHas two values true and false=> #[boolean] = 2=> size[boolean] ≥ 1 bit

Note: In general if #[T] = n then size[T] ≥ log2n bits

Valuefalsetrue

Possible Representation1bit byte(option 1) byte(option2)0 00000000 000000001 00000001 11111111

Page 23: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

24

Data Representation: Primitive Types

Type: integerFixed size representation, usually dependent (i.e. chosen based on) what is efficiently supported by target machine. Typically uses one word (16 bits, 32 bits, or 64 bits) of storage.

size[integer] = word (= 16 bits)=> # [integer] ≤ 216 = 65536

Modern processors use two’s complement representation of integers

1 0 0 0 0 1 0 0 1 0 0 1 0 1 1 1

Multiply with -(215) Multiply with 2n

Value = -1.215 +0.214 +…+0.23+1.22 +1.21 +1.20

n = position from left

Page 24: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

25

Data Representation: Primitive Types

Example: Primitive types in TAM

TypeBooleanCharInteger

Representation00...00 and 00...01 UnicodeTwo’s complement

Size1 word1 word1 word

Example: A (possible) representation of primitive types on a Pentium

TypeBooleanCharInteger

Representation00...00 and 11..11 ASCIITwo’s complement

Size1 byte1 byte1 word

Page 25: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

26

Data Representation: Composite Types

Composite types are types which are not “atomic”, but which are constructed from more primitive types.

• Records (called structs in C)Aggregates of several values of several different types

• ArraysAggregates of several values of the same type

• Variant Records or Disjoint Unions• (Pointers or References)• (Objects)• (Functions)

Page 26: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

27

Data Representation: Records

Example: Triangle Records

type Date = recordy : Integer,m : Integer,

d : Integer end;type Details = record

female : Boolean,dob : Date,status : Char

end;var today: Date;var my: Details

type Date = recordy : Integer,m : Integer,

d : Integer end;type Details = record

female : Boolean,dob : Date,status : Char

end;var today: Date;var my: Details

Page 27: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

28

Data Representation: Records

Example: Triangle Record Representation

today.m

2002

2

today.y

today.d 5

my.dob.m

1970

5

my.dob.y

my.dob.d 17

false

‘u’

my.female

my.dob

my.status

…1 word:

Page 28: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

29

Data Representation: Records

Records occur in some form or other in most programming languages:Ada, Pascal, Triangle (here they are actually called records)C, C++, C# (here they are called structs).

The usual representation of a record type is just the concatenation of individual representations of each of its component types.

r.I1

r.I2

r.In

value of type T1

value of type T2

value of type Tn

Page 29: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

30

Data Representation: Records

Example:size[Date] = 3*size[integer] = 3 wordsaddress[today.y] = address[today]+0address[today.m] = address[today]+1address[today.d] = address[today]+2

address[my.dob.m] = address[my.dob]+1 = address[my]+2

Q: How much space does a record take up? And how to access record elements?

Note: these formulas assume that addresses are indexes of words (not bytes) in memory (otherwise multiply offsets by 2)

Page 30: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

31

Data Representation: Disjoint Unions

What are disjoint unions?Like a record, has elements which are of different types. But the elements never exist at the same time. A “type tag” determines which of the elements is currently valid.

Example: Pascal variant records

type Number = record case discrete: Boolean of

true: (i: Integer); false: (r: Real) end;var num: Number

type Number = record case discrete: Boolean of

true: (i: Integer); false: (r: Real) end;var num: Number

Mathematically we write disjoint union types as: T = T1 | … | Tn

Page 31: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

32

Data Representation: Disjoint UnionsExample: Pascal variant records representation

type Number = record case discrete: Boolean of

true: (i: Integer); false: (r: Real) end;var num: Number

type Number = record case discrete: Boolean of

true: (i: Integer); false: (r: Real) end;var num: Number

Assuming size[Integer]=size[Boolean]=1 and size[Real]=2, thensize[Number] = size[Boolean] + MAX(size[Integer], size[Real]) = 1 + MAX(1, 2) = 3

num.i

true

15

num.discrete

unused

num.r

falsenum.discrete

3.14

Page 32: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

33

Data Representation: Disjoint Unions

type T = record case Itag: Ttag of v1: (I1: T1); v2: (I2: T2); ... vn: (In: Tn);end;var u: T

type T = record case Itag: Ttag of v1: (I1: T1); v2: (I2: T2); ... vn: (In: Tn);end;var u: T

v1

type T1

v2

type T2

vn

type Tnor …

u.I1 u.I2

u.Itag

u.In

u.Itag u.Itag

or or

size[T] = size[Ttag] + MAX(size[T1], ..., size[Tn])

address[u.Itag ] = address[u]

address[u.I1] = address[u]+size[Ttag] ...address[u.In] = address[u]+size[Ttag]

Page 33: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

34

Arrays

An array is a composite data type, an array value consists of multiple values of the same type. Arrays are in some sense like records, except that their elements all have the same type.

The elements of arrays are typically indexed using an integer value (In some languages such as for example Pascal, also other “ordinal” types can be used for indexing arrays).

Two kinds of arrays (with different runtime representation schemas):• static arrays: their size (number of elements) is known at

compile time.• dynamic arrays: their size can not be known at compile time

because the number of elements may vary at run-time.

Q: Which are the “cheapest” arrays? Why?

Page 34: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

35

Static Arrays

Example:type Name = array 6 of Char; var me: Name;var names: array 2 of Name

type Name = array 6 of Char; var me: Name;var names: array 2 of Name

‘K’‘r’‘i’‘s’‘ ’‘ ’

me[0]me[1]

me[2]

me[3]

me[4]

me[5]

‘J’‘o’‘h’‘n’‘ ’‘ ’

names[0][0]names[0][1]

names[0][2]

names[0][3]

names[0][4]

names[0][5]

Name

‘S’‘o’‘p’‘h’‘i’‘a’

names[1][0]names[1][1]

names[1][2]

names[1][3]

names[1][4]

names[1][5]

Name

Page 35: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

36

Static Arrays

Example:

type Coding = record Char c, Integer n

end

var code: array 3 of Coding

type Coding = record Char c, Integer n

end

var code: array 3 of Coding

‘K’5

code[0].ccode[0].n Coding

‘i’22

code[1].ccode[1].n Coding

‘d’4

code[2].ccode[2].n Coding

Page 36: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

37

Static Arrays

type T = array n of TE; var a : T;

type T = array n of TE; var a : T;

a[0]

a[1]

a[2]

a[n-1]

size[T] = n * size[TE]

address[a[0]] = address[a]address[a[1]] = address[a]+size[TE]address[a[2]] = address[a]+2*size[TE]…address[a[i] ] = address[a]+i*size[TE]…

Page 37: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

38

Dynamic Arrays

char[ ] buffer;

buffer = new char[buffersize];

... for (int i=0; i<buffer.length; i++) buffer[i] = ‘ ’;

char[ ] buffer;

buffer = new char[buffersize];

... for (int i=0; i<buffer.length; i++) buffer[i] = ‘ ’;

Example: Java Arrays (all arrays in Java are dynamic)

Dynamic arrays are arrays whose size is not known until run time.

Dynamic array: no size given in declaration

Array creation at runtime determines size

Can ask for size of an array at run time

Q: How could we represent Java arrays?

Page 38: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

39

Dynamic Arrays

char[ ] buffer;

buffer = new char[len];

char[ ] buffer;

buffer = new char[len];

Java Arrays

‘C’‘o’

buffer[0]

buffer[1]

‘m’ buffer[2]

buffer[3]‘p’

A possible representation for Java arrays

7•

buffer[4]‘i’buffer[5]‘l’buffer[6]‘e’

buffer.length

buffer.origin

Page 39: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

40

Dynamic Arrays

char[ ] buffer;

buffer = new char[len];

char[ ] buffer;

buffer = new char[len];

Java Arrays

‘C’‘o’

buffer[0]

buffer[1]

‘m’ buffer[2]

buffer[3]‘p’

Another possible representation for Java arrays

7•

buffer[4]‘i’buffer[5]‘l’buffer[6]‘e’

buffer.lengthbuffer

Note: In reality Java also stores a type in its representation for arrays, because Java arrays are objects (instances of classes).

Page 40: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

41

Static Storage Allocation

Example: Global variables in Triangle

let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date;in ...

let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date;in ...

Exist as long as program is running

Compiler can:• compute exactly how much memory is needed for globals.• allocate memory at a fixed position for each global variable.

Page 41: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

42

Static Storage Allocation

address[a] = 0address[b] = 3address[c] = 4address[t] = 5

a[0]

a[1]

a[2]

a

bc

t.y

t.m

t.d

t

let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date;

let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date;

Example: Global variables in Triangle

Page 42: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

43

Stack Storage Allocation

let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y() ~ let var d: Integer; var e: ... in ... ; proc Z() ~ let var f: Integer; in begin ...; Y(); ... endin begin ...; Y(); ...; Z(); end

let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y() ~ let var d: Integer; var e: ... in ... ; proc Z() ~ let var f: Integer; in begin ...; Y(); ... endin begin ...; Y(); ...; Z(); end

Example: When do the variables in this program “exist”

as long as the program is

running

when procedure Y is active

when procedure Z is active

Now we will look at allocation of local variables

Page 43: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

44

Stack Storage Allocation

Start of program End of program time

call depth

global

Y Z1

2 Y

Z

1) Procedure activation behaves like a stack (LIFO). 2) The local variables “live” as long as the procedure they are declared in.1+2 => Allocation of locals on the “call stack” is a good model.

A “picture” of our program running:

Page 44: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

45

Stack Storage Allocation: Accessing locals/globals

First time around, we assume that in a procedure only local variables declared in that procedure and global variables are accessible.We will extend on this later to include nested scopes and parameters.

A stack allocation model (under the above assumption):• Globals are allocated at the base of the stack. • Stack contains “frames”.

• Each frame corresponds to a currently active procedure. (It is often called an “activation frame”)

• When a procedure is called (activated) a frame is pushed on the stack

• When a procedure returns, its frame is popped from the stack.

Page 45: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

46

Stack Storage Allocation: Accessing locals/globals

SB

LB

ST

callframe

SB = Stack baseLB = Locals baseST = Stack top

callframe Dynamic link

globals

Page 46: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

47

What’s in a Frame?

A frame contains• A dynamic link: to next frame on

the stack (the frame of the caller)• Return address• Local variables for the current

activation

return address

locals

Link data

Local data

LB

ST

dynamic link

Page 47: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

48

What happens when a procedure is called?

LB

ST

SB = Stack baseLB = Locals baseST = Stack top

callframe

callframe

When procedure f() is called• push new f() call frame

on top of stack.• Make dynamic link in new

frame point to old LB• Update LB (becomes old ST)

new callframe

for f()

Page 48: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

49

What happens when a procedure returns?

LB

ST

When procedure f() returns• Update LB (from dynamic

link)• Update ST (to old LB)

currentcall

frame for f()

currentcall

frame for f()

callframe

Note, updating the ST implicitly “destroys” or “pops” the frame.

Page 49: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

50

Accessing global/local variables

Q: Is the stack frame for a procedure always at the same position on the stack?

A: No, look at the picture of procedure activation below. Imagine what the stack looks like at each point in time.

time

call depth

global

Y Z1

2 Y

Z

G

Y

G

Z

Y

Page 50: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

51

Accessing global/local variables

The global frame is always at the same place in the stack.=> Address global variables relative to SBA typical instruction to access a global variable: LOAD 4[SB]

Frames are not always at the same position in the stack.Depends on the number of frames already on the stack.=> Address local variables relative to LBA typical instruction to access a local variable: LOAD 3[LB]

RECAP: We are still working under the assumption of a “flat” block structure

How do we access global and local variables on the stack?

Page 51: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

52

Accessing global/local variables

Example: Compute the addresses of the variables in this program

let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y() ~ let var d: Integer; var e: ... in ... ; proc Z() ~ let var f: Integer; in begin ...; Y(); ... endin begin ...; Y(); ...; Z(); end

let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y() ~ let var d: Integer; var e: ... in ... ; proc Z() ~ let var f: Integer; in begin ...; Y(); ... endin begin ...; Y(); ...; Z(); end

Var Size Address

abc

de

f

311

[0]SB[3]SB[4]SB

1?

1

[2]LB[3]LB

[2]LB

Page 52: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

53

Accessing non-local variables

RECAP: We have discussed• stack allocation of locals in the call frames of procedures• Some other things stored in frames:

• A dynamic link: pointing to the previous frame on the stack=> corresponds to the “caller” of the current procedure.

• A return address: points to the next instruction of the caller.• Addressing global variables relative to SB (stack base)• Addressing local variables relative to LB (locals base)

Now… we will look at accessing non-local variables.

Or in other words. We will look into the question:

How does lexical scoping work?

Page 53: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

54

Accessing non-local variables: what is this about?

Example: How to access p1,p2 from within Q or S?

let var g1: array 3 of Integer; var g2: Boolean; proc P() ~ let var p1,p2 proc Q() ~ let var q:... in ... ; proc S() ~ let var s: Integer; in ... in ...in ...

let var g1: array 3 of Integer; var g2: Boolean; proc P() ~ let var p1,p2 proc Q() ~ let var q:... in ... ; proc S() ~ let var s: Integer; in ... in ...in ...

Scope Structurevar g1,g2proc P()

var p1,p2proc Q()

proc S()

var q

Q: When inside Q, does the dynamic link always point to frame of P?

var s

Page 54: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

55

Accessing non-local variables

Q: When inside Q, does the dynamic link always point to a frame P?A: No! Consider the following scenarios:

var g1,g2proc P()

var p1,p2proc Q()

proc S()

var q

var s

timeG

1 P

S

Q

timeG

P1

2

3

Q2

3P

S

Q

P

Q

Page 55: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

56

Accessing non-local variables

We can not rely on the dynamic links to get to the lexically scoped frame(s) of a procedure.

=> Another item is added in the link data: the static link.

The static link in a frame points to the next lexically scoped frame somewhere higher on the stack.

Registers L1, L2, etc. are used to point to the lexical scoped frames. (L1, is most local). A typical instruction for accessing a non-local variable looks like: LOAD [4]L1 LOAD [3]L2

These + LB and SB are called the display registers

Page 56: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

57

What’s in a Frame (revised)?

A frame contains• A dynamic link: to next frame on

the stack (the frame of the caller)• Return address• Local variables for the current

activation

static link

locals

Link data

Local data

LB

ST

dynamic link

return address

Page 57: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

58

Accessing non-local variables

proc P()

proc Q() ...proc S() ...

timeG

P

S

Q

LB

ST

P()frame

globalsSB

S()frame

Q()frame

Dynamic L.

Static Link

L1

Page 58: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

59

Accessing variables, addressing schemas overview

We now have a complete picture of the different kinds of addresses that are used for accessing variables stored on the stack.

Type of variable

Global

Local

Non-local, 1 level up

Non-local, 2 levels up

...

Load instruction

LOAD offset[SB]

LOAD offset[LB]

LOAD offset[L1]

LOAD offset[L2]

Page 59: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

60

Routines

We call the assembly language equivalent of procedures “routines”.

In the preceding material we already learned some things about the implementation of routines in terms of the stack allocation model:

• Addressing local and globals through LB,L1,L2,… and SB

• Link data: static link, dynamic link, return address.

We have yet to learn how the static link and the L1, L2, etc. registers are set up.

We have yet to learn how routines can receive arguments and return results from/to their caller.

Page 60: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

61

Routines

We call the assembly language equivalent of procedures “routines”.

What are routines? Unlike procedures/functions in higher level languages. They are not directly supported by language constructs. Instead they are modeled in terms of how to use the low-level machine to “emulate” procedures.

What behavior needs to be “emulated”?• Calling a routine and returning to the caller after completion.• Passing arguments to a called routine• Returning a result from a routine• Local and non-local variables.

Page 61: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

62

Routines

• Transferring control to and from routine:Most low-level processors have CALL and RETURN for transferring control from caller to callee and back.

• Transmitting arguments and return values:Caller and callee must agree on a method to transfer argument and return values. => This is called the “routine protocol”

There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee.

There are many possible ways to pass argument and return values. => A routine protocol is like a “contract” between the caller and the callee.

!The routine protocol is often dictated by the operating system.

Page 62: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

63

Routine Protocol Examples

The routine protocol depends on the machine architecture (e.g. stack machine versus register machine).

Example 1: A possible routine protocol for a RM- Passing of arguments:

first argument in R1, second argument in R2, etc.- Passing of return value:

return the result (if any) in R0

Note: this example is simplistic:- What if more arguments than registers?- What if the representation of an argument is larger than can be stored in a register.

For RM protocols, the protocol usually also specifies who (caller or callee) is responsible for saving contents of registers.

Page 63: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

64

Routine Protocol Examples

Example 2: A possible routine protocol for a stack machine

- Passing of arguments: pass arguments on the top of the stack.

- Passing of return value:leave the return value on the stack top, in place of the arguments.

Note: this protocol puts no boundary on the number of arguments and the size of the arguments.

Most micro-processors, have registers as well as a stack. Such “mixed” machines also often use a protocol like this one.

The Triangle Abstract Machine also adopts this routine protocol.We now look at it in detail (in TAM).

Page 64: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

65

TAM: Routine Protocol

SB

LB

ST

globals

just before the call just after the call

args

SB

LB

ST

globals

result

What happens in between?

Page 65: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

66

TAM: Routine Protocol

LB

ST

(1) just before the call

args

(2) just after entry

LB

ST

args

link data

note: Going from (1) -> (2) in TAM is the execution of a single CALL instruction.

Page 66: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

67

TAM: Routine Protocol

(2) just after entry

LB

ST

args

link data

(3.1) during execution of routine

LB

ST

args

link data

localdata

shrinks and grows during execution

Page 67: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

68

TAM: Routine Protocol

(3.2) just before return

LB

ST

args

link data

localdata

result

(4) just after return

LB

ST result

note: Going from (3.2) -> (4) in TAM is the execution of a single RETURN instruction.

Page 68: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

69

TAM: Routine Protocol, Example

let var g: Integer; func F(m: Integer, n: Integer) : Integer ~ m*n ; proc W(i:Integer) ~ let const s ~ i*i in begin putint(F(i,s)); putint(F(s,s)) endin begin getint(var g); W(g+1)end

let var g: Integer; func F(m: Integer, n: Integer) : Integer ~ m*n ; proc W(i:Integer) ~ let const s ~ i*i in begin putint(F(i,s)); putint(F(s,s)) endin begin getint(var g); W(g+1)end

Triangle Program

Page 69: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

70

TAM: Routine Protocol, Example

PUSH 1 -- expand globals make place for gLOADA 0[SB] -- push address of gCALL getint -- read integer into gCALL succ -- add 1CALL(SB) W -- call W (using SB as static link)POP 1 -- contract globalsHALT

PUSH 1 -- expand globals make place for gLOADA 0[SB] -- push address of gCALL getint -- read integer into gCALL succ -- add 1CALL(SB) W -- call W (using SB as static link)POP 1 -- contract globalsHALT

TAM assembly code:

let var g: Integer; ... in begin getint(var g); W(g+1)end

let var g: Integer; ... in begin getint(var g); W(g+1)end

Page 70: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

71

TAM: Routine Protocol, Example

F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return replacing 2 word argument pair by 1 word result

F: LOAD -2[LB] -- push value of argument m LOAD -1[LB] -- push value of argument n CALL mult -- multiply m and n RETURN(1) 2 -- return replacing 2 word argument pair by 1 word result

func F(m: Integer, n: Integer) : Integer ~ m*n ; func F(m: Integer, n: Integer) : Integer ~ m*n ;

arguments addressed relative to LB (negative offsets!)

Size of return value and argument space needed for updating the stack on return from call.

Page 71: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

72

TAM: Routine Protocol, Example

W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local var s CALL(SB) F -- call F (use SB as static link) … RETURN(0) 1 -- return, replacing 1 word argument by 0 word result

W: LOAD -1[LB] -- push value of argument i LOAD -1[LB] -- push value of argument i CALL mult -- multiply: result is value of s LOAD -1[LB] -- push value of argument i LOAD 3[LB] -- push value of local var s CALL(SB) F -- call F (use SB as static link) … RETURN(0) 1 -- return, replacing 1 word argument by 0 word result

proc W(i: Integer) ~ let const s~i*i in ... F(i,s) ...

proc W(i: Integer) ~ let const s~i*i in ... F(i,s) ...

Page 72: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

73

TAM: Routine Protocol, Example

let var g: Integer; ... in begin getint(var g); W(g+1)end

let var g: Integer; ... in begin getint(var g); W(g+1)end

SB g 3

after reading g

3

just before call to W

4STSB g

ST arg #1

Page 73: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

74

TAM: Routine Protocol, Example

proc W(i: Integer) ~ let const s~i*i in ... F(i,s) ...

proc W(i: Integer) ~ let const s~i*i in ... F(i,s) ...

just after entering W

34

SB g

LB arg #1

STlinkdata

just after computing s

34

SB g

LB arg i

ST

linkdata16

just before calling F

34

SB g

LB arg i

ST

linkdata164

16

s

arg #1arg #2

static link dynamic link

Page 74: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

75

TAM: Routine Protocol, Example

func F(m: Integer, n: Integer) : Integer ~ m*n ; func F(m: Integer, n: Integer) : Integer ~ m*n ;

just before calling F

34

SB g

LB arg i

ST

linkdata164

16

s

arg #1arg #2

just after entering F

34

SB g

LB

arg i

ST

linkdata164

16

s

arg marg n

linkdata

just before return from F

34

SB g

LB

arg i

ST

linkdata164

16

s

arg marg n

linkdata64

Page 75: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

76

TAM: Routine Protocol, Example

func F(m: Integer, n: Integer) : Integer ~ m*n ; func F(m: Integer, n: Integer) : Integer ~ m*n ;

just before return from F

34

SB g

LB

arg i

ST

linkdata164

16

s

arg marg n

linkdata64

after return from F

34

SB g

LB arg i

ST

linkdata1664

s …

Page 76: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

77

TAM Routine Protocol: Frame Layout Summary

LB

ST

local variablesand intermediate

results

dynamic linkstatic link

return address

Local data, grows and shrinksduring execution.

Link data

arguments Arguments for current procedurethey were put here by the caller.

Page 77: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

78

Accessing variables, addressing schemas overview(Revised)

We now have a complete picture of the different kinds of addresses that are used for accessing variables and formal parameters stored on the stack.

Type of variable

Global

Local

Parameter

Non-local, 1 level up

Non-local, 2 levels up...

Load instruction

LOAD +offset[SB]

LOAD +offset[LB]

LOAD -offset[LB]

LOAD +offset[L1]

LOAD +offset[L2]

Page 78: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

79

Arguments: by value or by reference

Some programming languages allow two kinds of function/procedure parameters.

Example: in Triangle (similar in Pascal)

let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end;in begin b := {y~2002, m ~ 2, d ~ 22}; S(var b.m, 6);end

let proc S(var n:Integer, i:Integer) ~ n:=n+i; var today: record y:integer, m:Integer, d:Integer end;in begin b := {y~2002, m ~ 2, d ~ 22}; S(var b.m, 6);end

Constant/Value parameterVar/reference parameter

Page 79: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

80

Arguments: by value or by reference

Value parameters:At the call site the argument is an expression, the evaluation of that expression leaves some value on the stack. The value is passed to the procedure/function.A typical instruction for putting a value parameter on the stack:LOADL 6

Var parameters:Instead of passing a value on the stack, the address of a memory location is pushed. This implies a restriction that only “variable-like” things can be passed to a var parameter. In Triangle there is an explicit keyword var at the call-site, to signal passing a var parameter. In Pascal and C++ the reference is created implicitly (but the same restrictions apply). Typical instructions: LOADA 5[LB] LOADA 10[SB]

Page 80: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

81

Recursion

How are recursive functions and procedures supported on a low-level machine?=> Surprise! The stack memory allocation model already works!Example:

let func fac(n:Integer) ~ if (n=1) then 1 else n*fac(n-1);in begin putint(fac(6));end

let func fac(n:Integer) ~ if (n=1) then 1 else n*fac(n-1);in begin putint(fac(6));end

why does it work? because every activation of a function gets its own activation record on the stack, with its own parameters, locals etc.=> procedures and functions are “reentrant”. Older languages (e.g. FORTRAN) which use static allocation for locals have problems with recursion.

Page 81: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

82

Recursion: General Idea

Why the stack allocation model works for recursion:Like other function/procedure calls, lifetimes of local variables and parameters for recursive calls behave like a stack.

fac(3)

fac(2)

fac(1)

fac(4) fac(4)

fac(3)

fac(2)

fac(4)fac(4)

fac(3) fac(3)

fac(2)

fac(2)

fac(1)

fac(3)

fac(2)

fac(4)

fac(3)?

?

fac(4)

Page 82: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

83

Recursion: In Detail

let func fac(n:Integer) ~ if (n=1) then 1 else n*fac(n-1);in begin putint(fac(6));end

let func fac(n:Integer) ~ if (n=1) then 1 else n*fac(n-1);in begin putint(fac(6));end

SB arg 1 6ST

before call to facSB arg 1 6

ST

right after enteringfac

linkdata

SB arg n 6

right before recursivecall to fac

linkdata

LB

ST

LB

6value of n5arg 1:value of n-1

Page 83: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

84

Recursion

SB arg n 6

right before recursivecall to fac

linkdata

ST

LB

6value of n5arg

SB arg n 6

right before next recursive call to fac

linkdata

ST

LB

6value of n5arg n

linkdata

54

value of narg

SB arg n 6

right before next recursive call to fac

linkdata

LB

6value of n5arg n

linkdata

54

value of narg

linkdata

43ST

value of narg

Page 84: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

85

Recursion

LB

ST

Is the spaghetti of static and dynamic links getting confusing?

Let’s zoom in on just a single activation of the fac procedure. The pattern is always the same:

argument n

link data

to caller context (= previous LB)to lexical context (=SB)

nn-1

Intermediate results in the computation of n*fac(n-1);

?

just before recursive call in fac

Page 85: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

86

Recursion

LB

ST

link data

result = 1

just before the return from the “deepest call”: n=1 after return from deepest call

LB

STresult=1

?caller frame

(what’s in here?)

argument n=2

link data

n=2 Next step:multiplyargument n=1

Page 86: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

87

Recursion

just before the return from the “deepest call”: n=1

after return from deepest call and multiply

LB

ST?

caller frame(what’s in here?)

argument n=2

link data

2*fac(1)=2 Next step:return

to caller contextto lexical context (=SB)

result

From here on down the stack is shrinking,multiplying each time with a bigger n

Page 87: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

88

Recursion

LB

ST

argument n

link data

nrecurs. arg: n-1

just before recursive call in fac

LB

ST

argument n

link data

nfac(n-1)

after completion of the recursivecall

Calling a recursive function is just like calling any other function. After completion it just leaves its result on the top of the stack!A recursive call can happen in the midst of expression evaluation.Intermediate results. local variables, etc. simply remain on the stack and computation proceeds when the recursive call is completed.

Page 88: 1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.

89

Summary

• Data Representation: how to represent values of the source language on the target machine.

• Storage Allocation: How to organize storage for variables (considering different lifetimes of global, local and heap variables)

• Routines: How to implement procedures, functions (and how to pass their parameters and return values)