From a5d2ef4f04c887d92a3f1e3c7bac64d5b145fa9f Mon Sep 17 00:00:00 2001 From: Krzysztof Szewczyk Date: Fri, 15 May 2020 12:55:38 +0200 Subject: [PATCH 1/7] another preprocessor (oh no) --- .gitignore | 3 ++- Makefile | 12 ++++++--- constpp.lex | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 constpp.lex diff --git a/.gitignore b/.gitignore index 7fd74b22..85a60ec3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ bin bfdata.c bflabels.c +constpp.c *.o bfpp/bfpp -*.pyc +*.pyc \ No newline at end of file diff --git a/Makefile b/Makefile index 283c7a06..a38db10f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # Krzysztof Szewczyk, Jul 2019 export CFLAGS=-Ofast -march=native -funroll-loops -fomit-frame-pointer -w $(COVERAGE) $(OPTIONS) -TARGETS=bfasm bfi bfintd bconv bfstrip bfderle bflabels bfdata +TARGETS=bfasm bfi bfintd bconv bfstrip bfderle bflabels constpp bfdata .PHONY: all clean setup test bfpp @@ -50,13 +50,19 @@ test-clean: rm -f test/*.b bflabels: bflabels.c - $(CC) $(CFLAGS) $^ -lfl -o $@ + $(CC) $(CFLAGS) $^ -o $@ bflabels.c: bflabels.lex lex -o $@ $^ +constpp: constpp.c + $(CC) $(CFLAGS) $^ -o $@ + +constpp.c: constpp.lex + lex -o $@ $^ + bfdata: bfdata.c - $(CC) $(CFLAGS) $^ -lfl -o $@ + $(CC) $(CFLAGS) $^ -o $@ bfdata.c: bfdata.lex lex -o $@ $^ diff --git a/constpp.lex b/constpp.lex new file mode 100644 index 00000000..8dbcee69 --- /dev/null +++ b/constpp.lex @@ -0,0 +1,71 @@ + +%{ +#include +#include +#include +#include +#include + +struct def_t { + char * find, * replace; + struct def_t * next; +}; + +struct def_t * ctx, * ctx_ptr; + +struct def_t * new_def(void) { + struct def_t * ret = malloc(sizeof(struct def_t)); + assert(ret); + return ret; +} + +void pop_def(char * text) { + struct def_t * p = ctx; + while(p->next && p->find && p->replace) { + if(!strcmp(text, p->find)) { + printf("%s", p->replace); + return; + } + + p = p->next; + } + printf("%s", text); +} + +void push_def(char * text) { + /* Skip the whitespace. */ + while(isspace((unsigned char)*text)) + text++; + + /* Skip the questionmark */ + text++; + + char * find = text, * replace = strchr(text, '=') + 1; + unsigned find_len = replace - find - 1, replace_len = strlen(replace); + + ctx_ptr->find = malloc(find_len); + memcpy(ctx_ptr->find, find, find_len); + + ctx_ptr->replace = malloc(replace_len); + memcpy(ctx_ptr->replace, replace, replace_len); + + ctx_ptr->next = new_def(); + ctx_ptr->next->find = ctx_ptr->next->replace = NULL; + ctx_ptr = ctx_ptr->next; +} + +int yywrap(void) { return 1; } + +int main(void) { + ctx = ctx_ptr = new_def(); + + yylex(); +} + +%} + +%% +^[ \t]*\?([A-Za-z_][A-Za-z0-9_]*)\=([A-Za-z_][A-Za-z0-9_]*) { push_def(yytext); } +(([A-Za-z_][A-Za-z0-9_]*)|"[^"\n]*([A-Za-z_][A-Za-z0-9_]*)) { pop_def(yytext); } +. { putchar(yytext[0]); } +%% From 9636f8873d515596620fc194eed80de3a983f1e0 Mon Sep 17 00:00:00 2001 From: KrzysztofSzewczyk Date: Fri, 15 May 2020 14:20:30 +0200 Subject: [PATCH 2/7] plug in the constpp --- bfmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bfmake b/bfmake index c315ae65..1f611ce7 100644 --- a/bfmake +++ b/bfmake @@ -49,4 +49,4 @@ fi # A special case: no file supplied [ "$file" == "" ] && exit 2 -{ ( [ -z "$no_libbfm" ] && echo "" || cat "$HOME/.asmbf/lib/lib-bfm.lua" ) ; cat "$file" | sed -e "1!b" -e '/#/d' ; } | "$HOME/.asmbf/bfpp" | "$HOME/.asmbf/bflabels" | "$HOME/.asmbf/bfdata" | sed '/^#/ d' | "$HOME/.asmbf/bfasm" | ( [ -z "$no_strip" ] && cat || "$HOME/.asmbf/bfstrip" ) > "${output}" +{ ( [ -z "$no_libbfm" ] && echo "" || cat "$HOME/.asmbf/lib/lib-bfm.lua" ) ; cat "$file" | sed -e "1!b" -e '/#/d' ; } | "$HOME/.asmbf/bfpp" | "$HOME/.asmbf/bflabels" | "$HOME/.asmbf/bfdata" | sed '/^#/ d' | "$HOME/.asmbf/constpp" | "$HOME/.asmbf/bfasm" | ( [ -z "$no_strip" ] && cat || "$HOME/.asmbf/bfstrip" ) > "${output}" From c3b4b3c45e1ce32ed3e2b6c835e5695139c1f257 Mon Sep 17 00:00:00 2001 From: KrzysztofSzewczyk Date: Fri, 15 May 2020 18:03:34 +0200 Subject: [PATCH 3/7] Update lib-bfm.lua --- lib-bfm.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib-bfm.lua b/lib-bfm.lua index 3e741d28..fa9ffed8 100644 --- a/lib-bfm.lua +++ b/lib-bfm.lua @@ -1,4 +1,10 @@ +; Don't ask questions, this is beyond explaining +?band=000 +?bor=001 +?bxor=002 +?bneg=003 + $( function include(path) From 1fd1074ed72e948663f6728e566c3dc4b9477da8 Mon Sep 17 00:00:00 2001 From: Krzysztof Szewczyk Date: Fri, 15 May 2020 18:18:07 +0200 Subject: [PATCH 4/7] patch lib-bfm for id's to begin with a letter --- lib-bfm.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib-bfm.lua b/lib-bfm.lua index fa9ffed8..a0a921cf 100644 --- a/lib-bfm.lua +++ b/lib-bfm.lua @@ -1,9 +1,9 @@ ; Don't ask questions, this is beyond explaining -?band=000 -?bor=001 -?bxor=002 -?bneg=003 +?band=x00 +?bor=x01 +?bxor=x02 +?bneg=x03 $( From 1b1f19ee36c4ae28505233c53ca8fce68beb2b6d Mon Sep 17 00:00:00 2001 From: Krzysztof Szewczyk Date: Fri, 15 May 2020 18:26:18 +0200 Subject: [PATCH 5/7] New instruction names. Yay? --- lib-bfm.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib-bfm.lua b/lib-bfm.lua index a0a921cf..0f63bc87 100644 --- a/lib-bfm.lua +++ b/lib-bfm.lua @@ -5,6 +5,29 @@ ?bxor=x02 ?bneg=x03 +; Some common defines +?push=psh +?xchg=swp + +; Conditional instructions +?cadd=cad +?csub=csu +?cmul=cmu +?cdiv=cdi +?cmod=cmd +?casl=csl +?casr=csr +?cpow=cpw +?cpush=cps +?cpsh=cps +?cpop=cpo +?cxchg=csw +?cswp=csw +?csrv=crv +?cmov=cmo +?crcl=crc +?csto=cst + $( function include(path) From fc9850bd64b1bd5f5a63fb76d6c057aeda287cb4 Mon Sep 17 00:00:00 2001 From: KrzysztofSzewczyk Date: Fri, 15 May 2020 19:30:30 +0200 Subject: [PATCH 6/7] xchg test --- test/xchg.asm | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 test/xchg.asm diff --git a/test/xchg.asm b/test/xchg.asm new file mode 100644 index 00000000..4c41583d --- /dev/null +++ b/test/xchg.asm @@ -0,0 +1,6 @@ + +mov r1, .0 +mov r2, .1 +xchg r1, r2 +out r1 +out r2 From 65d657857d59ca9e28b24c7e770e5bfa4a8766de Mon Sep 17 00:00:00 2001 From: Krzysztof Szewczyk Date: Fri, 15 May 2020 19:30:24 +0200 Subject: [PATCH 7/7] in/out for xchg test --- test/xchg.in | 1 + test/xchg.out | 1 + 2 files changed, 2 insertions(+) create mode 100644 test/xchg.in create mode 100644 test/xchg.out diff --git a/test/xchg.in b/test/xchg.in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/xchg.in @@ -0,0 +1 @@ + diff --git a/test/xchg.out b/test/xchg.out new file mode 100644 index 00000000..9a037142 --- /dev/null +++ b/test/xchg.out @@ -0,0 +1 @@ +10 \ No newline at end of file