Skip to content

Commit

Permalink
Align asm opcodes to 4 chars in formatter (FuelLabs#3823)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Jan 20, 2023
1 parent 19a7944 commit 441befa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
8 changes: 4 additions & 4 deletions examples/asm_return_tuple_pointer/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ script;
fn adder(a: u64, b: u64, c: u64) -> (u64, u64) {
let empty_tuple = (0u64, 0u64);
asm(output: empty_tuple, r1: a, r2: b, r3: c, r4, r5) {
add r4 r1 r2; // add a & b and put the result in r4
add r5 r2 r3; // add b & c and put the result in r5
sw output r4 i0; // store the word in r4 in output + 0 words
sw output r5 i1; // store the word in r5 in output + 1 word
add r4 r1 r2; // add a & b and put the result in r4
add r5 r2 r3; // add b & c and put the result in r5
sw output r4 i0; // store the word in r4 in output + 0 words
sw output r5 i1; // store the word in r5 in output + 1 word
output: (u64, u64) // return both values
}
}
Expand Down
25 changes: 25 additions & 0 deletions sway-ast/src/expr/op_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ macro_rules! push_register_arg_idents (
};
);

macro_rules! push_immediate_idents (
($vec_name:ident, ()) => {};
($vec_name:ident, ($arg_name_head:ident: reg, $($arg_name:ident: $arg_ty:tt,)*)) => {
let _ = $arg_name_head;
push_immediate_idents!($vec_name, ($($arg_name: $arg_ty,)*))
};
($vec_name:ident, ($arg_name_head:ident: imm, $($arg_name:ident: $arg_ty:tt,)*)) => {
$vec_name.push(Ident::new($arg_name_head.span()));
push_immediate_idents!($vec_name, ($($arg_name: $arg_ty,)*))
};
);

macro_rules! ignore_remaining (
() => {};
($arg_name_head:ident: $arg_ty_head:tt, $($arg_name:ident: $arg_ty:tt,)*) => {{
Expand Down Expand Up @@ -130,6 +142,19 @@ macro_rules! define_op_codes (
},)*
}
}

#[allow(clippy::vec_init_then_push)]
pub fn immediate_idents(&self) -> Vec<Ident> {
match self {
$(Instruction::$op_name { $($arg_name,)* .. } => {
#[allow(unused_mut)]
let mut ret = Vec::new();
push_immediate_idents!(ret, ($($arg_name: $arg_ty,)*));
ret
},)*
}
}

}

impl Spanned for Instruction {
Expand Down
29 changes: 29 additions & 0 deletions swayfmt/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,35 @@ impl MyContract for Contract {
}
}
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert_eq!(correct_sway_code, formatted_sway_code);
assert!(test_stability(formatted_sway_code, formatter));
}

#[test]
fn test_asm_block() {
let sway_code_to_format = r#"library my_lib;
fn foo() {
asm(r1: self, r2: other, r3, r4) {
addi r3 zero i32;
meq r4 r1 r2 r3;
r4: bool
}
}
"#;
let correct_sway_code = r#"library my_lib;
fn foo() {
asm(r1: self, r2: other, r3, r4) {
addi r3 zero i32;
meq r4 r1 r2 r3;
r4: bool
}
}
"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand Down
27 changes: 22 additions & 5 deletions swayfmt/src/utils/language/expr/asm_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,37 @@ impl Format for AsmRegisterDeclaration {
}
}

impl Format for Instruction {
fn format(
&self,
formatted_code: &mut FormattedCode,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(formatted_code, "{:<4}", &self.op_code_ident().as_str())?;
for arg in self.register_arg_idents() {
write!(formatted_code, " {}", arg.as_str())?
}
for imm in self.immediate_idents() {
write!(formatted_code, " {}", imm.as_str())?
}
Ok(())
}
}

impl Format for AsmBlockContents {
fn format(
&self,
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
for (instruction, semicolon_token) in self.instructions.iter() {
writeln!(
write!(
formatted_code,
"{}{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
instruction.span().as_str(),
semicolon_token.span().as_str()
"{}",
formatter.shape.indent.to_string(&formatter.config)?
)?;
instruction.format(formatted_code, formatter)?;
writeln!(formatted_code, "{}", semicolon_token.span().as_str())?
}
if let Some(final_expr) = &self.final_expr_opt {
if formatter.shape.code_line.line_style == LineStyle::Multiline {
Expand Down

0 comments on commit 441befa

Please sign in to comment.