Concepts Introduced in Chapter 6

Similar documents
Concepts Introduced in Chapter 6

Intermediate Code Generation

Chapter 6 Intermediate Code Generation

Type Checking. Error Checking

Intermediate Code Generation

Type systems. Static typing

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Intermediate Code Generation

CS2210: Compiler Construction. Code Generation

Static Checking and Type Systems

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

intermediate-code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Intermediate Code Generation Part II

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

UNIT IV INTERMEDIATE CODE GENERATION

LECTURE 17. Expressions and Assignment

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Intermediate Representations

Intermediate Code Generation

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Intermediate Representa.on

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

1 Lexical Considerations

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Introduction to Programming Using Java (98-388)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Languages and Compiler Design II IR Code Generation I

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Intermediate Representations

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Lexical Considerations

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate Code Generation

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Principles of Compiler Design

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

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Lexical Considerations

Dixita Kagathara Page 1

Intermediate Code Generation

Alternatives for semantic processing

Motivation for typed languages

Intermediate representation

A Simple Syntax-Directed Translator

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

LECTURE 18. Control Flow

Expressions and Assignment

Intermediate Representations & Symbol Tables

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Prof. Navrati Saxena TA: Rochak Sachan

Principles of Programming Languages

The PCAT Programming Language Reference Manual

Module 27 Switch-case statements and Run-time storage management

TACi: Three-Address Code Interpreter (version 1.0)

Principles of Compiler Design

CSE302: Compiler Design

Semantic actions for declarations and expressions

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

The SPL Programming Language Reference Manual

Type Systems, Type Inference, and Polymorphism

Chapter 2: Using Data

Expressions & Assignment Statements

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Computational Expression

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

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

NOTE: Answer ANY FOUR of the following 6 sections:

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Computer Components. Software{ User Programs. Operating System. Hardware

Expressions and Assignment Statements

Type Checking. Chapter 6, Section 6.3, 6.5

Types II. Hwansoo Han

Operators and Expressions

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Transcription:

Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler Construction 1

Intermediate Code Generation Is Performed by the Front End EECS 665 Compiler Construction 2

Intermediate Code Generation Intermediate code generation can be done in a separate pass (e.g. Ada requires complex semantic checks) or can be combined with parsing and static checking in a single pass (e.g. Pascal designed for one-pass compilation). Generating intermediate code rather than the target code directly facilitates retargeting allows a machine independent optimization pass to be applied to the intermediate representation EECS 665 Compiler Construction 3

Types of Intermediate Representation Syntax trees and Directed Acyclic Graphs (DAG) nodes represent language constructs children represent components of the construct DAG represents each common subexpression only once in the tree helps compiler optimize the generated code followed by Fig. 6.3, 6.4, 6.6 EECS 665 Compiler Construction 4

Types of Intermediate Representation Three-address code general form: x = y op z (2 source, 1 destination) widely used form of intermediate representation Types of three-address code quadruples, triples, static single assignment (SSA) Postfix 0 operands (just an operator) all operands are on a compiler-generated stack followed by Fig. 6.8 EECS 665 Compiler Construction 5

Types of Intermediate Representation Two-address code x := op y where x := x op y is implied One-address code op x where ac := ac op x is implied and ac is an accumulator EECS 665 Compiler Construction 6

Types of Three-Address Code Quadruples has 4 fields, called op, arg1, arg2, and result often used in compilers that perform global optimization on intermediate code. easy to rearrange code since result names are explicit. followed by Fig. 6.10 EECS 665 Compiler Construction 7

Triples Types of Three-Address Code (cont...) similar to quadruples, but implicit results and temporary values result of an operation is referred to by its position triples avoid symbol table entries for temporaries, but complicate rearrangement of code. indirect triples allow rearrangement of code since they reference a pointer to a triple instead. followed by Fig. 6.11, 6.12 EECS 665 Compiler Construction 8

Types of Three-Address Code (cont...) Static Single Assignment (SSA) form an increasing popular format in optimizing compilers all assignments in SSA are to variables with a distinct name see Figure 6.13 function to combine multiple variable definitions if (flag) if (flag) x = -1; x = -1; x 1 = -1; x 2 = -1; y = x * a; x 3 = (x1,x2); y = x 3 * a; followed by Fig. 6.13 EECS 665 Compiler Construction 9

Three Address Stmts Used in the Text x := y op z # binary operation x := op y # unary operation x := y # copy or move goto L # unconditional jump if x relop y goto L # conditional jump param x # pass argument call p,n # call procedure p with n args return y # return (value is optional) x := y[i], x[i] := y # indexed assignments x := &y # address assignment x := *y, *x = y # pointer assignments EECS 665 Compiler Construction 10

Postfix Having the operator after operand eliminates the need for parentheses. (a+b) * c ab + c * a * (b + c) abc + * (a + b) * (c + d) ab + cd + * Evaluate operands by pushing them on a stack. Evaluate operators by popping operands, pushing result. A = B * C + D ABC * D + = EECS 665 Compiler Construction 11

Postfix (cont.) Activity push A push B push C Stack A AB ABC * Ar* push D Ar*D + Ar+ = Code generation of postfix code is trivial for several types of architectures. EECS 665 Compiler Construction 12

Translation of Declarations Assign storage and data type to local variables. Using the declared data type determine the amount of storage (integer 4 bytes, float 8 bytes, etc.) assign each variable a relative offset from the start of the activation record for the procedure followed by Fig. 6.17, 6.15, 6.16 EECS 665 Compiler Construction 13

Translation of Expressions Translate arithmetic expressions into three-address code. see Figure 6.19 a = b +-c is translated into: t 1 = minus c t 2 = b + t 1 a = t 2 EECS 665 Compiler Construction 14

Translation of Boolean Expressions Boolean expressions are used in statements, such as if, while, to alter the flow of control. Boolean operators! NOT (highest precedence) && AND (mid precedence, left associative) OR (lowest precedence, left associative) <, <=, >, >=, =,!=, are relational operators Short-circuit code B1 B2, if B1 true, then don't evaluate B2 B1 && B2, if B1 false, then don't evaluate B2 followed by Fig. 6.37 EECS 665 Compiler Construction 15

Translation of Control-flow Statements Control-flow statements include: if statement if statement else statement while statement followed by Fig. 6.35, 6.36 EECS 665 Compiler Construction 16

Control-Flow Translation of if-statement Consider statement: if (x < 100 x > 200 && x!= y) x = 0; if x < 100 goto L 2 goto L 3 L 3 : if x > 200 goto L 4 goto L 1 L 4 : if x!= y goto L 2 goto L 1 L 2 : x = 0 L 1 : EECS 665 Compiler Construction 17

Backpatching Allows code for boolean expressions and flow-ofcontrol statements to be generated in a single pass. The targets of jumps will be filled in when the correct label is known. EECS 665 Compiler Construction 18

Backpatching an ADA While Loop Example while a < b loop a := a + cost; end loop; loop_stmt : WHILE m cexpr LOOP m seq_of_stmts n END LOOP m ';' { dowhile ($2, $3, $5, $7, $10); } ; EECS 665 Compiler Construction 19

Backpatching an Ada While Loop (cont.) loop_stmt : WHILE m cexpr LOOP m seq_of_stmts n END LOOP m ';' { dowhile ($2, $3, $5, $7, $10); } ; void dowhile (int m1, struct sem_rec *e, int m2, struct sem_rec *n1, int m3) { backpatch(e back.s_true, m2); } backpatch(e s_false, m3); backpatch(n1, m1); return(null); EECS 665 Compiler Construction 20

Backpatching an Ada If Statement Examples: if a < b then if a < b then if a < b then a := a +1; a := a + 1; a := a + 1; end if; else elsif a < c then a := a + 2; a := a + 2; end if;... end if; EECS 665 Compiler Construction 21

Backpatching an Ada If Statement (cont.) if_stmt : IF cexpr THEN m seq_of_stmts n elsif_list0 else_option END IF m ';' ; { doif($2, $4, $6, $7, $8, $11); } elsif_list0 : {$$ = (struct sem_rec *) NULL; } elsif_list0 ELSIF m cexpr THEN m seq_of_stmts n ; {$$ = doelsif($1, $3, $4, $6, $8); } else_option: { $$ = (struct sem_rec *) NULL; } ELSE m seq_of_stmts { $$ = $2; } EECS 665 Compiler Construction 22

m if_stmt : IF cexpr THEN m seq_of_stmts n elsif_list0 else_option END IF { doif($2, $4, $6, $7, $8, $11); } void doif(struct sem_rec *e, int m1, struct sem_rec *n1, struct sem_rec *elsif, int elsopt, int m2) { backpatch(e back.s_true, m1); backpatch(n1, m2); if (elsif!= NULL) { backpatch(e s_false, elsif s_place); } backpatch(elsif back.s_link, m2); if (elsopt!= 0) backpatch(elsif s_false, elsopt); else backpatch(elsif s_false, m2); } else if (elsopt!= 0) backpatch(e s_false, elsopt); else backpatch(e s_false, m2); EECS 665 Compiler Construction 23

Backpatching an Ada If Statement (cont.) elsif_list0 : { $$ = (struct sem_rec *) NULL; } elsif_list0 ELSIF m cexpr THEN m seq_of_stmts n { $$ = doelsif($1, $3, $4, $6, $8); } ; struct sem_rec *doelsif (struct sem_rec *elsif, int m1, struct sem_rec *e, int m2, struct sem_rec *n1) { backpatch (e back.s_true, m2); if (elsif!= NULL) { backpatch(elsif s_false, m1); return (node(elsif s_place, 0, merge(n1, elsif back.s_link), e s_false)); } else return (node(m1, 0, n1, e s_false)); } EECS 665 Compiler Construction 24

Translating Record Declarations Example: struct foo {int x; char y; float z;}; type : CHAR { $$ = node(0,t_char,1,0,0); } FLOAT { $$ = node(0,t_float,8,0,0); } INT { $$ = node(0,t_int,4,0,0); } STRUCT '{' fields '}' { $$ = node(0,t_struct,$3 width,0,0); } ; fields : field ';' { $$ = addfield($1,0); } fields field ';' { $$ = addfield($2,$1); } ; field : type ID { $$ = makefield($2,$1); } field '[' CON ']' { $1 width = $1 width*$3; $$ = $1; } ; EECS 665 Compiler Construction 25

Translating Record Declarations (cont.) fields : field ';' { $$ = addfield($1, 0); } fields field ';' { $$ = addfield($2,$1); } ; struct sem_rec *addfield(struct id_entry *field, struct sem_rec *fields) { if (fields!= NULL) { field s_offset = fields width; return (node(0,0,field s_width+fields width,0,0)); } else { field s_offset = 0; return (node(0,0,field s_width,0,0)); } } EECS 665 Compiler Construction 26

Translating Record Declarations (cont.) field : type ID {$$ = makefield($2,$1);} field '[' CON ']' {$1 s_width = $1 s_width*$3; $$ = $1;} ; struct id_entry *makefield(char *id, struct sem_rec *type) { struct id_entry *p; } if ((p = lookup(id, 0))!= NULL) fprintf( stderr, ''duplicate field name\n''); else { p = install(id, 0); p s_width = type width; p attributes = field_descriptor; } return (p); EECS 665 Compiler Construction 27

Translating Switch Statements switch (E) { case V1: case V2:... case Vn-1: default: } S1 S2 Sn-1 Sn EECS 665 Compiler Construction 28

Translating Large Switch Statements switch (E) { case 1: case 2:... case 1000: default: } S1 S2 S1000 S1001 EECS 665 Compiler Construction 29

Translating Large Switch Statements goto test L1: code for S1 L2: code for S2... L1000: code for S1000 LD: code for S1001 goto next test: check if expr is in range if not goto LD t := m[jump_table_base + expr << 2]; goto t; next: followed by Fig. 6.49, 6.50 EECS 665 Compiler Construction 30

Addressing One Dimensional Arrays Assume w is the width of each array element in array A[] and low is the first index value. The location of the ith element in A. base + (i low)*w Example: INTEGER ARRAY A[5:52];... N = A[I]; low=5, base=addr(a[5]), width=4 address(a[i])=addr(a[5])+(i 5)*4 EECS 665 Compiler Construction 31

Addressing One Dimensional Arrays Efficiently Can rewrite as: i*w + base low*w address(a[i]) = I*4 + addr(a[5]) 5*4 = I*4 + addr(a[5]) 20 EECS 665 Compiler Construction 32

Addressing Two Dimensional Arrays Assume row -major order, w is the width of each element, and n2 is the number of values i2 can take. address = base + ((i1 low1)*n2 + i2 low2)*w Example in Pascal: var a : array[3..10, 4..8] of real; addr(a[i][j]) = addr(a[3][4]) + ((i 3)*5 + j 4)*8 Can rewrite as address = ((i1*n2)+i2)*w + (base ((low1*n2)+low2)*w) addr(a[i][j]) = ((i*5)+j)*8 + addr(a[3][4]) ((3*5)+4)*8 = ((i*5)+j)*8 + addr(a[3][4]) 152 EECS 665 Compiler Construction 33

Addressing C Arrays Lower bound of each dimension of a C array is zero. 1 dimensional base + i*w 2 dimensional base + (i1*n2 + i2)*w 3 dimensional base + ((i1*n2 + i2)*n3 + i3)*w EECS 665 Compiler Construction 34

1. Type Checks Ex: int a, c[10], d; a = c + d; Static Checking 2. Flow-of-control Checks Ex: main { int i; i++; break; } EECS 665 Compiler Construction 35

Static Checking (cont.) 3. Uniqueness Checks Ex: program foo ( output ); var i, j : integer; a,i : real; 4. Name-related Checks Ex: LOOPA: LOOP EXIT WHEN I =N; I = I + 1; TERM := TERM / REAL ( I ); END LOOP LOOPB; EECS 665 Compiler Construction 36

Static and Dynamic Type Checking Static type checking is performed by the compiler. Dynamic type checking is performed when the target program is executing. Some checks can only be performed dynamically: var i : 0..255;... i := i+1; EECS 665 Compiler Construction 37

Why is Static Checking Preferable to Dynamic Checking? There is no guarantee that the dynamic check will be tested before the application is distributed. The cost of a static check is at compile time, where the cost of a dynamic check may occur every time the associated language construct is executed. EECS 665 Compiler Construction 38

Basic Terms Atomic types - types that are predefined or known by the compiler boolean, char, integer, real in Pascal Constructed types - types that one declares arrays, records, pointers, classes Type expression - the type associated with a language construct Type system - a collection of rules for assigning type expressions to various parts of a program EECS 665 Compiler Construction 39

Equivalence of Type Expressions Name equivalence - views each type name as a distinct type Structural equivalence - names are replaced by the type expressions they define Ex: type link = cell; var next : link; last : link; p : cell; q, r : cell; EECS 665 Compiler Construction 40

Equivalence of Type Expressions (cont.) Variable next last p q r Type Expression link link pointer (cell) pointer (cell) pointer (cell) structural equivalence - all are equivalent name equivalence - next == last, p == q == r but p!= next EECS 665 Compiler Construction 41

Type Checking Perform type checking assign type expression to all source language components determine conformance to the language type system A sound type system statically guarantees that type errors cannot occur at runtime. A language implementation is strongly typed if the compiler guarantees that the program it accepts will run without type errors. EECS 665 Compiler Construction 42

Type synthesis Rules for Type Checking build up type of expression from types of subexpressions if f has type s t and x has type s, then expression f(x) has type t Type inference determine type of a construct from the way it is used if f(x) is an expression then for some and, f has type and x has type EECS 665 Compiler Construction 43

Example of a Simple Type Checker Production P D; E Semantic Rule D D; D D id : T { addtype(id.entry, T.type); } T char { T.type = char; } T integer { T.type = integer; } T T 1 { T.type = pointer (T 1.type); } T array[num]of T 1 { T.type = array(num.val,t 1.type); } E literal { E.type = char; } E num { E.type = integer; } EECS 665 Compiler Construction 44

Example of a Simple Type Check (cont.) Production Semantic Rule E id { E.type = lookup(id.entry); } E E 1 mod E 2 E E 1 [E 2 ] { E.type = E 1.type == integer && E 2.type == integer? integer : type_error( ); } { E.type = E 2.type == integer && isarray(e 1.type, &t)? t : type_error( ); } E E 1 { E.type = ispointer(e 1.type,&t)? t : type_error( ); } EECS 665 Compiler Construction 45

Type Conversions - Coercions An implicit type conversion. In C or C++, some type conversions can be implicit assignments operands to arithmetic and logical operators parameter passing return values EECS 665 Compiler Construction 46

Overloading in Java A function or operator can represent different operations in different contexts Example 1 operators '+', '-' etc., are overloaded to work with different data types Example 2 function overloading resolved by looking at the arguments of a function void err ( ) {... } void err (String s) {... } EECS 665 Compiler Construction 47

Polymorphism The ability for a language construct to be executed with arguments of different types Example 1 function length can be called with different types of lists Example 2 fun length (x) = if null (x) then 0 else length (tail(x)) + 1 templates in C++ Example 3 using the object class in Java EECS 665 Compiler Construction 48