Fold opcode.{c,h} into bytecode.{c,h}
This commit is contained in:
1 parent
824ce76cec
commit
b49d65a276
10 files changed
+83
-96
No files matched your search
+4
-4
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \
|
JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \
|
||||||
forkable_stack.h frame_layout.h jv.h jv_alloc.h jv_aux.h jv_dtoa.h \
|
forkable_stack.h frame_layout.h jv.h jv_alloc.h jv_aux.h jv_dtoa.h \
|
||||||
jv_file.h jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h \
|
jv_file.h jv_parse.h jv_unicode.h locfile.h opcode_list.h parser.y \
|
||||||
parser.y jv_utf8_tables.h lexer.l
|
jv_utf8_tables.h lexer.l
|
||||||
|
|
||||||
JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c \
|
JQ_SRC = locfile.c bytecode.c compile.c execute.c builtin.c jv.c \
|
||||||
jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_file.c \
|
jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_file.c \
|
||||||
jv_alloc.c jq_test.c ${JQ_INCS}
|
jv_alloc.c jq_test.c ${JQ_INCS}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,11 @@
|
|||||||
#ifndef BUILTIN_H
|
#ifndef BUILTIN_H
|
||||||
#define BUILTIN_H
|
#define BUILTIN_H
|
||||||
|
|
||||||
|
#include "bytecode.h"
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
|
|
||||||
int builtins_bind(block*);
|
int builtins_bind(block*);
|
||||||
|
|
||||||
|
|
||||||
typedef void (*cfunction_ptr)(void);
|
|
||||||
|
|
||||||
struct cfunction {
|
|
||||||
cfunction_ptr fptr;
|
|
||||||
const char* name;
|
|
||||||
int nargs;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
jv cfunction_invoke(struct cfunction* function, jv input[]);
|
jv cfunction_invoke(struct cfunction* function, jv input[]);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+31
-1
@@ -3,9 +3,39 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
#include "opcode.h"
|
|
||||||
#include "jv_alloc.h"
|
#include "jv_alloc.h"
|
||||||
|
|
||||||
|
// flags, length
|
||||||
|
#define NONE 0, 1
|
||||||
|
#define CONSTANT OP_HAS_CONSTANT, 2
|
||||||
|
#define VARIABLE (OP_HAS_VARIABLE | OP_HAS_BINDING), 3
|
||||||
|
#define BRANCH OP_HAS_BRANCH, 2
|
||||||
|
#define CFUNC (OP_HAS_CFUNC | OP_HAS_BINDING), 3
|
||||||
|
#define UFUNC (OP_HAS_UFUNC | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 4
|
||||||
|
#define DEFINITION (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 0
|
||||||
|
#define CLOSURE_REF_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 2
|
||||||
|
|
||||||
|
#define OP(name, imm, in, out) \
|
||||||
|
{name, #name, imm, in, out},
|
||||||
|
|
||||||
|
static const struct opcode_description opcode_descriptions[] = {
|
||||||
|
#include "opcode_list.h"
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct opcode_description invalid_opcode_description = {
|
||||||
|
-1, "#INVALID", 0, 0, 0, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const struct opcode_description* opcode_describe(opcode op) {
|
||||||
|
if ((int)op >= 0 && (int)op < NUM_OPCODES) {
|
||||||
|
return &opcode_descriptions[op];
|
||||||
|
} else {
|
||||||
|
return &invalid_opcode_description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int bytecode_operation_length(uint16_t* codeptr) {
|
static int bytecode_operation_length(uint16_t* codeptr) {
|
||||||
int length = opcode_describe(*codeptr)->length;
|
int length = opcode_describe(*codeptr)->length;
|
||||||
if (*codeptr == CALL_JQ) {
|
if (*codeptr == CALL_JQ) {
|
||||||
|
|||||||
+44
-2
@@ -3,10 +3,52 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "opcode.h"
|
|
||||||
#include "builtin.h"
|
typedef enum {
|
||||||
|
#define OP(name, imm, in, out) name,
|
||||||
|
#include "opcode_list.h"
|
||||||
|
#undef OP
|
||||||
|
} opcode;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
NUM_OPCODES =
|
||||||
|
#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,
|
||||||
|
};
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
#define MAX_CFUNCTION_ARGS 10
|
#define MAX_CFUNCTION_ARGS 10
|
||||||
|
typedef void (*cfunction_ptr)(void);
|
||||||
|
struct cfunction {
|
||||||
|
cfunction_ptr fptr;
|
||||||
|
const char* name;
|
||||||
|
int nargs;
|
||||||
|
};
|
||||||
|
|
||||||
struct symbol_table {
|
struct symbol_table {
|
||||||
struct cfunction* cfunctions;
|
struct cfunction* cfunctions;
|
||||||
int ncfunctions;
|
int ncfunctions;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "opcode.h"
|
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
|
|||||||
@@ -2,13 +2,9 @@
|
|||||||
#define COMPILE_H
|
#define COMPILE_H
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "opcode.h"
|
#include "bytecode.h"
|
||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
|
|
||||||
struct bytecode;
|
|
||||||
struct symbol_table;
|
|
||||||
struct cfunction;
|
|
||||||
|
|
||||||
struct inst;
|
struct inst;
|
||||||
typedef struct inst inst;
|
typedef struct inst inst;
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
#include "execute.h"
|
#include "execute.h"
|
||||||
|
|
||||||
#include "exec_stack.h"
|
#include "exec_stack.h"
|
||||||
#include "opcode.h"
|
|
||||||
#include "bytecode.h"
|
#include "bytecode.h"
|
||||||
|
|
||||||
#include "jv_alloc.h"
|
#include "jv_alloc.h"
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#ifndef JQ_PARSER_H
|
#ifndef JQ_PARSER_H
|
||||||
#define JQ_PARSER_H
|
#define JQ_PARSER_H
|
||||||
|
#include "locfile.h"
|
||||||
|
#include "compile.h"
|
||||||
|
|
||||||
int jq_parse(struct locfile* source, block* answer);
|
int jq_parse(struct locfile* source, block* answer);
|
||||||
int jq_parse_library(struct locfile* locations, block* answer);
|
int jq_parse_library(struct locfile* locations, block* answer);
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
#include "opcode.h"
|
|
||||||
|
|
||||||
// flags, length
|
|
||||||
#define NONE 0, 1
|
|
||||||
#define CONSTANT OP_HAS_CONSTANT, 2
|
|
||||||
#define VARIABLE (OP_HAS_VARIABLE | OP_HAS_BINDING), 3
|
|
||||||
#define BRANCH OP_HAS_BRANCH, 2
|
|
||||||
#define CFUNC (OP_HAS_CFUNC | OP_HAS_BINDING), 3
|
|
||||||
#define UFUNC (OP_HAS_UFUNC | OP_HAS_BINDING | OP_IS_CALL_PSEUDO), 4
|
|
||||||
#define DEFINITION (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 0
|
|
||||||
#define CLOSURE_REF_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 2
|
|
||||||
|
|
||||||
#define OP(name, imm, in, out) \
|
|
||||||
{name, #name, imm, in, out},
|
|
||||||
|
|
||||||
static const struct opcode_description opcode_descriptions[] = {
|
|
||||||
#include "opcode_list.h"
|
|
||||||
};
|
|
||||||
|
|
||||||
static const struct opcode_description invalid_opcode_description = {
|
|
||||||
-1, "#INVALID", 0, 0, 0, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const struct opcode_description* opcode_describe(opcode op) {
|
|
||||||
if ((int)op >= 0 && (int)op < NUM_OPCODES) {
|
|
||||||
return &opcode_descriptions[op];
|
|
||||||
} else {
|
|
||||||
return &invalid_opcode_description;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
#ifndef OPCODE_H
|
|
||||||
#define OPCODE_H
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
#define OP(name, imm, in, out) name,
|
|
||||||
#include "opcode_list.h"
|
|
||||||
#undef OP
|
|
||||||
} opcode;
|
|
||||||
|
|
||||||
enum {
|
|
||||||
NUM_OPCODES =
|
|
||||||
#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,
|
|
||||||
};
|
|
||||||
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);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in new issue
Block a user