1 // Copyright (c) 2021 The Vulkano developers
2 // Licensed under the Apache License, Version 2.0
3 // <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
6 // at your option. All files in the project carrying such
7 // notice may not be copied, modified, or distributed except
8 // according to those terms.
9 
10 use serde::Deserialize;
11 use serde_json::Value;
12 use std::{
13     fs::File,
14     io::{BufReader, Read},
15     path::Path,
16 };
17 
18 #[derive(Clone, Debug, Deserialize)]
19 pub struct SpirvGrammar {
20     pub major_version: u16,
21     pub minor_version: u16,
22     pub revision: u16,
23     pub instructions: Vec<SpirvInstruction>,
24     pub operand_kinds: Vec<SpirvOperandKind>,
25 }
26 
27 impl SpirvGrammar {
new<P: AsRef<Path> + ?Sized>(path: &P) -> Self28     pub fn new<P: AsRef<Path> + ?Sized>(path: &P) -> Self {
29         let mut reader = BufReader::new(File::open(path).unwrap());
30         let mut json = String::new();
31         reader.read_to_string(&mut json).unwrap();
32         serde_json::from_str(&json).unwrap()
33     }
34 }
35 
36 #[derive(Clone, Debug, Deserialize)]
37 pub struct SpirvInstruction {
38     pub opname: String,
39     pub class: String,
40     pub opcode: u16,
41     #[serde(default)]
42     pub operands: Vec<SpirvOperand>,
43     #[serde(default)]
44     pub capabilities: Vec<String>,
45     #[serde(default)]
46     pub extensions: Vec<String>,
47 }
48 
49 #[derive(Clone, Debug, Deserialize)]
50 pub struct SpirvOperand {
51     pub kind: String,
52     pub quantifier: Option<char>,
53     pub name: Option<String>,
54 }
55 
56 #[derive(Clone, Debug, Deserialize)]
57 pub struct SpirvOperandKind {
58     pub category: String,
59     pub kind: String,
60     #[serde(default)]
61     pub enumerants: Vec<SpirvKindEnumerant>,
62 }
63 
64 #[derive(Clone, Debug, Deserialize)]
65 pub struct SpirvKindEnumerant {
66     pub enumerant: String,
67     pub value: Value,
68     #[serde(default)]
69     pub parameters: Vec<SpirvParameter>,
70     #[serde(default)]
71     pub capabilities: Vec<String>,
72 }
73 
74 #[derive(Clone, Debug, Deserialize)]
75 pub struct SpirvParameter {
76     pub kind: String,
77     pub name: Option<String>,
78 }
79