2017-02-11 19:01:06 +00:00
|
|
|
#include "disasm.h"
|
|
|
|
|
|
|
|
/* Width of padded opcode names */
|
|
|
|
#define OP_WIDTH 20
|
|
|
|
|
|
|
|
/* Print various register and arguments to instructions */
|
2017-02-16 02:02:00 +00:00
|
|
|
static void dasm_print_slot(FILE * out, uint16_t index) { fprintf(out, "%d ", index); }
|
|
|
|
static void dasm_print_i16(FILE * out, int16_t number) { fprintf(out, "#%d ", number); }
|
|
|
|
static void dasm_print_i32(FILE * out, int32_t number) { fprintf(out, "#%d ", number); }
|
|
|
|
static void dasm_print_f64(FILE * out, double number) { fprintf(out, "#%f ", number); }
|
|
|
|
static void dasm_print_literal(FILE * out, uint16_t index) { fprintf(out, "(%d) ", index); }
|
|
|
|
static void dasm_print_upvalue(FILE * out, uint16_t level, uint16_t index) {
|
2017-02-11 19:01:06 +00:00
|
|
|
fprintf(out, "<%d, %d> ", level, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the name of the argument but pad it */
|
2017-02-16 02:02:00 +00:00
|
|
|
static void dasm_print_arg(FILE * out, const char * name) {
|
2017-02-11 19:01:06 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
char c;
|
|
|
|
while ((c = *name++)) {
|
|
|
|
fputc(c, out);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
for (; i < OP_WIDTH; ++i)
|
|
|
|
fputc(' ', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print instructions that take a fixed number of arguments */
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint32_t dasm_fixed_op(FILE * out, const uint16_t * current,
|
2017-02-11 19:01:06 +00:00
|
|
|
const char * name, uint32_t size) {
|
|
|
|
uint32_t i;
|
2017-02-16 02:02:00 +00:00
|
|
|
dasm_print_arg(out, name);
|
2017-02-11 19:01:06 +00:00
|
|
|
for (i = 1; i <= size; ++i) {
|
2017-02-16 02:02:00 +00:00
|
|
|
dasm_print_slot(out, current[i]);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
return size + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print instructions that take a variable number of arguments */
|
2017-02-16 02:02:00 +00:00
|
|
|
static uint32_t dasm_varg_op(FILE * out, const uint16_t * current,
|
2017-02-11 19:01:06 +00:00
|
|
|
const char * name, uint32_t extra) {
|
|
|
|
uint32_t i, argCount;
|
2017-02-16 02:02:00 +00:00
|
|
|
dasm_print_arg(out, name);
|
2017-02-11 19:01:06 +00:00
|
|
|
for (i = 0; i < extra; ++i) {
|
2017-02-16 02:02:00 +00:00
|
|
|
dasm_print_slot(out, current[i + 1]);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
argCount = current[extra + 1];
|
|
|
|
fprintf(out, ": "); /* Argument separator */
|
2017-02-11 19:01:06 +00:00
|
|
|
for (i = 0; i < argCount; ++i) {
|
2017-02-16 02:02:00 +00:00
|
|
|
dasm_print_slot(out, current[i + extra + 2]);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
return argCount + extra + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the disassembly for a function definition */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_dasm_funcdef(FILE * out, GstFuncDef * def) {
|
|
|
|
gst_dasm(out, def->byteCode, def->byteCodeLen);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the disassembly for a function */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_dasm_function(FILE * out, GstFunction * f) {
|
|
|
|
gst_dasm(out, f->def->byteCode, f->def->byteCodeLen);
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Disassemble some bytecode and display it as opcode + arguments assembly */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_dasm(FILE * out, uint16_t *byteCode, uint32_t len) {
|
2017-02-11 19:01:06 +00:00
|
|
|
uint16_t *current = byteCode;
|
|
|
|
uint16_t *end = byteCode + len;
|
|
|
|
|
2017-02-13 02:54:18 +00:00
|
|
|
fprintf(out, "----- ASM BYTECODE START -----\n");
|
2017-02-12 15:27:18 +00:00
|
|
|
|
2017-02-11 19:01:06 +00:00
|
|
|
while (current < end) {
|
|
|
|
switch (*current) {
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_ADD:
|
|
|
|
current += dasm_fixed_op(out, current, "add", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SUB:
|
|
|
|
current += dasm_fixed_op(out, current, "sub", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_MUL:
|
|
|
|
current += dasm_fixed_op(out, current, "mul", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_DIV:
|
|
|
|
current += dasm_fixed_op(out, current, "div", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_NOT:
|
|
|
|
current += dasm_fixed_op(out, current, "not", 2);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LD0:
|
|
|
|
current += dasm_fixed_op(out, current, "load0", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LD1:
|
|
|
|
current += dasm_fixed_op(out, current, "load1", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_FLS:
|
|
|
|
current += dasm_fixed_op(out, current, "loadFalse", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_TRU:
|
|
|
|
current += dasm_fixed_op(out, current, "loadTrue", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_NIL:
|
|
|
|
current += dasm_fixed_op(out, current, "loadNil", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I16:
|
|
|
|
dasm_print_arg(out, "loadInt16");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_i16(out, ((int16_t *)current)[2]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 3;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_UPV:
|
|
|
|
dasm_print_arg(out, "loadUpValue");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_upvalue(out, current[2], current[3]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 4;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_JIF:
|
|
|
|
dasm_print_arg(out, "jumpIf");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_i32(out, ((int32_t *)(current + 2))[0]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 4;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_JMP:
|
|
|
|
dasm_print_arg(out, "jump");
|
|
|
|
dasm_print_i32(out, ((int32_t *)(current + 1))[0]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 3;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CAL:
|
|
|
|
current += dasm_varg_op(out, current, "call", 2);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_RET:
|
|
|
|
current += dasm_fixed_op(out, current, "return", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SUV:
|
|
|
|
dasm_print_arg(out, "setUpValue");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_upvalue(out, current[2], current[3]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 4;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CST:
|
|
|
|
dasm_print_arg(out, "loadLiteral");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_literal(out, current[2]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 3;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_I32:
|
|
|
|
dasm_print_arg(out, "loadInt32");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_i32(out, ((int32_t *)(current + 2))[0]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 4;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_F64:
|
|
|
|
dasm_print_arg(out, "loadFloat64");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_f64(out, ((double *)(current + 2))[0]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 6;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_MOV:
|
|
|
|
current += dasm_fixed_op(out, current, "move", 2);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_CLN:
|
|
|
|
dasm_print_arg(out, "makeClosure");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_literal(out, current[2]);
|
2017-02-11 19:01:06 +00:00
|
|
|
current += 3;
|
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_EQL:
|
|
|
|
current += dasm_fixed_op(out, current, "equals", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LTN:
|
|
|
|
current += dasm_fixed_op(out, current, "lessThan", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_LTE:
|
|
|
|
current += dasm_fixed_op(out, current, "lessThanEquals", 3);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_ARR:
|
|
|
|
current += dasm_varg_op(out, current, "array", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_DIC:
|
2017-02-16 20:10:59 +00:00
|
|
|
current += dasm_varg_op(out, current, "object", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_TCL:
|
|
|
|
current += dasm_varg_op(out, current, "tailCall", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_ADM:
|
|
|
|
current += dasm_varg_op(out, current, "addMultiple", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SBM:
|
|
|
|
current += dasm_varg_op(out, current, "subMultiple", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_MUM:
|
|
|
|
current += dasm_varg_op(out, current, "mulMultiple", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_DVM:
|
|
|
|
current += dasm_varg_op(out, current, "divMultiple", 1);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_RTN:
|
2017-02-22 23:19:46 +00:00
|
|
|
current += dasm_fixed_op(out, current, "returnNil", 0);
|
2017-02-11 19:01:06 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_GET:
|
|
|
|
current += dasm_fixed_op(out, current, "get", 3);
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
2017-02-16 02:02:00 +00:00
|
|
|
case GST_OP_SET:
|
|
|
|
current += dasm_fixed_op(out, current, "set", 3);
|
2017-02-22 23:19:46 +00:00
|
|
|
break;
|
|
|
|
case GST_OP_ERR:
|
|
|
|
current += dasm_fixed_op(out, current, "error", 1);
|
|
|
|
break;
|
|
|
|
case GST_OP_TRY:
|
|
|
|
dasm_print_arg(out, "try");
|
|
|
|
dasm_print_slot(out, current[1]);
|
|
|
|
dasm_print_i32(out, *(int32_t *)(current + 2));
|
|
|
|
current += 4;
|
|
|
|
break;
|
|
|
|
case GST_OP_UTY:
|
|
|
|
current += dasm_fixed_op(out, current, "untry", 0);
|
2017-02-13 02:54:18 +00:00
|
|
|
break;
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|
|
|
|
fprintf(out, "\n");
|
|
|
|
}
|
2017-02-12 15:27:18 +00:00
|
|
|
fprintf(out, "----- END ASM BYTECODE -----\n");
|
2017-02-11 19:01:06 +00:00
|
|
|
}
|