Skip to content
Myr

bytecode.h

This file

Path

src/bytecode.h

91 lines · 1.6 KiB · C

Last changed by

Declare cfunction.fptr as jv (*)() to avoid having to cast everywhereEmanuele Torrebefore adoption6716e23

History of this file

Take 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 1048struct cfunction {49  jv (*fptr)();50  const char* name;51  int nargs;52};5354struct symbol_table {55  struct cfunction* cfunctions;56  int ncfunctions;57  jv cfunc_names;58};5960// The bytecode format matters in:61//   execute.c  - interpreter62//   compile.c  - compiler63//   bytecode.c - disassembler6465#define ARG_NEWCLOSURE 0x10006667struct bytecode {68  uint16_t* code;69  int codelen;7071  int nlocals;72  int nclosures;7374  jv constants; // JSON array of constants75  struct symbol_table* globals;7677  struct bytecode** subfunctions;78  int nsubfunctions;7980  struct bytecode* parent;8182  jv debuginfo;83};8485void dump_disassembly(int, struct bytecode* code);86void dump_operation(struct bytecode* bc, uint16_t* op);8788int bytecode_operation_length(uint16_t* codeptr);89void bytecode_free(struct bytecode* bc);9091#endif