2009-05-28 15:25:54 -06:00
|
|
|
// Derived from Inferno utils/5c/swt.c
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/5c/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.
|
|
|
|
|
2011-08-25 14:25:10 -06:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
2009-05-28 15:25:54 -06:00
|
|
|
#include "gg.h"
|
|
|
|
|
2011-04-09 07:44:20 -06:00
|
|
|
int
|
|
|
|
dsname(Sym *sym, int off, char *t, int n)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
p = gins(ADATA, N, N);
|
|
|
|
p->from.type = D_OREG;
|
|
|
|
p->from.name = D_EXTERN;
|
|
|
|
p->from.etype = TINT32;
|
|
|
|
p->from.offset = off;
|
|
|
|
p->from.reg = NREG;
|
2013-12-08 20:51:55 -07:00
|
|
|
p->from.sym = linksym(sym);
|
2011-04-09 07:44:20 -06:00
|
|
|
|
|
|
|
p->reg = n;
|
|
|
|
|
|
|
|
p->to.type = D_SCONST;
|
|
|
|
p->to.name = D_NONE;
|
|
|
|
p->to.reg = NREG;
|
|
|
|
p->to.offset = 0;
|
2012-12-13 12:20:24 -07:00
|
|
|
memmove(p->to.u.sval, t, n);
|
2011-04-09 07:44:20 -06:00
|
|
|
return off + n;
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:25:54 -06:00
|
|
|
/*
|
|
|
|
* make a refer to the data s, s+len
|
|
|
|
* emitting DATA if needed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
datastring(char *s, int len, Addr *a)
|
|
|
|
{
|
2011-04-09 07:44:20 -06:00
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
sym = stringsym(s, len);
|
|
|
|
a->type = D_OREG;
|
|
|
|
a->name = D_EXTERN;
|
|
|
|
a->etype = TINT32;
|
|
|
|
a->offset = widthptr+4; // skip header
|
|
|
|
a->reg = NREG;
|
2013-12-08 20:51:55 -07:00
|
|
|
a->sym = linksym(sym);
|
2011-10-03 15:46:36 -06:00
|
|
|
a->node = sym->def;
|
2009-05-28 15:25:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make a refer to the string sval,
|
|
|
|
* emitting DATA if needed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
datagostring(Strlit *sval, Addr *a)
|
|
|
|
{
|
2011-04-09 07:44:20 -06:00
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
sym = stringsym(sval->s, sval->len);
|
|
|
|
a->type = D_OREG;
|
|
|
|
a->name = D_EXTERN;
|
|
|
|
a->etype = TINT32;
|
|
|
|
a->offset = 0; // header
|
|
|
|
a->reg = NREG;
|
2013-12-08 20:51:55 -07:00
|
|
|
a->sym = linksym(sym);
|
2011-10-03 15:46:36 -06:00
|
|
|
a->node = sym->def;
|
2009-05-28 15:25:54 -06:00
|
|
|
}
|
|
|
|
|
2009-09-17 20:15:54 -06:00
|
|
|
void
|
|
|
|
gdata(Node *nam, Node *nr, int wid)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
vlong v;
|
|
|
|
|
2011-08-31 05:37:14 -06:00
|
|
|
if(nr->op == OLITERAL) {
|
|
|
|
switch(nr->val.ctype) {
|
|
|
|
case CTCPLX:
|
|
|
|
gdatacomplex(nam, nr->val.u.cval);
|
|
|
|
return;
|
|
|
|
case CTSTR:
|
|
|
|
gdatastring(nam, nr->val.u.sval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 20:15:54 -06:00
|
|
|
if(wid == 8 && is64(nr->type)) {
|
|
|
|
v = mpgetfix(nr->val.u.xval);
|
|
|
|
p = gins(ADATA, nam, nodintconst(v));
|
|
|
|
p->reg = 4;
|
|
|
|
p = gins(ADATA, nam, nodintconst(v>>32));
|
|
|
|
p->reg = 4;
|
|
|
|
p->from.offset += 4;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p = gins(ADATA, nam, nr);
|
|
|
|
p->reg = wid;
|
|
|
|
}
|
|
|
|
|
2010-03-20 19:50:01 -06:00
|
|
|
void
|
|
|
|
gdatacomplex(Node *nam, Mpcplx *cval)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
int w;
|
|
|
|
|
|
|
|
w = cplxsubtype(nam->type->etype);
|
|
|
|
w = types[w]->width;
|
|
|
|
|
|
|
|
p = gins(ADATA, nam, N);
|
2010-03-23 19:13:20 -06:00
|
|
|
p->reg = w;
|
2010-03-20 19:50:01 -06:00
|
|
|
p->to.type = D_FCONST;
|
2012-12-13 12:20:24 -07:00
|
|
|
p->to.u.dval = mpgetflt(&cval->real);
|
2010-03-20 19:50:01 -06:00
|
|
|
|
|
|
|
p = gins(ADATA, nam, N);
|
2010-03-23 19:13:20 -06:00
|
|
|
p->reg = w;
|
2010-03-20 19:50:01 -06:00
|
|
|
p->from.offset += w;
|
|
|
|
p->to.type = D_FCONST;
|
2012-12-13 12:20:24 -07:00
|
|
|
p->to.u.dval = mpgetflt(&cval->imag);
|
2010-03-20 19:50:01 -06:00
|
|
|
}
|
|
|
|
|
2009-09-17 20:15:54 -06:00
|
|
|
void
|
|
|
|
gdatastring(Node *nam, Strlit *sval)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
Node nod1;
|
|
|
|
|
|
|
|
p = gins(ADATA, nam, N);
|
|
|
|
datastring(sval->s, sval->len, &p->to);
|
|
|
|
p->reg = types[tptr]->width;
|
|
|
|
p->to.type = D_CONST;
|
|
|
|
p->to.etype = TINT32;
|
|
|
|
//print("%P\n", p);
|
|
|
|
|
|
|
|
nodconst(&nod1, types[TINT32], sval->len);
|
|
|
|
p = gins(ADATA, nam, &nod1);
|
|
|
|
p->reg = types[TINT32]->width;
|
|
|
|
p->from.offset += types[tptr]->width;
|
|
|
|
}
|
|
|
|
|
2009-05-28 15:25:54 -06:00
|
|
|
int
|
|
|
|
dstringptr(Sym *s, int off, char *str)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
off = rnd(off, widthptr);
|
|
|
|
p = gins(ADATA, N, N);
|
agen, sgen, cgen_callret, cgen_asop, D_ADDR handling, gmove
8bit and 16bit, some optoas, replaced Addr.index with
Addr.name
empty function compiles, mutex compiles
R=rsc
APPROVED=rsc
DELTA=908 (83 added, 41 deleted, 784 changed)
OCL=31127
CL=31188
2009-07-06 07:42:37 -06:00
|
|
|
p->from.type = D_OREG;
|
|
|
|
p->from.name = D_EXTERN;
|
2013-12-08 20:51:55 -07:00
|
|
|
p->from.sym = linksym(s);
|
2009-05-28 15:25:54 -06:00
|
|
|
p->from.offset = off;
|
2009-07-07 00:04:56 -06:00
|
|
|
p->reg = widthptr;
|
2009-05-28 15:25:54 -06:00
|
|
|
|
|
|
|
datastring(str, strlen(str)+1, &p->to);
|
2009-07-07 00:04:56 -06:00
|
|
|
p->to.type = D_CONST;
|
2009-05-28 15:25:54 -06:00
|
|
|
p->to.etype = TINT32;
|
|
|
|
off += widthptr;
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-13 23:06:28 -06:00
|
|
|
dgostrlitptr(Sym *s, int off, Strlit *lit)
|
2009-05-28 15:25:54 -06:00
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
2009-07-13 23:06:28 -06:00
|
|
|
if(lit == nil)
|
|
|
|
return duintptr(s, off, 0);
|
2009-05-28 15:25:54 -06:00
|
|
|
|
2009-07-13 23:06:28 -06:00
|
|
|
off = rnd(off, widthptr);
|
2009-05-28 15:25:54 -06:00
|
|
|
p = gins(ADATA, N, N);
|
agen, sgen, cgen_callret, cgen_asop, D_ADDR handling, gmove
8bit and 16bit, some optoas, replaced Addr.index with
Addr.name
empty function compiles, mutex compiles
R=rsc
APPROVED=rsc
DELTA=908 (83 added, 41 deleted, 784 changed)
OCL=31127
CL=31188
2009-07-06 07:42:37 -06:00
|
|
|
p->from.type = D_OREG;
|
|
|
|
p->from.name = D_EXTERN;
|
2013-12-08 20:51:55 -07:00
|
|
|
p->from.sym = linksym(s);
|
2009-05-28 15:25:54 -06:00
|
|
|
p->from.offset = off;
|
2009-09-18 00:08:15 -06:00
|
|
|
p->reg = widthptr;
|
2009-07-13 23:06:28 -06:00
|
|
|
datagostring(lit, &p->to);
|
2009-05-28 15:25:54 -06:00
|
|
|
p->to.type = D_CONST;
|
2009-07-13 23:06:28 -06:00
|
|
|
p->to.etype = TINT32;
|
|
|
|
off += widthptr;
|
2009-05-28 15:25:54 -06:00
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-13 23:06:28 -06:00
|
|
|
dgostringptr(Sym *s, int off, char *str)
|
2009-05-28 15:25:54 -06:00
|
|
|
{
|
2009-07-13 23:06:28 -06:00
|
|
|
int n;
|
|
|
|
Strlit *lit;
|
2009-05-28 15:25:54 -06:00
|
|
|
|
2009-07-13 23:06:28 -06:00
|
|
|
if(str == nil)
|
|
|
|
return duintptr(s, off, 0);
|
|
|
|
|
|
|
|
n = strlen(str);
|
|
|
|
lit = mal(sizeof *lit + n);
|
|
|
|
strcpy(lit->s, str);
|
|
|
|
lit->len = n;
|
|
|
|
return dgostrlitptr(s, off, lit);
|
2009-05-28 15:25:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-13 23:06:28 -06:00
|
|
|
dsymptr(Sym *s, int off, Sym *x, int xoff)
|
2009-05-28 15:25:54 -06:00
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
off = rnd(off, widthptr);
|
|
|
|
|
|
|
|
p = gins(ADATA, N, N);
|
agen, sgen, cgen_callret, cgen_asop, D_ADDR handling, gmove
8bit and 16bit, some optoas, replaced Addr.index with
Addr.name
empty function compiles, mutex compiles
R=rsc
APPROVED=rsc
DELTA=908 (83 added, 41 deleted, 784 changed)
OCL=31127
CL=31188
2009-07-06 07:42:37 -06:00
|
|
|
p->from.type = D_OREG;
|
|
|
|
p->from.name = D_EXTERN;
|
2013-12-08 20:51:55 -07:00
|
|
|
p->from.sym = linksym(s);
|
2009-05-28 15:25:54 -06:00
|
|
|
p->from.offset = off;
|
2009-07-07 00:04:56 -06:00
|
|
|
p->reg = widthptr;
|
|
|
|
p->to.type = D_CONST;
|
agen, sgen, cgen_callret, cgen_asop, D_ADDR handling, gmove
8bit and 16bit, some optoas, replaced Addr.index with
Addr.name
empty function compiles, mutex compiles
R=rsc
APPROVED=rsc
DELTA=908 (83 added, 41 deleted, 784 changed)
OCL=31127
CL=31188
2009-07-06 07:42:37 -06:00
|
|
|
p->to.name = D_EXTERN;
|
2013-12-08 20:51:55 -07:00
|
|
|
p->to.sym = linksym(x);
|
2009-07-13 23:06:28 -06:00
|
|
|
p->to.offset = xoff;
|
2009-05-28 15:25:54 -06:00
|
|
|
off += widthptr;
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nopout(Prog *p)
|
|
|
|
{
|
|
|
|
p->as = ANOP;
|
|
|
|
}
|