@typesol/cli 0.42.0 → 0.44.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 +350 -0
- package/NOTICE +71 -0
- package/README.md +35 -18
- package/SECURITY.md +32 -5
- package/dist/ai.d.ts +1 -1
- package/dist/ai.js +5 -5
- package/dist/cli.js +196 -72
- package/dist/compiler/analyze.js +447 -12
- package/dist/compiler/diagnostics.d.ts +1 -0
- package/dist/compiler/diagnostics.js +13 -0
- package/dist/compiler/emit/alias.d.ts +45 -1
- package/dist/compiler/emit/alias.js +143 -6
- package/dist/compiler/emit/base.d.ts +28 -1
- package/dist/compiler/emit/base.js +77 -9
- package/dist/compiler/emit/borsh.js +11 -5
- package/dist/compiler/emit/calls.d.ts +80 -0
- package/dist/compiler/emit/calls.js +612 -54
- package/dist/compiler/emit/cpi-core.d.ts +23 -4
- package/dist/compiler/emit/cpi-core.js +261 -12
- package/dist/compiler/emit/cpi.js +8 -2
- package/dist/compiler/emit/create.js +3 -0
- package/dist/compiler/emit/expressions.js +44 -8
- package/dist/compiler/emit/function.js +10 -0
- package/dist/compiler/emit/program.js +30 -1
- package/dist/compiler/emit/rbtree.d.ts +7 -0
- package/dist/compiler/emit/rbtree.js +142 -0
- package/dist/compiler/emit/shared.d.ts +25 -0
- package/dist/compiler/emit/shared.js +45 -4
- package/dist/compiler/emit/statements.d.ts +27 -1
- package/dist/compiler/emit/statements.js +305 -23
- package/dist/compiler/emit/storage.d.ts +25 -2
- package/dist/compiler/emit/storage.js +116 -64
- package/dist/compiler/emit/token.js +2 -2
- package/dist/compiler/emit/views.d.ts +60 -0
- package/dist/compiler/emit/views.js +523 -0
- package/dist/compiler/idl.js +2 -1
- package/dist/compiler/index.d.ts +15 -1
- package/dist/compiler/index.js +40 -3
- package/dist/compiler/model.d.ts +65 -4
- package/dist/compiler/model.js +14 -0
- package/dist/compiler/toolchain.d.ts +56 -1
- package/dist/compiler/toolchain.js +246 -12
- package/dist/testing-stake.d.ts +2 -1
- package/dist/testing-stake.js +2 -1
- package/dist/testing.js +6 -4
- package/dist/tools/clientgen.d.ts +15 -0
- package/dist/tools/clientgen.js +173 -7
- package/dist/tools/cpigen.d.ts +0 -15
- package/dist/tools/cpigen.js +19 -8
- package/dist/tools/doctor.d.ts +17 -2
- package/dist/tools/doctor.js +64 -4
- package/dist/tools/portgen.js +43 -18
- package/dist/workspace.d.ts +20 -0
- package/dist/workspace.js +173 -6
- package/docs/ai.md +10 -5
- package/docs/architecture.md +10 -3
- package/docs/audit.md +63 -17
- package/docs/case-study-escrow.md +1 -1
- package/docs/case-study-phoenix.md +226 -0
- package/docs/case-study-raydium-amm.md +3 -3
- package/docs/case-study-spl-stake-pool.md +5 -5
- package/docs/cli.md +17 -8
- package/docs/client.md +11 -3
- package/docs/errors.md +1 -1
- package/docs/language.md +148 -12
- package/docs/mainnet.md +21 -10
- package/docs/migrate-from-anchor.md +3 -1
- package/docs/runtime.md +12 -2
- package/docs/security-model.md +38 -6
- package/docs/stability.md +42 -4
- package/docs/testing.md +25 -0
- package/docs/troubleshooting.md +17 -2
- package/lib/typesol.d.ts +221 -4
- package/llms-full.txt +336 -65
- package/llms.txt +2 -2
- package/package.json +4 -2
- package/runtime/typesol_rt.h +429 -13
- package/test/programs/LICENSE-Apache-2.0.txt +201 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { fieldC, scalarC } from "./shared.js";
|
|
2
|
+
/**
|
|
3
|
+
* The C functions of each RedBlackTree instance (`rbTreeAt<K, V>(…, compare?)`): its key order, and lib-sokoban 0.3's
|
|
4
|
+
* get_addr, _insert and _remove over runtime/typesol_rt.h's structural functions (ts_rbt_*), with the node stride a
|
|
5
|
+
* constant. Each returns SUCCESS or the error a comparator helper failed with; results go through `out`.
|
|
6
|
+
*/
|
|
7
|
+
export function emitRbTrees(model) {
|
|
8
|
+
const out = [];
|
|
9
|
+
for (const t of model.rbTrees.values())
|
|
10
|
+
out.push(...emitTree(model, t));
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
function emitTree(model, t) {
|
|
14
|
+
const K = fieldC(t.keyType);
|
|
15
|
+
const V = fieldC(t.valueType);
|
|
16
|
+
const S = t.stride;
|
|
17
|
+
const ks = t.keySize;
|
|
18
|
+
const vs = t.valueSize;
|
|
19
|
+
const p = t.c;
|
|
20
|
+
const cmp = t.compare ? helperCompare(t) : defaultCompare(model, t.keyType);
|
|
21
|
+
return [
|
|
22
|
+
`/* RedBlackTree<${t.key.split("|").slice(0, 2).join(", ")}>: lib-sokoban 0.3, stride ${S}${t.compare ? `, ordered by ${t.compare.display}` : ""} */`,
|
|
23
|
+
`static inline __attribute__((always_inline)) uint64_t ${p}_cmp(TsParams *params, const ${K} *a, const ${K} *b, int *out) {`,
|
|
24
|
+
` (void)params;`,
|
|
25
|
+
...cmp.map((l) => ` ${l}`),
|
|
26
|
+
`}`,
|
|
27
|
+
`/* get_addr: the node holding \`key\`, or 0. */`,
|
|
28
|
+
`static __attribute__((noinline, unused)) uint64_t ${p}_find(TsParams *params, const TsRbt *t, const ${K} *key, uint32_t *out) {`,
|
|
29
|
+
` uint32_t i = TS_RBT_ROOT(t->base);`,
|
|
30
|
+
` while (i != 0) {`,
|
|
31
|
+
` int c;`,
|
|
32
|
+
` TS_TRY(${p}_cmp(params, key, (const ${K} *)(ts_rbt_node(t, ${S}, i) + 16), &c));`,
|
|
33
|
+
` if (c == 0) break;`,
|
|
34
|
+
` i = ts_rbt_reg(t, ${S}, i, c < 0 ? TS_RBT_LEFT : TS_RBT_RIGHT);`,
|
|
35
|
+
` }`,
|
|
36
|
+
` *out = i;`,
|
|
37
|
+
` return SUCCESS;`,
|
|
38
|
+
`}`,
|
|
39
|
+
`/* _insert: the node now holding \`key\` (an equal key keeps its node and takes the value), or 0 when a new key finds the tree full. */`,
|
|
40
|
+
`static __attribute__((noinline, unused)) uint64_t ${p}_insert(TsParams *params, const TsRbt *t, const ${K} *key, const ${V} *value, uint32_t *out) {`,
|
|
41
|
+
` uint32_t parent = TS_RBT_ROOT(t->base);`,
|
|
42
|
+
` if (parent == 0) {`,
|
|
43
|
+
` uint32_t i = ts_rbt_add_node(t, ${S}, key, ${ks}, value, ${vs});`,
|
|
44
|
+
` TS_RBT_ROOT(t->base) = i;`,
|
|
45
|
+
` *out = i;`,
|
|
46
|
+
` return SUCCESS;`,
|
|
47
|
+
` }`,
|
|
48
|
+
` for (;;) {`,
|
|
49
|
+
` int c;`,
|
|
50
|
+
` TS_TRY(${p}_cmp(params, key, (const ${K} *)(ts_rbt_node(t, ${S}, parent) + 16), &c));`,
|
|
51
|
+
` if (c == 0) {`,
|
|
52
|
+
` ts_memcpy(ts_rbt_node(t, ${S}, parent) + ${16 + ks}, value, ${vs});`,
|
|
53
|
+
` *out = parent;`,
|
|
54
|
+
` return SUCCESS;`,
|
|
55
|
+
` }`,
|
|
56
|
+
` uint32_t dir = c < 0 ? TS_RBT_LEFT : TS_RBT_RIGHT;`,
|
|
57
|
+
` uint32_t target = ts_rbt_reg(t, ${S}, parent, dir);`,
|
|
58
|
+
` if (target == 0) {`,
|
|
59
|
+
` if (TS_RBT_SIZE(t->base) >= t->cap) {`,
|
|
60
|
+
` *out = 0;`,
|
|
61
|
+
` return SUCCESS;`,
|
|
62
|
+
` }`,
|
|
63
|
+
` uint32_t i = ts_rbt_add_node(t, ${S}, key, ${ks}, value, ${vs});`,
|
|
64
|
+
` ts_rbt_link_leaf(t, ${S}, parent, i, dir);`,
|
|
65
|
+
` *out = i;`,
|
|
66
|
+
` return SUCCESS;`,
|
|
67
|
+
` }`,
|
|
68
|
+
` parent = target;`,
|
|
69
|
+
` }`,
|
|
70
|
+
`}`,
|
|
71
|
+
`/* _remove: the node that held \`key\` (now free, its key and value bytes as they were), or 0. */`,
|
|
72
|
+
`static __attribute__((noinline, unused)) uint64_t ${p}_remove(TsParams *params, const TsRbt *t, const ${K} *key, uint32_t *out) {`,
|
|
73
|
+
` uint32_t i;`,
|
|
74
|
+
` TS_TRY(${p}_find(params, t, key, &i));`,
|
|
75
|
+
` if (i != 0) ts_rbt_remove_node(t, ${S}, i);`,
|
|
76
|
+
` *out = i;`,
|
|
77
|
+
` return SUCCESS;`,
|
|
78
|
+
`}`,
|
|
79
|
+
`/* removeAt: node \`i\` unlinked and freed as _remove frees the node it finds; 0 (nothing written) for index 0 or a node`,
|
|
80
|
+
` in no tree (not the root, no parent: a freed or never used one). Past the capacity panics as get_node does. */`,
|
|
81
|
+
`static __attribute__((noinline, unused)) uint32_t ${p}_remove_at(const TsRbt *t, uint32_t i) {`,
|
|
82
|
+
` if (i == 0 || (TS_RBT_ROOT(t->base) != i && ts_rbt_reg(t, ${S}, i, TS_RBT_PARENT) == 0)) return 0;`,
|
|
83
|
+
` ts_rbt_remove_node(t, ${S}, i);`,
|
|
84
|
+
` return i;`,
|
|
85
|
+
`}`,
|
|
86
|
+
``,
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
/** A comparator helper `(a: K, b: K) => int`: called as compare(searched, nodeKey), its sign the order. */
|
|
90
|
+
function helperCompare(t) {
|
|
91
|
+
const h = t.compare;
|
|
92
|
+
const arg = (side) => {
|
|
93
|
+
const pt = h.params[0].type;
|
|
94
|
+
if (pt.kind === "structRef")
|
|
95
|
+
return `(TSS_${pt.struct} *)${side}`;
|
|
96
|
+
if (pt.kind === "pubkey")
|
|
97
|
+
return `*${side}`;
|
|
98
|
+
if (pt.kind === "bool")
|
|
99
|
+
return `(*${side} != 0)`;
|
|
100
|
+
return `*${side}`;
|
|
101
|
+
};
|
|
102
|
+
const ret = h.returns;
|
|
103
|
+
return [
|
|
104
|
+
`${scalarC(ret)} r;`,
|
|
105
|
+
`TS_TRY(fn_${h.name}(params, ${arg("a")}, ${arg("b")}, &r));`,
|
|
106
|
+
// Signed: an unsigned result is refused where the tree is made (views.ts), as it could never say "less".
|
|
107
|
+
`*out = r < 0 ? -1 : r > 0 ? 1 : 0;`,
|
|
108
|
+
`return SUCCESS;`,
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
/** The default order: Rust's derived Ord, field by field in declaration order (integers by value, Pubkeys bytewise). */
|
|
112
|
+
function defaultCompare(model, key) {
|
|
113
|
+
const lines = [];
|
|
114
|
+
let n = 0;
|
|
115
|
+
const walk = (t, a, b) => {
|
|
116
|
+
switch (t.kind) {
|
|
117
|
+
case "int":
|
|
118
|
+
case "bool":
|
|
119
|
+
case "enum":
|
|
120
|
+
lines.push(`if (${a} != ${b}) {`, ` *out = ${a} < ${b} ? -1 : 1;`, ` return SUCCESS;`, `}`);
|
|
121
|
+
return;
|
|
122
|
+
case "pubkey": {
|
|
123
|
+
const c = `k${n++}`;
|
|
124
|
+
lines.push(`int64_t ${c} = ts_key_cmp(&${a}, &${b});`, `if (${c} != 0) {`, ` *out = (int)${c};`, ` return SUCCESS;`, `}`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
case "struct":
|
|
128
|
+
for (const f of model.structs.get(t.name).fields)
|
|
129
|
+
walk(f.type, `${a}.${f.name}`, `${b}.${f.name}`);
|
|
130
|
+
return;
|
|
131
|
+
case "array":
|
|
132
|
+
for (let i = 0; i < t.len; i++)
|
|
133
|
+
walk(t.elem, `${a}[${i}]`, `${b}[${i}]`);
|
|
134
|
+
return;
|
|
135
|
+
default:
|
|
136
|
+
throw new Error(`unreachable: no default order for ${t.kind}`);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
walk(key, "(*a)", "(*b)");
|
|
140
|
+
lines.push(`*out = 0;`, `return SUCCESS;`);
|
|
141
|
+
return lines;
|
|
142
|
+
}
|
|
@@ -85,6 +85,8 @@ export interface Ref {
|
|
|
85
85
|
big?: string;
|
|
86
86
|
/** `bigVec.slice(start, end)`: a view of some of its elements (`len` is then a value, not the stored length). */
|
|
87
87
|
slice?: boolean;
|
|
88
|
+
/** A `rawArrayAt` view (with `big` and `slice`): `len` its run-time count, nothing stored. */
|
|
89
|
+
raw?: boolean;
|
|
88
90
|
/** Growable Vecs stored after this one move when it grows or shrinks: where its elements end, and their paths. */
|
|
89
91
|
tail?: {
|
|
90
92
|
end: string;
|
|
@@ -115,6 +117,12 @@ export interface Local {
|
|
|
115
117
|
/** For struct / data-enum references: the storage paths they alias (e.g. "book.asks[]"), and the declaration. */
|
|
116
118
|
paths?: string[];
|
|
117
119
|
decl?: ts.VariableDeclaration | ts.ParameterDeclaration;
|
|
120
|
+
/** A struct reference into an element of a list whose elements can move (a growable Vec, a heap Vec, a BigVec). */
|
|
121
|
+
movable?: boolean;
|
|
122
|
+
/** A native view (`loadRaw`, `loadRawMutAt`, `rawArrayMutAt`, `rbTreeMutAt` …): under NATIVE_ACCOUNT_WRITES two views may share bytes. */
|
|
123
|
+
view?: boolean;
|
|
124
|
+
/** For a native view: the account it reads (see `viewAccountKey`), so closeRaw / resizeAccount can end it. */
|
|
125
|
+
viewAcct?: string;
|
|
118
126
|
/** A local closure (`const f = (…) => …` reading the enclosing function's variables), expanded where it is called. */
|
|
119
127
|
closure?: Closure;
|
|
120
128
|
/**
|
|
@@ -169,6 +177,23 @@ export declare function fieldC(t: FieldType): string;
|
|
|
169
177
|
export declare function storageDecl(t: FieldType, name: string): string;
|
|
170
178
|
/** Why an account of kind `have` cannot be passed where a helper takes `want`, or undefined when it can. */
|
|
171
179
|
export declare function accountMismatch(have: ParamKind, want: ParamKind): string | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* Helper parameters held in a C struct (a Pubkey, an Option, a string view, a u256) that are passed as a const pointer
|
|
182
|
+
* instead of by value. The SBF target passes every C struct argument `byval`, as a pointer to the caller's object, and
|
|
183
|
+
* its backend makes no copy: a helper that is not inlined then reads the caller's object itself, and sees its own writes
|
|
184
|
+
* to it (`rotate(v, v.admin)` reading v.admin after `v.admin = …`). A copy written in C does not help either: clang
|
|
185
|
+
* forwards it into the byval argument, as byval promises a copy. So these are pointers, and the call site copies what
|
|
186
|
+
* the helper could write (see Calls.pointerArg); a pointer to a copy is kept, as the helper may write the original.
|
|
187
|
+
*/
|
|
188
|
+
export declare const passedByPointer: (t: ValueType) => boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Whether a C argument names memory that no helper can write: a named C object (a local, a temporary, an instruction
|
|
191
|
+
* argument, a constant), a field of a struct instruction argument (decoded into memory of its own, read-only), an
|
|
192
|
+
* account's key, the program's id, or a helper's own by-pointer parameter. `c` is the pointer for a Pubkey, the
|
|
193
|
+
* value otherwise. A helper's by-pointer parameter points at a copy, or at an object the helper cannot reach: helpers
|
|
194
|
+
* reach memory only through the accounts, remaining accounts and Inout places they are given, and pass on a subset.
|
|
195
|
+
*/
|
|
196
|
+
export declare const unreachableArg: (c: string, pubkey: boolean) => boolean;
|
|
172
197
|
/** Helpers get the instruction's parameters (for CPIs and accounts), then their own, then the result slot. */
|
|
173
198
|
export declare function helperSignature(h: Helper): string;
|
|
174
199
|
export interface EmitOptions {
|
|
@@ -26,7 +26,14 @@ export const STRING = { kind: "string" };
|
|
|
26
26
|
export const HASH = { kind: "pubkey", hash: true };
|
|
27
27
|
export const cBytes = (bytes) => `{${bytes.join(", ")}}`;
|
|
28
28
|
/** C expressions that nothing in a TypeSOL expression can change: temporaries, locals, arguments, account keys, constants. */
|
|
29
|
-
export const isStable = (c) => /^\w+$/.test(c) ||
|
|
29
|
+
export const isStable = (c) => /^\w+$/.test(c) ||
|
|
30
|
+
/^\(&\w+\)$/.test(c) ||
|
|
31
|
+
/^&\w+->key$/.test(c) ||
|
|
32
|
+
/^\(\(uint8_t\)\d+\)$/.test(c) ||
|
|
33
|
+
/^\(?-?\d+U?LL\)?$/.test(c) ||
|
|
34
|
+
// A helper's by-pointer parameter (see passedByPointer): nothing the helper does changes what it points at.
|
|
35
|
+
/^\(&?\(\*p_\w+\)\)$/.test(c) ||
|
|
36
|
+
/^\(\*p_\w+\)$/.test(c);
|
|
30
37
|
/** A C string literal of the UTF-8 bytes. Octal escapes (at most three digits) cannot swallow following characters. */
|
|
31
38
|
export const cString = (s) => `"${[...Buffer.from(s, "utf8")].map((b) => (b >= 0x20 && b < 0x7f && b !== 0x22 && b !== 0x5c && b !== 0x3f ? String.fromCharCode(b) : `\\${b.toString(8).padStart(3, "0")}`)).join("")}"`;
|
|
32
39
|
export function intLiteral(value, name) {
|
|
@@ -77,6 +84,12 @@ export function valueC(t) {
|
|
|
77
84
|
return "TsEpochRewards";
|
|
78
85
|
case "structRef":
|
|
79
86
|
return `TSS_${t.struct} *`;
|
|
87
|
+
case "rawArray":
|
|
88
|
+
return `${fieldC(t.elem)} *`;
|
|
89
|
+
case "rbTree":
|
|
90
|
+
return "TsRbt";
|
|
91
|
+
case "rbEntry":
|
|
92
|
+
return "TsRbtEntry";
|
|
80
93
|
case "enumRef":
|
|
81
94
|
case "dataEnumArg":
|
|
82
95
|
return `TSE_${t.name} *`;
|
|
@@ -197,6 +210,29 @@ export function accountMismatch(have, want) {
|
|
|
197
210
|
}
|
|
198
211
|
return ok && mutOk ? undefined : name(want);
|
|
199
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Helper parameters held in a C struct (a Pubkey, an Option, a string view, a u256) that are passed as a const pointer
|
|
215
|
+
* instead of by value. The SBF target passes every C struct argument `byval`, as a pointer to the caller's object, and
|
|
216
|
+
* its backend makes no copy: a helper that is not inlined then reads the caller's object itself, and sees its own writes
|
|
217
|
+
* to it (`rotate(v, v.admin)` reading v.admin after `v.admin = …`). A copy written in C does not help either: clang
|
|
218
|
+
* forwards it into the byval argument, as byval promises a copy. So these are pointers, and the call site copies what
|
|
219
|
+
* the helper could write (see Calls.pointerArg); a pointer to a copy is kept, as the helper may write the original.
|
|
220
|
+
*/
|
|
221
|
+
export const passedByPointer = (t) => t.kind === "pubkey" || t.kind === "option" || t.kind === "string" || (t.kind === "int" && t.name === "u256");
|
|
222
|
+
/**
|
|
223
|
+
* Whether a C argument names memory that no helper can write: a named C object (a local, a temporary, an instruction
|
|
224
|
+
* argument, a constant), a field of a struct instruction argument (decoded into memory of its own, read-only), an
|
|
225
|
+
* account's key, the program's id, or a helper's own by-pointer parameter. `c` is the pointer for a Pubkey, the
|
|
226
|
+
* value otherwise. A helper's by-pointer parameter points at a copy, or at an object the helper cannot reach: helpers
|
|
227
|
+
* reach memory only through the accounts, remaining accounts and Inout places they are given, and pass on a subset.
|
|
228
|
+
*/
|
|
229
|
+
export const unreachableArg = (c, pubkey) => pubkey
|
|
230
|
+
? /^\(&\w+\)$/.test(c) ||
|
|
231
|
+
/^\(?&\w+->key\)?$/.test(c) ||
|
|
232
|
+
/^\(&\(\*p_\w+\)\)$/.test(c) ||
|
|
233
|
+
c === "params->program_id" ||
|
|
234
|
+
/^\(&\(\*arg_\w+\)(?:\.\w+)+\)$/.test(c)
|
|
235
|
+
: /^\w+$/.test(c) || /^\(\*p_\w+\)$/.test(c) || /^\(\*arg_\w+\)(?:\.\w+)+$/.test(c);
|
|
200
236
|
/** Helpers get the instruction's parameters (for CPIs and accounts), then their own, then the result slot. */
|
|
201
237
|
export function helperSignature(h) {
|
|
202
238
|
const params = ["TsParams *params"];
|
|
@@ -209,13 +245,18 @@ export function helperSignature(h) {
|
|
|
209
245
|
if (p.account.kind === "account" && p.account.mut && p.account.struct.vec)
|
|
210
246
|
params.push(`uint64_t entry_${p.name}`);
|
|
211
247
|
}
|
|
248
|
+
else if (p.inout?.kind === "vec")
|
|
249
|
+
params.push(`${fixedVecC(p.inout)} *p_${p.name}`); // the caller's list itself (Inout<FixedVec<T, N>>)
|
|
212
250
|
else if (p.type.kind === "signerSeeds")
|
|
213
251
|
params.push(`const SolSignerSeed *p_${p.name}`, `int p_${p.name}__n`);
|
|
214
252
|
// The remaining accounts: the index of the first one among the instruction's accounts.
|
|
215
253
|
else if (p.type.kind === "remaining")
|
|
216
254
|
params.push(`uint64_t p_${p.name}`);
|
|
217
|
-
|
|
218
|
-
|
|
255
|
+
// Pubkeys, Options, strings and u256s by pointer (see passedByPointer), never as a C struct by value. `restrict`:
|
|
256
|
+
// nothing changes what it points at while the helper runs (a copy taken for the call, or memory the helper cannot
|
|
257
|
+
// write), so clang may keep what it read in registers across the helper's other writes, as it did for byval.
|
|
258
|
+
else if (passedByPointer(p.type))
|
|
259
|
+
params.push(`const ${p.type.kind === "pubkey" ? "SolPubkey" : valueC(p.type)} *restrict p_${p.name}`);
|
|
219
260
|
else if (p.type.kind === "arrayArg")
|
|
220
261
|
params.push(storageDecl(p.type.array, `(*p_${p.name})`));
|
|
221
262
|
else
|
|
@@ -223,7 +264,7 @@ export function helperSignature(h) {
|
|
|
223
264
|
}
|
|
224
265
|
if (h.returns)
|
|
225
266
|
params.push(h.returns.kind === "struct" ? `TSS_${h.returns.name} *ret` : h.returns.kind === "string" ? `TS_Str *ret` : `${scalarC(h.returns)} *ret`);
|
|
226
|
-
return
|
|
267
|
+
return `${h.noinline ? "__attribute__((noinline)) " : ""}static uint64_t fn_${h.name}(${params.join(", ")})`;
|
|
227
268
|
}
|
|
228
269
|
/** C type of a growable Vec stored after the first one of struct `s`: `{ uint32_t len; T items[]; }`. */
|
|
229
270
|
export function laterVecC(s, field) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
import { type TypeScope } from "../analyze.js";
|
|
2
3
|
import { type FieldType } from "../model.js";
|
|
3
|
-
import { type Value, type Closure, type FunctionChoice, type Ref } from "./shared.js";
|
|
4
|
+
import { type Value, type Local, type Closure, type FunctionChoice, type Ref } from "./shared.js";
|
|
4
5
|
import { CpiEmitter } from "./cpi.js";
|
|
5
6
|
/**
|
|
6
7
|
* Statements: variables, assignment, control flow and return.
|
|
@@ -18,6 +19,13 @@ export declare abstract class StatementEmitter extends CpiEmitter {
|
|
|
18
19
|
* As in JS, the length is read again before every step, so a body that pushes or pops sees the new length.
|
|
19
20
|
*/
|
|
20
21
|
protected forOf(stmt: ts.ForOfStatement): void;
|
|
22
|
+
/**
|
|
23
|
+
* `for (const e of tree)` / `for (const e of tree.reversed())` over a RedBlackTree: lib-sokoban's in-order
|
|
24
|
+
* iterator (a stack of nodes, left registers down, then the right one), smallest key first or, reversed, largest
|
|
25
|
+
* first. `e` is the node (`e.key`, `e.value`, `e.index`), each read where it lies. The body may change values
|
|
26
|
+
* but not the tree's structure (insert / remove / initialize on it are compile errors, as Rust's borrow forbids).
|
|
27
|
+
*/
|
|
28
|
+
protected treeForOf(stmt: ts.ForOfStatement, d: ts.VariableDeclaration): boolean;
|
|
21
29
|
/** The functions a list literal names (`[f, g]`: closures, helpers or choices), or undefined when it holds anything else. */
|
|
22
30
|
protected functionList(list: ts.ArrayLiteralExpression): (Closure | FunctionChoice)[] | undefined;
|
|
23
31
|
/**
|
|
@@ -117,6 +125,11 @@ export declare abstract class StatementEmitter extends CpiEmitter {
|
|
|
117
125
|
* was declared with, so a block around the call that declares the same name does not change what it reads, and
|
|
118
126
|
* writes to captured `let`s are seen by the enclosing function. Returns the result (undefined for no value).
|
|
119
127
|
*/
|
|
128
|
+
/**
|
|
129
|
+
* An `Inout<T>` parameter of a function expanded where it is called: a pointer to the caller's place (a struct,
|
|
130
|
+
* FixedArray or FixedVec), which must be writable, bound as a reference local.
|
|
131
|
+
*/
|
|
132
|
+
protected inoutBinding(p: ts.ParameterDeclaration, arg: ts.Expression, calleeScope: TypeScope, fnName: string): Local;
|
|
120
133
|
protected expandClosure(c: Closure, args: readonly ts.Expression[], node: ts.Node, into?: Ref): Value | undefined;
|
|
121
134
|
/**
|
|
122
135
|
* `xs.forEach / some / every / find / findIndex / reduce / includes / indexOf` on a Vec, FixedArray, FixedVec or Vec
|
|
@@ -141,6 +154,19 @@ export declare abstract class StatementEmitter extends CpiEmitter {
|
|
|
141
154
|
protected iterationClaims(body: ts.Statement): () => void;
|
|
142
155
|
protected body(stmt: ts.Statement): void;
|
|
143
156
|
protected variable(list: ts.VariableDeclarationList, topLevel?: boolean): void;
|
|
157
|
+
/**
|
|
158
|
+
* Whether a declaration is `/** @heap *\/`: a local list or struct kept on the heap (the bump allocator) rather
|
|
159
|
+
* than in the function's 4 KiB stack frame. Only in an instruction, which runs once per instruction: a helper may
|
|
160
|
+
* run any number of times and the heap is never freed.
|
|
161
|
+
*/
|
|
162
|
+
protected heapLocal(d: ts.VariableDeclaration, aggregate: boolean): boolean;
|
|
163
|
+
/** A `@heap` local must fit in the heap (32 KiB, or HEAP_SIZE_KIB), which other allocations share. */
|
|
164
|
+
protected checkHeapRoom(bytes: number, at: ts.Node): void;
|
|
165
|
+
/**
|
|
166
|
+
* `const [address, bump] = findProgramAddressWithBump(seeds, program)`: Rust's `let (address, bump) =
|
|
167
|
+
* Pubkey::find_program_address(…)`, both from one search (the only array destructuring the language has).
|
|
168
|
+
*/
|
|
169
|
+
protected addressAndBump(d: ts.VariableDeclaration, pattern: ts.ArrayBindingPattern, mutable: boolean): void;
|
|
144
170
|
/**
|
|
145
171
|
* `const { a, b: c } = s`: one declaration per name, `const a = s.a`, in source order. The source must be a name or
|
|
146
172
|
* a field path (no calls), so reading it once per name reads the same thing JS reads once.
|