Compiler Construction Recap. 2 Announcements PA4: extension until end of exam period – Not a single day more, for any reason! PA5: bonus exercise – Will.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Intermediate Code Generation
Intermediate Representation III. 2 PAs PA2 deadline is
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Lecture 14 – Recap Eran Yahav Thanks to Ohad Shacham (TAU) for some of the slides in this lecture 1
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Environments and Evaluation
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
The Structure of the GNAT Compiler. A target-independent Ada95 front-end for GCC Ada components C components SyntaxSemExpandgigiGCC AST Annotated AST.
Compiler Construction Recap Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
COP4020 Programming Languages
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
CSC 338: Compiler design and implementation
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
CSC 8505 Compiler Construction
MiniJava Compiler A multi-back-end JIT compiler of Java.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Winter Compiler Construction T11 – Recap
A Simple Syntax-Directed Translator
Constructing Precedence Table
PROGRAMMING LANGUAGES
CS 536 / Fall 2017 Introduction to programming languages and compilers
Basic Program Analysis: AST
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Chapter 6 Intermediate-Code Generation
CSE401 Introduction to Compiler Construction
Winter Compiler Construction T10 – IR part 3
R.Rajkumar Asst.Professor CSE
Subject: Language Processor
Introduction Chapter : Introduction.
Presentation transcript:

Compiler Construction Recap

2 Announcements PA4: extension until end of exam period – Not a single day more, for any reason! PA5: bonus exercise – Will be posted immediately after semester ends

3 Exam 18/02/2013 at 9:00 Past exams on my website חומר פתוח Possible questions – Extend IC – Parsing – Register allocation – …

4 Scanning CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … Issues in lexical analysis: Language changes: New keywords New operators New meta-language features (e.g., annotations) // An example program class Hello { boolean state; static void main(string[] args) { Hello h = new Hello(); boolean s = h.rise(); Library.printb(s); h.setState(false); } boolean rise() { boolean oldState = state; state = true; return oldState; } void setState(boolean newState) { state = newState; } }

5 Parsing and AST CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … Issues in syntax analysis: Grammars: LL(1), LR(0) Ambiguity Parser uses token stream, and generates derivation tree

6 Parsing and AST prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … Syntax tree built during parsing Parser uses token stream, and generates derivation tree CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … ProgAST ClassAST classList FieldAST[0] type:BoolType name:state MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Should know difference between derivation tree and AST Know how to build AST from input

7 Question: Parsing Is the following grammar is LR(0)? S -> B $ B -> id P | id ( E ] P -> epsilon | ( E ) E -> B | B,E A grammar with epsilon productions is not LR(0)

8 Other possible questions Is the following grammar in LR(k)? Build a parser for given grammar Run an input string using your parser …

9 Semantic analysis ProgAST ClassAST classList FieldAST[0] type:BoolType MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Representing scopes Type checking Semantic checks SymbolKindType HelloclassHello SymbolKindTypeProperties statefieldbooleaninstance mainmethodstring[]->voidstatic risemethodvoid->booleaninstance setStatemethodboolean->voidinstance SymbolKindType newStateparamint (Program) (Hello) (setState) …

10 Semantic conditions What is checked at compile time, and what is checked at runtime? EventC/R Program execution haltsR (undecidable in general) break/continue inside a while statement C Array index within boundR (undecidable in general) In Java, the cast statement (A)f is legal Depends: if A is subtype of f then checked at runtime (potentially raising exception); otherwise flagged as an error during compilation In Java, call o.m(…) is illegal since m is private C

11 Question: IC language Support Java override annotation inside comments – – Annotation is written above method to indicate it overrides a method in superclass Describe the phases in the compiler affected by the change and the changes themselves class A { void rise() {…} } class B extends A { void rise() {…} } class A { void rise() {…} } class B extends A { void ris() {…} } Legal program Illegal program

12 Answer The change affects the lexical analysis, syntax analysis and semantic analysis Does not affect later phases – User-level semantic condition

13 Changes to scanner Add pattern inside comment state patterns Add Java action code to comments: Instead of not returning any token, we now return a token for the annotation boolean override=false; % // { override=false; yybegin(comment); { override=true; } \n { if (override) return new Token(…,override,…) }

14 Changes to parser and AST method  static type name params ‘{‘ mbody ‘}’ | type name params ‘{‘ mbody ‘}’ | OVERRIDE type name params ‘{‘ mbody ‘}’ Add a Boolean flag to the method AST node to indicate that the method is annotated

15 Changes to semantic analysis Suppose we have an override annotation above a method m in class A We check the following semantic conditions: 1.class A extends a superclass (otherwise it does not make sense to override a method) 2.Traverse the superclasses of A by going up the class hierarchy, until we find the first method m, and check that it has the same signature as A.m If we fail to find such a method, then we report an error

16 Question: IC language Add constructors to IC (must be called)

Answer Treat the constructor as a function, and call when object allocated – Lexical analysis: nothing – Parsing: AST node for constructor – Semantic analysis: Check that every class has a constructor Actual/formal compatibility – IR/code generation: Call the constructor on allocation

18 Translation to IR Accept annotated AST and translate functions into lists of instructions – Compute offsets for fields and virtual methods – Issues: dispatch tables, weighted register allocation

Question: IR Give the method tables for Rectangle and Square class Shape { boolean isShape() {return true;} boolean isRectangle() {return false;} boolean isSquare() {return false;} double surfaceArea() {…} } class Rectangle extends Shape { double surfaceArea() {…} boolean isRectangle() {return true;} } class Square extends Rectangle { boolean isSquare() {return true;} }

20 Answer Shape_isShape Rectangle_isRectangle Shape_isSqaure Rectangle_surfaceArea Shape_isShape Rectangle_isRectangle Sqaure_isSqaure Rectangle_surfaceArea Method table for rectangleMethod table for square

21 Question: IR Suppose we wish to provide type information at runtime – Similar to instanceof in Java x instanceof A returns true iff x is exactly of type A (in Java it can also be subtype of A) Describe the changes in runtime organization needed to support this operator and the translation to IR

22 Answer Use the pointer to the dispatch table as the type indicator Translate x instanceof A as Move x,R0 MoveField R0.0,R0 Compare R0,_DV_A If we want to support the Java operator – Represent the type hierarchy at runtime and generate code to search up the hierarchy – Keep ancestor info for each type to enable constant-time checking

23 Register allocation Sethi Ullman – can only handle expressions without side effect Global register allocation IR registers are treated as local variables When we have an actual spill we use the stack

24 Weighted register allocation Can save registers by reordering subtree computations Label each node with its weight – Weight = number of registers needed – Leaf weight known – Internal node weight w(left) > w(right) then w = left w(right) > w(left) then w = right w(right) = w(left) then w = left + 1 Choose heavier child as first to be translated Have to check that there are no side effects

25 Weighted reg. alloc. example b 5c * array access + a baseindex W=1W=0W=1 W=2 Phase 1: - check absence of side-effects in expression tree - assign weight to each AST node R0 := TR[ a+b[5*c] ]

26 Reminder R0 := TR[a+(b+(c*d))] b cd * + + a R0 R1 R2 Translation using all optimizations shown until now uses 3 registers R2 R1 left child first b cd * + + a R0 Managed to save two registers R0 right child first R0

27 Sethi Ullman What type of tree is worst case for SU with respect to the tree’s height?

28 Sethi Ullman What type of tree is best case for SU with respect to the tree’s height?......

29 Sethi Ullman What type of tree maximizes the ratio between registers allocated by traversing the tree left-to-right and right-to-left?......