Docsity

Intermediate Code - Homework 3 | Compiler Construction | CSC 4351, Study notes of Computer Science

Toggle sidebar
bg1
HW 3: Intermediate Code
CSC 4351, Spring 2014 Due: 9 April 2014
1. Suppose we change the Tiger language to use Ada’s copy-in-copy-out parameter passing
mechanism. If a parameter is declared with the keywords in out, it uses copy-in-copy-out.
Otherwise, we use call-by-value as before. With copy-in-copy-out, the argument is
copied into the parameter variable on entering the function. On function exit, the
parameter is copied back out into the argument vari- able. Describe in detail which
changes (if any) are necessary to semantic analysis, escape analysis, activation records,
and the intermediate representation.
2. Draw the intermediate code trees for questions 7.1(f), 7.1(h), 7.2(g), 7.2(j) on p. 160.
7.1 SupposeacertaincompilertranslatesallexpressionsandsubexpressionsintoTree.Exptrees, and
does not use the Nx and Cx constructors to represent expressions in different ways. Draw a
picture of the IR tree that results from each of the following expressions. Assume all variables are
nonescaping unless specified otherwise.
e. a<b, which should be implemented by making an ESEQ whose left-hand side moves a 1 or 0
into some newly defined temporary, and whose right-hand side is the temporary.
f. if a then b else c, where a is an integer variable (true if 9= 0); this should also be translated using
an ESEQ.
h. if a<b then c:=a else c:=b, translated using the a<b tree from part (e) above; the whole
statement will therefore be rather clumsy and inefficient.
7.2 Translate each of these expressions into IR trees, but using the Ex, Nx, and Cx constructors as
appropriate. In each case, just draw pictures of the trees; an Ex tree will be a Tree Exp, an Nx tree
will be a Tree Stm, and a Cx tree will be a Stm with holes labeled true and false into which labels
can later be placed.
g. if a then b else c, where a is an integer variable (true if 9= 0). j. if a<b then c:=a else c:=b
HW 4: Basic Blocks
CSC 4351, Spring 2014 Due: 28 April 2014
1. Solve problems 8.1 and 8.2(c) on p. 174 in the textbook. For finding the rewrite rules for 8.1,
you might want to have a look at the code in Canon.Canon. When rewriting 8.2(c), indicate for
every step which rewrite rule you are using.
2. Solve problems 8.6 and 8.7 on p. 175 in the textbook.
pf3
pf4
pf5
Discount

On special offer

Partial preview of the text

Download Intermediate Code - Homework 3 | Compiler Construction | CSC 4351 and more Study notes Computer Science in PDF only on Docsity!

HW 3: Intermediate Code

CSC 4351, Spring 2014 Due: 9 April 2014

1. Suppose we change the Tiger language to use Ada’s copy-in-copy-out parameter passing

mechanism. If a parameter is declared with the keywords in out, it uses copy-in-copy-out.

Otherwise, we use call-by-value as before. With copy-in-copy-out, the argument is

copied into the parameter variable on entering the function. On function exit, the

parameter is copied back out into the argument vari- able. Describe in detail which

changes (if any) are necessary to semantic analysis, escape analysis, activation records,

and the intermediate representation.

2. Draw the intermediate code trees for questions 7.1(f), 7.1(h), 7.2(g), 7.2(j) on p. 160.

7.1 SupposeacertaincompilertranslatesallexpressionsandsubexpressionsintoTree.Exptrees, and

does not use the Nx and Cx constructors to represent expressions in different ways. Draw a

picture of the IR tree that results from each of the following expressions. Assume all variables are

nonescaping unless specified otherwise.

e. a<b, which should be implemented by making an ESEQ whose left-hand side moves a 1 or 0

into some newly defined temporary, and whose right-hand side is the temporary.

f. if a then b else c, where a is an integer variable (true if ̸ = 0); this should also be translated using

an ESEQ.

h. if a<b then c:=a else c:=b, translated using the a<b tree from part (e) above; the whole

statement will therefore be rather clumsy and inefficient.

7.2 Translate each of these expressions into IR trees, but using the Ex, Nx, and Cx constructors as

appropriate. In each case, just draw pictures of the trees; an Ex tree will be a Tree Exp, an Nx tree

will be a Tree Stm, and a Cx tree will be a Stm with holes labeled true and false into which labels

can later be placed.

g. if a then b else c, where a is an integer variable (true if ̸ = 0). j. if a<b then c:=a else c:=b

HW 4: Basic Blocks

CSC 4351, Spring 2014 Due: 28 April 2014

1. Solve problems 8.1 and 8.2(c) on p. 174 in the textbook. For finding the rewrite rules for 8.1,

you might want to have a look at the code in Canon.Canon. When rewriting 8.2(c), indicate for

every step which rewrite rule you are using.

2. Solve problems 8.6 and 8.7 on p. 175 in the textbook.

Homework 3 Solutions

