Make ref/asxlat version of reference assembler to translate into asxxxx dialect
authorNick Downing <nick@ndcode.org>
Sat, 15 Jun 2019 02:48:22 +0000 (12:48 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 15 Jun 2019 02:48:22 +0000 (12:48 +1000)
.gitignore
aspdp11/n.sh
aspdp11/ref/Makefile
aspdp11/ref/as0.c
aspdp11/test/crt0.asm

index 79f280a..52697f2 100644 (file)
@@ -4,6 +4,8 @@
 /aspdp11/aspdp11
 /aspdp11/b
 /aspdp11/ref/as
+/aspdp11/ref/aslist
+/aspdp11/ref/asxlat
 /aspdp11/test/*.lst
 /asxmak/linux/build/aslink
 /asxmak/linux/build/aspdp11
index a3e593d..267537f 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 make -C ref
-ref/as test/crt0.s >a
+ref/aslist test/crt0.s >a
 make -C ../asxmak/linux/build aspdp11 aslink
 cp ../asxmak/linux/build/aspdp11 .
 ./aspdp11 -l test/crt0.asm 2>b
index e7f7cfc..8aac95a 100644 (file)
@@ -7,17 +7,38 @@ INSTALL=install
 #AS=   /bin/as
 #SEPFLAG= -i -X
 #SEPFLAG=-i
-CFLAGS=-m32 -g -DLISTING
+CFLAGS=-m32 -g
 
-all: as
+all: as aslist asxlat
 
 as: as0.o as2.o
 #      ${LD} ${SEPFLAG} -o $@ ${STAGEDIR}/lib/crt0.o as0.o as2.o -lc
 #      ${CC} ${SEPFLAG} -o $@ as0.o as2.o
        ${CC} ${CFLAGS} -o $@ as0.o as2.o
 
+as0.o: as0.c as0.h as2.h
+as2.o: as2.c as0.h as2.h
+
+aslist: aslist0.o aslist2.o
+       ${CC} ${CFLAGS} -o $@ aslist0.o aslist2.o
+
+aslist0.o: as0.c as0.h as2.h
+       ${CC} ${CFLAGS} -DLISTING -o $@ -c $<
+
+aslist2.o: as2.c as0.h as2.h
+       ${CC} ${CFLAGS} -DLISTING -o $@ -c $<
+
+asxlat: asxlat0.o asxlat2.o
+       ${CC} ${CFLAGS} -o $@ asxlat0.o asxlat2.o
+
+asxlat0.o: as0.c as0.h as2.h
+       ${CC} ${CFLAGS} -DTRANSLATE -o $@ -c $<
+
+asxlat2.o: as2.c as0.h as2.h
+       ${CC} ${CFLAGS} -DTRANSLATE -o $@ -c $<
+
 install: all
-       ${INSTALL} -s as ${DESTDIR}/bin
+       ${INSTALL} -s as aslist asxlat ${DESTDIR}/bin
 
 clean:
-       rm -f *.o a.out as core errs
+       rm -f *.o a.out as aslist asxlat core errs
index 9bfb4e4..551065e 100644 (file)
@@ -65,6 +65,19 @@ int leftvalue; /* r2 */
 int leftflags; /* r3 */
 intptr_t token; /* r4 */
 
+#ifdef TRANSLATE
+static char xbuf[0x1000];
+static char *xbufp = xbuf, *xwhitep, *xstartp;
+static char *xstartpsave, *xbufpsave;
+static size_t xsavestartn, xsavebufn;
+static char xsave[0x1000];
+static size_t xrestn;
+static char xrest[0x1000];
+static int xlab, xlabnext;
+static int xlabb[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+static int xlabf[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+#endif
+
 /* PDP-11 assembler */
 /* _main: */
 /*     jsr     r5,csv */
@@ -323,6 +336,9 @@ int main(argc, argv) int argc; char **argv; {
        printf("pass 0\n");
 #endif
        pass0();
+#ifdef TRANSLATE
+ write(1, xbuf, xbufp - xbuf);
+#endif
 
 /* flush the intermediate object file */
 /*     mov     $1024.,-(sp)            / write(fout, outbuf, 1024) */
@@ -609,13 +625,26 @@ void pass0() {
 /*     br      ealoop */
 
                tokensave = token;
+#ifdef TRANSLATE
+ xstartpsave = xstartp;
+ xbufpsave = xbufp;
+#endif
                p0readop();
                if (token == TEQUAL)
                        goto equal;
                if (token == TCOLON)
                        goto colon;
                savop = token;
+#ifdef TRANSLATE
+ xsavestartn = xstartp - xbufpsave;
+ xsavebufn = xbufp - xbufpsave;
+ memcpy(xsave, xbufpsave, xsavebufn);
+#endif
                token = tokensave;
+#ifdef TRANSLATE
+ xstartp = xstartpsave;
+ xbufp = xbufpsave;
+#endif
                p0opline();
                goto ealoop;
 
@@ -671,6 +700,18 @@ void pass0() {
                label = fbcheck(numval);
                curfbr[label] = pdot->flags;
                curfb[label] = pdot->value;
+#ifdef TRANSLATE
+ if (xlabf[label] == -1)
+  xlab = xlabnext++;
+ else {
+  xlab = xlabf[label];
+  xlabf[label] = -1;
+ }
+ xlabb[label] = xlab;
+ if (xstartp >= xbuf + 0xff0)
+  abort();
+ xbufp = xstartpsave + sprintf(xstartpsave, "_%d:", xlab);
+#endif
 
 /*     cmp     fbfree,$4               / room for another fb entry? */
 /*     bge     5f                      / yes - br */
@@ -727,6 +768,15 @@ void pass0() {
 /* printf("leftflags 0%o pdot->flags 0%o\n", leftflags, pdot->flags); */
                        if (leftflags != pdot->flags)
                                goto doterr;
+#ifdef TRANSLATE
+ xrestn = xbufp - xwhitep;
+ memcpy(xrest, xwhitep, xrestn);
+ if (xstartpsave + xrestn >= xbuf + 0xff0)
+  abort();
+ xbufp = xstartpsave + sprintf(xstartpsave, ".ds\t%d", leftvalue);
+ memcpy(xbufp, xrest, xrestn);
+ xbufp += xrestn;
+#endif
                }
 
 /*     bicb    $37,(r1) */
@@ -944,6 +994,11 @@ void rname() {
        *p++ = 0;
 /* printf("symbol = \"%s\"\n", symbol); */
        ch = c;
+#ifdef TRANSLATE
+ if (xbufp <= xbuf)
+  abort();
+ --xbufp;
+#endif
 /* if (ch) printf("a push 0x%02x\n", ch); */
 
 /*     mov     (sp)+,r1 */
@@ -1260,6 +1315,11 @@ int number() {
 /*     mov     r1,r0 */
 
        ch = c;
+#ifdef TRANSLATE
+ if (xbufp <= xbuf)
+  abort();
+ --xbufp;
+#endif
 /* if (ch) printf("b push 0x%02x\n", ch); */
        numbervalue = octal;
 
@@ -1309,6 +1369,16 @@ int rch() {
        if (ch) {
                c = ch;
                ch = 0;
+#ifdef TRANSLATE
+ //if (xbufp > xbuf && xbufp[-1] == '\n') {
+ // write(1, xbuf, xbufp - xbuf);
+ // xbufp = xbuf;
+ //}
+ //else
+ if (xbufp >= xbuf + 0x1000)
+  abort();
+ *xbufp++ = c;
+#endif
                return c;
        }
 
@@ -1329,6 +1399,16 @@ int rch() {
 /* putchar(c); */
                if (c == 0)
                        continue;
+#ifdef TRANSLATE
+ //if (xbufp > xbuf && xbufp[-1] == '\n') {
+ // write(1, xbuf, xbufp - xbuf);
+ // xbufp = xbuf;
+ //}
+ //else
+ if (xbufp >= xbuf + 0x1000)
+  abort();
+ *xbufp++ = c;
+#endif
                return c;
 
 /* 2: */
@@ -1494,9 +1574,19 @@ void p0readop() {
        int c;
 
 /* printf("p0readop() savop = 0x%08x\n", savop); */
+#ifdef TRANSLATE
+ xwhitep = xbufp;
+#endif
        if (savop) {
                token = savop;
                savop = 0;
+#ifdef TRANSLATE
+ if (xbufp + xsavebufn >= xbuf + 0x1000)
+  abort();
+ memcpy(xbufp, xsave, xsavebufn);
+ xstartp = xbufp + xsavestartn;
+ xbufp = xbufp + xsavebufn;
+#endif
                return;
        }
 
@@ -1508,6 +1598,9 @@ void p0readop() {
 /*     jmp     *1f-2(r1) */
 
        while (1) {
+#ifdef TRANSLATE
+ xstartp = xbufp;
+#endif
                c = rch();
                switch (chartab[c]) {
                default:
@@ -1573,6 +1666,9 @@ void p0readop() {
 /*     skip */
 
                case -006:
+#ifdef TRANSLATE
+ xbufp[-1] = ';';
+#endif
                        skip();
                        return;
 
@@ -1673,6 +1769,11 @@ void retread(c) int c; {
 
 void rdname(c) int c; {
        ch = c;
+#ifdef TRANSLATE
+ if (xbufp <= xbuf)
+  abort();
+ --xbufp;
+#endif
 /* if (ch) printf("c push 0x%02x\n", ch); */
        if (c >= '0' && c <= '9')
                rdnum();
@@ -1920,6 +2021,12 @@ void p0opline() {
        int flags;
 
 /* printf("p0opline() token 0x%08x\n", token); */
+#if 0 //def TRANSLATE
+ *xbufp = 0;
+ printf("p0opline() token 0x%08x xstartp \"%s\"\n", token, xstartp);
+ fflush(stdout);
+#endif
+
        if (token >= 0 && token < TASCII) {
                if (token == TSTRING)
                        p0opl17();
@@ -1942,6 +2049,22 @@ void p0opline() {
                p0xpr();
                return;
        }
+#ifdef TRANSLATE
+ if (xstartp >= xbuf + 0xff0)
+  abort();
+ switch (flags) {
+ case FDOTTEXT:
+  xbufp = xstartp + sprintf(xstartp, ".area TEXT");
+  break;
+ case FDOTDATA:
+  xbufp = xstartp + sprintf(xstartp, ".area DATA");
+  break;
+ case FDOTBSS:
+  xbufp = xstartp + sprintf(xstartp, ".area BSS");
+  break;
+ }
+#endif
+
 
 /*     mov     r0,-(sp) */
 /*     jsr     pc,p0readop */
@@ -2410,6 +2533,9 @@ void p0addres() {
                p0amin();
                return;
        case TDOLL:
+#ifdef TRANSLATE
+ xbufp[-1] = '#';
+#endif
                p0adoll();
                return;
        case TSTAR:
@@ -2497,10 +2623,23 @@ void p0alp() {
 /*     rts     pc */
 
 void p0amin() {
+#ifdef TRANSLATE
+ xstartpsave = xstartp;
+ xbufpsave = xbufp;
+#endif
        p0readop();
        if (token != TLPAREN) {
                savop = token;
+#ifdef TRANSLATE
+ xsavestartn = xstartp - xbufpsave;
+ xsavebufn = xbufp - xbufpsave;
+ memcpy(xsave, xbufpsave, xsavebufn);
+#endif
                token = TMIN;
+#ifdef TRANSLATE
+ xstartp = xstartpsave;
+ xbufp = xbufpsave;
+#endif
                p0getx();
        }
        else {
@@ -2661,6 +2800,11 @@ void p0expres() {
 
                if (token >= TLABEL) {
                        if (token < TLABEL + 10) {
+#ifdef TRANSLATE
+ if (xstartp >= xbuf + 0xff0)
+  abort();
+ xbufp = xstartp + sprintf(xstartp, "_%d", xlabb[token - TLABEL]);
+#endif
                                rightflags = curfbr[token - TLABEL];
  /* bug!! should be rightvalue / rightflags below: */
                                leftvalue = curfb[token - TLABEL];
@@ -2668,6 +2812,16 @@ void p0expres() {
                                        error('f');
                        }
                        else {
+#ifdef TRANSLATE
+ xlab = token - (TLABEL + 10);
+ if (xlab < 10) {
+  if (xlabf[xlab] == -1)
+   xlabf[xlab] = xlabnext++;
+  if (xstartp >= xbuf + 0xff0)
+   abort();
+  xbufp = xstartp + sprintf(xstartp, "_%d", xlabf[xlab]);
+ }
+#endif
                                leftflags = 0;
                                leftvalue = 0;
                        }
index 0717e1d..2e3ddc9 100644 (file)
@@ -14,13 +14,13 @@ start:
        mov     4(sp),(sp)
        tst     (r0)+
        mov     r0,2(sp)
-1$: ;1:
+_0:
        tst     (r0)+
-       bne     1$ ;1b
+       bne     _0
        cmp     r0,*2(sp)
-       blo     2$ ;1f
+       blo     _1
        tst     -(r0)
-2$: ;1:
+_1:
        mov     r0,4(sp)
        mov     r0,_environ
        jsr     pc,_main
@@ -29,8 +29,8 @@ start:
        jsr     pc,*#_exit
        sys     exit
 
-.area BSS ;.bss
+.area BSS
 _environ:
-       .ds 2 ;.=.+2
-.area DATA ;.data
-       .ds 2 ;.=.+2            / loc 0 for I/D; null ptr points here.
+       .ds     2
+.area DATA
+       .ds     2               ; loc 0 for I/D; null ptr points here.