lama_byterun/byterun/src/interpreter.c

475 lines
13 KiB
C
Raw Normal View History

2024-10-31 00:54:04 +03:00
#include "interpreter.h"
2024-10-31 21:08:48 +03:00
#include "../../runtime/gc.h"
#include "../../runtime/runtime.h"
2024-10-31 21:08:48 +03:00
#include "runtime_externs.h"
#include "stack.h"
#include "types.h"
#include "utils.h"
2024-11-14 14:02:36 +03:00
#define ASSERT_UNBOXED(memo, x) \
do \
if (!UNBOXED(x)) \
failure("unboxed value expected in %s\n", memo); \
while (0)
2024-11-12 02:38:26 +03:00
struct State s;
static inline int ip_read_int(char **ip) {
2024-11-14 00:50:43 +03:00
if (*ip + sizeof(int) > s.bf->code_ptr + s.bf->code_size) {
failure("last command is invalid, int parameter can not be read\n");
}
*ip += sizeof(int);
return *(int *)((*ip) - sizeof(int));
}
2024-11-14 00:50:43 +03:00
static inline uint8_t ip_read_byte(char **ip) {
if (*ip + sizeof(char) > s.bf->code_ptr + s.bf->code_size) {
failure("last command is invalid, byte parameter can not be read\n");
}
return *(*ip)++;
}
2024-11-14 00:50:43 +03:00
static inline const char *ip_read_string(char **ip) {
return get_string(s.bf, ip_read_int(ip));
}
2024-11-12 02:12:28 +03:00
const size_t BUFFER_SIZE = 1000;
2024-12-13 13:32:50 +03:00
void run(Bytefile *bf, int argc, char **argv) {
2024-11-12 21:08:41 +03:00
size_t stack[STACK_SIZE];
2024-11-12 02:12:28 +03:00
void *buffer[BUFFER_SIZE];
2024-11-14 00:50:43 +03:00
construct_state(bf, &s, (void **)stack);
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
printf("--- interpreter run ---\n");
2024-11-07 19:07:26 +03:00
#endif
2024-11-07 19:31:25 +03:00
// argc, argv
{
2024-11-12 02:38:26 +03:00
s_push_i(BOX(argc));
2024-11-07 19:31:25 +03:00
for (size_t i = 0; i < argc; ++i) {
2024-11-14 00:50:43 +03:00
s_push(Bstring((aint *)&argv[argc - i - 1]));
2024-11-07 19:31:25 +03:00
}
2024-11-14 00:50:43 +03:00
s_push(Barray((aint *)s_peek(), argc));
void *argv_elem = s_pop();
s_popn(argc);
s_push(argv_elem);
2024-11-07 19:31:25 +03:00
}
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
printf("- loop start\n");
2024-11-07 19:07:26 +03:00
#endif
do {
bool call_happened = false;
2024-11-12 21:08:41 +03:00
if (s.ip >= bf->code_ptr + bf->code_size) {
s_failure(&s, "instruction pointer is out of range (>= size)");
}
if (s.ip < bf->code_ptr) {
s_failure(&s, "instruction pointer is out of range (< 0)");
}
s.instr_ip = s.ip;
2024-11-14 00:50:43 +03:00
uint8_t x = ip_read_byte(&s.ip), h = (x & 0xF0) >> 4, l = x & 0x0F;
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
printf("0x%.8x\n", s.ip - bf->code_ptr - 1);
2024-11-07 19:07:26 +03:00
#endif
switch (h) {
2024-11-14 00:50:43 +03:00
case CMD_EXIT:
goto stop;
/* BINOP */
2024-11-14 00:50:43 +03:00
case CMD_BINOP: { // BINOP ops[l-1]
2024-11-14 14:02:36 +03:00
void *snd = s_pop();
void *fst = s_pop();
if (l == CMD_BINOP_SUB) {
s_push_i(Ls__Infix_45(fst, snd));
} else {
switch (l) {
#define BINOP_OPR(val, op) \
case val: \
ASSERT_UNBOXED("captured op:1", fst); \
ASSERT_UNBOXED("captured op:2", snd); \
s_push_i(BOX(UNBOX(fst) op UNBOX(snd))); \
break;
FORALL_BINOP(BINOP_OPR)
#undef BINOP_OPR
default:
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
break;
}
2024-11-14 00:50:43 +03:00
}
break;
2024-11-14 00:50:43 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC:
switch (l) {
2024-11-14 00:50:43 +03:00
case CMD_BASIC_CONST: // CONST %d
2024-11-12 02:38:26 +03:00
s_push_i(BOX(ip_read_int(&s.ip)));
break;
2024-11-14 00:50:43 +03:00
case CMD_BASIC_STRING: { // STRING %s
void *str = (void *)ip_read_string(&s.ip);
2024-11-12 02:38:26 +03:00
s_push(Bstring((aint *)&str));
break;
2024-11-02 01:19:54 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC_SEXP: { // SEXP %s %d // create sexpr with tag=%s and %d
// elements from
// stack
// params read from stack
2024-11-14 00:50:43 +03:00
const char *name = ip_read_string(&s.ip);
size_t args_count = ip_read_int(&s.ip);
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
2024-11-07 19:31:25 +03:00
printf("tag hash is %i, n is %i\n", UNBOX(LtagHash((char *)name)),
2024-11-07 01:14:57 +03:00
args_count);
2024-11-07 19:07:26 +03:00
#endif
2024-12-13 13:32:50 +03:00
void **opr_buffer = (void**)(args_count >= BUFFER_SIZE
2024-11-14 00:50:43 +03:00
? alloc((args_count + 1) * sizeof(void *))
2024-12-13 13:32:50 +03:00
: buffer);
2024-11-14 02:10:24 +03:00
// s_put_nth(args_count, (void *)LtagHash((char *)name));
2024-11-07 19:31:25 +03:00
for (size_t i = 1; i <= args_count; ++i) {
2024-11-12 02:38:26 +03:00
opr_buffer[args_count - i] = s_pop();
}
2024-11-12 02:12:28 +03:00
opr_buffer[args_count] = (void *)LtagHash((char *)name);
2024-11-12 02:12:28 +03:00
void *sexp = Bsexp((aint *)opr_buffer, BOX(args_count + 1));
2024-11-14 02:10:24 +03:00
// void *sexp = Bsexp((aint *)s_peek(), BOX(args_count + 1));
// s_popn(args_count + 1);
2024-11-12 02:38:26 +03:00
s_push(sexp);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC_STI: { // STI - write by ref (?)
2024-11-07 19:31:25 +03:00
// NOTE: example not found, no checks done
2024-11-12 02:38:26 +03:00
void *elem = s_pop();
void **addr = (void **)s_pop();
2024-11-07 01:14:57 +03:00
*addr = elem;
2024-11-12 02:38:26 +03:00
s_push(elem);
break;
2024-11-07 01:14:57 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC_STA: { // STA - write to array elem
2024-11-12 02:38:26 +03:00
void *elem = s_pop();
aint index = s_pop_i();
void *data = s_pop();
s_push(Bsta(data, index, elem));
break;
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC_JMP: { // JMP 0x%.8x
uint jmp_p = ip_read_int(&s.ip);
2024-11-14 00:50:43 +03:00
if (jmp_p >= bf->code_size) {
s_failure(&s, "jump out of file");
}
s.ip = bf->code_ptr + jmp_p;
break;
}
2024-11-14 00:50:43 +03:00
case CMD_BASIC_END: // END
2024-11-12 02:38:26 +03:00
if (!s_is_empty() && s.fp->prev_fp != 0) {
s.fp->ret = *s_peek();
s_pop();
}
2024-11-12 02:38:26 +03:00
s_exit_f();
break;
2024-11-14 00:50:43 +03:00
case CMD_BASIC_RET: // RET
2024-11-12 02:38:26 +03:00
if (!s_is_empty() && s.fp->prev_fp != 0) {
s.fp->ret = *s_peek();
s_pop();
}
2024-11-12 02:38:26 +03:00
s_exit_f();
break;
2024-11-14 00:50:43 +03:00
case CMD_BASIC_DROP: // DROP
2024-11-12 02:38:26 +03:00
s_pop();
break;
2024-11-14 00:50:43 +03:00
case CMD_BASIC_DUP: // DUP
{
2024-11-12 02:38:26 +03:00
s_push(*s_peek());
break;
}
2024-10-12 00:37:28 +03:00
2024-11-14 00:50:43 +03:00
case CMD_BASIC_SWAP: // SWAP
{
2024-11-14 00:50:43 +03:00
void *x = s_pop();
void *y = s_pop();
2024-11-12 02:38:26 +03:00
s_push(y);
s_push(x);
} break;
2024-11-14 00:50:43 +03:00
case CMD_BASIC_ELEM: // ELEM
{
2024-11-12 02:38:26 +03:00
aint index = s_pop_i();
void *data = s_pop();
s_push(Belem(data, index));
} break;
default:
2024-11-12 00:10:02 +03:00
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
}
break;
2024-11-14 00:50:43 +03:00
case CMD_LD: { // LD %d
void **var_ptr = var_by_category(to_var_category(l), ip_read_int(&s.ip));
2024-11-12 02:38:26 +03:00
s_push(*var_ptr);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_LDA: { // LDA %d
void **var_ptr = var_by_category(to_var_category(l), ip_read_int(&s.ip));
2024-11-12 02:38:26 +03:00
s_push(*var_ptr);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_ST: { // ST %d
void **var_ptr = var_by_category(to_var_category(l), ip_read_int(&s.ip));
2024-11-12 02:38:26 +03:00
*var_ptr = *s_peek();
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL:
switch (l) {
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CJMPz: { // CJMPz 0x%.8x
uint jmp_p = ip_read_int(&s.ip);
2024-11-14 00:50:43 +03:00
if (jmp_p >= bf->code_size) {
s_failure(&s, "jump out of file");
}
2024-11-12 02:38:26 +03:00
if (UNBOX(s_pop_i()) == 0) {
s.ip = bf->code_ptr + jmp_p;
2024-10-12 00:37:28 +03:00
}
break;
2024-10-12 00:37:28 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CJMPnz: { // CJMPnz 0x%.8x
uint jmp_p = ip_read_int(&s.ip);
2024-11-14 00:50:43 +03:00
if (jmp_p >= bf->code_size) {
s_failure(&s, "jump out of file");
}
2024-11-12 02:38:26 +03:00
if (UNBOX(s_pop_i()) != 0) {
s.ip = bf->code_ptr + jmp_p;
2024-10-12 00:37:28 +03:00
}
break;
2024-10-12 00:37:28 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_BEGIN: { // BEGIN %d %d // function begin
int args_sz = ip_read_int(&s.ip);
int locals_sz = ip_read_int(&s.ip);
if (s.fp != NULL && s.call_ip == NULL) {
2024-11-12 00:10:02 +03:00
s_failure(&s, "begin should only be called after call");
}
2024-11-12 02:38:26 +03:00
s_enter_f(s.call_ip /*ip from call*/, s.is_closure_call, args_sz,
2024-11-07 01:14:57 +03:00
locals_sz);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CBEGIN: { // CBEGIN %d %d
2024-11-07 19:31:25 +03:00
// NOTE: example not found, no checks done
int args_sz = ip_read_int(&s.ip);
int locals_sz = ip_read_int(&s.ip);
if (s.fp != NULL && s.call_ip == NULL) {
2024-11-12 00:10:02 +03:00
s_failure(&s, "begin should only be called after call");
}
2024-11-12 02:38:26 +03:00
s_enter_f(s.call_ip /*ip from call*/, s.is_closure_call, args_sz,
2024-11-07 01:14:57 +03:00
locals_sz);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CLOSURE: // CLOSURE 0x%.8x
2024-11-07 01:14:57 +03:00
{
aint call_offset = ip_read_int(&s.ip);
aint args_count = ip_read_int(&s.ip);
for (aint i = 0; i < args_count; i++) {
aint arg_type = ip_read_byte(&s.ip);
aint arg_id = ip_read_int(&s.ip);
2024-11-07 01:14:57 +03:00
void **var_ptr =
2024-11-12 02:38:26 +03:00
var_by_category(to_var_category(l), ip_read_int(&s.ip));
s_push(*var_ptr);
2024-11-07 01:14:57 +03:00
}
2024-11-22 17:10:40 +03:00
if (call_offset >= bf->code_size) {
s_failure(&s, "jump out of file");
}
2024-11-12 02:38:26 +03:00
s_push(bf->code_ptr + call_offset);
2024-11-14 00:50:43 +03:00
void *closure = Bclosure((aint *)__gc_stack_top, args_count);
2024-11-12 02:38:26 +03:00
s_popn(args_count + 1);
s_push(closure);
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CALLC: { // CALLC %d // call clojure
2024-11-07 00:56:21 +03:00
aint args_count = ip_read_int(&s.ip); // args count
call_happened = true;
s.is_closure_call = true;
s.call_ip = s.ip;
2024-12-13 13:32:50 +03:00
s.ip = (char*)Belem(*s_nth(args_count), BOX(0)); // use offset instead ??
break;
2024-11-07 00:56:21 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_CALL: { // CALL 0x%.8x %d // call function
uint call_p = ip_read_int(&s.ip);
ip_read_int(&s.ip); // args count
call_happened = true;
s.is_closure_call = false;
s.call_ip = s.ip;
2024-11-14 00:50:43 +03:00
if (call_p >= bf->code_size) {
s_failure(&s, "jump out of file");
}
s.ip = bf->code_ptr + call_p;
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_TAG: { // TAG %s %d
const char *name = ip_read_string(&s.ip);
aint args_count = ip_read_int(&s.ip);
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
2024-11-07 01:14:57 +03:00
printf("tag hash is %i, n is %i, peek is %i\n",
UNBOX(LtagHash((char *)name)), args_count, s_peek(&s));
2024-11-07 19:07:26 +03:00
#endif
2024-11-12 02:38:26 +03:00
s_push_i(Btag(s_pop(), LtagHash((char *)name), BOX(args_count)));
break;
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_ARRAY: // ARRAY %d
2024-11-12 02:38:26 +03:00
s_push_i(Barray_patt(s_pop(), BOX(ip_read_int(&s.ip))));
break;
2024-11-14 00:50:43 +03:00
case CMD_CTRL_FAIL: { // FAIL %d %d
int line = ip_read_int(&s.ip);
int col = ip_read_int(&s.ip);
2024-11-12 02:38:26 +03:00
Bmatch_failure(s_pop(), argv[0], BOX(line), BOX(col));
break;
2024-11-09 23:32:09 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_CTRL_LINE: // LINE %d
2024-11-09 23:32:09 +03:00
s.current_line = ip_read_int(&s.ip);
2024-10-12 00:37:28 +03:00
// maybe some metainfo should be collected
break;
default:
2024-11-12 00:10:02 +03:00
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
}
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT: // PATT pats[l]
2024-11-02 01:19:54 +03:00
// {"=str", "#string", "#array", "#sexp", "#ref", "#val", "#fun"}
switch (l) {
2024-11-14 00:50:43 +03:00
case CMD_PATT_STR: // =str
2024-11-12 02:38:26 +03:00
s_push_i(Bstring_patt(s_pop(), s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_STR_TAG: // #string
2024-11-12 02:38:26 +03:00
s_push_i(Bstring_tag_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_ARRAY_TAG: // #array
2024-11-12 02:38:26 +03:00
s_push_i(Barray_tag_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_SEXP_TAG: // #sexp
2024-11-12 02:38:26 +03:00
s_push_i(Bsexp_tag_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_REF_TAG: // #ref
2024-11-12 02:38:26 +03:00
s_push_i(Bunboxed_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_VAL_TAG: // #val
2024-11-12 02:38:26 +03:00
s_push_i(Bboxed_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
2024-11-14 00:50:43 +03:00
case CMD_PATT_FUN_TAG: // #fun
2024-11-12 02:38:26 +03:00
s_push_i(Bclosure_tag_patt(s_pop()));
2024-11-02 01:19:54 +03:00
break;
default:
2024-11-12 00:10:02 +03:00
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
2024-11-02 01:19:54 +03:00
}
break;
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN: {
switch (l) {
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN_Lread: // CALL Lread
2024-11-12 02:38:26 +03:00
s_push_i(Lread());
break;
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN_Lwrite: // CALL Lwrite
2024-11-12 02:38:26 +03:00
Lwrite(*s_peek_i());
break;
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN_Llength: // CALL Llength
2024-11-12 02:38:26 +03:00
s_push_i(Llength(s_pop()));
break;
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN_Lstring: { // CALL Lstring
2024-11-12 02:38:26 +03:00
void *val = s_pop();
2024-11-07 01:14:57 +03:00
void *str = Lstring((aint *)&val);
2024-11-12 02:38:26 +03:00
s_push(str);
break;
2024-11-02 01:19:54 +03:00
}
2024-11-14 00:50:43 +03:00
case CMD_BUILTIN_Barray: { // CALL Barray %d
2024-11-09 23:32:09 +03:00
size_t elem_count = ip_read_int(&s.ip);
2024-12-13 13:32:50 +03:00
void **opr_buffer = (void**)(elem_count > BUFFER_SIZE
2024-11-14 14:14:33 +03:00
? alloc(elem_count * sizeof(void *))
2024-12-13 13:32:50 +03:00
: buffer);
2024-11-14 14:14:33 +03:00
for (size_t i = 0; i < elem_count; ++i) {
opr_buffer[elem_count - i - 1] = s_pop();
}
// s_rotate_n(elem_count);
void *array =
Barray((aint *)opr_buffer,
BOX(elem_count)); // NOTE: not shure if elems should be
// added
// void *array = Barray((aint *)s_peek(), BOX(elem_count));
2024-11-12 02:38:26 +03:00
s_push(array);
break;
2024-11-02 01:19:54 +03:00
}
default:
2024-11-12 00:10:02 +03:00
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
}
} break;
default:
2024-11-12 00:10:02 +03:00
s_failure(&s, "invalid opcode"); // %d-%d\n", h, l);
}
if (!call_happened) {
s.is_closure_call = false;
s.call_ip = NULL;
}
if (s.fp == NULL) {
break;
}
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
print_stack(&s);
2024-11-07 19:07:26 +03:00
#endif
} while (1);
2024-10-12 00:37:28 +03:00
stop:;
2024-11-07 19:07:26 +03:00
#ifdef DEBUG_VERSION
printf("--- run end ---\n");
2024-11-07 19:07:26 +03:00
#endif
2024-10-31 21:08:48 +03:00
cleanup_state(&s);
}