-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalue.h
445 lines (390 loc) · 9.45 KB
/
value.h
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
#ifndef VALUE_H
#define VALUE_H
#include "prelude.h"
#include <inttypes.h>
#include "types.h"
static inline void
mass_result_set_error(
Mass_Result *result,
Mass_Error error
) {
assert(result->tag != Mass_Result_Tag_Error);
*result = (Mass_Result){ .tag = Mass_Result_Tag_Error, .Error.error = error };
}
static inline bool
mass_error(
Mass_Context *context,
Mass_Error error
) {
mass_result_set_error(context->result, error);
return false;
}
static inline bool
mass_result_is_error(
Mass_Result *result
) {
return result->tag != Mass_Result_Tag_Success;
}
#define mass_has_error(_CONTEXT_) mass_result_is_error((_CONTEXT_)->result)
static inline void *
mass_allocate_bytes(
Mass_Context *context,
u64 size,
u64 alignment
) {
return virtual_memory_buffer_allocate_bytes(&context->compilation->allocation_buffer, size, alignment);
}
#define mass_allocate(_CONTEXT_, _TYPE_) \
((_TYPE_ *)mass_allocate_bytes((_CONTEXT_), sizeof(_TYPE_), _Alignof(_TYPE_)))
static inline void *
mass_allocate_bytes_from_descriptor(
Mass_Context *context,
const Descriptor *descriptor
) {
if (!descriptor->bit_size.as_u64) return 0;
return virtual_memory_buffer_allocate_bytes(
&context->compilation->allocation_buffer,
descriptor->bit_size.as_u64 / 8,
descriptor->bit_alignment.as_u64 / 8
);
}
typedef struct {
u64 occupied;
} Temp_Mark;
static inline Temp_Mark
context_temp_mark(
Mass_Context *context
) {
return (Temp_Mark){context->compilation->temp_buffer.occupied};
}
static inline void
context_temp_reset_to_mark(
Mass_Context *context,
Temp_Mark mark
) {
context->compilation->temp_buffer.occupied = mark.occupied;
}
static inline Value_View
value_view_single(
Value **value
) {
return (Value_View) {
.values = value,
.length = 1,
.source_range = (*value)->source_range,
};
}
static inline Value_View
value_view_make_single(
const Allocator *allocator,
Value *value
) {
Value **values = allocator_allocate(allocator, Value *);
values[0] = value;
return (Value_View) {
.values = values,
.length = 1,
.source_range = value->source_range,
};
}
static inline Value *
value_view_peek(
const Value_View *view,
u64 index
) {
return index < view->length ? view->values[index] : 0;
}
static inline Value *
value_view_get(
const Value_View *view,
u64 index
) {
assert(index < view->length);
return view->values[index];
}
static inline Value *
value_view_next(
const Value_View *view,
u32 *peek_index
) {
Value *peek = value_view_peek(view, *peek_index);
if (peek) *peek_index += 1;
return peek;
}
static inline Value *
value_view_last(
const Value_View *view
) {
assert(view->length);
return value_view_get(view, view->length - 1);
}
static Value_View
value_view_slice(
const Value_View *view,
u32 start_index,
u32 end_index
) {
assert(end_index <= view->length);
assert(start_index <= end_index);
Source_Range source_range = view->source_range;
source_range.offsets.to = end_index == view->length
? view->source_range.offsets.to
: view->values[end_index]->source_range.offsets.from;
source_range.offsets.from = start_index == end_index
? source_range.offsets.to
: view->values[start_index]->source_range.offsets.from;
assert(source_range.offsets.from <= source_range.offsets.to);
return (Value_View) {
.values = view->values + start_index,
.length = end_index - start_index,
.source_range = source_range,
};
}
static inline Value_View
value_view_rest(
const Value_View *view,
u32 index
) {
return value_view_slice(view, index, view->length);
}
static inline Value_View
value_view_from_value_array(
Array_Value_Ptr value_array,
const Source_Range *source_range
) {
return (Value_View) {
.values = dyn_array_raw(value_array),
.length = u64_to_u32(dyn_array_length(value_array)),
.source_range = *source_range
};
}
#define dyn_array_copy_from_raw_memory(_TYPE_, _ALLOCATOR_, _TARGET_, _SOURCE_PTR_, _SOURCE_COUNT_)\
do {\
u64 copy_count = (_SOURCE_COUNT_);\
if (copy_count) {\
_TYPE_ copy_target = dyn_array_make(_TYPE_, .allocator = (_ALLOCATOR_), .capacity = copy_count);\
copy_target.data->length = copy_count;\
memcpy(dyn_array_raw(copy_target), (_SOURCE_PTR_), sizeof((_SOURCE_PTR_)[0]) * copy_count);\
*(_TARGET_) = copy_target;\
} else {\
*(_TARGET_) = dyn_array_static_empty(_TYPE_);\
}\
} while(0)
#define dyn_array_copy_from_temp(_TYPE_, _CONTEXT_, _TARGET_, _SOURCE_)\
do {\
_TYPE_ copy_source = (_SOURCE_);\
dyn_array_copy_from_raw_memory(\
_TYPE_, (_CONTEXT_)->allocator, (_TARGET_), dyn_array_raw(copy_source), dyn_array_length(copy_source)\
);\
} while(0)
static inline Array_Value_Ptr
value_view_to_value_array(
const Allocator *allocator,
Value_View view
) {
Array_Value_Ptr result;
dyn_array_copy_from_raw_memory(Array_Value_Ptr, allocator, &result, view.values, view.length);
return result;
}
static inline bool
storage_is_stack(
const Storage *operand
) {
return operand->tag == Storage_Tag_Memory
&& operand->Memory.location.tag == Memory_Location_Tag_Stack;
}
static inline bool
storage_is_label(
const Storage *operand
) {
return operand->tag == Storage_Tag_Memory
&& operand->Memory.location.tag == Memory_Location_Tag_Instruction_Pointer_Relative;
}
static inline Storage
storage_stack(
s32 offset,
Bits bit_size,
Stack_Area area
);
static inline bool
register_is_xmm(
Register reg
) {
return !!(reg & Register_Xmm0);
}
static inline bool
storage_equal(
const Storage *a,
const Storage *b
);
const Storage imm0 = { .tag = Storage_Tag_Immediate, .bit_size = {0} };
static Fixed_Buffer *
mass_error_to_string(
Compilation *compilation,
Mass_Error const* error
);
static inline Value *
value_init(
Value *result,
const Descriptor *descriptor,
Storage storage,
Source_Range source_range
) {
*result = (Value) {
.tag = Value_Tag_Forced,
.descriptor = descriptor,
.Forced.storage = storage,
.source_range = source_range,
};
if (descriptor && !storage_is_label(&storage)) {
assert(descriptor->bit_size.as_u64 == storage.bit_size.as_u64);
}
return result;
}
static Value *
value_make(
Mass_Context *context,
const Descriptor *descriptor,
Storage storage,
Source_Range source_range
) {
return value_init(
mass_allocate(context, Value),
descriptor,
storage,
source_range
);
}
static inline const Descriptor *
mass_expected_result_descriptor(
const Expected_Result *expected_result
) {
switch(expected_result->tag) {
case Expected_Result_Tag_Exact: return expected_result->Exact.descriptor;
case Expected_Result_Tag_Flexible: return expected_result->Flexible.descriptor;
}
panic("Unknown Expected_Result tag");
return 0;
}
static inline Expected_Result
mass_expected_result_exact(
const Descriptor *descriptor,
Storage storage
) {
return (Expected_Result) {
.tag = Expected_Result_Tag_Exact,
.Exact = { .descriptor = descriptor, .storage = storage },
};
}
static inline Expected_Result
mass_expected_result_exact_type(
Type type,
Storage storage
) {
return mass_expected_result_exact(type.descriptor, storage);
}
static inline Expected_Result
expected_result_any(
const Descriptor *descriptor
) {
return (Expected_Result){
.tag = Expected_Result_Tag_Flexible,
.Flexible = { .descriptor = descriptor },
};
}
static inline Value *
mass_make_void(
Mass_Context *context,
const Source_Range source_range
) {
Storage storage = { .tag = Storage_Tag_Immediate, .bit_size = {0} };
return value_make(context, &descriptor_void, storage, source_range);
}
static inline Value *
mass_make_never(
Mass_Context *context,
const Source_Range source_range
) {
Storage storage = { .tag = Storage_Tag_Immediate, .bit_size = {0} };
return value_make(context, &descriptor_never, storage, source_range);
}
static inline bool
descriptor_is_integer(
const Descriptor *descriptor
) {
if (!descriptor) return false;
if (descriptor->tag != Descriptor_Tag_Integer) {
return false;
}
return true;
}
static inline bool
descriptor_is_unsigned_integer(
const Descriptor *descriptor
) {
return descriptor_is_integer(descriptor) && !descriptor->Integer.is_signed;
}
static inline bool
descriptor_is_signed_integer(
const Descriptor *descriptor
) {
return descriptor_is_integer(descriptor) && descriptor->Integer.is_signed;
}
static inline bool
descriptor_is_float(
const Descriptor *descriptor
) {
if (!descriptor) return false;
if (descriptor->tag != Descriptor_Tag_Float) {
return false;
}
return true;
}
static inline const Descriptor *
maybe_unwrap_pointer_descriptor(
const Descriptor *descriptor
) {
if (descriptor->tag == Descriptor_Tag_Pointer_To) {
return descriptor->Pointer_To.descriptor;
}
return descriptor;
}
static inline u64
descriptor_byte_size(
const Descriptor *descriptor
);
static inline Descriptor *
descriptor_array_of(
const Allocator *allocator,
const Descriptor *item_descriptor,
u64 length
);
static void
source_range_print_start_position(
Compilation *compilation,
const Source_Range *source_range
);
static Slice
source_from_source_range(
Compilation *compilation,
const Source_Range *source_range
);
static inline bool
storage_is_register_index(
const Storage *storage,
Register reg_index
);
static fn_type_opaque
value_as_function(
Program *program,
Value *value
);
static inline void *
rip_value_pointer_from_label(
const Label *label
);
static void
print_storage(
const Storage *operand
);
#endif