gobj.c 10.9 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
// 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.

Russ Cox's avatar
Russ Cox committed
31 32
#include <u.h>
#include <libc.h>
33 34 35 36 37 38 39 40 41
#include "gg.h"

void
zname(Biobuf *b, Sym *s, int t)
{
	Bputc(b, ANAME);	/* as */
	Bputc(b, t);		/* type */
	Bputc(b, s->sym);	/* sym */

42
	Bputname(b, s);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

void
zfile(Biobuf *b, char *p, int n)
{
	Bputc(b, ANAME);
	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);
62 63
	Bputc(b, C_SCOND_NONE);
	Bputc(b, NREG);
64 65 66 67
	Bputc(b, line);
	Bputc(b, line>>8);
	Bputc(b, line>>16);
	Bputc(b, line>>24);
68
	zaddr(b, &zprog.from, 0, 0);
69 70 71 72 73
	a = zprog.to;
	if(offset != 0) {
		a.offset = offset;
		a.type = D_CONST;
	}
74
	zaddr(b, &a, 0, 0);
75 76 77
}

void
78
zaddr(Biobuf *b, Addr *a, int s, int gotype)
79 80
{
	int32 l;
81
	uint64 e;
82 83
	int i;
	char *n;
84

85 86 87
	switch(a->type) {
	case D_STATIC:
	case D_AUTO:
88
	case D_EXTERN:
89
	case D_PARAM:
90 91 92
		// TODO(kaib): remove once everything seems to work
		fatal("We should no longer generate these as types");

93 94 95 96
	default:
		Bputc(b, a->type);
		Bputc(b, a->reg);
		Bputc(b, s);
97
		Bputc(b, a->name);
98
		Bputc(b, gotype);
99
	}
100 101 102 103 104 105 106 107 108 109 110

	switch(a->type) {
	default:
		print("unknown type %d in zaddr\n", a->type);

	case D_NONE:
	case D_REG:
	case D_FREG:
	case D_PSR:
		break;

111 112 113 114 115 116
	case D_CONST2:
		l = a->offset2;
		Bputc(b, l);
		Bputc(b, l>>8);
		Bputc(b, l>>16);
		Bputc(b, l>>24); // fall through
117 118 119
	case D_OREG:
	case D_CONST:
	case D_SHIFT:
120 121
	case D_STATIC:
	case D_AUTO:
122 123
	case D_EXTERN:
	case D_PARAM:
124 125 126 127 128 129 130
		l = a->offset;
		Bputc(b, l);
		Bputc(b, l>>8);
		Bputc(b, l>>16);
		Bputc(b, l>>24);
		break;

131
	case D_BRANCH:
132
		if(a->u.branch == nil)
133
			fatal("unpatched branch");
134
		a->offset = a->u.branch->loc;
135 136 137 138 139 140 141
		l = a->offset;
		Bputc(b, l);
		Bputc(b, l>>8);
		Bputc(b, l>>16);
		Bputc(b, l>>24);
		break;

142
	case D_SCONST:
143
		n = a->u.sval;
144 145 146 147 148 149
		for(i=0; i<NSNAME; i++) {
			Bputc(b, *n);
			n++;
		}
		break;

Kai Backman's avatar
Kai Backman committed
150
	case D_REGREG:
151
	case D_REGREG2:
Kai Backman's avatar
Kai Backman committed
152 153 154
		Bputc(b, a->offset);
		break;

155
	case D_FCONST:
156
		ieeedtod(&e, a->u.dval);
157 158 159 160 161 162 163 164 165 166 167
		l = e;
		Bputc(b, l);
		Bputc(b, l>>8);
		Bputc(b, l>>16);
		Bputc(b, l>>24);
		l = e >> 32;
		Bputc(b, l);
		Bputc(b, l>>8);
		Bputc(b, l>>16);
		Bputc(b, l>>24);
		break;
168 169 170
	}
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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
static struct {
	struct { Sym *sym; short type; } h[NSYM];
	int sym;
} z;

static void
zsymreset(void)
{
	for(z.sym=0; z.sym<NSYM; z.sym++) {
		z.h[z.sym].sym = S;
		z.h[z.sym].type = 0;
	}
	z.sym = 1;
}

static int
zsym(Sym *s, int t, int *new)
{
	int i;

	*new = 0;
	if(s == S)
		return 0;

	i = s->sym;
	if(i < 0 || i >= NSYM)
		i = 0;
	if(z.h[i].type == t && z.h[i].sym == s)
		return i;
	i = z.sym;
	s->sym = i;
	zname(bout, s, t);
	z.h[i].sym = s;
	z.h[i].type = t;
	if(++z.sym >= NSYM)
		z.sym = 1;
	*new = 1;
	return i;
}

static int
zsymaddr(Addr *a, int *new)
{
	int t;

	t = a->name;
	if(t == D_ADDR)
		t = a->name;
	return zsym(a->sym, t, new);
}

222 223 224 225
void
dumpfuncs(void)
{
	Plist *pl;
226
	int sf, st, gf, gt, new;
227 228 229
	Sym *s;
	Prog *p;

230
	zsymreset();
231 232 233 234

	// fix up pc
	pcloc = 0;
	for(pl=plist; pl!=nil; pl=pl->link) {
Russ Cox's avatar
Russ Cox committed
235 236
		if(isblank(pl->name))
			continue;
237 238 239 240 241 242 243 244 245
		for(p=pl->firstpc; p!=P; p=p->link) {
			p->loc = pcloc;
			if(p->as != ADATA && p->as != AGLOBL)
				pcloc++;
		}
	}

	// put out functions
	for(pl=plist; pl!=nil; pl=pl->link) {
Russ Cox's avatar
Russ Cox committed
246 247
		if(isblank(pl->name))
			continue;
248

249 250
		// -S prints code; -SS prints code and data
		if(debug['S'] && (pl->name || debug['S']>1)) {
251 252 253 254 255 256 257 258 259
			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) {
260 261 262 263 264 265 266 267 268 269 270
			for(;;) {
				sf = zsymaddr(&p->from, &new);
				gf = zsym(p->from.gotype, D_EXTERN, &new);
				if(new && sf == gf)
					continue;
				st = zsymaddr(&p->to, &new);
				if(new && (st == sf || st == gf))
					continue;
				gt = zsym(p->to.gotype, D_EXTERN, &new);
				if(new && (gt == sf || gt == gf || gt == st))
					continue;
271 272
				break;
			}
273

274
			Bputc(bout, p->as);
275 276
			Bputc(bout, p->scond);
 			Bputc(bout, p->reg);
277 278 279 280
			Bputc(bout, p->lineno);
			Bputc(bout, p->lineno>>8);
			Bputc(bout, p->lineno>>16);
			Bputc(bout, p->lineno>>24);
281 282
			zaddr(bout, &p->from, sf, gf);
			zaddr(bout, &p->to, st, gt);
283 284 285 286
		}
	}
}

Russ Cox's avatar
Russ Cox committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
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;
	p->from.sym = sym;
	
	p->reg = n;
	
	p->to.type = D_SCONST;
	p->to.name = D_NONE;
	p->to.reg = NREG;
	p->to.offset = 0;
306
	memmove(p->to.u.sval, t, n);
Russ Cox's avatar
Russ Cox committed
307 308 309
	return off + n;
}

