Project Compiler. CS031 TA Help Session November 28, 2011

Similar documents
CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS453 CLASSES, VARIABLES, ASSIGNMENTS

Syntax-Directed Translation. Lecture 14

The role of semantic analysis in a compiler

Semantic Analysis. Compiler Architecture

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

SPIM Procedure Calls

Syntax Errors; Static Semantics

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 4 :: Semantic Analysis

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

LECTURE 3. Compiler Phases

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Implementing Classes and Assignments

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Introduction to Parsing. Lecture 5

Lecture 5. Announcements: Today: Finish up functions in MIPS

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Warm Up Problem. Write any equivalent MIPS program corresponding to the following code: int wain ( int a, int b){ int c = a + b; return c*a; }

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

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Functions in MIPS. Functions in MIPS 1

CMSC 330: Organization of Programming Languages

Semantic actions for declarations and expressions

Wednesday, October 15, 14. Functions

CS558 Programming Languages

CA Compiler Construction

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

CSE 401 Final Exam. December 16, 2010

Derived and abstract data types. TDT4205 Lecture 15

Result: original lp in main is. Goal was.

WACC Report. Zeshan Amjad, Rohan Padmanabhan, Rohan Pritchard, & Edward Stow

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Question Points Score

CS 6353 Compiler Construction Project Assignments

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Implementing Procedure Calls

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Alternatives for semantic processing

IR Generation. May 13, Monday, May 13, 13

Compilers and computer architecture: A realistic compiler to MIPS

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CIT Week13 Lecture

CS 2210 Programming Project (Part IV)

Intermediate Code Generation

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Semantic actions for declarations and expressions

Introduction to Parsing. Lecture 5

Compilers CS S-07 Building Abstract Assembly

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

Introduction to Parsing. Lecture 8

CMPT 379 Compilers. Parse trees

CS558 Programming Languages

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

About this exam review

G Programming Languages - Fall 2012

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

CSE 341: Programming Languages

CS 6353 Compiler Construction Project Assignments

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

We can emit stack-machine-style code for expressions via recursion

DDMD AND AUTOMATED CONVERSION FROM C++ TO D

Programming Languages Third Edition. Chapter 7 Basic Semantics

Code Generation. Lecture 12

Three-Address Code IR

5. Semantic Analysis!

CS558 Programming Languages

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Lecture 16: Static Semantics Overview 1

CS143 - Written Assignment 4 Reference Solutions

CSE Lecture In Class Example Handout

Today's Topics. CISC 458 Winter J.R. Cordy

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Run-time Environments - 2

COP4020 Programming Assignment 2 - Fall 2016

CS558 Programming Languages

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

CS61C : Machine Structures

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Today. Putting it all together

Code Generation & Parameter Passing

Intermediate Representations

CS558 Programming Languages

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 11

CS2383 Programming Assignment 3

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Transcription:

Project Compiler CS031 TA Help Session November 28, 2011

Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level language to assembly with a compiler. In this project, we will code some techniques used to create such compilers. We will create a compiler for the made-up language Blaise.

Blaise Blaise is a small procedural language. While not as complex as Java or C, Blaise is sufficiently powerful to program many things. It s an LL(1) language, so at any given point, only 1 token is needed to determine which rule to follow. Except for global variable declarations, all code goes inside procedures, and specific rules govern the usage of these procedures.

Blaise Sample Code Language features: Global variables (keyword var) Procedures, including main (keyword procedure)

Support Code We ve provided lots of code. The only support class that you may modify is MIPSCodeGenerator. Support code includes: Many nodes (for building your parse tree) Classes to help with scoping and procedures (including stack frames). A class to help with code generation

Compiler Stages Scanning Turn code into tokens (done for you!). Parsing Turn the tokens into the abstract syntax tree. Make sure the grammar is followed. Semantic analysis Make sure the AST makes sense (variable scope, type analysis, procedure usage). Gather info for code gen (variables and frames). Code generation Turn the AST into Assembly.

