Backpatching: - PowerPoint PPT Presentation

About This Presentation
Title:

Backpatching:

Description:

M.quad: the number for current instruction. Makelist(quad): create a list. E- E1 or M E2 { backpatch(E1. ... S - if E then M S1 { backpatch(E.truelist, M.quad) ... – PowerPoint PPT presentation

Number of Views:1129
Avg rating:3.0/5.0
Slides: 10
Provided by: wfa
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Backpatching:


1
  • Backpatching
  • The syntax directed definition we discussed
    before can be implemented in two or more passes
    (we have both synthesized attributes and
    inheritent attributes).
  • Build the tree first
  • Walk the tree in the depth first order.
  • The main difficulty with code generation in one
    pass is that we may not know the target of a
    branch when we generate code for flow-of control
    statements
  • Backpatching is the technique to get around this
    problem.
  • Generate branch instructions with empty targets
  • When the target is known, fill in the label of
    the branch instructions (backpatching).

2
  • Example generate intermediate code for the
    following program segment
  • If (a lt b) then I I1 else j I1
  • 100 if a lt b then goto ???
  • 101 goto ???
  • 102 t1 I1
  • 103 I t1
  • 104 goto ???
  • 105 t1 I1
  • 106 j t1
  • 107

3
  • Boolean expressions
  • E-gtE1 or M E2
  • E-gtE1 and M E2
  • E-gt not E1
  • E-gt ( E1 )
  • E-gtid1 relop id2
  • E-gttrue
  • E-gtfalse
  • M-gt
  • E has two attributes truelist and falselist to
    store the list of goto instructions with empty
    destinations.
  • Truelist goto TRUELABEL
  • Falselist goto FALSELABLE
  • M.quad the number for current instruction
  • Makelist(quad) create a list.

4
  • E-gtE1 or M E2 backpatch(E1.falselist, M.qual)
  • E.truelist merge(E1.truelist,
    E2.truelist) E.falselist E2.falselist
  • E-gtE1 and M E2 backpatch(E1.truelist, M.qual)
  • E.truelist E2.truelist E.falselist
    merge(E1.truelist, E2.truelist)
  • E-gt not E1 E.truelist E1.falselist,
    E.falselist E1.truelist
  • E-gt ( E1 ) E.truelist E1.truelist
    E.falselist E1.falselist
  • E-gtid1 relop id2 E.truelist makelist(nextquad)
  • E.falselist makelist(nextquad1)
  • emit(if id1.place relop.op id2.place
    goto ???)
  • emit(goto ???)
  • E-gttrue E.truelist makelist(nextquad)
    emit(goto ???)
  • E-gtfalse E.falselist makelist(nextquad)emit(g
    oto ???)
  • M-gt M.quad nextquad

5
  • Example generating code using one pass LR parser
  • a lt b or (c lt d and e lt f)
  • 100 If (a lt b) goto ???
  • 101 goto ???
  • 102 if c lt d goto ???
  • 103 goto ???
  • 104 If e lt f goto ???
  • 105 goto ???

6
  • Flow of control statements
  • S -gt if E then S1
  • S -gt if E then S1 else S2
  • S -gt while E do S1
  • S-gt begin L end
  • S-gt A
  • L-gtL S
  • L-gtS
  • Attributes E.truelist, E.falselist
  • S.nextlist goto statements to the next
    instruction after this statement.

7
  • S -gt if E then M S1 backpatch(E.truelist,
    M.quad)
  • S.nextlist merge(E.falselist,
    S1.nextlist)
  • S-gt if E then M1 S1 N else M2 S2
  • backpatch(E.truelist, M1.quad)
    backpatch(E.falselist, M2.quad)
  • S.nextlist merge(S1.nextlist, N.nextlist,
    S2.nextlist)
  • S -gt while M1 E do M2 S1 backpatch(E.truelist,
    M2.quad)
  • Backpatch(S1.nextlist, M1.quad)S.nextlist
    E.falselist
  • Emit(goto M1.quad)
  • S-gt begin L end S.nextlist L.nextlist
  • S-gt A S.nextlist null
  • L-gtL M S backpatch(L1.nextlist,
    M.quad)L.nextlist S.nextlist
  • L-gtS L.nextlist S.nextlist
  • N-gt N.nextlist makelist(nextquad) emit(goto
    ???)
  • M-gt M.quad nextquad

8
  • While (a lt b or (c lt d and e lt f)) do
  • if c lt d then
  • x yz
  • else
  • x y-z

9
  • Procedure calls
  • Must generate the calling sequence and returning
    sequence.
  • Can be done either at the calling or at the
    called procedure depending on machine and OS.
  • A simplified example
  • S-gtcall id (Elist) for each item p on queue do
  • emit(param p)
  • emit(call
    id.place)
  • Elist-gtElist1, E append E.place to the end of
    the queue
  • Elist-gtE initialize queue to contain only
    E.place
Write a Comment
User Comments (0)
About PowerShow.com