-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmeta.capy
511 lines (409 loc) · 12.2 KB
/
meta.capy
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
core :: mod "core";
// returns the underlying bit representation of the type id.
// builtin
meta_to_raw :: (ty: type) -> u32 extern;
Layout :: struct {
size: usize,
align: usize,
};
// builtin
array_layouts : [] Layout : extern;
// builtin
distinct_layouts : [] Layout : extern;
// builtin
struct_layouts : [] Layout : extern;
// builtin
pointer_layout : Layout : extern;
// The amount of space a type takes up in memory.
//
// i8 takes 1 byte to store, i32 takes four bytes.
//
// For structs it is the size of all the fields in a struct, plus any padding between those fields.
// (see `align_of` for more info on padding/alignment)
//
// The size of this struct,
//
// struct {
// a: i64, // 8 bytes (align = 8)
// b: i32, // 4 bytes (align = 4)
// }
//
// would be 12 bytes
//
// The size of this struct,
//
// struct {
// a: i32, // 4 bytes (align = 4)
// // 4 bytes of padding here
// b: i64, // 8 bytes (align = 8)
// }
//
// would be 16 bytes
//
// If you need to store multiple structs in a row (as in an array or buffer),
// `stride_of` is the function to use
size_of :: (ty: type) -> usize {
ty := meta_to_raw(ty);
// the discriminant is the leftmost 6 bits
discriminant := ty >> 26;
// all the discriminants < 16 are simple
// (they contain all relevant information as bit flags)
if discriminant < 16 {
// the first 5 bits in a simple type id are the size
(ty & 0b11111) as usize
} else {
// about 5 types have complex id's
// (they contain only a discriminant and an index)
layouts := if discriminant == 16 {
struct_layouts
} else if discriminant == 17 {
distinct_layouts
} else if discriminant == 18 {
array_layouts
} else if discriminant == 19 {
// slice
return pointer_layout.size * 2;
} else {
// it must be either a pointer, or a function
// either way, the size is the same
return pointer_layout.size;
};
// removes the discriminant (6 bits)
index := ty &~ (0b111111 << 26);
layouts[index].size
}
}
// The stride of a type is the space allocated for a single element in an array.
//
// Imagine a struct like so:
//
// struct {
// a: i64, // 8 bytes (align = 8)
// b: i32, // 4 bytes (align = 4)
// }
//
// The size of this struct is 12 bytes and its alignment is 8.
// If we naively allocated an array for this struct using its size (12 bytes), it would look like this,
//
// 0 12 24 36
// [ s ][ s ][ s ]
//
// But wait! This struct's alignment is 8 and the second element of this array starts on address 12.
// 12 is not a multiple of 8, and some architectures might complain if try to access the second field.
// So some padding needs to be added in between our structs.
//
// The "stride" of a type is the size plus the padding needed for the next struct.
// For our example, it would be 12 bytes plus the padding needed to bring that size to the next multiple of 8.
//
// In memory, an array which allocates using stride would look like this:
//
// 0 12 16 28 32 44 48
// [ s ][ p ][ s ][ p ][ s ][ p ]
//
// As you can see, all of the structs in the array start at addresses which are multiples of 8 (0, 16, 32).
//
// So when allocating space for arrays/buffers, allocate n * stride_of(T).
stride_of :: (ty: type) -> usize {
mask := align_of(ty) - 1;
{size_of(ty) + mask} &~ mask
}
// Most types must appear in addresses that are a multiple of a certain "alignment".
// This is a restriction of the underlying architecture.
//
// The alignment of `i16` is 2, so a value of type `i16`
// can only exist on addresses which are multiples of 2.
// If we put an `i16` on a bad address, some architectures might complain.
//
// For example, take the following struct,
//
// struct {
// a: i8, // 1 byte (align = 1)
// b: i16, // 2 bytes (align = 2)
// }
//
// If we naively laid this struct out in memory it'd look like this:
//
// 0 1 2 4
// [ a ][ b ]
//
// But wait! `b` (which is an `i16`) starts at address 1, but that isn't a multiple of 2.
// Our computer isn't going to like this!
//
// In order to ensure that `b` starts at an address which is a multiple of 2,
// the compile will have to add padding to the struct,
//
// struct {
// a: i8, // 1 byte
// padding: i8, // 1 byte (this is a secret, hidden field)
// b: i16, // 2 bytes
// }
//
// Now the struct looks like this in memory:
//
// 0 1 2 4 5
// [ a ][ ][ b ]
// ^
// |
// \ padding here
//
// As you can see, `b` now starts at address 2, which is a multiple of it's alignment (2).
//
// The alignment of a struct is the largest alignment of all it's fields
//
// struct {
// a: i64, // 8 bytes (align = 8)
// b: i16, // 2 bytes (align = 2)
// }
//
// This struct's alignment is 8, so this struct can only appear in addresses which are multiples of 8.
//
// An alignment of `1` is accepted in all addresses (every number is a multiple of 1)
align_of :: (ty: type) -> usize {
ty := meta_to_raw(ty);
// the discriminant is the leftmost 6 bits
discriminant := ty >> 26;
// all the discriminants < 16 are simple
// (they contain all relevant information as bit flags)
if discriminant < 16 {
// the second 4 bits in a simple type id are the alignment
((ty >> 5) & 0b1111) as usize
} else {
// about 5 types have complex id's
// (they contain only a discriminant and an index)
layouts := if discriminant == 16 {
struct_layouts
} else if discriminant == 17 {
distinct_layouts
} else if discriminant == 18 {
array_layouts
} else {
// it must be either a slice, pointer, or function
// either way, the align is the same
return pointer_layout.align;
};
// removes the discriminant (6 bits)
index := ty &~ (0b111111 << 26);
layouts[index].align
}
}
// `u8`, `u16`, `u32`, `u64`, `u128`, `usize`
// `i8`, `i16`, `i32`, `i64`, `i128`, `isize`
//
// these are the available integer types for use.
// `usize` and `isize` have the same size as a pointer on the target architecture.
int_discriminant : u32 : 2;
Int_Info :: struct {
bit_width: u8,
signed: bool,
};
is_int :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == int_discriminant
}
get_int_info :: (ty: type) -> Int_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == int_discriminant,
"called `get_int_info` on non-int",
);
Int_Info.{
// the first five bits is the size in bytes
bit_width = ((raw & 0b11111) * 8) as u8,
// the ninth bit is the sign flag
signed = ((raw >> 9) & 1) as bool,
}
}
// `f32`, `f64`
//
// these are the available floating point types for use
float_discriminant : u32 : 3;
Float_Info :: struct {
bit_width: u8,
};
is_float :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == float_discriminant
}
get_float_info :: (ty: type) -> Float_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == float_discriminant,
"called `get_float_info` on non-float",
);
Float_Info.{
// the first five bits is the size in bytes
bit_width = ((raw & 0b11111) * 8) as u8,
}
}
// `bool`
bool_discriminant : u32 : 4;
is_bool :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == bool_discriminant
}
// `str`
string_discriminant : u32 : 5;
is_string :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == string_discriminant
}
// `char`
char_discriminant : u32 : 6;
is_char :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == char_discriminant
}
// `[6] i32`
// `[10] u8`
// ...
//
// A standard array of data, layed out linearly in memory.
array_discriminant : u32 : 18;
array_infos : [] Array_Info : extern;
Array_Info :: struct {
len: usize,
ty: type,
};
is_array :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == array_discriminant
}
get_array_info :: (ty: type) -> Array_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == array_discriminant,
"called `get_array_info` on non-array",
);
// removes the discriminant (6 bits)
idx := raw &~ (0b111111 << 26);
array_infos[idx]
}
// `[] i8`
// `[] char`
// ...
//
// The slice. A reference to an array of any size. It contains only a pointer to the array, and the length of the array.
slice_discriminant : u32 : 19;
slice_infos : [] Slice_Info : extern;
Slice_Info :: struct {
ty: type,
};
is_slice :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == slice_discriminant
}
get_slice_info :: (ty: type) -> Slice_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == slice_discriminant,
"called `get_slice_info` on non-slice",
);
// removes the discriminant (6 bits)
idx := raw &~ (0b111111 << 26);
slice_infos[idx]
}
// `^i32`
// `^any`
// ...
pointer_discriminant : u32 : 20;
pointer_infos : [] Pointer_Info : extern;
Pointer_Info :: struct {
ty: type,
};
is_pointer :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == pointer_discriminant
}
get_pointer_info :: (ty: type) -> Pointer_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == pointer_discriminant,
"called `get_pointer_info` on non-pointer",
);
// removes the discriminant (6 bits)
idx := raw &~ (0b111111 << 26);
pointer_infos[idx]
}
// `distinct i32`
// `distinct bool`
// ...
//
// The distinct, A unique type with the same underlying semantics of it's sub-type
distinct_discriminant : u32 : 17;
distinct_infos : [] Distinct_Info : extern;
Distinct_Info :: struct {
ty: type,
};
is_distinct :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == distinct_discriminant
}
get_distinct_info :: (ty: type) -> Distinct_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == distinct_discriminant,
"called `get_distinct_info` on non-distinct",
);
// removes the discriminant (6 bits)
idx := raw &~ (0b111111 << 26);
distinct_infos[idx]
}
// `type`
//
// a "meta type". types are first-class values and `i32` when used as a value has the type of `type`
meta_type_discriminant : u32 : 7;
is_meta_type :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == meta_type_discriminant
}
// `any`
//
// can represent any arbitrary but unknown value.
// cannot be directly obtained, but only held through `^any`, `[] any`, etc.
any_discriminant : u32 : 8;
is_any :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == any_discriminant
}
// the file type
//
// this is perhaps the most useless type, but it is here for only covering all the possibilities.
// when `mod "core"`, or `import "file.capy"` are used as values, their type is `file`.
// no two `file` types are the same, `import "a.capy"` and `import "b.capy"` are really two unique types.
//
// If you're saying "this is a strange way to implement first-class imports", then you're probably right :)
file_discriminant : u32 : 9;
is_file :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == file_discriminant
}
// `() -> void`
// `(x: i32) -> bool`
// ...
//
// the function type is identical to an actual function except that it lacks a body
function_discriminant : u32 : 21;
is_function :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == function_discriminant
}
// `struct { a: i32, b: i32 }`
// `struct { foo: str }`
// ...
struct_discriminant : u32 : 16;
struct_infos : [] Struct_Info : extern;
Struct_Info :: struct {
members: [] Member_Info,
};
Member_Info :: struct {
name: str,
ty: type,
offset: usize,
};
is_struct :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == struct_discriminant
}
get_struct_info :: (ty: type) -> Struct_Info {
raw := meta_to_raw(ty);
core.assert_with(
raw >> 26 == struct_discriminant,
"called `get_struct_info` on non-struct",
);
// removes the discriminant (6 bits)
idx := raw &~ (0b111111 << 26);
struct_infos[idx]
}
// `void`
//
// an empty type, a `u0`
void_discriminant : u32 : 1;
is_void :: (ty: type) -> bool {
meta_to_raw(ty) >> 26 == void_discriminant
}