Chapter 6 Intermediate Code Generation

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

Intermediate Code Generation

UNIT IV INTERMEDIATE CODE GENERATION

intermediate-code Generation

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

Intermediate Code Generation

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

Intermediate Code Generation

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6

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

Intermediate Representa.on

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate-Code Generat ion

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

COP5621 Exam 3 - Spring 2005

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

5. Syntax-Directed Definitions & Type Analysis

CS2210: Compiler Construction. Code Generation

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

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

Dixita Kagathara Page 1

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

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

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

Intermediate Representations

Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation

Static Checking and Type Systems

Compiler. --- Intermediate Code Generation. Zhang Zhizheng.

Intermediate Code Generation

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

Module 26 Backpatching and Procedures

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Page No 1 (Please look at the next page )

Time : 1 Hour Max Marks : 30

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

CMPSC 160 Translation of Programming Languages. Three-Address Code

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

Lecture 14 Sections Mon, Mar 2, 2009

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

THEORY OF COMPILATION

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Alternatives for semantic processing

CS558 Programming Languages

Intermediate Representations

Compilers. Compiler Construction Tutorial The Front-end

Intermediate Code Generation

PRINCIPLES OF COMPILER DESIGN

A Simple Syntax-Directed Translator

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

Intermediate Code Generation

Syntax Directed Translation

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

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

Syntax-Directed Translation Part I

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

Principles of Compiler Design

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Intermediate Code Generation

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

CSC 467 Lecture 13-14: Semantic Analysis

CSE302: Compiler Design

Compiler Design. Prepared By Prof. S.N.Mishra Computer Sc Deptt.,IGIT,Sarang

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

Syntax-Directed Translation Part II

Intermediate Code Generation Part II

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

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

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

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

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

More On Syntax Directed Translation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Type Checking. Chapter 6, Section 6.3, 6.5

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

Intermediate Representations & Symbol Tables

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Intermediate Code Generation Part II

Intermediate Code Generation

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Intermediate Code Generation

Lexical Analysis-: The role of lexical analysis buffing, specification of tokens. Recognitions of tokens the lexical analyzer generator lexical

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Semantic Processing (Part 2)

Syntax-Directed Translation

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

1 Lexical Considerations

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

UNIT I INTRODUCTION TO COMPILING

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Semantic Analysis and Intermediate Code Generation

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Transcription:

Chapter 6 Intermediate Code Generation

Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching

Introduction Intermediate code is the interface between front end and back end in a compiler Ideally the details of source language are confined to the front end and the details of target machines to the back end (a m*n model) In this chapter we study intermediate representations, static type checking and intermediate code generation Parser Static Checker Intermediate Code Generator Code Generator Front end Back end

Variants of syntax trees It is sometimes beneficial to crate a DAG instead of tree for Expressions. This way we can easily show the common subexpressions and then use that knowledge during code generation Example: a+a*(b-c)+(b-c)*d + + * a * - d b c

SDD for creating DAG s Production 1) E -> E1+T 2) E -> E1-T 3) E -> T 4) T -> (E) 5) T -> id 6) T -> num Example: 1) p1=leaf(id, entry-a) 2) P2=Leaf(id, entry-a)=p1 3) p3=leaf(id, entry-b) 4) p4=leaf(id, entry-c) 5) p5=node( -,p3,p4) 6) p6=node( *,p1,p5) 7) p7=node( +,p1,p6) Semantic Rules E.node= new Node( +, E1.node,T.node) E.node= new Node( -, E1.node,T.node) E.node = T.node T.node = E.node T.node = new Leaf(id, id.entry) T.node = new Leaf(num, num.val) 8) p8=leaf(id,entry-b)=p3 9) p9=leaf(id,entry-c)=p4 10) p10=node( -,p3,p4)=p5 11) p11=leaf(id,entry-d) 12) p12=node( *,p5,p11) 13) p13=node( +,p7,p12)

Value-number method for constructing DAG s i = + 10 id num 10 + 1 2 3 1 3 To entry for i Algorithm Search the array for a node M with label op, left child l and right child r If there is such a node, return the value number M If not create in the array a new node N with label op, left child l, and right child r and return its value We may use a hash table

Three address code In a three address code there is at most one operator at the right side of an instruction Example: + + * * a - d t1 = b c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4 b c

Forms of three address instructions x = y op z x = op y x = y goto L if x goto L and iffalse x goto L if x relop y goto L Procedure calls using: param x call p,n y = call p,n x = y[i] and x[i] = y x = &y and x = *y and *x =y

Example do i = i+1; while (a[i] < v); L: t1 = i + 1 i = t1 t2 = i * 8 t3 = a[t2] if t3 < v goto L Symbolic labels 100: t1 = i + 1 101: i = t1 102: t2 = i * 8 103: t3 = a[t2] 104: if t3 < v goto 100 Position numbers

Data structures for three address codes Quadruples Has four fields: op, arg1, arg2 and result Triples Temporaries are not used and instead references to instructions are made Indirect triples In addition to triples we use a list of pointers to triples

Example b * minus c + b * minus c Three address code t1 = minus c t2 = b * t1 t3 = minus c t4 = b * t3 t5 = t2 + t4 a = t5 Quadruples op arg1 minus c * b minus c * b + t2 = t5 arg2 result t1 t1 t2 t3 t3 t4 t4 t5 a 0 1 2 3 4 5 op minus c * minus c * + = Triples arg1 arg2 b (0) b (2) (1) (3) a (4) op 35 (0) 36 (1) 37 (2) 38 (3) 39 (4) 40 (5) Indirect Triples 0 1 2 3 4 5 op minus c * minus c * + = arg1 arg2 b (0) b (2) (1) (3) a (4)

Type Expressions Example: int[2][3] array(2,array(3,integer)) A basic type is a type expression A type name is a type expression A type expression can be formed by applying the array type constructor to a number and a type expression. A record is a data structure with named field A type expression can be formed by using the type constructor g for function types If s and t are type expressions, then their Cartesian product s*t is a type expression Type expressions may contain variables whose values are type expressions

Type Equivalence They are the same basic type. They are formed by applying the same constructor to structurally equivalent types. One is a type name that denotes the other.

Declarations

Storage Layout for Local Names Computing types and their widths

Storage Layout for Local Names Syntax-directed translation of array types

Sequences of Declarations Actions at the end:

Fields in Records and Classes

Translation of Expressions and Statements We discussed how to find the types and offset of variables We have therefore necessary preparations to discuss about translation to intermediate code We also discuss the type checking

Three-address code for expressions

Incremental Translation

Addressing Array Elements Layouts for a two-dimensional array:

Semantic actions for array reference

Translation of Array References Nonterminal L has three synthesized attributes: L.addr L.array L.type

Conversions between primitive types in Java

Introducing type conversions into expression evaluation

Abstract syntax tree for the function definition fun length(x) = if null(x) then 0 else length(tl(x)+1) This is a polymorphic function in ML language

Inferring a type for the function length

Algorithm for Unification

Unification algorithm boolean unify (Node m, Node n) { s = find(m); t = find(n); if ( s = t ) return true; else if ( nodes s and t represent the same basic type ) return true; else if (s is an op-node with children s1 and s2 and t is an op-node with children t1 and t2) { union(s, t) ; return unify(s1, t1) and unify(s2, t2); } else if s or t represents a variable { union(s, t) ; return true; } else return false; }

Control Flow boolean expressions are often used to: Alter the flow of control. Compute logical values.

Short-Circuit Code

Flow-of-Control Statements

Syntax-directed definition

Generating three-address code for booleans

translation of a simple if-statement

Backpatching Previous codes for Boolean expressions insert symbolic labels for jumps It therefore needs a separate pass to set them to appropriate addresses We can use a technique named backpatching to avoid this We assume we save instructions into an array and labels will be indices in the array For nonterminal B we use two attributes B.truelist and B.falselist together with following functions: makelist(i): create a new list containing only I, an index into the array of instructions Merge(p1,p2): concatenates the lists pointed by p1 and p2 and returns a pointer to the concatenated list Backpatch(p,i): inserts i as the target label for each of the instruction on the list pointed to by p

Backpatching for Boolean Expressions

Backpatching for Boolean Expressions Annotated parse tree for x < 100 x > 200 && x! = y

Flow-of-Control Statements

Translation of a switch-statement

Readings Chapter 6 of the book