CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Size: px
Start display at page:

Download "CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall"

Transcription

1 CSE443 Compilers Dr. Carl Alphonce 343 Davis Hall

2 Announcements Weekly team meetings starting today Wednesday (4/4) will be a workshop Wednesday - Post questions you'd like addressed in Piazza by Sunday (4/1) evening (no questions posted as of this AM) - Post

3 Phases of a compiler Intermediate Representation (IR): specification and generation Figure 1.6, page 5 of text

4 Control flow Booleans to control flow Booleans as values

5 Boolean expressions! X X & Y X Y We will do short-circuit evaluation if (X Y & Z) then { A } else { B } is translated as if X goto LA iffalse Y goto LB iffalse Z goto LB LA: A goto END LB: B END: (next instruction)

6 Boolean expressions A more concrete example: if ( r < s r = s & 0 < s) then { A } else { B } is translated as if r < s goto LA iffalse r = s goto LB iffalse 0 < s goto LB LA: A goto END LB: B END: (next instruction)

7 Flow-of-Control (6.3.3) if ( B ) then S1 else S2 B.true = newlabel() B.false = newlabel() S.next = S1.next = S2.next S.code = B.code label(b.true) S1.code gen('goto' S.next) label(b.false) S2.code LS1 LS2 B.code S1.code goto END S2.code iftrue: goto LS1 iffalse: goto LS2 END

8 Flow-of-Control (6.3.3) S -> if ( B ) then S1 B.true = newlabel() B.false = S.next = S1.next S.code = B.code label(b.true) S1.code LS1 B.code S1.code iftrue: goto LS1 iffalse: goto END END

9 Flow-of-Control (6.3.3) while ( B ) then S1 BEGIN B.code iftrue: goto LS1 iffalse: begin = newlabel() LS1 goto END B.true = newlabel() B.false = S.next S1.code S1.next = begin S.code = label(begin) B.code label(b.true) S1.code gen('goto' begin) END goto BEGIN

10 6.6.6 Boolean values and jumping code "S -> id = E; if (E) S while (E) S S S Nonterminal E governs the flow on control in S -> while (E) S1. The same nonterminal E denotes a value in S -> id = E [ ]" [p. 408]

11 6.6.6 Boolean values and jumping code "Suppose that attribute E.n denotes the syntax-tree node for an expression E and that nodes are objects. Let method jump generate jumping code at an expression node, and let method rvalue generate code to compute the value of the node into a temporary." [p. 408]

12 Value of Boolean expression "When E appears in S -> while (E) S1, method jump is called at node E.n [ ] When E appears in S -> id = E;, method rvalue is called at node E.n" [p. 408]

13 Figure 6.42 [p. 409] "If E has the form E1 + E2, the method call E.n.rvalue() generates code as discussed in section 6.4." [p. 408] "E-> E1 + E2 E.addr = new Temp() E.code = E1.code E2.code gen(e.addr '=' E1.addr '+' E2.addr)" [p. 379] "If E has the form E1 && E2 we first generate jumping code for E and then assign true or false to a new temporary t at the true and false exits, respectively, from the jumping code." [p. 408] Translation of: x = a<b && c<d iffalse a < b goto L1 iffalse c < d goto L1 t = true goto L2 L1: t = false L2: x = t

14 How to deal with jumps

15 6.6.4 Control-flow translation of Boolean Expressions B -> B1 B2 B1.true = B.true B1.false = newlabel() B2.true = B.true B2.false = B.false B.code = B1.code label(b1.false) B2.code true true B.true B1 B1.false B2 false false B.false

16 6.6.4 Control-flow translation of Boolean Expressions B -> B1 B2 B1.true = B.true B1.false = newlabel() B2.true = B.true B2.false = B.false B.code = B1.code label(b1.false) B2.code B -> B1 && B2 B1.true = newlabel() B1.false = B.false B2.true = B.true B2.false = B.false B.code = B1.code label(b1.true) B2.code

17 Backpatching Allows jump targets to be filled in during a one-pass parse. When (forward) jumps are needed, keep a list of where the addresses need to be inserted. Once address is known, go back and fill in the address ("backpatching").

18 6.7 Backpatching "For specificity, we generate instructions into an instruction array, and labels will be indices into this array." [p. 410] We have an instruction pointer (the first available index in the array), called nextinstr.

19 6.7 Backpatching page 410 makelist(i) creates a new list containing only i, an index into the array of instructions; makelist returns a pointer to the newly created list. merge(p1,p2) concatenates the lists pointed to 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 instructions on the list pointed to by p

20 6.7.1 Backpatching for Boolean Expressions B -> B1 B2 B1.true = B.true B1.false = newlabel() B2.true = B.true B2.false = B.false B.code = B1.code label(b1.false) B2.code B -> B1 M B2 backpatch(b1.falselist, M.instr) B.truelist = merge(b1.truelist, B2.truelist) B.falselist = B2.falselist

21 6.7.1 Backpatching for Boolean Expressions B -> B1 B2 B1.true = B.true B1.false = newlabel() B2.true = B.true B2.false = B.false B.code = B1.code label(b1.false) B2.code B -> B1 M B2 Assume that B1 and B2 generate store code backpatch(b1.falselist, as attributes of the parse M.instr) tree B.truelist = merge(b1.truelist, nodes. B2.truelist) B.falselist = B2.falselist Actions happen at reduction step of rule B -> B1 B2

22 6.7.1 Backpatching for Boolean Expressions Assume that B1 B1.true and B2 = generate B.true code directly, rather B1.false than = storing newlabel() it in the B -> B1 B2 tree. B2.true = B.true B2.false = B.false Actions B.code happen = B1.code at reduction label(b1.false) step of rule B2.code B -> B1 M B2 B -> B1 M B2 backpatch(b1.falselist, M.instr) B.truelist = merge(b1.truelist, B2.truelist) B.falselist = B2.falselist

23 6.7.1 Backpatching for Boolean Expressions B -> B1 && B2 B1.true = newlabel() B1.false = B.false B2.true = B.true B2.false = B.false B.code = B1.code label(b1.true) B2.code B -> B1 && M B2 backpatch(b1.truelist, M.instr) B.truelist = B2.truelist B.falselist = merge(b1.falselist, B2.falselist)

24 6.7.1 Backpatching for Boolean Expressions B ->! B1 B1.true = B.false B1.false = B.true B.code = B1.code B ->! B1 B.truelist = B1.falselist B.falselist = B1.truelist

25 6.7.1 Backpatching for Boolean Expressions B -> ( B1 ) B1.true = B.true B1.false = B.false B.code = B1.code B -> ( B1 ) B.truelist = B1.truelist B.falselist = B1.falselist

26 6.7.1 Backpatching for Boolean Expressions B -> E1 rel E2 B.code = E1.code E2.code gen ('if' E1.addr rel.op E2.addr 'goto B.true') gen('goto' B.false) B -> E1 rel E2 B.truelist = makelist(nextinstr) B.falselist = makelist(nextinstr + 1) gen('if' E1.addr rel.op E2.addr 'goto _') gen('goto _')

27 6.7.1 Backpatching for Boolean Expressions B -> true B.code = gen('goto' B.true) B -> false B.code = gen('goto' B.false) B -> true B.truelist = makelist(nextinstr) gen('goto _') B -> false B.falselist = makelist(nextinstr) gen('goto _')

28 6.7.1 Backpatching for Boolean Expressions M -> ε M.instr = nextinstr

29 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101}

30 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102

31 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102 x > : if x > 200 goto 103: goto truelist = {102} falselist = {103}

32 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102 x > : if x > 200 goto 103: goto truelist = {102} falselist = {103} M M.instr = 104

33 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102 x > : if x > 200 goto 103: goto truelist = {102} falselist = {103} M M.instr = 104 x!= y 104: if x!= y goto 105: goto truelist = {104} falselist = {105}

