Java Reference
In-Depth Information
if(outStream!=null){
outStream.close();
}
}
catch(Exceptione){
System.err.println("Unabletocloseoutputstream.");
}
}
Execute javap on the class files produced by both javap and by your extensions to j--.
Make sure you are generating the proper JVM code.
Exercise 5.19. Study the Java Language Specification [Gosling et al., 2005] to determine
what it would take to implement the assert-statement in your compiler. Then add the
assert-statement to j--, adding it to your compiler and testing it thoroughly.
The next three exercises involve adding primitive types to our compiler. (There is no
reason why we cannot add built-in reference types such as matrices for which we can overload
the arithmetic operators; but doing so would go against the spirit of Java.) And, because
the primitive types in Java that are not already in j-- are numeric types, the new types
must work in concert with the current types.
Exercise 5.20. Add the primitive type double to j--, adding it to your compiler and test-
ing it thoroughly. Adding double-precision floating-point numbers to j-- introduces several
wrinkles:
1. Arithmetic requires new JVM instructions, for example, dadd .
2. Each double-precision floating-point number occupies two words. This requires that
the offset be incremented by 2 each time a double variable is declared.
3. Mixed arithmetic introduces the need for implicit conversion.
For example, consider the following contrived method, where d and e are double-precision
variables and i is an integer variable:
voidfoo(){
inti=4;
doubled=3.0;
doublee=5.0;
e=d*i;
}
The javac compiler produces the following JVM code:
voidfoo();
Code:
Stack=4,Locals=6,Args_size=1
0: iconst_4
1: istore_1
2: ldc2_w #2;//double3.0d
5: dstore_2
6: ldc2_w #4;//double5.0d
9: dstore 4
11:dload_2
12:iload_1
13:i2d
14:dmul
15:dstore 4
17:return
 
Search WWH ::




Custom Search