Slide 1

Slide 1 text

Ί΋Γʔ RubyVM Λ PHP Ͱ࣮૷͢Δ
 ʙHello World Λग़ྗ͢Δ·Ͱʙ PHPerKaigi 2024

Slide 2

Slide 2 text

Ί΋Γʔ m3m0r7 ෳ਺ͷελʔτΞοϓ΍্৔اۀͰΤϯδ χΞɼEMɼTLɼࣥߦ໾һ CTO ͷܦݧΛܦ ΔɻRuby on Rails ΍ Go Λ࢖༻͍ͯ͠Δ גࣜձࣾΤϯϖΠ΁ 2023 ೥ 7 ݄ʹΤϯδ χΞͱͯ͠δϣΠϯɻ2023 ೥ 10 ݄͔Β EM ͱͯ͠ಇ͍͍ͯ·͢ɻ memory1994 m3m0r7

Slide 3

Slide 3 text

Slide 4

Slide 4 text

աڈʹ JVM (Java Virtual Machine) Λ PHP Ͱ࣮૷ͨ͠಺༰Ͱొஃ͓ͯ͠Γ·͢ ※ݱࡏࢲ͸ BASE גࣜձࣾΛୀ৬͓ͯ͠Γɼ
 ࢿྉ͸౰࣌ͷ΋ͷΛҾ༻͓ͯ͠Γ·͢ɻ ※ݱࡏࢲ͸ גࣜձࣾ GameWith Λୀ৬͓ͯ͠Γɼ
 ࢿྉ͸౰࣌ͷ΋ͷΛҾ༻͓ͯ͠Γ·͢ɻ

Slide 5

Slide 5 text

ࢲࣗ਎ɼPHP Λච಄ʹ Go ΍ TypeScript ͳͲ ༷ʑͳݴޠΛֶश͖ͯ͠·ͨ͠

Slide 6

Slide 6 text

࣍ୈʹ৽͍͠ݴޠΛ "ී௨ʹ" ֶͿʹ͸
 ܹ͕ࢗ଍Γͳ͘ͳ͖ͬͯ·ͨ͠

Slide 7

Slide 7 text

Ruby ΋ྫ֎Ͱ͸͋Γ·ͤΜ

Slide 8

Slide 8 text

ֶशํ๏΋͋Δఔ౓ࣗ෼ͷதͰཱ֬Ͱ͖͍ͯΔ ͷͰϞνϕʔγϣϯ΋ͳ͔ͳ্͔͕Γ·ͤΜ

Slide 9

Slide 9 text

Ϟνϕʔγϣϯ্͕͕Βͳ͍ͷͰͦͷݴޠͷ
 ؾ࣋ͪΛཧղ͢ΔͳΜͯ΋ͬͯͷ΄͔Ͱ͢

Slide 10

Slide 10 text

ͦ͜Ͱߟ͑·ͨ͠

Slide 11

Slide 11 text

Ͳ͏΍ͬͨΒܹࢗυϦϒϯʹݴޠΛֶ΂Δͷ͔

Slide 12

Slide 12 text

Ͳ͏΍ͬͨΒؾ࣋ͪΛཧղͰ͖Δͷ͔

Slide 13

Slide 13 text

ʮͦ͏ͩ VM Λ࡞Ζ͏ʯ ʮVM Λ࡞Ε͹ؾ࣋ͪΛཧղͰ͖Δ͸ͣʯ

Slide 14

Slide 14 text

ࢲͷఱ࢖͕ͦ͏͞͞΍͘ͷͰ͢

Slide 15

Slide 15 text

΋͏ී௨ʹ͸໭Ε·ͤΜ

Slide 16

Slide 16 text

VM (Virtual Machine) ʹ͍ͭͯ RubyVM ͷ͘͠Έ RubyVM ͷ࡞Γํ DEMO (͕࣌ؒ͋Ε͹) ΞδΣϯμ Appendix

Slide 17

Slide 17 text

࿩͞ͳ͍͜ͱ

Slide 18

Slide 18 text

Ruby ͷ Lexer (ࣈ۟ղੳث) Ruby ͷ Parser (ߏจղੳث) Ruby ͷॻ͖ํ ࿩͞ͳ͍͜ͱ

Slide 19

Slide 19 text

Ruby 3.3.0 ͕ 2023/12/25 ʹ
 ϦϦʔε͞Ε·ͨ͠Ͷ🎉

Slide 20

Slide 20 text

͜ͷࢿྉ΋ Ruby 3.3.0 ʹରԠ͍ͯ͠·͢

Slide 21

Slide 21 text

VM (Virtual Machine) ʹ͍ͭͯ

Slide 22

Slide 22 text

VM ͱ͸ʁ

Slide 23

Slide 23 text

VM ͱ͸ʁ - VM ͱ͸Ծ૝Ϛγϯͷ͜ͱΛࢦ͠ɼίϯϐϡʔλʔͷಈ࡞ΛΤϛϡϨʔγϣϯ ʢ࠶ݱʣͤ͞Δ΋ͷͰ͢ɻ - VM ্Ͱ Linux/Unix Λಈ͔ͨ͠Γ͍ͯ͠Δํ΋͍Δͱࢥ͍·͢ɻ - VM ΛՄೳͱ͢Δ୅දతͳ΋ͷ͸ KVM (Kernel-based Virtual Machine) ͩͬ ͨΓɼHyper-V (Windows) ͩͬͨΓɼVirtualBox ͳͲ͕ྫʹ্͕ΔͰ͠ΐ ͏ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 24

Slide 24 text

VM ͱ͸ʁ - Type-1, Type-2 ͱ͍͏෼ྨ͕͋ΓɼType-1 ͸ϕΞϝλϧʢϋʔυ΢ΣΞʹμ ΠϨΫτͳ΋ͷʣɼType-2 ͸ϗετܕʢطଘͷ OS ্Ͱಈ͘΋ͷʣͰ͢ɻ͜ ΕΒΛ૯ͯ͡ Hyper VisorʢϋΠύʔόΠβʔʣͱݺͿ͜ͱ͕͋Γ·͢ɻ - Docker ΋Ծ૝Ϛγϯʹࣅ͍ͯ·͕͢ɼݫີʹ͸ҟͳΓ·͢ɻVM ͸׬શʹ෼ ཭͞Εͨήετ OS ͷΧʔωϧΛ͍࣋ͬͯΔͷʹରͯ͠ɼDocker ͸ϗετ OS ͷΧʔωϧΛڞ༗͍ͯ͠·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 25

Slide 25 text

VM ͱ͸ʁ - ͱ͍͏͜ͱΛॻ͖·͕ͨ͠ɼZend VMɼJVMɼ RubyVM ͸ VM Ͱ͸͋Δ΋ͷ ͷɼ͜ͷఆٛʹ͸ݫີʹ͸౰ͯ͸·Γ·ͤΜɻ - ͔͠͠ɼઌ΄Ͳॻ͍ͨʮVM ͱ͸Ծ૝Ϛγϯͷ͜ͱΛࢦ͠ɼίϯϐϡʔλʔͷ ಈ࡞ΛΤϛϡϨʔγϣϯʢ࠶ݱʣͤ͞Δ΋ͷͰ͢ɻʯͱ͸ಉ౳Ͱ͋ΔͱղऍͰ ͖·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 26

Slide 26 text

VM ͱ͸ʁ - CPU ͸໋ྩηοτʹैͬͯಈ࡞͠·͢ɻ͜ͷ໋ྩηοτ͸਎ۙͳ΋ͷͰݴ͑ ͹ Intel ΍ ARM ͳͲʹ΋͋ΓɼͦΕͧΕҟͳΓ·͢ɻ - ͜ͷ CPU ͷ໋ྩηοτΛஞ࣮࣍ߦͤ͞Δ͜ͱ͕ίϯϐϡʔλʔ͕ಈ͘ݪཧͷ ҰͭͰ͕͢ɼZend VM ΍ JVMɼRubyVM ʹ΋໋ྩηοτ͕͋Γɼ·ΔͰίϯ ϐϡʔλʔ͕ಈ͘ݪཧʹଇ࣮ͬͯߦ͞Ε͍ͯ·͢ɻ - ͦͷͨΊɼΤϛϡϨʔγϣϯͱ͍͏จ຺Ͱ͸ VM Ͱ͋ΔͱղऍͰ͖ΔͷͰ͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 27

Slide 27 text

VM ͱ͸ʁ - ͦͯ͠ɼ͜ΕΒͷ VM ͷ΄ͱΜͲ͸ελοΫϚγϯͱͯ͠ಈ࡞͠·͢ɻ - ଞʹҰൠతͳ΋ͷ͸ϝϞϦϨδελϚγϯͳͲ͕͋Γ·͢ɻ͜Ε͸ Intel ΍ ARM ͳͲ͕֘౰͠·͢ɻ - ຊτʔΫͰ͸ɼϝϞϦϨδελϚγϯʹ͍ͭͯͷղઆ͸ׂѪ͠·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 28

Slide 28 text

ελοΫϚγϯͬͯͳʹʁ

Slide 29

Slide 29 text

ελοΫϚγϯͬͯͳʹʁ - ελοΫϚγϯͱ͸ɼσʔλߏ଄ͷҰͭͰσʔλ Λ௥Ճʢϓογϡʣ͠ͳ͕Βɼ໋ྩηοτʹΑͬ ͯͦͷ௥Ճͨ͠σʔλΛऔಘʢϙοϓʣͯ͠ɼԿ ͔͠ΒͷܭࢉΛߦ͍ͬͯ͘ίϯϐϡʔλͷΞʔΩ ςΫνϟͰ͢ɻ - ྫ͑͹ʮελοΫ͔Β 2 ͭ஋Λϙοϓ͠Ճࢉͤ͞ ͨ΋ͷΛϓογϡ͢Δʯͱ໋͍ͬͨྩηοτ΋͋ Γ·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ 1 ճ໨ʹϓογϡ͞Εͨσʔλ 2 ճ໨ʹϓογϡ͞Εͨσʔλ N ճ໨ʹϓογϡ͞Εͨσʔλ ộ σʔλΛϓογϡ σʔλΛϙοϓ Կ͔͠Βͷ໋ྩηοτͰ σʔλΛ࢖༻ Կ͔͠Βͷ໋ྩηοτͰ σʔλΛελοΫʹ௥Ճ

Slide 30

Slide 30 text

໋ྩηοτͬͯͳʹʁ

Slide 31

Slide 31 text

໋ྩηοτͬͯͳʹʁ - ໋ྩηοτͱ͸ίϯϐϡʔλʹԿ͔͠Βͷಈ࡞Λࢦࣔ͢ΔͨΊͷίϚϯυʢ໋ ྩʣͷू߹ͷ͜ͱΛࢦ͠·͢ɻ - ྫ͑͹ɼΘ͔Γ΍໋͍͢ྩʹ͸ʮՃࢉʯʮݮࢉʯʮ৐ࢉʯʮআࢉʯͷΑ͏ͳ࢛ ଇԋࢉ΍ʮಛఆͷϧʔνϯΛ࣮ߦ͢ΔʯͰ͋ͬͨΓʮ഑ྻΛ࡞੒ʯ͢Δͱ͔ʮจ ࣈྻΛ਺ࣈʹม׵͢Δʯͱ͔༷ʑͳ΋ͷ͕͋Γ·͢ɻ - ΋ͪΖΜɼVM ʹΑ໋ͬͯྩηοτ͸ҟͳΓ·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 32

Slide 32 text

໋ྩηοτͬͯͳʹʁ - ͨͩݴ͑Δͷ͸ɼࣅͨΑ͏ͳ໋ྩηοτ͕େମͲͷ VM ʹ΋ଘࡏ͍ͯ͠·͢ɻ - ໋ྩηοτ͸ͲΕ΋୯७ԽΛ໨ࢦ͍ͯ͠Δ܏޲͕ଟ͍΋ͷ͕ଟ͍Ͱ͢ɻ୯७Խ Λ໨ࢦ͢ઃܭࢥ૝ͷ͜ͱΛ RISC ʢReduced Instruction Set Computerʣͱ ݴ͍·͢ɻٯʹ CISC (Complex Instruction Set Computer) ͱ͍͏ෳࡶͰ͸ ͋Δ໋͕ྩ਺ΛۃྗݮΒͨ͠λΠϓͷ΋ͷ΋͋Γ·͢ɻ - ࠷ۙͰ͸͋Μ·Γࠩҟ͕ͳ͍ͱ΋ݴΘΕ͍ͯ·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 33

Slide 33 text

໋ྩηοτͬͯͳʹʁ - ·໋ͨྩ͸Ҿ਺ʢΦϖϥϯυʣΛऔΔ΋ͷ͕͋Γ·͢ɻ͜ͷΦϖϥϯυͱε λοΫΛۦ࢖໋ͯ͠ྩΛ࣮ߦ͍͖ͯ͠·͢ɻ - ·ͨɼίϚϯυΛ൪߸Ͱදͨ͠΋ͷΛʮΦϖϨʔγϣϯίʔυʢΦϖίʔυɼ OP CodeʣʯɼώϡʔϚϯϦʔμϒϧʹͨ͠΋ͷΛʮχʔϞχοΫ ʢmnemonicʣʯͱݴͬͨΓ͠·͢ɻ - ࣍ͷϖʔδ͔Β 1 + 2 + 3 ΛελοΫϚγϯͱٖͯ͠ࣅ໋ྩηοτΛ༻͍ͯܭ ࢉ͢ΔྫΛࣔ͠·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 34

Slide 34 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ

Slide 35

Slide 35 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output - constadd ໋ྩ͸ 1 ͭͷΦϖϥϯυΛऔΓɼͦΕΛελοΫ ʹϓογϡ͢Δ໋ྩ - plus ͸ελοΫ͔Β 2 ͭ஋Λϙοϓ͠ɼͦΕΛՃࢉͨ͠΋ͷ ΛελοΫʹϓογϡ͢Δ໋ྩ - output ͸ελοΫ͔ΒҰͭϙοϓ͠ग़ྗ͢Δ໋ྩ - ্هͷ 3 ͭͷ໋ྩͰ࣮ݱՄೳͰ͢ɻ͜ͷҰ࿈ͷྲྀΕΛ໋ྩ γʔέϯεͱݴͬͨΓ͠·͢ɻ

