opcode.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 DEF_EXTERNAL_OP: u8 = 0x15;
  25. pub const DEF_CREATE_DWORD_FIELD_OP: u8 = 0x8a;
  26. pub const DEF_CREATE_WORD_FIELD_OP: u8 = 0x8b;
  27. pub const DEF_CREATE_BYTE_FIELD_OP: u8 = 0x8c;
  28. pub const DEF_CREATE_BIT_FIELD_OP: u8 = 0x8d;
  29. pub const DEF_CREATE_QWORD_FIELD_OP: u8 = 0x8f;
  30. pub const EXT_DEF_MUTEX_OP: u8 = 0x01;
  31. pub const EXT_DEF_CREATE_FIELD_OP: u8 = 0x13;
  32. pub const EXT_REVISION_OP: u8 = 0x30;
  33. pub const EXT_DEF_FATAL_OP: u8 = 0x32;
  34. pub const EXT_DEF_OP_REGION_OP: u8 = 0x80;
  35. pub const EXT_DEF_FIELD_OP: u8 = 0x81;
  36. pub const EXT_DEF_DEVICE_OP: u8 = 0x82;
  37. pub const EXT_DEF_PROCESSOR_OP: u8 = 0x83;
  38. pub const EXT_DEF_POWER_RES_OP: u8 = 0x84;
  39. pub const EXT_DEF_THERMAL_ZONE_OP: u8 = 0x85;
  40. /*
  41. * Type 1 opcodes
  42. */
  43. pub const DEF_CONTINUE_OP: u8 = 0x9f;
  44. pub const DEF_IF_ELSE_OP: u8 = 0xa0;
  45. pub const DEF_ELSE_OP: u8 = 0xa1;
  46. pub const DEF_WHILE_OP: u8 = 0xa2;
  47. pub const DEF_NOOP_OP: u8 = 0xa3;
  48. pub const DEF_RETURN_OP: u8 = 0xa4;
  49. pub const DEF_BREAK_OP: u8 = 0xa5;
  50. pub const DEF_BREAKPOINT_OP: u8 = 0xcc;
  51. /*
  52. * Type 2 opcodes
  53. */
  54. pub const DEF_STORE_OP: u8 = 0x70;
  55. pub const DEF_ADD_OP: u8 = 0x72;
  56. pub const DEF_CONCAT_OP: u8 = 0x73;
  57. pub const DEF_INCREMENT_OP: u8 = 0x75;
  58. pub const DEF_DECREMENT_OP: u8 = 0x76;
  59. pub const DEF_SHIFT_LEFT: u8 = 0x79;
  60. pub const DEF_SHIFT_RIGHT: u8 = 0x7a;
  61. pub const DEF_AND_OP: u8 = 0x7b;
  62. pub const DEF_CONCAT_RES_OP: u8 = 0x84;
  63. pub const DEF_L_OR_OP: u8 = 0x91;
  64. pub const DEF_L_NOT_OP: u8 = 0x92;
  65. pub const DEF_L_EQUAL_OP: u8 = 0x93;
  66. pub const DEF_L_GREATER_OP: u8 = 0x94;
  67. pub const DEF_L_LESS_OP: u8 = 0x95;
  68. pub const DEF_TO_INTEGER_OP: u8 = 0x99;
  69. pub const DEF_MID_OP: u8 = 0x9e;
  70. /*
  71. * Miscellaneous objects
  72. */
  73. pub const EXT_DEBUG_OP: u8 = 0x31;
  74. pub const LOCAL0_OP: u8 = 0x60;
  75. pub const LOCAL1_OP: u8 = 0x61;
  76. pub const LOCAL2_OP: u8 = 0x62;
  77. pub const LOCAL3_OP: u8 = 0x63;
  78. pub const LOCAL4_OP: u8 = 0x64;
  79. pub const LOCAL5_OP: u8 = 0x65;
  80. pub const LOCAL6_OP: u8 = 0x66;
  81. pub const LOCAL7_OP: u8 = 0x67;
  82. pub const ARG0_OP: u8 = 0x68;
  83. pub const ARG1_OP: u8 = 0x69;
  84. pub const ARG2_OP: u8 = 0x6a;
  85. pub const ARG3_OP: u8 = 0x6b;
  86. pub const ARG4_OP: u8 = 0x6c;
  87. pub const ARG5_OP: u8 = 0x6d;
  88. pub const ARG6_OP: u8 = 0x6e;
  89. pub const EXT_OPCODE_PREFIX: u8 = 0x5b;
  90. pub(crate) fn opcode<'a, 'c>(opcode: u8) -> impl Parser<'a, 'c, ()>
  91. where
  92. 'c: 'a,
  93. {
  94. move |input: &'a [u8], context: &'c mut AmlContext| match input.first() {
  95. None => Err((input, context, Propagate::Err(AmlError::UnexpectedEndOfStream))),
  96. Some(&byte) if byte == opcode => Ok((&input[1..], context, ())),
  97. Some(_) => Err((input, context, Propagate::Err(AmlError::WrongParser))),
  98. }
  99. }
  100. pub(crate) fn ext_opcode<'a, 'c>(ext_opcode: u8) -> impl Parser<'a, 'c, ()>
  101. where
  102. 'c: 'a,
  103. {
  104. opcode(EXT_OPCODE_PREFIX).then(opcode(ext_opcode)).discard_result()
  105. }
  106. #[cfg(test)]
  107. mod tests {
  108. use super::*;
  109. use crate::{test_utils::*, AmlError};
  110. #[test]
  111. fn empty() {
  112. let mut context = crate::test_utils::make_test_context();
  113. check_err!(opcode(NULL_NAME).parse(&[], &mut context), AmlError::UnexpectedEndOfStream, &[]);
  114. check_err!(ext_opcode(EXT_DEF_FIELD_OP).parse(&[], &mut context), AmlError::UnexpectedEndOfStream, &[]);
  115. }
  116. #[test]
  117. fn simple_opcodes() {
  118. let mut context = crate::test_utils::make_test_context();
  119. check_ok!(opcode(DEF_SCOPE_OP).parse(&[DEF_SCOPE_OP], &mut context), (), &[]);
  120. check_ok!(
  121. opcode(DEF_NAME_OP).parse(&[DEF_NAME_OP, 0x31, 0x55, 0xf3], &mut context),
  122. (),
  123. &[0x31, 0x55, 0xf3]
  124. );
  125. }
  126. #[test]
  127. fn extended_opcodes() {
  128. let mut context = crate::test_utils::make_test_context();
  129. check_err!(
  130. ext_opcode(EXT_DEF_FIELD_OP).parse(&[EXT_DEF_FIELD_OP, EXT_DEF_FIELD_OP], &mut context),
  131. AmlError::WrongParser,
  132. &[EXT_DEF_FIELD_OP, EXT_DEF_FIELD_OP]
  133. );
  134. check_ok!(
  135. ext_opcode(EXT_DEF_FIELD_OP).parse(&[EXT_OPCODE_PREFIX, EXT_DEF_FIELD_OP], &mut context),
  136. (),
  137. &[]
  138. );
  139. }
  140. }