Skip to content

Commit 980e969

Browse files
chore: Move from IntrinsicType::target to intrinsicType::metadata to
support architecture-specific use cases
1 parent dcad42e commit 980e969

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};
@@ -59,7 +61,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
5961
bit_len: Some(bl),
6062
simd_len,
6163
vec_len,
62-
target,
64+
metadata,
6365
..
6466
} = &self.0
6567
{
@@ -69,7 +71,11 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
6971
""
7072
};
7173

72-
let choose_workaround = language == Language::C && target.contains("v7");
74+
let choose_workaround = language == Language::C
75+
&& metadata
76+
.get("target")
77+
.filter(|value| value.contains("v7"))
78+
.is_some();
7379
format!(
7480
"vld{len}{quad}_{type}{size}",
7581
type = match k {
@@ -121,15 +127,17 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
121127
}
122128
}
123129

124-
fn from_c(s: &str, target: &str) -> Result<Self, String> {
130+
fn from_c(s: &str) -> Result<Self, String> {
125131
const CONST_STR: &str = "const";
132+
let mut metadata: HashMap<String, String> = HashMap::new();
133+
metadata.insert("type".to_string(), s.to_string());
126134
if let Some(s) = s.strip_suffix('*') {
127135
let (s, constant) = match s.trim().strip_suffix(CONST_STR) {
128136
Some(stripped) => (stripped, true),
129137
None => (s, false),
130138
};
131139
let s = s.trim_end();
132-
let temp_return = ArmIntrinsicType::from_c(s, target);
140+
let temp_return = ArmIntrinsicType::from_c(s);
133141
temp_return.map(|mut op| {
134142
op.ptr = true;
135143
op.ptr_constant = constant;
@@ -170,7 +178,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
170178
bit_len: Some(bit_len),
171179
simd_len,
172180
vec_len,
173-
target: target.to_string(),
181+
metadata,
174182
}))
175183
} else {
176184
let kind = start.parse::<TypeKind>()?;
@@ -186,7 +194,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
186194
bit_len,
187195
simd_len: None,
188196
vec_len: None,
189-
target: target.to_string(),
197+
metadata,
190198
}))
191199
}
192200
}

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)