type2.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. use crate::{
  2. name_object::{name_string, super_name, target},
  3. opcode::{self, opcode},
  4. parser::{
  5. choice,
  6. comment_scope,
  7. make_parser_concrete,
  8. n_of,
  9. take,
  10. take_to_end_of_pkglength,
  11. try_with_context,
  12. Parser,
  13. },
  14. pkg_length::pkg_length,
  15. term_object::{data_ref_object, term_arg},
  16. value::{AmlValue, Args},
  17. AmlError,
  18. DebugVerbosity,
  19. };
  20. use alloc::vec::Vec;
  21. use core::convert::TryInto;
  22. /// Type 2 opcodes return a value and so can be used in expressions.
  23. pub fn type2_opcode<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  24. where
  25. 'c: 'a,
  26. {
  27. /*
  28. * Type2Opcode := DefAquire | DefAdd | DefAnd | DefBuffer | DefConcat | DefConcatRes |
  29. * DefCondRefOf | DefCopyObject | DefDecrement | DefDerefOf | DefDivide |
  30. * DefFindSetLeftBit | DefFindSetRightBit | DefFromBCD | DefIncrement | DefIndex |
  31. * DefLAnd | DefLEqual | DefLGreater | DefLGreaterEqual | DefLLess | DefLLessEqual |
  32. * DefMid | DefLNot | DefLNotEqual | DefLoadTable | DefLOr | DefMatch | DefMod |
  33. * DefMultiply | DefNAnd | DefNOr | DefNot | DefObjectType | DefOr | DefPackage |
  34. * DefVarPackage | DefRefOf | DefShiftLeft | DefShitRight | DefSizeOf | DefStore |
  35. * DefSubtract | DefTimer | DefToBCD | DefToBuffer | DefToDecimalString |
  36. * DefToHexString | DefToInteger | DefToString | DefWait | DefXOr | MethodInvocation
  37. *
  38. * NOTE: MethodInvocation should always appear last in the choice.
  39. */
  40. make_parser_concrete!(comment_scope(
  41. DebugVerbosity::AllScopes,
  42. "Type2Opcode",
  43. choice!(
  44. def_and(),
  45. def_buffer(),
  46. def_l_equal(),
  47. def_l_or(),
  48. def_package(),
  49. def_shift_left(),
  50. def_shift_right(),
  51. def_store(),
  52. method_invocation()
  53. ),
  54. ))
  55. }
  56. pub fn def_and<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  57. where
  58. 'c: 'a,
  59. {
  60. /*
  61. * DefAnd := 0x7b Operand Operand Target
  62. * Operand := TermArg => Integer
  63. */
  64. opcode(opcode::DEF_AND_OP)
  65. .then(comment_scope(
  66. DebugVerbosity::AllScopes,
  67. "DefAnd",
  68. term_arg().then(term_arg()).then(target()).map_with_context(
  69. |((left_arg, right_arg), target), context| {
  70. let left = try_with_context!(context, left_arg.as_integer(context));
  71. let right = try_with_context!(context, right_arg.as_integer(context));
  72. (Ok(AmlValue::Integer(left & right)), context)
  73. },
  74. ),
  75. ))
  76. .map(|((), result)| Ok(result))
  77. }
  78. pub fn def_buffer<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  79. where
  80. 'c: 'a,
  81. {
  82. /*
  83. * DefBuffer := 0x11 PkgLength BufferSize ByteList
  84. * BufferSize := TermArg => Integer
  85. *
  86. * XXX: The spec says that zero-length buffers (e.g. the PkgLength is 0) are illegal, but
  87. * we've encountered them in QEMU-generated tables, so we return an empty buffer in these
  88. * cases.
  89. */
  90. opcode(opcode::DEF_BUFFER_OP)
  91. .then(comment_scope(
  92. DebugVerbosity::AllScopes,
  93. "DefBuffer",
  94. pkg_length().then(term_arg()).feed(|(pkg_length, buffer_size)| {
  95. take_to_end_of_pkglength(pkg_length).map_with_context(move |bytes, context| {
  96. let length = try_with_context!(context, buffer_size.as_integer(context));
  97. (Ok((bytes.to_vec(), length)), context)
  98. })
  99. }),
  100. ))
  101. .map(|((), (bytes, buffer_size))| Ok(AmlValue::Buffer { bytes, size: buffer_size }))
  102. }
  103. fn def_l_or<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  104. where
  105. 'c: 'a,
  106. {
  107. /*
  108. * DefLOr := 0x91 Operand Operand
  109. * Operand := TermArg => Integer
  110. */
  111. opcode(opcode::DEF_L_OR_OP)
  112. .then(comment_scope(
  113. DebugVerbosity::AllScopes,
  114. "DefLOr",
  115. term_arg().then(term_arg()).map_with_context(|(left_arg, right_arg), context| {
  116. let left = try_with_context!(context, left_arg.as_bool());
  117. let right = try_with_context!(context, right_arg.as_bool());
  118. (Ok(AmlValue::Boolean(left || right)), context)
  119. }),
  120. ))
  121. .map(|((), result)| Ok(result))
  122. }
  123. fn def_l_equal<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  124. where
  125. 'c: 'a,
  126. {
  127. /*
  128. * DefLEqual := 0x93 Operand Operand
  129. * Operand := TermArg => Integer
  130. */
  131. opcode(opcode::DEF_L_EQUAL_OP)
  132. .then(comment_scope(
  133. DebugVerbosity::AllScopes,
  134. "DefLEqual",
  135. term_arg().then(term_arg()).map_with_context(|(left_arg, right_arg), context| {
  136. let left = try_with_context!(context, left_arg.as_integer(context));
  137. let right = try_with_context!(context, right_arg.as_integer(context));
  138. (Ok(AmlValue::Boolean(left == right)), context)
  139. }),
  140. ))
  141. .map(|((), result)| Ok(result))
  142. }
  143. pub fn def_package<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  144. where
  145. 'c: 'a,
  146. {
  147. /*
  148. * DefPackage := 0x12 PkgLength NumElements PackageElementList
  149. * NumElements := ByteData
  150. * PackageElementList := Nothing | <PackageElement PackageElementList>
  151. * PackageElement := DataRefObject | NameString
  152. */
  153. opcode(opcode::DEF_PACKAGE_OP)
  154. .then(comment_scope(
  155. DebugVerbosity::AllScopes,
  156. "DefPackage",
  157. pkg_length().then(take()).feed(|(pkg_length, num_elements)| {
  158. move |mut input, mut context| {
  159. let mut package_contents = Vec::new();
  160. while pkg_length.still_parsing(input) {
  161. let (new_input, new_context, value) = package_element().parse(input, context)?;
  162. input = new_input;
  163. context = new_context;
  164. package_contents.push(value);
  165. }
  166. assert_eq!(package_contents.len(), num_elements as usize);
  167. Ok((input, context, AmlValue::Package(package_contents)))
  168. }
  169. }),
  170. ))
  171. .map(|((), package)| Ok(package))
  172. }
  173. pub fn package_element<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  174. where
  175. 'c: 'a,
  176. {
  177. choice!(data_ref_object(), name_string().map(|string| Ok(AmlValue::String(string.as_string()))))
  178. }
  179. fn def_shift_left<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  180. where
  181. 'c: 'a,
  182. {
  183. /*
  184. * DefShiftLeft := 0x79 Operand ShiftCount Target
  185. * Operand := TermArg => Integer
  186. * ShiftCount := TermArg => Integer
  187. */
  188. opcode(opcode::DEF_SHIFT_LEFT)
  189. .then(comment_scope(DebugVerbosity::Scopes, "DefShiftLeft", term_arg().then(term_arg()).then(target())))
  190. .map_with_context(|((), ((operand, shift_count), target)), context| {
  191. let operand = try_with_context!(context, operand.as_integer(context));
  192. let shift_count = try_with_context!(context, shift_count.as_integer(context));
  193. let shift_count =
  194. try_with_context!(context, shift_count.try_into().map_err(|_| AmlError::InvalidShiftLeft));
  195. let result = AmlValue::Integer(try_with_context!(
  196. context,
  197. operand.checked_shl(shift_count).ok_or(AmlError::InvalidShiftLeft)
  198. ));
  199. try_with_context!(context, context.store(target, result.clone()));
  200. (Ok(result), context)
  201. })
  202. }
  203. fn def_shift_right<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  204. where
  205. 'c: 'a,
  206. {
  207. /*
  208. * DefShiftRight := 0x7a Operand ShiftCount Target
  209. * Operand := TermArg => Integer
  210. * ShiftCount := TermArg => Integer
  211. */
  212. opcode(opcode::DEF_SHIFT_RIGHT)
  213. .then(comment_scope(DebugVerbosity::Scopes, "DefShiftRight", term_arg().then(term_arg()).then(target())))
  214. .map_with_context(|((), ((operand, shift_count), target)), context| {
  215. let operand = try_with_context!(context, operand.as_integer(context));
  216. let shift_count = try_with_context!(context, shift_count.as_integer(context));
  217. let shift_count =
  218. try_with_context!(context, shift_count.try_into().map_err(|_| AmlError::InvalidShiftRight));
  219. let result = AmlValue::Integer(try_with_context!(
  220. context,
  221. operand.checked_shr(shift_count).ok_or(AmlError::InvalidShiftRight)
  222. ));
  223. try_with_context!(context, context.store(target, result.clone()));
  224. (Ok(result), context)
  225. })
  226. }
  227. fn def_store<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  228. where
  229. 'c: 'a,
  230. {
  231. /*
  232. * DefStore := 0x70 TermArg SuperName
  233. *
  234. * Implicit conversion is only applied when the destination target is a `Name` - not when we
  235. * are storing into a method local or argument (these stores are semantically identical to
  236. * CopyObject). We must also make sure to return a copy of the data that is in the destination
  237. * after the store (as opposed to the data we think we put into it), because some stores can
  238. * alter the data during the store.
  239. */
  240. opcode(opcode::DEF_STORE_OP)
  241. .then(comment_scope(DebugVerbosity::Scopes, "DefStore", term_arg().then(super_name())))
  242. .map_with_context(|((), (value, target)), context| {
  243. (Ok(try_with_context!(context, context.store(target, value))), context)
  244. })
  245. }
  246. fn method_invocation<'a, 'c>() -> impl Parser<'a, 'c, AmlValue>
  247. where
  248. 'c: 'a,
  249. {
  250. /*
  251. * MethodInvocation := NameString TermArgList
  252. *
  253. * MethodInvocation is the worst of the AML structures, because you're meant to figure out how much you're
  254. * meant to parse using the name of the method (by knowing from its definition how how many arguments it
  255. * takes). However, the definition of a method can in theory appear after an invocation of that method, and
  256. * so parsing them properly can be very difficult.
  257. * NOTE: We don't support the case of the definition appearing after the invocation.
  258. */
  259. comment_scope(
  260. DebugVerbosity::Scopes,
  261. "MethodInvocation",
  262. name_string()
  263. .map_with_context(move |name, context| {
  264. let (full_path, handle) =
  265. try_with_context!(context, context.namespace.search(&name, &context.current_scope)).clone();
  266. /*
  267. * `None` if the path is not a method and so doesn't have arguments, or `Some(the number of
  268. * arguments to parse)` if it's a method.
  269. */
  270. let num_args = if let AmlValue::Method { flags, .. } =
  271. try_with_context!(context, context.namespace.get(handle))
  272. {
  273. Some(flags.arg_count())
  274. } else {
  275. None
  276. };
  277. (Ok((full_path, num_args)), context)
  278. })
  279. .feed(|(path, num_args)| {
  280. n_of(term_arg(), num_args.unwrap_or(0) as usize).map_with_context(move |arg_list, context| {
  281. if num_args.is_some() {
  282. let result = context.invoke_method(&path, Args::from_list(arg_list));
  283. (Ok(try_with_context!(context, result)), context)
  284. } else {
  285. (Ok(try_with_context!(context, context.namespace.get_by_path(&path)).clone()), context)
  286. }
  287. })
  288. }),
  289. )
  290. }