1 February 28, 2016 1 February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Compiler Construction Sohail Aslam Lecture Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
國立台灣大學 資訊工程學系 薛智文 98 Spring Intermediate Code Generation (textbook ch# 6.1–6.4, 6.5.1–6.5.3,
Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Intermediate Code Generation CS308 Compiler Theory1.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
1 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Code Generation How to produce intermediate or target code.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chap. 4, Intermediate Code Generation
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
February 27, February 27, 2016February 27, 2016February 27, 2016 Azusa, CA Sheldon X. Liang Ph. D. Software Engineering in CS at APU Azusa Pacific.
Three Address Code Generation of Control Statements continued..
1 Chapter 6: Semantic Analysis. 2 Semantic Analyzer ==> Semantic Structure - What is the program supposed to do? - Semantics analysis can be done during.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
March 19, March 19, 2016March 19, 2016March 19, 2016 Azusa, CA Sheldon X. Liang Ph. D. Software Engineering in CS at APU Azusa Pacific University,
Translation Scheme for Addressing Array Elements
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate Code Generation Part II
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
Three Address Code Generation – Backpatching
THREE ADDRESS CODE GENERATION
Intermediate Code Generation Part II
Intermediate Code Generation
Compiler Construction
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Compiler Construction
Presentation transcript:

1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

2 Advanced Intermediate Code Generation Techniques Reusing temporary names Addressing array elements Translating logical and relational expressions Translating short-circuit Boolean expressions and flow-of-control statements with backpatching lists Translating procedure calls February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

3 February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Keep in mind following questions Addressing –R–Reusing temporary names –A–Addressing array elements –T–Translating L & R exp Backpathcing –S–Short-circuit evaluation –S–Short-circuit backpatching –F–Flow of control backpatching Translating procedural call –F–Formal / local parameters –P–Passing via call-by-value –P–Passing via call-by-reference

4 Reusing Temporary Names Evaluate E 1 into t1 Evaluate E 2 into t2 t3 := t1 + t2 E1 + E2E1 + E2 Modify newtemp() to use a “stack”: Keep a counter c, initialized to 0 newtemp() increments c and returns temporary $ c Decrement counter on each use of a $ i in a three-address statement If t1 no longer used, can reuse t1 instead of using new temp t3 generate February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

5 Reusing Temporary Names (cont’d) x := a * b + c * d - e * f $0 := a * b $1 := c * d $0 := $0 + $1 $1 := e * f $0 := $0 - $1 x := $ cStatement February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

6 Addressing Array Elements: One- Dimensional Arrays February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

7 Addressing Array Elements: Multi- Dimensional Arrays A : array [1..2,1..3] of integer; low 1 = 1, low 2 = 1, n 1 = 2, n 2 = 3, w = 4 February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

8 Addressing Array Elements: Multi- Dimensional Arrays February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

9 Addressing Array Elements: Grammar S  L := E E  E + E | ( E ) | L L  Elist ] | id Elist  Elist, E | id [ E Synthesized attributes: E.placename of temp holding value of E Elist.arrayarray name Elist.placename of temp holding index value Elist.ndimnumber of array dimensions L.placelvalue (=name of temp) L.offsetindex into array (=name of temp) null indicates non-array simple id February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

10 Addressing Array Elements S  L := E{ if L.offset = null then emit(L.place ‘:=’ E.place) else emit(L.place[L.offset] ‘:=’ E.place) } E  E 1 + E 2 { E.place := newtemp(); emit(E.place ‘:=’ E 1.place ‘+’ E 2.place) } E  ( E 1 ){ E.place := E 1.place } E  L{ if L.offset = null then E.place := L.place else E.place := newtemp(); emit(E.place ‘:=’ L.place[L.offset] } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

11 Addressing Array Elements L  Elist ]{ L.place := newtemp(); L.offset := newtemp(); emit(L.place ‘:=’ c(Elist.array); emit(L.offset ‘:=’ Elist.place ‘*’ width(Elist.array)) } L  id { L.place := id.place; L.offset := null } Elist  Elist 1, E { t := newtemp(); m := Elist 1.ndim + 1; emit(t ‘:=’ Elist 1.place ‘*’ limit(Elist 1.array, m)); emit(t ‘:=’ t ‘+’ E.place); Elist.array := Elist 1.array; Elist.place := t; Elist.ndim := m } Elist  id [ E{ Elist.array := id.place; Elist.place := E.place; Elist.ndim := 1 } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

12 Translating Logical and Relational Expressions a or b and not c t1 := not c t2 := b and t1 t3 := a or t2 if a < b goto L1 t1 := 0 goto L2 L1: t1 := 1 L2: a < b February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

13 Translating Short-Circuit Expressions Using Backpatching E  E or M E | E and M E | not E | ( E ) | id relop id | true | false M   Synthesized attributes: E.codethree-address code E.truelistbackpatch list for jumps on true E.falselistbackpatch list for jumps on false M.quadlocation of current three-address quad February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

14 Backpatch Operations with Lists makelist(i) creates a new list containing three- address location i, returns a pointer to the list merge(p 1, p 2 ) concatenates lists pointed to by p 1 and p 2, returns a pointer to the concatenates list backpatch(p, i) inserts i as the target label for each of the statements in the list pointed to by p February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

15 Backpatching with Lists: Example a < b or c < d and e < f 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _ backpatch February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Short-circuit evaluation

16 Backpatching with Lists: Translation Scheme M   { M.quad := nextquad() } E  E 1 or M E 2 { backpatch(E 1.falselist, M.quad); E.truelist := merge(E 1.truelist, E 2.truelist); E.falselist := E 2.falselist } E  E 1 and M E 2 { backpatch(E 1.truelist, M.quad); E.truelist := E 2.truelist; E.falselist := merge(E 1.falselist, E 2.falselist); } E  not E 1 { E.truelist := E 1.falselist; E.falselist := E 1.truelist } E  ( E 1 ){ E.truelist := E 1.truelist; E.falselist := E 1.falselist } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

17 Backpatching with Lists: Translation Scheme (cont’d) E  id 1 relop id 2 { E.truelist := makelist(nextquad()); E.falselist := makelist(nextquad() + 1); emit(‘ if ’ id 1.place relop.op id 2.place ‘ goto _ ’); emit(‘ goto _ ’) } E  true{ E.truelist := makelist(nextquad()); E.falselist := nil; emit(‘ goto _ ’) } E  false{ E.falselist := makelist(nextquad()); E.truelist := nil; emit(‘ goto _ ’) } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

18 Flow-of-Control Statements and Backpatching: Grammar February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

19 Flow-of-Control Statements and Backpatching S  A{ S.nextlist := nil } S  begin L end { S.nextlist := L.nextlist } S  if E then M S 1 { backpatch(E.truelist, M.quad); S.nextlist := merge(E.falselist, S 1.nextlist) } L  L 1 ; M S{ backpatch(L 1.nextlist, M.quad); L.nextlist := S.nextlist; } L  S{ L.nextlist := S.nextlist; } M   { M.quad := nextquad() } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

20 Flow-of-Control Statements and Backpatching (cont’d) S  if E then M 1 S 1 N else M 2 S 2 { backpatch(E.truelist, M 1.quad); backpatch(E.falselist, M 2.quad); S.nextlist := merge(S 1.nextlist, merge(N.nextlist, S 2.nextlist)) } S  while M 1 E do M 2 S 1 { backpatch(S 1,nextlist, M 1.quad); backpatch(E.truelist, M 2.quad); S.nextlist := E.falselist; emit(‘ goto _ ’) } N   { N.nextlist := makelist(nextquad()); emit(‘ goto _ ’) } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

21 Translating Procedure Calls S  call id ( Elist ) Elist  Elist, E | E foo(a+1, b, 7) t1 := a + 1 t2 := b t3 := 7 param t1 param t2 param t3 call foo 3 February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Call by value Passing parameters via call-by-value Formal/local parameters

22 Translating Procedure Calls S  call id ( Elist ) Elist  Elist, E | E Swap (a, b) t1 := &a t2 := &b *param t1 *param t2 call swap 2 February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Call by referenc e Passing parameters via call-by-reference Formal/local parameters

23 Translating Procedure Calls S  call id ( Elist ){ for each item p on queue do emit(‘ param ’ p); emit(‘ call ’ id.place |queue|) } Elist  Elist, E{ append E.place to the end of queue } Elist  E{ initialize queue to contain only E.place } February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

24 February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Addressing –R–Reusing temporary names –A–Addressing array elements –T–Translating L & R exp Backpathcing –S–Short-circuit evaluation –S–Short-circuit backpatching –F–Flow of control backpatching Translating procedural call –F–Formal / local parameters –P–Passing via call-by-value –P–Passing via call-by-reference

25 Thank you very much! Questions? February 28, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction