Professional Documents
Culture Documents
Programming Languages
Ghezzi, et. al
Operational semantics: An
abstract semantic processor
SIMPLESEM
Components: pc, memory (code, data), processor
SIMPLESEM Languages
Static (no recursion)
C1, C2
Dynamic
C5
* definitely *
Memory Modification:
set target, source
Addressing:
(direct):
(indirect):
Input/Output:
set 9, D[15]+D[33]*D[41]
input-device:
output-device:
Unconditional:
Conditional:
Jumping:
Lets do an example!!
Memory Modification:
set target, source
Addressing:
(direct):
(indirect):
Input/Output:
set 9, D[15]+D[33]*D[41]
input-device:
output-device:
Unconditional:
Conditional:
Jumping:
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;
}
Local Variables
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
C2 +
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)
...
Input/Output:
input-device:
output-device:
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]]
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);
}