Ver código fonte

Parse local, arg, and debug objects

Isaac Woods 5 anos atrás
pai
commit
40cbee5c8d
4 arquivos alterados com 109 adições e 0 exclusões
  1. 1 0
      aml_parser/src/lib.rs
  2. 79 0
      aml_parser/src/misc.rs
  3. 20 0
      aml_parser/src/opcode.rs
  4. 9 0
      aml_parser/src/parser.rs

+ 1 - 0
aml_parser/src/lib.rs

@@ -42,6 +42,7 @@ extern crate std;
 #[cfg(test)]
 mod test_utils;
 
+pub(crate) mod misc;
 pub(crate) mod name_object;
 pub(crate) mod namespace;
 pub(crate) mod opcode;

+ 79 - 0
aml_parser/src/misc.rs

@@ -0,0 +1,79 @@
+use crate::{
+    opcode::{self, ext_opcode, opcode},
+    parser::{choice, comment_scope_verbose, id, Parser},
+};
+
+pub fn debug_obj<'a, 'c>() -> impl Parser<'a, 'c, ()>
+where
+    'c: 'a,
+{
+    /*
+     * DebugObj := ExtOpPrefix 0x31
+     */
+    ext_opcode(opcode::EXT_DEBUG_OP)
+}
+
+/// Takes a value between `0` and `7`, where 0 represents `Local0` etc.
+pub type LocalNum = u8;
+
+pub fn local_obj<'a, 'c>() -> impl Parser<'a, 'c, LocalNum>
+where
+    'c: 'a,
+{
+    /*
+     * LocalObj := Local0Op | Local1Op | Local2Op | Local3Op | Local4Op | Local5Op | Local6Op | Local7Op
+     * Local0Op := 0x60
+     * Local1Op := 0x61
+     * Local2Op := 0x62
+     * Local3Op := 0x63
+     * Local4Op := 0x64
+     * Local5Op := 0x65
+     * Local6Op := 0x66
+     * Local7Op := 0x67
+     */
+    let local_parser = |i, local_opcode| {
+        opcode(local_opcode).then(comment_scope_verbose("LocalObj", id())).map(move |((), _)| Ok(i))
+    };
+
+    choice!(
+        local_parser(0, opcode::LOCAL0_OP),
+        local_parser(1, opcode::LOCAL1_OP),
+        local_parser(2, opcode::LOCAL2_OP),
+        local_parser(3, opcode::LOCAL3_OP),
+        local_parser(4, opcode::LOCAL4_OP),
+        local_parser(5, opcode::LOCAL5_OP),
+        local_parser(6, opcode::LOCAL6_OP)
+    )
+}
+
+/// Takes a value between `0` and `6`, where 0 represents `Arg0` etc.
+pub type ArgNum = u8;
+
+pub fn arg_obj<'a, 'c>() -> impl Parser<'a, 'c, ArgNum>
+where
+    'c: 'a,
+{
+    /*
+     * ArgObj := Arg0Op | Arg1Op | Arg2Op | Arg3Op | Arg4Op | Arg5Op | Arg6Op
+     * Arg0Op = 0x68
+     * Arg1Op = 0x69
+     * Arg2Op = 0x6a
+     * Arg3Op = 0x6b
+     * Arg4Op = 0x6c
+     * Arg5Op = 0x6d
+     * Arg6Op = 0x6e
+     */
+    let arg_parser = |i, arg_opcode| {
+        opcode(arg_opcode).then(comment_scope_verbose("ArgObj", id())).map(move |((), _)| Ok(i))
+    };
+
+    choice!(
+        arg_parser(0, opcode::ARG0_OP),
+        arg_parser(1, opcode::ARG1_OP),
+        arg_parser(2, opcode::ARG2_OP),
+        arg_parser(3, opcode::ARG3_OP),
+        arg_parser(4, opcode::ARG4_OP),
+        arg_parser(5, opcode::ARG5_OP),
+        arg_parser(6, opcode::ARG6_OP)
+    )
+}

+ 20 - 0
aml_parser/src/opcode.rs

@@ -44,6 +44,26 @@ pub const DEF_RETURN_OP: u8 = 0xa4;
  */
 pub const DEF_L_EQUAL_OP: u8 = 0x93;
 
+/*
+ * Miscellaneous objects
+ */
+pub const EXT_DEBUG_OP: u8 = 0x31;
+pub const LOCAL0_OP: u8 = 0x60;
+pub const LOCAL1_OP: u8 = 0x61;
+pub const LOCAL2_OP: u8 = 0x62;
+pub const LOCAL3_OP: u8 = 0x63;
+pub const LOCAL4_OP: u8 = 0x64;
+pub const LOCAL5_OP: u8 = 0x65;
+pub const LOCAL6_OP: u8 = 0x66;
+pub const LOCAL7_OP: u8 = 0x67;
+pub const ARG0_OP: u8 = 0x68;
+pub const ARG1_OP: u8 = 0x69;
+pub const ARG2_OP: u8 = 0x6a;
+pub const ARG3_OP: u8 = 0x6b;
+pub const ARG4_OP: u8 = 0x6c;
+pub const ARG5_OP: u8 = 0x6d;
+pub const ARG6_OP: u8 = 0x6e;
+
 pub const EXT_OPCODE_PREFIX: u8 = 0x5b;
 
 pub(crate) fn opcode<'a, 'c>(opcode: u8) -> impl Parser<'a, 'c, ()>

+ 9 - 0
aml_parser/src/parser.rs

@@ -71,6 +71,15 @@ where
     }
 }
 
+/// The identity parser - returns the stream and context unchanged. Useful for producing parsers
+/// that produce a result without parsing anything by doing: `id().map(|()| Ok(foo))`.
+pub fn id<'a, 'c>() -> impl Parser<'a, 'c, ()>
+where
+    'c: 'a,
+{
+    move |input: &'a [u8], context: &'c mut AmlContext| Ok((input, context, ()))
+}
+
 pub fn take<'a, 'c>() -> impl Parser<'a, 'c, u8>
 where
     'c: 'a,