310 311 312 313 314 315 316
/*
 * make a refer to the data s, s+len
 * emitting DATA if needed.
 */
void
datastring(char *s, int len, Addr *a)
{
Russ Cox's avatar
Russ Cox committed
317 318 319 320 321 322 323 324 325
	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;
	a->sym = sym;
326
	a->node = sym->def;
327 328 329 330 331 332 333 334 335
}

/*
 * make a refer to the string sval,
 * emitting DATA if needed.
 */
void
datagostring(Strlit *sval, Addr *a)
{
Russ Cox's avatar
Russ Cox committed
336 337 338 339 340 341 342 343 344
	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;
	a->sym = sym;
345
	a->node = sym->def;
346 347
}

348 349 350 351 352 353
void
gdata(Node *nam, Node *nr, int wid)
{
	Prog *p;
	vlong v;

354 355 356 357 358 359 360 361 362 363 364
	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;
		}
	}

365 366 367 368 369 370 371 372 373 374 375 376 377
	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;
}

Ken Thompson's avatar
Ken Thompson committed
378 379 380 381 382 383 384 385 386 387
void
gdatacomplex(Node *nam, Mpcplx *cval)
{
	Prog *p;
	int w;

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

	p = gins(ADATA, nam, N);
Dean Prichard's avatar
Dean Prichard committed
388
	p->reg = w;
Ken Thompson's avatar
Ken Thompson committed
389
	p->to.type = D_FCONST;
390
	p->to.u.dval = mpgetflt(&cval->real);
Ken Thompson's avatar
Ken Thompson committed
391 392

	p = gins(ADATA, nam, N);
Dean Prichard's avatar
Dean Prichard committed
393
	p->reg = w;
Ken Thompson's avatar
Ken Thompson committed
394 395
	p->from.offset += w;
	p->to.type = D_FCONST;
396
	p->to.u.dval = mpgetflt(&cval->imag);
Ken Thompson's avatar
Ken Thompson committed
397 398
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
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;
}

418 419 420 421 422 423 424
int
dstringptr(Sym *s, int off, char *str)
{
	Prog *p;

	off = rnd(off, widthptr);
	p = gins(ADATA, N, N);
425 426
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
427 428
	p->from.sym = s;
	p->from.offset = off;
429
	p->reg = widthptr;
430 431

	datastring(str, strlen(str)+1, &p->to);
432
	p->to.type = D_CONST;
433 434 435 436 437 438 439
	p->to.etype = TINT32;
	off += widthptr;

	return off;
}

