Winter 2006-2007 Compiler Construction T8 – semantic analysis recap + IR part 1 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
Winter Compiler Construction T6 – semantic analysis part I scopes and symbol tables Mooly Sagiv and Roman Manevich School of Computer Science.
Advertisements

CPSC 388 – Compiler Design and Construction
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
Intermediate Representation III. 2 PAs PA2 deadline is
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
8 Intermediate code generation
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.
Program Representations. Representing programs Goals.
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,
Compiler Principles Fall Compiler Principles Lecture 7: Intermediate Representation Roman Manevich Ben-Gurion University.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Run time vs. Compile time
Compiler Construction Semantic Analysis II Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Environments and Evaluation
Compiler Summary Mooly Sagiv html://
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Intermediate Representation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Semantic Analysis II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Compiler Construction Semantic Analysis I Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Precision Going back to constant prop, in what cases would we lose precision?
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Semantic Analysis III + Intermediate Representation I.
Compilation /15a Lecture 7 Getting into the back-end Noam Rinetzky 1.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Chapter# 5 Intermediate code generation.
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.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Winter Compiler Construction T9 – IR part 2 + Runtime organization Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Intermediate Representation I High-Level to Low-Level IR Translation.
Introduction to Code Generation and Intermediate Representations
Compiler Construction Compiler Construction Semantic Analysis I.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Semantic Analysis III + Intermediate Representation I.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Scope of Variable. 2 Scope and visibility Scope (visibility) of identifier = portion of program where identifier can be referred to Lexical scope = textual.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 404 Introduction to Compiler Design
Intermediate code Jakub Yaghob
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 6 Intermediate-Code Generation
Mooly Sagiv and Roman Manevich School of Computer Science
Intermediate Code Generation
Mooly Sagiv and Roman Manevich School of Computer Science
Presentation transcript:

Winter Compiler Construction T8 – semantic analysis recap + IR part 1 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University

2 Announcements In PA3 use -print-ast option not - dump-ast (same option as in PA2) Compiler usage format: java IC.Compiler [options] Check that there is a file argument, don’t just crash Don’t do semantic analysis in IC.cup Exception handling Catch all exceptions you throw in main and handle them (don’t re-throw)

3 Today: Semantic analysis recap Intermediate representation HIR and LIR Beginning IR lowering Today IC Language ic Executable code exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation

4 Semantic analysis flow example class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class C { A o; int z; }

5 Parsing and AST construction IntType BoolType A B C f : int->int … TypeTable Table populated with user-defined types during parsing (or special AST pass) class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class C { A o; int z; } parser.parse() ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … …

6 Defined types and type table class A { int x; int f(int x) { boolean y;... } } class B extends A { boolean y; int t; } class C { A o; int z; } class TypeTable { public static Type boolType = new BoolType(); public static Type intType = new IntType();... public static ArrayType arrayType(Type elemType) {…} public static ClassType classType(String name, String super, ICClass ast) {…} public static MethodType methodType(String name,Type retType, Type[] paramTypes) {…} } abstract class Type { String name; boolean subtypeof(Type t) {...} } class IntType extends Type {...} class BoolType extends Type {...} class ArrayType extends Type { Type elemType; } class MethodType extends Type { Type[] paramTypes; Type returnType; } class ClassType extends Type { ICClass classAST; } IntType BoolType A B C f : int->int … TypeTable

7 Assigning types by declarations IntType BoolType... TypeTable ClassType name = A ClassType name = B ClassType name = C MethodType name = f retType paramTypes type super All type bindings available during parsing time ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … …

8 Symbol tables ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab abstract class SymbolTable { private SymbolTable parent; } class ClassSymbolTable extends SymbolTable { Map methodEntries; Map fieldEntries; } class MethodSymbolTable extends SymbolTable { Map variableEntries; } abstract class Symbol { String name; } class VarSymbol extends Symbol {…} class LocalVarSymbol extends Symbol {…} class ParamSymbol extends Symbol {…}...

