CSE 131B – Compiler Construction II Discussion 3: Project 1 – Constants, Type Aliases, Procedures 1/24/2007.

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

CPSC 388 – Compiler Design and Construction
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Intermediate Code Generation
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Compiler Construction Intermediate Code Generation.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compiler Construction
CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CSE 131B – Compiler Construction II Discussion 1: Getting Started 1/10/2007.
CSE 131B – Compiler Construction II Discussion 4: Project 1 1/31/2007.
CSE 131B – Compiler Construction II Discussion 7: Short-Circuiting, Pointers/Records, and Arrays 2/28/2007.
CS Winter 2011 Introduction to the C programming language.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CIS Computer Programming Logic
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
1 CS161 Introduction to Computer Science Topic #10.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
C++ Functions A bit of review (things we’ve covered so far)
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Lecture 9 Symbol Table and Attributed Grammars
Introduction to C++ Systems Programming.
Motivation and Overview
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Semantic Analysis with Emphasis on Name Analysis
Pointers and Pointer-Based Strings
C Basics.
CMPE 152: Compiler Design September 18 Class Meeting
Chapter 6 Intermediate-Code Generation
CSC 533: Programming Languages Spring 2015
CS 432: Compiler Construction Lecture 11
Pointers and Pointer-Based Strings
Compiler Construction
COMPILERS Semantic Analysis
CSE 131B – Compiler Construction II
CSC 533: Programming Languages Spring 2018
Compiler Construction
CSC 533: Programming Languages Spring 2019
Presentation transcript:

CSE 131B – Compiler Construction II Discussion 3: Project 1 – Constants, Type Aliases, Procedures 1/24/2007

Overview  Constants  Type Aliases  Procedures  Topics/Questions you may have

Constants  CONST c = 5;(* INTEGER *)  CONST d = 5.0;(* REAL *)  CONST e = FALSE;(* BOOLEAN *)  CONST f = 5 + c;(* folding! *)  CONST g = f – 11;(* folding! *)  VAR v : ARRAY g OF REAL; (* error, since dimension is negative *)

Type Aliases TYPE t1 = REAL; TYPE t2 = t1; TYPE t3 = t2; VAR x : t3; VAR y : REAL;

Type Aliases  Type Aliases provide a way for users to define their own types – like C’s “ typedef ”  So, what needs to be done? We mainly need to store 2 things: The name of the alias The base type it represents

Type Aliases  So, let’s look at this example more closely: TYPE t1 = REAL; TYPE t2 = t1; TYPE t3 = t2; VAR x : t3; VAR y : REAL;  In this case, “x” has a alias name of “t3”, and an underlying type of “REAL”. Notice we don’t need to remember the intermediate aliases!

So, How Do We Implement It?  Since type aliases are referred to by an identifier (their name), they will eventually need to get on the Symbol Table.  So, one way is to have some kind of STO for Type Aliases  TypeAliasSTO Since it extends your base STO, it will have the Name and Type fields you need to store your information.

Putting it on the Symbol Table  This method (using TypeAliasSTO) fits nicely into the given code, relying on the SymbolTable and STO hierarchy: void DoTypeDecl (String id, Type baseType) { if (m_symtab.access (id) != null) { m_nNumErrors++; m_errors.print (Formatter.toString(ErrorMsg.redeclared_id, id)); } TypeAliasSTO sto = new TypeAliasSTO (id, baseType); m_symtab.insert (sto); }

Type Aliases  There are other ways to handle Type Aliases, such as making an AliasType in your Type hierarchy or overloading the access method of the symbol table to handle Type as well as STO.  Think about what you are most comfortable implementing and what can be done to make your program work before the deadline.

More Type Checking  All basic types use structural equivalence  Arrays and Records use strict name equivalence  Pointers depend on what they point to  Type aliases use loose name equivalence

Alias Equivalence TYPE foo = INTEGER; TYPE bar = foo; TYPE baz = bar; VAR x : INTEGER; VAR y : baz; BEGIN x := 5; (* OK *) x := y; (* OK by loose name equivalence *) RETURN 0; END.

