Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HexDevs 2021

HexDevs 2021

Slides from my session on HexDevs with TenderJIT

Aaron Patterson

October 11, 2021
Tweet

More Decks by Aaron Patterson

Other Decks in Technology

Transcript

  1. Stack Machine 5 + 3 push 5 push 3 plus

    Source Code Machine Code 5 3 8 Machine Stack
  2. YARV Instructions $ cat thing.rb 5 + 3 $ ruby

    --dump=insns thing.rb == disasm: #<ISeq:<main>@thing.rb:1 (1,0)-(1,5)> (catch: FALSE) 0000 putobject 5 ( 1)[Li] 0002 putobject 3 0004 opt_plus <calldata!mid:+, argc:1, ARGS_SIMPLE>[CcCr] 0006 leave
  3. Stack Machine 5 + 3 push 5 push 3 plus

    Source Code Machine Code 5 3 Machine Stack PC (Program Counter) SP (Stack Pointer)
  4. Stack Machine 5 + 3 push 5 push 3 plus

    Source Code Machine Code 5 3 Machine Stack 0 1 2 … -1 1 0
  5. Control Frame Pointer Contains Information About the Current Frame def

    baz end def bar baz end def foo bar end foo Main Control Frame Pointers foo bar baz
  6. Control Frame Pointer Contains Information About the Current Frame def

    add 5 + 3 end Field Description iseq Instruction Sequence (the method) PC Program Counter (what are we running right now?) SP Stack Pointer (what is on the stack?) opt_plus 3 5
  7. Register Machine 5 + 3 mov r1, 5 mov r2,

    3 add r1, r2 Source Code Machine Code Machine Registers Register Name Value r1 r2 r3 … 5 3 8
  8. x86 Instructions int main(int argc, char *argv[]) { int x

    = 5; int y = 3; return x + y; } 100003fa2: mov dword ptr [rbp - 20], 5 100003fa9: mov dword ptr [rbp - 24], 3 100003fb0: mov eax, dword ptr [rbp - 20] 100003fb3: add eax, dword ptr [rbp - 24] Source Code Machine Code
  9. x86 Instructions int main(int argc, char *argv[]) { int x

    = 5; int y = 3; return x + y; } 100003fa2: mov dword ptr [rbp - 20], 5 100003fa9: mov dword ptr [rbp - 24], 3 100003fb0: mov eax, dword ptr [rbp - 20] 100003fb3: add eax, dword ptr [rbp - 24] Source Code Machine Code
  10. x86 Instructions int main(int argc, char *argv[]) { int x

    = 5; int y = 3; return x + y; } 100003fa2: mov dword ptr [rbp - 20], 5 100003fa9: mov dword ptr [rbp - 24], 3 100003fb0: mov eax, dword ptr [rbp - 20] 100003fb3: add eax, dword ptr [rbp - 24] Source Code Machine Code
  11. x86 Instructions int main(int argc, char *argv[]) { int x

    = 5; int y = 3; return x + y; } 100003fa2: mov dword ptr [rbp - 20], 5 100003fa9: mov dword ptr [rbp - 24], 3 100003fb0: mov eax, dword ptr [rbp - 20] 100003fb3: add eax, dword ptr [rbp - 24] Source Code Machine Code Register “EAX”
  12. x86 Instructions int main(int argc, char *argv[]) { int x

    = 5; int y = 3; return x + y; } 100003fa2: mov dword ptr [rbp - 20], 5 100003fa9: mov dword ptr [rbp - 24], 3 100003fb0: mov eax, dword ptr [rbp - 20] 100003fb3: add eax, dword ptr [rbp - 24] Source Code Machine Code
  13. Machine Code in Ruby require "fisk" require "fisk/helpers" # Allocate

    executable memory jitbuf = Fisk::Helpers.jitbuffer 4096 fisk = Fisk.new # Add 5 and 3 in machine language, and write to the JIT buffer fisk.asm(jitbuf) do mov rax, imm(5) # Store 5 in RAX mov r9, imm(3) # Store 3 in R9 add rax, r9 # Add R9 to RAX ret # Return from this function end # Define an add method define_method :add, &jitbuf.to_function([], Fiddle::TYPE_INT) p add # Call add and print it