34 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102 x > : if x > 200 goto 103: goto truelist = {102} falselist = {103} M M.instr = 104 x!= y 104: if x!= y goto 105: goto truelist = {104} falselist = {105} x>200 && x!=y backpatch({102},104) 102: if x > 200 goto : goto truelist = {104} falselist = {103,105}

35 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto truelist = {100} B -> B1 && B2 101: goto falselist = {101} B -> x > 200 && x!= y M M.instr = 102 x > : if x > 200 goto truelist = {102} 103: goto falselist = {103} M M.instr = : if x!= y goto x!= y 105: goto truelist = {104} falselist = {105} x>200 && x!=y backpatch({102},104) 102: if x > 200 goto : goto truelist = {104} falselist = {103,105} backpatch(b1.truelist, M.instr) B.truelist = B2.truelist B.falselist = merge(b1.falselist, B2.falselist)

36 Example 6.24 x < 100 x > 200 && x!= y x < : if x < 100 goto 101: goto truelist = {100} falselist = {101} M M.instr = 102 x > : if x > 200 goto 103: goto truelist = {102} falselist = {103} M M.instr = 104 x!= y 104: if x!= y goto 105: goto truelist = {104} falselist = {105} x>200 && x!=y backpatch({102},104) 102: if x > 200 goto : goto truelist = {104} falselist = {103,105} x < 100 x > 200 && x!= y backpatch({101},102) 100: if x < 100 goto 101: goto 102 truelist = {100,104} falselist = {103,105}

37 How does this play out during parse in array

38 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto truelist = {100} falselist = {101}

39 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto 102: if x > 200 goto 103: goto truelist = {100} falselist = {101} truelist = {102} falselist = {103}

40 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto 102: if x > 200 goto 103: goto 104: if x!= y goto 105: goto truelist = {100} falselist = {101} truelist = {102} falselist = {103} truelist = {104} falselist = {105}

41 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto 102: if x > 200 goto : goto 104: if x!= y goto 105: goto truelist = {100} falselist = {101} truelist = {104} falselist = {103,105}

42 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto : if x > 200 goto : goto 104: if x!= y goto 105: goto truelist = {100,104} falselist = {103,105}

43 Example 6.24 x < 100 x > 200 && x!= y 100: if x < 100 goto 101: goto : if x > 200 goto : goto 104: if x!= y goto 105: goto truelist = {100,102} falselist = {103,105} The remaining open jumps will be backpatched by other instructions, outside this expression

44 6.7.3 Backpatching Flow-of-Control statements S -> if (B) S1 else S2 B.true = newlabel() B.false = newlabel() S1.next = S2.next = S.next S.code = B.code label(b.true) S1.code gen('goto',s.next) label(b.false) S2.code S -> if (B) M1 S1 N else M2 S2 backpatch(b.truelist, M1.instr) backpatch(b.falselist, M2.instr) temp = merge(s1.nextlist, N.nextlist) S.nextlist = merge(temp, S2.nextlist)

45 6.7.3 Backpatching Flow-of-Control statements S -> while (B) S1 begin = newlabel() B.true = newlabel() B.false = S.next() S1.next = begin S.code = label(begin) B.code label(b.true) S1.code gen('goto' begin) S -> while M1 (B) M2 S1 backpatch(s1.nextlist, M1.instr) backpatch(b.truelist, M2.instr) S.nextlist = B.falselist gen('goto' M1.instr)

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

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018 Compilerconstructie najaar 2018 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 7, vrijdag 2 november 2018 + werkcollege

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei shift/reduce conflict with

More information

Intermediate Representa.on

Intermediate Representa.on IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent

More information

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs(dot)nl werkcollege 6, dinsdag 22 oktober 2013 Intermediate Code

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

intermediate-code Generation

intermediate-code Generation intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer

More information

Module 26 Backpatching and Procedures

Module 26 Backpatching and Procedures Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions

More information

Chapter 6 Intermediate Code Generation

Chapter 6 Intermediate Code Generation 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