int
440
dgostrlitptr(Sym *s, int off, Strlit *lit)
441 442 443
{
	Prog *p;

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

447
	off = rnd(off, widthptr);
448
	p = gins(ADATA, N, N);
449 450
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
451 452
	p->from.sym = s;
	p->from.offset = off;
453
	p->reg = widthptr;
454
	datagostring(lit, &p->to);
455
	p->to.type = D_CONST;
456 457
	p->to.etype = TINT32;
	off += widthptr;
458 459 460 461 462

	return off;
}

int
463
dgostringptr(Sym *s, int off, char *str)
464
{
465 466
	int n;
	Strlit *lit;
467

468 469 470 471 472 473 474 475
	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);
476 477 478
}

int
479
duintxx(Sym *s, int off, uint64 v, int wid)
480
{
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	Prog *p;

	off = rnd(off, wid);

	p = gins(ADATA, N, N);
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
	p->from.sym = s;
	p->from.offset = off;
	p->reg = wid;
	p->to.type = D_CONST;
	p->to.name = D_NONE;
	p->to.offset = v;
	off += wid;

	return off;
497 498 499
}

int
500
dsymptr(Sym *s, int off, Sym *x, int xoff)
501 502 503 504 505 506
{
	Prog *p;

	off = rnd(off, widthptr);

	p = gins(ADATA, N, N);
507 508
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
509 510
	p->from.sym = s;
	p->from.offset = off;
511 512
	p->reg = widthptr;
	p->to.type = D_CONST;
513
	p->to.name = D_EXTERN;
514
	p->to.sym = x;
515
	p->to.offset = xoff;
516 517 518 519 520 521 522
	off += widthptr;

	return off;
}


void
Russ Cox's avatar
Russ Cox committed
523
genembedtramp(Type *rcvr, Type *method, Sym *newnam, int iface)
524
{
525
	// TODO(kaib): re-implement genembedtramp
Russ Cox's avatar
Russ Cox committed
526
	genwrapper(rcvr, method, newnam, iface);
527
/*
Kai Backman's avatar
Kai Backman committed
528 529 530 531 532 533 534
	Sym *e;
	int c, d, o;
	Prog *p;
	Type *f;

	e = method->sym;
	for(d=0; d<nelem(dotlist); d++) {
535
		c = adddot1(e, rcvr, d, nil, 0);
Kai Backman's avatar
Kai Backman committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
		if(c == 1)
			goto out;
	}
	fatal("genembedtramp %T.%S", rcvr, method->sym);

out:
	newplist()->name = newname(newnam);

	//TEXT	main·S_test2(SB),7,$0
	p = pc;
	gins(ATEXT, N, N);
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
	p->from.sym = newnam;
	p->to.type = D_CONST2;
	p->reg = 7;
	p->to.offset2 = 0;
	p->to.reg = NREG;
554
//print("1. %P\n", p);
Kai Backman's avatar
Kai Backman committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

	o = 0;
	for(c=d-1; c>=0; c--) {
		f = dotlist[c].field;
		o += f->width;
		if(!isptr[f->type->etype])
			continue;

		//MOVW	o(R0), R0
		p = pc;
		gins(AMOVW, N, N);
		p->from.type = D_OREG;
		p->from.reg = REGARG;
		p->from.offset = o;
		p->to.type = D_REG;
		p->to.reg = REGARG;
571
//print("2. %P\n", p);
Kai Backman's avatar
Kai Backman committed
572 573 574 575 576 577 578 579 580 581 582
		o = 0;
	}
	if(o != 0) {
		//MOVW	$XX(R0), R0
		p = pc;
		gins(AMOVW, N, N);
		p->from.type = D_CONST;
		p->from.reg = REGARG;
		p->from.offset = o;
		p->to.type = D_REG;
		p->to.reg = REGARG;
583
//print("3. %P\n", p);
Kai Backman's avatar
Kai Backman committed
584 585 586 587 588 589 590 591 592 593 594
	}

	f = dotlist[0].field;
	//B	main·*Sub_test2(SB)
	if(isptr[f->type->etype])
		f = f->type;
	p = pc;
	gins(AB, N, N);
	p->to.type = D_OREG;
	p->to.reg = NREG;
	p->to.name = D_EXTERN;
595
	p->to.sym = methodsym(method->sym, ptrto(f->type), 0);
596
//print("4. %P\n", p);
Kai Backman's avatar
Kai Backman committed
597 598

	pc->as = ARET;	// overwrite AEND
599
*/
600 601 602 603 604 605 606
}

void
nopout(Prog *p)
{
	p->as = ANOP;
}