Slide 36

Slide 36 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 1 ελοΫʹ 1 Λϓογϡ

Slide 37

Slide 37 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 1 ελοΫʹ 2 Λϓογϡ 2

Slide 38

Slide 38 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 1 2 ͭͷσʔλΛϙοϓ 2 3 Ճࢉͨ݁͠ՌΛϓογϡ

Slide 39

Slide 39 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 3 ελοΫʹ 3 Λϓογϡ 3

Slide 40

Slide 40 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 3 2 ͭͷσʔλΛϙοϓ 3 6 Ճࢉͨ݁͠ՌΛϓογϡ

Slide 41

Slide 41 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ VM (Virtual Machine) ʹ͍ͭͯ constadd (operand: 1) constadd (operand: 2) constadd (operand: 3) plus plus output ελοΫ 6 1 ͭͷσʔλΛϙοϓ

Slide 42

Slide 42 text

1 + 2 + 3 Λٙࣅ໋ྩηοτͰܭࢉ͢Δ - ֓Ͷ΄ͱΜͲͷ VM ͸ઌఔͷղઆͷΑ͏ͳܗͰ࣮૷͞Ε͍ͯ·͢ɻ͜ΕΒΛԠ ༻ͨ͠΋ͷ͕ Zend VM ͩͬͨΓɼJVM ͩͬͨΓɼͦͯ͠ຊτʔΫͷझࢫͰ͋ Δ RubyVM Ͱ͢ɻ - Ͱ͸ RubyVM ͸ͲͷΑ͏ʹ࣮૷͢ΔͷͰ͠ΐ͏͔ɻ࣍ͷηΫγϣϯͰղઆ͠ ͍͖ͯ·͢ɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 43

Slide 43 text

༨ஊ - ઌ΄Ͳͷ໋ྩγʔέϯεΛ࠷దԽ͢Δͱ࣮͸ʮconstadd (operand: 6)ʯͱ ʮoutputʯ͚ͩͰࡁΈ·͢ɻ໋ྩ࠷దԽΛߦ͏͜ͱͰܭࢉ͢΂͖εςοϓΛল ུͰ͖ΔͷͰɼߴ଎Խ͕ݟࠐ·Ε·͢ɻ - ͜͏͍ͬͨߴ଎ԽΛߦ͏ͷ͕ʮJIT/AOT ࠷దԽʯͱݴΘΕͨΓ͠·͢ɻ - JIT ʢJust In TimeʣίϯύΠϧํࣜͱ AOT ʢAhead Of TimeʣίϯύΠϧํ ͕ࣜ͋Γ·͕͢ɼຊτʔΫͰ͸ׂѪ͠·͢ɻChatGPT ʹฉ͍ͯΈͯͶɻ VM (Virtual Machine) ʹ͍ͭͯ

Slide 44

Slide 44 text

VM Λ࡞Δͷʹඞཁͳ΋ͷ

Slide 45

Slide 45 text

΍Δؾ ࠜؾ ෛ͚ͳ͍ؾ 1 2 3 VM Λ࡞Δͷʹඞཁͳ΋ͷ

Slide 46

Slide 46 text

RubyVM ͷ͘͠Έ

Slide 47

Slide 47 text

RubyVM ͱ͸ʁ

Slide 48

Slide 48 text

RubyVM ͱ͸ - RubyVM ͸ YARV (Yet Another Ruby Vm) ͱ΋ݺ͹Ε Sasada Koichi (@koichisasada) ͞Μ͕ Ruby ޲͚ͷԾ૝Ϛγϯͱ࣮ͯ͠૷͖ͯͨ͠΋ͷͰ͢ [1]ɻ - YARV ͸୺తʹݴ͑͹ɼISeqʢInstruction Sequenceʣͱݺ͹ΕΔ໋ྩγʔέ ϯεʢ࣮ߦ͢΂໋͖ྩͷҰཡʣͱɼͦͷ໋ྩγʔέϯεʹର͢Δϝλ৘ใͷू ߹Ͱ͢ɻ RubyVM ͷ͘͠Έ [1]: https://sitaramshelke.medium.com/inside-rubyvm-967b25e234db

Slide 49

Slide 49 text

RubyVM ͱ͸ - Լਤ͸ Hello World! Λग़ྗ͢ΔͨΊͷ YARV Ͱ͢ɻ RubyVM ͷ͘͠Έ

Slide 50

Slide 50 text

RubyVM ͱ͸ - Լਤ͸ Hello World! Λग़ྗ͢ΔͨΊͷ YARV Ͱ͢ɻ RubyVM ͷ͘͠Έ Hello World! ͱ͍͏จࣈྻ͕ݟ͑·͢Ͷ

Slide 51

Slide 51 text

Ruby 3.2 ·Ͱ

Slide 52

Slide 52 text

The header section (36 bytes) The payload section The instruction sequence offsets section (The information is each of instruction sequence offsets; N>0 * 4 bytes) The global object offsets section (The information is each of global object offsets; N>0 * 4 bytes) The extra data (if embedded extra data; N>=0 bytes) A part of instruction sequences A part of global objects The RUBY_PLATFORM name section (string) An information of string / class / fi xed number / bool types and data An information of instruction sequence section (In normally, 44 info * 4 bytes = 176 bytes notice: no considered hamming weight) ộ A code section (N>0 bytes) ộ A local table section (N>=0 bytes) A call info entry section (N>=0 bytes) ộ The structure of YARV The alignment section (Filled by 0xff to align every 2 bytes) ※ Ruby ຊମͷ C ࣮૷ΛಡΜͰࢲ͕ղऍͨ͠΋ͷΛςΩετɾਤʹදͨ͠΋ͷͰ͢ɻ

Slide 53

Slide 53 text

Ruby 3.3 ͔Β

Slide 54

Slide 54 text

The header section (36 bytes) The payload section The instruction sequence offsets section (The information is each of instruction sequence offsets; N>0 * 4 bytes) The global object offsets section (The information is each of global object offsets; N>0 * 4 bytes) The extra data (if embedded extra data; N>=0 bytes) A part of instruction sequences A part of global objects The endian section (2 bytes) An information of string / class / fi xed number / bool types and data An information of instruction sequence section (In normally, 44 info * 4 bytes = 176 bytes notice: no considered hamming weight) ộ A code section (N>0 bytes) ộ A local table section (N>=0 bytes) A call info entry section (N>=0 bytes) ộ The structure of YARV The alignment section (Filled by 0xff to align every 2 ※ Ruby ຊମͷ C ࣮૷ΛಡΜͰࢲ͕ղऍͨ͠΋ͷΛςΩετɾਤʹදͨ͠΋ͷͰ͢ɻ The word size section (2 bytes)

Slide 55

Slide 55 text

The header section (36 bytes) The payload section The instruction sequence offsets section (The information is each of instruction sequence offsets; N>0 * 4 bytes) The global object offsets section (The information is each of global object offsets; N>0 * 4 bytes) The extra data (if embedded extra data; N>=0 bytes) A part of instruction sequences A part of global objects An information of string / class / fi xed number / bool types and data An information of instruction sequence section (In normally, 44 info * 4 bytes = 176 bytes notice: no considered hamming weight) ộ A code section (N>0 bytes) ộ A local table section (N>=0 bytes) A call info entry section (N>=0 bytes) ộ The structure of YARV The alignment section (Filled by 0xff to align every 2 ※ Ruby ຊମͷ C ࣮૷ΛಡΜͰࢲ͕ղऍͨ͠΋ͷΛςΩετɾਤʹදͨ͠΋ͷͰ͢ɻ The endian section (2 bytes) The word size section (2 bytes) Platform name section ͕
 endian section ͱ word size section ʹมߋɻ
 Ruby 3.2 → 3.3 Ͱ YARV ͷߏ଄͸͜ΕҎ֎มߋͳ͠ɻ

Slide 56

Slide 56 text

RubyVM ͱ͸ - Ruby Ͱ Hello World! Λग़ྗ͢Δʹ͸࣮͸ࠨਤͷ ίʔυͰॆ଍͠·͢ɻ͔͠͠ɼYARV ʹͳΔͱ࿩ ͕มΘΓ·͢ɻ - RubyVM ʹݶΒͣ VM Λ࣮૷͠ɼHello World Λ ग़ྗ͢Δ·Ͱَ͕໳Ͱ͢ɻ໋ྩηοτΛ࣮૷͢Δ ͷ͸ͦ͜·Ͱۤ࿑͠·ͤΜ͕ɼ໋ྩηοτΛΤ ϛϡϨʔτ͢Δ VM ͦͷ΋ͷΛ࣮૷͢Δͷʹۤ࿑ ͠·͢ɻͦΕָ͕ܹࢗ͘͠తͳͷͰ͢ɻ RubyVM ͷ͘͠Έ

Slide 57

Slide 57 text

Hello World ͕ग़ྗͰ͖ͨͱ͖͸ɼͦΕ͸΋͏

Slide 58

Slide 58 text

ຐԦ৓ʹ౸ୡͨ͠Α͏ͳؾ࣋ͪʹͳΓ·͢

Slide 59

Slide 59 text

RubyVM ͷ࡞Γํ

Slide 60

Slide 60 text

Slide 61

Slide 61 text

ChatGPT Ͱ͸ղઆͰ͖ͳ͍͜ͱΛղઆ͠·͢

Slide 62

Slide 62 text

※ PHP Ҏ֎Ͱ΋࣮૷Ͱ͖·͢

Slide 63

Slide 63 text

ͦ΋ͦ΋Ͳ͏΍࣮ͬͯ૷͢Δͷʁ

Slide 64

Slide 64 text

ͦ΋ͦ΋Ͳ͏΍࣮ͬͯ૷͢Δͷʁ - JVM ʹ͸ Java Virtual Machine Speci fi cation[1] ͱݺ͹ΕΔυΩϡϝϯτ͕ ॆ࣮͍ͯ͠·͢ɻ - Java ͸اۀओମͰ͕͢ɼRubyVM ͷ৔߹ίϛϡχςΟʹΑͬͯ։ൃ͞Ε͍ͯ Δ͜ͱ΋͋Γ༗ࢤͨͪʹΑͬͯυΩϡϝϯτͷ੔උ͕ͳ͞Ε͍ͯ·͕͢ VM ͷ ࣮૷ํ๏ʹݶͬͯݴ͑͹اۀओମͱൺֱ͢ΔͱͲ͏ͯ͠΋ݶք͕͋Γ·͢ɻ - ͦΜͳதɼRubyVM Λ࡞Δʹ͸Ͳ͏ͨ͠Β͍͍Ͱ͠ΐ͏͔ɻ RubyVM ͷ࡞Γํ [1]: https://docs.oracle.com/javase/specs/jvms/se8/html/

Slide 65

Slide 65 text

౴͑: C Ͱॻ͔ΕͨίʔυΛಡΉ

Slide 66

Slide 66 text

https://github.com/m3m0r7/rubyvm-on-php

Slide 67

Slide 67 text

RubyVM ͷߏ଄

Slide 68

Slide 68 text

※ Ruby ͷόʔδϣϯ͸ 3.3.0 Ͱղઆ͠·͢

Slide 69

Slide 69 text

େ·͔ͳྲྀΕ

Slide 70

Slide 70 text

େ·͔ͳྲྀΕ RubyVM ͷ࡞Γํ YARV ϔομʔͷಡΈࠐΈ Instruction Sequence ͷΦϑηοτ৘ใͷಡΈࠐΈ άϩʔόϧΦϒδΣΫτͷΦϑηοτ৘ใͷ 
 ಡΈࠐΈ Instruction Sequence ͷಡΈࠐΈ 0 ൪໨ͷ Instruction Sequence ͷ࣮ߦ

Slide 71

Slide 71 text

Ͳ͏΍ͬͯ PHP ͰόΠφϦΛಡΉͷ͔

Slide 72

Slide 72 text

Ͳ͏΍ͬͯ PHP ͰόΠφϦΛಡΉͷ͔ - ͦ΋ͦ΋ YARV ϑΝΠϧΛੜ੒͢Δͱ͜Ζ͔ΒελʔτͰ͢ɻRuby ʹ͸ Instruction Sequence Λు͖ग़ͨ͢Ίͷɹ RubyVM::InstructionSequence.compile ͕༻ҙ͞Ε͍ͯ·͢ɻҎԼͷΑ͏ͳ ίϚϯυΛ༻͍ͯ HelloWorld.yarv ͱ͍͏ YARV ϑΝΠϧΛ࡞੒͠·͢ɻ RubyVM ͷ࡞Γํ

Slide 73

Slide 73 text

Ͳ͏΍ͬͯ PHP ͰόΠφϦΛಡΉͷ͔ - VM ࣮૷ʹ͓͍ͯɼΑ͘༻͍ΒΕΔ PHP ͷؔ਺͸ fread, fseek, unpack ͷ 3 छ ྨͰ͢ɻ͓͖֮͑ͯ·͠ΐ͏ɻ - ΋ͪΖΜ unpack Λ࢖Θͳ͍Ͱߦ͏࣮૷΋ϏοτԋࢉΛ༻͍Ε͹ՄೳͰ͢ɻϏο τԋࢉʹ͍ͭͯ͸ׂѪ͠·͢ɻ - PHP ͸ C ݴޠͱҧ͍ɼࢦఆͨ͠ܕͰόΠφϦΛಡΜͰ͘Εͳ͍ͷͰɼࢦఆͨ͠ Φϑηοτʢfseekʣ͔ΒจࣈྻͰҰ౓औಘʢfreadʣͨ͋͠ͱʹ਺஋ܕͳΓɼ ුಈখ਺఺ܕͳΓʹม׵ͯ͋͛͠Δඞཁʢunpackʣ͕ੜ͡·͢ɻ RubyVM ͷ࡞Γํ

Slide 74

Slide 74 text

ϔομʔΛಡΉ

Slide 75

Slide 75 text

େ·͔ͳྲྀΕ RubyVM ͷ࡞Γํ YARV ϔομʔͷಡΈࠐΈ Instruction Sequence ͷΦϑηοτ৘ใͷಡΈࠐΈ άϩʔόϧΦϒδΣΫτͷΦϑηοτ৘ใͷ 
 ಡΈࠐΈ Instruction Sequence ͷಡΈࠐΈ 0 ൪໨ͷ Instruction Sequence ͷ࣮ߦ

Slide 76

Slide 76 text

magic major version minor version size extra size global object list size iseq list offset global object list offset YARV ͷϔομʔ 4 byte 4 byte 4 byte 4 byte 4 byte 4 byte 4 byte 4 byte YARB (Yet Another Ruby Binary) ͱ͍͏ 
 ஋͕औಘͰ͖·͢ ίϯύΠϧ͞Εͨ Ruby ͷϝδϟʔόʔδϣϯ͕औಘ Ͱ͖·͢ɻྫͷ৔߹ʮ3ʯͰ͢ ίϯύΠϧ͞Εͨ Ruby ͷϚΠφʔόʔδϣϯ͕औಘ Ͱ͖·͢ɻྫͷ৔߹ʮ2ʯͰ͢ όΠφϦͷจࣈྻ௕͕औಘͰ͖·͢ Instruction Sequence ͕͍ͭ֨͘ೲ͞Ε͍ͯΔ͔Λ औಘͰ͖·͢ Ruby ͷγϯϘϧͷ৘ใ͕͍ͭ֨͘ೲ͞Ε͍ͯΔ͔Λ औಘͰ͖·͢ɻ όΠφϦ্ͷ Instruction Sequence ͷҐஔ৘ใΛऔ ಘͰ͖·͢ɻ Ruby ͷγϯϘϧͷ৘ใͳͲͷҐஔ৘ใ͕֨ೲ͞Εͯ ͍ΔϦετΛऔಘͰ͖·͢ɻ ͏·͘औಘͰ͖Δͱ
 ӈਤͷΑ͏ʹͳΓ·͢ iseq size 4 byte ಠࣗʹ௥Ճ͞ΕΔ֦ுσʔλͷจࣈྻ௕͕औಘͰ͖ ·͢

Slide 77

Slide 77 text

V (unsigned long, ϦτϧΤϯσΟΞϯ) Ͱऔಘ͠·͢

Slide 78

Slide 78 text

Φϑηοτ৘ใΛऔಘ

Slide 79

Slide 79 text

େ·͔ͳྲྀΕ RubyVM ͷ࡞Γํ YARV ϔομʔͷಡΈࠐΈ Instruction Sequence ͷΦϑηοτ৘ใͷಡΈࠐΈ άϩʔόϧΦϒδΣΫτͷΦϑηοτ৘ใͷ 
 ಡΈࠐΈ Instruction Sequence ͷಡΈࠐΈ 0 ൪໨ͷ Instruction Sequence ͷ࣮ߦ

Slide 80

Slide 80 text

iseq list offset ʹΧʔιϧΛҠಈ iseq list size ෼ϧʔϓΛճ͠ 4 όΠτ͝ͱͷ 
 Φϑηοτ৘ใΛऔಘ͢Δ global object list offset ʹΧʔιϧΛҠಈ global object list size ෼ϧʔϓΛճ͠ 4 όΠτ͝ͱ ͷΦϑηοτ৘ใΛऔಘ͢Δ Instruction Sequence ͷΦϑηοτ৘ใϦετΛऔಘ Global Object ͷΦϑηοτ৘ใϦετΛऔಘ

Slide 81

Slide 81 text

Instruction Sequence ͷಡΈࠐΈ

Slide 82

Slide 82 text

େ·͔ͳྲྀΕ RubyVM ͷ࡞Γํ YARV ϔομʔͷಡΈࠐΈ Instruction Sequence ͷΦϑηοτ৘ใͷಡΈࠐΈ άϩʔόϧΦϒδΣΫτͷΦϑηοτ৘ใͷ 
 ಡΈࠐΈ Instruction Sequence ͷಡΈࠐΈ 0 ൪໨ͷ Instruction Sequence ͷ࣮ߦ

Slide 83

Slide 83 text

Instruction Sequence ͷಡΈࠐΈ - RubyVM ͷ࣮૷ʹ͸ޮ཰తʹόΠφϦΛѻ͏ͨΊʹ ʮibf_(?:load|dump_write)_small_valueʯͱ͍͏֓ ೦͕͋Γ·͢[1]ɻ - ϋϛϯάॏΈ[2] ʢpopcount, population countʣͳ ͲΛ༻͍ͯόΠτ௕ΛՄมతʹѻ͍ͬͯΔ΋ͷͰ͢ɻ ࣮૷ྫ͸ࠨਤͷ௨Γɻ RubyVM ͷ࡞Γํ [1]: https://github.com/ruby/ruby/blob/2f603bc4/compile.c#L11262-L11273
 [2]: https://ja.wikipedia.org/wiki/%E3%83%8F%E3%83%9F%E3%83%B3%E3%82%B0%E9%87%8D%E3%81%BF

Slide 84

Slide 84 text

Instruction Sequence ͷಡΈࠐΈ - readSmallValue ͕࣮૷Ͱ͖ͨΒಡΈࠐΉ΂͖ 0 ൪໨ͷ Instruction Sequence ͷσʔλߏ଄ΛಡΈࠐΜͰ͍͖·͢ɻ - ͜ͷσʔλߏ଄ɼ࣮͸ͱͯ΋ڊେͰ Instruction Sequence ͷϝλ৘ใʢྫ֎ ςʔϒϧͳͲʣʹ֘౰͢Δ΋ͷͳͷͰ͕͢ɼͦͷϝλ৘ใͷ਺͸ 50 ݸҎ্͋ Γ·͢…ɻ - ͜ͷσʔλߏ଄શͯΛಡ·ͳͯ͘΋ɼHelloWorld ͷग़ྗͰ͋Ε͹໰୊ͳ͘Մ ೳͳͷͰɼׂѪͯ͠ඞཁͳ 4 ͭͷϝλ৘ใΛಡΈࠐΈ·͢ɻ RubyVM ͷ࡞Γํ

Slide 85

Slide 85 text

Instruction Sequence ͷಡΈࠐΈ - ۩ମతͳ࣮૷ྫ͸ Ruby ຊՈͷ࣮૷ʢ https://github.com/ruby/ruby/blob/ ruby_3_3/compile.c#L12514 ʣΛಡΉ͔ɼRubyVM on PHP ʢ https:// github.com/m3m0r7/rubyvm-on-php/blob/0.3.3.0/src/VM/Core/Runtime/ Kernel/Ruby3_3/InstructionSequence/ InstructionSequenceProcessor.php#L59 ʣͷ࣮૷Λ͝ཡ͍ͩ͘͞ɻ RubyVM ͷ࡞Γํ

Slide 86

Slide 86 text

type iseq size bytecode offset bytecode size Instruction Sequence ΛಡΉ sv sv sv sv ※ sv ͸ small value ͷུ Instruction Sequence ͷΦϑηοτϦετ 0 ൪໨ 
 ʹΧʔιϧΛҠಈ

Slide 87

Slide 87 text

Instruction Sequence ͷ࣮ߦ

Slide 88

Slide 88 text

େ·͔ͳྲྀΕ RubyVM ͷ࡞Γํ YARV ϔομʔͷಡΈࠐΈ Instruction Sequence ͷΦϑηοτ৘ใͷಡΈࠐΈ άϩʔόϧΦϒδΣΫτͷΦϑηοτ৘ใͷ 
 ಡΈࠐΈ Instruction Sequence ͷಡΈࠐΈ 0 ൪໨ͷ Instruction Sequence ͷ࣮ߦ

Slide 89

Slide 89 text

Instruction Sequence ͷ࣮ߦ - ઌ΄ͲಡΈࠐΜͩ Instruction Sequence ͔Β $bytecodeO ff set, $iseqSize Λ༻͍ͯɼ໋ྩγʔέϯε Λऔಘ͠·͢ɻ - HelloWorld! Λग़ྗ͢Δࡍʹ࢖͏໋ྩηοτ͸ʮputself (18)ʯʮputstring (21)ʯʮopt_send_without_block (51)ʯʮleave (60)ʯͷ 4 छྨͰ͢ɻ - ࠨਤͷΑ͏ʹχʔϞχοΫΛॻ͖ग़͠·͢ɻ RubyVM ͷ࡞Γํ ͜ͷΦϖϨʔγϣϯίʔυͱχʔϞχοΫͷඥ͚ͮ͸ https://github.com/ruby/ruby/blob/ruby_3_3/yjit/src/cruby_bindings.inc.rs#L669- L872 Ͱॻ͔Ε͍ͯ·͢

Slide 90

Slide 90 text

Instruction Sequence ͷ࣮ߦ RubyVM ͷ࡞Γํ putself putstring(operand: "HelloWorld!") leave opt_send_without_block ελοΫ ελοΫʹ࣮ߦதͷίϯςΩετΛ
 ϓογϡ ࣮ߦதͷίϯςΩετ

Slide 91

Slide 91 text

Instruction Sequence ͷ࣮ߦ RubyVM ͷ࡞Γํ putself putstring(operand: "HelloWorld!") leave opt_send_without_block ελοΫ ελοΫʹ HelloWorld! ͷ
 จࣈྻΛϓογϡ ࣮ߦதͷίϯςΩετ "HelloWorld!"

Slide 92

Slide 92 text

Instruction Sequence ͷ࣮ߦ RubyVM ͷ࡞Γํ putself putstring(operand: "HelloWorld!") leave opt_send_without_block ελοΫ 2 ͭͷελοΫΛϙοϓ ࣮ߦதͷίϯςΩετͷ 
 puts ϝιουΛݺͼग़͠ ࣮ߦதͷίϯςΩετ "HelloWorld!"

Slide 93

Slide 93 text

Instruction Sequence ͷ࣮ߦ RubyVM ͷ࡞Γํ putself putstring(operand: "HelloWorld!") leave opt_send_without_block ελοΫ ←࣮ߦͷऴྃॲཧɼฦΓ஋ΛૹΔ

Slide 94

Slide 94 text

Instruction Sequence ͷ࣮ߦ - ͪͳΈʹɼYARV ʹม׵͞Εͨ͋ͱͷ໋ྩγʔέϯεΛऔಘ͢Δʹ͸ʢjavap ί ϚϯυͷΑ͏ͳ΋ͷ͸ʣʮputs RubyVM::InstructionSequence.compile("puts 'HelloWorld!'", "HelloWorld.rb").disasmʯͷΑ͏ʹ͢ΔͱՄೳͰ͢ɻ RubyVM ͷ࡞Γํ

Slide 95

Slide 95 text

Instruction Sequence ͷ࣮ߦ - ࠨਤͷΑ͏ʹݺͼग़͠ݩͷ Main ΫϥεͱάϩʔόϧͳΦ ϒδΣΫτʹอଘ͞Ε͍ͯΔ஋ Λऔಘ͢ΔͨΊͷ loadObject Λ࣮૷͠·͢ɻ RubyVM ͷ࡞Γํ ͜͜ͷ൪߸͸ https://github.com/ruby/ruby/blob/ruby_3_3/compile.c#L13303- L13336 ͷ 5 ൪໨ͷؔ਺Λࢦ͍ͯ͠·͢ (ibf_load_object_string)

Slide 96

Slide 96 text

putself ໋ྩͷ࣮૷ putstring ໋ྩͷ࣮૷ opt_send_without_block ໋ྩͷ࣮૷ leave ໋ྩͷ࣮૷

Slide 97

Slide 97 text

Instruction Sequence ͷ࣮ߦ - ࠨਤͷΑ͏ʹ HelloWorld! ͕ग़ྗ͞ΕΔ͜ ͱ͕Θ͔Γ·͢ɻ - ࠓ·Ͱͷιʔείʔυͷྫ͸ҎԼͷ gist ʹ ·ͱΊͯ͋Γ·͢ɻ - https://gist.github.com/ m3m0r7/226e20c8115caf4a9d43b291861 f978b RubyVM ͷ࡞Γํ

Slide 98

Slide 98 text

DEMOʢ͕࣌ؒ͋Ε͹ʣ

Slide 99

Slide 99 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ ʢ࣍ϖʔδҎ߱ʹ Appendix ͕͖ͭͮ·͢ʣ

Slide 100

Slide 100 text

Appendix: CallInfoEntry ʹ͍ͭͯ

Slide 101

Slide 101 text

Appendix: CallInfoEntry ʹ͍ͭͯ RubyVM ͷ࡞Γํ - RubyVM ্ͷ CallInfoEntry ͸࣮ߦ͞ΕΔϝιουͷϝλ৘ใΛऔಘ͢ΔͨΊ ʹඞཁͳ΋ͷͰ͢ɻ - ֨ೲ͞Ε͍ͯΔ৘ใ͸ʮϝιου໊ʯʮҾ਺ͷ਺ʯͳͲͰ͢ɻຊདྷ͸͜ΕΒΛ ༻͍ͯɼಈతʹϝιουΛ࣮ߦʢopt_send_without_block ໋ྩͷ࣮ߦʣΛ͞ ͤΔඞཁ͕͋Γ·͢ɻ - CallInfoEntry ͸࣍ͷϖʔδͷΑ͏ʹ࣮૷͢Δ͜ͱͰ puts Λࢦఆͤͣʹݺͼग़ ͢͜ͱ͕Ͱ͖·͢ɻ

Slide 102

Slide 102 text

$bytecodeSize ม਺ͷਅԼʹ௥Ճɻ 
 ࢖͏ͷ͸͜ͷ 2 ͭͷม਺ͷΈ ※ଞͷม਺΋ʮϩʔΧϧม਺ʯͱ͔ ʮྫ֎ॲཧʯͱ͔༷ʑͳ༻్ͰඞཁͰ͕͢ HelloWorld! ͷ
 ग़ྗͰ͸ඞਢͰ͸ͳ͍ͷͰׂѪ͠·͢ $ciSize ͷਅԼʹҎԼΛ௥Ճ ग़ྗ݁Ռ

Slide 103

Slide 103 text

มߋ

Slide 104

Slide 104 text

$ciIndex = 0 ͷ௥Ճ มߋ

Slide 105

Slide 105 text

Appendix: CallInfoEntry ʹ͍ͭͯ RubyVM ͷ࡞Γํ - ͜ΕͰ CallInfoEntry Λ࢖ͬͯϝιου໊Λࣗಈతʹղܾɼ·ͨҾ਺ͷ਺΋ղ ܾ͍ͯ͠ΔͷͰɼҾ਺͕૿͑ͨ৔߹Ͱ΋ϝιουͷݺͼग़͕͠Ͱ͖ΔΑ͏ʹͳ Γ·͢ɻ - ΋ͪΖΜ CallInfoEntry Ҏ֎ʹ΋ϩʔΧϧม਺ςʔϒϧʢϝιου͕ड͚औΔ Ҿ਺ͷ৘ใʣͰ͋ͬͨΓɼྫ֎ॲཧͰ͋ͬͨΓɼॏཁͳ৘ใ΋͋Γ·͢ɻ - HelloWorld! Λग़ྗͰ͖ͨઌɼ΋͋͠ͳ͕ͨ RubyVM ͷ࣮૷ʹڵຯΛ࣋ͪ࢝ ΊͨΒɼίʔυΛಡΜͰҙຯΛཧղ࣮͠૷ͯ͠Έͯ͸͍͔͕Ͱ͠ΐ͏͔ɻ

Slide 106

Slide 106 text

Appendix: CallInfoEntry ʹ͍ͭͯ RubyVM ͷ࡞Γํ - มߋՕॴ·ͰؚΊͨίʔυ͸ҎԼͷ gist Λࢀর͍ͯͩ͘͠͞ - https://gist.github.com/m3m0r7/226e20c8115caf4a9d43b291861f978b? permalink_comment_id=4686761#gistcomment-4686761

Slide 107

Slide 107 text

Appendix: RubyVM ͷ໋ྩηοτ਺ʹ͍ͭͯ

Slide 108

Slide 108 text

Appendix: RubyVM ͷ໋ྩηοτ਺ʹ͍ͭͯ RubyVM ͷ࡞Γํ - RubyVM ͷ໋ྩηοτͷ਺͸ 100 ݸ΄ͲͰ͢ʢ࣮ମ͸ 200 ݸ΄ͲͰ͕͢ɼ൒ ෼͸໋ྩΛτϨʔε͢ΔͨΊͷ໋ྩηοτͰ͢ʣɻͪͳΈʹ JVM ͸ 150 ݸ΄ ͲͰ͢ʢSE 13ʣɻ - ୯७ͳ HelloWorld! ͷग़ྗ΍ FizzBuzzɼΫΠοΫιʔτͷϨϕϧײͰ͋Ε ͹ɼશ࣮ͯ૷͠ͳͯ͘΋͍͔ͭ͘ͷ໋ྩηοτΛ࣮૷͢Ε͹࣮ߦ͢Δ͜ͱ͕Մ ೳʹͳΓ·͢ɻ

Slide 109

Slide 109 text

Appendix: RubyVM ͷ໋ྩηοτ਺ʹ͍ͭͯ RubyVM ͷ࡞Γํ - RubyVM ͕ఏڙ͍ͯ͠Δ໋ྩηοτ͸ҎԼΛࢀর͍ͯͩ͘͠͞ - https://github.com/ruby/ruby/blob/ruby_3_3/insns.def - PHP Ͱͷ࣮૷ྫ͸ҎԼΛࢀর͍ͯͩ͘͠͞ɻ - https://github.com/m3m0r7/rubyvm-on-php/tree/0.3.3.0/src/VM/Core/ Runtime/Executor/Insn/Processor

Slide 110

Slide 110 text

Appendix: RubyVM ͷ໋ྩηοτ਺ʹ͍ͭͯ RubyVM ͷ࡞Γํ - Ruby 3.3 ͔Β ҎԼͷ 6 ͭͷ໋ྩηοτ͕૿͑·ͨ͠ɻ - Ruby ͸໋ྩηοτ͕૿͑Δͱɼଞͷ໋ྩηοτͷ OpCode ͕ζϨΔΜͰ͢ΑͶ…ɻྫ͑͹ OPT_SEND_WITHOUT_BLOCK ͸ Ruby 3.2 Ͱ͸ 51 ͩͬͨͷ͕ Ruby 3.3 Ͱ͸ 53 ʹͳ͍ͬͯ·͢ɻ
 RubyVM ΛϚϧνόʔδϣϯରԠ͢Δ৔߹͸ɼ͜ͷ࢓༷Λߟྀ͢Δඞཁ͕͋Γ·͢Ͷɻ OpCode χʔϞχοΫ 33 SPLATKW 45 DEFINEDIVAR 58 OPT_NEWARRAY_SEND 135 TRACE_SPLATKW 147 TRACE_DEFINEDIVAR 160 TRACE_OPT_NEWARRAY_SEND

Slide 111

Slide 111 text

Appendix: Ͳ͏΍ͬͯ Ruby ຊମΛࣸܦ͢Δ͔

Slide 112

Slide 112 text

Appendix: Ͳ͏΍ͬͯ Ruby ຊମΛࣸܦ͢Δ͔ RubyVM ͷ࡞Γํ - Ruby ຊମͷɼͱΓΘ͚ VM Λ࣮૷͍ͯ͠ΔՕॴΛͲ͏΍ͬͯࣸܦ͢Δ͔Ͱ͢ ͕ɼجຊతʹ͸ compile.c ʢ https://github.com/ruby/ruby/blob/ruby_3_3/ compile.c ʣΛோΊΔͷ͕Ұ൪खͬऔΓૣ͍Ͱ͢ɻ - `ibf_load_*` ͱॻ͔Ε͍ͯΔؔ਺Λ௥͏ͱྑ͍Ͱ͢ɻ - ͱ͸͍͑ɼͲͷॱ൪Ͱݺͼग़͞ΕΔ͔ʹ͍ͭͯ͸ίʔυΛ௥͍͔ͬͯͳ͍ͱΘ ͔Βͳ͍ͷͰɼେ·͔ʹͲ͏΍ͬͯ௥͑͹͍͍͔࣍ϖʔδͰϑϩʔਤΛॻ͍ͯ ͓͖·͢ɻ

Slide 113

Slide 113 text

Appendix: Ͳ͏΍ͬͯ Ruby ຊମΛࣸܦ͢Δ͔ RubyVM ͷ࡞Γํ rb_iseq_ibf_load ibf_load_setup ibf_load_iseq rb_ibf_load_iseq_compl ete ibf_load_iseq_each ibf_load_code ಡΈࠐΜͩ iseq Λ 
 ฦ͢ ibf_load_small_value ibf_load_object ibf_load_outer_variables ibf_load_param_opt_tabl e ibf_load_param_keywor d ibf_load_insns_info_bod y ibf_load_insns_info_posi tions ibf_load_local_table ibf_load_catch_table ibf_load_iseq_each Ͱݺ͹ΕΔؔ਺܈ ࠶ؼతʹ ibf_load_iseq ͕ݺͼग़͞ΕΔ compile.c ← Ruby ͷ RubyVM::InstructionSequence.load_from_binary ϝιουΛݺͼग़ͨ͠ࡍʹݺ͹ΕΔؔ਺ 
 (https://github.com/ruby/ruby/blob/ruby_3_3/iseq.c#L3870, https://github.com/ruby/ruby/blob/ruby_3_3/iseq.c#L4079) iseqw_s_load_from_bina ry iseq.c ibf_load_id

Slide 114

Slide 114 text

Appendix: ϩʔΧϧม਺ͷׂ౰

Slide 115

Slide 115 text

Appendix: ϩʔΧϧม਺ͷׂ౰ RubyVM ͷ࡞Γํ - ఆٛͨ͠ϢʔβʔϥϯυͷϝιουΛ࣮૷͢Δࡍʹ᪴͘ϙΠϯτͷҰͭͱͯ͠ ϩʔΧϧม਺ͷׂ౰͕͋Γ·͢ɻࢲࣗ਎΋͍᪴ͯ·ͨ͠ɻ - ఆٛ͞ΕͨϝιουΛ࣮ߦ͢Δࡍʹɼ࣮ߦ࣌ʹʢελοΫʹϓογϡ͓ͯ͘͠ ͷͰ͸ͳ͘ʣҾ਺Λ༧ΊϩʔΧϧςʔϒϧʹηοτ͓ͯ͘͠ඞཁ͕͋ΔͷͰ͢ ͕ɼͦͷηοτ͢ΔҾ਺ͷҐஔΛٻΊͳ͚Ε͹ͳΓ·ͤΜɻ - ιʔείʔυΛಡΉݶΓ EP (environment pointer) Λ༻ҙͯ͠ٻΊ͍ͯΔΑ͏ Ͱ͕͢ɼຊࢿྉͰ͸ EP Λ࢖Θͳ͍ͰٻΊ·͢ɻ

Slide 116

Slide 116 text

VM_ENV_DATA_SIZE ϝιου಺ͷม਺܈ ϝιουͷҾ਺ local table size: 4 0 1 2 3 4 6 5 slot index var4 -> slot[3] var3 -> slot[4] var2 -> slot[5] var1 -> slot[6] call info argc: 2 - ྫͷ varN (N ͸ࣗવ਺, N>0) ͸ ʮVM_ENV_DATA_SIZE + local table size - (N - 1)ʯͰٻΊΒΕ·͢ɻͭ·Γ var1 ͸ʮVM_ENV_DATA_SIZE(3) + local table size(4) - N(1)ʯͰ slot[6] ʹ֨ೲ͞ΕΔ͜ͱ͕Θ͔Γ·͢ɻ - ϝιουʹ౉͢Ҿ਺͸ఆٛ͞ΕͨҾ਺ͷॱংͱ͸ٯͰ slot index ʹඥ෇͚Δඞཁ͕͋Γ·͢ - opcode ͷ [gs]etlocal(?:_WC[01]|) ΋͜Εʹैͬͯ஋ΛऔΔΑ͏ʹͳ͍ͬͯ·͢ - 3 ൪໨͔Βελʔτͳͷ͸ɼRubyVM ʹඞཁͳ৘ใΛຒΊࠐΜͰ͍ΔͨΊͬΆ͍ʢʁʣʢVM_ENV_DATA_INDEX_ME_CREF, VM_ENV_DATA_INDEX_SPECVAL, VM_ENV_DATA_INDEX_FLAGSʣ - Ҿ਺͕͋Δ৔߹͸ϝιουݺͼग़͠ͷલʹ༧Ί slot ʹ஋ΛೖΕͯϝιουݺͼग़͠Λߦ͏ඞཁ͕͋Γ·͢ɻྫͰ͸ var1 ͸ slot[6], var2 ͸ slot[5] ʹ༧Ί஋ΛೖΕ͓ͯ͘ඞཁ͕͋Γ·͢ɻ
 ͳ͓ɼvar3 ΍ var4 ͸಺෦ͷ໋ྩγʔέϯεʹ͓͍ͯ setlocal ͕ݺͼग़͞ΕΔͷͰɼ༧Ί஋ΛೖΕ͓ͯ͘ඞཁ͸͋Γ·ͤΜɻ
 (࣮૷ͷώϯτ: https://github.com/m3m0r7/rubyvm-on-php/blob/0.3.3.0/src/VM/Core/Runtime/Executor/CallBlockHelper.php#L121 )

Slide 117

Slide 117 text

Appendix: ϩʔΧϧม਺ͷׂ౰ RubyVM ͷ࡞Γํ - ͞Βʹ RubyVM ͷϩʔΧϧม਺ʹ͸ level ͱ͍͏֓೦͕͋Γ·͢ɻlevel = 0 ͸ݱ ࡏͷ࣮ߦίϯςΩετΛද͢΋ͷͰɼlevel ͕ 1, 2, 3 ... ͱ૿͍͑ͯ͘͝ͱʹ level ͷ਺͚ͩલͷίϯςΩετʢRuby ຊମͷίʔυ্Ͱ͸ VM_ENV_PREV_EP ϚΫ ϩʹ֘౰ʣͷϩʔΧϧม਺Λࢀর͠·͢ʢ࣍ϖʔδਤࢀরʣɻ - ࣮ߦதͷίϯςΩετ͔Βݟͯɼ૬ରҐஔͱͯ͠ม਺ͷ͋ΔίϯςΩετͷ৔ॴ Λදͨ͠ͷ͕ level ͩͱࢥ͏ͱΘ͔Γ΍͍͔͢΋͠Ε·ͤΜɻ - ͦͷͨΊɼ࣮૷࣌ͷ஫ҙ఺ͱͯ͠ίϯςΩετ͸ḪΕΔΑ͏ʹ͓ͯ͘͠ඞཁ͕͋ Γ·͢ɻ

Slide 118

Slide 118 text

var3 ΁ͷ୅ೖ͸࣮ߦ࣌ͷίϯςΩετͳͷͰ 
 level ͸ 0 Ͱ setlocal_WC0 ͕૸Δ var3 ͕ఆٛ͞Ε͍ͯΔͷ͸ҰͭલͷίϯςΩετͳͷͰ 
 ࣮ߦதͷίϯςΩετ͔Βݟͯ level ͸ 1 Ͱ getlocal_WC1 ͕૸Δ var3 ͕ఆٛ͞Ε͍ͯΔͷ͸ҰͭલͷίϯςΩετͳͷͰ 
 ࣮ߦதͷίϯςΩετ͔Βݟͯ level ͸ 1 Ͱ setlocal_WC1 ͕૸Δ var1, var2 ͕ఆٛ͞Ε͍ͯΔͷ͸࣮ߦதͷίϯςΩετͳͷͰ level ͸ 0 Ͱ getlocal_WC0 ͕૸Δ ݺͼग़͠ʢ಺෦తʹ send/ opt_send_without_block ͳͲͷ opcode ͕ݺͼग़͞Εͱ͖ʣ͕͋Δͱ ࣮ߦίϯςΩετ͕มΘΔ