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

Similar documents
Formal Languages and Compilers Lecture X Intermediate Code Generation

CMPSC 160 Translation of Programming Languages. Three-Address Code

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

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

Intermediate Code Generation

Syntax-Directed Translation

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

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

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CS2210: Compiler Construction. Code Generation

Module 25 Control Flow statements and Boolean Expressions

Module 26 Backpatching and Procedures

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

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

Intermediate Code Generation

UNIT IV INTERMEDIATE CODE GENERATION

Languages and Compiler Design II IR Code Generation I

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

Intermediate Code Generation

Dixita Kagathara Page 1

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

Generating 3-Address Code from an Attribute Grammar

Intermediate Representations

Intermediate Code Generation

Intermediate Code Generation

THEORY OF COMPILATION

CSCI Compiler Design

Chapter 6 Intermediate Code Generation

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

Intermediate Code Generation

Concepts Introduced in Chapter 6

Intermediate Code Generation Part II

Syntax-Directed Translation Part II

Intermediate Code Generation

Page No 1 (Please look at the next page )

intermediate-code Generation

Time : 1 Hour Max Marks : 30

Lexical Considerations

Formal Languages and Compilers Lecture I: Introduction to Compilers

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

1 Lexical Considerations

Lexical Considerations

Concepts Introduced in Chapter 6

Formal Languages and Compilers Lecture VI: Lexical Analysis

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

CSc 453 Intermediate Code Generation

PART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309

TACi: Three-Address Code Interpreter (version 1.0)

Compilers. Compiler Construction Tutorial The Front-end

Databases 2 Lecture IV. Alessandro Artale

Intermediate Representa.on

Intermediate Code Generation

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

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

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

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Principles of Compiler Design

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

PRINCIPLES OF COMPILER DESIGN

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

KU Compilerbau - Programming Assignment

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

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

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

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

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

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

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Decaf Language Reference Manual

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

LECTURE 17. Expressions and Assignment

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

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

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

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

CSCI565 Compiler Design

This book is licensed under a Creative Commons Attribution 3.0 License

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Lecture 1. Types, Expressions, & Variables

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

ECS 142 Project: Code generation hints (2)

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

Intermediate Representations

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

& Simulator. Three-Address Instructions. Three-Address Instructions. Three-Address Instructions. Structure of an Assembly Program.

Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching

The PCAT Programming Language Reference Manual

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Three-Address Code IR

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Transcription:

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free University of Bolzano Room: 221 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ 2003/2004 Second Semester

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (2 Summary of Lecture VIII Three-Address Code Code for Assignments Boolean Expressions and Flow-of-Control Statements

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (3 Intermediate Code Generation An intermediate code is generated as a program for an abstract machine. 1. The intermediate code should be easy to translate into the target program. 2. A machine-independent Code Optimizer can be applied before generating the target code. As intermediate code we consider the three-address code, similar to assembly: sequence of instructions with at most three operands such that: 1. There is at most one operator, in addition to the assignment (we make explicit the operators precedence. 2. The general form is: x := y op z where x,y,z are identifiers, constants or compiler-generated temporary names. 3. Temporary names must be generated to compute intermediate operations.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (4 Types of Three-Address Statements Three-Address statements are akin to assembly code: Statements can have labels and there are statements for flow-of-control. 1. Assignment Statements: x := y op z. 2. Unary Assignment Statements: x := op y. 3. Copy Statements: x := y. 4. Unconditional Jump: goto L, with L a label of a statement. 5. Conditional Jump: if x relop y goto L. 6. Procedure Call: param x, call p,n for calling a procedure, p, with n parameters. return y is the returned value of the procedure. 7. Indexed assignments: x := y[i] or x[i] := y. Note: x[i] denotes the value in the location i memory units beyond location x. 8. Pointer Assignmets: x := &y, x := *y, or *x :=y; where &y stands for the address of y, and *y the value of y.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (5 Summary Three-Address Code Code for Assignments Boolean Expressions and Flow-of-Control Statements

" Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (6 Code for Assignments The following S-attributed definition generates three-address code for assignments. PRODUCTION SEMANTIC RULES!!! "! # # %$ + & * ( '& id

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (7 Code for Assignments (Cont. The synthesized attribute assignment. represents the three-address code for the To expressions we associate two synthesized attributes: 1., holds the name for the value of ; 2., the three-address code for evaluating. The function newtemp generates distinct names. The function gen generates three-address code such that: 1. Everything quoted is taken literally; 2. The rest is evaluated.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (8 & " Code for Assignments: An Example Given the assignment " $ the code generated by the above grammar is: S id := E + & * ( a E + E * E id id - id d b c

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (9 Assignments and Symbol Tables We assume that names stands for pointers to their symbol table entries. Note. Under this assumption Temporary Names must be also entered into the symbol table as they are created by the newtemp function. The function lookup(id.name return nil if the entry is not found, otherwise a pointer to the entry is returned. The lookup(id.name can be easily modified to account for scope: If name does not appear in the current symbol table the enclosing symbol table is checked (see the Lecture on Symbol Table. Instead of building a code attribute we emit three-address code to an output file This is always possible if the code of the non-terminal on the left is obtained by concatenating the code attributes of the non-terminals on the right in the same order (perhaps with some additional strings in between.

" Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (10 Assignments and Symbol Tables: The Translation PRODUCTION SEMANTIC RULES +!! "!! # %$! + & * ( &! id +

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (11 Summary Three-Address Code Code for Assignments Boolean Expressions and Flow-of-Control Statements

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (12 + Boolean Expressions Boolean Expressions are used to either compute logical values or as conditional expressions in flow-of-control statements. We consider Boolean Expressions with the following grammar: & There are two methods to evaluate Boolean Expressions 1. Numerical Representation. Encode true with 1 and false with 0 and we proceed analogously to arithmetic expressions. 2. Jumping Code. We represent the value of a Boolean Expression by a position reached in a program.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (13 Numerical Representation of Boolean Expressions Expressions will be evaluated from left to right assuming that: or and and are left-associative, and that or has lowest precedence, then and, then not. Example 1. The translation for a or (b and (not c is: Example 2. A relational expression such as a b is equivalent to the conditional statement if a b then 1 else 0 and its translation involves jumps to labeled statements: %

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (14 Numerical Representation: The Translation The following S-Attributed definition makes use of the global variable nextstat that gives the index of the next three-address code statement and is incremented by emit. We use the attribute op to determine which of the comparison operators is represented by relop.

+ Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (15 PRODUCTION SEMANTIC RULES!!!!! # #!! # &

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (16 + Jumping Code for Boolean Expressions The value of a Boolean Expression is represented by a position in the code. Consider Example 2: We can tell what value statement 101 or statement 103. will have by whether we reach Jumping code is extremely useful when Boolean Expressions are in the context of flow-of-control statements. We start by presenting the translation for flow-of-control statements generated by the following grammar:

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (17 Flow-of-Control Statements In the translation, we assume that a three-address code statement can have a symbolic label, and that the function newlabel generates such labels. We associate with two labels: 1. E.true, the label to which control flows if 2. E.false, the label to which control flows if is true; is false. We associate to the inherited attribute S.nest that represents the label attached to the first statement after the code for. Note 1. This method of generating labels associated to S.nest can lead to a proliferation of label: The backpatching method (see the Book creates labels only when needed. Note 2. In the following translation the initialization of S.next is not shown.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (18 Flow-of-Control Statements (Cont. The following figures show how the flow-of-control statements are translated.

+ Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (19 Flow-of-Control Statements: The Translation PRODUCTION SEMANTIC RULES

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (20 Jumping Code for Boolean Expressions (Cont. Boolean Expressions are translated in a sequence of conditional and unconditional jumps to either E.true or E.false. a b. The code is of the form: if a b then goto E.true goto E.false! If!! is true then must be evaluated, so code for. # is true, so Analogous considerations apply.. Otherwise, is set to the label of the first statement in the not. We just interchange the true and false with that for!. Note. Both the true and false attributes are inherited and the translation is an L-attributed grammar.

+ Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (21 Jumping Code for Boolean Expressions: The Translation PRODUCTION SEMANTIC RULES!!!! # #!!!!!!!!!!!!!! &

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (22 Flow-of-Control and Boolean Expressions: An Example Example. Translate the following statement: while a if c else b do d then x := y + z x := y - z

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (23 Mixed-Mode Boolean Expressions Boolean Expressions often contain Arithmetic sub-expressions e.g. (a+b c. On the other hand, if true = 1 and false = 0, then (a b+(b a can be an Arithmetic expression with value if a=b and otherwise. The method of representing Boolean Expressions by Jumping code is still a good option.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (24 Mixed-Mode Boolean Expressions (Cont. Consider the following Grammar:, produces an arithmetic result, and the arguments can be mixed;, produces a Boolean result, and both arguments must be Boolean;, produces a Boolean result, and the arguments can be mixed; id is assumed of type arithmetic.

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (25 Mixed-Mode Boolean Expressions: The Translation To generate code we use a synthesized attribute arith or bool., that will be either Boolean Expressions will have inherited attributes E.true and E.false useful for the jumping code. Arithmetic Expressions will have the synthesized attribute E.place standing for the (temporary variable holding the value of. The global variable nextstat gives the index of the next three-address code statement and is incremented by gen.

+ Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (26 Mixed-Mode Boolean Expressions: The Translation (Cont.. Let just show the semantic rule for!!! *!!!! *!! +!!!! #!

Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (27 Summary of Lecture VIII Three-Address Code Code for Assignments Boolean Expressions and Flow-of-Control Statements