Backpatching in Compiler Design
Backpatching in Compiler Design
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".
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.
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:
1. Production Table
2. Finding Three address codes
3. Making the parse tree for the expression
x < 100 || y > 200 && x != y
Let us first understand the expression: || 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:
B → B1 || MB2 {Backpatch(B1.fl,M.instr);
B.tl = merge(B1.tl,B2.tl);
B.fl = B2.fl }
M→ ε {m.instr = nextinstr; }
Parse tree:
• 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.