More information

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

Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching Theory of Compilation 236360 rez Petrank Lecture 7: Intermediate Representation Part 2: ackpatching 1 You are here Compiler Source text txt Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep.

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 24, 2007 Outline Recap Three

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Announcements Weekly team meetings with me: - Doodle poll link in Piazza Wednesday (4/4) will be a workshop Wednesday - Post questions

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting

More information

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7 CS322 Languages and Compiler Design II Spring 2012 Lecture 7 1 USES OF BOOLEAN EXPRESSIONS Used to drive conditional execution of program sections, e.g. IF (a < 17) OR (b = 12) THEN... ELSE...; WHILE NOT

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Module 25 Control Flow statements and Boolean Expressions

Module 25 Control Flow statements and Boolean Expressions Module 25 Control Flow statements and Boolean Expressions In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Announcements Be sure to

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II 1 Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 Advanced Intermediate Code Generation

More information

CMPSC 160 Translation of Programming Languages. Three-Address Code

CMPSC 160 Translation of Programming Languages. Three-Address Code CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory

More information

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

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation. UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2015, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2011, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

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

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1 SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different

More information

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

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures

More information

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

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

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

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures

More information

CS2210: Compiler Construction. Code Generation

CS2210: Compiler Construction. Code Generation Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate

More information

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

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale 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

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Intermediate-Code Genera=on Intermediate representa=ons

More information

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

2 Intermediate Representation. HIR (high level IR) preserves loop structure and. More of a wizardry rather than science. in a set of source languages Acknowledgements The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal Intermediate Code Generation... Front translates a source program into an intermediate representation

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Announcements Grading survey

More information

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

Compiler. --- Intermediate Code Generation. Zhang Zhizheng. Compiler --- Intermediate Code Generation Zhang Zhizheng seu_zzz@seu.edu.cn School of Computer Science and Engineering, Software College Southeast University 2013/12/12 Zhang Zhizheng, Southeast University

More information

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

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic

More information

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015 Boolean Expressions Lecture 31 Sections 6.6, 6.7 Robb T. Koether Hampden-Sydney College Wed, Apr 8, 2015 Robb T. Koether (Hampden-Sydney College) Boolean Expressions Wed, Apr 8, 2015 1 / 22 1 Relational

More information

PRINCIPLES OF COMPILER DESIGN

PRINCIPLES OF COMPILER DESIGN PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Phases of a compiler Target machine code generation Figure 1.6, page 5 of text B1 i = 1 B2 j = 1 B3 t1 = 10 * i t2 = t1 + j t3 = 8

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

CSc 453 Intermediate Code Generation

CSc 453 Intermediate Code Generation CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson Overview Intermediate representations span the gap between the source and target languages: closer to target language;

More information

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009 Lecture 25 Sections 8.4, 8.6 Hampden-Sydney College Fri, Apr 24, 2009 Outline 1 2 3 Definition (Equality and ) The equality operators are the operators == and!=. The relational operators are the operators

More information

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

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ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

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT IV INTERMEDIATE CODE GENERATION UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of

More information

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 Compiler Optimizations Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 2 Local vs. Global Optimizations Local: inside a single basic block Simple forms of common subexpression elimination, dead code elimination,

More information

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 5 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 IR Lowering Use temporary variables for the translation

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 Compiler Optimizations Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 2 Local vs. Global Optimizations Local: inside a single basic block Simple forms of common subexpression elimination, dead code elimination,

More information

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

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

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

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program. COMPILER DESIGN 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the target

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms

More information

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

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum IR Lowering CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 7 March 07 Use temporary variables for the translation Temporary variables in the Low IR store intermediate

More information

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

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Phases of a compiler Target

More information

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM 2:00 PM Friday 8:30 AM 10:30 AM OR request appointment via e-mail

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

Syntax-Directed Translation Part II

Syntax-Directed Translation Part II Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed

More information

VIVA QUESTIONS WITH ANSWERS

VIVA QUESTIONS WITH ANSWERS VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the

More information

Page No 1 (Please look at the next page )

