Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
13c22af
[sway-ir]: Add switch instruction
vaivaswatha Dec 4, 2025
cf2e018
(incomplete) introduce Switch in abstract asm
vaivaswatha Dec 12, 2025
36a8165
Merge branch 'master' of github.com:FuelLabs/sway into vaivaswatha/ir…
vaivaswatha Jan 5, 2026
1afcac8
Assembly generation for switch
vaivaswatha Jan 7, 2026
d032d1a
Merge branch 'master' of github.com:FuelLabs/sway into vaivaswatha/ir…
vaivaswatha Jan 7, 2026
fb75622
handle switch in Block::successors
vaivaswatha Jan 8, 2026
3e8989a
Get the test harness to build IR tests
vaivaswatha Jan 15, 2026
d8bc591
fix some warnings
vaivaswatha Jan 15, 2026
fc15ba9
bugfixes, the IR test now passes
vaivaswatha Jan 19, 2026
547c3d9
update IR test to test all cases for that example
vaivaswatha Jan 20, 2026
e4ba0e1
Handle descriminant being lesser than min case value
vaivaswatha Jan 20, 2026
84ebbde
Test descriminant being holes in the jump table
vaivaswatha Jan 20, 2026
4513056
Optimization and test for exhaustive switches
vaivaswatha Jan 21, 2026
4356eb9
Merge branch 'master' of github.com:FuelLabs/sway into vaivaswatha/ir…
vaivaswatha Jan 21, 2026
248be3f
typo and clippy fixes
vaivaswatha Jan 21, 2026
a2fcaca
fix Cargo.toml fmt
vaivaswatha Jan 21, 2026
0da711f
Another toml fix
vaivaswatha Jan 21, 2026
d0701fb
cursor comment: Missing validation for duplicate case values in switches
vaivaswatha Jan 24, 2026
3c776ab
Address comment on empty switches
vaivaswatha Jan 24, 2026
bc54961
Add a check for same destination block from multiple cases
vaivaswatha Jan 24, 2026
293a3f2
handle switches in replace_successor
vaivaswatha Jan 24, 2026
802d07a
Merge branch 'master' into vaivaswatha/ir_switch
vaivaswatha Jan 26, 2026
cf094b0
Merge branch 'master' into vaivaswatha/ir_switch
vaivaswatha Feb 3, 2026
8ea5624
Handle switch in `get_succ_params_mut`
vaivaswatha Feb 3, 2026
a6d10f5
Merge branch 'master' into vaivaswatha/ir_switch
ironcev Feb 16, 2026
c85baaf
Update Cargo.lock
ironcev Feb 16, 2026
9988e37
Merge branch 'master' into vaivaswatha/ir_switch
ironcev Feb 17, 2026
da29f71
Merge branch 'master' into vaivaswatha/ir_switch
ironcev Feb 19, 2026
f7605f3
Typos, comments and renamings
ironcev Mar 2, 2026
2c40750
Verifier and printer nitpicks
ironcev Mar 2, 2026
faca067
Extend `simplify_cfg` tests and make discirminant non-const
ironcev Mar 2, 2026
2fa5d55
Fix fmt issues
ironcev Mar 2, 2026
af91c69
Refactor copy-pastes in `TextContext::run` and capture bytecode perf
ironcev Mar 2, 2026
b58f86e
Add ASM printing capability to "ir_run" tests
ironcev Mar 3, 2026
208c958
Improve printing of ASM and bytecode
ironcev Mar 3, 2026
f982849
Fix clippy issues
ironcev Mar 3, 2026
62f3856
Add additional tests
ironcev Mar 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ serde_with.workspace = true
sway-core.workspace = true
sway-error.workspace = true
sway-features.workspace = true
sway-ir.workspace = true
sway-types.workspace = true
sway-utils.workspace = true
tar.workspace = true
Expand Down
85 changes: 84 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ use sway_core::{
write_dwarf, BuildTarget, Engines, FinalizedEntry, LspConfig,
};
use sway_core::{namespace::Package, Observer};
use sway_core::{set_bytecode_configurables_offset, DbgGeneration, IrCli, PrintAsm};
use sway_core::{
set_bytecode_configurables_offset, BuildConfig, DbgGeneration, IrCli, PanicOccurrences,
PanickingCallOccurrences, PrintAsm,
};
use sway_error::{error::CompileError, handler::Handler, warning::CompileWarning};
use sway_features::ExperimentalFeatures;
use sway_types::{Ident, ProgramId, Span, Spanned};
Expand Down Expand Up @@ -1940,6 +1943,84 @@ pub fn compile(
Ok(compiled_package)
}

