CSCI Compiler Design

Similar documents
b. How many ternary words of length 23 with eight 0 s, nine 1 s and six 2 s?

COMP108 Algorithmic Foundations

Register Allocation. How to assign variables to finitely many registers? What to do when it can t be done? How to do so efficiently?

Present state Next state Q + M N

Math 61 : Discrete Structures Final Exam Instructor: Ciprian Manolescu. You have 180 minutes.

A Simple Code Generator. Code generation Algorithm. Register and Address Descriptors. Example 3/31/2008. Code Generation

Register Allocation. Register Allocation. Principle Phases. Principle Phases. Example: Build. Spills 11/14/2012

(2) If we multiplied a row of B by λ, then the value is also multiplied by λ(here lambda could be 0). namely

1 Introduction to Modulo 7 Arithmetic

0.1. Exercise 1: the distances between four points in a graph

Module graph.py. 1 Introduction. 2 Graph basics. 3 Module graph.py. 3.1 Objects. CS 231 Naomi Nishimura

Using the Printable Sticker Function. Using the Edit Screen. Computer. Tablet. ScanNCutCanvas

Integration Continued. Integration by Parts Solving Definite Integrals: Area Under a Curve Improper Integrals

Constructive Geometric Constraint Solving

Exam 1 Solution. CS 542 Advanced Data Structures and Algorithms 2/14/2013

ECE COMBINATIONAL BUILDING BLOCKS - INVEST 13 DECODERS AND ENCODERS

Planar Upward Drawings

CS553 Lecture Register Allocation 1

# 1 ' 10 ' 100. Decimal point = 4 hundred. = 6 tens (or sixty) = 5 ones (or five) = 2 tenths. = 7 hundredths.

CS 241 Analysis of Algorithms

CSE 373: More on graphs; DFS and BFS. Michael Lee Wednesday, Feb 14, 2018

CSE 373: AVL trees. Warmup: Warmup. Interlude: Exploring the balance invariant. AVL Trees: Invariants. AVL tree invariants review

An undirected graph G = (V, E) V a set of vertices E a set of unordered edges (v,w) where v, w in V

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs

12/3/12. Outline. Part 10. Graphs. Circuits. Euler paths/circuits. Euler s bridge problem (Bridges of Konigsberg Problem)

5/9/13. Part 10. Graphs. Outline. Circuits. Introduction Terminology Implementing Graphs

CSE303 - Introduction to the Theory of Computing Sample Solutions for Exercises on Finite Automata

CIVL 8/ D Boundary Value Problems - Rectangular Elements 1/7

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

QUESTIONS BEGIN HERE!

Module 2 Motion Instructions

CS 461, Lecture 17. Today s Outline. Example Run

Why the Junction Tree Algorithm? The Junction Tree Algorithm. Clique Potential Representation. Overview. Chris Williams 1.

HIGHER ORDER DIFFERENTIAL EQUATIONS

Outline. 1 Introduction. 2 Min-Cost Spanning Trees. 4 Example

The University of Sydney MATH2969/2069. Graph Theory Tutorial 5 (Week 12) Solutions 2008

Formal Concept Analysis

Problem solving by search

(a) v 1. v a. v i. v s. (b)

Paths. Connectivity. Euler and Hamilton Paths. Planar graphs.

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology!

Graph Isomorphism. Graphs - II. Cayley s Formula. Planar Graphs. Outline. Is K 5 planar? The number of labeled trees on n nodes is n n-2

Algorithmic and NP-Completeness Aspects of a Total Lict Domination Number of a Graph

EE1000 Project 4 Digital Volt Meter

CSC Design and Analysis of Algorithms. Example: Change-Making Problem

16.unified Introduction to Computers and Programming. SOLUTIONS to Examination 4/30/04 9:05am - 10:00am

The Procedure Abstraction Part II: Symbol Tables and Activation Records

Instructions for Section 1

Seven-Segment Display Driver

Minimum Spanning Trees

COMPLEXITY OF COUNTING PLANAR TILINGS BY TWO BARS

S i m p l i f y i n g A l g e b r a SIMPLIFYING ALGEBRA.

Announcements. Programming Project 4 due Saturday, August 18 at 11:30AM

Graphs. Graphs. Graphs: Basic Terminology. Directed Graphs. Dr Papalaskari 1

CS September 2018

CSE 373. Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9. slides created by Marty Stepp

Garnir Polynomial and their Properties

INTEGRALS. Chapter 7. d dx. 7.1 Overview Let d dx F (x) = f (x). Then, we write f ( x)

Chapter 16. 1) is a particular point on the graph of the function. 1. y, where x y 1

135-91G00N ONE-WAY CUSTOMER UNIT FOR VAT 30GX AND Easy-aire 10GX

FSA. CmSc 365 Theory of Computation. Finite State Automata and Regular Expressions (Chapter 2, Section 2.3) ALPHABET operations: U, concatenation, *

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management

Last time: introduced our first computational model the DFA.

Graphs. CSC 1300 Discrete Structures Villanova University. Villanova CSC Dr Papalaskari

Outline. Circuits. Euler paths/circuits 4/25/12. Part 10. Graphs. Euler s bridge problem (Bridges of Konigsberg Problem)

CS200: Graphs. Graphs. Directed Graphs. Graphs/Networks Around Us. What can this represent? Sometimes we want to represent directionality:

12. Traffic engineering

Weighted graphs -- reminder. Data Structures LECTURE 15. Shortest paths algorithms. Example: weighted graph. Two basic properties of shortest paths

Announcements. Not graphs. These are Graphs. Applications of Graphs. Graph Definitions. Graphs & Graph Algorithms. A6 released today: Risk

Decimals DECIMALS.

DUET WITH DIAMONDS COLOR SHIFTING BRACELET By Leslie Rogalski

CS61B Lecture #33. Administrivia: Autograder will run this evening. Today s Readings: Graph Structures: DSIJ, Chapter 12

Designing A Concrete Arch Bridge

Trees as operads. Lecture A formalism of trees

Logarithms. Secondary Mathematics 3 Page 164 Jordan School District

TOPIC 5: INTEGRATION

Fundamental Algorithms for System Modeling, Analysis, and Optimization

Linked-List Implementation. Linked-lists for two sets. Multiple Operations. UNION Implementation. An Application of Disjoint-Set 1/9/2014

Greedy Algorithms, Activity Selection, Minimum Spanning Trees Scribes: Logan Short (2015), Virginia Date: May 18, 2016

CS553 Lecture Register Allocation I 3

XML and Databases. Outline. Recall: Top-Down Evaluation of Simple Paths. Recall: Top-Down Evaluation of Simple Paths. Sebastian Maneth NICTA and UNSW

Lecture 11 Waves in Periodic Potentials Today: Questions you should be able to address after today s lecture:

Limits Indeterminate Forms and L Hospital s Rule

Nefertiti. Echoes of. Regal components evoke visions of the past MULTIPLE STITCHES. designed by Helena Tang-Lim

Chem 107: Inorganic Chemistry (40720)

MASSACHUSETTS INSTITUTE OF TECHNOLOGY HAYSTACK OBSERVATORY WESTFORD, MASSACHUSETTS

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Ch 1.2: Solutions of Some Differential Equations

A 4-state solution to the Firing Squad Synchronization Problem based on hybrid rule 60 and 102 cellular automata

UNCORRECTED SAMPLE PAGES 4-1. Naming fractions KEY IDEAS. 1 Each shape represents ONE whole. a i ii. b i ii

Solutions to Homework 5

a b c cat CAT A B C Aa Bb Cc cat cat Lesson 1 (Part 1) Verbal lesson: Capital Letters Make The Same Sound Lesson 1 (Part 1) continued...

QUESTIONS BEGIN HERE!

Multi-Section Coupled Line Couplers

Numbering Boundary Nodes

d e c b a d c b a d e c b a a c a d c c e b

Section 3: Antiderivatives of Formulas

NP-Completeness. CS3230 (Algorithm) Traveling Salesperson Problem. What s the Big Deal? Given a Problem. What s the Big Deal? What s the Big Deal?

Graph Contraction and Connectivity

Transcription:

CSCI 565 - Compilr Dsign Spring 2013 Sond Tst - Solution My 1, 2013 t 3.30 PM in Room RTH 115 Durtion: 2h 30 min. Pls ll ll pgs you turn in with your nm nd studnt numr. Nm: Numr: Grd: Prolm 1 [20 points]: Prolm 2 [35 points]: Prolm 3 [25 points]: Prolm 4 [20 points]: Totl: Instrutions: 1. This is losd ook Exm. 2. Th tst ooklt ontins our (5) pgs inluding this ovr pg. 3. Clrly ll ll pgs you turn in with your nm nd studnt ID numr. 4. Appnd, y stpling or tthing your nswr pgs. 5. Us lk or lu pn (not pnil). 1 o 9

Prolm 1. Run-Tim Environmnts nd Dt Lyout [20 points] Considr th ollowing PASCAL sour progrm shown low. 01: progrm min(input, output); 02: vr trk[0..3]: intgr; 03: vr id: intgr; 04: untion P1(: intgr) : intgr; 05: vr nt : intgr; 06: gin (* P1 *) 07: id := ; 08: i( > 0) 09: nt := P1(-1); 10: ls 11: trk[id] = ; 12: P1 := nt; 13: nd; 14: produr P2(: intgr); 15: gin (* P2 *) 16: id := 0; 17: P1() 18: nd; 19: produr P3(: intgr); 20: gin (* P3 *) 21: id := 3; 22: P2() 23: nd; 24: produr P4(d: intgr); 25: gin (* P4 *) 26: id := 4; 27: P3(d); 28: nd; 29: gin (* min *) 30: P4(1) 31: nd. Qustions:. [05 points] Show th ll tr or this prtiulr progrm nd disuss or this prtiulr od i th Ativtion Rords (ARs) or h o th produrs P1 through P4 n llotd sttilly or not. Explin why or why not.. [10 points] Assuming you r using stk to sv th tivtion rords o ll th untion's invotions, drw th ontnts o th stk whn th ontrol rhs th lin in th sour od lld s 11+ i.., or th progrm xuts th rturn sttmnt orrsponding to th invotion ll t this lin. For th purpos o inditing th rturn ddrsss inlud th dsigntion s N+ or ll instrution on lin N. For instn, thn produr P2 invoks th produr P1 in lin 17, th orrsponding rturn ddrss n lld s 17+ to indit tht th rturn ddrss should immditly tr lin 17. Indit th ontnts o th glol nd lol vrils to h produr s wll s th links in th AR. Us th AR orgniztion dsrid in lss inditing th lotion o h produr s lol vril in th orrsponding AR.. [05 points] For this prtiulr od do you nd to rly on th Ass s on th AR to ss nonlol vrils? Would thr sustntil dvntg to th us o th Disply mhnism? Nm: 2 o 9

Answrs:. [05 points] Givn tht thr r rursiv untion lls (in this prtiulr s P1), w nnot in gnrl llot th tivtion rords o ll ths untions in glol rgion o th storg. Howvr, in this prtiulr s w osrv tht th rursiv lls r onind to vry smll portion o th ll grph s P1 only lls itsl. As suh w ould in prinipl llot ll th rmindr produrs sttilly nd us simplr stk or th tivtions o P1.. [10 points] Th igur low dpits oth th ll tr nd th stk onigurtion whn th xution rhs th lin 11+ in th sour progrm. min(input,output) P4(1) P3(1) min trk[0]: 0 trk[1]: und trk[2]: und trk[3]: und id: 0 Rt. Addrss: ----- P2 P1 Rt. Addrss: 22+ Rt. Vlu: -- Arg 1: = 1 ntr: 0 P2(1) P1(1) P4 Rt. Addrss: 30+ Rt. Vlu: -- Arg 1: d = 1 P1 Rt. Addrss: 17+ Rt. Vlu: -- Arg 1: = 1 ntr: 0 P3 SP P1(0) Rt. Addrss: 27+ Rt. Addrss: 9+ Rt. Vlu: -- Rt. Vlu: -- Arg 1: = 1 Arg 1: = 1 () Cll Tr () Stk Lyout. [05 points] Givn tht ths r only sss to lol vrils within h produr or glol vrils (whih r llotd in spii stti dt stion) nd thr r no sss to othr produr's lol vrils, thr is no nd to us th Ass s (Ass) in th Ativtion Rords (AR) nd hn no nd to us nd mintin th disply ss mhnism. Nm: 3 o 9

Prolm 2. Control-Flow Anlysis [35 points] Considr th thr-ddrss od low or produr with input/output rgumnts pssd on th Ativtion Rord on th stk. 01: t0 = $p - 4 02: t0 = *t0 03: t1 = t0 04: t0 = $p - 8 05: t0 = *t0 06: t2 = t0 07: t3 = 0 08: t4 = 0 09: t5 = t1 10: L0: i t2 = 16 goto L2 11: goto L1 12: L1: i t1 <= 0 goto L2 13: t2 = t2 + t1 14: t3 = 4 * t2 15: t4 = t3-1 16: goto L4 17: L4: t6 = 0 18: t0 = $p - 12 19: t0 = *t0 20: i t0 > 0 goto L3 21: t5 = t5 + 1 22: *t5 = 0 23: goto L5 24: L3: t6 = 1 25: L5: goto L0 26: L2: t0 = $p - 16 27: *t0 = t4 28: t0 = $p - 20 29: *t0 = t3 30: rturn Qustions: For this od dtrmin th ollowing:. [10 points] Bsi loks nd th orrsponding ontrol-low grph (CFG) inditing or h si lok th orrsponding lin numrs o th od ov.. [10 points] Domintor tr nd th nturl loops in this od long with th orrsponding k dg(s).. [15 points] Loop invrints instrutions nd indution vrils or th loops idntiid in (). Inlud in your nswr lmnts rom Dt-Flow Anlysis (suh s rhing dinition or liv vril nlysis) tht ould hlp ompilr to idntiy ths opportunitis, i.., xplin how ompilr ould mk us o th inormtion unovrd y thos nlyss to dtrmin tht ths spii ss o instrutions r loop invrint nd tht ths spii vrils r indution vrils. Nm: 4 o 9

Answrs:. [10 points] S igur low on th lt.. [10 points] S igur low on th right. ENTRY 01: t0 = $p 4 02: t0 = *t0 03: t1 = t0 04: t0 = $p 4 05: t0 = *t0 06: t2 = t0 07: t3 = 0 08: t4 = 0 09: t5 = t1 10: L1: i t2 = 16 goto L2 1 2 11: goto L1 3 12: L1: i t1 <= 0 goto L2 4 13: t2 = t2 + t1 14: t3 = 4 * t2 15: t4 = t3-1 16: goto L4 BB5 5 10 BB6 17: L4: t6 = 0 18: t0 = *p - 12 19: t0 = *t0 20: i t0 > 0 goto L3 6 21: L4: t5 = t5 + 1 22: *t5 = 0 23: goto L5 BB7 24: L3: t6 = 1 BB8 7 8 9 0 26: L2: t0 = $p - 16 27: *t0 = t4 28: t0 = $p - 20 29: *t0 = t5 30: rturn 25: L5: goto L0 BB9 k dg = { (9, 2) }! loop = { 2, 3, 4, 5, 6, 7, 8, 9 } EXIT ) Control-Flow Grph (CFG) ) Domintor Tr, Bk-dg nd Nturl Loop. [15 points] Thr r two potntil ss o loop invrint od in lins 17 nd 24 rgrding vril t6 s in ths sttmnts th vlus ssignd to t6 r onstnt. Thr r howvr two ssignmnt in two dirnt si loks nithr o whih post-domints th xit o th loop. As suh w nnot mov th ssignmnt "t6 = 0" (in th lok tht domints th sond ssignmnt) to th hdr o th loop. Noti tht in t ths two ssignmnts r in t "dd" instrutions, s t6 is nvr usd in this od. As to indution vrils w osrv th squn in instrutions 13, 14 nd 15. In instrutions 13 "t2 = t2 + t1" th us o t1 is in t loop invrint - th vlu o t1 dos not hng within th loop. This mns tht rom th prsptiv o th loop, t1 is onstnt nd thus t2 is si indution vril. Susquntly, t3 nd thus t4 will drivd indutions vrils with oiint tupls (t2, 4, 0) nd (t2, 4, -1) rsptivly. Nm: 5 o 9

