refactor src/codegen#1923
Conversation
Add the TargetCodegen strategy trait (interface.rs) plus a make_target factory (targets/mod.rs) that the orchestrator drives instead of branching on ns.target. Re-export EventEmitter from interface.rs as the second boundary trait (visibility bumped to pub(crate)). Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Move soroban.rs, dispatch/soroban.rs, encoding/soroban_encoding.rs and events/soroban.rs under targets/soroban/ as history-preserving renames, and move the SorobanTarget impl alongside them. Repoint imports and bump the cross-boundary helpers to pub(crate). Pure relocation: golden CFG tests pass with zero CHECK edits. Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
…tCodegen Convert five scattered target branches in the shared lowering to TargetCodegen methods, each with a behaviour-preserving default: - selector_hash_algorithm() (was Solana/else Sha256/Keccak256 ternary) - storage_array_length_is_inline() (was Solana||Soroban inline-length branch) - lower_load() (was Soroban handle lazy-decode in Load arm) - prepare_storage_value() (was Soroban encode before SetStorage/Store) - default_storage_value() (was Soroban soroban_init_with_vec block) Soroban overrides the three behavioural hooks; Solana overrides the two property methods; Polkadot inherits the defaults. Golden CFG (zero // CHECK: edits) and soroban suites pass; lower_load verified IR-identical via before/after emit-cfg diff on handle-loading contracts. Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
…gh TargetCodegen Signed-off-by: Islam-Imad <islamimad404@gmail.com>
…ltin Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
…ze; seal TargetCodegen Signed-off-by: Islam-Imad <islamimad404@gmail.com>
…fset Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
|
The direction is right. Replacing the scattered Two things to tighten before merge:
|
|
Please fix the issues causing the failed build tests for Linux x86-64. |
Signed-off-by: Islam-Imad <islamimad404@gmail.com>
d2e8a20 to
9dad537
Compare
Problem
Target-specific policy was scattered across shared codegen files as inline
if ns.target == …/match ns.target { … }branches.Solution:
TargetCodegentraitsrc/codegen/interface.rsintroduces a singleTargetCodegentrait.SorobanTarget,SolanaTarget, andPolkadotTargeteach implement it.1. Selector hash algorithm —
selector_hash_algorithm()A target property (constant that differs per target) replaces a branch. Solana uses Sha256; every other target uses Keccak256. (
c3ae3d24,expression.rs)2. Function dispatch —
function_dispatch()A standalone
match ns.targetfactory indispatch/mod.rsis replaced by a trait method; each target owns its dispatcher. (b9cbddb7,dispatch/mod.rs→mod.rs)3. Pre-CFG validation —
validate_contract()Soroban's ABI type checks ran as a guarded block inside the shared program loop. (
b9cbddb7,mod.rs)4. Post-CFG validation —
validate_cfgs()Soroban's post-CFG ABI check ran inline after all CFGs were built. (
b9cbddb7,mod.rs)5. Post-program processing —
post_process_program()Solana's account collection and management ran as a guarded block at the end of
codegen(). (b9cbddb7,mod.rs)6. Initial storage slot —
initial_storage_slot()Solana reserves the first 16 bytes for account metadata; all other targets begin at slot 0. (
6dbe54b8,mod.rs)7. Storage slot alignment —
align_storage_slot()Solana aligns each field to its natural alignment; other targets pack without padding. (
6dbe54b8,mod.rs)8. Storage array length —
lower_storage_array_length()Solana and Soroban store the length inline; Polkadot/EVM load it from a separate storage slot. (
3fada021,expression.rs)9. Storage array push —
storage_array_push()Solana and
storage_bytesuse a flatarray_push; Polkadot/Soroban use the slot-hashing path. (9930674b,expression.rs)10. Storage array pop —
storage_array_pop()Same split as push: Solana/bytes use
array_pop; Polkadot/Soroban usestorage_slots_array_pop. (9930674b,expression.rs)11. Storage array entry offset —
storage_array_entry_offset()Soroban encodes the index and uses a
Subscript; Polkadot/EVM hash the slot with keccak256. (6bb4269b,storage.rs)12. Storage struct member access —
lower_storage_struct_member()Three-way split: Solana uses pre-computed
storage_offsets; Polkadot/EVM sum field slots; Soroban encodes the offset index and callsVecPushBack. (6dbe54b8,expression.rs)13. Load expression —
lower_load()Soroban lazily decodes handle references on load; all other targets return the
Loadexpression as-is. (c3ae3d24,expression.rs)14. Load-storage result —
lower_load_storage()After reading from storage, Soroban decodes any
SorobanHandlereference; others pass through. (6dbe54b8,storage.rs)15. Prepare storage value —
prepare_storage_value()Before writing to storage, Soroban encodes the value; all other targets write it unchanged. (
c3ae3d24,expression.rs)16. Default storage value —
default_storage_value()For Soroban,
String/DynamicBytes/dynamic-array variables are initialised withsoroban_vec_new; other targets skip initialisation and usecontinue. (c3ae3d24,mod.rs)17. ABI encode —
abi_encode()Soroban's early-return in the shared encoder is replaced by a per-target method. (
ddac492c,encoding/mod.rs)18. ABI decode —
abi_decode()Same pattern as encode: Soroban's early-return is replaced by a trait method. (
ddac492c,encoding/mod.rs)19. Event emitter —
event_emitter()A standalone
new_event_emitterfactory with a fullmatch ns.targetis replaced by a trait method. (e45cbd83,events/mod.rs)20. Default gas —
default_gas_builtin()default_gas(ns)was a free function that branched onns.target; now each target returns its constant directly. (6dbe54b8,expression.rs)21. Print expression —
lower_print_expr()Polkadot prepends a
print:/,\nprefix; EVM and other targets pass the expression through unchanged. (6dbe54b8,expression.rs)22. Mapping subscript —
lower_mapping_subscript()Polkadot hashes
(array, index)with keccak256 to form the storage key; EVM/Solana/Soroban use a directSubscript. (6dbe54b8,expression.rs)23. Target-specific builtins —
lower_builtin()All per-target builtin arms (EVM
Gasprice,PayableSend/PayableTransfer, and SorobanRequireAuth/AuthAsCurrContract/ExtendTtl) are consolidated behind a single hook. (6dbe54b8,57e4f941,d740e636,expression.rs)Before — three separate inline arms scattered across
expression.rs:After — one hook that every target opts into: