Skip to content

Commit 2c7ef96

Browse files
feat: made constraint common
1 parent f4650c0 commit 2c7ef96

File tree

9 files changed

+78
-114
lines changed

9 files changed

+78
-114
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_types::{
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_types::{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-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +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_types::IntrinsicTypeDefinition;
4-
use serde_json::Value;
55

66
/// An argument for the intrinsic.
77
#[derive(Debug, PartialEq, Clone)]
8-
pub struct Argument<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
8+
pub struct Argument<T: IntrinsicTypeDefinition> {
99
/// The argument's index in the intrinsic function call.
1010
pub pos: usize,
1111
/// The argument name.
1212
pub name: String,
1313
/// The type of the argument.
1414
pub ty: T,
1515
/// Any constraints that are on this argument
16-
pub metadata: Vec<M>,
16+
pub constraint: Option<Constraint>,
1717
}
1818

19-
pub trait MetadataDefinition {
20-
fn from_metadata(metadata: Option<Value>) -> Vec<Box<Self>>;
21-
}
22-
23-
impl<T, M> Argument<T, M>
19+
impl<T> Argument<T>
2420
where
2521
T: IntrinsicTypeDefinition,
26-
M: MetadataDefinition,
2722
{
2823
pub fn to_c_type(&self) -> String {
2924
self.ty.c_type()
@@ -38,7 +33,7 @@ where
3833
}
3934

4035
pub fn has_constraint(&self) -> bool {
41-
!self.metadata.is_empty()
36+
!self.constraint.is_some()
4237
}
4338

4439
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
@@ -71,20 +66,18 @@ where
7166
pos: usize,
7267
arg: &str,
7368
target: &String,
74-
metadata: Option<Value>,
75-
) -> Argument<T, M> {
69+
constraint: Option<Constraint>,
70+
) -> Argument<T> {
7671
let (ty, var_name) = Self::type_and_name_from_c(arg);
7772

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

81-
let metadata: Vec<M> = M::from_metadata(metadata).into_iter().map(|b| *b).collect();
82-
8376
Argument {
8477
pos,
8578
name: String::from(var_name),
8679
ty: *ty,
87-
metadata,
80+
constraint,
8881
}
8982
}
9083

@@ -94,14 +87,13 @@ where
9487
}
9588

9689
#[derive(Debug, PartialEq, Clone)]
97-
pub struct ArgumentList<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
98-
pub args: Vec<Argument<T, M>>,
90+
pub struct ArgumentList<T: IntrinsicTypeDefinition> {
91+
pub args: Vec<Argument<T>>,
9992
}
10093

101-
impl<T, M> ArgumentList<T, M>
94+
impl<T> ArgumentList<T>
10295
where
10396
T: IntrinsicTypeDefinition,
104-
M: MetadataDefinition,
10597
{
10698
/// Converts the argument list into the call parameters for a C function call.
10799
/// e.g. this would generate something like `a, &b, c`
@@ -218,7 +210,7 @@ where
218210
.collect()
219211
}
220212

221-
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T, M>> {
213+
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T>> {
222214
self.args.iter()
223215
}
224216
}
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)