@fluffylabs/anan-as 1.1.6 → 1.2.0-41e43f6

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 (103) hide show
  1. package/dist/bin/index.js +50 -49
  2. package/dist/bin/src/trace-replay.js +2 -2
  3. package/dist/build/compiler-inline.js +1 -1
  4. package/dist/build/compiler.wasm +0 -0
  5. package/dist/build/debug-inline.js +1 -1
  6. package/dist/build/debug-raw-inline.js +1 -1
  7. package/dist/build/debug-raw.d.ts +54 -105
  8. package/dist/build/debug-raw.js +66 -120
  9. package/dist/build/debug-raw.wasm +0 -0
  10. package/dist/build/debug.d.ts +54 -105
  11. package/dist/build/debug.js +70 -127
  12. package/dist/build/debug.wasm +0 -0
  13. package/dist/build/js/assembly/api-debugger.d.ts +55 -0
  14. package/dist/build/js/assembly/api-debugger.js +245 -0
  15. package/dist/build/js/assembly/api-internal.d.ts +13 -0
  16. package/dist/build/js/assembly/api-internal.js +192 -0
  17. package/dist/build/js/assembly/api-types.d.ts +46 -0
  18. package/dist/build/js/assembly/api-types.js +53 -0
  19. package/dist/build/js/assembly/api-utils.d.ts +72 -0
  20. package/dist/build/js/assembly/api-utils.js +206 -0
  21. package/dist/build/js/assembly/arguments.d.ts +44 -0
  22. package/dist/build/js/assembly/arguments.js +164 -0
  23. package/dist/build/js/assembly/codec.d.ts +24 -0
  24. package/dist/build/js/assembly/codec.js +139 -0
  25. package/dist/build/js/assembly/gas-costs.d.ts +6 -0
  26. package/dist/build/js/assembly/gas-costs.js +39 -0
  27. package/dist/build/js/assembly/gas.d.ts +11 -0
  28. package/dist/build/js/assembly/gas.js +32 -0
  29. package/dist/build/js/assembly/index-shared.d.ts +4 -0
  30. package/dist/build/js/assembly/index-shared.js +4 -0
  31. package/dist/build/js/assembly/instructions/bit.d.ts +11 -0
  32. package/dist/build/js/assembly/instructions/bit.js +53 -0
  33. package/dist/build/js/assembly/instructions/branch.d.ts +17 -0
  34. package/dist/build/js/assembly/instructions/branch.js +120 -0
  35. package/dist/build/js/assembly/instructions/jump.d.ts +5 -0
  36. package/dist/build/js/assembly/instructions/jump.js +21 -0
  37. package/dist/build/js/assembly/instructions/load.d.ts +17 -0
  38. package/dist/build/js/assembly/instructions/load.js +134 -0
  39. package/dist/build/js/assembly/instructions/logic.d.ts +10 -0
  40. package/dist/build/js/assembly/instructions/logic.js +47 -0
  41. package/dist/build/js/assembly/instructions/math.d.ts +28 -0
  42. package/dist/build/js/assembly/instructions/math.js +225 -0
  43. package/dist/build/js/assembly/instructions/misc.d.ts +6 -0
  44. package/dist/build/js/assembly/instructions/misc.js +22 -0
  45. package/dist/build/js/assembly/instructions/mov.d.ts +6 -0
  46. package/dist/build/js/assembly/instructions/mov.js +35 -0
  47. package/dist/build/js/assembly/instructions/outcome.d.ts +30 -0
  48. package/dist/build/js/assembly/instructions/outcome.js +61 -0
  49. package/dist/build/js/assembly/instructions/rot.d.ts +15 -0
  50. package/dist/build/js/assembly/instructions/rot.js +66 -0
  51. package/dist/build/js/assembly/instructions/set.d.ts +7 -0
  52. package/dist/build/js/assembly/instructions/set.js +36 -0
  53. package/dist/build/js/assembly/instructions/shift.d.ts +19 -0
  54. package/dist/build/js/assembly/instructions/shift.js +121 -0
  55. package/dist/build/js/assembly/instructions/store.d.ts +17 -0
  56. package/dist/build/js/assembly/instructions/store.js +101 -0
  57. package/dist/build/js/assembly/instructions/utils.d.ts +23 -0
  58. package/dist/build/js/assembly/instructions/utils.js +75 -0
  59. package/dist/build/js/assembly/instructions-exe.d.ts +2 -0
  60. package/dist/build/js/assembly/instructions-exe.js +245 -0
  61. package/dist/build/js/assembly/instructions.d.ts +11 -0
  62. package/dist/build/js/assembly/instructions.js +252 -0
  63. package/dist/build/js/assembly/interpreter.d.ts +29 -0
  64. package/dist/build/js/assembly/interpreter.js +228 -0
  65. package/dist/build/js/assembly/math.d.ts +8 -0
  66. package/dist/build/js/assembly/math.js +14 -0
  67. package/dist/build/js/assembly/memory-page.d.ts +36 -0
  68. package/dist/build/js/assembly/memory-page.js +74 -0
  69. package/dist/build/js/assembly/memory.d.ts +83 -0
  70. package/dist/build/js/assembly/memory.js +480 -0
  71. package/dist/build/js/assembly/portable.d.ts +24 -0
  72. package/dist/build/js/assembly/portable.js +362 -0
  73. package/dist/build/js/assembly/program-build.d.ts +2 -0
  74. package/dist/build/js/assembly/program-build.js +104 -0
  75. package/dist/build/js/assembly/program.d.ts +78 -0
  76. package/dist/build/js/assembly/program.js +290 -0
  77. package/dist/build/js/assembly/registers.d.ts +6 -0
  78. package/dist/build/js/assembly/registers.js +9 -0
  79. package/dist/build/js/assembly/spi.d.ts +92 -0
  80. package/dist/build/js/assembly/spi.js +152 -0
  81. package/dist/build/js/portable/bootstrap.d.ts +1 -0
  82. package/dist/build/js/portable/bootstrap.js +5 -0
  83. package/dist/build/js/portable/index.d.ts +4 -0
  84. package/dist/build/js/portable/index.js +6 -0
  85. package/dist/build/js/portable-bundle.js +4409 -0
  86. package/dist/build/release-inline.js +1 -1
  87. package/dist/build/release-mini-inline.js +1 -1
  88. package/dist/build/release-mini.d.ts +54 -105
  89. package/dist/build/release-mini.js +70 -127
  90. package/dist/build/release-mini.wasm +0 -0
  91. package/dist/build/release-stub-inline.js +1 -1
  92. package/dist/build/release-stub.d.ts +54 -105
  93. package/dist/build/release-stub.js +70 -127
  94. package/dist/build/release-stub.wasm +0 -0
  95. package/dist/build/release.d.ts +54 -105
  96. package/dist/build/release.js +70 -127
  97. package/dist/build/release.wasm +0 -0
  98. package/dist/build/test-inline.js +1 -1
  99. package/dist/build/test.wasm +0 -0
  100. package/dist/test/test-w3f-common.js +126 -0
  101. package/dist/test/test-w3f-portable.js +5 -0
  102. package/dist/test/test-w3f.js +3 -120
  103. package/package.json +20 -8
@@ -0,0 +1,362 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ // Runtime binding for TS decorator emit in portable JS builds.
8
+ const inline = (_target = 0, _propertyKey = 0, descriptor = 0) => descriptor;
9
+ export class portable {
10
+ // @ts-ignore: @inline is an AS-only decorator
11
+ static installPolyfills() {
12
+ if (ASC_TARGET !== 0) {
13
+ return;
14
+ }
15
+ // @ts-ignore: JS runtime only
16
+ const g = globalThis;
17
+ if (g.__ananPortablePolyfillsInstalled) {
18
+ return;
19
+ }
20
+ g.__ananPortablePolyfillsInstalled = true;
21
+ // StaticArray -> Array with AS-like zero-initialized length constructor.
22
+ g.StaticArray = function StaticArray(length) {
23
+ const arr = new Array(length);
24
+ arr.fill(0);
25
+ return arr;
26
+ };
27
+ g.ASC_TARGET = 0;
28
+ g.i8 = (value) => {
29
+ const n = typeof value === "bigint" ? Number(value) : value;
30
+ return (n << 24) >> 24;
31
+ };
32
+ g.i16 = (value) => {
33
+ const n = typeof value === "bigint" ? Number(value) : value;
34
+ return (n << 16) >> 16;
35
+ };
36
+ const i32Fn = (value) => {
37
+ if (typeof value === "bigint") {
38
+ return Number(BigInt.asIntN(32, value));
39
+ }
40
+ return value | 0;
41
+ };
42
+ i32Fn.MIN_VALUE = -2147483648;
43
+ i32Fn.MAX_VALUE = 2147483647;
44
+ g.i32 = i32Fn;
45
+ const i64Fn = (value) => {
46
+ return BigInt.asIntN(64, BigInt(value));
47
+ };
48
+ i64Fn.MAX_VALUE = BigInt("9223372036854775807");
49
+ i64Fn.MIN_VALUE = BigInt("-9223372036854775808");
50
+ g.i64 = i64Fn;
51
+ g.u8 = (v) => {
52
+ if (typeof v === "bigint") {
53
+ return Number(v & BigInt(0xff)) >>> 0;
54
+ }
55
+ return (v & 0xff) >>> 0;
56
+ };
57
+ g.u16 = (v) => {
58
+ if (typeof v === "bigint") {
59
+ return Number(v & BigInt(0xffff)) >>> 0;
60
+ }
61
+ return (v & 0xffff) >>> 0;
62
+ };
63
+ g.u32 = (v) => {
64
+ if (typeof v === "bigint") {
65
+ return Number(v & BigInt(4294967295)) >>> 0;
66
+ }
67
+ return (v & 4294967295) >>> 0;
68
+ };
69
+ const u64Fn = (value) => {
70
+ return BigInt.asUintN(64, BigInt(value));
71
+ };
72
+ u64Fn.MAX_VALUE = BigInt("18446744073709551615");
73
+ u64Fn.MIN_VALUE = BigInt(0);
74
+ g.u64 = u64Fn;
75
+ g.f32 = (v) => Math.fround(v);
76
+ g.f64 = (v) => +v;
77
+ g.bool = (v) => !!v;
78
+ const dataViewProto = DataView.prototype;
79
+ if (!dataViewProto.setUint64) {
80
+ dataViewProto.setUint64 = function (byteOffset, value, littleEndian) {
81
+ const high = Number((value >> BigInt(32)) & BigInt(4294967295));
82
+ const low = Number(value & BigInt(4294967295));
83
+ if (littleEndian) {
84
+ this.setUint32(byteOffset, low, true);
85
+ this.setUint32(byteOffset + 4, high, true);
86
+ }
87
+ else {
88
+ this.setUint32(byteOffset, high, false);
89
+ this.setUint32(byteOffset + 4, low, false);
90
+ }
91
+ };
92
+ }
93
+ if (!dataViewProto.getUint64) {
94
+ dataViewProto.getUint64 = function (byteOffset, littleEndian) {
95
+ if (littleEndian) {
96
+ const low = BigInt(this.getUint32(byteOffset, true));
97
+ const high = BigInt(this.getUint32(byteOffset + 4, true));
98
+ return (high << BigInt(32)) | low;
99
+ }
100
+ const high = BigInt(this.getUint32(byteOffset, false));
101
+ const low = BigInt(this.getUint32(byteOffset + 4, false));
102
+ return (high << BigInt(32)) | low;
103
+ };
104
+ }
105
+ g.unchecked = (v) => v;
106
+ g.inline = inline;
107
+ g.changetype = (v) => v;
108
+ }
109
+ // @ts-ignore: @inline is an AS-only decorator
110
+ // @ts-ignore: parameter type differs between AS and JS
111
+ static asArray(v) {
112
+ if (ASC_TARGET === 0) {
113
+ // @ts-ignore: JS runtime - v is an iterator, convert to array
114
+ return Array.from(v);
115
+ }
116
+ return v;
117
+ }
118
+ // @ts-ignore: @inline is an AS-only decorator
119
+ static arrayAt(v, i) {
120
+ if (ASC_TARGET === 0) {
121
+ return v[i];
122
+ }
123
+ return unchecked(v[i]);
124
+ }
125
+ // @ts-ignore: @inline is an AS-only decorator
126
+ static staticArrayAt(v, i) {
127
+ if (ASC_TARGET === 0) {
128
+ return v[i];
129
+ }
130
+ return unchecked(v[i]);
131
+ }
132
+ // @ts-ignore: @inline is an AS-only decorator
133
+ static asU32(v) {
134
+ if (ASC_TARGET === 0) {
135
+ return v >>> 0;
136
+ }
137
+ return v;
138
+ }
139
+ // @ts-ignore: @inline is an AS-only decorator
140
+ static uint8ArrayView(data, offset, length) {
141
+ if (ASC_TARGET === 0) {
142
+ // @ts-ignore: JS runtime supports Uint8Array(buffer, offset, length)
143
+ return new Uint8Array(data, offset, length);
144
+ }
145
+ // @ts-ignore: Uint8Array.wrap is an AS-only API
146
+ return Uint8Array.wrap(data, offset, length);
147
+ }
148
+ // --- bswap ---
149
+ // @ts-ignore: @inline is an AS-only decorator
150
+ static bswap_u16(v) {
151
+ if (ASC_TARGET === 0) {
152
+ return u16(((v & 0xff) << 8) | ((v >> 8) & 0xff));
153
+ }
154
+ return bswap(v);
155
+ }
156
+ // @ts-ignore: @inline is an AS-only decorator
157
+ static bswap_u32(v) {
158
+ if (ASC_TARGET === 0) {
159
+ return u32(((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v >> 8) & 0xff00) | ((v >> 24) & 0xff));
160
+ }
161
+ return bswap(v);
162
+ }
163
+ // @ts-ignore: @inline is an AS-only decorator
164
+ static bswap_u64(v) {
165
+ if (ASC_TARGET === 0) {
166
+ const lo = u32(v);
167
+ const hi = u32(v >> u64(32));
168
+ const sLo = portable.bswap_u32(lo);
169
+ const sHi = portable.bswap_u32(hi);
170
+ return u64((u64(sLo) << u64(32)) | u64(sHi));
171
+ }
172
+ return bswap(v);
173
+ }
174
+ // --- popcnt ---
175
+ // @ts-ignore: @inline is an AS-only decorator
176
+ static popcnt_u32(v) {
177
+ if (ASC_TARGET === 0) {
178
+ v = v - ((v >>> 1) & 0x55555555);
179
+ v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);
180
+ return (((v + (v >>> 4)) & 0x0f0f0f0f) * 0x01010101) >>> 24;
181
+ }
182
+ return popcnt(v);
183
+ }
184
+ // @ts-ignore: @inline is an AS-only decorator
185
+ static popcnt_u64(v) {
186
+ if (ASC_TARGET === 0) {
187
+ const lo = portable.popcnt_u32(u32(v));
188
+ const hi = portable.popcnt_u32(u32(v >> u64(32)));
189
+ return u64(lo + hi);
190
+ }
191
+ return popcnt(v);
192
+ }
193
+ // --- clz ---
194
+ // @ts-ignore: @inline is an AS-only decorator
195
+ static clz_u32(v) {
196
+ if (ASC_TARGET === 0) {
197
+ // @ts-ignore: Math.clz32 exists in ES2015+
198
+ return Math.clz32(v);
199
+ }
200
+ return clz(v);
201
+ }
202
+ // @ts-ignore: @inline is an AS-only decorator
203
+ static clz_u64(v) {
204
+ if (ASC_TARGET === 0) {
205
+ const hi = u32(v >> u64(32));
206
+ if (hi !== 0) {
207
+ return u64(portable.clz_u32(hi));
208
+ }
209
+ return u64(32 + portable.clz_u32(u32(v)));
210
+ }
211
+ return clz(v);
212
+ }
213
+ // --- ctz ---
214
+ // @ts-ignore: @inline is an AS-only decorator
215
+ static ctz_u32(v) {
216
+ if (ASC_TARGET === 0) {
217
+ if (v === 0)
218
+ return 32;
219
+ // @ts-ignore: Math.clz32 exists in ES2015+
220
+ return (31 - Math.clz32(v & -v));
221
+ }
222
+ return ctz(v);
223
+ }
224
+ // @ts-ignore: @inline is an AS-only decorator
225
+ static ctz_u64(v) {
226
+ if (ASC_TARGET === 0) {
227
+ const lo = u32(v);
228
+ if (lo !== 0) {
229
+ return u64(portable.ctz_u32(lo));
230
+ }
231
+ return u64(32 + portable.ctz_u32(u32(v >> u64(32))));
232
+ }
233
+ return ctz(v);
234
+ }
235
+ // --- rotr ---
236
+ // @ts-ignore: @inline is an AS-only decorator
237
+ static rotr_u32(v, shift) {
238
+ if (ASC_TARGET === 0) {
239
+ shift &= 31;
240
+ return u32((v >>> shift) | (v << (32 - shift)));
241
+ }
242
+ return rotr(v, shift);
243
+ }
244
+ // @ts-ignore: @inline is an AS-only decorator
245
+ static rotr_u64(v, shift) {
246
+ if (ASC_TARGET === 0) {
247
+ shift &= u64(63);
248
+ return u64((v >> shift) | (v << (u64(64) - shift)));
249
+ }
250
+ return rotr(v, shift);
251
+ }
252
+ // --- rotl ---
253
+ // @ts-ignore: @inline is an AS-only decorator
254
+ static rotl_u32(v, shift) {
255
+ if (ASC_TARGET === 0) {
256
+ shift &= 31;
257
+ return u32((v << shift) | (v >>> (32 - shift)));
258
+ }
259
+ return rotl(v, shift);
260
+ }
261
+ // @ts-ignore: @inline is an AS-only decorator
262
+ static rotl_u64(v, shift) {
263
+ if (ASC_TARGET === 0) {
264
+ shift &= u64(63);
265
+ return u64((v << shift) | (v >> (u64(64) - shift)));
266
+ }
267
+ return rotl(v, shift);
268
+ }
269
+ // --- u64 wrapping arithmetic ---
270
+ // @ts-ignore: @inline is an AS-only decorator
271
+ static u64_add(a, b) {
272
+ if (ASC_TARGET === 0) {
273
+ // @ts-ignore: BigInt
274
+ return BigInt.asUintN(64, BigInt(a) + BigInt(b));
275
+ }
276
+ return a + b;
277
+ }
278
+ // @ts-ignore: @inline is an AS-only decorator
279
+ static u64_sub(a, b) {
280
+ if (ASC_TARGET === 0) {
281
+ // @ts-ignore: BigInt
282
+ return BigInt.asUintN(64, BigInt(a) - BigInt(b));
283
+ }
284
+ return a - b;
285
+ }
286
+ // @ts-ignore: @inline is an AS-only decorator
287
+ static u64_mul(a, b) {
288
+ if (ASC_TARGET === 0) {
289
+ // @ts-ignore: BigInt
290
+ return BigInt.asUintN(64, BigInt(a) * BigInt(b));
291
+ }
292
+ return a * b;
293
+ }
294
+ }
295
+ __decorate([
296
+ inline
297
+ ], portable, "installPolyfills", null);
298
+ __decorate([
299
+ inline
300
+ // @ts-ignore: parameter type differs between AS and JS
301
+ ], portable, "asArray", null);
302
+ __decorate([
303
+ inline
304
+ ], portable, "arrayAt", null);
305
+ __decorate([
306
+ inline
307
+ ], portable, "staticArrayAt", null);
308
+ __decorate([
309
+ inline
310
+ ], portable, "asU32", null);
311
+ __decorate([
312
+ inline
313
+ ], portable, "uint8ArrayView", null);
314
+ __decorate([
315
+ inline
316
+ ], portable, "bswap_u16", null);
317
+ __decorate([
318
+ inline
319
+ ], portable, "bswap_u32", null);
320
+ __decorate([
321
+ inline
322
+ ], portable, "bswap_u64", null);
323
+ __decorate([
324
+ inline
325
+ ], portable, "popcnt_u32", null);
326
+ __decorate([
327
+ inline
328
+ ], portable, "popcnt_u64", null);
329
+ __decorate([
330
+ inline
331
+ ], portable, "clz_u32", null);
332
+ __decorate([
333
+ inline
334
+ ], portable, "clz_u64", null);
335
+ __decorate([
336
+ inline
337
+ ], portable, "ctz_u32", null);
338
+ __decorate([
339
+ inline
340
+ ], portable, "ctz_u64", null);
341
+ __decorate([
342
+ inline
343
+ ], portable, "rotr_u32", null);
344
+ __decorate([
345
+ inline
346
+ ], portable, "rotr_u64", null);
347
+ __decorate([
348
+ inline
349
+ ], portable, "rotl_u32", null);
350
+ __decorate([
351
+ inline
352
+ ], portable, "rotl_u64", null);
353
+ __decorate([
354
+ inline
355
+ ], portable, "u64_add", null);
356
+ __decorate([
357
+ inline
358
+ ], portable, "u64_sub", null);
359
+ __decorate([
360
+ inline
361
+ ], portable, "u64_mul", null);
362
+ portable.installPolyfills();
@@ -0,0 +1,2 @@
1
+ /** Turn given bytecode into a valid program. Add JumpTable and Mask. */
2
+ export declare function wrapAsProgram(bytecode: Uint8Array): Uint8Array;
@@ -0,0 +1,104 @@
1
+ import { Arguments, higNibble, lowNibble, REQUIRED_BYTES } from "./arguments";
2
+ import { encodeVarU32 } from "./codec";
3
+ import { INSTRUCTIONS, MISSING_INSTRUCTION } from "./instructions";
4
+ import { minI32 } from "./math";
5
+ /** Turn given bytecode into a valid program. Add JumpTable and Mask. */
6
+ export function wrapAsProgram(bytecode) {
7
+ const jumpTableLength = 0;
8
+ const jumpTableItemLength = 0;
9
+ const codeLength = bytecode.length;
10
+ const mask = buildMask(bytecode);
11
+ const codeLengthBytes = encodeVarU32(codeLength);
12
+ const data = new Uint8Array(1 + 1 + codeLengthBytes.length + codeLength + mask.length);
13
+ data[0] = jumpTableLength;
14
+ data[1] = jumpTableItemLength;
15
+ let offset = 2;
16
+ for (let i = 0; i < codeLengthBytes.length; i++) {
17
+ data[offset] = codeLengthBytes[i];
18
+ offset++;
19
+ }
20
+ for (let i = 0; i < bytecode.length; i++) {
21
+ data[offset] = bytecode[i];
22
+ offset++;
23
+ }
24
+ for (let i = 0; i < mask.length; i++) {
25
+ data[offset] = mask[i];
26
+ offset++;
27
+ }
28
+ return data;
29
+ }
30
+ function skipBytes(kind, data) {
31
+ switch (kind) {
32
+ case Arguments.Zero:
33
+ return 0;
34
+ case Arguments.OneImm:
35
+ return immBytes(data.length, 0);
36
+ case Arguments.TwoImm: {
37
+ const low = lowNibble(data[0]);
38
+ const split = low + 1;
39
+ return 1 + split + immBytes(data.length, split + 1);
40
+ }
41
+ case Arguments.OneOff:
42
+ return immBytes(data.length, 0);
43
+ case Arguments.OneRegOneImm:
44
+ return 1 + immBytes(data.length, 1);
45
+ case Arguments.OneRegOneExtImm:
46
+ return 9;
47
+ case Arguments.OneRegTwoImm: {
48
+ const hig = higNibble(data[0]);
49
+ const split = hig + 1;
50
+ return 1 + split + immBytes(data.length, 1 + split);
51
+ }
52
+ case Arguments.OneRegOneImmOneOff: {
53
+ const hig = higNibble(data[0]);
54
+ const split = hig + 1;
55
+ return 1 + split + immBytes(data.length, 1 + split);
56
+ }
57
+ case Arguments.TwoReg:
58
+ return 1;
59
+ case Arguments.TwoRegOneImm:
60
+ return 1 + minI32(4, data.length);
61
+ case Arguments.TwoRegOneOff:
62
+ return 1 + minI32(4, data.length);
63
+ case Arguments.TwoRegTwoImm: {
64
+ const low = lowNibble(data[1]);
65
+ const split = low + 1;
66
+ return 2 + split + immBytes(data.length, 2 + split);
67
+ }
68
+ case Arguments.ThreeReg:
69
+ return 2;
70
+ default:
71
+ throw new Error(`Unhandled arguments kind: ${kind}`);
72
+ }
73
+ }
74
+ function buildMask(bytecode) {
75
+ const mask = new StaticArray(bytecode.length);
76
+ for (let i = 0; i < bytecode.length; i++) {
77
+ const instruction = bytecode[i];
78
+ const iData = instruction < INSTRUCTIONS.length ? INSTRUCTIONS[instruction] : MISSING_INSTRUCTION;
79
+ mask[i] = true;
80
+ const requiredBytes = REQUIRED_BYTES[iData.kind];
81
+ if (i + 1 + requiredBytes <= bytecode.length) {
82
+ i += skipBytes(iData.kind, bytecode.subarray(i + 1));
83
+ }
84
+ }
85
+ // pack mask
86
+ const packed = [];
87
+ for (let i = 0; i < mask.length; i += 8) {
88
+ let byte = 0;
89
+ for (let j = i; j < i + 8; j++) {
90
+ byte >>= 1;
91
+ if (j < mask.length && mask[j]) {
92
+ byte |= 128;
93
+ }
94
+ }
95
+ packed.push(byte);
96
+ }
97
+ return packed;
98
+ }
99
+ function immBytes(dataLength, required) {
100
+ if (dataLength < required) {
101
+ return 0;
102
+ }
103
+ return minI32(4, dataLength - required);
104
+ }
@@ -0,0 +1,78 @@
1
+ import { Args, Arguments } from "./arguments";
2
+ import { Registers } from "./registers";
3
+ export type ProgramCounter = u32;
4
+ export declare class CodeAndMetadata {
5
+ readonly code: Uint8Array;
6
+ readonly metadata: Uint8Array;
7
+ constructor(code: Uint8Array, metadata: Uint8Array);
8
+ }
9
+ /** https://graypaper.fluffylabs.dev/#/cc517d7/109a01109a01?v=0.6.5 */
10
+ export declare function extractCodeAndMetadata(data: Uint8Array): CodeAndMetadata;
11
+ /** Convert `u8` to `Uint8Array` */
12
+ export declare function liftBytes(data: u8[]): Uint8Array;
13
+ /** Convert `Uint8Array` to `u8` */
14
+ export declare function lowerBytes(data: Uint8Array): u8[];
15
+ /** https://graypaper.fluffylabs.dev/#/cc517d7/234f01234f01?v=0.6.5 */
16
+ export declare function deblob(program: Uint8Array): Program;
17
+ /**
18
+ * https://graypaper.fluffylabs.dev/#/cc517d7/236e01236e01?v=0.6.5
19
+ */
20
+ export declare class Mask {
21
+ /**
22
+ * NOTE: might be longer than code (bit-alignment).
23
+ * In this array we keep `skip(n) + 1` from the Gray Paper
24
+ * for non-instruction bytes.
25
+ * In case the in-code mask says there is an instruction at that location
26
+ * we store `0` here.
27
+ */
28
+ readonly bytesToSkip: StaticArray<u32>;
29
+ constructor(packedMask: Uint8Array, codeLength: i32);
30
+ isInstruction(index: ProgramCounter): boolean;
31
+ /**
32
+ * Given we are at instruction `i`, how many bytes should be skipped to
33
+ * reach the next instruction (i.e. `skip(i) + 1` from the GP).
34
+ *
35
+ * NOTE: we don't guarantee that `isInstruction()` will return true
36
+ * for the new program counter, since `skip` function is bounded by
37
+ * an upper limit of `24` bytes.
38
+ */
39
+ skipBytesToNextInstruction(i: u32): u32;
40
+ toString(): string;
41
+ }
42
+ export declare enum BasicBlock {
43
+ NONE = 0,
44
+ START = 2,
45
+ END = 4
46
+ }
47
+ /**
48
+ * https://graypaper.fluffylabs.dev/#/cc517d7/23fe0123fe01?v=0.6.5
49
+ */
50
+ export declare class BasicBlocks {
51
+ readonly isStartOrEnd: StaticArray<BasicBlock>;
52
+ constructor(code: u8[], mask: Mask);
53
+ isStart(newPc: u32): boolean;
54
+ toString(): string;
55
+ }
56
+ export declare class JumpTable {
57
+ readonly jumps: StaticArray<u64>;
58
+ constructor(itemBytes: u8, data: Uint8Array);
59
+ toString(): string;
60
+ }
61
+ export declare class Program {
62
+ readonly code: u8[];
63
+ readonly mask: Mask;
64
+ readonly jumpTable: JumpTable;
65
+ readonly basicBlocks: BasicBlocks;
66
+ constructor(code: u8[], mask: Mask, jumpTable: JumpTable, basicBlocks: BasicBlocks);
67
+ toString(): string;
68
+ }
69
+ export declare function decodeArguments(args: Args, kind: Arguments, code: u8[], offset: i32, lim: u32): Args;
70
+ declare class ResolvedArguments {
71
+ a: i64;
72
+ b: i64;
73
+ c: i64;
74
+ d: i64;
75
+ decoded: Args;
76
+ }
77
+ export declare function resolveArguments(argsRes: Args, kind: Arguments, code: u8[], offset: u32, lim: u32, registers: Registers): ResolvedArguments | null;
78
+ export {};