Skip to content

Commit

Permalink
Introduce __smo intrinsic (FuelLabs#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadfawaz authored Dec 14, 2022
1 parent 60c18fe commit 967f223
Show file tree
Hide file tree
Showing 215 changed files with 1,652 additions and 197 deletions.
10 changes: 10 additions & 0 deletions docs/book/src/reference/compiler_intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ __ptr_sub(ptr: raw_ptr, offset: u64)
**Constraints:** None.

___

```sway
__smo<T>(recipient: b256, data: T, output_index: u64, coins: u64)
```

**Description:** Sends a message `data` of arbitrary type `T` and `coins` amount of the base asset to address `recipient`. This intrinsic assumes that an OutputMessage is available at index `output_index`.

**Constraints:** None.

___
5 changes: 5 additions & 0 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,11 @@ fn update_all_types(
update_json_type_application(&mut logged_type.application, old_to_new_id);
}
}
if let Some(messages_types) = &mut json_abi_program.messages_types {
for logged_type in messages_types.iter_mut() {
update_json_type_application(&mut logged_type.application, old_to_new_id);
}
}
}

/// Recursively updates the type IDs used in a `fuels_types::TypeApplication` given a HashMap from
Expand Down
3 changes: 3 additions & 0 deletions sway-ast/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Intrinsic {
Revert,
PtrAdd,
PtrSub,
Smo,
}

impl fmt::Display for Intrinsic {
Expand All @@ -45,6 +46,7 @@ impl fmt::Display for Intrinsic {
Intrinsic::Revert => "revert",
Intrinsic::PtrAdd => "ptr_add",
Intrinsic::PtrSub => "ptr_sub",
Intrinsic::Smo => "smo",
};
write!(f, "{}", s)
}
Expand Down Expand Up @@ -73,6 +75,7 @@ impl Intrinsic {
"__revert" => Revert,
"__ptr_add" => PtrAdd,
"__ptr_sub" => PtrSub,
"__smo" => Smo,
_ => return None,
})
}
Expand Down
38 changes: 38 additions & 0 deletions sway-core/src/asm_generation/asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ impl<'ir> AsmBuilder<'ir> {
}
}
Instruction::Revert(revert_val) => self.compile_revert(instr_val, revert_val),
Instruction::Smo {
recipient_and_message,
message_size,
output_index,
coins,
} => self.compile_smo(
instr_val,
recipient_and_message,
message_size,
output_index,
coins,
),
Instruction::StateLoadQuadWord { load_val, key } => check!(
self.compile_state_access_quad_word(
instr_val,
Expand Down Expand Up @@ -1399,6 +1411,32 @@ impl<'ir> AsmBuilder<'ir> {
});
}

fn compile_smo(
&mut self,
instr_val: &Value,
recipient_and_message: &Value,
message_size: &Value,
output_index: &Value,
coins: &Value,
) {
let owning_span = self.md_mgr.val_to_span(self.context, *instr_val);
let recipient_and_message_reg = self.value_to_register(recipient_and_message);
let message_size_reg = self.value_to_register(message_size);
let output_index_reg = self.value_to_register(output_index);
let coins_reg = self.value_to_register(coins);

self.cur_bytecode.push(Op {
owning_span,
opcode: Either::Left(VirtualOp::SMO(
recipient_and_message_reg,
message_size_reg,
output_index_reg,
coins_reg,
)),
comment: "".into(),
});
}

fn offset_reg(
&mut self,
base_reg: &VirtualRegister,
Expand Down
10 changes: 10 additions & 0 deletions sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn compile_program(
kind,
root,
logged_types,
messages_types,
declarations,
..
} = program;
Expand All @@ -38,6 +39,11 @@ pub fn compile_program(
.map(|(log_id, type_id)| (*type_id, *log_id))
.collect();

let messages_types = messages_types
.iter()
.map(|(message_id, type_id)| (*type_id, *message_id))
.collect();

let mut ctx = Context::default();
match kind {
// predicates and scripts have the same codegen, their only difference is static
Expand All @@ -49,6 +55,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
&messages_types,
&test_fns,
),
ty::TyProgramKind::Predicate { main_function } => compile::compile_predicate(
Expand All @@ -58,6 +65,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
&messages_types,
&test_fns,
),
ty::TyProgramKind::Contract { abi_entries } => compile::compile_contract(
Expand All @@ -66,6 +74,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
&messages_types,
&test_fns,
type_engine,
),
Expand All @@ -75,6 +84,7 @@ pub fn compile_program(
&root.namespace,
declarations,
&logged_types,
&messages_types,
&test_fns,
),
}?;
Expand Down
27 changes: 26 additions & 1 deletion sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
language::{ty, Visibility},
metadata::MetadataManager,
semantic_analysis::namespace,
type_system::{LogId, TypeId},
type_system::{LogId, MessageId, TypeId},
TypeEngine,
};

Expand All @@ -19,13 +19,15 @@ use sway_types::{span::Span, Spanned};

use std::collections::HashMap;

#[allow(clippy::too_many_arguments)]
pub(super) fn compile_script(
type_engine: &TypeEngine,
context: &mut Context,
main_function: &ty::TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(ty::TyFunctionDeclaration, DeclarationId)],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Script);
Expand All @@ -47,6 +49,7 @@ pub(super) fn compile_script(
module,
main_function,
logged_types_map,
messages_types_map,
None,
)?;
compile_tests(
Expand All @@ -55,19 +58,22 @@ pub(super) fn compile_script(
&mut md_mgr,
module,
logged_types_map,
messages_types_map,
test_fns,
)?;

Ok(module)
}

#[allow(clippy::too_many_arguments)]
pub(super) fn compile_predicate(
type_engine: &TypeEngine,
context: &mut Context,
main_function: &ty::TyFunctionDeclaration,
namespace: &namespace::Module,
declarations: &[ty::TyDeclaration],
logged_types: &HashMap<TypeId, LogId>,
messages_types: &HashMap<TypeId, MessageId>,
test_fns: &[(ty::TyFunctionDeclaration, DeclarationId)],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Predicate);
Expand All @@ -89,6 +95,7 @@ pub(super) fn compile_predicate(
module,
main_function,
&HashMap::new(),
&HashMap::new(),
None,
)?;
compile_tests(
Expand All @@ -97,18 +104,21 @@ pub(super) fn compile_predicate(
&mut md_mgr,
module,
logged_types,
messages_types,
test_fns,
)?;

Ok(module)
}

#[allow(clippy::too_many_arguments)]
pub(super) fn compile_contract(
context: &mut Context,
abi_entries: &[ty::TyFunctionDeclaration],
namespace: &namespace::Module,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(ty::TyFunctionDeclaration, DeclarationId)],
type_engine: &TypeEngine,
) -> Result<Module, CompileError> {
Expand All @@ -131,6 +141,7 @@ pub(super) fn compile_contract(
module,
decl,
logged_types_map,
messages_types_map,
type_engine,
)?;
}
Expand All @@ -140,6 +151,7 @@ pub(super) fn compile_contract(
&mut md_mgr,
module,
logged_types_map,
messages_types_map,
test_fns,
)?;

Expand All @@ -152,6 +164,7 @@ pub(super) fn compile_library(
namespace: &namespace::Module,
declarations: &[ty::TyDeclaration],
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(ty::TyFunctionDeclaration, DeclarationId)],
) -> Result<Module, CompileError> {
let module = Module::new(context, Kind::Library);
Expand All @@ -172,6 +185,7 @@ pub(super) fn compile_library(
&mut md_mgr,
module,
logged_types_map,
messages_types_map,
test_fns,
)?;

Expand Down Expand Up @@ -281,6 +295,7 @@ pub(super) fn compile_function(
module: Module,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
is_entry: bool,
test_decl_id: Option<DeclarationId>,
) -> Result<Option<Function>, CompileError> {
Expand All @@ -305,19 +320,22 @@ pub(super) fn compile_function(
args,
None,
logged_types_map,
messages_types_map,
test_decl_id,
)
.map(Some)
}
}

#[allow(clippy::too_many_arguments)]
pub(super) fn compile_entry_function(
type_engine: &TypeEngine,
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_decl_id: Option<DeclarationId>,
) -> Result<Function, CompileError> {
let is_entry = true;
Expand All @@ -328,6 +346,7 @@ pub(super) fn compile_entry_function(
module,
ast_fn_decl,
logged_types_map,
messages_types_map,
is_entry,
test_decl_id,
)
Expand All @@ -340,6 +359,7 @@ pub(super) fn compile_tests(
md_mgr: &mut MetadataManager,
module: Module,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(ty::TyFunctionDeclaration, DeclarationId)],
) -> Result<Vec<Function>, CompileError> {
test_fns
Expand All @@ -352,6 +372,7 @@ pub(super) fn compile_tests(
module,
ast_fn_decl,
logged_types_map,
messages_types_map,
Some(decl_id.clone()),
)
})
Expand Down Expand Up @@ -387,6 +408,7 @@ fn compile_fn_with_args(
args: Vec<(String, Type, Span)>,
selector: Option<[u8; 4]>,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_decl_id: Option<DeclarationId>,
) -> Result<Function, CompileError> {
let inline_opt = ast_fn_decl.inline();
Expand Down Expand Up @@ -451,6 +473,7 @@ fn compile_fn_with_args(
func,
returns_by_ref,
logged_types_map,
messages_types_map,
);
let mut ret_val = compiler.compile_code_block(context, md_mgr, body)?;

Expand Down Expand Up @@ -529,6 +552,7 @@ fn compile_abi_method(
module: Module,
ast_fn_decl: &ty::TyFunctionDeclaration,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
type_engine: &TypeEngine,
) -> Result<Function, CompileError> {
// Use the error from .to_fn_selector_value() if possible, else make an CompileError::Internal.
Expand Down Expand Up @@ -574,6 +598,7 @@ fn compile_abi_method(
args,
Some(selector),
logged_types_map,
messages_types_map,
None,
)
}
Loading

0 comments on commit 967f223

Please sign in to comment.