Slide 1

Slide 1 text

JVMアセンブリ⾔語 2017೥7݄20೔ @takesection

Slide 2

Slide 2 text

ࣗݾ঺հ ঙ࢘ॏथ Java/ScalaΤϯδχΞ Arduino΍PICͷΑ͏ͳ૊ࠐܥͷASMɺCɺC++΋޷͖

Slide 3

Slide 3 text

JVMͷಛ௃ εέʔϥϏϦςΟ ϚϧνOS ϚϧνϓϥοτϑΥʔϜ ελοΫϚγϯ

Slide 4

Slide 4 text

ελοΫݴޠ FORTH, PostScript %! % PostScript 100 200 moveto 300 600 lineto 500 200 lineto stroke

Slide 5

Slide 5 text

ϦΞϧͳCPUͷ৔߹ɺPC(Program Counter)ɺStack Pointerɺ RegisterɺALU( Arithmetic Logic Unit)Λ͍࣋ͬͯΔ

Slide 6

Slide 6 text

ελοΫͱ͸

Slide 7

Slide 7 text

ελοΫσʔλߏ଄͸ɺ αϒϧʔνϯίʔϧ΍ׂ ࠐ࣌ͷঢ়ଶΛอଘ͢Δͷ ʹద͍ͯͯ͠ݹ͔Β࢖Θ Ε͖ͯͨߏ଄ɻ

Slide 8

Slide 8 text

αϒϧʔνϯίʔϧ loop: call suba jmp loop; ᶃ suba: call subb ret; ᶄ subb: ret ᶃ call suba ᶄ call subb ᶃ ᶄ ret(subb)→ ᶃ ᶃ ret(suba) →

Slide 9

Slide 9 text

JVMͷσʔλྖҬ The pc Register - ֤εϨου୯ҐͰอ࣋͞ΕΔ Java Virtual Machine Stacks - εϨου͸εϨουͷੜ੒ͱಉ࣌ʹੜ੒͞ΕΔϓ ϥΠϕʔτͳJava Virtual Machine StackΛ࣋ͭɻ͜ͷελοΫʹ͸Frame͕֨ೲ͞ΕΔ Run-Time Constant Pool - ࣮ߦ࣌ίϯελϯτɾϓʔϧ Method Area Heap Native Method Stacks

Slide 10

Slide 10 text

FRAME ϝιου͕ىಈ͞ΕΔͨͼʹੜ੒͞Εɺىಈ͞Εͨϝιουͷऴྃʢਖ਼ৗɾ ్தऴྃʹؔ܎ͳ͘ʣͰഁغ͞ΕΔ Local Variables - ϩʔΧϧม਺΍αϒϧʔνϯίʔϧͷΞυϨεΛอ࣋͢Δ Operand Stacks - Φϖίʔυ͸ԋࢉର৅ͷ஋ΛLIFOͷελοΫΛओʹ࢖༻͢Δ Dynamic Linking - ࣮ߦ࣌ίϯελϯτɾϓʔϧ΁ͷࢀরΛอ࣋͢Δ Normal Method Invocation Completion Abrupt Method Invocation Completion

Slide 11

Slide 11 text

ελοΫͷجຊతૢ࡞ PUSH POP DUP SWAP

Slide 12

Slide 12 text

PUSH a a b push a push b

Slide 13

Slide 13 text

POP a a b pop pop

Slide 14

Slide 14 text

DUP a a push a dup a

Slide 15

Slide 15 text

SWAP a swap b b a

Slide 16

Slide 16 text

Slide 17

Slide 17 text

int add(int a, int b) { return a + b; } a 00: public class Foo { 01: public Foo(); 02: Code: 03: 0: aload_0 04: 1: invokespecial #1 05: 4: return 06: 07: static int add(int, int); 08: Code: 09: 0: iload_0 10: 1: iload_1 11: 2: iadd 12: 3: ireturn 13: 14: } 0 1 int int

Slide 18

Slide 18 text

int add(int a, int b) { return a + b; } 00: public class Foo { 01: public Foo(); 02: Code: 03: 0: aload_0 04: 1: invokespecial #1 05: 4: return 06: 07: static int add(int, int); 08: Code: 09: 0: iload_0 10: 1: iload_1 11: 2: iadd 12: 3: ireturn 13: 14: } a 0 1 int int b

Slide 19

Slide 19 text

int add(int a, int b) { return a + b; } 00: public class Foo { 01: public Foo(); 02: Code: 03: 0: aload_0 04: 1: invokespecial #1 05: 4: return 06: 07: static int add(int, int); 08: Code: 09: 0: iload_0 10: 1: iload_1 11: 2: iadd 12: 3: ireturn 13: 14: } 0 1 int int

Slide 20

Slide 20 text

int add(int a, int b) { return a + b; } 00: public class Foo { 01: public Foo(); 02: Code: 03: 0: aload_0 04: 1: invokespecial #1 05: 4: return 06: 07: static int add(int, int); 08: Code: 09: 0: iload_0 10: 1: iload_1 11: 2: iadd 12: 3: ireturn 13: 14: } a + b 0 1 int int

Slide 21

Slide 21 text

int add(int a, int b) { return a + b; } Note: intܕͷreturn(ireturn)͸ɺελοΫ ͔Βint஋Λpop͠ɺݺͼग़͠ଆͷ ϑϨʔϜʹpush͢Δ 00: public class Foo { 01: public Foo(); 02: Code: 03: 0: aload_0 04: 1: invokespecial #1 05: 4: return 06: 07: static int add(int, int); 08: Code: 09: 0: iload_0 10: 1: iload_1 11: 2: iadd 12: 3: ireturn 13: 14: }

Slide 22

Slide 22 text

INVOKE OVERVIEW invokevirtual - C++ʹ͓͚Δόʔνϟϧϝιουͷݺͼग़͠ͱಉ༷ʹɺର ৅Ϋϥεͷద੾ͳϝιουΛىಈ invokeintetface - ΠϯλʔϑΣʔεϝιουΛىಈ͢Δ invokedynamic - ಈతͳΫϥεੜ੒ͱΠϯελϯεੜ੒Λ࣮ߦ͢Δ invokestatic - Ϋϥε(static)ϝιουΛىಈ invokespecial - εʔύʔΫϥεɺϓϥΠϕʔτϝιουɺΠϯελϯεॳ ظԽϝιου()Λىಈ

Slide 23

Slide 23 text

ྫ֎ catch΍finally͕࣮ߦ͢Δϋϯυϥʔͷ։࢝pcΞυϨε͸classϑΝΠϧத ͷCodeΞτϦϏϡʔτʹఆٛ͞ΕΔexception_table഑ྻʹهड़͞ΕΔ Java Virutual Machine͸νΣοΫྫ֎ͱϥϯλΠϜྫ֎΍ΤϥʔΛ۠ผ͠ ͍ͯͳ͍ɺ·ͨΫϥεϑΝΠϧϩʔυ࣌ʹ΋͜ͷ͜ͱʹΑΔνΣοΫ ΛߦΘͳ͍ɻ͕ͨͬͯ͠ɺthrows΍catch͕ͳ͍৔߹Ͱ΋ͲΜͳྫ֎Λ εϩʔͯ͠΋໰୊͸ͳ͍ɻScala͸ͦͷͨΊthrowsએݴʹ͋ͨΔ΋ͷ͕ ͳ͍(31.2.2 ౤͛ΒΕͨྫ֎ʰScalaεέʔϥϒϧϓϩάϥϛϯάୈ3 ൛ʱ)

Slide 24

Slide 24 text

JSR, JSR_W, RET jsr, jsr_w - ϝιου಺ͷαϒϧʔνϯίʔϧͷͨΊͷΠϯε τϥΫγϣϯ(returnAddressܕΛOperand Stacksʹpush͢Δ) ret - αϒϧʔνϯίʔϧ͔Βͷ෮ؼͷͨΊͷΠϯετϥΫ γϣϯ(returnAddressΛLocal Variables͔Βऔಘ͢Δ) finallyઅͷ࣮૷தͰ͔ͭΘΕ͍ͯΔ

Slide 25

Slide 25 text

ελοΫϚγϯͰѻ͑Δܕ 32bitͷ஋Λѻ͏Category 1ͱ64bitͷ஋Λѻ͏Category 2͕͋ Δ ΦϒδΣΫτͷΠϯελϯε͸Category 1ͷreferenceܕͰද ͞ΕΔ

Slide 26

Slide 26 text

࣮ࡍͷܕ (ACTUAL TYPE) ܭࢉ্ͷܕ (COMPUTATIONAL TYPE) CATEGORY boolean int 1 byte int 1 char int 1 short int 1 int int 1 float float 1 reference reference 1 returnAddress returnAddress 1 long long 2 double double 2

Slide 27

Slide 27 text

NEW new - ৽ͨͳΦϒδΣΫτΛੜ੒͢Δ(͜ͷΠϯετϥΫ γϣϯ͸৽ͨͳΠϯελϯεΛ׬શʹੜ੒͢Δ΋ͷͰ͸ͳ ͍)

Slide 28

Slide 28 text

഑ྻ ഑ྻͷੜ੒͸ΠϯετϥΫγϣϯͰಛผѻ͍͞Ε͍ͯΔ (anewarray, newarray, multianewarray)

Slide 29

Slide 29 text

Ξηϯϒϥίʔυ

Slide 30

Slide 30 text

package com.pigumer; public class Foo { static int add(int a, int b) throws java.lang.RuntimeException { return a + b; } void foo() { } public void bar(int a, int b) { try { add(a, b); } finally { foo(); } } }

Slide 31

Slide 31 text

Compiled from "Foo.java" public class com.pigumer.Foo { public com.pigumer.Foo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Foo; static int add(int, int) throws java.lang.RuntimeException; Code: 0: iload_0 1: iload_1 2: iadd 3: ireturn LineNumberTable: line 6: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I void foo(); Code: 0: return LineNumberTable: line 10: 0 LocalVariableTable: Start Length Slot Name Signature 0 1 0 this Lcom/pigumer/Foo;

Slide 32

Slide 32 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I }

Slide 33

Slide 33 text

ADD(ྫࢀর) static int add(int, int) throws java.lang.RuntimeException; Code: 0: iload_0 1: iload_1 2: iadd 3: ireturn LineNumberTable: line 6: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I

Slide 34

Slide 34 text

ίϯετϥΫλ public com.pigumer.Foo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Foo;

Slide 35

Slide 35 text

public com.pigumer.Foo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Foo; reference 0 reference

Slide 36

Slide 36 text

public com.pigumer.Foo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Foo; 0 reference #1 = Methodref #5.#27 // java/lang/Object."":()V

Slide 37

Slide 37 text

public com.pigumer.Foo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Foo; 0

Slide 38

Slide 38 text

STATICϝιουͷ࣮ߦ

Slide 39

Slide 39 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } int b int a reference 0 1 2 int a

Slide 40

Slide 40 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } int b int a reference 0 1 2 int a int b

Slide 41

Slide 41 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } #2 = Methodref #4.#28 // com/pigumer/Foo.add:(II)I int b int a reference 0 1 2

Slide 42

Slide 42 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } int b int a reference 0 1 2 int a + b #2 = Methodref #4.#28 // com/pigumer/Foo.add:(II)I

Slide 43

Slide 43 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } int b int a reference 0 1 2 public void bar(int a, int b) { try { add(a, b); } finally { foo(); } }

