feat: made constraint common
This commit is contained in:
parent
86f23a6c2a
commit
9b4768921f
9 changed files with 78 additions and 113 deletions
|
|
@ -1,60 +0,0 @@
|
|||
use super::json_parser::ArgPrep;
|
||||
|
||||
use crate::common::argument::MetadataDefinition;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Deserialize)]
|
||||
pub enum Constraint {
|
||||
Equal(i64),
|
||||
Range(Range<i64>),
|
||||
}
|
||||
|
||||
impl Constraint {
|
||||
pub fn to_range(&self) -> Range<i64> {
|
||||
match self {
|
||||
Constraint::Equal(eq) => *eq..*eq + 1,
|
||||
Constraint::Range(range) => range.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MetadataDefinition for Constraint {
|
||||
fn from_metadata(metadata: Option<Value>) -> Vec<Box<Self>> {
|
||||
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| {
|
||||
if let Value::Object(_) = a {
|
||||
a.try_into().ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
|
||||
vec![constraint]
|
||||
.into_iter()
|
||||
.filter_map(|a| a)
|
||||
.map(|a| Box::new(a))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// ARM-specific
|
||||
impl TryFrom<ArgPrep> for Constraint {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> {
|
||||
let parsed_ints = match prep {
|
||||
ArgPrep::Immediate { min, max } => Ok((min, max)),
|
||||
_ => Err(()),
|
||||
};
|
||||
if let Ok((min, max)) = parsed_ints {
|
||||
if min == max {
|
||||
Ok(Constraint::Equal(min))
|
||||
} else {
|
||||
Ok(Constraint::Range(min..max + 1))
|
||||
}
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
use super::config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices};
|
||||
use super::intrinsic::ArmIntrinsicType;
|
||||
use crate::arm::constraint::Constraint;
|
||||
use crate::common::argument::Argument;
|
||||
use crate::common::compile_c::CompilationCommandBuilder;
|
||||
use crate::common::gen_c::{compile_c, create_c_filenames, generate_c_program};
|
||||
|
|
@ -17,14 +16,14 @@ const PASSES: u32 = 20;
|
|||
|
||||
fn gen_code_c(
|
||||
indentation: Indentation,
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
|
||||
constraints: &[&Argument<ArmIntrinsicType, Constraint>],
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType>,
|
||||
constraints: &[&Argument<ArmIntrinsicType>],
|
||||
name: String,
|
||||
target: &str,
|
||||
) -> String {
|
||||
if let Some((current, constraints)) = constraints.split_last() {
|
||||
let range = current
|
||||
.metadata
|
||||
.constraint
|
||||
.iter()
|
||||
.map(|c| c.to_range())
|
||||
.flat_map(|r| r.into_iter());
|
||||
|
|
@ -57,7 +56,7 @@ fn gen_code_c(
|
|||
|
||||
fn generate_c_program_arm(
|
||||
header_files: &[&str],
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType>,
|
||||
target: &str,
|
||||
) -> String {
|
||||
let constraints = intrinsic
|
||||
|
|
@ -89,13 +88,14 @@ fn generate_c_program_arm(
|
|||
|
||||
fn gen_code_rust(
|
||||
indentation: Indentation,
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
|
||||
constraints: &[&Argument<ArmIntrinsicType, Constraint>],
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType>,
|
||||
constraints: &[&Argument<ArmIntrinsicType>],
|
||||
name: String,
|
||||
) -> String {
|
||||
println!("{}", name);
|
||||
if let Some((current, constraints)) = constraints.split_last() {
|
||||
let range = current
|
||||
.metadata
|
||||
.constraint
|
||||
.iter()
|
||||
.map(|c| c.to_range())
|
||||
.flat_map(|r| r.into_iter());
|
||||
|
|
@ -125,10 +125,7 @@ fn gen_code_rust(
|
|||
}
|
||||
}
|
||||
|
||||
fn generate_rust_program_arm(
|
||||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>,
|
||||
target: &str,
|
||||
) -> String {
|
||||
fn generate_rust_program_arm(intrinsic: &Intrinsic<ArmIntrinsicType>, target: &str) -> String {
|
||||
let constraints = intrinsic
|
||||
.arguments
|
||||
.iter()
|
||||
|
|
@ -222,7 +219,7 @@ fn compile_c_arm(
|
|||
}
|
||||
|
||||
pub fn build_c(
|
||||
intrinsics: &Vec<Intrinsic<ArmIntrinsicType, Constraint>>,
|
||||
intrinsics: &Vec<Intrinsic<ArmIntrinsicType>>,
|
||||
compiler: Option<&str>,
|
||||
target: &str,
|
||||
cxx_toolchain_dir: Option<&str>,
|
||||
|
|
@ -248,7 +245,7 @@ pub fn build_c(
|
|||
}
|
||||
|
||||
pub fn build_rust(
|
||||
intrinsics: &[Intrinsic<ArmIntrinsicType, Constraint>],
|
||||
intrinsics: &[Intrinsic<ArmIntrinsicType>],
|
||||
toolchain: Option<&str>,
|
||||
target: &str,
|
||||
linker: Option<&str>,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
use super::constraint::Constraint;
|
||||
use crate::base_intrinsictype_trait_def_macro;
|
||||
use crate::common::argument::ArgumentList;
|
||||
use crate::common::cli::Language;
|
||||
|
|
@ -10,8 +9,8 @@ use crate::common::intrinsic_helpers::{
|
|||
|
||||
base_intrinsictype_trait_def_macro! {ArmIntrinsicType}
|
||||
|
||||
impl IntrinsicDefinition<ArmIntrinsicType, Constraint> for Intrinsic<ArmIntrinsicType, Constraint> {
|
||||
fn arguments(&self) -> ArgumentList<ArmIntrinsicType, Constraint> {
|
||||
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
|
||||
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {
|
||||
self.arguments.clone()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use super::constraint::Constraint;
|
||||
use super::intrinsic::ArmIntrinsicType;
|
||||
use crate::common::argument::{Argument, ArgumentList};
|
||||
use crate::common::constraint::Constraint;
|
||||
use crate::common::intrinsic::Intrinsic;
|
||||
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition};
|
||||
use serde::Deserialize;
|
||||
|
|
@ -55,7 +55,7 @@ struct JsonIntrinsic {
|
|||
pub fn get_neon_intrinsics(
|
||||
filename: &Path,
|
||||
target: &String,
|
||||
) -> Result<Vec<Intrinsic<ArmIntrinsicType, Constraint>>, Box<dyn std::error::Error>> {
|
||||
) -> Result<Vec<Intrinsic<ArmIntrinsicType>>, Box<dyn std::error::Error>> {
|
||||
let file = std::fs::File::open(filename)?;
|
||||
let reader = std::io::BufReader::new(file);
|
||||
let json: Vec<JsonIntrinsic> = serde_json::from_reader(reader).expect("Couldn't parse JSON");
|
||||
|
|
@ -76,7 +76,7 @@ pub fn get_neon_intrinsics(
|
|||
fn json_to_intrinsic(
|
||||
mut intr: JsonIntrinsic,
|
||||
target: &String,
|
||||
) -> Result<Intrinsic<ArmIntrinsicType, Constraint>, Box<dyn std::error::Error>> {
|
||||
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
|
||||
let name = intr.name.replace(['[', ']'], "");
|
||||
|
||||
let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?;
|
||||
|
|
@ -86,11 +86,13 @@ fn json_to_intrinsic(
|
|||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(i, arg)| {
|
||||
let arg_name = Argument::<ArmIntrinsicType, Constraint>::type_and_name_from_c(&arg).1;
|
||||
let arg_name = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg).1;
|
||||
let metadata = intr.args_prep.as_mut();
|
||||
let metadata = metadata.and_then(|a| a.remove(arg_name));
|
||||
let mut arg =
|
||||
Argument::<ArmIntrinsicType, Constraint>::from_c(i, &arg, target, metadata);
|
||||
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| a.try_into().ok());
|
||||
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
|
||||
|
||||
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint);
|
||||
|
||||
// The JSON doesn't list immediates as const
|
||||
let IntrinsicType {
|
||||
|
|
@ -103,7 +105,7 @@ fn json_to_intrinsic(
|
|||
})
|
||||
.collect();
|
||||
|
||||
let arguments = ArgumentList::<ArmIntrinsicType, Constraint> { args };
|
||||
let arguments = ArgumentList::<ArmIntrinsicType> { args };
|
||||
|
||||
Ok(Intrinsic {
|
||||
name,
|
||||
|
|
@ -112,3 +114,24 @@ fn json_to_intrinsic(
|
|||
arch_tags: intr.architectures,
|
||||
})
|
||||
}
|
||||
|
||||
/// ARM-specific
|
||||
impl TryFrom<ArgPrep> for Constraint {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> {
|
||||
let parsed_ints = match prep {
|
||||
ArgPrep::Immediate { min, max } => Ok((min, max)),
|
||||
_ => Err(()),
|
||||
};
|
||||
if let Ok((min, max)) = parsed_ints {
|
||||
if min == max {
|
||||
Ok(Constraint::Equal(min))
|
||||
} else {
|
||||
Ok(Constraint::Range(min..max + 1))
|
||||
}
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
mod config;
|
||||
mod constraint;
|
||||
mod functions;
|
||||
mod intrinsic;
|
||||
mod json_parser;
|
||||
mod types;
|
||||
|
||||
use crate::arm::constraint::Constraint;
|
||||
use crate::arm::intrinsic::ArmIntrinsicType;
|
||||
use crate::common::SupportedArchitectureTest;
|
||||
use crate::common::cli::ProcessedCli;
|
||||
|
|
@ -16,7 +14,7 @@ use functions::{build_c, build_rust};
|
|||
use json_parser::get_neon_intrinsics;
|
||||
|
||||
pub struct ArmArchitectureTest {
|
||||
intrinsics: Vec<Intrinsic<ArmIntrinsicType, Constraint>>,
|
||||
intrinsics: Vec<Intrinsic<ArmIntrinsicType>>,
|
||||
cli_options: ProcessedCli,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
use crate::common::cli::Language;
|
||||
use crate::common::constraint::Constraint;
|
||||
use crate::common::indentation::Indentation;
|
||||
use crate::common::intrinsic_helpers::IntrinsicTypeDefinition;
|
||||
|
||||
/// An argument for the intrinsic.
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Argument<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
|
||||
pub struct Argument<T: IntrinsicTypeDefinition> {
|
||||
/// The argument's index in the intrinsic function call.
|
||||
pub pos: usize,
|
||||
/// The argument name.
|
||||
|
|
@ -12,17 +13,12 @@ pub struct Argument<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
|
|||
/// The type of the argument.
|
||||
pub ty: T,
|
||||
/// Any constraints that are on this argument
|
||||
pub metadata: Vec<M>,
|
||||
pub constraint: Option<Constraint>,
|
||||
}
|
||||
|
||||
pub trait MetadataDefinition {
|
||||
fn from_metadata(metadata: Option<Value>) -> Vec<Box<Self>>;
|
||||
}
|
||||
|
||||
impl<T, M> Argument<T, M>
|
||||
impl<T> Argument<T>
|
||||
where
|
||||
T: IntrinsicTypeDefinition,
|
||||
M: MetadataDefinition,
|
||||
{
|
||||
pub fn to_c_type(&self) -> String {
|
||||
self.ty.c_type()
|
||||
|
|
@ -37,7 +33,7 @@ where
|
|||
}
|
||||
|
||||
pub fn has_constraint(&self) -> bool {
|
||||
!self.metadata.is_empty()
|
||||
!self.constraint.is_some()
|
||||
}
|
||||
|
||||
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
|
||||
|
|
@ -70,20 +66,18 @@ where
|
|||
pos: usize,
|
||||
arg: &str,
|
||||
target: &String,
|
||||
metadata: Option<Value>,
|
||||
) -> Argument<T, M> {
|
||||
constraint: Option<Constraint>,
|
||||
) -> Argument<T> {
|
||||
let (ty, var_name) = Self::type_and_name_from_c(arg);
|
||||
|
||||
let ty =
|
||||
T::from_c(ty, target).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
|
||||
|
||||
let metadata: Vec<M> = M::from_metadata(metadata).into_iter().map(|b| *b).collect();
|
||||
|
||||
Argument {
|
||||
pos,
|
||||
name: String::from(var_name),
|
||||
ty: *ty,
|
||||
metadata,
|
||||
constraint,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -93,14 +87,13 @@ where
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ArgumentList<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
|
||||
pub args: Vec<Argument<T, M>>,
|
||||
pub struct ArgumentList<T: IntrinsicTypeDefinition> {
|
||||
pub args: Vec<Argument<T>>,
|
||||
}
|
||||
|
||||
impl<T, M> ArgumentList<T, M>
|
||||
impl<T> ArgumentList<T>
|
||||
where
|
||||
T: IntrinsicTypeDefinition,
|
||||
M: MetadataDefinition,
|
||||
{
|
||||
/// Converts the argument list into the call parameters for a C function call.
|
||||
/// e.g. this would generate something like `a, &b, c`
|
||||
|
|
@ -217,7 +210,7 @@ where
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T, M>> {
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, Argument<T>> {
|
||||
self.args.iter()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
use serde::Deserialize;
|
||||
use std::ops::Range;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Deserialize)]
|
||||
pub enum Constraint {
|
||||
Equal(i64),
|
||||
Range(Range<i64>),
|
||||
}
|
||||
|
||||
impl Constraint {
|
||||
pub fn to_range(&self) -> Range<i64> {
|
||||
match self {
|
||||
Constraint::Equal(eq) => *eq..*eq + 1,
|
||||
Constraint::Range(range) => range.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,14 @@ use crate::common::argument::ArgumentList;
|
|||
use crate::common::indentation::Indentation;
|
||||
use crate::common::intrinsic_helpers::IntrinsicTypeDefinition;
|
||||
|
||||
use super::argument::MetadataDefinition;
|
||||
|
||||
/// An intrinsic
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Intrinsic<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
|
||||
pub struct Intrinsic<T: IntrinsicTypeDefinition> {
|
||||
/// The function name of this intrinsic.
|
||||
pub name: String,
|
||||
|
||||
/// Any arguments for this intrinsic.
|
||||
pub arguments: ArgumentList<T, M>,
|
||||
pub arguments: ArgumentList<T>,
|
||||
|
||||
/// The return type of this intrinsic.
|
||||
pub results: T,
|
||||
|
|
@ -20,12 +18,11 @@ pub struct Intrinsic<T: IntrinsicTypeDefinition, M: MetadataDefinition> {
|
|||
pub arch_tags: Vec<String>,
|
||||
}
|
||||
|
||||
pub trait IntrinsicDefinition<T, M>
|
||||
pub trait IntrinsicDefinition<T>
|
||||
where
|
||||
T: IntrinsicTypeDefinition,
|
||||
M: MetadataDefinition,
|
||||
{
|
||||
fn arguments(&self) -> ArgumentList<T, M>;
|
||||
fn arguments(&self) -> ArgumentList<T>;
|
||||
|
||||
fn results(&self) -> T;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ pub mod argument;
|
|||
pub mod cli;
|
||||
pub mod compare;
|
||||
pub mod compile_c;
|
||||
pub mod constraint;
|
||||
pub mod gen_c;
|
||||
pub mod gen_rust;
|
||||
pub mod indentation;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue