summaryrefslogtreecommitdiff
path: root/src/cmd/6l/go.c
blob: e073959ded3da97e41474c9d79681a531a8f34ca (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
// 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.

// go-specific

// accumulate all type information from .6 files.
// check for inconsistencies.
// define gotypestrings variable if needed.
// define gotypesigs variable if needed.

// TODO:
//	include type info for non-exported types.
//	generate debugging section in binary.
//	once the dust settles, try to move some code to
//		libmach, so that other linkers and ar can share.
//	try to make this completely portable and shared
//		across linkers

#include "l.h"

/*
 *	package import data
 */
typedef struct Import Import;
struct Import
{
	Import *hash;	// next in hash table
	int export;	// marked as export?
	char *prefix;	// "type", "var", "func", "const"
	char *name;
	char *def;
	char *file;
};
enum {
	NIHASH = 1024
};
static Import *ihash[NIHASH];
static int nimport;

static int
hashstr(char *name)
{
	int h;
	char *cp;

	h = 0;
	for(cp = name; *cp; h += *cp++)
		h *= 1119;
	if(h < 0)
		h = ~h;
	return h;
}

static Import *
ilookup(char *name)
{
	int h;
	Import *x;

	h = hashstr(name) % NIHASH;
	for(x=ihash[h]; x; x=x->hash)
		if(x->name[0] == name[0] && strcmp(x->name, name) == 0)
			return x;
	x = mal(sizeof *x);
	x->name = name;
	x->hash = ihash[h];
	ihash[h] = x;
	nimport++;
	return x;
}

static void loadpkgdata(char*, char*, int);
static int parsemethod(char**, char*, char**);
static int parsepkgdata(char*, char**, char*, int*, char**, char**, char**);

void
ldpkg(Biobuf *f, int64 len, char *filename)
{
	char *data, *p0, *p1;

	if(debug['g'])
		return;

	if((int)len != len) {
		fprint(2, "6l: too much pkg data in %s\n", filename);
		return;
	}
	data = mal(len);
	if(Bread(f, data, len) != len) {
		fprint(2, "6l: short pkg read %s\n", filename);
		return;
	}

	// first \n$$ marks beginning of exports - skip rest of line
	p0 = strstr(data, "\n$$");
	if(p0 == nil)
		return;
	p0 += 3;
	while(*p0 != '\n' && *p0 != '\0')
		p0++;

	// second marks end of exports / beginning of local data
	p1 = strstr(p0, "\n$$");
	if(p1 == nil) {
		fprint(2, "6l: cannot find end of exports in %s\n", filename);
		return;
	}
	while(*p0 == ' ' || *p0 == '\t' || *p0 == '\n')
		p0++;
	if(strncmp(p0, "package ", 8) != 0) {
		fprint(2, "6l: bad package section in %s\n", filename);
		return;
	}
	p0 += 8;
	while(*p0 == ' ' || *p0 == '\t' || *p0 == '\n')
		p0++;
	while(*p0 != ' ' && *p0 != '\t' && *p0 != '\n')
		p0++;

	loadpkgdata(filename, p0, p1 - p0);

	// local types begin where exports end.
	// skip rest of line after $$ we found above
	p0 = p1 + 3;
	while(*p0 != '\n' && *p0 != '\0')
		p0++;

	// local types end at next \n$$.
	p1 = strstr(p0, "\n$$");
	if(p1 == nil) {
		fprint(2, "6l: cannot find end of local types in %s\n", filename);
		return;
	}

	loadpkgdata(filename, p0, p1 - p0);
}

static void
loadpkgdata(char *file, char *data, int len)
{
	int export;
	char *p, *ep, *prefix, *name, *def;
	Import *x;

	file = strdup(file);
	p = data;
	ep = data + len;
	while(parsepkgdata(file, &p, ep, &export, &prefix, &name, &def) > 0) {
		x = ilookup(name);
		if(x->prefix == nil) {
			x->prefix = prefix;
			x->def = def;
			x->file = file;
			x->export = export;
		} else {
			if(strcmp(x->prefix, prefix) != 0) {
				fprint(2, "6l: conflicting definitions for %s\n", name);
				fprint(2, "%s:\t%s %s ...\n", x->file, x->prefix, name);
				fprint(2, "%s:\t%s %s ...\n", file, prefix, name);
				nerrors++;
			}
			else if(strcmp(x->def, def) != 0) {
				fprint(2, "6l: conflicting definitions for %s\n", name);
				fprint(2, "%s:\t%s %s %s\n", x->file, x->prefix, name, x->def);
				fprint(2, "%s:\t%s %s %s\n", file, prefix, name, def);
				nerrors++;
			}

			// okay if some .6 say export and others don't.
			// all it takes is one.
			if(export)
				x->export = 1;
		}
	}
}

static int
parsepkgdata(char *file, char **pp, char *ep, int *exportp, char **prefixp, char **namep, char **defp)
{
	char *p, *prefix, *name, *def, *edef, *meth;
	int n;

	// skip white space
	p = *pp;
	while(p < ep && (*p == ' ' || *p == '\t' || *p == '\n'))
		p++;
	if(p == ep || strncmp(p, "$$\n", 3) == 0)
		return 0;

	// [export|package ]
	*exportp = 0;
	if(p + 7 <= ep && strncmp(p, "export ", 7) == 0) {
		*exportp = 1;
		p += 7;
	}
	else if(p + 8 <= ep && strncmp(p, "package ", 8) == 0) {
		*exportp = 2;
		p += 8;
	}

	// prefix: (var|type|func|const)
	prefix = p;

	prefix = p;
	if(p + 6 > ep)
		return -1;
	if(strncmp(p, "var ", 4) == 0)
		p += 4;
	else if(strncmp(p, "type ", 5) == 0)
		p += 5;
	else if(strncmp(p, "func ", 5) == 0)
		p += 5;
	else if(strncmp(p, "const ", 6) == 0)
		p += 6;
	else{
		fprint(2, "6l: confused in pkg data near <<%.20s>>\n", p);
		nerrors++;
		return -1;
	}
	p[-1] = '\0';

	// name: a.b followed by space
	name = p;
	while(p < ep && *p != ' ')
		p++;
	if(p >= ep)
		return -1;
	*p++ = '\0';

	// def: free form to new line
	def = p;
	while(p < ep && *p != '\n')
		p++;
	if(p >= ep)
		return -1;
	edef = p;
	*p++ = '\0';

	// include methods on successive lines in def of named type
	while(parsemethod(&p, ep, &meth) > 0) {
		*edef++ = '\n';	// overwrites '\0'
		if(edef+1 > meth) {
			// We want to indent methods with a single \t.
			// 6g puts at least one char of indent before all method defs,
			// so there will be room for the \t.  If the method def wasn't
			// indented we could do something more complicated,
			// but for now just diagnose the problem and assume
			// 6g will keep indenting for us.
			fprint(2, "6l: %s: expected methods to be indented %p %p %.10s\n",
				file, edef, meth, meth);
			nerrors++;
			return -1;
		}
		*edef++ = '\t';
		n = strlen(meth);
		memmove(edef, meth, n);
		edef += n;
	}

	// done
	*pp = p;
	*prefixp = prefix;
	*namep = name;
	*defp = def;
	return 1;
}

static int
parsemethod(char **pp, char *ep, char **methp)
{
	char *p;

	// skip white space
	p = *pp;
	while(p < ep && (*p == ' ' || *p == '\t'))
		p++;
	if(p == ep)
		return 0;

	// if it says "func (", it's a method
	if(p + 6 >= ep || strncmp(p, "func (", 6) != 0)
		return 0;

	// definition to end of line
	*methp = p;
	while(p < ep && *p != '\n')
		p++;
	if(p >= ep) {
		fprint(2, "6l: lost end of line in method definition\n");
		*pp = ep;
		return -1;
	}
	*p++ = '\0';
	*pp = p;
	return 1;
}

static int
importcmp(const void *va, const void *vb)
{
	Import *a, *b;

	a = *(Import**)va;
	b = *(Import**)vb;
	return strcmp(a->name, b->name);
}

static int
symcmp(const void *va, const void *vb)
{
	Sym *a, *b;

	a = *(Sym**)va;
	b = *(Sym**)vb;
	return strcmp(a->name, b->name);
}

// if there is an undefined reference to gotypestrings,
// create it.  c declaration is
//	extern char gotypestrings[];
// ironically, gotypestrings is a c variable, because there
// is no way to forward declare a string in go.
void
definetypestrings(void)
{
	int i, j, len, n, w;
	char *p;
	Import **all, *x;
	Fmt f;
	Prog *prog;
	Sym *s;

	if(debug['g'])
		return;

	if(debug['v'])
		Bprint(&bso, "%5.2f definetypestrings\n", cputime());

	s = lookup("gotypestrings", 0);
	if(s->type == 0)
		return;
	if(s->type != SXREF) {
		diag("gotypestrings already defined");
		return;
	}
	s->type = SDATA;

	// make a list of all the type exports
	n = 0;
	for(i=0; i<NIHASH; i++)
		for(x=ihash[i]; x; x=x->hash)
			if(strcmp(x->prefix, "type") == 0)
				n++;
	all = mal(n*sizeof all[0]);
	j = 0;
	for(i=0; i<NIHASH; i++)
		for(x=ihash[i]; x; x=x->hash)
			if(strcmp(x->prefix, "type") == 0)
				all[j++] = x;

	// sort them by name
	qsort(all, n, sizeof all[0], importcmp);

	// make a big go string containing all the types
	fmtstrinit(&f);
	fmtprint(&f, "xxxx");	// 4-byte length
	for(i=0; i<n; i++) {
		p = strchr(all[i]->def, '\n');
		if(p)
			len = p - all[i]->def;
		else
			len = strlen(all[i]->def);
		fmtprint(&f, "%s %.*s\n", all[i]->name, utfnlen(all[i]->def, len), all[i]->def);
	}
	p = fmtstrflush(&f);
	n = strlen(p);
	s->value = n;

	// go strings begin with 4-byte length.
	// amd64 is little-endian.
	len = n - 4;
	p[0] = len;
	p[1] = len >> 8;
	p[2] = len >> 16;
	p[3] = len >> 24;

	// have data, need to create linker representation.
	// linker stores big data as sequence of pieces
	// with int8 length, so break p into 100-byte chunks.
	// (had to add D_SBIG even to do that; the compiler
	// would have generated 8-byte chunks.)
	for(i=0; i<n; i+=100) {
		w = 100;
		if(w > n - i)
			w = n - i;
		prog = newdata(s, i, w, D_EXTERN);
		prog->to.type = D_SBIG;
		prog->to.sbig = p + i;
	}

	if(debug['v'])
		Bprint(&bso, "%5.2f typestrings %d\n", cputime(), n);
}

// if there is an undefined reference to gotypesigs, create it.
// c declaration is
//	extern Sigt *gotypesigs[];
//	extern int ngotypesigs;
// used by sys.unreflect runtime.
void
definetypesigs(void)
{
	int i, j, n;
	Sym **all, *s, *x;
	Prog *prog;

	if(debug['g'])
		return;

	if(debug['v'])
		Bprint(&bso, "%5.2f definetypesigs\n", cputime());

	s = lookup("gotypesigs", 0);
	if(s->type == 0)
		return;
	if(s->type != SXREF) {
		diag("gotypesigs already defined");
		return;
	}
	s->type = SDATA;

	// make a list of all the sigt symbols.
	n = 0;
	for(i=0; i<NHASH; i++)
		for(x = hash[i]; x; x=x->link)
			if(memcmp(x->name, "sigtĀ·", 6) == 0)
				n++;
	all = mal(n*sizeof all[0]);
	j = 0;
	for(i=0; i<NHASH; i++)
		for(x = hash[i]; x; x=x->link)
			if(memcmp(x->name, "sigtĀ·", 6) == 0)
				all[j++] = x;

	// sort them by name
	qsort(all, n, sizeof all[0], symcmp);

	// emit array as sequence of references.
	enum { PtrSize = 8 };
	for(i=0; i<n; i++) {
		prog = newdata(s, PtrSize*i, PtrSize, D_EXTERN);
		prog->to.type = D_ADDR;
		prog->to.index = D_EXTERN;
		prog->to.sym = all[i];
	}
	s->value = PtrSize*n;
	if(n == 0)
		s->value = 1;	// must have non-zero size or 6l complains

	// emit count
	s = lookup("ngotypesigs", 0);
	s->type = SDATA;
	s->value = sizeof(int32);
	prog = newdata(s, 0, sizeof(int32), D_EXTERN);
	prog->to.offset = n;

	if(debug['v'])
		Bprint(&bso, "%5.2f typestrings %d\n", cputime(), n);

}