From e4515e9657745da852a7a796a6f20d1aa94a43ee Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 1 Dec 2014 22:19:38 -0500 Subject: [PATCH] Combined standalone interpreter into onward.c with a build option --- build.rb | 8 +- source/main.c | 109 ----------------- source/{onward => }/onward.c | 195 ++++++++++++++++++++++++++----- source/{onward => }/onward.ft | 0 source/{onward => }/onward.h | 0 source/{onward => }/onward_sys.h | 0 source/syscall.c | 75 ------------ source/syscall.h | 14 --- 8 files changed, 169 insertions(+), 232 deletions(-) delete mode 100755 source/main.c rename source/{onward => }/onward.c (80%) rename source/{onward => }/onward.ft (100%) rename source/{onward => }/onward.h (100%) rename source/{onward => }/onward_sys.h (100%) delete mode 100644 source/syscall.c delete mode 100644 source/syscall.h diff --git a/build.rb b/build.rb index c7c3dc5..f48b8d6 100755 --- a/build.rb +++ b/build.rb @@ -34,8 +34,7 @@ end #------------------------------------------------------------------------------ unless Opts[:profile].include? "no-tests" test_env.Program('onward-tests', [ - 'source/onward/onward.c', - 'source/syscall.c', + 'source/onward.c', 'modules/atf/source/atf.c'] + Dir['tests/**/*.c']) test_env.Command('Unit Tests', ['./onward-tests'], 'CMD' => ['./onward-tests']) @@ -44,6 +43,7 @@ end #------------------------------------------------------------------------------ # Release Build Targets #------------------------------------------------------------------------------ -main_env.Library('libonward.a', FileList['source/onward/*.c']) -main_env.Program('onward', ['source/main.c', 'libonward.a']) +main_env.Library('libonward.a', FileList['source/*.c']) +main_env.Program('onward', FileList['source/*.c'], + 'CFLAGS' => main_env['CFLAGS'] + ['-DSTANDALONE']) diff --git a/source/main.c b/source/main.c deleted file mode 100755 index 6644411..0000000 --- a/source/main.c +++ /dev/null @@ -1,109 +0,0 @@ -#include "onward_sys.h" -#include "syscall.h" -#include -#include -#include - -defcode("dumpw", dumpw, LATEST_BUILTIN, 0u) { - word_t* word = (word_t*)onward_aspop(); - printf("name:\t'%s'\n", word->name); - printf("flags:\t%#lx\n", word->flags); - printf("link:\t%p\n", word->link); - /* Print the word's instructions */ - if (word->flags & F_PRIMITIVE_MSK) { - printf("code:\t%p\n", word->code); - } else { - printf("code:"); - word_t** code = (word_t**)word->code; - while(*code) { - printf("\t%s", (*code)->name); - if ((*code == &lit) || (*code == &zbr) || (*code == &br)) - printf(" %zd", (intptr_t)*(++code)); - code++; - puts(""); - } - printf("\tret\n"); - } -} - -defcode("syscall", syscall, &dumpw, 0u) { - System_Calls[onward_aspop()](); -} - -defvar("infile", infile, 0u, &syscall); -defvar("outfile", outfile, 0u, &infile_word); -defvar("errfile", errfile, 0u, &outfile_word); - -/*****************************************************************************/ -bool Newline_Consumed = false; - -value_t fetch_char(void) -{ - value_t ch = (value_t)fgetc((FILE*)infile); - if ((char)ch == '\n') - Newline_Consumed = true; - return ch; -} - -void emit_char(value_t val) -{ - fputc((int)val, (FILE*)outfile); -} - -void print_stack(void) { - value_t* base = (value_t*)asb; - value_t* top = (value_t*)asp; - printf("( "); - int i; - if (top-5 >= base) - printf("... "); - for (i = 4; i >= 0; i--) { - value_t* curr = top-i; - if (curr > base) - printf("%zd ", *curr); - } - puts(")"); - printf("errcode: %zd\n", errcode); - puts(!errcode ? "OK." : "?"); -} - -void parse(FILE* file) { - value_t old = infile; - infile = (value_t)file; - if (file == stdin) - printf(":> "); - while (!feof(file)) { - interp_code(); - if ((file == stdin) && Newline_Consumed) { - print_stack(); - printf(":> "); - Newline_Consumed = false; - errcode = 0; - } - } - infile = old; -} - -void parse_file(char* fname) { - FILE* file = fopen(fname, "r"); - if (file) { - parse(file); - fclose(file); - } -} - -int main(int argc, char** argv) { - int i; - /* Initialize implementation specific words */ - latest = (value_t)&errfile_word; - infile = (value_t)stdin; - outfile = (value_t)stdout; - errfile = (value_t)stderr; - /* Load any dictionaries specified on the command line */ - for (i = 1; i < argc; i++) - parse_file(argv[i]); - /* Start the REPL */ - parse(stdin); - return 0; -} - diff --git a/source/onward/onward.c b/source/onward.c similarity index 80% rename from source/onward/onward.c rename to source/onward.c index d196df2..402a2f5 100755 --- a/source/onward/onward.c +++ b/source/onward.c @@ -4,7 +4,6 @@ #include static value_t char_oneof(char ch, char* chs); -static void abort_on_error(value_t cond, value_t errcode); /** Version number of the implementation */ defconst("VERSION", VERSION, 0, 0u); @@ -523,7 +522,6 @@ defcode("~", bnot, &bxor, 0u) { /* Helper C Functions *****************************************************************************/ - value_t onward_pcfetch(void) { value_t* reg = (value_t*)pc; value_t val = *reg++; @@ -532,16 +530,8 @@ value_t onward_pcfetch(void) { } void onward_aspush(value_t val) { - #ifndef UNSAFE_MODE - if ((asp-asb) <= assz) { - #endif - asp += sizeof(value_t); - *((value_t*)asp) = val; - #ifndef UNSAFE_MODE - } else { - abort_on_error(1, ERR_ARG_STACK_OVRFLW); - } - #endif + asp += sizeof(value_t); + *((value_t*)asp) = val; } value_t onward_aspeek(value_t val) { @@ -551,27 +541,17 @@ value_t onward_aspeek(value_t val) { value_t onward_aspop(void) { value_t val = *((value_t*)asp); asp -= sizeof(value_t); - abort_on_error(asp < asb, ERR_ARG_STACK_UNDRFLW); return val; } void onward_rspush(value_t val) { - #ifndef UNSAFE_MODE - if ((rsp-rsb) <= rssz) { - #endif - rsp += sizeof(value_t); - *((value_t*)rsp) = val; - #ifndef UNSAFE_MODE - } else { - abort_on_error(1, ERR_RET_STACK_OVRFLW); - } - #endif + rsp += sizeof(value_t); + *((value_t*)rsp) = val; } value_t onward_rspop(void) { value_t val = *((value_t*)rsp); rsp -= sizeof(value_t); - abort_on_error(rsp < rsb, ERR_RET_STACK_UNDRFLW); return val; } @@ -587,11 +567,166 @@ static value_t char_oneof(char ch, char* chs) { return ret; } -static void abort_on_error(value_t cond, value_t errcode) { - #ifndef UNSAFE_MODE - if(cond) { - errno = errcode; - _abort_code(); +/* System Calls + *****************************************************************************/ +#ifdef STANDALONE +#include +#include + +static void syscall_open(void) +{ + intptr_t modenum = onward_aspop(); + char* fname = (char*)onward_aspop(); + char* mode; + switch (modenum) { + case 0: mode = "r"; break; + case 1: mode = "w"; break; + case 2: mode = "a"; break; + case 3: mode = "r+"; break; + case 4: mode = "w+"; break; + case 5: mode = "a+"; break; + default: mode = NULL; break; } - #endif + onward_aspush(mode ? (intptr_t)fopen(fname, mode) : 0); +} + +static void syscall_close(void) +{ + onward_aspush(fclose((FILE*)onward_aspop())); +} + +static void syscall_read(void) +{ + size_t nbytes = (size_t)onward_aspop(); + FILE* fhndl = (FILE*)onward_aspop(); + void* dest = (void*)onward_aspop(); + onward_aspush(nbytes != fread(dest, 1u, nbytes, fhndl)); +} + +static void syscall_write(void) +{ + size_t nbytes = (size_t)onward_aspop(); + void* src = (void*)onward_aspop(); + FILE* fhndl = (FILE*)onward_aspop(); + onward_aspush(nbytes != fwrite(src, 1u, nbytes, fhndl)); +} + +static void syscall_seek(void) +{ + intptr_t nbytes = onward_aspop(); + intptr_t origin = onward_aspop(); + FILE* fhndl = (FILE*)onward_aspop(); + origin = (origin == 0) ? SEEK_CUR : (origin < 0) ? SEEK_SET : SEEK_END; + onward_aspush(fseek(fhndl, nbytes, origin)); +} + +static void syscall_alloc(void) +{ + onward_aspush((intptr_t)malloc((size_t)onward_aspop())); } + +static void syscall_free(void) +{ + free((void*)onward_aspop()); +} + +typedef void (*syscall_fn_t)(void); + +static syscall_fn_t System_Calls[7] = { + /* File Operations */ + &syscall_open, + &syscall_close, + &syscall_read, + &syscall_write, + &syscall_seek, + + /* Memory Operations */ + &syscall_alloc, + &syscall_free, +}; +#endif + +/* Standalone Interpreter + *****************************************************************************/ +#ifdef STANDALONE +#include + +defvar("infile", infile, 0u, LATEST_BUILTIN); +defvar("outfile", outfile, 0u, &infile_word); +defvar("errfile", errfile, 0u, &outfile_word); +defcode("syscall", syscall, &errfile_word, 0u) { + System_Calls[onward_aspop()](); +} + +static bool Newline_Consumed = false; + +value_t fetch_char(void) +{ + value_t ch = (value_t)fgetc((FILE*)infile); + if ((char)ch == '\n') + Newline_Consumed = true; + return ch; +} + +void emit_char(value_t val) +{ + fputc((int)val, (FILE*)outfile); +} + +void print_stack(void) { + value_t* base = (value_t*)asb; + value_t* top = (value_t*)asp; + printf("( "); + int i; + if (top-5 >= base) + printf("... "); + for (i = 4; i >= 0; i--) { + value_t* curr = top-i; + if (curr > base) + printf("%zd ", *curr); + } + puts(")"); + printf("errcode: %zd\n", errcode); + puts(!errcode ? "OK." : "?"); +} + +void parse(FILE* file) { + value_t old = infile; + infile = (value_t)file; + if (file == stdin) + printf(":> "); + while (!feof(file)) { + interp_code(); + if ((file == stdin) && Newline_Consumed) { + print_stack(); + printf(":> "); + Newline_Consumed = false; + errcode = 0; + } + } + infile = old; +} + +void parse_file(char* fname) { + FILE* file = fopen(fname, "r"); + if (file) { + parse(file); + fclose(file); + } +} + +int main(int argc, char** argv) { + int i; + /* Initialize implementation specific words */ + latest = (value_t)&syscall; + infile = (value_t)stdin; + outfile = (value_t)stdout; + errfile = (value_t)stderr; + /* Load any dictionaries specified on the command line */ + for (i = 1; i < argc; i++) + parse_file(argv[i]); + /* Start the REPL */ + parse(stdin); + return 0; +} +#endif diff --git a/source/onward/onward.ft b/source/onward.ft similarity index 100% rename from source/onward/onward.ft rename to source/onward.ft diff --git a/source/onward/onward.h b/source/onward.h similarity index 100% rename from source/onward/onward.h rename to source/onward.h diff --git a/source/onward/onward_sys.h b/source/onward_sys.h similarity index 100% rename from source/onward/onward_sys.h rename to source/onward_sys.h diff --git a/source/syscall.c b/source/syscall.c deleted file mode 100644 index 434aab9..0000000 --- a/source/syscall.c +++ /dev/null @@ -1,75 +0,0 @@ -#include "syscall.h" -#include "onward.h" -#include -#include - -void syscall_open(void) -{ - intptr_t modenum = onward_aspop(); - char* fname = (char*)onward_aspop(); - char* mode; - switch (modenum) { - case 0: mode = "r"; break; - case 1: mode = "w"; break; - case 2: mode = "a"; break; - case 3: mode = "r+"; break; - case 4: mode = "w+"; break; - case 5: mode = "a+"; break; - default: mode = NULL; break; - } - onward_aspush(mode ? (intptr_t)fopen(fname, mode) : 0); -} - -void syscall_close(void) -{ - onward_aspush(fclose((FILE*)onward_aspop())); -} - -void syscall_read(void) -{ - size_t nbytes = (size_t)onward_aspop(); - FILE* fhndl = (FILE*)onward_aspop(); - void* dest = (void*)onward_aspop(); - onward_aspush(nbytes != fread(dest, 1u, nbytes, fhndl)); -} - -void syscall_write(void) -{ - size_t nbytes = (size_t)onward_aspop(); - void* src = (void*)onward_aspop(); - FILE* fhndl = (FILE*)onward_aspop(); - onward_aspush(nbytes != fwrite(src, 1u, nbytes, fhndl)); -} - -void syscall_seek(void) -{ - intptr_t nbytes = onward_aspop(); - intptr_t origin = onward_aspop(); - FILE* fhndl = (FILE*)onward_aspop(); - origin = (origin == 0) ? SEEK_CUR : (origin < 0) ? SEEK_SET : SEEK_END; - onward_aspush(fseek(fhndl, nbytes, origin)); -} - -void syscall_alloc(void) -{ - onward_aspush((intptr_t)malloc((size_t)onward_aspop())); -} - -void syscall_free(void) -{ - free((void*)onward_aspop()); -} - -syscall_fn_t System_Calls[7] = { - /* File Operations */ - &syscall_open, - &syscall_close, - &syscall_read, - &syscall_write, - &syscall_seek, - - /* Memory Operations */ - &syscall_alloc, - &syscall_free, -}; - diff --git a/source/syscall.h b/source/syscall.h deleted file mode 100644 index b244f33..0000000 --- a/source/syscall.h +++ /dev/null @@ -1,14 +0,0 @@ -/** - @file sycall.h - @brief TODO: Describe this file - $Revision$ - $HeadURL$ -*/ -#ifndef SYCALL_H -#define SYCALL_H - -typedef void (*syscall_fn_t)(void); - -syscall_fn_t System_Calls[7]; - -#endif /* SYCALL_H */ -- 2.52.0