-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error for uninitialized register on asm blocks (#5231)
## Description This PR closes #4997. The problem here is that "asm blocks registers" without initializers are considered uninitialized. There is no fallback to "capture" an available variable. This could be easily done, but that would leave us with no obvious syntax for unitialized registers. With this in mind this PR solves the compiler bug with two diagnostics: 1 - uninitialized registers cannot be read before they are written. This generates an error: ``` 5 | let _ = asm(r1) { | ^^ Unitialized register is being read before being written 6 | r1: u64 7 | }; ``` 2 - a warning when a unitialized register shadows an available variable with the same name. This is almost for sure a problem, and it is better the name the registe with something else and avoid all the confusion. ``` 27 | let r5 = 0; 28 | asm(r5) {}; | -- This unitialized register is shadowing a variable, you probably meant to also initialize it like "r5: r5". 29 | 30 | 0 ``` ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] 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. - [x] 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
Showing
13 changed files
with
133 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_fail/asm_read_from_uninitialized_reg/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = "asm_read_from_uninitialized_reg" | ||
source = "member" |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/asm_read_from_uninitialized_reg/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
implicit-std = false | ||
license = "Apache-2.0" | ||
name = "asm_read_from_uninitialized_reg" |
31 changes: 31 additions & 0 deletions
31
test/src/e2e_vm_tests/test_programs/should_fail/asm_read_from_uninitialized_reg/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
script; | ||
|
||
fn main() -> u64 { | ||
// Returning an unitialized register is not ok | ||
let _ = asm(r1) { | ||
r1: u64 | ||
}; | ||
|
||
// Reading unitialized register is not ok | ||
asm(r2) { | ||
sw r2 r2 i0; | ||
}; | ||
|
||
// Writing before reading unitialized register is ok | ||
asm(r3) { | ||
movi r3 i0; | ||
sw r3 r3 i0; | ||
}; | ||
|
||
// Writing before returning unitialized register is ok | ||
let _ = asm(r4) { | ||
movi r4 i0; | ||
r4: u64 | ||
}; | ||
|
||
// Shadowing a variable is a warning | ||
let r5 = 0; | ||
asm(r5) {}; | ||
|
||
0 | ||
} |
9 changes: 9 additions & 0 deletions
9
test/src/e2e_vm_tests/test_programs/should_fail/asm_read_from_uninitialized_reg/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
category = "fail" | ||
|
||
# check: $()Returning an unitialized register is not ok | ||
# nextln: $()let _ = asm(r1) | ||
# nextln: $()Unitialized register is being read before being written | ||
|
||
# check: $()Reading unitialized register is not ok | ||
# nextln: $()asm(r2) | ||
# nextln: $()Unitialized register is being read before being written |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters