简体   繁体   English

bipush在JVM中如何工作?

[英]How does bipush work in JVM?

I understand iload takes in integers -1 to 5, but how can you extend to higher numbers using a bipush instruction? 我知道iload接受的整数是-1到5,但是如何使用bipush指令扩展到更高的数字? How is the specific integer being stored with the bytecode? 特定的整数如何与字节码一起存储?

I think you're looking for section 2.11 of the JVMS , which deals with instruction representation. 我认为您正在寻找JVMS的2.11节 ,其中涉及指令表示。 In particular, it uses the obvious order: the opcode, immediately followed by operands in order, big-endian (as all Java representations). 特别是,它使用明显的顺序:操作码,紧随其后的是操作数,顺序为big-endian(作为所有Java表示形式)。 In the case of bipush , this would be the byte 0x10 followed by the literal value. bipush的情况下,这将是字节0x10,后跟文字值。

There's several different instructions that can be used to push an integer constant. 有几种不同的指令可用于推送整数常量。

The smallest is the iconst_* instructions. 最小的是iconst_ *指令。 These are only a single byte, because the value is encoded in the opcode itself. 这些只是一个字节,因为该值是在操作码本身中编码的。 iconst_1, iconst_2, etc. are different opcodes. iconst_1,iconstst_2等是不同的操作码。 iconst_5 for example would be encoded as the byte 08 . 例如, iconst_5将被编码为字节08

Note: iload is a completely unrelated instruction used for loading the value of a local variable. 注意: iload是完全不相关的指令,用于加载局部变量的值。 You must have been thinking of iconst_*. 您一定在想过icont_ *。

Next is bipush , which can push a constant between -128 and 127. This instruction is two bytes long - the first byte is the opcode, and the second byte is a signed 8 bit integer. 接下来是bipush ,可以在-128到127之间推送一个常数。该指令的长度为2个字节-第一个字节为操作码,第二个字节为带符号的8位整数。 You can even use it to push constants in the range -1 to 5, although doing so would take up more space in the classfile than necessary. 您甚至可以使用它在-1到5范围内推送常量,尽管这样做会在类文件中占用不必要的空间。 For example, bipush 5 would be encoded as 10 05 . 例如, bipush 5将被编码为10 05 (0x10 is the opcode for bipush) (0x10是bipush的操作码)

Next is sipush , which is the same except that it stores a 16 bit constant instead of an 8 bit constant, and hence the instruction is three bytes long. 接下来是sipush ,除了它存储一个16位常量而不是一个8位常量之外,其他都是相同的,因此该指令的长度为3个字节。 The opcode for sipush is 0x11, so sipush 5 would be encoded as the three byte sequence 11 00 05 . sipush的操作码为0x11,因此sipush 5将被编码为三个字节的序列11 00 05

You might wonder how integer constants which don't fit in 16 bits are stored. 您可能想知道如何存储不适合16位的整数常量。 In this case, the compiler creates entries in a separate section of the classfile called the constant pool, and then uses the ldc or ldc_w instruction to refer to the constant pool entry. 在这种情况下,编译器将在类文件的单独部分(称为常量池)中创建条目,然后使用ldcldc_w指令引用常量池条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM