Skip to content

Commit

Permalink
section-mangler: Add prefix constant, derive Debug and PartialEq
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Mar 27, 2024
1 parent 5ffee18 commit c66b9eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 10 additions & 2 deletions compiler/section_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionArgument>,
return_type: Option<Type>,
}

#[derive(Debug, PartialEq)]
pub struct VariableMangler {
name: String,
ty: Type,
Expand Down Expand Up @@ -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),
Expand All @@ -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`
Expand All @@ -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,
Expand Down Expand Up @@ -237,16 +243,18 @@ 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
.into_iter()
/* 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}")
}
14 changes: 12 additions & 2 deletions compiler/section_mangler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn parse_prefix(input: &str) -> ParseResult<Prefix> {
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))
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
}
}

0 comments on commit c66b9eb

Please sign in to comment.