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

cse443 compilers
SMART_READER_LITE
LIVE PREVIEW

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):


slide-1
SLIDE 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

slide-2
SLIDE 2

Phases of a compiler

Figure 1.6, page 5 of text

Intermediate Representation (IR): specification and generation

slide-3
SLIDE 3

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

B1 B2 B1.false

true false true false

B.true B.false

slide-4
SLIDE 4

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

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

slide-6
SLIDE 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

  • f the instructions on the list pointed to by p
slide-7
SLIDE 7

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

slide-8
SLIDE 8

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)

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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 _')

slide-12
SLIDE 12

6.7.1 Backpatching for Boolean Expressions

B -> true B -> false B.code = gen('goto' B.true) B.code = gen('goto' B.false) B -> true B -> false B.truelist = makelist(nextinstr) gen('goto _') B.truelist = makelist(nextinstr) gen('goto _')

slide-13
SLIDE 13

6.7.1 Backpatching for Boolean Expressions

M -> 𝜁 M.instr = nextinstr

slide-14
SLIDE 14

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101}

slide-15
SLIDE 15

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102

slide-16
SLIDE 16

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102 x > 200 102: if x > 200 goto ___ 103: goto ___ truelist = {102} falselist = {103}

slide-17
SLIDE 17

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102 x > 200 102: if x > 200 goto ___ 103: goto ___ truelist = {102} falselist = {103} M M.instr = 104

slide-18
SLIDE 18

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102 x > 200 102: 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}

slide-19
SLIDE 19

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102 x > 200 102: 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 104 103: goto ___ truelist = {104} falselist = {103,105}

slide-20
SLIDE 20

Example 6.24 x < 100 || x > 200 && x != y

x < 100 100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101} M M.instr = 102 x > 200 102: 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 104 103: 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}

slide-21
SLIDE 21

Example 6.24 x < 100 || x > 200 && x != y

100: if x < 100 goto ___ 101: goto ___ truelist = {100} falselist = {101}

slide-22
SLIDE 22

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}

slide-23
SLIDE 23

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}

slide-24
SLIDE 24

Example 6.24 x < 100 || x > 200 && x != y

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

slide-25
SLIDE 25

Example 6.24 x < 100 || x > 200 && x != y

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

slide-26
SLIDE 26

Example 6.24 x < 100 || x > 200 && x != y

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

The remaining open jumps will be backpatched by

  • ther instructions, outside

this expression

slide-27
SLIDE 27

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)

slide-28
SLIDE 28

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)

slide-29
SLIDE 29

WW example

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