@typesol/cli 0.42.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +227 -0
  2. package/README.md +10 -7
  3. package/dist/compiler/analyze.js +443 -12
  4. package/dist/compiler/diagnostics.d.ts +1 -0
  5. package/dist/compiler/diagnostics.js +13 -0
  6. package/dist/compiler/emit/alias.d.ts +44 -0
  7. package/dist/compiler/emit/alias.js +135 -4
  8. package/dist/compiler/emit/base.d.ts +25 -0
  9. package/dist/compiler/emit/base.js +58 -7
  10. package/dist/compiler/emit/calls.d.ts +67 -0
  11. package/dist/compiler/emit/calls.js +521 -46
  12. package/dist/compiler/emit/cpi-core.d.ts +20 -3
  13. package/dist/compiler/emit/cpi-core.js +226 -5
  14. package/dist/compiler/emit/cpi.js +8 -2
  15. package/dist/compiler/emit/expressions.js +38 -7
  16. package/dist/compiler/emit/program.js +30 -1
  17. package/dist/compiler/emit/rbtree.d.ts +7 -0
  18. package/dist/compiler/emit/rbtree.js +142 -0
  19. package/dist/compiler/emit/shared.d.ts +8 -0
  20. package/dist/compiler/emit/shared.js +9 -1
  21. package/dist/compiler/emit/statements.d.ts +27 -1
  22. package/dist/compiler/emit/statements.js +288 -19
  23. package/dist/compiler/emit/storage.d.ts +23 -2
  24. package/dist/compiler/emit/storage.js +100 -56
  25. package/dist/compiler/emit/token.js +2 -2
  26. package/dist/compiler/emit/views.d.ts +60 -0
  27. package/dist/compiler/emit/views.js +523 -0
  28. package/dist/compiler/idl.js +2 -1
  29. package/dist/compiler/model.d.ts +65 -4
  30. package/dist/compiler/model.js +14 -0
  31. package/dist/compiler/toolchain.d.ts +15 -0
  32. package/dist/compiler/toolchain.js +146 -11
  33. package/docs/architecture.md +9 -2
  34. package/docs/case-study-phoenix.md +226 -0
  35. package/docs/language.md +139 -8
  36. package/docs/migrate-from-anchor.md +1 -0
  37. package/docs/runtime.md +1 -1
  38. package/docs/stability.md +21 -2
  39. package/docs/troubleshooting.md +5 -2
  40. package/lib/typesol.d.ts +221 -4
  41. package/llms-full.txt +169 -14
  42. package/llms.txt +1 -1
  43. package/package.json +2 -2
  44. package/runtime/typesol_rt.h +407 -0
@@ -68,13 +68,21 @@
68
68
  #define TS_ERR_NO_RETURN 5114
69
69
  #define TS_ERR_ACCOUNT_MISSING 5115
70
70
 
71
+ #ifdef TS_PANIC_ABORT
72
+ extern __attribute__((noreturn)) void abort(void); /* declared again with the other syscalls below */
73
+ #endif
71
74
  /* Every failure logs through this one sol_log_ call: each syscall call site costs a 16-byte relocation in the binary. */
72
75
  static __attribute__((noinline)) uint64_t ts_fail(uint64_t code, const char *msg, uint64_t len) {
73
76
  sol_log_(msg, len);
74
77
  #ifdef TS_PANIC_ON_OVERFLOW
75
78
  /* PANIC_ON_OVERFLOW: arithmetic fails as a Rust `checked_*().unwrap()` does, with a panic (ProgramFailedToComplete). */
79
+ #ifdef TS_PANIC_ABORT
80
+ /* PANIC_STYLE "solana-1.14": a panic ends in the abort syscall ("SBF program panicked"), as that panic handler's. */
81
+ if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) abort();
82
+ #else
76
83
  if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) sol_panic_("", 0, 0, 0);
77
84
  #endif
85
+ #endif
78
86
  #ifdef TS_CUSTOM_ZERO
79
87
  /* An error with code 0 (a native program's first error) is returned as solana_program returns
80
88
  * ProgramError::Custom(0): the built-in CUSTOM_ZERO, since 0 itself means success. */
@@ -88,6 +96,24 @@ static __attribute__((noinline, unused)) uint64_t ts_fail_code(uint32_t code, co
88
96
  return ts_fail(code ? code : TS_ERR_OVERFLOW, msg, sol_strlen(msg));
89
97
  }
90
98
 
99
+ /* A failure that logs nothing (`const ERROR_LOG_PREFIX = null` / `PROGRAM_ERROR_LOG_PREFIX = null`): the code ts_fail
100
+ * would return, as a native program returns an error its own checks already explained (or never explain). */
101
+ static inline __attribute__((always_inline, unused)) uint64_t ts_fail_quiet(uint64_t code) {
102
+ #ifdef TS_PANIC_ON_OVERFLOW
103
+ #ifdef TS_PANIC_ABORT
104
+ if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) abort();
105
+ #else
106
+ if (code == TS_ERR_OVERFLOW || code == TS_ERR_DIV_ZERO) sol_panic_("", 0, 0, 0);
107
+ #endif
108
+ #endif
109
+ #ifdef TS_CUSTOM_ZERO
110
+ if (code == 0) return (uint64_t)1 << 32;
111
+ #endif
112
+ return code;
113
+ }
114
+ /* ts_fail_code's quiet form: a code computed at run time, 0 failing as an overflow. */
115
+ static inline __attribute__((always_inline, unused)) uint64_t ts_fail_code_quiet(uint32_t code) { return ts_fail_quiet(code ? code : TS_ERR_OVERFLOW); }
116
+
91
117
  #define TS_TRY(expr) \
92
118
  do { \
93
119
  uint64_t ts_r_ = (expr); \
@@ -557,7 +583,16 @@ typedef struct {
557
583
  #else
558
584
  uint64_t loaded_mut; /* bit i: ka[i] loaded with loadAccountMut (zeroed by the entrypoint of a program that uses it) */
559
585
  #endif
586
+ #ifdef TS_OWNER_CACHE
587
+ const TsAccount *owned; /* the account the last owner check found this program's (see ts_owned) */
588
+ #endif
560
589
  } TsParams;
590
+ /* Every CPI and every owner write of the program forgets the cached owner check (an owner may have changed). */
591
+ #ifdef TS_OWNER_CACHE
592
+ #define TS_OWNER_FORGET(p) ((p)->owned = NULL)
593
+ #else
594
+ #define TS_OWNER_FORGET(p) ((void)0)
595
+ #endif
561
596
 
562
597
  #ifdef TS_SCRATCH_ON_HEAP
563
598
  static uint64_t ts_infos_reserve(TsParams *params, uint64_t n);
@@ -599,6 +634,11 @@ static inline __attribute__((always_inline)) void ts_deserialize(const uint8_t *
599
634
  }
600
635
  TsAccount *a = (TsAccount *)input;
601
636
  params->ka[i] = a;
637
+ #ifdef TS_RECORD_ENTRY_LEN
638
+ /* resizeAccount on a remaining account: its size now, in the record's padding, as solana_program 1.14's
639
+ entrypoint keeps original_data_len there for AccountInfo::realloc (ts_entry_len). */
640
+ *(ts_u32_unaligned *)a->padding = (uint32_t)a->data_len;
641
+ #endif
602
642
  input = (const uint8_t *)(((uint64_t)(input + 88 + a->data_len + MAX_PERMITTED_DATA_INCREASE) + 7) & ~(uint64_t)7) + 8;
603
643
  }
604
644
  params->data_len = *(const uint64_t *)input;
@@ -634,6 +674,7 @@ static inline __attribute__((always_inline)) uint64_t ts_invoke(TsParams *params
634
674
  TS_INFOS_RESERVE(params, ix->account_len);
635
675
  SolAccountInfo *infos = params->infos;
636
676
  for (uint64_t i = 0; i < ix->account_len; i++) ts_info((const TsAccount *)((const uint8_t *)ix->accounts[i].pubkey - 8), &infos[i]);
677
+ TS_OWNER_FORGET(params);
637
678
  return sol_invoke_signed(ix, infos, ix->account_len, signers, nsigners);
638
679
  }
639
680
 
@@ -724,6 +765,19 @@ static inline void ts_copy(void *dst, const void *src, uint64_t n) {
724
765
  if (dst != src) ts_memcpy(dst, src, n);
725
766
  }
726
767
 
768
+ static inline __attribute__((always_inline)) bool ts_key_eq(const SolPubkey *a, const SolPubkey *b);
769
+ #ifdef TS_OWNER_CACHE
770
+ /* Is account `a` this program's? The views of native accounts (loadRaw, loadRawAt, rawArrayAt, rbTreeAt) check it each
771
+ time they are made, often of one account again and again (helpers that each make their own view, views in loops).
772
+ The last account found this program's is remembered until a CPI or an owner write (TS_OWNER_FORGET): until then
773
+ its owner cannot have changed, so the same record passes with one pointer compare instead of 32 bytes. */
774
+ static inline __attribute__((always_inline)) bool ts_owned(TsParams *params, const TsAccount *a) {
775
+ if (params->owned == a) return true;
776
+ if (!ts_key_eq(&a->owner, params->program_id)) return false;
777
+ params->owned = a;
778
+ return true;
779
+ }
780
+ #endif
727
781
  static inline __attribute__((always_inline)) bool ts_key_eq(const SolPubkey *a, const SolPubkey *b) {
728
782
  return ts_ld64(a->x) == ts_ld64(b->x) && ts_ld64(a->x + 8) == ts_ld64(b->x + 8) && ts_ld64(a->x + 16) == ts_ld64(b->x + 16) &&
729
783
  ts_ld64(a->x + 24) == ts_ld64(b->x + 24);
@@ -1370,6 +1424,11 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
1370
1424
  return SUCCESS;
1371
1425
  }
