Java Reference
In-Depth Information
intdigitClose(char);
Code:
Stack=1,Locals=2,Args_size=2
0:iload_1
1:tableswitch{//48to51
48:34;
49:32;
50:38;
51:36;
default:38}
32:iconst_1
33:ireturn
34:iconst_0
35:ireturn
36:iconst_3
37:ireturn
38:iconst_m1
39:ireturn
Notice a sequential table is produced. Of course, the entry for character '2' (Unicode 50)
maps to location 38 (the default case) because it is not one of the explicit cases.
So our compiler must construct a list of value-label pairs, mapping a case value to a
label we will emit to mark the location of code to branch to. We then sort that list on
the case values and decide, based on sparseness, whether to use a tableswitch instruction
(not sparse) or a lookupswitch (sparse) instruction. CLEmitter provides a method for each
choice.
CLEmitter 's addTABLESWITCHInstruction() method provides for several arguments: a
default label, a lower bound on the case values, an upper bound on the case values, and
a sequential list of labels.
CLEmitter 's addLOOKUPSWITCHInstruction() method provides for a different set of
arguments: a default label, a count of the number of value-label pairs in the table, and a
TreeMap that maps case values to labels.
Of course, our compiler must decide which of the two instructions to use.
The next three exercises deal with exception handling. Exception handling in Java is
captured by the try-catch-finally and throws-statement. Additionally, there is the throws-
clause, which serves to help the compiler ensure that there is a catch for every exception
thrown.
To illustrate the JVM code that is generated for exceptions, consider the following class
declaration:
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.lang.IndexOutOfBoundsException;
importjava.lang.System;
importjava.io.FileNotFoundException;
importjava.io.IOException;
publicclassCopy{
privatestaticfinalintEOF=-1; //endoffilecharacterrep.
publicstaticvoidmain(String[]args)throwsIOException{
FileReaderinStream =null;
FileWriteroutStream=null;
intch;
try{
//openthefiles
 
Search WWH ::




Custom Search