I found a write-up for call-by-reference in my files. Here is the explanation for call-by-reference. For copy-in-copy-out, there are similar considerations, e.g., the argument must be a variable, except that there are no side-effects while the function is executing and the argument variable doesn't escape. -- Call-by-reference means that instead of copying the argument value, we pass a pointer to the argument. If the corresponding parameter is assigned to inside the function, the argument gets modified. Semantic analysis: There may be restrictions on what values can be passed by reference. Since one of the purposes of call-by-reference is that a parameter can be used as an output parameter, it may not make sense to pass a constant as argument. Some languages require that the argument for a call-by-reference parameter must be an l-value. An alternative would be that an r-value is copied into a temporary and that the address of the temporary is passed as argument. Semantic analysis needs to enforce such rules. There are no real changes to type checking. It may be useful, to type reference parameters as pointer types, but that's not necesary. Escape analysis: Every variable or record field that's passed by reference to another function escapes. If constants are copied into a temporary, the temporary escapes as well. The parameters themselves do not escape; they are pointers that could be kept in registers (unless they are passed to other functions or accessed from inner functions as non-local variables). Activation records: There are no changes here. We don't need any extra information in the activation records, we just need to make pointers out of parameters. Intermediate representation: The translation to IR must translate accesses to reference parameters as indirect accesses through a pointer. I.e., an additional MEM node must be added. For a var parameter x that can be kept in registers, the access becomes MEM(TEMP t). For a var parameter that escapes, the access becomes MEM(MEM(+(TEMP fp, CONST k))). For a call-by-reference argument, we must pass the address. E.g., if if a variable x is passed by reference, the expression for evaluating the argument is simply +(TEMP fp, CONST k), without a MEM node around. This forces us to distinguish between l-values and r-values in the translation to intermediate code and to convert l-values to r-values where necessary. If we allow arbitrary expressions to be passed by reference, we also need a conversion from r-values to l-values by storing the r-value in a temporary (which escapes) and by passing the temporary. For an r-value e as argument, we get ESEQ(MOVE(MEM(+(TEMP fp, CONST k)), e), +(TEMP fp, CONST k)), where k is the offset for the new temporary. Since this temporary escapes, it can't be in registers but must be in the frame.

SEQ(LABEL l2, SEQ(MOVE(TEMP t, c), LABEL l3)))))), TEMP t) 7.1(h): if a<b then c:=a else c:=b where a<b is converted to a boolean first For the boolean expression a<b, we get ESEQ(SEQ(CJUMP(LT, a, b, l1, l2), SEQ(LABEL l1, SEQ(MOVE(TEMP t1, CONST 1), SEQ(JUMP l3, SEQ(LABEL l2, SEQ(MOVE(TEMP t1, CONST 0), LABEL l3)))))), TEMP t1) Since every subexpression ends up as an Exp node, we need to make an Exp node out of the assignments. E.g., for the assignment c:=a, we get ESEQ(MOVE(c, a), CONST 0) where the CONST 0 is used just so there is a value. I.e., it's a no-op. Then for translating the if-expression itself, we don't know what values we got for the then- and the else-parts. We just use the same translation as for 7.1(f) above and introduce a temporary that holds the value of either the then-part or the else-part. ESEQ(SEQ(CJUMP(NE, ESEQ(SEQ(CJUMP(LT, a, b, l1, l2), SEQ(LABEL l1, SEQ(MOVE(TEMP t1, CONST 1), SEQ(JUMP l3, SEQ(LABEL l2, SEQ(MOVE(TEMP t1, CONST 0), LABEL l3)))))), TEMP t1), CONST 0, l4, l5), SEQ(LABEL l4, SEQ(MOVE(TEMP t2, ESEQ(MOVE(c, a), CONST 0)), SEQ(JUMP l6, SEQ(LABEL l5, SEQ(MOVE(TEMP t2, ESEQ(MOVE(c, b), CONST 0)), LABEL l6)))))), TEMP t2) 7.2(j): Same as above, but this time we use Nx nodes for then- and else-parts and make a Stm node for the whole if-expression. Again, the condition becomes easier, since we don't need convert into a boolean first. Assuming that the if-expression is used in an Nx context, the unNx method of IfThenElseExp will construct a simple sequence of statements without introducing any temporary for the then- and else-parts. Similarly, for the

condition we built a RelCx node, whose unCx method simply generates a single CJUMP. We end up getting the code SEQ(CJUMP(LT, a, b, l1, l2), SEQ(LABEL l1, SEQ(MOVE(c, a), SEQ(JUMP l3, SEQ(LABEL L2, SEQ(MOVE(c, b), LABEL l3)))))) instead of the tangle above. Homework 4 Solutions 8.1(a) SEQ(s, MOVE(TEMP t, e)) (b) SEQ(s, MOVE(MEM(e1), e2)) (c) SEQ(MOVE(TEMP t, e1), SEQ(s, MOVE(MEM(TEMP t), e2)) (d) SEQ(s, EXP(e)) (e) SEQ(s, EXP(CALL(e, args)) (f) SEQ(s, MOVE(TEMP t, CALL(e, args))) (g) SEQ(MOVE(TEMP t), e1), SEQ(MOVE(TEMP t2, e2), SEQ(s, CALL(TEMP t1, [TEMP t2, e3, e4])))) 8.2(c) ESEQ(SEQ(MOVE(TEMP t1, CALL(NAME f, [TEMP x])), SEQ(MOVE(TEMP x, CONST 0), MOVE(TEMP t2, CALL(NAME g, [TEMP x])))), BINOP(PLUS, TEMP t1, TEMP t2)) The NAME g does not have to be moved out, since NAME g and MOVE(TEMP x, CONST 0) commute. 8.6/8.7 It turns out that the trace schedule does not reorder the basic blocks, so I'll show the basic blocks in the final order directly: LABEL(L1) m := 0 v := 0 JUMP(L2) /* can be deleted / LABEL(L2) CJUMP(>=, v, n, L9, L3) LABEL(L3) r := v s := 0 JUMP(L4) / can be deleted */ LABEL(L4) CJUMP(<, r, n, L6, L5) LABEL(L5) v := v + 1 JUMP(L2)