R
Roedy Green
I poked for a short while around in the goldfish bowl book to see if I
could figure out what would be needed to change the JVM if an unsigned
byte type were added to java.
Here is what I noticed.
Arithmetic works on the stack with ints and long. I did not see any op
codes to do arithmetic on byte, char or short.
There is a byte array load (baload) but I did not see a corresponding
load for a single isolated byte (bload). I think single bytes must be
stored as full ints. There is an op code i2b that corrals a value into
the range -128..127. I believe this acts like a & 0xff followed by a
sign extend. I think signed bytes are stored with their high order
bits already in place! There is thus no need for byte sign extension
on load for isolated bytes.
I suspect char and short work the same way.
So to add unsigned byte support you would need an new unsignedbyte
array load (ubload) and an new i2ub, which just does a & 0xff. You
also need to add a code for unsigned byte method signatures in the
class files.
You could do it with even fewer changes to the JVM by the compiler
inserting and 0xff after every byte array load, and doing an iand 0xff
prior to an unsigned byte store, rather than the usual i2b.
I wonder if anyone more familiar with the JVM internals could confirm
this. If you are a newbie wanting to get your feet wet with
understanding the JVM, you could write some code and decompile it with
Javah to see how arithmetic with bytes, chars, ints and shorts and
longs is compiled.
see http://mindprod.com/jgloss/jasm.html
for information on how the JVM works.
could figure out what would be needed to change the JVM if an unsigned
byte type were added to java.
Here is what I noticed.
Arithmetic works on the stack with ints and long. I did not see any op
codes to do arithmetic on byte, char or short.
There is a byte array load (baload) but I did not see a corresponding
load for a single isolated byte (bload). I think single bytes must be
stored as full ints. There is an op code i2b that corrals a value into
the range -128..127. I believe this acts like a & 0xff followed by a
sign extend. I think signed bytes are stored with their high order
bits already in place! There is thus no need for byte sign extension
on load for isolated bytes.
I suspect char and short work the same way.
So to add unsigned byte support you would need an new unsignedbyte
array load (ubload) and an new i2ub, which just does a & 0xff. You
also need to add a code for unsigned byte method signatures in the
class files.
You could do it with even fewer changes to the JVM by the compiler
inserting and 0xff after every byte array load, and doing an iand 0xff
prior to an unsigned byte store, rather than the usual i2b.
I wonder if anyone more familiar with the JVM internals could confirm
this. If you are a newbie wanting to get your feet wet with
understanding the JVM, you could write some code and decompile it with
Javah to see how arithmetic with bytes, chars, ints and shorts and
longs is compiled.
see http://mindprod.com/jgloss/jasm.html
for information on how the JVM works.