solar-codegen 0.2.0

Solidity MIR and EVM code generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Structured assembler block program and final linear assembly program.

use super::{AsmInst, AsmInstKind, DeferredConst, Label, op};
use crate::backend::evm::ir::{
    EvmIrBlock, EvmIrBlockHotness, EvmIrInstruction, EvmIrInstructionKind, EvmIrModule,
    EvmIrOperand, EvmIrPass, EvmIrStackEffect, EvmIrStackOp, EvmIrTerminator, EvmIrTerminatorKind,
    verify_evm_ir_module,
};
use alloy_primitives::U256;
use solar_data_structures::map::FxHashSet;

const OP_PREFIX: &str = "op_";
const PUSH_MNEMONIC: &str = "push";
const PUSH_DEFERRED_MNEMONIC: &str = "push_deferred";
const PUSH_IMMUTABLE_MNEMONIC: &str = "push_immutable";

pub(in crate::backend::evm) trait StructuredAsmContext {
    fn push_value(&self, index: super::PushValueId) -> U256;
    fn push_inst(&mut self, value: U256) -> AsmInst;
    fn new_label(&mut self) -> Label;
    /// Whether the bridge should run the experimental EVM IR `StackSchedule`
    /// pass. Off by default; see [`StructuredAsmProgram::optimize_with_evm_ir`].
    fn run_evm_ir_stack_schedule(&self) -> bool {
        false
    }
    /// Whether the bridge should run EVM IR layout/code-size passes.
    fn run_evm_ir_layout_passes(&self) -> bool {
        false
    }
}

/// Structured assembler block program used while MIR lowering emits EVM code.
///
/// This is intentionally still instruction-close to the final assembly layer:
/// operands such as unresolved labels, deferred constants, and immutable
/// placeholders are preserved as assembler operands. The parseable EVM backend
/// IR lives in [`crate::backend::evm::ir`]; this private layer is the bridge
/// between that target-specific IR direction and final linear assembly.
#[derive(Clone, Debug, Default)]
pub(in crate::backend::evm) struct StructuredAsmProgram {
    blocks: Vec<StructuredAsmBlock>,
    current: Option<usize>,
    cold_labels: FxHashSet<Label>,
}

impl StructuredAsmProgram {
    /// Clears all blocks and metadata.
    pub(in crate::backend::evm) fn clear(&mut self) {
        self.blocks.clear();
        self.current = None;
        self.cold_labels.clear();
    }

    /// Emits an instruction into the current structured assembler block.
    pub(in crate::backend::evm) fn push(&mut self, inst: AsmInst) {
        let block = self.current_block_mut();
        block.instructions.push(inst);
    }

    /// Defines a label, starting a new structured assembler block.
    pub(in crate::backend::evm) fn define_label(&mut self, label: Label) {
        let block = StructuredAsmBlock {
            label: Some(label),
            cold: self.cold_labels.contains(&label),
            instructions: Vec::new(),
        };
        self.blocks.push(block);
        self.current = Some(self.blocks.len() - 1);
    }

    /// Marks the block beginning at `label` as cold.
    pub(in crate::backend::evm) fn mark_cold(&mut self, label: Label) {
        self.cold_labels.insert(label);
        if let Some(block) = self.blocks.iter_mut().find(|block| block.label == Some(label)) {
            block.cold = true;
        }
    }

    /// Returns a linear assembly view for tests.
    #[cfg(test)]
    pub(in crate::backend::evm) fn instructions(&self) -> Vec<AsmInst> {
        self.to_asm_program().instructions
    }

    /// Lowers structured assembler blocks to the final linear assembly program.
    pub(in crate::backend::evm) fn to_asm_program(&self) -> EvmAsmProgram {
        let mut program = EvmAsmProgram::default();
        for block in &self.blocks {
            if let Some(label) = block.label {
                program.instructions.push(AsmInst::label(label));
            }
            program.instructions.extend_from_slice(&block.instructions);
        }
        program
    }

