2012-08-16 01:00:30 +01:00
|
|
|
#ifndef BYTECODE_H
|
|
|
|
|
#define BYTECODE_H
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2012-09-02 16:31:59 +01:00
|
|
|
#include "jv.h"
|
2013-06-18 01:36:18 +01:00
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
#define OP(name, imm, in, out) name,
|
|
|
|
|
#include "opcode_list.h"
|
|
|
|
|
#undef OP
|
|
|
|
|
} opcode;
|
|
|
|
|
|
|
|
|
|
enum {
|
2015-07-19 09:36:16 -07:00
|
|
|
NUM_OPCODES =
|
2013-06-18 01:36:18 +01:00
|
|
|
#define OP(name, imm, in, out) +1
|
|
|
|
|
#include "opcode_list.h"
|
|
|
|
|
#undef OP
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
OP_HAS_CONSTANT = 2,
|
|
|
|
|
OP_HAS_VARIABLE = 4,
|
|
|
|
|
OP_HAS_BRANCH = 8,
|
|
|
|
|
OP_HAS_CFUNC = 32,
|
|
|
|
|
OP_HAS_UFUNC = 64,
|
|
|
|
|
OP_IS_CALL_PSEUDO = 128,
|
|
|
|
|
OP_HAS_BINDING = 1024,
|
2014-12-28 00:32:06 -06:00
|
|
|
// NOTE: Not actually part of any op -- a pseudo-op flag for special
|
|
|
|
|
// handling of `break`.
|
|
|
|
|
OP_BIND_WILDCARD = 2048,
|
2013-06-18 01:36:18 +01:00
|
|
|
};
|
|
|
|
|
struct opcode_description {
|
|
|
|
|
opcode op;
|
|
|
|
|
const char* name;
|
|
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
|
|
// length in 16-bit units
|
|
|
|
|
int length;
|
|
|
|
|
|
|
|
|
|
int stack_in, stack_out;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const struct opcode_description* opcode_describe(opcode op);
|
|
|
|
|
|
2012-08-16 01:00:30 +01:00
|
|
|
|
|
|
|
|
#define MAX_CFUNCTION_ARGS 10
|
2013-06-21 12:06:28 +01:00
|
|
|
typedef void (*cfunction_ptr)();
|
2013-06-18 01:36:18 +01:00
|
|
|
struct cfunction {
|
|
|
|
|
cfunction_ptr fptr;
|
|
|
|
|
const char* name;
|
|
|
|
|
int nargs;
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-16 01:00:30 +01:00
|
|
|
struct symbol_table {
|
|
|
|
|
struct cfunction* cfunctions;
|
|
|
|
|
int ncfunctions;
|
2013-05-16 00:51:17 +01:00
|
|
|
jv cfunc_names;
|
2012-08-16 01:00:30 +01:00
|
|
|
};
|
|
|
|
|
|
2012-08-21 18:14:13 +01:00
|
|
|
// The bytecode format matters in:
|
|
|
|
|
// execute.c - interpreter
|
|
|
|
|
// compile.c - compiler
|
|
|
|
|
// bytecode.c - disassembler
|
|
|
|
|
|
|
|
|
|
#define ARG_NEWCLOSURE 0x1000
|
2012-08-16 01:00:30 +01:00
|
|
|
|
|
|
|
|
struct bytecode {
|
|
|
|
|
uint16_t* code;
|
|
|
|
|
int codelen;
|
2012-08-21 12:35:36 +01:00
|
|
|
|
|
|
|
|
int nlocals;
|
|
|
|
|
int nclosures;
|
|
|
|
|
|
2012-09-02 16:31:59 +01:00
|
|
|
jv constants; // JSON array of constants
|
2012-08-16 01:00:30 +01:00
|
|
|
struct symbol_table* globals;
|
2012-08-21 12:35:36 +01:00
|
|
|
|
2012-08-21 18:14:13 +01:00
|
|
|
struct bytecode** subfunctions;
|
2012-08-21 12:35:36 +01:00
|
|
|
int nsubfunctions;
|
2012-08-21 18:14:13 +01:00
|
|
|
|
|
|
|
|
struct bytecode* parent;
|
2013-05-16 00:51:17 +01:00
|
|
|
|
|
|
|
|
jv debuginfo;
|
2012-08-16 01:00:30 +01:00
|
|
|
};
|
|
|
|
|
|
2012-08-21 18:14:13 +01:00
|
|
|
void dump_disassembly(int, struct bytecode* code);
|
2012-08-16 01:00:30 +01:00
|
|
|
void dump_operation(struct bytecode* bc, uint16_t* op);
|
2012-08-21 12:35:36 +01:00
|
|
|
|
2014-06-30 23:41:20 -05:00
|
|
|
int bytecode_operation_length(uint16_t* codeptr);
|
2012-09-02 16:31:59 +01:00
|
|
|
void bytecode_free(struct bytecode* bc);
|
|
|
|
|
|
2012-08-16 01:00:30 +01:00
|
|
|
#endif
|