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

Similar documents
Intermediate Code Generation

CMPSC 160 Translation of Programming Languages. Three-Address Code

2 Intermediate Representation. HIR (high level IR) preserves loop structure and. More of a wizardry rather than science. in a set of source languages

Module 26 Backpatching and Procedures

Intermediate Code Generation Part II

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

CS2210: Compiler Construction. Code Generation

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

Formal Languages and Compilers Lecture X Intermediate Code Generation

Module 25 Control Flow statements and Boolean Expressions

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

Dixita Kagathara Page 1

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

Syntax-Directed Translation

Principles of Programming Languages

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

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

Run-Time Environments

Intermediate Code Generation

Intermediate Code Generation

Time : 1 Hour Max Marks : 30

Generating 3-Address Code from an Attribute Grammar

Intermediate Code Generation

COP5621 Exam 3 - Spring 2005

Chapter 6 Intermediate Code Generation

THEORY OF COMPILATION

Intermediate Representations

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

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

CSCI Compiler Design

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Intermediate Representations & Symbol Tables

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate Code Generation

Intermediate Code Generation

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Intermediate Code Generation

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

PRINCIPLES OF COMPILER DESIGN

Intermediate Code Generation

Semantic Actions and 3-Address Code Generation

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

CSCI565 Compiler Design

intermediate-code Generation

Intermediate Code Generation

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

CSc 453 Intermediate Code Generation

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

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

Compiler Construction Assignment 4 Spring 2017


Programming Languages and Compiler Design

CSE302: Compiler Design

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

Concepts Introduced in Chapter 6

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

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

Languages and Compiler Design II IR Code Generation I

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

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

UNIT IV INTERMEDIATE CODE GENERATION

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

CSCI565 Compiler Design

CSC 467 Lecture 13-14: Semantic Analysis

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Concepts Introduced in Chapter 6

CS111: PROGRAMMING LANGUAGE II

VIVA QUESTIONS WITH ANSWERS

ECS 142 Project: Code generation hints (2)

Syntax-Directed Translation Part II

G Programming Languages - Fall 2012

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

Type Checking. Chapter 6, Section 6.3, 6.5

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run-Time Environments

Type Checking. Error Checking

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

Intermediate-Code Generat ion

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

Introduction. C provides two styles of flow control:

CSC Web Programming. Introduction to JavaScript

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Intermediate Code Generation Part II

CS 432 Fall Mike Lam, Professor. Code Generation

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

Compiler Theory. (Semantic Analysis and Run-Time Environments)

5. Semantic Analysis!

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Compiler Construction

Static Checking and Type Systems

Syntax-Directed Translation

Java Basic Programming Constructs

COP5621 Exam 4 - Spring 2005

Intermediate Code Generation

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

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

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

Transcription:

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 11a Intermediate Code Generation Elias Athanasopoulos eliasathan@cs.ucy.ac.cy

Declarations For each local name Creation of symbol-table entry Add type and relative address of storage enter(name, type, offset) name of variable type of variable offset address relative to the current block

Translation P à { offset := 0 } à D D à D; D D à id: T { enter(id.name, T.type, offset); à offset := offset + T.width } T à integer { T.type := integer; à T.width := 4} T à real { T.type := real; à T.width := 8} T à array [num] of T 1 { T.type := array(num.val, T 1.type); T à ^T 1 T.width := num.val x T 1.width; } { T.type := pointer(t 1.type); T.width := 4 }

Scope Each procedure is associated with a symbol table, A new symbol table is created when a production is seen: D à proc id D 1 ; S Local variables are placed to the relevant symbol table. Symbol tables are linked with each other, according to how procedures are called.

Example (source code) program sort(input, output); var a: array[0..10] of integer; x: integer; procedure readarray; var i: integer; begin... end; procedure exchange(i, j: integer); begin... end; procedure quicksort(m, n: integer); var k, v: integer; function partition(y, z: integer): integer; var i, j: integer; begin... end {partition}; begin... end {quicksort}; begin... end {sort}.

Example (symbol tables) nil header a x sort readarray header i readarray exchange quicksort readarray exchange quicksort exchange header quicksort header k v partition partition header i j

Symbol-table functions mktable(previous) creates a new table and returns a pointer to the new table. The argument previous points to a previously created symbol table (stored in header), presumably that for the enclosing procedure. enter(table, name, type, offset) creates a new entry of name name in the symbol table pointer to by table. addwidth(table, width) records the cumulative width of all the entries in table in the header associated with this symbol table. enterproc(table, name, newtable) creates a new entry for procedure name in the symbol table pointed to by table. The argument newtable points to the symbol table for this procedure name.

Translation P à M D { addwidth(top(tblptr), top(offset)); pop(tblptr); pop(offset) } M à ε { t := mktable(nil); push(t, tblptr); push(0, offset) } D à D 1 ; D 2 D à proc id ; N D 1 ; S { t := top(tblptr); addwidth(t, top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t) } D à id: T { enter(top(tblptr), id.name, T.type, top(offset)); top(offset) := top(offset) + T.width } N à ε { t := mktable(top(tblptr)); push(t, tblptr); push(0, offset) }

Syntax-directed Definition for Three-address Code PRODUCTION S à id := E SEMANTIC RULES S.code := E.code gen(id.place := E.place) E à E 1 + E 2 E.place:= newtemp; E.code := E 1.code E 2.code gen(e.place := E 1.place + E 2.place) E à E 1 * E 2 E.place:= newtemp; E.code := E 1.code E 2.code gen(e.place := E 1.place * E 2.place) E à -E 1 E.place:= newtemp; E.code := E 1.code gen(e.place := uminus E 1.place) E à ( E 1 ) E.place:= E 1.place; E.code := E 1.code E à id E.place:= id.place; E.code :=

