Skip to content
Myr

bytecode.h

This file

Path

src/bytecode.h

96 lines · 1.7 KiB · C

Last changed by

builtin.c: typecheck builtin cfunctions in function_listEmanuele Torrebefore adoption0b82b38

History of this file

Take it

1#ifndef BYTECODE_H2#define BYTECODE_H3#include <stdint.h>45#include "jq.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 448struct cfunction {49  union {50    jv (*a1)(jq_state *, jv);51    jv (*a2)(jq_state *, jv, jv);52    jv (*a3)(jq_state *, jv, jv, jv);53    jv (*a4)(jq_state *, jv, jv, jv, jv);54  } fptr;55  const char* name;56  int nargs;57};5859struct symbol_table {60  struct cfunction* cfunctions;61  int ncfunctions;62  jv cfunc_names;63};6465// The bytecode format matters in:66//   execute.c  - interpreter67//   compile.c  - compiler68//   bytecode.c - disassembler6970#define ARG_NEWCLOSURE 0x10007172struct bytecode {73  uint16_t* code;74  int codelen;7576  int nlocals;77  int nclosures;7879  jv constants; // JSON array of constants80  struct symbol_table* globals;8182  struct bytecode** subfunctions;83  int nsubfunctions;8485  struct bytecode* parent;8687  jv debuginfo;88};8990void dump_disassembly(int, struct bytecode* code);91void dump_operation(struct bytecode* bc, uint16_t* op);9293int bytecode_operation_length(uint16_t* codeptr);94void bytecode_free(struct bytecode* bc);9596#endif