CSC448: Code Generation: Run-Time Environment IV [57/133] |
Suppose that the language is extended with procedures that can declare local variables:
exp ::= <ID> | <INT> | exp "+" exp | exp "-" exp | exp "<=" exp | exp "=" exp stat ::= <ID> ":=" exp | "label" <ID> | "if" exp "goto" <ID> | "print" exp | "var" <ID> | "invoke" <ID> "(" args ")" proc ::= "proc" <ID> "(" params ")" { stat* } params ::= ( <ID> ( "," <ID> )* )? args ::= ( exp ( "," exp )* )?
If the language does not permit recursive calls, how can we organize the run-time storage?
Arguments and local variables for a procedure are grouped together into an activation record or frame.
The position of local variables is known at compile-time.
How can we record information about the calling procedures? Note that the number of calling procedures is bounded by the number of procedures.
A sample program:
proc upto (x) { var y; y := 0; goto l2; label l1; print (y); y := y + 1; label l2; if (y <= x) goto l1; } proc f () { var z; z := 3; goto l4; label l3; print (z); z := z - 1; label l4; if (0 <= z) goto l3; }