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

Hiç yorum yok:

Yorum Gönder