dmcstyle.c revision 1.7 1 1.1 christos /*******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: dmcstyle - Support for C-style operator disassembly
4 1.1 christos *
5 1.1 christos ******************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.7 christos * Copyright (C) 2000 - 2017, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpi.h"
45 1.1 christos #include "accommon.h"
46 1.1 christos #include "acparser.h"
47 1.1 christos #include "amlcode.h"
48 1.1 christos #include "acdebug.h"
49 1.1 christos
50 1.1 christos
51 1.1 christos #define _COMPONENT ACPI_CA_DEBUGGER
52 1.1 christos ACPI_MODULE_NAME ("dmcstyle")
53 1.1 christos
54 1.1 christos
55 1.1 christos /* Local prototypes */
56 1.1 christos
57 1.2 christos static const char *
58 1.1 christos AcpiDmGetCompoundSymbol (
59 1.1 christos UINT16 AslOpcode);
60 1.1 christos
61 1.1 christos static void
62 1.1 christos AcpiDmPromoteTarget (
63 1.1 christos ACPI_PARSE_OBJECT *Op,
64 1.1 christos ACPI_PARSE_OBJECT *Target);
65 1.1 christos
66 1.1 christos static BOOLEAN
67 1.1 christos AcpiDmIsValidTarget (
68 1.1 christos ACPI_PARSE_OBJECT *Op);
69 1.1 christos
70 1.1 christos static BOOLEAN
71 1.1 christos AcpiDmIsTargetAnOperand (
72 1.1 christos ACPI_PARSE_OBJECT *Target,
73 1.1 christos ACPI_PARSE_OBJECT *Operand,
74 1.1 christos BOOLEAN TopLevel);
75 1.1 christos
76 1.7 christos static BOOLEAN
77 1.7 christos AcpiDmIsOptimizationIgnored (
78 1.7 christos ACPI_PARSE_OBJECT *StoreOp,
79 1.7 christos ACPI_PARSE_OBJECT *StoreArgument);
80 1.7 christos
81 1.1 christos
82 1.1 christos /*******************************************************************************
83 1.1 christos *
84 1.1 christos * FUNCTION: AcpiDmCheckForSymbolicOpcode
85 1.1 christos *
86 1.1 christos * PARAMETERS: Op - Current parse object
87 1.1 christos * Walk - Current parse tree walk info
88 1.1 christos *
89 1.1 christos * RETURN: TRUE if opcode can be converted to symbolic, FALSE otherwise
90 1.1 christos *
91 1.1 christos * DESCRIPTION: This is the main code that implements disassembly of AML code
92 1.1 christos * to C-style operators. Called during descending phase of the
93 1.1 christos * parse tree walk.
94 1.1 christos *
95 1.1 christos ******************************************************************************/
96 1.1 christos
97 1.1 christos BOOLEAN
98 1.1 christos AcpiDmCheckForSymbolicOpcode (
99 1.1 christos ACPI_PARSE_OBJECT *Op,
100 1.1 christos ACPI_OP_WALK_INFO *Info)
101 1.1 christos {
102 1.2 christos const char *OperatorSymbol = NULL;
103 1.7 christos ACPI_PARSE_OBJECT *Argument1;
104 1.7 christos ACPI_PARSE_OBJECT *Argument2;
105 1.1 christos ACPI_PARSE_OBJECT *Target;
106 1.7 christos ACPI_PARSE_OBJECT *Target2;
107 1.1 christos
108 1.1 christos
109 1.1 christos /* Exit immediately if ASL+ not enabled */
110 1.1 christos
111 1.1 christos if (!AcpiGbl_CstyleDisassembly)
112 1.1 christos {
113 1.1 christos return (FALSE);
114 1.1 christos }
115 1.1 christos
116 1.1 christos /* Get the first operand */
117 1.1 christos
118 1.7 christos Argument1 = AcpiPsGetArg (Op, 0);
119 1.7 christos if (!Argument1)
120 1.1 christos {
121 1.1 christos return (FALSE);
122 1.1 christos }
123 1.1 christos
124 1.1 christos /* Get the second operand */
125 1.1 christos
126 1.7 christos Argument2 = Argument1->Common.Next;
127 1.1 christos
128 1.1 christos /* Setup the operator string for this opcode */
129 1.1 christos
130 1.1 christos switch (Op->Common.AmlOpcode)
131 1.1 christos {
132 1.1 christos case AML_ADD_OP:
133 1.1 christos OperatorSymbol = " + ";
134 1.1 christos break;
135 1.1 christos
136 1.1 christos case AML_SUBTRACT_OP:
137 1.1 christos OperatorSymbol = " - ";
138 1.1 christos break;
139 1.1 christos
140 1.1 christos case AML_MULTIPLY_OP:
141 1.1 christos OperatorSymbol = " * ";
142 1.1 christos break;
143 1.1 christos
144 1.1 christos case AML_DIVIDE_OP:
145 1.1 christos OperatorSymbol = " / ";
146 1.1 christos break;
147 1.1 christos
148 1.1 christos case AML_MOD_OP:
149 1.1 christos OperatorSymbol = " % ";
150 1.1 christos break;
151 1.1 christos
152 1.1 christos case AML_SHIFT_LEFT_OP:
153 1.1 christos OperatorSymbol = " << ";
154 1.1 christos break;
155 1.1 christos
156 1.1 christos case AML_SHIFT_RIGHT_OP:
157 1.1 christos OperatorSymbol = " >> ";
158 1.1 christos break;
159 1.1 christos
160 1.1 christos case AML_BIT_AND_OP:
161 1.1 christos OperatorSymbol = " & ";
162 1.1 christos break;
163 1.1 christos
164 1.1 christos case AML_BIT_OR_OP:
165 1.1 christos OperatorSymbol = " | ";
166 1.1 christos break;
167 1.1 christos
168 1.1 christos case AML_BIT_XOR_OP:
169 1.1 christos OperatorSymbol = " ^ ";
170 1.1 christos break;
171 1.1 christos
172 1.1 christos /* Logical operators, no target */
173 1.1 christos
174 1.1 christos case AML_LAND_OP:
175 1.1 christos OperatorSymbol = " && ";
176 1.1 christos break;
177 1.1 christos
178 1.1 christos case AML_LEQUAL_OP:
179 1.1 christos OperatorSymbol = " == ";
180 1.1 christos break;
181 1.1 christos
182 1.1 christos case AML_LGREATER_OP:
183 1.1 christos OperatorSymbol = " > ";
184 1.1 christos break;
185 1.1 christos
186 1.1 christos case AML_LLESS_OP:
187 1.1 christos OperatorSymbol = " < ";
188 1.1 christos break;
189 1.1 christos
190 1.1 christos case AML_LOR_OP:
191 1.1 christos OperatorSymbol = " || ";
192 1.1 christos break;
193 1.1 christos
194 1.1 christos case AML_LNOT_OP:
195 1.1 christos /*
196 1.1 christos * Check for the LNOT sub-opcodes. These correspond to
197 1.1 christos * LNotEqual, LLessEqual, and LGreaterEqual. There are
198 1.1 christos * no actual AML opcodes for these operators.
199 1.1 christos */
200 1.7 christos switch (Argument1->Common.AmlOpcode)
201 1.1 christos {
202 1.1 christos case AML_LEQUAL_OP:
203 1.1 christos OperatorSymbol = " != ";
204 1.1 christos break;
205 1.1 christos
206 1.1 christos case AML_LGREATER_OP:
207 1.1 christos OperatorSymbol = " <= ";
208 1.1 christos break;
209 1.1 christos
210 1.1 christos case AML_LLESS_OP:
211 1.1 christos OperatorSymbol = " >= ";
212 1.1 christos break;
213 1.1 christos
214 1.1 christos default:
215 1.1 christos
216 1.1 christos /* Unary LNOT case, emit "!" immediately */
217 1.1 christos
218 1.1 christos AcpiOsPrintf ("!");
219 1.1 christos return (TRUE);
220 1.1 christos }
221 1.1 christos
222 1.7 christos Argument1->Common.DisasmOpcode = ACPI_DASM_LNOT_SUFFIX;
223 1.1 christos Op->Common.DisasmOpcode = ACPI_DASM_LNOT_PREFIX;
224 1.1 christos
225 1.1 christos /* Save symbol string in the next child (not peer) */
226 1.1 christos
227 1.7 christos Argument2 = AcpiPsGetArg (Argument1, 0);
228 1.7 christos if (!Argument2)
229 1.1 christos {
230 1.1 christos return (FALSE);
231 1.1 christos }
232 1.1 christos
233 1.7 christos Argument2->Common.OperatorSymbol = OperatorSymbol;
234 1.1 christos return (TRUE);
235 1.1 christos
236 1.1 christos case AML_INDEX_OP:
237 1.4 christos /*
238 1.4 christos * Check for constant source operand. Note: although technically
239 1.4 christos * legal syntax, the iASL compiler does not support this with
240 1.4 christos * the symbolic operators for Index(). It doesn't make sense to
241 1.4 christos * use Index() with a constant anyway.
242 1.4 christos */
243 1.7 christos if ((Argument1->Common.AmlOpcode == AML_STRING_OP) ||
244 1.7 christos (Argument1->Common.AmlOpcode == AML_BUFFER_OP) ||
245 1.7 christos (Argument1->Common.AmlOpcode == AML_PACKAGE_OP) ||
246 1.7 christos (Argument1->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
247 1.4 christos {
248 1.4 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_CLOSING_PAREN;
249 1.4 christos return (FALSE);
250 1.4 christos }
251 1.4 christos
252 1.4 christos /* Index operator is [] */
253 1.4 christos
254 1.7 christos Argument1->Common.OperatorSymbol = " [";
255 1.7 christos Argument2->Common.OperatorSymbol = "]";
256 1.1 christos break;
257 1.1 christos
258 1.1 christos /* Unary operators */
259 1.1 christos
260 1.1 christos case AML_DECREMENT_OP:
261 1.1 christos OperatorSymbol = "--";
262 1.1 christos break;
263 1.1 christos
264 1.1 christos case AML_INCREMENT_OP:
265 1.1 christos OperatorSymbol = "++";
266 1.1 christos break;
267 1.1 christos
268 1.1 christos case AML_BIT_NOT_OP:
269 1.1 christos case AML_STORE_OP:
270 1.1 christos OperatorSymbol = NULL;
271 1.1 christos break;
272 1.1 christos
273 1.1 christos default:
274 1.1 christos return (FALSE);
275 1.1 christos }
276 1.1 christos
277 1.7 christos if (Argument1->Common.DisasmOpcode == ACPI_DASM_LNOT_SUFFIX)
278 1.1 christos {
279 1.1 christos return (TRUE);
280 1.1 christos }
281 1.1 christos
282 1.1 christos /*
283 1.1 christos * This is the key to how the disassembly of the C-style operators
284 1.1 christos * works. We save the operator symbol in the first child, thus
285 1.1 christos * deferring symbol output until after the first operand has been
286 1.1 christos * emitted.
287 1.1 christos */
288 1.7 christos if (!Argument1->Common.OperatorSymbol)
289 1.1 christos {
290 1.7 christos Argument1->Common.OperatorSymbol = OperatorSymbol;
291 1.1 christos }
292 1.1 christos
293 1.1 christos /*
294 1.1 christos * Check for a valid target as the 3rd (or sometimes 2nd) operand
295 1.1 christos *
296 1.1 christos * Compound assignment operator support:
297 1.1 christos * Attempt to optimize constructs of the form:
298 1.1 christos * Add (Local1, 0xFF, Local1)
299 1.1 christos * to:
300 1.1 christos * Local1 += 0xFF
301 1.1 christos *
302 1.1 christos * Only the math operators and Store() have a target.
303 1.1 christos * Logicals have no target.
304 1.1 christos */
305 1.1 christos switch (Op->Common.AmlOpcode)
306 1.1 christos {
307 1.1 christos case AML_ADD_OP:
308 1.1 christos case AML_SUBTRACT_OP:
309 1.1 christos case AML_MULTIPLY_OP:
310 1.1 christos case AML_DIVIDE_OP:
311 1.1 christos case AML_MOD_OP:
312 1.1 christos case AML_SHIFT_LEFT_OP:
313 1.1 christos case AML_SHIFT_RIGHT_OP:
314 1.1 christos case AML_BIT_AND_OP:
315 1.1 christos case AML_BIT_OR_OP:
316 1.1 christos case AML_BIT_XOR_OP:
317 1.1 christos
318 1.1 christos /* Target is 3rd operand */
319 1.1 christos
320 1.7 christos Target = Argument2->Common.Next;
321 1.1 christos if (Op->Common.AmlOpcode == AML_DIVIDE_OP)
322 1.1 christos {
323 1.7 christos Target2 = Target->Common.Next;
324 1.7 christos
325 1.1 christos /*
326 1.1 christos * Divide has an extra target operand (Remainder).
327 1.7 christos * Default behavior is to simply ignore ASL+ conversion
328 1.7 christos * if the remainder target (modulo) is specified.
329 1.1 christos */
330 1.7 christos if (!AcpiGbl_DoDisassemblerOptimizations)
331 1.1 christos {
332 1.7 christos if (AcpiDmIsValidTarget (Target))
333 1.7 christos {
334 1.7 christos Argument1->Common.OperatorSymbol = NULL;
335 1.7 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
336 1.7 christos return (FALSE);
337 1.7 christos }
338 1.7 christos
339 1.7 christos Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
340 1.7 christos Target = Target2;
341 1.1 christos }
342 1.7 christos else
343 1.7 christos {
344 1.7 christos /*
345 1.7 christos * Divide has an extra target operand (Remainder).
346 1.7 christos * If both targets are specified, it cannot be converted
347 1.7 christos * to a C-style operator.
348 1.7 christos */
349 1.7 christos if (AcpiDmIsValidTarget (Target) &&
350 1.7 christos AcpiDmIsValidTarget (Target2))
351 1.7 christos {
352 1.7 christos Argument1->Common.OperatorSymbol = NULL;
353 1.7 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
354 1.7 christos return (FALSE);
355 1.7 christos }
356 1.7 christos
357 1.7 christos if (AcpiDmIsValidTarget (Target)) /* Only first Target is valid (remainder) */
358 1.7 christos {
359 1.7 christos /* Convert the Divide to Modulo */
360 1.1 christos
361 1.7 christos Op->Common.AmlOpcode = AML_MOD_OP;
362 1.7 christos
363 1.7 christos Argument1->Common.OperatorSymbol = " % ";
364 1.7 christos Target2->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
365 1.7 christos }
366 1.7 christos else /* Only second Target (quotient) is valid */
367 1.7 christos {
368 1.7 christos Target->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
369 1.7 christos Target = Target2;
370 1.7 christos }
371 1.7 christos }
372 1.1 christos }
373 1.1 christos
374 1.1 christos /* Parser should ensure there is at least a placeholder target */
375 1.1 christos
376 1.1 christos if (!Target)
377 1.1 christos {
378 1.1 christos return (FALSE);
379 1.1 christos }
380 1.1 christos
381 1.1 christos if (!AcpiDmIsValidTarget (Target))
382 1.1 christos {
383 1.1 christos /* Not a valid target (placeholder only, from parser) */
384 1.1 christos break;
385 1.1 christos }
386 1.1 christos
387 1.1 christos /*
388 1.1 christos * Promote the target up to the first child in the parse
389 1.1 christos * tree. This is done because the target will be output
390 1.1 christos * first, in the form:
391 1.1 christos * <Target> = Operands...
392 1.1 christos */
393 1.1 christos AcpiDmPromoteTarget (Op, Target);
394 1.1 christos
395 1.3 christos /* Check operands for conversion to a "Compound Assignment" */
396 1.3 christos
397 1.3 christos switch (Op->Common.AmlOpcode)
398 1.1 christos {
399 1.3 christos /* Commutative operators */
400 1.3 christos
401 1.3 christos case AML_ADD_OP:
402 1.3 christos case AML_MULTIPLY_OP:
403 1.3 christos case AML_BIT_AND_OP:
404 1.3 christos case AML_BIT_OR_OP:
405 1.3 christos case AML_BIT_XOR_OP:
406 1.3 christos /*
407 1.3 christos * For the commutative operators, we can convert to a
408 1.3 christos * compound statement only if at least one (either) operand
409 1.3 christos * is the same as the target.
410 1.3 christos *
411 1.3 christos * Add (A, B, A) --> A += B
412 1.3 christos * Add (B, A, A) --> A += B
413 1.3 christos * Add (B, C, A) --> A = (B + C)
414 1.3 christos */
415 1.7 christos if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)) ||
416 1.7 christos (AcpiDmIsTargetAnOperand (Target, Argument2, TRUE)))
417 1.3 christos {
418 1.3 christos Target->Common.OperatorSymbol =
419 1.3 christos AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
420 1.1 christos
421 1.3 christos /* Convert operator to compound assignment */
422 1.1 christos
423 1.5 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
424 1.7 christos Argument1->Common.OperatorSymbol = NULL;
425 1.3 christos return (TRUE);
426 1.3 christos }
427 1.3 christos break;
428 1.3 christos
429 1.3 christos /* Non-commutative operators */
430 1.3 christos
431 1.3 christos case AML_SUBTRACT_OP:
432 1.3 christos case AML_DIVIDE_OP:
433 1.3 christos case AML_MOD_OP:
434 1.3 christos case AML_SHIFT_LEFT_OP:
435 1.3 christos case AML_SHIFT_RIGHT_OP:
436 1.3 christos /*
437 1.3 christos * For the non-commutative operators, we can convert to a
438 1.3 christos * compound statement only if the target is the same as the
439 1.3 christos * first operand.
440 1.3 christos *
441 1.3 christos * Subtract (A, B, A) --> A -= B
442 1.3 christos * Subtract (B, A, A) --> A = (B - A)
443 1.3 christos */
444 1.7 christos if ((AcpiDmIsTargetAnOperand (Target, Argument1, TRUE)))
445 1.3 christos {
446 1.3 christos Target->Common.OperatorSymbol =
447 1.3 christos AcpiDmGetCompoundSymbol (Op->Common.AmlOpcode);
448 1.3 christos
449 1.3 christos /* Convert operator to compound assignment */
450 1.3 christos
451 1.5 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_COMPOUND_ASSIGNMENT;
452 1.7 christos Argument1->Common.OperatorSymbol = NULL;
453 1.3 christos return (TRUE);
454 1.3 christos }
455 1.3 christos break;
456 1.3 christos
457 1.3 christos default:
458 1.3 christos break;
459 1.1 christos }
460 1.1 christos
461 1.1 christos /*
462 1.1 christos * If we are within a C-style expression, emit an extra open
463 1.1 christos * paren. Implemented by examining the parent op.
464 1.1 christos */
465 1.1 christos switch (Op->Common.Parent->Common.AmlOpcode)
466 1.1 christos {
467 1.1 christos case AML_ADD_OP:
468 1.1 christos case AML_SUBTRACT_OP:
469 1.1 christos case AML_MULTIPLY_OP:
470 1.1 christos case AML_DIVIDE_OP:
471 1.1 christos case AML_MOD_OP:
472 1.1 christos case AML_SHIFT_LEFT_OP:
473 1.1 christos case AML_SHIFT_RIGHT_OP:
474 1.1 christos case AML_BIT_AND_OP:
475 1.1 christos case AML_BIT_OR_OP:
476 1.1 christos case AML_BIT_XOR_OP:
477 1.1 christos case AML_LAND_OP:
478 1.1 christos case AML_LEQUAL_OP:
479 1.1 christos case AML_LGREATER_OP:
480 1.1 christos case AML_LLESS_OP:
481 1.1 christos case AML_LOR_OP:
482 1.1 christos
483 1.1 christos Op->Common.DisasmFlags |= ACPI_PARSEOP_ASSIGNMENT;
484 1.1 christos AcpiOsPrintf ("(");
485 1.1 christos break;
486 1.1 christos
487 1.1 christos default:
488 1.1 christos break;
489 1.1 christos }
490 1.1 christos
491 1.1 christos /* Normal output for ASL/AML operators with a target operand */
492 1.1 christos
493 1.1 christos Target->Common.OperatorSymbol = " = (";
494 1.1 christos return (TRUE);
495 1.1 christos
496 1.1 christos /* Binary operators, no parens */
497 1.1 christos
498 1.1 christos case AML_DECREMENT_OP:
499 1.1 christos case AML_INCREMENT_OP:
500 1.1 christos return (TRUE);
501 1.1 christos
502 1.1 christos case AML_INDEX_OP:
503 1.1 christos
504 1.1 christos /* Target is optional, 3rd operand */
505 1.1 christos
506 1.7 christos Target = Argument2->Common.Next;
507 1.1 christos if (AcpiDmIsValidTarget (Target))
508 1.1 christos {
509 1.1 christos AcpiDmPromoteTarget (Op, Target);
510 1.1 christos
511 1.1 christos if (!Target->Common.OperatorSymbol)
512 1.1 christos {
513 1.1 christos Target->Common.OperatorSymbol = " = ";
514 1.1 christos }
515 1.1 christos }
516 1.1 christos return (TRUE);
517 1.1 christos
518 1.1 christos case AML_STORE_OP:
519 1.1 christos /*
520 1.7 christos * For Store, the Target is the 2nd operand. We know the target
521 1.7 christos * is valid, because it is not optional.
522 1.6 christos *
523 1.7 christos * Ignore any optimizations/folding if flag is set.
524 1.7 christos * Used for iASL/disassembler test suite only.
525 1.6 christos */
526 1.7 christos if (AcpiDmIsOptimizationIgnored (Op, Argument1))
527 1.6 christos {
528 1.7 christos return (FALSE);
529 1.6 christos }
530 1.6 christos
531 1.6 christos /*
532 1.7 christos * Perform conversion.
533 1.1 christos * In the parse tree, simply swap the target with the
534 1.1 christos * source so that the target is processed first.
535 1.1 christos */
536 1.7 christos Target = Argument1->Common.Next;
537 1.3 christos if (!Target)
538 1.3 christos {
539 1.3 christos return (FALSE);
540 1.3 christos }
541 1.3 christos
542 1.1 christos AcpiDmPromoteTarget (Op, Target);
543 1.1 christos if (!Target->Common.OperatorSymbol)
544 1.1 christos {
545 1.1 christos Target->Common.OperatorSymbol = " = ";
546 1.1 christos }
547 1.1 christos return (TRUE);
548 1.1 christos
549 1.1 christos case AML_BIT_NOT_OP:
550 1.1 christos
551 1.1 christos /* Target is optional, 2nd operand */
552 1.1 christos
553 1.7 christos Target = Argument1->Common.Next;
554 1.1 christos if (!Target)
555 1.1 christos {
556 1.1 christos return (FALSE);
557 1.1 christos }
558 1.1 christos
559 1.1 christos if (AcpiDmIsValidTarget (Target))
560 1.1 christos {
561 1.1 christos /* Valid target, not a placeholder */
562 1.1 christos
563 1.1 christos AcpiDmPromoteTarget (Op, Target);
564 1.1 christos Target->Common.OperatorSymbol = " = ~";
565 1.1 christos }
566 1.1 christos else
567 1.1 christos {
568 1.1 christos /* No target. Emit this prefix operator immediately */
569 1.1 christos
570 1.1 christos AcpiOsPrintf ("~");
571 1.1 christos }
572 1.1 christos return (TRUE);
573 1.1 christos
574 1.1 christos default:
575 1.1 christos break;
576 1.1 christos }
577 1.1 christos
578 1.7 christos /* All other operators, emit an open paren */
579 1.7 christos
580 1.7 christos AcpiOsPrintf ("(");
581 1.7 christos return (TRUE);
582 1.7 christos }
583 1.7 christos
584 1.7 christos
585 1.7 christos /*******************************************************************************
586 1.7 christos *
587 1.7 christos * FUNCTION: AcpiDmIsOptimizationIgnored
588 1.7 christos *
589 1.7 christos * PARAMETERS: StoreOp - Store operator parse object
590 1.7 christos * StoreArgument - Target associate with the Op
591 1.7 christos *
592 1.7 christos * RETURN: TRUE if this Store operator should not be converted/removed.
593 1.7 christos *
594 1.7 christos * DESCRIPTION: The following function implements "Do not optimize if a
595 1.7 christos * store is immediately followed by a math/bit operator that
596 1.7 christos * has no target".
597 1.7 christos *
598 1.7 christos * Function is ignored if DoDisassemblerOptimizations is TRUE.
599 1.7 christos * This is the default, ignore this function.
600 1.7 christos *
601 1.7 christos * Disables these types of optimizations, and simply emits
602 1.7 christos * legacy ASL code:
603 1.7 christos * Store (Add (INT1, 4), INT2) --> Add (INT1, 4, INT2)
604 1.7 christos * --> INT2 = INT1 + 4
605 1.7 christos *
606 1.7 christos * Store (Not (INT1), INT2) --> Not (INT1, INT2)
607 1.7 christos * --> INT2 = ~INT1
608 1.7 christos *
609 1.7 christos * Used only for the ASL test suite. For the test suite, we
610 1.7 christos * don't want to perform some optimizations to ensure binary
611 1.7 christos * compatibility with the generation of the legacy ASL->AML.
612 1.7 christos * In other words, for all test modules we want exactly:
613 1.7 christos * (ASL+ -> AML) == (ASL- -> AML)
614 1.7 christos *
615 1.7 christos ******************************************************************************/
616 1.7 christos
617 1.7 christos static BOOLEAN
618 1.7 christos AcpiDmIsOptimizationIgnored (
619 1.7 christos ACPI_PARSE_OBJECT *StoreOp,
620 1.7 christos ACPI_PARSE_OBJECT *StoreArgument)
621 1.7 christos {
622 1.7 christos ACPI_PARSE_OBJECT *Argument1;
623 1.7 christos ACPI_PARSE_OBJECT *Argument2;
624 1.7 christos ACPI_PARSE_OBJECT *Target;
625 1.7 christos
626 1.7 christos
627 1.7 christos /* No optimizations/folding for the typical case */
628 1.7 christos
629 1.7 christos if (AcpiGbl_DoDisassemblerOptimizations)
630 1.7 christos {
631 1.7 christos return (FALSE);
632 1.7 christos }
633 1.7 christos
634 1.5 christos /*
635 1.7 christos * Only a small subset of ASL/AML operators can be optimized.
636 1.7 christos * Can only optimize/fold if there is no target (or targets)
637 1.7 christos * specified for the operator. And of course, the operator
638 1.7 christos * is surrrounded by a Store() operator.
639 1.5 christos */
640 1.7 christos switch (StoreArgument->Common.AmlOpcode)
641 1.5 christos {
642 1.7 christos case AML_ADD_OP:
643 1.7 christos case AML_SUBTRACT_OP:
644 1.7 christos case AML_MULTIPLY_OP:
645 1.7 christos case AML_MOD_OP:
646 1.7 christos case AML_SHIFT_LEFT_OP:
647 1.7 christos case AML_SHIFT_RIGHT_OP:
648 1.7 christos case AML_BIT_AND_OP:
649 1.7 christos case AML_BIT_OR_OP:
650 1.7 christos case AML_BIT_XOR_OP:
651 1.7 christos case AML_INDEX_OP:
652 1.7 christos
653 1.7 christos /* These operators have two arguments and one target */
654 1.7 christos
655 1.7 christos Argument1 = StoreArgument->Common.Value.Arg;
656 1.7 christos Argument2 = Argument1->Common.Next;
657 1.7 christos Target = Argument2->Common.Next;
658 1.7 christos
659 1.7 christos if (!AcpiDmIsValidTarget (Target))
660 1.7 christos {
661 1.7 christos StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
662 1.7 christos return (TRUE);
663 1.7 christos }
664 1.7 christos break;
665 1.7 christos
666 1.7 christos case AML_DIVIDE_OP:
667 1.7 christos
668 1.7 christos /* This operator has two arguments and two targets */
669 1.7 christos
670 1.7 christos Argument1 = StoreArgument->Common.Value.Arg;
671 1.7 christos Argument2 = Argument1->Common.Next;
672 1.7 christos Target = Argument2->Common.Next;
673 1.7 christos
674 1.7 christos if (!AcpiDmIsValidTarget (Target) ||
675 1.7 christos !AcpiDmIsValidTarget (Target->Common.Next))
676 1.7 christos {
677 1.7 christos StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
678 1.7 christos return (TRUE);
679 1.7 christos }
680 1.7 christos break;
681 1.7 christos
682 1.7 christos case AML_BIT_NOT_OP:
683 1.7 christos
684 1.7 christos /* This operator has one operand and one target */
685 1.7 christos
686 1.7 christos Argument1 = StoreArgument->Common.Value.Arg;
687 1.7 christos Target = Argument1->Common.Next;
688 1.7 christos
689 1.7 christos if (!AcpiDmIsValidTarget (Target))
690 1.7 christos {
691 1.7 christos StoreOp->Common.DisasmFlags |= ACPI_PARSEOP_LEGACY_ASL_ONLY;
692 1.7 christos return (TRUE);
693 1.7 christos }
694 1.7 christos break;
695 1.7 christos
696 1.7 christos default:
697 1.7 christos break;
698 1.5 christos }
699 1.5 christos
700 1.7 christos return (FALSE);
701 1.1 christos }
702 1.1 christos
703 1.1 christos
704 1.1 christos /*******************************************************************************
705 1.1 christos *
706 1.1 christos * FUNCTION: AcpiDmCloseOperator
707 1.1 christos *
708 1.1 christos * PARAMETERS: Op - Current parse object
709 1.1 christos *
710 1.1 christos * RETURN: None
711 1.1 christos *
712 1.1 christos * DESCRIPTION: Closes an operator by adding a closing parentheses if and
713 1.1 christos * when necessary. Called during ascending phase of the
714 1.1 christos * parse tree walk.
715 1.1 christos *
716 1.1 christos ******************************************************************************/
717 1.1 christos
718 1.1 christos void
719 1.1 christos AcpiDmCloseOperator (
720 1.1 christos ACPI_PARSE_OBJECT *Op)
721 1.1 christos {
722 1.6 christos
723 1.1 christos /* Always emit paren if ASL+ disassembly disabled */
724 1.1 christos
725 1.1 christos if (!AcpiGbl_CstyleDisassembly)
726 1.1 christos {
727 1.1 christos AcpiOsPrintf (")");
728 1.1 christos return;
729 1.1 christos }
730 1.1 christos
731 1.6 christos if (Op->Common.DisasmFlags & ACPI_PARSEOP_LEGACY_ASL_ONLY)
732 1.6 christos {
733 1.6 christos AcpiOsPrintf (")");
734 1.6 christos return;
735 1.6 christos }
736 1.6 christos
737 1.1 christos /* Check if we need to add an additional closing paren */
738 1.1 christos
739 1.1 christos switch (Op->Common.AmlOpcode)
740 1.1 christos {
741 1.1 christos case AML_ADD_OP:
742 1.1 christos case AML_SUBTRACT_OP:
743 1.1 christos case AML_MULTIPLY_OP:
744 1.1 christos case AML_DIVIDE_OP:
745 1.1 christos case AML_MOD_OP:
746 1.1 christos case AML_SHIFT_LEFT_OP:
747 1.1 christos case AML_SHIFT_RIGHT_OP:
748 1.1 christos case AML_BIT_AND_OP:
749 1.1 christos case AML_BIT_OR_OP:
750 1.1 christos case AML_BIT_XOR_OP:
751 1.1 christos case AML_LAND_OP:
752 1.1 christos case AML_LEQUAL_OP:
753 1.1 christos case AML_LGREATER_OP:
754 1.1 christos case AML_LLESS_OP:
755 1.1 christos case AML_LOR_OP:
756 1.1 christos
757 1.1 christos /* Emit paren only if this is not a compound assignment */
758 1.1 christos
759 1.5 christos if (Op->Common.DisasmFlags & ACPI_PARSEOP_COMPOUND_ASSIGNMENT)
760 1.1 christos {
761 1.1 christos return;
762 1.1 christos }
763 1.1 christos
764 1.1 christos /* Emit extra close paren for assignment within an expression */
765 1.1 christos
766 1.1 christos if (Op->Common.DisasmFlags & ACPI_PARSEOP_ASSIGNMENT)
767 1.1 christos {
768 1.1 christos AcpiOsPrintf (")");
769 1.1 christos }
770 1.1 christos break;
771 1.1 christos
772 1.4 christos case AML_INDEX_OP:
773 1.4 christos
774 1.4 christos /* This is case for unsupported Index() source constants */
775 1.4 christos
776 1.4 christos if (Op->Common.DisasmFlags & ACPI_PARSEOP_CLOSING_PAREN)
777 1.4 christos {
778 1.4 christos AcpiOsPrintf (")");
779 1.4 christos }
780 1.4 christos return;
781 1.1 christos
782 1.1 christos /* No need for parens for these */
783 1.1 christos
784 1.1 christos case AML_DECREMENT_OP:
785 1.1 christos case AML_INCREMENT_OP:
786 1.1 christos case AML_LNOT_OP:
787 1.1 christos case AML_BIT_NOT_OP:
788 1.1 christos case AML_STORE_OP:
789 1.1 christos return;
790 1.1 christos
791 1.1 christos default:
792 1.1 christos
793 1.1 christos /* Always emit paren for non-ASL+ operators */
794 1.1 christos break;
795 1.1 christos }
796 1.1 christos
797 1.1 christos AcpiOsPrintf (")");
798 1.1 christos }
799 1.1 christos
800 1.1 christos
801 1.1 christos /*******************************************************************************
802 1.1 christos *
803 1.1 christos * FUNCTION: AcpiDmGetCompoundSymbol
804 1.1 christos *
805 1.1 christos * PARAMETERS: AslOpcode
806 1.1 christos *
807 1.1 christos * RETURN: String containing the compound assignment symbol
808 1.1 christos *
809 1.1 christos * DESCRIPTION: Detect opcodes that can be converted to compound assignment,
810 1.1 christos * return the appropriate operator string.
811 1.1 christos *
812 1.1 christos ******************************************************************************/
813 1.1 christos
814 1.2 christos static const char *
815 1.1 christos AcpiDmGetCompoundSymbol (
816 1.1 christos UINT16 AmlOpcode)
817 1.1 christos {
818 1.2 christos const char *Symbol;
819 1.1 christos
820 1.1 christos
821 1.1 christos switch (AmlOpcode)
822 1.1 christos {
823 1.1 christos case AML_ADD_OP:
824 1.1 christos Symbol = " += ";
825 1.1 christos break;
826 1.1 christos
827 1.1 christos case AML_SUBTRACT_OP:
828 1.1 christos Symbol = " -= ";
829 1.1 christos break;
830 1.1 christos
831 1.1 christos case AML_MULTIPLY_OP:
832 1.1 christos Symbol = " *= ";
833 1.1 christos break;
834 1.1 christos
835 1.1 christos case AML_DIVIDE_OP:
836 1.1 christos Symbol = " /= ";
837 1.1 christos break;
838 1.1 christos
839 1.1 christos case AML_MOD_OP:
840 1.1 christos Symbol = " %= ";
841 1.1 christos break;
842 1.1 christos
843 1.1 christos case AML_SHIFT_LEFT_OP:
844 1.1 christos Symbol = " <<= ";
845 1.1 christos break;
846 1.1 christos
847 1.1 christos case AML_SHIFT_RIGHT_OP:
848 1.1 christos Symbol = " >>= ";
849 1.1 christos break;
850 1.1 christos
851 1.1 christos case AML_BIT_AND_OP:
852 1.1 christos Symbol = " &= ";
853 1.1 christos break;
854 1.1 christos
855 1.1 christos case AML_BIT_OR_OP:
856 1.1 christos Symbol = " |= ";
857 1.1 christos break;
858 1.1 christos
859 1.1 christos case AML_BIT_XOR_OP:
860 1.1 christos Symbol = " ^= ";
861 1.1 christos break;
862 1.1 christos
863 1.1 christos default:
864 1.1 christos
865 1.1 christos /* No operator string for all other opcodes */
866 1.4 christos
867 1.1 christos return (NULL);
868 1.1 christos }
869 1.1 christos
870 1.1 christos return (Symbol);
871 1.1 christos }
872 1.1 christos
873 1.1 christos
874 1.1 christos /*******************************************************************************
875 1.1 christos *
876 1.1 christos * FUNCTION: AcpiDmPromoteTarget
877 1.1 christos *
878 1.1 christos * PARAMETERS: Op - Operator parse object
879 1.1 christos * Target - Target associate with the Op
880 1.1 christos *
881 1.1 christos * RETURN: None
882 1.1 christos *
883 1.1 christos * DESCRIPTION: Transform the parse tree by moving the target up to the first
884 1.1 christos * child of the Op.
885 1.1 christos *
886 1.1 christos ******************************************************************************/
887 1.1 christos
888 1.1 christos static void
889 1.1 christos AcpiDmPromoteTarget (
890 1.1 christos ACPI_PARSE_OBJECT *Op,
891 1.1 christos ACPI_PARSE_OBJECT *Target)
892 1.1 christos {
893 1.1 christos ACPI_PARSE_OBJECT *Child;
894 1.1 christos
895 1.1 christos
896 1.1 christos /* Link target directly to the Op as first child */
897 1.1 christos
898 1.1 christos Child = Op->Common.Value.Arg;
899 1.1 christos Op->Common.Value.Arg = Target;
900 1.1 christos Target->Common.Next = Child;
901 1.1 christos
902 1.1 christos /* Find the last peer, it is linked to the target. Unlink it. */
903 1.1 christos
904 1.1 christos while (Child->Common.Next != Target)
905 1.1 christos {
906 1.1 christos Child = Child->Common.Next;
907 1.1 christos }
908 1.1 christos
909 1.1 christos Child->Common.Next = NULL;
910 1.1 christos }
911 1.1 christos
912 1.1 christos
913 1.1 christos /*******************************************************************************
914 1.1 christos *
915 1.1 christos * FUNCTION: AcpiDmIsValidTarget
916 1.1 christos *
917 1.1 christos * PARAMETERS: Target - Target Op from the parse tree
918 1.1 christos *
919 1.1 christos * RETURN: TRUE if the Target is real. FALSE if it is just a placeholder
920 1.1 christos * Op that was inserted by the parser.
921 1.1 christos *
922 1.1 christos * DESCRIPTION: Determine if a Target Op is a placeholder Op or a real Target.
923 1.1 christos * In other words, determine if the optional target is used or
924 1.3 christos * not. Note: If Target is NULL, something is seriously wrong,
925 1.3 christos * probably with the parse tree.
926 1.1 christos *
927 1.1 christos ******************************************************************************/
928 1.1 christos
929 1.1 christos static BOOLEAN
930 1.1 christos AcpiDmIsValidTarget (
931 1.1 christos ACPI_PARSE_OBJECT *Target)
932 1.1 christos {
933 1.1 christos
934 1.3 christos if (!Target)
935 1.3 christos {
936 1.3 christos return (FALSE);
937 1.3 christos }
938 1.3 christos
939 1.1 christos if ((Target->Common.AmlOpcode == AML_INT_NAMEPATH_OP) &&
940 1.1 christos (Target->Common.Value.Arg == NULL))
941 1.1 christos {
942 1.1 christos return (FALSE);
943 1.1 christos }
944 1.1 christos
945 1.1 christos return (TRUE);
946 1.1 christos }
947 1.1 christos
948 1.1 christos
949 1.1 christos /*******************************************************************************
950 1.1 christos *
951 1.1 christos * FUNCTION: AcpiDmIsTargetAnOperand
952 1.1 christos *
953 1.1 christos * PARAMETERS: Target - Target associated with the expression
954 1.1 christos * Operand - An operand associated with expression
955 1.1 christos *
956 1.1 christos * RETURN: TRUE if expression can be converted to a compound assignment.
957 1.1 christos * FALSE otherwise.
958 1.1 christos *
959 1.1 christos * DESCRIPTION: Determine if the Target duplicates the operand, in order to
960 1.1 christos * detect if the expression can be converted to a compound
961 1.1 christos * assigment. (+=, *=, etc.)
962 1.1 christos *
963 1.1 christos ******************************************************************************/
964 1.1 christos
965 1.1 christos static BOOLEAN
966 1.1 christos AcpiDmIsTargetAnOperand (
967 1.1 christos ACPI_PARSE_OBJECT *Target,
968 1.1 christos ACPI_PARSE_OBJECT *Operand,
969 1.1 christos BOOLEAN TopLevel)
970 1.1 christos {
971 1.1 christos const ACPI_OPCODE_INFO *OpInfo;
972 1.1 christos BOOLEAN Same;
973 1.1 christos
974 1.1 christos
975 1.1 christos /*
976 1.1 christos * Opcodes must match. Note: ignoring the difference between nameseg
977 1.1 christos * and namepath for now. May be needed later.
978 1.1 christos */
979 1.1 christos if (Target->Common.AmlOpcode != Operand->Common.AmlOpcode)
980 1.1 christos {
981 1.1 christos return (FALSE);
982 1.1 christos }
983 1.1 christos
984 1.1 christos /* Nodes should match, even if they are NULL */
985 1.1 christos
986 1.1 christos if (Target->Common.Node != Operand->Common.Node)
987 1.1 christos {
988 1.1 christos return (FALSE);
989 1.1 christos }
990 1.1 christos
991 1.1 christos /* Determine if a child exists */
992 1.1 christos
993 1.1 christos OpInfo = AcpiPsGetOpcodeInfo (Operand->Common.AmlOpcode);
994 1.1 christos if (OpInfo->Flags & AML_HAS_ARGS)
995 1.1 christos {
996 1.1 christos Same = AcpiDmIsTargetAnOperand (Target->Common.Value.Arg,
997 1.1 christos Operand->Common.Value.Arg, FALSE);
998 1.1 christos if (!Same)
999 1.1 christos {
1000 1.1 christos return (FALSE);
1001 1.1 christos }
1002 1.1 christos }
1003 1.1 christos
1004 1.1 christos /* Check the next peer, as long as we are not at the top level */
1005 1.1 christos
1006 1.1 christos if ((!TopLevel) &&
1007 1.1 christos Target->Common.Next)
1008 1.1 christos {
1009 1.1 christos Same = AcpiDmIsTargetAnOperand (Target->Common.Next,
1010 1.1 christos Operand->Common.Next, FALSE);
1011 1.1 christos if (!Same)
1012 1.1 christos {
1013 1.1 christos return (FALSE);
1014 1.1 christos }
1015 1.1 christos }
1016 1.1 christos
1017 1.1 christos /* Supress the duplicate operand at the top-level */
1018 1.1 christos
1019 1.1 christos if (TopLevel)
1020 1.1 christos {
1021 1.1 christos Operand->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
1022 1.1 christos }
1023 1.1 christos return (TRUE);
1024 1.1 christos }
1025