Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Backpatching

Backpatching is a one-pass technique to generate correct jump targets (labels) for forward jumps in intermediate code, using lists of unresolved jump instructions and three small operations: makelist, merge and backpatch.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Backpatching is used during the translation phase of a compiler to generate intermediate code (usually three-address code) when the targets of forward jumps are not yet known. An engineer uses backpatching while building a single-pass or syntax-directed translator (for example inside a parser-generator action or a compiler front end) to implement boolean expressions, conditionals, loops and short-circuit operators correctly and efficiently.

Key ideas

Start from first principles: a compiler translates high-level control flow into instructions that jump to labels. When the parser reads a forward jump (for example the then-part of an if before the else or the loop head before the exit), the numeric label of the target is not known yet. Backpatching is a technique to record those unresolved jump instructions and fill in their target addresses later.

Definitions (exact):

  • Three-address code: an intermediate representation where each instruction contains at most three addresses (operands), typically of the form x = y op z or if x relop y goto L or goto L. Assumption: we can number instructions and labels monotonically.
  • Backpatching: a method for handling forward jumps by maintaining lists of instruction indices whose jump targets are unknown and later "patching" (filling) those targets when the label becomes known.
  • makelist(i): an operation that creates a new list containing a single instruction index i that needs its target filled later.
  • merge(p, q): an operation that concatenates two lists p and q, returning a combined list of instruction indices.
  • backpatch(p, L): an operation that sets the target label L into all jump instructions whose indices are in list p.

Assumptions required by backpatching:

  1. The compiler is producing code in a linear list where each emitted instruction has a unique instruction number (or next instruction index). Call the next instruction number nextinstr.
  2. Jump instructions carry a placeholder for the target that can later be patched (for example if x goto _ or goto _).
  3. Boolean expressions are not evaluated to a boolean value but are translated into jumps: each boolean expression comes with two lists: truelist (places to goto if expression is true) and falselist (places to goto if expression is false).

Standard semantic rules (the exact productions and associated actions universities expect):

  • For relational expressions E -> E1 relop E2:
    • Generate code if E1 relop E2 goto _ and goto _.
    • E.truelist = makelist(index-of-if)
    • E.falselist = makelist(index-of-goto)
  • For logical OR E -> E1 OR E2:
    • backpatch(E1.falselist, E2.start) // link where E2 code begins
    • E.truelist = merge(E1.truelist, E2.truelist)
    • E.falselist = E2.falselist
  • For logical AND E -> E1 AND E2:
    • backpatch(E1.truelist, E2.start)
    • E.truelist = E2.truelist
    • E.falselist = merge(E1.falselist, E2.falselist)
  • For NOT E -> NOT E1:
    • E.truelist = E1.falselist
    • E.falselist = E1.truelist

Markers for statements (used in many university formulations):

  • M -> ε: no code; records position with attribute M.instr = nextinstr.
  • N -> ε: emit goto _ and set N.nextlist = makelist(index-of-that-goto).

Statement productions and actions:

  • S -> if (E) M S1 Actions: backpatch(E.truelist, M.instr); S.nextlist = merge(E.falselist, S1.nextlist).
  • S -> if (E) M S1 N else M S2 Actions: backpatch(E.truelist, M1.instr); backpatch(E.falselist, M2.instr); S.nextlist = merge(merge(S1.nextlist, N.nextlist), S2.nextlist).
  • S -> while M1 (E) M2 S1 Actions: backpatch(E.truelist, M2.instr); backpatch(S1.nextlist, M1.instr); emit goto M1.instr; S.nextlist = E.falselist.

For statements the usual lists are:

  • nextlist: places to be filled with the label of the statement that follows (useful for fall-through after a statement or an emitted goto in sequences and conditionals). For loops, track break and continue separately as breaklist (patched to the after-loop label) and continuelist (patched to the loop test or increment).

