Some threeaddress statements that will be used later: - PowerPoint PPT Presentation

About This Presentation
Title:

Some threeaddress statements that will be used later:

Description:

Some three-address statements that will be used later: ... Param x, set a parameter for a procedure call. Call p, n call procedure p with n parameters ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Some threeaddress statements that will be used later:


1
  • Some three-address statements that will be used
    later
  • Assignment statements
  • With a binary operation x y op z
  • With a unary operation x op y
  • With no operation(copy) x y
  • Branch statements
  • Unconditional jump goto L
  • Conditional jumps if x relop y goto L
  • Statement for procedure calls
  • Param x, set a parameter for a procedure call
  • Call p, n call procedure p with n parameters
  • Return y return from a procedure with return
    value y

2
  • Example instructions for procedure call p(x1,
    x2, x3, , xn)
  • param x1
  • param x2
  • param xn
  • call p, n
  • Indexed assignments
  • x yi and xi y
  • Address and pointer assignments
  • x y, x y

3
  • Translation scheme to produce three address code
    for assignment statement.
  • Emit (instruction) emit a three address
    statement to the output
  • newtemp return a new temporary
  • lookup(identifier) check if the identifier is in
    the symbol table.
  • Grammar
  • S-gtidE
  • E-gtE1E2
  • E-gtE1E2
  • E-gt-E
  • E-gt(E1)
  • E-gtid
  • E-gtnum

4
  • S-gtidE plookup(id.name)
  • if (p!nil) then emit(p
    E.place) else error
  • E-gtE1E2 E.place newtemp
  • emit(E.place E1.place
    E2.place)
  • E-gtE1E2 E.place newtemp
  • emit(E.place E1.place
    E2.place)
  • E-gt-E1 E.place newtemp
  • emit(E.place -
    E1.place)
  • E-gt(E1) E.place E1.place
  • E-gtid p lookup(id.name)
  • if (p! nil) then E.place p
    else error
  • E-gtnum E.place num.val
  • Example x a 10 20 -30 with bottom-up
    parsing.

5
  • Temporary names can be reused
  • The code generated by E-gtE1E2 has the general
    form
  • Evaluate E1 into t1
  • Evaluate E2 into t2
  • t3 t1 t2
  • All temporaries used in E1 are dead after t1 is
    evaluated
  • All temporaries used in E2 are dead after t2 is
    evaluated
  • T1 and t2 are dead after t3 is assigned
  • Temporaries can be managed as a stack
  • Keep a counter c, newtemp increases c, when a
    temporary is used, decreases c.
  • See the previous example.

6
  • Addressing array elements
  • Arrays are typically stored in block of
    consecutive locations.
  • One-dimensional arrays
  • A arraylow..high of
  • the ith elements is at
  • base (i-low)width ? iwidth (base
    lowwidth)
  • Multi-dimensional arrays
  • Row major or column major forms
  • Row major a1,1, a1,2, a1,3, a2,1,
    a2,2, a2,3
  • Column major a1,1, a2,1, a1, 2, a2,
    2,a1, 3,a2,3
  • In raw major form, the address of ai1, i2 is
  • Base((i1-low1)(high2-low21)i2-low2)width

7
  • In general, for any dimensional array, the
    address of ai1, i2, i3, , ik can be computed
    as follows
  • Let highj and lowj be for bounds for ij
  • Let nj highj lowj 1
  • The address is
  • (((i1n2i2)n3)nkik)width base
    ((low1n2low2)n3)nklowk) width
  • When generating code for array references, we
    need to explicitly compute the address.

8
  • Translation scheme for array elements
  • Limit(array, j) returns njhighj-lowj1
  • .place the temporarys or variables
  • .offset offset from the base, null if not an
    array reference
  • Grammar
  • S-gtL E
  • E-gtEE
  • E-gt(E)
  • E-gtL
  • L-gtElist
  • L-gtid
  • Elist-gtElist, E
  • Elist-gtidE

9
  • S-gtL E if L.offset null then emit(L.place
    E.place)
  • else emit(L.placeL.offset
    E.place)
  • E-gtE1E2 E.place newtemp
  • emit(E.place E1.place
    E2.place)
  • E-gt(E1) E.place E1.place
  • E-gtL if L.offset null then E.place L.place
  • else E.place newtemp
  • emit(E.place L.place
    L.offset )
  • L-gtElist L.place newtemp L.offset
    newtemp
  • emit(L.place
    c(Elist.array))
  • emit(L.offset Elist.place
    width(Elist.array)

10
  • L-gtid L.place lookup(id.name)
  • L.offset null
  • Elist-gtElist1, E
  • t newtemp
  • m Elist1.ndim 1
  • emit(t Elist1.place limit(Elist1.array
    , m))
  • emit(t, t E.place)
  • Elist.array Elist1.array
  • Elist.place t
  • Elist.ndim m
  • Elist-gtidE Elist.array lookup(id.name)
  • Elist.place E.place
  • Elist.ndim 1

11
  • Example A array1..10, 1..20 of integer
  • Translate x Ay, z
  • Accessing fields in records?
  • a.b.c.d x
  • X a.b.c.d
Write a Comment
User Comments (0)
About PowerShow.com