As part of one of my school project I need to dig into java bytecode. I started to write simple programs and use javap
utility to view generated bytecode and I have one question concerning *ipush
instructions.
When I view bytecode of this code:
public class Main{
public static void main(String []args){
int a;
a=5;
a=a+32765;
}
}
I am getting
public class Main
SourceFile: "Main.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#12 // java/lang/Object."<init>":()V
#2 = Class #13 // Main
#3 = Class #14 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 main
#9 = Utf8 ([Ljava/lang/String;)V
#10 = Utf8 SourceFile
#11 = Utf8 Main.java
#12 = NameAndType #4:#5 // "<init>":()V
#13 = Utf8 Main
#14 = Utf8 java/lang/Object
{
public Main();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: iconst_5
1: istore_1
2: iload_1
3: sipush 32765
6: iadd
7: istore_1
8: return
LineNumberTable:
line 4: 0
line 5: 2
line 6: 8
}
and when I swap 32765 for 32769 which is not in the range of short I am getting
public class Main
SourceFile: "Main.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#13 // java/lang/Object."<init>":()V
#2 = Integer 32769
#3 = Class #14 // Main
#4 = Class #15 // java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Utf8 LineNumberTable
#9 = Utf8 main
#10 = Utf8 ([Ljava/lang/String;)V
#11 = Utf8 SourceFile
#12 = Utf8 Main.java
#13 = NameAndType #5:#6 // "<init>":()V
#14 = Utf8 Main
#15 = Utf8 java/lang/Object
{
public Main();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: iconst_5
1: istore_1
2: iload_1
3: ldc #2 // int 32769
5: iadd
6: istore_1
7: return
LineNumberTable:
line 4: 0
line 5: 2
line 6: 7
}
so now the number is stored in constant pool. I know that there is not instruction ipush
which I could use to push integer constant to stack, but I am wondering why there is no such instruction?
Based on simple CISC design, they tried to make each instruction as short as possible (in bytes) Instructions which use 32-bit or 64-bit constants are easier/shorter to refer to a constant table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With