    /// Runs machine-level EVM IR passes over the structured assembler blocks.
    ///
    /// MIR lowering currently emits stack-scheduled assembler instructions.
    /// This bridge makes the production backend pass through the same untyped
    /// block IR used by `solar evm-opt` while preserving unresolved assembler
    /// operands such as labels, deferred constants, and immutable placeholders.
    ///
    /// # Experimental `StackSchedule` gating
    ///
    /// When `context.run_evm_ir_stack_schedule()` is true (off by default) the
    /// bridge runs [`EvmIrPass::StackSchedule`]. The reality of what the bridge
    /// feeds the scheduler matters here: MIR lowering has already materialized
    /// every virtual stack-word operand into physical `dup`/`swap`/`pop` and
    /// `push`/opcode instructions, so `to_evm_ir_module` produces
    /// *operand-cleared* IR — no instruction carries an [`EvmIrOperand::Value`],
    /// and the only terminators emitted here (`jump`/`fallthrough`/raw terminal
    /// opcode/`stop`/`invalid`) carry no value operands either. `StackSchedule`
    /// only rewrites instructions that have value operands to materialize, so on
    /// this input it has nothing to materialize: every instruction is replayed
    /// onto its model stack and pushed back unchanged, and blocks it cannot model
    /// are restored verbatim. It is therefore a *near no-op* whose only
    /// observable effect is recording inferred `(in ...)` entry signatures,
    /// which `from_evm_ir_module` ignores.
    ///
    /// To make turning the flag on provably safe, the scheduled module is checked
    /// against the verifier oracle and the bytecode-bearing instruction stream is
    /// required to be unchanged; if either check fails the scheduled module is
    /// discarded and the original (pre-schedule) module is used, so enabling the
    /// flag can never produce invalid or divergent bytecode.
    pub(in crate::backend::evm) fn optimize_with_evm_ir<C>(&mut self, context: &mut C) -> usize
    where
        C: StructuredAsmContext,
    {
        let Some((mut module, mut labels)) = self.to_evm_ir_module(context) else {
            return 0;
        };

        debug_assert!(verify_evm_ir_module(&module).is_ok());
        let mut changed = 0;

        if context.run_evm_ir_stack_schedule() {
            // Differential safety net: schedule a clone, and only adopt it if the
            // verifier accepts it and its bytecode-bearing instruction stream is
            // identical to the input. On the bridge's operand-cleared IR this
            // always holds (the pass is a near no-op), but the guard means an
            // unexpected rewrite is dropped instead of changing produced code.
            let mut scheduled = module.clone();
            if EvmIrPass::StackSchedule.run(&mut scheduled)
                && verify_evm_ir_module(&scheduled).is_ok()
                && modules_have_equal_code(&module, &scheduled)
            {
                module = scheduled;
                changed += 1;
            }
        }

        if context.run_evm_ir_layout_passes() {
            for pass in [EvmIrPass::ColdLayout, EvmIrPass::TerminalDedup] {
                changed += usize::from(pass.run(&mut module));
            }
        }
        debug_assert!(verify_evm_ir_module(&module).is_ok());

        *self = Self::from_evm_ir_module(&module, &mut labels, context);
        changed
    }

    /// Resolves deferred constants while preserving structured block boundaries.
    pub(in crate::backend::evm) fn resolve_deferred_consts<F>(&mut self, mut resolve: F)
    where
        F: FnMut(DeferredConst) -> AsmInst,
    {
        for block in &mut self.blocks {
            for inst in &mut block.instructions {
                if let AsmInstKind::PushDeferred(id) = inst.kind() {
                    *inst = resolve(id);
                }
            }
        }
    }

    fn current_block_mut(&mut self) -> &mut StructuredAsmBlock {
        let index = match self.current {
            Some(index) => index,
            None => {
                self.blocks.push(StructuredAsmBlock::default());
                let index = self.blocks.len() - 1;
                self.current = Some(index);
                index
            }
        };
        &mut self.blocks[index]
    }

