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