gobj.c 5.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// 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 <u.h>
#include <libc.h>
#include "gg.h"

int
dsname(Sym *s, int off, char *t, int n)
{
	Prog *p;

	p = gins(ADATA, N, N);
41 42
	p->from.type = TYPE_MEM;
	p->from.name = NAME_EXTERN;
43 44 45
	p->from.offset = off;
	p->from.sym = linksym(s);

46 47
	p->from3.type = TYPE_CONST;
	p->from3.offset = n;
48
	
49 50
	p->to.type = TYPE_SCONST;
	p->to.name = NAME_NONE;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	p->to.offset = 0;
	memmove(p->to.u.sval, t, n);
	return off + n;
}

/*
 * make a refer to the data s, s+len
 * emitting DATA if needed.
 */
void
datastring(char *s, int len, Addr *a)
{
	Sym *sym;
	
	sym = stringsym(s, len);
66 67
	a->type = TYPE_MEM;
	a->name = NAME_EXTERN;
68 69
	a->etype = simtype[TINT];
	a->offset = widthptr+widthint;  // skip header
70
	a->reg = 0;
71 72 73 74 75 76 77 78 79 80 81 82 83 84
	a->sym = linksym(sym);
	a->node = sym->def;
}

/*
 * make a refer to the string sval,
 * emitting DATA if needed.
 */
void
datagostring(Strlit *sval, Addr *a)
{
	Sym *sym;

	sym = stringsym(sval->s, sval->len);
85 86
	a->type = TYPE_MEM;
	a->name = NAME_EXTERN;
87
	a->sym = linksym(sym);
88
	a->reg = 0;
89 90
	a->node = sym->def;
	a->offset = 0;  // header
91
	a->etype = TSTRING;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

void
gdata(Node *nam, Node *nr, int wid)
{
	Prog *p;

	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;
		}
	}
	p = gins(ADATA, nam, nr);
110 111
	p->from3.type = TYPE_CONST;
	p->from3.offset = wid;
112 113 114 115 116 117 118 119 120 121 122 123
}

void
gdatacomplex(Node *nam, Mpcplx *cval)
{
	Prog *p;
	int w;

	w = cplxsubtype(nam->type->etype);
	w = types[w]->width;

	p = gins(ADATA, nam, N);
124 125
	p->from3.type = TYPE_CONST;
	p->from3.offset = w;
126
	p->to.type = TYPE_FCONST;
127 128 129
	p->to.u.dval = mpgetflt(&cval->real);

	p = gins(ADATA, nam, N);
130 131
	p->from3.type = TYPE_CONST;
	p->from3.offset = w;
132
	p->from.offset += w;
133
	p->to.type = TYPE_FCONST;
134 135 136 137 138 139 140 141 142 143 144
	p->to.u.dval = mpgetflt(&cval->imag);
}

void
gdatastring(Node *nam, Strlit *sval)
{
	Prog *p;
	Node nod1;

	p = gins(ADATA, nam, N);
	datastring(sval->s, sval->len, &p->to);
145 146
	p->from3.type = TYPE_CONST;
	p->from3.offset = types[tptr]->width;
147
	p->to.type = TYPE_CONST;
148 149 150 151
	p->to.etype = simtype[tptr];

	nodconst(&nod1, types[TINT], sval->len);
	p = gins(ADATA, nam, &nod1);
152 153
	p->from3.type = TYPE_CONST;
	p->from3.offset = widthint;
154 155 156 157 158 159 160 161 162 163
	p->from.offset += widthptr;
}

int
dstringptr(Sym *s, int off, char *str)
{
	Prog *p;

	off = rnd(off, widthptr);
	p = gins(ADATA, N, N);
164 165
	p->from.type = TYPE_MEM;
	p->from.name = NAME_EXTERN;
166 167
	p->from.sym = linksym(s);
	p->from.offset = off;
168 169
	p->from3.type = TYPE_CONST;
	p->from3.offset = widthptr;
170 171

	datastring(str, strlen(str)+1, &p->to);
172
	p->to.type = TYPE_CONST;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	p->to.etype = simtype[TINT];
	off += widthptr;

	return off;
}

int
dgostrlitptr(Sym *s, int off, Strlit *lit)
{
	Prog *p;

	if(lit == nil)
		return duintptr(s, off, 0);

	off = rnd(off, widthptr);
	p = gins(ADATA, N, N);
189 190
	p->from.type = TYPE_MEM;
	p->from.name = NAME_EXTERN;
191 192
	p->from.sym = linksym(s);
	p->from.offset = off;
193 194
	p->from3.type = TYPE_CONST;
	p->from3.offset = widthptr;
195
	datagostring(lit, &p->to);
196
	p->to.type = TYPE_CONST;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	p->to.etype = simtype[TINT];
	off += widthptr;

	return off;
}

int
dgostringptr(Sym *s, int off, char *str)
{
	int n;
	Strlit *lit;

	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);
}

int
dsymptr(Sym *s, int off, Sym *x, int xoff)
{
	Prog *p;

	off = rnd(off, widthptr);

	p = gins(ADATA, N, N);
227 228
	p->from.type = TYPE_MEM;
	p->from.name = NAME_EXTERN;
229 230
	p->from.sym = linksym(s);
	p->from.offset = off;
231 232
	p->from3.type = TYPE_CONST;
	p->from3.offset = widthptr;
233 234
	p->to.type = TYPE_CONST;
	p->to.name = NAME_EXTERN;
235 236 237 238 239 240 241 242 243 244 245
	p->to.sym = linksym(x);
	p->to.offset = xoff;
	off += widthptr;

	return off;
}

void
nopout(Prog *p)
{
	p->as = ANOP;
Russ Cox's avatar
Russ Cox committed
246 247 248 249
	p->from = zprog.from;
	p->from3 = zprog.from3;
	p->reg = zprog.reg;
	p->to = zprog.to;
250
}