-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a string_literal type, as a subtype of string
The main motivation for this is for the Sail->SystemVerilog backend, where we can easily represent string literals as elements of an enumeration-like type, but cannot deal at all with run-time created strings. There is an option -sv_nostrings that removes all strings, but with this option we can at least keep literal strings. Eventually the plan would be to use the effect system to track runtime created strings in such a way we can guarantee that they never affect the behaviour of the model, so can be safely removed. This commit adds a -string_literal_type option that causes string literals to be inferred as the `string_literal` type which is a subtype of the `string` type. We already have subtyping for refinement types so mostly this just works with a few minor adjustments for mappings and string literal patterns.
- Loading branch information
Showing
17 changed files
with
120 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
foo | ||
foo | ||
baz | ||
baz | ||
ok | ||
ok | ||
ok |
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,33 @@ | ||
$include <prelude.sail> | ||
|
||
$option -string_literal_type | ||
|
||
val test : string_literal -> unit | ||
|
||
function test(s) = print_endline(s) | ||
|
||
val main : unit -> unit | ||
|
||
function main() = { | ||
let x : string_literal = "foo"; | ||
var y : string_literal = "bar"; | ||
let z = "baz"; | ||
y = x; | ||
print_endline(y); | ||
test(y); | ||
y = z; | ||
print_endline(y); | ||
test(y); | ||
match x { | ||
"foo" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
}; | ||
match y { | ||
"baz" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
}; | ||
match concat_str(x, z) { | ||
"foobaz" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
} | ||
} |
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,34 @@ | ||
$include <prelude.sail> | ||
|
||
$option -string_literal_type | ||
|
||
val test : string_literal -> unit | ||
|
||
function test(s) = print_endline(s) | ||
|
||
val main : unit -> unit | ||
|
||
function main() = { | ||
let x : string_literal = "foo"; | ||
var y : string_literal = "bar"; | ||
let z = "baz"; | ||
test(z); | ||
y = x; | ||
print_endline(y); | ||
test(y); | ||
y = z; | ||
print_endline(y); | ||
test(y); | ||
match x { | ||
"foo" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
}; | ||
match y { | ||
"baz" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
}; | ||
match concat_str(x, z) { | ||
"foobaz" => print_endline("ok"), | ||
_ => print_endline("fail"), | ||
} | ||
} |
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,5 @@ | ||
[93mType error[0m: | ||
[96mpass/string_literal_type/v1.sail[0m:14.7-23: | ||
14[96m |[0m test(concat_str(x, z)) | ||
[91m |[0m [91m^--------------^[0m | ||
[91m |[0m string is not a subtype of string_literal |
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,15 @@ | ||
$include <prelude.sail> | ||
|
||
$option -string_literal_type | ||
|
||
val test : string_literal -> unit | ||
|
||
function test(s) = print_endline(s) | ||
|
||
val main : unit -> unit | ||
|
||
function main() = { | ||
let x : string_literal = "foo"; | ||
let z = "baz"; | ||
test(concat_str(x, z)) | ||
} |