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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use alloc::{
    string::{String, ToString},
    vec::Vec,
};

use crate::{
    acpi::aml::{parser::PackageElement, structured::ElementType},
    testing,
};

use super::{
    parser::{resource_template::ResourceTemplate, IntegerData, TermArg, UnresolvedDataObject},
    structured::{StructuredAml, StructuredAmlError},
};

#[derive(Debug, Clone)]
pub struct Package {
    size: IntegerData,
    elements: Vec<PackageElement<DataObject>>,
}

#[allow(dead_code)]
impl Package {
    pub fn size(&self) -> usize {
        self.size.as_u64() as usize
    }

    pub fn get(&self, index: usize) -> Option<&PackageElement<DataObject>> {
        self.elements.get(index)
    }

    pub fn iter(&self) -> impl Iterator<Item = &PackageElement<DataObject>> {
        self.elements.iter()
    }
}

/// A version of [UnresolvedDataObject] that is after execution
/// so it doesn't have dynamic contents, references, or expressions
#[derive(Debug, Clone)]
pub enum DataObject {
    Integer(IntegerData),
    ResourceTemplate(ResourceTemplate),
    Buffer(IntegerData, Vec<u8>),
    Package(Package),
    String(String),
    EisaId(String),
}

#[allow(dead_code)]
impl DataObject {
    pub fn as_integer(&self) -> Option<&IntegerData> {
        match self {
            Self::Integer(data) => Some(data),
            _ => None,
        }
    }

