Binary function names#91
Binary function names#91stoklund merged 9 commits intobytecodealliance:masterfrom zummenix:feature/#47-binary-function-names
Conversation
stoklund
left a comment
There was a problem hiding this comment.
Thanks for working on this!
All your changes to the file tests and unit tests look good.
The FunctionName implementation should be able to print itself, using the %nnn syntax when possible, and falling back to the #xxxx syntax otherwise.
It would be good to add some test containing Unicode alphabetic characters, just to make sure they are not printed with %nnn.
| fn basic() { | ||
| let mut f = Function::new(); | ||
| assert_eq!(f.to_string(), "function \"\"() {\n}\n"); | ||
| assert_eq!(f.to_string(), "function %() {\n}\n"); |
There was a problem hiding this comment.
Yep. I think this is an OK syntax for the empty function name.
lib/cretonne/src/ir/funcname.rs
Outdated
| /// | ||
| /// Caller should validate that the string contains only | ||
| /// ASCII alphanumerical characters and `_`. | ||
| pub fn from_string(s: &str) -> FunctionName { |
There was a problem hiding this comment.
I think with_str would be a better name for this constructor. Thefrom_string name implies a conversion constructor which would be expected to consume a String. See the Rust naming conventions.
I don't think the comment is correct. The caller should be allowed to pass any string, and it will be printed in #xxx form if it is not pure ASCII.
lib/cretonne/src/ir/funcname.rs
Outdated
| f.write_str(&self.0) | ||
| } | ||
| f.write_char('%')?; | ||
| f.write_str(&self.0) |
There was a problem hiding this comment.
Perhaps easier: write!(f, "%{}", self.0).
I would like for the Display implementation to still detect if the function name is pure ASCII so it can be printed with the % syntax. If it can't, it should be printed in hexadecimal instead with #xxxx. The legal characters in the %xx notating is ASCII alphanumerical characters and _.
lib/cretonne/src/ir/funcname.rs
Outdated
| fn formatting_string() { | ||
| assert_eq!(FunctionName::from_string("").to_string(), "%"); | ||
| assert_eq!(FunctionName::from_string("x").to_string(), "%x"); | ||
| assert_eq!(FunctionName::from_string(" ").to_string(), "% "); |
There was a problem hiding this comment.
The first two are correct, the second one should print as #20.
|
BTW, some of this may be easier if you switch the internal representation from a It's still useful for the parser to have a |
|
Sorry, didn't mean to close this! |
|
Thank you for the review! Initially I thought that So my next actions are:
Is that correct? |
|
Yes, you got it right |
|
@stoklund, It would be helpful to have some dependency or module that can convert to/from hex. I'm hesitant to copy code from other crates. |
lib/cretonne/src/ir/funcname.rs
Outdated
| fn is_id_continue(c: char) -> bool { | ||
| c.is_ascii() && (c == '_' || c.is_alphanumeric()) | ||
| /// Creates a new function name from a sequence of bytes. | ||
| pub fn new(bytes: Vec<u8>) -> FunctionName { |
There was a problem hiding this comment.
I think you should just take a bytes: &[u8] slice here. We don't want to force the caller to allocate a Vec.
There was a problem hiding this comment.
Let's leave one generic constructor:pub fn new<T>(v: T) -> FunctionName where T: Into<Vec<u8>> ?
There was a problem hiding this comment.
Do you mean in addition to a &[u8] constructor? That would be OK.
There was a problem hiding this comment.
No, I mean only one constructor. That way we can create FunctionName using &str and &[u8]
There was a problem hiding this comment.
I see what you mean. As long as we can construct from both an &str and an &[u8], that is fine.
lib/cretonne/src/ir/funcname.rs
Outdated
| f.write_char('#')?; | ||
| for i in self.0.iter().map(|&b| b as usize) { | ||
| f.write_char(HEX_CHARS[i >> 4] as char)?; | ||
| f.write_char(HEX_CHARS[i & 0xf] as char)?; |
There was a problem hiding this comment.
You can print a hexadecimal byte like this:
write!(f, "{:02x}", b)?There was a problem hiding this comment.
Thanks, didn't think of it.
|
For parsing the hexadecimal string, you can use |
stoklund
left a comment
There was a problem hiding this comment.
This looks great. Thanks!
Eventually will close #47
I hope I move in right direction.