From 1df389bfd5c23ba4332cd13d3d7c5b61be29a3d4 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Tue, 10 Nov 2015 17:57:05 -0500 Subject: fix some compiler bugs --- src/compile.c | 141 ++++++++++++++++++++++++++++++++-------------------------- src/ducky.h | 1 + src/interp.c | 3 +- 3 files changed, 80 insertions(+), 65 deletions(-) (limited to 'src') diff --git a/src/compile.c b/src/compile.c index dd4703e..ca855e2 100644 --- a/src/compile.c +++ b/src/compile.c @@ -4,24 +4,13 @@ #include "opcodes.h" /******************************************************************************* - * Bytecode format: - * { }... - * 0x00 - delay S[0] ms, pop - * 0x01 - mark as constant - vars[A].const = true - * 0x02 - push vars[A] - * 0x03 - push *B - * 0x04 - pop into vars[A] - * 0x05 - add the two numbers on top of the stack - * 0x06 - subtract the number on the top of the stack from the one below it - * 0x07 - multiply the two numbers on top of the stack - * 0x08 - S[1]/S[0] - * 0x09 - jump to S[0]; pop - * 0x0A - if S[1] == 0; JUMP S[0]; pop both - * 0x0B - eval S[1] ** S[0] - * 0x0C - eval S[1] && S[0] - * 0x0D - eval S[1] & S[0] - * 0x0E - eval S[1] << S[0] - * REPEAT: exec line s[0] s[1] times + * Bytecode format (all numbers are little-endian): + * + * +0x00: 'DucK' signature (4 bytes) + * +0x04: number of lines (4 bytes) + * +0x08: line offsets (4 * num lines) + * + : compiler-generated init code + * +(0x08): start of compiled code */ /*** Defines ***/ @@ -43,8 +32,6 @@ #define MIN(x,y) ((xMAXOPSTACK - 1) { @@ -545,22 +552,6 @@ static const struct op_s *pop_opstack(void) return opstack[--nopstack]; } -static void push_numstack(vartype num) -{ - if(nnumstack>MAXNUMSTACK - 1) { - error("number stack overflow"); - } - numstack[nnumstack++] = num; -} - -static vartype pop_numstack(void) -{ - if(!nnumstack) { - error("number stack empty"); - } - return numstack[--nnumstack]; -} - static bool isDigit(char c) { return '0' <= c && c <= '9'; @@ -713,7 +704,7 @@ static vartype eval_expr(char *str) /* isolate the variable name into a buffer */ char varname[VARNAME_MAX + 1] = { 0 }; - memcpy(varname, str, expr - tstart); + memcpy(varname, tstart, expr - tstart); write_varid(get_varid(varname)); } else @@ -733,7 +724,7 @@ static vartype eval_expr(char *str) /* isolate the variable name into a buffer */ char varname[VARNAME_MAX + 1] = { 0 }; - memcpy(varname, str, expr - tstart); + memcpy(varname, tstart, expr - tstart); write_varid(get_varid(varname)); } else @@ -762,7 +753,7 @@ static vartype eval_expr(char *str) /* isolate the variable name into a buffer */ char varname[VARNAME_MAX + 1] = { 0 }; - memcpy(varname, str, expr - tstart); + memcpy(varname, tstart, expr - tstart); write_varid(get_varid(varname)); } else @@ -938,6 +929,7 @@ static int logvar_handler(char **save) static int rem_handler(char **save) { (void) save; + vid_logf("REM, skipping line"); return BREAK; } @@ -1179,6 +1171,7 @@ static void init_globals(void) file_des = -1; stack_frame = 0; current_line = 0; + bytes_written = 0; } void ducky_compile(int fd, bool verbose, int out) @@ -1201,20 +1194,33 @@ void ducky_compile(int fd, bool verbose, int out) init_optable(); init_tokmap(); - /* initialize some other constants */ - //setVariable("true", 1); - //setConst("true", true); - - //setVariable("false", 0); - //setConst("false", true); - line_offset = index_lines(file_des, &num_lines); + write_imm(0x4475634B); + write_imm(num_lines); + off_t linetab_off = bytes_written; + for(unsigned i = 1; i <= num_lines; ++i) + { + write_imm(0); + } if(verbose) { vid_logf("Indexing complete (%u lines).", num_lines); vid_logf("Compiling..."); } + + /* initialize some other constants */ + + setVariable("true", 1); + setConst("true", true); + + setVariable("false", 0); + setConst("false", true); + + /* initialize labels (using output from index_lines) */ + index_labels(file_des); + int repeats_left = 0; + off_t code_start = bytes_written; while(1) { @@ -1232,7 +1238,9 @@ void ducky_compile(int fd, bool verbose, int out) char *buf = instr_buf; - /* execute all the commands on this line/instruction */ + line_offset[current_line] = bytes_written; + + /* compile all the commands on this line/instruction */ do { tok = strtok_r(buf, " -\t", &save); buf = NULL; @@ -1270,5 +1278,12 @@ void ducky_compile(int fd, bool verbose, int out) done: + /* go back and fill in the offset table */ + lseek(out_fd, linetab_off, SEEK_SET); + for(unsigned i = 1; i <= num_lines; ++i) + { + write_imm(line_offset[i]); + } + return; } diff --git a/src/ducky.h b/src/ducky.h index b92b96c..043492b 100644 --- a/src/ducky.h +++ b/src/ducky.h @@ -7,3 +7,4 @@ void ducky_compile(int fd, bool verbose, int out_fd); typedef int32_t imm_t; typedef uint8_t instr_t; typedef uint16_t varid_t; +typedef imm_t vartype; diff --git a/src/interp.c b/src/interp.c index 0d809dc..6c198b3 100644 --- a/src/interp.c +++ b/src/interp.c @@ -1,4 +1,5 @@ #include +#include /******************************************************************************* * The scripting language implemented here is an extension of DuckyScript. @@ -44,8 +45,6 @@ #define MIN(x,y) ((x