From f7272a2ed67bea824440647bfe2197bc1b1c8d0d Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 5 Jun 2020 16:54:30 +0800 Subject: [PATCH] refactor code --- configure.ac | 2 +- src/runtime.c | 9 ++++++--- src/runtime.h | 2 +- src/u6a.c | 4 ++-- src/vm_defs.h | 2 ++ src/vm_stack.c | 7 ++++--- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index cae618f..250d56c 100644 --- a/configure.ac +++ b/configure.ac @@ -26,7 +26,7 @@ case "${host_os}" in esac dnl Checks for programs. -AC_PROG_CC_C99 +AC_PROG_CC_STDC dnl Checks for header files. AC_CHECK_HEADERS([arpa/inet.h inttypes.h stddef.h stdint.h stdlib.h string.h unistd.h], diff --git a/src/runtime.c b/src/runtime.c index 2309ed5..aa68fb8 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -206,7 +206,7 @@ u6a_runtime_init(struct u6a_runtime_options* options) { return false; } -U6A_HOT union u6a_vm_var +U6A_HOT struct u6a_vm_var_fn u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) { struct u6a_vm_var_fn acc = { 0 }, top = { 0 }; struct u6a_vm_ins* ins = text + text_subst_len; @@ -345,7 +345,7 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) { VM_JMP(0x03); case u6a_vf_e: // Every program should terminate with explicit `e` function - return U6A_VM_VAR_FN(arg); + return arg; default: CHECK_FORCE(u6a_err_invalid_vm_func, func.token.fn); } @@ -365,6 +365,9 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) { ACC_FN_REF(u6a_vf_d1_s, u6a_vm_pool_alloc2(func, arg)); } else { acc = u6a_vm_stack_xch(acc); + if (UNLIKELY(U6A_VM_VAR_FN_IS_EMPTY(acc))) { + goto runtime_error; + } } break; case u6a_vo_del: @@ -387,7 +390,7 @@ u6a_runtime_execute(FILE* restrict istream, FILE* restrict ostream) { } runtime_error: - return U6A_VM_VAR_PTR(NULL); + return U6A_VM_VAR_FN_EMPTY; } void diff --git a/src/runtime.h b/src/runtime.h index fce8499..6422b8a 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -40,7 +40,7 @@ u6a_runtime_info(FILE* restrict istream, const char* file_name); bool u6a_runtime_init(struct u6a_runtime_options* options); -union u6a_vm_var +struct u6a_vm_var_fn u6a_runtime_execute(FILE* restrict istream, FILE* restrict output_stream); void diff --git a/src/u6a.c b/src/u6a.c index 4b3d035..cfd4735 100644 --- a/src/u6a.c +++ b/src/u6a.c @@ -153,8 +153,8 @@ int main(int argc, char** argv) { exit_code = EC_ERR_INIT; goto terminate; } - union u6a_vm_var exec_result = u6a_runtime_execute(stdin, stdout); - if (UNLIKELY(exec_result.ptr == NULL)) { + struct u6a_vm_var_fn exec_result = u6a_runtime_execute(stdin, stdout); + if (UNLIKELY(U6A_VM_VAR_FN_IS_EMPTY(exec_result))) { exit_code = EC_ERR_RUNTIME; goto terminate; } diff --git a/src/vm_defs.h b/src/vm_defs.h index fc06778..31faa66 100644 --- a/src/vm_defs.h +++ b/src/vm_defs.h @@ -89,6 +89,8 @@ struct u6a_vm_var_fn { }; #define U6A_VM_VAR_FN_REF(fn_, ref_) (struct u6a_vm_var_fn) { .token.fn = (fn_), .ref = (ref_) } +#define U6A_VM_VAR_FN_EMPTY (struct u6a_vm_var_fn) { 0 } +#define U6A_VM_VAR_FN_IS_EMPTY(fn_) ( ((union u6a_vm_var)(fn_)).ptr == NULL ) union u6a_vm_var { struct u6a_vm_var_fn fn; diff --git a/src/vm_stack.c b/src/vm_stack.c index 578585e..a7e64a9 100644 --- a/src/vm_stack.c +++ b/src/vm_stack.c @@ -108,7 +108,7 @@ u6a_vm_stack_top() { if (UNLIKELY(vs->top == UINT32_MAX)) { vs = vs->prev; if (UNLIKELY(vs == NULL)) { - return (struct u6a_vm_var_fn) { 0 }; + return U6A_VM_VAR_FN_EMPTY; } active_stack = vs; } @@ -232,12 +232,13 @@ u6a_vm_stack_xch(struct u6a_vm_var_fn v0) { } else { struct vm_stack* prev = vs->prev; if (UNLIKELY(prev == NULL)) { - return (struct u6a_vm_var_fn) { 0 }; + u6a_err_stack_underflow(err_stage); + return U6A_VM_VAR_FN_EMPTY; } if (--prev->refcnt > 0) { prev = vm_stack_dup(prev); if (UNLIKELY(prev == NULL)) { - return (struct u6a_vm_var_fn) { 0 }; + return U6A_VM_VAR_FN_EMPTY; } } if (vs->top == 0) {