26

What does backpatching mean ? Please illustrate with a simple example.

saplingPro
  • 18,513
  • 51
  • 134
  • 185

7 Answers7

24

Back patching usually refers to the process of resolving forward branches that have been planted in the code, e.g. at 'if' statements, when the value of the target becomes known, e.g. when the closing brace or matching 'else' is encountered.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • 4
    I wrote the same answer provided by you in my today's exam. :) – HarshitMadhav Apr 03 '17 at 09:55
  • @user207421, Different countries , Different methods & rules. Don't assume. Some countries exams are conducted in a different manner. For ex, in India: once you are in exam hall you don't have anything but a pencil or pen. When you are writing whatever in your head on paper, you don't have to give the citation. Counter question to this would be: do you give citations in SAT ? or subject test of GRE ? – rak Mar 19 '19 at 12:37
  • 1
    @rak Counter-counter question: what on earth is SAT? And GRE? The risk associated with not paraphrasing or citing properly is that the examiner will Google your answer and find literally the same text here, and mark accordingly. – user207421 Mar 27 '19 at 23:40
  • @user207421, if you write it in your own words (from your head), I doubt if it will match. Again not every examiner will google (different countries/ education system!) especially if your exam is on pen and paper. You will need an OCR or some kind of automated system to do that. Which if they have, why would they use pen and paper method for exam at the first place? – rak Apr 03 '19 at 20:45
  • How does backpatching work in the context of an assembler translating code from assembly language to machine code? – danmaze Mar 17 '20 at 18:45
12

In intermediate code generation stage of a compiler we often need to execute "jump" instructions to places in the code that don't exist yet. To deal with this type of cases a target label is inserted for that instruction. A marker nonterminal in the production rule causes the semantic action to pick up.

Mostafiz Rahman
  • 6,939
  • 6
  • 45
  • 68
6

Some statements like conditional statements, while, etc. will be represented as a bunch of "if" and "goto" syntax while generating the intermediate code. The problem is that, These "goto" instructions, do not have a valid reference at the beginning(when the compiler starts reading the source code line by line - A.K.A 1st pass). But, after reading the whole source code for the first time, the labels and references these "goto"s are pointing to, are determined.

The problem is that can we make the compiler able to fill the X in the "goto X" statements in one single pass or not? The answer is yes.

If we don't use backpatching, this can be achieved by a 2 pass analysis on the source code. But, backpatching lets us to create and hold a separate list which is exclusively designed for "goto" statements. Since it is done in only one pass, the first pass will not fill the X in the "goto X" statements because the comipler doesn't know where the X is at first glance. But, it does stores the X in that exclusive list and after going through the whole code and finding that X, the X is replaced by that address or reference.

user207421
  • 289,834
  • 37
  • 266
  • 440
hexpheus
  • 647
  • 9
  • 16
4

Backpaching is the process of leaving blank entries for the goto instruction where the target address is unkonown in the forward transfer in the first pass and filling these unknown in the second pass.

Anu Goit
  • 41
  • 1
3

Backpatching: The syntax directed definition can be implemented in two or more passes (we have both synthesized attributes and inherited attributes).

Build the tree first.

Walk the tree in the depth-first order.

The main difficulty with code generation in one pass is that we may not know the target of a branch when we generate code for flow of control statements

Backpatching is the technique to get around this problem. Generate branch instructions with empty targets When the target is known, fill in the label of the branch instructions (backpatching).

Brij Raj Kishore
  • 1,465
  • 1
  • 9
  • 20
2

backpatching is a process in which the operand field of an instruction containing a forward reference is left blank initially. the address of the forward reference symbol is put into this field when its definition is encountered in the program.

0

Back patching is the activity of filling up the unspecified information of labels by using the appropriate semantic expression in during the code generation process.

It is done by:

  1. boolean expression.
  2. flow of control statement.
user207421
  • 289,834
  • 37
  • 266
  • 440