    pub fn as_package(&self) -> Option<&Package> {
        match self {
            Self::Package(package) => Some(package),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub enum AmlExecutionError {
    LableNotFound(String),
    StructuredAmlError(StructuredAmlError),
    ElementNotExecutable(String),
    UnexpectedTermResultType(TermArg, String),
}

impl From<StructuredAmlError> for AmlExecutionError {
    fn from(err: StructuredAmlError) -> Self {
        Self::StructuredAmlError(err)
    }
}

#[derive(Debug, Default, Clone)]
pub struct ExecutionContext {}
impl ExecutionContext {
    pub fn execute(
        &self,
        structured: &StructuredAml,
        label: &str,
        _args: &[UnresolvedDataObject],
    ) -> Result<DataObject, AmlExecutionError> {
        let element_to_execute = structured
            .find_object(label)?
            .ok_or(AmlExecutionError::LableNotFound(label.to_string()))?;

        let data = match element_to_execute {
            ElementType::Method(_) => todo!("Execute method"),
            ElementType::Name(data) => data,
            ElementType::UnknownElements(_) => {
                // This label is internal and should never be reached
                return Err(AmlExecutionError::LableNotFound(label.to_string()));
            }
            ElementType::PowerResource(_)
            | ElementType::RegionFields(_, _)
            | ElementType::IndexField(_)
            | ElementType::ScopeOrDevice(_)
            | ElementType::Processor(_) => {
                return Err(AmlExecutionError::ElementNotExecutable(label.to_string()))
            }
        };

        self.evaluate_data_object(data.clone(), label)
    }

    fn execute_term_arg(
        &self,
        term: &TermArg,
        _reference_path: &str,
    ) -> Result<DataObject, AmlExecutionError> {
        todo!("Execute term: {:?}", term)
    }

    fn convert_package_elements(
        &self,
        elements: Vec<PackageElement<UnresolvedDataObject>>,
        reference_path: &str,
    ) -> Result<Vec<PackageElement<DataObject>>, AmlExecutionError> {
        elements
            .into_iter()
            .map(|e| {
                Ok(match e {
                    PackageElement::DataObject(data) => {
                        PackageElement::DataObject(self.evaluate_data_object(data, reference_path)?)
                    }
                    PackageElement::Name(name) => PackageElement::Name(name),
                })
            })
            .collect::<Result<Vec<_>, _>>()
    }

    fn evaluate_data_object(
        &self,
        data: UnresolvedDataObject,
        reference_path: &str,
    ) -> Result<DataObject, AmlExecutionError> {
        match data {
            UnresolvedDataObject::Buffer(buffer) => {
                let size_term = self.execute_term_arg(buffer.size.as_ref(), reference_path)?;

                let size_term = match size_term {
                    DataObject::Integer(i) => i,
                    _ => {
                        return Err(AmlExecutionError::UnexpectedTermResultType(
                            buffer.size.as_ref().clone(),
                            "Integer".to_string(),
                        ))
                    }
                };

                Ok(DataObject::Buffer(
                    size_term,
                    buffer.data.into_iter().collect(),
                ))
            }
            UnresolvedDataObject::ResourceTemplate(template) => {
                Ok(DataObject::ResourceTemplate(template))
            }
            UnresolvedDataObject::Package(size, elements) => Ok(DataObject::Package(Package {
                size: IntegerData::ByteConst(size),
                elements: self.convert_package_elements(elements, reference_path)?,
            })),
            UnresolvedDataObject::VarPackage(term, elements) => {
                let size_term = self.execute_term_arg(term.as_ref(), reference_path)?;

                let size_term = match size_term {
                    DataObject::Integer(i) => i,
                    _ => {
                        return Err(AmlExecutionError::UnexpectedTermResultType(
                            term.as_ref().clone(),
                            "Integer".to_string(),
                        ))
                    }
                };

                Ok(DataObject::Package(Package {
                    size: size_term,
                    elements: self.convert_package_elements(elements, reference_path)?,
                }))
            }
            UnresolvedDataObject::Integer(i) => Ok(DataObject::Integer(i)),
            UnresolvedDataObject::String(s) => Ok(DataObject::String(s)),
            UnresolvedDataObject::EisaId(s) => Ok(DataObject::EisaId(s)),
        }
    }
}

/// Test executing and getting data from
/// ```
/// Name("_S5_", Package(4) {0x5, 0x5, Zero, Zero}
/// Name("_S4_", Package(4) {0x4, 0x4, Zero, Zero}
/// ```
/// ```
#[macro_rules_attribute::apply(testing::test)]
fn test_execute_normal_sleep_package() {
    use super::parser::{AmlCode, AmlTerm};
    use alloc::vec;

    fn return_package_of_name(
        ctx: &mut ExecutionContext,
        structured_code: &StructuredAml,
        name: &str,
    ) -> Vec<u8> {
        ctx.execute(structured_code, name, &[])
            .expect("label")
            .as_package()
            .expect("package")
            .iter()
            .map(|d| {
                d.as_data()
                    .expect("data")
                    .as_integer()
                    .expect("integer")
                    .as_u8()
                    .unwrap()
            })
            .collect::<Vec<_>>()
    }

    let code = AmlCode {
        term_list: vec![
            AmlTerm::NameObj(
                "_S5_".to_string(),
                UnresolvedDataObject::Package(
                    4,
                    vec![
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ByteConst(5),
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ByteConst(5),
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ConstZero,
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ConstZero,
                        )),
                    ],
                ),
            ),
            AmlTerm::NameObj(
                "_S4_".to_string(),
                UnresolvedDataObject::Package(
                    4,
                    vec![
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ByteConst(4),
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ByteConst(4),
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ConstZero,
                        )),
                        PackageElement::DataObject(UnresolvedDataObject::Integer(
                            IntegerData::ConstZero,
                        )),
                    ],
                ),
            ),
        ],
    };

    let structured_code = StructuredAml::parse(&code);

    let mut execution_ctx = ExecutionContext::default();

    assert_eq!(
        return_package_of_name(&mut execution_ctx, &structured_code, "\\_S5_"),
        vec![5, 5, 0, 0]
    );
    assert_eq!(
        return_package_of_name(&mut execution_ctx, &structured_code, "\\_S4_"),
        vec![4, 4, 0, 0]
    );
}