9 Scope nesting in IC SymbolKindTypeProperties Global SymbolKindTypeProperties Class SymbolKindTypeProperties Method SymbolKindTypeProperties Block names of all classes fields and methods formals + locals variables defined in block class GlobalSymbolTable extends SymbolTable {} class ClassSymbolTable extends SymbolTable {} class MethodSymbolTable extends SymbolTable {} class BlockSymbolTable extends SymbolTable {

10 Symbol tables ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab this belongs to method scope ret can be used later for type-checking return statements Location name = x type = ? …

11 Sym. tables phase 1 : construction ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab class TableBuildingVisitor implements Visitor {... } Location name = x type = ? … Build tables, Link each AST node to enclosing table abstract class ASTNode { SymbolTable enclosingScope; } enclosingScope symbol ?

12 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab class TableBuildingVisitor implements Visitor {... } During this phase, add symbols from definitions, not uses, e.g., assignment to variable x symbol ? Location name = x type = ? … Sym. tables phase 1 : construct

13 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … ACLASS B C Global symtab xFIELDIntType fMETHODint->int A symtab oCLASSA zFIELDIntType C symtab tFIELDIntType yFIELDBoolType B symtab xPARAMIntType yVARBoolType this VARA ret RET_VARIntType f symtab symbol Location name = x type=? … Sym. tables phase 2 : resolve Resolve each id to a symbol, e.g., in x=5 in foo, x is the formal parameter of f check scope rules: illegal symbol re-definitions, illegal shadowing, illegal use of undefined symbols... class SymResolvingVisitor implements Visitor {... } enclosingScope

14 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … Location name = x type = IntType … Type-check AST IntType BoolType... TypeTable class TypeCheckingVisitor implements Visitor {... } Use type-rules to infer types for all AST expression nodes Check type rules for statements

15 ICClass name = A Field name = x type = IntType Method name = f Param name = x type = IntType Declaration varName = y initExpr = null type = BoolType fields[0] methods[0] body parameters[0] AST Program file = … classes[0] ICClass name = B super = A classes[1] classes[2] … ICClass name = C … … Location name = x type = IntType … Miscellaneous semantic checks class SemanticChecker {... } Check remaining semantic checks: single main method, break/continue inside loops etc.

16 How to write PA3 1. Implement skeleton of type hierarchy + type table 1. Modify IC.cup/Library.cup to use types 2. Check result using -print-ast option 2. Implement Symbol classes and SymbolTable classes 3. Implement symbol table construction Check using -dump-symtab option 4. Implement symbol resolution 5. Implement checks 1. Scope rules 2. Type-checks 3. Remaining semantic rules

17 Class quiz Classify the following events according to compile time / runtime / other time (Specify exact compiler phase and information used by the compiler) 1. x is declared twice in method foo 2. Grammar G is ambiguous 3. Attempt to dereference a null pointer x 4. Number of arguments passed to foo is different from number of parameters 5. reduce/reduce conflict between two rules 6. Assignment to a+5 is illegal since it is not an l-value 7. The non-terminal X does not derive any finite string 8. Test expression in an if statement is not Boolean 9. $r is not a legal class name 10. Size of the activation record for method foo is 40 bytes

18 Intermediate representation Allows language-independent, machine independent optimizations and transformations Easy to translate from AST Easy to translate to assembly Narrow interface: small number of node types (instructions) ASTIR Pentium Java bytecode Sparc optimize

19 Multiple IRs Some optimizations require high-level structure Others more appropriate on low-level code Solution: use multiple IR stages ASTLIR Pentium Java bytecode Sparc optimize HIR optimize

20 What’s in an AST? Administration Declarations For example, class declarations Many nodes do not generate code Expressions Data manipulation Flow of control If-then, while, switch Target language (usually) more limited Usually only jumps and conditional jumps

21 High-level IR (HIR) Close to AST representation High-level language constructs Statement and expression nodes Method bodies Only program’s computation Statement nodes if nodes while nodes statement blocks assignments break, continue method call and return

22 High-level IR (HIR) Expression nodes unary and binary expressions Array accesses field accesses variables method calls New constructor expressions length-of expressions Constants In this project we can make do with HIR=AST

23 Low-level IR (LIR) An abstract machine language Generic instruction set Not specific to a particular machine Low-level language constructs No looping structures, only jumps/conditional jumps We will use – two-operand instructions Advantage – close to Intel assembly language Other alternatives Three-address code: a = b OP c Has at most three addresses (or fewer) Also named quadruples: (a,b,c,OP) Stack machine (Java bytecodes)

24 Arithmetic / logic instructions Abstract machine supports variety of operations a = b OP ca = OP b Arithmetic operations: ADD, SUB, DIV, MUL Logic operations: AND, OR Comparisons: EQ, NEQ, LEQ, GE, GEQ Unary operations: MINUS, NEG

25 Data movement Copy instruction: a = b Load/store instructions: a = *b*a = b Address of instruction a=&b Not used by IC Array accesses: a = b[i]a[i] = b Field accesses: a = b.fa.f = b

26 Branch instructions Label instruction label L Unconditional jump: go to statement after label L jump L Conditional jump: test condition variable a; if true, jump to label L cjump a L Alternative: two conditional jumps: tjump a Lfjump a L

27 Call instruction Supports call statements call f(a1,…,an) And function call assignments a = call f(a1,…,an) No explicit representation of argument passing, stack frame setup, etc.

28 InstructionMeaning Load_Const c, RnRn = c Load_Mem x, RnRn = x Store_Reg Rn, xx = Rn Add_Reg Rm, RnRn = Rn + Rm Subtr_Reg Rm, RnRn = Rn – Rm Mult_Reg Rm, RnRn = Rn * Rm... Note 1: rightmost operand = operation destination Note 2: two register instr - second operand doubles as source and destination Register machine instructions

29 Example x = 42; while (x > 0) { x = x - 1; } Load_Const 42,R1 Store_Mem R1,x test_label: Load_Mem x,R1 Compare_greater R1,0 False_Jump end_label Load_Mem x,R1 Load_Const 1,R2 Subtr_Reg R1,R2 Store_Reg R2,x Jump test_label end_label: (warning: code shown is a naïve translation)

30 Translation (IR Lowering) How to translate HIR to LIR? Assuming HIR has AST form (ignore non-computation nodes) Define how each HIR node is translated Recursively translate HIR (HIR tree traversal) TR[e] = LIR translation of HIR construct e A sequence of LIR instructions Temporary variables = new locations Use temporary variables to store intermediate values during translation

31 LocationEx id = x AddExpr leftright ValueExpr val = 42 visit visit (left) visit (right) TR[x + 42] Load_Mem x, R1 Load_Const 42, R2 Add_Reg R2, R1 Load_Mem x, R1Load_Const 42, R2 Add_Reg R2, R1 Translating expressions – Example

32 Translating expressions (HIR) AST Visitor Generate LIR sequence for each visited node Propagating visitor – register information When visiting a expression node A single Target register designated for storing result A set of available auxiliary registers TR[node, target, available set] Leaf nodes Emit code using target register No auxiliaries required What about internal nodes?

33 Translating expressions Internal nodes Process first child, store result in target register Process second child Target is now occupied by first result Allocate a new register Target2 from available set for result of second child Apply node operation on Target and Target2 Store result in Target All initially available register now available again Result of internal node stored in Target (as expected)

34 LocationEx id = x AddExpr leftright ValueExpr val = 42 visit visit (left) visit (right) TR[x + 42,T,A] Load_Mem x, R1 Load_Const 42, R2 Add_Reg R2, R1 Load_Mem x, R1Load_Const 42, R2 Add_Reg R2, R1 T=R1,A={R2,…,Rn} T=R2,A={R3,…,Rn} Translating expressions – example

35 See you next week