@typesol/cli 0.41.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.
- package/CHANGELOG.md +319 -0
- package/README.md +11 -7
- package/dist/compiler/analyze.js +484 -13
- package/dist/compiler/diagnostics.d.ts +1 -0
- package/dist/compiler/diagnostics.js +13 -0
- package/dist/compiler/emit/alias.d.ts +44 -0
- package/dist/compiler/emit/alias.js +135 -4
- package/dist/compiler/emit/base.d.ts +65 -7
- package/dist/compiler/emit/base.js +91 -7
- package/dist/compiler/emit/borsh.d.ts +7 -0
- package/dist/compiler/emit/borsh.js +86 -1
- package/dist/compiler/emit/calls.d.ts +169 -2
- package/dist/compiler/emit/calls.js +1170 -81
- package/dist/compiler/emit/cpi-core.d.ts +28 -5
- package/dist/compiler/emit/cpi-core.js +238 -12
- package/dist/compiler/emit/cpi.js +16 -16
- package/dist/compiler/emit/create.d.ts +3 -3
- package/dist/compiler/emit/create.js +33 -25
- package/dist/compiler/emit/expressions.js +89 -9
- package/dist/compiler/emit/function.js +2 -2
- package/dist/compiler/emit/outline.d.ts +24 -0
- package/dist/compiler/emit/outline.js +100 -0
- package/dist/compiler/emit/program.js +37 -3
- package/dist/compiler/emit/rbtree.d.ts +7 -0
- package/dist/compiler/emit/rbtree.js +142 -0
- package/dist/compiler/emit/shared.d.ts +43 -1
- package/dist/compiler/emit/shared.js +18 -2
- package/dist/compiler/emit/statements.d.ts +58 -1
- package/dist/compiler/emit/statements.js +549 -22
- package/dist/compiler/emit/storage.d.ts +48 -1
- package/dist/compiler/emit/storage.js +220 -63
- package/dist/compiler/emit/token.d.ts +11 -8
- package/dist/compiler/emit/token.js +233 -104
- package/dist/compiler/emit/views.d.ts +60 -0
- package/dist/compiler/emit/views.js +523 -0
- package/dist/compiler/hints.js +6 -0
- package/dist/compiler/idl.js +2 -1
- package/dist/compiler/model.d.ts +84 -4
- package/dist/compiler/model.js +19 -0
- package/dist/compiler/toolchain.d.ts +15 -0
- package/dist/compiler/toolchain.js +146 -11
- package/dist/testing-stake.d.ts +216 -0
- package/dist/testing-stake.js +526 -0
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +1 -0
- package/docs/architecture.md +9 -2
- package/docs/case-study-phoenix.md +226 -0
- package/docs/case-study-spl-stake-pool.md +122 -0
- package/docs/errors.md +1 -1
- package/docs/language.md +244 -20
- package/docs/migrate-from-anchor.md +1 -0
- package/docs/runtime.md +1 -1
- package/docs/security-model.md +4 -0
- package/docs/stability.md +33 -1
- package/docs/test-your-program.md +47 -0
- package/docs/testing.md +19 -0
- package/docs/troubleshooting.md +5 -2
- package/lib/typesol.d.ts +462 -44
- package/llms-full.txt +338 -26
- package/llms.txt +1 -1
- package/package.json +2 -2
- package/runtime/typesol_rt.h +476 -0
package/llms.txt
CHANGED
|
@@ -27,4 +27,4 @@ The rules and the whole reference in one file: https://typesol.com/llms-full.txt
|
|
|
27
27
|
|
|
28
28
|
## Optional
|
|
29
29
|
|
|
30
|
-
- [Case studies](https://typesol.com/docs/case-study-marginfi/): mainnet programs (Marginfi v2, Raydium AMM v4, CLMM, CP-Swap, Jito) ported and checked against their binaries
|
|
30
|
+
- [Case studies](https://typesol.com/docs/case-study-marginfi/): mainnet programs (Marginfi v2, Phoenix v1, SPL Stake Pool, Raydium AMM v4, CLMM, CP-Swap, Jito) ported and checked against their binaries
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typesol/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.43.0",
|
|
4
4
|
"description": "Compile a TypeScript-syntax language directly to Solana SBF programs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"test": "vitest run",
|
|
66
66
|
"typesol": "node dist/cli.js",
|
|
67
67
|
"smoke": "node scripts/smoke.ts",
|
|
68
|
-
"fuzz": "FUZZ_RUNS=2000 FUZZ_STEPS=600 FUZZ_VERBOSE=1 vitest run test/verification/fuzz-arith.test.ts test/verification/fuzz-state.test.ts test/verification/fuzz-layout.test.ts test/verification/differential.test.ts showcase/test/fuzz-jito.test.ts showcase/test/fuzz-marginfi.test.ts --silent=false",
|
|
68
|
+
"fuzz": "FUZZ_RUNS=2000 FUZZ_STEPS=600 FUZZ_VERBOSE=1 vitest run test/verification/fuzz-arith.test.ts test/verification/fuzz-state.test.ts test/verification/fuzz-layout.test.ts test/verification/differential.test.ts showcase/test/fuzz-jito.test.ts showcase/test/fuzz-marginfi.test.ts showcase/test/phoenix-fuzz.test.ts --silent=false",
|
|
69
69
|
"build:twins": "cd test/anchor-twins && cargo-build-sbf",
|
|
70
70
|
"prepack": "npm run build",
|
|
71
71
|
"fetch:programs": "bash scripts/fetch-programs.sh",
|
package/runtime/typesol_rt.h
CHANGED
|
@@ -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);
|
|
@@ -938,6 +992,26 @@ static inline uint64_t ts_last_restart_slot(uint64_t *out) {
|
|
|
938
992
|
return SUCCESS;
|
|
939
993
|
}
|
|
940
994
|
|
|
995
|
+
/* EpochRewards, as the syscall writes it: Rust's #[repr(C, align(16))] struct (81 bytes of fields, 96 in memory). The
|
|
996
|
+
runtime checks the destination's 16-byte alignment, hence the attribute. */
|
|
997
|
+
typedef struct __attribute__((aligned(16))) {
|
|
998
|
+
uint64_t distribution_starting_block_height;
|
|
999
|
+
uint64_t num_partitions;
|
|
1000
|
+
SolPubkey parent_blockhash;
|
|
1001
|
+
unsigned __int128 total_points;
|
|
1002
|
+
uint64_t total_rewards;
|
|
1003
|
+
uint64_t distributed_rewards;
|
|
1004
|
+
uint8_t active;
|
|
1005
|
+
} TsEpochRewards;
|
|
1006
|
+
_Static_assert(sizeof(TsEpochRewards) == 96 && __builtin_offsetof(TsEpochRewards, total_points) == 48 &&
|
|
1007
|
+
__builtin_offsetof(TsEpochRewards, active) == 80,
|
|
1008
|
+
"EpochRewards layout");
|
|
1009
|
+
uint64_t sol_get_epoch_rewards_sysvar(void *ret);
|
|
1010
|
+
static inline uint64_t ts_epoch_rewards(TsEpochRewards *out) {
|
|
1011
|
+
if (sol_get_epoch_rewards_sysvar(out) != SUCCESS) TS_FAIL(TS_ERR_SYSVAR, "TypeSOL: epoch rewards sysvar unavailable");
|
|
1012
|
+
return SUCCESS;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
941
1015
|
/* The general case, in integers: exactly (uint64_t)((double)per_year * threshold) as soft-float computes it (the
|
|
942
1016
|
conversion and the product rounded to nearest, ties to even; NaN, negative and < 1 give 0), or UINT64_MAX when that
|
|
943
1017
|
product is >= 2^64 (no double below 2^64 converts to UINT64_MAX: the largest is 2^64 - 2048). No soft-float routine
|
|
@@ -1350,6 +1424,11 @@ static inline uint64_t ts_resize(TsParams *params, TsAccount *payer, TsAccount *
|
|
|
1350
1424
|
return SUCCESS;
|
|
1351
1425
|
}
|
|
1352
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
|
+
|
|
1353
1432
|
/* resizeAccount(): the data length only (AccountInfo::resize), new bytes zeroed; no rent moves. */
|
|
1354
1433
|
static inline uint64_t ts_resize_raw(TsAccount *acct, uint64_t new_len, uint64_t entry_len) {
|
|
1355
1434
|
if (new_len > entry_len + MAX_PERMITTED_DATA_INCREASE)
|
|
@@ -1414,6 +1493,22 @@ static inline uint64_t ts_cpi(TsParams *params, const TsAccount *program, uint8_
|
|
|
1414
1493
|
return ts_invoke(params, &ix, &signer, nseeds > 0 ? 1 : 0);
|
|
1415
1494
|
}
|
|
1416
1495
|
|
|
1496
|
+
/* A CPI whose account infos are `accounts` (one per meta, in order), as Rust's invoke_signed(&ix, &[a, b, …]) takes
|
|
1497
|
+
them: a meta may name an address other than its account's key (`named(account, address)`: a sysvar, as
|
|
1498
|
+
solana_program's stake instruction builders name them). The runtime looks each meta's key up among these infos only,
|
|
1499
|
+
so a slot holding another account fails the call (MissingAccount) as it fails the original's, wherever else in the
|
|
1500
|
+
instruction the named account is. */
|
|
1501
|
+
static inline uint64_t ts_cpi_named(TsParams *params, const TsAccount *program, uint8_t *data, uint64_t len, SolAccountMeta *metas,
|
|
1502
|
+
uint64_t n, const TsAccount *const *accounts, const SolSignerSeed *seeds, int nseeds) {
|
|
1503
|
+
SolInstruction ix = {(SolPubkey *)&program->key, metas, n, data, len};
|
|
1504
|
+
SolSignerSeeds signer = {seeds, (uint64_t)nseeds};
|
|
1505
|
+
TS_INFOS_RESERVE(params, n);
|
|
1506
|
+
SolAccountInfo *infos = params->infos;
|
|
1507
|
+
for (uint64_t i = 0; i < n; i++) ts_info(accounts[i], &infos[i]);
|
|
1508
|
+
TS_OWNER_FORGET(params);
|
|
1509
|
+
return sol_invoke_signed(&ix, infos, n, &signer, nseeds > 0 ? 1 : 0);
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1417
1512
|
/* ---- Token-2022 extensions (TLV after the 165-byte base and the account-type byte) ---- */
|
|
1418
1513
|
/* `a` must be a token-program mint (want 1) or account (want 2), or either (want 0): legacy SPL Token accounts
|
|
1419
1514
|
simply have no extensions; a Token-2022 multisig or a foreign account is refused. */
|
|
@@ -1734,6 +1829,387 @@ static inline bool ts_ix_is(const TsIx *ix, const SolPubkey *program, const uint
|
|
|
1734
1829
|
}
|
|
1735
1830
|
/* How deep in CPIs the program runs: 1 when the transaction calls it directly. */
|
|
1736
1831
|
extern uint64_t sol_get_stack_height(void);
|
|
1832
|
+
/* The runtime's `abort` syscall (Rust's abort on SBF, e.g. the allocator's out-of-memory error): ProgramFailedToComplete,
|
|
1833
|
+
* logged as "SBF program panicked", with no location. */
|
|
1834
|
+
extern __attribute__((noreturn)) void abort(void);
|
|
1835
|
+
|
|
1836
|
+
/*
|
|
1837
|
+
* ---- BigVec (SPL's big_vec) ------------------------------------------------
|
|
1838
|
+
* The memmove syscall, for BigVec::retain's block moves as the Rust does: it costs max(10, n / 250) CU, where the
|
|
1839
|
+
* word loop of ts_memmove costs about n / 2 (a 1.4 MB validator list moves in a few thousand CU, not hundreds of
|
|
1840
|
+
* thousands). Overlapping ranges are allowed. The "no SDK byte loops" rule is about the SDK's C loops, not this.
|
|
1841
|
+
*/
|
|
1842
|
+
extern void sol_memmove_(void *dst, const void *src, uint64_t n);
|
|
1843
|
+
/* The memcpy syscall, for bulk copies of a length known only at run time (FixedVec.pushAll): max(10, n / 250) CU plus
|
|
1844
|
+
* the call, against about 0.74 CU a byte for ts_memcpy's word loop over unaligned data (measured: 1000 bytes of
|
|
1845
|
+
* instruction data), so copies under 32 bytes keep the loop. The ranges never overlap (a FixedVec's free slots and
|
|
1846
|
+
* another list, or its own elements). */
|
|
1847
|
+
extern void sol_memcpy_(void *dst, const void *src, uint64_t n);
|
|
1848
|
+
#define TS_MEMCPY_SYSCALL_MIN 32
|
|
1849
|
+
static inline __attribute__((always_inline)) void ts_memcpy_bulk(void *dst, const void *src, uint64_t n) {
|
|
1850
|
+
if (n >= TS_MEMCPY_SYSCALL_MIN) sol_memcpy_(dst, src, n);
|
|
1851
|
+
else ts_memcpy(dst, src, n);
|
|
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
|
+
}
|
|
1869
|
+
/* A BigVec's room: a BigVec struct is only ever a loadRaw view, which sits at a TsAccount's data, right after its
|
|
1870
|
+
* data_len (see TsAccount); the elements that fit in the account past the struct (its fixed fields and the length). */
|
|
1871
|
+
static inline __attribute__((always_inline)) uint64_t ts_bigvec_cap(const uint8_t *data, uint64_t fixed, uint64_t elem) {
|
|
1872
|
+
uint64_t len = ts_ld64(data - 8);
|
|
1873
|
+
return len < fixed ? 0 : (len - fixed) / elem;
|
|
1874
|
+
}
|
|
1875
|
+
/* The same pointer, but one the optimizer cannot relate to `p`. BigVec::retain's scan loop steps one pointer over the
|
|
1876
|
+
* elements; its removal branch computes the moves from this copy, so clang's loop strength reduction does not rebuild
|
|
1877
|
+
* the scan around offsets the moves share (measured: 5 instructions per kept element, against 6 to 9 without). */
|
|
1878
|
+
static inline __attribute__((always_inline)) uint8_t *ts_opaque_ptr(uint8_t *p) {
|
|
1879
|
+
__asm__("" : "+r"(p));
|
|
1880
|
+
return p;
|
|
1881
|
+
}
|
|
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
|
+
}
|
|
1737
2213
|
|
|
1738
2214
|
/*
|
|
1739
2215
|
* Did an Ed25519 precompile instruction in this transaction verify `message` signed by `signer`?
|