



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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)