    fn to_evm_ir_module<C>(&self, context: &mut C) -> Option<(EvmIrModule, Vec<Option<Label>>)>
    where
        C: StructuredAsmContext,
    {
        if self.blocks.is_empty() {
            return None;
        }

        let labels: Vec<_> = self.blocks.iter().map(|block| block.label).collect();
        let mut label_to_block = std::collections::BTreeMap::new();
        for (index, block) in self.blocks.iter().enumerate() {
            if let Some(label) = block.label {
                label_to_block.insert(label, crate::backend::evm::EvmIrBlockId::from_usize(index));
            }
        }

        let mut module = EvmIrModule::new("asm");
        for (index, block) in self.blocks.iter().enumerate() {
            let mut ir_block = EvmIrBlock::new(format!("bb{index}"));
            if block.cold {
                ir_block.metadata.hotness = EvmIrBlockHotness::Cold;
            }
            module.add_block(ir_block);
        }

        for (index, block) in self.blocks.iter().enumerate() {
            let block_id = crate::backend::evm::EvmIrBlockId::from_usize(index);
            let next_block = (index + 1 < self.blocks.len())
                .then(|| crate::backend::evm::EvmIrBlockId::from_usize(index + 1));
            let (instructions, terminator) =
                Self::translate_block_to_evm_ir(block, next_block, &label_to_block, context)?;
            module.blocks[block_id].instructions = instructions;
            module.blocks[block_id].terminator = Some(EvmIrTerminator::new(terminator));
        }

        Some((module, labels))
    }

    fn translate_block_to_evm_ir<C>(
        block: &StructuredAsmBlock,
        next_block: Option<crate::backend::evm::EvmIrBlockId>,
        label_to_block: &std::collections::BTreeMap<Label, crate::backend::evm::EvmIrBlockId>,
        context: &mut C,
    ) -> Option<(Vec<EvmIrInstruction>, EvmIrTerminatorKind)>
    where
        C: StructuredAsmContext,
    {
        let mut body_len = block.instructions.len();
        let terminator =
            if let Some((target, len)) = Self::trailing_static_jump(block, label_to_block) {
                body_len = len;
                EvmIrTerminatorKind::Jump(target)
            } else if let Some(AsmInstKind::Op(opcode)) =
                block.instructions.last().map(|inst| inst.kind())
                && op::is_terminal(opcode)
            {
                body_len = body_len.saturating_sub(1);
                EvmIrTerminatorKind::RawOpcode(opcode)
            } else {
                EvmIrTerminatorKind::Fallthrough(next_block?)
            };

        let mut instructions = Vec::with_capacity(body_len);
        for &inst in &block.instructions[..body_len] {
            instructions.push(Self::inst_to_evm_ir(inst, label_to_block, context)?);
        }
        Some((instructions, terminator))
    }

    fn trailing_static_jump(
        block: &StructuredAsmBlock,
        label_to_block: &std::collections::BTreeMap<Label, crate::backend::evm::EvmIrBlockId>,
    ) -> Option<(crate::backend::evm::EvmIrBlockId, usize)> {
        let [rest @ .., push, jump] = block.instructions.as_slice() else {
            return None;
        };
        if jump.kind() != AsmInstKind::Op(op::JUMP) {
            return None;
        }
        let AsmInstKind::PushLabel(label) = push.kind() else {
            return None;
        };
        Some((*label_to_block.get(&label)?, rest.len()))
    }

    fn inst_to_evm_ir<C>(
        inst: AsmInst,
        label_to_block: &std::collections::BTreeMap<Label, crate::backend::evm::EvmIrBlockId>,
        context: &mut C,
    ) -> Option<EvmIrInstruction>
    where
        C: StructuredAsmContext,
    {
        Some(match inst.kind() {
            AsmInstKind::Op(opcode) => {
                if let Some(stack_op) = stack_op_from_opcode(opcode) {
                    EvmIrInstruction::stack_op(stack_op)
                } else {
                    let mut inst = EvmIrInstruction::new(opcode_mnemonic(opcode), Vec::new());
                    inst.metadata.stack = Some(EvmIrStackEffect::new(0, 0));
                    inst
                }
            }
            AsmInstKind::PushInline(value) => {
                push_instruction(EvmIrOperand::Immediate(U256::from(value)))
            }
            AsmInstKind::Push(index) => {
                push_instruction(EvmIrOperand::Immediate(context.push_value(index)))
            }
            AsmInstKind::PushLabel(label) => {
                let block = *label_to_block.get(&label)?;
                push_instruction(EvmIrOperand::Block(block))
            }
            AsmInstKind::PushDeferred(id) => EvmIrInstruction::new(
                PUSH_DEFERRED_MNEMONIC,
                vec![EvmIrOperand::Immediate(U256::from(id.index()))],
            ),
            AsmInstKind::PushImmutable(id) => EvmIrInstruction::new(
                PUSH_IMMUTABLE_MNEMONIC,
                vec![EvmIrOperand::Immediate(U256::from(id))],
            ),
            AsmInstKind::Label(_) => return None,
        })
    }

