diff --git a/compiler/section_mangler/src/lib.rs b/compiler/section_mangler/src/lib.rs index f1904923b9..b13b95e199 100644 --- a/compiler/section_mangler/src/lib.rs +++ b/compiler/section_mangler/src/lib.rs @@ -49,17 +49,20 @@ mod parser; /// The main builder type of this crate. Use it to create mangling contexts, in /// order to encode and decode binary type information. // TODO: Add example code for using this builder +#[derive(Debug, PartialEq)] pub enum SectionMangler { Function(FunctionMangler), Variable(VariableMangler), } +#[derive(Debug, PartialEq)] pub struct FunctionMangler { name: String, parameters: Vec, return_type: Option, } +#[derive(Debug, PartialEq)] pub struct VariableMangler { name: String, ty: Type, @@ -113,6 +116,7 @@ impl SectionMangler { // NOTE: This is called `variable_linkage` in the `MemberInfo` struct. /// We have to encode this because if it changes, the function needs to be reloaded - this is an ABI breakage +#[derive(Debug, PartialEq)] pub enum FunctionArgument { ByValue(Type), ByRef(Type), @@ -129,6 +133,7 @@ impl fmt::Display for FunctionArgument { } // TODO: Do we have to encode this? Does that affect ABI? Probably +#[derive(Debug, PartialEq)] pub enum StringEncoding { // TODO: Should we encode this differently? this could cause problems compared to encoding unsigned types /// Encoded as `8u` @@ -148,6 +153,7 @@ impl fmt::Display for StringEncoding { // This maps directly to the [`DataTypeInformation`] enum in RuSTy - we simply remove some fields and add the ability to encode/decode serialize/deserialize // TODO: Do we have to handle Generic? +#[derive(Debug, PartialEq)] pub enum Type { /// Encoded as `v` Void, @@ -237,6 +243,8 @@ impl fmt::Display for Type { } } +pub const PREFIX: &str = "$RUSTY$"; + // TODO: How to encode variadics? fn mangle_function(FunctionMangler { name, parameters, return_type }: FunctionMangler) -> String { let mangled = parameters @@ -244,9 +252,9 @@ fn mangle_function(FunctionMangler { name, parameters, return_type }: FunctionMa /* FIXME: Is that correct? */ .fold(return_type.unwrap_or(Type::Void).to_string(), |mangled, arg| format!("{mangled}[{arg}]")); - format!("{name}:{mangled}") + format!("{PREFIX}{name}:{mangled}") } fn mangle_variable(VariableMangler { name, ty }: VariableMangler) -> String { - format!("{name}:{ty}") + format!("{PREFIX}{name}:{ty}") } diff --git a/compiler/section_mangler/src/parser.rs b/compiler/section_mangler/src/parser.rs index 092d12c982..e466974e1c 100644 --- a/compiler/section_mangler/src/parser.rs +++ b/compiler/section_mangler/src/parser.rs @@ -18,6 +18,7 @@ fn parse_prefix(input: &str) -> ParseResult { let fn_prefix = tag("fn").map(|_| Prefix::Fn); let var_prefix = tag("var").map(|_| Prefix::Var); + let (input, _) = tag(crate::PREFIX)(input)?; let (input, prefix) = alt((fn_prefix, var_prefix))(input)?; Ok((input, prefix)) @@ -87,8 +88,12 @@ mod tests { #[test] fn parse_prefix_valid() { - assert!(parse_prefix("fn").is_ok()); - assert!(parse_prefix("var").is_ok()); + assert!(parse_prefix("$RUSTY$fn").is_ok()); + assert!(parse_prefix("$RUSTY$var").is_ok()); + + assert!(parse_prefix("$RUSTY$random").is_err()); + assert!(parse_prefix("fn").is_err()); + assert!(parse_prefix("var").is_err()); assert!(parse_prefix("").is_err()); assert!(parse_prefix("a random prefix").is_err()); @@ -138,4 +143,9 @@ mod tests { assert!(type_pointer("p").is_err()); assert!(type_pointer("i0").is_err()); } + + #[test] + fn parse_variable() { + SectionMangler::from("$RUSTY$var-name:u8"); + } }