You are on page 1of 16

Implementation of Subprograms

Programming Languages
Ghezzi, et. al

School of Information and Computer


Science
UC Irvine
Shannon Alfaro

Operational semantics: An
abstract semantic processor
SIMPLESEM
Components: pc, memory (code, data), processor

Operation Sequence (until halt)


1. Get Current Instruction: C[pc]
2. Increment pc
3. Execute Current Instruction

Semantics intuitively known


The semantics of PL
described by rules that specify how each construct
of the language is translated into a sequence of
equivalent SIMPLESEM instructions.

SIMPLESEM Languages
Static (no recursion)
C1, C2

Stack-Based (recursion, no heap)


C3, C4

Dynamic
C5

* definitely *

Basic SIMPLESEM Instructions


Memory References:
D[x], C[x] =>Xth cell of D, C

Memory Modification:
set target, source

Addressing:
(direct):
(indirect):

set 10, D[20]


set D[10], D[20]

Address Calculations: liberal

Input/Output:

set 9, D[15]+D[33]*D[41]

input-device:
output-device:

set 15, read


set write, D[50]

Unconditional:
Conditional:

jump 47,or jump D[3]


jumpt 47, D[3] > D[8]

Jumping:

C1: simple types, simple


statements

Static (no recursion)


no functions
static memory requirements
Memory requirements known statically
Main routine only
Input: get, Output: put

Lets do an example!!

Basic SIMPLESEM Instructions


Memory References:
D[x], C[x] =>Xth cell of D, C

Memory Modification:
set target, source

Addressing:
(direct):
(indirect):

set 10, D[20]


set D[10], D[20]

Address Calculations: liberal

Input/Output:

set 9, D[15]+D[33]*D[41]

input-device:
output-device:

set 15, read


set write, D[50]

Unconditional:
Conditional:

jump 47, jump D[3]


jumpt 47, D[3] > D[8]

Jumping:

Guidelines for Control Flow Constructs

C2: Adding Simple Routines


Static (no recursion)

C1 + Global data

Define routines
Routines declare local data
Activation Records
Size is known
Allocated Statically

No Nesting
No Return Values
No Recursion
Program Structure
1) {data declarations} //global data
2) {routine declarations/definitions}
3) main() (as before)

int i = 1, j =2, k = 3;
alpha()
{ int i=4, l=5;
5: i = i + k;
6: i = i + l;
};
beta()
{int k =6;
3: i = j + k;
4: alpha();
};
main()
{
0: i = 1;
1: beta();
2: i = 1;
}

C2: Data Segment Update


Each procedure is associated with an
Activation Record (AR)

Return Point (AR.RP)

Local Variables

Lets look at another example!!

SIMPLESEM Instructions for C2


Memory References:
D[x], C[x]

Memory Modification:
set target, source

Input/Output:
input-device:
output-device:

Jumping:
Unconditional:
Conditional:

Routine Call
Set return pointer in callees AR:
Jump to 1st instruction of callee:

Routine Return

set 15, read


set write, D[50]
jump addr
jumpt addr, condition
set AR.RP, ip+1
jump start_addr

Last statement in callee: jump D[return pointer]

C2 +

C3: Supporting Recursive


Direct Recursion
Functions
Indirect Recursion
Return Values

Effects of Recursion
Stack Implementation

Stack-Based
(recursion, no heap)

AR allocated LIFO
AR stores:
return pointer, at offset 0 in AR
the base address of the callers AR, at offset 1 (dynamic link)

STATICALLY KNOWN
each AR has a fixed size
variable offsets in AR
code segment associated with each AR (shared)

STATICALLY UNKNOWN (DYNAMIC)


how many instances of any AR during execution
the base address of the AR

C3: Data Segment Update


Each function call is associated with an
Activation Record (AR)
Return Value (appended to callers AR)
Return Point (in callees AR)
Dynamic Link (in callees AR)

Local Variables (in callees AR)


Dynamic Link Definition:
Pointer to base address of the
callers activation record

C3: Data Segment Update


Each function call is associated with an
Activation Record (AR)
Return Value (in callers AR)
Return Point (in callees AR)
Dynamic Link (in callees AR)

Local Variables (in callees AR)


Dynamic Link Chain:
Dynamic links originating in the
currently active AR: sequence of
dynamic unit activations

C3: Data Segment Update


Dynamic Stack Management:
Implemented as LIFO
Memory Management: pointer to FREE memory:
Stack Pointer: SP
Base Address of CURRENTly Executing Function: FP
Frame Pointer: FP
0: FP/CURRENT D[0]
1: SP/FREE D[1]

...

SIMPLESEM Instructions for C3


Memory Modification:

Code for a function call


set target, source

Input/Output:
input-device:
output-device:

set 15, read


set write, D[50]

Unconditional:
Conditional:

jump addr
jumpt addr, condition

Jumping:
Routine Call
Allocate space for return value in caller AR:
Set return pointer in callees AR:
Set dynamic link of the callees
AR to point to callers AR :
Set FP to currently executing AR:
Allocate memory: Set SP (AR is size of callees AR):
Jump to 1st instruction of callee:

Routine Return
Set SP (deallocate):
Set FP (re-activate caller):
jump to stored return pointer:

set 1, D[1]+1
set D[1], ip+4
set D[1]+1, D[0]
set 0, D[1]
set 1, D[1]+AR
jump start_addr
set 1, D[0]
set 0, D[D[0]+1]
jump D[D[1]]

SIMPLESEM Instructions for C3


Write Code Segment

Routine Call
Allocate space for return value:
set 1, D[1]+1
Set return pointer in callees AR:
set D[1], ip+4
Set dynamic link of the callees
AR to point to callers AR :
set D[1]+1, D[0]
Set FP to currently executing AR:
set 0, D[1]
Allocate memory: Set SP:
set 1, D[1]+AR
Jump to 1st instruction of callee:
jump start_addr
Routine Return
Set SP (deallocate):
set 1, D[0]
Set FP (re-activate caller):
set 0, D[D[0]+1]
jump to stored return pointer:
jump D[D[1]]

Simulate execution
n=3
int n;
int fact()
{
int loc;
if (n>1){
loc = n--;
return loc*fact();
}
else return 1;
}
main()
{
get(n);
if(n>=0)
print(fact());
else
print(input error);
}

You might also like