1#include <stdio.h>2#include <stdint.h>3#include <stdlib.h>45#include "bytecode.h"6#include "jv_alloc.h"78// flags, length9#define NONE 0, 110#define CONSTANT OP_HAS_CONSTANT, 211#define VARIABLE (OP_HAS_VARIABLE | OP_HAS_BINDING), 312#define GLOBAL (OP_HAS_CONSTANT | OP_HAS_VARIABLE | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 413#define BRANCH OP_HAS_BRANCH, 214#define CFUNC (OP_HAS_CFUNC | OP_HAS_BINDING), 315#define UFUNC (OP_HAS_UFUNC | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 416#define DEFINITION (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 017#define CLOSURE_REF_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 21819#define OP(name, imm, in, out) \20 {name, #name, imm, in, out},2122static const struct opcode_description opcode_descriptions[] = {23#include "opcode_list.h"24};2526static const struct opcode_description invalid_opcode_description = {27 -1, "#INVALID", 0, 0, 0, 028};293031const struct opcode_description* opcode_describe(opcode op) {32 if ((int)op >= 0 && (int)op < NUM_OPCODES) {33 return &opcode_descriptions[op];34 } else {35 return &invalid_opcode_description;36 }37}383940int bytecode_operation_length(uint16_t* codeptr) {41 int length = opcode_describe(*codeptr)->length;42 if (*codeptr == CALL_JQ || *codeptr == TAIL_CALL_JQ) {43 length += codeptr[1] * 2;44 }45 return length;46}4748static void dump_code(int indent, struct bytecode* bc) {49 int pc = 0;50 while (pc < bc->codelen) {51 printf("%*s", indent, "");52 dump_operation(bc, bc->code + pc);53 printf("\n");54 pc += bytecode_operation_length(bc->code + pc);55 }56}5758static void symbol_table_free(struct symbol_table* syms) {59 jv_mem_free(syms->cfunctions);60 jv_free(syms->cfunc_names);61 jv_mem_free(syms);62}6364void dump_disassembly(int indent, struct bytecode* bc) {65 if (bc->nclosures > 0) {66 printf("%*s[params: ", indent, "");67 jv params = jv_object_get(jv_copy(bc->debuginfo), jv_string("params"));68 for (int i=0; i<bc->nclosures; i++) {69 if (i) printf(", ");70 jv name = jv_array_get(jv_copy(params), i);71 printf("%s", jv_string_value(name));72 jv_free(name);73 }74 jv_free(params);75 printf("]\n");76 }77 dump_code(indent, bc);78 for (int i=0; i<bc->nsubfunctions; i++) {79 struct bytecode* subfn = bc->subfunctions[i];80 jv name = jv_object_get(jv_copy(subfn->debuginfo), jv_string("name"));81 printf("%*s%s:%d:\n", indent, "", jv_string_value(name), i);82 jv_free(name);83 dump_disassembly(indent+2, subfn);84 }85}8687static struct bytecode* getlevel(struct bytecode* bc, int level) {88 while (level > 0) {89 bc = bc->parent;90 level--;91 }92 return bc;93}9495void dump_operation(struct bytecode* bc, uint16_t* codeptr) {96 int pc = codeptr - bc->code;97 printf("%04d ", pc);98 const struct opcode_description* op = opcode_describe(bc->code[pc++]);99 printf("%s", op->name);100 if (op->length > 1) {101 uint16_t imm = bc->code[pc++];102 if (op->op == CALL_JQ || op->op == TAIL_CALL_JQ) {103 for (int i=0; i<imm+1; i++) {104 uint16_t level = bc->code[pc++];105 uint16_t idx = bc->code[pc++];106 jv name;107 if (idx & ARG_NEWCLOSURE) {108 idx &= ~ARG_NEWCLOSURE;109 name = jv_object_get(jv_copy(getlevel(bc,level)->subfunctions[idx]->debuginfo),110 jv_string("name"));111 } else {112 name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,level)->debuginfo),113 jv_string("params")), idx);114 }115 printf(" %s:%d",116 jv_string_value(name),117 idx);118 jv_free(name);119 if (level) {120 printf("^%d", level);121 }122 }123 } else if (op->op == CALL_BUILTIN) {124 int func = bc->code[pc++];125 jv name = jv_array_get(jv_copy(bc->globals->cfunc_names), func);126 printf(" %s", jv_string_value(name));127 jv_free(name);128 } else if (op->flags & OP_HAS_BRANCH) {129 printf(" %04d", pc + imm);130 } else if (op->flags & OP_HAS_CONSTANT) {131 printf(" ");132 jv_dump(jv_array_get(jv_copy(bc->constants), imm), 0);133 } else if (op->flags & OP_HAS_VARIABLE) {134 uint16_t v = bc->code[pc++];135 jv name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,imm)->debuginfo), jv_string("locals")), v);136 printf(" $%s:%d",137 jv_string_value(name),138 v);139 jv_free(name);140 if (imm) {141 printf("^%d", imm);142 }143 } else {144 printf(" %d", imm);145 }146 }147}148149void bytecode_free(struct bytecode* bc) {150 if (!bc)151 return;152 jv_mem_free(bc->code);153 jv_free(bc->constants);154 for (int i=0; i<bc->nsubfunctions; i++)155 bytecode_free(bc->subfunctions[i]);156 if (!bc->parent)157 symbol_table_free(bc->globals);158 jv_mem_free(bc->subfunctions);159 jv_free(bc->debuginfo);160 jv_mem_free(bc);161}