31
Runtime Environments Runtime Environments Compiler Construction Compiler Construction Chapter 7 Chapter 7

Runtime Environments Compiler Construction Chapter 7

Embed Size (px)

Citation preview

Page 1: Runtime Environments Compiler Construction Chapter 7

Runtime EnvironmentsRuntime Environments

Compiler ConstructionCompiler Construction

Chapter 7Chapter 7

Page 2: Runtime Environments Compiler Construction Chapter 7

In This LectureIn This Lecture

Runtime EnvironmentsRuntime EnvironmentsLanguage Features for different Language Features for different

EnvironmentsEnvironmentsScoping and AllocationScoping and AllocationProcedure CallsProcedure CallsParameter PassingParameter Passing

Page 3: Runtime Environments Compiler Construction Chapter 7

Runtime EnvironmentsRuntime Environments

Three typesThree typesFully StaticFully Static

Fortran77Fortran77

Stack-basedStack-basedC, C++, Pascal, AdaC, C++, Pascal, Ada

Fully DynamicFully DynamicLISPLISP

Page 4: Runtime Environments Compiler Construction Chapter 7

Memory OrganizationMemory Organization

Code for Proc 1

Code for Proc 2

Code for Proc n

Entry Point for Proc 1

Code memory

Entry Point for Proc 2

Entry Point for Proc n

Page 5: Runtime Environments Compiler Construction Chapter 7

Static Code and DataStatic Code and Data The code area of a program is fixed prior to executionThe code area of a program is fixed prior to execution Thus the address for code can be computed at compile Thus the address for code can be computed at compile

timetime Actually, the location of the code is usually relative to some base Actually, the location of the code is usually relative to some base

register, or some other memory addressing schemeregister, or some other memory addressing scheme The address for data (variables etc.) is in general not The address for data (variables etc.) is in general not

assigned fixed locations at compile timeassigned fixed locations at compile time Static data however, does have a fixed memory address Static data however, does have a fixed memory address

and is known at compile timeand is known at compile time Constants can also be assigned a fixed addressConstants can also be assigned a fixed address

This is usually reserved for strings and other large constantsThis is usually reserved for strings and other large constants Other constants like ‘0’ or ‘1’ are inserted directly into the codeOther constants like ‘0’ or ‘1’ are inserted directly into the code

Page 6: Runtime Environments Compiler Construction Chapter 7

Dynamic Data AllocationDynamic Data Allocation

The memory for dynamic data is typically The memory for dynamic data is typically organized into two major areasorganized into two major areasThe stackThe stack

Used for local variables, parameter variables, Used for local variables, parameter variables, return addresses and return valuesreturn addresses and return values

The heapThe heapUsed for dynamically allocated variablesUsed for dynamically allocated variables

Page 7: Runtime Environments Compiler Construction Chapter 7

Runtime Storage OrganizationRuntime Storage Organization

Code Area

Global Static Area

Stack

Free space

HeapThe heap generally growstowards lower memory

The stack generally growstowards higher memory

Page 8: Runtime Environments Compiler Construction Chapter 7

Procedure Activation RecordsProcedure Activation Records

arguments (parameters)

return address etc.

local variables

In C – like languages these records are stored on the stackIn Fortran they are in the static storage areaIn Lisp they are in the heap area

called a stack-frameif stored on the stack

Depending on theNumber of Registersmuch of this data might be stored in registers

Page 9: Runtime Environments Compiler Construction Chapter 7

Calling Sequence Calling Sequence Operations that must be performed when a Operations that must be performed when a

procedure is called the procedure is called the calling sequencecalling sequence IncludesIncludes

allocation of memory for activation recordallocation of memory for activation record computation and storing of argumentscomputation and storing of arguments storing and setting necessary registersstoring and setting necessary registers

Additional operations are needed when a Additional operations are needed when a procedure returns called the procedure returns called the return sequencereturn sequence placing the return valueplacing the return value readjustment of registersreadjustment of registers de-allocation of memory for activation recordde-allocation of memory for activation record

Page 10: Runtime Environments Compiler Construction Chapter 7

Important RegistersImportant Registers

Program Counter (pc)Program Counter (pc)set this for procedure callsset this for procedure calls

Stack Pointer (sp)Stack Pointer (sp) for activation frame info in stack-based for activation frame info in stack-based

environmentsenvironmentsBase Pointer (bp)Base Pointer (bp)

similar to the stack pointer, but points to the similar to the stack pointer, but points to the beginning of the activation recordbeginning of the activation record

Page 11: Runtime Environments Compiler Construction Chapter 7

