2 /******************************************************************************
4 * Module Name: aslfold - Constant folding
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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 /*******************************************************************************
79 * FUNCTION: OpcAmlEvaluationWalk1
81 * PARAMETERS: ASL_WALK_CALLBACK
85 * DESCRIPTION: Descending callback for AML execution of constant subtrees
87 ******************************************************************************/
90 OpcAmlEvaluationWalk1 (
91 ACPI_PARSE_OBJECT *Op,
95 ACPI_WALK_STATE *WalkState = Context;
97 ACPI_PARSE_OBJECT *OutOp;
101 WalkState->Opcode = Op->Common.AmlOpcode;
102 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
104 /* Copy child pointer to Arg for compatibility with Interpreter */
108 Op->Common.Value.Arg = Op->Asl.Child;
111 /* Call AML dispatcher */
113 Status = AcpiDsExecBeginOp (WalkState, &OutOp);
114 if (ACPI_FAILURE (Status))
116 AcpiOsPrintf ("Constant interpretation failed - %s\n",
117 AcpiFormatException (Status));
124 /*******************************************************************************
126 * FUNCTION: OpcAmlEvaluationWalk2
128 * PARAMETERS: ASL_WALK_CALLBACK
132 * DESCRIPTION: Ascending callback for AML execution of constant subtrees
134 ******************************************************************************/
137 OpcAmlEvaluationWalk2 (
138 ACPI_PARSE_OBJECT *Op,
142 ACPI_WALK_STATE *WalkState = Context;
147 WalkState->Opcode = Op->Common.AmlOpcode;
148 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
150 /* Copy child pointer to Arg for compatibility with Interpreter */
154 Op->Common.Value.Arg = Op->Asl.Child;
157 /* Call AML dispatcher */
159 Status = AcpiDsExecEndOp (WalkState);
160 if (ACPI_FAILURE (Status))
162 AcpiOsPrintf ("Constant interpretation failed - %s\n",
163 AcpiFormatException (Status));
170 /*******************************************************************************
172 * FUNCTION: OpcAmlCheckForConstant
174 * PARAMETERS: ASL_WALK_CALLBACK
178 * DESCRIPTION: Check one Op for a type 3/4/5 AML opcode
180 ******************************************************************************/
183 OpcAmlCheckForConstant (
184 ACPI_PARSE_OBJECT *Op,
188 ACPI_WALK_STATE *WalkState = Context;
192 WalkState->Opcode = Op->Common.AmlOpcode;
193 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
195 DbgPrint (ASL_PARSE_OUTPUT, "[%.4d] Opcode: %12.12s ",
196 Op->Asl.LogicalLineNumber, Op->Asl.ParseOpName);
198 if (!(WalkState->OpInfo->Flags & AML_CONSTANT))
200 /* The opcode is not a Type 3/4/5 opcode */
202 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
204 DbgPrint (ASL_PARSE_OUTPUT,
205 "**** Valid Target, cannot reduce ****\n");
209 DbgPrint (ASL_PARSE_OUTPUT,
210 "**** Not a Type 3/4/5 opcode ****\n");
213 if (WalkState->WalkType == ACPI_WALK_CONST_OPTIONAL)
216 * We are looking at at normal expression to see if it can be
217 * reduced. It can't. No error
223 * This is an expression that MUST reduce to a constant, and it
224 * can't be reduced. This is an error
226 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
228 AslError (ASL_ERROR, ASL_MSG_INVALID_TARGET, Op,
229 Op->Asl.ParseOpName);
233 AslError (ASL_ERROR, ASL_MSG_INVALID_CONSTANT_OP, Op,
234 Op->Asl.ParseOpName);
242 DbgPrint (ASL_PARSE_OUTPUT, "TYPE_345");
244 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
246 DbgPrint (ASL_PARSE_OUTPUT, " TARGET");
248 if (Op->Asl.CompileFlags & NODE_IS_TERM_ARG)
250 DbgPrint (ASL_PARSE_OUTPUT, " TERMARG");
252 DbgPrint (ASL_PARSE_OUTPUT, "\n");
258 /*******************************************************************************
260 * FUNCTION: OpcAmlConstantWalk
262 * PARAMETERS: ASL_WALK_CALLBACK
266 * DESCRIPTION: Reduce an Op and its subtree to a constant if possible
268 ******************************************************************************/
272 ACPI_PARSE_OBJECT *Op,
276 ACPI_WALK_STATE *WalkState;
277 ACPI_STATUS Status = AE_OK;
278 ACPI_OPERAND_OBJECT *ObjDesc;
279 ACPI_PARSE_OBJECT *RootOp;
280 ACPI_PARSE_OBJECT *OriginalParentOp;
285 * Only interested in subtrees that could possibly contain
286 * expressions that can be evaluated at this time
288 if ((!(Op->Asl.CompileFlags & NODE_COMPILE_TIME_CONST)) ||
289 (Op->Asl.CompileFlags & NODE_IS_TARGET))
294 /* Set the walk type based on the reduction used for this op */
296 if (Op->Asl.CompileFlags & NODE_IS_TERM_ARG)
298 /* Op is a TermArg, constant folding is merely optional */
300 if (!Gbl_FoldConstants)
302 return (AE_CTRL_DEPTH);
305 WalkType = ACPI_WALK_CONST_OPTIONAL;
309 /* Op is a DataObject, the expression MUST reduced to a constant */
311 WalkType = ACPI_WALK_CONST_REQUIRED;
314 /* Create a new walk state */
316 WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
322 WalkState->NextOp = NULL;
323 WalkState->Params = NULL;
324 WalkState->CallerReturnDesc = &ObjDesc;
325 WalkState->WalkType = WalkType;
328 * Examine the entire subtree -- all nodes must be constants
329 * or type 3/4/5 opcodes
331 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_DOWNWARD,
332 OpcAmlCheckForConstant, NULL, WalkState);
335 * Did we find an entire subtree that contains all constants and type 3/4/5
336 * opcodes? (Only AE_OK or AE_TYPE returned from above)
338 if (Status == AE_TYPE)
340 /* Subtree cannot be reduced to a constant */
342 if (WalkState->WalkType == ACPI_WALK_CONST_OPTIONAL)
344 AcpiDsDeleteWalkState (WalkState);
348 /* Don't descend any further, and use a default "constant" value */
350 Status = AE_CTRL_DEPTH;
354 /* Subtree can be reduced */
356 /* Allocate a new temporary root for this subtree */
358 RootOp = TrAllocateNode (PARSEOP_INTEGER);
361 return (AE_NO_MEMORY);
364 RootOp->Common.AmlOpcode = AML_INT_EVAL_SUBTREE_OP;
366 OriginalParentOp = Op->Common.Parent;
367 Op->Common.Parent = RootOp;
369 /* Hand off the subtree to the AML interpreter */
371 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_TWICE,
372 OpcAmlEvaluationWalk1, OpcAmlEvaluationWalk2, WalkState);
373 Op->Common.Parent = OriginalParentOp;
375 /* TBD: we really *should* release the RootOp node */
377 if (ACPI_SUCCESS (Status))
381 /* Get the final result */
383 Status = AcpiDsResultPop (&ObjDesc, WalkState);
387 if (ACPI_FAILURE (Status))
389 /* We could not resolve the subtree for some reason */
391 AslCoreSubsystemError (Op, Status,
392 "Failure during constant evaluation", FALSE);
393 AslError (ASL_ERROR, ASL_MSG_CONSTANT_EVALUATION, Op,
394 Op->Asl.ParseOpName);
396 /* Set the subtree value to ZERO anyway. Eliminates further errors */
398 Op->Asl.ParseOpcode = PARSEOP_INTEGER;
399 Op->Common.Value.Integer = 0;
400 OpcSetOptimalIntegerSize (Op);
404 AslError (ASL_OPTIMIZATION, ASL_MSG_CONSTANT_FOLDED, Op,
405 Op->Asl.ParseOpName);
408 * Because we know we executed type 3/4/5 opcodes above, we know that
409 * the result must be either an Integer, String, or Buffer.
411 switch (ObjDesc->Common.Type)
413 case ACPI_TYPE_INTEGER:
415 Op->Asl.ParseOpcode = PARSEOP_INTEGER;
416 Op->Common.Value.Integer = ObjDesc->Integer.Value;
417 OpcSetOptimalIntegerSize (Op);
419 DbgPrint (ASL_PARSE_OUTPUT,
420 "Constant expression reduced to (INTEGER) %8.8X%8.8X\n",
421 ACPI_FORMAT_UINT64 (ObjDesc->Integer.Value));
425 case ACPI_TYPE_STRING:
427 Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
428 Op->Common.AmlOpcode = AML_STRING_OP;
429 Op->Asl.AmlLength = ACPI_STRLEN (ObjDesc->String.Pointer) + 1;
430 Op->Common.Value.String = ObjDesc->String.Pointer;
432 DbgPrint (ASL_PARSE_OUTPUT,
433 "Constant expression reduced to (STRING) %s\n",
434 Op->Common.Value.String);
439 case ACPI_TYPE_BUFFER:
441 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
442 Op->Common.AmlOpcode = AML_BUFFER_OP;
443 Op->Asl.CompileFlags = NODE_AML_PACKAGE;
444 UtSetParseOpName (Op);
446 /* Child node is the buffer length */
448 RootOp = TrAllocateNode (PARSEOP_INTEGER);
450 RootOp->Asl.AmlOpcode = AML_DWORD_OP;
451 RootOp->Asl.Value.Integer = ObjDesc->Buffer.Length;
452 RootOp->Asl.Parent = Op;
454 (void) OpcSetOptimalIntegerSize (RootOp);
456 Op->Asl.Child = RootOp;
458 UtSetParseOpName (Op);
460 /* Peer to the child is the raw buffer data */
462 RootOp = TrAllocateNode (PARSEOP_RAW_DATA);
463 RootOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
464 RootOp->Asl.AmlLength = ObjDesc->Buffer.Length;
465 RootOp->Asl.Value.String = (char *) ObjDesc->Buffer.Pointer;
466 RootOp->Asl.Parent = Op->Asl.Parent;
468 Op->Asl.Next = RootOp;
471 DbgPrint (ASL_PARSE_OUTPUT,
472 "Constant expression reduced to (BUFFER) length %X\n",
473 ObjDesc->Buffer.Length);
478 printf ("Unsupported return type: %s\n",
479 AcpiUtGetObjectTypeName (ObjDesc));
484 UtSetParseOpName (Op);
485 Op->Asl.Child = NULL;
487 AcpiDsDeleteWalkState (WalkState);
489 return (AE_CTRL_DEPTH);