From cfc723250f05efa6b6b2f87572f725e94a5a1853 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 27 Nov 2016 11:58:59 +0100 Subject: [PATCH] Initial EM-ification; start threading word size stuff through the code. --- build.lua | 1 + lang/b/{distr => }/LICENSE | 0 lang/b/{distr => }/README.md | 0 lang/b/{distr => compiler}/b.h | 2 + lang/b/{distr => compiler}/b0.c | 68 ++++++++++++++++++++------------- lang/b/{distr => compiler}/b1.c | 0 lang/b/compiler/build.lua | 21 ++++++++++ 7 files changed, 65 insertions(+), 27 deletions(-) rename lang/b/{distr => }/LICENSE (100%) rename lang/b/{distr => }/README.md (100%) rename lang/b/{distr => compiler}/b.h (98%) rename lang/b/{distr => compiler}/b0.c (96%) rename lang/b/{distr => compiler}/b1.c (100%) create mode 100644 lang/b/compiler/build.lua diff --git a/build.lua b/build.lua index 607a83a3b..dc9c0e03d 100644 --- a/build.lua +++ b/build.lua @@ -36,6 +36,7 @@ installable { "lang/cem/cemcom.ansi+pkg", "lang/m2/comp+pkg", "lang/pc/comp+pkg", + "lang/b/compiler+pkg", "util/ack+pkg", "util/amisc+pkg", "util/arch+pkg", diff --git a/lang/b/distr/LICENSE b/lang/b/LICENSE similarity index 100% rename from lang/b/distr/LICENSE rename to lang/b/LICENSE diff --git a/lang/b/distr/README.md b/lang/b/README.md similarity index 100% rename from lang/b/distr/README.md rename to lang/b/README.md diff --git a/lang/b/distr/b.h b/lang/b/compiler/b.h similarity index 98% rename from lang/b/distr/b.h rename to lang/b/compiler/b.h index 9aae94601..9e108325f 100644 --- a/lang/b/distr/b.h +++ b/lang/b/compiler/b.h @@ -1,6 +1,7 @@ #include #include #include +#include #define NCPS 8 /* chars per symbol */ #define NCPW 4 /* chars per word */ @@ -38,6 +39,7 @@ struct swtab { int swval; }; +extern int wordsize; struct hshtab hshtab[HSHSIZ]; int hshused; int eof; diff --git a/lang/b/distr/b0.c b/lang/b/compiler/b0.c similarity index 96% rename from lang/b/distr/b0.c rename to lang/b/compiler/b0.c index 424e1afdb..25aa59c6b 100644 --- a/lang/b/distr/b0.c +++ b/lang/b/compiler/b0.c @@ -14,6 +14,8 @@ int peeksym = -1, peeksym2 = -1;; int contlab = -1; int brklab = -1; +int wordsize = 4; + void init(char *s, int val) { @@ -32,24 +34,37 @@ init(char *s, int val) int main(int argc, char *argv[]) { - if (argc < 3) { - error("Arg count"); - exit(1); - } - if (freopen(argv[1], "r", stdin) == NULL) { - error("Can't find %s", argv[1]); - exit(1); - } - if ((sbufp=fopen(argv[2], "w")) == NULL) { - error("Can't create %s", argv[2]); - exit(1); - } - if (argc > 3) { - if (freopen(argv[3], "w", stdout) == NULL) { - error("Can't create %s", argv[2]); - exit(1); + + for (;;) { + int opt = getopt(argc, argv, "-w:i:o:"); + if (opt == -1) + break; + + switch (opt) { + case 'w': + wordsize = atoi(optarg); + break; + + case 'i': + if (freopen(optarg, "r", stdin) == NULL) { + error("Can't find %s", optarg); + exit(1); + } + break; + + case 'o': + if (freopen(optarg, "w", stdout) == NULL) { + error("Can't create %s", optarg); + exit(1); + } + break; + + derfault: + error("Usage: em_b [-w wordsize] [-i inputfile] [-o outputfile]"); + exit(1); } } + init("auto", AUTO); init("extrn", EXTERN); init("case", CASE); @@ -61,7 +76,6 @@ main(int argc, char *argv[]) init("return", RETURN); init("default", DEFAULT); init("break", BREAK); - fprintf(sbufp, "\t.data\n"); while (!eof) { extdef(); blkend(); @@ -191,16 +205,16 @@ getstr(void) int c; int i; - fprintf(sbufp, "\t.align 4\n"); - fprintf(sbufp, "L%d:", cval = isn++); + printf("\t.align %d\n", wordsize); + printf("L%d:", cval = isn++); if ((c = mapch('"')) >= 0) - fprintf(sbufp, "\t.byte %04o", c); + printf("\t.byte %04o", c); for (i = 2; (c = mapch('"')) >= 0; i++) - fprintf(sbufp, ",%04o", c); - fprintf(sbufp, ",04"); + printf(",%04o", c); + printf(",04"); while ((i++%4) != 0) - fprintf(sbufp, ",00"); - fprintf(sbufp, "\n"); + printf(",00"); + printf("\n"); return STRING; } @@ -525,7 +539,7 @@ extdef(void) global(bs); bsymb(bs,1); printf("_%s:\t.long 1f\n", bs); - printf("\t.text\n\t.align 4\n1:"); + printf("\t.text\n\t.align %s\n1:", wordsize); function(); done: printf("\n"); @@ -568,8 +582,8 @@ blkhed(void) struct hshtab *bs; declist(); - stack = al = -4; - pl = 8; + stack = al = -wordsize; + pl = wordsize*2; while (paraml) { paraml = (bs = paraml)->next; bs->offset = pl; diff --git a/lang/b/distr/b1.c b/lang/b/compiler/b1.c similarity index 100% rename from lang/b/distr/b1.c rename to lang/b/compiler/b1.c diff --git a/lang/b/compiler/build.lua b/lang/b/compiler/build.lua new file mode 100644 index 000000000..76fac395f --- /dev/null +++ b/lang/b/compiler/build.lua @@ -0,0 +1,21 @@ + +cprogram { + name = "em_b", + srcs = { + "./*.c", + }, + deps = { + "modules+headers", + "modules/src/em_code+lib_k", + "modules/src/em_data+lib", + "modules/src/em_mes+lib", + } +} + +installable { + name = "pkg", + map = { + ["$(PLATDEP)/em_b"] = "+em_b" + } +} +