Prolm 3. Rgistr Allotion [25 points] Considr th ollowing ontrol low grph (CFG) rgmnt low whr it is ssumd ll slr vrils through r dd on produr xit. Entry 1: d = 0! 2: =...! 3: =...! 4: i ( > ) 5: = 1! 6: = d +! 7: goto 8: = 0! 9: = + 1! 10: d =...! 11: i ( > 4) 12: = + 1! 13: = + 1! 14: d = + 1! 15: i ( > 0) BB5 16: print(,, d) Exit Qustions:. [10 points] Dtrmin th liv rngs nd th orrsponding ws or th vrils,,, d, nd. Show th ws or th vrils dind in trms o th lin numrs dpitd nxt to h instrution. Explin th wy you omin th d-us hins or th vril '' to orm th w or tht vril. You do not nd to s spii or th othr vrils, so prsnt only th orrsponding ws.. [05 points] Driv th intrrn grphs (or tl) or ths vrils using th rind intrrn dsrid in lss (this is th "sond" mor sophistitd dinition tht tks into ount th rd nd writ o h spii vril/tmporry). Explin in dtil th intrrn (or lk thro) twn th ws orrsponding to th vrils '' nd ''.. [10 points] Cn you olor th rsulting intrrn grphs with 3 olors? Why or why not? Us th grph-oloring lgorithm dsrid in lss or oloring this grph nd ssign spii rgistrs to th vrious ws. Dsri whr spill od would hv to inludd nd why so tht you ould olor th grph with 3 olors using th sm lgorithm (not y mnul insption). Nm: 6 o 9

Answrs:. [10 points] Th grph low illustrts th vrious liv rngs or th vrils '', '', 'd' nd ''. On th right-hnd-sid w hv th liv rngs or h vril dind in trms o th lin numrs nd th orrsponding intrrn grph or th ws tht n gnrtd rom ths rngs. For vril '' thr r som rngs, nmly: rng #1 = {2,3,4,8,9,10,11,16} s this orrsponds to dinition in lin {2} tht propgts to th mny uss in lins {4}, {9} nd {16}; rng #2 - {13, 14, 15, 16} s this orrsponds to th dinition in lin {13} tht propgts dirtly to lin {16} nd thn rng #3 = {13, 14, 15, 8, 9} s this orrsponds to th dinition in lin {13} tht propgts long th k dg o th loop to th uss in 9 (nd vntully to 16). 5: = 1! 6: = d +! 7: goto Entry 1: d = 0! 2: =...! 3: =...! 4: i ( > ) 8: = 0! 9: = + 1! 10: d =...! 11: i ( > 4) Vr!!! d!! D-Us Chins {2, 3, 4, 8, 9, 10, 11, 16} ; {13, 14, 15, 16} ; {13, 14, 15, 8, 9 }! {3, 4, 5, 6, 7, 16} ; {9, 10, 11, 16} ; {9, 10, 11, 12, 13, 14, 15, 16}! {12, 13, 14, 15}! {1, 2, 3, 4, 5, 6, 7, 16} ; {10, 11, 16} ; 14, 15, 16}! {8, 9, 10, 11, 12}! {5, 6} DU hins or vrils '' through '' 12: = + 1! 13: = + 1! 14: d = + 1! 15: i ( > 0) d BB5 16: print(,, d) Intrrn grph Exit Liv Rng Ws or Vrils '', '', 'd' nd ''. [05 points] This is shown ov on th right-hnd sid (lowr sid). Noti tht th w ssoitd with vril. [10 points] No, w nnot olor this grph with 3 olors s th sust o nods in this grph orrsponding to th vrils {,,,d} orm 4-liqu. As suh w rmov th nod 'd' rom th intrrn grph, s this is th nod with th lrgst dgr. W thn us th huristi grph oloring lgorithm dsrid in lss y pushing onto stk th nods in th squn s shown low nd prod to olor thm s thy r ing "poppd" out o th stk. Th ssignmnt o olor is s shown to th right. () Originl Intrrn grph d () Pushing ordr into th stk:,,,, nd oloring during 'pop' oprtions using 'rd', 'grn' nd 'lu'. d d () Rmoving nod with th highst dgr Nm: 7 o 9

