ACompressed recap
🧾
Three-address code has at most one operator per instruction, with temporaries for intermediate values; it is stored as quadruples (op, arg1, arg2, result), triples (results by position) or indirect triples (plus an order list). Arrays need address arithmetic: row-major base + ((i − l₁)n₂ + (j − l₂))w. Conditions become jumping code with short-circuit exits; if and while statements add labels and jumps; backpatching fills jump targets later from truelists and falselists.
BMerged rule card
4.2.1 Three-address code instruction forms; quadruples, triples, indirect triples
- TAC: ≤ 1 operator, ≤ 3 addresses per instruction; temporaries for intermediate values.
- Forms: x = y op z, x = op y, x = y, goto L, if x relop y goto L, param/call/return, x = y[i], x[i] = y, &, *.
- Quadruple: (op, arg1, arg2, result).
- Triple: (op, arg1, arg2), result = position (k).
- Indirect triples: pointer list gives order; easy reordering.
4.2.2 TAC for expressions and array accesses (address calculation)
- 1-D: base + (i − low) × w.
- Row-major 2-D: base + ((i − low₁) × n₂ + (j − low₂)) × w.
- Column-major 2-D: base + ((j − low₂) × n₁ + (i − low₁)) × w.
- k-D row-major: Horner form ((i₁ n₂ + i₂) n₃ + …) × w.
- TAC: scale indices by constants, then x = a[t] / a[t] = y with byte offset t.
4.2.3 TAC for if/while and Boolean expressions (short-circuit; backpatching brief)
- Relop: if a relop b goto B.true; goto B.false.
- ||: B₁.true = B.true, B₁.false → start of B₂. &&: B₁.true → start of B₂, B₁.false = B.false. !: swap.
- if: B; L1: S1; next. if-else: B; L1: S1; goto next; L2: S2; next. while: begin: B; L1: S; goto begin; next.
- Backpatching: truelist/falselist/nextlist; || fills B₁.falselist, && fills B₁.truelist.
CInterleaved GATE-style questions
Q1NAT2 marksAddress
int M[1..10][1..20] (4 bytes) row-major from 1000. Address of M[3][5]?
Numerical answer: ________
Q2MCQ1 markTriples
In triples, the result of the instruction at position 4 is referenced as:
- (A) t4
- (B) (4)
- (C) r4
- (D) a new variable
Q3NAT1 markInstructions
How many three-address instructions (no sharing, last op directly into x) does x = a * b + c * d − e need?
Numerical answer: ________
Q4MCQ2 marksJumping code
For B₁ && B₂ in jumping code, which is correct?
- (A) B₁.true = B.true
- (B) B₁.false = B.false
- (C) B₂.false = start of B₁
- (D) B₁.false = start of B₂
Q5MSQ1 markRepresentations
Which representations name temporaries explicitly?
- (A) quadruples
- (B) triples
- (C) indirect triples
- (D) three-address code text
Q6NAT2 marksBackpatching
Backpatching x < 100 || x > 200 && x != y from 100: what is the sum of the instruction numbers on the final falselist?
Numerical answer: ________
Q7MCQ1 markColumn-major
Fortran stores 2-D arrays in:
- (A) row-major order
- (B) column-major order
- (C) random order
- (D) diagonal order
Q8NAT1 markCount
How many instructions does the generated code for s = 0; i = 0; while (i < 10) { s = s + i; i = i + 2; } t = s * 2; have?
Numerical answer: ________
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: 11762 marks
1000 + ((3 − 1) × 20 + (5 − 1)) × 4 = 1176.
Re-read: Lesson 4.2.2 — TAC for expressions and array accesses (address calculation)
Q2 Answer: (B)1 mark
By position.
- (A) is wrong: Triples avoid named temporaries.
- (C) is wrong: No such notation.
- (D) is wrong: No.
Re-read: Lesson 4.2.1 — Three-address code instruction forms; quadruples, triples, indirect triples
Q3 Answer: 41 mark
Four operators (a * b, c * d, +, −) give 4 instructions, the last one writing x.
Re-read: Lesson 4.2.1 — Three-address code instruction forms; quadruples, triples, indirect triples
Q4 Answer: (B)2 marks
If B₁ is false, the whole && is false.
- (A) is wrong: B₁ true must continue to B₂.
- (C) is wrong: No loops back.
- (D) is wrong: That is the || rule.
Re-read: Lesson 4.2.3 — TAC for if/while and Boolean expressions (short-circuit; backpatching brief)
Q5 Answer: (A) (D)1 mark
Triples (direct or indirect) use positions.
Re-read: Lesson 4.2.1 — Three-address code instruction forms; quadruples, triples, indirect triples
Q6 Answer: 2082 marks
Falselist = { 103, 105 } (computed), sum 208.
Re-read: Lesson 4.2.3 — TAC for if/while and Boolean expressions (short-circuit; backpatching brief)
Q7 Answer: (B)1 mark
Columns are contiguous.
- (A) is wrong: C is row-major.
- (C) is wrong: No.
- (D) is wrong: No.
Re-read: Lesson 4.2.2 — TAC for expressions and array accesses (address calculation)
Q8 Answer: 81 mark
Two initialisations, two jumps for the test, two body assignments, the jump back, the final assignment: 8.
Re-read: Lesson 4.2.3 — TAC for if/while and Boolean expressions (short-circuit; backpatching brief)