Skip to content

feat: Add functions for name mangling#170

Open
TheZoq2 wants to merge 3 commits into
ethanuppal:mainfrom
TheZoq2:mangling
Open

feat: Add functions for name mangling#170
TheZoq2 wants to merge 3 commits into
ethanuppal:mainfrom
TheZoq2:mangling

Conversation

@TheZoq2

@TheZoq2 TheZoq2 commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

No description provided.

@TheZoq2 TheZoq2 changed the title Add functions for name mangling and --no-trace-top Add functions for name mangling Dec 12, 2025
@TheZoq2
TheZoq2 force-pushed the mangling branch 2 times, most recently from b8c9eb6 to 3f3adbb Compare December 12, 2025 10:46
@TheZoq2 TheZoq2 changed the title Add functions for name mangling [feat] Add functions for name mangling Dec 12, 2025
@TheZoq2 TheZoq2 changed the title [feat] Add functions for name mangling feat: Add functions for name mangling Dec 12, 2025
@TheZoq2
TheZoq2 force-pushed the mangling branch 2 times, most recently from 9c73c17 to d4503f7 Compare December 12, 2025 10:50

@ethanuppal ethanuppal left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Comment thread verilator/src/lib.rs Outdated

/// Performs the inverse of Verilator's name manglging
/// https://verilator.org/guide/latest/languages.html#signal-naming
pub fn demangle(s: &str) -> String {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs Outdated
Comment thread verilator/src/lib.rs Outdated

/// Performs the inverse of Verilator's name manglging
/// https://verilator.org/guide/latest/languages.html#signal-naming
pub fn demangle(s: &str) -> String {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also function name could be better, maybe demangle_verilator_identifier or demangle_verilator_model_name or something idk?

Comment thread verilator/src/lib.rs Outdated
} else {
// We already checked that everything is ASCII, so this encoding trick
// will not panic
let mut buf = [0];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id write it as buffer

Comment thread verilator/src/lib.rs Outdated
result
})
.collect::<Vec<_>>()
.join("_05F"))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you write this as computed from _'s ascii manually instead of leaving it magic?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't want to do this, comment still not needed. You can make a constant called VERILATOR_MANGLED_UNDERSCORE: &'static str

Comment thread verilator/src/lib.rs Outdated
"{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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment not really needed I think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it clarifies things

Comment thread verilator/src/lib.rs Outdated


/// Performs Verilator's name manglging https://verilator.org/guide/latest/languages.html#signal-naming
pub fn mangle(s: &str) -> Result<String, String> {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs Outdated
Comment thread verilator/src/lib.rs Outdated
if c.is_ascii_alphanumeric() || c == '_' {
result.push(c);
} else {
// We already checked that everything is ASCII, so this encoding trick

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs
// split on those, and then join them with their replacement
Ok(s.split("__")
.map(|segment| {
let mut result = String::new();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be uglier to write this as a fold instead of a stateful for loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer for loops over folds personally, it feels a bit more idiomatic to me somehow

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think ::new or ::default reads better here

@TheZoq2
TheZoq2 force-pushed the mangling branch 3 times, most recently from f72dda9 to 4fc94c5 Compare February 27, 2026 10:24
Comment thread verilator/src/lib.rs
ffi_names::{DPI_INIT_CALLBACK, TRACE_EVER_ON},
};

#[derive(Debug, Snafu)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol you're ahead of me, see #187, ideally I do this all in one PR and this can be an enum case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you want me to do with this one then? whatever!()?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah for now unfortunately, it sucks but I think it's better to have consistency

Comment thread verilator/src/lib.rs Outdated
result
})
.collect::<Vec<_>>()
.join("_05F"))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't want to do this, comment still not needed. You can make a constant called VERILATOR_MANGLED_UNDERSCORE: &'static str

Comment thread verilator/src/lib.rs Outdated
NonAsciiName { name: String },
}

/// Performs Verilator's name manglging https://verilator.org/guide/latest/languages.html#signal-naming

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • typos
  • all doc comments should end with periods
  • can you [name mangling](link)?

Comment thread verilator/src/lib.rs Outdated
}
}

/// Performs the inverse of Verilator's name manglging

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • typos
  • all doc comments should end with periods
  • can you [name mangling](link)

Comment thread verilator/src/lib.rs Outdated
/// 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("__")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be a const as well? it's unclear why this is different from "_05F"

Comment thread verilator/src/lib.rs
.collect::<Vec<_>>()
.join("")
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add idempotency tests and small unit tests?

Comment thread verilator/src/lib.rs
if c.is_ascii_alphanumeric() || c == '_' {
result.push(c);
} else {
// We already checked that everything is ASCII, so this

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment not needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? It does hint that the following code can panic, but will not panic under current conditions

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs
/// 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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment not needed, interested people can refer to the link you put

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs Outdated
/// https://verilator.org/guide/latest/languages.html#signal-naming
pub fn demangle_verilator_name(name: &str) -> String {
name.split("__")
.map(|s| {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe segment instead of s?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@TheZoq2

TheZoq2 commented Feb 27, 2026

Copy link
Copy Markdown
Contributor Author

Added some tests, ended up refactoring it a bit because I re-read the docs and saw some stuff I missed before

@ethanuppal ethanuppal left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Comment thread verilator/src/lib.rs Outdated
}

#[test]
fn name_mangling_and_demangling_is_noop() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread verilator/src/lib.rs Outdated
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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Period at end of sentence

Comment thread verilator/src/lib.rs Outdated
// character, so we'll split on those, and then join them with
// their replacement
Ok(name
.split("__")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should be constant

Comment thread verilator/src/lib.rs Outdated
pub fn demangle_verilator_name(name: &str) -> String {
name.split(VERILATOR_MANGLED_PREFIX)
.enumerate()
.map(|(i, s)| {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

segment?

Comment thread verilator/src/build_library.rs Outdated
.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)])

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be mangle not demangle? verilator/verilator#6940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants