Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ BUILTIN_RB_SRCS = \
$(srcdir)/timev.rb \
$(srcdir)/prelude.rb \
$(srcdir)/gem_prelude.rb \
$(srcdir)/ujit.rb \
$(empty)
BUILTIN_RB_INCS = $(BUILTIN_RB_SRCS:.rb=.rbinc)

Expand Down Expand Up @@ -15409,6 +15410,7 @@ ujit_core.$(OBJEXT): {$(VPATH)}vm_core.h
ujit_core.$(OBJEXT): {$(VPATH)}vm_debug.h
ujit_core.$(OBJEXT): {$(VPATH)}vm_opts.h
ujit_core.$(OBJEXT): {$(VPATH)}vm_sync.h
ujit_iface.$(OBJEXT): {$(VPATH)}ujit.rbinc
ujit_iface.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
ujit_iface.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
ujit_iface.$(OBJEXT): $(CCAN_DIR)/list/list.h
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,15 @@ AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV
AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX
AC_CHECK_LIB(socket, shutdown) # SunOS/Solaris

if pkg-config --exists capstone; then
CAPSTONE_CFLAGS=`pkg-config --cflags capstone`
CAPSTONE_LIB_L=`pkg-config --libs-only-L capstone`
LDFLAGS="$LDFLAGS $CAPSTONE_LIB_L"
CFLAGS="$CFLAGS $CAPSTONE_CFLAGS"
fi

AC_CHECK_LIB(capstone, cs_open) # Capstone

dnl Checks for header files.
AC_HEADER_DIRENT
dnl AC_HEADER_STDC has been checked in AC_USE_SYSTEM_EXTENSIONS
Expand Down
1 change: 1 addition & 0 deletions inits.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ rb_call_builtin_inits(void)
BUILTIN(array);
BUILTIN(kernel);
BUILTIN(timev);
BUILTIN(ujit);
Init_builtin_prelude();
}
#undef CALL
35 changes: 0 additions & 35 deletions misc/ujit_disasm.rb

This file was deleted.

28 changes: 28 additions & 0 deletions ujit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module UJIT
def self.disasm(iseq)
blocks = UJIT.blocks_for(iseq)
return if blocks.empty?

str = ""

cs = UJIT::Disasm.new

str << iseq.disasm
str << "\n"

blocks.sort_by(&:address).reverse.each do |block|
str << "== ISEQ RANGE: #{block.iseq_start_index} -> #{block.iseq_end_index} ".ljust(80, "=")
str << "\n"

cs.disasm(block.code, 0).each do |i|
str << sprintf(
"\t0x%<address>x:\t%<instruction>s\t%<details>s\n",
address: i.address,
instruction: i.mnemonic,
details: i.op_str
)
end
end
str
end if defined?(Disasm)
end
53 changes: 53 additions & 0 deletions ujit_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
#include "ujit_codegen.h"
#include "ujit_core.h"
#include "ujit_hooks.inc"
#include "ujit.rbinc"

#if HAVE_LIBCAPSTONE
#include <capstone/capstone.h>
#endif

VALUE cUjitBlock;
VALUE cUjitDisasm;
VALUE cUjitDisasmInsn;

extern st_table * version_tbl;
extern codeblock_t *cb;
Expand Down Expand Up @@ -366,6 +373,44 @@ iseq_end_index(VALUE self)
return INT2NUM(block->end_idx);
}

#if HAVE_LIBCAPSTONE
static const rb_data_type_t ujit_disasm_type = {
"UJIT/Disasm",
{0, (void(*)(void *))cs_close, 0, },
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

static VALUE
ujit_disasm_init(VALUE klass)
{
csh * handle;
VALUE disasm = TypedData_Make_Struct(klass, csh, &ujit_disasm_type, handle);
cs_open(CS_ARCH_X86, CS_MODE_64, handle);
return disasm;
}

static VALUE
ujit_disasm(VALUE self, VALUE code, VALUE from)
{
size_t count;
csh * handle;
cs_insn *insns;

TypedData_Get_Struct(self, csh, &ujit_disasm_type, handle);
count = cs_disasm(*handle, StringValuePtr(code), RSTRING_LEN(code), NUM2INT(from), 0, &insns);
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));
rb_ary_push(insn_list, rb_struct_alloc(cUjitDisasmInsn, vals));
}
cs_free(insns, count);
return insn_list;
}
#endif

void
rb_ujit_init(void)
{
Expand All @@ -389,6 +434,14 @@ rb_ujit_init(void)
rb_define_method(cUjitBlock, "iseq_start_index", iseq_start_index, 0);
rb_define_method(cUjitBlock, "iseq_end_index", iseq_end_index, 0);

#if HAVE_LIBCAPSTONE
cUjitDisasm = rb_define_class_under(mUjit, "Disasm", rb_cObject);
rb_define_alloc_func(cUjitDisasm, ujit_disasm_init);
rb_define_method(cUjitDisasm, "disasm", ujit_disasm, 2);

cUjitDisasmInsn = rb_struct_define_under(cUjitDisasm, "Insn", "address", "mnemonic", "op_str", NULL);
#endif

// Initialize the GC hooks
method_lookup_dependency = st_init_numtable();
struct ujit_root_struct *root;
Expand Down