-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsunder.h
2681 lines (2540 loc) · 89.6 KB
/
sunder.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
#ifndef SUNDER_H_INCLUDED
#define SUNDER_H_INCLUDED
#include <stdarg.h> /* va_list */
#include <stdbool.h>
#include <stddef.h> /* size_t, NULL, offsetof */
#include <stdint.h>
////////////////////////////////////////////////////////////////////////////////
//////// util.c ////////////////////////////////////////////////////////////////
struct bitarr;
struct bigint;
struct string;
#if __STDC_VERSION__ >= 201112L /* C11+ */
# define NORETURN _Noreturn
#elif defined(__GNUC__) /* GCC and Clang */
# define NORETURN __attribute__((noreturn))
#else
# define NORETURN /* nothing */
#endif
#define STRINGIFY(a) STRINGIFY_(a)
#define STRINGIFY_(a) #a
// C99 compatible max_align_t.
// clang-format off
typedef union {
_Bool bool_;
char char_;
short short_;
int int_;
long long_;
long long long_long_;
float float_;
double double_;
long double long_double_;
void* void_ptr_;
} max_align_type;
// clang-format on
// C99 compatible DECIMAL_DIG constants for IEEE-754 floating point numbers.
#define IEEE754_FLT_DECIMAL_DIG 9
#define IEEE754_DBL_DECIMAL_DIG 17
// Exact integer representation limits for IEEE-754 floating point numbers.
#define IEEE754_FLT_INTEGER_MIN -16777216 // -2**24
#define IEEE754_FLT_INTEGER_MAX +16777216 // +2**24
#define IEEE754_DBL_INTEGER_MIN -9007199254740992 // -2**53
#define IEEE754_DBL_INTEGER_MAX +9007199254740992 // +2**53
// Number of elements in an array.
#define ARRAY_COUNT(array) (sizeof(array) / sizeof((array)[0]))
// Number of characters in a string literal, excluding the NUL-terminator.
#define STR_LITERAL_COUNT(str_literal) (ARRAY_COUNT(str_literal) - 1)
// C99 compatible(ish) _Static_assert. Macro parameter `what` should be a valid
// identifier describing the assertion. Flips the order of arguments from C11's
// _Static_assert so that assertions read as if they were a sentence.
//
// Example:
// // Assert that we are compiling on a 64-bit machine.
// STATIC_ASSERT(pointers_are_eight_bytes, sizeof(void*) == 8);
// clang-format off
#define STATIC_ASSERT(what, expr) enum {STATIC_ASSERT__ ## what = 1/!!(expr)}
// clang-format on
// Alternatives to the C99 standard library functions in ctype.h. These
// functions always use the "C" locale and will not result in undefined
// behavior if passed a value not representable by an unsigned char.
// clang-format off
int safe_isalnum(int c);
int safe_isalpha(int c);
int safe_isdigit(int c);
int safe_isgraph(int c);
int safe_islower(int c);
int safe_isprint(int c);
int safe_ispunct(int c);
int safe_isspace(int c);
int safe_isupper(int c);
int safe_isbdigit(int c); // Not in C99. Binary digit.
int safe_isodigit(int c); // Not in C99. Octal digit.
int safe_isxdigit(int c);
int safe_tolower(int c);
int safe_toupper(int c);
// clang-format on
// Alternatives to the C99 standard library functions in string.h. These
// functions do not result in undefined behavior when passed an invalid pointer
// argument paired with a memory-size argument of zero.
// clang-format off
int safe_memcmp(void const* s1, void const* s2, size_t n);
void* safe_memmove(void* dest, void const* src, size_t n);
void* safe_memset(void* s, int c, size_t n);
// clang-format on
// General purpose allocator function with out-of-memory error checking. The
// behavior of xalloc is similar to realloc and with the following exceptions:
// (1) On allocation failure an error message will be printed followed by
// program termination via abort.
// (2) The call xalloc(ptr, 0) is guaranteed to free the memory backing ptr. A
// pointer returned by xalloc may be freed with xalloc(ptr, 0) or the
// equivalent xalloc(ptr, XALLOC_FREE).
// The macro XALLOC_FREE may be used in place of the constant zero to indicate
// that a call xalloc(ptr, XALLOC_FREE) is intended as a free operation.
void*
xalloc(void* ptr, size_t size);
#define XALLOC_FREE ((size_t)0)
char const* // interned
canonical_path(char const* path);
char const* // interned
directory_path(char const* path);
// Excludes `.` and `..`.
// Files are returned in lexicographically sorted order.
char const* /* interned */* /* sbuf */
directory_files(char const* path);
bool
file_exists(char const* path);
bool
file_is_directory(char const* path);
// Read the full contents of the file specified by path.
// Memory for the read content is allocated with xalloc.
// Returns zero on success.
int
file_read_all(char const* path, void** buf, size_t* buf_size);
// Write the contents of a buffer into the file specified by path.
// The file specified by path is created if it does not exist.
// Returns zero on success.
// On failure, the contents of the file specified by path is undefined.
int
file_write_all(char const* path, void const* buf, size_t buf_size);
// Returns an xalloc-allocated cstring of the first count bytes of start.
// This function behaves similarly to the POSIX strdupn function.
char*
cstr_new(char const* start, size_t count);
// Returns an xalloc-allocated copy of the provided cstring.
// This function behaves similarly to the POSIX strdup function.
char*
cstr_new_cstr(char const* cstr);
// Returns an xalloc-allocated cstring from the provided formatted text.
char*
cstr_new_fmt(char const* fmt, ...);
// Returns an xalloc-allocated cstring from the provided formatted text with
// format arguments provided by a `va_list`.
char*
cstr_new_vfmt(char const* fmt, va_list args);
// Returns true if cstr starts with target.
bool
cstr_starts_with(char const* cstr, char const* target);
// Returns true if cstr ends with target.
bool
cstr_ends_with(char const* cstr, char const* target);
// Returns true if lhs and rhs are equal, ignoring case.
bool
cstr_eq_ignore_case(char const* lhs, char const* rhs);
char const* // interned
cstr_replace(char const* cstr, char const* target, char const* replacement);
uintmax_t
hash(void const* start, size_t count);
// Initialize the interned string set.
void
intern_init(void);
// Deinitialize the interned string set.
void
intern_fini(void);
// Intern the string specified by the first count bytes of start.
// Returns the canonical NUL-terminated representation of the interned string.
char const*
intern(char const* start, size_t count);
// Intern the string specified by the provided NUL-terminated cstring.
// Returns the canonical NUL-terminated representation of the interned string.
char const*
intern_cstr(char const* cstr);
// Intern the string built from the provided printf-style format string.
// Returns the canonical NUL-terminated representation of the interned string.
char const*
intern_fmt(char const* fmt, ...);
// General purpose type-safe dynamic array (a.k.a stretchy buffer).
//
// A stretchy buffer works by storing metadata about the number of allocated
// and in-use elements in a header just before the address of the buffer's
// first element. The ith element of a stretchy buffer may be accessed using
// the array index operator, sbuf[i], and a stretchy buffer containing elements
// of type T may be passed to subroutines as if it were regular array-like
// pointer of type T* or T const*. The address of a stretchy buffer may change
// when a resizing operation is performed, similar to resizing operations done
// with realloc, so the address of a stretchy buffer should not be considered
// stable.
//
// +--------+---------+---------+---------+--
// | HEADER | SBUF[0] | SBUF[1] | SBUF[2] | ...
// +--------+---------+---------+---------+--
// ^
// Pointer manipulated by the user / sbuf_* macros.
//
// Example:
// // The declaration:
// // TYPE* identifier = NULL;
// // creates an empty stretchy buffer holding TYPE values.
// // An equivalent declaration:
// // sbuf(TYPE) identifier = NULL;
// // may also be used in most cases.
// int* vals = NULL;
// printf("count == %zu\n", sbuf_count(vals)); /* count == 0 */
//
// for (int i = 0; i < 3; ++i) {
// sbuf_push(vals, (i + 1) * 2);
// }
// printf("count == %zu\n", sbuf_count(vals)); /* count == 3 */
// printf("vals[0] == %d\n", vals[0]); /* vals[0] == 2 */
// printf("vals[1] == %d\n", vals[1]); /* vals[1] == 4 */
// printf("vals[2] == %d\n", vals[2]); /* vals[2] == 6 */
//
// printf("popped == %d\n", sbuf_pop(vals)); /* popped == 6 */
// printf("count == %zu\n", sbuf_count(vals)); /* count == 2 */
//
// // Free memory allocated to the sbuf.
// // This is safe to call even if vals == NULL.
// sbuf_fini(vals);
// Convenience macro used to explicitly annotate a pointer as a stretchy
// buffer. Type annotations for types such as fixed-size arrays and function
// pointers are not supported by this macro due to the complicated nature of C
// variable/type declarations.
//
// Example:
// sbuf(int) sbuf = NULL;
// sbuf_push(sbuf, 1);
#define sbuf(TYPE) TYPE*
// void sbuf_fini(TYPE* sbuf)
// ------------------------------------------------------------
// Free resources associated with the stretchy buffer.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_fini(sbuf) \
((void)((sbuf) != NULL ? SBUF__FREE_NON_NULL_HEAD_(sbuf) : NULL))
// void sbuf_freeze(TYPE* sbuf)
// ------------------------------------------------------------
// Freeze the stretchy buffer.
#define sbuf_freeze(sbuf) \
((void)((sbuf) != NULL ? SBUF__FREEZE_NON_NULL_HEAD_(sbuf), NULL : NULL))
// size_t sbuf_count(TYPE* sbuf)
// ------------------------------------------------------------
// The number of elements in the stretchy buffer.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_count(sbuf) \
((size_t)((sbuf) != NULL ? SBUF__PHEAD_CONST_(sbuf)->cnt_ : 0u))
// size_t sbuf_capacity(TYPE* sbuf)
// ------------------------------------------------------------
// The number of elements the allocated in the sbuf.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_capacity(sbuf) \
((size_t)((sbuf) != NULL ? SBUF__PHEAD_CONST_(sbuf)->cap_ : 0u))
// void sbuf_reserve(TYPE* sbuf, size_t n)
// ------------------------------------------------------------
// Update the minimum capacity of the stretchy buffer to n elements.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_reserve(sbuf, /*n*/...) \
((void)((sbuf) = sbuf__rsv_(sizeof(*(sbuf)), sbuf, __VA_ARGS__)))
// void sbuf_resize(TYPE* sbuf, size_t n)
// ------------------------------------------------------------
// Update the count of the stretchy buffer to n elements.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_resize(sbuf, /*n*/...) \
((void)((sbuf) = sbuf__rsz_(sizeof(*(sbuf)), sbuf, __VA_ARGS__)))
// void sbuf_push(TYPE* sbuf, TYPE val)
// ------------------------------------------------------------
// Append val as the last element of the stretchy buffer.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_push(sbuf, /*val*/...) \
((void)(SBUF__MAYBE_GROW_(sbuf), SBUF__APPEND_(sbuf, __VA_ARGS__)))
// TYPE sbuf_pop(TYPE* sbuf)
// ------------------------------------------------------------
// Remove and return the last element of the stretchy buffer.
// This macro does *not* perform bounds checking.
// Macro parameter sbuf is evaluated multiple times.
#define sbuf_pop(sbuf) ((sbuf)[--SBUF__PHEAD_MUTBL_(sbuf)->cnt_])
// Internal utilities that must be visible to other header/source files that
// wish to use the sbuf_* API. Do not use these directly!
// clang-format off
struct sbuf__header_{size_t cnt_; size_t cap_; max_align_type _[];};
enum{SBUF__HEADER_OFFSET_ = sizeof(struct sbuf__header_)};
#define SBUF__PHEAD_MUTBL_(sbuf_) \
((struct sbuf__header_ *) \
((char *)(sbuf_)-SBUF__HEADER_OFFSET_))
#define SBUF__PHEAD_CONST_(sbuf_) \
((struct sbuf__header_ const*) \
((char const*)(sbuf_)-SBUF__HEADER_OFFSET_))
#define SBUF__FREE_NON_NULL_HEAD_(sbuf_) \
(xalloc(SBUF__PHEAD_MUTBL_(sbuf_), XALLOC_FREE))
#define SBUF__FREEZE_NON_NULL_HEAD_(sbuf_) \
(freeze(SBUF__PHEAD_MUTBL_(sbuf_)))
#define SBUF__MAYBE_GROW_(sbuf_) \
((sbuf_count(sbuf_) == sbuf_capacity(sbuf_)) \
? (sbuf_) = sbuf__grw_(sizeof(*(sbuf_)), sbuf_) \
: (sbuf_))
#define SBUF__APPEND_(sbuf_, ...) \
((sbuf_)[SBUF__PHEAD_MUTBL_(sbuf_)->cnt_++] = (__VA_ARGS__))
void* sbuf__rsv_(size_t elemsize, void* sbuf, size_t cap);
void* sbuf__rsz_(size_t elemsize, void* sbuf, size_t cnt);
void* sbuf__grw_(size_t elemsize, void* sbuf);
// clang-format on
// Allocate and initialize a bit array with count bits.
// The bit array is initially zeroed.
struct bitarr*
bitarr_new(size_t count);
// Deinitialize and free the bit array.
// Does nothing if self == NULL.
void
bitarr_del(struct bitarr* self);
// Freeze the bit array.
void
bitarr_freeze(struct bitarr* self);
// Returns the number of bits in this bit array.
size_t
bitarr_count(struct bitarr const* self);
// Set the nth bit (zero indexed) of self to value.
// Fatally exits after printing an error message if n is out of bounds.
void
bitarr_set(struct bitarr* self, size_t n, int value);
// Returns the value (one or zero) of the nth bit (zero indexed) of self.
// Fatally exits after printing an error message if n is out of bounds.
int
bitarr_get(struct bitarr const* self, size_t n);
// self = othr
// Fatally exits after printing an error message if the count of self is not
// equal to the count of othr.
void
bitarr_assign(struct bitarr* self, struct bitarr const* othr);
// res = ~rhs
// Fatally exits after printing an error message if the count of res and rhs
// are not equal.
void
bitarr_compl(struct bitarr* res, struct bitarr const* rhs);
// res = -rhs
// Fatally exits after printing an error message if the count of res and rhs
// are not equal.
void
bitarr_twos_complement_neg(struct bitarr* res, struct bitarr* rhs);
// res = lhs << nbits (logical shift left)
// Fatally exits after printing an error message if the count of res and lhs
// are not equal.
void
bitarr_shiftl(struct bitarr* res, struct bitarr const* lhs, size_t nbits);
// res = lhs >> nbits (logical shift right)
// Fatally exits after printing an error message if the count of res and lhs
// are not equal.
void
bitarr_shiftr(
struct bitarr* res, struct bitarr const* lhs, size_t nbits, int high_bit);
// res = lhs & rhs
// Fatally exits after printing an error message if the count of res, lhs, and
// rhs are not equal.
void
bitarr_and(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs);
// res = lhs ^ rhs
// Fatally exits after printing an error message if the count of res, lhs, and
// rhs are not equal.
void
bitarr_xor(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs);
// res = lhs | rhs
// Fatally exits after printing an error message if the count of res, lhs, and
// rhs are not equal.
void
bitarr_or(
struct bitarr* res, struct bitarr const* lhs, struct bitarr const* rhs);
// Convert a two's complement bit array into a a bigint.
void
bitarr_to_bigint(
struct bigint* res, struct bitarr const* bitarr, bool is_signed);
extern struct bigint const* const BIGINT_ZERO; // 0
extern struct bigint const* const BIGINT_POS_ONE; // +1
extern struct bigint const* const BIGINT_NEG_ONE; // -1
// Allocate and initialize a bigint to the specified bigint value.
// The call bigint_new(BIGINT_ZERO) will zero-initialize a bigint.
struct bigint*
bigint_new(struct bigint const* othr);
// Allocate and initialize a bigint to the specified uintmax_t value.
struct bigint*
bigint_new_umax(uintmax_t umax);
// Allocate and initialize a bigint to the specified intmax_t value.
struct bigint*
bigint_new_smax(intmax_t smax);
// Allocate and initialize a bigint from a two's complement bit array.
struct bigint*
bigint_new_bitarr(struct bitarr const* bitarr, bool is_signed);
// Allocate and initialize a bigint from the provided NUL-terminated cstring.
// Returns NULL if the cstring could not be parsed.
//
// The cstring may begin with a plus (+) or minus (-) sign.
// In the absence of a plus or minus sign the cstring will interpreted as a
// non-negative number.
//
// The digits of the cstring may be prefixed with a radix identifier:
// 0b (binary), 0o (octal), or 0x (hexadecimal).
// In the absence of a radix identifier, the digits of the cstring will decoded
// with radix 10 (decimal).
//
// The cstring *must* not have any leading or trailing whitespace.
struct bigint*
bigint_new_cstr(char const* cstr);
// Allocate and initialize a bigint from the provided string slice.
// Returns NULL if the string could not be parsed.
// This function uses the same string-grammar as bigint_new_cstr().
struct bigint*
bigint_new_text(char const* start, size_t count);
// Deinitialize and free the bigint.
// Does nothing if self == NULL.
void
bigint_del(struct bigint* self);
// Free the bigint.
void
bigint_freeze(struct bigint* self);
// Return an int less than, equal to, or greater than zero if lhs is
// semantically less than, equal to, or greater than rhs, respectively.
int
bigint_cmp(struct bigint const* lhs, struct bigint const* rhs);
// self = othr
void
bigint_assign(struct bigint* self, struct bigint const* othr);
// res = -rhs
void
bigint_neg(struct bigint* res, struct bigint const* rhs);
// res = abs(rhs)
void
bigint_abs(struct bigint* res, struct bigint const* rhs);
// res = lhs + rhs
void
bigint_add(
struct bigint* res, struct bigint const* lhs, struct bigint const* rhs);
// res = lhs - rhs
void
bigint_sub(
struct bigint* res, struct bigint const* lhs, struct bigint const* rhs);
// res = lhs * rhs
void
bigint_mul(
struct bigint* res, struct bigint const* lhs, struct bigint const* rhs);
// res = lhs / rhs
// rem = lhs % rhs
// If res is NULL then the result will not be written to res.
// If rem is NULL then the remainder will not be written to rem.
//
// This function matches the behavior of the / and % operators as defined by
// the C99 standard, satisfying the expression:
// (lhs/rhs)*rhs + lhs%rhs == lhs
// where:
// lhs/rhs == res
// lhs%rhs == rem
void
bigint_divrem(
struct bigint* res,
struct bigint* rem,
struct bigint const* lhs,
struct bigint const* rhs);
// self.magnitude = self.magnitude << nbits (logical shift left)
// This function is sign-oblivious (the sign of self is not altered).
void
bigint_magnitude_shiftl(struct bigint* self, size_t nbits);
// self.magnitude = self.magnitude >> nbits (logical shift right)
// This function is sign-oblivious (the sign of self is not altered).
void
bigint_magnitude_shiftr(struct bigint* self, size_t nbits);
// Returns the number of bits required to store the magnitude of self.
// This function is sign-oblivious (the sign of self is not considered).
size_t
bigint_magnitude_bit_count(struct bigint const* self);
// Returns the value (one or zero) of the nth bit (zero indexed) of the
// magnitude of self.
// This function is sign-oblivious (the sign of self is not considered).
int
bigint_magnitude_bit_get(struct bigint const* self, size_t n);
// Set the nth bit (zero indexed) of the magnitude of self to value.
// This function is sign-oblivious (the sign of self is not altered).
void
bigint_magnitude_bit_set(struct bigint* self, size_t n, int value);
// Convert a bigint to a uint8_t.
// Returns zero on success.
// Returns non-zero if the provided bigint is out-of-range, in which case *res
// is left unmodified.
int
bigint_to_u8(uint8_t* res, struct bigint const* bigint);
// Convert a bigint to a size_t.
// Returns zero on success.
// Returns non-zero if the provided bigint is out-of-range, in which case *res
// is left unmodified.
int
bigint_to_uz(size_t* res, struct bigint const* bigint);
// Convert a bigint to a uintmax_t.
// Returns zero on success.
// Returns non-zero if the provided bigint is out-of-range, in which case *res
// is left unmodified.
int
bigint_to_umax(uintmax_t* res, struct bigint const* bigint);
// Convert a bigint to an intmax_t.
// Returns zero on success.
// Returns non-zero if the provided bigint is out-of-range, in which case *res
// is left unmodified.
int
bigint_to_smax(intmax_t* res, struct bigint const* bigint);
// Convert a bigint to a two's complement bit array.
// Returns zero on success.
// Returns non-zero if the provided bigint is out-of-range would require more
// than bitarr_count(res) bits to express, in which case *res is left
// unmodified.
int
bigint_to_bitarr(struct bitarr* res, struct bigint const* bigint);
// Returns an xalloc-allocated cstring representation of the provided bigint
// formatted as a decimal number.
char*
bigint_to_new_cstr(struct bigint const* self);
// Allocate and initialize a string from the first count bytes of start.
struct string*
string_new(char const* start, size_t count);
// Allocate and initialize a string from the provided NUL-terminated cstring.
// If cstr is NULL then string will be initialized to the empty string.
struct string*
string_new_cstr(char const* cstr);
// Allocate and initialize a string from the provided formatted text.
struct string*
string_new_fmt(char const* fmt, ...);
// Deinitialize and free the string.
// Does nothing if self == NULL.
void
string_del(struct string* self);
// Freeze the string.
void
string_freeze(struct string* self);
// Pointer to the start of the underlying char array of the string.
// Returns a pointer to a NUL terminator when the count of the string is zero.
char const*
string_start(struct string const* self);
// The number of bytes in the string *NOT* including the NUL terminator.
size_t
string_count(struct string const* self);
// Update the count of the string.
// If count is greater than the current count of the string then additional
// elements are initialized with garbage data.
void
string_resize(struct string* self, size_t count);
// Append count bytes of start onto the end of the string.
void
string_append(struct string* self, char const* start, size_t count);
// Append the provided NUL-terminated cstring onto the end of the string.
void
string_append_cstr(struct string* self, char const* cstr);
// Append the formatted text to the end of the string.
void
string_append_fmt(struct string* self, char const* fmt, ...);
void
string_append_vfmt(struct string* self, char const* fmt, va_list args);
// Split the string on all occurrences of the provided separator. Empty strings
// are *NOT* removed from the result. This function returns a stretchy buffer
// of newly allocated string pointers containing the results of the split.
//
// Example:
// "ABCBB" ===split on "B"===> "A" "C" "" ""
struct string** /* sbuf */
string_split(
struct string const* self, char const* separator, size_t separator_size);
// Register a pointer to xalloc-allocated memory to be frozen.
void
freeze(void* ptr);
// Deinitialize the frozen object list free frozen objects.
void
freeze_fini(void);
// Returns the string contents of a file with the provided path. The produced
// string is NUL-prefixed and NUL-terminated. This function will cause a fatal
// error if the file cannot be read.
char*
read_source(char const* path);
// Returns a pointer to the first character of the line containing ptr in a
// source string produced by read_source.
char const*
source_line_start(char const* ptr);
// Returns a pointer to the end-of-line newline or NUL of the line containing
// ptr in a source string produced by read_source.
char const*
source_line_end(char const* ptr);
#define NO_PATH ((char const*)NULL)
#define NO_LINE ((size_t)0u)
#define NO_PSRC ((char const*)NULL)
#define NO_LOCATION ((struct source_location){NO_PATH, NO_LINE, NO_PATH})
struct source_location {
// Optional (NULL indicates no value).
// NOTE: Source locations produced by the lexing phase will use a module's
// `name` (i.e. non-canonical path) member for the source location path.
char const* path;
// Optional (zero indicates no value).
size_t line;
// Optional (NULL indicates no value) pointer to the source character
// within the module specified by path. If non-NULL then a log-messages
// will display the line in question with a caret pointing to this
// character as such:
// ```
// [file.sunder:3] error: foo is not properly frobnicated
// var foo: usize = 123u;
// ^
// ```
char const* psrc;
};
#if defined(__GNUC__) /* GCC and Clang */
# define MESSAGEF __attribute__((format(printf, 2, 3)))
#else
# define MESSAGEF /* nothing */
#endif
MESSAGEF void
info(struct source_location location, char const* fmt, ...);
MESSAGEF void
warning(struct source_location location, char const* fmt, ...);
MESSAGEF void
error(struct source_location location, char const* fmt, ...);
MESSAGEF NORETURN void
fatal(struct source_location location, char const* fmt, ...);
NORETURN void
unreachable(char const* file, int line);
#define UNREACHABLE() unreachable(__FILE__, __LINE__)
// Round up to the nearest multiple of 8.
uintmax_t
ceil8umax(uintmax_t x);
// Spawn a subprocess and wait for it to complete.
// Returns the exit status of the spawned process.
int
spawnvpw(char const* const* argv);
////////////////////////////////////////////////////////////////////////////////
//////// sunder.c //////////////////////////////////////////////////////////////
// Global compiler state.
enum arch {
ARCH_AMD64,
ARCH_ARM64,
ARCH_WASM32,
};
enum arch
cstr_to_arch(char const* cstr);
struct module {
// True if the module has been fully loaded/resolved.
bool loaded;
// The shorthand path of this module. For a module imported as:
// import "foo/bar.sunder";
// this member will hold the string "foo/bar.sunder".
char const* name; // interned
// The canonical path of this module. For a module imported as:
// import "foo/bar.sunder";
// this member will hold the string "/full/path/to/foo/bar.sunder".
char const* path; // interned
// NUL-prefixed, NUL-terminated text contents of the module. When the
// module source is loaded a NUL-prefix is added to the beginning of the
// source string at position source[-1] and source[source_count + 1] so
// that either a forwards or backward search through the source text may
// stop if a NUL byte is encountered.
char const* source;
size_t source_count;
// Global symbols.
struct symbol_table* symbols;
// Exported symbols declared in this module.
struct symbol_table* exports;
// Concrete syntax tree for the module. Initialized to NULL and populated
// during the parse phase.
struct cst_module const* cst;
// List of top level declarations topologically ordered such that the
// declaration with index k does not depend on any declaration with index
// k+n for all n. Initialized to NULL and populated during the order phase.
sbuf(struct cst_decl const*) ordered;
};
struct module*
module_new(char const* name, char const* path);
void
module_del(struct module* self);
struct context {
// Interned strings.
struct {
// clang-format off
char const* empty; // ""
char const* builtin; // "builtin"
char const* return_; // "return"
char const* main; // "main"
char const* any; // "any"
char const* void_; // "void"
char const* bool_; // "bool"
char const* u8; // "u8"
char const* s8; // "s8"
char const* u16; // "u16"
char const* s16; // "s16"
char const* u32; // "u32"
char const* s32; // "s32"
char const* u64; // "u64"
char const* s64; // "s64"
char const* byte; // "byte"
char const* usize; // "usize"
char const* ssize; // "ssize"
char const* integer; // "integer"
char const* y; // "y"
char const* u; // "u"
char const* s; // "s"
char const* f32; // "f32"
char const* f64; // "f64"
char const* real; // "real"
// clang-format on
} interned;
// Environment variables used by the compiler.
struct {
char const* SUNDER_HOME;
char const* SUNDER_ARCH;
char const* SUNDER_HOST;
char const* SUNDER_SEARCH_PATH;
char const* SUNDER_CC;
char const* SUNDER_CFLAGS;
} env;
// Target SUNDER_ARCH.
enum arch arch;
// Integer (bigint) constants.
struct bigint const* u8_min;
struct bigint const* u8_max;
struct bigint const* s8_min;
struct bigint const* s8_max;
struct bigint const* u16_min;
struct bigint const* u16_max;
struct bigint const* s16_min;
struct bigint const* s16_max;
struct bigint const* u32_min;
struct bigint const* u32_max;
struct bigint const* s32_min;
struct bigint const* s32_max;
struct bigint const* u64_min;
struct bigint const* u64_max;
struct bigint const* s64_min;
struct bigint const* s64_max;
struct bigint const* usize_min;
struct bigint const* usize_max;
struct bigint const* ssize_min;
struct bigint const* ssize_max;
struct bigint const* f32_integer_min;
struct bigint const* f32_integer_max;
struct bigint const* f64_integer_min;
struct bigint const* f64_integer_max;
// Language builtins.
struct {
struct source_location location;
struct type const* any;
struct type const* void_;
struct type const* bool_;
struct type const* byte;
struct type const* u8;
struct type const* s8;
struct type const* u16;
struct type const* s16;
struct type const* u32;
struct type const* s32;
struct type const* u64;
struct type const* s64;
struct type const* usize;
struct type const* ssize;
struct type const* integer;
struct type const* f32;
struct type const* f64;
struct type const* real;
struct type const* pointer_to_byte;
struct type const* slice_of_byte;
} builtin;
// List of all types instantiated by the compiler.
sbuf(struct type const*) types;
// List of all symbols with static storage duration.
sbuf(struct symbol const*) static_symbols;
// Global symbol table.
struct symbol_table* global_symbol_table;
// Currently loaded/loading modules.
sbuf(struct module*) modules;
// List of symbol tables to be frozen before (successful) program exit.
//
// Some symbol tables, such as those belonging to types and templates,
// cannot be frozen until after all modules have been resolved as type
// extensions and template instantiations may be defined in modules other
// than the module where the symbol table was created.
//
// Other symbol tables, such as those belonging to a module namespace
// symbol, cannot be frozen until after that module has been resolved as
// they may have symbols added over the course of that module's resolution.
// These symbol tables *could* be frozen as soon as the module is fully
// resolved, but adding them to this list and freezing them some time after
// the module has been resolved makes no semantic difference in how they
// are used, and avoids making separate chilling symbol table lists for
// each module.
sbuf(struct symbol_table*) chilling_symbol_tables;
// Chain of templates currently being instantiated. When a new template
// instantiation occurs, information about the instantiation is pushed to
// this list. After the template instantiation has completed, that same
// information is popped from this list. If a warning, error, or fatal
// error message is emitted, then the elements of this linked list are used
// to report the chain of template instantiations that lead to that message
// being emitted.
struct template_instantiation_link {
// The parent instantiation that is responsible for this instantiation.
// NULL if this is the outer-most link in the instantiation chain.
struct template_instantiation_link const* next;
// Full name of template being instantiated.
char const* name; // interned
// Location where the instantiation occurred.
struct source_location location;
// This link's position in the instantiation chain.
size_t depth;
} const* template_instantiation_chain;
};
void
context_init(void);
void
context_fini(void);
struct context*
context(void);
struct module const*
load_module(char const* name, char const* path);
struct module const*
lookup_module(char const* path);
void
validate_main_is_defined_correctly(void);
////////////////////////////////////////////////////////////////////////////////
//////// lex.c /////////////////////////////////////////////////////////////////
// clang-format off
enum token_kind {
// Keywords
TOKEN_TRUE,
TOKEN_FALSE,
TOKEN_NOT,
TOKEN_OR,
TOKEN_AND,
TOKEN_NAMESPACE,
TOKEN_IMPORT,
TOKEN_VAR,
TOKEN_LET,
TOKEN_FUNC,
TOKEN_STRUCT,
TOKEN_UNION,
TOKEN_ENUM,
TOKEN_TYPE,
TOKEN_EXTEND,
TOKEN_EXTERN,
TOKEN_SWITCH,
TOKEN_RETURN,
TOKEN_ASSERT,
TOKEN_DEFER,
TOKEN_IF,
TOKEN_ELIF,
TOKEN_ELSE,
TOKEN_WHEN,
TOKEN_ELWHEN,
TOKEN_FOR,
TOKEN_IN,
TOKEN_BREAK,
TOKEN_CONTINUE,
TOKEN_DEFINED,
TOKEN_ALIGNOF,
TOKEN_STARTOF,
TOKEN_COUNTOF,
TOKEN_SIZEOF,
TOKEN_TYPEOF,
TOKEN_FILEOF,
TOKEN_LINEOF,
TOKEN_UNINIT,
TOKEN_EMBED,
// Sigils
TOKEN_PLUS_PERCENT_ASSIGN, // +%=
TOKEN_DASH_PERCENT_ASSIGN, // -%=
TOKEN_STAR_PERCENT_ASSIGN, // *%=
TOKEN_PLUS_ASSIGN, // +=
TOKEN_DASH_ASSIGN, // -=
TOKEN_STAR_ASSIGN, // *=
TOKEN_FSLASH_ASSIGN, // /=
TOKEN_PERCENT_ASSIGN, // %=
TOKEN_SHL_ASSIGN, // <<=
TOKEN_SHR_ASSIGN, // >>=
TOKEN_PIPE_ASSIGN, // |=
TOKEN_CARET_ASSIGN, // ^=
TOKEN_AMPERSAND_ASSIGN, // &=
TOKEN_SHL, // <<
TOKEN_SHR, // >>
TOKEN_EQ, // ==
TOKEN_NE, // !=
TOKEN_LE, // <=
TOKEN_LT, // <
TOKEN_GE, // >=
TOKEN_GT, // >
TOKEN_ASSIGN, // =
TOKEN_PLUS_PERCENT, // +%
TOKEN_DASH_PERCENT, // -%
TOKEN_STAR_PERCENT, // *%
TOKEN_PLUS, // +
TOKEN_DASH, // -
TOKEN_STAR, // *
TOKEN_FSLASH, // /
TOKEN_PERCENT, // %
TOKEN_TILDE, // ~
TOKEN_PIPE, // |
TOKEN_CARET, // ^
TOKEN_AMPERSAND, // &
TOKEN_LPAREN, // (
TOKEN_RPAREN, // )
TOKEN_LBRACE, // {
TOKEN_RBRACE, // }
TOKEN_LBRACKET, // [
TOKEN_RBRACKET, // ]
TOKEN_COMMA, // ,
TOKEN_ELLIPSIS, // ...
TOKEN_DOT_STAR, // .*
TOKEN_DOT, // .
TOKEN_COLON_COLON, // ::
TOKEN_COLON, // :
TOKEN_SEMICOLON, // ;
// Identifiers and Non-Keyword Literals
TOKEN_IDENTIFIER,
TOKEN_INTEGER,
TOKEN_IEEE754,
TOKEN_CHARACTER,
TOKEN_BYTES,
// Meta
TOKEN_EOF,
};
// clang-format on
char const*
token_kind_to_cstr(enum token_kind kind);
struct token {
char const* start;
size_t count;
struct source_location location;
enum token_kind kind;
union {
char const* identifier; // interned
// Contains the value and type-suffix of the integer literal.
struct {
struct bigint const* value;
char const* suffix; // interned