Skip to content

Commit

Permalink
Merge branch 'jjcnn/move-external-modules-to-root' of github.com:Fuel…
Browse files Browse the repository at this point in the history
…Labs/sway into jjcnn/move-external-modules-to-root
  • Loading branch information
jjcnn committed Jan 16, 2025
2 parents 8e72b31 + 5a5ef4b commit 2feb7e2
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 160 deletions.
8 changes: 4 additions & 4 deletions forc-plugins/forc-client/tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ async fn test_simple_deploy() {
node.kill().unwrap();
let expected = vec![DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"02a7e78ef0514b80ab56b409f06f895d8939640b6f6d746fcbb15d3e0c6a1a3b",
"6c1ba5b247a2fc44e7a5166c3518c96f3cd566a4f144483a5310dcc82d08d010",
)
.unwrap(),
proxy: None,
Expand Down Expand Up @@ -416,7 +416,7 @@ async fn test_deploy_submit_only() {
node.kill().unwrap();
let expected = vec![DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"02a7e78ef0514b80ab56b409f06f895d8939640b6f6d746fcbb15d3e0c6a1a3b",
"6c1ba5b247a2fc44e7a5166c3518c96f3cd566a4f144483a5310dcc82d08d010",
)
.unwrap(),
proxy: None,
Expand Down Expand Up @@ -462,12 +462,12 @@ async fn test_deploy_fresh_proxy() {
node.kill().unwrap();
let impl_contract = DeployedPackage::Contract(DeployedContract {
id: ContractId::from_str(
"02a7e78ef0514b80ab56b409f06f895d8939640b6f6d746fcbb15d3e0c6a1a3b",
"6c1ba5b247a2fc44e7a5166c3518c96f3cd566a4f144483a5310dcc82d08d010",
)
.unwrap(),
proxy: Some(
ContractId::from_str(
"6eb0db0e120222a4ac3ced8dfbf15ae56753b852aa7989849fa20e5aca47af44",
"90b8a908206724fcff5430ee124f6032f0a52010cd2f4c1549c4c9fdd65431e5",
)
.unwrap(),
),
Expand Down
8 changes: 4 additions & 4 deletions forc/tests/cli_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
// Assert that the output is correct
process.exp_string(" test test_log_4")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12384,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12384,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;

process.process.exit()?;
Ok(())
Expand All @@ -74,11 +74,11 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
process.exp_string(" test test_log_4")?;
process.exp_string("Decoded log value: 4, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12384,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Decoded log value: 2, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12672,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12384,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.process.exit()?;
Ok(())
}
6 changes: 5 additions & 1 deletion sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ fn to_bytecode_mut(
{
8
}
AllocatedOpcode::AddrDataId(_, _data_id) => 8,
AllocatedOpcode::AddrDataId(_, id)
if data_section.data_id_to_offset(id) > usize::from(Imm12::MAX.to_u16()) =>
{
8
}
AllocatedOpcode::ConfigurablesOffsetPlaceholder => 8,
AllocatedOpcode::DataSectionOffsetPlaceholder => 8,
AllocatedOpcode::BLOB(count) => count.value as u64 * 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
data_section::{DataSection, Entry},
};

use fuel_vm::fuel_asm::Imm12;
use indexmap::{IndexMap, IndexSet};
use sway_types::span::Span;

Expand Down Expand Up @@ -457,7 +458,13 @@ impl AllocatedAbstractInstructionSet {
}
}

Either::Left(AllocatedOpcode::AddrDataId(_, ref _data_id)) => 2,
Either::Left(AllocatedOpcode::AddrDataId(_, ref id)) => {
if data_section.data_id_to_offset(id) > usize::from(Imm12::MAX.to_u16()) {
2
} else {
1
}
}

// cfei 0 and cfsi 0 are omitted from asm emission, don't count them for offsets
Either::Left(AllocatedOpcode::CFEI(ref op))
Expand Down
47 changes: 31 additions & 16 deletions sway-core/src/asm_generation/fuel/fuel_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
BuildConfig,
};

use fuel_vm::fuel_asm::Imm12;
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
Expand Down Expand Up @@ -1461,23 +1462,37 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
let dst_reg = self.value_to_register(dst_val_ptr)?;
let src_reg = self.value_to_register(src_val_ptr)?;

let len_reg = self.reg_seqr.next();
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MOVI(
len_reg.clone(),
VirtualImmediate18 {
value: byte_len as u32,
},
)),
comment: "get data length for memory copy".into(),
owning_span: owning_span.clone(),
});
if byte_len <= u64::from(Imm12::MAX.to_u16()) {
// Can be done using a single MCPI instruction.
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MCPI(
dst_reg,
src_reg,
VirtualImmediate12::new_unchecked(byte_len, "argument size checked above"),
)),
comment: "copy memory".into(),
owning_span,
});
} else {
// Too many bytes for MCPI, so we need to use a separate register to hold the length.
let len_reg = self.reg_seqr.next();
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MOVI(
len_reg.clone(),
VirtualImmediate18 {
value: byte_len as u32,
},
)),
comment: "get data length for memory copy".into(),
owning_span: owning_span.clone(),
});

self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MCP(dst_reg, src_reg, len_reg)),
comment: "copy memory".into(),
owning_span,
});
self.cur_bytecode.push(Op {
opcode: Either::Left(VirtualOp::MCP(dst_reg, src_reg, len_reg)),
comment: "copy memory".into(),
owning_span,
});
}

Ok(())
}
Expand Down
33 changes: 22 additions & 11 deletions sway-core/src/asm_lang/allocated_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::{
fuel_prelude::fuel_asm::{self, op},
};
use fuel_vm::fuel_asm::{
op::{ADD, MOVI},
Imm18,
op::{ADD, ADDI, MOVI},
Imm12, Imm18,
};
use std::fmt::{self, Write};
use sway_types::span::Span;
Expand Down Expand Up @@ -773,17 +773,28 @@ fn addr_of(
data_section: &DataSection,
) -> Vec<fuel_asm::Instruction> {
let offset_bytes = data_section.data_id_to_offset(data_id) as u64;
vec![
fuel_asm::Instruction::MOVI(MOVI::new(
dest.to_reg_id(),
Imm18::new(offset_bytes.try_into().unwrap()),
)),
fuel_asm::Instruction::ADD(ADD::new(
dest.to_reg_id(),

if offset_bytes <= u64::from(Imm12::MAX.to_u16()) {
// Small enough to fit into an ADDI instruction immediate
vec![fuel_asm::Instruction::ADDI(ADDI::new(
dest.to_reg_id(),
fuel_asm::RegId::new(DATA_SECTION_REGISTER),
)),
]
Imm12::new(offset_bytes.try_into().unwrap()),
))]
} else {
// Offset too large to fit into ADDI immediate, so we need to use MOVI first
vec![
fuel_asm::Instruction::MOVI(MOVI::new(
dest.to_reg_id(),
Imm18::new(offset_bytes.try_into().unwrap()),
)),
fuel_asm::Instruction::ADD(ADD::new(
dest.to_reg_id(),
dest.to_reg_id(),
fuel_asm::RegId::new(DATA_SECTION_REGISTER),
)),
]
}
}

/// Converts a virtual load word instruction which uses data labels into one which uses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,82 +62,82 @@
{
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"name": "BOOL",
"offset": 7048
"offset": 6640
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "U8",
"offset": 7240
"offset": 6832
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "ANOTHER_U8",
"offset": 6976
"offset": 6568
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"name": "U16",
"offset": 7184
"offset": 6776
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"name": "U32",
"offset": 7224
"offset": 6816
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"name": "U64",
"offset": 7232
"offset": 6824
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"name": "U256",
"offset": 7192
"offset": 6784
},
{
"concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b",
"name": "B256",
"offset": 7016
"offset": 6608
},
{
"concreteTypeId": "81fc10c4681a3271cf2d66b2ec6fbc8ed007a442652930844fcf11818c295bff",
"name": "CONFIGURABLE_STRUCT",
"offset": 7136
"offset": 6728
},
{
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
"name": "CONFIGURABLE_ENUM_A",
"offset": 7056
"offset": 6648
},
{
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
"name": "CONFIGURABLE_ENUM_B",
"offset": 7096
"offset": 6688
},
{
"concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6",
"name": "ARRAY_BOOL",
"offset": 6984
"offset": 6576
},
{
"concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199",
"name": "ARRAY_U64",
"offset": 6992
"offset": 6584
},
{
"concreteTypeId": "c998ca9a5f221fe7b5c66ae70c8a9562b86d964408b00d17f883c906bc1fe4be",
"name": "TUPLE_BOOL_U64",
"offset": 7168
"offset": 6760
},
{
"concreteTypeId": "94f0fa95c830be5e4f711963e83259fe7e8bc723278ab6ec34449e791a99b53a",
"name": "STR_4",
"offset": 7160
"offset": 6752
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"name": "NOT_USED",
"offset": 7152
"offset": 6744
}
],
"encodingVersion": "1",
Expand Down
Loading

0 comments on commit 2feb7e2

Please sign in to comment.