Binary function names (#91)
* Function names should start with % * Create FunctionName from string * Implement displaying of FunctionName as %nnnn with fallback to #xxxx * Run rustfmt and fix FunctionName::with_string in parser * Implement FunctionName::new as a generic function * Binary function names should start with # * Implement NameRepr for function name * Fix examples in docs to reflect that function names start with % * Rebase and fix filecheck tests
This commit is contained in:
committed by
Jakob Stoklund Olesen
parent
731278aad8
commit
8b484b1c77
@@ -6,73 +6,119 @@
|
||||
use std::fmt::{self, Write};
|
||||
use std::ascii::AsciiExt;
|
||||
|
||||
/// The name of a function can be any UTF-8 string.
|
||||
/// The name of a function can be any sequence of bytes.
|
||||
///
|
||||
/// Function names are mostly a testing and debugging tool.
|
||||
/// In particular, `.cton` files use function names to identify functions.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct FunctionName(String);
|
||||
pub struct FunctionName(NameRepr);
|
||||
|
||||
impl FunctionName {
|
||||
/// Create new function name equal to `s`.
|
||||
pub fn new<S: Into<String>>(s: S) -> FunctionName {
|
||||
FunctionName(s.into())
|
||||
/// Creates a new function name from a sequence of bytes.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use cretonne::ir::FunctionName;
|
||||
/// // Create `FunctionName` from a string.
|
||||
/// let name = FunctionName::new("hello");
|
||||
/// assert_eq!(name.to_string(), "%hello");
|
||||
///
|
||||
/// // Create `FunctionName` from a sequence of bytes.
|
||||
/// let bytes: &[u8] = &[10, 9, 8];
|
||||
/// let name = FunctionName::new(bytes);
|
||||
/// assert_eq!(name.to_string(), "#0a0908");
|
||||
/// ```
|
||||
pub fn new<T>(v: T) -> FunctionName
|
||||
where T: Into<Vec<u8>>
|
||||
{
|
||||
let vec = v.into();
|
||||
if vec.len() <= NAME_LENGTH_THRESHOLD {
|
||||
let mut bytes = [0u8; NAME_LENGTH_THRESHOLD];
|
||||
for (i, &byte) in vec.iter().enumerate() {
|
||||
bytes[i] = byte;
|
||||
}
|
||||
FunctionName(NameRepr::Short {
|
||||
length: vec.len() as u8,
|
||||
bytes: bytes,
|
||||
})
|
||||
} else {
|
||||
FunctionName(NameRepr::Long(vec))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_id_start(c: char) -> bool {
|
||||
c.is_ascii() && (c == '_' || c.is_alphabetic())
|
||||
/// Tries to interpret bytes as ASCII alphanumerical characters and `_`.
|
||||
fn try_as_name(bytes: &[u8]) -> Option<String> {
|
||||
let mut name = String::with_capacity(bytes.len());
|
||||
for c in bytes.iter().map(|&b| b as char) {
|
||||
if c.is_ascii() && c.is_alphanumeric() || c == '_' {
|
||||
name.push(c);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(name)
|
||||
}
|
||||
|
||||
fn is_id_continue(c: char) -> bool {
|
||||
c.is_ascii() && (c == '_' || c.is_alphanumeric())
|
||||
const NAME_LENGTH_THRESHOLD: usize = 22;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum NameRepr {
|
||||
Short {
|
||||
length: u8,
|
||||
bytes: [u8; NAME_LENGTH_THRESHOLD],
|
||||
},
|
||||
Long(Vec<u8>),
|
||||
}
|
||||
|
||||
// The function name may need quotes if it doesn't parse as an identifier.
|
||||
fn needs_quotes(name: &str) -> bool {
|
||||
let mut iter = name.chars();
|
||||
if let Some(ch) = iter.next() {
|
||||
!is_id_start(ch) || !iter.all(is_id_continue)
|
||||
} else {
|
||||
// A blank function name needs quotes.
|
||||
true
|
||||
impl AsRef<[u8]> for NameRepr {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
match *self {
|
||||
NameRepr::Short { length, ref bytes } => &bytes[0..length as usize],
|
||||
NameRepr::Long(ref vec) => vec.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NameRepr {
|
||||
fn default() -> Self {
|
||||
NameRepr::Short {
|
||||
length: 0,
|
||||
bytes: [0; NAME_LENGTH_THRESHOLD],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FunctionName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if needs_quotes(&self.0) {
|
||||
f.write_char('"')?;
|
||||
for c in self.0.chars().flat_map(char::escape_default) {
|
||||
f.write_char(c)?;
|
||||
}
|
||||
f.write_char('"')
|
||||
if let Some(name) = try_as_name(self.0.as_ref()) {
|
||||
write!(f, "%{}", name)
|
||||
} else {
|
||||
f.write_str(&self.0)
|
||||
f.write_char('#')?;
|
||||
for byte in self.0.as_ref() {
|
||||
write!(f, "{:02x}", byte)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{needs_quotes, FunctionName};
|
||||
use super::FunctionName;
|
||||
|
||||
#[test]
|
||||
fn quoting() {
|
||||
assert_eq!(needs_quotes(""), true);
|
||||
assert_eq!(needs_quotes("x"), false);
|
||||
assert_eq!(needs_quotes(" "), true);
|
||||
assert_eq!(needs_quotes("0"), true);
|
||||
assert_eq!(needs_quotes("x0"), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escaping() {
|
||||
assert_eq!(FunctionName::new("").to_string(), "\"\"");
|
||||
assert_eq!(FunctionName::new("x").to_string(), "x");
|
||||
assert_eq!(FunctionName::new(" ").to_string(), "\" \"");
|
||||
assert_eq!(FunctionName::new(" \n").to_string(), "\" \\n\"");
|
||||
assert_eq!(FunctionName::new("a\u{1000}v").to_string(),
|
||||
"\"a\\u{1000}v\"");
|
||||
fn displaying() {
|
||||
assert_eq!(FunctionName::new("").to_string(), "%");
|
||||
assert_eq!(FunctionName::new("x").to_string(), "%x");
|
||||
assert_eq!(FunctionName::new("x_1").to_string(), "%x_1");
|
||||
assert_eq!(FunctionName::new(" ").to_string(), "#20");
|
||||
assert_eq!(FunctionName::new("кретон").to_string(),
|
||||
"#d0bad180d0b5d182d0bed0bd");
|
||||
assert_eq!(FunctionName::new("印花棉布").to_string(),
|
||||
"#e58db0e88ab1e6a389e5b883");
|
||||
assert_eq!(FunctionName::new(vec![0, 1, 2, 3, 4, 5]).to_string(),
|
||||
"#000102030405");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -365,26 +365,26 @@ mod tests {
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut f = Function::new();
|
||||
assert_eq!(f.to_string(), "function \"\"() {\n}\n");
|
||||
assert_eq!(f.to_string(), "function %() {\n}\n");
|
||||
|
||||
f.name = FunctionName::new("foo".to_string());
|
||||
assert_eq!(f.to_string(), "function foo() {\n}\n");
|
||||
f.name = FunctionName::new("foo");
|
||||
assert_eq!(f.to_string(), "function %foo() {\n}\n");
|
||||
|
||||
f.stack_slots.push(StackSlotData::new(4));
|
||||
assert_eq!(f.to_string(),
|
||||
"function foo() {\n ss0 = stack_slot 4\n}\n");
|
||||
"function %foo() {\n ss0 = stack_slot 4\n}\n");
|
||||
|
||||
let ebb = f.dfg.make_ebb();
|
||||
f.layout.append_ebb(ebb);
|
||||
assert_eq!(f.to_string(),
|
||||
"function foo() {\n ss0 = stack_slot 4\n\nebb0:\n}\n");
|
||||
"function %foo() {\n ss0 = stack_slot 4\n\nebb0:\n}\n");
|
||||
|
||||
f.dfg.append_ebb_arg(ebb, types::I8);
|
||||
assert_eq!(f.to_string(),
|
||||
"function foo() {\n ss0 = stack_slot 4\n\nebb0(v0: i8):\n}\n");
|
||||
"function %foo() {\n ss0 = stack_slot 4\n\nebb0(v0: i8):\n}\n");
|
||||
|
||||
f.dfg.append_ebb_arg(ebb, types::F32.by(4).unwrap());
|
||||
assert_eq!(f.to_string(),
|
||||
"function foo() {\n ss0 = stack_slot 4\n\nebb0(v0: i8, v1: f32x4):\n}\n");
|
||||
"function %foo() {\n ss0 = stack_slot 4\n\nebb0(v0: i8, v1: f32x4):\n}\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -723,10 +723,25 @@ impl<'a> Parser<'a> {
|
||||
//
|
||||
fn parse_function_name(&mut self) -> Result<FunctionName> {
|
||||
match self.token() {
|
||||
Some(Token::Identifier(s)) => {
|
||||
Some(Token::Name(s)) => {
|
||||
self.consume();
|
||||
Ok(FunctionName::new(s))
|
||||
}
|
||||
Some(Token::HexSequence(s)) => {
|
||||
if s.len() % 2 != 0 {
|
||||
return err!(self.loc,
|
||||
"expected binary function name to have length multiple of two");
|
||||
}
|
||||
let mut bin_name = Vec::with_capacity(s.len() / 2);
|
||||
let mut i = 0;
|
||||
while i + 2 <= s.len() {
|
||||
let byte = u8::from_str_radix(&s[i..i + 2], 16).unwrap();
|
||||
bin_name.push(byte);
|
||||
i += 2;
|
||||
}
|
||||
self.consume();
|
||||
Ok(FunctionName::new(bin_name))
|
||||
}
|
||||
_ => err!(self.loc, "expected function name"),
|
||||
}
|
||||
}
|
||||
@@ -1723,7 +1738,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn aliases() {
|
||||
let (func, details) = Parser::new("function qux() {
|
||||
let (func, details) = Parser::new("function %qux() {
|
||||
ebb0:
|
||||
v4 = iconst.i8 6
|
||||
v3 -> v4
|
||||
@@ -1731,7 +1746,7 @@ mod tests {
|
||||
}")
|
||||
.parse_function(None)
|
||||
.unwrap();
|
||||
assert_eq!(func.name.to_string(), "qux");
|
||||
assert_eq!(func.name.to_string(), "%qux");
|
||||
let v4 = details.map.lookup_str("v4").unwrap();
|
||||
assert_eq!(v4.to_string(), "v0");
|
||||
let v3 = details.map.lookup_str("v3").unwrap();
|
||||
@@ -1777,13 +1792,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn stack_slot_decl() {
|
||||
let (func, _) = Parser::new("function foo() {
|
||||
let (func, _) = Parser::new("function %foo() {
|
||||
ss3 = stack_slot 13
|
||||
ss1 = stack_slot 1
|
||||
}")
|
||||
.parse_function(None)
|
||||
.unwrap();
|
||||
assert_eq!(func.name.to_string(), "foo");
|
||||
assert_eq!(func.name.to_string(), "%foo");
|
||||
let mut iter = func.stack_slots.keys();
|
||||
let ss0 = iter.next().unwrap();
|
||||
assert_eq!(ss0.to_string(), "ss0");
|
||||
@@ -1794,7 +1809,7 @@ mod tests {
|
||||
assert_eq!(iter.next(), None);
|
||||
|
||||
// Catch duplicate definitions.
|
||||
assert_eq!(Parser::new("function bar() {
|
||||
assert_eq!(Parser::new("function %bar() {
|
||||
ss1 = stack_slot 13
|
||||
ss1 = stack_slot 1
|
||||
}")
|
||||
@@ -1806,13 +1821,13 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ebb_header() {
|
||||
let (func, _) = Parser::new("function ebbs() {
|
||||
let (func, _) = Parser::new("function %ebbs() {
|
||||
ebb0:
|
||||
ebb4(v3: i32):
|
||||
}")
|
||||
.parse_function(None)
|
||||
.unwrap();
|
||||
assert_eq!(func.name.to_string(), "ebbs");
|
||||
assert_eq!(func.name.to_string(), "%ebbs");
|
||||
|
||||
let mut ebbs = func.layout.ebbs();
|
||||
|
||||
@@ -1828,7 +1843,7 @@ mod tests {
|
||||
#[test]
|
||||
fn comments() {
|
||||
let (func, Details { comments, .. }) = Parser::new("; before
|
||||
function comment() { ; decl
|
||||
function %comment() { ; decl
|
||||
ss10 = stack_slot 13 ; stackslot.
|
||||
; Still stackslot.
|
||||
jt10 = jump_table ebb0
|
||||
@@ -1839,7 +1854,7 @@ mod tests {
|
||||
; More trailing.")
|
||||
.parse_function(None)
|
||||
.unwrap();
|
||||
assert_eq!(func.name.to_string(), "comment");
|
||||
assert_eq!(func.name.to_string(), "%comment");
|
||||
assert_eq!(comments.len(), 8); // no 'before' comment.
|
||||
assert_eq!(comments[0],
|
||||
Comment {
|
||||
@@ -1868,7 +1883,7 @@ mod tests {
|
||||
test verify
|
||||
set enable_float=false
|
||||
; still preamble
|
||||
function comment() {}")
|
||||
function %comment() {}")
|
||||
.unwrap();
|
||||
assert_eq!(tf.commands.len(), 2);
|
||||
assert_eq!(tf.commands[0].command, "cfg");
|
||||
@@ -1884,23 +1899,23 @@ mod tests {
|
||||
assert_eq!(tf.preamble_comments[0].text, "; before");
|
||||
assert_eq!(tf.preamble_comments[1].text, "; still preamble");
|
||||
assert_eq!(tf.functions.len(), 1);
|
||||
assert_eq!(tf.functions[0].0.name.to_string(), "comment");
|
||||
assert_eq!(tf.functions[0].0.name.to_string(), "%comment");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn isa_spec() {
|
||||
assert!(parse_test("isa
|
||||
function foo() {}")
|
||||
function %foo() {}")
|
||||
.is_err());
|
||||
|
||||
assert!(parse_test("isa riscv
|
||||
set enable_float=false
|
||||
function foo() {}")
|
||||
function %foo() {}")
|
||||
.is_err());
|
||||
|
||||
match parse_test("set enable_float=false
|
||||
isa riscv
|
||||
function foo() {}")
|
||||
function %foo() {}")
|
||||
.unwrap()
|
||||
.isa_spec {
|
||||
IsaSpec::None(_) => panic!("Expected some ISA"),
|
||||
@@ -1910,4 +1925,41 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binary_function_name() {
|
||||
// Valid characters in the name.
|
||||
let func = Parser::new("function #1234567890AbCdEf() {
|
||||
ebb0:
|
||||
trap
|
||||
}")
|
||||
.parse_function(None)
|
||||
.unwrap()
|
||||
.0;
|
||||
assert_eq!(func.name.to_string(), "#1234567890abcdef");
|
||||
|
||||
// Invalid characters in the name.
|
||||
let mut parser = Parser::new("function #12ww() {
|
||||
ebb0:
|
||||
trap
|
||||
}");
|
||||
assert!(parser.parse_function(None).is_err());
|
||||
|
||||
// The length of binary function name should be multiple of two.
|
||||
let mut parser = Parser::new("function #1() {
|
||||
ebb0:
|
||||
trap
|
||||
}");
|
||||
assert!(parser.parse_function(None).is_err());
|
||||
|
||||
// Empty binary function name should be valid.
|
||||
let func = Parser::new("function #() {
|
||||
ebb0:
|
||||
trap
|
||||
}")
|
||||
.parse_function(None)
|
||||
.unwrap()
|
||||
.0;
|
||||
assert_eq!(func.name.to_string(), "%");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn details() {
|
||||
let tf = parse_test("function detail() {
|
||||
let tf = parse_test("function %detail() {
|
||||
ss10 = stack_slot 13
|
||||
jt10 = jump_table ebb0
|
||||
ebb0(v4: i32, v7: i32):
|
||||
|
||||
Reference in New Issue
Block a user