Slide 44

Slide 44 text

VIRTUALϝιουͷ࣮ߦ InvokeVirtual

Slide 45

Slide 45 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } b a this 0 1 2 this

Slide 46

Slide 46 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } b a this 0 1 2 3 = Methodref #4.#29 // com/pigumer/Foo.foo:()V

Slide 47

Slide 47 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } b a this 0 1 2

Slide 48

Slide 48 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } 0 1 2

Slide 49

Slide 49 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } public void bar(int a, int b) { try { add(a, b); } finally { foo(); } }

Slide 50

Slide 50 text

ྫ֎ɺFINALLY exception_table[]ʹࢦఆ͞ΕͨΞυϨε಺ͷίʔυͰεϩʔ ͞Εͨྫ֎ͷϋϯυϥʔͷࢦఆʹΑΓྫ֎࣌ͷίʔυ͕࣮ ߦ͞ΕΔɻ ※ end_pc͕ࣗ਎ͷ஋ΛؚΜͰ͍ͳ͍ͷ͸ɺJava Virtual Machineͷઃܭʹ͓͚Δྺ࢙తͳաͪͱ͞Ε͍ͯΔɻ

Slide 51

Slide 51 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } public void bar(int a, int b) { try { add(a, b); } finally { foo(); } } b a this 0 1 2 Throwable 3

Slide 52

Slide 52 text

Throwable public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } b a this 0 1 2 3

Slide 53

Slide 53 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } Throwable b a this 0 1 2 3 this

Slide 54

Slide 54 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } Throwable b a this 0 1 2 3 3 = Methodref #4.#29 // com/pigumer/Foo.foo:()V

Slide 55

Slide 55 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } Throwable b a this 0 1 2 3 Throwable

Slide 56

Slide 56 text

public void bar(int, int); Code: 0: iload_1 1: iload_2 2: invokestatic #2 // Method add:(II)I 5: pop 6: aload_0 7: invokevirtual #3 // Method foo:()V 10: goto 20 13: astore_3 14: aload_0 15: invokevirtual #3 // Method foo:()V 18: aload_3 19: athrow 20: return Exception table: from to target type 0 6 13 any LineNumberTable: line 14: 0 line 16: 6 line 17: 10 line 16: 13 line 18: 20 LocalVariableTable: Start Length Slot Name Signature 0 21 0 this Lcom/pigumer/Foo; 0 21 1 a I 0 21 2 b I } 0 1 2 3

Slide 57

Slide 57 text

ྫ֎ϋϯυϥʔ The Java Virtual Machine Specification, Java SE 8 Edition ʹهࡌ͞ Ε͍ͯΔྫ

Slide 58

Slide 58 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any void tryFinally() { try { tryItOut(); } finally { wrapItUp(); } } 0 1 2 this

Slide 59

Slide 59 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any 0 1 2 this Throwable

Slide 60

Slide 60 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any 0 1 2 Throwable this

Slide 61

Slide 61 text

returnAddress 0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any Throwable 0 1 2 this

Slide 62

Slide 62 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any returnAddress Throwable 0 1 2 this

Slide 63

Slide 63 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any this returnAddress Throwable 0 1 2 this

Slide 64

Slide 64 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any returnAddress Throwable 0 1 2 this

Slide 65

Slide 65 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any returnAddress Throwable 0 1 2 this

Slide 66

Slide 66 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any Throwable returnAddress Throwable 0 1 2 this

Slide 67

Slide 67 text

0 aload_0 1 invokevirtual #6 4 jsr 14 7 return 8 astore_1 9 jsr 14 12 aload_1 13 athrow 14 astore_2 15 aload_0 16 invokevirtual #5 19 ret 2 Exception table: From To Target 0 4 8 any 0 1 2

Slide 68

Slide 68 text

NEW

Slide 69

Slide 69 text

package com.pigumer; public class Bar { public static void main(String[] args) { Foo foo = new Foo(); Baz baz = a -> a + 1; System.out.println(baz.baz(3)); } }

Slide 70

Slide 70 text

Compiled from "Bar.java" public class com.pigumer.Bar { public com.pigumer.Bar(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/pigumer/Bar; public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz;

Slide 71

Slide 71 text

private static int lambda$main$0(int); Code: 0: iload_0 1: iconst_1 2: iadd 3: ireturn LineNumberTable: line 8: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I }

Slide 72

Slide 72 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; args 0 1 2 Foo #2 = Class #21 // com/pigumer/Foo

Slide 73

Slide 73 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; args 0 1 2 Foo Foo

Slide 74

Slide 74 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; args 0 1 2 Foo #3 = Methodref #2.#20 // com/pigumer/Foo."":()V

Slide 75

Slide 75 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; foo args 0 1 2

Slide 76

Slide 76 text

INVOKE InvokeDynamic InvokeInterface

Slide 77

Slide 77 text

InnerClasses: public static final #62= #61 of #65; //Lookup=class java/lang/invoke/ MethodHandles$Lookup of class java/lang/invoke/MethodHandles BootstrapMethods: 0: #34 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/ MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/ MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/ invoke/CallSite; Method arguments: #35 (I)I #36 invokestatic com/pigumer/Bar.lambda$main$0:(I)I #35 (I)I

Slide 78

Slide 78 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; Baz baz = a -> a + 1; foo args 0 1 2 $$Lambda #4 = InvokeDynamic #0:#26 // #0:baz:()Lcom/pigumer/Baz;

Slide 79

Slide 79 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; Baz baz = a -> a + 1; $$Lambda foo args 0 1 2

Slide 80

Slide 80 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; 0 1 2 $$Lambda foo args PrintStream $$Lambda

Slide 81

Slide 81 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; 0 1 2 $$Lambda foo args PrintStream $$Lambda 3

Slide 82

Slide 82 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; $$Lambda foo args 0 1 2 #6 = InterfaceMethodref #29.#30 // com/pigumer/Baz.baz:(I)I PrintStream

Slide 83

Slide 83 text

public static void main(java.lang.String[]); Code: 0: new #2 // class com/pigumer/Foo 3: dup 4: invokespecial #3 // Method com/pigumer/Foo."":()V 7: astore_1 8: invokedynamic #4, 0 // InvokeDynamic #0:baz:()Lcom/pigumer/Baz; 13: astore_2 14: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: iconst_3 19: invokeinterface #6, 2 // InterfaceMethod com/pigumer/Baz.baz:(I)I 24: invokevirtual #7 // Method java/io/PrintStream.println:(I)V 27: return LineNumberTable: line 6: 0 line 8: 8 line 9: 14 line 10: 27 LocalVariableTable: Start Length Slot Name Signature 0 28 0 args [Ljava/lang/String; 8 20 1 foo Lcom/pigumer/Foo; 14 14 2 baz Lcom/pigumer/Baz; $$Lambda foo args 0 1 2 #6 = InterfaceMethodref #29.#30 // com/pigumer/Baz.baz:(I)I PrintStream 4

Slide 84

Slide 84 text

JVMΞηϯϒϥΛѻ͏ϥΠϒϥϦ BCEL - https://commons.apache.org/proper/commons-bcel/ ASM - http://asm.ow2.org/ invokedynamic ࢀߟࢿྉ https://www.slideshare.net/miyakawataku/lambda-meets- invokedynamic