...

Function Definition Syntax Tree Lecture 34 Section 6.9 Robb T. Koether

by user

on
Category: Documents
10

views

Report

Comments

Transcript

Function Definition Syntax Tree Lecture 34 Section 6.9 Robb T. Koether
Function Definition Syntax Tree
Lecture 34
Section 6.9
Robb T. Koether
Hampden-Sydney College
Wed, Apr 15, 2015
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
1 / 21
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
2 / 21
Outline
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
3 / 21
Building the Function Definition Syntax Tree
The function definition syntax tree is rather simple.
Recall that it is the responsibility of the calling function (the
“caller”) to push the actual parameters onto the stack.
Therefore, in the syntax tree for the function definition, we do not
need to deal with the actual parameters, but only the formal
parameters and the local variables.
The formal parameters and local variables are not part of the
syntax tree.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
4 / 21
Building the Function Definition Syntax Tree
To write the assembly code for the beginning of a function
definition, we must know
The name of the function.
The size of the local variable block.
The relevant production is
fbeg → fname fargs } dcls
Therefore, the information is available.
This information will be stored in the left and right subtrees of a
FUNC tree.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
5 / 21
Building the Function Definition Syntax Tree
From the production
fbeg → fname fargs } dcls
we see that the function name, parameters, and declarations have
already been processed.
Also, the function name should be global, while the parameters
and declarations should be local.
Therefore, we must call on enter_block() after installing the
function name, but before installing the parameters or local
variables.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
6 / 21
A Function Definition Syntax Tree
FUNC
f.mode
ID
f.id.name
f.mode
Robb T. Koether (Hampden-Sydney College)
NUM
n.num
Function Definition Syntax Tree
Wed, Apr 15, 2015
7 / 21
A Function Definition Syntax Tree
FUNC
f.mode
Name of
the function
ID
f.id.name
f.mode
Robb T. Koether (Hampden-Sydney College)
NUM
n.num
Function Definition Syntax Tree
Wed, Apr 15, 2015
7 / 21
A Function Definition Syntax Tree
FUNC
f.mode
ID
f.id.name
f.mode
NUM
n.num
Return type
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
7 / 21
A Function Definition Syntax Tree
FUNC
f.mode
Size of the
local variable
block
ID
f.id.name
f.mode
Robb T. Koether (Hampden-Sydney College)
NUM
n.num
Function Definition Syntax Tree
Wed, Apr 15, 2015
7 / 21
Outline
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
8 / 21
Building the Function Return Syntax Tree
To write the assembly code to return from a function, we must
know
The return type.
The object to be returned.
The return type will be stored in the SymbolTable class variable
ret_type.
(It is also stored in the mode member of the function’s IdEntry.)
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
9 / 21
Function Return Syntax Trees
RET
retType
RET
retType
RET
retType
e
e.mode
CAST
retType
e
e.mode
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
10 / 21
Outline
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
11 / 21
Building the Function End Syntax Tree
To write the assembly code for the end of a function definition, we
do not need any special information, although we will know the
name of the function.
This marks the physical end of the function.
A return statement marks a logical end.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
12 / 21
Building the Function End Syntax Tree
Since this marks the physical end of the function, the block level
should be decreased.
Be sure to call on leave_block() to do this, so that it can delete
the hash table of local variables.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
13 / 21
Return at End of Function
At the end of the function, there needs to be an automatic return,
even if the programmer did not write a return statement.
But then, what about the return value?
Similarly, what if the programmer writes
return;
without specifying a return value when there should be one?
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
14 / 21
A Function End Syntax Tree
FEND
f.mode
ID
f.mode
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
15 / 21
Outline
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
16 / 21
The Symbol Table
The “background” work of processing the function definition is to
install in the symbol table
The function identifier at the GLOBAL level.
Each formal parameter at the PARAM level.
Each local variable at the LOCAL level.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
17 / 21
The Symbol Table
The semantic action of the productions
fargs → ( args ) | ( )
args → args , arg | arg
arg → type ID
will install each parameter in the symbol table.
Each will be given a positive offset equal to the current value of
f_arg_size.
Then f_arg_size is increased by the size of the parameter.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
18 / 21
The Symbol Table
The semantic action of the productions
fbeg → fname fargs { dcls
dcls → dcls dcl | ε
dcl → type ID ;
will install each local variable in the symbol table.
For each local variable, f_dcl_size is increased by the size of
the local variable.
Then it is given a negative offset equal to the current value of
f_dcl_size.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
19 / 21
Outline
1
Function Definition Syntax Trees
Function Return
Function End
2
The Symbol Table
3
Assignment
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
20 / 21
Assignment
Assignment
Read Section 6.9.
Robb T. Koether (Hampden-Sydney College)
Function Definition Syntax Tree
Wed, Apr 15, 2015
21 / 21
Fly UP