CMPE 152: Compiler Design October 4 Class Meeting

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Lecture 9 Symbol Table and Attributed Grammars
CS 153: Concepts of Compiler Design September 14 Class Meeting
Constructing Precedence Table
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 432: Compiler Construction Lecture 10
Review: Chapter 5: Syntax directed translation
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 25 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design August 30 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
Subject Name:Sysytem Software Subject Code: 10SCS52
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design September 20 Class Meeting
CMPE 152: Compiler Design September 27 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
UNIT V Run Time Environments.
7 Arrays.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design February 26 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design March 5 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design September 26 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CMPE 152: Compiler Design October 4 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Executing Procedure and Function Calls Chapter 12

Class CallDeclaredExecutor Method execute() Create a new activation record based on the called routine’s symbol table. Execute the actual parameter expressions. Initialize the memory map of the new activation record. The symbol table entry of the name of the called routine points to the routine’s symbol table. Copy values of actual parameters passed by value. Set pointers to actual parameters passed by reference.

Class CallDeclaredExecutor cont’d Method execute() cont’d Push the new activation record onto the runtime stack. Access the root of the called routine’s parse tree. The symbol table entry of the name of the called routine points to the routine’s parse tree. Execute the routine. Pop the activation record off the runtime stack.

Class CallDeclaredExecutor Method execute_actual_parms() Obtain the formal parameter cell in the new activation record: wci::backend::interpreter::executors::CallDeclaredExecutor.cpp Cell formalCell = newAr.getCell(formalId.getName());

Class CallDeclaredExecutor Method execute_actual_parms() cont’d Value parameter: wci::backend::interpreter::executors::CallDeclaredExecutor.cpp Object cell_value = expression_executor.execute(actual_node); assignment_executor.assign_value(actual_node, formal_id,                                  formal_cell, formal_typespec,                                  cell_value, value_typespec); Set a copy of the value of the actual parameter into the memory cell for the formal parameter.

Class CallDeclaredExecutor Method executeActualParms() cont’d VAR (reference) parameter: Method ExpressionExecutor.executeVariable() executes the parse tree for an actual parameter and returns the reference to the value. wci::backend::interpreter::executors::CallDeclaredExecutor.cpp Cell *actual_cell =     expression_executor.execute_variable(actual_node); formal_cell->set_value(actual_cell); Set a reference to the actual parameter into the memory cell for the formal parameter.

Class CellImpl Since a memory cell can contain a value of any type or a reference, we implement it simply as a boost::any. wci::Object.h #include <boost/any.hpp> #define Object boost::any wci::backend::interpreter::memoryimpl::CellImpl.cpp Object CellImpl::get_value() const { return cell_value; } void CellImpl::set_value(Object new_value) { cell_value = new_value; }

Runtime Error Checking Range error Assign a value to a variable with a subrange type. Verify the value is within range Not less than the minimum value and not greater than the maximum value. Method StatementExecutor.check_range() Division by zero error Before executing a division operation, check that the divisor’s value is not zero.

Pascal Interpreter Now we can execute entire Pascal programs! Demo

Midterm Review: Question 1 The parser builds data structures consisting of symbol table entry objects (STEO) and type specification objects (TSO). How are these structures used at compile time? STEO: Store information about locally-declared identifiers. TSO: Represent type information. Both: Assign types to variables. Both: Do type checking in statements and expressions.

Midterm Review: Question 1, cont’d How does the interpreter use these structures at run time? STEO: Create the memory map for each activation record that’s pushed onto the runtime stack. STEO: Get the values of defined and enumeration constants. TSO: Do runtime range checking for subranges. TSO: Get the data types of array indexes and elements.

Midterm Review: Question 2 How does the symbol table stack … … allow a variable declared in an enclosing scope to be redeclared in the local scope? Push a new symbol table onto the symbol table stack for the local scope. Enter the variable’s name into the local table. … prevent a variable already declared in the local scope from being redeclared in the local scope? First check the local symbol table to see whether the variable’s name is already entered into the table.

Midterm Review: Question 2, cont’d How does the symbol table stack … … allow two record types defined in the same procedure to declare fields with the same names? Push a separate symbol table for each record type onto the symbol table stack when the parser is parsing the record type specification. Pop it off when the parser is done parsing the specification.

Midterm Review: Question 3 Explain the role of synchronization sets during parsing. Each synchronization set tells the parser what tokens to look for at a particular point of the source program. Some members of the set are tokens that are syntactically valid at that point, and other members are there to allow the parser to recover from syntax errors.

Midterm Review: Question 4 Some programming languages use parentheses to enclose array subscripts and to enclose arguments to function calls. Suppose Pascal did the same. Consider the assignment statement: x := arr(i, 2*j) Describe how the Pascal parser could (or could not) determine whether it is parsing a call to function arr or an access to an element of array arr.

Midterm Review: Question 4, cont’d x := arr(i, 2*j) By the time the parser is parsing the assignment statement, it would have already parsed the declaration of arr as either an array or a function. Therefore, when the parser encounters arr in the assignment statement, it looks in the symbol table to see how arr was declared.

Midterm Review: Question 5 Suppose Pascal has an exponentiation operator **, so that n**4 represents the mathematical expression n4. Exponentiation has a higher operator precedence level than the multiplicative operators. Therefore, n**i*j is evaluated as (n**i)*j. Furthermore, exponentiation is right-associative, so that n**i**j**k is evaluated as n**(i**(j**k)).

Midterm Review: Question 5 What modifications to the expression, simple expression, term, and factor syntax diagrams are required to accommodate the ** operator? Redraw only the diagrams that change. (Not all the diagrams may need to change, and you may need to add new diagrams.)

Midterm Review: Question 5, cont'd

Midterm Review: Question 5, cont'd

Midterm Review: Question 5, cont'd Draw the parse tree for the assignment statement r := a**i*j**(n-1)**k You may assume the existence of a node type for the ** operator. r := (a**i) * (j** ((n-1)**k)) := r * ** ** a i j ** - k n 1

Midterm Review: Question 6 Given the Pascal program structure and call chain: PROGRAM main; PROCEDURE procA; PROCEDURE procB; PROCEDURE procC; PROCEDURE procD; PROCEDURE procE; main  procD  procE  procE  procA  procB  procC  procB  procC  procD AR: main AR: procD AR: procE AR: procA AR: procB AR: procC RUNTIME STACK Draw the runtime stack, activation records, and runtime display at the end of the call chain. Show the display pointers and the activation record links. 1 2 3 RUNTIME DISPLAY 4

Midterm Review: Question 7 Suppose Pascal had a LOOP-AGAIN statement. For example: Like the REPEAT-UNTIL statement, the LOOP-AGAIN statement can contain any number of statements that will be repeatedly executed. Any one or more (or none) of the contained statements can be a WHEN-LEAVE statement which will cause a break out of the loop if its Boolean expression evaluates to true. A WHEN-LEAVE statement can only appear inside of a LOOP-AGAIN statement. LOOP k := k + 1; WHEN k > 10 LEAVE; j := 2*k AGAIN

Midterm Review: Question 7, cont'd Draw syntax diagrams for the LOOP-AGAIN and WHEN-LEAVE statements. You may assume that diagrams already exist for “statement” and “expression” and that LOOP, AGAIN, WHEN, and LEAVE are reserved words. LOOP k := k + 1; WHEN k > 10 LEAVE; j := 2*k AGAIN

Midterm Review: Question 7, cont'd Using only the existing node types defined in class ICodeNodeTypeImpl, draw the parse tree for the LOOP-AGAIN statement shown on the previous page. LOOP k := k + 1; WHEN k > 10 LEAVE; j := 2*k AGAIN