[ Pobierz całość w formacie PDF ]
.This means you couldchoose to interpret the Java bytecodes, creating a system similar to the Java or HotJavaexecutables.Or, you could recompile the incoming Java code into native machine format tobenefit from native code performance.If you really need to produce the best possible speed,you could even implement the JVM in silicon.Of course, it would then be a JM rather than aJVM.The Instruction SetThe instruction set for the JVM is exactly equivalent to the instruction set for a CPU.Whenyou compile Java source code into binary, you are in essence creating an assembly languageprogram just as in C.Each instruction in Java consists of an opcode followed by an optionaloperand.Example opcodes include the following:Instructions for loading integers from memory (iload loads an integer)Managing arrays (anewarray allocates a new array)Logical operators (and logically ands two integers)Flow control (ret returns from a method call).Each opcode is represented by an 8-bit number, followed by varying length operands.Theseoperands give the needed data for each opcode, such as where to jump or what number to usein a computation.Many opcodes do not have any operands.In computing, it is typical to align all opcodes and operands to 32- or 64-bit words.Thisenables the machine to move through the code in constant jumps, knowing exactly where thenext instruction will be.Because the opcodes are only eight bits and the operands vary in size,however, aligning to anything larger than eight bits would waste space (see fig.12.2).Thewasted space would be a function of the average operand size and how much larger thebytecode alignment was.Deciding that compactness was more important than the perfor-mance hit incurred, the Java designers specifically chose this method.Operands are often more than 8 bits long and need to be divided into two or more bytes.TheJVM uses the big endian encoding scheme, in which the larger order bits are stored in thelower ordered memory spaces.This is the standard for Motorola and other RISC chips.Intelchips, however, use little endian encoding, placing the least significant bits in the lowestmemory address.The two methods are compared in table 12.2.Java Security 7218 bit OPCODE OPERAND OPCODE OPERAND OPCODE OPERANDFigure 12.2An 8-bit byte alignment32 bit OPCODE OPERANDversus a 32- or 64-bit bytealignment.64 bit OPCODEwasted spaceTable 12.2Big versus Little Endian EncodingMemory Address 0 Memory Address 1Big EndianByte 1 * 256 Byte 2Little EndianByte 1 Byte 2 * 256The differences can be confusing when trying to move data between two opposing systems thatrequire larger than 8-bit fragments to be encoded their way.The instruction set lends a great amount of functionality to the JVM and is specificallydesigned as an implementation of the Java language.This includes instructions for invokingmethods and monitoring multithreading systems.The 8-bit size of the opcode limits thenumber of instructions to 256, and there are already 160 opcodes that can be used.It isunlikely that this number will ever rise, unless future advances in hardware cannot be managedunder the current JVM specification.The RegistersAll processors have registers that hold information that the processor uses to store the currentstate of the system.Each processor type has different numbers of registers.The more registers aprocessor has, the more items it can deal with quickly, without having to refer to the stack, orglobal memory, which would result in a reduction in performance.Because of the widedifference in register variables, it was decided that Java would not have very many.If it hadmore than any processor it was being ported to, those CPUs would take enormous perfor-mance penalties when attempting to mimic the register states in regular memory.Therefore,the register set was limited to the following four registers:pc.Program counteroptop.Pointer to top of the operand stack722 Part IV: Modern Concernsframe.Pointer to current execution environmentvars.Pointer to the first (0th) local variable of the current execution environmentEach of these registers is 32 bits wide, and some of them might not need to be used in aspecific implementation.The program counter (pc) keeps track of where the program is in execution.This register doesnot need to be used if recompiling into native code.The optop, frame, and vars registers holdpointers to areas in the Java stack, which is discussed in the next section.The Java StackThe Java stack is the principal storage method for the JVM, which is considered a stack-basedmachine.When the JVM is given the bytecodes of a Java application, it creates a stack framefor each method of a class that holds information about its state.Each frame holds three kindsof information:Local variablesExecution environmentOperand stackLocal VariablesThe local variables in a Java stack frame are an array of 32-bit variables, the beginning of whichis marked by the vars register.This effectively is a large store for method variables.When theyare needed in the computation of an instruction, they can be loaded onto and stored from theoperand stack
[ Pobierz całość w formacie PDF ]