Typical translation for if (E) S1:

  1. Evaluate E to produce E.truelist and E.falselist, and E.start (the instruction index where E's code begins).
  2. backpatch(E.truelist, S1.start) so true jumps enter S1.
  3. The statement's nextlist is merge(E.falselist, S1.nextlist) so they go after the if body.

Typical translation for if (E) S1 else S2:

  1. Evaluate E.
  2. backpatch(E.truelist, S1.start)
  3. Let S1.nextlist combine with a goto _ to jump to S2's end; then backpatch both S1.nextlist and E.falselist appropriately to S2.start or after.

What to draw (diagram for exam answers):

  • Draw a small control-flow sketch: for an if-else, draw a diamond labelled E with two outgoing arrows: true arrow to S1 block, false arrow to S2 block. Label the arrows with the lists: true: E.truelist, false: E.falselist. Show S1 and S2 each with their own nextlist pointing to the join point labelled L. On the code list side, draw instruction numbers with placeholders _ for targets and show how backpatch fills them.

Why this is provably correct (short proof outline):

  • Each boolean expression's semantics is captured by producing two lists of jumps: every runtime true must follow a jump in truelist to the code that implements the "true" continuation; similarly for false. Backpatching fills exactly these jumps with the correct continuation labels when those continuation points are known. The operations makelist/merge/backpatch preserve the set of unresolved jumps, and backpatch terminates because each jump is patched exactly once when its continuation becomes known.

Formulas

makelist(i) — creates a list containing the single instruction index i; i is an integer instruction number; use when emitting a jump whose target is unknown.

merge(p, q) — returns a list that is the concatenation of lists p and q; p and q are lists of instruction indices; use when combining unresolved jump lists from two subexpressions.

backpatch(p, L) — for each instruction index i in list p, fill its target field with label L; p is a list of instruction indices, L is an instruction number or label. Applies when the target label L is known.

nextinstr — the number/index of the next instruction to be emitted; integer, monotonically increasing. Use to record starting addresses, e.g. E.start = nextinstr before emitting E's code.

if x relop y goto _ and goto _ — templates for jump instructions with an unknown target; replace _ by label L when backpatching.

Notes: these are syntactic conventions rather than physical units. There are no SI units.

Worked examples

Example 1 — Translate: if (a < b) c = d; else c = e; Assume instruction numbering starts at 100 for clarity. Show each emitted instruction and list updates.

  1. Start: nextinstr = 100.
  2. Parse a < b:
    • Emit: 100: if a < b goto _ // index 100
    • Emit: 101: goto _ // index 101
    • For E: truelist = makelist(100) = [100], falselist = makelist(101) = [101].
  3. Record E.start = 100.
  4. S1 (c = d) starts at nextinstr = 102. Emit 102: c = d.
    • S1.nextlist = empty (no implicit goto)
  5. After S1, emit 103: goto _ to jump to code after else (to be patched). Let this be S1.nextlist = makelist(103).
  6. S2 (c = e) will start at nextinstr = 104. Before that, backpatch E.truelist to S1.start (102): backpatch([100], 102) so instruction 100 becomes if a < b goto 102.
  7. Now backpatch E.falselist to S2.start (104): backpatch([101], 104) so instruction 101 becomes goto 104.
  8. Emit 104: c = e.
  9. The final join (after else) is at nextinstr = 105. Backpatch S1.nextlist ([103]) to 105: instruction 103 becomes goto 105.
  10. Final code list: 100: if a < b goto 102 101: goto 104 102: c = d 103: goto 105 104: c = e 105: <next>

Example 2 — Boolean expression with short-circuit: a < b || c < d && e == f We show truelist/falselist computation and merges.

  1. Start nextinstr = 200.
  2. For a < b emit 200: if a < b goto _ and 201: goto _ => truelist [200], falselist [201]. E1.start = 200; nextinstr = 202.
  3. For c < d emit 202: if c < d goto _ and 203: goto _ => truelist [202], falselist [203]; nextinstr = 204.
  4. For e == f emit 204: if e == f goto _ and 205: goto _ => truelist [204], falselist [205]; nextinstr = 206.
  5. Combine c < d && e == f: backpatch(c.truelist, e.start=204) => backpatch([202], 204). New truelist = e.truelist = [204]. New falselist = merge(c.falselist, e.falselist) = [203] + [205] = [203,205].
  6. Combine a < b || (c<d && e==f): backpatch(a.falselist, start of RHS which is 202) => backpatch([201], 202). truelist = merge(a.truelist, rhs.truelist) = [200] + [204] = [200,204]. falselist = rhs.falselist = [203,205].
  7. The final expression has truelist [200,204] and falselist [203,205]; later these are backpatched to the appropriate true/false continuations.

Example 3 — while loop: while (E) S

  1. Let begin = nextinstr (start of E). Evaluate E producing E.truelist and E.falselist.
  2. backpatch(E.truelist, S.start) so true goes to loop body.
  3. After S, emit goto begin to loop back.
  4. backpatch(S.nextlist, begin) to iterate; backpatch(E.falselist, after-loop). If the language has break/continue, backpatch breaklist to the after-loop label and continuelist to begin.

Show with numbers: nextinstr = 300.

  • E at 300: emit jumps making truelist [300], falselist [301]. nextinstr=302.
  • S at 302: ... after S nextinstr=305, emit 305: goto 300.
  • backpatch([300], 302) and backpatch([301], 306) where 306 is after-loop.

Check each instruction index and patches in an exam answer.

Common mistakes

  • Not emitting a goto after S1 in an if-then-else (so control falls through incorrectly) or emitting an unnecessary goto in if without else.
  • Using values (0/1) instead of jump lists for boolean expressions when the grammar and semantic rules expect jumps.
  • Off-by-one errors in instruction numbering; always show nextinstr updates clearly in exams.
  • Failing to merge lists in the right order (merge is associative but the order affects displayed instruction sequence in a traced answer — be consistent).

For your exam

Kinds of questions and what full-marks answers include:

  • Definitions: define backpatching, makelist/merge/backpatch, truelist/falselist/nextlist. A full answer shows concise definitions and an example sentence of use.
  • Short notes: explain backpatching steps and assumptions. Full marks need the three operations, their roles, and why it solves forward jumps.
  • Derivations: show semantic rules for boolean expressions and control statements (if, if-else, while). Full marks require rules written as grammar productions with semantic actions using makelist/merge/backpatch and an explanation.
  • Traces/algorithms: given a small program fragment, produce the step-by-step three-address code with instruction numbers and list contents at each step. Full marks need correct instruction numbers, correct list contents, and final backpatched code.
  • Diagrams: draw the control-flow diamond for conditions with labels and annotate the truelist/falselist and nextlist. Full marks require matching the diagram to the produced code.

Exam strategy: always show nextinstr before emitting code, show makelist/merge results explicitly, and show each backpatch call with the list and the label applied.

Quick check

  1. What are the three primitive operations used in backpatching?
  2. For E1 AND E2, which list from E1 do you backpatch to E2.start?
  3. In if (E) S1 else S2, to which label do you backpatch E.truelist?

Answers: makelist, merge, backpatch; E1.truelist; S1.start

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?