1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
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;
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct ReturnType {
value: String,
}
#[derive(Deserialize, Debug)]
#[serde(untagged, deny_unknown_fields)]
pub enum ArgPrep {
Register {
#[serde(rename = "register")]
#[allow(dead_code)]
reg: String,
},
Immediate {
#[serde(rename = "minimum")]
min: i64,
#[serde(rename = "maximum")]
max: i64,
},
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")]
simd_isa: String,
name: String,
arguments: Vec<String>,
return_type: ReturnType,
#[serde(rename = "Arguments_Preparation")]
args_prep: Option<HashMap<String, Value>>,
#[serde(rename = "Architectures")]
architectures: Vec<String>,
}
pub fn get_neon_intrinsics(
filename: &Path,
target: &str,
) -> 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");
let parsed = json
.into_iter()
.filter_map(|intr| {
if intr.simd_isa == "Neon" {
Some(json_to_intrinsic(intr, target).expect("Couldn't parse JSON"))
} else {
None
}
})
.collect();
Ok(parsed)
}
fn json_to_intrinsic(
mut intr: JsonIntrinsic,
target: &str,
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
let name = intr.name.replace(['[', ']'], "");
let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?;
let args = intr
.arguments
.into_iter()
.enumerate()
.map(|(i, arg)| {
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 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 {
ref mut constant, ..
} = arg.ty.0;
if arg.name.starts_with("imm") {
*constant = true
}
arg
})
.collect();
let arguments = ArgumentList::<ArmIntrinsicType> { args };
Ok(Intrinsic {
name,
arguments,
results: *results,
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(())
}
}
}
|