Function Definition Syntax Tree

Similar documents
Lecture 20 Sections Wed, Apr 1, 2009

Building the Abstract Syntax Trees

Minimal Spanning Trees

Recursive Descent Parsers

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Critical-Path Algorithm

The Decreasing-Time Algorithm

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The x86 Instruction Set

Abstract Syntax Trees Synthetic and Inherited Attributes

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Scope and Parameter Passing

Introduction to Compiler Design

Sampling Distribution Examples Sections 15.4, 15.5

Rotations and Translations

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Scope and Parameter Passing

LR Parsing - Conflicts

List Iterator Implementation

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Binary Tree Applications

The Graphics Pipeline

Scheduling and Digraphs

Density Curves Sections

The Class Construct Part 1

The Pairwise-Comparison Method

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The Traveling Salesman Problem Brute Force Method

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Coefficient of Determination

Recursive Linked Lists

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Solving Recursive Sequences by Iteration

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

PHP Queries and HTML Forms Lecture 23

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

The Plurality-with-Elimination Method

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

LR Parsing - The Items

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Graphics Pipeline

Magnification and Minification

Street-Routing Problems

Implementing Linked Lists

DTDs and XML Attributes

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Friends and Unary Operators

Stacks and their Applications

Programming Languages

Programming Languages

Dynamic Allocation of Memory

Displaying Distributions - Quantitative Variables

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

XQuery FLOWR Expressions Lecture 35

XML and AJAX Lecture 28

The x86 Architecture

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Lecture 14 Sections Mon, Mar 2, 2009

The Projection Matrix

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

The CYK Parsing Algorithm

The Normal Distribution

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Insertions, Deletions, and Updates

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

The Traveling Salesman Problem Cheapest-Link Algorithm

Dynamic Allocation of Memory

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Nondeterministic Programming in C++

Introduction to Databases

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Inheritance: The Fundamental Functions

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Recognition of Tokens

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The x87 Floating-Point Unit

Ambient and Diffuse Light

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

The Standard Template Library Classes

Relational Databases

Binary Tree Implementation

The Class Construct Part 2

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Binary Tree Implementation

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Transcription:

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 NUM n.num Robb T. Koether (Hampden-Sydney College) Function Definition Syntax Tree Wed, Apr 15, 2015 7 / 21

A Function Definition Syntax Tree Name of the function FUNC f.mode ID f.id.name f.mode NUM n.num Robb T. Koether (Hampden-Sydney College) 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 ID f.id.name f.mode FUNC f.mode NUM n.num Size of the local variable block Robb T. Koether (Hampden-Sydney College) 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