Commit efec14bc authored by Ken Thompson's avatar Ken Thompson

plan9 line numbers and line table

SVN=122793
parent 1ad1044b
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Use of this source code is governed by a BSD-style # Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
for i in lib9 libbio for i in lib9 libbio libmach_amd64
do do
cd $i cd $i
make clean make clean
......
...@@ -200,6 +200,13 @@ belexinit(int lextype) ...@@ -200,6 +200,13 @@ belexinit(int lextype)
int i; int i;
Sym *s0, *s1; Sym *s0, *s1;
zprog.link = P;
zprog.as = AGOK;
zprog.from.type = D_NONE;
zprog.from.index = D_NONE;
zprog.from.scale = 0;
zprog.to = zprog.from;
for(i=0; i<nelem(typedefs); i+=2) { for(i=0; i<nelem(typedefs); i+=2) {
s1 = lookup(typedefs[i+1]); s1 = lookup(typedefs[i+1]);
if(s1->lexical != lextype) if(s1->lexical != lextype)
......
...@@ -97,6 +97,8 @@ EXTERN long stringo; // size of string objects ...@@ -97,6 +97,8 @@ EXTERN long stringo; // size of string objects
EXTERN long pcloc; // instruction counter EXTERN long pcloc; // instruction counter
EXTERN String emptystring; EXTERN String emptystring;
extern char* anames[]; extern char* anames[];
EXTERN Hist* hist;
EXTERN Prog zprog;
/* /*
* gen.c * gen.c
...@@ -195,6 +197,7 @@ void zaddr(Biobuf*, Addr*, int); ...@@ -195,6 +197,7 @@ void zaddr(Biobuf*, Addr*, int);
void ieeedtod(Ieee*, double); void ieeedtod(Ieee*, double);
void dumpstrings(void); void dumpstrings(void);
void dumpsignatures(void); void dumpsignatures(void);
void outhist(Biobuf*);
/* /*
* align * align
......
...@@ -52,12 +52,12 @@ Pconv(Fmt *fp) ...@@ -52,12 +52,12 @@ Pconv(Fmt *fp)
sconsize = 8; sconsize = 8;
if(p->as == ADATA) { if(p->as == ADATA) {
sconsize = p->from.scale; sconsize = p->from.scale;
snprint(str, sizeof(str), "%.4ld %-7A %D/%d,%D", snprint(str, sizeof(str), "%.4ld (%4ld) %-7A %D/%d,%D",
p->loc, p->as, &p->from, sconsize, &p->to); p->loc, p->lineno, p->as, &p->from, sconsize, &p->to);
return fmtstrcpy(fp, str); return fmtstrcpy(fp, str);
} }
snprint(str, sizeof(str), "%.4ld %-7A %D,%D", snprint(str, sizeof(str), "%.4ld (%4ld) %-7A %D,%D",
p->loc, p->as, &p->from, &p->to); p->loc, p->lineno, p->as, &p->from, &p->to);
return fmtstrcpy(fp, str); return fmtstrcpy(fp, str);
} }
......
...@@ -55,6 +55,8 @@ dumpobj(void) ...@@ -55,6 +55,8 @@ dumpobj(void)
dumpexport(); dumpexport();
Bprint(bout, "\n!\n"); Bprint(bout, "\n!\n");
outhist(bout);
// add globals // add globals
nodconst(&n1, types[TINT32], 0); nodconst(&n1, types[TINT32], 0);
for(d=externdcl; d!=D; d=d->forw) { for(d=externdcl; d!=D; d=d->forw) {
...@@ -158,6 +160,9 @@ dumpobj(void) ...@@ -158,6 +160,9 @@ dumpobj(void)
zaddr(bout, &p->to, st); zaddr(bout, &p->to, st);
} }
} }
Bterm(bout);
return;
Bterm(bout);
} }
void void
...@@ -258,6 +263,69 @@ zaddr(Biobuf *b, Addr *a, int s) ...@@ -258,6 +263,69 @@ zaddr(Biobuf *b, Addr *a, int s)
Bputc(b, a->type); Bputc(b, a->type);
} }
void
outhist(Biobuf *b)
{
Hist *h;
char *p, *q, *op;
Prog pg;
int n;
pg = zprog;
pg.as = AHISTORY;
for(h = hist; h != H; h = h->link) {
p = h->name;
op = 0;
if(p && p[0] != '/' && h->offset == 0 && pathname && pathname[0] == '/') {
op = p;
p = pathname;
}
while(p) {
q = utfrune(p, '/');
if(q) {
n = q-p;
if(n == 0)
n = 1; // leading "/"
q++;
} else {
n = strlen(p);
q = 0;
}
if(n) {
Bputc(b, ANAME);
Bputc(b, ANAME>>8);
Bputc(b, D_FILE);
Bputc(b, 1);
Bputc(b, '<');
Bwrite(b, p, n);
Bputc(b, 0);
}
p = q;
if(p == 0 && op) {
p = op;
op = 0;
}
}
pg.lineno = h->line;
pg.to.type = zprog.to.type;
pg.to.offset = h->offset;
if(h->offset)
pg.to.type = D_CONST;
Bputc(b, pg.as);
Bputc(b, pg.as>>8);
Bputc(b, pg.lineno);
Bputc(b, pg.lineno>>8);
Bputc(b, pg.lineno>>16);
Bputc(b, pg.lineno>>24);
zaddr(b, &pg.from, 0);
zaddr(b, &pg.to, 0);
}
}
void void
ieeedtod(Ieee *ieee, double native) ieeedtod(Ieee *ieee, double native)
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Use of this source code is governed by a BSD-style # Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
for i in 6l 6a 6c 6g gc cc for i in 6l 6a 6c 6g gc cc db
do do
cd $i cd $i
make clean make clean
......
...@@ -27,6 +27,7 @@ OFILES=\ ...@@ -27,6 +27,7 @@ OFILES=\
const.$O\ const.$O\
mpatof.$O\ mpatof.$O\
sysimport.$O\ sysimport.$O\
compat.$O\
$(LIB): $(OFILES) $(LIB): $(OFILES)
ar rsc $(LIB) $(OFILES) ar rsc $(LIB) $(OFILES)
......
...@@ -511,13 +511,14 @@ popdcl(char *why) ...@@ -511,13 +511,14 @@ popdcl(char *why)
// if(dflag()) // if(dflag())
// print("revert\n"); // print("revert\n");
for(d=dclstack; d!=S; d=d->link) { for(d=dclstack; d!=S; d=d->link) {
if(d->name == nil) if(d->name == nil)
break; break;
s = pkglookup(d->name, d->package); s = pkglookup(d->name, d->package);
dcopy(s, d); dcopy(s, d);
if(dflag()) if(dflag())
print("\t%ld pop %S\n", curio.lineno, s); print("\t%L pop %S\n", lineno, s);
} }
if(d == S) if(d == S)
fatal("popdcl: no mark"); fatal("popdcl: no mark");
...@@ -537,7 +538,7 @@ poptodcl(void) ...@@ -537,7 +538,7 @@ poptodcl(void)
s = pkglookup(d->name, d->package); s = pkglookup(d->name, d->package);
dcopy(s, d); dcopy(s, d);
if(dflag()) if(dflag())
print("\t%ld pop %S\n", curio.lineno, s); print("\t%L pop %S\n", lineno, s);
} }
if(d == S) if(d == S)
fatal("poptodcl: no mark"); fatal("poptodcl: no mark");
......
...@@ -35,7 +35,7 @@ loop: ...@@ -35,7 +35,7 @@ loop:
d = mal(sizeof(*d)); d = mal(sizeof(*d));
d->dsym = s; d->dsym = s;
d->dnode = N; d->dnode = N;
d->lineno = curio.lineno; d->lineno = lineno;
r = exportlist; r = exportlist;
d->back = r->back; d->back = r->back;
......
...@@ -14,6 +14,7 @@ tothinkabout: ...@@ -14,6 +14,7 @@ tothinkabout:
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include <bio.h> #include <bio.h>
#include "compat.h"
#ifndef EXTERN #ifndef EXTERN
#define EXTERN extern #define EXTERN extern
...@@ -28,6 +29,7 @@ enum ...@@ -28,6 +29,7 @@ enum
YYMAXDEPTH = 500, YYMAXDEPTH = 500,
MAXALIGN = 7, MAXALIGN = 7,
UINF = 100, UINF = 100,
HISTSZ = 10,
PRIME1 = 3, PRIME1 = 3,
PRIME2 = 10007, PRIME2 = 10007,
...@@ -189,6 +191,16 @@ struct Iter ...@@ -189,6 +191,16 @@ struct Iter
Node* n; Node* n;
}; };
typedef struct Hist Hist;
struct Hist
{
Hist* link;
char* name;
long line;
long offset;
};
#define H ((Hist*)0)
enum enum
{ {
OXXX, OXXX,
...@@ -313,13 +325,18 @@ struct Io ...@@ -313,13 +325,18 @@ struct Io
{ {
char* infile; char* infile;
Biobuf* bin; Biobuf* bin;
long lineno; long ilineno;
int peekc; int peekc;
char* cp; // used for content when bin==nil char* cp; // used for content when bin==nil
}; };
EXTERN Io curio; EXTERN Io curio;
EXTERN Io pushedio; EXTERN Io pushedio;
EXTERN long lineno;
EXTERN char* pathname;
EXTERN Hist* hist;
EXTERN Hist* ehist;
EXTERN char* infile; EXTERN char* infile;
EXTERN char* outfile; EXTERN char* outfile;
...@@ -416,6 +433,7 @@ Sym* pkglookup(char*, char*); ...@@ -416,6 +433,7 @@ Sym* pkglookup(char*, char*);
void yyerror(char*, ...); void yyerror(char*, ...);
void warn(char*, ...); void warn(char*, ...);
void fatal(char*, ...); void fatal(char*, ...);
void linehist(char*, long);
Node* nod(int, Node*, Node*); Node* nod(int, Node*, Node*);
Type* typ(int); Type* typ(int);
Dcl* dcl(void); Dcl* dcl(void);
...@@ -457,6 +475,7 @@ Type* funcnext(Iter*); ...@@ -457,6 +475,7 @@ Type* funcnext(Iter*);
int Econv(Fmt*); int Econv(Fmt*);
int Jconv(Fmt*); int Jconv(Fmt*);
int Lconv(Fmt*);
int Oconv(Fmt*); int Oconv(Fmt*);
int Sconv(Fmt*); int Sconv(Fmt*);
int Tconv(Fmt*); int Tconv(Fmt*);
......
...@@ -39,6 +39,10 @@ mainlex(int argc, char *argv[]) ...@@ -39,6 +39,10 @@ mainlex(int argc, char *argv[])
if(argc != 1) if(argc != 1)
goto usage; goto usage;
pathname = mal(100);
if(mygetwd(pathname, 99) == 0)
strcpy(pathname, "/???");
fmtinstall('O', Oconv); // node opcodes fmtinstall('O', Oconv); // node opcodes
fmtinstall('E', Econv); // etype opcodes fmtinstall('E', Econv); // etype opcodes
fmtinstall('J', Jconv); // all the node flags fmtinstall('J', Jconv); // all the node flags
...@@ -46,15 +50,19 @@ mainlex(int argc, char *argv[]) ...@@ -46,15 +50,19 @@ mainlex(int argc, char *argv[])
fmtinstall('T', Tconv); // type pointer fmtinstall('T', Tconv); // type pointer
fmtinstall('N', Nconv); // node pointer fmtinstall('N', Nconv); // node pointer
fmtinstall('Z', Zconv); // escaped string fmtinstall('Z', Zconv); // escaped string
fmtinstall('L', Lconv); // line number
lexinit(); lexinit();
lineno = 1;
infile = argv[0]; infile = argv[0];
curio.infile = infile; linehist(infile, 0);
curio.bin = Bopen(curio.infile, OREAD); curio.infile = infile;
curio.bin = Bopen(infile, OREAD);
if(curio.bin == nil) if(curio.bin == nil)
fatal("cant open: %s", curio.infile); fatal("cant open: %s", infile);
curio.peekc = 0;
externdcl = mal(sizeof(*externdcl)); externdcl = mal(sizeof(*externdcl));
externdcl->back = externdcl; externdcl->back = externdcl;
...@@ -69,14 +77,11 @@ mainlex(int argc, char *argv[]) ...@@ -69,14 +77,11 @@ mainlex(int argc, char *argv[])
fskel->right->left = nod(ODCLFIELD, N, N); fskel->right->left = nod(ODCLFIELD, N, N);
fskel->right->right = nod(ODCLFIELD, N, N); fskel->right->right = nod(ODCLFIELD, N, N);
curio.peekc = 0;
curio.lineno = 1;
nerrors = 0; nerrors = 0;
yyparse(); yyparse();
linehist(nil, 0);
Bterm(curio.bin); Bterm(curio.bin);
if(bout != nil)
Bterm(bout);
if(nerrors) if(nerrors)
errorexit(); errorexit();
...@@ -104,6 +109,7 @@ void ...@@ -104,6 +109,7 @@ void
importfile(Val *f) importfile(Val *f)
{ {
Biobuf *imp; Biobuf *imp;
char *file;
long c; long c;
if(f->ctype != CTSTR) { if(f->ctype != CTSTR) {
...@@ -112,12 +118,12 @@ importfile(Val *f) ...@@ -112,12 +118,12 @@ importfile(Val *f)
} }
// BOTCH need to get .8 from backend // BOTCH need to get .8 from backend
snprint(namebuf, sizeof(namebuf), "%Z.6", f->sval); snprint(namebuf, sizeof(namebuf), "%Z.6", f->sval);
file = strdup(namebuf);
linehist(file, 0);
imp = Bopen(namebuf, OREAD); imp = Bopen(file, OREAD);
if(imp == nil) { if(imp == nil)
yyerror("cant open import: %s", namebuf); fatal("cant open import: %s", namebuf);
return;
}
/* /*
* position the input right * position the input right
...@@ -125,9 +131,8 @@ importfile(Val *f) ...@@ -125,9 +131,8 @@ importfile(Val *f)
*/ */
pushedio = curio; pushedio = curio;
curio.bin = imp; curio.bin = imp;
curio.lineno = 1;
curio.peekc = 0; curio.peekc = 0;
curio.infile = strdup(namebuf); curio.infile = file;
for(;;) { for(;;) {
c = getc(); c = getc();
if(c == EOF) if(c == EOF)
...@@ -148,6 +153,8 @@ importfile(Val *f) ...@@ -148,6 +153,8 @@ importfile(Val *f)
void void
unimportfile(void) unimportfile(void)
{ {
linehist(nil, 0);
if(curio.bin != nil) { if(curio.bin != nil) {
Bterm(curio.bin); Bterm(curio.bin);
curio.bin = nil; curio.bin = nil;
...@@ -160,12 +167,17 @@ unimportfile(void) ...@@ -160,12 +167,17 @@ unimportfile(void)
void void
cannedimports(void) cannedimports(void)
{ {
char *file;
file = "sys.6";
linehist(file, 0);
pushedio = curio; pushedio = curio;
curio.bin = nil; curio.bin = nil;
curio.lineno = 1;
curio.peekc = 0; curio.peekc = 0;
curio.infile = "internal sys.go"; curio.infile = file;
curio.cp = sysimport; curio.cp = sysimport;
pkgmyname = S; pkgmyname = S;
inimportsys = 1; inimportsys = 1;
} }
...@@ -619,7 +631,7 @@ getc(void) ...@@ -619,7 +631,7 @@ getc(void)
if(c != 0) { if(c != 0) {
curio.peekc = 0; curio.peekc = 0;
if(c == '\n') if(c == '\n')
curio.lineno++; lineno++;
return c; return c;
} }
...@@ -636,7 +648,7 @@ getc(void) ...@@ -636,7 +648,7 @@ getc(void)
return EOF; return EOF;
case '\n': case '\n':
curio.lineno++; lineno++;
break; break;
} }
return c; return c;
...@@ -647,7 +659,7 @@ ungetc(int c) ...@@ -647,7 +659,7 @@ ungetc(int c)
{ {
curio.peekc = c; curio.peekc = c;
if(c == '\n') if(c == '\n')
curio.lineno--; lineno--;
} }
long long
...@@ -688,7 +700,7 @@ getnsc(void) ...@@ -688,7 +700,7 @@ getnsc(void)
if(!isspace(c)) if(!isspace(c))
return c; return c;
if(c == '\n') { if(c == '\n') {
curio.lineno++; lineno++;
return c; return c;
} }
c = getc(); c = getc();
......
...@@ -13,25 +13,12 @@ errorexit(void) ...@@ -13,25 +13,12 @@ errorexit(void)
myexit(1); myexit(1);
} }
void
myexit(int x)
{
if(x)
exits("error");
exits(nil);
}
void void
yyerror(char *fmt, ...) yyerror(char *fmt, ...)
{ {
va_list arg; va_list arg;
long lno;
lno = dynlineno; print("%L: ");
if(lno == 0)
lno = curio.lineno;
print("%s:%ld: ", curio.infile, lno);
va_start(arg, fmt); va_start(arg, fmt);
vfprint(1, fmt, arg); vfprint(1, fmt, arg);
va_end(arg); va_end(arg);
...@@ -48,13 +35,8 @@ void ...@@ -48,13 +35,8 @@ void
warn(char *fmt, ...) warn(char *fmt, ...)
{ {
va_list arg; va_list arg;
long lno;
lno = dynlineno; print("%L warning: ");
if(lno == 0)
lno = curio.lineno;
print("%s:%ld: ", curio.infile, lno);
va_start(arg, fmt); va_start(arg, fmt);
vfprint(1, fmt, arg); vfprint(1, fmt, arg);
va_end(arg); va_end(arg);
...@@ -67,13 +49,8 @@ void ...@@ -67,13 +49,8 @@ void
fatal(char *fmt, ...) fatal(char *fmt, ...)
{ {
va_list arg; va_list arg;
long lno;
lno = dynlineno;
if(lno == 0)
lno = curio.lineno;
print("%s:%ld: fatal error: ", curio.infile, lno); print("%L fatal error: ");
va_start(arg, fmt); va_start(arg, fmt);
vfprint(1, fmt, arg); vfprint(1, fmt, arg);
va_end(arg); va_end(arg);
...@@ -83,6 +60,31 @@ fatal(char *fmt, ...) ...@@ -83,6 +60,31 @@ fatal(char *fmt, ...)
myexit(1); myexit(1);
} }
void
linehist(char *file, long off)
{
Hist *h;
if(debug['i'])
if(file != nil)
print("%L: import %s\n", file);
else
print("%L: <eof>\n");
h = alloc(sizeof(Hist));
h->name = file;
h->line = lineno;
h->offset = off;
h->link = H;
if(ehist == H) {
hist = h;
ehist = h;
return;
}
ehist->link = h;
ehist = h;
}
ulong ulong
stringhash(char *p) stringhash(char *p)
{ {
...@@ -248,7 +250,7 @@ nod(int op, Node *nleft, Node *nright) ...@@ -248,7 +250,7 @@ nod(int op, Node *nleft, Node *nright)
n->right = nright; n->right = nright;
n->lineno = dynlineno; n->lineno = dynlineno;
if(dynlineno == 0) if(dynlineno == 0)
n->lineno = curio.lineno; n->lineno = lineno;
return n; return n;
} }
...@@ -646,6 +648,74 @@ Oconv(Fmt *fp) ...@@ -646,6 +648,74 @@ Oconv(Fmt *fp)
return fmtstrcpy(fp, opnames[o]); return fmtstrcpy(fp, opnames[o]);
} }
int
Lconv(Fmt *fp)
{
char str[STRINGSZ], s[STRINGSZ];
struct
{
Hist* incl; /* start of this include file */
long idel; /* delta line number to apply to include */
Hist* line; /* start of this #line directive */
long ldel; /* delta line number to apply to #line */
} a[HISTSZ];
long lno, d;
int i, n;
Hist *h;
lno = dynlineno;
if(lno == 0)
lno = lineno;
n = 0;
for(h=hist; h!=H; h=h->link) {
if(lno < h->line)
break;
if(h->name) {
if(n < HISTSZ) { /* beginning of file */
a[n].incl = h;
a[n].idel = h->line;
a[n].line = 0;
}
n++;
continue;
}
n--;
if(n > 0 && n < HISTSZ) {
d = h->line - a[n].incl->line;
a[n-1].ldel += d;
a[n-1].idel += d;
}
}
if(n > HISTSZ)
n = HISTSZ;
str[0] = 0;
for(i=n-1; i>=0; i--) {
if(i != n-1) {
if(fp->flags & ~(FmtWidth|FmtPrec))
break;
strcat(str, " ");
}
if(a[i].line)
snprint(s, STRINGSZ, "%s:%ld[%s:%ld]",
a[i].line->name, lno-a[i].ldel+1,
a[i].incl->name, lno-a[i].idel+1);
else
snprint(s, STRINGSZ, "%s:%ld",
a[i].incl->name, lno-a[i].idel+1);
if(strlen(s)+strlen(str) >= STRINGSZ-10)
break;
strcat(str, s);
lno = a[i].incl->line - 1; /* now print out start of this file */
}
if(n == 0)
strcat(str, "<eof>");
return fmtstrcpy(fp, str);
}
/* /*
s%,%,\n%g s%,%,\n%g
s%\n+%\n%g s%\n+%\n%g
......
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