2 /******************************************************************************
4 * Module Name: aslfold - Constant folding
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
48 #include <contrib/dev/acpica/include/amlcode.h>
50 #include <contrib/dev/acpica/include/acdispat.h>
51 #include <contrib/dev/acpica/include/acparser.h>
53 #define _COMPONENT ACPI_COMPILER
54 ACPI_MODULE_NAME ("aslfold")
56 /* Local prototypes */
59 OpcAmlEvaluationWalk1 (
60 ACPI_PARSE_OBJECT *Op,
65 OpcAmlEvaluationWalk2 (
66 ACPI_PARSE_OBJECT *Op,
71 OpcAmlCheckForConstant (
72 ACPI_PARSE_OBJECT *Op,
77 OpcUpdateIntegerNode (
78 ACPI_PARSE_OBJECT *Op,
82 /*******************************************************************************
84 * FUNCTION: OpcAmlEvaluationWalk1
86 * PARAMETERS: ASL_WALK_CALLBACK
90 * DESCRIPTION: Descending callback for AML execution of constant subtrees
92 ******************************************************************************/
95 OpcAmlEvaluationWalk1 (
96 ACPI_PARSE_OBJECT *Op,
100 ACPI_WALK_STATE *WalkState = Context;
102 ACPI_PARSE_OBJECT *OutOp;
106 WalkState->Opcode = Op->Common.AmlOpcode;
107 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
109 /* Copy child pointer to Arg for compatibility with Interpreter */
113 Op->Common.Value.Arg = Op->Asl.Child;
116 /* Call AML dispatcher */
118 Status = AcpiDsExecBeginOp (WalkState, &OutOp);
119 if (ACPI_FAILURE (Status))
121 AcpiOsPrintf ("Constant interpretation failed - %s\n",
122 AcpiFormatException (Status));
129 /*******************************************************************************
131 * FUNCTION: OpcAmlEvaluationWalk2
133 * PARAMETERS: ASL_WALK_CALLBACK
137 * DESCRIPTION: Ascending callback for AML execution of constant subtrees
139 ******************************************************************************/
142 OpcAmlEvaluationWalk2 (
143 ACPI_PARSE_OBJECT *Op,
147 ACPI_WALK_STATE *WalkState = Context;
152 WalkState->Opcode = Op->Common.AmlOpcode;
153 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
155 /* Copy child pointer to Arg for compatibility with Interpreter */
159 Op->Common.Value.Arg = Op->Asl.Child;
162 /* Call AML dispatcher */
164 Status = AcpiDsExecEndOp (WalkState);
165 if (ACPI_FAILURE (Status))
167 AcpiOsPrintf ("Constant interpretation failed - %s\n",
168 AcpiFormatException (Status));
175 /*******************************************************************************
177 * FUNCTION: OpcAmlCheckForConstant
179 * PARAMETERS: ASL_WALK_CALLBACK
183 * DESCRIPTION: Check one Op for a type 3/4/5 AML opcode
185 ******************************************************************************/
188 OpcAmlCheckForConstant (
189 ACPI_PARSE_OBJECT *Op,
193 ACPI_WALK_STATE *WalkState = Context;
197 WalkState->Opcode = Op->Common.AmlOpcode;
198 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
200 DbgPrint (ASL_PARSE_OUTPUT, "[%.4d] Opcode: %12.12s ",
201 Op->Asl.LogicalLineNumber, Op->Asl.ParseOpName);
204 * These opcodes do not appear in the OpcodeInfo table, but
205 * they represent constants, so abort the constant walk now.
207 if ((WalkState->Opcode == AML_RAW_DATA_BYTE) ||
208 (WalkState->Opcode == AML_RAW_DATA_WORD) ||
209 (WalkState->Opcode == AML_RAW_DATA_DWORD) ||
210 (WalkState->Opcode == AML_RAW_DATA_QWORD))
212 WalkState->WalkType = ACPI_WALK_CONST_OPTIONAL;
216 if (!(WalkState->OpInfo->Flags & AML_CONSTANT))
218 /* The opcode is not a Type 3/4/5 opcode */
220 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
222 DbgPrint (ASL_PARSE_OUTPUT,
223 "**** Valid Target, cannot reduce ****\n");
227 DbgPrint (ASL_PARSE_OUTPUT,
228 "**** Not a Type 3/4/5 opcode ****\n");
231 if (WalkState->WalkType == ACPI_WALK_CONST_OPTIONAL)
234 * We are looking at at normal expression to see if it can be
235 * reduced. It can't. No error
241 * This is an expression that MUST reduce to a constant, and it
242 * can't be reduced. This is an error
244 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
246 AslError (ASL_ERROR, ASL_MSG_INVALID_TARGET, Op,
247 Op->Asl.ParseOpName);
251 AslError (ASL_ERROR, ASL_MSG_INVALID_CONSTANT_OP, Op,
252 Op->Asl.ParseOpName);
260 DbgPrint (ASL_PARSE_OUTPUT, "TYPE_345");
262 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
264 DbgPrint (ASL_PARSE_OUTPUT, " TARGET");
266 if (Op->Asl.CompileFlags & NODE_IS_TERM_ARG)
268 DbgPrint (ASL_PARSE_OUTPUT, " TERMARG");
271 DbgPrint (ASL_PARSE_OUTPUT, "\n");
276 /*******************************************************************************
278 * FUNCTION: OpcAmlConstantWalk
280 * PARAMETERS: ASL_WALK_CALLBACK
284 * DESCRIPTION: Reduce an Op and its subtree to a constant if possible
286 ******************************************************************************/
290 ACPI_PARSE_OBJECT *Op,
294 ACPI_WALK_STATE *WalkState;
295 ACPI_STATUS Status = AE_OK;
296 ACPI_OPERAND_OBJECT *ObjDesc;
297 ACPI_PARSE_OBJECT *RootOp;
298 ACPI_PARSE_OBJECT *OriginalParentOp;
303 * Only interested in subtrees that could possibly contain
304 * expressions that can be evaluated at this time
306 if ((!(Op->Asl.CompileFlags & NODE_COMPILE_TIME_CONST)) ||
307 (Op->Asl.CompileFlags & NODE_IS_TARGET))
312 /* Set the walk type based on the reduction used for this op */
314 if (Op->Asl.CompileFlags & NODE_IS_TERM_ARG)
316 /* Op is a TermArg, constant folding is merely optional */
318 if (!Gbl_FoldConstants)
320 return (AE_CTRL_DEPTH);
323 WalkType = ACPI_WALK_CONST_OPTIONAL;
327 /* Op is a DataObject, the expression MUST reduced to a constant */
329 WalkType = ACPI_WALK_CONST_REQUIRED;
332 /* Create a new walk state */
334 WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
340 WalkState->NextOp = NULL;
341 WalkState->Params = NULL;
342 WalkState->WalkType = WalkType;
343 WalkState->CallerReturnDesc = &ObjDesc;
346 * Examine the entire subtree -- all nodes must be constants
347 * or type 3/4/5 opcodes
349 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_DOWNWARD,
350 OpcAmlCheckForConstant, NULL, WalkState);
353 * Did we find an entire subtree that contains all constants and type 3/4/5
354 * opcodes? (Only AE_OK or AE_TYPE returned from above)
356 if (Status == AE_TYPE)
358 /* Subtree cannot be reduced to a constant */
360 if (WalkState->WalkType == ACPI_WALK_CONST_OPTIONAL)
362 AcpiDsDeleteWalkState (WalkState);
366 /* Don't descend any further, and use a default "constant" value */
368 Status = AE_CTRL_DEPTH;
372 /* Subtree can be reduced */
374 /* Allocate a new temporary root for this subtree */
376 RootOp = TrAllocateNode (PARSEOP_INTEGER);
379 return (AE_NO_MEMORY);
382 RootOp->Common.AmlOpcode = AML_INT_EVAL_SUBTREE_OP;
384 OriginalParentOp = Op->Common.Parent;
385 Op->Common.Parent = RootOp;
387 /* Hand off the subtree to the AML interpreter */
389 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_TWICE,
390 OpcAmlEvaluationWalk1, OpcAmlEvaluationWalk2, WalkState);
391 Op->Common.Parent = OriginalParentOp;
393 /* TBD: we really *should* release the RootOp node */
395 if (ACPI_SUCCESS (Status))
399 /* Get the final result */
401 Status = AcpiDsResultPop (&ObjDesc, WalkState);
404 /* Check for error from the ACPICA core */
406 if (ACPI_FAILURE (Status))
408 AslCoreSubsystemError (Op, Status,
409 "Failure during constant evaluation", FALSE);
413 if (ACPI_FAILURE (Status))
415 /* We could not resolve the subtree for some reason */
417 AslError (ASL_ERROR, ASL_MSG_CONSTANT_EVALUATION, Op,
418 Op->Asl.ParseOpName);
420 /* Set the subtree value to ZERO anyway. Eliminates further errors */
422 OpcUpdateIntegerNode (Op, 0);
426 AslError (ASL_OPTIMIZATION, ASL_MSG_CONSTANT_FOLDED, Op,
427 Op->Asl.ParseOpName);
430 * Because we know we executed type 3/4/5 opcodes above, we know that
431 * the result must be either an Integer, String, or Buffer.
433 switch (ObjDesc->Common.Type)
435 case ACPI_TYPE_INTEGER:
437 OpcUpdateIntegerNode (Op, ObjDesc->Integer.Value);
439 DbgPrint (ASL_PARSE_OUTPUT,
440 "Constant expression reduced to (%s) %8.8X%8.8X\n",
442 ACPI_FORMAT_UINT64 (Op->Common.Value.Integer));
446 case ACPI_TYPE_STRING:
448 Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
449 Op->Common.AmlOpcode = AML_STRING_OP;
450 Op->Asl.AmlLength = ACPI_STRLEN (ObjDesc->String.Pointer) + 1;
451 Op->Common.Value.String = ObjDesc->String.Pointer;
453 DbgPrint (ASL_PARSE_OUTPUT,
454 "Constant expression reduced to (STRING) %s\n",
455 Op->Common.Value.String);
460 case ACPI_TYPE_BUFFER:
462 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
463 Op->Common.AmlOpcode = AML_BUFFER_OP;
464 Op->Asl.CompileFlags = NODE_AML_PACKAGE;
465 UtSetParseOpName (Op);
467 /* Child node is the buffer length */
469 RootOp = TrAllocateNode (PARSEOP_INTEGER);
471 RootOp->Asl.AmlOpcode = AML_DWORD_OP;
472 RootOp->Asl.Value.Integer = ObjDesc->Buffer.Length;
473 RootOp->Asl.Parent = Op;
475 (void) OpcSetOptimalIntegerSize (RootOp);
477 Op->Asl.Child = RootOp;
479 UtSetParseOpName (Op);
481 /* Peer to the child is the raw buffer data */
483 RootOp = TrAllocateNode (PARSEOP_RAW_DATA);
484 RootOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
485 RootOp->Asl.AmlLength = ObjDesc->Buffer.Length;
486 RootOp->Asl.Value.String = (char *) ObjDesc->Buffer.Pointer;
487 RootOp->Asl.Parent = Op->Asl.Parent;
489 Op->Asl.Next = RootOp;
492 DbgPrint (ASL_PARSE_OUTPUT,
493 "Constant expression reduced to (BUFFER) length %X\n",
494 ObjDesc->Buffer.Length);
499 printf ("Unsupported return type: %s\n",
500 AcpiUtGetObjectTypeName (ObjDesc));
505 UtSetParseOpName (Op);
506 Op->Asl.Child = NULL;
508 AcpiDsDeleteWalkState (WalkState);
509 return (AE_CTRL_DEPTH);
513 /*******************************************************************************
515 * FUNCTION: OpcUpdateIntegerNode
517 * PARAMETERS: Op - Current parse object
521 * DESCRIPTION: Update node to the correct integer type.
523 ******************************************************************************/
526 OpcUpdateIntegerNode (
527 ACPI_PARSE_OBJECT *Op,
531 Op->Common.Value.Integer = Value;
534 * The AmlLength is used by the parser to indicate a constant,
535 * (if non-zero). Length is either (1/2/4/8)
537 switch (Op->Asl.AmlLength)
540 TrUpdateNode (PARSEOP_BYTECONST, Op);
541 Op->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
545 TrUpdateNode (PARSEOP_WORDCONST, Op);
546 Op->Asl.AmlOpcode = AML_RAW_DATA_WORD;
550 TrUpdateNode (PARSEOP_DWORDCONST, Op);
551 Op->Asl.AmlOpcode = AML_RAW_DATA_DWORD;
555 TrUpdateNode (PARSEOP_QWORDCONST, Op);
556 Op->Asl.AmlOpcode = AML_RAW_DATA_QWORD;
561 OpcSetOptimalIntegerSize (Op);
562 TrUpdateNode (PARSEOP_INTEGER, Op);
566 Op->Asl.AmlLength = 0;