summaryrefslogtreecommitdiffstats
path: root/library/stdarch/crates/intrinsic-test/src/json_parser.rs
blob: 8b3c7869c64efa100bae92059f2cc464bda7e70e (plain)
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
use std::collections::HashMap;
use std::path::Path;

use serde::Deserialize;

use crate::argument::{Argument, ArgumentList};
use crate::intrinsic::Intrinsic;
use crate::types::IntrinsicType;

#[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")]
        reg: String,
    },
    Immediate {
        #[serde(rename = "minimum")]
        min: i64,
        #[serde(rename = "maximum")]
        max: i64,
    },
    Nothing {},
}

#[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, ArgPrep>>,
    #[serde(rename = "Architectures")]
    architectures: Vec<String>,
}

pub fn get_neon_intrinsics(filename: &Path) -> Result<Vec<Intrinsic>, 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).expect("Couldn't parse JSON"))
            } else {
                None
            }
        })
        .collect();
    Ok(parsed)
}

fn json_to_intrinsic(mut intr: JsonIntrinsic) -> Result<Intrinsic, Box<dyn std::error::Error>> {
    let name = intr.name.replace(['[', ']'], "");

    let results = IntrinsicType::from_c(&intr.return_type.value)?;

    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);
            // The JSON doesn't list immediates as const
            if let IntrinsicType::Type {
                ref mut constant, ..
            } = arg.ty
            {
                if arg.name.starts_with("imm") {
                    *constant = true
                }
            }
            arg
        })
        .collect();

    let arguments = ArgumentList { args };

    Ok(Intrinsic {
        name,
        arguments,
        results,
        a64_only: intr.architectures == vec!["A64".to_string()],
    })
}