diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 231326e353e..1b78fb2ef3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -131,7 +131,9 @@ jobs: uses: peaceiris/actions-mdbook@v1 with: mdbook-version: "0.4.15" - - name: Emit book logs to tmp.txt, fail if build logs contain 'ERROR' + - name: Build the Sway Book + run: MDBOOK_preprocessor__FORC_documenter__STRICT="true" mdbook build docs/book + - name: Emit Sway Book logs to tmp.txt, fail if build logs contain 'ERROR' run: | MDBOOK_preprocessor__FORC_documenter__STRICT="true" mdbook build docs/book &> tmp.txt if cat tmp.txt | grep 'ERROR' @@ -141,18 +143,8 @@ jobs: else rm tmp.txt && exit 0 fi - - - name: Emit reference logs to tmp.txt, fail if build logs contain 'ERROR' - run: | - mdbook build docs/reference - mdbook build docs/reference &> tmp.txt - if cat tmp.txt | grep 'ERROR' - then - cat tmp.txt - rm tmp.txt && exit 1 - else - rm tmp.txt && exit 0 - fi + - name: Build the Sway Reference + run: mdbook build docs/reference build-forc-test-project: runs-on: ubuntu-latest @@ -255,7 +247,7 @@ jobs: runs-on: ubuntu-latest services: fuel-core: - image: ghcr.io/fuellabs/fuel-core:v0.16.1 + image: ghcr.io/fuellabs/fuel-core:v0.17.0 ports: - 4000:4000 steps: diff --git a/Cargo.lock b/Cargo.lock index a3935820e39..b0645847adc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1825,9 +1825,9 @@ dependencies = [ [[package]] name = "fuel-asm" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f56306747294a7ec2da07e9be050783cf6adbfd3d73bf820a66bde16336a309" +checksum = "7df7be33ffb99e92e8c76a25de7d6460566822420ee86e59d2214eec58e90a1d" dependencies = [ "fuel-types", "serde", @@ -1835,9 +1835,9 @@ dependencies = [ [[package]] name = "fuel-core-chain-config" -version = "0.16.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0da777465f1d427d52b5e0873fbc79f393229088cf96dc0cf98fb4cbb9d41243" +checksum = "c9b5df3fe3472fabc003f230fffcd4312baaede23475a9f8934b421a4c2259de" dependencies = [ "anyhow", "bech32 0.9.1", @@ -1855,9 +1855,9 @@ dependencies = [ [[package]] name = "fuel-core-client" -version = "0.16.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e30353fd185786c82486aeff551b903e811060eeecdbb766e46fb236e6deadd" +checksum = "e0d0b14e85e962dd55fe363092084e9eac8975833f1c905e88f451fd7bae0fea" dependencies = [ "anyhow", "cynic", @@ -1873,13 +1873,14 @@ dependencies = [ "serde_json", "tai64", "thiserror", + "tracing", ] [[package]] name = "fuel-core-storage" -version = "0.16.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66f31e83c3377c33aa9a49e00fc3fa48003f6e1d26344d503ee3342dbdc83ba3" +checksum = "1e3b6ccd404322e1cb19e19b795835af3416938217850e496d36131cf8ca74f8" dependencies = [ "anyhow", "fuel-core-types", @@ -1889,9 +1890,9 @@ dependencies = [ [[package]] name = "fuel-core-types" -version = "0.16.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53a4cdc283d3905cb537cb6500d9e38edee8338ecce1447f9a9742cb77fade6e" +checksum = "093e4fc1616f23f67d57610051d946b12034329e7c86b546c9db6f0bb889479f" dependencies = [ "anyhow", "derive_more", @@ -1905,9 +1906,9 @@ dependencies = [ [[package]] name = "fuel-crypto" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eedf87b364293690782968202c247fd08470e3e17d2e2f42489b46d3f5c966e7" +checksum = "46f09781cbc54fc422456e1bc0c7b3101d879d848cc13992aeae60d088cf1bbd" dependencies = [ "borrown", "coins-bip32", @@ -1995,9 +1996,9 @@ dependencies = [ [[package]] name = "fuel-merkle" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9878202ea2f874de70e66b0349af8b1ad8300dcd2dec216920bb44850c5000ba" +checksum = "c54de90857ac30eee21c9bbc60e0e6701c0d21913418f6c8be88d0ea8d1cded9" dependencies = [ "digest 0.10.6", "fuel-storage", @@ -2009,15 +2010,15 @@ dependencies = [ [[package]] name = "fuel-storage" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d076e82c7c5d1d15cd983f744edd0bcb2552fab3428bde403c32fca07884212" +checksum = "b93354a7d1ba3de35a3073237eebf492df9c76a4bf4ccfc4af8ed76fee2b164f" [[package]] name = "fuel-tx" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f692bcc2f6bfaf2cdadef553462fb23936865af5c646acf3fedfd6e0c136df3d" +checksum = "f0da1d6c6886e670e0c37da53f7b73935712ea346d216d275d0e72b81c2eb982" dependencies = [ "derivative", "fuel-asm", @@ -2033,9 +2034,9 @@ dependencies = [ [[package]] name = "fuel-types" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7910fa716d6092210481a386ad29eeb6193fd911f0c6785af3c8b033817b05b" +checksum = "b6a893af21a88f56f912964c734d007c9d46224146e3e9877262a39e25135fa3" dependencies = [ "rand", "serde", @@ -2043,9 +2044,9 @@ dependencies = [ [[package]] name = "fuel-vm" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0be0f72a67691df7f0929dee984eafbc47ded6b58d2c7d15dc85d4f2612e2b92" +checksum = "dfbc26d8221b5d40f4ac24a6d566475416665443adae5b6a9cedc3712d7bc185" dependencies = [ "bitflags", "fuel-asm", @@ -2063,10 +2064,28 @@ dependencies = [ "tracing", ] +[[package]] +name = "fuels-code-gen" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0f4dc5d3c341dc45798913c8d5334d13f05cc62b4cb7fb9c5e05c33cc160546" +dependencies = [ + "Inflector", + "fuel-abi-types 0.2.0", + "itertools", + "lazy_static", + "proc-macro2", + "quote", + "regex", + "serde_json", + "syn", +] + [[package]] name = "fuels-core" -version = "0.34.0" -source = "git+https://github.com/FuelLabs/fuels-rs.git?branch=Voxelot/fuel-core-0.16#b7cb1b17c07e014c7fb656a5f0f3dcb4b99e9a39" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "954598e797e94f530af81ecb2bc5fcf93a1d787abd355823580f941f193c6b76" dependencies = [ "fuel-tx", "fuel-types", @@ -2079,11 +2098,13 @@ dependencies = [ [[package]] name = "fuels-macros" -version = "0.34.0" -source = "git+https://github.com/FuelLabs/fuels-rs.git?branch=Voxelot/fuel-core-0.16#b7cb1b17c07e014c7fb656a5f0f3dcb4b99e9a39" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75383dfdb605a98c7d18a610a97d002c25157004d41387c67d9ae61d8d4d9bb2" dependencies = [ "Inflector", "fuel-abi-types 0.2.0", + "fuels-code-gen", "itertools", "lazy_static", "proc-macro2", @@ -2096,8 +2117,9 @@ dependencies = [ [[package]] name = "fuels-signers" -version = "0.34.0" -source = "git+https://github.com/FuelLabs/fuels-rs.git?branch=Voxelot/fuel-core-0.16#b7cb1b17c07e014c7fb656a5f0f3dcb4b99e9a39" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f192607ddd1a138228a9984822c6b5b77841ce2427b6f04b91ccb96c8f297e8" dependencies = [ "async-trait", "bytes", @@ -2123,8 +2145,9 @@ dependencies = [ [[package]] name = "fuels-types" -version = "0.34.0" -source = "git+https://github.com/FuelLabs/fuels-rs.git?branch=Voxelot/fuel-core-0.16#b7cb1b17c07e014c7fb656a5f0f3dcb4b99e9a39" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a522e7a3cbe922d80d15e1495aa74fa19aa7a8d6ece325f1701db7ac261ff820" dependencies = [ "bech32 0.9.1", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 90e85bf5df9..8390775d82e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,12 +33,11 @@ exclude = [ debug = 2 [workspace.dependencies] -fuel-asm = "0.25" -fuel-crypto = "0.25" -fuel-tx = "0.25" -fuel-core-client = "0.16" -fuel-vm = "0.25" -fuels-core = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" } -fuels-signers = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" } -fuels-types = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" } -fuels = { git = "https://github.com/FuelLabs/fuels-rs.git", branch = "Voxelot/fuel-core-0.16" } +fuel-asm = "0.26" +fuel-crypto = "0.26" +fuel-tx = "0.26" +fuel-core-client = "0.17" +fuel-vm = "0.26" +fuels-core = "0.36.0" +fuels-signers = "0.36.0" +fuels-types = "0.36.0" diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index 2bb00d4f0c2..5d7ad8154d1 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -76,6 +76,7 @@ - [forc client](./forc/plugins/forc_client/index.md) - [forc deploy](./forc/plugins/forc_client/forc_deploy.md) - [forc run](./forc/plugins/forc_client/forc_run.md) + - [forc doc](./forc/plugins/forc_doc.md) - [forc explore](./forc/plugins/forc_explore.md) - [forc fmt](./forc/plugins/forc_fmt.md) - [forc lsp](./forc/plugins/forc_lsp.md) diff --git a/forc-test/src/lib.rs b/forc-test/src/lib.rs index b824fd8cbae..eab267f4492 100644 --- a/forc-test/src/lib.rs +++ b/forc-test/src/lib.rs @@ -4,7 +4,7 @@ use forc_pkg as pkg; use fuel_tx as tx; use fuel_vm::checked_transaction::builder::TransactionBuilderExt; use fuel_vm::gas::GasCosts; -use fuel_vm::{self as vm, prelude::Opcode}; +use fuel_vm::{self as vm, fuel_asm, prelude::Instruction}; use pkg::TestPassCondition; use pkg::{Built, BuiltPackage, CONTRACT_ID_CONSTANT_NAME}; use rand::{distributions::Standard, prelude::Distribution, Rng, SeedableRng}; @@ -501,7 +501,7 @@ fn run_tests(built: BuiltTests) -> anyhow::Result { fn patch_test_bytecode(bytecode: &[u8], test_offset: u32) -> std::borrow::Cow<[u8]> { // TODO: Standardize this or add metadata to bytecode. const PROGRAM_START_INST_OFFSET: u32 = 6; - const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Opcode::LEN; + const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Instruction::SIZE; // If our desired entry point is the program start, no need to jump. if test_offset == PROGRAM_START_INST_OFFSET { @@ -509,7 +509,7 @@ fn patch_test_bytecode(bytecode: &[u8], test_offset: u32) -> std::borrow::Cow<[u } // Create the jump instruction and splice it into the bytecode. - let ji = Opcode::JI(test_offset); + let ji = fuel_asm::op::ji(test_offset); let ji_bytes = ji.to_bytes(); let start = PROGRAM_START_BYTE_OFFSET; let end = start + ji_bytes.len(); diff --git a/forc/src/cli/commands/parse_bytecode.rs b/forc/src/cli/commands/parse_bytecode.rs index 41743496f31..bc6972f7cd7 100644 --- a/forc/src/cli/commands/parse_bytecode.rs +++ b/forc/src/cli/commands/parse_bytecode.rs @@ -19,15 +19,10 @@ pub(crate) fn exec(command: Command) -> Result<()> { .map_err(|_| anyhow!("{}: file not found", command.file_path))?; let mut buffer = vec![0; metadata.len() as usize]; f.read_exact(&mut buffer).expect("buffer overflow"); - let mut instructions = vec![]; - for i in (0..buffer.len()).step_by(4) { - let raw = &buffer[i..i + 4]; - unsafe { - let op = fuel_asm::Opcode::from_bytes_unchecked(raw); - instructions.push((raw, op)); - }; - } + let instructions = fuel_asm::from_bytes(buffer.iter().cloned()) + .zip(buffer.chunks(fuel_asm::Instruction::SIZE)); + let mut table = term_table::Table::new(); table.separate_rows = false; table.add_row(Row::new(vec![ @@ -38,34 +33,36 @@ pub(crate) fn exec(command: Command) -> Result<()> { TableCell::new("notes"), ])); table.style = term_table::TableStyle::empty(); - for (word_ix, instruction) in instructions.iter().enumerate() { - use fuel_asm::Opcode::*; - let notes = match instruction.1 { - JI(num) => format!("jump to byte {}", num * 4), - JNEI(_, _, num) => format!("conditionally jump to byte {}", num * 4), - JNZI(_, num) => format!("conditionally jump to byte {}", num * 4), - Undefined if word_ix == 2 || word_ix == 3 => { - let parsed_raw = u32::from_be_bytes([ - instruction.0[0], - instruction.0[1], - instruction.0[2], - instruction.0[3], - ]); + for (word_ix, (result, raw)) in instructions.enumerate() { + use fuel_asm::Instruction; + let notes = match result { + Ok(Instruction::JI(ji)) => format!("jump to byte {}", u32::from(ji.imm24()) * 4), + Ok(Instruction::JNEI(jnei)) => { + format!("conditionally jump to byte {}", u32::from(jnei.imm12()) * 4) + } + Ok(Instruction::JNZI(jnzi)) => { + format!("conditionally jump to byte {}", u32::from(jnzi.imm18()) * 4) + } + Err(fuel_asm::InvalidOpcode) if word_ix == 2 || word_ix == 3 => { + let parsed_raw = u32::from_be_bytes([raw[0], raw[1], raw[2], raw[3]]); format!( "data section offset {} ({})", if word_ix == 2 { "lo" } else { "hi" }, parsed_raw ) } - _ => "".into(), + Ok(_) | Err(fuel_asm::InvalidOpcode) => "".into(), }; table.add_row(Row::new(vec![ TableCell::new_with_alignment(word_ix, 1, Alignment::Right), TableCell::new(word_ix * 4), - TableCell::new(format!("{:?}", instruction.1)), + TableCell::new(match result { + Ok(inst) => format!("{inst:?}"), + Err(err) => format!("{err:?}"), + }), TableCell::new(format!( "{:02x} {:02x} {:02x} {:02x}", - instruction.0[0], instruction.0[1], instruction.0[2], instruction.0[3], + raw[0], raw[1], raw[2], raw[3], )), TableCell::new(notes), ])); diff --git a/sway-core/src/asm_generation/finalized_asm.rs b/sway-core/src/asm_generation/finalized_asm.rs index cf720caf47a..e6c14eead3c 100644 --- a/sway-core/src/asm_generation/finalized_asm.rs +++ b/sway-core/src/asm_generation/finalized_asm.rs @@ -13,7 +13,7 @@ use sway_error::error::CompileError; use sway_types::span::Span; use either::Either; -use std::{collections::BTreeMap, fmt, io::Read}; +use std::{collections::BTreeMap, fmt}; /// Represents an ASM set which has had register allocation, jump elimination, and optimization /// applied to it @@ -143,14 +143,13 @@ fn to_bytecode_mut( if ops.len() > 1 { buf.resize(buf.len() + ((ops.len() - 1) * 4), 0); } - for mut op in ops { + for op in ops { if let Some(span) = &span { source_map.insert(half_word_ix, span); } let read_range_upper_bound = core::cmp::min(half_word_ix * 4 + std::mem::size_of_val(&op), buf.len()); - op.read_exact(&mut buf[half_word_ix * 4..read_range_upper_bound]) - .expect("Failed to write to in-memory buffer."); + buf[half_word_ix * 4..read_range_upper_bound].copy_from_slice(&op.to_bytes()); half_word_ix += 1; } } diff --git a/sway-core/src/asm_lang/allocated_ops.rs b/sway-core/src/asm_lang/allocated_ops.rs index c14dd6e5e96..e5905f23363 100644 --- a/sway-core/src/asm_lang/allocated_ops.rs +++ b/sway-core/src/asm_lang/allocated_ops.rs @@ -15,7 +15,7 @@ use crate::{ compiler_constants::DATA_SECTION_REGISTER, data_section::{DataId, DataSection}, }, - fuel_prelude::fuel_asm::{self, Opcode as VmOp}, + fuel_prelude::fuel_asm::{self, op}, }; use either::Either; use std::fmt::{self, Write}; @@ -43,10 +43,10 @@ impl fmt::Display for AllocatedRegister { } impl AllocatedRegister { - fn to_register_id(&self) -> fuel_asm::RegisterId { + fn to_reg_id(&self) -> fuel_asm::RegId { match self { - AllocatedRegister::Allocated(a) => (a + 16) as fuel_asm::RegisterId, - AllocatedRegister::Constant(constant) => constant.to_register_id(), + AllocatedRegister::Allocated(a) => fuel_asm::RegId::new(a + 16), + AllocatedRegister::Constant(constant) => constant.to_reg_id(), } } } @@ -453,149 +453,122 @@ impl AllocatedOp { &self, offset_to_data_section: u64, data_section: &mut DataSection, - ) -> Either, DoubleWideData> { + ) -> Either, DoubleWideData> { use AllocatedOpcode::*; Either::Left(vec![match &self.opcode { /* Arithmetic/Logic (ALU) Instructions */ - ADD(a, b, c) => VmOp::ADD(a.to_register_id(), b.to_register_id(), c.to_register_id()), - ADDI(a, b, c) => VmOp::ADDI(a.to_register_id(), b.to_register_id(), c.value), - AND(a, b, c) => VmOp::AND(a.to_register_id(), b.to_register_id(), c.to_register_id()), - ANDI(a, b, c) => VmOp::ANDI(a.to_register_id(), b.to_register_id(), c.value), - DIV(a, b, c) => VmOp::DIV(a.to_register_id(), b.to_register_id(), c.to_register_id()), - DIVI(a, b, c) => VmOp::DIVI(a.to_register_id(), b.to_register_id(), c.value), - EQ(a, b, c) => VmOp::EQ(a.to_register_id(), b.to_register_id(), c.to_register_id()), - EXP(a, b, c) => VmOp::EXP(a.to_register_id(), b.to_register_id(), c.to_register_id()), - EXPI(a, b, c) => VmOp::EXPI(a.to_register_id(), b.to_register_id(), c.value), - GT(a, b, c) => VmOp::GT(a.to_register_id(), b.to_register_id(), c.to_register_id()), - LT(a, b, c) => VmOp::LT(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MLOG(a, b, c) => VmOp::MLOG(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MOD(a, b, c) => VmOp::MOD(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MODI(a, b, c) => VmOp::MODI(a.to_register_id(), b.to_register_id(), c.value), - MOVE(a, b) => VmOp::MOVE(a.to_register_id(), b.to_register_id()), - MOVI(a, b) => VmOp::MOVI(a.to_register_id(), b.value), - MROO(a, b, c) => VmOp::MROO(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MUL(a, b, c) => VmOp::MUL(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MULI(a, b, c) => VmOp::MULI(a.to_register_id(), b.to_register_id(), c.value), - NOOP => VmOp::NOOP, - NOT(a, b) => VmOp::NOT(a.to_register_id(), b.to_register_id()), - OR(a, b, c) => VmOp::OR(a.to_register_id(), b.to_register_id(), c.to_register_id()), - ORI(a, b, c) => VmOp::ORI(a.to_register_id(), b.to_register_id(), c.value), - SLL(a, b, c) => VmOp::SLL(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SLLI(a, b, c) => VmOp::SLLI(a.to_register_id(), b.to_register_id(), c.value), - SRL(a, b, c) => VmOp::SRL(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SRLI(a, b, c) => VmOp::SRLI(a.to_register_id(), b.to_register_id(), c.value), - SUB(a, b, c) => VmOp::SUB(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SUBI(a, b, c) => VmOp::SUBI(a.to_register_id(), b.to_register_id(), c.value), - XOR(a, b, c) => VmOp::XOR(a.to_register_id(), b.to_register_id(), c.to_register_id()), - XORI(a, b, c) => VmOp::XORI(a.to_register_id(), b.to_register_id(), c.value), + ADD(a, b, c) => op::ADD::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + ADDI(a, b, c) => op::ADDI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + AND(a, b, c) => op::AND::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + ANDI(a, b, c) => op::ANDI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + DIV(a, b, c) => op::DIV::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + DIVI(a, b, c) => op::DIVI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + EQ(a, b, c) => op::EQ::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + EXP(a, b, c) => op::EXP::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + EXPI(a, b, c) => op::EXPI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + GT(a, b, c) => op::GT::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + LT(a, b, c) => op::LT::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MLOG(a, b, c) => op::MLOG::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MOD(a, b, c) => op::MOD::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MODI(a, b, c) => op::MODI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + MOVE(a, b) => op::MOVE::new(a.to_reg_id(), b.to_reg_id()).into(), + MOVI(a, b) => op::MOVI::new(a.to_reg_id(), b.value.into()).into(), + MROO(a, b, c) => op::MROO::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MUL(a, b, c) => op::MUL::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MULI(a, b, c) => op::MULI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + NOOP => op::NOOP::new().into(), + NOT(a, b) => op::NOT::new(a.to_reg_id(), b.to_reg_id()).into(), + OR(a, b, c) => op::OR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + ORI(a, b, c) => op::ORI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + SLL(a, b, c) => op::SLL::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SLLI(a, b, c) => op::SLLI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + SRL(a, b, c) => op::SRL::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SRLI(a, b, c) => op::SRLI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + SUB(a, b, c) => op::SUB::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SUBI(a, b, c) => op::SUBI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + XOR(a, b, c) => op::XOR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + XORI(a, b, c) => op::XORI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), /* Control Flow Instructions */ - JMP(a) => VmOp::JMP(a.to_register_id()), - JI(a) => VmOp::JI(a.value), - JNE(a, b, c) => VmOp::JNE(a.to_register_id(), b.to_register_id(), c.to_register_id()), - JNEI(a, b, c) => VmOp::JNEI(a.to_register_id(), b.to_register_id(), c.value), - JNZI(a, b) => VmOp::JNZI(a.to_register_id(), b.value), - RET(a) => VmOp::RET(a.to_register_id()), + JMP(a) => op::JMP::new(a.to_reg_id()).into(), + JI(a) => op::JI::new(a.value.into()).into(), + JNE(a, b, c) => op::JNE::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + JNEI(a, b, c) => op::JNEI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + JNZI(a, b) => op::JNZI::new(a.to_reg_id(), b.value.into()).into(), + RET(a) => op::RET::new(a.to_reg_id()).into(), /* Memory Instructions */ - ALOC(a) => VmOp::ALOC(a.to_register_id()), - CFEI(a) => VmOp::CFEI(a.value), - CFSI(a) => VmOp::CFSI(a.value), - LB(a, b, c) => VmOp::LB(a.to_register_id(), b.to_register_id(), c.value), - LW(a, b, c) => VmOp::LW(a.to_register_id(), b.to_register_id(), c.value), - MCL(a, b) => VmOp::MCL(a.to_register_id(), b.to_register_id()), - MCLI(a, b) => VmOp::MCLI(a.to_register_id(), b.value), - MCP(a, b, c) => VmOp::MCP(a.to_register_id(), b.to_register_id(), c.to_register_id()), - MCPI(a, b, c) => VmOp::MCPI(a.to_register_id(), b.to_register_id(), c.value), - MEQ(a, b, c, d) => VmOp::MEQ( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - SB(a, b, c) => VmOp::SB(a.to_register_id(), b.to_register_id(), c.value), - SW(a, b, c) => VmOp::SW(a.to_register_id(), b.to_register_id(), c.value), + ALOC(a) => op::ALOC::new(a.to_reg_id()).into(), + CFEI(a) => op::CFEI::new(a.value.into()).into(), + CFSI(a) => op::CFSI::new(a.value.into()).into(), + LB(a, b, c) => op::LB::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + LW(a, b, c) => op::LW::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + MCL(a, b) => op::MCL::new(a.to_reg_id(), b.to_reg_id()).into(), + MCLI(a, b) => op::MCLI::new(a.to_reg_id(), b.value.into()).into(), + MCP(a, b, c) => op::MCP::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + MCPI(a, b, c) => op::MCPI::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + MEQ(a, b, c, d) => { + op::MEQ::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + SB(a, b, c) => op::SB::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), + SW(a, b, c) => op::SW::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), /* Contract Instructions */ - BAL(a, b, c) => VmOp::BAL(a.to_register_id(), b.to_register_id(), c.to_register_id()), - BHEI(a) => VmOp::BHEI(a.to_register_id()), - BHSH(a, b) => VmOp::BHSH(a.to_register_id(), b.to_register_id()), - BURN(a) => VmOp::BURN(a.to_register_id()), - CALL(a, b, c, d) => VmOp::CALL( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - CB(a) => VmOp::CB(a.to_register_id()), - CCP(a, b, c, d) => VmOp::CCP( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - CROO(a, b) => VmOp::CROO(a.to_register_id(), b.to_register_id()), - CSIZ(a, b) => VmOp::CSIZ(a.to_register_id(), b.to_register_id()), - LDC(a, b, c) => VmOp::LDC(a.to_register_id(), b.to_register_id(), c.to_register_id()), - LOG(a, b, c, d) => VmOp::LOG( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - LOGD(a, b, c, d) => VmOp::LOGD( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - MINT(a) => VmOp::MINT(a.to_register_id()), - RETD(a, b) => VmOp::RETD(a.to_register_id(), b.to_register_id()), - RVRT(a) => VmOp::RVRT(a.to_register_id()), - SMO(a, b, c, d) => VmOp::SMO( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - SCWQ(a, b, c) => VmOp::SCWQ(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SRW(a, b, c) => VmOp::SRW(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SRWQ(a, b, c, d) => VmOp::SRWQ( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - SWW(a, b, c) => VmOp::SWW(a.to_register_id(), b.to_register_id(), c.to_register_id()), - SWWQ(a, b, c, d) => VmOp::SWWQ( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), - TIME(a, b) => VmOp::TIME(a.to_register_id(), b.to_register_id()), - TR(a, b, c) => VmOp::TR(a.to_register_id(), b.to_register_id(), c.to_register_id()), - TRO(a, b, c, d) => VmOp::TRO( - a.to_register_id(), - b.to_register_id(), - c.to_register_id(), - d.to_register_id(), - ), + BAL(a, b, c) => op::BAL::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + BHEI(a) => op::BHEI::new(a.to_reg_id()).into(), + BHSH(a, b) => op::BHSH::new(a.to_reg_id(), b.to_reg_id()).into(), + BURN(a) => op::BURN::new(a.to_reg_id()).into(), + CALL(a, b, c, d) => { + op::CALL::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + CB(a) => op::CB::new(a.to_reg_id()).into(), + CCP(a, b, c, d) => { + op::CCP::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + CROO(a, b) => op::CROO::new(a.to_reg_id(), b.to_reg_id()).into(), + CSIZ(a, b) => op::CSIZ::new(a.to_reg_id(), b.to_reg_id()).into(), + LDC(a, b, c) => op::LDC::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + LOG(a, b, c, d) => { + op::LOG::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + LOGD(a, b, c, d) => { + op::LOGD::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + MINT(a) => op::MINT::new(a.to_reg_id()).into(), + RETD(a, b) => op::RETD::new(a.to_reg_id(), b.to_reg_id()).into(), + RVRT(a) => op::RVRT::new(a.to_reg_id()).into(), + SMO(a, b, c, d) => { + op::SMO::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + SCWQ(a, b, c) => op::SCWQ::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SRW(a, b, c) => op::SRW::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SRWQ(a, b, c, d) => { + op::SRWQ::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + SWW(a, b, c) => op::SWW::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + SWWQ(a, b, c, d) => { + op::SWWQ::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } + TIME(a, b) => op::TIME::new(a.to_reg_id(), b.to_reg_id()).into(), + TR(a, b, c) => op::TR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + TRO(a, b, c, d) => { + op::TRO::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id(), d.to_reg_id()).into() + } /* Cryptographic Instructions */ - ECR(a, b, c) => VmOp::ECR(a.to_register_id(), b.to_register_id(), c.to_register_id()), - K256(a, b, c) => VmOp::K256(a.to_register_id(), b.to_register_id(), c.to_register_id()), - S256(a, b, c) => VmOp::S256(a.to_register_id(), b.to_register_id(), c.to_register_id()), + ECR(a, b, c) => op::ECR::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + K256(a, b, c) => op::K256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), + S256(a, b, c) => op::S256::new(a.to_reg_id(), b.to_reg_id(), c.to_reg_id()).into(), /* Other Instructions */ - FLAG(a) => VmOp::FLAG(a.to_register_id()), - GM(a, b) => VmOp::GM(a.to_register_id(), b.value), - GTF(a, b, c) => VmOp::GTF(a.to_register_id(), b.to_register_id(), c.value), + FLAG(a) => op::FLAG::new(a.to_reg_id()).into(), + GM(a, b) => op::GM::new(a.to_reg_id(), b.value.into()).into(), + GTF(a, b, c) => op::GTF::new(a.to_reg_id(), b.to_reg_id(), c.value.into()).into(), /* Non-VM Instructions */ BLOB(a) => { return Either::Left( - std::iter::repeat(VmOp::NOOP) + std::iter::repeat(op::NOOP::new().into()) .take(a.value as usize) .collect(), ) @@ -603,15 +576,16 @@ impl AllocatedOp { DataSectionOffsetPlaceholder => { return Either::Right(offset_to_data_section.to_be_bytes()) } - DataSectionRegisterLoadPlaceholder => VmOp::LW( - DATA_SECTION_REGISTER as fuel_asm::RegisterId, - ConstantRegister::InstructionStart.to_register_id(), - 1, - ), + DataSectionRegisterLoadPlaceholder => op::LW::new( + fuel_asm::RegId::new(DATA_SECTION_REGISTER), + ConstantRegister::InstructionStart.to_reg_id(), + 1.into(), + ) + .into(), LWDataId(a, b) => { return Either::Left(realize_lw(a, b, data_section, offset_to_data_section)) } - Undefined => VmOp::Undefined, + Undefined => unreachable!("Sway cannot generate undefined ASM opcodes"), }]) } } @@ -625,7 +599,7 @@ fn realize_lw( data_id: &DataId, data_section: &mut DataSection, offset_to_data_section: u64, -) -> Vec { +) -> Vec { // all data is word-aligned right now, and `offset_to_id` returns the offset in bytes let offset_bytes = data_section.data_id_to_offset(data_id) as u64; let offset_words = offset_bytes / 8; @@ -656,17 +630,21 @@ fn realize_lw( offset_to_data_section, )); // add $is to the pointer since it is relative to the data section - buf.push(VmOp::ADD( - dest.to_register_id(), - dest.to_register_id(), - ConstantRegister::InstructionStart.to_register_id(), - )); + buf.push( + fuel_asm::op::ADD::new( + dest.to_reg_id(), + dest.to_reg_id(), + ConstantRegister::InstructionStart.to_reg_id(), + ) + .into(), + ); buf } else { - vec![VmOp::LW( - dest.to_register_id(), - DATA_SECTION_REGISTER as usize, - offset.value, - )] + vec![fuel_asm::op::LW::new( + dest.to_reg_id(), + fuel_asm::RegId::new(DATA_SECTION_REGISTER), + offset.value.into(), + ) + .into()] } } diff --git a/sway-core/src/asm_lang/virtual_register.rs b/sway-core/src/asm_lang/virtual_register.rs index 38abb8b90c7..dd87ced13cf 100644 --- a/sway-core/src/asm_lang/virtual_register.rs +++ b/sway-core/src/asm_lang/virtual_register.rs @@ -72,40 +72,37 @@ pub enum ConstantRegister { use crate::asm_generation::fuel::compiler_constants; impl ConstantRegister { - pub(crate) fn to_register_id(self) -> fuel_asm::RegisterId { - use fuel_vm::consts::*; + pub(crate) fn to_reg_id(self) -> fuel_asm::RegId { use ConstantRegister::*; match self { - Zero => REG_ZERO, - One => REG_ONE, - Overflow => REG_OF, - ProgramCounter => REG_PC, - StackStartPointer => REG_SSP, - StackPointer => REG_SP, - FramePointer => REG_FP, - HeapPointer => REG_HP, - Error => REG_ERR, - GlobalGas => REG_GGAS, - ContextGas => REG_CGAS, - Balance => REG_BAL, - InstructionStart => REG_IS, - ReturnValue => REG_RET, - ReturnLength => REG_RETL, - Flags => REG_FLAG, + Zero => fuel_asm::RegId::ZERO, + One => fuel_asm::RegId::ONE, + Overflow => fuel_asm::RegId::OF, + ProgramCounter => fuel_asm::RegId::PC, + StackStartPointer => fuel_asm::RegId::SSP, + StackPointer => fuel_asm::RegId::SP, + FramePointer => fuel_asm::RegId::FP, + HeapPointer => fuel_asm::RegId::HP, + Error => fuel_asm::RegId::ERR, + GlobalGas => fuel_asm::RegId::GGAS, + ContextGas => fuel_asm::RegId::CGAS, + Balance => fuel_asm::RegId::BAL, + InstructionStart => fuel_asm::RegId::IS, + ReturnValue => fuel_asm::RegId::RET, + ReturnLength => fuel_asm::RegId::RETL, + Flags => fuel_asm::RegId::FLAG, - DataSectionStart => (compiler_constants::DATA_SECTION_REGISTER) as fuel_asm::RegisterId, - CallReturnAddress => { - (compiler_constants::RETURN_ADDRESS_REGISTER) as fuel_asm::RegisterId - } - CallReturnValue => (compiler_constants::RETURN_VALUE_REGISTER) as fuel_asm::RegisterId, - Scratch => (compiler_constants::SCRATCH_REGISTER) as fuel_asm::RegisterId, + DataSectionStart => fuel_asm::RegId::new(compiler_constants::DATA_SECTION_REGISTER), + CallReturnAddress => fuel_asm::RegId::new(compiler_constants::RETURN_ADDRESS_REGISTER), + CallReturnValue => fuel_asm::RegId::new(compiler_constants::RETURN_VALUE_REGISTER), + Scratch => fuel_asm::RegId::new(compiler_constants::SCRATCH_REGISTER), - FuncArg0 => compiler_constants::ARG_REG0 as fuel_asm::RegisterId, - FuncArg1 => compiler_constants::ARG_REG1 as fuel_asm::RegisterId, - FuncArg2 => compiler_constants::ARG_REG2 as fuel_asm::RegisterId, - FuncArg3 => compiler_constants::ARG_REG3 as fuel_asm::RegisterId, - FuncArg4 => compiler_constants::ARG_REG4 as fuel_asm::RegisterId, - FuncArg5 => compiler_constants::ARG_REG5 as fuel_asm::RegisterId, + FuncArg0 => fuel_asm::RegId::new(compiler_constants::ARG_REG0), + FuncArg1 => fuel_asm::RegId::new(compiler_constants::ARG_REG1), + FuncArg2 => fuel_asm::RegId::new(compiler_constants::ARG_REG2), + FuncArg3 => fuel_asm::RegId::new(compiler_constants::ARG_REG3), + FuncArg4 => fuel_asm::RegId::new(compiler_constants::ARG_REG4), + FuncArg5 => fuel_asm::RegId::new(compiler_constants::ARG_REG5), } }