summaryrefslogtreecommitdiff
path: root/libstd/introspect.myr
blob: 1da7af008dec24ef9f95858ca45d917c93058162 (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
use "types.use"
use "die.use"

pkg std =
	type typedesc = union
		`Tynone

		/* atomic types */
		`Tyvoid
		`Tybool
		`Tychar

		`Tyint8
		`Tyint16
		`Tyint
		`Tyint32
		`Tyint64
		`Tylong

		`Tybyte
		`Tyuint8
		`Tyuint16
		`Tyuint
		`Tyuint32
		`Tyuint64
		`Tyulong
		`Tyflt32
		`Tyflt64
		`Tyvalist

		/* compound types */
		`Typtr byte[:]
		`Tyfunc	typecursor
		`Tyslice byte[:]
		`Tyarray (size, byte[:])

		/* aggregate types */
		`Tytuple	typecursor
		`Tystruct	namecursor
		`Tyunion	namecursor
		/* name info */
		`Tyname (byte[:], byte[:])
	;;

	type typecursor = struct
		nelt	: size
		rem	: byte[:]
	;;

	type namecursor = struct
		nelt	: size
		rem	: byte[:]
	;;

	type typeinfo = struct
		size	: size
		align	: size
	;;

	generic typeof	: (v : @a -> byte[:])
	const typeenc	: (p : ...# -> typecursor)
	const typedecode	: (e : byte[:] -> typedesc)
	const typedesc	: (e : byte[:] -> typedesc)
	const typeinfo	: (e : byte[:] -> typeinfo)

	const tcnext	: (t : typecursor# -> byte[:])
	const tcpeek	: (t : typecursor# -> byte[:])
	const ncnext	: (t : namecursor# -> (byte[:], byte[:]))
;;

extern const put	: (fmt : byte[:], args : ... -> size)

const Encnone	: byte = 0
const Encvoid	: byte = 1
const Encbool	: byte = 2
const Encchar	: byte = 3

const Encint8	: byte = 4
const Encint16	: byte = 5
const Encint	: byte = 6
const Encint32	: byte = 7
const Encint64	: byte = 8
const Enclong	: byte = 9

const Encbyte	: byte = 10
const Encuint8	: byte = 11
const Encuint16	: byte = 12
const Encuint	: byte = 13
const Encuint32	: byte = 14
const Encuint64	: byte = 15
const Enculong	: byte = 16
const Encflt32	: byte = 17
const Encflt64	: byte = 18
const Encvalist	: byte = 19

/* compound types */
const Encptr	: byte = 20
const Encfunc	: byte = 21
const Encslice	: byte = 22
const Encarray	: byte = 23

/* aggregate types */
const Enctuple	: byte = 24
const Encstruct	: byte = 25
const Encunion	: byte = 26
const Encname	: byte = 30
const Encindname	:byte = 30 | 0x80

generic typeof = {v : @a -> byte[:]
	var tc

	tc = typesof(v)
	-> tcnext(&tc)
}

const typeenc = {ap : ...#
	var e

	e = getenc(ap castto(byte##))
	e = skiptypeinfo(e[1:])
	-> lentypecursor(e)
}
	
const typesof : (a : ... -> typecursor) = {a : ...
	-> typeenc(&a)
}

const tcnext = {tc
	var n, sz, cur

	if tc.rem.len == 0
		-> ""
	;;
	(n, sz) = getipacked(tc.rem)
	cur = tc.rem[sz:sz+n]
	tc.rem = tc.rem[sz+n:]
	-> cur
}

const tcpeek = {tc
	var n, sz

	if tc.rem.len == 0
		-> ""
	;;
	(n, sz) = getipacked(tc.rem)
	-> tc.rem[sz:sz+n]
}

const ncnext = {nc
	var n, sz, name, enc

	if nc.rem.len == 0
		-> ("", "")
	;;

	/* get the name */
	(n, sz) = getipacked(nc.rem)
	name = nc.rem[sz:sz+n]
	nc.rem = nc.rem[sz+n:]

	/* and the type */
	(n, sz) = getipacked(nc.rem)
	enc = nc.rem[sz:sz+n]
	nc.rem = nc.rem[sz+n:]
	-> (name, enc)
}


const getenc = {p : byte##
	var val, sz, x

	(val, sz) = getipacked(p#[:8])
	x = &sz castto(byte#)
	-> p#[sz:sz+val]
}

const typedesc = {ti
	var len,sz, p

	match ti[0]
	| Encnone:	-> `Tynone
	| Encvoid:	-> `Tyvoid
	| Encbool:	-> `Tybool
	| Encchar:	-> `Tychar

	| Encint8:	-> `Tyint8
	| Encint16:	-> `Tyint16
	| Encint:	-> `Tyint
	| Encint32:	-> `Tyint32
	| Encint64:	-> `Tyint64
	| Enclong:	-> `Tylong

	| Encbyte:	-> `Tybyte
	| Encuint8:	-> `Tyuint8
	| Encuint16:	-> `Tyuint16
	| Encuint:	-> `Tyuint
	| Encuint32:	-> `Tyuint32
	| Encuint64:	-> `Tyuint64
	| Enculong:	-> `Tyulong
	| Encflt32:	-> `Tyflt32
	| Encflt64:	-> `Tyflt64
	| Encvalist:	-> `Tyvalist

	/* compound types */
	| Encptr:	-> `Typtr getsub(ti[1:])
	| Encfunc:	-> `Tyfunc lentypecursor(ti[1:])
	| Encslice:	-> `Tyslice getsub(ti[1:])
	| Encarray:
		ti = skiptypeinfo(ti[1:])
		(len, sz) = getipacked(ti)
		-> `Tyarray (len, getsub(ti[sz:]))


	/* aggregate types */
	| Enctuple:
		ti = skiptypeinfo(ti[1:])
		-> `Tytuple lentypecursor(ti)
	| Encstruct:
		ti = skiptypeinfo(ti[1:])
		-> `Tystruct lennamecursor(ti)
	| Encunion:
		ti = skiptypeinfo(ti[1:])
		-> `Tyunion lennamecursor(ti)
	| Encname:
		-> `Tyname namedesc(ti[1:])
	| Encindname:
		/* 
		ugly hack: the slice contains a pointer to the
		value, so if we cast it to a byte##, we can
		pull the indirect value out of the pointer.
		*/
		p = ti[1:] castto(byte##)
		-> typedesc(getenc(p))
	| _:
		std.die("unknown type encoding")
	;;
}

const typeinfo = {ti
	var p

	match ti[0]
	| Encnone:	-> [.size=0, .align=1]
	| Encvoid:	-> [.size=0, .align=1]
	| Encbool:	-> [.size=0, .align=1]
	| Encchar:	-> [.size=4, .align=4]

	| Encint8:	-> [.size=1, .align=1]
	| Encint16:	-> [.size=2, .align=2]
	| Encint:	-> [.size=4, .align=4]
	| Encint32:	-> [.size=4, .align=4]
	| Encint64:	-> [.size=8, .align=8]
	| Enclong:	-> [.size=8, .align=8]

	| Encbyte:	-> [.size=1, .align=1]
	| Encuint8:	-> [.size=1, .align=1]
	| Encuint16:	-> [.size=2, .align=2]
	| Encuint:	-> [.size=4, .align=4]
	| Encuint32:	-> [.size=4, .align=4]
	| Encuint64:	-> [.size=8, .align=8]
	| Enculong:	-> [.size=8, .align=8]
	| Encflt32:	-> [.size=4, .align=4]
	| Encflt64:	-> [.size=8, .align=8]
	| Encvalist:	-> [.size=8, .align=8]

	/* compound types */
	| Encptr:	-> [.size=8, .align=8]
	| Encfunc:	-> [.size=8, .align=8]
	| Encslice:	-> [.size=16, .align=8]

	| Encarray: 	-> gettypeinfo(ti[1:])
	| Enctuple:	-> gettypeinfo(ti[1:])
	| Encstruct:	-> gettypeinfo(ti[1:])
	| Encunion:	-> gettypeinfo(ti[1:])
	| Encname:	-> getnameinfo(ti[1:])
	| Encindname:
		p = ti[1:] castto(byte##)
		-> typeinfo(getenc(p))
	| _:
		std.die("unknown type encoding")
	;;
}

const gettypeinfo = {e
	var size, align, sz

	(size, sz) = getipacked(e)	/* size */
	e = e[sz:]
	(align, sz) = getipacked(e)	/* align */
	-> [.size = size, .align = align]
}

const skiptypeinfo = {e
	var ignore, sz

	(ignore, sz) = getipacked(e)	/* size */
	e = e[sz:]
	(ignore, sz) = getipacked(e)	/* align */
	-> e[sz:]
}

const getnameinfo = {e
	var n, name, sz, enc

	(n, sz) = getipacked(e)
	name = e[sz:n+sz]
	e = e[n+sz:]
	(n, sz) = getipacked(e)
	enc = e[sz:n+sz]

	-> typeinfo(enc)
}

const namedesc = {e
	var n, sz, name, enc

	(n, sz) = getipacked(e)
	name = e[sz:n+sz]
	e = e[n+sz:]
	(n, sz) = getipacked(e)
	enc = e[sz:n+sz]

	-> (name, enc)
}

const lentypecursor = {e
	var n, sz

	(n, sz) = getipacked(e)
	-> [.nelt=n, .rem=e[sz:]]
}

const lennamecursor = {e 
	var n, sz

	(n, sz) = getipacked(e)
	-> [.nelt=n, .rem=e[sz:]]
}

const getsub = {e
	var n, sz

	(n, sz) = getipacked(e)
	-> e[sz:sz+n]
}

const getipacked : (p : byte[:] -> (size, size)) = {p : byte[:]
	var mask, val, len, i

	mask = 0x80
	val = 0
	len = 1
	while p[0] & mask != mask << 1
		len++
		mask >>= 1
		mask |= 0x80
	;;

	val = (p[0] castto(size))  & ~(1 << (8 - len))
	for i = 1; i < len; i++
		val |= (p[i] castto(size)) << (i*8 - len)
	;;
	-> (val, len)
}