123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- #include "l.h"
- void
- dodata(void)
- {
- int i, t;
- Sym *s;
- Prog *p;
- long orig, v;
- if(debug['v'])
- Bprint(&bso, "%5.2f dodata\n", cputime());
- Bflush(&bso);
- for(p = datap; p != P; p = p->link) {
- s = p->from.sym;
- if(p->as == ADYNT || p->as == AINIT)
- s->value = dtype;
- if(s->type == SBSS)
- s->type = SDATA;
- if(s->type != SDATA)
- diag("initialize non-data (%d): %s\n%P",
- s->type, s->name, p);
- v = p->from.offset + p->reg;
- if(v > s->value)
- diag("initialize bounds (%ld): %s\n%P",
- s->value, s->name, p);
- }
- if(debug['t']) {
- /*
- * pull out string constants
- */
- for(p = datap; p != P; p = p->link) {
- s = p->from.sym;
- if(p->to.type == D_SCONST)
- s->type = SSTRING;
- }
- }
- /*
- * pass 1
- * assign 'small' variables to data segment
- * (rational is that data segment is more easily
- * addressed through offset on R12)
- */
- orig = 0;
- for(i=0; i<NHASH; i++)
- for(s = hash[i]; s != S; s = s->link) {
- t = s->type;
- if(t != SDATA && t != SBSS)
- continue;
- v = s->value;
- if(v == 0) {
- diag("%s: no size", s->name);
- v = 1;
- }
- while(v & 3)
- v++;
- s->value = v;
- if(v > MINSIZ)
- continue;
- s->value = orig;
- orig += v;
- s->type = SDATA1;
- }
- /*
- * pass 2
- * assign large 'data' variables to data segment
- */
- for(i=0; i<NHASH; i++)
- for(s = hash[i]; s != S; s = s->link) {
- t = s->type;
- if(t != SDATA) {
- if(t == SDATA1)
- s->type = SDATA;
- continue;
- }
- v = s->value;
- s->value = orig;
- orig += v;
- }
- while(orig & 7)
- orig++;
- datsize = orig;
- /*
- * pass 3
- * everything else to bss segment
- */
- for(i=0; i<NHASH; i++)
- for(s = hash[i]; s != S; s = s->link) {
- if(s->type != SBSS)
- continue;
- v = s->value;
- s->value = orig;
- orig += v;
- }
- while(orig & 7)
- orig++;
- bsssize = orig-datsize;
- xdefine("setR12", SDATA, 0L+BIG);
- xdefine("bdata", SDATA, 0L);
- xdefine("edata", SDATA, datsize);
- xdefine("end", SBSS, datsize+bsssize);
- xdefine("etext", STEXT, 0L);
- }
- void
- undef(void)
- {
- int i;
- Sym *s;
- for(i=0; i<NHASH; i++)
- for(s = hash[i]; s != S; s = s->link)
- if(s->type == SXREF)
- diag("%s: not defined", s->name);
- }
- Prog*
- brchain(Prog *p)
- {
- int i;
- for(i=0; i<20; i++) {
- if(p == P || p->as != AB)
- return p;
- p = p->cond;
- }
- return P;
- }
- int
- relinv(int a)
- {
- switch(a) {
- case ABEQ: return ABNE;
- case ABNE: return ABEQ;
- case ABCS: return ABCC;
- case ABHS: return ABLO;
- case ABCC: return ABCS;
- case ABLO: return ABHS;
- case ABMI: return ABPL;
- case ABPL: return ABMI;
- case ABVS: return ABVC;
- case ABVC: return ABVS;
- case ABHI: return ABLS;
- case ABLS: return ABHI;
- case ABGE: return ABLT;
- case ABLT: return ABGE;
- case ABGT: return ABLE;
- case ABLE: return ABGT;
- }
- diag("unknown relation: %s", anames[a]);
- return a;
- }
- void
- follow(void)
- {
- if(debug['v'])
- Bprint(&bso, "%5.2f follow\n", cputime());
- Bflush(&bso);
- firstp = prg();
- lastp = firstp;
- xfol(textp);
- firstp = firstp->link;
- lastp->link = P;
- }
- void
- xfol(Prog *p)
- {
- Prog *q, *r;
- int a, i;
- loop:
- if(p == P)
- return;
- a = p->as;
- if(a == ATEXT)
- curtext = p;
- if(a == AB) {
- q = p->cond;
- if(q != P) {
- p->mark |= FOLL;
- p = q;
- if(!(p->mark & FOLL))
- goto loop;
- }
- }
- if(p->mark & FOLL) {
- for(i=0,q=p; i<4; i++,q=q->link) {
- if(q == lastp)
- break;
- a = q->as;
- if(a == ANOP) {
- i--;
- continue;
- }
- if(a == AB || (a == ARET && q->scond == 14) || a == ARFE)
- goto copy;
- if(!q->cond || (q->cond->mark&FOLL))
- continue;
- if(a != ABEQ && a != ABNE)
- continue;
- copy:
- for(;;) {
- r = prg();
- *r = *p;
- if(!(r->mark&FOLL))
- print("cant happen 1\n");
- r->mark |= FOLL;
- if(p != q) {
- p = p->link;
- lastp->link = r;
- lastp = r;
- continue;
- }
- lastp->link = r;
- lastp = r;
- if(a == AB || (a == ARET && q->scond == 14) || a == ARFE)
- return;
- r->as = ABNE;
- if(a == ABNE)
- r->as = ABEQ;
- r->cond = p->link;
- r->link = p->cond;
- if(!(r->link->mark&FOLL))
- xfol(r->link);
- if(!(r->cond->mark&FOLL))
- print("cant happen 2\n");
- return;
- }
- }
- a = AB;
- q = prg();
- q->as = a;
- q->line = p->line;
- q->to.type = D_BRANCH;
- q->to.offset = p->pc;
- q->cond = p;
- p = q;
- }
- p->mark |= FOLL;
- lastp->link = p;
- lastp = p;
- if(a == AB || (a == ARET && p->scond == 14) || a == ARFE){
- return;
- }
- if(p->cond != P)
- if(a != ABL && p->link != P) {
- q = brchain(p->link);
- if(a != ATEXT && a != ABCASE)
- if(q != P && (q->mark&FOLL)) {
- p->as = relinv(a);
- p->link = p->cond;
- p->cond = q;
- }
- xfol(p->link);
- q = brchain(p->cond);
- if(q == P)
- q = p->cond;
- if(q->mark&FOLL) {
- p->cond = q;
- return;
- }
- p = q;
- goto loop;
- }
- p = p->link;
- goto loop;
- }
- void
- patch(void)
- {
- long c, vexit;
- Prog *p, *q;
- Sym *s;
- int a;
- if(debug['v'])
- Bprint(&bso, "%5.2f patch\n", cputime());
- Bflush(&bso);
- mkfwd();
- s = lookup("exit", 0);
- vexit = s->value;
- for(p = firstp; p != P; p = p->link) {
- a = p->as;
- if(a == ATEXT)
- curtext = p;
- if((a == ABL || a == AB || a == ARET) &&
- p->to.type != D_BRANCH && p->to.sym != S) {
- s = p->to.sym;
- switch(s->type) {
- default:
- diag("undefined: %s\n%P", s->name, p);
- s->type = STEXT;
- s->value = vexit;
- break;
- case STEXT:
- p->to.offset = s->value;
- p->to.type = D_BRANCH;
- break;
- case SUNDEF:
- if(p->as != ABL)
- diag("help: SUNDEF in AB || ARET");
- p->to.offset = 0;
- p->to.type = D_BRANCH;
- p->cond = UP;
- break;
- }
- }
- if(p->to.type != D_BRANCH || p->cond == UP)
- continue;
- c = p->to.offset;
- for(q = firstp; q != P;) {
- if(q->forwd != P)
- if(c >= q->forwd->pc) {
- q = q->forwd;
- continue;
- }
- if(c == q->pc)
- break;
- q = q->link;
- }
- if(q == P) {
- diag("branch out of range %ld\n%P", c, p);
- p->to.type = D_NONE;
- }
- p->cond = q;
- }
- for(p = firstp; p != P; p = p->link) {
- if(p->as == ATEXT)
- curtext = p;
- if(p->cond != P && p->cond != UP) {
- p->cond = brloop(p->cond);
- if(p->cond != P)
- if(p->to.type == D_BRANCH)
- p->to.offset = p->cond->pc;
- }
- }
- }
- #define LOG 5
- void
- mkfwd(void)
- {
- Prog *p;
- long dwn[LOG], cnt[LOG], i;
- Prog *lst[LOG];
- for(i=0; i<LOG; i++) {
- if(i == 0)
- cnt[i] = 1; else
- cnt[i] = LOG * cnt[i-1];
- dwn[i] = 1;
- lst[i] = P;
- }
- i = 0;
- for(p = firstp; p != P; p = p->link) {
- if(p->as == ATEXT)
- curtext = p;
- i--;
- if(i < 0)
- i = LOG-1;
- p->forwd = P;
- dwn[i]--;
- if(dwn[i] <= 0) {
- dwn[i] = cnt[i];
- if(lst[i] != P)
- lst[i]->forwd = p;
- lst[i] = p;
- }
- }
- }
- Prog*
- brloop(Prog *p)
- {
- Prog *q;
- int c;
- for(c=0; p!=P;) {
- if(p->as != AB)
- return p;
- q = p->cond;
- if(q <= p) {
- c++;
- if(q == p || c > 5000)
- break;
- }
- p = q;
- }
- return P;
- }
- long
- atolwhex(char *s)
- {
- long n;
- int f;
- n = 0;
- f = 0;
- while(*s == ' ' || *s == '\t')
- s++;
- if(*s == '-' || *s == '+') {
- if(*s++ == '-')
- f = 1;
- while(*s == ' ' || *s == '\t')
- s++;
- }
- if(s[0]=='0' && s[1]){
- if(s[1]=='x' || s[1]=='X'){
- s += 2;
- for(;;){
- if(*s >= '0' && *s <= '9')
- n = n*16 + *s++ - '0';
- else if(*s >= 'a' && *s <= 'f')
- n = n*16 + *s++ - 'a' + 10;
- else if(*s >= 'A' && *s <= 'F')
- n = n*16 + *s++ - 'A' + 10;
- else
- break;
- }
- } else
- while(*s >= '0' && *s <= '7')
- n = n*8 + *s++ - '0';
- } else
- while(*s >= '0' && *s <= '9')
- n = n*10 + *s++ - '0';
- if(f)
- n = -n;
- return n;
- }
- long
- rnd(long v, long r)
- {
- long c;
- if(r <= 0)
- return v;
- v += r - 1;
- c = v % r;
- if(c < 0)
- c += r;
- v -= c;
- return v;
- }
- void
- import(void)
- {
- int i;
- Sym *s;
- for(i = 0; i < NHASH; i++)
- for(s = hash[i]; s != S; s = s->link)
- if(s->sig != 0 && s->type == SXREF && (nimports == 0 || s->subtype == SIMPORT)){
- undefsym(s);
- Bprint(&bso, "IMPORT: %s sig=%lux v=%ld\n", s->name, s->sig, s->value);
- }
- }
- void
- ckoff(Sym *s, long v)
- {
- if(v < 0 || v >= 1<<Roffset)
- diag("relocation offset %ld for %s out of range", v, s->name);
- }
- static Prog*
- newdata(Sym *s, int o, int w, int t)
- {
- Prog *p;
- p = prg();
- p->link = datap;
- datap = p;
- p->as = ADATA;
- p->reg = w;
- p->from.type = D_OREG;
- p->from.name = t;
- p->from.sym = s;
- p->from.offset = o;
- p->to.type = D_CONST;
- p->to.name = D_NONE;
- return p;
- }
- void
- export(void)
- {
- int i, j, n, off, nb, sv, ne;
- Sym *s, *et, *str, **esyms;
- Prog *p;
- char buf[NSNAME], *t;
- n = 0;
- for(i = 0; i < NHASH; i++)
- for(s = hash[i]; s != S; s = s->link)
- if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
- n++;
- esyms = malloc(n*sizeof(Sym*));
- ne = n;
- n = 0;
- for(i = 0; i < NHASH; i++)
- for(s = hash[i]; s != S; s = s->link)
- if(s->sig != 0 && s->type != SXREF && s->type != SUNDEF && (nexports == 0 || s->subtype == SEXPORT))
- esyms[n++] = s;
- for(i = 0; i < ne-1; i++)
- for(j = i+1; j < ne; j++)
- if(strcmp(esyms[i]->name, esyms[j]->name) > 0){
- s = esyms[i];
- esyms[i] = esyms[j];
- esyms[j] = s;
- }
- nb = 0;
- off = 0;
- et = lookup(EXPTAB, 0);
- if(et->type != 0 && et->type != SXREF)
- diag("%s already defined", EXPTAB);
- et->type = SDATA;
- str = lookup(".string", 0);
- if(str->type == 0)
- str->type = SDATA;
- sv = str->value;
- for(i = 0; i < ne; i++){
- s = esyms[i];
- Bprint(&bso, "EXPORT: %s sig=%lux t=%d\n", s->name, s->sig, s->type);
- /* signature */
- p = newdata(et, off, sizeof(long), D_EXTERN);
- off += sizeof(long);
- p->to.offset = s->sig;
- /* address */
- p = newdata(et, off, sizeof(long), D_EXTERN);
- off += sizeof(long);
- p->to.name = D_EXTERN;
- p->to.sym = s;
- /* string */
- t = s->name;
- n = strlen(t)+1;
- for(;;){
- buf[nb++] = *t;
- sv++;
- if(nb >= NSNAME){
- p = newdata(str, sv-NSNAME, NSNAME, D_STATIC);
- p->to.type = D_SCONST;
- p->to.sval = malloc(NSNAME);
- memmove(p->to.sval, buf, NSNAME);
- nb = 0;
- }
- if(*t++ == 0)
- break;
- }
- /* name */
- p = newdata(et, off, sizeof(long), D_EXTERN);
- off += sizeof(long);
- p->to.name = D_STATIC;
- p->to.sym = str;
- p->to.offset = sv-n;
- }
- if(nb > 0){
- p = newdata(str, sv-nb, nb, D_STATIC);
- p->to.type = D_SCONST;
- p->to.sval = malloc(NSNAME);
- memmove(p->to.sval, buf, nb);
- }
- for(i = 0; i < 3; i++){
- newdata(et, off, sizeof(long), D_EXTERN);
- off += sizeof(long);
- }
- et->value = off;
- if(sv == 0)
- sv = 1;
- str->value = sv;
- exports = ne;
- free(esyms);
- }
|