Skip to content

Commit

Permalink
Allow any Ident when parsing asm block instruction opcodes (FuelL…
Browse files Browse the repository at this point in the history
…abs#4431)

## Description
The idea is to not check for Sway keywords when parsing the opcodes in
`asm` blocks. This is important because some Sway keywords, such as
`mod`, are also FuelVM opcodes. Note that the validity of the opcodes
are later checked anyways so we're not losing any useful checks here.

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
mohammadfawaz authored Apr 12, 2023
1 parent ed0f800 commit 83d5578
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
10 changes: 9 additions & 1 deletion sway-parse/src/expr/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ impl ParseToEnd for AsmBlockContents {
if let Some(consumed) = parser.check_empty() {
break (None, consumed);
}
let ident = parser.parse()?;

// Parse the opcode directly instead of calling `parser.parse()` to avoid checking for
// illegal identifiers such as keywords. opcode names should not be subject to those
// checks because some opcodes, such as `mod`, are also Sway keywords.
let ident = match parser.take::<Ident>() {
Some(ident) => ident,
None => return Err(parser.emit_error(ParseErrorKind::ExpectedIdent)),
};

if let Some(consumed) = parser.check_empty() {
let final_expr = AsmFinalExpr {
register: ident,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'unrecognized_opcode'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
implicit-std = false
name = "unrecognized_opcode"
entry = "main.sw"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
script;

fn main() {
let _x = asm (r1, r2: 0, r3: 0) {
modd r1 r2 r3;
r1: u64
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()modd r1 r2 r3;
# check: $()Unrecognized op code.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "asm_expr_basic"
entry = "main.sw"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ struct GasCounts {

fn get_gas() -> GasCounts {
GasCounts {
global_gas: asm() {
ggas
},
context_gas: asm() {
cgas
}
global_gas: asm() { ggas },
context_gas: asm() { cgas },
}
}

Expand Down Expand Up @@ -66,5 +62,10 @@ fn main() -> u32 {

let _flag = asm() { flag };

let _x = asm(r1, r2: 2, r3: 1) {
mod r1 r2 r3;
r1: u64
};

return 6u32;
}

0 comments on commit 83d5578

Please sign in to comment.