Previous    Next

## EXERCISES

• 7.1 Suppose a certain compiler translates all statements and expressions into Tree.Exp trees, 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 MiniJava statements and expressions.

1. a+5

2. b[i+1]
3. 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.
4. a = x+y; which should be translated with an EXP node at the top.
5. if (a<b) c=a; else c=b; translated using the a<b tree from part (c) above; the whole statement will therefore be rather clumsy and inefficient.
6. if (a<b) c=a; else c=b; translated in a less clumsy way.
• 7.2 Translate each of these MiniJava statements and 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, anda Cx tree will be a Stm with holes labeled trueand falseinto which labels can later be placed.
1. a+5;

2. b[i+1]=0;
3. while (a<0) a=a+1;
4. a<b moves a 1 or 0 into some newly defined temporary, and whose right-hand side is the temporary.
5. a = x+y;
6. if (a<b) c=a; else c=b;
• 7.3 Using the C compiler of your choice (or a compiler for another language), translate some functions to assembly language. On Unix this is done with the -S option to the C compiler.

Then identify all the components of the calling sequence (items 1-11), and explain what each line of assembly language does (especially the pseudoinstructions that comprise items 1 and 11). Try one small function that returns without much computation (a leaffunction), and one that calls another function before eventually returning.

• 7.4 The Tree intermediate language has no operators for floating-point variables. Show how the language would look with new binops for floating-point arithmetic, and new relops for floating-point comparisons. You may find it useful to introduce a variant of MEM nodes to describe fetching and storing floating-point values.
• *7.5 The Tree intermediate language has no provision for data values that are not exactly one word long. The C coding language has signed and unsigned integers of several sizes, with conversion operators among the different sizes. Augment the intermediate language to accommodate several sizes of integers, with conversions among them.

Hint: Do not distinguish signed values from unsigned values in the intermediate trees, but do distinguish between signed operators and unsigned operators. See also Fraser and Hanson [1995], Sections 5.5 and 9.1.

 JaVa Previous    Next