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:
Aleksey Kuznetsov
2017-06-10 22:30:37 +05:00
committed by Jakob Stoklund Olesen
parent 2f33848fcd
commit 706eef23d3
41 changed files with 306 additions and 208 deletions

View File

@@ -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");
}
}

View File

@@ -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");
}
}