Java Reference
In-Depth Information
Notice several things here. First, notice that the stack offsets take account of i being a one-
word int , and d and e being two-word double s. Second, notice the mix of types an iload
instruction is used to load the integer and a dload instruction is used to load the double-
precision value. The arithmetic is done in double precision, using the dmul instruction.
Therefore, the integer i must be promoted (converted to a more precise representation) to
a double-precision value using the i2d instruction.
In the assignment and arithmetic operations, analyze() must be modified for dealing
with this mixed arithmetic. In the case of a mix of int and double , the rule is pretty simple:
convert the int to a double . One will want to consult the Java Language Specification
[Gosling et al., 2005], especially Chapters 5 and 15. The rules are pretty easy to ferret out
for just int s and double s.
Exercise 5.21. Add the primitive type long to j--, adding it to your compiler and testing it
thoroughly. When adding additional primitive types (to int and double ), the rules become
more complicated. One must worry about when to do conversions and how to do them. One
must follow the Java Language Specification [Gosling et al., 2005].
Once one has more than two numeric types to deal with, it is probably best to re-think
the conversion scheme and build a simple framework for deciding how to promote one type
to another. There are fifteen primitive type conversion instructions available to us in the
JVM, and we will want to make effective use of them.
Exercise 5.22. Add the primitive type float to j--, adding it to your compiler and testing
it thoroughly.
Exercise 5.23. The JVM provides instructions making it relatively straightforward to add
synchronized-statement. Study the Java Language Specification [Gosling et al., 2005] to
determine what it would take to implement the synchronized-statement in your compiler.
Then add the synchronized-statement to j--, adding it to your compiler and testing it
thoroughly.
Exercise 5.24. (Small project) Study the Java Language Specification [Gosling et al., 2005]
to determine what it would take to implement nested classes in your compiler. Then add
nested classes to j--, adding it to your compiler and testing them thoroughly.
In implementing nested classes, one must take into account and enforce both the scope
rules and access rules. The j-- compiler includes a little helpful machinery as it stands, in
the form of a class context entry in the symbol table where nested classes might be stored.
Exercise 5.25. (Small project) Study the Java Language Specification [Gosling et al., 2005]
to determine what it would take to implement enum types in your compiler. Then add enum
types to j--, adding it to your compiler and testing them thoroughly.
The implementation of enum types simply requires enumerating the values supplied in
the type declaration and storing these values along with their enumeration; of course, one
must take into account explicit assignation in the declaration.
Exercise 5.26. (Large project) Study the Java Language Specification [Gosling et al.,
2005] and other articles to determine what it would take to implement generic types in your
compiler. Then add generic types to j--, adding it to your compiler and testing it thoroughly.
Implementing generic types is a major project. Generics are described in [Bracha, 2004].
Exercise 5.27. (Large project) Add implicit type conversions to your implementation of
j--, implementing them in your compiler and testing them thoroughly.
What makes this dicult is that it leads to complexity in the identification and selection
from applicable methods in resolving method invocation. There are hints to an implemen-
tation in the steps outlined in [Gosling et al., 2005].
 
Search WWH ::




Custom Search