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

#include "gg.h"

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

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

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);
60 61
	Bputc(b, C_SCOND_NONE);
	Bputc(b, NREG);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	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);
}

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

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

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

	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;

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

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

139 140 141 142 143 144 145 146
	case D_SCONST:
		n = a->sval;
		for(i=0; i<NSNAME; i++) {
			Bputc(b, *n);
			n++;
		}
		break;

Kai Backman's avatar
Kai Backman committed
147 148 149 150
	case D_REGREG:
		Bputc(b, a->offset);
		break;

151
	case D_FCONST:
152 153 154 155 156 157 158 159 160 161 162 163
		ieeedtod(&e, a->dval);
		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;
164 165 166 167 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
	}
}

void
dumpfuncs(void)
{
	Plist *pl;
	int sf, st, t, sym;
	struct { Sym *sym; short type; } h[NSYM];
	Sym *s;
	Prog *p;

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

	// 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;
			if(p->as != ADATA && p->as != AGLOBL)
				pcloc++;
		}
	}

	// put out functions
	for(pl=plist; pl!=nil; pl=pl->link) {

		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;
212
				t = p->from.name;
213
				if(t == D_ADDR)
214
					t = p->from.name;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
				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;
234
				t = p->to.name;
235
				if(t == D_ADDR)
236
					t = p->to.name;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
				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);
253 254
			Bputc(bout, p->scond);
 			Bputc(bout, p->reg);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			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);
		}
	}
}

/* deferred DATA output */
static Prog *strdat;
static Prog *estrdat;
static int gflag;
static Prog *savepc;

Russ Cox's avatar
Russ Cox committed
271
void
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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;
}

Russ Cox's avatar
Russ Cox committed
288
void
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
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;
}

Russ Cox's avatar
Russ Cox committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
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;
	memmove(p->to.sval, t, n);
	return off + n;
}

336 337 338 339 340 341 342
/*
 * 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
343 344 345 346 347 348 349 350 351
	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;
352 353 354 355 356 357 358 359 360
}

/*
 * make a refer to the string sval,
 * emitting DATA if needed.
 */
void
datagostring(Strlit *sval, Addr *a)
{
Russ Cox's avatar
Russ Cox committed
361 362 363 364 365 366 367 368 369
	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;
370 371
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
void
gdata(Node *nam, Node *nr, int wid)
{
	Prog *p;
	vlong v;

	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
391 392 393 394 395 396 397 398 399 400
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
401
	p->reg = w;
Ken Thompson's avatar
Ken Thompson committed
402 403 404 405
	p->to.type = D_FCONST;
	p->to.dval = mpgetflt(&cval->real);

	p = gins(ADATA, nam, N);
Dean Prichard's avatar
Dean Prichard committed
406
	p->reg = w;
Ken Thompson's avatar
Ken Thompson committed
407 408 409 410 411
	p->from.offset += w;
	p->to.type = D_FCONST;
	p->to.dval = mpgetflt(&cval->imag);
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
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;
}

431 432 433 434 435 436 437
int
dstringptr(Sym *s, int off, char *str)
{
	Prog *p;

	off = rnd(off, widthptr);
	p = gins(ADATA, N, N);
438 439
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
440 441
	p->from.sym = s;
	p->from.offset = off;
442
	p->reg = widthptr;
443 444

	datastring(str, strlen(str)+1, &p->to);
445
	p->to.type = D_CONST;
446 447 448 449 450 451 452
	p->to.etype = TINT32;
	off += widthptr;

	return off;
}

int
453
dgostrlitptr(Sym *s, int off, Strlit *lit)
454 455 456
{
	Prog *p;

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

460
	off = rnd(off, widthptr);
461
	p = gins(ADATA, N, N);
462 463
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
464 465
	p->from.sym = s;
	p->from.offset = off;
466
	p->reg = widthptr;
467
	datagostring(lit, &p->to);
468
	p->to.type = D_CONST;
469 470
	p->to.etype = TINT32;
	off += widthptr;
471 472 473 474 475

	return off;
}

int
476
dgostringptr(Sym *s, int off, char *str)
477
{
478 479
	int n;
	Strlit *lit;
480

481 482 483 484 485 486 487 488
	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);
489 490 491
}

int
492
duintxx(Sym *s, int off, uint64 v, int wid)
493
{
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	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;
510 511 512
}

int
513
dsymptr(Sym *s, int off, Sym *x, int xoff)
514 515 516 517 518 519
{
	Prog *p;

	off = rnd(off, widthptr);

	p = gins(ADATA, N, N);
520 521
	p->from.type = D_OREG;
	p->from.name = D_EXTERN;
522 523
	p->from.sym = s;
	p->from.offset = off;
524 525
	p->reg = widthptr;
	p->to.type = D_CONST;
526
	p->to.name = D_EXTERN;
527
	p->to.sym = x;
528
	p->to.offset = xoff;
529 530 531 532 533 534 535
	off += widthptr;

	return off;
}


void
Russ Cox's avatar
Russ Cox committed
536
genembedtramp(Type *rcvr, Type *method, Sym *newnam, int iface)
537
{
538
	// TODO(kaib): re-implement genembedtramp
Russ Cox's avatar
Russ Cox committed
539
	genwrapper(rcvr, method, newnam, iface);
540
/*
Kai Backman's avatar
Kai Backman committed
541 542 543 544 545 546 547
	Sym *e;
	int c, d, o;
	Prog *p;
	Type *f;

	e = method->sym;
	for(d=0; d<nelem(dotlist); d++) {
548
		c = adddot1(e, rcvr, d, nil, 0);
Kai Backman's avatar
Kai Backman committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		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;
567
//print("1. %P\n", p);
Kai Backman's avatar
Kai Backman committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

	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;
584
//print("2. %P\n", p);
Kai Backman's avatar
Kai Backman committed
585 586 587 588 589 590 591 592 593 594 595
		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;
596
//print("3. %P\n", p);
Kai Backman's avatar
Kai Backman committed
597 598 599 600 601 602 603 604 605 606 607
	}

	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;
608
	p->to.sym = methodsym(method->sym, ptrto(f->type), 0);
609
//print("4. %P\n", p);
Kai Backman's avatar
Kai Backman committed
610 611

	pc->as = ARET;	// overwrite AEND
612
*/
613 614 615 616 617 618 619
}

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