Alias/Record Equivalence TYPE r1 = POINTER TO RECORD a, b : REAL; TYPE r2 = r1; VAR x : POINTER TO RECORD a, b : REAL; VAR y : r1; VAR z : r2; x  y? NO x  z? NO y  z? YES

Now, Procedures!  First, some fundamental differences from 131A: We are writing a static translator, not an interpreter. Once we finish the function declaration, including the body, we’re done. We don’t need to remember the code in the body, since we’ll just be spitting out assembly code (Project 2). A procedure call will basically boil down to an assembly “call foo” type instruction, once all type checking is complete.

Procedures  For Project 1, we will need to: Check the procedure call against the procedure declaration to ensure argument symmetry. Check the body of the procedure, type checking the same way we check statements in main. Check the return logic of the procedure.

ProcSTO  In order to do most of these checks, we will need to store some information about the function. ProcSTO is designed for this!  Things that should be stored: The Return Type (currently only stores if there is a return type, but not the actual type). All parameter information: Total number of parameters Type of each parameter, including whether VAR or non-VAR

VAR vs. non-VAR  All parameters are VarSTOs, whether or not they have the keyword “VAR” in front of them.  In Oberon, the keyword “VAR” for a procedure parameter means that variable is a reference variable (instead of a value variable).  Think of passing by value versus passing by reference.  If everything was good and well in the world, the keyword “VAR” for parameters would’ve been “REF” instead.

Procedure Declaration ProcDecl ::= T_PROCEDURE OptReceiver:_2 IdentDef:_3 {: ((OParser) parser).SaveLineNum (); ((OParser) parser).DoProcDecl_1 (_3, _2); :} OptFormalParams T_SEMI DeclSeq OptStmts T_END T_ID:_9 {: ((OParser) parser).DoProcDecl (_3, _9); :} ;

Procedure Declaration void DoProcDecl_1 (String id, STO stoReceiverVar) { if (m_symtab.access (id) != null) { m_nNumErrors++; m_errors.print (Formatter.toString(ErrorMsg.redeclared_id, id)); } ProcSTO sto = new ProcSTO (id); if (stoReceiverVar == null) { m_symtab.insert (sto);// Inserted into outer scope } m_symtab.openScope ();// New scope opened m_symtab.setProc (sto);// Current Proc we’re in is set if (stoReceiverVar != null) { m_symtab.insert (stoReceiverVar); }

Procedure Declaration void DoProcDecl (String strBeginID, String strEndID) { if (!strBeginID.equals (strEndID)) { m_nNumErrors++; m_errors.print (Formatter.toString (ErrorMsg.improper_proc_end, strEndID)); } m_symtab.closeScope ();// Close scope (pops top scope off) m_symtab.setProc (null);// Says we’re back in “main” }

Procedure Declaration PROCEDURE foo (a, b : REAL; VAR c : REAL) : BOOLEAN; VAR x : BOOLEAN; (* Local Variable *) BEGIN x := a > b; x := x & c; RETURN x; END foo;  In this example, we want to create a ProcSTO with the name “foo”, set its return type to BOOLEAN, set its param count to 3, and remember the parameters are: non-VAR REAL, non-VAR REAL, and VAR REAL).  Furthermore, we want to insert the VarSTO for a, b, c, and x into the SymTable so they are available for the body of the procedure.

Procedure Call  Now that we have a ProcSTO in the SymTable, and type-checked the procedure declaration, we are ready to call it. foo(1, 2, 3);  Given the above call, there would be an error since ‘3’ is not addressable and cannot be sent to a VAR (reference) parameter.

Procedure Call  So, when we call a procedure, we want to get the ProcSTO from the SymTable and check its parameters with the arguments.  Consider making a Vector of some new object (you create it how you want) that holds the parameters. When you call the function, compare the two vectors. Important design choices!!!

What to do Next! 1.Finish up Phase 2. 2.Write more test programs. 3.Start Phase 3. 4.Come to lab hours and ask questions.

Topics/Questions you may have  Anything else you would like me to go over now?  Anything in particular you would like to see next week?