Page No 1 (Please look at the next page ) Salman Bin Abdul Aziz University Collage of Computer Engineering and Sciences Computer Science Department 1433-1434 (2012-2013) First Term CS 4300 Compiler Construction 8 th Level Final Exam 120 Minutes

More information

Dixita Kagathara Page 1

Dixita Kagathara Page 1 2014 Sem-VII Intermediate Code Generation 1) What is intermediate code? Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler

More information

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

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code) Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while

More information

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006 Homework 1 Answers CS 322 Compiler Construction Winter Quarter 2006 Problem 1 m := 0 i := 0 L1: if i

More information

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

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation Comp 204: Computer Systems and Their Implementation Lecture 22: Code Generation and Optimisation 1 Today Code generation Three address code Code optimisation Techniques Classification of optimisations

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

OO software systems are systems of interacting objects.

OO software systems are systems of interacting objects. OO software systems are systems of interacting objects. Objects have Objects properties: these are things that objects know e.g. what you had for breakfast behaviors: these are things objects do e.g. being

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Phases of a compiler Target

More information

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM 2:00 PM Friday 8:30 AM 10:30 AM OR request appointment via e-mail

More information

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

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Translating source program into an intermediate language. Simple CPU Indepent, yet, close in spirit to machine language. Three Address Code (quadruples) Two Address Code, etc.

More information

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES UNIT 3 SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES M.B.Chandak www.mbchandak.com hodcs@rknec.edu CONTROL STRUCTURES 1. If (condition) then Statement 2. If (condition) then Statement 1 else statement

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Intermediate codes are machine independent codes, but they are close to machine instructions The given program in a source language is converted to an equivalent program in

More information

COP5621 Exam 3 - Spring 2005

COP5621 Exam 3 - Spring 2005 COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

Oberon2 Compiler CS335: Compiler Project

Oberon2 Compiler CS335: Compiler Project Oberon2 Compiler CS335: Compiler Project Ashish Gupta (Y8140) Shitikanth (Y8480) Anindya Jyoti Roy (Y8078) Rajeev Rathore (Y8398) Sanjay (Y8448) April 16, 2011 1 / 12 Characterization Source Language(S)

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007 Outline Recap Syntax-directed

More information

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

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Compiler Architecture Source language Scanner (lexical analysis) tokens Parser (syntax

More information

Intermediate-Code Generat ion

Intermediate-Code Generat ion Chapter 6 Intermediate-Code Generat ion In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates

More information

TACi: Three-Address Code Interpreter (version 1.0)

TACi: Three-Address Code Interpreter (version 1.0) TACi: Three-Address Code Interpreter (version 1.0) David Sinclair September 23, 2018 1 Introduction TACi is an interpreter for Three-Address Code, the common intermediate representation (IR) used in compilers.

More information

Syntax Directed Translation

Syntax Directed Translation Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Fall 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [30 points] In this problem you ned to develop a grammar for array variable expressions

More information

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

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Spring 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [25 points] Conser the grammar fragment below. It allows for the declaration of

More information

Semantic Actions and 3-Address Code Generation

Semantic Actions and 3-Address Code Generation Compiler Design 1 Semantic Actions and 3-Address Code Generation Compiler Design 2 Introduction We start with different constructs of the given grammar (laboratory assignment-5) and discuss semantic actions

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei BUILD A COMPILER! Learning

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours: CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM 2:00 PM Friday 8:30 AM 10:30 AM OR request appointment via e-mail

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

More information

ECS 142 Project: Code generation hints (2)

ECS 142 Project: Code generation hints (2) ECS 142 Project: Code generation hints (2) Winter 2011 This document describes the nature of final code generation for your project. I have made a number of assumptions here, mostly in terms of availability

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Phases of a compiler Syntactic structure Figure 1.6, page 5 of text Recap Lexical analysis: LEX/FLEX (regex -> lexer) Syntactic analysis:

More information

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

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 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

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 CSCI 565 - Compiler Design Spring 26 Midterm Exam March, 26 at : PM in class (KAP 47) Duration: 2h 3 min. Please label

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information