/// Compiles the given Sway-IR script.
pub fn compile_ir(
ir_file: &Path,
engines: &Engines,
build_config: Option<&BuildConfig>,
source_map: &mut SourceMap,
experimental: ExperimentalFeatures,
) -> Result<BuiltPackageBytecode> {
let source = fs::read_to_string(ir_file)
.with_context(|| format!("Failed to read Sway-IR file: {}", ir_file.display()))?;

let sway_ir = sway_ir::parser::parse(
&source,
engines.se(),
experimental,
build_config
.map(|config| config.backtrace.into())
.unwrap_or_default(),
)?;

let handler = Handler::default();
let asm = sway_core::asm_generation::from_ir::compile_ir_context_to_finalized_asm(
&handler,
&sway_ir,
build_config,
);

let fail = |handler: Handler| {
let (errors, warnings, infos) = handler.consume();
print_on_failure(engines.se(), false, &infos, &warnings, &errors, false);
bail!("Failed to compile {}", ir_file.display());
};

let finalized_asm = match asm {
Err(_) => return fail(handler),
Ok(asm) => asm,
};

let mut compiled_asm = sway_core::CompiledAsm {
finalized_asm,
panic_occurrences: PanicOccurrences::default(),
panicking_call_occurrences: PanickingCallOccurrences::default(),
};

let entries = compiled_asm
.finalized_asm
.entries
.iter()
.map(|finalized_entry| PkgEntry::from_finalized_entry(finalized_entry, engines))
.collect::<anyhow::Result<_>>()?;

let bytecode_res = sway_core::asm_to_bytecode(
&handler,
&mut compiled_asm,
source_map,
engines.se(),
build_config.unwrap_or(&BuildConfig::dummy_for_asm_generation()),
);

let errored = handler.has_errors();

let compiled = match bytecode_res {
Ok(compiled) if !errored => compiled,
_ => return fail(handler),
};

let (_, _warnings, _infos) = handler.consume();
// TODO: Print infos and warnings?
// TODO: Set configurables offset metadata if needed.

let bytecode = BuiltPackageBytecode {
bytes: compiled.bytecode,
entries,
};

Ok(bytecode)
}

/// Reports assembly information for a compiled package to an external `dyno` process through `stdout`.
fn report_assembly_information(
compiled_asm: &sway_core::CompiledAsm,
Expand Down Expand Up @@ -1982,6 +2063,8 @@ fn report_assembly_information(
}
}

sway_core::asm_generation::Datum::WordArray(words) => (words.len() * 8) as u64,

sway_core::asm_generation::Datum::Collection(items) => {
items.iter().map(calculate_entry_size).sum()
}
Expand Down
16 changes: 16 additions & 0 deletions sway-core/src/asm_generation/evm/evm_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ impl<'ir, 'eng> EvmAsmBuilder<'ir, 'eng> {
} => {
self.compile_conditional_branch(handler, cond_value, true_block, false_block)?
}
InstOp::Switch {
discriminant,
cases,
default,
} => self.compile_switch(handler, instr_val, discriminant, cases, default)?,
InstOp::ContractCall {
params,
coins,
Expand Down Expand Up @@ -444,6 +449,17 @@ impl<'ir, 'eng> EvmAsmBuilder<'ir, 'eng> {
todo!();
}

fn compile_switch(
&mut self,
handler: &Handler,
instr_val: &Value,
discriminant: &Value,
cases: &[(u64, BranchToWithArgs)],
default: &Option<BranchToWithArgs>,
) -> Result<(), ErrorEmitted> {
todo!();
}

fn compile_branch_to_phi_value(&mut self, to_block: &BranchToWithArgs) {
todo!();
}
Expand Down
10 changes: 10 additions & 0 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::BuildConfig;

use etk_asm::asm::Assembler;
use fuel_vm::fuel_asm::{Imm06, Imm12, Imm18, Imm24, Instruction, RegId};
use itertools::Itertools;
use sway_error::error::CompileError;
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::span::Span;
Expand Down Expand Up @@ -323,6 +324,15 @@ fn to_bytecode_mut(
}
println!("\"");
}
Datum::WordArray(ws) => {
print!(".words as hex (");
Itertools::intersperse(
ws.iter().map(|w| format!("{:02X?}", w.to_be_bytes())),
", ".to_string(),
)
.for_each(|item| print!("{item}"));
println!("), len i{}", ws.len());
}
Datum::Slice(bs) => {
print!(".slice as hex ({bs:02X?}), len i{}, as ascii \"", bs.len());

Expand Down
Loading
Loading