Browse Source

fix bug for promises; refactor vm_stack

master
CismonX 1 year ago
parent
commit
300c1a09a1
No known key found for this signature in database GPG Key ID: 315D6652268C5007
  1. 82
      src/runtime.c
  2. 21
      src/vm_stack.c
  3. 5
      src/vm_stack.h

82
src/runtime.c

@ -46,56 +46,58 @@ static const uint32_t text_subst_len = sizeof(text_subst) / sizeof(struct u6a_vm
static const char* err_runtime = "runtime error";
static const char* info_runtime = "runtime";
#define CHECK_BC_HEADER_VER(file_header) \
#define CHECK_BC_HEADER_VER(file_header) \
( (file_header).ver_major == U6A_VER_MAJOR && (file_header).ver_minor == U6A_VER_MINOR )
// Addref before free, for acc may equal to fn
#define ACC_FN(fn_) \
vm_var_fn_addref(fn_); \
vm_var_fn_free(acc); \
#define ACC_FN(fn_) \
vm_var_fn_addref(fn_); \
vm_var_fn_free(acc); \
acc = fn_
#define ACC_FN_INIT(fn_) \
vm_var_fn_free(acc); \
#define ACC_FN_INIT(fn_) \
vm_var_fn_free(acc); \
acc = fn_
#define ACC_FN_REF(fn_, ref_) \
vm_var_fn_free(acc); \
acc = U6A_VM_VAR_FN_REF(fn_, ref_); \
if (UNLIKELY(acc.ref == UINT32_MAX)) { \
goto runtime_error; \
#define ACC_FN_REF(fn_, ref_) \
vm_var_fn_free(acc); \
acc = U6A_VM_VAR_FN_REF(fn_, ref_); \
if (UNLIKELY(acc.ref == UINT32_MAX)) { \
goto runtime_error; \
}
#define VM_JMP(dest) \
ins = text + (dest); \
#define VM_JMP(dest) \
ins = text + (dest); \
continue
#define VM_VAR_JMP \
#define VM_VAR_JMP \
U6A_VM_VAR_FN_REF(u6a_vf_j, ins - text)
#define CHECK_FORCE(log_func, err_val) \
if (!force_exec) { \
log_func(err_runtime, err_val); \
goto runtime_error; \
#define VM_VAR_FINALIZE \
U6A_VM_VAR_FN_REF(u6a_vf_f, ins - text)
#define CHECK_FORCE(log_func, err_val) \
if (!force_exec) { \
log_func(err_runtime, err_val); \
goto runtime_error; \
}
#define STACK_PUSH1(fn_0) \
vm_var_fn_addref(fn_0); \
if (UNLIKELY(!u6a_vm_stack_push1(fn_0))) { \
goto runtime_error; \
#define STACK_PUSH1(fn_0) \
vm_var_fn_addref(fn_0); \
if (UNLIKELY(!u6a_vm_stack_push1(fn_0))) { \
goto runtime_error; \
}
#define STACK_PUSH2(fn_0, fn_1) \
if (UNLIKELY(!u6a_vm_stack_push2(fn_0, fn_1))) { \
goto runtime_error; \
#define STACK_PUSH2(fn_0, fn_1) \
if (UNLIKELY(!u6a_vm_stack_push2(fn_0, fn_1))) { \
goto runtime_error; \
}
#define STACK_PUSH3(fn_0, fn_12) \
if (UNLIKELY(!u6a_vm_stack_push3(fn_0, fn_12))) { \
goto runtime_error; \
#define STACK_PUSH3(fn_0, fn_1, fn_2) \
if (UNLIKELY(!u6a_vm_stack_push3(fn_0, fn_1, fn_2))) { \
goto runtime_error; \
}
#define STACK_PUSH4(fn_0, fn_1, fn_23) \
if (UNLIKELY(!u6a_vm_stack_push4(fn_0, fn_1, fn_23))) { \
goto runtime_error; \
#define STACK_PUSH4(fn_0, fn_1, fn_2, fn_3) \
if (UNLIKELY(!u6a_vm_stack_push4(fn_0, fn_1, fn_2, fn_3))) { \
goto runtime_error; \
}
#define STACK_POP() \
vm_var_fn_free(top); \
top = u6a_vm_stack_top(); \
if (UNLIKELY(!u6a_vm_stack_pop())) { \
goto runtime_error; \
#define STACK_POP() \
vm_var_fn_free(top); \
top = u6a_vm_stack_top(); \
if (UNLIKELY(!u6a_vm_stack_pop())) { \
goto runtime_error; \
}
static inline bool
@ -251,9 +253,9 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
vm_var_fn_addref(tuple.v2.fn);
vm_var_fn_addref(arg);
if (ins - text == 0x03) {
STACK_PUSH3(arg, tuple);
STACK_PUSH3(arg, tuple.v2.fn, tuple.v1.fn);
} else {
STACK_PUSH4(VM_VAR_JMP, arg, tuple);
STACK_PUSH4(VM_VAR_JMP, arg, tuple.v2.fn, tuple.v1.fn);
}
ACC_FN(arg);
VM_JMP(0x00);
@ -305,11 +307,11 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) {
VM_JMP(0x03);
case u6a_vf_d1_s:
tuple = u6a_vm_pool_get2(func.ref);
STACK_PUSH1(tuple.v1.fn);
STACK_PUSH3(vm_var_fn_addref(arg), VM_VAR_FINALIZE, tuple.v1.fn);
ACC_FN(tuple.v2.fn);
VM_JMP(0x03);
case u6a_vf_d1_d:
STACK_PUSH2(vm_var_fn_addref(arg), U6A_VM_VAR_FN_REF(u6a_vf_f, ins - text));
STACK_PUSH2(vm_var_fn_addref(arg), VM_VAR_FINALIZE);
VM_JMP(func.ref);
case u6a_vf_v:
vm_var_fn_free(acc);

21
src/vm_stack.c

@ -154,12 +154,12 @@ u6a_vm_stack_push2(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1) {
}
U6A_HOT bool
u6a_vm_stack_push3(struct u6a_vm_var_fn v0, struct u6a_vm_var_tuple v12) {
u6a_vm_stack_push3(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1, struct u6a_vm_var_fn v2) {
struct vm_stack* vs = active_stack;
if (LIKELY(vs->top + 3 < stack_seg_len)) {
vs->elems[++vs->top] = v0;
vs->elems[++vs->top] = v12.v2.fn;
vs->elems[++vs->top] = v12.v1.fn;
vs->elems[++vs->top] = v1;
vs->elems[++vs->top] = v2;
return true;
}
active_stack = vm_stack_create(vs, 2);
@ -169,19 +169,20 @@ u6a_vm_stack_push3(struct u6a_vm_var_fn v0, struct u6a_vm_var_tuple v12) {
}
++vs->refcnt;
active_stack->elems[0] = v0;
active_stack->elems[1] = v12.v2.fn;
active_stack->elems[2] = v12.v1.fn;
active_stack->elems[1] = v1;
active_stack->elems[2] = v2;
return true;
}
U6A_HOT bool
u6a_vm_stack_push4(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1, struct u6a_vm_var_tuple v23) {
u6a_vm_stack_push4(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2, struct u6a_vm_var_fn v3) {
struct vm_stack* vs = active_stack;
if (LIKELY(vs->top + 4 < stack_seg_len)) {
vs->elems[++vs->top] = v0;
vs->elems[++vs->top] = v1;
vs->elems[++vs->top] = v23.v2.fn;
vs->elems[++vs->top] = v23.v1.fn;
vs->elems[++vs->top] = v2;
vs->elems[++vs->top] = v3;
return true;
}
active_stack = vm_stack_create(vs, 3);
@ -192,8 +193,8 @@ u6a_vm_stack_push4(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1, struct u6a_
++vs->refcnt;
active_stack->elems[0] = v0;
active_stack->elems[1] = v1;
active_stack->elems[2] = v23.v2.fn;
active_stack->elems[3] = v23.v1.fn;
active_stack->elems[2] = v2;
active_stack->elems[3] = v3;
return true;
}

5
src/vm_stack.h

@ -41,10 +41,11 @@ u6a_vm_stack_push2(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1);
// Functions push3 and push4 are made for the s2 function to alleviate overhead caused by hot split
bool
u6a_vm_stack_push3(struct u6a_vm_var_fn v0, struct u6a_vm_var_tuple v12);
u6a_vm_stack_push3(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1, struct u6a_vm_var_fn v2);
bool
u6a_vm_stack_push4(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1, struct u6a_vm_var_tuple v23);
u6a_vm_stack_push4(struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2, struct u6a_vm_var_fn v3);
bool
u6a_vm_stack_pop();

Loading…
Cancel
Save