    fn from_evm_ir_module<C>(
        module: &EvmIrModule,
        labels: &mut Vec<Option<Label>>,
        context: &mut C,
    ) -> Self
    where
        C: StructuredAsmContext,
    {
        let mut program = Self::default();
        for (block_id, block) in module.blocks.iter_enumerated() {
            let original = original_block_index(&block.label);
            let label = original.and_then(|index| labels.get(index).copied().flatten());
            if let Some(label) = label {
                program.define_label(label);
                if block.metadata.hotness == EvmIrBlockHotness::Cold {
                    program.mark_cold(label);
                }
            } else {
                program.blocks.push(StructuredAsmBlock {
                    label: None,
                    cold: block.metadata.hotness == EvmIrBlockHotness::Cold,
                    instructions: Vec::new(),
                });
                program.current = Some(program.blocks.len() - 1);
            }

            for inst in &block.instructions {
                if let Some(asm_inst) = Self::evm_ir_inst_to_asm(inst, module, labels, context) {
                    program.push(asm_inst);
                }
            }

            if let Some(term) = &block.terminator {
                Self::emit_evm_ir_terminator(
                    &mut program,
                    block_id,
                    &term.kind,
                    module,
                    labels,
                    context,
                );
            }
        }
        program
    }

    fn evm_ir_inst_to_asm<C>(
        inst: &EvmIrInstruction,
        module: &EvmIrModule,
        labels: &mut Vec<Option<Label>>,
        context: &mut C,
    ) -> Option<AsmInst>
    where
        C: StructuredAsmContext,
    {
        match &inst.kind {
            EvmIrInstructionKind::Stack(op) => Some(AsmInst::op(opcode_from_stack_op(*op))),
            EvmIrInstructionKind::Operation(mnemonic) if mnemonic == PUSH_MNEMONIC => {
                match inst.operands.as_slice() {
                    [EvmIrOperand::Immediate(value)] => Some(context.push_inst(*value)),
                    [EvmIrOperand::Block(block)] => {
                        Some(AsmInst::push_label(label_for_block(module, *block, labels, context)))
                    }
                    _ => None,
                }
            }
            EvmIrInstructionKind::Operation(mnemonic) if mnemonic == PUSH_DEFERRED_MNEMONIC => {
                let [EvmIrOperand::Immediate(value)] = inst.operands.as_slice() else {
                    return None;
                };
                Some(AsmInst::push_deferred(DeferredConst::from_usize(
                    usize::try_from(*value).ok()?,
                )))
            }
            EvmIrInstructionKind::Operation(mnemonic) if mnemonic == PUSH_IMMUTABLE_MNEMONIC => {
                let [EvmIrOperand::Immediate(value)] = inst.operands.as_slice() else {
                    return None;
                };
                Some(AsmInst::push_immutable(u32::try_from(*value).ok()?))
            }
            EvmIrInstructionKind::Operation(mnemonic) => {
                parse_opcode_mnemonic(mnemonic).map(AsmInst::op)
            }
        }
    }

    fn emit_evm_ir_terminator<C>(
        program: &mut Self,
        block_id: crate::backend::evm::EvmIrBlockId,
        kind: &EvmIrTerminatorKind,
        module: &EvmIrModule,
        labels: &mut Vec<Option<Label>>,
        context: &mut C,
    ) where
        C: StructuredAsmContext,
    {
        match kind {
            EvmIrTerminatorKind::Fallthrough(target) => {
                if next_block(module, block_id) != Some(*target) {
                    let label = label_for_block(module, *target, labels, context);
                    program.push(AsmInst::push_label(label));
                    program.push(AsmInst::op(op::JUMP));
                }
            }
            EvmIrTerminatorKind::Jump(target) => {
                let label = label_for_block(module, *target, labels, context);
                program.push(AsmInst::push_label(label));
                program.push(AsmInst::op(op::JUMP));
            }
            EvmIrTerminatorKind::RawOpcode(opcode) => program.push(AsmInst::op(*opcode)),
            EvmIrTerminatorKind::Stop => program.push(AsmInst::op(op::STOP)),
            EvmIrTerminatorKind::Invalid => program.push(AsmInst::op(op::INVALID)),
            EvmIrTerminatorKind::Return { .. }
            | EvmIrTerminatorKind::Revert { .. }
            | EvmIrTerminatorKind::SelfDestruct { .. }
            | EvmIrTerminatorKind::Branch { .. }
            | EvmIrTerminatorKind::Switch { .. } => {
                unreachable!("structured assembler bridge only emits machine-level terminators")
            }
        }
    }
}

