Updated Argument::from_c to remove ArgPrep specific argument

This commit is contained in:
Madhav Madhusoodanan 2025-04-14 13:35:05 +05:30 committed by Amanieu d'Antras
parent 9d3c09ed53
commit a993b4427c
2 changed files with 31 additions and 7 deletions

View file

@ -2,6 +2,9 @@ use super::format::Indentation;
use super::json_parser::ArgPrep;
use super::types::{IntrinsicType, TypeKind};
use crate::common::types::Language;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::ops::Range;
/// An argument for the intrinsic.
@ -17,7 +20,7 @@ pub struct Argument {
pub constraints: Vec<Constraint>,
}
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub enum Constraint {
Equal(i64),
Range(Range<i64>),
@ -79,12 +82,26 @@ impl Argument {
}
// ARM-specific
pub fn from_c(pos: usize, arg: &str, arg_prep: Option<ArgPrep>, target: &String) -> Argument {
pub fn from_c(
pos: usize,
arg: &str,
target: &String,
metadata: Option<&mut HashMap<String, Value>>,
) -> Argument {
let (ty, var_name) = Self::type_and_name_from_c(arg);
let ty = IntrinsicType::from_c(ty, target)
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
let arg_name = Argument::type_and_name_from_c(&arg).1;
let arg = metadata.and_then(|a| a.remove(arg_name));
let arg_prep: Option<ArgPrep> = arg.and_then(|a| {
if let Value::Object(_) = a {
a.try_into().ok()
} else {
None
}
});
let constraint = arg_prep.and_then(|a| a.try_into().ok());
Argument {

View file

@ -2,6 +2,7 @@ use super::argument::{Argument, ArgumentList};
use super::intrinsic::Intrinsic;
use super::types::IntrinsicType;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;
@ -28,6 +29,14 @@ pub enum ArgPrep {
Nothing {},
}
impl TryFrom<Value> for ArgPrep {
type Error = serde_json::Error;
fn try_from(value: Value) -> Result<Self, Self::Error> {
serde_json::from_value(value)
}
}
#[derive(Deserialize, Debug)]
struct JsonIntrinsic {
#[serde(rename = "SIMD_ISA")]
@ -36,7 +45,7 @@ struct JsonIntrinsic {
arguments: Vec<String>,
return_type: ReturnType,
#[serde(rename = "Arguments_Preparation")]
args_prep: Option<HashMap<String, ArgPrep>>,
args_prep: Option<HashMap<String, Value>>,
#[serde(rename = "Architectures")]
architectures: Vec<String>,
}
@ -70,15 +79,13 @@ fn json_to_intrinsic(
let results = IntrinsicType::from_c(&intr.return_type.value, target)?;
let mut args_prep = intr.args_prep.as_mut();
let args = intr
.arguments
.into_iter()
.enumerate()
.map(|(i, arg)| {
let arg_name = Argument::type_and_name_from_c(&arg).1;
let arg_prep = args_prep.as_mut().and_then(|a| a.remove(arg_name));
let mut arg = Argument::from_c(i, &arg, arg_prep, target);
// let arg_name = Argument::type_and_name_from_c(&arg).1;
let mut arg = Argument::from_c(i, &arg, target, intr.args_prep.as_mut());
// The JSON doesn't list immediates as const
if let IntrinsicType::Type {
ref mut constant, ..