RUN-TIME
ENVIRONMENTS
Compiler Front- and Back-end
Source program (character stream) Abstract syntax tree or
other intermediate form
Scanner
(lexical analysis) Machine-Independent
Tokens Code Improvement
Modified intermediate form
Front end
Parser
Back end
synthesis
analysis
(syntax analysis)
Parse tree Target Code Generation
Assembly or object code
Semantic Analysis and
Intermediate Code
Generation Machine-Specific Code
Improvement
Abstract syntax tree or
Modified assembly or object code
other intermediate form
Parsing examples
Pos = init + rate * 60
id1 = id2 + id3 * const
-> parse tree
:=
id1 +
id2 *
id3 60
:=
•-> AST Id1(val) +
Id2(val) *
id3(val) 60 (const)
Code Generation and Intermediate Code
Forms
Other intermediate code forms
intermediate code is something that is both close to the final machine code
and easy to manipulate (for optimization). One example is the three-address
code:
dst = op1 op op2
The three-address code for the assignment statement:
temp1 = 60
temp2 = id3 + temp1
temp3 = id2 + temp2
id1 = temp3
Machine-independent Intermediate code improvement
temp1 = id3 * 60.0
id1 = id2 + temp1
Target Code Generation and Optimization
From the machine-independent form assembly or
object code is generated by the compiler
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
This machine-specific code is optimized to exploit
specific hardware features
Coming up next….
Before we get into the low-level details of final
code generation, we first take a look at the
layout of memory and
the runtime data structures for managing the stack and
heap
What is Run-time Support?
It is not enough if we generate machine code from
intermediate code
There is a need to manage memory when a program is
running
This memory management must connect to the data
objects of programs
Programs request for memory blocks and release
memory blocks
Passing parameters to functions needs attention
These are the main tasks of run-time support
Parameter Passing Methods
- Call-by-value
At runtime, prior to the call, the parameter is evaluated, and
its actual value is put in a location private to the called
procedure
Thus, there is no way to change the actual parameters.
Found in C and C++
C has only call-by-value method available
Passing pointers does not constitute call-by-reference
Pointers are also copied to another location
Hence in C, there is no way to write a function to insert a
node at the front of a linked list (just after the header) without
using pointers to pointers
Problem with Call-by-Value
Parameter Passing Methods
- Call-by-Reference
At runtime, prior to the call, the parameter is
evaluated and put in a temporary location, if it is
not a variable
The address of the variable (or the temporary) is
passed to the called procedure
Thus, the actual parameter may get changed due
to changes to the parameter in the called procedure
Found in C++ and Java
Call-by-Value-Result
Call-by-value-result is a hybrid of Call-by-value and Call-by reference
Actual parameter is calculated by the calling procedure and is copied to
a local location of the called procedure
Actual parameter’s value is not affected during execution of the called
procedure
At return, the value of the formal parameter is copied to the actual
parameter, if the actual parameter is a variable
Becomes different from call-by-reference method when global
variables are passed as parameters to the called procedure and the
same global variables are also updated in another procedure invoked by
the called procedure
Found in Ada
Call-by-Value-Result
Code and Data Area in Memory
Most programming languages distinguish between code and data
Code consists of only machine instructions and normally does not
have embedded data
Code area normally does not grow or shrink in size as execution
proceeds
Unless code is loaded dynamically or code is produced
dynamically
As in Java – dynamic loading of classes or producing classes and
instantiating them dynamically through reflection
Memory area can be allocated to code statically
Data area of a program may grow or shrink in size during
execution
Static Versus Dynamic Storage Allocation
Static allocation
Compiler makes the decision regarding storage allocation by looking
only at the program text
Dynamic allocation
Storage allocation decisions are made only while the program is
running
Stack allocation
Names local to a procedure are allocated space on a stack
Heap allocation
Used for data that may live even after a procedure call returns
Ex: dynamic data structures such as symbol tables
Requires memory manager with garbage collection
Static Data Storage Allocation
Compiler allocates space for all
variables (local and global) of all
procedures at compile time
No stack/heap allocation; no
overheads
Ex: Fortran IV and Fortran 77
Variable access is fast since
addresses are known at compile
time
No recursion
Dynamic Data Storage Allocation
Compiler allocates space only for global variables at
compile time
Space for variables of procedures will be allocated at
run-time
Stack/heap allocation
Ex: C, C++, Java, Fortran 8/9
Variable access is slow (compared to static allocation)
since addresses are accessed through the stack/heap
pointer
Recursion can be implemented
Activation Record Structure
An activation record is Temporaries
chunk of computer
Local Data
memory which holds the
arguments and “normal” Machine status
local variables of a
Access Link
function.
An activation record for a Control Link
procedure has fields to Parameters
holds machine status
information, result, local Return Values
variables and so on.
Activation Record Structure….
Temporaries: Expression is evaluated in it. (X=y+Z)
Local variables of the procedure find a place here, then we require
temporaries to evaluate large expression and so on and so forth.
Access link: non local data is accessed.
Control link: Points the activation record of the caller.
Actual parameters which are passed to this particular procedure call are
stored here. The caller will evaluate the parameters and then depending
on the type of parameter passing, either the value or the address is
placed in the actual parameter list.
Activation Record Structure…
Return Value: Value to be return is stored in this.
Machine status holds information about machine status
before procedure call.
The machine status for example, the caller would be
using some registers and before calling this particular
procedure, it would have saved its registers but, this
procedure possibly calls some other procedure and then
it has to store its register contents in some place. So
saved machine status is the place, where it stores the
registers before calling the next procedure.