Part (a):
+-------------------------------+ | Enter | +-------------------------------+ | v +-------------------------------+ | L1: | | read a | | read x | | ans = 1 | | goto L2 | +-------------------------------+ | v +-------------------------------+ | L2: | | if (x > 0) |---------+ | then goto L3 | | | else goto L4 |<--+ | +-------------------------------+ | | | | | v | | +-------------------------------+ | | | L3: | | | | ans = ans * a | | | | x = x - 1 |---+ | | goto L2 | | +-------------------------------+ | | +--------------------+ | | v +-------------------------------+ | L4: | | print ans | | goto Exit | +-------------------------------+ | v +-------------------------------+ | Exit: | | return | +-------------------------------+
Part (b): Here is the trace of the execution of the PE algorithm.
Stmt S | Current bb | vs | Emitted Code |
---|---|---|---|
<L1, (2)> | |||
read a | L1 | 2 | read a |
read x | |||
ans = 1 | ans = 1 | ||
goto L2 | |||
if (x > 0) then goto L3 else goto L4 | L2 | ||
ans = ans * a | L3 | ans = ans * a | |
x = x - 1 | 1 | ||
goto L2 | |||
if (x > 0) then goto L3 else goto L4 | L2 | ||
ans = ans * a | L3 | ans = ans * a | |
x = x - 1 | 0 | ||
goto L2 | |||
if (x > 0) then goto L3 else goto L4 | L2 | ||
print ans | L4 | print ans |
And here is the residual program:
read a ans = 1 ans = ans * a ans = ans * a print ans
Here is the CFG for the example program (in its high-level form), and the PDG for the high-level form.
Here is the low-level version of the program shown in its CFG form:
Assuming that the initial classification of the inputs is
Here is a trace of the PE algorithm:
Stmt S | Current bb | vs | Emitted Code |
---|---|---|---|
<L1,(len:3, n:0)> | |||
read array | L1 | vs0 = (len:3, n:0) | read array |
read len | |||
n=0 | |||
result = 0 | result = 0 | ||
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs0 | |
item = array[n] | L3 | item = array[0] | |
if(even(item)) then goto L4 else goto L5 | if(even(item)) then goto <L4,vs0> else goto <L5, vs0> | ||
<L4, vs0> | |||
result = result + item | L4 | vs0 | result = result + item |
goto L6 | |||
n = n+1 | L6 | vs1 = (len:3,n:1) | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs1 | |
item = array[n] | L3 | item = array[1] | |
if(even(item)) then goto L4 else goto L5 | if(even(item)) then goto <L4,vs1> else goto <L5, vs1> | ||
<L5, vs0> | |||
result = result - item | L5 | vs0 | result = result - item |
goto L6 | |||
n =n + 1 | L6 | vs1 | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs1 | |
item = array[n] | L3 | item = array[1] | |
if(even(item)) then goto L4 else goto L5 | if(even(item)) then goto <L4,vs2> else goto <L5, vs2> | ||
<L4,vs1> | |||
result=result+item | L4 | vs1 | result = result + item |
goto L6 | |||
n = n + 1 | L6 | vs2 = (len:3, n:2) | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs2 | |
item = array[n] | L3 | vs2 | item = array[2] |
if(even(item)) then goto L4 else goto L5 | if(even(item)) then goto <L4,vs2> else goto <L5, vs2> | ||
<L5,vs1> | |||
result = result - item | L5 | vs1 | result = result - item |
goto L6 | |||
n = n + 1 | L6 | vs2 | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs2 | |
item = array[n] | L3 | vs2 | item = array[2] |
if(even(item)) then goto L4 else goto L5 | if(even(item)) then goto <L4,vs2> else goto <L5, vs2> | ||
<L4,vs2> | |||
result = result + item | L4 | vs2 | result = result + item |
goto L6 | |||
n =n + 1 | L6 | vs3 = (len:3,n:3) | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs3 | |
print result | L7 | print result | |
return | return | ||
<L5,vs2> | |||
result = result - item | L5 | vs2 | result = result - item |
goto L6 | |||
n =n + 1 | L6 | vs3 | |
goto L2 | |||
if (n < len) then goto L3 else goto L7 | L2 | vs3 | |
print result | L7 | print result | |
return | return |
And here is the residual program:
<L1, (len:3, n:0)>: read array result = 0 item = array[0] if (even(item)) then goto <L4, (len:3, n:0)> else goto <L5, (len:3, n:0)> <L4, (len:3, n:0>: result = result + item item = array[1] if (even(item)) then goto <L4, (len:3, n:1)> else goto <L5, (len:3, n:1)> <L5, (len:3, n:0)>: result = result - item item = array[1] if (even(item)) then goto <L4, (len:3, n:2)> else goto <L5, (len:3, n:2)> <L4, (len:3, n:1>: result = result + item item = array[2] if (even(item)) then goto <L4, (len:3, n:2)> else goto <L5, (len:3, n:2)> <L5, (len:3, n:1)>: result = result - item item = array[2] if (even(item)) then goto <L4, (len:3, n:2)> else goto <L5, (len:3, n:2)> <L4, (len:3, n:2>: result = result + item print result return <L5, (len:3, n:2)>: result = result - item print result return