Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created February 5, 2014 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondale-sc/8817230 to your computer and use it in GitHub Desktop.
Save rondale-sc/8817230 to your computer and use it in GitHub Desktop.
/**
* Disassemble a instruction
* Iseq -> Iseq inspect object
*/
int
rb_iseq_disasm_insn(VALUE ret, VALUE *iseq, size_t pos,
rb_iseq_t *iseqdat, VALUE child)
{
VALUE insn = iseq[pos];
int len = insn_len(insn);
int j;
const char *types = insn_op_types(insn);
VALUE str = rb_str_new(0, 0);
const char *insn_name_buff;
insn_name_buff = insn_name(insn);
if (1) {
rb_str_catf(str, "%04"PRIdSIZE" %-16s ", pos, insn_name_buff);
}
else {
rb_str_catf(str, "%04"PRIdSIZE" %-16.*s ", pos,
(int)strcspn(insn_name_buff, "_"), insn_name_buff);
}
for (j = 0; types[j]; j++) {
const char *types = insn_op_types(insn);
VALUE opstr = rb_insn_operand_intern(iseqdat, insn, j, iseq[pos + j + 1],
len, pos, &iseq[pos + j + 2],
child);
rb_str_concat(str, opstr);
if (types[j + 1]) {
rb_str_cat2(str, ", ");
}
}
{
unsigned int line_no = find_line_no(iseqdat, pos);
unsigned int prev = pos == 0 ? 0 : find_line_no(iseqdat, pos - 1);
if (line_no && line_no != prev) {
long slen = RSTRING_LEN(str);
slen = (slen > 70) ? 0 : (70 - slen);
str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
}
}
if (ret) {
rb_str_cat2(str, "\n");
rb_str_concat(ret, str);
}
else {
printf("%s\n", RSTRING_PTR(str));
}
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment