#include "l.h" #define Dbufslop 100 long entryvalue(void) { char *a; Sym *s; a = INITENTRY; if(*a >= '0' && *a <= '9') return atolwhex(a); s = lookup(a, 0); if(s->type == 0) return INITTEXT; if(s->type != STEXT) diag("entry not text: %s", s->name); return s->value; } void asmb(void) { Prog *p; long v; int a; uchar *op1; if(debug['v']) Bprint(&bso, "%5.2f asmb\n", cputime()); Bflush(&bso); seek(cout, HEADR, 0); pc = INITTEXT; curp = firstp; for(p = firstp; p != P; p = p->link) { if(p->as == ATEXT) curtext = p; if(p->pc != pc) { if(!debug['a']) print("%P\n", curp); diag("phase error %lux sb %lux in %s\n", p->pc, pc, TNAME); pc = p->pc; } curp = p; asmins(p); if(cbc < sizeof(and)) cflush(); a = (andptr - and); if(debug['a']) { Bprint(&bso, pcstr, pc); for(op1 = and; op1 < andptr; op1++) Bprint(&bso, "%.2ux", *op1 & 0xff); Bprint(&bso, "%.*s", (24+7-printcol)/8, "\t\t\t"); Bprint(&bso, "%P\n", curp); } memmove(cbp, and, a); cbp += a; pc += a; cbc -= a; } cflush(); switch(HEADTYPE) { default: diag("unknown header type %d\n", HEADTYPE); case 0: seek(cout, rnd(HEADR+textsize, 8192), 0); break; case 1: textsize = rnd(HEADR+textsize, 4096)-HEADR; seek(cout, textsize+HEADR, 0); break; case 2: seek(cout, HEADR+textsize, 0); break; case 3: seek(cout, HEADR+rnd(textsize, INITRND), 0); break; } if(debug['v']) Bprint(&bso, "%5.2f datblk\n", cputime()); Bflush(&bso); for(v = 0; v < datsize; v += sizeof(buf)-Dbufslop) { if(datsize-v > sizeof(buf)-Dbufslop) datblk(v, sizeof(buf)-Dbufslop); else datblk(v, datsize-v); } symsize = 0; spsize = 0; lcsize = 0; if(!debug['s']) { if(debug['v']) Bprint(&bso, "%5.2f sym\n", cputime()); Bflush(&bso); switch(HEADTYPE) { default: case 0: seek(cout, rnd(HEADR+textsize, 8192)+datsize, 0); break; case 1: seek(cout, rnd(HEADR+textsize, INITRND)+datsize, 0); break; case 2: seek(cout, HEADR+textsize+datsize, 0); break; case 3: debug['s'] = 1; break; } if(!debug['s']) asmsym(); if(debug['v']) Bprint(&bso, "%5.2f sp\n", cputime()); Bflush(&bso); if(!debug['s']) asmsp(); if(debug['v']) Bprint(&bso, "%5.2f pc\n", cputime()); Bflush(&bso); if(!debug['s']) asmlc(); cflush(); } if(debug['v']) Bprint(&bso, "%5.2f headr\n", cputime()); Bflush(&bso); seek(cout, 0L, 0); switch(HEADTYPE) { default: case 0: /* garbage */ lput(0x160L<<16); /* magic and sections */ lput(0L); /* time and date */ lput(rnd(HEADR+textsize, 4096)+datsize); lput(symsize); /* nsyms */ lput((0x38L<<16)|7L); /* size of optional hdr and flags */ lput((0413<<16)|0437L); /* magic and version */ lput(rnd(HEADR+textsize, 4096)); /* sizes */ lput(datsize); lput(bsssize); lput(entryvalue()); /* va of entry */ lput(INITTEXT-HEADR); /* va of base of text */ lput(INITDAT); /* va of base of data */ lput(INITDAT+datsize); /* va of base of bss */ lput(~0L); /* gp reg mask */ lput(0L); lput(0L); lput(0L); lput(0L); lput(~0L); /* gp value ?? */ break; lputl(0); /* x */ case 1: /* unix coff */ /* * file header */ lputl(0x0004014c); /* 4 sections, magic */ lputl(0); /* unix time stamp */ lputl(0); /* symbol table */ lputl(0); /* nsyms */ lputl(0x0003001c); /* flags, sizeof a.out header */ /* * a.out header */ lputl(0x10b); /* magic, version stamp */ lputl(rnd(textsize, INITRND)); /* text sizes */ lputl(datsize); /* data sizes */ lputl(bsssize); /* bss sizes */ lput(entryvalue()); /* va of entry */ lputl(INITTEXT); /* text start */ lputl(INITDAT); /* data start */ /* * text section header */ s8put(".text"); lputl(HEADR); /* pa */ lputl(HEADR); /* va */ lputl(textsize); /* text size */ lputl(HEADR); /* file offset */ lputl(0); /* relocation */ lputl(0); /* line numbers */ lputl(0); /* relocation, line numbers */ lputl(0x20); /* flags text only */ /* * data section header */ s8put(".data"); lputl(INITDAT); /* pa */ lputl(INITDAT); /* va */ lputl(datsize); /* data size */ lputl(HEADR+textsize); /* file offset */ lputl(0); /* relocation */ lputl(0); /* line numbers */ lputl(0); /* relocation, line numbers */ lputl(0x40); /* flags data only */ /* * bss section header */ s8put(".bss"); lputl(INITDAT+datsize); /* pa */ lputl(INITDAT+datsize); /* va */ lputl(bsssize); /* bss size */ lputl(0); /* file offset */ lputl(0); /* relocation */ lputl(0); /* line numbers */ lputl(0); /* relocation, line numbers */ lputl(0x80); /* flags bss only */ /* * comment section header */ s8put(".comment"); lputl(0); /* pa */ lputl(0); /* va */ lputl(symsize+lcsize); /* comment size */ lputl(HEADR+textsize+datsize); /* file offset */ lputl(HEADR+textsize+datsize); /* offset of syms */ lputl(HEADR+textsize+datsize+symsize);/* offset of line numbers */ lputl(0); /* relocation, line numbers */ lputl(0x200); /* flags comment only */ break; case 2: /* plan9 */ lput(4*11*11+7); /* magic */ lput(textsize); /* sizes */ lput(datsize); lput(bsssize); lput(symsize); /* nsyms */ lput(entryvalue()); /* va of entry */ lput(spsize); /* sp offsets */ lput(lcsize); /* line offsets */ break; case 3: /* msdos boot */ break; } cflush(); } void lput(long l) { CPUT(l>>24) CPUT(l>>16) CPUT(l>>8) CPUT(l) } void lputl(long l) { CPUT(l) CPUT(l>>8) CPUT(l>>16) CPUT(l>>24) } void s8put(char *n) { char name[8]; int i; strncpy(name, n, sizeof(name)); for(i=0; ilink) { curp = p; l = p->from.sym->value + p->from.offset - s; c = p->from.scale; i = 0; if(l < 0) { if(l+c <= 0) continue; while(l < 0) { l++; i++; } } if(l >= n) continue; for(j=l+(c-i)-1; j>=l; j--) if(buf.dbuf[j]) { print("%P\n", p); diag("multiple initialization\n"); break; } switch(p->to.type) { case D_FCONST: switch(c) { default: case 4: fl = ieeedtof(&p->to.ieee); cast = (char*)&fl; if(debug['a'] && i == 0) { Bprint(&bso, pcstr, l+s+INITDAT); for(j=0; jto.ieee; if(debug['a'] && i == 0) { Bprint(&bso, pcstr, l+s+INITDAT); for(j=0; jto.scon[j] & 0xff); Bprint(&bso, "%.*s", (24+7-printcol)/8, "\t\t\t"); Bprint(&bso, "%P\n", curp); } for(; ito.scon[i]; l++; } break; default: fl = p->to.offset; if(p->to.type == D_ADDR) { if(p->to.index != D_STATIC && p->to.index != D_EXTERN) diag("DADDR type%P\n", p); if(p->to.sym) { fl += p->to.sym->value; if(p->to.sym->type != STEXT) fl += INITDAT; } } cast = (char*)&fl; switch(c) { default: diag("bad nuxi %d %d\n%P\n", c, i, curp); break; case 1: if(debug['a'] && i == 0) { Bprint(&bso, pcstr, l+s+INITDAT); for(j=0; j