feat: Add functions for name mangling#170
Conversation
b8c9eb6 to
3f3adbb
Compare
9c73c17 to
d4503f7
Compare
|
|
||
| /// Performs the inverse of Verilator's name manglging | ||
| /// https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn demangle(s: &str) -> String { |
There was a problem hiding this comment.
for chars its fine but for strings I like to manually write out the full thing like string, which then usually means that my naming was bad and I should really name it something like name or identifier etc
|
|
||
| /// Performs the inverse of Verilator's name manglging | ||
| /// https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn demangle(s: &str) -> String { |
There was a problem hiding this comment.
also function name could be better, maybe demangle_verilator_identifier or demangle_verilator_model_name or something idk?
| } else { | ||
| // We already checked that everything is ASCII, so this encoding trick | ||
| // will not panic | ||
| let mut buf = [0]; |
| result | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("_05F")) |
There was a problem hiding this comment.
could you write this as computed from _'s ascii manually instead of leaving it magic?
There was a problem hiding this comment.
if you don't want to do this, comment still not needed. You can make a constant called VERILATOR_MANGLED_UNDERSCORE: &'static str
| "{s} is a non-ascii name which is unsupported for name demangling." | ||
| )) | ||
| } else { | ||
| // Every character _except_ double _ can be handled as a single c haracter, so we'll |
There was a problem hiding this comment.
comment not really needed I think
There was a problem hiding this comment.
I personally think it clarifies things
|
|
||
|
|
||
| /// Performs Verilator's name manglging https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn mangle(s: &str) -> Result<String, String> { |
There was a problem hiding this comment.
could you figure out how to make the error type more semantic than String? there are a bunch of ways and I trust your judgment
| if c.is_ascii_alphanumeric() || c == '_' { | ||
| result.push(c); | ||
| } else { | ||
| // We already checked that everything is ASCII, so this encoding trick |
There was a problem hiding this comment.
trying to figure out how to get rid of this comment; maybe you could just make the buffer have length 4, or have some documentating code like assert(c.is_ascii()? or maybe its fine to just remove it because people know the spec of encode_utf8
| // split on those, and then join them with their replacement | ||
| Ok(s.split("__") | ||
| .map(|segment| { | ||
| let mut result = String::new(); |
There was a problem hiding this comment.
would it be uglier to write this as a fold instead of a stateful for loop?
There was a problem hiding this comment.
I tend to prefer for loops over folds personally, it feels a bit more idiomatic to me somehow
There was a problem hiding this comment.
do you think ::new or ::default reads better here
f72dda9 to
4fc94c5
Compare
| ffi_names::{DPI_INIT_CALLBACK, TRACE_EVER_ON}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Snafu)] |
There was a problem hiding this comment.
Lol you're ahead of me, see #187, ideally I do this all in one PR and this can be an enum case
There was a problem hiding this comment.
What do you want me to do with this one then? whatever!()?
There was a problem hiding this comment.
Yeah for now unfortunately, it sucks but I think it's better to have consistency
| result | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("_05F")) |
There was a problem hiding this comment.
if you don't want to do this, comment still not needed. You can make a constant called VERILATOR_MANGLED_UNDERSCORE: &'static str
| NonAsciiName { name: String }, | ||
| } | ||
|
|
||
| /// Performs Verilator's name manglging https://verilator.org/guide/latest/languages.html#signal-naming |
There was a problem hiding this comment.
- typos
- all doc comments should end with periods
- can you
[name mangling](link)?
| } | ||
| } | ||
|
|
||
| /// Performs the inverse of Verilator's name manglging |
There was a problem hiding this comment.
- typos
- all doc comments should end with periods
- can you
[name mangling](link)
| /// Performs the inverse of Verilator's name manglging | ||
| /// https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn demangle_verilator_name(name: &str) -> String { | ||
| name.split("__") |
There was a problem hiding this comment.
can this be a const as well? it's unclear why this is different from "_05F"
| .collect::<Vec<_>>() | ||
| .join("") | ||
| } | ||
|
|
There was a problem hiding this comment.
Can you add idempotency tests and small unit tests?
| if c.is_ascii_alphanumeric() || c == '_' { | ||
| result.push(c); | ||
| } else { | ||
| // We already checked that everything is ASCII, so this |
There was a problem hiding this comment.
Are you sure? It does hint that the following code can panic, but will not panic under current conditions
There was a problem hiding this comment.
I think it's fine to assume anyone reading the code has perfect knowledge if the standard library docs, so you don't need to replicate that documentation
| /// Performs Verilator's name manglging https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn mangle_verilator_name(name: &str) -> Result<String, ManglingError> { | ||
| if name.is_ascii() { | ||
| // Every character _except_ double underscore can be handled as a single |
There was a problem hiding this comment.
Comment not needed, interested people can refer to the link you put
There was a problem hiding this comment.
I don't think it is good to have to read another document for an explanation of why things are done if we can put the information right here.
This also prevents issues if upstream would move their docs around or something
| /// https://verilator.org/guide/latest/languages.html#signal-naming | ||
| pub fn demangle_verilator_name(name: &str) -> String { | ||
| name.split("__") | ||
| .map(|s| { |
There was a problem hiding this comment.
I'm not sure, I think segment implies something more than it is (a split result where the start is an escapee, and the rest is normal text
|
Added some tests, ended up refactoring it a bit because I re-read the docs and saw some stuff I missed before |
| } | ||
|
|
||
| #[test] | ||
| fn name_mangling_and_demangling_is_noop() { |
There was a problem hiding this comment.
id call it idempotent not noop
might be worth doing a simple fuzz as well with strings guaranteed to include double idents or symbols, but it is a simple enough function that it might not be needed. Your call
| if name.is_ascii() { | ||
| // Every character _except_ double underscore can be handled as a single | ||
| // character, so we'll split on those, and then join them with | ||
| // their replacement |
| // character, so we'll split on those, and then join them with | ||
| // their replacement | ||
| Ok(name | ||
| .split("__") |
| pub fn demangle_verilator_name(name: &str) -> String { | ||
| name.split(VERILATOR_MANGLED_PREFIX) | ||
| .enumerate() | ||
| .map(|(i, s)| { |
| .args(["--lib-create", &library_name]) | ||
| .args(["--Mdir", verilator_artifact_directory.as_str()]) | ||
| .args(["--top-module", top_module]) | ||
| .args(["--top-module", &demangle_verilator_name(top_module)]) |
There was a problem hiding this comment.
Wouldn't this be mangle not demangle? verilator/verilator#6940
No description provided.