Consider the intermediate code given below.
(1) i=1 (2) j=1 (3) t1 = 5 * i (4) t2 = t1 + j (5) t3 = 4 * t2 (6) t4 = t3 (7) a[t4] = -1 (8) j = j + 1 (9) if j <= 5 goto (3) (10) i = i +1 (11) if i < 5 goto (2)
The number of nodes and edges in control-flow-graph constructed for the above code, respectively, are
View worked solution
Answer: B
To construct the Control Flow Graph (CFG), we first identify the leaders (the first instructions of the basic blocks):
- Rule 1: The first instruction is a leader.
Line (1) is a leader. - Rule 2: Any instruction that is the target of a conditional or unconditional jump is a leader.
Line (2) (target ofgoto (2)at line 11) and Line (3) (target ofgoto (3)at line 9) are leaders. - Rule 3: Any instruction immediately following a conditional or unconditional jump is a leader.
Line (10) (following the jump at line 9) is a leader.
Using these leaders, we partition the code into 4 basic blocks:
- : Line (1)
- : Line (2)
- : Lines (3) to (9)
- : Lines (10) to (11)
Standard CFG construction includes an Entry and an Exit node. Thus, the total number of nodes is:
The control flow edges are:
- (loop back when
j <= 5) - (fall through when
j > 5) - (loop back when
i < 5) - (fall through when
i >= 5)
This gives a total of 7 edges.
Free practice · Your answers on this page are not saved.
