-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mazeppa.h
760 lines (611 loc) · 31.9 KB
/
mazeppa.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
// Prefer writing portable and standard-compliant code. If something can be done
// easily with standard C11, do not resort to GNU C.
//
// Do not use functions and macros prefixed with `mz_priv_` or `MZ_PRIV_` in
// your code: this is private functionality. The rest of the API is public; you
// can use it just like any other library.
//
// By default, Mazeppa checks value tags before performing any operation.
// Unexpected tags cause Mazeppa to print an error message to `stderr` and abort
// the program immediately. You can define `NDEBUG` to disable these checks, but
// then illegal operations like pattern-matching on a string will result in
// undefined behaviour. Consult the language reference for more information on
// what is allowed and what is not.
#ifndef MAZEPPA_H
#define MAZEPPA_H
#ifndef __GNUC__
#error Mazeppa requires GNU language extensions.
#endif
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef NDEBUG
#define GC_DEBUG
#endif
#include "sds.h"
#include <gc.h>
#define MZ_ENUM_USER_TAGS(...) enum { mz_priv_StartSym = op_F, __VA_ARGS__ }
#define MZ_CALL_MAIN(...) op_main(MZ_PRIV_PTR_TO(mz_Value, __VA_ARGS__))
// Debugging functionality
// =============================================================================
// By default, we use `mz_Value`. However, if we need a mutable variable, we
// use `struct mz_value` instead.
typedef const struct mz_value mz_Value;
typedef const struct {
const char *file, *func;
int line;
} mz_DebugInfo;
#define MZ_DEBUG_INFO ((mz_DebugInfo){__FILE__, __func__, __LINE__})
typedef const struct {
mz_DebugInfo debug;
uint64_t expected_tag;
const char *expected_tag_name;
} mz_TagMismatch;
#define MZ_TAG_MISMATCH(variant) \
((mz_TagMismatch){MZ_DEBUG_INFO, mz_##variant##Tag, #variant})
inline static void mz_stuck(const uint64_t tag, mz_TagMismatch info) {
mz_TagMismatch i = info;
fprintf(
stderr, "%s:%d: %s: Expected tag %" PRIu64 " (`%s`), got %" PRIu64 "\n",
i.debug.file, i.debug.line, i.debug.func, i.expected_tag,
i.expected_tag_name, tag);
abort();
}
// Miscellaneous macros
// =============================================================================
#define MZ_PRIV_BOX(T, ...) MZ_PRIV_BOX_MANY(T, 1, __VA_ARGS__)
#define MZ_PRIV_BOX_MANY(T, n, ...) \
memcpy( \
mz_malloc(n * sizeof(T)), MZ_PRIV_PTR_TO(T, __VA_ARGS__), \
n * sizeof(T))
#define MZ_PRIV_PTR_TO(T, ...) ((T *)(T[]){__VA_ARGS__})
#define MZ_PRIV_DEREF(...) (*(__VA_ARGS__))
#define MZ_PRIV_STRING_SIZE(s) \
(sizeof((char[]){s}) - /* the null character */ 1)
#define MZ_PRIV_GEN_SYM(prefix, sym) MZ_PRIV_CAT_4(prefix, sym, _, __COUNTER__)
#define MZ_PRIV_CAT(a, b) MZ_PRIV_CAT_PRIMITIVE(a, b)
#define MZ_PRIV_CAT_3(a, b, c) MZ_PRIV_CAT(a, MZ_PRIV_CAT(b, c))
#define MZ_PRIV_CAT_4(a, b, c, d) \
MZ_PRIV_CAT(a, MZ_PRIV_CAT(b, MZ_PRIV_CAT(c, d)))
#define MZ_PRIV_CAT_PRIMITIVE(a, b) a##b
#define MZ_PRIV_STRINGIFY(...) MZ_PRIV_STRINGIFY_PRIMITIVE(__VA_ARGS__)
#define MZ_PRIV_STRINGIFY_PRIMITIVE(...) #__VA_ARGS__
#define MZ_PRIV_ID(...) __VA_ARGS__
#define MZ_PRIV_SND(...) MZ_PRIV_SND_AUX(__VA_ARGS__, ~)
#define MZ_PRIV_SND_AUX(_x, y, ...) y
// Fixed-width integers
// =============================================================================
typedef uint8_t mz_prim_U8;
typedef uint16_t mz_prim_U16;
typedef uint32_t mz_prim_U32;
typedef uint64_t mz_prim_U64;
typedef unsigned __int128 mz_prim_U128;
typedef int8_t mz_prim_I8;
typedef int16_t mz_prim_I16;
typedef int32_t mz_prim_I32;
typedef int64_t mz_prim_I64;
typedef __int128 mz_prim_I128;
#define MZ_U8_MIN ((mz_prim_U8)0)
#define MZ_U8_MAX UINT8_MAX
#define MZ_U16_MIN ((mz_prim_U16)0)
#define MZ_U16_MAX UINT16_MAX
#define MZ_U32_MIN ((mz_prim_U32)0)
#define MZ_U32_MAX UINT32_MAX
#define MZ_U64_MIN ((mz_prim_U64)0)
#define MZ_U64_MAX UINT64_MAX
#define MZ_U128_MIN ((unsigned __int128)0)
#define MZ_U128_MAX (~MZ_U128_MIN)
#define MZ_I8_MIN INT8_MIN
#define MZ_I8_MAX INT8_MAX
#define MZ_I16_MIN INT16_MIN
#define MZ_I16_MAX INT16_MAX
#define MZ_I32_MIN INT32_MIN
#define MZ_I32_MAX INT32_MAX
#define MZ_I64_MIN INT64_MIN
#define MZ_I64_MAX INT64_MAX
#define MZ_I128_MIN (~MZ_I128_MAX)
#define MZ_I128_MAX ((__int128)(MZ_U128_MAX >> 1))
// The minimum size of a `char` array that is able to contain an arbitrary
// integer in decimal notation. It is equal to the number of characters in the
// minimum 128-bit signed number: `-170141183460469231731687303715884105728`,
// plus the null character.
#define MZ_MAX_INT_PRINT_SIZE (40 + /* the null character */ 1)
// Macro routines for fixed-width integers
// =============================================================================
#define MZ_ENUM_INT_TYPES(X, x) \
X(U, 8, x) \
X(U, 16, x) \
X(U, 32, x) \
X(U, 64, x) \
X(I, 8, x) \
X(I, 16, x) \
X(I, 32, x) \
X(I, 64, x)
#define MZ_ENUM_INT_TYPES_EXTENDED(X, x) \
MZ_ENUM_INT_TYPES(X, x) \
X(U, 128, x) \
X(I, 128, x)
// For using from within `MZ_ENUM_INT_TYPES` and `MZ_ENUM_INT_TYPES_EXTENDED`.
// Otherwise, the macros will get blocked.
#define MZ_ENUM_INT_TYPES_2(X, x) \
X(U, 8, x) \
X(U, 16, x) \
X(U, 32, x) \
X(U, 64, x) \
X(I, 8, x) \
X(I, 16, x) \
X(I, 32, x) \
X(I, 64, x)
#define MZ_ENUM_INT_TYPES_EXTENDED_2(X, x) \
MZ_ENUM_INT_TYPES_2(X, x) \
X(U, 128, x) \
X(I, 128, x)
#define MZ_ENUM_INT_OP2(X, x) \
X(add, x) \
X(sub, x) \
X(mul, x) \
X(div, x) \
X(rem, x) \
X(bit_or, x) \
X(bit_and, x) \
X(bit_xor, x) \
X(shift_left, x) \
X(shift_right, x) \
X(equal, x) \
X(not_equal, x) \
X(greater_than, x) \
X(greater_than_equal, x) \
X(less_than, x) \
X(less_than_equal, x)
#define MZ_PRIV_IS_SIGNED(signedness) MZ_PRIV_IS_SIGNED_##signedness
#define MZ_PRIV_IS_SIGNED_U false
#define MZ_PRIV_IS_SIGNED_I true
#define MZ_PRIV_INT_FMT(signedness, bitness) \
MZ_PRIV_INT_FMT_##signedness(bitness)
#define MZ_PRIV_INT_FMT_U(bitness) PRIu##bitness
#define MZ_PRIV_INT_FMT_I(bitness) PRId##bitness
#define MZ_PRIV_INT_FMT_TY(signedness, bitness) \
MZ_PRIV_INT_FMT_TY_##signedness(bitness)
#define MZ_PRIV_INT_FMT_TY_U(bitness) "u" #bitness
#define MZ_PRIV_INT_FMT_TY_I(bitness) "i" #bitness
#define MZ_PRIV_INT_REPR(signedness, bitness) \
MZ_PRIV_SND( \
MZ_PRIV_INT_REPR_##bitness(signedness), mz_prim_##signedness##bitness)
#define MZ_PRIV_INT_REPR_128(signedness) \
/* 128-bit integers are boxed to fit values in two machine words. */ \
~, const mz_prim_##signedness##128 *
// Garbage collection utilities
// =============================================================================
#define X(name, gc) \
inline static void *name(const size_t nbytes) { \
assert(nbytes > 0); \
\
void *mem = gc(nbytes); \
if (NULL == mem) { \
fprintf(stderr, "Failed to allocate %zu bytes\n", nbytes); \
abort(); \
} \
\
return mem; \
}
X(mz_malloc, GC_MALLOC)
X(mz_malloc_atomic, GC_MALLOC_ATOMIC)
#undef X
#define X(signedness, bitness) \
inline static const mz_prim_##signedness##bitness \
*mz_priv_box_##signedness##bitness( \
const mz_prim_##signedness##bitness x) { \
mz_prim_##signedness##bitness *const y = mz_malloc_atomic(sizeof(*y)); \
*y = x; \
return y; \
}
X(U, 128)
X(I, 128)
#undef X
// 128-bit integers utilities
// =============================================================================
inline static char *
mz_priv_sprint_u128(const unsigned __int128 n, char *restrict buffer) {
if (n >= 10) {
buffer = mz_priv_sprint_u128(n / 10, buffer);
}
*(buffer++) = "0123456789"[n % 10];
return buffer;
}
inline static char *
mz_priv_sprint_i128(const __int128 n, char *restrict buffer) {
if (n < 0) {
*(buffer++) = '-';
}
unsigned __int128 unsigned_n = 0;
if (MZ_I128_MIN == n) {
// `MZ_I128_MIN` is irrepresentable as a positive integer.
unsigned_n = (unsigned __int128)MZ_I128_MAX + 1;
} else if (n < 0) {
unsigned_n = (unsigned __int128)-n;
} else {
unsigned_n = (unsigned __int128)n;
}
return mz_priv_sprint_u128(unsigned_n, buffer);
}
// Language values
// =============================================================================
typedef mz_Value *const mz_EnvPtr, *const mz_ArgsPtr;
typedef const struct mz_thunk {
mz_Value (*callback)(mz_EnvPtr env);
mz_Value *env;
} mz_Thunk;
#define ENUM_VALUE_VARIANTS \
MZ_ENUM_INT_TYPES_EXTENDED(X_INT, ~) \
X(String, sds) \
X(Thunk, mz_Thunk *)
#define X_INT(signedness, bitness, _dummy) \
X(signedness##bitness, MZ_PRIV_INT_REPR(signedness, bitness))
#define X(variant, _T) mz_##variant##Tag,
// clang-format off
enum {
ENUM_VALUE_VARIANTS
op_T, op_F
}; // clang-format on
#undef X
// The value representation is a 16-byte "fat pointer": the first 64 bits are
// for the tag, the last 64 bits are for the data. There are a lot of possible
// representations (see [1]); our choice has a merit of simplicity and the
// ability to have unboxed data of 64 bits and less. The single tag field is
// used both for built-in types (such as integers/strings) and user-defined
// constructors (such as `Nil` or `Cons`).
//
// [1] Gudeman, David. Representing type information in dynamically typed
// languages. Univ., 1995.
struct mz_value {
uint64_t tag;
#define X(variant, T) T variant;
// clang-format off
union {
ENUM_VALUE_VARIANTS
struct mz_value *payload;
}; // clang-format on
#undef X
};
static_assert(
sizeof(mz_Value) == 16, "The value representation must be two words long");
#undef ENUM_VALUE_VARIANTS
#undef X_INT
#define MZ_GET_UNCHECKED(variant, ...) \
MZ_PRIV_SND(MZ_GET_UNCHECKED_##variant, MZ_PRIV_ID)((__VA_ARGS__).variant)
#define MZ_GET_UNCHECKED_U128 ~, MZ_PRIV_DEREF
#define MZ_GET_UNCHECKED_I128 ~, MZ_PRIV_DEREF
#ifdef NDEBUG
#define MZ_GET MZ_GET_UNCHECKED
#define MZ_UNEXPECTED_TAG(_tag) __builtin_unreachable()
#else
#define MZ_GET(variant, ...) \
MZ_PRIV_GET_SYM(MZ_PRIV_GEN_SYM(MZ_GET_, val), variant, __VA_ARGS__)
#define MZ_PRIV_GET_SYM(sym, variant, ...) \
({ \
mz_Value sym = (__VA_ARGS__); \
if (mz_##variant##Tag != sym.tag) { \
mz_stuck(sym.tag, MZ_TAG_MISMATCH(variant)); \
} \
MZ_GET_UNCHECKED(variant, sym); \
})
#define MZ_UNEXPECTED_TAG(tag) \
(fprintf(stderr, "An unexpected tag: %" PRIu64 "\n", tag), abort())
#endif // NDEBUG
// Value forcing takes place whenever a variable coming from a constructor
// pattern is used somewhere in the code.
inline static mz_Value mz_force(mz_Value v) {
mz_Thunk *const thunk = MZ_GET(Thunk, v);
return thunk->callback(thunk->env);
}
// Value constructors
// =============================================================================
#define MZ_VALUE(variant, ...) \
((mz_Value){mz_##variant##Tag, .variant = __VA_ARGS__})
#define MZ_INT(signedness, bitness, x) mz_priv_##signedness##bitness(x)
#define MZ_INT_EXPLICIT_CAST(signedness, bitness, x) \
MZ_INT(signedness, bitness, (mz_prim_##signedness##bitness)x)
#define X(signedness, bitness, _dummy) \
inline static mz_Value mz_priv_##signedness##bitness( \
const mz_prim_##signedness##bitness x) { \
return MZ_VALUE( \
signedness##bitness, \
MZ_PRIV_SND(X_##bitness(signedness), MZ_PRIV_ID)(x)); \
}
#define X_128(signedness) ~, mz_priv_box_##signedness##128
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_128
#define MZ_STRING(s) MZ_VALUE(String, sdsnewlen(s, MZ_PRIV_STRING_SIZE(s)))
// GCC does not allow arbitrary 128-bit literals, so we have to construct them
// from the high and low 64-bit parts.
#define X(signedness) \
inline static mz_prim_##signedness##128 mz_##signedness##128_of_parts( \
const mz_prim_U64 high, const mz_prim_U64 low) { \
return (mz_prim_##signedness##128)high << 64 | \
(mz_prim_##signedness##128)low; \
}
X(U)
X(I)
#undef X
#define MZ_DATA(tag, nvalues, ...) \
((mz_Value){ \
tag, .payload = MZ_PRIV_BOX_MANY(mz_Value, nvalues, __VA_ARGS__)})
#define MZ_EMPTY_DATA(tag) ((mz_Value){tag, .payload = NULL})
#define MZ_BOOL(b) ((b) ? MZ_EMPTY_DATA(op_T) : MZ_EMPTY_DATA(op_F))
#define MZ_THUNK(callback, nvalues, ...) \
MZ_VALUE( \
Thunk, \
MZ_PRIV_BOX( \
mz_Thunk, \
{callback, MZ_PRIV_BOX_MANY(mz_Value, nvalues, __VA_ARGS__)}))
#define MZ_EMPTY_THUNK(callback) \
MZ_VALUE(Thunk, MZ_PRIV_BOX(mz_Thunk, {callback, NULL}))
#define MZ_SIMPLE_THUNK(x) MZ_THUNK(mz_priv_env_var, 1, x)
#define MZ_SIMPLE_THUNK_LAZY(x) MZ_THUNK(mz_priv_env_var_lazy, 1, x)
// Here `inline` is used to suppress the unused function warning.
inline static mz_Value mz_priv_env_var(mz_EnvPtr env) { return env[0]; }
inline static mz_Value mz_priv_env_var_lazy(mz_EnvPtr env) {
return mz_force(env[0]);
}
// Unary operators
// =============================================================================
inline static mz_Value mz_string_of(mz_Value v);
inline static mz_Value mz_panic(mz_Value v) {
switch (v.tag) {
#define X(signedness, bitness, _dummy) \
case mz_##signedness##bitness##Tag: \
fprintf( \
stderr, "Execution panic: %s%s\n", mz_string_of(v).String, \
MZ_PRIV_INT_FMT_TY(signedness, bitness)); \
break;
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
case mz_StringTag:;
const sds s = sdscatrepr(sdsempty(), v.String, sdslen(v.String));
fprintf(stderr, "Execution panic: %s\n", s);
break;
case mz_ThunkTag:
fprintf(stderr, "Execution panic: <opaque thunk>\n");
break;
default:
fprintf(
stderr, "Execution panic: <opaque data> (tag %" PRIu64 ")\n",
v.tag);
break;
}
abort();
}
#define X(signedness, bitness, _dummy) \
inline static mz_Value mz_##signedness##bitness##_cast(mz_Value v) { \
typedef mz_prim_##signedness##bitness TargetTy; \
TargetTy y = 0; \
\
/* Fix some constants for `X_CASE`. Constant propagation & folding \
* should optimize these variables away. */ \
static const TargetTy target_max = MZ_##signedness##bitness##_MAX; \
static const bool is_signed_target = MZ_PRIV_IS_SIGNED(signedness); \
static const int target_bitness = bitness; \
\
MZ_ENUM_INT_TYPES_EXTENDED_2(X_CASE, ~) \
MZ_UNEXPECTED_TAG(v.tag); \
\
done: \
return MZ_INT(signedness, bitness, y); \
}
#define X_CASE(signedness, bitness, _dummy) \
if (mz_##signedness##bitness##Tag == v.tag) { \
typedef mz_prim_##signedness##bitness SourceTy; \
const SourceTy x = MZ_GET_UNCHECKED(signedness##bitness, v); \
y = (TargetTy)x; \
if ((SourceTy)y != x || \
(MZ_PRIV_IS_SIGNED(signedness) && !is_signed_target && x < 0) || \
(!MZ_PRIV_IS_SIGNED(signedness) && is_signed_target && \
target_bitness <= bitness && x > (SourceTy)target_max)) { \
return mz_panic(MZ_STRING("out of range")); \
} \
goto done; \
}
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_CASE
inline static mz_Value mz_bit_not(mz_Value v) {
switch (v.tag) {
#define X(signedness, bitness, _dummy) \
case mz_##signedness##bitness##Tag: \
return MZ_INT( \
signedness, bitness, ~MZ_GET_UNCHECKED(signedness##bitness, v));
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
default:
MZ_UNEXPECTED_TAG(v.tag);
}
}
inline static mz_Value mz_string_of(mz_Value v) {
char buffer[MZ_MAX_INT_PRINT_SIZE] = {0};
switch (v.tag) {
#define X(signedness, bitness, _dummy) \
case mz_##signedness##bitness##Tag: \
sprintf( \
buffer, "%" MZ_PRIV_INT_FMT(signedness, bitness), \
MZ_GET_UNCHECKED(signedness##bitness, v)); \
break;
MZ_ENUM_INT_TYPES(X, ~)
#undef X
case mz_U128Tag:
mz_priv_sprint_u128(*v.U128, buffer);
break;
case mz_I128Tag:
mz_priv_sprint_i128(*v.I128, buffer);
break;
case mz_StringTag:
return v;
default:
MZ_UNEXPECTED_TAG(v.tag);
}
return MZ_VALUE(String, sdsnew(buffer));
}
inline static mz_Value mz_string_of_char(mz_Value v) {
const char x = (char)MZ_GET(U8, v);
return MZ_VALUE(String, sdsnewlen(&x, 1));
}
inline static mz_Value mz_length_of(mz_Value v) {
const sds s = MZ_GET(String, v);
return MZ_INT_EXPLICIT_CAST(U, 64, sdslen(s));
}
// Binary operators
// =============================================================================
// The order of evaluation of arguments is unspecified by C; this macro
// preserves the left-to-right order of Mazeppa.
#define MZ_OP2(t1, op, t2) \
MZ_PRIV_OP2_SYM(MZ_PRIV_GEN_SYM(MZ_OP2_, seq_point), t1, op, t2)
#define MZ_PRIV_OP2_SYM(sym, t1, op, t2) \
({ \
/* Force a sequence point for `t1`. */ \
mz_Value sym = t1; \
mz_priv_##op(sym, t2); \
})
#define X(signedness, bitness, _dummy) \
X_OP(add, __builtin_add_overflow, signedness, bitness) \
X_OP(sub, __builtin_sub_overflow, signedness, bitness) \
X_OP(mul, __builtin_mul_overflow, signedness, bitness)
#define X_OP(op, builtin_f, signedness, bitness) \
inline static mz_Value mz_priv_##signedness##bitness##_##op( \
mz_prim_##signedness##bitness x, mz_Value v2) { \
const mz_prim_##signedness##bitness y = \
MZ_GET(signedness##bitness, v2); \
\
mz_prim_##signedness##bitness z = 0; \
if (builtin_f(x, y, &z)) { \
return mz_panic(MZ_STRING("out of range")); \
} else { \
return MZ_INT(signedness, bitness, z); \
} \
}
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_OP
#define X(signedness, bitness, _dummy) \
X_OP(div, signedness, bitness, x / y) \
X_OP(rem, signedness, bitness, x % y)
#define X_OP(op, signedness, bitness, ...) \
inline static mz_Value mz_priv_##signedness##bitness##_##op( \
mz_prim_##signedness##bitness x, mz_Value v2) { \
const mz_prim_##signedness##bitness y = \
MZ_GET(signedness##bitness, v2); \
\
if (0 == y) { \
/* Division by zero. */ \
return mz_panic(MZ_STRING("out of range")); \
} else if (X_CHECK_OVERFLOW_##signedness(bitness, x, y)) { \
/* Overflow. */ \
return mz_panic(MZ_STRING("out of range")); \
} else { \
return MZ_INT(signedness, bitness, (__VA_ARGS__)); \
} \
}
#define X_CHECK_OVERFLOW_U(_bitness, _x, _y) false
#define X_CHECK_OVERFLOW_I(bitness, x, y) MZ_I##bitness##_MIN == x && -1 == y
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_OP
#undef X_CHECK_OVERFLOW_U
#undef X_CHECK_OVERFLOW_I
#define X(signedness, bitness, _dummy) \
X_OP(bit_or, signedness, bitness, MZ_INT(signedness, bitness, x | y)) \
X_OP(bit_and, signedness, bitness, MZ_INT(signedness, bitness, (x & y))) \
X_OP(bit_xor, signedness, bitness, MZ_INT(signedness, bitness, x ^ y)) \
X_OP(equal, signedness, bitness, MZ_BOOL(x == y)) \
X_OP(not_equal, signedness, bitness, MZ_BOOL(x != y)) \
X_OP(greater_than, signedness, bitness, MZ_BOOL(x > y)) \
X_OP(greater_than_equal, signedness, bitness, MZ_BOOL(x >= y)) \
X_OP(less_than, signedness, bitness, MZ_BOOL(x < y)) \
X_OP(less_than_equal, signedness, bitness, MZ_BOOL(x <= y))
#define X_OP(op, signedness, bitness, ...) \
inline static mz_Value mz_priv_##signedness##bitness##_##op( \
mz_prim_##signedness##bitness x, mz_Value v2) { \
const mz_prim_##signedness##bitness y = \
MZ_GET(signedness##bitness, v2); \
\
return __VA_ARGS__; \
}
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_OP
#define X(signedness, bitness, _dummy) \
X_OP(shift_left, signedness, bitness, x << y) \
X_OP(shift_right, signedness, bitness, x >> y)
#define X_OP(op, signedness, bitness, ...) \
inline static mz_Value mz_priv_##signedness##bitness##_##op( \
mz_prim_##signedness##bitness x, mz_Value v2) { \
const mz_prim_##signedness##bitness y = \
MZ_GET(signedness##bitness, v2); \
\
if (y < 0) { \
/* Underflow. */ \
return mz_panic(MZ_STRING("out of range")); \
} else if (y >= bitness) { \
/* Overflow. */ \
return mz_panic(MZ_STRING("out of range")); \
} else { \
return MZ_INT_EXPLICIT_CAST(signedness, bitness, (__VA_ARGS__)); \
} \
}
MZ_ENUM_INT_TYPES_EXTENDED(X, ~)
#undef X
#undef X_OP
#define X(op, _dummy) \
inline static mz_Value mz_priv_##op(mz_Value v1, mz_Value v2) { \
switch (v1.tag) { \
MZ_ENUM_INT_TYPES_EXTENDED(X_INT_CASE, op) \
MZ_PRIV_SND(X_CASE_##op, /* empty */); \
\
default: \
MZ_UNEXPECTED_TAG(v1.tag); \
} \
}
#define X_INT_CASE(signedness, bitness, op) \
case mz_##signedness##bitness##Tag: \
return mz_priv_##signedness##bitness##_##op( \
MZ_GET_UNCHECKED(signedness##bitness, v1), v2);
#define X_STRING_CASE(cmp) \
case mz_StringTag: \
return MZ_BOOL(sdscmp(v1.String, MZ_GET(String, v2)) cmp 0)
#define X_CASE_equal ~, X_STRING_CASE(==)
#define X_CASE_not_equal ~, X_STRING_CASE(!=)
#define X_CASE_greater_than ~, X_STRING_CASE(>)
#define X_CASE_greater_than_equal ~, X_STRING_CASE(>=)
#define X_CASE_less_than ~, X_STRING_CASE(<)
#define X_CASE_less_than_equal ~, X_STRING_CASE(<=)
MZ_ENUM_INT_OP2(X, ~)
#undef X
#undef X_INT_CASE
#undef X_STRING_CASE
#undef X_CASE_equal
#undef X_CASE_not_equal
#undef X_CASE_greater_than
#undef X_CASE_greater_than_equal
#undef X_CASE_less_than
#undef X_CASE_less_than_equal
inline static mz_Value mz_priv_plus_plus(mz_Value v1, mz_Value v2) {
const sds s1 = MZ_GET(String, v1);
const sds s2 = MZ_GET(String, v2);
return MZ_VALUE(String, sdscatsds(sdsdup(s1), s2));
}
inline static mz_Value mz_priv_get(mz_Value v1, mz_Value v2) {
const sds s = MZ_GET(String, v1);
const uint64_t i = MZ_GET(U64, v2);
if (i >= sdslen(s)) {
return mz_panic(MZ_STRING("out of bounds"));
}
return MZ_INT_EXPLICIT_CAST(U, 8, s[i]);
}
// TODO: implement floating-point types through FFI. Floats can be represented
// as unsigned integers via type punning.
// TODO: implement modular arithmetic in a similar way.
#endif // MAZEPPA_H