zexus 1.6.2
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/LICENSE +0 -0
- package/README.md +2513 -0
- package/bin/zexus +2 -0
- package/bin/zpics +2 -0
- package/bin/zpm +2 -0
- package/bin/zx +2 -0
- package/bin/zx-deploy +2 -0
- package/bin/zx-dev +2 -0
- package/bin/zx-run +2 -0
- package/package.json +66 -0
- package/scripts/README.md +24 -0
- package/scripts/postinstall.js +44 -0
- package/shared_config.json +24 -0
- package/src/README.md +1525 -0
- package/src/tests/run_zexus_tests.py +117 -0
- package/src/tests/test_all_phases.zx +346 -0
- package/src/tests/test_blockchain_features.zx +306 -0
- package/src/tests/test_complexity_features.zx +321 -0
- package/src/tests/test_core_integration.py +185 -0
- package/src/tests/test_phase10_ecosystem.zx +177 -0
- package/src/tests/test_phase1_modifiers.zx +87 -0
- package/src/tests/test_phase2_plugins.zx +80 -0
- package/src/tests/test_phase3_security.zx +97 -0
- package/src/tests/test_phase4_vfs.zx +116 -0
- package/src/tests/test_phase5_types.zx +117 -0
- package/src/tests/test_phase6_metaprogramming.zx +125 -0
- package/src/tests/test_phase7_optimization.zx +132 -0
- package/src/tests/test_phase9_advanced_types.zx +157 -0
- package/src/tests/test_security_features.py +419 -0
- package/src/tests/test_security_features.zx +276 -0
- package/src/tests/test_simple_zx.zx +1 -0
- package/src/tests/test_verification_simple.zx +69 -0
- package/src/zexus/__init__.py +28 -0
- package/src/zexus/__main__.py +5 -0
- package/src/zexus/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/advanced_types.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/builtin_modules.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/capability_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/complexity_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/concurrency_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/config.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/dependency_injection.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/ecosystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/environment.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/error_reporter.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/hybrid_orchestrator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/metaprogramming.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/module_cache.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/object.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/optimization.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/plugin_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/policy_engine.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/security.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/stdlib_integration.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/strategy_recovery.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/syntax_validator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/type_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/virtual_filesystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_token.cpython-312.pyc +0 -0
- package/src/zexus/advanced_types.py +401 -0
- package/src/zexus/blockchain/__init__.py +40 -0
- package/src/zexus/blockchain/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/crypto.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/ledger.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/transaction.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/crypto.py +463 -0
- package/src/zexus/blockchain/ledger.py +255 -0
- package/src/zexus/blockchain/transaction.py +267 -0
- package/src/zexus/builtin_modules.py +284 -0
- package/src/zexus/builtin_plugins.py +317 -0
- package/src/zexus/capability_system.py +372 -0
- package/src/zexus/cli/__init__.py +2 -0
- package/src/zexus/cli/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/cli/__pycache__/main.cpython-312.pyc +0 -0
- package/src/zexus/cli/main.py +707 -0
- package/src/zexus/cli/zpm.py +203 -0
- package/src/zexus/compare_interpreter_compiler.py +146 -0
- package/src/zexus/compiler/__init__.py +169 -0
- package/src/zexus/compiler/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/compiler/bytecode.py +266 -0
- package/src/zexus/compiler/compat_runtime.py +277 -0
- package/src/zexus/compiler/lexer.py +257 -0
- package/src/zexus/compiler/parser.py +779 -0
- package/src/zexus/compiler/semantic.py +118 -0
- package/src/zexus/compiler/zexus_ast.py +454 -0
- package/src/zexus/complexity_system.py +575 -0
- package/src/zexus/concurrency_system.py +493 -0
- package/src/zexus/config.py +201 -0
- package/src/zexus/crypto_bridge.py +19 -0
- package/src/zexus/dependency_injection.py +423 -0
- package/src/zexus/ecosystem.py +434 -0
- package/src/zexus/environment.py +101 -0
- package/src/zexus/environment_manager.py +119 -0
- package/src/zexus/error_reporter.py +314 -0
- package/src/zexus/evaluator/__init__.py +12 -0
- package/src/zexus/evaluator/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/bytecode_compiler.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/core.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/expressions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/functions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/integration.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/statements.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/utils.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/bytecode_compiler.py +700 -0
- package/src/zexus/evaluator/core.py +891 -0
- package/src/zexus/evaluator/expressions.py +827 -0
- package/src/zexus/evaluator/functions.py +3989 -0
- package/src/zexus/evaluator/integration.py +396 -0
- package/src/zexus/evaluator/statements.py +4303 -0
- package/src/zexus/evaluator/utils.py +126 -0
- package/src/zexus/evaluator_original.py +2041 -0
- package/src/zexus/external_bridge.py +16 -0
- package/src/zexus/find_affected_imports.sh +155 -0
- package/src/zexus/hybrid_orchestrator.py +152 -0
- package/src/zexus/input_validation.py +259 -0
- package/src/zexus/lexer.py +571 -0
- package/src/zexus/logging.py +89 -0
- package/src/zexus/lsp/__init__.py +9 -0
- package/src/zexus/lsp/completion_provider.py +207 -0
- package/src/zexus/lsp/definition_provider.py +22 -0
- package/src/zexus/lsp/hover_provider.py +71 -0
- package/src/zexus/lsp/server.py +269 -0
- package/src/zexus/lsp/symbol_provider.py +31 -0
- package/src/zexus/metaprogramming.py +321 -0
- package/src/zexus/module_cache.py +89 -0
- package/src/zexus/module_manager.py +107 -0
- package/src/zexus/object.py +973 -0
- package/src/zexus/optimization.py +424 -0
- package/src/zexus/parser/__init__.py +31 -0
- package/src/zexus/parser/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_context.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_structural.cpython-312.pyc +0 -0
- package/src/zexus/parser/integration.py +86 -0
- package/src/zexus/parser/parser.py +3977 -0
- package/src/zexus/parser/strategy_context.py +7254 -0
- package/src/zexus/parser/strategy_structural.py +1033 -0
- package/src/zexus/persistence.py +391 -0
- package/src/zexus/plugin_system.py +290 -0
- package/src/zexus/policy_engine.py +365 -0
- package/src/zexus/profiler/__init__.py +5 -0
- package/src/zexus/profiler/profiler.py +233 -0
- package/src/zexus/purity_system.py +398 -0
- package/src/zexus/runtime/__init__.py +20 -0
- package/src/zexus/runtime/async_runtime.py +324 -0
- package/src/zexus/search_old_imports.sh +65 -0
- package/src/zexus/security.py +1407 -0
- package/src/zexus/stack_trace.py +233 -0
- package/src/zexus/stdlib/__init__.py +27 -0
- package/src/zexus/stdlib/blockchain.py +341 -0
- package/src/zexus/stdlib/compression.py +167 -0
- package/src/zexus/stdlib/crypto.py +124 -0
- package/src/zexus/stdlib/datetime.py +163 -0
- package/src/zexus/stdlib/db_mongo.py +199 -0
- package/src/zexus/stdlib/db_mysql.py +162 -0
- package/src/zexus/stdlib/db_postgres.py +163 -0
- package/src/zexus/stdlib/db_sqlite.py +133 -0
- package/src/zexus/stdlib/encoding.py +230 -0
- package/src/zexus/stdlib/fs.py +195 -0
- package/src/zexus/stdlib/http.py +219 -0
- package/src/zexus/stdlib/http_server.py +248 -0
- package/src/zexus/stdlib/json_module.py +61 -0
- package/src/zexus/stdlib/math.py +360 -0
- package/src/zexus/stdlib/os_module.py +265 -0
- package/src/zexus/stdlib/regex.py +148 -0
- package/src/zexus/stdlib/sockets.py +253 -0
- package/src/zexus/stdlib/test_framework.zx +208 -0
- package/src/zexus/stdlib/test_runner.zx +119 -0
- package/src/zexus/stdlib_integration.py +341 -0
- package/src/zexus/strategy_recovery.py +256 -0
- package/src/zexus/syntax_validator.py +356 -0
- package/src/zexus/testing/zpics.py +407 -0
- package/src/zexus/testing/zpics_runtime.py +369 -0
- package/src/zexus/type_system.py +374 -0
- package/src/zexus/validation_system.py +569 -0
- package/src/zexus/virtual_filesystem.py +355 -0
- package/src/zexus/vm/__init__.py +8 -0
- package/src/zexus/vm/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/async_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/bytecode.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/cache.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/jit.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_manager.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_pool.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/parallel_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/peephole_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/profiler.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_allocator.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/ssa_converter.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/async_optimizer.py +420 -0
- package/src/zexus/vm/bytecode.py +428 -0
- package/src/zexus/vm/bytecode_converter.py +297 -0
- package/src/zexus/vm/cache.py +532 -0
- package/src/zexus/vm/jit.py +720 -0
- package/src/zexus/vm/memory_manager.py +520 -0
- package/src/zexus/vm/memory_pool.py +511 -0
- package/src/zexus/vm/optimizer.py +478 -0
- package/src/zexus/vm/parallel_vm.py +899 -0
- package/src/zexus/vm/peephole_optimizer.py +452 -0
- package/src/zexus/vm/profiler.py +527 -0
- package/src/zexus/vm/register_allocator.py +462 -0
- package/src/zexus/vm/register_vm.py +520 -0
- package/src/zexus/vm/ssa_converter.py +757 -0
- package/src/zexus/vm/vm.py +1392 -0
- package/src/zexus/zexus_ast.py +1782 -0
- package/src/zexus/zexus_token.py +253 -0
- package/src/zexus/zpm/__init__.py +15 -0
- package/src/zexus/zpm/installer.py +116 -0
- package/src/zexus/zpm/package_manager.py +208 -0
- package/src/zexus/zpm/publisher.py +98 -0
- package/src/zexus/zpm/registry.py +110 -0
- package/src/zexus.egg-info/PKG-INFO +2235 -0
- package/src/zexus.egg-info/SOURCES.txt +876 -0
- package/src/zexus.egg-info/dependency_links.txt +1 -0
- package/src/zexus.egg-info/entry_points.txt +3 -0
- package/src/zexus.egg-info/not-zip-safe +1 -0
- package/src/zexus.egg-info/requires.txt +14 -0
- package/src/zexus.egg-info/top_level.txt +2 -0
- package/zexus.json +14 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Bytecode Definitions for Zexus VM
|
|
3
|
+
|
|
4
|
+
This module provides comprehensive bytecode representation and manipulation
|
|
5
|
+
for both the compiler and interpreter (evaluator) paths.
|
|
6
|
+
"""
|
|
7
|
+
from typing import List, Any, Tuple, Dict, Optional
|
|
8
|
+
from enum import IntEnum
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Opcode(IntEnum):
|
|
12
|
+
"""
|
|
13
|
+
Comprehensive opcode set for Zexus VM.
|
|
14
|
+
Supports both high-level and low-level operations.
|
|
15
|
+
"""
|
|
16
|
+
# Stack operations
|
|
17
|
+
LOAD_CONST = 1 # Push constant onto stack
|
|
18
|
+
LOAD_NAME = 2 # Load variable by name
|
|
19
|
+
STORE_NAME = 3 # Store value to variable
|
|
20
|
+
STORE_FUNC = 4 # Store function descriptor
|
|
21
|
+
POP = 5 # Pop top of stack
|
|
22
|
+
DUP = 6 # Duplicate top of stack
|
|
23
|
+
|
|
24
|
+
# Arithmetic operations
|
|
25
|
+
ADD = 10
|
|
26
|
+
SUB = 11
|
|
27
|
+
MUL = 12
|
|
28
|
+
DIV = 13
|
|
29
|
+
MOD = 14
|
|
30
|
+
POW = 15
|
|
31
|
+
NEG = 16 # Unary negation
|
|
32
|
+
|
|
33
|
+
# Comparison operations
|
|
34
|
+
EQ = 20 # ==
|
|
35
|
+
NEQ = 21 # !=
|
|
36
|
+
LT = 22 # <
|
|
37
|
+
GT = 23 # >
|
|
38
|
+
LTE = 24 # <=
|
|
39
|
+
GTE = 25 # >=
|
|
40
|
+
|
|
41
|
+
# Logical operations
|
|
42
|
+
AND = 30 # &&
|
|
43
|
+
OR = 31 # ||
|
|
44
|
+
NOT = 32 # !
|
|
45
|
+
|
|
46
|
+
# Control flow
|
|
47
|
+
JUMP = 40 # Unconditional jump
|
|
48
|
+
JUMP_IF_FALSE = 41 # Conditional jump
|
|
49
|
+
JUMP_IF_TRUE = 42 # Conditional jump (true)
|
|
50
|
+
RETURN = 43 # Return from function
|
|
51
|
+
|
|
52
|
+
# Function/Action calls
|
|
53
|
+
CALL_NAME = 50 # Call function by name
|
|
54
|
+
CALL_FUNC_CONST = 51 # Call function by constant descriptor
|
|
55
|
+
CALL_TOP = 52 # Call function on top of stack
|
|
56
|
+
CALL_BUILTIN = 53 # Call builtin function
|
|
57
|
+
|
|
58
|
+
# Collections
|
|
59
|
+
BUILD_LIST = 60 # Build list from stack items
|
|
60
|
+
BUILD_MAP = 61 # Build map from stack items
|
|
61
|
+
BUILD_SET = 62 # Build set from stack items
|
|
62
|
+
INDEX = 63 # Index into collection
|
|
63
|
+
SLICE = 64 # Slice operation
|
|
64
|
+
|
|
65
|
+
# Async/Concurrency
|
|
66
|
+
SPAWN = 70 # Spawn coroutine/task
|
|
67
|
+
AWAIT = 71 # Await coroutine
|
|
68
|
+
SPAWN_CALL = 72 # Spawn function call as task
|
|
69
|
+
|
|
70
|
+
# Events
|
|
71
|
+
REGISTER_EVENT = 80
|
|
72
|
+
EMIT_EVENT = 81
|
|
73
|
+
|
|
74
|
+
# Modules
|
|
75
|
+
IMPORT = 90
|
|
76
|
+
EXPORT = 91
|
|
77
|
+
|
|
78
|
+
# Advanced features
|
|
79
|
+
DEFINE_ENUM = 100
|
|
80
|
+
DEFINE_PROTOCOL = 101
|
|
81
|
+
|
|
82
|
+
# Blockchain-specific operations (110-119)
|
|
83
|
+
# These opcodes provide native VM support for blockchain operations
|
|
84
|
+
# dramatically improving smart contract execution performance
|
|
85
|
+
HASH_BLOCK = 110 # Hash a block structure (SHA-256)
|
|
86
|
+
VERIFY_SIGNATURE = 111 # Verify cryptographic signature
|
|
87
|
+
MERKLE_ROOT = 112 # Calculate Merkle tree root
|
|
88
|
+
STATE_READ = 113 # Read from blockchain state
|
|
89
|
+
STATE_WRITE = 114 # Write to blockchain state (TX context only)
|
|
90
|
+
TX_BEGIN = 115 # Begin transaction context
|
|
91
|
+
TX_COMMIT = 116 # Commit transaction changes
|
|
92
|
+
TX_REVERT = 117 # Revert transaction (rollback)
|
|
93
|
+
GAS_CHARGE = 118 # Deduct gas from execution limit
|
|
94
|
+
LEDGER_APPEND = 119 # Append entry to immutable ledger
|
|
95
|
+
ASSERT_PROTOCOL = 102
|
|
96
|
+
|
|
97
|
+
# Register-based operations (200-299) - Phase 5
|
|
98
|
+
# These provide 1.5-3x faster arithmetic via register allocation
|
|
99
|
+
LOAD_REG = 200 # Load constant to register: LOAD_REG r1, 42
|
|
100
|
+
LOAD_VAR_REG = 201 # Load variable to register: LOAD_VAR_REG r1, "x"
|
|
101
|
+
STORE_REG = 202 # Store register to variable: STORE_REG r1, "x"
|
|
102
|
+
MOV_REG = 203 # Move between registers: MOV_REG r2, r1
|
|
103
|
+
|
|
104
|
+
ADD_REG = 210 # r3 = r1 + r2
|
|
105
|
+
SUB_REG = 211 # r3 = r1 - r2
|
|
106
|
+
MUL_REG = 212 # r3 = r1 * r2
|
|
107
|
+
DIV_REG = 213 # r3 = r1 / r2
|
|
108
|
+
MOD_REG = 214 # r3 = r1 % r2
|
|
109
|
+
POW_REG = 215 # r3 = r1 ** r2
|
|
110
|
+
NEG_REG = 216 # r2 = -r1 (unary)
|
|
111
|
+
|
|
112
|
+
EQ_REG = 220 # r3 = (r1 == r2)
|
|
113
|
+
NEQ_REG = 221 # r3 = (r1 != r2)
|
|
114
|
+
LT_REG = 222 # r3 = (r1 < r2)
|
|
115
|
+
GT_REG = 223 # r3 = (r1 > r2)
|
|
116
|
+
LTE_REG = 224 # r3 = (r1 <= r2)
|
|
117
|
+
GTE_REG = 225 # r3 = (r1 >= r2)
|
|
118
|
+
|
|
119
|
+
AND_REG = 230 # r3 = r1 && r2
|
|
120
|
+
OR_REG = 231 # r3 = r1 || r2
|
|
121
|
+
NOT_REG = 232 # r2 = !r1
|
|
122
|
+
|
|
123
|
+
PUSH_REG = 240 # Push register value to stack (hybrid mode)
|
|
124
|
+
POP_REG = 241 # Pop stack value to register (hybrid mode)
|
|
125
|
+
|
|
126
|
+
# Parallel execution operations (300-399) - Phase 6
|
|
127
|
+
# These enable multi-core parallel bytecode execution for 2-4x speedup
|
|
128
|
+
PARALLEL_START = 300 # Mark start of parallelizable region
|
|
129
|
+
PARALLEL_END = 301 # Mark end of parallelizable region
|
|
130
|
+
BARRIER = 302 # Synchronization barrier (wait for all parallel tasks)
|
|
131
|
+
SPAWN_TASK = 303 # Spawn a new parallel task
|
|
132
|
+
TASK_JOIN = 304 # Wait for specific task to complete
|
|
133
|
+
TASK_RESULT = 305 # Get result from completed task
|
|
134
|
+
LOCK_ACQUIRE = 306 # Acquire shared lock
|
|
135
|
+
LOCK_RELEASE = 307 # Release shared lock
|
|
136
|
+
ATOMIC_ADD = 308 # Atomic addition to shared variable
|
|
137
|
+
ATOMIC_CAS = 309 # Compare-and-swap operation
|
|
138
|
+
|
|
139
|
+
# I/O Operations
|
|
140
|
+
PRINT = 250
|
|
141
|
+
READ = 251
|
|
142
|
+
WRITE = 252
|
|
143
|
+
|
|
144
|
+
# High-level constructs (for evaluator)
|
|
145
|
+
DEFINE_SCREEN = 120
|
|
146
|
+
DEFINE_COMPONENT = 121
|
|
147
|
+
DEFINE_THEME = 122
|
|
148
|
+
|
|
149
|
+
# Special
|
|
150
|
+
NOP = 255 # No operation
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class Bytecode:
|
|
154
|
+
"""
|
|
155
|
+
Bytecode container with instructions and constants pool.
|
|
156
|
+
Used by both compiler and evaluator to represent compiled code.
|
|
157
|
+
"""
|
|
158
|
+
def __init__(self):
|
|
159
|
+
self.instructions: List[Tuple[str, Any]] = []
|
|
160
|
+
self.constants: List[Any] = []
|
|
161
|
+
self.metadata: Dict[str, Any] = {
|
|
162
|
+
'source_file': None,
|
|
163
|
+
'version': '1.0',
|
|
164
|
+
'created_by': 'unknown'
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
def add_instruction(self, opcode: str, operand: Any = None) -> int:
|
|
168
|
+
"""Add an instruction and return its index"""
|
|
169
|
+
idx = len(self.instructions)
|
|
170
|
+
self.instructions.append((opcode, operand))
|
|
171
|
+
return idx
|
|
172
|
+
|
|
173
|
+
def add_constant(self, value: Any) -> int:
|
|
174
|
+
"""Add a constant to the pool and return its index"""
|
|
175
|
+
# Check if constant already exists (optimization)
|
|
176
|
+
for i, const in enumerate(self.constants):
|
|
177
|
+
if const == value and type(const) == type(value):
|
|
178
|
+
return i
|
|
179
|
+
|
|
180
|
+
idx = len(self.constants)
|
|
181
|
+
self.constants.append(value)
|
|
182
|
+
return idx
|
|
183
|
+
|
|
184
|
+
def update_instruction(self, idx: int, opcode: str, operand: Any = None):
|
|
185
|
+
"""Update an instruction at a specific index"""
|
|
186
|
+
if 0 <= idx < len(self.instructions):
|
|
187
|
+
self.instructions[idx] = (opcode, operand)
|
|
188
|
+
|
|
189
|
+
def get_instruction(self, idx: int) -> Optional[Tuple[str, Any]]:
|
|
190
|
+
"""Get instruction at index"""
|
|
191
|
+
if 0 <= idx < len(self.instructions):
|
|
192
|
+
return self.instructions[idx]
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
def get_constant(self, idx: int) -> Any:
|
|
196
|
+
"""Get constant at index"""
|
|
197
|
+
if 0 <= idx < len(self.constants):
|
|
198
|
+
return self.constants[idx]
|
|
199
|
+
return None
|
|
200
|
+
|
|
201
|
+
def set_metadata(self, key: str, value: Any):
|
|
202
|
+
"""Set metadata for this bytecode"""
|
|
203
|
+
self.metadata[key] = value
|
|
204
|
+
|
|
205
|
+
def disassemble(self) -> str:
|
|
206
|
+
"""
|
|
207
|
+
Generate human-readable disassembly of bytecode.
|
|
208
|
+
Useful for debugging.
|
|
209
|
+
"""
|
|
210
|
+
lines = []
|
|
211
|
+
lines.append(f"Bytecode Object ({len(self.instructions)} instructions, {len(self.constants)} constants)")
|
|
212
|
+
lines.append("=" * 60)
|
|
213
|
+
|
|
214
|
+
# Constants section
|
|
215
|
+
if self.constants:
|
|
216
|
+
lines.append("\nConstants:")
|
|
217
|
+
for i, const in enumerate(self.constants):
|
|
218
|
+
const_repr = repr(const)
|
|
219
|
+
if len(const_repr) > 50:
|
|
220
|
+
const_repr = const_repr[:47] + "..."
|
|
221
|
+
lines.append(f" {i:3d}: {const_repr}")
|
|
222
|
+
|
|
223
|
+
# Instructions section
|
|
224
|
+
lines.append("\nInstructions:")
|
|
225
|
+
for i, (opcode, operand) in enumerate(self.instructions):
|
|
226
|
+
if operand is not None:
|
|
227
|
+
lines.append(f" {i:4d} {opcode:20s} {operand}")
|
|
228
|
+
else:
|
|
229
|
+
lines.append(f" {i:4d} {opcode:20s}")
|
|
230
|
+
|
|
231
|
+
return "\n".join(lines)
|
|
232
|
+
|
|
233
|
+
def __repr__(self):
|
|
234
|
+
return f"Bytecode({len(self.instructions)} instructions, {len(self.constants)} constants)"
|
|
235
|
+
|
|
236
|
+
def __len__(self):
|
|
237
|
+
return len(self.instructions)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
class BytecodeBuilder:
|
|
241
|
+
"""
|
|
242
|
+
Helper class for building bytecode with higher-level constructs.
|
|
243
|
+
Provides convenience methods for common patterns.
|
|
244
|
+
"""
|
|
245
|
+
def __init__(self):
|
|
246
|
+
self.bytecode = Bytecode()
|
|
247
|
+
self._label_positions: Dict[str, int] = {}
|
|
248
|
+
self._forward_refs: Dict[str, List[int]] = {}
|
|
249
|
+
|
|
250
|
+
def emit(self, opcode: str, operand: Any = None) -> int:
|
|
251
|
+
"""Emit an instruction"""
|
|
252
|
+
return self.bytecode.add_instruction(opcode, operand)
|
|
253
|
+
|
|
254
|
+
def emit_constant(self, value: Any) -> int:
|
|
255
|
+
"""Emit LOAD_CONST instruction"""
|
|
256
|
+
const_idx = self.bytecode.add_constant(value)
|
|
257
|
+
return self.emit("LOAD_CONST", const_idx)
|
|
258
|
+
|
|
259
|
+
def emit_load(self, name: str) -> int:
|
|
260
|
+
"""Emit LOAD_NAME instruction"""
|
|
261
|
+
name_idx = self.bytecode.add_constant(name)
|
|
262
|
+
return self.emit("LOAD_NAME", name_idx)
|
|
263
|
+
|
|
264
|
+
def emit_store(self, name: str) -> int:
|
|
265
|
+
"""Emit STORE_NAME instruction"""
|
|
266
|
+
name_idx = self.bytecode.add_constant(name)
|
|
267
|
+
return self.emit("STORE_NAME", name_idx)
|
|
268
|
+
|
|
269
|
+
def emit_call(self, name: str, arg_count: int) -> int:
|
|
270
|
+
"""Emit CALL_NAME instruction"""
|
|
271
|
+
name_idx = self.bytecode.add_constant(name)
|
|
272
|
+
return self.emit("CALL_NAME", (name_idx, arg_count))
|
|
273
|
+
|
|
274
|
+
def mark_label(self, label: str):
|
|
275
|
+
"""Mark a position with a label for jumps"""
|
|
276
|
+
self._label_positions[label] = len(self.bytecode.instructions)
|
|
277
|
+
|
|
278
|
+
def emit_jump(self, label: str) -> int:
|
|
279
|
+
"""Emit a jump to a label (resolved later)"""
|
|
280
|
+
idx = self.emit("JUMP", None)
|
|
281
|
+
self._forward_refs.setdefault(label, []).append(idx)
|
|
282
|
+
return idx
|
|
283
|
+
|
|
284
|
+
def emit_jump_if_false(self, label: str) -> int:
|
|
285
|
+
"""Emit a conditional jump to a label"""
|
|
286
|
+
idx = self.emit("JUMP_IF_FALSE", None)
|
|
287
|
+
self._forward_refs.setdefault(label, []).append(idx)
|
|
288
|
+
return idx
|
|
289
|
+
|
|
290
|
+
def resolve_labels(self):
|
|
291
|
+
"""Resolve all forward label references"""
|
|
292
|
+
for label, instr_indices in self._forward_refs.items():
|
|
293
|
+
if label in self._label_positions:
|
|
294
|
+
target = self._label_positions[label]
|
|
295
|
+
for idx in instr_indices:
|
|
296
|
+
opcode, _ = self.bytecode.instructions[idx]
|
|
297
|
+
self.bytecode.instructions[idx] = (opcode, target)
|
|
298
|
+
|
|
299
|
+
# Blockchain-specific helper methods
|
|
300
|
+
def emit_hash_block(self) -> int:
|
|
301
|
+
"""Emit HASH_BLOCK instruction - expects block data on stack"""
|
|
302
|
+
return self.emit("HASH_BLOCK")
|
|
303
|
+
|
|
304
|
+
def emit_verify_signature(self) -> int:
|
|
305
|
+
"""Emit VERIFY_SIGNATURE - expects signature, message, public_key on stack"""
|
|
306
|
+
return self.emit("VERIFY_SIGNATURE")
|
|
307
|
+
|
|
308
|
+
def emit_merkle_root(self, leaf_count: int) -> int:
|
|
309
|
+
"""Emit MERKLE_ROOT - expects leaf_count items on stack"""
|
|
310
|
+
return self.emit("MERKLE_ROOT", leaf_count)
|
|
311
|
+
|
|
312
|
+
def emit_state_read(self, key: str) -> int:
|
|
313
|
+
"""Emit STATE_READ instruction"""
|
|
314
|
+
key_idx = self.bytecode.add_constant(key)
|
|
315
|
+
return self.emit("STATE_READ", key_idx)
|
|
316
|
+
|
|
317
|
+
def emit_state_write(self, key: str) -> int:
|
|
318
|
+
"""Emit STATE_WRITE instruction - expects value on stack"""
|
|
319
|
+
key_idx = self.bytecode.add_constant(key)
|
|
320
|
+
return self.emit("STATE_WRITE", key_idx)
|
|
321
|
+
|
|
322
|
+
def emit_tx_begin(self) -> int:
|
|
323
|
+
"""Emit TX_BEGIN instruction"""
|
|
324
|
+
return self.emit("TX_BEGIN")
|
|
325
|
+
|
|
326
|
+
def emit_tx_commit(self) -> int:
|
|
327
|
+
"""Emit TX_COMMIT instruction"""
|
|
328
|
+
return self.emit("TX_COMMIT")
|
|
329
|
+
|
|
330
|
+
def emit_tx_revert(self, reason: str = None) -> int:
|
|
331
|
+
"""Emit TX_REVERT instruction with optional reason"""
|
|
332
|
+
if reason:
|
|
333
|
+
reason_idx = self.bytecode.add_constant(reason)
|
|
334
|
+
return self.emit("TX_REVERT", reason_idx)
|
|
335
|
+
return self.emit("TX_REVERT")
|
|
336
|
+
|
|
337
|
+
def emit_gas_charge(self, amount: int) -> int:
|
|
338
|
+
"""Emit GAS_CHARGE instruction"""
|
|
339
|
+
return self.emit("GAS_CHARGE", amount)
|
|
340
|
+
|
|
341
|
+
def emit_ledger_append(self) -> int:
|
|
342
|
+
"""Emit LEDGER_APPEND - expects entry on stack"""
|
|
343
|
+
return self.emit("LEDGER_APPEND")
|
|
344
|
+
|
|
345
|
+
# Convenience methods for test compatibility
|
|
346
|
+
def emit_load_const(self, value: Any) -> int:
|
|
347
|
+
"""Emit LOAD_CONST instruction (alias for emit_constant)"""
|
|
348
|
+
return self.emit_constant(value)
|
|
349
|
+
|
|
350
|
+
def emit_load_name(self, name: str) -> int:
|
|
351
|
+
"""Emit LOAD_NAME instruction (alias for emit_load)"""
|
|
352
|
+
return self.emit_load(name)
|
|
353
|
+
|
|
354
|
+
def emit_store_name(self, name: str) -> int:
|
|
355
|
+
"""Emit STORE_NAME instruction (alias for emit_store)"""
|
|
356
|
+
return self.emit_store(name)
|
|
357
|
+
|
|
358
|
+
def emit_add(self) -> int:
|
|
359
|
+
"""Emit ADD instruction"""
|
|
360
|
+
return self.emit("ADD")
|
|
361
|
+
|
|
362
|
+
def emit_sub(self) -> int:
|
|
363
|
+
"""Emit SUB instruction"""
|
|
364
|
+
return self.emit("SUB")
|
|
365
|
+
|
|
366
|
+
def emit_mul(self) -> int:
|
|
367
|
+
"""Emit MUL instruction"""
|
|
368
|
+
return self.emit("MUL")
|
|
369
|
+
|
|
370
|
+
def emit_div(self) -> int:
|
|
371
|
+
"""Emit DIV instruction"""
|
|
372
|
+
return self.emit("DIV")
|
|
373
|
+
|
|
374
|
+
def emit_pow(self) -> int:
|
|
375
|
+
"""Emit POW instruction"""
|
|
376
|
+
return self.emit("POW")
|
|
377
|
+
|
|
378
|
+
def emit_mod(self) -> int:
|
|
379
|
+
"""Emit MOD instruction"""
|
|
380
|
+
return self.emit("MOD")
|
|
381
|
+
|
|
382
|
+
def emit_eq(self) -> int:
|
|
383
|
+
"""Emit EQ instruction"""
|
|
384
|
+
return self.emit("EQ")
|
|
385
|
+
|
|
386
|
+
def emit_lt(self) -> int:
|
|
387
|
+
"""Emit LT instruction"""
|
|
388
|
+
return self.emit("LT")
|
|
389
|
+
|
|
390
|
+
def emit_gt(self) -> int:
|
|
391
|
+
"""Emit GT instruction"""
|
|
392
|
+
return self.emit("GT")
|
|
393
|
+
|
|
394
|
+
def emit_pop(self) -> int:
|
|
395
|
+
"""Emit POP instruction"""
|
|
396
|
+
return self.emit("POP")
|
|
397
|
+
|
|
398
|
+
def emit_return(self) -> int:
|
|
399
|
+
"""Emit RETURN instruction"""
|
|
400
|
+
return self.emit("RETURN")
|
|
401
|
+
|
|
402
|
+
def emit_spawn(self, operand: Any = None) -> int:
|
|
403
|
+
"""Emit SPAWN instruction"""
|
|
404
|
+
return self.emit("SPAWN", operand)
|
|
405
|
+
|
|
406
|
+
def emit_await(self) -> int:
|
|
407
|
+
"""Emit AWAIT instruction"""
|
|
408
|
+
return self.emit("AWAIT")
|
|
409
|
+
|
|
410
|
+
def emit_register_event(self, event_data: Tuple[str, str]) -> int:
|
|
411
|
+
"""Emit REGISTER_EVENT instruction"""
|
|
412
|
+
event_name_idx = self.bytecode.add_constant(event_data[0])
|
|
413
|
+
handler_idx = self.bytecode.add_constant(event_data[1])
|
|
414
|
+
return self.emit("REGISTER_EVENT", (event_name_idx, handler_idx))
|
|
415
|
+
|
|
416
|
+
def emit_emit_event(self, event_name: str) -> int:
|
|
417
|
+
"""Emit EMIT_EVENT instruction"""
|
|
418
|
+
event_idx = self.bytecode.add_constant(event_name)
|
|
419
|
+
return self.emit("EMIT_EVENT", (event_idx,))
|
|
420
|
+
|
|
421
|
+
def emit_label(self, label: str):
|
|
422
|
+
"""Alias for mark_label - for compatibility"""
|
|
423
|
+
return self.mark_label(label)
|
|
424
|
+
|
|
425
|
+
def build(self) -> Bytecode:
|
|
426
|
+
"""Finalize and return the bytecode"""
|
|
427
|
+
self.resolve_labels()
|
|
428
|
+
return self.bytecode
|