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++)
}
}
+/* 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
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<len; w+=8) {
p = pc;
gins(ADATA, N, N);
- // .stringo<>+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)
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;
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
-
- wi = types[TINT32]->width;
+ ao.sym = nil; // fill in
+
+ // $string len+ptr
+ datastring(sval->s, sval->len, &ap);
+ ap.index = ap.type;
+ ap.type = D_ADDR;
+ ap.etype = TINT32;
+
+ wi = types[TUINT32]->width;
+ wp = types[tptr]->width;
+
+ 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;
+ }
- // lay out (count+string)
- for(l=poolist; l!=nil; l=l->link) {
+ *a = ao;
+ if(ao.sym->uniq)
+ return;
+ ao.sym->uniq = 1;
- p = pc;
- gins(ADATA, N, N);
+ data();
+ // DATA gostring, wp, $cstring
+ p = pc;
+ gins(ADATA, N, N);
+ p->from = ao;
+ p->from.scale = wp;
+ p->to = ap;
- // .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;
+ // 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;
- datastring(l->sval->s, l->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
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;
}