From 021abfbd28aca7d6699f6b16ef46aa44692fa928 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 26 May 2009 14:46:06 -0700 Subject: [PATCH] 8g: hello world works again * string format changed * files got renamed * new files that i forgot to check in last time updates are all copy and paste from 6g R=ken OCL=29385 CL=29400 --- src/cmd/8g/Makefile | 6 +- src/cmd/8g/{align.c => galign.c} | 0 src/cmd/8g/gg.h | 3 +- src/cmd/8g/{gen.c => ggen.c} | 5 + src/cmd/8g/{obj.c => gobj.c} | 187 +++++++++++++++++++++++++------ src/cmd/gc/go.h | 1 + src/runtime/386/traceback.c | 4 +- 7 files changed, 164 insertions(+), 42 deletions(-) rename src/cmd/8g/{align.c => galign.c} (100%) rename src/cmd/8g/{gen.c => ggen.c} (99%) rename src/cmd/8g/{obj.c => gobj.c} (75%) diff --git a/src/cmd/8g/Makefile b/src/cmd/8g/Makefile index 485cc56206b..78e0ee4ec46 100644 --- a/src/cmd/8g/Makefile +++ b/src/cmd/8g/Makefile @@ -16,9 +16,9 @@ HFILES=\ OFILES=\ ../8l/enam.$O\ list.$O\ - align.$O\ - obj.$O\ - gen.$O\ + galign.$O\ + gobj.$O\ + ggen.$O\ gsubr.$O\ cgen.$O\ # peep.$O\ diff --git a/src/cmd/8g/align.c b/src/cmd/8g/galign.c similarity index 100% rename from src/cmd/8g/align.c rename to src/cmd/8g/galign.c diff --git a/src/cmd/8g/gg.h b/src/cmd/8g/gg.h index c7be24decb3..b3e35ea0595 100644 --- a/src/cmd/8g/gg.h +++ b/src/cmd/8g/gg.h @@ -47,7 +47,7 @@ EXTERN Biobuf* bout; EXTERN int32 dynloc; EXTERN uchar reg[D_NONE]; EXTERN int32 pcloc; // instruction counter -EXTERN String emptystring; +EXTERN Strlit emptystring; extern char* anames[]; EXTERN Hist* hist; EXTERN Prog zprog; @@ -124,6 +124,7 @@ void nodreg(Node*, Type*, int); void nodindreg(Node*, Type*, int); void nodconst(Node*, Type*, vlong); void gconreg(int, vlong, int); +void datagostring(Strlit*, Addr*); void buildtxt(void); Plist* newplist(void); int isfat(Type*); diff --git a/src/cmd/8g/gen.c b/src/cmd/8g/ggen.c similarity index 99% rename from src/cmd/8g/gen.c rename to src/cmd/8g/ggen.c index d610dc0d288..04b0224948e 100644 --- a/src/cmd/8g/gen.c +++ b/src/cmd/8g/ggen.c @@ -327,3 +327,8 @@ cgen_bmul(int op, Node *nl, Node *nr, Node *res) fatal("cgen_bmul"); } +int +gen_as_init(Node *nr, Node *nl) +{ + return 0; +} diff --git a/src/cmd/8g/obj.c b/src/cmd/8g/gobj.c similarity index 75% rename from src/cmd/8g/obj.c rename to src/cmd/8g/gobj.c index 79d1cf037d7..e080d8aea6a 100644 --- a/src/cmd/8g/obj.c +++ b/src/cmd/8g/gobj.c @@ -40,7 +40,7 @@ zname(Biobuf *b, Sym *s, int t) Bputc(b, t); /* type */ Bputc(b, s->sym); /* sym */ - for(n=s->opackage; *n; n++) + for(n=s->package; *n; n++) Bputc(b, *n); Bputdot(b); for(n=s->name; *n; n++) @@ -259,19 +259,75 @@ dumpfuncs(void) } } +/* deferred DATA output */ +static Prog *strdat; +static Prog *estrdat; +static int gflag; +static Prog *savepc; + +static void +data(void) +{ + gflag = debug['g']; + debug['g'] = 0; + + if(estrdat == nil) { + strdat = mal(sizeof(*pc)); + clearp(strdat); + estrdat = strdat; + } + if(savepc) + fatal("data phase error"); + savepc = pc; + pc = estrdat; +} + +static void +text(void) +{ + if(!savepc) + fatal("text phase error"); + debug['g'] = gflag; + estrdat = pc; + pc = savepc; + savepc = nil; +} + void -datastring(char *s, int len) +dumpdata(void) +{ + Prog *p; + + if(estrdat == nil) + return; + *pc = *strdat; + if(gflag) + for(p=pc; p!=estrdat; p=p->link) + print("%P\n", p); + pc = estrdat; +} + +/* + * make a refer to the data s, s+len + * emitting DATA if needed. + */ +void +datastring(char *s, int len, Addr *a) { int w; Prog *p; Addr ac, ao; + static int gen; + struct { + Strlit lit; + char buf[100]; + } tmp; // string memset(&ao, 0, sizeof(ao)); ao.type = D_STATIC; ao.index = D_NONE; ao.etype = TINT32; - ao.sym = symstringo; ao.offset = 0; // fill in // constant @@ -280,13 +336,37 @@ datastring(char *s, int len) ac.index = D_NONE; ac.offset = 0; // fill in + // huge strings are made static to avoid long names. + if(len > 100) { + snprint(namebuf, sizeof(namebuf), ".string.%d", gen++); + ao.sym = lookup(namebuf); + ao.type = D_STATIC; + } else { + if(len > 0 && s[len-1] == '\0') + len--; + tmp.lit.len = len; + memmove(tmp.lit.s, s, len); + tmp.lit.s[len] = '\0'; + len++; + snprint(namebuf, sizeof(namebuf), "\"%Z\"", &tmp.lit); + ao.sym = pkglookup(namebuf, "string"); + ao.type = D_EXTERN; + } + *a = ao; + + // only generate data the first time. + if(ao.sym->uniq) + return; + ao.sym->uniq = 1; + + data(); for(w=0; w+oo, [NSNAME], $"xxx" + // DATA s+w, [NSNAME], $"xxx" p->from = ao; - p->from.offset = stringo; + p->from.offset = w; p->from.scale = NSNAME; if(w+8 > len) @@ -296,23 +376,29 @@ datastring(char *s, int len) p->to.type = D_SCONST; p->to.offset = len; memmove(p->to.sval, s+w, p->from.scale); - stringo += p->from.scale; } + p = pc; + ggloblsym(ao.sym, len, ao.type == D_EXTERN); + if(ao.type == D_STATIC) + p->from.type = D_STATIC; + text(); } +/* + * make a refer to the string sval, + * emitting DATA if needed. + */ void -dumpstrings(void) +datagostring(Strlit *sval, Addr *a) { - Pool *l; Prog *p; - Addr ac, ao; - int32 wi; - - if(poolist == nil) - return; + Addr ac, ao, ap; + int32 wi, wp; + static int gen; memset(&ac, 0, sizeof(ac)); memset(&ao, 0, sizeof(ao)); + memset(&ap, 0, sizeof(ap)); // constant ac.type = D_CONST; @@ -320,31 +406,61 @@ dumpstrings(void) ac.offset = 0; // fill in // string len+ptr - ao.type = D_STATIC; + ao.type = D_STATIC; // fill in ao.index = D_NONE; ao.etype = TINT32; - ao.sym = symstringo; - ao.offset = 0; // fill in + ao.sym = nil; // fill in - wi = types[TINT32]->width; + // $string len+ptr + datastring(sval->s, sval->len, &ap); + ap.index = ap.type; + ap.type = D_ADDR; + ap.etype = TINT32; - // lay out (count+string) - for(l=poolist; l!=nil; l=l->link) { + wi = types[TUINT32]->width; + wp = types[tptr]->width; - p = pc; - gins(ADATA, N, N); - - // .stringo<>+xx, wi, $len - stringo = rnd(stringo, wi); - p->from = ao; - p->from.offset = stringo; - p->from.scale = wi; - p->to = ac; - p->to.offset = l->sval->len; - stringo += wi; - - datastring(l->sval->s, l->sval->len); + if(ap.index == D_STATIC) { + // huge strings are made static to avoid long names + snprint(namebuf, sizeof(namebuf), ".gostring.%d", ++gen); + ao.sym = lookup(namebuf); + ao.type = D_STATIC; + } else { + // small strings get named by their contents, + // so that multiple modules using the same string + // can share it. + snprint(namebuf, sizeof(namebuf), "\"%Z\"", sval); + ao.sym = pkglookup(namebuf, "go.string"); + ao.type = D_EXTERN; } + + *a = ao; + if(ao.sym->uniq) + return; + ao.sym->uniq = 1; + + data(); + // DATA gostring, wp, $cstring + p = pc; + gins(ADATA, N, N); + p->from = ao; + p->from.scale = wp; + p->to = ap; + + // DATA gostring+wp, wi, $len + p = pc; + gins(ADATA, N, N); + p->from = ao; + p->from.offset = wp; + p->from.scale = wi; + p->to = ac; + p->to.offset = sval->len; + + p = pc; + ggloblsym(ao.sym, types[TSTRING]->width, ao.type == D_EXTERN); + if(ao.type == D_STATIC) + p->from.type = D_STATIC; + text(); } int @@ -359,14 +475,13 @@ dstringptr(Sym *s, int off, char *str) p->from.sym = s; p->from.offset = off; p->from.scale = widthptr; + + datastring(str, strlen(str)+1, &p->to); + p->to.index = p->to.type; p->to.type = D_ADDR; - p->to.index = D_STATIC; p->to.etype = TINT32; - p->to.sym = symstringo; - p->to.offset = stringo; off += widthptr; - datastring(str, strlen(str)+1); return off; } diff --git a/src/cmd/gc/go.h b/src/cmd/gc/go.h index c5d28cca7ea..373b5140b9b 100644 --- a/src/cmd/gc/go.h +++ b/src/cmd/gc/go.h @@ -229,6 +229,7 @@ struct Node int32 vargen; // unique name for OTYPE/ONAME int32 lineno; vlong xoffset; + int32 ostk; }; #define N ((Node*)0) diff --git a/src/runtime/386/traceback.c b/src/runtime/386/traceback.c index 23e92d8926b..2d5714e24ff 100644 --- a/src/runtime/386/traceback.c +++ b/src/runtime/386/traceback.c @@ -82,7 +82,7 @@ traceback(byte *pc0, byte *sp, G *g) // func caller(n int) (pc uint64, file string, line int, ok bool) void -runtime·Caller(int32 n, uint64 retpc, string retfile, int32 retline, bool retbool) +runtime·Caller(int32 n, uint64 retpc, String retfile, int32 retline, bool retbool) { uint64 pc; byte *sp; @@ -97,7 +97,7 @@ runtime·Caller(int32 n, uint64 retpc, string retfile, int32 retline, bool retbo error: retpc = 0; retline = 0; - retfile = nil; + retfile = emptystring; retbool = false; FLUSH(&retpc); FLUSH(&retfile);