1372
1426
 
1427
+ #ifdef TS_RECORD_ENTRY_LEN
1428
+ /* A remaining account's data length when the instruction started (recorded by ts_deserialize). */
1429
+ static inline __attribute__((always_inline)) uint64_t ts_entry_len(const TsAccount *a) { return *(const ts_u32_unaligned *)a->padding; }
1430
+ #endif
1431
+
1373
1432
  /* resizeAccount(): the data length only (AccountInfo::resize), new bytes zeroed; no rent moves. */
1374
1433
  static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
1375
1434
  if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
@@ -1446,6 +1505,7 @@ static inline uint64_t ts_cpi_named(TsParams *params, const TsAccount *program,
1446
1505
  TS_INFOS_RESERVE(params, n);
1447
1506
  SolAccountInfo *infos = params->infos;
1448
1507
  for (uint64_t i = 0; i < n; i++) ts_info(accounts[i], &infos[i]);
1508
+ TS_OWNER_FORGET(params);
1449
1509
  return sol_invoke_signed(&ix, infos, n, &signer, nseeds > 0 ? 1 : 0);
1450
1510
  }
1451
1511
 
@@ -1790,6 +1850,22 @@ static inline __attribute__((always_inline)) void ts_memcpy_bulk(void *dst, cons
1790
1850
  if (n >= TS_MEMCPY_SYSCALL_MIN) sol_memcpy_(dst, src, n);
1791
1851
  else ts_memcpy(dst, src, n);
1792
1852
  }
1853
+ /* The memset syscall, for zeroing large regions (a local FixedVec's free slots, so bytes past its length read as zero
1854
+ * wherever its whole layout is copied: account data, events): max(10, n / 250) CU plus the call, against n / 8 stores
1855
+ * for ts_memzero's words (constant sizes unroll), so regions under 128 bytes keep the stores. */
1856
+ extern void sol_memset_(void *dst, uint8_t c, uint64_t n);
1857
+ #define TS_MEMSET_SYSCALL_MIN 128
1858
+ static inline __attribute__((always_inline, unused)) void ts_memzero_bulk(void *dst, uint64_t n) {
1859
+ if (n >= TS_MEMSET_SYSCALL_MIN) sol_memset_(dst, 0, n);
1860
+ else ts_memzero(dst, n);
1861
+ }
1862
+ /* A copy of a size known when compiling (a whole local FixedVec): the syscall from TS_MEMCPY_CONST_MIN bytes, where
1863
+ * the unrolled words (a load and a store per 8 bytes) cost more than the call. */
1864
+ #define TS_MEMCPY_CONST_MIN 128
1865
+ static inline __attribute__((always_inline, unused)) void ts_memcpy_const(void *dst, const void *src, uint64_t n) {
1866
+ if (n >= TS_MEMCPY_CONST_MIN) sol_memcpy_(dst, src, n);
1867
+ else ts_memcpy(dst, src, n);
1868
+ }
1793
1869
  /* A BigVec's room: a BigVec struct is only ever a loadRaw view, which sits at a TsAccount's data, right after its
1794
1870
  * data_len (see TsAccount); the elements that fit in the account past the struct (its fixed fields and the length). */
1795
1871
  static inline __attribute__((always_inline)) uint64_t ts_bigvec_cap(const uint8_t *data, uint64_t fixed, uint64_t elem) {
@@ -1804,6 +1880,337 @@ static inline __attribute__((always_inline)) uint8_t *ts_opaque_ptr(uint8_t *p)
1804
1880
  return p;
1805
1881
  }
1806
1882
 
