2008-06-04 15:37:38 -06:00
|
|
|
// Derived from Inferno utils/6c/swt.c
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/6c/swt.c
|
|
|
|
//
|
|
|
|
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
|
|
|
|
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
// Portions Copyright © 1997-1999 Vita Nuova Limited
|
|
|
|
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
|
|
|
|
// Portions Copyright © 2004,2006 Bruce Ellis
|
|
|
|
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
|
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
#include "gg.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
zname(Biobuf *b, Sym *s, int t)
|
|
|
|
{
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
Bputc(b, ANAME); /* as */
|
|
|
|
Bputc(b, ANAME>>8); /* as */
|
|
|
|
Bputc(b, t); /* type */
|
|
|
|
Bputc(b, s->sym); /* sym */
|
|
|
|
|
2009-05-08 16:40:31 -06:00
|
|
|
for(n=s->package; *n; n++)
|
2008-06-04 15:37:38 -06:00
|
|
|
Bputc(b, *n);
|
2008-06-24 18:16:06 -06:00
|
|
|
Bputdot(b);
|
2008-06-04 15:37:38 -06:00
|
|
|
for(n=s->name; *n; n++)
|
|
|
|
Bputc(b, *n);
|
|
|
|
Bputc(b, 0);
|
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
void
|
|
|
|
zfile(Biobuf *b, char *p, int n)
|
|
|
|
{
|
|
|
|
Bputc(b, ANAME);
|
|
|
|
Bputc(b, ANAME>>8);
|
|
|
|
Bputc(b, D_FILE);
|
|
|
|
Bputc(b, 1);
|
|
|
|
Bputc(b, '<');
|
|
|
|
Bwrite(b, p, n);
|
|
|
|
Bputc(b, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zhist(Biobuf *b, int line, vlong offset)
|
|
|
|
{
|
|
|
|
Addr a;
|
|
|
|
|
|
|
|
Bputc(b, AHISTORY);
|
|
|
|
Bputc(b, AHISTORY>>8);
|
|
|
|
Bputc(b, line);
|
|
|
|
Bputc(b, line>>8);
|
|
|
|
Bputc(b, line>>16);
|
|
|
|
Bputc(b, line>>24);
|
|
|
|
zaddr(b, &zprog.from, 0);
|
|
|
|
a = zprog.to;
|
|
|
|
if(offset != 0) {
|
|
|
|
a.offset = offset;
|
|
|
|
a.type = D_CONST;
|
|
|
|
}
|
|
|
|
zaddr(b, &a, 0);
|
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
|
|
|
zaddr(Biobuf *b, Addr *a, int s)
|
|
|
|
{
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 l;
|
2009-03-30 22:31:29 -06:00
|
|
|
uint64 e;
|
2008-06-04 15:37:38 -06:00
|
|
|
int i, t;
|
|
|
|
char *n;
|
|
|
|
|
|
|
|
t = 0;
|
|
|
|
if(a->index != D_NONE || a->scale != 0)
|
|
|
|
t |= T_INDEX;
|
|
|
|
if(s != 0)
|
|
|
|
t |= T_SYM;
|
|
|
|
|
|
|
|
switch(a->type) {
|
2008-11-18 20:24:37 -07:00
|
|
|
|
|
|
|
case D_BRANCH:
|
|
|
|
a->offset = a->branch->loc;
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
default:
|
|
|
|
t |= T_TYPE;
|
2008-11-18 20:24:37 -07:00
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
case D_NONE:
|
|
|
|
if(a->offset != 0) {
|
|
|
|
t |= T_OFFSET;
|
|
|
|
l = a->offset;
|
|
|
|
if((vlong)l != a->offset)
|
|
|
|
t |= T_64;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D_FCONST:
|
|
|
|
t |= T_FCONST;
|
|
|
|
break;
|
|
|
|
case D_SCONST:
|
|
|
|
t |= T_SCONST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Bputc(b, t);
|
|
|
|
|
|
|
|
if(t & T_INDEX) { /* implies index, scale */
|
|
|
|
Bputc(b, a->index);
|
|
|
|
Bputc(b, a->scale);
|
|
|
|
}
|
|
|
|
if(t & T_OFFSET) { /* implies offset */
|
|
|
|
l = a->offset;
|
|
|
|
Bputc(b, l);
|
|
|
|
Bputc(b, l>>8);
|
|
|
|
Bputc(b, l>>16);
|
|
|
|
Bputc(b, l>>24);
|
|
|
|
if(t & T_64) {
|
|
|
|
l = a->offset>>32;
|
|
|
|
Bputc(b, l);
|
|
|
|
Bputc(b, l>>8);
|
|
|
|
Bputc(b, l>>16);
|
|
|
|
Bputc(b, l>>24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(t & T_SYM) /* implies sym */
|
|
|
|
Bputc(b, s);
|
|
|
|
if(t & T_FCONST) {
|
|
|
|
ieeedtod(&e, a->dval);
|
2009-03-30 22:31:29 -06:00
|
|
|
l = e;
|
2008-06-04 15:37:38 -06:00
|
|
|
Bputc(b, l);
|
|
|
|
Bputc(b, l>>8);
|
|
|
|
Bputc(b, l>>16);
|
|
|
|
Bputc(b, l>>24);
|
2009-03-30 22:31:29 -06:00
|
|
|
l = e >> 32;
|
2008-06-04 15:37:38 -06:00
|
|
|
Bputc(b, l);
|
|
|
|
Bputc(b, l>>8);
|
|
|
|
Bputc(b, l>>16);
|
|
|
|
Bputc(b, l>>24);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(t & T_SCONST) {
|
|
|
|
n = a->sval;
|
|
|
|
for(i=0; i<NSNAME; i++) {
|
|
|
|
Bputc(b, *n);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(t & T_TYPE)
|
|
|
|
Bputc(b, a->type);
|
|
|
|
}
|
|
|
|
|
2008-06-13 19:16:23 -06:00
|
|
|
void
|
2009-03-30 22:31:29 -06:00
|
|
|
dumpfuncs(void)
|
2008-06-13 19:16:23 -06:00
|
|
|
{
|
2009-03-30 22:31:29 -06:00
|
|
|
Plist *pl;
|
|
|
|
int sf, st, t, sym;
|
|
|
|
struct { Sym *sym; short type; } h[NSYM];
|
|
|
|
Sym *s;
|
|
|
|
Prog *p;
|
2008-06-13 19:16:23 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
for(sym=0; sym<NSYM; sym++) {
|
|
|
|
h[sym].sym = S;
|
|
|
|
h[sym].type = 0;
|
|
|
|
}
|
|
|
|
sym = 1;
|
2008-06-13 19:16:23 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
// fix up pc
|
|
|
|
pcloc = 0;
|
|
|
|
for(pl=plist; pl!=nil; pl=pl->link) {
|
|
|
|
for(p=pl->firstpc; p!=P; p=p->link) {
|
|
|
|
p->loc = pcloc;
|
|
|
|
pcloc++;
|
|
|
|
}
|
2008-06-13 19:16:23 -06:00
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
// put out functions
|
|
|
|
for(pl=plist; pl!=nil; pl=pl->link) {
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
if(debug['S']) {
|
|
|
|
s = S;
|
|
|
|
if(pl->name != N)
|
|
|
|
s = pl->name->sym;
|
|
|
|
print("\n--- prog list \"%S\" ---\n", s);
|
|
|
|
for(p=pl->firstpc; p!=P; p=p->link)
|
|
|
|
print("%P\n", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(p=pl->firstpc; p!=P; p=p->link) {
|
|
|
|
jackpot:
|
|
|
|
sf = 0;
|
|
|
|
s = p->from.sym;
|
|
|
|
while(s != S) {
|
|
|
|
sf = s->sym;
|
|
|
|
if(sf < 0 || sf >= NSYM)
|
|
|
|
sf = 0;
|
|
|
|
t = p->from.type;
|
|
|
|
if(t == D_ADDR)
|
|
|
|
t = p->from.index;
|
|
|
|
if(h[sf].type == t)
|
|
|
|
if(h[sf].sym == s)
|
|
|
|
break;
|
|
|
|
s->sym = sym;
|
|
|
|
zname(bout, s, t);
|
|
|
|
h[sym].sym = s;
|
|
|
|
h[sym].type = t;
|
|
|
|
sf = sym;
|
|
|
|
sym++;
|
|
|
|
if(sym >= NSYM)
|
|
|
|
sym = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
st = 0;
|
|
|
|
s = p->to.sym;
|
|
|
|
while(s != S) {
|
|
|
|
st = s->sym;
|
|
|
|
if(st < 0 || st >= NSYM)
|
|
|
|
st = 0;
|
|
|
|
t = p->to.type;
|
|
|
|
if(t == D_ADDR)
|
|
|
|
t = p->to.index;
|
|
|
|
if(h[st].type == t)
|
|
|
|
if(h[st].sym == s)
|
|
|
|
break;
|
|
|
|
s->sym = sym;
|
|
|
|
zname(bout, s, t);
|
|
|
|
h[sym].sym = s;
|
|
|
|
h[sym].type = t;
|
|
|
|
st = sym;
|
|
|
|
sym++;
|
|
|
|
if(sym >= NSYM)
|
|
|
|
sym = 1;
|
|
|
|
if(st == sf)
|
|
|
|
goto jackpot;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Bputc(bout, p->as);
|
|
|
|
Bputc(bout, p->as>>8);
|
|
|
|
Bputc(bout, p->lineno);
|
|
|
|
Bputc(bout, p->lineno>>8);
|
|
|
|
Bputc(bout, p->lineno>>16);
|
|
|
|
Bputc(bout, p->lineno>>24);
|
|
|
|
zaddr(bout, &p->from, sf);
|
|
|
|
zaddr(bout, &p->to, st);
|
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
/* 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;
|
2009-05-06 18:06:06 -06:00
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
/*
|
2009-05-06 14:47:40 -06:00
|
|
|
* make a refer to the data s, s+len
|
|
|
|
* emitting DATA if needed.
|
2009-04-09 19:16:21 -06:00
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2009-05-06 14:47:40 -06:00
|
|
|
datastring(char *s, int len, Addr *a)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
int w;
|
|
|
|
Prog *p;
|
|
|
|
Addr ac, ao;
|
2009-05-06 14:47:40 -06:00
|
|
|
static int gen;
|
|
|
|
struct {
|
|
|
|
Strlit lit;
|
|
|
|
char buf[100];
|
|
|
|
} tmp;
|
2009-05-06 18:06:06 -06:00
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
// string
|
|
|
|
memset(&ao, 0, sizeof(ao));
|
|
|
|
ao.type = D_STATIC;
|
|
|
|
ao.index = D_NONE;
|
|
|
|
ao.etype = TINT32;
|
|
|
|
ao.offset = 0; // fill in
|
|
|
|
|
|
|
|
// constant
|
|
|
|
memset(&ac, 0, sizeof(ac));
|
|
|
|
ac.type = D_CONST;
|
|
|
|
ac.index = D_NONE;
|
|
|
|
ac.offset = 0; // fill in
|
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
// 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();
|
2008-06-04 15:37:38 -06:00
|
|
|
for(w=0; w<len; w+=8) {
|
|
|
|
p = pc;
|
|
|
|
gins(ADATA, N, N);
|
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
// DATA s+w, [NSNAME], $"xxx"
|
2008-06-04 15:37:38 -06:00
|
|
|
p->from = ao;
|
2009-05-06 14:47:40 -06:00
|
|
|
p->from.offset = w;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
p->from.scale = NSNAME;
|
|
|
|
if(w+8 > len)
|
|
|
|
p->from.scale = len-w;
|
|
|
|
|
|
|
|
p->to = ac;
|
|
|
|
p->to.type = D_SCONST;
|
|
|
|
p->to.offset = len;
|
|
|
|
memmove(p->to.sval, s+w, p->from.scale);
|
|
|
|
}
|
2009-05-06 14:47:40 -06:00
|
|
|
p = pc;
|
|
|
|
ggloblsym(ao.sym, len, ao.type == D_EXTERN);
|
|
|
|
if(ao.type == D_STATIC)
|
|
|
|
p->from.type = D_STATIC;
|
|
|
|
text();
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
/*
|
2009-05-06 14:47:40 -06:00
|
|
|
* make a refer to the string sval,
|
|
|
|
* emitting DATA if needed.
|
2009-04-09 19:16:21 -06:00
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2009-05-06 14:47:40 -06:00
|
|
|
datagostring(Strlit *sval, Addr *a)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
Prog *p;
|
2009-04-09 19:16:21 -06:00
|
|
|
Addr ac, ao, ap;
|
2009-05-06 14:47:40 -06:00
|
|
|
int32 wi, wp;
|
|
|
|
static int gen;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
memset(&ac, 0, sizeof(ac));
|
|
|
|
memset(&ao, 0, sizeof(ao));
|
2009-04-09 19:16:21 -06:00
|
|
|
memset(&ap, 0, sizeof(ap));
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
// constant
|
|
|
|
ac.type = D_CONST;
|
|
|
|
ac.index = D_NONE;
|
|
|
|
ac.offset = 0; // fill in
|
|
|
|
|
|
|
|
// string len+ptr
|
2009-05-06 14:47:40 -06:00
|
|
|
ao.type = D_STATIC; // fill in
|
2008-06-04 15:37:38 -06:00
|
|
|
ao.index = D_NONE;
|
|
|
|
ao.etype = TINT32;
|
2009-05-06 14:47:40 -06:00
|
|
|
ao.sym = nil; // fill in
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
// $string len+ptr
|
2009-05-06 14:47:40 -06:00
|
|
|
datastring(sval->s, sval->len, &ap);
|
|
|
|
ap.index = ap.type;
|
2009-04-09 19:16:21 -06:00
|
|
|
ap.type = D_ADDR;
|
|
|
|
ap.etype = TINT32;
|
|
|
|
|
|
|
|
wi = types[TUINT32]->width;
|
|
|
|
wp = types[tptr]->width;
|
2009-05-06 18:06:06 -06:00
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
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;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
2009-05-06 14:47:40 -06:00
|
|
|
|
|
|
|
*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;
|
2009-05-06 18:06:06 -06:00
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
p = pc;
|
|
|
|
ggloblsym(ao.sym, types[TSTRING]->width, ao.type == D_EXTERN);
|
|
|
|
if(ao.type == D_STATIC)
|
|
|
|
p->from.type = D_STATIC;
|
|
|
|
text();
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
int
|
|
|
|
dstringptr(Sym *s, int off, char *str)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
2009-03-30 22:31:29 -06:00
|
|
|
Prog *p;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
off = rnd(off, widthptr);
|
|
|
|
p = gins(ADATA, N, N);
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.index = D_NONE;
|
|
|
|
p->from.sym = s;
|
|
|
|
p->from.offset = off;
|
|
|
|
p->from.scale = widthptr;
|
2009-04-09 19:16:21 -06:00
|
|
|
|
2009-05-06 14:47:40 -06:00
|
|
|
datastring(str, strlen(str)+1, &p->to);
|
|
|
|
p->to.index = p->to.type;
|
2009-03-30 22:31:29 -06:00
|
|
|
p->to.type = D_ADDR;
|
|
|
|
p->to.etype = TINT32;
|
|
|
|
off += widthptr;
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
2008-10-24 21:14:28 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
int
|
|
|
|
duintxx(Sym *s, int off, uint64 v, int wid)
|
2008-10-24 21:14:28 -06:00
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
off = rnd(off, wid);
|
|
|
|
|
|
|
|
p = gins(ADATA, N, N);
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.index = D_NONE;
|
|
|
|
p->from.sym = s;
|
|
|
|
p->from.offset = off;
|
|
|
|
p->from.scale = wid;
|
|
|
|
p->to.type = D_CONST;
|
|
|
|
p->to.index = D_NONE;
|
|
|
|
p->to.offset = v;
|
|
|
|
off += wid;
|
|
|
|
|
|
|
|
return off;
|
2008-10-24 21:14:28 -06:00
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
int
|
|
|
|
duint32(Sym *s, int off, uint32 v)
|
2008-10-24 21:14:28 -06:00
|
|
|
{
|
2009-03-30 22:31:29 -06:00
|
|
|
return duintxx(s, off, v, 4);
|
|
|
|
}
|
2008-10-24 21:14:28 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
int
|
|
|
|
duint16(Sym *s, int off, uint32 v)
|
|
|
|
{
|
|
|
|
return duintxx(s, off, v, 2);
|
2008-10-24 21:14:28 -06:00
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
int
|
|
|
|
duintptr(Sym *s, int off, uint32 v)
|
|
|
|
{
|
|
|
|
return duintxx(s, off, v, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dsymptr(Sym *s, int off, Sym *x)
|
2008-10-24 21:14:28 -06:00
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
off = rnd(off, widthptr);
|
2008-10-24 21:14:28 -06:00
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
p = gins(ADATA, N, N);
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.index = D_NONE;
|
|
|
|
p->from.sym = s;
|
|
|
|
p->from.offset = off;
|
2008-10-24 21:14:28 -06:00
|
|
|
p->from.scale = widthptr;
|
2009-03-30 22:31:29 -06:00
|
|
|
p->to.type = D_ADDR;
|
|
|
|
p->to.index = D_EXTERN;
|
|
|
|
p->to.sym = x;
|
|
|
|
p->to.offset = 0;
|
|
|
|
off += widthptr;
|
|
|
|
|
|
|
|
return off;
|
2008-10-24 21:14:28 -06:00
|
|
|
}
|
|
|
|
|
2009-03-30 22:31:29 -06:00
|
|
|
|
2008-10-24 21:14:28 -06:00
|
|
|
void
|
2009-01-08 15:30:00 -07:00
|
|
|
genembedtramp(Type *t, Sig *b)
|
2008-10-24 21:14:28 -06:00
|
|
|
{
|
|
|
|
Sym *e;
|
2009-05-06 18:06:06 -06:00
|
|
|
int c, d, o, loaded;
|
2008-10-25 14:31:25 -06:00
|
|
|
Prog *p;
|
|
|
|
Type *f;
|
2008-10-24 21:14:28 -06:00
|
|
|
|
|
|
|
e = lookup(b->name);
|
|
|
|
for(d=0; d<nelem(dotlist); d++) {
|
2009-01-08 15:30:00 -07:00
|
|
|
c = adddot1(e, t, d, nil);
|
2008-10-24 21:14:28 -06:00
|
|
|
if(c == 1)
|
|
|
|
goto out;
|
|
|
|
}
|
2009-01-20 16:36:57 -07:00
|
|
|
fatal("genembedtramp %T.%s", t, b->name);
|
2008-10-24 21:14:28 -06:00
|
|
|
|
|
|
|
out:
|
2009-01-08 15:30:00 -07:00
|
|
|
// print("genembedtramp %d\n", d);
|
2008-10-25 14:31:25 -06:00
|
|
|
// print(" t = %lT\n", t);
|
|
|
|
// print(" name = %s\n", b->name);
|
|
|
|
// print(" sym = %S\n", b->sym);
|
|
|
|
// print(" hash = 0x%ux\n", b->hash);
|
2008-10-24 21:14:28 -06:00
|
|
|
|
2009-01-08 15:30:00 -07:00
|
|
|
newplist()->name = newname(b->sym);
|
|
|
|
|
2008-10-25 14:31:25 -06:00
|
|
|
//TEXT main·S_test2(SB),7,$0
|
|
|
|
p = pc;
|
|
|
|
gins(ATEXT, N, N);
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.sym = b->sym;
|
|
|
|
p->to.type = D_CONST;
|
|
|
|
p->to.offset = 0;
|
|
|
|
p->from.scale = 7;
|
|
|
|
//print("1. %P\n", p);
|
|
|
|
|
2009-05-06 18:06:06 -06:00
|
|
|
loaded = 0;
|
2008-10-25 14:31:25 -06:00
|
|
|
o = 0;
|
2008-10-24 21:14:28 -06:00
|
|
|
for(c=d-1; c>=0; c--) {
|
2008-10-25 14:31:25 -06:00
|
|
|
f = dotlist[c].field;
|
|
|
|
o += f->width;
|
|
|
|
if(!isptr[f->type->etype])
|
|
|
|
continue;
|
2009-05-06 18:06:06 -06:00
|
|
|
if(!loaded) {
|
|
|
|
loaded = 1;
|
|
|
|
//MOVQ 8(SP), AX
|
|
|
|
p = pc;
|
|
|
|
gins(AMOVQ, N, N);
|
|
|
|
p->from.type = D_INDIR+D_SP;
|
|
|
|
p->from.offset = 8;
|
|
|
|
p->to.type = D_AX;
|
|
|
|
//print("2. %P\n", p);
|
|
|
|
}
|
|
|
|
|
2008-10-25 14:31:25 -06:00
|
|
|
//MOVQ o(AX), AX
|
|
|
|
p = pc;
|
|
|
|
gins(AMOVQ, N, N);
|
|
|
|
p->from.type = D_INDIR+D_AX;
|
|
|
|
p->from.offset = o;
|
|
|
|
p->to.type = D_AX;
|
|
|
|
//print("3. %P\n", p);
|
|
|
|
o = 0;
|
|
|
|
}
|
|
|
|
if(o != 0) {
|
|
|
|
//ADDQ $XX, AX
|
|
|
|
p = pc;
|
|
|
|
gins(AADDQ, N, N);
|
|
|
|
p->from.type = D_CONST;
|
|
|
|
p->from.offset = o;
|
2009-05-06 18:06:06 -06:00
|
|
|
if(loaded)
|
|
|
|
p->to.type = D_AX;
|
|
|
|
else {
|
|
|
|
p->to.type = D_INDIR+D_SP;
|
|
|
|
p->to.offset = 8;
|
|
|
|
}
|
2008-10-25 14:31:25 -06:00
|
|
|
//print("4. %P\n", p);
|
2008-10-24 21:14:28 -06:00
|
|
|
}
|
|
|
|
|
2008-10-25 14:31:25 -06:00
|
|
|
//MOVQ AX, 8(SP)
|
2009-05-06 18:06:06 -06:00
|
|
|
if(loaded) {
|
|
|
|
p = pc;
|
|
|
|
gins(AMOVQ, N, N);
|
|
|
|
p->from.type = D_AX;
|
|
|
|
p->to.type = D_INDIR+D_SP;
|
|
|
|
p->to.offset = 8;
|
2008-10-25 14:31:25 -06:00
|
|
|
//print("5. %P\n", p);
|
2009-05-06 18:06:06 -06:00
|
|
|
} else {
|
|
|
|
// TODO(rsc): obviously this is unnecessary,
|
|
|
|
// but 6l has a bug, and it can't handle
|
|
|
|
// JMP instructions too close to the top of
|
|
|
|
// a new function.
|
|
|
|
p = pc;
|
|
|
|
gins(ANOP, N, N);
|
|
|
|
}
|
2008-10-25 14:31:25 -06:00
|
|
|
|
|
|
|
f = dotlist[0].field;
|
2009-01-08 15:30:00 -07:00
|
|
|
//JMP main·*Sub_test2(SB)
|
2008-10-25 14:31:25 -06:00
|
|
|
if(isptr[f->type->etype])
|
|
|
|
f = f->type;
|
|
|
|
p = pc;
|
|
|
|
gins(AJMP, N, N);
|
|
|
|
p->to.type = D_EXTERN;
|
2009-01-08 15:30:00 -07:00
|
|
|
p->to.sym = methodsym(lookup(b->name), ptrto(f->type));
|
2008-10-25 14:31:25 -06:00
|
|
|
//print("6. %P\n", p);
|
2009-01-08 15:30:00 -07:00
|
|
|
|
|
|
|
pc->as = ARET; // overwrite AEND
|
2008-10-24 21:14:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-30 22:31:29 -06:00
|
|
|
nopout(Prog *p)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
2009-03-30 22:31:29 -06:00
|
|
|
p->as = ANOP;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
2008-10-24 21:14:28 -06:00
|
|
|
|