OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [lcc/] [src/] [bytecode.c] - Diff between revs 4 and 21

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 21
#include "c.h"
#include "c.h"
#define I(f) b_##f
#define I(f) b_##f
 
 
static char rcsid[] = "$Id: bytecode.c,v 1.1 2002/08/28 23:12:41 drh Exp $";
static char rcsid[] = "$Id: bytecode.c,v 1.1 2002/08/28 23:12:41 drh Exp $";
 
 
static void I(segment)(int n) {
static void I(segment)(int n) {
        static int cseg;
        static int cseg;
 
 
        if (cseg != n)
        if (cseg != n)
                switch (cseg = n) {
                switch (cseg = n) {
                case CODE: print("code\n"); return;
                case CODE: print("code\n"); return;
                case DATA: print("data\n"); return;
                case DATA: print("data\n"); return;
                case BSS:  print("bss\n");  return;
                case BSS:  print("bss\n");  return;
                case LIT:  print("lit\n");  return;
                case LIT:  print("lit\n");  return;
                default: assert(0);
                default: assert(0);
                }
                }
}
}
 
 
static void I(address)(Symbol q, Symbol p, long n) {
static void I(address)(Symbol q, Symbol p, long n) {
        q->x.name = stringf("%s%s%D", p->x.name, n > 0 ? "+" : "", n);
        q->x.name = stringf("%s%s%D", p->x.name, n > 0 ? "+" : "", n);
}
}
 
 
static void I(defaddress)(Symbol p) {
static void I(defaddress)(Symbol p) {
        print("address %s\n", p->x.name);
        print("address %s\n", p->x.name);
}
}
 
 
static void I(defconst)(int suffix, int size, Value v) {
static void I(defconst)(int suffix, int size, Value v) {
        switch (suffix) {
        switch (suffix) {
        case I:
        case I:
                if (size > sizeof (int))
                if (size > sizeof (int))
                        print("byte %d %D\n", size, v.i);
                        print("byte %d %D\n", size, v.i);
                else
                else
                        print("byte %d %d\n", size, v.i);
                        print("byte %d %d\n", size, v.i);
                return;
                return;
        case U:
        case U:
                if (size > sizeof (unsigned))
                if (size > sizeof (unsigned))
                        print("byte %d %U\n", size, v.u);
                        print("byte %d %U\n", size, v.u);
                else
                else
                        print("byte %d %u\n", size, v.u);
                        print("byte %d %u\n", size, v.u);
                return;
                return;
        case P: print("byte %d %U\n", size, (unsigned long)v.p); return;
        case P: print("byte %d %U\n", size, (unsigned long)v.p); return;
        case F:
        case F:
                if (size == 4) {
                if (size == 4) {
                        float f = v.d;
                        float f = v.d;
                        print("byte 4 %u\n", *(unsigned *)&f);
                        print("byte 4 %u\n", *(unsigned *)&f);
                } else {
                } else {
                        double d = v.d;
                        double d = v.d;
                        unsigned *p = (unsigned *)&d;
                        unsigned *p = (unsigned *)&d;
                        print("byte 4 %u\n", p[swap]);
                        print("byte 4 %u\n", p[swap]);
                        print("byte 4 %u\n", p[1 - swap]);
                        print("byte 4 %u\n", p[1 - swap]);
                }
                }
                return;
                return;
        }
        }
        assert(0);
        assert(0);
}
}
 
 
static void I(defstring)(int len, char *str) {
static void I(defstring)(int len, char *str) {
        char *s;
        char *s;
 
 
        for (s = str; s < str + len; s++)
        for (s = str; s < str + len; s++)
                print("byte 1 %d\n", (*s)&0377);
                print("byte 1 %d\n", (*s)&0377);
}
}
 
 
static void I(defsymbol)(Symbol p) {
static void I(defsymbol)(Symbol p) {
        if (p->scope == CONSTANTS)
        if (p->scope == CONSTANTS)
                switch (optype(ttob(p->type))) {
                switch (optype(ttob(p->type))) {
                case I: p->x.name = stringf("%D", p->u.c.v.i); break;
                case I: p->x.name = stringf("%D", p->u.c.v.i); break;
                case U: p->x.name = stringf("%U", p->u.c.v.u); break;
                case U: p->x.name = stringf("%U", p->u.c.v.u); break;
                case P: p->x.name = stringf("%U", p->u.c.v.p); break;
                case P: p->x.name = stringf("%U", p->u.c.v.p); break;
                default: assert(0);
                default: assert(0);
                }
                }
        else if (p->scope >= LOCAL && p->sclass == STATIC)
        else if (p->scope >= LOCAL && p->sclass == STATIC)
                p->x.name = stringf("$%d", genlabel(1));
                p->x.name = stringf("$%d", genlabel(1));
        else if (p->scope == LABELS || p->generated)
        else if (p->scope == LABELS || p->generated)
                p->x.name = stringf("$%s", p->name);
                p->x.name = stringf("$%s", p->name);
        else
        else
                p->x.name = p->name;
                p->x.name = p->name;
}
}
 
 
static void dumptree(Node p) {
static void dumptree(Node p) {
        switch (specific(p->op)) {
        switch (specific(p->op)) {
        case ASGN+B:
        case ASGN+B:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(p->kids[1]);
                assert(p->kids[1]);
                assert(p->syms[0]);
                assert(p->syms[0]);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                dumptree(p->kids[1]);
                dumptree(p->kids[1]);
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.u);
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.u);
                return;
                return;
        case RET+V:
        case RET+V:
                assert(!p->kids[0]);
                assert(!p->kids[0]);
                assert(!p->kids[1]);
                assert(!p->kids[1]);
                print("%s\n", opname(p->op));
                print("%s\n", opname(p->op));
                return;
                return;
        }
        }
        switch (generic(p->op)) {
        switch (generic(p->op)) {
        case CNST: case ADDRG: case ADDRF: case ADDRL: case LABEL:
        case CNST: case ADDRG: case ADDRF: case ADDRL: case LABEL:
                assert(!p->kids[0]);
                assert(!p->kids[0]);
                assert(!p->kids[1]);
                assert(!p->kids[1]);
                assert(p->syms[0] && p->syms[0]->x.name);
                assert(p->syms[0] && p->syms[0]->x.name);
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
                return;
                return;
        case CVF: case CVI: case CVP: case CVU:
        case CVF: case CVI: case CVP: case CVU:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(!p->kids[1]);
                assert(!p->kids[1]);
                assert(p->syms[0]);
                assert(p->syms[0]);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.i);
                print("%s %d\n", opname(p->op), p->syms[0]->u.c.v.i);
                return;
                return;
        case ARG: case BCOM: case NEG: case INDIR: case JUMP: case RET:
        case ARG: case BCOM: case NEG: case INDIR: case JUMP: case RET:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(!p->kids[1]);
                assert(!p->kids[1]);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                print("%s\n", opname(p->op));
                print("%s\n", opname(p->op));
                return;
                return;
        case CALL:
        case CALL:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(!p->kids[1]);
                assert(!p->kids[1]);
                assert(optype(p->op) != B);
                assert(optype(p->op) != B);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                print("%s\n", opname(p->op));
                print("%s\n", opname(p->op));
                return;
                return;
        case ASGN: case BOR: case BAND: case BXOR: case RSH: case LSH:
        case ASGN: case BOR: case BAND: case BXOR: case RSH: case LSH:
        case ADD: case SUB: case DIV: case MUL: case MOD:
        case ADD: case SUB: case DIV: case MUL: case MOD:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(p->kids[1]);
                assert(p->kids[1]);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                dumptree(p->kids[1]);
                dumptree(p->kids[1]);
                print("%s\n", opname(p->op));
                print("%s\n", opname(p->op));
                return;
                return;
        case EQ: case NE: case GT: case GE: case LE: case LT:
        case EQ: case NE: case GT: case GE: case LE: case LT:
                assert(p->kids[0]);
                assert(p->kids[0]);
                assert(p->kids[1]);
                assert(p->kids[1]);
                assert(p->syms[0]);
                assert(p->syms[0]);
                assert(p->syms[0]->x.name);
                assert(p->syms[0]->x.name);
                dumptree(p->kids[0]);
                dumptree(p->kids[0]);
                dumptree(p->kids[1]);
                dumptree(p->kids[1]);
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
                print("%s %s\n", opname(p->op), p->syms[0]->x.name);
                return;
                return;
        }
        }
        assert(0);
        assert(0);
}
}
 
 
static void I(emit)(Node p) {
static void I(emit)(Node p) {
        for (; p; p = p->link)
        for (; p; p = p->link)
                dumptree(p);
                dumptree(p);
}
}
 
 
static void I(export)(Symbol p) {
static void I(export)(Symbol p) {
        print("export %s\n", p->x.name);
        print("export %s\n", p->x.name);
}
}
 
 
static void I(function)(Symbol f, Symbol caller[], Symbol callee[], int ncalls) {
static void I(function)(Symbol f, Symbol caller[], Symbol callee[], int ncalls) {
        int i;
        int i;
 
 
        (*IR->segment)(CODE);
        (*IR->segment)(CODE);
        offset = 0;
        offset = 0;
        for (i = 0; caller[i] && callee[i]; i++) {
        for (i = 0; caller[i] && callee[i]; i++) {
                offset = roundup(offset, caller[i]->type->align);
                offset = roundup(offset, caller[i]->type->align);
                caller[i]->x.name = callee[i]->x.name = stringf("%d", offset);
                caller[i]->x.name = callee[i]->x.name = stringf("%d", offset);
                caller[i]->x.offset = callee[i]->x.offset = offset;
                caller[i]->x.offset = callee[i]->x.offset = offset;
                offset += caller[i]->type->size;
                offset += caller[i]->type->size;
        }
        }
        maxargoffset = maxoffset = argoffset = offset = 0;
        maxargoffset = maxoffset = argoffset = offset = 0;
        gencode(caller, callee);
        gencode(caller, callee);
        print("proc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
        print("proc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
        emitcode();
        emitcode();
        print("endproc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
        print("endproc %s %d %d\n", f->x.name, maxoffset, maxargoffset);
 
 
}
}
 
 
static void gen02(Node p) {
static void gen02(Node p) {
        assert(p);
        assert(p);
        if (generic(p->op) == ARG) {
        if (generic(p->op) == ARG) {
                assert(p->syms[0]);
                assert(p->syms[0]);
                argoffset += (p->syms[0]->u.c.v.i < 4 ? 4 : p->syms[0]->u.c.v.i);
                argoffset += (p->syms[0]->u.c.v.i < 4 ? 4 : p->syms[0]->u.c.v.i);
        } else if (generic(p->op) == CALL) {
        } else if (generic(p->op) == CALL) {
                maxargoffset = (argoffset > maxargoffset ? argoffset : maxargoffset);
                maxargoffset = (argoffset > maxargoffset ? argoffset : maxargoffset);
                argoffset = 0;
                argoffset = 0;
        }
        }
}
}
 
 
static void gen01(Node p) {
static void gen01(Node p) {
        if (p) {
        if (p) {
                gen01(p->kids[0]);
                gen01(p->kids[0]);
                gen01(p->kids[1]);
                gen01(p->kids[1]);
                gen02(p);
                gen02(p);
        }
        }
}
}
 
 
static Node I(gen)(Node p) {
static Node I(gen)(Node p) {
        Node q;
        Node q;
 
 
        assert(p);
        assert(p);
        for (q = p; q; q = q->link)
        for (q = p; q; q = q->link)
                gen01(q);
                gen01(q);
        return p;
        return p;
}
}
 
 
static void I(global)(Symbol p) {
static void I(global)(Symbol p) {
        print("align %d\n", p->type->align > 4 ? 4 : p->type->align);
        print("align %d\n", p->type->align > 4 ? 4 : p->type->align);
        print("LABELV %s\n", p->x.name);
        print("LABELV %s\n", p->x.name);
}
}
 
 
static void I(import)(Symbol p) {
static void I(import)(Symbol p) {
        print("import %s\n", p->x.name);
        print("import %s\n", p->x.name);
}
}
 
 
static void I(local)(Symbol p) {
static void I(local)(Symbol p) {
        offset = roundup(offset, p->type->align);
        offset = roundup(offset, p->type->align);
        p->x.name = stringf("%d", offset);
        p->x.name = stringf("%d", offset);
        p->x.offset = offset;
        p->x.offset = offset;
        offset += p->type->size;
        offset += p->type->size;
}
}
 
 
static void I(progbeg)(int argc, char *argv[]) {}
static void I(progbeg)(int argc, char *argv[]) {}
 
 
static void I(progend)(void) {}
static void I(progend)(void) {}
 
 
static void I(space)(int n) {
static void I(space)(int n) {
        print("skip %d\n", n);
        print("skip %d\n", n);
}
}
 
 
static void I(stabline)(Coordinate *cp) {
static void I(stabline)(Coordinate *cp) {
        static char *prevfile;
        static char *prevfile;
        static int prevline;
        static int prevline;
 
 
        if (cp->file && (prevfile == NULL || strcmp(prevfile, cp->file) != 0)) {
        if (cp->file && (prevfile == NULL || strcmp(prevfile, cp->file) != 0)) {
                print("file \"%s\"\n", prevfile = cp->file);
                print("file \"%s\"\n", prevfile = cp->file);
                prevline = 0;
                prevline = 0;
        }
        }
        if (cp->y != prevline)
        if (cp->y != prevline)
                print("line %d\n", prevline = cp->y);
                print("line %d\n", prevline = cp->y);
}
}
 
 
#define b_blockbeg blockbeg
#define b_blockbeg blockbeg
#define b_blockend blockend
#define b_blockend blockend
 
 
Interface bytecodeIR = {
Interface bytecodeIR = {
        1, 1, 0, /* char */
        1, 1, 0, /* char */
        2, 2, 0, /* short */
        2, 2, 0, /* short */
        4, 4, 0, /* int */
        4, 4, 0, /* int */
        4, 4, 0, /* long */
        4, 4, 0, /* long */
        4, 4, 0, /* long long */
        4, 4, 0, /* long long */
        4, 4, 1,        /* float */
        4, 4, 1,        /* float */
        8, 8, 1,        /* double */
        8, 8, 1,        /* double */
        8, 8, 1,        /* long double */
        8, 8, 1,        /* long double */
        4, 4, 0, /* T* */
        4, 4, 0, /* T* */
        0, 4, 0,  /* struct */
        0, 4, 0,  /* struct */
        0,               /* little_endian */
        0,               /* little_endian */
        0,               /* mulops_calls */
        0,               /* mulops_calls */
        0,               /* wants_callb */
        0,               /* wants_callb */
        0,               /* wants_argb */
        0,               /* wants_argb */
        1,              /* left_to_right */
        1,              /* left_to_right */
        0,               /* wants_dag */
        0,               /* wants_dag */
        0,               /* unsigned_char */
        0,               /* unsigned_char */
        I(address),
        I(address),
        I(blockbeg),
        I(blockbeg),
        I(blockend),
        I(blockend),
        I(defaddress),
        I(defaddress),
        I(defconst),
        I(defconst),
        I(defstring),
        I(defstring),
        I(defsymbol),
        I(defsymbol),
        I(emit),
        I(emit),
        I(export),
        I(export),
        I(function),
        I(function),
        I(gen),
        I(gen),
        I(global),
        I(global),
        I(import),
        I(import),
        I(local),
        I(local),
        I(progbeg),
        I(progbeg),
        I(progend),
        I(progend),
        I(segment),
        I(segment),
        I(space),
        I(space),
        0,               /* I(stabblock) */
        0,               /* I(stabblock) */
        0,               /* I(stabend) */
        0,               /* I(stabend) */
        0,               /* I(stabfend) */
        0,               /* I(stabfend) */
        0,               /* I(stabinit) */
        0,               /* I(stabinit) */
        I(stabline),
        I(stabline),
        0,               /* I(stabsym) */
        0,               /* I(stabsym) */
        0,               /* I(stabtype) */
        0,               /* I(stabtype) */
};
};
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.