jaelis-node 1.9.0 → 2.0.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/README.md +147 -433
- package/bin/jaelis-node.js +79 -504
- package/lib/index.js +31 -2740
- package/lib/node.js +271 -0
- package/lib/rpc.js +315 -0
- package/lib/storage.js +198 -0
- package/lib/sync.js +366 -0
- package/package.json +19 -53
- package/config/default.json +0 -74
- package/config/mainnet.json +0 -30
- package/config/testnet.json +0 -26
- package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
- package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
- package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
- package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
- package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
- package/lib/JAELIS-VM/lib/index.js +0 -440
- package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
- package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
- package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
- package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
- package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
- package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
- package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
- package/lib/JAELIS-VM/lib/unified/index.js +0 -456
- package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
- package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
- package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
- package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
- package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
- package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
- package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
- package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
- package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
- package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
- package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
- package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
- package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
- package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
- package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
- package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
- package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
- package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
- package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
- package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
- package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
- package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
- package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
- package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
- package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
- package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
- package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
- package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
- package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
- package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
- package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
- package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
- package/lib/JAELIS-VM/package-lock.json +0 -284
- package/lib/JAELIS-VM/package.json +0 -38
- package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
- package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
- package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
- package/lib/JAELIS-VM/test/unified.test.js +0 -166
- package/lib/JAELIS-VM/test/vm.test.js +0 -599
- package/lib/settlement-server.js +0 -999
- package/lib/vm/index.js +0 -397
|
@@ -1,1097 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JAELIS INTERMEDIATE REPRESENTATION (JIR) COMPILER
|
|
3
|
-
*
|
|
4
|
-
* Compiles source code from ANY blockchain language to JIR
|
|
5
|
-
*
|
|
6
|
-
* JIR is based on WASM 3.0 with extensions for:
|
|
7
|
-
* - Blockchain-specific opcodes (SLOAD, SSTORE, CALL, etc.)
|
|
8
|
-
* - Account model abstraction (UTXO vs Account)
|
|
9
|
-
* - Cross-VM state management
|
|
10
|
-
* - Gas/Lode metering
|
|
11
|
-
*
|
|
12
|
-
* @version 0.1.0
|
|
13
|
-
* @author Mario Papaleo - JAELIS Foundation
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const crypto = require('crypto');
|
|
17
|
-
|
|
18
|
-
// JIR Opcodes (WASM 3.0 base + blockchain extensions)
|
|
19
|
-
const JIR_OPCODES = {
|
|
20
|
-
// WASM 3.0 Core
|
|
21
|
-
NOP: 0x00,
|
|
22
|
-
BLOCK: 0x01,
|
|
23
|
-
LOOP: 0x02,
|
|
24
|
-
IF: 0x03,
|
|
25
|
-
ELSE: 0x04,
|
|
26
|
-
END: 0x05,
|
|
27
|
-
BR: 0x06,
|
|
28
|
-
BR_IF: 0x07,
|
|
29
|
-
RETURN: 0x08,
|
|
30
|
-
CALL: 0x09,
|
|
31
|
-
CALL_INDIRECT: 0x0A,
|
|
32
|
-
|
|
33
|
-
// Stack operations
|
|
34
|
-
DROP: 0x10,
|
|
35
|
-
SELECT: 0x11,
|
|
36
|
-
LOCAL_GET: 0x12,
|
|
37
|
-
LOCAL_SET: 0x13,
|
|
38
|
-
LOCAL_TEE: 0x14,
|
|
39
|
-
GLOBAL_GET: 0x15,
|
|
40
|
-
GLOBAL_SET: 0x16,
|
|
41
|
-
|
|
42
|
-
// Memory operations (64-bit addressing!)
|
|
43
|
-
I32_LOAD: 0x20,
|
|
44
|
-
I64_LOAD: 0x21,
|
|
45
|
-
F32_LOAD: 0x22,
|
|
46
|
-
F64_LOAD: 0x23,
|
|
47
|
-
I32_STORE: 0x24,
|
|
48
|
-
I64_STORE: 0x25,
|
|
49
|
-
F32_STORE: 0x26,
|
|
50
|
-
F64_STORE: 0x27,
|
|
51
|
-
MEMORY_SIZE: 0x28,
|
|
52
|
-
MEMORY_GROW: 0x29,
|
|
53
|
-
MEMORY_COPY: 0x2A, // WASM 3.0
|
|
54
|
-
MEMORY_FILL: 0x2B, // WASM 3.0
|
|
55
|
-
|
|
56
|
-
// Numeric operations
|
|
57
|
-
I32_CONST: 0x30,
|
|
58
|
-
I64_CONST: 0x31,
|
|
59
|
-
F32_CONST: 0x32,
|
|
60
|
-
F64_CONST: 0x33,
|
|
61
|
-
I32_ADD: 0x34,
|
|
62
|
-
I32_SUB: 0x35,
|
|
63
|
-
I32_MUL: 0x36,
|
|
64
|
-
I32_DIV_S: 0x37,
|
|
65
|
-
I32_DIV_U: 0x38,
|
|
66
|
-
I64_ADD: 0x39,
|
|
67
|
-
I64_SUB: 0x3A,
|
|
68
|
-
I64_MUL: 0x3B,
|
|
69
|
-
I64_DIV_S: 0x3C,
|
|
70
|
-
I64_DIV_U: 0x3D,
|
|
71
|
-
|
|
72
|
-
// Comparison operations
|
|
73
|
-
I32_EQ: 0x40,
|
|
74
|
-
I32_NE: 0x41,
|
|
75
|
-
I32_LT_S: 0x42,
|
|
76
|
-
I32_LT_U: 0x43,
|
|
77
|
-
I32_GT_S: 0x44,
|
|
78
|
-
I32_GT_U: 0x45,
|
|
79
|
-
I32_LE_S: 0x46,
|
|
80
|
-
I32_LE_U: 0x47,
|
|
81
|
-
I32_GE_S: 0x48,
|
|
82
|
-
I32_GE_U: 0x49,
|
|
83
|
-
I64_EQ: 0x4A,
|
|
84
|
-
I64_NE: 0x4B,
|
|
85
|
-
|
|
86
|
-
// Bitwise operations
|
|
87
|
-
I32_AND: 0x50,
|
|
88
|
-
I32_OR: 0x51,
|
|
89
|
-
I32_XOR: 0x52,
|
|
90
|
-
I32_SHL: 0x53,
|
|
91
|
-
I32_SHR_S: 0x54,
|
|
92
|
-
I32_SHR_U: 0x55,
|
|
93
|
-
I64_AND: 0x56,
|
|
94
|
-
I64_OR: 0x57,
|
|
95
|
-
I64_XOR: 0x58,
|
|
96
|
-
|
|
97
|
-
// WASM 3.0 GC operations
|
|
98
|
-
REF_NULL: 0x60,
|
|
99
|
-
REF_IS_NULL: 0x61,
|
|
100
|
-
REF_FUNC: 0x62,
|
|
101
|
-
STRUCT_NEW: 0x63,
|
|
102
|
-
STRUCT_GET: 0x64,
|
|
103
|
-
STRUCT_SET: 0x65,
|
|
104
|
-
ARRAY_NEW: 0x66,
|
|
105
|
-
ARRAY_GET: 0x67,
|
|
106
|
-
ARRAY_SET: 0x68,
|
|
107
|
-
ARRAY_LEN: 0x69,
|
|
108
|
-
|
|
109
|
-
// WASM 3.0 Threads
|
|
110
|
-
ATOMIC_LOAD: 0x70,
|
|
111
|
-
ATOMIC_STORE: 0x71,
|
|
112
|
-
ATOMIC_ADD: 0x72,
|
|
113
|
-
ATOMIC_SUB: 0x73,
|
|
114
|
-
ATOMIC_AND: 0x74,
|
|
115
|
-
ATOMIC_OR: 0x75,
|
|
116
|
-
ATOMIC_XOR: 0x76,
|
|
117
|
-
ATOMIC_CMPXCHG: 0x77,
|
|
118
|
-
ATOMIC_WAIT: 0x78,
|
|
119
|
-
ATOMIC_NOTIFY: 0x79,
|
|
120
|
-
|
|
121
|
-
// ═══════════════════════════════════════════════════════════════
|
|
122
|
-
// BLOCKCHAIN EXTENSIONS (0x80-0xFF)
|
|
123
|
-
// ═══════════════════════════════════════════════════════════════
|
|
124
|
-
|
|
125
|
-
// Storage operations (unified across all VMs)
|
|
126
|
-
SLOAD: 0x80, // Load from contract storage
|
|
127
|
-
SSTORE: 0x81, // Store to contract storage
|
|
128
|
-
SLOAD_EXT: 0x82, // Load from external contract
|
|
129
|
-
SSTORE_EXT: 0x83, // Store to external contract (cross-contract)
|
|
130
|
-
|
|
131
|
-
// Balance/Transfer operations
|
|
132
|
-
BALANCE: 0x84, // Get balance of address
|
|
133
|
-
TRANSFER: 0x85, // Transfer native token
|
|
134
|
-
TRANSFER_TOKEN: 0x86, // Transfer any token
|
|
135
|
-
|
|
136
|
-
// Contract calls
|
|
137
|
-
CONTRACT_CALL: 0x87, // Call another contract
|
|
138
|
-
DELEGATE_CALL: 0x88, // Delegate call (use caller's context)
|
|
139
|
-
STATIC_CALL: 0x89, // Static call (read-only)
|
|
140
|
-
CREATE: 0x8A, // Create new contract
|
|
141
|
-
CREATE2: 0x8B, // Create with deterministic address
|
|
142
|
-
SELFDESTRUCT: 0x8C, // Destroy contract
|
|
143
|
-
|
|
144
|
-
// Context operations
|
|
145
|
-
CALLER: 0x90, // msg.sender equivalent
|
|
146
|
-
ORIGIN: 0x91, // tx.origin equivalent
|
|
147
|
-
CALLVALUE: 0x92, // msg.value equivalent
|
|
148
|
-
CALLDATA: 0x93, // Get calldata
|
|
149
|
-
CALLDATASIZE: 0x94, // Get calldata size
|
|
150
|
-
RETURNDATASIZE: 0x95, // Get return data size
|
|
151
|
-
RETURNDATACOPY: 0x96, // Copy return data
|
|
152
|
-
|
|
153
|
-
// Block context
|
|
154
|
-
BLOCKHASH: 0xA0,
|
|
155
|
-
COINBASE: 0xA1,
|
|
156
|
-
TIMESTAMP: 0xA2,
|
|
157
|
-
BLOCKNUMBER: 0xA3,
|
|
158
|
-
DIFFICULTY: 0xA4,
|
|
159
|
-
GASLIMIT: 0xA5,
|
|
160
|
-
CHAINID: 0xA6,
|
|
161
|
-
SELFBALANCE: 0xA7,
|
|
162
|
-
BASEFEE: 0xA8,
|
|
163
|
-
|
|
164
|
-
// Crypto operations
|
|
165
|
-
SHA256: 0xB0,
|
|
166
|
-
KECCAK256: 0xB1,
|
|
167
|
-
BLAKE2B: 0xB2,
|
|
168
|
-
BLAKE3: 0xB3,
|
|
169
|
-
POSEIDON: 0xB4, // ZK-friendly hash
|
|
170
|
-
ECRECOVER: 0xB5,
|
|
171
|
-
ECADD: 0xB6,
|
|
172
|
-
ECMUL: 0xB7,
|
|
173
|
-
ECPAIRING: 0xB8,
|
|
174
|
-
ED25519_VERIFY: 0xB9, // For Solana/Sui
|
|
175
|
-
|
|
176
|
-
// Logging
|
|
177
|
-
LOG0: 0xC0,
|
|
178
|
-
LOG1: 0xC1,
|
|
179
|
-
LOG2: 0xC2,
|
|
180
|
-
LOG3: 0xC3,
|
|
181
|
-
LOG4: 0xC4,
|
|
182
|
-
|
|
183
|
-
// Cross-VM operations (JAELIS specific!)
|
|
184
|
-
CROSS_VM_CALL: 0xD0, // Call contract on different VM
|
|
185
|
-
CROSS_VM_TRANSFER: 0xD1, // Transfer to different VM
|
|
186
|
-
CROSS_VM_STATE: 0xD2, // Read state from different VM
|
|
187
|
-
BRIDGE_LOCK: 0xD3, // Lock assets for bridging
|
|
188
|
-
BRIDGE_UNLOCK: 0xD4, // Unlock bridged assets
|
|
189
|
-
BRIDGE_MINT: 0xD5, // Mint bridged representation
|
|
190
|
-
BRIDGE_BURN: 0xD6, // Burn bridged representation
|
|
191
|
-
|
|
192
|
-
// Gas/Lode metering
|
|
193
|
-
GAS: 0xE0, // Get remaining gas
|
|
194
|
-
LODE_SIGNAL: 0xE1, // JAELIS dynamic lode signal
|
|
195
|
-
METER_START: 0xE2, // Start metering block
|
|
196
|
-
METER_END: 0xE3, // End metering block
|
|
197
|
-
|
|
198
|
-
// Debugging (removed in production)
|
|
199
|
-
DEBUG_PRINT: 0xF0,
|
|
200
|
-
DEBUG_BREAK: 0xF1,
|
|
201
|
-
DEBUG_TRACE: 0xF2,
|
|
202
|
-
|
|
203
|
-
// Halt
|
|
204
|
-
REVERT: 0xFE,
|
|
205
|
-
HALT: 0xFF
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
// Type definitions for JIR
|
|
209
|
-
const JIR_TYPES = {
|
|
210
|
-
VOID: 0x00,
|
|
211
|
-
I32: 0x01,
|
|
212
|
-
I64: 0x02,
|
|
213
|
-
F32: 0x03,
|
|
214
|
-
F64: 0x04,
|
|
215
|
-
V128: 0x05, // SIMD
|
|
216
|
-
FUNCREF: 0x06,
|
|
217
|
-
EXTERNREF: 0x07,
|
|
218
|
-
|
|
219
|
-
// Blockchain types
|
|
220
|
-
ADDRESS: 0x10, // 20-byte address (EVM) or 32-byte (Solana)
|
|
221
|
-
UINT256: 0x11, // 256-bit unsigned int
|
|
222
|
-
INT256: 0x12, // 256-bit signed int
|
|
223
|
-
BYTES32: 0x13, // 32-byte fixed
|
|
224
|
-
BYTES: 0x14, // Dynamic bytes
|
|
225
|
-
STRING: 0x15, // UTF-8 string
|
|
226
|
-
BOOL: 0x16,
|
|
227
|
-
|
|
228
|
-
// Complex types
|
|
229
|
-
ARRAY: 0x20,
|
|
230
|
-
STRUCT: 0x21,
|
|
231
|
-
MAP: 0x22,
|
|
232
|
-
TUPLE: 0x23
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* JIR Compiler
|
|
237
|
-
*
|
|
238
|
-
* Compiles source code from any VM to JAELIS Intermediate Representation
|
|
239
|
-
*/
|
|
240
|
-
class JIRCompiler {
|
|
241
|
-
constructor(config = {}) {
|
|
242
|
-
this.config = config;
|
|
243
|
-
this.opcodes = JIR_OPCODES;
|
|
244
|
-
this.types = JIR_TYPES;
|
|
245
|
-
|
|
246
|
-
// Parser cache for performance
|
|
247
|
-
this.parserCache = new Map();
|
|
248
|
-
|
|
249
|
-
// Optimization passes
|
|
250
|
-
this.optimizationPasses = [
|
|
251
|
-
'constant-folding',
|
|
252
|
-
'dead-code-elimination',
|
|
253
|
-
'common-subexpression',
|
|
254
|
-
'loop-invariant-motion',
|
|
255
|
-
'inline-small-functions',
|
|
256
|
-
'peephole'
|
|
257
|
-
];
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Parse source code to AST
|
|
262
|
-
*
|
|
263
|
-
* @param {string} sourceCode - Source code
|
|
264
|
-
* @param {string} sourceVM - Source VM type
|
|
265
|
-
* @returns {object} Abstract Syntax Tree
|
|
266
|
-
*/
|
|
267
|
-
async parse(sourceCode, sourceVM) {
|
|
268
|
-
console.log(`[JIR] Parsing ${sourceVM.toUpperCase()} source code...`);
|
|
269
|
-
|
|
270
|
-
switch (sourceVM) {
|
|
271
|
-
case 'evm':
|
|
272
|
-
return this._parseEVM(sourceCode);
|
|
273
|
-
case 'svm':
|
|
274
|
-
return this._parseSVM(sourceCode);
|
|
275
|
-
case 'move':
|
|
276
|
-
return this._parseMove(sourceCode);
|
|
277
|
-
case 'tvm':
|
|
278
|
-
return this._parseTVM(sourceCode);
|
|
279
|
-
case 'wasm':
|
|
280
|
-
return this._parseWASM(sourceCode);
|
|
281
|
-
case 'cairo':
|
|
282
|
-
return this._parseCairo(sourceCode);
|
|
283
|
-
case 'bitcoin':
|
|
284
|
-
return this._parseBitcoinScript(sourceCode);
|
|
285
|
-
default:
|
|
286
|
-
throw new Error(`Unsupported source VM: ${sourceVM}`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Semantic analysis on AST
|
|
292
|
-
*/
|
|
293
|
-
async analyze(ast, sourceVM) {
|
|
294
|
-
console.log(`[JIR] Performing semantic analysis...`);
|
|
295
|
-
|
|
296
|
-
const analysis = {
|
|
297
|
-
functions: [],
|
|
298
|
-
storage: [],
|
|
299
|
-
imports: [],
|
|
300
|
-
exports: [],
|
|
301
|
-
types: [],
|
|
302
|
-
errors: [],
|
|
303
|
-
warnings: []
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
// Type checking
|
|
307
|
-
this._typeCheck(ast, analysis);
|
|
308
|
-
|
|
309
|
-
// Storage layout analysis
|
|
310
|
-
this._analyzeStorage(ast, analysis, sourceVM);
|
|
311
|
-
|
|
312
|
-
// Control flow analysis
|
|
313
|
-
this._analyzeControlFlow(ast, analysis);
|
|
314
|
-
|
|
315
|
-
// Security checks
|
|
316
|
-
this._securityAnalysis(ast, analysis);
|
|
317
|
-
|
|
318
|
-
return {
|
|
319
|
-
ast,
|
|
320
|
-
...analysis
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Generate JIR from analyzed AST
|
|
326
|
-
*/
|
|
327
|
-
async generateJIR(analyzed, sourceVM, options = {}) {
|
|
328
|
-
console.log(`[JIR] Generating JAELIS Intermediate Representation...`);
|
|
329
|
-
|
|
330
|
-
const jir = {
|
|
331
|
-
magic: Buffer.from([0x4A, 0x49, 0x52, 0x00]), // 'JIR\0'
|
|
332
|
-
version: [0, 1, 0], // v0.1.0
|
|
333
|
-
sourceVM,
|
|
334
|
-
|
|
335
|
-
// Sections (similar to WASM but extended)
|
|
336
|
-
types: [],
|
|
337
|
-
imports: [],
|
|
338
|
-
functions: [],
|
|
339
|
-
tables: [],
|
|
340
|
-
memory: {
|
|
341
|
-
initial: 1, // 64KB pages
|
|
342
|
-
maximum: 256, // 16MB max
|
|
343
|
-
shared: false
|
|
344
|
-
},
|
|
345
|
-
globals: [],
|
|
346
|
-
exports: [],
|
|
347
|
-
start: null,
|
|
348
|
-
elements: [],
|
|
349
|
-
code: [],
|
|
350
|
-
data: [],
|
|
351
|
-
|
|
352
|
-
// Blockchain extensions
|
|
353
|
-
storage: [],
|
|
354
|
-
events: [],
|
|
355
|
-
|
|
356
|
-
// Metadata
|
|
357
|
-
metadata: {
|
|
358
|
-
name: options.name || 'contract',
|
|
359
|
-
compiler: 'jaelis-jir-compiler',
|
|
360
|
-
compilerVersion: '0.1.0',
|
|
361
|
-
sourceHash: this._hashSource(analyzed.ast),
|
|
362
|
-
features: []
|
|
363
|
-
},
|
|
364
|
-
|
|
365
|
-
// Compiled bytecode
|
|
366
|
-
bytecode: null
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
// Generate type section
|
|
370
|
-
jir.types = this._generateTypes(analyzed);
|
|
371
|
-
|
|
372
|
-
// Generate function section
|
|
373
|
-
for (const func of analyzed.functions) {
|
|
374
|
-
jir.functions.push(this._compileFunction(func, sourceVM));
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// Generate storage layout
|
|
378
|
-
jir.storage = this._generateStorageLayout(analyzed.storage, sourceVM);
|
|
379
|
-
|
|
380
|
-
// Generate event signatures
|
|
381
|
-
jir.events = this._generateEvents(analyzed.ast);
|
|
382
|
-
|
|
383
|
-
// Generate exports
|
|
384
|
-
jir.exports = analyzed.exports.map(exp => ({
|
|
385
|
-
name: exp.name,
|
|
386
|
-
kind: 'function',
|
|
387
|
-
index: jir.functions.findIndex(f => f.name === exp.name)
|
|
388
|
-
}));
|
|
389
|
-
|
|
390
|
-
// Compile to bytecode
|
|
391
|
-
jir.bytecode = this._assembleToBytecode(jir);
|
|
392
|
-
|
|
393
|
-
return jir;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Optimize JIR
|
|
398
|
-
*/
|
|
399
|
-
async optimize(jir, level = 2) {
|
|
400
|
-
console.log(`[JIR] Optimizing at level ${level}...`);
|
|
401
|
-
|
|
402
|
-
let optimized = { ...jir };
|
|
403
|
-
|
|
404
|
-
if (level >= 1) {
|
|
405
|
-
optimized = this._constantFolding(optimized);
|
|
406
|
-
optimized = this._deadCodeElimination(optimized);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
if (level >= 2) {
|
|
410
|
-
optimized = this._commonSubexpressionElimination(optimized);
|
|
411
|
-
optimized = this._loopInvariantMotion(optimized);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (level >= 3) {
|
|
415
|
-
optimized = this._inlineSmallFunctions(optimized);
|
|
416
|
-
optimized = this._peepholeOptimization(optimized);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// Re-assemble bytecode after optimization
|
|
420
|
-
optimized.bytecode = this._assembleToBytecode(optimized);
|
|
421
|
-
|
|
422
|
-
const reduction = ((jir.bytecode.length - optimized.bytecode.length) / jir.bytecode.length * 100).toFixed(1);
|
|
423
|
-
console.log(`[JIR] Optimization reduced size by ${reduction}%`);
|
|
424
|
-
|
|
425
|
-
return optimized;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Validate JIR module
|
|
430
|
-
*/
|
|
431
|
-
async validate(jir) {
|
|
432
|
-
const errors = [];
|
|
433
|
-
const warnings = [];
|
|
434
|
-
|
|
435
|
-
// Magic number check
|
|
436
|
-
if (!jir.magic || !jir.magic.equals(Buffer.from([0x4A, 0x49, 0x52, 0x00]))) {
|
|
437
|
-
errors.push('Invalid magic number');
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// Version check
|
|
441
|
-
if (!jir.version || jir.version[0] > 1) {
|
|
442
|
-
errors.push('Unsupported JIR version');
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// Type validation
|
|
446
|
-
for (let i = 0; i < jir.types.length; i++) {
|
|
447
|
-
if (!this._validateType(jir.types[i])) {
|
|
448
|
-
errors.push(`Invalid type at index ${i}`);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
// Function validation
|
|
453
|
-
for (let i = 0; i < jir.functions.length; i++) {
|
|
454
|
-
const funcErrors = this._validateFunction(jir.functions[i], jir);
|
|
455
|
-
errors.push(...funcErrors.map(e => `Function ${i}: ${e}`));
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// Bytecode validation
|
|
459
|
-
if (jir.bytecode) {
|
|
460
|
-
const bytecodeErrors = this._validateBytecode(jir.bytecode);
|
|
461
|
-
errors.push(...bytecodeErrors);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
return {
|
|
465
|
-
valid: errors.length === 0,
|
|
466
|
-
errors,
|
|
467
|
-
warnings
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// ═══════════════════════════════════════════════════════════════
|
|
472
|
-
// VM-SPECIFIC PARSERS
|
|
473
|
-
// ═══════════════════════════════════════════════════════════════
|
|
474
|
-
|
|
475
|
-
_parseEVM(sourceCode) {
|
|
476
|
-
// Parse Solidity or Yul
|
|
477
|
-
const ast = {
|
|
478
|
-
type: 'Program',
|
|
479
|
-
sourceType: 'solidity',
|
|
480
|
-
body: [],
|
|
481
|
-
contracts: []
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
// Simple tokenizer for demonstration
|
|
485
|
-
const tokens = this._tokenize(sourceCode);
|
|
486
|
-
|
|
487
|
-
// Parse contracts
|
|
488
|
-
let i = 0;
|
|
489
|
-
while (i < tokens.length) {
|
|
490
|
-
if (tokens[i] === 'contract' || tokens[i] === 'library' || tokens[i] === 'interface') {
|
|
491
|
-
const contract = this._parseContract(tokens, i);
|
|
492
|
-
ast.contracts.push(contract.node);
|
|
493
|
-
i = contract.index;
|
|
494
|
-
} else if (tokens[i] === 'pragma') {
|
|
495
|
-
// Skip pragma
|
|
496
|
-
while (tokens[i] !== ';' && i < tokens.length) i++;
|
|
497
|
-
i++;
|
|
498
|
-
} else {
|
|
499
|
-
i++;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
return ast;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
_parseSVM(sourceCode) {
|
|
507
|
-
// Parse Rust/Anchor for Solana
|
|
508
|
-
const ast = {
|
|
509
|
-
type: 'Program',
|
|
510
|
-
sourceType: 'anchor',
|
|
511
|
-
body: [],
|
|
512
|
-
programs: []
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
// Parse #[program] modules
|
|
516
|
-
const programMatch = sourceCode.match(/#\[program\]\s*pub\s+mod\s+(\w+)/g);
|
|
517
|
-
if (programMatch) {
|
|
518
|
-
for (const match of programMatch) {
|
|
519
|
-
const name = match.match(/mod\s+(\w+)/)[1];
|
|
520
|
-
ast.programs.push({
|
|
521
|
-
type: 'AnchorProgram',
|
|
522
|
-
name,
|
|
523
|
-
instructions: this._parseAnchorInstructions(sourceCode, name)
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return ast;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
_parseMove(sourceCode) {
|
|
532
|
-
// Parse Move language (Aptos/Sui)
|
|
533
|
-
const ast = {
|
|
534
|
-
type: 'Program',
|
|
535
|
-
sourceType: 'move',
|
|
536
|
-
body: [],
|
|
537
|
-
modules: []
|
|
538
|
-
};
|
|
539
|
-
|
|
540
|
-
// Parse module declarations
|
|
541
|
-
const moduleMatch = sourceCode.match(/module\s+[\w:]+\s*\{/g);
|
|
542
|
-
if (moduleMatch) {
|
|
543
|
-
// Parse module contents
|
|
544
|
-
for (const match of moduleMatch) {
|
|
545
|
-
const moduleName = match.match(/module\s+([\w:]+)/)[1];
|
|
546
|
-
ast.modules.push({
|
|
547
|
-
type: 'MoveModule',
|
|
548
|
-
name: moduleName,
|
|
549
|
-
functions: this._parseMoveFunctions(sourceCode),
|
|
550
|
-
structs: this._parseMoveStructs(sourceCode)
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
return ast;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
_parseTVM(sourceCode) {
|
|
559
|
-
// Parse FunC for TON
|
|
560
|
-
const ast = {
|
|
561
|
-
type: 'Program',
|
|
562
|
-
sourceType: 'func',
|
|
563
|
-
body: [],
|
|
564
|
-
functions: []
|
|
565
|
-
};
|
|
566
|
-
|
|
567
|
-
// Parse function declarations - multiple patterns for FunC
|
|
568
|
-
// Pattern 1: () name(params) impure { }
|
|
569
|
-
// Pattern 2: int name() method_id { }
|
|
570
|
-
// Pattern 3: (int, int) name(slice) { }
|
|
571
|
-
const patterns = [
|
|
572
|
-
/\(\)\s+(\w+)\s*\([^)]*\)\s*(?:impure\s*)?\{/g, // () recv_internal(...)
|
|
573
|
-
/(\w+)\s+(\w+)\s*\(\s*\)\s*(?:method_id\s*)?\{/g, // int get_balance() method_id
|
|
574
|
-
/\([^)]*\)\s+(\w+)\s*\([^)]*\)\s*\{/g // (int, int) func_name(...)
|
|
575
|
-
];
|
|
576
|
-
|
|
577
|
-
const foundFunctions = new Set();
|
|
578
|
-
|
|
579
|
-
// Pattern 1: () name(params) impure { }
|
|
580
|
-
const match1 = sourceCode.matchAll(/\(\)\s+(\w+)\s*\([^)]*\)\s*(?:impure\s*)?\{/g);
|
|
581
|
-
for (const m of match1) {
|
|
582
|
-
if (!foundFunctions.has(m[1])) {
|
|
583
|
-
foundFunctions.add(m[1]);
|
|
584
|
-
ast.functions.push({
|
|
585
|
-
type: 'FunCFunction',
|
|
586
|
-
name: m[1],
|
|
587
|
-
signature: m[0],
|
|
588
|
-
returns: 'void'
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// Pattern 2: int name() method_id { }
|
|
594
|
-
const match2 = sourceCode.matchAll(/(\w+)\s+(\w+)\s*\([^)]*\)\s*(?:method_id\s*)?\{/g);
|
|
595
|
-
for (const m of match2) {
|
|
596
|
-
const returnType = m[1];
|
|
597
|
-
const funcName = m[2];
|
|
598
|
-
// Exclude keywords
|
|
599
|
-
if (!['if', 'while', 'for', 'return', 'impure'].includes(returnType) && !foundFunctions.has(funcName)) {
|
|
600
|
-
foundFunctions.add(funcName);
|
|
601
|
-
ast.functions.push({
|
|
602
|
-
type: 'FunCFunction',
|
|
603
|
-
name: funcName,
|
|
604
|
-
signature: m[0],
|
|
605
|
-
returns: returnType
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
return ast;
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
_parseWASM(sourceCode) {
|
|
614
|
-
// Parse WAT (WebAssembly Text) or WASM binary
|
|
615
|
-
const ast = {
|
|
616
|
-
type: 'Program',
|
|
617
|
-
sourceType: 'wasm',
|
|
618
|
-
body: []
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
if (Buffer.isBuffer(sourceCode) || sourceCode[0] === 0x00) {
|
|
622
|
-
// Binary WASM
|
|
623
|
-
return this._parseWASMBinary(sourceCode);
|
|
624
|
-
} else {
|
|
625
|
-
// WAT text format
|
|
626
|
-
return this._parseWAT(sourceCode);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
_parseCairo(sourceCode) {
|
|
631
|
-
// Parse Cairo for Starknet
|
|
632
|
-
const ast = {
|
|
633
|
-
type: 'Program',
|
|
634
|
-
sourceType: 'cairo',
|
|
635
|
-
body: [],
|
|
636
|
-
contracts: []
|
|
637
|
-
};
|
|
638
|
-
|
|
639
|
-
// Parse #[contract] modules
|
|
640
|
-
const contractMatch = sourceCode.match(/#\[contract\]\s*mod\s+(\w+)/g);
|
|
641
|
-
if (contractMatch) {
|
|
642
|
-
for (const match of contractMatch) {
|
|
643
|
-
const name = match.match(/mod\s+(\w+)/)[1];
|
|
644
|
-
const contract = {
|
|
645
|
-
type: 'CairoContract',
|
|
646
|
-
name,
|
|
647
|
-
functions: []
|
|
648
|
-
};
|
|
649
|
-
|
|
650
|
-
// Parse functions: #[external] fn name(...) or #[view] fn name(...)
|
|
651
|
-
const funcMatches = sourceCode.matchAll(/#\[(external|view)\]\s*fn\s+(\w+)/g);
|
|
652
|
-
for (const funcMatch of funcMatches) {
|
|
653
|
-
contract.functions.push({
|
|
654
|
-
type: 'CairoFunction',
|
|
655
|
-
name: funcMatch[2],
|
|
656
|
-
visibility: funcMatch[1]
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
// Also parse regular fn declarations
|
|
661
|
-
const plainFuncMatches = sourceCode.matchAll(/fn\s+(\w+)\s*\([^)]*\)\s*(?:->|{)/g);
|
|
662
|
-
const seenFuncs = new Set(contract.functions.map(f => f.name));
|
|
663
|
-
for (const funcMatch of plainFuncMatches) {
|
|
664
|
-
if (!seenFuncs.has(funcMatch[1])) {
|
|
665
|
-
contract.functions.push({
|
|
666
|
-
type: 'CairoFunction',
|
|
667
|
-
name: funcMatch[1],
|
|
668
|
-
visibility: 'public'
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
ast.contracts.push(contract);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
return ast;
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
_parseBitcoinScript(sourceCode) {
|
|
681
|
-
// Parse Bitcoin Script
|
|
682
|
-
const ast = {
|
|
683
|
-
type: 'Program',
|
|
684
|
-
sourceType: 'bitcoin',
|
|
685
|
-
body: [],
|
|
686
|
-
scripts: []
|
|
687
|
-
};
|
|
688
|
-
|
|
689
|
-
// Parse opcodes
|
|
690
|
-
const opcodes = sourceCode.split(/\s+/).filter(Boolean);
|
|
691
|
-
ast.scripts.push({
|
|
692
|
-
type: 'BitcoinScript',
|
|
693
|
-
opcodes
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
return ast;
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
// ═══════════════════════════════════════════════════════════════
|
|
700
|
-
// HELPER METHODS
|
|
701
|
-
// ═══════════════════════════════════════════════════════════════
|
|
702
|
-
|
|
703
|
-
_tokenize(code) {
|
|
704
|
-
// Simple tokenizer
|
|
705
|
-
const tokens = [];
|
|
706
|
-
const pattern = /(\b\w+\b|[{}()\[\];,.:=+\-*/<>!&|^~%]|"[^"]*"|'[^']*')/g;
|
|
707
|
-
let match;
|
|
708
|
-
while ((match = pattern.exec(code)) !== null) {
|
|
709
|
-
tokens.push(match[1]);
|
|
710
|
-
}
|
|
711
|
-
return tokens;
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
_parseContract(tokens, startIndex) {
|
|
715
|
-
const contract = {
|
|
716
|
-
type: tokens[startIndex] === 'contract' ? 'ContractDeclaration' :
|
|
717
|
-
tokens[startIndex] === 'library' ? 'LibraryDeclaration' : 'InterfaceDeclaration',
|
|
718
|
-
name: tokens[startIndex + 1],
|
|
719
|
-
inheritance: [],
|
|
720
|
-
body: []
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
let i = startIndex + 2;
|
|
724
|
-
|
|
725
|
-
// Parse inheritance
|
|
726
|
-
if (tokens[i] === 'is') {
|
|
727
|
-
i++;
|
|
728
|
-
while (tokens[i] !== '{') {
|
|
729
|
-
if (tokens[i] !== ',') {
|
|
730
|
-
contract.inheritance.push(tokens[i]);
|
|
731
|
-
}
|
|
732
|
-
i++;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
// Skip to body
|
|
737
|
-
while (tokens[i] !== '{' && i < tokens.length) i++;
|
|
738
|
-
i++; // Skip opening brace
|
|
739
|
-
|
|
740
|
-
// Parse contract body
|
|
741
|
-
let braceCount = 1;
|
|
742
|
-
while (braceCount > 0 && i < tokens.length) {
|
|
743
|
-
if (tokens[i] === '{') braceCount++;
|
|
744
|
-
if (tokens[i] === '}') braceCount--;
|
|
745
|
-
|
|
746
|
-
if (tokens[i] === 'function') {
|
|
747
|
-
const func = this._parseSolidityFunction(tokens, i);
|
|
748
|
-
contract.body.push(func.node);
|
|
749
|
-
i = func.index;
|
|
750
|
-
} else if (tokens[i] === 'mapping' || tokens[i] === 'uint256' || tokens[i] === 'address') {
|
|
751
|
-
// State variable
|
|
752
|
-
const variable = this._parseStateVariable(tokens, i);
|
|
753
|
-
contract.body.push(variable.node);
|
|
754
|
-
i = variable.index;
|
|
755
|
-
} else {
|
|
756
|
-
i++;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
return { node: contract, index: i };
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
_parseSolidityFunction(tokens, startIndex) {
|
|
764
|
-
const func = {
|
|
765
|
-
type: 'FunctionDeclaration',
|
|
766
|
-
name: tokens[startIndex + 1],
|
|
767
|
-
params: [],
|
|
768
|
-
visibility: 'public',
|
|
769
|
-
stateMutability: null,
|
|
770
|
-
returns: [],
|
|
771
|
-
body: []
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
let i = startIndex + 2;
|
|
775
|
-
|
|
776
|
-
// Parse parameters
|
|
777
|
-
if (tokens[i] === '(') {
|
|
778
|
-
i++;
|
|
779
|
-
while (tokens[i] !== ')' && i < tokens.length) {
|
|
780
|
-
if (tokens[i] !== ',' && tokens[i] !== '(') {
|
|
781
|
-
// Parse param type and name
|
|
782
|
-
const paramType = tokens[i];
|
|
783
|
-
const paramName = tokens[i + 1];
|
|
784
|
-
if (paramName && paramName !== ')' && paramName !== ',') {
|
|
785
|
-
func.params.push({ type: paramType, name: paramName });
|
|
786
|
-
i += 2;
|
|
787
|
-
} else {
|
|
788
|
-
i++;
|
|
789
|
-
}
|
|
790
|
-
} else {
|
|
791
|
-
i++;
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
i++; // Skip closing paren
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
// Parse modifiers
|
|
798
|
-
while (tokens[i] !== '{' && tokens[i] !== ';' && i < tokens.length) {
|
|
799
|
-
if (['public', 'private', 'internal', 'external'].includes(tokens[i])) {
|
|
800
|
-
func.visibility = tokens[i];
|
|
801
|
-
} else if (['view', 'pure', 'payable'].includes(tokens[i])) {
|
|
802
|
-
func.stateMutability = tokens[i];
|
|
803
|
-
} else if (tokens[i] === 'returns') {
|
|
804
|
-
i++; // Skip 'returns'
|
|
805
|
-
if (tokens[i] === '(') {
|
|
806
|
-
i++;
|
|
807
|
-
while (tokens[i] !== ')' && i < tokens.length) {
|
|
808
|
-
if (tokens[i] !== ',' && tokens[i] !== '(') {
|
|
809
|
-
func.returns.push(tokens[i]);
|
|
810
|
-
}
|
|
811
|
-
i++;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
i++;
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// Skip function body for now
|
|
819
|
-
if (tokens[i] === '{') {
|
|
820
|
-
let braceCount = 1;
|
|
821
|
-
i++;
|
|
822
|
-
while (braceCount > 0 && i < tokens.length) {
|
|
823
|
-
if (tokens[i] === '{') braceCount++;
|
|
824
|
-
if (tokens[i] === '}') braceCount--;
|
|
825
|
-
i++;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
return { node: func, index: i };
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
_parseStateVariable(tokens, startIndex) {
|
|
833
|
-
const variable = {
|
|
834
|
-
type: 'StateVariableDeclaration',
|
|
835
|
-
dataType: tokens[startIndex],
|
|
836
|
-
name: null,
|
|
837
|
-
visibility: 'internal'
|
|
838
|
-
};
|
|
839
|
-
|
|
840
|
-
let i = startIndex + 1;
|
|
841
|
-
|
|
842
|
-
// Handle mapping
|
|
843
|
-
if (tokens[startIndex] === 'mapping') {
|
|
844
|
-
// Skip mapping type definition
|
|
845
|
-
let parenCount = 0;
|
|
846
|
-
while (i < tokens.length) {
|
|
847
|
-
if (tokens[i] === '(') parenCount++;
|
|
848
|
-
if (tokens[i] === ')') {
|
|
849
|
-
parenCount--;
|
|
850
|
-
if (parenCount === 0) {
|
|
851
|
-
i++;
|
|
852
|
-
break;
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
i++;
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
// Parse visibility and name
|
|
860
|
-
while (tokens[i] !== ';' && i < tokens.length) {
|
|
861
|
-
if (['public', 'private', 'internal'].includes(tokens[i])) {
|
|
862
|
-
variable.visibility = tokens[i];
|
|
863
|
-
} else if (tokens[i] !== '=' && !['public', 'private', 'internal', 'constant', 'immutable'].includes(tokens[i])) {
|
|
864
|
-
variable.name = tokens[i];
|
|
865
|
-
}
|
|
866
|
-
i++;
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
return { node: variable, index: i + 1 };
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
_parseAnchorInstructions(sourceCode, programName) {
|
|
873
|
-
const instructions = [];
|
|
874
|
-
const funcPattern = /pub\s+fn\s+(\w+)\s*\(/g;
|
|
875
|
-
let match;
|
|
876
|
-
while ((match = funcPattern.exec(sourceCode)) !== null) {
|
|
877
|
-
instructions.push({
|
|
878
|
-
type: 'AnchorInstruction',
|
|
879
|
-
name: match[1]
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
return instructions;
|
|
883
|
-
}
|
|
884
|
-
|
|
885
|
-
_parseMoveFunctions(sourceCode) {
|
|
886
|
-
const functions = [];
|
|
887
|
-
const funcPattern = /public\s+fun\s+(\w+)|fun\s+(\w+)/g;
|
|
888
|
-
let match;
|
|
889
|
-
while ((match = funcPattern.exec(sourceCode)) !== null) {
|
|
890
|
-
functions.push({
|
|
891
|
-
type: 'MoveFunction',
|
|
892
|
-
name: match[1] || match[2],
|
|
893
|
-
visibility: match[1] ? 'public' : 'private'
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
return functions;
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
_parseMoveStructs(sourceCode) {
|
|
900
|
-
const structs = [];
|
|
901
|
-
const structPattern = /struct\s+(\w+)/g;
|
|
902
|
-
let match;
|
|
903
|
-
while ((match = structPattern.exec(sourceCode)) !== null) {
|
|
904
|
-
structs.push({
|
|
905
|
-
type: 'MoveStruct',
|
|
906
|
-
name: match[1]
|
|
907
|
-
});
|
|
908
|
-
}
|
|
909
|
-
return structs;
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
_parseWASMBinary(binary) {
|
|
913
|
-
// Parse WASM binary format
|
|
914
|
-
return {
|
|
915
|
-
type: 'Program',
|
|
916
|
-
sourceType: 'wasm-binary',
|
|
917
|
-
body: [],
|
|
918
|
-
sections: []
|
|
919
|
-
};
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
_parseWAT(text) {
|
|
923
|
-
// Parse WebAssembly Text format
|
|
924
|
-
return {
|
|
925
|
-
type: 'Program',
|
|
926
|
-
sourceType: 'wat',
|
|
927
|
-
body: []
|
|
928
|
-
};
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
_typeCheck(ast, analysis) {
|
|
932
|
-
// Type checking pass
|
|
933
|
-
for (const contract of ast.contracts || []) {
|
|
934
|
-
for (const node of contract.body || []) {
|
|
935
|
-
if (node.type === 'FunctionDeclaration') {
|
|
936
|
-
analysis.functions.push({
|
|
937
|
-
name: node.name,
|
|
938
|
-
params: node.params,
|
|
939
|
-
returns: node.returns,
|
|
940
|
-
visibility: node.visibility,
|
|
941
|
-
stateMutability: node.stateMutability
|
|
942
|
-
});
|
|
943
|
-
} else if (node.type === 'StateVariableDeclaration') {
|
|
944
|
-
analysis.storage.push({
|
|
945
|
-
name: node.name,
|
|
946
|
-
dataType: node.dataType,
|
|
947
|
-
visibility: node.visibility
|
|
948
|
-
});
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
|
|
954
|
-
_analyzeStorage(ast, analysis, sourceVM) {
|
|
955
|
-
// Analyze storage layout based on source VM
|
|
956
|
-
// EVM uses 256-bit slots, Solana uses account data, etc.
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
_analyzeControlFlow(ast, analysis) {
|
|
960
|
-
// Control flow analysis for optimization
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
_securityAnalysis(ast, analysis) {
|
|
964
|
-
// Security checks: reentrancy, overflow, etc.
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
_generateTypes(analyzed) {
|
|
968
|
-
return analyzed.types || [];
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
_compileFunction(func, sourceVM) {
|
|
972
|
-
return {
|
|
973
|
-
name: func.name,
|
|
974
|
-
params: func.params.map(p => ({ name: p.name, type: this._mapType(p.type, sourceVM) })),
|
|
975
|
-
returns: func.returns.map(r => this._mapType(r, sourceVM)),
|
|
976
|
-
locals: [],
|
|
977
|
-
code: []
|
|
978
|
-
};
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
_mapType(type, sourceVM) {
|
|
982
|
-
// Map source VM types to JIR types
|
|
983
|
-
const typeMap = {
|
|
984
|
-
'uint256': JIR_TYPES.UINT256,
|
|
985
|
-
'int256': JIR_TYPES.INT256,
|
|
986
|
-
'address': JIR_TYPES.ADDRESS,
|
|
987
|
-
'bool': JIR_TYPES.BOOL,
|
|
988
|
-
'bytes32': JIR_TYPES.BYTES32,
|
|
989
|
-
'bytes': JIR_TYPES.BYTES,
|
|
990
|
-
'string': JIR_TYPES.STRING,
|
|
991
|
-
'uint8': JIR_TYPES.I32,
|
|
992
|
-
'uint16': JIR_TYPES.I32,
|
|
993
|
-
'uint32': JIR_TYPES.I32,
|
|
994
|
-
'uint64': JIR_TYPES.I64,
|
|
995
|
-
'int8': JIR_TYPES.I32,
|
|
996
|
-
'int16': JIR_TYPES.I32,
|
|
997
|
-
'int32': JIR_TYPES.I32,
|
|
998
|
-
'int64': JIR_TYPES.I64
|
|
999
|
-
};
|
|
1000
|
-
return typeMap[type] || JIR_TYPES.I64;
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
_generateStorageLayout(storage, sourceVM) {
|
|
1004
|
-
return storage.map((s, i) => ({
|
|
1005
|
-
slot: i,
|
|
1006
|
-
name: s.name,
|
|
1007
|
-
type: this._mapType(s.dataType, sourceVM),
|
|
1008
|
-
offset: 0
|
|
1009
|
-
}));
|
|
1010
|
-
}
|
|
1011
|
-
|
|
1012
|
-
_generateEvents(ast) {
|
|
1013
|
-
const events = [];
|
|
1014
|
-
// Extract event definitions from AST
|
|
1015
|
-
return events;
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
_assembleToBytecode(jir) {
|
|
1019
|
-
// Assemble JIR to bytecode
|
|
1020
|
-
const bytecode = [];
|
|
1021
|
-
|
|
1022
|
-
// Magic number
|
|
1023
|
-
bytecode.push(...jir.magic);
|
|
1024
|
-
|
|
1025
|
-
// Version
|
|
1026
|
-
bytecode.push(...jir.version);
|
|
1027
|
-
|
|
1028
|
-
// Sections
|
|
1029
|
-
for (const func of jir.functions) {
|
|
1030
|
-
// Function header
|
|
1031
|
-
bytecode.push(0x01); // Function section marker
|
|
1032
|
-
bytecode.push(func.params.length);
|
|
1033
|
-
bytecode.push(func.returns.length);
|
|
1034
|
-
|
|
1035
|
-
// Function code
|
|
1036
|
-
for (const op of func.code) {
|
|
1037
|
-
bytecode.push(op);
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
bytecode.push(JIR_OPCODES.END);
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
bytecode.push(JIR_OPCODES.HALT);
|
|
1044
|
-
|
|
1045
|
-
return Buffer.from(bytecode);
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
_hashSource(ast) {
|
|
1049
|
-
return crypto.createHash('sha256')
|
|
1050
|
-
.update(JSON.stringify(ast))
|
|
1051
|
-
.digest('hex');
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
_constantFolding(jir) {
|
|
1055
|
-
// Evaluate constant expressions at compile time
|
|
1056
|
-
return jir;
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
_deadCodeElimination(jir) {
|
|
1060
|
-
// Remove unreachable code
|
|
1061
|
-
return jir;
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
_commonSubexpressionElimination(jir) {
|
|
1065
|
-
// Reuse computed values
|
|
1066
|
-
return jir;
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
_loopInvariantMotion(jir) {
|
|
1070
|
-
// Move invariant computations out of loops
|
|
1071
|
-
return jir;
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
_inlineSmallFunctions(jir) {
|
|
1075
|
-
// Inline small functions to reduce call overhead
|
|
1076
|
-
return jir;
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
_peepholeOptimization(jir) {
|
|
1080
|
-
// Local optimizations on instruction sequences
|
|
1081
|
-
return jir;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
_validateType(type) {
|
|
1085
|
-
return true;
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
_validateFunction(func, jir) {
|
|
1089
|
-
return [];
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
_validateBytecode(bytecode) {
|
|
1093
|
-
return [];
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
module.exports = JIRCompiler;
|