Skip to content

Commit 86e1a16

Browse files
feat: fetching c_type representation from IntrinsicType's hashmap directly instead of
constructing it. Further details: 1. moved "type_and_name_from_c" from Argument<T> to Argument<ArmIntrinsicType> 2. removed "from_c" in Argument<T>
1 parent f72a1e6 commit 86e1a16

File tree

6 files changed

+41
-54
lines changed

6 files changed

+41
-54
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::arm::intrinsic::ArmIntrinsicType;
2+
use crate::common::argument::Argument;
3+
4+
impl Argument<ArmIntrinsicType> {
5+
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
6+
let split_index = arg
7+
.rfind([' ', '*'])
8+
.expect("Couldn't split type and argname");
9+
10+
(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
11+
}
12+
}

crates/intrinsic-test/src/arm/json_parser.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ fn json_to_intrinsic(
8686
.into_iter()
8787
.enumerate()
8888
.map(|(i, arg)| {
89-
let arg_name = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg).1;
90-
let metadata = intr.args_prep.as_mut();
91-
let metadata = metadata.and_then(|a| a.remove(arg_name));
92-
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| a.try_into().ok());
89+
let (type_name, arg_name) = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg);
90+
let ty = ArmIntrinsicType::from_c(type_name)
91+
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
92+
93+
let arg_prep = intr.args_prep.as_mut();
94+
let arg_prep = arg_prep.and_then(|a| a.remove(arg_name));
95+
let arg_prep: Option<ArgPrep> = arg_prep.and_then(|a| a.try_into().ok());
9396
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
9497

95-
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, constraint);
98+
let mut arg =
99+
Argument::<ArmIntrinsicType>::new(i, arg_name.to_string(), ty, constraint);
96100
arg.ty
97101
.set_metadata("target".to_string(), target.to_string());
98102

crates/intrinsic-test/src/arm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod argument;
12
mod compile;
23
mod config;
34
mod intrinsic;

crates/intrinsic-test/src/arm/types.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@ use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, T
66

77
impl IntrinsicTypeDefinition for ArmIntrinsicType {
88
/// Gets a string containing the typename for this type in C format.
9+
/// This assumes that the metadata hashmap contains this value at the
10+
/// "type" key
911
fn c_type(&self) -> String {
10-
let prefix = self.0.kind.c_prefix();
11-
let const_prefix = if self.0.constant { "const " } else { "" };
12-
13-
if let (Some(bit_len), simd_len, vec_len) =
14-
(self.0.bit_len, self.0.simd_len, self.0.vec_len)
15-
{
16-
match (simd_len, vec_len) {
17-
(None, None) => format!("{const_prefix}{prefix}{bit_len}_t"),
18-
(Some(simd), None) => format!("{prefix}{bit_len}x{simd}_t"),
19-
(Some(simd), Some(vec)) => format!("{prefix}{bit_len}x{simd}x{vec}_t"),
20-
(None, Some(_)) => todo!("{:#?}", self), // Likely an invalid case
21-
}
22-
} else {
23-
todo!("{:#?}", self)
24-
}
12+
self.metadata
13+
.get("type")
14+
.expect("Failed to extract the C typename in Aarch!")
15+
.to_string()
2516
}
2617

2718
fn c_single_vector_type(&self) -> String {
@@ -129,6 +120,8 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
129120

130121
fn from_c(s: &str) -> Result<Self, String> {
131122
const CONST_STR: &str = "const";
123+
let mut metadata: HashMap<String, String> = HashMap::new();
124+
metadata.insert("type".to_string(), s.to_string());
132125
if let Some(s) = s.strip_suffix('*') {
133126
let (s, constant) = match s.trim().strip_suffix(CONST_STR) {
134127
Some(stripped) => (stripped, true),
@@ -176,7 +169,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
176169
bit_len: Some(bit_len),
177170
simd_len,
178171
vec_len,
179-
metadata: HashMap::new(),
172+
metadata,
180173
}))
181174
} else {
182175
let kind = start.parse::<TypeKind>()?;
@@ -192,7 +185,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
192185
bit_len,
193186
simd_len: None,
194187
vec_len: None,
195-
metadata: HashMap::new(),
188+
metadata,
196189
}))
197190
}
198191
}

crates/intrinsic-test/src/common/argument.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ where
4545
self.constraint.is_some()
4646
}
4747

48-
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
49-
let split_index = arg
50-
.rfind([' ', '*'])
51-
.expect("Couldn't split type and argname");
52-
53-
(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
54-
}
55-
5648
/// The binding keyword (e.g. "const" or "let") for the array of possible test inputs.
5749
fn rust_vals_array_binding(&self) -> impl std::fmt::Display {
5850
if self.ty.is_rust_vals_array_const() {
@@ -71,19 +63,6 @@ where
7163
}
7264
}
7365

74-
pub fn from_c(pos: usize, arg: &str, constraint: Option<Constraint>) -> Argument<T> {
75-
let (ty, var_name) = Self::type_and_name_from_c(arg);
76-
77-
let ty = T::from_c(ty).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
78-
79-
Argument {
80-
pos,
81-
name: String::from(var_name),
82-
ty: ty,
83-
constraint,
84-
}
85-
}
86-
8766
fn as_call_param_c(&self) -> String {
8867
self.ty.as_call_param_c(&self.name)
8968
}

crates/intrinsic-test/src/x86/types.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ use crate::common::cli::Language;
66
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
77

88
impl IntrinsicTypeDefinition for X86IntrinsicType {
9-
/// Gets a string containing the typename for this type in C format.
9+
/// Gets a string containing the type in C format.
10+
/// This function assumes that this value is present in the metadata hashmap.
1011
fn c_type(&self) -> String {
11-
let part_0 = if self.constant { "const" } else { "" };
12-
let part_1 = match self.kind {
13-
TypeKind::Int(false) => "unsigned int",
14-
TypeKind::Char(false) => "unsigned char",
15-
_ => self.kind.c_prefix(),
16-
};
17-
let part_2 = if self.ptr { "*" } else { "" };
18-
19-
String::from(vec![part_0, part_1, part_2].join(" ").trim())
12+
self.metadata
13+
.get("type")
14+
.expect("Failed to extract the C typename in X86!")
15+
.to_string()
2016
}
2117

2218
fn c_single_vector_type(&self) -> String {
@@ -39,6 +35,8 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
3935

4036
fn from_c(s: &str) -> Result<Self, String> {
4137
let mut s_copy = s.to_string();
38+
let mut metadata: HashMap<String, String> = HashMap::new();
39+
metadata.insert("type".to_string(), s.to_string());
4240
s_copy = s_copy
4341
.replace("*", "")
4442
.replace("constexpr", "")
@@ -89,7 +87,7 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
8987
bit_len: None,
9088
simd_len: None,
9189
vec_len: None,
92-
metadata: HashMap::new(),
90+
metadata,
9391
}))
9492
}
9593
}

0 commit comments

Comments
 (0)