summaryrefslogtreecommitdiff
path: root/src/runtime/iface.c
blob: 42a572f3515a208f4c60d428514457a448c6c77d (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "runtime.h"

int32	iface_debug	= 0;

typedef	struct	Sigt	Sigt;
typedef	struct	Sigi	Sigi;
typedef	struct	Itype	Itype;

/*
 * the layout of Iface, Sigt and Sigi are known to the compiler
 */
struct	Sigt
{
	byte*	name;                   // name of basic type
	Sigt*	link;			// for linking into hash tables
	uint32	thash;                  // hash of type
	uint32	mhash;                  // hash of methods
	uint16	width;			// width of base type in bytes
	uint16	alg;			// algorithm
	// note: on amd64 there is a 32-bit pad here.
	struct {
		byte*	fname;
		uint32	fhash;		// hash of type
		uint32	offset;		// offset of substruct
		void	(*fun)(void);
	} meth[1];			// one or more - last name is nil
};

struct	Sigi
{
	byte*	name;
	uint32	hash;
	uint32	size;			// number of methods
	struct {
		byte*	fname;
		uint32	fhash;
		uint32	perm;		// location of fun in Sigt
	} meth[1];			// [size+1] - last name is nil
};

struct	Itype
{
	Sigi*	sigi;
	Sigt*	sigt;
	Itype*	link;
	int32	bad;
	int32	unused;
	void	(*fun[])(void);
};

static	Iface	niliface;
static	Itype*	hash[1009];
static	Lock	ifacelock;

Sigi	sigi·empty[2] =	{ (byte*)"interface { }" };

static void
printsigi(Sigi *si)
{
	int32 i;
	byte *name;

	sys·printpointer(si);
	prints("{");
	prints((int8*)si->name);
	prints(":");
	for(i=0;; i++) {
		name = si->meth[i].fname;
		if(name == nil)
			break;
		prints("[");
		sys·printint(i);
		prints("]\"");
		prints((int8*)name);
		prints("\"");
		sys·printint(si->meth[i].fhash%999);
		prints("/");
		sys·printint(si->meth[i].perm);
	}
	prints("}");
}

static void
printsigt(Sigt *st)
{
	int32 i;
	byte *name;

	sys·printpointer(st);
	prints("{");
	prints((int8*)st->name);
	prints(":");
	sys·printint(st->thash%999);	// type hash
	prints(",");
	sys·printint(st->mhash%999);	// method hash
	prints(",");
	sys·printint(st->width);	// width
	prints(",");
	sys·printint(st->alg);	// algorithm
	for(i=0;; i++) {
		name = st->meth[i].fname;
		if(name == nil)
			break;
		prints("[");
		sys·printint(i);
		prints("]\"");
		prints((int8*)name);
		prints("\"");
		sys·printint(st->meth[i].fhash%999);
		prints("/");
		sys·printint(st->meth[i].offset);
		prints("/");
		sys·printpointer(st->meth[i].fun);
	}
	prints("}");
}

static void
printiface(Iface i)
{
	prints("(");
	sys·printpointer(i.type);
	prints(",");
	sys·printpointer(i.data);
	prints(")");
}

static Itype*
itype(Sigi *si, Sigt *st, int32 canfail)
{
	int32 locked;
	int32 nt, ni;
	uint32 ihash, h;
	byte *sname, *iname;
	Itype *m;

	// compiler has provided some good hash codes for us.
	h = 0;
	if(si)
		h += si->hash;
	if(st) {
		h += st->thash;
		h += st->mhash;
	}

	h %= nelem(hash);

	// look twice - once without lock, once with.
	// common case will be no lock contention.
	for(locked=0; locked<2; locked++) {
		if(locked)
			lock(&ifacelock);
		for(m=hash[h]; m!=nil; m=m->link) {
			if(m->sigi == si && m->sigt == st) {
				if(m->bad) {
					m = nil;
					if(!canfail) {
						// this can only happen if the conversion
						// was already done once using the , ok form
						// and we have a cached negative result.
						// the cached result doesn't record which
						// interface function was missing, so jump
						// down to the interface check, which will
						// give a better error.
						goto throw;
					}
				}
				// prints("old itype\n");
				if(locked)
					unlock(&ifacelock);
				return m;
			}
		}
	}

	ni = si->size;
	m = mal(sizeof(*m) + ni*sizeof(m->fun[0]));
	m->sigi = si;
	m->sigt = st;

throw:
	nt = 0;
	for(ni=0;; ni++) {
		iname = si->meth[ni].fname;
		if(iname == nil)
			break;

		// pick up next name from
		// interface signature
		ihash = si->meth[ni].fhash;

		for(;; nt++) {
			// pick up and compare next name
			// from structure signature
			sname = st->meth[nt].fname;
			if(sname == nil) {
				if(!canfail) {
					printf("cannot convert type %s to interface %s: missing method %s\n",
						st->name, si->name, iname);
					if(iface_debug) {
						prints("interface");
						printsigi(si);
						prints("\ntype");
						printsigt(st);
						prints("\n");
					}
					throw("interface conversion");
				}
				m->bad = 1;
				m->link = hash[h];
				hash[h] = m;
				if(locked)
					unlock(&ifacelock);
				return nil;
			}
			if(ihash == st->meth[nt].fhash && strcmp(sname, iname) == 0)
				break;
		}
		m->fun[si->meth[ni].perm] = st->meth[nt].fun;
	}
	m->link = hash[h];
	hash[h] = m;
	if(locked)
		unlock(&ifacelock);

	// printf("new itype %p\n", m);
	return m;
}

// ifaceT2I(sigi *byte, sigt *byte, elem any) (ret any);
void
sys·ifaceT2I(Sigi *si, Sigt *st, ...)
{
	byte *elem;
	Iface *ret;
	int32 alg, wid;

	elem = (byte*)(&st+1);

	if(iface_debug) {
		prints("T2I sigi=");
		printsigi(si);
		prints(" sigt=");
		printsigt(st);
		prints(" elem=");
		sys·printpointer(*(void**)elem);
		prints("\n");
	}

	wid = st->width;
	alg = st->alg;
	ret = (Iface*)(elem + rnd(wid, sizeof(uintptr)));
	ret->type = itype(si, st, 0);

	if(wid <= sizeof(ret->data))
		algarray[alg].copy(wid, &ret->data, elem);
	else {
		ret->data = mal(wid);
		if(iface_debug)
			printf("T2I mal %d %p\n", wid, ret->data);
		algarray[alg].copy(wid, ret->data, elem);
	}

	if(iface_debug) {
		prints("T2I ret=");
		printiface(*ret);
		prints("\n");
	}

	FLUSH(&ret);
}

// ifaceI2T(sigt *byte, iface any) (ret any);
void
sys·ifaceI2T(Sigt *st, Iface i, ...)
{
	Itype *im;
	byte *ret;
	int32 wid, alg;

	ret = (byte*)(&i+1);

	if(iface_debug) {
		prints("I2T sigt=");
		printsigt(st);
		prints(" iface=");
		printiface(i);
		prints("\n");
	}

	im = i.type;
	if(im == nil) {
		prints("interface is nil, not ");
		prints((int8*)st->name);
		prints("\n");
		throw("interface conversion");
	}

	if(im->sigt != st) {
		prints((int8*)im->sigi->name);
		prints(" is ");
		prints((int8*)im->sigt->name);
		prints(", not ");
		prints((int8*)st->name);
		prints("\n");
		throw("interface conversion");
	}

	alg = st->alg;
	wid = st->width;
	if(wid <= sizeof(i.data))
		algarray[alg].copy(wid, ret, &i.data);
	else
		algarray[alg].copy(wid, ret, i.data);

	if(iface_debug) {
		prints("I2T ret=");
		sys·printpointer(*(void**)ret);
		prints("\n");
	}
	FLUSH(&ret);
}

// ifaceI2T2(sigt *byte, iface any) (ret any, ok bool);
void
sys·ifaceI2T2(Sigt *st, Iface i, ...)
{
	byte *ret;
	bool *ok;
	Itype *im;
	int32 alg, wid;


	if(iface_debug) {
		prints("I2T2 sigt=");
		printsigt(st);
		prints(" iface=");
		printiface(i);
		prints("\n");
	}

	ret = (byte*)(&i+1);
	alg = st->alg;
	wid = st->width;
	ok = (bool*)(ret+rnd(wid, 1));

	im = i.type;
	if(im == nil || im->sigt != st) {
		*ok = false;
		sys·memclr(ret, wid);
	} else {
		*ok = true;
		if(wid <= sizeof(i.data))
			algarray[alg].copy(wid, ret, &i.data);
		else
			algarray[alg].copy(wid, ret, i.data);
	}
	if(iface_debug) {
		prints("I2T2 ret=");
		sys·printpointer(*(void**)ret);
		sys·printbool(*ok);
		prints("\n");
	}
}

// ifaceI2I(sigi *byte, iface any) (ret any);
void
sys·ifaceI2I(Sigi *si, Iface i, Iface ret)
{
	Itype *im;

	if(iface_debug) {
		prints("I2I sigi=");
		printsigi(si);
		prints(" iface=");
		printiface(i);
		prints("\n");
	}

	im = i.type;
	if(im == nil) {
		// If incoming interface is uninitialized (zeroed)
		// make the outgoing interface zeroed as well.
		ret = niliface;
	} else {
		ret = i;
		if(im->sigi != si)
			ret.type = itype(si, im->sigt, 0);
	}

	if(iface_debug) {
		prints("I2I ret=");
		printiface(ret);
		prints("\n");
	}

	FLUSH(&ret);
}

// ifaceI2I2(sigi *byte, iface any) (ret any, ok bool);
void
sys·ifaceI2I2(Sigi *si, Iface i, Iface ret, bool ok)
{
	Itype *im;

	if(iface_debug) {
		prints("I2I2 sigi=");
		printsigi(si);
		prints(" iface=");
		printiface(i);
		prints("\n");
	}

	im = i.type;
	if(im == nil) {
		// If incoming interface is uninitialized (zeroed)
		// make the outgoing interface zeroed as well.
		ret = niliface;
		ok = 1;
	} else {
		ret = i;
		ok = 1;
		if(im->sigi != si) {
			ret.type = itype(si, im->sigt, 1);
			if(ret.type == nil) {
				ret = niliface;
				ok = 0;
			}
		}
	}

	if(iface_debug) {
		prints("I2I ret=");
		printiface(ret);
		prints("\n");
	}

	FLUSH(&ret);
	FLUSH(&ok);
}

uint64
ifacehash(Iface a)
{
	int32 alg, wid;
	Sigt *sigt;

	if(a.type == nil)
		return 0;

	sigt = a.type->sigt;
	alg = sigt->alg;
	wid = sigt->width;
	if(algarray[alg].hash == nohash) {
		// calling nohash will throw too,
		// but we can print a better error.
		printf("hash of unhashable type %s\n", sigt->name);
		if(alg == AFAKE)
			throw("fake interface hash");
		throw("interface hash");
	}
	if(wid <= sizeof(a.data))
		return algarray[alg].hash(wid, &a.data);
	return algarray[alg].hash(wid, a.data);
}

bool
ifaceeq(Iface i1, Iface i2)
{
	int32 alg, wid;
	bool ret;

	if(iface_debug) {
		prints("Ieq i1=");
		printiface(i1);
		prints(" i2=");
		printiface(i2);
		prints("\n");
	}

	ret = false;

	// are they both nil
	if(i1.type == nil) {
		if(i2.type == nil)
			goto yes;
		goto no;
	}
	if(i2.type == nil)
		goto no;

	// are they the same type?
	if(i1.type->sigt != i2.type->sigt)
		goto no;

	alg = i1.type->sigt->alg;
	wid = i1.type->sigt->width;

	if(algarray[alg].equal == noequal) {
		// calling noequal will throw too,
		// but we can print a better error.
		printf("comparing uncomparable type %s\n", i1.type->sigt->name);
		if(alg == AFAKE)
			throw("fake interface compare");
		throw("interface compare");
	}

	if(wid <= sizeof(i1.data)) {
		if(!algarray[alg].equal(wid, &i1.data, &i2.data))
			goto no;
	} else {
		if(!algarray[alg].equal(wid, i1.data, i2.data))
			goto no;
	}

yes:
	ret = true;
no:
	if(iface_debug) {
		prints("Ieq ret=");
		sys·printbool(ret);
		prints("\n");
	}
	return ret;
}

// ifaceeq(i1 any, i2 any) (ret bool);
void
sys·ifaceeq(Iface i1, Iface i2, bool ret)
{
	ret = ifaceeq(i1, i2);
	FLUSH(&ret);
}

// ifacethash(i1 any) (ret uint32);
void
sys·ifacethash(Iface i1, uint32 ret)
{
	Itype *im;
	Sigt *st;

	ret = 0;
	im = i1.type;
	if(im != nil) {
		st = im->sigt;
		if(st != nil)
			ret = st->thash;
	}
	FLUSH(&ret);
}

void
sys·printinter(Iface i)
{
	printiface(i);
}

void
sys·Reflect(Iface i, uint64 retit, string rettype, bool retindir)
{
	int32 wid;

	if(i.type == nil) {
		retit = 0;
		rettype = nil;
		retindir = false;
	} else {
		retit = (uint64)i.data;
		rettype = gostring(i.type->sigt->name);
		wid = i.type->sigt->width;
		retindir = wid > sizeof(i.data);
	}
	FLUSH(&retit);
	FLUSH(&rettype);
	FLUSH(&retindir);
}

extern Sigt *gotypesigs[];
extern int32 ngotypesigs;


// The reflection library can ask to unreflect on a type
// that has never been used, so we don't have a signature for it.
// For concreteness, suppose a program does
//
// 	type T struct{ x []int }
// 	var t T;
// 	v := reflect.NewValue(v);
// 	vv := v.Field(0);
// 	if s, ok := vv.Interface().(string) {
// 		print("first field is string");
// 	}
//
// vv.Interface() returns the result of sys.Unreflect with
// a typestring of "[]int".  If []int is not used with interfaces
// in the rest of the program, there will be no signature in gotypesigs
// for "[]int", so we have to invent one.  The requirements
// on the fake signature are:
//
//	(1) any interface conversion using the signature will fail
//	(2) calling sys.Reflect() returns the args to unreflect
//	(3) the right algorithm type is used, for == and map insertion
//
// (1) is ensured by the fact that we allocate a new Sigt,
// so it will necessarily be != any Sigt in gotypesigs.
// (2) is ensured by storing the type string in the signature
// and setting the width to force the correct value of the bool indir.
// (3) is ensured by sniffing the type string.
//
// Note that (1) is correct behavior: if the program had tested
// for .([]int) instead of .(string) above, then there would be a
// signature with type string "[]int" in gotypesigs, and unreflect
// wouldn't call fakesigt.

static	Sigt*	fake[1009];
static	int32	nfake;

enum
{
	SizeofInt = 4,
	SizeofFloat = 4,
};

// Table of prefixes of names of comparable types.
static	struct {
	int8 *s;
	int8 n;
	int8 alg;
	int8 w;
} cmp[] =
{
	// basic types
	"int", 3+1, AMEM, SizeofInt, // +1 is NUL
	"uint", 4+1, AMEM, SizeofInt,
	"int8", 4+1, AMEM, 1,
	"uint8", 5+1, AMEM, 1,
	"int16", 5+1, AMEM, 2,
	"uint16", 6+1, AMEM, 2,
	"int32", 5+1, AMEM, 4,
	"uint32", 6+1, AMEM, 4,
	"int64", 5+1, AMEM, 8,
	"uint64", 6+1, AMEM, 8,
	"uintptr", 7+1, AMEM, sizeof(uintptr),
	"float", 5+1, AMEM, SizeofFloat,
	"float32", 7+1, AMEM, 4,
	"float64", 7+1, AMEM, 8,
	"bool", 4+1, AMEM, sizeof(bool),

	// string compare is special
	"string", 6+1, ASTRING, sizeof(string),

	// generic types, identified by prefix
	"*", 1, AMEM, sizeof(uintptr),
	"chan ", 5, AMEM, sizeof(uintptr),
	"func(", 5, AMEM, sizeof(uintptr),
	"map[", 4, AMEM, sizeof(uintptr),
};

static Sigt*
fakesigt(string type, bool indir)
{
	Sigt *sigt;
	uint32 h;
	int32 i, locked;

	if(type == nil)
		type = emptystring;

	h = 0;
	for(i=0; i<type->len; i++)
		h = h*37 + type->str[i];
	h += indir;
	h %= nelem(fake);

	for(locked=0; locked<2; locked++) {
		if(locked)
			lock(&ifacelock);
		for(sigt = fake[h]; sigt != nil; sigt = sigt->link) {
			// don't need to compare indir.
			// same type string but different indir will have
			// different hashes.
			if(mcmp(sigt->name, type->str, type->len) == 0)
			if(sigt->name[type->len] == '\0') {
				if(locked)
					unlock(&ifacelock);
				return sigt;
			}
		}
	}

	sigt = mal(sizeof(*sigt));
	sigt->name = mal(type->len + 1);
	mcpy(sigt->name, type->str, type->len);

	sigt->alg = AFAKE;
	sigt->width = 1;  // small width
	if(indir)
		sigt->width = 2*sizeof(niliface.data);  // big width

	// AFAKE is like ANOEQ; check whether the type
	// should have a more capable algorithm.
	for(i=0; i<nelem(cmp); i++) {
		if(mcmp((byte*)sigt->name, (byte*)cmp[i].s, cmp[i].n) == 0) {
			sigt->alg = cmp[i].alg;
			sigt->width = cmp[i].w;
			break;
		}
	}

	sigt->link = fake[h];
	fake[h] = sigt;

	unlock(&ifacelock);
	return sigt;
}

static int32
cmpstringchars(string a, uint8 *b)
{
	int32 i;
	byte c1, c2;

	for(i=0;; i++) {
		if(i == a->len)
			c1 = 0;
		else
			c1 = a->str[i];
		c2 = b[i];
		if(c1 < c2)
			return -1;
		if(c1 > c2)
			return +1;
		if(c1 == 0)
			return 0;
	}
}

static Sigt*
findtype(string type, bool indir)
{
	int32 i, lo, hi, m;

	lo = 0;
	hi = ngotypesigs;
	while(lo < hi) {
		m = lo + (hi - lo)/2;
		i = cmpstringchars(type, gotypesigs[m]->name);
		if(i == 0)
			return gotypesigs[m];
		if(i < 0)
			hi = m;
		else
			lo = m+1;
	}
	return fakesigt(type, indir);
}


void
sys·Unreflect(uint64 it, string type, bool indir, Iface ret)
{
	Sigt *sigt;

	ret = niliface;

	if(cmpstring(type, emptystring) == 0)
		goto out;

	// if we think the type should be indirect
	// and caller does not, play it safe, return nil.
	sigt = findtype(type, indir);
	if(indir != (sigt->width > sizeof(ret.data)))
		goto out;

	ret.type = itype(sigi·empty, sigt, 0);
	ret.data = (void*)it;

out:
	FLUSH(&ret);
}