Incorporating the symbol table S à id := E { p := lookup(id.name); if p!= nil then emit(p := E.place) else error } E à E1 + E2 { E.place := newtemp; emit(e.place := E1.place + E2.place) } E à E1 * E2 { E.place := newtemp; emit(e.place := E1.place * E2.place) } E à -E1 { E.place := newtemp; emit(e.place := uminus E1.place) } E à ( E1 ) { E.place := E1.place) } E à id { p := lookup(id.name); if p!= nil then E.place := p else error }

Reusing Temporary Names Temporary variables occupy slots in the symbol table evaluate E 1 to t 1 evaluate E 2 to t 2 t := t 1 + t 2 Use a counter c, initialize to zero. Whenever a temporary name is used, decrement c Whenever a temporary name is created, increment c

Example x := a*b+c*d-e*f STATEMENT VALUE OF c $0 := a * b 1 $1 := c * d 2 $0 := $0 + $1 1 ($0 was used -1, $1 was used -1, $0 is created +1) $1 := e * f 2 $0 := $0 - $1 1 ($0 was used -1, $1 was used -1, $0 is created +1) x := $0 0 0

Boolean Expressions Λογικές Εκφράσεις Boolean expressions are composed by boolean operators (and, or, and not) and relational expressions EàE or E E and E not E (E) id relop id true false relop à < <= = <> > >= Two methods for translating Numerical representation (1 denotes true, 0 denotes false) Flow-of-control representation

TAC for Boolean Expressions a or b and not c t 1 := not c t 2 := b and t 1 t 3 := a or t 2 a < b 100: if a < b goto 103 101: t := 0 102: goto 104 103: t := 1 104: a < b or c < d and e < f 100: if a < b goto 103 101: t 1 := 0 102: goto 104 103: t 1 := 1 104: if c < d goto 107 105: t 2 := 0 106: goto 108 107: t 2 := 1 108: if e < f goto 111 109: t 3 := 0 110: goto 112 111: t 3 := 1 112: t 4 := t 2 and t 3 113: t 5 := t 1 or t 3

Translation Scheme E à E1 or E2 { E.place := newtemp; emit(e.place := E1.place or E2.place) } E à E1 and E2 { E.place := newtemp; emit(e.place := E1.place and E2.place) } E à not E1 { E.place := newtemp; emit(e.place := not E1.place) } E à ( E1 ) { E.place := E1.place) } E à id1 relop id2 { E.place := newtemp; emit( if id1.place relop.op id2.place goto nextstat+3); emit(e.place := 0 ); emit( goto nexstat+2); emit(e.place := 1 ) } E à true { E.place := newtemp; emit(e.place := 1 ) } E à false { E.place := newtemp; emit(e.place := 0 ) }

Syntax-directed Definition PRODUCTION SEMANTIC RULES E à E1 or E2 E 1.true := E.true; E 1.false := newlabel; E 2.true := E.true; E 2.false := E.false; E.code:= E 1.code gen(e 1.false : ) E 2.code E à E 1 and E 2 E 1.true := newlabel; E 1.false := E.false; E 2.true := E.true; E 2.false := E.false; E.code:= E 1.code gen(e 1.true : ) E 2.code E à not E 1 E 1.true:=E.false;E 1.false:=E.true; E.code:= E 1.code E à id 1 relop id 2 E.code:=gen( if id 1.place relop.op id 2. place goto E.true) gen( goto, E.false)

Examples a < b or c < d and e < f if a < b goto Ltrue goto L1 L1: goto L2 goto Lfalse L2: if c < d goto 107 goto Lfalse while a < b do if c < d then x := y + z else x := y z L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t 1 := y + 2 x := t 1 goto L1 L4: t 2 := y - z x := t 2 goto L1 Lnext: goto Lfalse

Backpatching We use two passes One to construct the syntax tree One to traverse the syntax tree (depth-first order) and execute the translation Sometimes labels for booleans and flow-ofcontrol statements are not known in advance in a single pass To reduce passes, we put unknown labels in a list and, once labels are known, we revisit only the ones that are currently unknown

Backpatching functions makelist(i) creates a new list containing only i, an index into the array of quadruples; makelist returns a pointer to the list it has made merge(p 1, p 2 ) concatenates the lists pointed to by p 1 and p 2, and returns a pointer to the concatenated list backpatch(p, i) inserts i as the target label for each of the statements on the list pointed to by p

Translation Scheme E à E1 or M E2 { backpatch(e1.falselist, M.quad); E.truelist := merge(e1.truelist, E2.truelist); E.falselist := E2.falselist } E à E1 and M E2 { backpatch(e1.truelist, M.quad); E.truelist := E2.truelist; E.falselist := merge(e1.falselist, E2.falselist); } E à not E1 { E.truelist := E1.falselist; E.falselist := E1.truelist; }

Translation E à ( E1 ) { E.truelist := E1.truelist; E.falselist := E1.falselist; } E à id1 relop id2 { E.truelist := makelist(nextquad); E.falselist:= makelist(nextquad+1); emit( if id1.place relop id2.place goto _ ); emit( goto _ ) } E à true { E.truelist := makelist(nextquad); emit( goto _ ) } E à false { E.falselist := makelist(nextquad); emit( goto _ ) } M à ε { M.quad := nextquad }