1883
+ /*
1884
+ * ---- A Rust panic as solana-program up to 1.17 reports it ----------------------------------------------------
1885
+ * The program's panic handler logs the PanicInfo ("panicked at '<msg>', <file>:<line>:<col>", Rust before 1.73) and
1886
+ * the runtime's abort ends the program ("SBF program panicked", ProgramFailedToComplete). The built-in
1887
+ * RedBlackTree panics this way, where lib-sokoban 0.3 does, with sokoban's own messages and source locations.
1888
+ */
1889
+ static __attribute__((noinline, noreturn, unused)) void ts_rust_panic(const char *msg, uint64_t msg_len, const char *loc, uint64_t loc_len) {
1890
+ char line[160];
1891
+ uint64_t n = 0;
1892
+ const char *head = "panicked at '";
1893
+ for (uint64_t i = 0; head[i]; i++) line[n++] = head[i];
1894
+ for (uint64_t i = 0; i < msg_len && n < sizeof(line) - 4 - loc_len; i++) line[n++] = msg[i];
1895
+ line[n++] = '\'';
1896
+ line[n++] = ',';
1897
+ line[n++] = ' ';
1898
+ for (uint64_t i = 0; i < loc_len && n < sizeof(line); i++) line[n++] = loc[i];
1899
+ sol_log_(line, n);
1900
+ abort();
1901
+ }
1902
+ /* `msg` then the decimal digits of `v` (Rust's `panic!("Buffer is full, size {}", self.size)`). */
1903
+ static __attribute__((noinline, noreturn, unused)) void ts_rust_panic_u64(const char *msg, uint64_t msg_len, uint64_t v, const char *loc, uint64_t loc_len) {
1904
+ char text[112];
1905
+ uint64_t n = 0;
1906
+ for (; n < msg_len && n < 88; n++) text[n] = msg[n];
1907
+ char digits[20];
1908
+ uint64_t d = 0;
1909
+ do {
1910
+ digits[d++] = (char)('0' + v % 10);
1911
+ v /= 10;
1912
+ } while (v);
1913
+ while (d) text[n++] = digits[--d];
1914
+ ts_rust_panic(text, n, loc, loc_len);
1915
+ }
1916
+ #define TS_RUST_PANIC(msg, loc) ts_rust_panic((msg), sizeof(msg) - 1, (loc), sizeof(loc) - 1)
1917
+
1918
+ /*
1919
+ * ---- RedBlackTree (lib-sokoban 0.3's RedBlackTree<K, V, N>, byte for byte) ------------------------------------
1920
+ * At `base`: root u32 (0 = SENTINEL), padding [u32; 3], then the NodeAllocator: size u64, bump_index u32,
1921
+ * free_list_head u32, then N nodes. Node i (1-based) is at base + 32 + (i - 1) * stride: registers [u32; 4] =
1922
+ * [left, right, parent, color (0 black, 1 red)], then the key, then the value. These functions are sokoban's own,
1923
+ * statement for statement (red_black_tree.rs, node_allocator.rs), so every register, size, bump index and free-list
1924
+ * write is the one sokoban makes: the free list is LIFO through register 0 and ends at the bump index (not at 0);
1925
+ * a node taken from the bump index has no register written, one taken from the free list only register 0; removing a
1926
+ * node clears its registers and leaves its key and value bytes. `stride` is a constant at every call (the per-tree
1927
+ * functions the compiler writes), so a node's address is one multiply or shift. Every node index read is checked
1928
+ * against the capacity (as Rust's `nodes[i - 1]` is): a damaged tree panics instead of reading outside the account.
1929
+ */
1930
+ typedef struct {
1931
+ uint8_t *base;
1932
+ uint64_t cap;
1933
+ } TsRbt;
1934
+ /* A node handed out by min / max / nodeAt / iteration: its index (0: none) and its first byte. */
1935
+ typedef struct {
1936
+ uint8_t *node;
1937
+ uint32_t index;
1938
+ } TsRbtEntry;
1939
+ #define TS_RBT_ROOT(b) (*(ts_u32_unaligned *)(b))
1940
+ #define TS_RBT_SIZE(b) (*(ts_u64_unaligned *)((b) + 16))
1941
+ #define TS_RBT_BUMP(b) (*(ts_u32_unaligned *)((b) + 24))
1942
+ #define TS_RBT_FREE(b) (*(ts_u32_unaligned *)((b) + 28))
1943
+ #define TS_RBT_LEFT 0
1944
+ #define TS_RBT_RIGHT 1
1945
+ #define TS_RBT_PARENT 2
1946
+ #define TS_RBT_COLOR 3
1947
+
1948
+ /* Rust's `&self.nodes[(i - 1) as usize]` out of range: index 0 underflows (overflow checks on), past N is out of bounds. */
1949
+ static __attribute__((noinline, noreturn, unused)) void ts_rbt_bad_index(uint64_t cap, uint32_t i) {
1950
+ if (i == 0) TS_RUST_PANIC("attempt to subtract with overflow", "src/node_allocator.rs:255:21");
1951
+ char text[96];
1952
+ uint64_t n = 0;
1953
+ const char *a = "index out of bounds: the len is ";
1954
+ for (uint64_t k = 0; a[k]; k++) text[n++] = a[k];
1955
+ char digits[20];
1956
+ uint64_t d = 0, v = cap;
1957
+ do {
1958
+ digits[d++] = (char)('0' + v % 10);
1959
+ v /= 10;
1960
+ } while (v);
1961
+ while (d) text[n++] = digits[--d];
1962
+ const char *b = " but the index is ";
1963
+ for (uint64_t k = 0; b[k]; k++) text[n++] = b[k];
1964
+ ts_rust_panic_u64(text, n, (uint64_t)i - 1, "src/node_allocator.rs:255:10", 28);
1965
+ }
1966
+ static inline __attribute__((always_inline)) uint8_t *ts_rbt_node(const TsRbt *t, uint64_t stride, uint32_t i) {
1967
+ if ((uint64_t)i - 1 >= t->cap) ts_rbt_bad_index(t->cap, i);
1968
+ return t->base + 32 + ((uint64_t)i - 1) * stride;
1969
+ }
1970
+ /* allocator.get_register / set_register / connect: SENTINEL reads 0 and ignores writes. */
1971
+ /* Node i's first byte, for an index already checked against the capacity (a walk checks each node once, when it
1972
+ steps onto it). */
1973
+ static inline __attribute__((always_inline)) uint8_t *ts_rbt_node_checked(const TsRbt *t, uint64_t stride, uint32_t i) {
1974
+ return t->base + 32 + ((uint64_t)i - 1) * stride;
1975
+ }
1976
+ static inline __attribute__((always_inline)) uint32_t ts_rbt_reg(const TsRbt *t, uint64_t stride, uint32_t i, uint32_t r) {
1977
+ return i ? ((ts_u32_unaligned *)ts_rbt_node(t, stride, i))[r] : 0;
1978
+ }
1979
+ static inline __attribute__((always_inline)) void ts_rbt_set(const TsRbt *t, uint64_t stride, uint32_t i, uint32_t r, uint32_t v) {
1980
+ if (i) ((ts_u32_unaligned *)ts_rbt_node(t, stride, i))[r] = v;
1981
+ }
1982
+ /* _connect(parent, child, dir): parent's register `dir` takes the child (0 too), the child's parent register the parent. */
1983
+ static inline __attribute__((always_inline)) void ts_rbt_connect(const TsRbt *t, uint64_t stride, uint32_t parent, uint32_t child, uint32_t dir) {
1984
+ ts_rbt_set(t, stride, parent, dir, child);
1985
+ ts_rbt_set(t, stride, child, TS_RBT_PARENT, parent);
1986
+ }
1987
+ static inline __attribute__((always_inline)) bool ts_rbt_is_red(const TsRbt *t, uint64_t stride, uint32_t i) {
1988
+ return ts_rbt_reg(t, stride, i, TS_RBT_COLOR) == 1;
1989
+ }
1990
+ static inline __attribute__((always_inline)) bool ts_rbt_is_black(const TsRbt *t, uint64_t stride, uint32_t i) {
1991
+ return ts_rbt_reg(t, stride, i, TS_RBT_COLOR) == 0;
1992
+ }
1993
+ static inline __attribute__((always_inline)) uint32_t ts_rbt_child_dir(const TsRbt *t, uint64_t stride, uint32_t parent, uint32_t child) {
1994
+ uint32_t left = ts_rbt_reg(t, stride, parent, TS_RBT_LEFT);
1995
+ uint32_t right = ts_rbt_reg(t, stride, parent, TS_RBT_RIGHT);
1996
+ if (child == left) return TS_RBT_LEFT;
1997
+ if (child == right) return TS_RBT_RIGHT;
1998
+ TS_RUST_PANIC("Nodes are not connected", "src/red_black_tree.rs:429:13");
1999
+ }
2000
+ static inline __attribute__((always_inline)) void ts_rbt_rotate(const TsRbt *t, uint64_t stride, uint32_t parent, uint32_t dir) {
2001
+ uint32_t grandparent = ts_rbt_reg(t, stride, parent, TS_RBT_PARENT);
2002
+ uint32_t sibling = ts_rbt_reg(t, stride, parent, 1 - dir);
2003
+ if (sibling == 0) return;
2004
+ uint32_t child = ts_rbt_reg(t, stride, sibling, dir);
2005
+ ts_rbt_connect(t, stride, sibling, parent, dir);
2006
+ ts_rbt_connect(t, stride, parent, child, 1 - dir);
2007
+ if (grandparent != 0) {
2008
+ ts_rbt_connect(t, stride, grandparent, sibling, ts_rbt_child_dir(t, stride, grandparent, parent));
2009
+ } else {
2010
+ ts_rbt_set(t, stride, sibling, TS_RBT_PARENT, 0);
2011
+ TS_RBT_ROOT(t->base) = sibling;
2012
+ }
2013
+ }
2014
+ /* NodeAllocator::initialize: size, bump index and free list all 0, else "Cannot reinitialize NodeAllocator". */
2015
+ static inline void ts_rbt_initialize(const TsRbt *t) {
2016
+ uint8_t *b = t->base;
2017
+ if (TS_RBT_SIZE(b) == 0 && TS_RBT_BUMP(b) == 0 && TS_RBT_FREE(b) == 0) {
2018
+ TS_RBT_BUMP(b) = 1;
2019
+ TS_RBT_FREE(b) = 1;
2020
+ } else {
2021
+ TS_RUST_PANIC("Cannot reinitialize NodeAllocator", "src/node_allocator.rs:249:13");
2022
+ }
2023
+ }
2024
+ /* NodeAllocator::add_node: the free list's head, or the bump index when the list is used up; then the key and value. */
2025
+ static inline __attribute__((always_inline)) uint32_t ts_rbt_add_node(const TsRbt *t, uint64_t stride, const void *key, uint64_t ksz,
2026
+ const void *value, uint64_t vsz) {
2027
+ uint8_t *b = t->base;
2028
+ uint32_t i = TS_RBT_FREE(b);
2029
+ if (i == TS_RBT_BUMP(b)) {
2030
+ if ((uint64_t)TS_RBT_BUMP(b) == t->cap + 1) ts_rust_panic_u64("Buffer is full, size ", 21, TS_RBT_SIZE(b), "src/node_allocator.rs:269:17", 28);
2031
+ TS_RBT_BUMP(b) += 1;
2032
+ TS_RBT_FREE(b) = TS_RBT_BUMP(b);
2033
+ } else {
2034
+ uint8_t *n = ts_rbt_node(t, stride, i);
2035
+ TS_RBT_FREE(b) = ((ts_u32_unaligned *)n)[0];
2036
+ ((ts_u32_unaligned *)n)[0] = 0;
2037
+ }
2038
+ uint8_t *n = ts_rbt_node(t, stride, i);
2039
+ ts_memcpy(n + 16, key, ksz);
2040
+ ts_memcpy(n + 16 + ksz, value, vsz);
2041
+ TS_RBT_SIZE(b) += 1;
2042
+ return i;
2043
+ }
2044
+ static inline __attribute__((always_inline)) void ts_rbt_fix_insert(const TsRbt *t, uint64_t stride, uint32_t node) {
2045
+ while (ts_rbt_is_red(t, stride, ts_rbt_reg(t, stride, node, TS_RBT_PARENT))) {
2046
+ uint32_t parent = ts_rbt_reg(t, stride, node, TS_RBT_PARENT);
2047
+ uint32_t grandparent = ts_rbt_reg(t, stride, parent, TS_RBT_PARENT);
2048
+ if (grandparent == 0) {
2049
+ if (TS_RBT_ROOT(t->base) != parent) TS_RUST_PANIC("assertion failed: self.is_root(parent)", "src/red_black_tree.rs:503:17");
2050
+ break;
2051
+ }
2052
+ uint32_t dir = ts_rbt_child_dir(t, stride, grandparent, parent);
2053
+ uint32_t uncle = ts_rbt_reg(t, stride, grandparent, 1 - dir);
2054
+ if (ts_rbt_is_red(t, stride, uncle)) {
2055
+ ts_rbt_set(t, stride, uncle, TS_RBT_COLOR, 0);
2056
+ ts_rbt_set(t, stride, parent, TS_RBT_COLOR, 0);
2057
+ ts_rbt_set(t, stride, grandparent, TS_RBT_COLOR, 1);
2058
+ node = grandparent;
2059
+ } else {
2060
+ if (ts_rbt_child_dir(t, stride, parent, node) == 1 - dir) {
2061
+ ts_rbt_rotate(t, stride, parent, dir);
2062
+ node = parent;
2063
+ }
2064
+ parent = ts_rbt_reg(t, stride, node, TS_RBT_PARENT);
2065
+ grandparent = ts_rbt_reg(t, stride, parent, TS_RBT_PARENT);
2066
+ ts_rbt_set(t, stride, parent, TS_RBT_COLOR, 0);
2067
+ ts_rbt_set(t, stride, grandparent, TS_RBT_COLOR, 1);
2068
+ ts_rbt_rotate(t, stride, grandparent, 1 - dir);
2069
+ }
2070
+ }
2071
+ ts_rbt_set(t, stride, TS_RBT_ROOT(t->base), TS_RBT_COLOR, 0);
2072
+ }
2073
+ /* _insert's new leaf under `parent` (on side `dir`): red, linked, then _fix_insert unless the parent is the root. */
2074
+ static inline __attribute__((always_inline)) void ts_rbt_link_leaf(const TsRbt *t, uint64_t stride, uint32_t parent, uint32_t node, uint32_t dir) {
2075
+ ts_rbt_set(t, stride, node, TS_RBT_COLOR, 1);
2076
+ ts_rbt_connect(t, stride, parent, node, dir);
2077
+ if (ts_rbt_reg(t, stride, parent, TS_RBT_PARENT) != 0) ts_rbt_fix_insert(t, stride, node);
2078
+ }
2079
+ static inline __attribute__((always_inline)) uint32_t ts_rbt_find_min(const TsRbt *t, uint64_t stride, uint32_t node) {
2080
+ while (ts_rbt_reg(t, stride, node, TS_RBT_LEFT) != 0) node = ts_rbt_reg(t, stride, node, TS_RBT_LEFT);
2081
+ return node;
2082
+ }
2083
+ static inline __attribute__((always_inline)) uint32_t ts_rbt_find_max(const TsRbt *t, uint64_t stride, uint32_t node) {
2084
+ while (ts_rbt_reg(t, stride, node, TS_RBT_RIGHT) != 0) node = ts_rbt_reg(t, stride, node, TS_RBT_RIGHT);
2085
+ return node;
2086
+ }
2087
+ static inline __attribute__((always_inline)) void ts_rbt_transplant(const TsRbt *t, uint64_t stride, uint32_t target, uint32_t source) {
2088
+ uint32_t parent = ts_rbt_reg(t, stride, target, TS_RBT_PARENT);
2089
+ if (parent == 0) {
2090
+ TS_RBT_ROOT(t->base) = source;
2091
+ ts_rbt_set(t, stride, source, TS_RBT_PARENT, 0);
2092
+ return;
2093
+ }
2094
+ ts_rbt_connect(t, stride, parent, source, ts_rbt_child_dir(t, stride, parent, target));
2095
+ }
2096
+ static inline __attribute__((always_inline)) void ts_rbt_fix_remove(const TsRbt *t, uint64_t stride, uint32_t node, bool has_pd, uint32_t parent, uint32_t dir) {
2097
+ /* `parent_and_dir.unwrap_or({ … })` evaluates its argument first: a pure read here (node is SENTINEL whenever
2098
+ parent_and_dir is Some), kept for the panic it could raise on a damaged tree. */
2099
+ uint32_t p0 = ts_rbt_reg(t, stride, node, TS_RBT_PARENT);
2100
+ uint32_t d0 = ts_rbt_child_dir(t, stride, p0, node);
2101
+ if (!has_pd) {
2102
+ parent = p0;
2103
+ dir = d0;
2104
+ }
2105
+ for (;;) {
2106
+ uint32_t sibling = ts_rbt_reg(t, stride, parent, 1 - dir);
2107
+ if (ts_rbt_is_red(t, stride, sibling)) {
2108
+ ts_rbt_set(t, stride, sibling, TS_RBT_COLOR, 0);
2109
+ ts_rbt_set(t, stride, parent, TS_RBT_COLOR, 1);
2110
+ ts_rbt_rotate(t, stride, parent, dir);
2111
+ sibling = ts_rbt_reg(t, stride, parent, 1 - dir);
2112
+ }
2113
+ if (ts_rbt_is_black(t, stride, ts_rbt_reg(t, stride, sibling, TS_RBT_LEFT)) &&
2114
+ ts_rbt_is_black(t, stride, ts_rbt_reg(t, stride, sibling, TS_RBT_RIGHT))) {
2115
+ ts_rbt_set(t, stride, sibling, TS_RBT_COLOR, 1);
2116
+ node = parent;
2117
+ } else {
2118
+ if (ts_rbt_is_black(t, stride, ts_rbt_reg(t, stride, sibling, 1 - dir))) {
2119
+ ts_rbt_set(t, stride, ts_rbt_reg(t, stride, sibling, dir), TS_RBT_COLOR, 0);
2120
+ ts_rbt_set(t, stride, sibling, TS_RBT_COLOR, 1);
2121
+ ts_rbt_rotate(t, stride, sibling, 1 - dir);
2122
+ sibling = ts_rbt_reg(t, stride, parent, 1 - dir);
2123
+ }
2124
+ ts_rbt_set(t, stride, sibling, TS_RBT_COLOR, ts_rbt_reg(t, stride, parent, TS_RBT_COLOR));
2125
+ ts_rbt_set(t, stride, parent, TS_RBT_COLOR, 0);
2126
+ ts_rbt_set(t, stride, ts_rbt_reg(t, stride, sibling, 1 - dir), TS_RBT_COLOR, 0);
2127
+ ts_rbt_rotate(t, stride, parent, dir);
2128
+ node = TS_RBT_ROOT(t->base);
2129
+ }
2130
+ if (TS_RBT_ROOT(t->base) == node || ts_rbt_is_red(t, stride, node)) break;
2131
+ parent = ts_rbt_reg(t, stride, node, TS_RBT_PARENT);
2132
+ dir = ts_rbt_child_dir(t, stride, parent, node);
2133
+ }
2134
+ ts_rbt_set(t, stride, node, TS_RBT_COLOR, 0);
2135
+ }
2136
+ /* _remove_tree_node: unlink node `n` (a leaf, one child transplanted, or replaced by its predecessor), free it, rebalance. */
2137
+ static inline __attribute__((always_inline)) void ts_rbt_remove_node(const TsRbt *t, uint64_t stride, uint32_t n) {
2138
+ uint8_t *b = t->base;
2139
+ bool black = ts_rbt_is_black(t, stride, n);
2140
+ uint32_t left = ts_rbt_reg(t, stride, n, TS_RBT_LEFT);
2141
+ uint32_t right = ts_rbt_reg(t, stride, n, TS_RBT_RIGHT);
2142
+ uint32_t pivot;
2143
+ bool has_pd = false;
2144
+ uint32_t pd_parent = 0, pd_dir = 0;
2145
+ if (ts_rbt_reg(t, stride, n, TS_RBT_LEFT) == 0 && ts_rbt_reg(t, stride, n, TS_RBT_RIGHT) == 0) {
2146
+ if (TS_RBT_ROOT(b) != n) {
2147
+ uint32_t parent = ts_rbt_reg(t, stride, n, TS_RBT_PARENT);
2148
+ uint32_t dir = ts_rbt_child_dir(t, stride, parent, n);
2149
+ ts_rbt_connect(t, stride, parent, 0, dir);
2150
+ pivot = 0;
2151
+ has_pd = true;
2152
+ pd_parent = parent;
2153
+ pd_dir = dir;
2154
+ } else {
2155
+ TS_RBT_ROOT(b) = 0;
2156
+ pivot = 0;
2157
+ }
2158
+ } else if (left == 0) {
2159
+ ts_rbt_transplant(t, stride, n, right);
2160
+ pivot = right;
2161
+ } else if (right == 0) {
2162
+ ts_rbt_transplant(t, stride, n, left);
2163
+ pivot = left;
2164
+ } else {
2165
+ uint32_t max_left = ts_rbt_find_max(t, stride, left);
2166
+ uint32_t max_left_parent = ts_rbt_reg(t, stride, max_left, TS_RBT_PARENT);
2167
+ uint32_t max_left_child = ts_rbt_reg(t, stride, max_left, TS_RBT_LEFT);
2168
+ black = ts_rbt_is_black(t, stride, max_left);
2169
+ if (ts_rbt_reg(t, stride, max_left, TS_RBT_PARENT) != n) {
2170
+ ts_rbt_transplant(t, stride, max_left, max_left_child);
2171
+ ts_rbt_connect(t, stride, max_left, ts_rbt_reg(t, stride, n, TS_RBT_LEFT), TS_RBT_LEFT);
2172
+ if (max_left_child == 0) {
2173
+ has_pd = true;
2174
+ pd_parent = max_left_parent;
2175
+ pd_dir = TS_RBT_RIGHT;
2176
+ }
2177
+ } else if (max_left_child == 0) {
2178
+ if (ts_rbt_reg(t, stride, max_left, TS_RBT_LEFT) != 0 || ts_rbt_reg(t, stride, max_left, TS_RBT_RIGHT) != 0)
2179
+ TS_RUST_PANIC("assertion failed: self.is_leaf(max_left)", "src/red_black_tree.rs:598:17");
2180
+ has_pd = true;
2181
+ pd_parent = max_left;
2182
+ pd_dir = TS_RBT_LEFT;
2183
+ }
2184
+ ts_rbt_transplant(t, stride, n, max_left);
2185
+ ts_rbt_connect(t, stride, max_left, ts_rbt_reg(t, stride, n, TS_RBT_RIGHT), TS_RBT_RIGHT);
2186
+ ts_rbt_set(t, stride, max_left, TS_RBT_COLOR, ts_rbt_reg(t, stride, n, TS_RBT_COLOR));
2187
+ pivot = max_left_child;
2188
+ }
2189
+ /* _remove_allocator_node: registers cleared, then onto the free list (key and value bytes stay). */
2190
+ ts_rbt_set(t, stride, n, TS_RBT_PARENT, 0);
2191
+ ts_rbt_set(t, stride, n, TS_RBT_COLOR, 0);
2192
+ ts_rbt_set(t, stride, n, TS_RBT_LEFT, 0);
2193
+ ts_rbt_set(t, stride, n, TS_RBT_RIGHT, 0);
2194
+ if (n != 0) {
2195
+ ts_rbt_set(t, stride, n, 0, TS_RBT_FREE(b));
2196
+ TS_RBT_FREE(b) = n;
2197
+ if (TS_RBT_SIZE(b) == 0) TS_RUST_PANIC("attempt to subtract with overflow", "src/node_allocator.rs:291:9");
2198
+ TS_RBT_SIZE(b) -= 1;
2199
+ }
2200
+ if (black) {
2201
+ if (TS_RBT_ROOT(b) == pivot)
2202
+ ts_rbt_set(t, stride, pivot, TS_RBT_COLOR, 0);
2203
+ else
2204
+ ts_rbt_fix_remove(t, stride, pivot, has_pd, pd_parent, pd_dir);
2205
+ }
2206
+ }
2207
+ /* For iteration: an in-order walk keeps at most the tree's height of nodes, under 64 for any red-black tree a u32
2208
+ index can address; a deeper (damaged) tree stops with a panic. */
2209
+ #define TS_RBT_STACK 64
2210
+ static __attribute__((noinline, noreturn, unused)) void ts_rbt_too_deep(void) {
2211
+ TS_RUST_PANIC("TypeSOL: RedBlackTree deeper than a red-black tree can be", "rbtree");
2212
+ }
2213
+
1807
2214
  /*
1808
2215
  * Did an Ed25519 precompile instruction in this transaction verify `message` signed by `signer`?
1809
2216
  * Reads the Instructions sysvar (data: u16 count, u16 offsets[count], then per instruction