aboutsummaryrefslogtreecommitdiffstats
path: root/code/tools/lcc/src/symbolic.c
blob: affa67a08e9d4c95a70c01ec7da0bd34c2750eba (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
#include <time.h>
#include <ctype.h>
#include "c.h"

#define I(f) s_##f

static Node *tail;
static int off, maxoff, uid = 0, verbose = 0, html = 0;

static const char *yyBEGIN(const char *tag) {
	if (html)
		print("<%s>", tag);
	return tag;
}

static void yyEND(const char *tag) {
	if (html)
		print("</%s>", tag);
	if (isupper(*tag))
		print("\n");
}

#define BEGIN(tag) do { const char *yytag=yyBEGIN(#tag);
#define END yyEND(yytag); } while (0)
#define ITEM BEGIN(li)
#define START BEGIN(LI)
#define ANCHOR(attr,code) do { const char *yytag="a"; if (html) { printf("<a " #attr "=\""); code; print("\">"); }
#define NEWLINE print(html ? "<br>\n" : "\n")

static void emitCoord(Coordinate src) {
	if (src.file && *src.file) {
		ANCHOR(href,print("%s", src.file)); print("%s", src.file); END;
		print(":");
	}
	print("%d.%d", src.y, src.x);
}

static void emitString(int len, const char *s) {
	for ( ; len-- > 0; s++)
		if (*s == '&' && html)
			print("&amp;");
		else if (*s == '<' && html)
			print("&lt;");
		else if (*s == '>' && html)
			print("&lt;");
		else if (*s == '"' || *s == '\\')
			print("\\%c", *s);
		else if (*s >= ' ' && *s < 0177)
			print("%c", *s);
		else
			print("\\%d%d%d", (*s>>6)&3, (*s>>3)&7, *s&7);
}

static void emitSymRef(Symbol p) {
	(*IR->defsymbol)(p);
	ANCHOR(href,print("#%s", p->x.name)); BEGIN(code); print("%s", p->name); END; END;
}

static void emitSymbol(Symbol p) {
	(*IR->defsymbol)(p);
	ANCHOR(name,print("%s", p->x.name)); BEGIN(code); print("%s", p->name); END; END;
	BEGIN(ul);
#define xx(field,code) ITEM; if (!html) print(" "); print(#field "="); code; END
	if (verbose && (src.y || src.x))
		xx(src,emitCoord(p->src));
	xx(type,print("%t", p->type));
	xx(sclass,print("%k", p->sclass));
	switch (p->scope) {
	case CONSTANTS: xx(scope,print("CONSTANTS")); break;
	case LABELS:    xx(scope,print("LABELS"));    break;
	case GLOBAL:    xx(scope,print("GLOBAL"));    break;
	case PARAM:     xx(scope,print("PARAM"));     break;
	case LOCAL:     xx(scope,print("LOCAL"));     break;
	default:
		if (p->scope > LOCAL)
			xx(scope,print("LOCAL+%d", p->scope-LOCAL));
		else
			xx(scope,print("%d", p->scope));
	}
	if (p->scope >= PARAM && p->sclass != STATIC)
		xx(offset,print("%d", p->x.offset));
	xx(ref,print("%f", p->ref));
	if (p->temporary && p->u.t.cse)
		xx(u.t.cse,print("%p", p->u.t.cse));
	END;
#undef xx
}

/* address - initialize q for addressing expression p+n */
static void I(address)(Symbol q, Symbol p, long n) {
	q->name = stringf("%s%s%D", p->name, n > 0 ? "+" : "", n);
	(*IR->defsymbol)(q);
	START; print("address "); emitSymbol(q); END;
}

/* blockbeg - start a block */
static void I(blockbeg)(Env *e) {
	e->offset = off;
	START; print("blockbeg off=%d", off); END;
}

/* blockend - start a block */
static void I(blockend)(Env *e) {
	if (off > maxoff)
		maxoff = off;
	START; print("blockend off=%d", off); END;
	off = e->offset;
}

/* defaddress - initialize an address */
static void I(defaddress)(Symbol p){
	START; print("defaddress "); emitSymRef(p); END;
}

/* defconst - define a constant */
static void I(defconst)(int suffix, int size, Value v) {
	START;
	print("defconst ");
	switch (suffix) {
	case I:
		print("int.%d ", size);
		BEGIN(code);
		if (size > sizeof (int))
			print("%D", v.i);
		else
			print("%d", (int)v.i);
		END;
		break;
	case U:
		print("unsigned.%d ", size);
		BEGIN(code);
		if (size > sizeof (unsigned))
			print("%U", v.u);
		else
			print("%u", (unsigned)v.u);
		END;
		break;
	case P: print("void*.%d ", size); BEGIN(code); print("%p", v.p); END; break;
	case F: print("float.%d ", size); BEGIN(code); print("%g", (double)v.d); END; break;
	default: assert(0);
	}
	END;
}

/* defstring - emit a string constant */
static void I(defstring)(int len, char *s) {
	START; print("defstring ");
	BEGIN(code); print("\""); emitString(len, s); print("\""); END;
	END;
}

/* defsymbol - define a symbol: initialize p->x */
static void I(defsymbol)(Symbol p) {
	if (p->x.name == NULL)
		p->x.name = stringd(++uid);
}

/* emit - emit the dags on list p */
static void I(emit)(Node p){
	ITEM;
	if (!html)
		print(" ");
	for (; p; p = p->x.next) {
		if (p->op == LABEL+V) {
			assert(p->syms[0]);
			ANCHOR(name,print("%s", p->syms[0]->x.name));
			BEGIN(code); print("%s", p->syms[0]->name); END;
			END;
			print(":");
		} else {
			int i;
			if (p->x.listed) {
				BEGIN(strong); print("%d", p->x.inst); END; print("'");
				print(" %s", opname(p->op));
			} else
				print("%d. %s", p->x.inst, opname(p->op));
			if (p->count > 1)
				print(" count=%d", p->count);
			for (i = 0; i < NELEMS(p->kids) && p->kids[i]; i++)
				print(" #%d", p->kids[i]->x.inst);
			if (generic(p->op) == CALL && p->syms[0] && p->syms[0]->type)
				print(" {%t}", p->syms[0]->type);
			else
				for (i = 0; i < NELEMS(p->syms) && p->syms[i]; i++) {
					print(" ");
					if (p->syms[i]->scope == CONSTANTS)
						print(p->syms[i]->name);
					else
						emitSymRef(p->syms[i]);
				}
		}
		NEWLINE;
	}
	END;
}

/* export - announce p as exported */
static void I(export)(Symbol p) {
	START; print("export "); emitSymRef(p); END;
}

/* function - generate code for a function */
static void I(function)(Symbol f, Symbol caller[], Symbol callee[], int ncalls) {
	int i;

	(*IR->defsymbol)(f);
	off = 0;
	for (i = 0; caller[i] && callee[i]; i++) {
		off = roundup(off, caller[i]->type->align);
		caller[i]->x.offset = callee[i]->x.offset = off;
		off += caller[i]->type->size;
	}
	if (!html) {
		print("function ");
		emitSymbol(f);
		print(" ncalls=%d\n", ncalls);
		for (i = 0; caller[i]; i++)
			START; print("caller "); emitSymbol(caller[i]); END;
		for (i = 0; callee[i]; i++)
			START; print("callee "); emitSymbol(callee[i]); END;
	} else {
		START;
		print("function");
		BEGIN(UL);
#define xx(field,code) ITEM; print(#field "="); code; END
		xx(f,emitSymbol(f));
		xx(ncalls,print("%d", ncalls));
		if (caller[0]) {
			ITEM; print("caller"); BEGIN(OL);
			for (i = 0; caller[i]; i++)
				ITEM; emitSymbol(caller[i]); END;
			END; END;
			ITEM; print("callee"); BEGIN(OL);
			for (i = 0; callee[i]; i++)
				ITEM; emitSymbol(callee[i]); END;
			END; END;
		} else {
			xx(caller,BEGIN(em); print("empty"); END);
			xx(callee,BEGIN(em); print("empty"); END);
		}
		END;
		END;
	}
	maxoff = off = 0;
	gencode(caller, callee);
	if (html)
		START; print("emitcode"); BEGIN(ul); emitcode(); END; END;
	else
		emitcode();
	START; print("maxoff=%d", maxoff); END;
#undef xx
}

/* visit - generate code for *p */
static int visit(Node p, int n) {
	if (p && p->x.inst == 0) {
		p->x.inst = ++n;
		n = visit(p->kids[0], n);
		n = visit(p->kids[1], n);
		*tail = p;
		tail = &p->x.next;
	}
	return n;
}

/* gen0 - generate code for the dags on list p */
static Node I(gen)(Node p) {
	int n;
	Node nodelist;

	tail = &nodelist;
	for (n = 0; p; p = p->link) {
		switch (generic(p->op)) {	/* check for valid forest */
		case CALL:
			assert(IR->wants_dag || p->count == 0);
			break;
		case ARG:
		case ASGN: case JUMP: case LABEL: case RET:
		case EQ: case GE: case GT: case LE: case LT: case NE:
			assert(p->count == 0);
			break;
		case INDIR:
			assert(IR->wants_dag && p->count > 0);
			break;
		default:
			assert(0);
		}
		check(p);
		p->x.listed = 1;
		n = visit(p, n);
	}
	*tail = 0;
	return nodelist;
}

/* global - announce a global */
static void I(global)(Symbol p) {
	START; print("global "); emitSymbol(p); END;
}

/* import - import a symbol */
static void I(import)(Symbol p) {
	START; print("import "); emitSymRef(p); END;
}

/* local - local variable */
static void I(local)(Symbol p) {
	if (p->temporary)
		p->name = stringf("t%s", p->name);
	(*IR->defsymbol)(p);
	off = roundup(off, p->type->align);
	p->x.offset = off;
	off += p->type->size;
	START; print(p->temporary ? "temporary " : "local "); emitSymbol(p); END;
}

/* progbeg - beginning of program */
static void I(progbeg)(int argc, char *argv[]) {
	int i;

	for (i = 1; i < argc; i++)
		if (strcmp(argv[i], "-v") == 0)
			verbose++;
		else if (strcmp(argv[i], "-html") == 0)
			html++;
	if (html) {
		print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
		print("<html>");
		BEGIN(head);
		if (firstfile && *firstfile)
			BEGIN(title); emitString(strlen(firstfile), firstfile);	END;
		print("<link rev=made href=\"mailto:drh@microsoft.com\">\n");
		END;
		print("<body>\n");
		if (firstfile && *firstfile)
			BEGIN(h1); emitString(strlen(firstfile), firstfile); END;
		BEGIN(P); BEGIN(em);
		print("Links lead from uses of identifiers and labels to their definitions.");
		END; END;
		print("<ul>\n");
		START;
		print("progbeg");
		BEGIN(ol);
		for (i = 1; i < argc; i++) {
			ITEM;
			BEGIN(code); print("\""); emitString(strlen(argv[i]), argv[i]); print("\""); END;
			END;
		}
		END;
		END;
	}
}

/* progend - end of program */
static void I(progend)(void) {
	START; print("progend"); END;
	if (html) {
		time_t t;
		print("</ul>\n");
		time(&t);
		print("<hr><address>%s</address>\n", ctime(&t));
		print("</body></html>\n");
	}
}

/* segment - switch to segment s */
static void I(segment)(int s) {
	START; print("segment %s", &"text\0bss\0.data\0lit\0.sym\0."[5*s-5]); END;
}

/* space - initialize n bytes of space */
static void I(space)(int n) {
	START; print("space %d", n); END;
}

static void I(stabblock)(int brace, int lev, Symbol *p) {}

/* stabend - finalize stab output */
static void I(stabend)(Coordinate *cp, Symbol p, Coordinate **cpp, Symbol *sp, Symbol *stab) {
	int i;

	if (p)
		emitSymRef(p);
	print("\n");
	if (cpp && sp)
		for (i = 0; cpp[i] && sp[i]; i++) {
			print("%w.%d: ", cpp[i], cpp[i]->x);
			emitSymRef(sp[i]);
			print("\n");
		}
}

static void I(stabfend)(Symbol p, int lineno) {}
static void I(stabinit)(char *file, int argc, char *argv[]) {}

/* stabline - emit line number information for source coordinate *cp */
static void I(stabline)(Coordinate *cp) {
	if (cp->file)
		print("%s:", cp->file);
	print("%d.%d:\n", cp->y, cp->x);
}

static void I(stabsym)(Symbol p) {}
static void I(stabtype)(Symbol p) {}

Interface symbolicIR = {
	{1, 1, 0},	/* char */
	{2, 2, 0},	/* short */
	{4, 4, 0},	/* int */
	{4, 4, 0},	/* long */
	{4, 4, 0},	/* long long */
	{4, 4, 1},	/* float */
	{8, 8, 1},	/* double */
	{8, 8, 1},	/* long double */
	{4, 4, 0},	/* T* */
	{0, 4, 0},	/* struct */
	0,		/* little_endian */
	0,		/* mulops_calls */
	0,		/* wants_callb */
	1,		/* wants_argb */
	1,		/* left_to_right */
	1,		/* wants_dag */
	0,		/* unsigned_char */
	I(address),
	I(blockbeg),
	I(blockend),
	I(defaddress),
	I(defconst),
	I(defstring),
	I(defsymbol),
	I(emit),
	I(export),
	I(function),
	I(gen),
	I(global),
	I(import),
	I(local),
	I(progbeg),
	I(progend),
	I(segment),
	I(space),
	I(stabblock),
	I(stabend),
	I(stabfend),
	I(stabinit),
	I(stabline),
	I(stabsym),
	I(stabtype)
};

Interface symbolic64IR = {
	{1, 1, 0},	/* char */
	{2, 2, 0},	/* short */
	{4, 4, 0},	/* int */
	{8, 8, 0},	/* long */
	{8, 8, 0},	/* long long */
	{4, 4, 1},	/* float */
	{8, 8, 1},	/* double */
	{8, 8, 1},	/* long double */
	{8, 8, 0},	/* T* */
	{0, 1, 0},	/* struct */
	1,		/* little_endian */
	0,		/* mulops_calls */
	0,		/* wants_callb */
	1,		/* wants_argb */
	1,		/* left_to_right */
	1,		/* wants_dag */
	0,		/* unsigned_char */
	I(address),
	I(blockbeg),
	I(blockend),
	I(defaddress),
	I(defconst),
	I(defstring),
	I(defsymbol),
	I(emit),
	I(export),
	I(function),
	I(gen),
	I(global),
	I(import),
	I(local),
	I(progbeg),
	I(progend),
	I(segment),
	I(space),
	I(stabblock),
	I(stabend),
	I(stabfend),
	I(stabinit),
	I(stabline),
	I(stabsym),
	I(stabtype)
};