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