Skip to content

Commit 17f35f4

Browse files
chore: Move from IntrinsicType::target to intrinsicType::metadata to
support architecture-specific use cases
1 parent eec7134 commit 17f35f4

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,25 @@ fn json_to_intrinsic(
7979
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
8080
let name = intr.name.replace(['[', ']'], "");
8181

82-
let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?;
82+
let mut results = ArmIntrinsicType::from_c(&intr.return_type.value)?;
83+
results.set_metadata("target".to_string(), target.to_string());
8384

8485
let args = intr
8586
.arguments
8687
.into_iter()
8788
.enumerate()
8889
.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());
90+
let (type_name, arg_name) = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg);
91+
let ty = ArmIntrinsicType::from_c(type_name)
92+
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
93+
94+
let arg_prep = intr.args_prep.as_mut();
95+
let arg_prep = arg_prep.and_then(|a| a.remove(arg_name));
96+
let arg_prep: Option<ArgPrep> = arg_prep.and_then(|a| a.try_into().ok());
9397
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
9498

95-
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint);
99+
let mut arg =
100+
Argument::<ArmIntrinsicType>::new(i, arg_name.to_string(), ty, constraint);
96101

97102
// The JSON doesn't list immediates as const
98103
let IntrinsicType {

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: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashMap;
2+
13
use super::intrinsic::ArmIntrinsicType;
24
use crate::common::cli::Language;
35
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, Sign, TypeKind};
@@ -40,7 +42,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
4042
bit_len: Some(bl),
4143
simd_len,
4244
vec_len,
43-
target,
45+
metadata,
4446
..
4547
} = &self.0
4648
{
@@ -50,7 +52,11 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
5052
""
5153
};
5254

53-
let choose_workaround = language == Language::C && target.contains("v7");
55+
let choose_workaround = language == Language::C
56+
&& metadata
57+
.get("target")
58+
.filter(|value| value.contains("v7"))
59+
.is_some();
5460
format!(
5561
"vld{len}{quad}_{type}{size}",
5662
type = match k {
@@ -102,15 +108,17 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
102108
}
103109
}
104110

105-
fn from_c(s: &str, target: &str) -> Result<Self, String> {
111+
fn from_c(s: &str) -> Result<Self, String> {
106112
const CONST_STR: &str = "const";
113+
let mut metadata: HashMap<String, String> = HashMap::new();
114+
metadata.insert("type".to_string(), s.to_string());
107115
if let Some(s) = s.strip_suffix('*') {
108116
let (s, constant) = match s.trim().strip_suffix(CONST_STR) {
109117
Some(stripped) => (stripped, true),
110118
None => (s, false),
111119
};
112120
let s = s.trim_end();
113-
let temp_return = ArmIntrinsicType::from_c(s, target);
121+
let temp_return = ArmIntrinsicType::from_c(s);
114122
temp_return.map(|mut op| {
115123
op.ptr = true;
116124
op.ptr_constant = constant;
@@ -151,7 +159,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
151159
bit_len: Some(bit_len),
152160
simd_len,
153161
vec_len,
154-
target: target.to_string(),
162+
metadata,
155163
}))
156164
} else {
157165
let kind = start.parse::<TypeKind>()?;
@@ -167,7 +175,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
167175
bit_len,
168176
simd_len: None,
169177
vec_len: None,
170-
target: target.to_string(),
178+
metadata,
171179
}))
172180
}
173181
}

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ impl<T> Argument<T>
2020
where
2121
T: IntrinsicTypeDefinition,
2222
{
23+
pub fn new(pos: usize, name: String, ty: T, constraint: Option<Constraint>) -> Self {
24+
Argument {
25+
pos,
26+
name,
27+
ty,
28+
constraint,
29+
}
30+
}
31+
2332
pub fn to_c_type(&self) -> String {
2433
self.ty.c_type()
2534
}
@@ -36,14 +45,6 @@ where
3645
self.constraint.is_some()
3746
}
3847

39-
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
40-
let split_index = arg
41-
.rfind([' ', '*'])
42-
.expect("Couldn't split type and argname");
43-
44-
(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
45-
}
46-
4748
/// The binding keyword (e.g. "const" or "let") for the array of possible test inputs.
4849
fn rust_vals_array_binding(&self) -> impl std::fmt::Display {
4950
if self.ty.is_rust_vals_array_const() {
@@ -62,25 +63,6 @@ where
6263
}
6364
}
6465

65-
pub fn from_c(
66-
pos: usize,
67-
arg: &str,
68-
target: &str,
69-
constraint: Option<Constraint>,
70-
) -> Argument<T> {
71-
let (ty, var_name) = Self::type_and_name_from_c(arg);
72-
73-
let ty =
74-
T::from_c(ty, target).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
75-
76-
Argument {
77-
pos,
78-
name: String::from(var_name),
79-
ty: ty,
80-
constraint,
81-
}
82-
}
83-
8466
fn as_call_param_c(&self) -> String {
8567
self.ty.as_call_param_c(&self.name)
8668
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fmt;
23
use std::ops::Deref;
34
use std::str::FromStr;
@@ -121,7 +122,7 @@ pub struct IntrinsicType {
121122
/// A value of `None` can be assumed to be 1 though.
122123
pub vec_len: Option<u32>,
123124

124-
pub target: String,
125+
pub metadata: HashMap<String, String>,
125126
}
126127

127128
impl IntrinsicType {
@@ -153,6 +154,10 @@ impl IntrinsicType {
153154
self.ptr
154155
}
155156

157+
pub fn set_metadata(&mut self, key: String, value: String) {
158+
self.metadata.insert(key, value);
159+
}
160+
156161
pub fn c_scalar_type(&self) -> String {
157162
match self.kind() {
158163
TypeKind::Char(_) => String::from("char"),
@@ -322,7 +327,7 @@ pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> {
322327
fn get_lane_function(&self) -> String;
323328

324329
/// can be implemented in an `impl` block
325-
fn from_c(_s: &str, _target: &str) -> Result<Self, String>
330+
fn from_c(_s: &str) -> Result<Self, String>
326331
where
327332
Self: Sized;
328333

0 commit comments

Comments
 (0)