@fluffylabs/anan-as 1.2.0 → 1.3.0-39d3435

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