diff --git a/.gitignore b/.gitignore index 81e258a..e54bd6f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ eaxcc library/tommyds/ tmp-* .cmdtest-filter +*.pluglock # temp test_me_please diff --git a/Makefile b/Makefile index 65bb852..88023f9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,29 @@ -.PHONY: clean test +.PHONY: clean test bootstrap .SUFFIXES: +# --- Paths / files +SOURCE.d := source +OBJECT.d := object + +SOURCE := main.c assembler.c eaxhla.c unix.c +OBJECT := $(addprefix ${OBJECT.d}/,${SOURCE}) +OBJECT := ${OBJECT:.c=.o} + +LIBS := tommy.o +LIBS := $(addprefix ${OBJECT.d}/,${LIBS.source}) + +GENSOURCE := eaxhla.yy.c eaxhla.tab.c +GENSOURCE := $(addprefix ${OBJECT.d}/,${GENSOURCE}) +GENOBJECT := $(subst .c,.o,${GENSOURCE}) +PLUGLOCK := token_list.pluglock scanner_instructions.pluglock parser_rules.pluglock +PLUGLOCK := $(addprefix ${OBJECT.d}/,${PLUGLOCK}) +PLUGFILES := ${SOURCE.d}/eaxhla.y ${SOURCE.d}/eaxhla.l + +OUT := eaxhla + +# --- Tools/Flags +PLUG := tool/plug + ifeq (${DEBUG}, 1) LFLAGS += --debug --trace CFLAGS += -O0 -ggdb -fno-inline @@ -18,32 +41,11 @@ else endif CFLAGS += -Wall -Wextra -Wpedantic -CPPFLAGS += -Ilibrary/tommyds -LDFLAGS += -lm - -OUT := eaxhla - -PLUG := tool/plug - -SOURCE.d := source -OBJECT.d := object - -SOURCE := main.c assembler.c eaxhla.c unix.c -OBJECT := $(addprefix ${OBJECT.d}/,${SOURCE}) -OBJECT := ${OBJECT:.c=.o} - -# see library/, run library/bootstrap.sh -LIBS := tommy.o -LIBS := $(addprefix ${OBJECT.d}/,${LIBS.source}) - -GENSOURCE := eaxhla.yy.c eaxhla.tab.c -GENSOURCE := $(addprefix ${OBJECT.d}/,${GENSOURCE}) -GENOBJECT := $(subst .c,.o,${GENSOURCE}) - -CPPFLAGS += -I${OBJECT.d} -I${SOURCE.d} - +CPPFLAGS += -Ilibrary/ -I${OBJECT.d} -I${SOURCE.d} LDLIBS := -lm +# --- Rule Section --- + all: ${OUT} ${OBJECT.d}/%.yy.c: ${SOURCE.d}/%.l @@ -62,29 +64,30 @@ ${OBJECT.d}/%.tab.o: ${OBJECT.d}/%.tab.c ${OBJECT.d}/%.o: ${SOURCE.d}/%.c ${COMPILE.c} -o $@ $< -${OUT}: ${GENSOURCE} ${GENOBJECT} ${OBJECT} ${LIB} - ${LINK.c} -o $@ ${OBJECT} ${GENOBJECT} ${LDLIBS} library/tommyds/tommy.o +${OBJECT.d}/%.pp: tool/instruction_generator/%.tcl tool/instruction_generator/instructions.tcl + tclsh $< > $@ + +unplug: + -rm ${PLUGLOCK} + ${PLUG} -u -d token_list '' -d scanner_instructions '' -d parser_rules '' source/eaxhla.l source/eaxhla.y + +#plug: ${PLUGDEF} +# ${PLUG} -g -e token_list ${OBJECT.d}/token_list.pp source/eaxhla.y +# ${PLUG} -g -e scanner_instructions ${OBJECT.d}/scanner_instructions.pp source/eaxhla.l + +${OBJECT.d}/%.pluglock: ${OBJECT.d}/%.pp + ${PLUG} -g -e $* $< ${PLUGFILES} + touch $@ + +test: ${OUT} + ORIGIN="$$(realpath .)" PATH="$$(realpath .):${PATH}" cmdtest + ${WRAP} ./${OUT} test/nop.eax bootstrap: ./library/bootstrap.sh -test: ${OUT} - #${WRAP} ./${OUT} debug/xop.eax - ORIGIN="$$(realpath .)" PATH="$$(realpath .):${PATH}" cmdtest - clean: unplug -rm ${OUT} ${OBJECT} ${GENOBJECT} ${GENSOURCE} -${OBJECT.d}/%.pp: debug/%.tcl debug/instructions.tcl - tclsh $< > $@ - -unplug: - ${PLUG} -u -d token_list '' -d scanner_instructions '' -d parser_rules '' source/eaxhla.l source/eaxhla.y - -plug: ${OBJECT.d}/token_list.pp ${OBJECT.d}/scanner_instructions.pp ${OBJECT.d}/parser_rules.pp - ${PLUG} -g -e token_list ${OBJECT.d}/token_list.pp source/eaxhla.y - ${PLUG} -g -e scanner_instructions ${OBJECT.d}/scanner_instructions.pp source/eaxhla.l - ${PLUG} -g -e parser_rules ${OBJECT.d}/parser_rules.pp source/eaxhla.y - - -.PHONY: test clean bootstrap +${OUT}: ${PLUGLOCK} ${GENSOURCE} ${GENOBJECT} ${OBJECT} ${LIB} + ${LINK.c} -o $@ ${OBJECT} ${GENOBJECT} ${LDLIBS} library/tommyds/tommy.o diff --git a/source/eaxhla.h b/source/eaxhla.h index 8e74483..c93daba 100644 --- a/source/eaxhla.h +++ b/source/eaxhla.h @@ -1,6 +1,6 @@ #ifndef EAXHLA_H -#include +#include #define WORD_SIZE_IN_BYTES (64/8) diff --git a/source/eaxhla.l b/source/eaxhla.l index e7111c0..3be0d9a 100644 --- a/source/eaxhla.l +++ b/source/eaxhla.l @@ -5,6 +5,13 @@ char * string_literal_buffer; int string_litral_buffer_size = 0; int string_litral_buffer_capacity = 128; + void string_literal_ensure_surplus(int s) { + if (string_litral_buffer_size + s > string_litral_buffer_capacity) { + string_litral_buffer_capacity *= 2; + void * ignore = realloc(string_literal_buffer, string_litral_buffer_capacity); + (void)ignore; + } + } #define YY_USER_INIT \ string_literal_buffer = malloc(128); @@ -37,8 +44,10 @@ repeat { return REPEAT; } if { return IF; } then { return THEN; } machine { return MACHINE; } +library { return LIBRARY; } break { return BREAK; } +UNTIL { return UNTIL; } exit { return EXIT; } end { BEGIN IN_END; } @@ -157,6 +166,7 @@ procedure { BEGIN INITIAL; free(scope); scope = NULL; return END_PROCEDURE; repeat { BEGIN INITIAL; return END_REPEAT; } if { BEGIN INITIAL; return END_IF; } machine { BEGIN INITIAL; return END_MACHINE; } +library { BEGIN INITIAL; return END_LIBRARY; } {wsnl} { ; } . { yyless(0); BEGIN IN_UNKNOWN_END; } } @@ -167,22 +177,22 @@ machine { BEGIN INITIAL; return END_MACHINE; } { /* XXX: escapes; multiline strings will die */ + /* XXX: the first WORD_SIZE_IN_BYTES bytes should be 0'd */ \" { BEGIN INITIAL; - if (yyleng <= WORD_SIZE_IN_BYTES) { + if (string_litral_buffer_size <= WORD_SIZE_IN_BYTES) { memcpy(&yylval.intval, yytext, WORD_SIZE_IN_BYTES); return LITERAL; } + string_literal_ensure_surplus(1); + string_literal_buffer[string_litral_buffer_size+1] = '\0'; yylval.strval = strdup(string_literal_buffer); + string_litral_buffer_size = 0; return ARRAY_LITERAL; } . { - if (string_litral_buffer_size + yyleng > string_litral_buffer_capacity) { - string_litral_buffer_capacity *= 2; - void * ignore = realloc(string_literal_buffer, string_litral_buffer_capacity); - (void)ignore; - } + string_literal_ensure_surplus(yyleng); memcpy(string_literal_buffer + string_litral_buffer_size, yytext, yyleng); string_litral_buffer_size += yyleng; } diff --git a/source/eaxhla.y b/source/eaxhla.y index 8ec8559..0631be0 100644 --- a/source/eaxhla.y +++ b/source/eaxhla.y @@ -37,6 +37,9 @@ %token REPEAT END_REPEAT %token IF THEN ELSE END_IF %token MACHINE END_MACHINE +%token LIBRARY END_LIBRARY + +%token UNTIL %token IDENTIFIER LABEL @@ -90,11 +93,13 @@ %% hla: %empty // { issue_warning("empty file, noting to be done."); } + // | library hla + | declaration hla // tmp | program hla | function hla ; -program: program_head declaration_section MYBEGIN code END_PROGRAM +program: program_head declaration_section MYBEGIN code END_PROGRAM { scope = NULL; } ; program_head: program_specifier PROGRAM IDENTIFIER { @@ -114,7 +119,8 @@ system_specifier: UNIX { system_type = UNIX; } | WIN64 { system_type = WIN64; } ; -function: function_head declaration_section MYBEGIN code END_PROCEDURE + // XXX: end procedure thing +function: function_head declaration_section MYBEGIN code END_PROCEDURE { scope = NULL; } ; function_head: function_specifier PROCEDURE IDENTIFIER { scope = $3; } @@ -194,11 +200,11 @@ value: artimetric_block ; code: %empty - | error code { yyerrok; } + | error code { /*yyerrok;*/ } | repeat code | if code | call code - | LABEL code { free($1); } + | LABEL code { /* XXX */ free($1); } | machine code | BREAK code | exit code @@ -206,6 +212,7 @@ code: %empty ; repeat: REPEAT code END_REPEAT + | UNTIL logic REPEAT code END_REPEAT ; if: IF logic THEN code END_IF @@ -224,6 +231,7 @@ logic: logical_operand ITAND logical_operand logical_operand: sublogic | register | artimetric_block + | dereference | LITERAL | IDENTIFIER ; @@ -321,6 +329,13 @@ artimetric_operand: LITERAL exit: EXIT value ; +library: LIBRARY declaration_section MYBEGIN library_code END_LIBRARY + ; + +library_code: %empty + | function library_code + ; + instruction: INOP { append_instruction_t1(NOP); } /* | ISYSCALL { append_instruction_t1 (SYSCALL); } diff --git a/tool/instruction_generator/parser_rules.tcl b/tool/instruction_generator/parser_rules.tcl index c96c575..d529efd 100644 --- a/tool/instruction_generator/parser_rules.tcl +++ b/tool/instruction_generator/parser_rules.tcl @@ -1,4 +1,4 @@ -source debug/instructions.tcl +source tool/instruction_generator/instructions.tcl proc make_parser_rules {is} { proc init_iarg {a n} { diff --git a/tool/instruction_generator/scanner_instructions.tcl b/tool/instruction_generator/scanner_instructions.tcl index 7a1d438..ddc65ff 100644 --- a/tool/instruction_generator/scanner_instructions.tcl +++ b/tool/instruction_generator/scanner_instructions.tcl @@ -1,4 +1,4 @@ -source debug/instructions.tcl +source tool/instruction_generator/instructions.tcl proc make_scanner_instructions {is} { proc make_scanner_instruction {i} { diff --git a/tool/instruction_generator/token_list.tcl b/tool/instruction_generator/token_list.tcl index d26df35..8a15eb1 100644 --- a/tool/instruction_generator/token_list.tcl +++ b/tool/instruction_generator/token_list.tcl @@ -1,4 +1,4 @@ -source debug/instructions.tcl +source tool/instruction_generator/instructions.tcl proc make_token_list {is} { proc uniq_instl {is} {