Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures: Building the Syntax Tree

Similar presentations


Presentation on theme: "Procedures: Building the Syntax Tree"— Presentation transcript:

1 Procedures: Building the Syntax Tree
Lecture 21 Wed, Mar 31, 2004

2 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 LBRACE dcls Therefore, the information is available. This information will be stored in the left and right subtrees of a FUNC tree.

3 Building the Function Definition Syntax Tree
From the production fbeg  fname fargs LBRACE 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.

4 Building the Function Definition Syntax Tree
Therefore, we must call on enterBlock() after installing the function name, but before installing the parameters or local variables.

5 A Function Definition Syntax Tree
f.mode ID:f f.id.name f.mode NUM:n n.num

6 A Function Definition Syntax Tree
f.mode Name of the function ID:f f.id.name f.mode NUM:n n.num

7 A Function Definition Syntax Tree
f.mode ID:f f.id.name f.mode NUM:n n.num Return type

8 A Function Definition Syntax Tree
f.mode Size of the local variable block ID:f f.id.name f.mode NUM:n n.num

9 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 retType.

10 Function Return Syntax Trees
retType RET retType RET retType e e.mode CAST retType e e.mode

11 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. To be safe, at the physical end of the function, we will insert a return with no returned value.

12 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 leaveBlock() to do this, so that it can delete the hash table of local variables.

13 Return at End of Function
At the end of the function, there needs to be an automatic return, even if the programmer didn’t 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?

14 A Function End Syntax Tree
FEND f.mode ID f.mode

15 Building a Function Call Syntax Tree
To write the assembly code for a function call, we need to know The name of the function. Each actual parameter. Each actual parameter is an expression. The parameters are arranged in a LIST tree.

16 LIST Trees A LIST tree is a recursive structure where the right subtree is an expression tree and the left subtree is an expression tree, or a LIST tree. In the LIST tree of actual parameters, each LIST node, except the last, has an parameter in its right subtree and a LIST node in its left subtree. The last LIST node has two parameters in its subtrees.

17 A LIST Tree of 4 Parameters

18 Example The syntax tree for the function call a = sum(10, 20, 30);
would be ASSIGN INT ID PTR|INT value = “a" CALL INT ID PTR|PROC|INT value = "sum" LIST NUM INT value = 10 NUM INT value = 20 NUM INT value = 30

19 A LIST Tree of Two Parameters

20 A Tree of One Parameter If there is only one parameter, then we do not need a LIST tree. param1

21 Building a Function Call Syntax Tree
The productions involved in function calls are expr  ID LPAREN exprs RPAREN | ID LPAREN RPAREN exprs  exprs COMMA expr | expr The production exprs  exprs COMMA expr tells us that the parameters are parsed from left to right.

22 A Function Call Syntax Tree
f.mode ID f.mode LIST LIST param3 param1 param2

23 A Function Call with Only One Parameter
f.mode ID f.mode param1


Download ppt "Procedures: Building the Syntax Tree"

Similar presentations


Ads by Google