bytecode.h
This file
Path
bytecode.h
92 lines · 1.6 KiB · C
Last changed by
Clean up trailing whitespaceDavid Tolnaybefore adoption1628bbf
History of this fileTake it
1#ifndef BYTECODE_H2#define BYTECODE_H3#include <stdint.h>45#include "jv.h"67typedef enum {8#define OP(name, imm, in, out) name,9#include "opcode_list.h"10#undef OP11} opcode;1213enum {14 NUM_OPCODES =15#define OP(name, imm, in, out) +116#include "opcode_list.h"17#undef OP18};1920enum {21 OP_HAS_CONSTANT = 2,22 OP_HAS_VARIABLE = 4,23 OP_HAS_BRANCH = 8,24 OP_HAS_CFUNC = 32,25 OP_HAS_UFUNC = 64,26 OP_IS_CALL_PSEUDO = 128,27 OP_HAS_BINDING = 1024,28 // NOTE: Not actually part of any op -- a pseudo-op flag for special29 // handling of `break`.30 OP_BIND_WILDCARD = 2048,31};32struct opcode_description {33 opcode op;34 const char* name;3536 int flags;3738 // length in 16-bit units39 int length;4041 int stack_in, stack_out;42};4344const struct opcode_description* opcode_describe(opcode op);454647#define MAX_CFUNCTION_ARGS 1048typedef void (*cfunction_ptr)();49struct cfunction {50 cfunction_ptr fptr;51 const char* name;52 int nargs;53};5455struct symbol_table {56 struct cfunction* cfunctions;57 int ncfunctions;58 jv cfunc_names;59};6061// The bytecode format matters in:62// execute.c - interpreter63// compile.c - compiler64// bytecode.c - disassembler6566#define ARG_NEWCLOSURE 0x10006768struct bytecode {69 uint16_t* code;70 int codelen;7172 int nlocals;73 int nclosures;7475 jv constants; // JSON array of constants76 struct symbol_table* globals;7778 struct bytecode** subfunctions;79 int nsubfunctions;8081 struct bytecode* parent;8283 jv debuginfo;84};8586void dump_disassembly(int, struct bytecode* code);87void dump_operation(struct bytecode* bc, uint16_t* op);8889int bytecode_operation_length(uint16_t* codeptr);90void bytecode_free(struct bytecode* bc);9192#endif