Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 653f42da authored by Jack Palevich's avatar Jack Palevich
Browse files

Pointer-ize the acc front end.

The ACC compiler used to be able to compile itself. This was a neat
feature, but because ACC only supports ints, pointers are stored as
ints, and cast to pointers when used.

This checkin turns many ints that are really pointers back into
pointers, so that the code is clearer.

 int ch;
 char* glo;
 char* sym_stack;
 char* dstk;
 char* dptr;
 int dch;
 char* last_id;
parent 730c8d7c
Loading
Loading
Loading
Loading
+47 −46
Original line number Original line Diff line number Diff line
@@ -47,7 +47,7 @@ namespace acc {


class Compiler {
class Compiler {
    class CodeBuf {
    class CodeBuf {
        char* ind;
        char* ind; // Output code pointer
        char* pProgramBase;
        char* pProgramBase;


        void release() {
        void release() {
@@ -1027,20 +1027,21 @@ class Compiler {
        size_t mPosition;
        size_t mPosition;
    };
    };


    /* vars: value of variables
    int ch; // Current input character, or EOF
     loc : local variable index
    intptr_t tok;     // token
     glo : global variable index
    intptr_t tokc;    // token extra info
     ind : output code ptr
    int tokl;         // token operator level
     rsym: return symbol
    intptr_t rsym; // return symbol
     prog: output code
    intptr_t loc; // local variable index
     dstk: define stack
    char* glo;  // global variable index
     dptr, dch: macro state
    char* sym_stk;
     */
    char* dstk; // Define stack
    intptr_t tok, tokc, tokl, ch, vars, rsym, loc, glo, sym_stk, dstk,
    char* dptr; // Macro state: Points to macro text during macro playback.
            dptr, dch, last_id;
    int dch;    // Macro state: Saves old value of ch during a macro playback.
    char* last_id;
    void* pSymbolBase;
    void* pSymbolBase;
    void* pGlobalBase;
    void* pGlobalBase;
    void* pVarsBase;
    char* pVarsBase; // Value of variables


    InputStream* file;
    InputStream* file;


@@ -1106,12 +1107,12 @@ class Compiler {
    static const char operatorLevel[];
    static const char operatorLevel[];


    void pdef(int t) {
    void pdef(int t) {
        *(char *) dstk++ = t;
        *dstk++ = t;
    }
    }


    void inp() {
    void inp() {
        if (dptr) {
        if (dptr) {
            ch = *(char *) dptr++;
            ch = *dptr++;
            if (ch == TAG_MACRO) {
            if (ch == TAG_MACRO) {
                dptr = 0;
                dptr = 0;
                ch = dch;
                ch = dch;
@@ -1145,7 +1146,7 @@ class Compiler {
                    next();
                    next();
                    pdef(TAG_TOK); /* fill last ident tag */
                    pdef(TAG_TOK); /* fill last ident tag */
                    *(int *) tok = SYM_DEFINE;
                    *(int *) tok = SYM_DEFINE;
                    *(int *) (tok + 4) = dstk; /* define stack */
                    *(char* *) (tok + 4) = dstk; /* define stack */
                }
                }
                /* well we always save the values ! */
                /* well we always save the values ! */
                while (ch != '\n') {
                while (ch != '\n') {
@@ -1168,21 +1169,21 @@ class Compiler {
                inp();
                inp();
            }
            }
            if (isdigit(tok)) {
            if (isdigit(tok)) {
                tokc = strtol((char*) last_id, 0, 0);
                tokc = strtol(last_id, 0, 0);
                tok = TOK_NUM;
                tok = TOK_NUM;
            } else {
            } else {
                *(char *) dstk = TAG_TOK; /* no need to mark end of string (we
                * dstk = TAG_TOK; /* no need to mark end of string (we
                 suppose data is initialized to zero by calloc) */
                 suppose data is initialized to zero by calloc) */
                tok = (intptr_t) (strstr((char*) sym_stk, (char*) (last_id - 1))
                tok = (intptr_t) (strstr(sym_stk, (last_id - 1))
                        - sym_stk);
                        - sym_stk);
                *(char *) dstk = 0; /* mark real end of ident for dlsym() */
                * dstk = 0; /* mark real end of ident for dlsym() */
                tok = tok * 8 + TOK_IDENT;
                tok = tok * 8 + TOK_IDENT;
                if (tok > TOK_DEFINE) {
                if (tok > TOK_DEFINE) {
                    tok = vars + tok;
                    tok = (intptr_t) (pVarsBase + tok);
                    /*        printf("tok=%s %x\n", last_id, tok); */
                    /*        printf("tok=%s %x\n", last_id, tok); */
                    /* define handling */
                    /* define handling */
                    if (*(int *) tok == SYM_DEFINE) {
                    if (*(int *) tok == SYM_DEFINE) {
                        dptr = *(int *) (tok + 4);
                        dptr = *(char* *) (tok + 4);
                        dch = ch;
                        dch = ch;
                        inp();
                        inp();
                        next();
                        next();
@@ -1243,17 +1244,17 @@ class Compiler {
        }
        }
#if 0
#if 0
        {
        {
            int p;
            char* p;


            printf("tok=0x%x ", tok);
            printf("tok=0x%x ", tok);
            if (tok >= TOK_IDENT) {
            if (tok >= TOK_IDENT) {
                printf("'");
                printf("'");
                if (tok> TOK_DEFINE)
                if (tok> TOK_DEFINE)
                p = sym_stk + 1 + (tok - vars - TOK_IDENT) / 8;
                p = sym_stk + 1 + ((char*) tok - pVarsBase - TOK_IDENT) / 8;
                else
                else
                p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
                p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
                while (*(char *)p != TAG_TOK && *(char *)p)
                while (*p != TAG_TOK && *p)
                printf("%c", *(char *)p++);
                printf("%c", *p++);
                printf("'\n");
                printf("'\n");
            } else if (tok == TOK_NUM) {
            } else if (tok == TOK_NUM) {
                printf("%d\n", tokc);
                printf("%d\n", tokc);
@@ -1284,19 +1285,20 @@ class Compiler {


    /* l is one if '=' parsing wanted (quick hack) */
    /* l is one if '=' parsing wanted (quick hack) */
    void unary(intptr_t l) {
    void unary(intptr_t l) {
        intptr_t n, t, a, c;
        intptr_t n, t, a;
        int c;
        t = 0;
        t = 0;
        n = 1; /* type of expression 0 = forward, 1 = value, other =
        n = 1; /* type of expression 0 = forward, 1 = value, other =
         lvalue */
         lvalue */
        if (tok == '\"') {
        if (tok == '\"') {
            pGen->li(glo);
            pGen->li((int) glo);
            while (ch != '\"') {
            while (ch != '\"') {
                getq();
                getq();
                *(char *) glo++ = ch;
                *glo++ = ch;
                inp();
                inp();
            }
            }
            *(char *) glo = 0;
            *glo = 0;
            glo = (glo + 4) & -4; /* align heap */
            glo = (char*) (((intptr_t) glo + 4) & -4); /* align heap */
            inp();
            inp();
            next();
            next();
        } else {
        } else {
@@ -1349,7 +1351,7 @@ class Compiler {
                n = *(int *) t;
                n = *(int *) t;
                /* forward reference: try dlsym */
                /* forward reference: try dlsym */
                if (!n) {
                if (!n) {
                    n = (intptr_t) dlsym(RTLD_DEFAULT, (char*) last_id);
                    n = (intptr_t) dlsym(RTLD_DEFAULT, last_id);
                }
                }
                if ((tok == '=') & l) {
                if ((tok == '=') & l) {
                    /* assignment */
                    /* assignment */
@@ -1398,7 +1400,7 @@ class Compiler {
        }
        }
    }
    }


    void sum(intptr_t l) {
    void sum(int l) {
        intptr_t t, n, a;
        intptr_t t, n, a;
        t = 0;
        t = 0;
        if (l-- == 1)
        if (l-- == 1)
@@ -1518,7 +1520,7 @@ class Compiler {
    void decl(bool l) {
    void decl(bool l) {
        intptr_t a;
        intptr_t a;


        while ((tok == TOK_INT) | ((tok != -1) & (!l))) {
        while ((tok == TOK_INT) | ((tok != EOF) & (!l))) {
            if (tok == TOK_INT) {
            if (tok == TOK_INT) {
                next();
                next();
                while (tok != ';') {
                while (tok != ';') {
@@ -1526,7 +1528,7 @@ class Compiler {
                        loc = loc + 4;
                        loc = loc + 4;
                        *(int *) tok = -loc;
                        *(int *) tok = -loc;
                    } else {
                    } else {
                        *(int *) tok = glo;
                        *(int* *) tok = (int*) glo;
                        glo = glo + 4;
                        glo = glo + 4;
                    }
                    }
                    next();
                    next();
@@ -1565,7 +1567,7 @@ class Compiler {


    void cleanup() {
    void cleanup() {
        if (sym_stk != 0) {
        if (sym_stk != 0) {
            free((void*) sym_stk);
            free(sym_stk);
            sym_stk = 0;
            sym_stk = 0;
        }
        }
        if (pGlobalBase != 0) {
        if (pGlobalBase != 0) {
@@ -1591,7 +1593,7 @@ class Compiler {
        tokc = 0;
        tokc = 0;
        tokl = 0;
        tokl = 0;
        ch = 0;
        ch = 0;
        vars = 0;
        pVarsBase = 0;
        rsym = 0;
        rsym = 0;
        loc = 0;
        loc = 0;
        glo = 0;
        glo = 0;
@@ -1664,14 +1666,13 @@ public:
        }
        }
        pGen->init(&codeBuf);
        pGen->init(&codeBuf);
        file = new TextInputStream(text, textLength);
        file = new TextInputStream(text, textLength);
        sym_stk = (intptr_t) calloc(1, ALLOC_SIZE);
        sym_stk = (char*) calloc(1, ALLOC_SIZE);
        dstk = (intptr_t) strcpy((char*) sym_stk,
        dstk = strcpy(sym_stk,
                " int if else while break return for define main ")
                " int if else while break return for define main ")
                + TOK_STR_SIZE;
                + TOK_STR_SIZE;
        pGlobalBase = calloc(1, ALLOC_SIZE);
        pGlobalBase = calloc(1, ALLOC_SIZE);
        glo = (intptr_t) pGlobalBase;
        glo = (char*) pGlobalBase;
        pVarsBase = calloc(1, ALLOC_SIZE);
        pVarsBase = (char*) calloc(1, ALLOC_SIZE);
        vars = (intptr_t) pVarsBase;
        inp();
        inp();
        next();
        next();
        decl(0);
        decl(0);
@@ -1681,7 +1682,7 @@ public:


    int run(int argc, char** argv) {
    int run(int argc, char** argv) {
        typedef int (*mainPtr)(int argc, char** argv);
        typedef int (*mainPtr)(int argc, char** argv);
        mainPtr aMain = (mainPtr) *(int*) (vars + TOK_MAIN);
        mainPtr aMain = (mainPtr) *(int*) (pVarsBase + TOK_MAIN);
        if (!aMain) {
        if (!aMain) {
            fprintf(stderr, "Could not find function \"main\".\n");
            fprintf(stderr, "Could not find function \"main\".\n");
            return -1;
            return -1;
@@ -1706,7 +1707,7 @@ public:
            return NULL;
            return NULL;
        }
        }
        size_t nameLen = strlen(name);
        size_t nameLen = strlen(name);
        char* pSym = (char*) sym_stk;
        char* pSym = sym_stk;
        char c;
        char c;
        for(;;) {
        for(;;) {
            c = *pSym++;
            c = *pSym++;
@@ -1716,12 +1717,12 @@ public:
            if (c == TAG_TOK) {
            if (c == TAG_TOK) {
                if (memcmp(pSym, name, nameLen) == 0
                if (memcmp(pSym, name, nameLen) == 0
                        && pSym[nameLen] == TAG_TOK) {
                        && pSym[nameLen] == TAG_TOK) {
                    int tok = pSym - 1 - (char*) sym_stk;
                    int tok = pSym - 1 - sym_stk;
                    tok = tok * 8 + TOK_IDENT;
                    tok = tok * 8 + TOK_IDENT;
                    if (tok <= TOK_DEFINE) {
                    if (tok <= TOK_DEFINE) {
                        return 0;
                        return 0;
                    } else {
                    } else {
                        tok = vars + tok;
                        tok = (intptr_t) (pVarsBase + tok);
                        return * (void**) tok;
                        return * (void**) tok;
                    }
                    }
                }
                }