Skip to content

Commit 546554d

Browse files
port aarch-specific json_parser to within the aarch module
1 parent 5ef22a7 commit 546554d

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::aarch::parser::ArgPrep;
2+
use crate::argument::{Argument, Constraint};
3+
use crate::types::IntrinsicType;
4+
5+
impl TryFrom<ArgPrep> for Constraint {
6+
type Error = ();
7+
8+
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> {
9+
let parsed_ints = match prep {
10+
ArgPrep::Immediate { min, max } => Ok((min, max)),
11+
_ => Err(()),
12+
};
13+
if let Ok((min, max)) = parsed_ints {
14+
if min == max {
15+
Ok(Constraint::Equal(min))
16+
} else {
17+
Ok(Constraint::Range(min..max + 1))
18+
}
19+
} else {
20+
Err(())
21+
}
22+
}
23+
}
24+
25+
// TODO: Add FromC named impl for this function.
26+
impl Argument {
27+
pub fn from_c(pos: usize, arg: &str, arg_prep: Option<ArgPrep>) -> Argument {
28+
let (ty, var_name) = Self::type_and_name_from_c(arg);
29+
30+
let ty = IntrinsicType::from_c(ty)
31+
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
32+
33+
let constraint = arg_prep.and_then(|a| a.try_into().ok());
34+
35+
Argument {
36+
pos,
37+
name: String::from(var_name),
38+
ty,
39+
constraints: constraint.map_or(vec![], |r| vec![r]),
40+
}
41+
}
42+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod argument;
2+
pub mod parser;

crates/intrinsic-test/src/json_parser.rs renamed to crates/intrinsic-test/src/aarch/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct JsonIntrinsic {
4343
architectures: Vec<String>,
4444
}
4545

46-
pub fn get_neon_intrinsics(filename: &Path) -> Result<Vec<Intrinsic>, Box<dyn std::error::Error>> {
46+
pub fn parse_file(filename: &Path) -> Result<Vec<Intrinsic>, Box<dyn std::error::Error>> {
4747
let file = std::fs::File::open(filename)?;
4848
let reader = std::io::BufReader::new(file);
4949
let json: Vec<JsonIntrinsic> = serde_json::from_reader(reader).expect("Couldn't parse JSON");

crates/intrinsic-test/src/argument.rs

-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::ops::Range;
22

33
use crate::Language;
44
use crate::format::Indentation;
5-
use crate::json_parser::ArgPrep;
65
use crate::types::{IntrinsicType, TypeKind};
76

87
/// An argument for the intrinsic.
@@ -24,26 +23,6 @@ pub enum Constraint {
2423
Range(Range<i64>),
2524
}
2625

27-
impl TryFrom<ArgPrep> for Constraint {
28-
type Error = ();
29-
30-
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> {
31-
let parsed_ints = match prep {
32-
ArgPrep::Immediate { min, max } => Ok((min, max)),
33-
_ => Err(()),
34-
};
35-
if let Ok((min, max)) = parsed_ints {
36-
if min == max {
37-
Ok(Constraint::Equal(min))
38-
} else {
39-
Ok(Constraint::Range(min..max + 1))
40-
}
41-
} else {
42-
Err(())
43-
}
44-
}
45-
}
46-
4726
impl Constraint {
4827
pub fn to_range(&self) -> Range<i64> {
4928
match self {
@@ -78,22 +57,6 @@ impl Argument {
7857
(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
7958
}
8059

81-
pub fn from_c(pos: usize, arg: &str, arg_prep: Option<ArgPrep>) -> Argument {
82-
let (ty, var_name) = Self::type_and_name_from_c(arg);
83-
84-
let ty = IntrinsicType::from_c(ty)
85-
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
86-
87-
let constraint = arg_prep.and_then(|a| a.try_into().ok());
88-
89-
Argument {
90-
pos,
91-
name: String::from(var_name),
92-
ty,
93-
constraints: constraint.map_or(vec![], |r| vec![r]),
94-
}
95-
}
96-
9760
fn is_rust_vals_array_const(&self) -> bool {
9861
use TypeKind::*;
9962
match self.ty {

crates/intrinsic-test/src/main.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ use types::TypeKind;
1414

1515
use crate::argument::Argument;
1616
use crate::format::Indentation;
17-
use crate::json_parser::get_neon_intrinsics;
1817

18+
mod aarch;
1919
mod argument;
2020
mod format;
2121
mod intrinsic;
22-
mod json_parser;
2322
mod types;
2423
mod values;
2524

@@ -519,7 +518,7 @@ fn main() {
519518
Default::default()
520519
};
521520
let a32 = target.contains("v7");
522-
let mut intrinsics = get_neon_intrinsics(&filename).expect("Error parsing input file");
521+
let mut intrinsics = aarch::parser::parse_file(&filename).expect("Error parsing input file");
523522

524523
intrinsics.sort_by(|a, b| a.name.cmp(&b.name));
525524

0 commit comments

Comments
 (0)