Professional Documents
Culture Documents
GENERATION
AMEYAA BIWALKAR
PHASES
COMPILERS AND INTERPRETERS
Triples
Temporaries are not used and instead references to instructions are
made
Indirect triples
In addition to triples we use a list of pointers to triples
INSTRUCTIONS FOR 3AC
Assignment instructions:
a = b biop c, a = uop b, and a = b (copy), where biop is any binary arithmetic,
logical, or relational operator
uop is any unary arithmetic (-, shift, conversion) or logical operator (∼)
Conversion operators are useful for converting integers to floating point
numbers, etc.
Jump instructions:
goto L (unconditional jump to L),
if t goto L (it t is true then jump to L),
if a relop b goto L (jump to L if a relop b is true),
Where L is the label of the next three-address instruction to be executed
t is a boolean variable
a and b are either variables or constants
INSTRUCTIONS FOR 3AC
INDEXED COPY INSTRUCTIONS:
FUNCTIONS:
a = b[i] (a is set to
func begin <name> (beginning of the
contents(contents(b)+contents(i)),
function),
where b is (usually) the base address of an array
func end (end of a function),
a[i] = b (ith location of array a is set to b)
param p (place a value parameter p on stack),
refparam p (place a reference parameter p on
stack),
call f, n (call a function f with n parameters), POINTER ASSIGNMENTS:
return (return from a function), a = &b (a is set to the address of b, i.e., a
points to b)
return a (return from a function with a value
a) *a = b (contents(contents(a)) is set to
contents(b))
a = *b (a is set to contents(contents(b)))
TRANSLATION OF A SIMPLE STATEMENT
CODE TEMPLATE FOR IF-ELSE LOOPS
Assumption: No short-circuit evaluation for E (i.e., no jumps within the intermediate code
for E)
If (E) S1 else S2
code for E (result in T)
if T≤ 0 goto L1 /* if T is false, jump to else part */
code for S1 /* all exits from within S1 also jump to L2 */
goto L2 /* jump to exit */
L1: code for S2 /* all exits from within S2 also jump to L2 */
L2: /* exit */
If (E) S
code for E (result in T)
if T≤ 0 goto L1 /* if T is false, jump to exit */
code for S /* all exits from within S also jump to L1 */
L1: /* exit */
CODE TEMPLATE FOR WHILE LOOPS
BACKPATCHING
Previous codes for Boolean expressions insert symbolic labels for jumps
It therefore needs a separate pass to set them to appropriate addresses
We can use a technique named backpatching to avoid this
We assume we save instructions into an array and labels will be indices in the
array
For nonterminal B we use two attributes B.truelist and B.falselist together with
following functions:
makelist(i): create a new list containing only I, an index into the array of instructions
Merge(p1,p2): concatenates the lists pointed 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 instruction on the list
pointed to by p
BACKPATCHING FOR BOOLEAN EXPRESSIONS
BACKPATCHING FOR BOOLEAN EXPRESSIONS
Annotated parse tree for x < 100 || x > 200 && x ! = y 100: if (x<100)
then goto _____
101: goto _____
102: id (x>200)
then goto _____
103: goto _____
104: if (x!=y) then
goto ____
105: goto ____
100: if (x<100) then goto _____
101: goto _____
102: id (x>200) then goto _____
103: goto _____
104: if (x!=y) then goto ____
105: goto ____