Fully Static Fully Static Runtime EnvironmentsRuntime Environments

All variables are allocated staticallyAll variables are allocated statically including local variablesincluding local variables

No pointers or dynamic allocationNo pointers or dynamic allocationNo recursionNo recursion

why?why?All procedures have a single activation All procedures have a single activation

recordrecordallocated statically before program executionallocated statically before program execution

Page 12: Runtime Environments Compiler Construction Chapter 7

Static Runtime EnvironmentStatic Runtime Environment

code for main

code for proc 1

code for proc n

activation record for main

activation record for proc 1

global data area

activation record for proc 2

Code Area

Data Area

Page 13: Runtime Environments Compiler Construction Chapter 7

Stack-basedStack-basedRuntime EnvironmentsRuntime Environments

Supports RecursionSupports RecursionNo way of knowing at compile time, how many No way of knowing at compile time, how many

times a procedure will need to be called times a procedure will need to be called Use stack-based activation records in order to Use stack-based activation records in order to

support recursionsupport recursion

Page 14: Runtime Environments Compiler Construction Chapter 7

GCDGCD

int x,y;

int gcd ( int u, int v){

if (v == 0) return u;else return gcd (v, u%v);

}

void main(){

scanf(“%d%d”,&x,&y);printf(“%d\n”,gcd(x,y));

}

Page 15: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

Page 16: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

Page 17: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 15v: 10

main bpreturn value

return address

first call to gcd

Page 18: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 10v: 5old bp

return valuereturn address

first call to gcd

second call to gcd

u: 15v: 10

main bpreturn value

return address

Page 19: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 10v: 5old bp

return valuereturn address

first call to gcd

second call to gcd

u: 15v: 10

main bpreturn value

return address

u: 5v: 0old bp

return address

third call to gcd

Page 20: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 10v: 5old bprv: 5

return address

first call to gcd

second call to gcd

u: 15v: 10

main bpreturn value

return address

Page 21: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 15v: 10

main bprv : 5

return address

first call to gcd

Page 22: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 15y: 10

global static area

activation record of main

u: 15v: 10

main bprv : 5

return address

first call to gcd

this value sent to printf

Page 23: Runtime Environments Compiler Construction Chapter 7

Sample codeSample code

int x = 2;

void f(int n){ static int x= 1; g(n); x--;}void g(int m){ int y=m-1; if (y>0) { f(y); x--; g(y); }}int main (){ g(x) return 0;}

Page 24: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 2f::x 1

global static area

return value activation record of mainspbp

Page 25: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 2f::x 1

global static area

return value activation record of main

m: 2main bp

return addressy: 1

call to g( )

sp

bp

Page 26: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 2f::x 1

global static area

return value activation record of main

m: 2main bp

return addressy: 1

call to g( )

n: 10g: bp

return address

call to f( )

sp

bp

Page 27: Runtime Environments Compiler Construction Chapter 7

Activation RecordsActivation Records

x: 2f::x 1

global static area

return value activation record of main

m: 2main bp

return addressy: 1

call to g( )

n: 10g: bp

return address

call to f( )

m: 1main bp

return addressy: 0

call to g( )

sp

bp

Page 28: Runtime Environments Compiler Construction Chapter 7

Arrays on the stackArrays on the stack

void f ( int x, char c){

int a[10];double y;…

}

Consider function f:

Page 29: Runtime Environments Compiler Construction Chapter 7

Activation RecordActivation Record

x: 2c: 1

old bp:return address

a[9] …

a[2]a[1]a[0]

ysp

bp

low memory

high memory

offset of a = -13*2 = -26

a[i] = bp + a + 2*i

calculation of a[i]

Page 30: Runtime Environments Compiler Construction Chapter 7

Calling SequenceCalling Sequence Create space for return valueCreate space for return value

push 0push 0 Store a return address Store a return address

push [pc + 4]push [pc + 4] Store the current base pointerStore the current base pointer

push bppush bp Set bp to new addressSet bp to new address

mov bp, spmov bp, sp Create space for local variablesCreate space for local variables Compute the arguments and store them in the new Compute the arguments and store them in the new

activation recordactivation record (push arguments in order)(push arguments in order)

Jump to the new addressJump to the new address

Page 31: Runtime Environments Compiler Construction Chapter 7

Return SequenceReturn Sequence

change the sp to pop the argumentschange the sp to pop the arguments load the control link to the bpload the control link to the bp

pop bppop bp jump to the return addressjump to the return address

pop pcpop pccopy retvalue into right locationcopy retvalue into right location