Backpatching in Compiler Design
Backpatching is basically a process of fulfilling unspecified information. This information is of labels. It basically uses the appropriate semantic actions during the process of code generation. It may indicate the address of the Label in goto statements while producing TACs for the given expressions. Here basically two passes are used because assigning the positions of these label statements in one pass is quite challenging. It can leave these addresses unidentified in the first pass and then populate them in the second round. Backpatching is the process of filling up gaps in incomplete transformations and information.
Need for Backpatching:
Backpatching is mainly used for two purposes:
1. Boolean expression:
Boolean expressions are statements whose results can be either true or false. A boolean expression which is named for mathematician George Boole is an expression that evaluates to either true or false. Let’s look at some common language examples:
- My favorite color is blue. → true
- I am afraid of mathematics. → false
- 2 is greater than 5. → false
2. Flow of control statements:
The flow of control statements needs to be controlled during the execution of statements in a program. For example:
3. Labels and Gotos:
The most elementary programming language construct for changing the flow of control in a program is a label and goto. When a compiler encounters a statement like goto L, it must check that there is exactly one statement with label L in the scope of this goto statement. If the label has already appeared, then the symbol table will have an entry giving the compiler-generated label for the first three-address instruction associated with the source statement labeled L. For the translation, we generate a goto three-address statement with that compiler-generated label as a target.
When a label L is encountered for the first time in the source program, either in a declaration or as the target of the forward goto, we enter L into the symbol table and generate a symbolic table for L.
One-pass code generation using backpatching:
In a single pass, backpatching may be used to create a boolean expressions program as well as the flow of control statements. The synthesized properties truelist and falselist of non-terminal B are used to handle labels in jumping code for Boolean statements. The label to which control should go if B is true should be added to B.truelist, which is a list of a jump or conditional jump instructions. B.falselist is the list of instructions that eventually get the label to which control is assigned when B is false. The jumps to true and false exist, as well as the label field, are left blank when the program is generated for B. The lists B.truelist and B.falselist, respectively, contain these early jumps.
A statement S, for example, has a synthesized attribute S.nextlist, which indicates a list of jumps to the instruction immediately after the code for S. It can generate instructions into an instruction array, with labels serving as indexes. We utilize three functions to modify the list of jumps:
- Makelist (i): Create a new list including only i, an index into the array of instructions and the makelist also returns a pointer to the newly generated list.
- Merge(p1,p2): Concatenates the lists pointed to by p1, and p2 and returns a pointer to the concatenated list.
- Backpatch (p, i): Inserts i as the target label for each of the instructions on the record pointed to by p.
Backpatching for Boolean Expressions:
Using a translation technique, it can create code for Boolean expressions during bottom-up parsing. In grammar, a non-terminal marker M creates a semantic action that picks up the index of the next instruction to be created at the proper time.
For Example, Backpatching using boolean expressions production rules table:
Step 1: Generation of the production table
Step 2: We have to find the TAC(Three address code) for the given expression using backpatching:
A < B OR C < D AND P < Q
Step 3: Now we will make the parse tree for the expression:
The flow of Control Statements:
Control statements are those that alter the order in which statements are executed. If, If-else, Switch-Case, and while-do statements are examples. Boolean expressions are often used in computer languages to
- Alter the flow of control: Boolean expressions are conditional expressions that change the flow of control in a statement. The value of such a Boolean statement is implicit in the program’s position. For example, if (A) B, the expression A must be true if statement B is reached.
- Compute logical values: During bottom-up parsing, it may generate code for Boolean statements via a translation mechanism. A non-terminal marker M in the grammar establishes a semantic action that takes the index of the following instruction to be formed at the appropriate moment.
Applications of Backpatching:
- Backpatching is used to translate flow-of-control statements in one pass itself.
- Backpatching is used for producing quadruples for boolean expressions during bottom-up parsing.
- It is the activity of filling up unspecified information of labels during the code generation process.
- It helps to resolve forward branches that have been planted in the code.