2 /******************************************************************************
4 * Module Name: aslfold - Constant folding
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #include "aslcompiler.h"
119 #include "aslcompiler.y.h"
122 #include "acdispat.h"
123 #include "acparser.h"
125 #define _COMPONENT ACPI_COMPILER
126 ACPI_MODULE_NAME ("aslfold")
128 /* Local prototypes */
131 OpcAmlEvaluationWalk1 (
132 ACPI_PARSE_OBJECT
*Op
,
137 OpcAmlEvaluationWalk2 (
138 ACPI_PARSE_OBJECT
*Op
,
143 OpcAmlCheckForConstant (
144 ACPI_PARSE_OBJECT
*Op
,
149 /*******************************************************************************
151 * FUNCTION: OpcAmlEvaluationWalk1
153 * PARAMETERS: ASL_WALK_CALLBACK
157 * DESCRIPTION: Descending callback for AML execution of constant subtrees
159 ******************************************************************************/
162 OpcAmlEvaluationWalk1 (
163 ACPI_PARSE_OBJECT
*Op
,
167 ACPI_WALK_STATE
*WalkState
= Context
;
169 ACPI_PARSE_OBJECT
*OutOp
;
173 WalkState
->Opcode
= Op
->Common
.AmlOpcode
;
174 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.AmlOpcode
);
176 /* Copy child pointer to Arg for compatibility with Interpreter */
180 Op
->Common
.Value
.Arg
= Op
->Asl
.Child
;
183 /* Call AML dispatcher */
185 Status
= AcpiDsExecBeginOp (WalkState
, &OutOp
);
186 if (ACPI_FAILURE (Status
))
188 AcpiOsPrintf ("Constant interpretation failed - %s\n",
189 AcpiFormatException (Status
));
196 /*******************************************************************************
198 * FUNCTION: OpcAmlEvaluationWalk2
200 * PARAMETERS: ASL_WALK_CALLBACK
204 * DESCRIPTION: Ascending callback for AML execution of constant subtrees
206 ******************************************************************************/
209 OpcAmlEvaluationWalk2 (
210 ACPI_PARSE_OBJECT
*Op
,
214 ACPI_WALK_STATE
*WalkState
= Context
;
219 WalkState
->Opcode
= Op
->Common
.AmlOpcode
;
220 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.AmlOpcode
);
222 /* Copy child pointer to Arg for compatibility with Interpreter */
226 Op
->Common
.Value
.Arg
= Op
->Asl
.Child
;
229 /* Call AML dispatcher */
231 Status
= AcpiDsExecEndOp (WalkState
);
232 if (ACPI_FAILURE (Status
))
234 AcpiOsPrintf ("Constant interpretation failed - %s\n",
235 AcpiFormatException (Status
));
242 /*******************************************************************************
244 * FUNCTION: OpcAmlCheckForConstant
246 * PARAMETERS: ASL_WALK_CALLBACK
250 * DESCRIPTION: Check one Op for a type 3/4/5 AML opcode
252 ******************************************************************************/
255 OpcAmlCheckForConstant (
256 ACPI_PARSE_OBJECT
*Op
,
260 ACPI_WALK_STATE
*WalkState
= Context
;
264 WalkState
->Opcode
= Op
->Common
.AmlOpcode
;
265 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.AmlOpcode
);
267 DbgPrint (ASL_PARSE_OUTPUT
, "[%.4d] Opcode: %12.12s ",
268 Op
->Asl
.LogicalLineNumber
, Op
->Asl
.ParseOpName
);
270 if (!(WalkState
->OpInfo
->Flags
& AML_CONSTANT
))
272 /* The opcode is not a Type 3/4/5 opcode */
274 if (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
)
276 DbgPrint (ASL_PARSE_OUTPUT
,
277 "**** Valid Target, cannot reduce ****\n");
281 DbgPrint (ASL_PARSE_OUTPUT
,
282 "**** Not a Type 3/4/5 opcode ****\n");
285 if (WalkState
->WalkType
== ACPI_WALK_CONST_OPTIONAL
)
288 * We are looking at at normal expression to see if it can be
289 * reduced. It can't. No error
295 * This is an expression that MUST reduce to a constant, and it
296 * can't be reduced. This is an error
298 if (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
)
300 AslError (ASL_ERROR
, ASL_MSG_INVALID_TARGET
, Op
,
301 Op
->Asl
.ParseOpName
);
305 AslError (ASL_ERROR
, ASL_MSG_INVALID_CONSTANT_OP
, Op
,
306 Op
->Asl
.ParseOpName
);
314 DbgPrint (ASL_PARSE_OUTPUT
, "TYPE_345");
316 if (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
)
318 DbgPrint (ASL_PARSE_OUTPUT
, " TARGET");
320 if (Op
->Asl
.CompileFlags
& NODE_IS_TERM_ARG
)
322 DbgPrint (ASL_PARSE_OUTPUT
, " TERMARG");
324 DbgPrint (ASL_PARSE_OUTPUT
, "\n");
330 /*******************************************************************************
332 * FUNCTION: OpcAmlConstantWalk
334 * PARAMETERS: ASL_WALK_CALLBACK
338 * DESCRIPTION: Reduce an Op and its subtree to a constant if possible
340 ******************************************************************************/
344 ACPI_PARSE_OBJECT
*Op
,
348 ACPI_WALK_STATE
*WalkState
;
349 ACPI_STATUS Status
= AE_OK
;
350 ACPI_OPERAND_OBJECT
*ObjDesc
;
351 ACPI_PARSE_OBJECT
*RootOp
;
352 ACPI_PARSE_OBJECT
*OriginalParentOp
;
357 * Only interested in subtrees that could possibly contain
358 * expressions that can be evaluated at this time
360 if ((!(Op
->Asl
.CompileFlags
& NODE_COMPILE_TIME_CONST
)) ||
361 (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
))
366 /* Set the walk type based on the reduction used for this op */
368 if (Op
->Asl
.CompileFlags
& NODE_IS_TERM_ARG
)
370 /* Op is a TermArg, constant folding is merely optional */
372 if (!Gbl_FoldConstants
)
374 return (AE_CTRL_DEPTH
);
377 WalkType
= ACPI_WALK_CONST_OPTIONAL
;
381 /* Op is a DataObject, the expression MUST reduced to a constant */
383 WalkType
= ACPI_WALK_CONST_REQUIRED
;
386 /* Create a new walk state */
388 WalkState
= AcpiDsCreateWalkState (0, NULL
, NULL
, NULL
);
394 WalkState
->NextOp
= NULL
;
395 WalkState
->Params
= NULL
;
396 WalkState
->CallerReturnDesc
= &ObjDesc
;
397 WalkState
->WalkType
= WalkType
;
400 * Examine the entire subtree -- all nodes must be constants
401 * or type 3/4/5 opcodes
403 Status
= TrWalkParseTree (Op
, ASL_WALK_VISIT_DOWNWARD
,
404 OpcAmlCheckForConstant
, NULL
, WalkState
);
407 * Did we find an entire subtree that contains all constants and type 3/4/5
408 * opcodes? (Only AE_OK or AE_TYPE returned from above)
410 if (Status
== AE_TYPE
)
412 /* Subtree cannot be reduced to a constant */
414 if (WalkState
->WalkType
== ACPI_WALK_CONST_OPTIONAL
)
416 AcpiDsDeleteWalkState (WalkState
);
420 /* Don't descend any further, and use a default "constant" value */
422 Status
= AE_CTRL_DEPTH
;
426 /* Subtree can be reduced */
428 /* Allocate a new temporary root for this subtree */
430 RootOp
= TrAllocateNode (PARSEOP_INTEGER
);
433 return (AE_NO_MEMORY
);
436 RootOp
->Common
.AmlOpcode
= AML_INT_EVAL_SUBTREE_OP
;
438 OriginalParentOp
= Op
->Common
.Parent
;
439 Op
->Common
.Parent
= RootOp
;
441 /* Hand off the subtree to the AML interpreter */
443 Status
= TrWalkParseTree (Op
, ASL_WALK_VISIT_TWICE
,
444 OpcAmlEvaluationWalk1
, OpcAmlEvaluationWalk2
, WalkState
);
445 Op
->Common
.Parent
= OriginalParentOp
;
447 /* TBD: we really *should* release the RootOp node */
449 if (ACPI_SUCCESS (Status
))
453 /* Get the final result */
455 Status
= AcpiDsResultPop (&ObjDesc
, WalkState
);
459 if (ACPI_FAILURE (Status
))
461 /* We could not resolve the subtree for some reason */
463 AslCoreSubsystemError (Op
, Status
,
464 "Failure during constant evaluation", FALSE
);
465 AslError (ASL_ERROR
, ASL_MSG_CONSTANT_EVALUATION
, Op
,
466 Op
->Asl
.ParseOpName
);
468 /* Set the subtree value to ZERO anyway. Eliminates further errors */
470 Op
->Asl
.ParseOpcode
= PARSEOP_INTEGER
;
471 Op
->Common
.Value
.Integer
= 0;
472 OpcSetOptimalIntegerSize (Op
);
476 AslError (ASL_OPTIMIZATION
, ASL_MSG_CONSTANT_FOLDED
, Op
,
477 Op
->Asl
.ParseOpName
);
480 * Because we know we executed type 3/4/5 opcodes above, we know that
481 * the result must be either an Integer, String, or Buffer.
483 switch (ObjDesc
->Common
.Type
)
485 case ACPI_TYPE_INTEGER
:
487 Op
->Asl
.ParseOpcode
= PARSEOP_INTEGER
;
488 Op
->Common
.Value
.Integer
= ObjDesc
->Integer
.Value
;
489 OpcSetOptimalIntegerSize (Op
);
491 DbgPrint (ASL_PARSE_OUTPUT
,
492 "Constant expression reduced to (INTEGER) %8.8X%8.8X\n",
493 ACPI_FORMAT_UINT64 (ObjDesc
->Integer
.Value
));
497 case ACPI_TYPE_STRING
:
499 Op
->Asl
.ParseOpcode
= PARSEOP_STRING_LITERAL
;
500 Op
->Common
.AmlOpcode
= AML_STRING_OP
;
501 Op
->Asl
.AmlLength
= ACPI_STRLEN (ObjDesc
->String
.Pointer
) + 1;
502 Op
->Common
.Value
.String
= ObjDesc
->String
.Pointer
;
504 DbgPrint (ASL_PARSE_OUTPUT
,
505 "Constant expression reduced to (STRING) %s\n",
506 Op
->Common
.Value
.String
);
511 case ACPI_TYPE_BUFFER
:
513 Op
->Asl
.ParseOpcode
= PARSEOP_BUFFER
;
514 Op
->Common
.AmlOpcode
= AML_BUFFER_OP
;
515 Op
->Asl
.CompileFlags
= NODE_AML_PACKAGE
;
516 UtSetParseOpName (Op
);
518 /* Child node is the buffer length */
520 RootOp
= TrAllocateNode (PARSEOP_INTEGER
);
522 RootOp
->Asl
.AmlOpcode
= AML_DWORD_OP
;
523 RootOp
->Asl
.Value
.Integer
= ObjDesc
->Buffer
.Length
;
524 RootOp
->Asl
.Parent
= Op
;
526 (void) OpcSetOptimalIntegerSize (RootOp
);
528 Op
->Asl
.Child
= RootOp
;
530 UtSetParseOpName (Op
);
532 /* Peer to the child is the raw buffer data */
534 RootOp
= TrAllocateNode (PARSEOP_RAW_DATA
);
535 RootOp
->Asl
.AmlOpcode
= AML_RAW_DATA_BUFFER
;
536 RootOp
->Asl
.AmlLength
= ObjDesc
->Buffer
.Length
;
537 RootOp
->Asl
.Value
.String
= (char *) ObjDesc
->Buffer
.Pointer
;
538 RootOp
->Asl
.Parent
= Op
->Asl
.Parent
;
540 Op
->Asl
.Next
= RootOp
;
543 DbgPrint (ASL_PARSE_OUTPUT
,
544 "Constant expression reduced to (BUFFER) length %X\n",
545 ObjDesc
->Buffer
.Length
);
550 printf ("Unsupported return type: %s\n",
551 AcpiUtGetObjectTypeName (ObjDesc
));
556 UtSetParseOpName (Op
);
557 Op
->Asl
.Child
= NULL
;
559 AcpiDsDeleteWalkState (WalkState
);
561 return (AE_CTRL_DEPTH
);