/// Whether two modules produce the same bytecode-bearing block stream.
///
/// `StackSchedule` records inferred `(in ...)` entry signatures on the blocks it
/// schedules, but `from_evm_ir_module` never reads `entry_stack`, so it does not
/// affect produced bytecode. This compares the parts the bridge actually lowers
/// back to assembly — block labels, hot/cold metadata, instruction streams, and
/// terminators — while ignoring the entry-signature bookkeeping.
fn modules_have_equal_code(before: &EvmIrModule, after: &EvmIrModule) -> bool {
    before.entry_block == after.entry_block
        && before.blocks.len() == after.blocks.len()
        && before.blocks.iter().zip(after.blocks.iter()).all(|(a, b)| {
            a.label == b.label
                && a.metadata == b.metadata
                && a.instructions == b.instructions
                && a.terminator == b.terminator
        })
}

fn push_instruction(operand: EvmIrOperand) -> EvmIrInstruction {
    let mut inst = EvmIrInstruction::new(PUSH_MNEMONIC, vec![operand]);
    inst.metadata.stack = Some(EvmIrStackEffect::new(0, 1));
    inst
}

fn opcode_mnemonic(opcode: u8) -> String {
    format!("{OP_PREFIX}{opcode:02x}")
}

fn parse_opcode_mnemonic(mnemonic: &str) -> Option<u8> {
    let value = mnemonic.strip_prefix(OP_PREFIX)?;
    u8::from_str_radix(value, 16).ok()
}

fn stack_op_from_opcode(opcode: u8) -> Option<EvmIrStackOp> {
    match opcode {
        op::POP => Some(EvmIrStackOp::Pop),
        op::DUP1..=op::DUP16 => EvmIrStackOp::dup(opcode - op::DUP1 + 1),
        op::SWAP1..=op::SWAP16 => EvmIrStackOp::swap(opcode - op::SWAP1 + 1),
        _ => None,
    }
}

fn opcode_from_stack_op(op: EvmIrStackOp) -> u8 {
    match op {
        EvmIrStackOp::Dup(n) => op::dup(n),
        EvmIrStackOp::Swap(n) => op::swap(n),
        EvmIrStackOp::Pop => op::POP,
    }
}

fn original_block_index(label: &str) -> Option<usize> {
    label.strip_prefix("bb")?.parse().ok()
}

fn next_block(
    module: &EvmIrModule,
    block: crate::backend::evm::EvmIrBlockId,
) -> Option<crate::backend::evm::EvmIrBlockId> {
    let next = block.index() + 1;
    (next < module.blocks.len()).then(|| crate::backend::evm::EvmIrBlockId::from_usize(next))
}

fn label_for_block<C>(
    module: &EvmIrModule,
    block: crate::backend::evm::EvmIrBlockId,
    labels: &mut Vec<Option<Label>>,
    context: &mut C,
) -> Label
where
    C: StructuredAsmContext,
{
    let original =
        original_block_index(&module.blocks[block].label).unwrap_or_else(|| block.index());
    if original >= labels.len() {
        labels.resize_with(original + 1, || None);
    }
    *labels[original].get_or_insert_with(|| context.new_label())
}

#[derive(Clone, Debug, Default)]
struct StructuredAsmBlock {
    label: Option<Label>,
    cold: bool,
    instructions: Vec<AsmInst>,
}

/// Linear EVM assembly program used by the final assembler.
///
/// This is the MC-like layer below structured assembler blocks: a label-bearing opcode
/// stream with unresolved PUSH operands, ready for final assembly into bytecode.
#[derive(Clone, Debug, Default)]
pub(in crate::backend::evm) struct EvmAsmProgram {
    pub(in crate::backend::evm) instructions: Vec<AsmInst>,
}