Lecture 20 Sections Wed, Apr 1, 2009

Similar documents
Function Definition Syntax Tree

Building the Abstract Syntax Trees

Recursive Descent Parsers

Abstract Syntax Trees Synthetic and Inherited Attributes

Lecture 14 Sections Mon, Mar 2, 2009

The x86 Instruction Set

Introduction to Compiler Design

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

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

Minimal Spanning Trees

LR Parsing - The Items

Scope and Parameter Passing

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

Scope and Parameter Passing

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

Binary Tree Applications

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

The Decreasing-Time Algorithm

Recursive Linked Lists

Lecture 29 Section Wed, Apr 1, 2009

List Iterator Implementation

Lecture 37 Section 9.4. Wed, Apr 22, 2009

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

Lecture Code Generation for WLM

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

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

The Critical-Path Algorithm

Solving Recursive Sequences by Iteration

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

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

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

The Plurality-with-Elimination Method

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

syntax tree - * * * * * *

The Coefficient of Determination

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

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

Semantic Analysis. Lecture 9. February 7, 2018

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

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

Inheritance: The Fundamental Functions

Compiler Theory. (Semantic Analysis and Run-Time Environments)

ChAmElEoN Parse Tree

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

CMPT 379 Compilers. Parse trees

Project Compiler. CS031 TA Help Session November 28, 2011

The Standard Template Library Classes

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

Binary Tree Implementation

CS-201 Introduction to Programming with Java

CSE302: Compiler Design

The Graphics Pipeline

Semantic actions for declarations and expressions

Stacks and their Applications

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

LECTURE 3. Compiler Phases

Programming in Haskell Aug-Nov 2015

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

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

CS1150 Principles of Computer Science Methods

CSC 2400: Computer Systems. Using the Stack for Function Calls

Plan. Regression testing: Demo of how to use the regress.sh script.

Programming Languages

Programming Languages

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Street-Routing Problems

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

Friends and Unary Operators

The Class Construct Part 1

Implementing Linked Lists

PHP Queries and HTML Forms Lecture 23

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Density Curves Sections

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

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

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

CS111: PROGRAMMING LANGUAGE II

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

Compiler construction 2002 week 5

Assembler Programming. Lecture 10

The x87 Floating-Point Unit

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

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Dynamic Allocation of Memory

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Dynamic Allocation of Memory

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

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

Syntax-Directed Translation. Lecture 14

Compiler construction in4020 lecture 5

CSC 2400: Computer Systems. Using the Stack for Function Calls

The x86 Architecture

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

Sampling Distribution Examples Sections 15.4, 15.5

Transcription:

s Return End Call s Lecture 20 Sections 7.1-7.5 Hampden-Sydney College Wed, Apr 1, 2009

Outline s Return 1 s Return End End Call s 2 Call s 3

s Return End Call s 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.

From the production s Return End Call s 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 enterblock() after installing the function name, but before installing the parameters or local variables.

A s Return FUNC f.mode End Call s ID f.id.name f.mode NUM n.num

A s Return End Call s Name of the function ID f.id.name f.mode FUNC f.mode NUM n.num

A s Return FUNC f.mode End Call s ID f.id.name f.mode NUM n.num Return type

A s Return End Call s ID f.id.name f.mode FUNC f.mode NUM n.num Size of the local variable block

Return s Return End Call s 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.

Return s s RET rettype RET rettype RET rettype Return End Call s e e.mode CAST rettype e e.mode

End s Return End Call s 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.

End s Return End Call s 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.

Return at End of s Return End Call s 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?

A End s Return FEND f.mode End Call s ID f.mode

Building a Call s Return End Call s 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.

s Return End Call s 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.

s Return End Call s In the LIST tree, each LIST node, except the last, has an actual parameter in its right subtree and a LIST node in its left subtree. The last LIST node has two parameters in its subtrees.

A LIST Tree of Four Parameters LIST s Return End Call s LIST param4 LIST param3 param1 param2

Example s Return End Call s Example (A LIST Tree) ASSIGN INT ID PTR INT value = "a" CALL INT ID PTR PROC INT value = "sum" LIST LIST NUM INT value = 10 NUM INT value = 20 NUM INT value = 30 The syntax tree for the function call a = sum(10, 20, 30); would be

A LIST Tree of Two Parameters s Return LIST End Call s param1 param2

A Tree of One Parameter s Return If there is only one parameter, then we do not need a LIST tree at all. End Call s param1

Building a Call s Return End Call s The productions involved in function calls are expr id ( exprs ) id ( ) exprs exprs, expr expr

Building a Call s Return End Call s Suppose the parameter list is a, b, c. Then by the LALR algorithm, a will match expr and invoke the production exprs expr. Then b will match expr and invoke the production exprs exprs, expr. Similarly, c will match expr and invoke the production exprs exprs, expr.

Building a Call s Return End Call s Had the production been exprs expr, exprs, then a, b, and c would each match expr and expr, expr, expr would be pushed onto the stack. Then the parser would match expr with exprs. Then it would match expr, exprs with exprs, and so on.

Building a Call s Return End Call s The implication is that the expressions would be recognized as part of an actual argument in reverse order, from right to left. Does that matter? Consider the dynamics of building the AST and later traversing it.

A Call CALL f.mode s Return End Call s ID f.mode LIST LIST param3 param1 param2

A Call with Only One Parameter s Return CALL f.mode End Call s ID f.mode param1

s Return End Call s Homework Read Sections 7.1-7.5.