Skip to content

Commit 030ce32

Browse files
feat: made constraint common
1 parent dc5c5a1 commit 030ce32

File tree

9 files changed

+78
-113
lines changed

9 files changed

+78
-113
lines changed

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

-60
This file was deleted.

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices};
22
use super::intrinsic::ArmIntrinsicType;
3-
use crate::arm::constraint::Constraint;
43
use crate::common::argument::Argument;
54
use crate::common::compile_c::CompilationCommandBuilder;
65
use crate::common::gen_c::{compile_c, create_c_filenames, generate_c_program};
@@ -17,14 +16,14 @@ const PASSES: u32 = 20;
1716

1817
fn gen_code_c(
1918
indentation: Indentation,
20-
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
21-
constraints: &[&Argument<ArmIntrinsicType, Constraint>],
19+
intrinsic: &Intrinsic<ArmIntrinsicType>,
20+
constraints: &[&Argument<ArmIntrinsicType>],
2221
name: String,
2322
target: &str,
2423
) -> String {
2524
if let Some((current, constraints)) = constraints.split_last() {
2625
let range = current
27-
.metadata
26+
.constraint
2827
.iter()
2928
.map(|c| c.to_range())
3029
.flat_map(|r| r.into_iter());
@@ -57,7 +56,7 @@ fn gen_code_c(
5756

5857
fn generate_c_program_arm(
5958
header_files: &[&str],
60-
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
59+
intrinsic: &Intrinsic<ArmIntrinsicType>,
6160
target: &str,
6261
) -> String {
6362
let constraints = intrinsic
@@ -89,13 +88,14 @@ fn generate_c_program_arm(
8988

9089
fn gen_code_rust(
9190
indentation: Indentation,
92-
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
93-
constraints: &[&Argument<ArmIntrinsicType, Constraint>],
91+
intrinsic: &Intrinsic<ArmIntrinsicType>,
92+
constraints: &[&Argument<ArmIntrinsicType>],
9493
name: String,
9594
) -> String {
95+
println!("{}", name);
9696
if let Some((current, constraints)) = constraints.split_last() {
9797
let range = current
98-
.metadata
98+
.constraint
9999
.iter()
100100
.map(|c| c.to_range())
101101
.flat_map(|r| r.into_iter());
@@ -125,10 +125,7 @@ fn gen_code_rust(
125125
}
126126
}
127127

128-
fn generate_rust_program_arm(
129-
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
130-
target: &str,
131-
) -> String {
128+
fn generate_rust_program_arm(intrinsic: &Intrinsic<ArmIntrinsicType>, target: &str) -> String {
132129
let constraints = intrinsic
133130
.arguments
134131
.iter()
@@ -222,7 +219,7 @@ fn compile_c_arm(
222219
}
223220

224221
pub fn build_c(
225-
intrinsics: &Vec<Intrinsic<ArmIntrinsicType, Constraint>>,
222+
intrinsics: &Vec<Intrinsic<ArmIntrinsicType>>,
226223
compiler: Option<&str>,
227224
target: &str,
228225
cxx_toolchain_dir: Option<&str>,
@@ -248,7 +245,7 @@ pub fn build_c(
248245
}
249246

250247
pub fn build_rust(
251-
intrinsics: &[Intrinsic<ArmIntrinsicType, Constraint>],
248+
intrinsics: &[Intrinsic<ArmIntrinsicType>],
252249
toolchain: Option<&str>,
253250
target: &str,
254251
linker: Option<&str>,

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::constraint::Constraint;
21
use crate::base_intrinsictype_trait_def_macro;
32
use crate::common::argument::ArgumentList;
43
use crate::common::cli::Language;
@@ -10,8 +9,8 @@ use crate::common::intrinsic_helpers::{
109

1110
base_intrinsictype_trait_def_macro! {ArmIntrinsicType}
1211

13-
impl IntrinsicDefinition<ArmIntrinsicType, Constraint> for Intrinsic<ArmIntrinsicType, Constraint> {
14-
fn arguments(&self) -> ArgumentList<ArmIntrinsicType, Constraint> {
12+
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
13+
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {
1514
self.arguments.clone()
1615
}
1716

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

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use super::constraint::Constraint;
21
use super::intrinsic::ArmIntrinsicType;
32
use crate::common::argument::{Argument, ArgumentList};
3+
use crate::common::constraint::Constraint;
44
use crate::common::intrinsic::Intrinsic;
55
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition};
66
use serde::Deserialize;
@@ -55,7 +55,7 @@ struct JsonIntrinsic {
5555
pub fn get_neon_intrinsics(
5656
filename: &Path,
5757
target: &String,
58-
) -> Result<Vec<Intrinsic<ArmIntrinsicType, Constraint>>, Box<dyn std::error::Error>> {
58+
) -> Result<Vec<Intrinsic<ArmIntrinsicType>>, Box<dyn std::error::Error>> {
5959
let file = std::fs::File::open(filename)?;
6060
let reader = std::io::BufReader::new(file);
6161
let json: Vec<JsonIntrinsic> = serde_json::from_reader(reader).expect("Couldn't parse JSON");
@@ -76,7 +76,7 @@ pub fn get_neon_intrinsics(
7676
fn json_to_intrinsic(
7777
mut intr: JsonIntrinsic,
7878
target: &String,
79-
) -> Result<Intrinsic<ArmIntrinsicType, Constraint>, Box<dyn std::error::Error>> {
79+
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
8080
let name = intr.name.replace(['[', ']'], "");
8181

8282
let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?;
@@ -86,11 +86,13 @@ fn json_to_intrinsic(
8686
.into_iter()
8787
.enumerate()
8888
.map(|(i, arg)| {
89-
let arg_name = Argument::<ArmIntrinsicType, Constraint>::type_and_name_from_c(&arg).1;
89+
let arg_name = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg).1;
9090
let metadata = intr.args_prep.as_mut();
9191
let metadata = metadata.and_then(|a| a.remove(arg_name));
92-
let mut arg =
93-
Argument::<ArmIntrinsicType, Constraint>::from_c(i, &arg, target, metadata);
92+
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| a.try_into().ok());
93+
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
94+
95+
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint);
9496

9597
// The JSON doesn't list immediates as const
9698
let IntrinsicType {
@@ -103,7 +105,7 @@ fn json_to_intrinsic(
103105
})
104106
.collect();
105107

106-
let arguments = ArgumentList::<ArmIntrinsicType, Constraint> { args };
108+
let arguments = ArgumentList::<ArmIntrinsicType> { args };
107109

108110
Ok(Intrinsic {
109111
name,
@@ -112,3 +114,24 @@ fn json_to_intrinsic(
112114
arch_tags: intr.architectures,
113115
})
114116
}
117+
118+
/// ARM-specific
119+
impl TryFrom<ArgPrep> for Constraint {
120+
type Error = ();
121+
122+
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> {
123+
let parsed_ints = match prep {
124+
ArgPrep::Immediate { min, max } => Ok((min, max)),
125+
_ => Err(()),
126+
};
127+
if let Ok((min, max)) = parsed_ints {
128+
if min == max {
129+
Ok(Constraint::Equal(min))
130+
} else {
131+
Ok(Constraint::Range(min..max + 1))
132+
}
133+
} else {
134+
Err(())
135+
}
136+
}
137+
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
mod config;
2-
mod constraint;
32
mod functions;
43
mod intrinsic;
54
mod json_parser;
65
mod types;
76

8-
use crate::arm::constraint::Constraint;
97
use crate::arm::intrinsic::ArmIntrinsicType;
108
use crate::common::SupportedArchitectureTest;
119
use crate::common::cli::ProcessedCli;
@@ -16,7 +14,7 @@ use functions::{build_c, build_rust};
1614
use json_parser::get_neon_intrinsics;
1715

1816
pub struct ArmArchitectureTest {
19-
intrinsics: Vec<Intrinsic<ArmIntrinsicType, Constraint>>,
17+
intrinsics: Vec<Intrinsic<ArmIntrinsicType>>,
2018
cli_options: ProcessedCli,
2119
}
2220

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

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
use crate::common::cli::Language;
2+
use crate::common::constraint::Constraint;
23
use crate::common::indentation::Indentation;
34
use crate::common::intrinsic_helpers::IntrinsicTypeDefinition;
45

56
/// An argument for the intrinsic.
67
#[derive(Debug, PartialEq, Clone)]
7-
pub struct Argument<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
8+
pub struct Argument<T: IntrinsicTypeDefinition> {
89
/// The argument's index in the intrinsic function call.
910
pub pos: usize,
1011
/// The argument name.
1112
pub name: String,
1213
/// The type of the argument.
1314
pub ty: T,
1415
/// Any constraints that are on this argument
15-
pub metadata: Vec<M>,
16+
pub constraint: Option<Constraint>,
1617
}
1718

18-
pub trait MetadataDefinition {
19-
fn from_metadata(metadata: Option<Value>) -> Vec<Box<Self>>;
20-
}
21-
22-
impl<T, M> Argument<T, M>
19+
impl<T> Argument<T>
2320
where
2421
T: IntrinsicTypeDefinition,
25-
M: MetadataDefinition,
2622
{
2723
pub fn to_c_type(&self) -> String {
2824
self.ty.c_type()
@@ -37,7 +33,7 @@ where
3733
}
3834

3935
pub fn has_constraint(&self) -> bool {
40-
!self.metadata.is_empty()
36+
!self.constraint.is_some()
4137
}
4238

4339
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
@@ -70,20 +66,18 @@ where
7066
pos: usize,
7167
arg: &str,
7268
target: &String,
73-
metadata: Option<Value>,
74-
) -> Argument<T, M> {
69+
constraint: Option<Constraint>,
70+
) -> Argument<T> {
7571
let (ty, var_name) = Self::type_and_name_from_c(arg);
7672

7773
let ty =
7874
T::from_c(ty, target).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
7975

80-
let metadata: Vec<M> = M::from_metadata(metadata).into_iter().map(|b| *b).collect();
81-
8276
Argument {
8377
pos,
8478
name: String::from(var_name),
8579
ty: *ty,
86-
metadata,
80+
constraint,
8781
}
8882
}
8983

@@ -93,14 +87,13 @@ where
9387
}
9488

9589
#[derive(Debug, PartialEq, Clone)]
96-
pub struct ArgumentList<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
97-
pub args: Vec<Argument<T, M>>,
90+
pub struct ArgumentList<T: IntrinsicTypeDefinition> {
91+
pub args: Vec<Argument<T>>,
9892
}
9993

100-
impl<T, M> ArgumentList<T, M>
94+
impl<T> ArgumentList<T>
10195
where
10296
T: IntrinsicTypeDefinition,
103-
M: MetadataDefinition,
10497
{
10598
/// Converts the argument list into the call parameters for a C function call.
10699
/// e.g. this would generate something like `a, &b, c`
@@ -217,7 +210,7 @@ where
217210
.collect()
218211
}
219212

220-
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T, M>> {
213+
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T>> {
221214
self.args.iter()
222215
}
223216
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use serde::Deserialize;
2+
use std::ops::Range;
3+
4+
#[derive(Debug, PartialEq, Clone, Deserialize)]
5+
pub enum Constraint {
6+
Equal(i64),
7+
Range(Range<i64>),
8+
}
9+
10+
impl Constraint {
11+
pub fn to_range(&self) -> Range<i64> {
12+
match self {
13+
Constraint::Equal(eq) => *eq..*eq + 1,
14+
Constraint::Range(range) => range.clone(),
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)