2
0

opcode.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use crate::{parser::*, AmlContext, AmlError};
  2. pub const NULL_NAME: u8 = 0x00;
  3. pub const DUAL_NAME_PREFIX: u8 = 0x2E;
  4. pub const MULTI_NAME_PREFIX: u8 = 0x2F;
  5. pub const ROOT_CHAR: u8 = b'\\';
  6. pub const PREFIX_CHAR: u8 = b'^';
  7. pub const RESERVED_FIELD: u8 = 0x00;
  8. pub const ACCESS_FIELD: u8 = 0x01;
  9. pub const CONNECT_FIELD: u8 = 0x02;
  10. pub const EXTENDED_ACCESS_FIELD: u8 = 0x03;
  11. pub const ZERO_OP: u8 = 0x00;
  12. pub const ONE_OP: u8 = 0x01;
  13. pub const ONES_OP: u8 = 0xff;
  14. pub const BYTE_CONST: u8 = 0x0a;
  15. pub const WORD_CONST: u8 = 0x0b;
  16. pub const DWORD_CONST: u8 = 0x0c;
  17. pub const STRING_PREFIX: u8 = 0x0d;
  18. pub const QWORD_CONST: u8 = 0x0e;
  19. pub const DEF_NAME_OP: u8 = 0x08;
  20. pub const DEF_SCOPE_OP: u8 = 0x10;
  21. pub const DEF_BUFFER_OP: u8 = 0x11;
  22. pub const DEF_PACKAGE_OP: u8 = 0x12;
  23. pub const DEF_METHOD_OP: u8 = 0x14;
  24. pub const EXT_REVISION_OP: u8 = 0x30;
  25. pub const EXT_DEF_OP_REGION_OP: u8 = 0x80;
  26. pub const EXT_DEF_FIELD_OP: u8 = 0x81;
  27. pub const EXT_DEF_DEVICE_OP: u8 = 0x82;
  28. pub const EXT_OPCODE_PREFIX: u8 = 0x5b;
  29. pub(crate) fn opcode<'a, 'c>(opcode: u8) -> impl Parser<'a, 'c, ()>
  30. where
  31. 'c: 'a,
  32. {
  33. move |input: &'a [u8], context: &'c mut AmlContext| match input.first() {
  34. None => Err((input, context, AmlError::UnexpectedEndOfStream)),
  35. Some(&byte) if byte == opcode => Ok((&input[1..], context, ())),
  36. Some(&byte) => Err((input, context, AmlError::UnexpectedByte(byte))),
  37. }
  38. }
  39. pub(crate) fn ext_opcode<'a, 'c>(ext_opcode: u8) -> impl Parser<'a, 'c, ()>
  40. where
  41. 'c: 'a,
  42. {
  43. opcode(EXT_OPCODE_PREFIX).then(opcode(ext_opcode)).map(|_| ())
  44. }
  45. #[cfg(test)]
  46. mod tests {
  47. use super::*;
  48. use crate::{test_utils::*, AmlError};
  49. #[test]
  50. fn empty() {
  51. let mut context = AmlContext::new();
  52. check_err!(
  53. opcode(NULL_NAME).parse(&[], &mut context),
  54. AmlError::UnexpectedEndOfStream,
  55. &[]
  56. );
  57. check_err!(
  58. ext_opcode(EXT_DEF_FIELD_OP).parse(&[], &mut context),
  59. AmlError::UnexpectedEndOfStream,
  60. &[]
  61. );
  62. }
  63. #[test]
  64. fn simple_opcodes() {
  65. let mut context = AmlContext::new();
  66. check_ok!(opcode(DEF_SCOPE_OP).parse(&[DEF_SCOPE_OP], &mut context), (), &[]);
  67. check_ok!(
  68. opcode(DEF_NAME_OP).parse(&[DEF_NAME_OP, 0x31, 0x55, 0xf3], &mut context),
  69. (),
  70. &[0x31, 0x55, 0xf3]
  71. );
  72. }
  73. #[test]
  74. fn extended_opcodes() {
  75. let mut context = AmlContext::new();
  76. check_err!(
  77. ext_opcode(EXT_DEF_FIELD_OP).parse(&[EXT_DEF_FIELD_OP, EXT_DEF_FIELD_OP], &mut context),
  78. AmlError::UnexpectedByte(EXT_DEF_FIELD_OP),
  79. &[EXT_DEF_FIELD_OP, EXT_DEF_FIELD_OP]
  80. );
  81. check_ok!(
  82. ext_opcode(EXT_DEF_FIELD_OP)
  83. .parse(&[EXT_OPCODE_PREFIX, EXT_DEF_FIELD_OP], &mut context),
  84. (),
  85. &[]
  86. );
  87. }
  88. }