Commit 97fe5572 authored by Kai Backman's avatar Kai Backman

Change 5l to use Biobufs for IO.

R=rsc
APPROVED=rsc
DELTA=132  (16 added, 45 deleted, 71 changed)
OCL=29468
CL=29497
parent e81d97ea
...@@ -414,7 +414,7 @@ double ieeedtod(Ieee*); ...@@ -414,7 +414,7 @@ double ieeedtod(Ieee*);
int32 ieeedtof(Ieee*); int32 ieeedtof(Ieee*);
void import(void); void import(void);
int isnop(Prog*); int isnop(Prog*);
void ldobj(int, int32, char*); void ldobj(Biobuf*, int32, char*);
void loadlib(void); void loadlib(void);
void listinit(void); void listinit(void);
Sym* lookup(char*, int); Sym* lookup(char*, int);
......
...@@ -371,7 +371,8 @@ void ...@@ -371,7 +371,8 @@ void
objfile(char *file) objfile(char *file)
{ {
int32 off, esym, cnt, l; int32 off, esym, cnt, l;
int f, work; int work;
Biobuf *f;
Sym *s; Sym *s;
char magbuf[SARMAG]; char magbuf[SARMAG];
char name[100], pname[150]; char name[100], pname[150];
...@@ -390,24 +391,24 @@ objfile(char *file) ...@@ -390,24 +391,24 @@ objfile(char *file)
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
Bflush(&bso); Bflush(&bso);
f = open(file, 0); f = Bopen(file, 0);
if(f < 0) { if(f == nil) {
diag("cannot open file: %s", file); diag("cannot open file: %s", file);
errorexit(); errorexit();
} }
l = read(f, magbuf, SARMAG); l = Bread(f, magbuf, SARMAG);
if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
/* load it as a regular file */ /* load it as a regular file */
l = seek(f, 0L, 2); l = Bseek(f, 0L, 2);
seek(f, 0L, 0); Bseek(f, 0L, 0);
ldobj(f, l, file); ldobj(f, l, file);
close(f); Bterm(f);
return; return;
} }
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file); Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file);
l = read(f, &arhdr, SAR_HDR); l = Bread(f, &arhdr, SAR_HDR);
if(l != SAR_HDR) { if(l != SAR_HDR) {
diag("%s: short read on archive file symbol header", file); diag("%s: short read on archive file symbol header", file);
goto out; goto out;
...@@ -423,12 +424,12 @@ objfile(char *file) ...@@ -423,12 +424,12 @@ objfile(char *file)
/* /*
* just bang the whole symbol file into memory * just bang the whole symbol file into memory
*/ */
seek(f, off, 0); Bseek(f, off, 0);
cnt = esym - off; cnt = esym - off;
start = malloc(cnt + 10); start = malloc(cnt + 10);
cnt = read(f, start, cnt); cnt = Bread(f, start, cnt);
if(cnt <= 0){ if(cnt <= 0){
close(f); Bterm(f);
return; return;
} }
stop = &start[cnt]; stop = &start[cnt];
...@@ -452,8 +453,8 @@ objfile(char *file) ...@@ -452,8 +453,8 @@ objfile(char *file)
l |= (e[2] & 0xff) << 8; l |= (e[2] & 0xff) << 8;
l |= (e[3] & 0xff) << 16; l |= (e[3] & 0xff) << 16;
l |= (e[4] & 0xff) << 24; l |= (e[4] & 0xff) << 24;
seek(f, l, 0); Bseek(f, l, 0);
l = read(f, &arhdr, SAR_HDR); l = Bread(f, &arhdr, SAR_HDR);
if(l != SAR_HDR) if(l != SAR_HDR)
goto bad; goto bad;
if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
...@@ -473,33 +474,42 @@ objfile(char *file) ...@@ -473,33 +474,42 @@ objfile(char *file)
bad: bad:
diag("%s: bad or out of date archive", file); diag("%s: bad or out of date archive", file);
out: out:
close(f); Bterm(f);
} }
int int32
zaddr(uchar *p, Adr *a, Sym *h[]) Bget4(Biobuf *f)
{
uchar p[4];
if(Bread(f, p, 4) != 4)
return 0;
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
void
zaddr(Biobuf *f, Adr *a, Sym *h[])
{ {
int i, c; int i, c;
int32 l; int32 l;
Sym *s; Sym *s;
Auto *u; Auto *u;
c = p[2]; a->type = Bgetc(f);
a->reg = Bgetc(f);
c = Bgetc(f);
if(c < 0 || c > NSYM){ if(c < 0 || c > NSYM){
print("sym out of range: %d\n", c); print("sym out of range: %d\n", c);
p[0] = ALAST+1; Bputc(f, ALAST+1);
return 0; return;
} }
a->type = p[0];
a->reg = p[1];
a->sym = h[c]; a->sym = h[c];
a->name = p[3]; a->name = Bgetc(f);
c = 4;
if(a->reg < 0 || a->reg > NREG) { if(a->reg < 0 || a->reg > NREG) {
print("register out of range %d\n", a->reg); print("register out of range %d\n", a->reg);
p[0] = ALAST+1; Bputc(f, ALAST+1);
return 0; /* force real diagnostic */ return; /* force real diagnostic */
} }
if(a->type == D_CONST || a->type == D_OCONST) { if(a->type == D_CONST || a->type == D_OCONST) {
...@@ -516,8 +526,8 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -516,8 +526,8 @@ zaddr(uchar *p, Adr *a, Sym *h[])
switch(a->type) { switch(a->type) {
default: default:
print("unknown type %d\n", a->type); print("unknown type %d\n", a->type);
p[0] = ALAST+1; Bputc(f, ALAST+1);
return 0; /* force real diagnostic */ return; /* force real diagnostic */
case D_NONE: case D_NONE:
case D_REG: case D_REG:
...@@ -527,7 +537,7 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -527,7 +537,7 @@ zaddr(uchar *p, Adr *a, Sym *h[])
break; break;
case D_REGREG: case D_REGREG:
a->offset = p[4]; a->offset = Bgetc(f);
c++; c++;
break; break;
...@@ -536,9 +546,7 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -536,9 +546,7 @@ zaddr(uchar *p, Adr *a, Sym *h[])
case D_CONST: case D_CONST:
case D_OCONST: case D_OCONST:
case D_SHIFT: case D_SHIFT:
a->offset = p[4] | (p[5]<<8) | a->offset = Bget4(f);
(p[6]<<16) | (p[7]<<24);
c += 4;
break; break;
case D_SCONST: case D_SCONST:
...@@ -548,7 +556,7 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -548,7 +556,7 @@ zaddr(uchar *p, Adr *a, Sym *h[])
nhunk -= NSNAME; nhunk -= NSNAME;
hunk += NSNAME; hunk += NSNAME;
memmove(a->sval, p+4, NSNAME); Bread(f, a->sval, NSNAME);
c += NSNAME; c += NSNAME;
break; break;
...@@ -559,19 +567,16 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -559,19 +567,16 @@ zaddr(uchar *p, Adr *a, Sym *h[])
nhunk -= NSNAME; nhunk -= NSNAME;
hunk += NSNAME; hunk += NSNAME;
a->ieee->l = p[4] | (p[5]<<8) | a->ieee->l = Bget4(f);
(p[6]<<16) | (p[7]<<24); a->ieee->h = Bget4(f);
a->ieee->h = p[8] | (p[9]<<8) |
(p[10]<<16) | (p[11]<<24);
c += 8;
break; break;
} }
s = a->sym; s = a->sym;
if(s == S) if(s == S)
return c; return;
i = a->name; i = a->name;
if(i != D_AUTO && i != D_PARAM) if(i != D_AUTO && i != D_PARAM)
return c; return;
l = a->offset; l = a->offset;
for(u=curauto; u; u=u->link) for(u=curauto; u; u=u->link)
...@@ -579,7 +584,7 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -579,7 +584,7 @@ zaddr(uchar *p, Adr *a, Sym *h[])
if(u->type == i) { if(u->type == i) {
if(u->aoffset > l) if(u->aoffset > l)
u->aoffset = l; u->aoffset = l;
return c; return;
} }
while(nhunk < sizeof(Auto)) while(nhunk < sizeof(Auto))
...@@ -593,7 +598,6 @@ zaddr(uchar *p, Adr *a, Sym *h[]) ...@@ -593,7 +598,6 @@ zaddr(uchar *p, Adr *a, Sym *h[])
u->asym = s; u->asym = s;
u->aoffset = l; u->aoffset = l;
u->type = i; u->type = i;
return c;
} }
void void
...@@ -748,37 +752,22 @@ nopout(Prog *p) ...@@ -748,37 +752,22 @@ nopout(Prog *p)
p->to.type = D_NONE; p->to.type = D_NONE;
} }
uchar*
readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
{
int n;
n = stop - good;
memmove(buf, good, stop - good);
stop = buf + n;
n = MAXIO - n;
if(n > max)
n = max;
n = read(f, stop, n);
if(n <= 0)
return 0;
return stop + n;
}
static void puntfp(Prog *); static void puntfp(Prog *);
void void
ldobj(int f, int32 c, char *pn) ldobj(Biobuf *f, int32 len, char *pn)
{ {
int32 ipc; int32 ipc;
Prog *p, *t; Prog *p, *t;
uchar *bloc, *bsize, *stop;
Sym *h[NSYM], *s, *di; Sym *h[NSYM], *s, *di;
int v, o, r, skip; int v, o, r, skip;
uint32 sig; uint32 sig;
static int files; static int files;
static char **filen; static char **filen;
char **nfilen; char **nfilen,*name;
vlong eof;
eof = Boffset(f) + len;
if((files&15) == 0){ if((files&15) == 0){
nfilen = malloc((files+16)*sizeof(char*)); nfilen = malloc((files+16)*sizeof(char*));
...@@ -788,8 +777,6 @@ ldobj(int f, int32 c, char *pn) ...@@ -788,8 +777,6 @@ ldobj(int f, int32 c, char *pn)
} }
filen[files++] = strdup(pn); filen[files++] = strdup(pn);
bsize = buf.xbuf;
bloc = buf.xbuf;
di = S; di = S;
newloop: newloop:
...@@ -800,52 +787,38 @@ newloop: ...@@ -800,52 +787,38 @@ newloop:
skip = 0; skip = 0;
loop: loop:
if(c <= 0) if(f->state == Bracteof || Boffset(f) >= eof)
goto eof; goto eof;
r = bsize - bloc; o = Bgetc(f);
if(r < 100 && r < c) { /* enough for largest prog */ if(o == Beof)
bsize = readsome(f, buf.xbuf, bloc, bsize, c); goto eof;
if(bsize == 0) // TODO(kaib): I wonder if this is an issue.
goto eof; // o |= Bgetc(f) << 8; 6l does this, 5l doesn't. I think 5g outputs 2 byte
bloc = buf.xbuf; // AXXX's
goto loop;
}
o = bloc[0]; /* as */
if(o <= AXXX || o >= ALAST) { if(o <= AXXX || o >= ALAST) {
diag("%s: line %ld: opcode out of range %d", pn, pc-ipc, o); diag("%s:#%lld: opcode out of range: %#ux", pn, Boffset(f), o);
print(" probably not a .5 file\n"); print(" probably not a .5 file\n");
errorexit(); errorexit();
} }
if(o == ANAME || o == ASIGNAME) { if(o == ANAME || o == ASIGNAME) {
sig = 0; sig = 0;
if(o == ASIGNAME){ if(o == ASIGNAME)
sig = bloc[1] | (bloc[2]<<8) | (bloc[3]<<16) | (bloc[4]<<24); sig = Bget4(f);
bloc += 4; v = Bgetc(f); /* type */
c -= 4; o = Bgetc(f); /* sym */
} r = 0;
stop = memchr(&bloc[3], 0, bsize-&bloc[3]); if(v == D_STATIC)
if(stop == 0){ r = version;
bsize = readsome(f, buf.xbuf, bloc, bsize, c); name = Brdline(f, '\0');
if(bsize == 0) if(name == nil) {
goto eof; if(Blinelen(f) > 0) {
bloc = buf.xbuf;
stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
if(stop == 0){
fprint(2, "%s: name too long\n", pn); fprint(2, "%s: name too long\n", pn);
errorexit(); errorexit();
} }
goto eof;
} }
v = bloc[1]; /* type */ s = lookup(name, r);
o = bloc[2]; /* sym */
bloc += 3;
c -= 3;
r = 0;
if(v == D_STATIC)
r = version;
s = lookup((char*)bloc, r);
c -= &stop[1] - bloc;
bloc = stop + 1;
if(sig != 0){ if(sig != 0){
if(s->sig != 0 && s->sig != sig) if(s->sig != 0 && s->sig != sig)
...@@ -881,16 +854,14 @@ loop: ...@@ -881,16 +854,14 @@ loop:
hunk += sizeof(Prog); hunk += sizeof(Prog);
p->as = o; p->as = o;
p->scond = bloc[1]; p->scond = Bgetc(f);
p->reg = bloc[2]; p->reg = Bgetc(f);
p->line = bloc[3] | (bloc[4]<<8) | (bloc[5]<<16) | (bloc[6]<<24); p->line = Bget4(f);
r = zaddr(bloc+7, &p->from, h) + 7; zaddr(f, &p->from, h);
r += zaddr(bloc+r, &p->to, h); zaddr(f, &p->to, h);
bloc += r;
c -= r;
if(p->reg < 0 || p->reg > NREG) if(p->reg > NREG)
diag("register out of range %d", p->reg); diag("register out of range %d", p->reg);
p->link = P; p->link = P;
...@@ -918,9 +889,9 @@ loop: ...@@ -918,9 +889,9 @@ loop:
curtext->to.autom = curauto; curtext->to.autom = curauto;
curauto = 0; curauto = 0;
curtext = P; curtext = P;
if(c) if(Boffset(f) == eof)
goto newloop; return;
return; goto newloop;
case AGLOBL: case AGLOBL:
s = p->from.sym; s = p->from.sym;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment