Byte-code etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Byte-code etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

27 Ağustos 2023 Pazar

Byte-code/Bytecode - Integer Instructions

Giriş
iX Talimatları Integer ile çalışıldığını gösterir

Operand stack
Matematiksel işlemler ve metod çağrılarında kullanılır

Örnek
Elimizde şöyle bir kod olsun
int a = 1;
a++;
int b = a * a;
int c = b - a;
System.out.println(c);
Bytecode çıktısı şöyle
0: iconst_1
1: istore_1
2: iinc          1, 1
5: iload_1
6: iload_1
7: imul
8: istore_2
9: iload_2
10: iload_1
11: isub
12: istore_3
13: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
16: iload_3
17: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
20: return


iconst_X Instruction
Açıklaması şöyle
... the instruction iconst_1 is used to load a constant integer value of 1 onto the operand stack.
Örnek
iconst_1
Şu koda denk gelir
int a = 1;
iinc_X Instruction
Açıklaması şöyle
iinc instructs to increment an integer value. 
Örnek
iinc 1, 1
Şu koda denk gelir
a++;
Açıklaması şöyle
 The values 1, 1 specify the index of the variable to be incremented and the value by which it should be incremented, respectively.
iload_X Instruction - load an int value from a local variable #index
Bu instruction integer içindir. Farklı tipler için floaddload gibi instrunction'lar da var.

Toplama gibi bir işlem yapmak için değişkendeki değerin stack'a yazılması gerekir. iload_X bu işi yapar. Daha sonra iadd ile toplama yapılır. Ve istore_X ile sonuç değişkene yazılır. Şu kodda bu işlemleri görebiliriz
int a = ...;
int b = ...;
int c = a + b;
//Toplama işlemi şöyledir
iload_1
iload_2
iadd
istore_3
imul Instruction
Çarpma yapar

ireturn Instruction
Elimizde şöyle bir kod olsun
public static int sum(final int a, final int b) {
  return a + b;
}
Şu işlemleri görebiliriz
iload_0
iload_1
iadd
ireturn
isub Instruction
Çıkartma yapar. Açıklaması şöyle
This operation takes two integer values from the operand stack. It subtracts the integer value loaded last from the integer value loaded first. The resulting integer is loaded onto the operand stack.

istore_X Instruction
Açıklaması şöyle
Instructs to store the integer value that is currently at the top of the operand stack as a variable. It removes the top integer value from the operand stack and saves it in the local variable array at specified index
Örnek
istore_1
Şu koda denk gelir
a++;

23 Kasım 2020 Pazartesi

Byte-code/Bytecode

Giriş
Tüm byte-code instruction listesi burada. Bytecode JVM içindeki Interpreter tarafından kullanılır. Açıklaması şöyle
The execution engine has a few sub-components. 
- Interpreter
- Profiler
- Java Native Interface
- Native Interface Library
Byte-code Kelimesi Ne Anlama Gelir
Açıklaması şöyle. Her instruction 1 byte uzunluğunda. Uzantısı ".class" olan dosyalarda byte-code instruction'lar var
Interesting fact: byte-code is called byte-code, as each instruction in byte-code is of byte length, so that it can be loaded into the CPU cache, and in fact there were also java CPUs built!!! didn’t take-off
Söz Dizimi
Söz dizimi şöyledir
opcode (1 byte)      operand1 (optional)      operand2 (optional)      ...
astore_X Instruction - store a reference into a local variable #index
Bir değişkene değer atar. Tersi ise aload_X
Örnek
Elimizde şöyle bir kod olsun
class Outer {
  private class Inner {...}

  void foo() {
    Inner a = this.new Inner();
    Inner b = new Outer.Inner();
    Inner c = new Inner();       // Recommended way to write it
  }
}
Byte-code olarak şunu elde ederiz.
0: new           #7                  // class Outer$Inner
3: dup
4: aload_0
5: invokespecial #9                  // Method Outer$Inner."<init>":(LOuter;)V
8: astore_1

9: new           #7                  // class Outer$Inner
12: dup
13: aload_0
14: invokespecial #9                  // Method Outer$Inner."<init>":(LOuter;)V
17: astore_2

18: new           #7                  // class Outer$Inner
21: dup
22: aload_0
23: invokespecial #9                  // Method Outer$Inner."<init>":(LOuter;)V
26: astore_3
getstatic Instruction
Bir sınıfın static alanına erişir
Örnek
Elimizde şöyle bir kod olsun
public static void main(String[] args) {
  System.out.println("" == ".".substring(1));
}
Çıktı olarak şunu alırız
0: getstatic     #7            // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc           #13           // String
5: ldc           #15           // String .
7: iconst_1
8: invokevirtual #17           // Method java/lang/String.substring:(I)Ljava/lang/String;
11: if_acmpne     18
14: iconst_1
15: goto          19
18: iconst_0
19: invokevirtual #23          // Method java/io/PrintStream.println:(Z)V
22: return
iX Talimatları
Integer ile çalışıldığını gösterir. Integer Instructions yazısına taşıdım



invokedynamic  Instruction
Açıklaması şöyle
This instruction was added in Java 7 with the goal of better support for dynamic languages in the JVM, such as Groovy and JRuby. 
Java da ise Lambda için kullanılıyor. Açıklaması şöyle.
Java has introduced byte code instruction invokedynamic to construct the anonymous class and then  generate byte code for lambdas. So, In case of lambdas java generates the implementation class and generate byte code at runtime.
Açıklaması şöyle. Yani CallSite bir kere yapılır
In a nutshell, an invokedynamic invocation consists of two phases: looking up a CallSite and then invoking the MethodHandle the CallSite holds. If the same invokedynamic instruction is executed another time, CallSite from the initial lookup will be invoked.
invokestatic Instruction
Bir sınıfın static metodunu çağırır
Örnek
Elimizde şöyle bir kod olsun
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
byteBuffer.flip();
Java 11 byte-code çıktısı şöyledir. () karakterinden sonra metodun döndürdüğü tip görülebilir.
 0: bipush        64
 2: invokestatic  #19   // Method java/nio/ByteBuffer.allocate:(I)Ljava/nio/ByteBuffer;
 5: astore_1
 6: aload_1
 7: invokevirtual #25   // Method java/nio/ByteBuffer.flip:()Ljava/nio/ByteBuffer;
10: pop
invokevirtual Instruction
Bir sınıfın virtual metodunu çağırır