Prolm 4: Itrtiv Dt-Flow Anlysis [20 points] Thr r svrl ompilr psss tht rly on th inormtion out whih vrils r dind nd usd. Rgistr llotion is suh s whos inormtion n drivd dirtly rom liv vril nlysis or y d-us (dind-usd) hins. In this prolm you r skd to dsri th d-us (DU) dt-low nlysis in dtil nd disuss its us to driv inormtion or progrm nhning trnsormtions. Qustions:. [10 points] Formult th d-us itrtiv dt-low nlysis prolm inditing th strutur o th ltti, th mt oprtor; th trnsr untions nd th initiliztion vlus or th nods in th progrm ssumd to th nods in th CFG orrsponding to th progrm s si loks. Justiy th hoi o initil vlu in trms o prision nd sty o th initil nd th rsulting inl solution it lds to.. [10 points] Using th ormultion you hv dvlopd in. ov pply it to th CFG strutur dpitd low whr you n ssum tht th initil vlus or th dt-low strtions r mpty nd tht thy do not hng ovr th vrious itrtions o your lgorithm. Morovr, ssum thr r no dinitions to th x vril othr thn th ons dpitd hr. Show th intrmdit vlus o th IN nd OUT strtions or h si lok. Nm: 8 o 9

Answrs:. [15 points] W us th sm ormultion s dsrid in lss using th ommon strtions o In nd Out to rprsnt th dinitions tht rh th input nd th output o h si lok in th progrm s CFG. As suh th ltti will onsist o th st o dinitions or h vril nd it losd undr th sust rltionship. Th top lmnt o th ltti (T) is th st o ll dinitions nd th ottom lmnt ( ), nd sst, is th mpty st. Th Gn st or h si lok is th st o dinitions tht r downwrd xposd or downwrd vill nd orrspond to th dinition st in th urrnt si lok or vrils tht r not ltr rdind in th sm si lok. Th Kill st or h si lok orrsponds to th dinition whos vrils r dind in th si lok. Not tht th Kill st dos inlud dinition in othr si loks s wll s th dinition in th si lok to whih it orrsponds. Th mt oprtor is in this s th st union nd th trnsr untion or th si lok is dind y th qution In(n) = U Out(p) or ll prdssors nods p o n Out(n) = Gn(n) U (In(n Kill(n)) Th ltti ing th powr-st o th dinition in th progrm is o init hight (s wll s width) nd th st-union is ommuttiv, distriutiv nd ssoitiv. This mns tht not only dos th itrtiv work-list lgorithm dos onvrg, ut tht lso th MOP is th sm solution s th MFP omputd y this lgorithm. As to th initiliztion, ll sts In nd Out should initilizd to mpty sts. This is th sst ssumption to th rhing dinitions whr w lim tht no dinition rhs ny point o th progrm.. [10 points] Th igur low dpits th vrious vlus or th itrtiv dt-low nlysis lgorithm or this DU-hin prolm or th inputs CFG. d1 gn =! kill = { d1, d2, d3, d4 } d1 In =! Out = d2 d3 d4 d2 d3 d4 gn = { d2 }! kill = { d1, d2, d3, d4 } gn = { d4 }! kill = { d1, d2, d3, d4 } In =! Out = { d2 } In = { d1, d2, d4 }! Out = { d4 } gn =! kill = In = { d2, d4 }! Out = { d2, d4 } Itrtion 0 Itrtion 1 Itrtion 2 Itrtion 3 BB In Out In Out In Out In Out 1 2 { d2 } { d2 } { d2 } 3 { d4 } { d4 } { d1, d2, d4 } { d4 } 4 { d2, d4 } { d2, d4 } { d2, d4 } { d2, d4 } { d2, d4 } { d2, d4 } Ordr =,, Nm: 9 o 9