Skip to content

Commit b854d54

Browse files
committed
Implement DefIncrement and DefDecrement
1 parent ed04000 commit b854d54

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

aml/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ impl AmlContext {
292292
Ok(())
293293
}
294294

295+
pub(crate) fn read_target(&self, target: &Target) -> Result<&AmlValue, AmlError> {
296+
match target {
297+
Target::Null => todo!(),
298+
Target::Name(name) => {
299+
let (_, handle) = self.namespace.search(name, &self.current_scope)?;
300+
self.namespace.get(handle)
301+
}
302+
Target::Debug => todo!(),
303+
Target::Arg(arg) => self.current_arg(*arg),
304+
Target::Local(local) => self.local(*local),
305+
}
306+
}
307+
295308
/// Get the value of an argument by its argument number. Can only be executed from inside a control method.
296309
pub(crate) fn current_arg(&self, arg: ArgNum) -> Result<&AmlValue, AmlError> {
297310
self.method_context.as_ref().ok_or(AmlError::NotExecutingControlMethod)?.args.arg(arg)

aml/src/opcode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub const DEF_BREAKPOINT_OP: u8 = 0xcc;
6060
pub const DEF_STORE_OP: u8 = 0x70;
6161
pub const DEF_ADD_OP: u8 = 0x72;
6262
pub const DEF_CONCAT_OP: u8 = 0x73;
63+
pub const DEF_INCREMENT_OP: u8 = 0x75;
64+
pub const DEF_DECREMENT_OP: u8 = 0x76;
6365
pub const DEF_SHIFT_LEFT: u8 = 0x79;
6466
pub const DEF_SHIFT_RIGHT: u8 = 0x7a;
6567
pub const DEF_AND_OP: u8 = 0x7b;

aml/src/type2.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ where
5151
def_buffer(),
5252
def_concat(),
5353
def_concat_res(),
54+
def_increment(),
55+
def_decrement(),
5456
def_l_equal(),
5557
def_l_greater(),
5658
def_l_greater_equal(),
@@ -258,6 +260,50 @@ where
258260
.map(|((), result)| Ok(result))
259261
}
260262

263+
fn def_increment<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
264+
where
265+
'c: 'a,
266+
{
267+
/*
268+
* DefIncrement := 0x75 SuperName
269+
*/
270+
opcode(opcode::DEF_INCREMENT_OP)
271+
.then(comment_scope(
272+
DebugVerbosity::AllScopes,
273+
"DefIncrement",
274+
super_name().map_with_context(|addend, context| {
275+
let value = try_with_context!(context, context.read_target(&addend));
276+
let value = try_with_context!(context, value.as_integer(context));
277+
let new_value = AmlValue::Integer(value + 1);
278+
try_with_context!(context, context.store(addend, new_value.clone()));
279+
(Ok(new_value), context)
280+
}),
281+
))
282+
.map(|((), result)| Ok(result))
283+
}
284+
285+
fn def_decrement<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
286+
where
287+
'c: 'a,
288+
{
289+
/*
290+
* DefDecrement := 0x76 SuperName
291+
*/
292+
opcode(opcode::DEF_DECREMENT_OP)
293+
.then(comment_scope(
294+
DebugVerbosity::AllScopes,
295+
"DefDecrement",
296+
super_name().map_with_context(|minuend, context| {
297+
let value = try_with_context!(context, context.read_target(&minuend));
298+
let value = try_with_context!(context, value.as_integer(context));
299+
let new_value = AmlValue::Integer(value - 1);
300+
try_with_context!(context, context.store(minuend, new_value.clone()));
301+
(Ok(new_value), context)
302+
}),
303+
))
304+
.map(|((), result)| Ok(result))
305+
}
306+
261307
fn def_l_or<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
262308
where
263309
'c: 'a,

tests/while.asl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// TODO: when implemented, the `+= 1`s can be turned into `++` - this requires DefIncrement
21
DefinitionBlock("while.aml", "DSDT", 1, "RSACPI", "WHILE", 1) {
32
Name(X, 0)
43
While (X < 5) {
5-
X += 1
4+
X++
65
}
76

87
// Test `DefBreak` - Y should only make it to 5
@@ -12,15 +11,15 @@ DefinitionBlock("while.aml", "DSDT", 1, "RSACPI", "WHILE", 1) {
1211
Break
1312
}
1413

15-
Y += 1
14+
Y++
1615
}
1716

1817
// Test `DefContinue` - Z should remain at zero
1918
Name(CNT, 0)
2019
Name(Z, 0)
2120
While (CNT < 5) {
22-
CNT += 1
21+
CNT++
2322
Continue
24-
Z += 1
23+
Z++
2524
}
2625
}

0 commit comments

Comments
 (0)