2021-03-12 12:25:58 -05:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "vm_core.h"
|
2020-12-08 16:54:41 -05:00
|
|
|
#include "insns.inc"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "vm_sync.h"
|
|
|
|
#include "vm_callinfo.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "internal/compile.h"
|
|
|
|
#include "internal/class.h"
|
|
|
|
#include "insns_info.inc"
|
2021-03-06 18:46:56 -05:00
|
|
|
#include "yjit.h"
|
|
|
|
#include "yjit_iface.h"
|
|
|
|
#include "yjit_codegen.h"
|
|
|
|
#include "yjit_core.h"
|
|
|
|
#include "yjit_hooks.inc"
|
2021-02-12 17:12:18 -05:00
|
|
|
#include "darray.h"
|
2021-01-22 14:26:20 -05:00
|
|
|
|
|
|
|
#if HAVE_LIBCAPSTONE
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
#include <capstone/capstone.h>
|
2021-01-22 14:26:20 -05:00
|
|
|
#endif
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
static VALUE mYjit;
|
|
|
|
static VALUE cYjitBlock;
|
|
|
|
static VALUE cYjitDisasm;
|
|
|
|
static VALUE cYjitDisasmInsn;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
#if RUBY_DEBUG
|
2021-01-26 15:21:47 -05:00
|
|
|
static int64_t vm_insns_count = 0;
|
2021-01-27 16:13:27 -05:00
|
|
|
static int64_t exit_op_count[VM_INSTRUCTION_SIZE] = { 0 };
|
2021-02-16 11:15:29 -05:00
|
|
|
int64_t rb_compiled_iseq_count = 0;
|
2021-03-06 18:46:56 -05:00
|
|
|
struct rb_yjit_runtime_counters yjit_runtime_counters = { 0 };
|
2021-02-25 15:10:38 -05:00
|
|
|
#endif
|
2021-01-26 15:21:47 -05:00
|
|
|
|
2021-02-16 11:15:29 -05:00
|
|
|
// Machine code blocks (executable memory)
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
extern codeblock_t *cb;
|
2021-02-01 17:17:26 -05:00
|
|
|
extern codeblock_t *ocb;
|
2021-02-16 11:15:29 -05:00
|
|
|
|
2021-01-26 15:21:47 -05:00
|
|
|
// Hash table of encoded instructions
|
|
|
|
extern st_table *rb_encoded_insn_data;
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
struct rb_yjit_options rb_yjit_opts;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
static const rb_data_type_t yjit_block_type = {
|
|
|
|
"YJIT/Block",
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
{0, 0, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// Write the YJIT entry point pre-call bytes
|
2021-02-25 15:10:38 -05:00
|
|
|
void
|
2020-12-08 17:19:28 -05:00
|
|
|
cb_write_pre_call_bytes(codeblock_t* cb)
|
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
for (size_t i = 0; i < sizeof(yjit_with_ec_pre_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, yjit_with_ec_pre_call_bytes[i]);
|
2020-12-08 17:19:28 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// Write the YJIT exit post-call bytes
|
2021-02-25 15:10:38 -05:00
|
|
|
void
|
2020-12-08 17:19:28 -05:00
|
|
|
cb_write_post_call_bytes(codeblock_t* cb)
|
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
for (size_t i = 0; i < sizeof(yjit_with_ec_post_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, yjit_with_ec_post_call_bytes[i]);
|
2020-12-08 17:19:28 -05:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:03:11 -05:00
|
|
|
// Get the PC for a given index in an iseq
|
|
|
|
VALUE *iseq_pc_at_idx(const rb_iseq_t *iseq, uint32_t insn_idx)
|
|
|
|
{
|
|
|
|
RUBY_ASSERT(iseq != NULL);
|
|
|
|
RUBY_ASSERT(insn_idx < iseq->body->iseq_size);
|
|
|
|
VALUE *encoded = iseq->body->iseq_encoded;
|
|
|
|
VALUE *pc = &encoded[insn_idx];
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// Keep track of mapping from instructions to generated code
|
|
|
|
// See comment for rb_encoded_insn_data in iseq.c
|
|
|
|
void
|
|
|
|
map_addr2insn(void *code_ptr, int insn)
|
|
|
|
{
|
|
|
|
const void * const *table = rb_vm_get_insns_address_table();
|
|
|
|
const void * const translated_address = table[insn];
|
|
|
|
st_data_t encoded_insn_data;
|
|
|
|
if (st_lookup(rb_encoded_insn_data, (st_data_t)translated_address, &encoded_insn_data)) {
|
|
|
|
st_insert(rb_encoded_insn_data, (st_data_t)code_ptr, encoded_insn_data);
|
|
|
|
}
|
|
|
|
else {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_bug("yjit: failed to find info for original instruction while dealing with addr2insn");
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
opcode_at_pc(const rb_iseq_t *iseq, const VALUE *pc)
|
|
|
|
{
|
|
|
|
const VALUE at_pc = *pc;
|
|
|
|
if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
|
|
|
|
return rb_vm_insn_addr2opcode((const void *)at_pc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (int)at_pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 17:10:52 -05:00
|
|
|
// Verify that calling with cd on receiver goes to callee
|
|
|
|
void
|
|
|
|
check_cfunc_dispatch(VALUE receiver, struct rb_call_data *cd, void *callee, rb_callable_method_entry_t *compile_time_cme)
|
|
|
|
{
|
|
|
|
if (METHOD_ENTRY_INVALIDATED(compile_time_cme)) {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_bug("yjit: output code uses invalidated cme %p", (void *)compile_time_cme);
|
2021-01-15 17:10:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool callee_correct = false;
|
|
|
|
const rb_callable_method_entry_t *cme = rb_callable_method_entry(CLASS_OF(receiver), vm_ci_mid(cd->ci));
|
|
|
|
if (cme->def->type == VM_METHOD_TYPE_CFUNC) {
|
|
|
|
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
|
|
|
|
if ((void *)cfunc->func == callee) {
|
|
|
|
callee_correct = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!callee_correct) {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_bug("yjit: output code calls wrong method cd->cc->klass: %p", (void *)cd->cc->klass);
|
2021-01-15 17:10:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MJIT_FUNC_EXPORTED VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
|
|
|
|
|
|
|
bool
|
|
|
|
cfunc_needs_frame(const rb_method_cfunc_t *cfunc)
|
|
|
|
{
|
|
|
|
void* fptr = (void*)cfunc->func;
|
|
|
|
|
|
|
|
// Leaf C functions do not need a stack frame
|
|
|
|
// or a stack overflow check
|
|
|
|
return !(
|
|
|
|
// Hash#key?
|
|
|
|
fptr == (void*)rb_hash_has_key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// GC root for interacting with the GC
|
2021-03-06 18:46:56 -05:00
|
|
|
struct yjit_root_struct {
|
2021-02-16 16:50:09 -05:00
|
|
|
int unused; // empty structs are not legal in C99
|
|
|
|
};
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
static void
|
2021-03-06 18:46:56 -05:00
|
|
|
block_array_shuffle_remove(rb_yjit_block_array_t blocks, block_t *to_remove) {
|
2021-02-25 15:10:38 -05:00
|
|
|
block_t **elem;
|
|
|
|
rb_darray_foreach(blocks, i, elem) {
|
|
|
|
if (*elem == to_remove) {
|
|
|
|
// Remove the current element by moving the last element here then popping.
|
|
|
|
*elem = rb_darray_get(blocks, rb_darray_size(blocks) - 1);
|
|
|
|
rb_darray_pop_back(blocks);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
// Map cme_or_cc => [block]
|
|
|
|
static st_table *method_lookup_dependency;
|
2020-12-08 16:54:41 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
add_lookup_dependency_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
|
|
|
|
{
|
2021-02-25 15:10:38 -05:00
|
|
|
block_t *new_block = (block_t *)data;
|
2021-01-14 16:58:20 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_block_array_t blocks = NULL;
|
2020-12-08 16:54:41 -05:00
|
|
|
if (existing) {
|
2021-03-06 18:46:56 -05:00
|
|
|
blocks = (rb_yjit_block_array_t)*value;
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
2021-02-25 15:10:38 -05:00
|
|
|
if (!rb_darray_append(&blocks, new_block)) {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_bug("yjit: failed to add method lookup dependency"); // TODO: we could bail out of compiling instead
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
2021-02-12 17:12:18 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
*value = (st_data_t)blocks;
|
2020-12-08 16:54:41 -05:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:01:16 -05:00
|
|
|
// Hash table of BOP blocks
|
|
|
|
static st_table *blocks_assuming_bops;
|
|
|
|
|
|
|
|
bool
|
|
|
|
assume_bop_not_redefined(block_t *block, int redefined_flag, enum ruby_basic_operators bop)
|
|
|
|
{
|
|
|
|
if (BASIC_OP_UNREDEFINED_P(bop, redefined_flag)) {
|
|
|
|
if (blocks_assuming_bops) {
|
|
|
|
st_insert(blocks_assuming_bops, (st_data_t)block, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
// Remember that the currently compiling block is only valid while cme and cc are valid
|
2020-12-08 16:54:41 -05:00
|
|
|
void
|
2021-02-12 17:12:18 -05:00
|
|
|
assume_method_lookup_stable(const struct rb_callcache *cc, const rb_callable_method_entry_t *cme, block_t *block)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
2021-01-22 13:29:09 -05:00
|
|
|
RUBY_ASSERT(block != NULL);
|
2021-02-12 17:12:18 -05:00
|
|
|
RUBY_ASSERT(block->dependencies.cc == 0 && block->dependencies.cme == 0);
|
2021-02-25 15:10:38 -05:00
|
|
|
st_update(method_lookup_dependency, (st_data_t)cme, add_lookup_dependency_i, (st_data_t)block);
|
2021-02-12 17:12:18 -05:00
|
|
|
block->dependencies.cme = (VALUE)cme;
|
2021-02-25 15:10:38 -05:00
|
|
|
st_update(method_lookup_dependency, (st_data_t)cc, add_lookup_dependency_i, (st_data_t)block);
|
2021-02-12 17:12:18 -05:00
|
|
|
block->dependencies.cc = (VALUE)cc;
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
static st_table *blocks_assuming_single_ractor_mode;
|
|
|
|
|
|
|
|
// Can raise NoMemoryError.
|
|
|
|
RBIMPL_ATTR_NODISCARD()
|
|
|
|
bool
|
|
|
|
assume_single_ractor_mode(block_t *block) {
|
|
|
|
if (rb_multi_ractor_p()) return false;
|
|
|
|
|
|
|
|
st_insert(blocks_assuming_single_ractor_mode, (st_data_t)block, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static st_table *blocks_assuming_stable_global_constant_state;
|
|
|
|
|
|
|
|
// Assume that the global constant state has not changed since call to this function.
|
|
|
|
// Can raise NoMemoryError.
|
|
|
|
RBIMPL_ATTR_NODISCARD()
|
|
|
|
bool
|
|
|
|
assume_stable_global_constant_state(block_t *block) {
|
|
|
|
st_insert(blocks_assuming_stable_global_constant_state, (st_data_t)block, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
static int
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_root_mark_i(st_data_t k, st_data_t v, st_data_t ignore)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
2021-02-12 17:12:18 -05:00
|
|
|
// Lifetime notes: cc and cme get added in pairs into the table. One of
|
|
|
|
// them should become invalid before dying. When one of them invalidate we
|
|
|
|
// remove the pair from the table. Blocks remove themself from the table
|
|
|
|
// when they die.
|
|
|
|
rb_gc_mark_movable((VALUE)k);
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
method_lookup_dep_table_update_keys(st_data_t *key, st_data_t *value, st_data_t argp, int existing)
|
|
|
|
{
|
|
|
|
*key = rb_gc_location(rb_gc_location((VALUE)*key));
|
2020-12-08 16:54:41 -05:00
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
static int
|
|
|
|
replace_all(st_data_t key, st_data_t value, st_data_t argp, int error)
|
|
|
|
{
|
|
|
|
return ST_REPLACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC callback during compaction
|
|
|
|
static void
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_root_update_references(void *ptr)
|
2021-02-12 17:12:18 -05:00
|
|
|
{
|
|
|
|
if (method_lookup_dependency) {
|
|
|
|
if (st_foreach_with_replace(method_lookup_dependency, replace_all, method_lookup_dep_table_update_keys, 0)) {
|
|
|
|
RUBY_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 11:54:37 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_branches_update_references();
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// GC callback during mark phase
|
|
|
|
static void
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_root_mark(void *ptr)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
if (method_lookup_dependency) {
|
2021-03-06 18:46:56 -05:00
|
|
|
st_foreach(method_lookup_dependency, yjit_root_mark_i, 0);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_root_free(void *ptr)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
// Do nothing. The root lives as long as the process.
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_root_memsize(const void *ptr)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
// Count off-gc-heap allocation size of the dependency table
|
|
|
|
return st_memsize(method_lookup_dependency); // TODO: more accurate accounting
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom type for interacting with the GC
|
|
|
|
// TODO: compaction support
|
|
|
|
// TODO: make this write barrier protected
|
2021-03-06 18:46:56 -05:00
|
|
|
static const rb_data_type_t yjit_root_type = {
|
|
|
|
"yjit_root",
|
|
|
|
{yjit_root_mark, yjit_root_free, yjit_root_memsize, yjit_root_update_references},
|
2020-12-08 16:54:41 -05:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
|
|
|
// Callback when cme or cc become invalid
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_method_lookup_change(VALUE cme_or_cc)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
2021-01-14 16:58:20 -05:00
|
|
|
if (!method_lookup_dependency)
|
|
|
|
return;
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-01-29 12:07:18 -05:00
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(cme_or_cc, imemo_ment) || IMEMO_TYPE_P(cme_or_cc, imemo_callcache));
|
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
// Invalidate all regions that depend on the cme or cc
|
|
|
|
st_data_t key = (st_data_t)cme_or_cc, image;
|
|
|
|
if (st_delete(method_lookup_dependency, &key, &image)) {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_block_array_t array = (void *)image;
|
2021-02-25 15:10:38 -05:00
|
|
|
block_t **elem;
|
2021-01-29 12:07:18 -05:00
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
rb_darray_foreach(array, i, elem) {
|
2021-02-25 15:10:38 -05:00
|
|
|
invalidate_block_version(*elem);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
rb_darray_free(array);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
2021-01-29 12:07:18 -05:00
|
|
|
|
|
|
|
RB_VM_LOCK_LEAVE();
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
// Remove a block from the method lookup dependency table
|
|
|
|
static void
|
|
|
|
remove_method_lookup_dependency(VALUE cc_or_cme, block_t *block)
|
|
|
|
{
|
|
|
|
st_data_t key = (st_data_t)cc_or_cme, image;
|
|
|
|
if (st_lookup(method_lookup_dependency, key, &image)) {
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_block_array_t array = (void *)image;
|
2021-02-12 17:12:18 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
block_array_shuffle_remove(array, block);
|
2021-02-12 17:12:18 -05:00
|
|
|
|
|
|
|
if (rb_darray_size(array) == 0) {
|
|
|
|
st_delete(method_lookup_dependency, &key, NULL);
|
|
|
|
rb_darray_free(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_unlink_method_lookup_dependency(block_t *block)
|
2021-02-12 17:12:18 -05:00
|
|
|
{
|
|
|
|
if (block->dependencies.cc) remove_method_lookup_dependency(block->dependencies.cc, block);
|
|
|
|
if (block->dependencies.cme) remove_method_lookup_dependency(block->dependencies.cme, block);
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_block_assumptions_free(block_t *block)
|
2021-02-25 15:10:38 -05:00
|
|
|
{
|
|
|
|
st_data_t as_st_data = (st_data_t)block;
|
|
|
|
if (blocks_assuming_stable_global_constant_state) {
|
|
|
|
st_delete(blocks_assuming_stable_global_constant_state, &as_st_data, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blocks_assuming_single_ractor_mode) {
|
|
|
|
st_delete(blocks_assuming_single_ractor_mode, &as_st_data, NULL);
|
|
|
|
}
|
2021-03-09 14:01:16 -05:00
|
|
|
|
|
|
|
if (blocks_assuming_bops) {
|
|
|
|
st_delete(blocks_assuming_bops, &as_st_data, NULL);
|
|
|
|
}
|
2021-02-25 15:10:38 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
VALUE *encoded = (VALUE *)iseq->body->iseq_encoded;
|
|
|
|
|
2020-12-10 00:06:10 -05:00
|
|
|
// Compile a block version starting at the first instruction
|
2021-03-01 20:43:58 -05:00
|
|
|
uint8_t* code_ptr = gen_entry_point(iseq, 0, ec);
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-01-13 14:14:16 -05:00
|
|
|
if (code_ptr)
|
|
|
|
{
|
|
|
|
// Map the code address to the corresponding opcode
|
|
|
|
int first_opcode = opcode_at_pc(iseq, &encoded[0]);
|
|
|
|
map_addr2insn(code_ptr, first_opcode);
|
|
|
|
encoded[0] = (VALUE)code_ptr;
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|
2020-12-10 00:06:10 -05:00
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
struct yjit_block_itr {
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
const rb_iseq_t *iseq;
|
|
|
|
VALUE list;
|
|
|
|
};
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
/* Get a list of the YJIT blocks associated with `rb_iseq` */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
static VALUE
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_blocks_for(VALUE mod, VALUE rb_iseq)
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
{
|
2021-02-04 13:00:29 -05:00
|
|
|
if (CLASS_OF(rb_iseq) != rb_cISeq) {
|
2021-02-04 12:29:36 -05:00
|
|
|
return rb_ary_new();
|
|
|
|
}
|
2021-02-12 17:12:18 -05:00
|
|
|
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
const rb_iseq_t *iseq = rb_iseqw_to_iseq(rb_iseq);
|
|
|
|
|
2021-03-04 12:05:18 -05:00
|
|
|
VALUE all_versions = rb_ary_new();
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_darray_for(iseq->body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t versions = rb_darray_get(iseq->body->yjit_blocks, version_array_idx);
|
2021-03-04 15:31:37 -05:00
|
|
|
|
|
|
|
rb_darray_for(versions, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(versions, block_idx);
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
VALUE rb_block = TypedData_Wrap_Struct(cYjitBlock, &yjit_block_type, block);
|
2021-02-12 17:12:18 -05:00
|
|
|
rb_ary_push(all_versions, rb_block);
|
|
|
|
}
|
|
|
|
}
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
return all_versions;
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
/* Get the address of the the code associated with a YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
static VALUE
|
|
|
|
block_address(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-06 18:46:56 -05:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
2021-01-24 18:21:18 -05:00
|
|
|
uint8_t* code_addr = cb_get_ptr(cb, block->start_pos);
|
|
|
|
return LONG2NUM((intptr_t)code_addr);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
/* Get the machine code for YJIT::Block as a binary string */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
static VALUE
|
|
|
|
block_code(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-06 18:46:56 -05:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-01-22 12:22:34 -05:00
|
|
|
return (VALUE)rb_str_new(
|
|
|
|
(const char*)cb->mem_block + block->start_pos,
|
|
|
|
block->end_pos - block->start_pos
|
|
|
|
);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the start index in the Instruction Sequence that corresponds to this
|
2021-03-06 18:46:56 -05:00
|
|
|
* YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
static VALUE
|
|
|
|
iseq_start_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-06 18:46:56 -05:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
|
|
|
return INT2NUM(block->blockid.idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the end index in the Instruction Sequence that corresponds to this
|
2021-03-06 18:46:56 -05:00
|
|
|
* YJIT::Block */
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
static VALUE
|
|
|
|
iseq_end_index(VALUE self)
|
|
|
|
{
|
|
|
|
block_t * block;
|
2021-03-06 18:46:56 -05:00
|
|
|
TypedData_Get_Struct(self, block_t, &yjit_block_type, block);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
|
|
|
return INT2NUM(block->end_idx);
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
static int
|
|
|
|
block_invalidation_iterator(st_data_t key, st_data_t value, st_data_t data) {
|
|
|
|
block_t *block = (block_t *)key;
|
|
|
|
invalidate_block_version(block); // Thankfully, st_table supports deleteing while iterating
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:01:16 -05:00
|
|
|
/* Called when a basic operation is redefined */
|
|
|
|
void
|
|
|
|
rb_yjit_bop_redefined(VALUE klass, const rb_method_entry_t *me, enum ruby_basic_operators bop)
|
|
|
|
{
|
2021-03-11 11:19:02 -05:00
|
|
|
if (blocks_assuming_bops) {
|
|
|
|
st_foreach(blocks_assuming_bops, block_invalidation_iterator, 0);
|
|
|
|
}
|
2021-03-09 14:01:16 -05:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:09:34 -05:00
|
|
|
/* Called when the constant state changes */
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_constant_state_changed(void)
|
2021-02-03 18:09:34 -05:00
|
|
|
{
|
2021-02-25 15:10:38 -05:00
|
|
|
if (blocks_assuming_stable_global_constant_state) {
|
|
|
|
st_foreach(blocks_assuming_stable_global_constant_state, block_invalidation_iterator, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_before_ractor_spawn(void)
|
2021-02-25 15:10:38 -05:00
|
|
|
{
|
|
|
|
if (blocks_assuming_single_ractor_mode) {
|
|
|
|
st_foreach(blocks_assuming_single_ractor_mode, block_invalidation_iterator, 0);
|
|
|
|
}
|
2021-02-03 18:09:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:26:20 -05:00
|
|
|
#if HAVE_LIBCAPSTONE
|
2021-03-06 18:46:56 -05:00
|
|
|
static const rb_data_type_t yjit_disasm_type = {
|
|
|
|
"YJIT/Disasm",
|
2021-01-22 14:26:20 -05:00
|
|
|
{0, (void(*)(void *))cs_close, 0, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
static VALUE
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_disasm_init(VALUE klass)
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
{
|
|
|
|
csh * handle;
|
2021-03-06 18:46:56 -05:00
|
|
|
VALUE disasm = TypedData_Make_Struct(klass, csh, &yjit_disasm_type, handle);
|
2021-01-22 14:26:20 -05:00
|
|
|
cs_open(CS_ARCH_X86, CS_MODE_64, handle);
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
return disasm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_disasm(VALUE self, VALUE code, VALUE from)
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
csh * handle;
|
|
|
|
cs_insn *insns;
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
TypedData_Get_Struct(self, csh, &yjit_disasm_type, handle);
|
2021-01-22 15:24:04 -05:00
|
|
|
count = cs_disasm(*handle, (uint8_t*)StringValuePtr(code), RSTRING_LEN(code), NUM2INT(from), 0, &insns);
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
VALUE insn_list = rb_ary_new_capa(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
VALUE vals = rb_ary_new_from_args(3, LONG2NUM(insns[i].address),
|
|
|
|
rb_str_new2(insns[i].mnemonic),
|
|
|
|
rb_str_new2(insns[i].op_str));
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_ary_push(insn_list, rb_struct_alloc(cYjitDisasmInsn, vals));
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
}
|
|
|
|
cs_free(insns, count);
|
|
|
|
return insn_list;
|
|
|
|
}
|
2021-01-22 14:26:20 -05:00
|
|
|
#endif
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
|
2021-03-02 18:27:50 -05:00
|
|
|
static VALUE
|
|
|
|
at_exit_print_stats(RB_BLOCK_CALL_FUNC_ARGLIST(yieldarg, data))
|
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
// Defined in yjit.rb
|
|
|
|
rb_funcall(mYjit, rb_intern("_print_stats"), 0);
|
2021-03-02 18:27:50 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// Primitive called in yjit.rb. Export all runtime counters as a Ruby hash.
|
2021-03-02 18:27:50 -05:00
|
|
|
static VALUE
|
|
|
|
get_stat_counters(rb_execution_context_t *ec, VALUE self)
|
|
|
|
{
|
|
|
|
#if RUBY_DEBUG
|
2021-03-06 18:46:56 -05:00
|
|
|
if (!rb_yjit_opts.gen_stats) return Qnil;
|
2021-03-02 18:27:50 -05:00
|
|
|
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
int64_t *counter_reader = (int64_t *)&yjit_runtime_counters;
|
|
|
|
int64_t *counter_reader_end = &yjit_runtime_counters.last_member;
|
2021-03-02 18:27:50 -05:00
|
|
|
|
|
|
|
// Iterate through comma separated counter name list
|
2021-03-06 18:46:56 -05:00
|
|
|
char *name_reader = yjit_counter_names;
|
|
|
|
char *counter_name_end = yjit_counter_names + sizeof(yjit_counter_names);
|
2021-03-02 18:27:50 -05:00
|
|
|
while (name_reader < counter_name_end && counter_reader < counter_reader_end) {
|
|
|
|
if (*name_reader == ',' || *name_reader == ' ') {
|
|
|
|
name_reader++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute name of counter name
|
|
|
|
int name_len;
|
|
|
|
char *name_end;
|
|
|
|
{
|
|
|
|
name_end = strchr(name_reader, ',');
|
|
|
|
if (name_end == NULL) break;
|
|
|
|
name_len = (int)(name_end - name_reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put counter into hash
|
|
|
|
VALUE key = ID2SYM(rb_intern2(name_reader, name_len));
|
|
|
|
VALUE value = LL2NUM((long long)*counter_reader);
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
|
|
|
|
counter_reader++;
|
|
|
|
name_reader = name_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
return hash;
|
|
|
|
#else
|
|
|
|
return Qnil;
|
|
|
|
#endif // if RUBY_DEBUG
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// Primitive called in yjit.rb. Zero out all the counters.
|
2021-03-03 16:56:24 -05:00
|
|
|
static VALUE
|
|
|
|
reset_stats_bang(rb_execution_context_t *ec, VALUE self)
|
|
|
|
{
|
|
|
|
#if RUBY_DEBUG
|
|
|
|
vm_insns_count = 0;
|
|
|
|
rb_compiled_iseq_count = 0;
|
|
|
|
memset(&exit_op_count, 0, sizeof(exit_op_count));
|
2021-03-06 18:46:56 -05:00
|
|
|
memset(&yjit_runtime_counters, 0, sizeof(yjit_runtime_counters));
|
2021-03-03 16:56:24 -05:00
|
|
|
#endif // if RUBY_DEBUG
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
#include "yjit.rbinc"
|
2021-03-02 18:27:50 -05:00
|
|
|
|
2021-01-27 16:13:27 -05:00
|
|
|
#if RUBY_DEBUG
|
2021-03-06 18:46:56 -05:00
|
|
|
// implementation for --yjit-stats
|
2021-01-27 16:13:27 -05:00
|
|
|
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_collect_vm_usage_insn(int insn)
|
2021-01-27 16:13:27 -05:00
|
|
|
{
|
|
|
|
vm_insns_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VALUE *
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_count_side_exit_op(const VALUE *exit_pc)
|
2021-01-27 16:13:27 -05:00
|
|
|
{
|
|
|
|
int insn = rb_vm_insn_addr2opcode((const void *)*exit_pc);
|
|
|
|
exit_op_count[insn]++;
|
|
|
|
return exit_pc; // This function must return exit_pc!
|
|
|
|
}
|
|
|
|
|
|
|
|
struct insn_count {
|
|
|
|
int64_t insn;
|
|
|
|
int64_t count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
insn_count_sort_comp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct insn_count *count_a = a;
|
|
|
|
const struct insn_count *count_b = b;
|
|
|
|
if (count_a->count > count_b->count) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (count_a->count < count_b->count) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct insn_count insn_sorting_buffer[VM_INSTRUCTION_SIZE];
|
|
|
|
static const struct insn_count *
|
|
|
|
sort_insn_count_array(int64_t *array)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < VM_INSTRUCTION_SIZE; i++) {
|
|
|
|
insn_sorting_buffer[i] = (struct insn_count) { i, array[i] };
|
|
|
|
}
|
|
|
|
qsort(insn_sorting_buffer, VM_INSTRUCTION_SIZE, sizeof(insn_sorting_buffer[0]), &insn_count_sort_comp);
|
|
|
|
return insn_sorting_buffer;
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:21:47 -05:00
|
|
|
static void
|
2021-01-27 16:13:27 -05:00
|
|
|
print_insn_count_buffer(const struct insn_count *buffer, int how_many, int left_pad)
|
2021-01-26 15:21:47 -05:00
|
|
|
{
|
2021-01-27 16:13:27 -05:00
|
|
|
size_t longest_insn_len = 0;
|
2021-01-28 22:56:38 -05:00
|
|
|
size_t total_exit_count = 0;
|
|
|
|
|
2021-01-27 16:13:27 -05:00
|
|
|
for (int i = 0; i < how_many; i++) {
|
|
|
|
const char *instruction_name = insn_name(buffer[i].insn);
|
|
|
|
size_t len = strlen(instruction_name);
|
|
|
|
if (len > longest_insn_len) {
|
|
|
|
longest_insn_len = len;
|
|
|
|
}
|
2021-01-28 22:56:38 -05:00
|
|
|
total_exit_count += buffer[i].count;
|
2021-01-27 16:13:27 -05:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:18:43 -05:00
|
|
|
fprintf(stderr, "total_exit_count: %10ld\n", total_exit_count);
|
|
|
|
fprintf(stderr, "most frequent exit op:\n");
|
|
|
|
|
2021-01-27 16:13:27 -05:00
|
|
|
for (int i = 0; i < how_many; i++) {
|
|
|
|
const char *instruction_name = insn_name(buffer[i].insn);
|
|
|
|
size_t padding = left_pad + longest_insn_len - strlen(instruction_name);
|
|
|
|
for (size_t j = 0; j < padding; j++) {
|
|
|
|
fputc(' ', stderr);
|
|
|
|
}
|
2021-01-28 22:56:38 -05:00
|
|
|
double percent = 100 * buffer[i].count / (double)total_exit_count;
|
|
|
|
fprintf(stderr, "%s: %10" PRId64 " (%.1f%%)\n", instruction_name, buffer[i].count, percent);
|
2021-01-26 15:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:13:27 -05:00
|
|
|
__attribute__((destructor))
|
|
|
|
static void
|
2021-03-06 18:46:56 -05:00
|
|
|
print_yjit_stats(void)
|
2021-01-26 15:21:47 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
if (!rb_yjit_opts.gen_stats) return;
|
2021-01-27 16:13:27 -05:00
|
|
|
|
|
|
|
const struct insn_count *sorted_exit_ops = sort_insn_count_array(exit_op_count);
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
double total_insns_count = vm_insns_count + yjit_runtime_counters.exec_instruction;
|
|
|
|
double ratio = yjit_runtime_counters.exec_instruction / total_insns_count;
|
2021-01-27 16:13:27 -05:00
|
|
|
|
2021-02-16 11:15:29 -05:00
|
|
|
fprintf(stderr, "compiled_iseq_count: %10" PRId64 "\n", rb_compiled_iseq_count);
|
2021-02-01 17:17:26 -05:00
|
|
|
fprintf(stderr, "main_block_code_size: %6.1f MiB\n", ((double)cb->write_pos) / 1048576.0);
|
|
|
|
fprintf(stderr, "side_block_code_size: %6.1f MiB\n", ((double)ocb->write_pos) / 1048576.0);
|
2021-01-27 16:13:27 -05:00
|
|
|
fprintf(stderr, "vm_insns_count: %10" PRId64 "\n", vm_insns_count);
|
2021-03-06 18:46:56 -05:00
|
|
|
fprintf(stderr, "yjit_exec_insns_count: %10" PRId64 "\n", yjit_runtime_counters.exec_instruction);
|
|
|
|
fprintf(stderr, "ratio_in_yjit: %9.1f%%\n", ratio * 100);
|
2021-01-28 22:56:38 -05:00
|
|
|
print_insn_count_buffer(sorted_exit_ops, 10, 4);
|
2021-03-02 18:27:50 -05:00
|
|
|
//print_runtime_counters();
|
2021-01-26 15:21:47 -05:00
|
|
|
}
|
2021-01-27 16:13:27 -05:00
|
|
|
#endif // if RUBY_DEBUG
|
2021-01-26 15:21:47 -05:00
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_iseq_mark(const struct rb_iseq_constant_body *body)
|
2021-02-12 17:12:18 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 12:05:18 -05:00
|
|
|
|
2021-03-04 15:31:37 -05:00
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
2021-03-04 12:05:18 -05:00
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
rb_gc_mark_movable((VALUE)block->blockid.iseq);
|
|
|
|
rb_gc_mark_movable(block->dependencies.cc);
|
|
|
|
rb_gc_mark_movable(block->dependencies.cme);
|
2021-02-19 15:03:12 -05:00
|
|
|
|
|
|
|
// Walk over references to objects in generated code.
|
|
|
|
uint32_t *offset_element;
|
|
|
|
rb_darray_foreach(block->gc_object_offsets, offset_idx, offset_element) {
|
|
|
|
uint32_t offset_to_value = *offset_element;
|
|
|
|
uint8_t *value_address = cb_get_ptr(cb, offset_to_value);
|
|
|
|
|
|
|
|
VALUE object;
|
|
|
|
memcpy(&object, value_address, SIZEOF_VALUE);
|
|
|
|
rb_gc_mark_movable(object);
|
|
|
|
}
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_iseq_update_references(const struct rb_iseq_constant_body *body)
|
2021-02-12 17:12:18 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 15:31:37 -05:00
|
|
|
|
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
|
|
|
|
2021-02-12 17:12:18 -05:00
|
|
|
block->blockid.iseq = (const rb_iseq_t *)rb_gc_location((VALUE)block->blockid.iseq);
|
|
|
|
|
|
|
|
block->dependencies.cc = rb_gc_location(block->dependencies.cc);
|
|
|
|
block->dependencies.cme = rb_gc_location(block->dependencies.cme);
|
2021-02-19 15:03:12 -05:00
|
|
|
|
|
|
|
// Walk over references to objects in generated code.
|
|
|
|
uint32_t *offset_element;
|
|
|
|
rb_darray_foreach(block->gc_object_offsets, offset_idx, offset_element) {
|
|
|
|
uint32_t offset_to_value = *offset_element;
|
|
|
|
uint8_t *value_address = cb_get_ptr(cb, offset_to_value);
|
|
|
|
|
|
|
|
VALUE object;
|
|
|
|
memcpy(&object, value_address, SIZEOF_VALUE);
|
|
|
|
VALUE possibly_moved = rb_gc_location(object);
|
|
|
|
// Only write when the VALUE moves, to be CoW friendly.
|
|
|
|
if (possibly_moved != object) {
|
|
|
|
memcpy(value_address, &possibly_moved, SIZEOF_VALUE);
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// Free the yjit resources associated with an iseq
|
2021-02-12 17:12:18 -05:00
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_iseq_free(const struct rb_iseq_constant_body *body)
|
2021-02-12 17:12:18 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_darray_for(body->yjit_blocks, version_array_idx) {
|
|
|
|
rb_yjit_block_array_t version_array = rb_darray_get(body->yjit_blocks, version_array_idx);
|
2021-03-04 15:31:37 -05:00
|
|
|
|
|
|
|
rb_darray_for(version_array, block_idx) {
|
|
|
|
block_t *block = rb_darray_get(version_array, block_idx);
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_free_block(block);
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
2021-03-04 15:31:37 -05:00
|
|
|
|
|
|
|
rb_darray_free(version_array);
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_darray_free(body->yjit_blocks);
|
2021-02-12 17:12:18 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
bool rb_yjit_enabled_p(void)
|
2021-02-23 15:22:20 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
return rb_yjit_opts.yjit_enabled;
|
2021-02-23 15:22:20 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
unsigned rb_yjit_call_threshold(void)
|
2021-02-23 15:22:20 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
return rb_yjit_opts.call_threshold;
|
2021-02-23 15:22:20 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
void
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_init(struct rb_yjit_options *options)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
2021-03-06 18:46:56 -05:00
|
|
|
if (!yjit_scrape_successful || !PLATFORM_SUPPORTED_P)
|
2020-12-08 16:54:41 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
rb_yjit_opts = *options;
|
|
|
|
rb_yjit_opts.yjit_enabled = true;
|
2021-01-26 15:21:47 -05:00
|
|
|
|
2021-02-23 15:22:20 -05:00
|
|
|
// Normalize options
|
2021-03-06 18:46:56 -05:00
|
|
|
if (rb_yjit_opts.call_threshold < 1) {
|
|
|
|
rb_yjit_opts.call_threshold = 2;
|
2021-02-23 15:22:20 -05:00
|
|
|
}
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-02-25 15:10:38 -05:00
|
|
|
blocks_assuming_stable_global_constant_state = st_init_numtable();
|
|
|
|
blocks_assuming_single_ractor_mode = st_init_numtable();
|
2021-03-09 14:01:16 -05:00
|
|
|
blocks_assuming_bops = st_init_numtable();
|
2021-02-25 15:10:38 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
yjit_init_core();
|
|
|
|
yjit_init_codegen();
|
2020-12-08 16:54:41 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// YJIT Ruby module
|
|
|
|
mYjit = rb_define_module("YJIT");
|
|
|
|
rb_define_module_function(mYjit, "blocks_for", yjit_blocks_for, 1);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// YJIT::Block (block version, code block)
|
|
|
|
cYjitBlock = rb_define_class_under(mYjit, "Block", rb_cObject);
|
|
|
|
rb_define_method(cYjitBlock, "address", block_address, 0);
|
|
|
|
rb_define_method(cYjitBlock, "code", block_code, 0);
|
|
|
|
rb_define_method(cYjitBlock, "iseq_start_index", iseq_start_index, 0);
|
|
|
|
rb_define_method(cYjitBlock, "iseq_end_index", iseq_end_index, 0);
|
Expose methods for inspecting Micro JIT code blocks
This commit adds a module `UJIT`. The module allows you to insert the
initial Micro JIT instruction in to an arbitrary iseq like this:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
```
After the initial jump is added, we can make Micro JIT do some work:
```ruby
100.times { foo(0) }
```
The `UJIT` module also exposes a method for finding all compiled blocks
for a given iseq, like this:
```ruby
blocks = UJIT.blocks_for(iseq)
```
We can sort the blocks by address and use the Crabstone gem (which is a
wrapper around `capstone`) to disassemble the generated code.
Here is the full code example:
```ruby
def foo(x)
if x < 1
"less than one"
else
"something else"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
UJIT.insert(iseq) # Add initial jump
100.times { foo(0) }
blocks = UJIT.blocks_for(iseq)
# brew install capstone
# gem install crabstone
require "crabstone"
cs = Crabstone::Disassembler.new(Crabstone::ARCH_X86, Crabstone::MODE_64)
puts iseq.disasm
blocks.sort_by(&:address).reverse.each do |block|
puts "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
cs.disasm(block.code, 0).each do |i|
printf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
```
Here is the output:
```
$ ./ruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "less than one" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "something else" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 7 -> 7 ==========================================================
0x0: movabs rax, 0x7fcd014cd518
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fcd0180ac00
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffe0da
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffe0da
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffe0da
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: je 0x3ffe111
0x6c: jmp 0xffffffffffffffa3
```
2021-01-20 13:50:13 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
// YJIT disassembler interface
|
2021-01-22 14:26:20 -05:00
|
|
|
#if HAVE_LIBCAPSTONE
|
2021-03-06 18:46:56 -05:00
|
|
|
cYjitDisasm = rb_define_class_under(mYjit, "Disasm", rb_cObject);
|
|
|
|
rb_define_alloc_func(cYjitDisasm, yjit_disasm_init);
|
|
|
|
rb_define_method(cYjitDisasm, "disasm", yjit_disasm, 2);
|
|
|
|
cYjitDisasmInsn = rb_struct_define_under(cYjitDisasm, "Insn", "address", "mnemonic", "op_str", NULL);
|
2021-01-22 14:26:20 -05:00
|
|
|
#endif
|
Directly link libcapstone for easier development
This lets us use libcapstone directly from miniruby so we don't need a
Ruby Gem to to dev work.
Example usage:
```ruby
def foo(x)
if x < 1
"wow"
else
"neat"
end
end
iseq = RubyVM::InstructionSequence.of(method(:foo))
puts UJIT.disasm(iseq)
100.times { foo 1 }
puts UJIT.disasm(iseq)
```
Then in the terminal
```
$ ./miniruby test.rb
== disasm: #<ISeq:foo@test.rb:1 (1,0)-(7,3)> (catch: FALSE)
local table (size: 1, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0<Arg>
0000 getlocal_WC_0 x@0 ( 2)[LiCa]
0002 putobject_INT2FIX_1_
0003 opt_lt <calldata!mid:<, argc:1, ARGS_SIMPLE>
0005 branchunless 10
0007 putstring "wow" ( 3)[Li]
0009 leave ( 7)[Re]
0010 putstring "neat" ( 5)[Li]
0012 leave ( 7)[Re]
== ISEQ RANGE: 10 -> 10 ========================================================
0x0: movabs rax, 0x7fe816e2d1a0
0xa: mov qword ptr [rdi], rax
0xd: mov r8, rax
0x10: mov r9, rax
0x13: mov r11, r12
0x16: jmp qword ptr [rax]
== ISEQ RANGE: 0 -> 7 ==========================================================
0x0: mov rax, qword ptr [rdi + 0x20]
0x4: mov rax, qword ptr [rax - 0x18]
0x8: mov qword ptr [rdx], rax
0xb: mov qword ptr [rdx + 8], 3
0x13: movabs rax, 0x7fe817808200
0x1d: test byte ptr [rax + 0x3e6], 1
0x24: jne 0x3ffff7b
0x2a: test byte ptr [rdx], 1
0x2d: je 0x3ffff7b
0x33: test byte ptr [rdx + 8], 1
0x37: je 0x3ffff7b
0x3d: mov rax, qword ptr [rdx]
0x40: cmp rax, qword ptr [rdx + 8]
0x44: movabs rax, 0
0x4e: movabs rcx, 0x14
0x58: cmovl rax, rcx
0x5c: mov qword ptr [rdx], rax
0x5f: test qword ptr [rdx], -9
0x66: jne 0x3ffffd5
```
Make sure to `brew install pkg-config capstone`
2021-01-22 13:43:26 -05:00
|
|
|
|
2021-03-06 18:46:56 -05:00
|
|
|
if (RUBY_DEBUG && rb_yjit_opts.gen_stats) {
|
2021-03-02 18:27:50 -05:00
|
|
|
// Setup at_exit callback for printing out counters
|
|
|
|
rb_block_call(rb_mKernel, rb_intern("at_exit"), 0, NULL, at_exit_print_stats, Qfalse);
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:54:41 -05:00
|
|
|
// Initialize the GC hooks
|
|
|
|
method_lookup_dependency = st_init_numtable();
|
2021-03-06 18:46:56 -05:00
|
|
|
struct yjit_root_struct *root;
|
|
|
|
VALUE yjit_root = TypedData_Make_Struct(0, struct yjit_root_struct, &yjit_root_type, root);
|
|
|
|
rb_gc_register_mark_object(yjit_root);
|
2020-12-08 16:54:41 -05:00
|
|
|
}
|