In this exercise we explore a dataflow analysis for pointers. We begin with a Haskell data type for instructions for a simple machine with registers:
data Instr = Define String - "label:"For the purposes of this question, an extra instruction, called New, has been added. For example, this instruction, with identifier id,
| Mov Reg Reg - "mov.l xxx yyy" (yyy:=xxx)
| Cmp Reg Reg - "cmp.l xxx yyy" (test yyy-xxx)
| Bgt String - "bgt label" (branch if greater than zero)
| New Int Reg - "yyy = malloc(xxx)" (storage allocation)
data Register = D0 D1 D2 D3 D4 D5 D6 D7
id: New 8 D0allocates 8 bytes of memory and sets register D0 to point to it. Our goal is to define a data-flow analysis that computes, for each Register, the set of identifiers of the allocations that the register might point to. For example, given:
1: New 8 D0then, after line 5, we can say that D0 points to the allocation at line 1, and D3 may point to either the allocation at line 1 or the allocation at line 2. We represent this as a points-to-set { (D0, 1), (D3, 1), (D3, 2) }. The effect of an instruction on the points-to-set before its execution depends on the instruction; we define a function effect:
2: New 8 D3
3: Cmp D1 D2
4: Bgt L
5: Mov D0 D3
6: L:
effect :: PointsToSet Instruction PointsToSet
effect pts (Node id (Cmp r1 r2)) = pts
effect pts (Node id (Bgt label)) = pts
effect pts (Node id (New n r)) = pts {(r, id)}
effect pts (Node id (Mov r1 r2)) =
Paul Kelly Imperial College November 2010
(i): We need to add points-to relations saying that r2 might now point to anything r1 might point to:
effect pts (Node id (Mov r1 r2)) = pts [(r2, id) (r1,id) pts]
(ii):
(iii): The rule for New does not account for the points-to elements that are killed by the assignment. To do better we need to remove them:
effect pts (Node id (New n r))
= pts' {(r, id)}
where pts' = [(reg,t) (reg,t) pts, reg r]
Paul Kelly Imperial College November 2010