Parsing Make sure you understand the tokens generated in the scanning phase. Given the grammar, look at tokens one at a time (LL(1) grammar). Based on which rules you follow, create nodes appropriately. If no rule fits, throw a SyntaxException. Use helper methods and consider recursion! These nodes and their children form an abstract syntax tree, rooted in a NodeProgram, which will be traversed in the remaining steps.

Parsing - Example Does x * 4-3 parse? How? What about 6 + 3 * x?

Semantic Analysis Even if code can be parsed into an AST, it may not be correct. Goals: Catch semantic errors by traversing the tree, gather information about variables and frames. Errors may arise in scoping, type checking, or procedure usage. This step is more complicated if you choose to implement extra credit.

Semantic Errors Scoping errors occur when a variable is used out of scope, meaning they haven't yet been declared in the scope in which they're used. Scoping errors also happen when a variable is declared twice in the same scope (no variable shadowing in Blaise!) A block, implicit or explicit, creates a scope in Blaise. Consider using the provided Scope class to track variables scopes. Variables must be used as the same type as which they were declared. ints must be used as ints and not bools or arrays. 3 > true makes no sense; if (1) doesn t either (in Blaise, anyways).

Semantic Errors Procedures can cause numerous semantic errors. There must be a no-args procedure named main. Arguments must be used correctly (right number and right type). One can make procedure calls only to procedures declared earlier in the code. For simplicity, no procedure overloading is allowed. Additionally, return 0; is implicitly called at the end of every procedure, so you don't need to check for valid return statements. Be sure no variable has the same name as a procedure! How do we check all of this?

Visitor Pattern Visitor Pattern a common design pattern for compilers. We create a visitor for each task (semantic analysis, code gen, printing the AST, etc.) with a method for each node type. The node passes itself to the visitor. Ex: public void handleexprplus(nodeexprplus n) Advantage: easy to add new operations (just create 1 new class) Disadvantage: harder to add new nodes (need to edit many classes)

Semantic Analysis (cont.) Information about variables must be collected. Arguments and local variables for a procedure are placed on the stack in a frame. Each of these variables has an offset from the frame pointer that allows us to access the variable. You have to push and pop frames during procedure calls, but we provide code to help you set them up. Note that our frames are laid out differently than those from lecture for simplicity. In particular, arguments and local variables are put together. (See HW09 for details!)

Code Generation We want to turn our beautiful, error-free AST into MIPS assembly code. How? Visitor Pattern again. We want to write code to a file for each node in the AST. We have provided a class that will generate MIPS instructions for you (MIPSCodeGenerator). You must write the visitor that calls MIPSCodeGenerator s methods (e.g. you may call genpop, which writes the MIPS code needed to pop something off of the stack.

Code Generation (cont.) You are writing code for a stack-based machine. The result of each operation is pushed onto the stack, to be popped by other operations The less than relational operator assumes that its two arguments are on the stack and pushes its result onto the stack. What should we do if we calculate a sum for a NodeExprPlus and the result is in $s1?

Code Generation (cont.)

Code Generation (cont.) Our frame support code assumes that all $s registers, plus $ra and $fp are pushed to the stack each time a procedure is called, so your code generator should do so. Hints: Remember to return 0 at the end of every procedure. Also, be careful to restore your stack pointer after procedure calls! Global and local variables will be stored in different places, so be sure to distinguish between them.

Testing How do you know that your compiler works? We have provided several functional Blaise programs and several buggy Blaise programs. You should write your own, more-exhaustive test cases. Make sure you test valid and invalid Blaise code. Does your compiler generate arrays correctly? Does it correctly identify semantic errors? Does short-circuiting work correctly? Recursion? Test your compiler thoroughly!

Extra Credit Numerous extra credit options exist. Only attempt these after your compiler is fully functional. If you attempt extra credit, hand in your regular compiler in addition to the one with extra credit. In general, you will be adding functionality to the Blaise language and altering your compiler appropriately. If you have any questions, ask at the end of the help session or e-mail the TAs.

Final Comments Start soon each part of the assignment may take a while. Make sure you understand scoping and frames, as well as the visitor pattern before starting Semantic Analysis. Any questions about the assignment? If you have any further questions, see a TA on hours or e-mail the list. Good luck!