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