summaryrefslogtreecommitdiff
path: root/6/blob.c
blob: c78a6f2391958746b1c89b3a4ca8cf3a258f2762 (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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "util.h"
#include "parse.h"
#include "mi.h"
#include "asm.h"
#include "../config.h"

static size_t blobrec(Blob *b, Htab *globls, Htab *strtab, Node *n);

Blob *
mkblobi(Blobtype type, uint64_t ival)
{
	Blob *b;

	b = zalloc(sizeof(Blob));
	b->type = type;
	b->ival = ival;
	return b;
}

Blob *
mkblobpad(size_t sz)
{
	Blob *b;

	b = zalloc(sizeof(Blob));
	b->type = Btpad;
	b->npad = sz;
	return b;
}

Blob *
mkblobbytes(char *buf, size_t len)
{
	Blob *b;

	b = zalloc(sizeof(Blob));
	b->type = Btbytes;
	b->bytes.buf = buf;
	b->bytes.len = len;
	return b;
}

Blob *
mkblobseq(Blob **sub, size_t nsub)
{
	Blob *b;

	b = zalloc(sizeof(Blob));
	b->type = Btseq;
	b->seq.sub = sub;
	b->seq.nsub = nsub;
	return b;
}

Blob *
mkblobref(char *lbl, size_t off, int isextern)
{
	Blob *b;

	b = zalloc(sizeof(Blob));
	b->type = Btref;
	b->ref.str = strdup(lbl);
	b->ref.isextern = isextern;
	b->ref.off = off;
	return b;
}

void
blobfree(Blob *b)
{
	size_t i;

	if (!b)
		return;
	switch (b->type) {
	case Btref:
		free(b->lbl);
		break;
	case Btseq:
		for (i = 0; i < b->seq.nsub; i++)

			blobfree(b->seq.sub[i]);
		break;
	default:
		break;
	}
	free(b);
}

void
b(Blob *b, Blob *n)
{
	lappend(&b->seq.sub, &b->seq.nsub, n);
}

static size_t
blobpad(Blob *seq, size_t sz)
{
	if (sz)
		b(seq, mkblobpad(sz));
	return sz;
}

static size_t
bloblit(Blob *seq, Htab *strtab, Node *v, Type *ty)
{
	char buf[128];
	char *lbl;
	size_t sz;
	Blobtype intsz[] = {
		[1] = Bti8,
		[2] = Bti16,
		[4] = Bti32,
		[8] = Bti64
	};
	union {
		float fv;
		double dv;
		uint64_t qv;
		uint32_t lv;
	} u;

	assert(v->type == Nlit);
	sz = tysize(ty);
	switch (v->lit.littype) {
	case Lvoid:	break;
	case Lint:	b(seq, mkblobi(intsz[sz], v->lit.intval));	break;
	case Lbool:	b(seq, mkblobi(Bti8, v->lit.boolval));	break;
	case Lchr:	b(seq, mkblobi(Bti32, v->lit.chrval));	break;
	case Lflt:
		if (tybase(v->lit.type)->type == Tyflt32) {
			u.fv = v->lit.fltval;
			b(seq, mkblobi(Bti32, u.lv));
		} else if (tybase(v->lit.type)->type == Tyflt64) {
			u.dv = v->lit.fltval;
			b(seq, mkblobi(Bti64, u.qv));
		}
		break;
	case Lstr:
		if (hthas(strtab, &v->lit.strval)) {
			lbl = htget(strtab, &v->lit.strval);
		} else {
			lbl = genlocallblstr(buf, sizeof buf);
			htput(strtab, &v->lit.strval, strdup(lbl));
		}
		if (v->lit.strval.len > 0)
			b(seq, mkblobref(lbl, 0, 1));
		else
			b(seq, mkblobi(Bti64, 0));
		b(seq, mkblobi(Bti64, v->lit.strval.len));
		break;
	case Lfunc:
		lbl = asmname(v);
		b(seq, mkblobi(Bti64, 0));
		b(seq, mkblobref(lbl, 0, 0));
		break;
	case Llbl:
		die("Can't generate literal labels, ffs. They're not data.");
		break;
	}
	return sz;
}

static size_t
blobslice(Blob *seq,  Htab *globls, Htab *strtab, Node *n)
{
	Node *base, *lo, *hi;
	vlong loval, hival, baseval, sz;
	Blob *slbase;
	char *lbl;

	base = n->expr.args[0];
	lo = n->expr.args[1];
	hi = n->expr.args[2];

	/* by this point, all slicing operations should have had their base
	 * pulled out, and we should have vars with their pseudo-decls in their
	 * place */
	if (!getintlit(lo, &loval))
		fatal(lo, "lower bound in slice is not constant literal");
	if (!getintlit(hi, &hival))
		fatal(hi, "upper bound in slice is not constant literal");
	if (exprop(base) == Ovar && base->expr.isconst) {
		sz = tysize(tybase(exprtype(base))->sub[0]);
		lbl = htget(globls, base);
		slbase = mkblobref(lbl, loval*sz, 1);
	} else if (exprop(base) == Olit || exprop(base) == Oarr) {
		if (!getintlit(base, &baseval))
			fatal(base, "invalid base expr");
		slbase = mkblobi(Bti64, baseval);
	} else {
		fatal(base, "slice base is not a constant value");
	}

	b(seq, slbase);
	b(seq, mkblobi(Bti64, (hival - loval)));
	return 16;
}

static Node *
structmemb(Node *n, char *dcl)
{
	size_t i;

	for (i = 0; i < n->expr.nargs; i++)
		if (!strcmp(namestr(n->expr.args[i]->expr.idx), dcl))
			return n->expr.args[i];
	return NULL;
}

static size_t
blobstruct(Blob *seq, Htab *globls, Htab *strtab, Node *n)
{
	size_t i, sz, pad, end, ndcl;
	Node **dcl, *m;
	Type *t;

	sz = 0;
	t = tybase(exprtype(n));
	assert(t->type == Tystruct);
	dcl = t->sdecls;
	ndcl = t->nmemb;

	for (i = 0; i < ndcl; i++) {
		pad = alignto(sz, decltype(dcl[i]));
		m = structmemb(n, declname(dcl[i]));
		sz += blobpad(seq, pad - sz);
		if (m)
			sz += blobrec(seq, globls, strtab, m);
		else
			sz += blobpad(seq, size(dcl[i]));
	}
	end = alignto(sz, t);
	sz += blobpad(seq, end - sz);
	return sz;
}

static size_t
blobucon(Blob *seq, Htab *globls, Htab *strtab, Node *n)
{
	size_t sz, align;
	Ucon *uc;

	sz = 4;
	uc = finducon(exprtype(n), n->expr.args[0]);
	b(seq, mkblobi(Bti32, uc->id));
	align = 1;
	if (n->expr.nargs > 1) {
		align = tyalign(exprtype(n->expr.args[1]));
		if (align > sz)
			sz += blobpad(seq, align - sz);
		sz += blobrec(seq, globls, strtab, n->expr.args[1]);
	}
	sz += blobpad(seq, size(n) - sz);
	return sz;
}

static size_t
blobvar(Blob *seq, Htab *strtab, Node *var, Type *ty)
{
	Node *dcl;
	Ty tt;

	dcl = decls[var->expr.did];
	tt = tybase(ty)->type;
	if ((tt != Tyfunc && tt != Tycode)|| !dcl->decl.isglobl)
		fatal(var, "non-constant initializer %s", namestr(var->expr.args[0]));
	b(seq, mkblobi(Bti64, 0));
	b(seq, mkblobref(asmname(dcl), 0, 1));
	return 16;
}

static size_t
blobrec(Blob *b, Htab *globls, Htab *strtab, Node *n)
{
	size_t i, sz, end;

	switch(exprop(n)) {
	case Oucon:	sz = blobucon(b, globls, strtab, n);	break;
	case Oslice:	sz = blobslice(b, globls, strtab, n);	break;
	case Ostruct:	sz = blobstruct(b, globls, strtab, n);	break;
	case Ovar:	sz = blobvar(b, strtab, n, exprtype(n));	break;
	case Olit:	sz = bloblit(b, strtab, n->expr.args[0], exprtype(n));	break;
	case Otup:
	case Oarr:
		/* Assumption: We sorted this while folding */
		sz = 0;
		if (!n->expr.args)
			break;
		for (i = 0; i < n->expr.nargs; i++) {
			end = alignto(sz, exprtype(n->expr.args[i]));
			sz += blobpad(b, end - sz);
			sz += blobrec(b, globls, strtab, n->expr.args[i]);
		}
		/* if we need padding at the end.. */
		end = alignto(sz, exprtype(n));
		sz += blobpad(b, end - sz);
		break;
	default:
		dump(n, stdout);
		die("Nonliteral initializer for global");
		break;
	}
	return sz;
}

Blob *
litblob(Htab *globls, Htab *strtab, Node *n)
{
	Blob *b;

	b = mkblobseq(NULL, 0);
	blobrec(b, globls, strtab, n);
	return b;
}