cse443 compilers
play

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - PowerPoint PPT Presentation

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 Intermediate Representation (IR):


  1. 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

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

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

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

  5. 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").

  6. 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

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

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

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

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

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

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

  13. 6.7.1 Backpatching for Boolean Expressions M -> 𝜁 M.instr = nextinstr

  14. Example 6.24 x < 100 || x > 200 && x != y 100: if x < 100 goto ___ truelist = {100} x < 100 101: goto ___ falselist = {101}

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

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

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

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

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

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

  21. Example 6.24 x < 100 || x > 200 && x != y 100: if x < 100 goto ___ truelist = {100} 101: goto ___ falselist = {101}

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

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

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

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

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

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

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

  29. WW example Show the intermediate code that should result from processing the following statement: while ( x < 25 ) { x := x + 1; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend