1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2003, 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 *****************************************************************************/
119 * Parse the AML and build an operation tree as most interpreters,
120 * like Perl, do. Parsing is done by hand rather than with a YACC
121 * generated parser to tightly constrain stack and dynamic memory
122 * usage. At the same time, parsing is kept flexible and the code
123 * fairly compact by parsing based on a list of AML opcode
124 * templates in AmlOpInfo[]
128 #include "acparser.h"
129 #include "acdispat.h"
131 #include "acnamesp.h"
132 #include "acinterp.h"
134 #define _COMPONENT ACPI_PARSER
135 ACPI_MODULE_NAME ("psparse")
138 static UINT32 AcpiGbl_Depth = 0;
141 /*******************************************************************************
143 * FUNCTION: AcpiPsGetOpcodeSize
145 * PARAMETERS: Opcode - An AML opcode
147 * RETURN: Size of the opcode, in bytes (1 or 2)
149 * DESCRIPTION: Get the size of the current opcode.
151 ******************************************************************************/
154 AcpiPsGetOpcodeSize (
158 /* Extended (2-byte) opcode if > 255 */
165 /* Otherwise, just a single byte opcode */
171 /*******************************************************************************
173 * FUNCTION: AcpiPsPeekOpcode
175 * PARAMETERS: ParserState - A parser state object
179 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
181 ******************************************************************************/
185 ACPI_PARSE_STATE *ParserState)
191 Aml = ParserState->Aml;
192 Opcode = (UINT16) ACPI_GET8 (Aml);
195 if (Opcode == AML_EXTOP)
197 /* Extended opcode */
200 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
207 /*******************************************************************************
209 * FUNCTION: AcpiPsCompleteThisOp
211 * PARAMETERS: WalkState - Current State
212 * Op - Op to complete
216 * DESCRIPTION: Perform any cleanup at the completion of an Op.
218 ******************************************************************************/
221 AcpiPsCompleteThisOp (
222 ACPI_WALK_STATE *WalkState,
223 ACPI_PARSE_OBJECT *Op)
225 ACPI_PARSE_OBJECT *Prev;
226 ACPI_PARSE_OBJECT *Next;
227 const ACPI_OPCODE_INFO *ParentInfo;
228 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
231 ACPI_FUNCTION_TRACE_PTR ("PsCompleteThisOp", Op);
234 /* Check for null Op, can happen if AML code is corrupt */
241 /* Delete this op and the subtree below it if asked to */
243 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) == ACPI_PARSE_DELETE_TREE) &&
244 (WalkState->OpInfo->Class != AML_CLASS_ARGUMENT))
246 /* Make sure that we only delete this subtree */
248 if (Op->Common.Parent)
251 * Check if we need to replace the operator and its subtree
252 * with a return value op (placeholder op)
254 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
256 switch (ParentInfo->Class)
258 case AML_CLASS_CONTROL:
261 case AML_CLASS_CREATE:
264 * These opcodes contain TermArg operands. The current
265 * op must be replaced by a placeholder return op
267 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
274 case AML_CLASS_NAMED_OBJECT:
277 * These opcodes contain TermArg operands. The current
278 * op must be replaced by a placeholder return op
280 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
281 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
282 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
283 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
284 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
286 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
293 if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
294 (WalkState->DescendingCallback != AcpiDsExecBeginOp))
297 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
298 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
299 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
301 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
307 ReplacementOp->Named.Data = Op->Named.Data;
308 ReplacementOp->Named.Length = Op->Named.Length;
314 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
321 /* We must unlink this op from the parent tree */
323 Prev = Op->Common.Parent->Common.Value.Arg;
326 /* This op is the first in the list */
330 ReplacementOp->Common.Parent = Op->Common.Parent;
331 ReplacementOp->Common.Value.Arg = NULL;
332 ReplacementOp->Common.Node = Op->Common.Node;
333 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
334 ReplacementOp->Common.Next = Op->Common.Next;
338 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
342 /* Search the parent list */
346 /* Traverse all siblings in the parent's argument list */
348 Next = Prev->Common.Next;
353 ReplacementOp->Common.Parent = Op->Common.Parent;
354 ReplacementOp->Common.Value.Arg = NULL;
355 ReplacementOp->Common.Node = Op->Common.Node;
356 Prev->Common.Next = ReplacementOp;
357 ReplacementOp->Common.Next = Op->Common.Next;
362 Prev->Common.Next = Op->Common.Next;
371 /* Now we can actually delete the subtree rooted at op */
373 AcpiPsDeleteParseTree (Op);
382 /*******************************************************************************
384 * FUNCTION: AcpiPsNextParseState
386 * PARAMETERS: ParserState - Current parser state object
390 * DESCRIPTION: Update the parser state based upon the return exception from
391 * the parser callback.
393 ******************************************************************************/
396 AcpiPsNextParseState (
397 ACPI_WALK_STATE *WalkState,
398 ACPI_PARSE_OBJECT *Op,
399 ACPI_STATUS CallbackStatus)
401 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
402 ACPI_STATUS Status = AE_CTRL_PENDING;
405 ACPI_FUNCTION_TRACE_PTR ("PsNextParseState", Op);
408 switch (CallbackStatus)
410 case AE_CTRL_TERMINATE:
413 * A control method was terminated via a RETURN statement.
414 * The walk of this method is complete.
416 ParserState->Aml = ParserState->AmlEnd;
417 Status = AE_CTRL_TERMINATE;
423 ParserState->Aml = WalkState->AmlLastWhile;
424 WalkState->ControlState->Common.Value = FALSE;
425 Status = AE_CTRL_BREAK;
428 case AE_CTRL_CONTINUE:
431 ParserState->Aml = WalkState->AmlLastWhile;
432 Status = AE_CTRL_CONTINUE;
435 case AE_CTRL_PENDING:
437 ParserState->Aml = WalkState->AmlLastWhile;
443 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
451 * Predicate of an IF was true, and we are at the matching ELSE.
452 * Just close out this package
454 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
461 * Either an IF/WHILE Predicate was false or we encountered a BREAK
462 * opcode. In both cases, we do not execute the rest of the
463 * package; We simply close out the parent (finishing the walk of
464 * this branch of the tree) and continue execution at the parent
467 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
469 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
471 WalkState->ControlState->Common.Value = FALSE;
472 Status = AE_CTRL_END;
476 case AE_CTRL_TRANSFER:
479 * A method call (invocation) -- transfer control
481 Status = AE_CTRL_TRANSFER;
482 WalkState->PrevOp = Op;
483 WalkState->MethodCallOp = Op;
484 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
486 /* Will return value (if any) be used by the caller? */
488 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
493 Status = CallbackStatus;
494 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
501 return_ACPI_STATUS (Status);
505 /*******************************************************************************
507 * FUNCTION: AcpiPsParseLoop
509 * PARAMETERS: ParserState - Current parser state object
513 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
516 ******************************************************************************/
520 ACPI_WALK_STATE *WalkState)
522 ACPI_STATUS Status = AE_OK;
523 ACPI_PARSE_OBJECT *Op = NULL; /* current op */
524 ACPI_PARSE_OBJECT *Arg = NULL;
525 ACPI_PARSE_OBJECT PreOp;
526 ACPI_PARSE_STATE *ParserState;
527 UINT8 *AmlOpStart = NULL;
530 ACPI_FUNCTION_TRACE_PTR ("PsParseLoop", WalkState);
532 if (WalkState->DescendingCallback == NULL)
534 return_ACPI_STATUS (AE_BAD_PARAMETER);
537 ParserState = &WalkState->ParserState;
538 WalkState->ArgTypes = 0;
540 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
541 if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
543 /* We are restarting a preempted control method */
545 if (AcpiPsHasCompletedScope (ParserState))
548 * We must check if a predicate to an IF or WHILE statement
551 if ((ParserState->Scope->ParseScope.Op) &&
552 ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
553 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
554 (WalkState->ControlState) &&
555 (WalkState->ControlState->Common.State ==
556 ACPI_CONTROL_PREDICATE_EXECUTING))
559 * A predicate was just completed, get the value of the
560 * predicate and branch based on that value
562 WalkState->Op = NULL;
563 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
564 if (ACPI_FAILURE (Status) &&
565 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
567 if (Status == AE_AML_NO_RETURN_VALUE)
569 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
570 "Invoked method did not return a value, %s\n",
571 AcpiFormatException (Status)));
574 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "GetPredicate Failed, %s\n",
575 AcpiFormatException (Status)));
576 return_ACPI_STATUS (Status);
579 Status = AcpiPsNextParseState (WalkState, Op, Status);
582 AcpiPsPopScope (ParserState, &Op,
583 &WalkState->ArgTypes, &WalkState->ArgCount);
584 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
586 else if (WalkState->PrevOp)
588 /* We were in the middle of an op */
590 Op = WalkState->PrevOp;
591 WalkState->ArgTypes = WalkState->PrevArgTypes;
597 * Iterative parsing loop, while there is more aml to process:
599 while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
601 AmlOpStart = ParserState->Aml;
604 /* Get the next opcode from the AML stream */
606 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (ParserState->Aml,
607 ParserState->AmlStart);
608 WalkState->Opcode = AcpiPsPeekOpcode (ParserState);
611 * First cut to determine what we have found:
612 * 1) A valid AML opcode
614 * 3) An unknown/invalid opcode
616 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
617 switch (WalkState->OpInfo->Class)
619 case AML_CLASS_ASCII:
620 case AML_CLASS_PREFIX:
622 * Starts with a valid prefix or ASCII char, this is a name
623 * string. Convert the bare name string to a namepath.
625 WalkState->Opcode = AML_INT_NAMEPATH_OP;
626 WalkState->ArgTypes = ARGP_NAMESTRING;
629 case AML_CLASS_UNKNOWN:
631 /* The opcode is unrecognized. Just skip unknown opcodes */
633 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
634 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
635 WalkState->Opcode, ParserState->Aml, WalkState->AmlOffset));
637 ACPI_DUMP_BUFFER (ParserState->Aml, 128);
639 /* Assume one-byte bad opcode */
646 /* Found opcode info, this is a normal opcode */
648 ParserState->Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
649 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
653 /* Create Op structure and append to parent's argument list */
655 if (WalkState->OpInfo->Flags & AML_NAMED)
657 PreOp.Common.Value.Arg = NULL;
658 PreOp.Common.AmlOpcode = WalkState->Opcode;
661 * Get and append arguments until we find the node that contains
662 * the name (the type ARGP_NAME).
664 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
665 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
667 Status = AcpiPsGetNextArg (WalkState, ParserState,
668 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
669 if (ACPI_FAILURE (Status))
674 AcpiPsAppendArg (&PreOp, Arg);
675 INCREMENT_ARG_LIST (WalkState->ArgTypes);
678 /* Make sure that we found a NAME and didn't run out of arguments */
680 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
682 return_ACPI_STATUS (AE_AML_NO_OPERAND);
685 /* We know that this arg is a name, move to next arg */
687 INCREMENT_ARG_LIST (WalkState->ArgTypes);
690 * Find the object. This will either insert the object into
691 * the namespace or simply look it up
693 WalkState->Op = NULL;
695 Status = WalkState->DescendingCallback (WalkState, &Op);
696 if (ACPI_FAILURE (Status))
698 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "During name lookup/catalog, %s\n",
699 AcpiFormatException (Status)));
708 Status = AcpiPsNextParseState (WalkState, Op, Status);
709 if (Status == AE_CTRL_PENDING)
715 if (ACPI_FAILURE (Status))
720 AcpiPsAppendArg (Op, PreOp.Common.Value.Arg);
723 if (Op->Common.AmlOpcode == AML_REGION_OP)
726 * Defer final parsing of an OperationRegion body,
727 * because we don't have enough info in the first pass
728 * to parse it correctly (i.e., there may be method
729 * calls within the TermArg elements of the body.)
731 * However, we must continue parsing because
732 * the opregion is not a standalone package --
733 * we don't know where the end is at this point.
735 * (Length is unknown until parse of the body complete)
737 Op->Named.Data = AmlOpStart;
738 Op->Named.Length = 0;
743 /* Not a named opcode, just allocate Op and append to parent */
745 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
746 Op = AcpiPsAllocOp (WalkState->Opcode);
749 return_ACPI_STATUS (AE_NO_MEMORY);
752 if (WalkState->OpInfo->Flags & AML_CREATE)
755 * Backup to beginning of CreateXXXfield declaration
756 * BodyLength is unknown until we parse the body
758 Op->Named.Data = AmlOpStart;
759 Op->Named.Length = 0;
762 AcpiPsAppendArg (AcpiPsGetParentScope (ParserState), Op);
764 if ((WalkState->DescendingCallback != NULL))
767 * Find the object. This will either insert the object into
768 * the namespace or simply look it up
772 Status = WalkState->DescendingCallback (WalkState, &Op);
773 Status = AcpiPsNextParseState (WalkState, Op, Status);
774 if (Status == AE_CTRL_PENDING)
780 if (ACPI_FAILURE (Status))
787 Op->Common.AmlOffset = WalkState->AmlOffset;
789 if (WalkState->OpInfo)
791 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
792 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
793 (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
794 Op, ParserState->Aml, Op->Common.AmlOffset));
799 /* Start ArgCount at zero because we don't know if there are any args yet */
801 WalkState->ArgCount = 0;
803 if (WalkState->ArgTypes) /* Are there any arguments that must be processed? */
807 switch (Op->Common.AmlOpcode)
809 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
810 case AML_WORD_OP: /* AML_WORDDATA_ARG */
811 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
812 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
813 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
815 /* Fill in constant or string argument directly */
817 AcpiPsGetNextSimpleArg (ParserState,
818 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
821 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
823 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Op, 1);
824 if (ACPI_FAILURE (Status))
829 WalkState->ArgTypes = 0;
834 /* Op is not a constant or string, append each argument to the Op */
836 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
837 !WalkState->ArgCount)
839 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (ParserState->Aml,
840 ParserState->AmlStart);
841 Status = AcpiPsGetNextArg (WalkState, ParserState,
842 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
843 if (ACPI_FAILURE (Status))
850 Arg->Common.AmlOffset = WalkState->AmlOffset;
851 AcpiPsAppendArg (Op, Arg);
853 INCREMENT_ARG_LIST (WalkState->ArgTypes);
856 /* Special processing for certain opcodes */
858 switch (Op->Common.AmlOpcode)
863 * Skip parsing of control method
864 * because we don't have enough info in the first pass
865 * to parse it correctly.
867 * Save the length and address of the body
869 Op->Named.Data = ParserState->Aml;
870 Op->Named.Length = (UINT32) (ParserState->PkgEnd - ParserState->Aml);
872 /* Skip body of method */
874 ParserState->Aml = ParserState->PkgEnd;
875 WalkState->ArgCount = 0;
880 case AML_VAR_PACKAGE_OP:
882 if ((Op->Common.Parent) &&
883 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
884 (WalkState->DescendingCallback != AcpiDsExecBeginOp))
887 * Skip parsing of Buffers and Packages
888 * because we don't have enough info in the first pass
889 * to parse them correctly.
891 Op->Named.Data = AmlOpStart;
892 Op->Named.Length = (UINT32) (ParserState->PkgEnd - AmlOpStart);
896 ParserState->Aml = ParserState->PkgEnd;
897 WalkState->ArgCount = 0;
903 if (WalkState->ControlState)
905 WalkState->ControlState->Control.PackageEnd = ParserState->PkgEnd;
911 /* No action for all other opcodes */
918 /* Check for arguments that need to be processed */
920 if (WalkState->ArgCount)
922 /* There are arguments (complex ones), push Op and prepare for argument */
924 Status = AcpiPsPushScope (ParserState, Op,
925 WalkState->ArgTypes, WalkState->ArgCount);
926 if (ACPI_FAILURE (Status))
928 return_ACPI_STATUS (Status);
934 /* All arguments have been processed -- Op is complete, prepare for next */
936 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
937 if (WalkState->OpInfo->Flags & AML_NAMED)
944 if (Op->Common.AmlOpcode == AML_REGION_OP)
947 * Skip parsing of control method or opregion body,
948 * because we don't have enough info in the first pass
949 * to parse them correctly.
951 * Completed parsing an OpRegion declaration, we now
954 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
958 if (WalkState->OpInfo->Flags & AML_CREATE)
961 * Backup to beginning of CreateXXXfield declaration (1 for
964 * BodyLength is unknown until we parse the body
966 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
969 /* This op complete, notify the dispatcher */
971 if (WalkState->AscendingCallback != NULL)
974 WalkState->Opcode = Op->Common.AmlOpcode;
976 Status = WalkState->AscendingCallback (WalkState);
977 Status = AcpiPsNextParseState (WalkState, Op, Status);
978 if (Status == AE_CTRL_PENDING)
988 * Finished one argument of the containing scope
990 ParserState->Scope->ParseScope.ArgCount--;
992 /* Close this Op (will result in parse subtree deletion) */
994 AcpiPsCompleteThisOp (WalkState, Op);
1003 case AE_CTRL_TRANSFER:
1006 * We are about to transfer to a called method.
1008 WalkState->PrevOp = Op;
1009 WalkState->PrevArgTypes = WalkState->ArgTypes;
1010 return_ACPI_STATUS (Status);
1015 AcpiPsPopScope (ParserState, &Op,
1016 &WalkState->ArgTypes, &WalkState->ArgCount);
1021 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1022 WalkState->Opcode = Op->Common.AmlOpcode;
1024 Status = WalkState->AscendingCallback (WalkState);
1025 Status = AcpiPsNextParseState (WalkState, Op, Status);
1027 AcpiPsCompleteThisOp (WalkState, Op);
1035 case AE_CTRL_CONTINUE:
1037 /* Pop off scopes until we find the While */
1039 while (!Op || (Op->Common.AmlOpcode != AML_WHILE_OP))
1041 AcpiPsPopScope (ParserState, &Op,
1042 &WalkState->ArgTypes, &WalkState->ArgCount);
1045 /* Close this iteration of the While loop */
1048 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1049 WalkState->Opcode = Op->Common.AmlOpcode;
1051 Status = WalkState->AscendingCallback (WalkState);
1052 Status = AcpiPsNextParseState (WalkState, Op, Status);
1054 AcpiPsCompleteThisOp (WalkState, Op);
1061 case AE_CTRL_TERMINATE:
1070 AcpiPsCompleteThisOp (WalkState, Op);
1072 AcpiPsPopScope (ParserState, &Op,
1073 &WalkState->ArgTypes, &WalkState->ArgCount);
1077 return_ACPI_STATUS (Status);
1080 default: /* All other non-AE_OK status */
1086 AcpiPsCompleteThisOp (WalkState, Op);
1088 AcpiPsPopScope (ParserState, &Op,
1089 &WalkState->ArgTypes, &WalkState->ArgCount);
1095 * TBD: Cleanup parse ops on error
1100 AcpiPsPopScope (ParserState, &Op,
1101 &WalkState->ArgTypes, &WalkState->ArgCount);
1104 WalkState->PrevOp = Op;
1105 WalkState->PrevArgTypes = WalkState->ArgTypes;
1106 return_ACPI_STATUS (Status);
1109 /* This scope complete? */
1111 if (AcpiPsHasCompletedScope (ParserState))
1113 AcpiPsPopScope (ParserState, &Op,
1114 &WalkState->ArgTypes, &WalkState->ArgCount);
1115 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
1122 } /* while ParserState->Aml */
1126 * Complete the last Op (if not completed), and clear the scope stack.
1127 * It is easily possible to end an AML "package" with an unbounded number
1128 * of open scopes (such as when several ASL blocks are closed with
1129 * sequential closing braces). We want to terminate each one cleanly.
1131 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
1136 if (WalkState->AscendingCallback != NULL)
1139 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1140 WalkState->Opcode = Op->Common.AmlOpcode;
1142 Status = WalkState->AscendingCallback (WalkState);
1143 Status = AcpiPsNextParseState (WalkState, Op, Status);
1144 if (Status == AE_CTRL_PENDING)
1150 if (Status == AE_CTRL_TERMINATE)
1159 AcpiPsCompleteThisOp (WalkState, Op);
1162 AcpiPsPopScope (ParserState, &Op,
1163 &WalkState->ArgTypes, &WalkState->ArgCount);
1167 return_ACPI_STATUS (Status);
1170 else if (ACPI_FAILURE (Status))
1172 AcpiPsCompleteThisOp (WalkState, Op);
1173 return_ACPI_STATUS (Status);
1177 AcpiPsCompleteThisOp (WalkState, Op);
1180 AcpiPsPopScope (ParserState, &Op, &WalkState->ArgTypes,
1181 &WalkState->ArgCount);
1185 return_ACPI_STATUS (Status);
1189 /*******************************************************************************
1191 * FUNCTION: AcpiPsParseAml
1193 * PARAMETERS: StartScope - The starting point of the parse. Becomes the
1194 * root of the parsed op tree.
1195 * Aml - Pointer to the raw AML code to parse
1196 * AmlSize - Length of the AML to parse
1201 * DESCRIPTION: Parse raw AML and return a tree of ops
1203 ******************************************************************************/
1207 ACPI_WALK_STATE *WalkState)
1210 ACPI_STATUS TerminateStatus;
1211 ACPI_THREAD_STATE *Thread;
1212 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
1213 ACPI_WALK_STATE *PreviousWalkState;
1216 ACPI_FUNCTION_TRACE ("PsParseAml");
1218 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Entered with WalkState=%p Aml=%p size=%X\n",
1219 WalkState, WalkState->ParserState.Aml, WalkState->ParserState.AmlSize));
1222 /* Create and initialize a new thread state */
1224 Thread = AcpiUtCreateThreadState ();
1227 return_ACPI_STATUS (AE_NO_MEMORY);
1230 WalkState->Thread = Thread;
1231 AcpiDsPushWalkState (WalkState, Thread);
1234 * This global allows the AML debugger to get a handle to the currently
1235 * executing control method.
1237 AcpiGbl_CurrentWalkList = Thread;
1240 * Execute the walk loop as long as there is a valid Walk State. This
1241 * handles nested control method invocations without recursion.
1243 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
1248 if (ACPI_SUCCESS (Status))
1251 * The ParseLoop executes AML until the method terminates
1252 * or calls another method.
1254 Status = AcpiPsParseLoop (WalkState);
1257 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1258 "Completed one call to walk loop, %s State=%p\n",
1259 AcpiFormatException (Status), WalkState));
1261 if (Status == AE_CTRL_TRANSFER)
1264 * A method call was detected.
1265 * Transfer control to the called control method
1267 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
1270 * If the transfer to the new method method call worked, a new walk
1271 * state was created -- get it
1273 WalkState = AcpiDsGetCurrentWalkState (Thread);
1276 else if (Status == AE_CTRL_TERMINATE)
1280 else if (Status != AE_OK)
1282 ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1283 WalkState->MethodNode, NULL, Status);
1286 /* We are done with this walk, move on to the parent if any */
1288 WalkState = AcpiDsPopWalkState (Thread);
1290 /* Reset the current scope to the beginning of scope stack */
1292 AcpiDsScopeStackClear (WalkState);
1295 * If we just returned from the execution of a control method,
1296 * there's lots of cleanup to do
1298 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE)
1300 TerminateStatus = AcpiDsTerminateControlMethod (WalkState);
1301 if (ACPI_FAILURE (TerminateStatus))
1303 ACPI_REPORT_ERROR ((
1304 "Could not terminate control method properly\n"));
1306 /* Ignore error and continue */
1310 /* Delete this walk state and all linked control states */
1312 AcpiPsCleanupScope (&WalkState->ParserState);
1314 PreviousWalkState = WalkState;
1316 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "ReturnValue=%p, State=%p\n",
1317 WalkState->ReturnDesc, WalkState));
1319 /* Check if we have restarted a preempted walk */
1321 WalkState = AcpiDsGetCurrentWalkState (Thread);
1324 if (ACPI_SUCCESS (Status))
1327 * There is another walk state, restart it.
1328 * If the method return value is not used by the parent,
1329 * The object is deleted
1331 Status = AcpiDsRestartControlMethod (WalkState,
1332 PreviousWalkState->ReturnDesc);
1333 if (ACPI_SUCCESS (Status))
1335 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
1340 /* On error, delete any return object */
1342 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1347 * Just completed a 1st-level method, save the final internal return
1350 else if (PreviousWalkState->CallerReturnDesc)
1352 *(PreviousWalkState->CallerReturnDesc) = PreviousWalkState->ReturnDesc; /* NULL if no return value */
1354 else if (PreviousWalkState->ReturnDesc)
1356 /* Caller doesn't want it, must delete it */
1358 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1361 AcpiDsDeleteWalkState (PreviousWalkState);
1366 AcpiExReleaseAllMutexes (Thread);
1367 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
1368 AcpiGbl_CurrentWalkList = PrevWalkList;
1369 return_ACPI_STATUS (Status);