Backpacking in compiler design refers to reducing the size of a compiler by removing unnecessary components, such as unused variables, functions, or code, to make it more efficient and optimized. This process is known as "compiler backpacking" or "compiler slimming".
During the code generation phase, the compiler must conduct leaps, but the values required for these jumps may not be known in one pass, so it improvises by putting in values that will be replaced once the correct values are known, a process known as backpatching.
Backpatching is a technique used in compiler design to delay the assignment of addresses to code or data structures until a later stage of the compilation process. This allows the compiler to generate code with placeholder addresses that are later updated or "backpatched" with the correct addresses once they are known. Backpatching is commonly used in compilers for languages that support complex control structures or dynamic memory allocation.
Solving this problem will increase your chance to get selected in this company
Skill covered: Programming
How do you create a function in JavaScript?
def myFunction() {}create function myFunction()function myFunction() {}
Choose another skill to practice
One-Pass Code Generation Using Backpatching
Backpatching can be used to generate a boolean expressions program and the flow of control statements in a single pass. In jumping code for Boolean statements, the synthesized properties truelist and falselist of non-terminal B are utilized to handle labels.
B.truelist, which is a list of the jump or conditional jump instructions, should contain the label to which control should move if B is true.
When B is false, B.falselist is a list of instructions that will eventually result in the label to which control will be assigned.
All of the jumps to true and false, as well as the label field, are left blank when the program for B is created. The lists B.truelist and B.falselist include these early leaps.
A list of jumps to the instruction immediately after the code for S is displayed by the synthesized attribute S.nextlist on a statement S, for example. It can generate instructions into an instruction array, with labels acting as indexes. Three functions are used to change the list of leaps:
Makelist (i): Make a new list with only i, an index into the array of instructions, then return a pointer to the newly produced list with the makelist command.
Merge (p1,p2): Returns a pointer to the concatenated list after concatenating the lists pointed to by p1 and p2.
Backpatch (p, i): For each of the instructions on the record pointed to by p, inserts i like the target label.
Boolean Expressions
It can construct a translation scheme suitable for generating 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.
The steps for backpatching using boolean expressions are as follows:
Let’s look at an example to understand this concept clearly.
x < 100 || y > 200 && x != y
Let us first understand the expression: II stands for OR operation means either of the statements if true results the whole expression as TRUE. && stands for AND operation means both statements must be true if the expression needs to be true.
In our expression, if x<100 is true, then there is no need to go to the next statement after the OR operation. If it is not the case, there is a need to go to the next statement. The next statement is again an expression that contains AND operation meaning both the statements must be true. If either statement fails then the whole expression will be considered false. Since in single pass where to go next is undefined as shown below:
100: if x < 100 goto ____
101: goto ____
102: if y > 200 goto ____
103: goto ____
104: if x!=y goto ____
105: goto ____
106: true
107: false
Now using backpatching,
100: if x < 100 goto __106__
101: goto __102__
102: if y > 200 goto __104__
103: goto __107__
104: if x!=y goto __106__
105: goto __107__
106: true
107: false
Generating Translation Rules:
B → B1 || MB2
{ Backpatch(B1.fl,M.instr);
B.tl = merge(B1.tl,B2.tl);
B.fl = B2.fl }
B → B1 && MB2
{Backpatch(B1.tl,M.instr);
B.tl = B2.tl;
B.fl = merge(B1.fl,B2.fl);}
B → !B1
{B.tl = B1.fl ;
B.fl = B1.tl;}
B → B1
{B.tl = B1.tl;
B.fl = B1.fl;}
M→ ε
{m.instr = nextinstr; }
Parse tree:
Flow of Control Statement
Control statements are those that change the execution order of statements. Statements such as If, If-else, Switch-Case, and while-do are examples. In computer languages, Boolean statements are widely used to:
Alter the flow of control−Conditional expressions that change the flow of control in a statement are known as Boolean expressions. The program's location implies the value of such a Boolean statement. For example, if claim S is met, the phrase E must be true.
Compute logical values− True or false values can be expressed using a Boolean expression. Three address instructions with logical operators can be used to compute such Boolean expressions in parallel to arithmetic expressions.
Labels and Gotos
Labels and Gotos are programming constructs that allow programmers to control the flow of execution in their code. A label is a named location in code that a goto statement can reference. A goto statement allows a programmer to transfer control to the labelled location in code, bypassing any statements in between. While powerful, goto statements are usually discouraged because they make code more difficult to understand and maintain. Goto statements are not supported by many computer languages, including Java and Python.
Some Competitive Questions on Backpatching
Question.1: If (a<b then t=1) else t=0.
Fill in the missing goto statements.
if (a < b) goto ____
t = 0
Goto ____
t = 1
Solution:
In the above question, we need to fill the black spaces with the indexing of the lines where we have to jump in order to create a program that is mentioned in the above question.
First of all in Line-1 if (a<b) we need to jump to line no 4 in order to get (t = 1).
Now if the condition in line no 1 is false then we will not jump and proceed further to the next line hence getting (t = 0).
To avoid going to line no 4 we need to exit the code at the last line so now we will jump to line no 5 from line no 3 in order to avoid line no 4 and hence we have filled all the black spaces.
The answer to the above question is given below:
if (a < b) goto __4__
t = 0
Goto __5__
t = 1
Question.2: If (a<b) && (c<d) then t=1 else t=0.
Fill in the missing goto statements.
if (a < b) goto ____
t = 0
goto ____
if (c < d) goto ____
goto ____
t = 1
Solution: In this question, a nested condition is given where we need to take the union of two conditions.
We need to get (t = 1) when the two conditions (a<b && c<d). This is possible when we travel along line 1 to line 4 and then get the value of t at line 6 and terminate at line 7.
When the first condition is false we need to get the value of t in line 2 and then terminate at line no 7.
When the second condition is false, we need to jump to line 2 to get the t = 0 and terminate the immediate goto to 7.
The answer to the above question is given below:
if (a < b) goto __4__
t = 0
goto __7__
if (c < d) goto __6__
goto __2__
t = 1
Question.3: For (i = 1; i <= n; i++){
X = a+b*c;
}
Fill In the missing goto statements.
i = 1
if (i <= n) goto ____
goto ____
t1 = b * c
t2 = a + t1
x = t2
i = i +1
goto ____
Solution: In the question above, there is a need for a loop to be created in order to perform the task under a specific condition. Hence the explanation is given below.
First of all initialization of variables takes place in line no 1.
Now every loop has a necessary condition under which the loop repeats itself hence if (i <= n) we need to update the values of t1 and t2 and this can be done by jumping to line no 4 and then we again need to check the condition hence we need to return from line 8 to line no 2 back.
If the condition in line no 2 is false then we need to end the loop and exit from line no 9 hence we will jump to line no 9.
The answer to the above question is given below:
i = 1
if (i <= n) goto __4__
goto __9__
t1 = b * c
t2 = a + t1
x = t2
i = i +1
goto __2__
Applications of Backpatching
It aids in the resolution of code that has been seeded with forwarding branches.
Backpatching is a technique for converting flow-of-control statements into a single pass.
During the code generation process, it is the action of filling in blank labels with undetermined data.
During bottom-up parsing, backpatching is utilized to generate quadruples for boolean expressions.
Frequently Asked Questions
What are the three functions used in Backpatching?
To generate code using backpatching, three procedures are run in two passes: makelist(), merge(), and backpatch().
What does backpatching entail?
Backpatching is a technique for converting flow-of-control statements into a single pass. During bottom-up parsing, backpatching is utilized to generate quadruples for boolean expressions. During the code generation process, it is the action of filling in unspecified information on labels.
What is meant by back patching?
Back patching is a technique used in compiler design to delay the assignment of addresses to code or data structures until a later stage of the compilation process. This allows the compiler to generate code with placeholder addresses that are later updated with the correct addresses once they are known.
What are the methods of translating boolean expressions?
The two common methods of translating boolean expressions are algebraic and truth table methods. In the algebraic method, boolean expressions are manipulated using laws of boolean algebra. In the truth table method, the possible input combinations are listed and the output values are determined for each combination.
What are the three kinds of intermediate representation in compiler design?
The three kinds of intermediate representation (IR) in compiler design are high-level IR, mid-level IR, and low-level IR. High - level IR is the source code and is easy to read. Low- level IR is the optimization for efficient execution. Mid - level acts as a bridge between the High level and Low level.
Conclusion
Hope you learned something. But the knowledge never stops, we have gained knowledge of concepts of compiler design like backpatching for boolean expressions, Flow of control Statements, etc. and now we can easily do any question related to backpatching. To learn more you can visit our website for more articles.