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 *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/psparse.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ */
120 * Parse the AML and build an operation tree as most interpreters,
121 * like Perl, do. Parsing is done by hand rather than with a YACC
122 * generated parser to tightly constrain stack and dynamic memory
123 * usage. At the same time, parsing is kept flexible and the code
124 * fairly compact by parsing based on a list of AML opcode
125 * templates in AmlOpInfo[]
129 #include "acparser.h"
130 #include "acdispat.h"
132 #include "acnamesp.h"
133 #include "acinterp.h"
135 #define _COMPONENT ACPI_PARSER
136 ACPI_MODULE_NAME ("psparse")
139 static UINT32 AcpiGbl_Depth = 0;
142 /*******************************************************************************
144 * FUNCTION: AcpiPsGetOpcodeSize
146 * PARAMETERS: Opcode - An AML opcode
148 * RETURN: Size of the opcode, in bytes (1 or 2)
150 * DESCRIPTION: Get the size of the current opcode.
152 ******************************************************************************/
155 AcpiPsGetOpcodeSize (
159 /* Extended (2-byte) opcode if > 255 */
166 /* Otherwise, just a single byte opcode */
172 /*******************************************************************************
174 * FUNCTION: AcpiPsPeekOpcode
176 * PARAMETERS: ParserState - A parser state object
180 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
182 ******************************************************************************/
186 ACPI_PARSE_STATE *ParserState)
192 Aml = ParserState->Aml;
193 Opcode = (UINT16) ACPI_GET8 (Aml);
196 if (Opcode == AML_EXTOP)
198 /* Extended opcode */
201 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
208 /*******************************************************************************
210 * FUNCTION: AcpiPsCompleteThisOp
212 * PARAMETERS: WalkState - Current State
213 * Op - Op to complete
217 * DESCRIPTION: Perform any cleanup at the completion of an Op.
219 ******************************************************************************/
222 AcpiPsCompleteThisOp (
223 ACPI_WALK_STATE *WalkState,
224 ACPI_PARSE_OBJECT *Op)
226 ACPI_PARSE_OBJECT *Prev;
227 ACPI_PARSE_OBJECT *Next;
228 const ACPI_OPCODE_INFO *ParentInfo;
229 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
232 ACPI_FUNCTION_TRACE_PTR ("PsCompleteThisOp", Op);
235 /* Check for null Op, can happen if AML code is corrupt */
242 /* Delete this op and the subtree below it if asked to */
244 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) == ACPI_PARSE_DELETE_TREE) &&
245 (WalkState->OpInfo->Class != AML_CLASS_ARGUMENT))
247 /* Make sure that we only delete this subtree */
249 if (Op->Common.Parent)
252 * Check if we need to replace the operator and its subtree
253 * with a return value op (placeholder op)
255 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
257 switch (ParentInfo->Class)
259 case AML_CLASS_CONTROL:
262 case AML_CLASS_CREATE:
265 * These opcodes contain TermArg operands. The current
266 * op must be replaced by a placeholder return op
268 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
275 case AML_CLASS_NAMED_OBJECT:
278 * These opcodes contain TermArg operands. The current
279 * op must be replaced by a placeholder return op
281 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
282 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
283 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
284 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
285 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
287 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
294 if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
295 (WalkState->DescendingCallback != AcpiDsExecBeginOp))
298 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
299 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
300 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
302 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
308 ReplacementOp->Named.Data = Op->Named.Data;
309 ReplacementOp->Named.Length = Op->Named.Length;
315 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
322 /* We must unlink this op from the parent tree */
324 Prev = Op->Common.Parent->Common.Value.Arg;
327 /* This op is the first in the list */
331 ReplacementOp->Common.Parent = Op->Common.Parent;
332 ReplacementOp->Common.Value.Arg = NULL;
333 ReplacementOp->Common.Node = Op->Common.Node;
334 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
335 ReplacementOp->Common.Next = Op->Common.Next;
339 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
343 /* Search the parent list */
347 /* Traverse all siblings in the parent's argument list */
349 Next = Prev->Common.Next;
354 ReplacementOp->Common.Parent = Op->Common.Parent;
355 ReplacementOp->Common.Value.Arg = NULL;
356 ReplacementOp->Common.Node = Op->Common.Node;
357 Prev->Common.Next = ReplacementOp;
358 ReplacementOp->Common.Next = Op->Common.Next;
363 Prev->Common.Next = Op->Common.Next;
372 /* Now we can actually delete the subtree rooted at op */
374 AcpiPsDeleteParseTree (Op);
383 /*******************************************************************************
385 * FUNCTION: AcpiPsNextParseState
387 * PARAMETERS: ParserState - Current parser state object
391 * DESCRIPTION: Update the parser state based upon the return exception from
392 * the parser callback.
394 ******************************************************************************/
397 AcpiPsNextParseState (
398 ACPI_WALK_STATE *WalkState,
399 ACPI_PARSE_OBJECT *Op,
400 ACPI_STATUS CallbackStatus)
402 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
403 ACPI_STATUS Status = AE_CTRL_PENDING;
406 ACPI_FUNCTION_TRACE_PTR ("PsNextParseState", Op);
409 switch (CallbackStatus)
411 case AE_CTRL_TERMINATE:
414 * A control method was terminated via a RETURN statement.
415 * The walk of this method is complete.
417 ParserState->Aml = ParserState->AmlEnd;
418 Status = AE_CTRL_TERMINATE;
424 ParserState->Aml = WalkState->AmlLastWhile;
425 WalkState->ControlState->Common.Value = FALSE;
426 Status = AE_CTRL_BREAK;
429 case AE_CTRL_CONTINUE:
432 ParserState->Aml = WalkState->AmlLastWhile;
433 Status = AE_CTRL_CONTINUE;
436 case AE_CTRL_PENDING:
438 ParserState->Aml = WalkState->AmlLastWhile;
444 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
452 * Predicate of an IF was true, and we are at the matching ELSE.
453 * Just close out this package
455 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
462 * Either an IF/WHILE Predicate was false or we encountered a BREAK
463 * opcode. In both cases, we do not execute the rest of the
464 * package; We simply close out the parent (finishing the walk of
465 * this branch of the tree) and continue execution at the parent
468 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
470 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
472 WalkState->ControlState->Common.Value = FALSE;
473 Status = AE_CTRL_END;
477 case AE_CTRL_TRANSFER:
480 * A method call (invocation) -- transfer control
482 Status = AE_CTRL_TRANSFER;
483 WalkState->PrevOp = Op;
484 WalkState->MethodCallOp = Op;
485 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
487 /* Will return value (if any) be used by the caller? */
489 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
494 Status = CallbackStatus;
495 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
502 return_ACPI_STATUS (Status);
506 /*******************************************************************************
508 * FUNCTION: AcpiPsParseLoop
510 * PARAMETERS: ParserState - Current parser state object
514 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
517 ******************************************************************************/
521 ACPI_WALK_STATE *WalkState)
523 ACPI_STATUS Status = AE_OK;
524 ACPI_PARSE_OBJECT *Op = NULL; /* current op */
525 ACPI_PARSE_OBJECT *Arg = NULL;
526 ACPI_PARSE_OBJECT PreOp;
527 ACPI_PARSE_STATE *ParserState;
528 UINT8 *AmlOpStart = NULL;
531 ACPI_FUNCTION_TRACE_PTR ("PsParseLoop", WalkState);
533 if (WalkState->DescendingCallback == NULL)
535 return_ACPI_STATUS (AE_BAD_PARAMETER);
539 ParserState = &WalkState->ParserState;
540 WalkState->ArgTypes = 0;
542 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
543 if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
545 /* We are restarting a preempted control method */
547 if (AcpiPsHasCompletedScope (ParserState))
550 * We must check if a predicate to an IF or WHILE statement
553 if ((ParserState->Scope->ParseScope.Op) &&
554 ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
555 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
556 (WalkState->ControlState) &&
557 (WalkState->ControlState->Common.State ==
558 ACPI_CONTROL_PREDICATE_EXECUTING))
561 * A predicate was just completed, get the value of the
562 * predicate and branch based on that value
564 WalkState->Op = NULL;
565 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
566 if (ACPI_FAILURE (Status) &&
567 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
569 if (Status == AE_AML_NO_RETURN_VALUE)
571 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
572 "Invoked method did not return a value, %s\n",
573 AcpiFormatException (Status)));
576 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "GetPredicate Failed, %s\n",
577 AcpiFormatException (Status)));
578 return_ACPI_STATUS (Status);
581 Status = AcpiPsNextParseState (WalkState, Op, Status);
584 AcpiPsPopScope (ParserState, &Op,
585 &WalkState->ArgTypes, &WalkState->ArgCount);
586 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
588 else if (WalkState->PrevOp)
590 /* We were in the middle of an op */
592 Op = WalkState->PrevOp;
593 WalkState->ArgTypes = WalkState->PrevArgTypes;
599 * Iterative parsing loop, while there is more aml to process:
601 while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
603 AmlOpStart = ParserState->Aml;
606 /* Get the next opcode from the AML stream */
608 WalkState->AmlOffset = ACPI_PTR_DIFF (ParserState->Aml,
609 ParserState->AmlStart);
610 WalkState->Opcode = AcpiPsPeekOpcode (ParserState);
613 * First cut to determine what we have found:
614 * 1) A valid AML opcode
616 * 3) An unknown/invalid opcode
618 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
619 switch (WalkState->OpInfo->Class)
621 case AML_CLASS_ASCII:
622 case AML_CLASS_PREFIX:
624 * Starts with a valid prefix or ASCII char, this is a name
625 * string. Convert the bare name string to a namepath.
627 WalkState->Opcode = AML_INT_NAMEPATH_OP;
628 WalkState->ArgTypes = ARGP_NAMESTRING;
631 case AML_CLASS_UNKNOWN:
633 /* The opcode is unrecognized. Just skip unknown opcodes */
635 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
636 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
637 WalkState->Opcode, ParserState->Aml, WalkState->AmlOffset));
639 ACPI_DUMP_BUFFER (ParserState->Aml, 128);
641 /* Assume one-byte bad opcode */
648 /* Found opcode info, this is a normal opcode */
650 ParserState->Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
651 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
655 /* Create Op structure and append to parent's argument list */
657 if (WalkState->OpInfo->Flags & AML_NAMED)
659 PreOp.Common.Value.Arg = NULL;
660 PreOp.Common.AmlOpcode = WalkState->Opcode;
663 * Get and append arguments until we find the node that contains
664 * the name (the type ARGP_NAME).
666 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
667 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
669 Status = AcpiPsGetNextArg (WalkState, ParserState,
670 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
671 if (ACPI_FAILURE (Status))
676 AcpiPsAppendArg (&PreOp, Arg);
677 INCREMENT_ARG_LIST (WalkState->ArgTypes);
680 /* Make sure that we found a NAME and didn't run out of arguments */
682 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
684 return_ACPI_STATUS (AE_AML_NO_OPERAND);
687 /* We know that this arg is a name, move to next arg */
689 INCREMENT_ARG_LIST (WalkState->ArgTypes);
692 * Find the object. This will either insert the object into
693 * the namespace or simply look it up
695 WalkState->Op = NULL;
697 Status = WalkState->DescendingCallback (WalkState, &Op);
698 if (ACPI_FAILURE (Status))
700 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "During name lookup/catalog, %s\n",
701 AcpiFormatException (Status)));
710 Status = AcpiPsNextParseState (WalkState, Op, Status);
711 if (Status == AE_CTRL_PENDING)
717 if (ACPI_FAILURE (Status))
722 AcpiPsAppendArg (Op, PreOp.Common.Value.Arg);
725 if (Op->Common.AmlOpcode == AML_REGION_OP)
728 * Defer final parsing of an OperationRegion body,
729 * because we don't have enough info in the first pass
730 * to parse it correctly (i.e., there may be method
731 * calls within the TermArg elements of the body.)
733 * However, we must continue parsing because
734 * the opregion is not a standalone package --
735 * we don't know where the end is at this point.
737 * (Length is unknown until parse of the body complete)
739 Op->Named.Data = AmlOpStart;
740 Op->Named.Length = 0;
745 /* Not a named opcode, just allocate Op and append to parent */
747 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
748 Op = AcpiPsAllocOp (WalkState->Opcode);
751 return_ACPI_STATUS (AE_NO_MEMORY);
754 if (WalkState->OpInfo->Flags & AML_CREATE)
757 * Backup to beginning of CreateXXXfield declaration
758 * BodyLength is unknown until we parse the body
760 Op->Named.Data = AmlOpStart;
761 Op->Named.Length = 0;
764 AcpiPsAppendArg (AcpiPsGetParentScope (ParserState), Op);
766 if ((WalkState->DescendingCallback != NULL))
769 * Find the object. This will either insert the object into
770 * the namespace or simply look it up
774 Status = WalkState->DescendingCallback (WalkState, &Op);
775 Status = AcpiPsNextParseState (WalkState, Op, Status);
776 if (Status == AE_CTRL_PENDING)
782 if (ACPI_FAILURE (Status))
789 Op->Common.AmlOffset = WalkState->AmlOffset;
791 if (WalkState->OpInfo)
793 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
794 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
795 (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
796 Op, ParserState->Aml, Op->Common.AmlOffset));
801 /* Start ArgCount at zero because we don't know if there are any args yet */
803 WalkState->ArgCount = 0;
805 if (WalkState->ArgTypes) /* Are there any arguments that must be processed? */
809 switch (Op->Common.AmlOpcode)
811 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
812 case AML_WORD_OP: /* AML_WORDDATA_ARG */
813 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
814 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
815 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
817 /* Fill in constant or string argument directly */
819 AcpiPsGetNextSimpleArg (ParserState,
820 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
823 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
825 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Op, 1);
826 if (ACPI_FAILURE (Status))
831 WalkState->ArgTypes = 0;
837 /* Op is not a constant or string, append each argument */
839 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
840 !WalkState->ArgCount)
842 WalkState->AmlOffset = ACPI_PTR_DIFF (ParserState->Aml,
843 ParserState->AmlStart);
844 Status = AcpiPsGetNextArg (WalkState, ParserState,
845 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
846 if (ACPI_FAILURE (Status))
853 Arg->Common.AmlOffset = WalkState->AmlOffset;
854 AcpiPsAppendArg (Op, Arg);
856 INCREMENT_ARG_LIST (WalkState->ArgTypes);
859 switch (Op->Common.AmlOpcode)
863 /* For a method, save the length and address of the body */
866 * Skip parsing of control method or opregion body,
867 * because we don't have enough info in the first pass
868 * to parse them correctly.
870 Op->Named.Data = ParserState->Aml;
871 Op->Named.Length = (UINT32) (ParserState->PkgEnd - ParserState->Aml);
873 * Skip body of method. For OpRegions, we must continue
874 * parsing because the opregion is not a standalone
875 * package (We don't know where the end is).
877 ParserState->Aml = ParserState->PkgEnd;
878 WalkState->ArgCount = 0;
883 case AML_VAR_PACKAGE_OP:
885 if ((Op->Common.Parent) &&
886 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
887 (WalkState->DescendingCallback != AcpiDsExecBeginOp))
891 * because we don't have enough info in the first pass
892 * to parse them correctly.
894 Op->Named.Data = AmlOpStart;
895 Op->Named.Length = (UINT32) (ParserState->PkgEnd - AmlOpStart);
899 ParserState->Aml = ParserState->PkgEnd;
900 WalkState->ArgCount = 0;
906 if (WalkState->ControlState)
908 WalkState->ControlState->Control.PackageEnd = ParserState->PkgEnd;
913 /* No action for all other opcodes */
920 /* Check for arguments that need to be processed */
922 if (WalkState->ArgCount)
924 /* There are arguments (complex ones), push Op and prepare for argument */
926 Status = AcpiPsPushScope (ParserState, Op,
927 WalkState->ArgTypes, WalkState->ArgCount);
928 if (ACPI_FAILURE (Status))
930 return_ACPI_STATUS (Status);
936 /* All arguments have been processed -- Op is complete, prepare for next */
938 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
939 if (WalkState->OpInfo->Flags & AML_NAMED)
946 if (Op->Common.AmlOpcode == AML_REGION_OP)
949 * Skip parsing of control method or opregion body,
950 * because we don't have enough info in the first pass
951 * to parse them correctly.
953 * Completed parsing an OpRegion declaration, we now
956 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
960 if (WalkState->OpInfo->Flags & AML_CREATE)
963 * Backup to beginning of CreateXXXfield declaration (1 for
966 * BodyLength is unknown until we parse the body
968 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
971 /* This op complete, notify the dispatcher */
973 if (WalkState->AscendingCallback != NULL)
976 WalkState->Opcode = Op->Common.AmlOpcode;
978 Status = WalkState->AscendingCallback (WalkState);
979 Status = AcpiPsNextParseState (WalkState, Op, Status);
980 if (Status == AE_CTRL_PENDING)
990 * Finished one argument of the containing scope
992 ParserState->Scope->ParseScope.ArgCount--;
994 /* Close this Op (will result in parse subtree deletion) */
996 AcpiPsCompleteThisOp (WalkState, Op);
1005 case AE_CTRL_TRANSFER:
1008 * We are about to transfer to a called method.
1010 WalkState->PrevOp = Op;
1011 WalkState->PrevArgTypes = WalkState->ArgTypes;
1012 return_ACPI_STATUS (Status);
1017 AcpiPsPopScope (ParserState, &Op,
1018 &WalkState->ArgTypes, &WalkState->ArgCount);
1023 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1024 WalkState->Opcode = Op->Common.AmlOpcode;
1026 Status = WalkState->AscendingCallback (WalkState);
1027 Status = AcpiPsNextParseState (WalkState, Op, Status);
1029 AcpiPsCompleteThisOp (WalkState, Op);
1037 case AE_CTRL_CONTINUE:
1039 /* Pop off scopes until we find the While */
1041 while (!Op || (Op->Common.AmlOpcode != AML_WHILE_OP))
1043 AcpiPsPopScope (ParserState, &Op,
1044 &WalkState->ArgTypes, &WalkState->ArgCount);
1047 /* Close this iteration of the While loop */
1050 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1051 WalkState->Opcode = Op->Common.AmlOpcode;
1053 Status = WalkState->AscendingCallback (WalkState);
1054 Status = AcpiPsNextParseState (WalkState, Op, Status);
1056 AcpiPsCompleteThisOp (WalkState, Op);
1063 case AE_CTRL_TERMINATE:
1072 AcpiPsCompleteThisOp (WalkState, Op);
1074 AcpiPsPopScope (ParserState, &Op,
1075 &WalkState->ArgTypes, &WalkState->ArgCount);
1079 return_ACPI_STATUS (Status);
1082 default: /* All other non-AE_OK status */
1088 AcpiPsCompleteThisOp (WalkState, Op);
1090 AcpiPsPopScope (ParserState, &Op,
1091 &WalkState->ArgTypes, &WalkState->ArgCount);
1097 * TBD: Cleanup parse ops on error
1102 AcpiPsPopScope (ParserState, &Op,
1103 &WalkState->ArgTypes, &WalkState->ArgCount);
1106 WalkState->PrevOp = Op;
1107 WalkState->PrevArgTypes = WalkState->ArgTypes;
1108 return_ACPI_STATUS (Status);
1111 /* This scope complete? */
1113 if (AcpiPsHasCompletedScope (ParserState))
1115 AcpiPsPopScope (ParserState, &Op,
1116 &WalkState->ArgTypes, &WalkState->ArgCount);
1117 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
1124 } /* while ParserState->Aml */
1128 * Complete the last Op (if not completed), and clear the scope stack.
1129 * It is easily possible to end an AML "package" with an unbounded number
1130 * of open scopes (such as when several ASL blocks are closed with
1131 * sequential closing braces). We want to terminate each one cleanly.
1133 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
1138 if (WalkState->AscendingCallback != NULL)
1141 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1142 WalkState->Opcode = Op->Common.AmlOpcode;
1144 Status = WalkState->AscendingCallback (WalkState);
1145 Status = AcpiPsNextParseState (WalkState, Op, Status);
1146 if (Status == AE_CTRL_PENDING)
1152 if (Status == AE_CTRL_TERMINATE)
1161 AcpiPsCompleteThisOp (WalkState, Op);
1164 AcpiPsPopScope (ParserState, &Op,
1165 &WalkState->ArgTypes, &WalkState->ArgCount);
1169 return_ACPI_STATUS (Status);
1172 else if (ACPI_FAILURE (Status))
1174 AcpiPsCompleteThisOp (WalkState, Op);
1175 return_ACPI_STATUS (Status);
1179 AcpiPsCompleteThisOp (WalkState, Op);
1182 AcpiPsPopScope (ParserState, &Op, &WalkState->ArgTypes,
1183 &WalkState->ArgCount);
1187 return_ACPI_STATUS (Status);
1191 /*******************************************************************************
1193 * FUNCTION: AcpiPsParseAml
1195 * PARAMETERS: StartScope - The starting point of the parse. Becomes the
1196 * root of the parsed op tree.
1197 * Aml - Pointer to the raw AML code to parse
1198 * AmlSize - Length of the AML to parse
1203 * DESCRIPTION: Parse raw AML and return a tree of ops
1205 ******************************************************************************/
1209 ACPI_WALK_STATE *WalkState)
1212 ACPI_STATUS TerminateStatus;
1213 ACPI_THREAD_STATE *Thread;
1214 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
1215 ACPI_WALK_STATE *PreviousWalkState;
1216 ACPI_OPERAND_OBJECT **CallerReturnDesc = WalkState->CallerReturnDesc;
1217 ACPI_OPERAND_OBJECT *EffectiveReturnDesc = NULL;
1221 ACPI_FUNCTION_TRACE ("PsParseAml");
1223 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Entered with WalkState=%p Aml=%p size=%X\n",
1224 WalkState, WalkState->ParserState.Aml, WalkState->ParserState.AmlSize));
1227 /* Create and initialize a new thread state */
1229 Thread = AcpiUtCreateThreadState ();
1232 return_ACPI_STATUS (AE_NO_MEMORY);
1235 WalkState->Thread = Thread;
1236 AcpiDsPushWalkState (WalkState, Thread);
1239 * This global allows the AML debugger to get a handle to the currently
1240 * executing control method.
1242 AcpiGbl_CurrentWalkList = Thread;
1245 * Execute the walk loop as long as there is a valid Walk State. This
1246 * handles nested control method invocations without recursion.
1248 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
1253 if (ACPI_SUCCESS (Status))
1256 * The ParseLoop executes AML until the method terminates
1257 * or calls another method.
1259 Status = AcpiPsParseLoop (WalkState);
1262 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1263 "Completed one call to walk loop, %s State=%p\n",
1264 AcpiFormatException (Status), WalkState));
1266 if (Status == AE_CTRL_TRANSFER)
1269 * A method call was detected.
1270 * Transfer control to the called control method
1272 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
1275 * If the transfer to the new method method call worked, a new walk
1276 * state was created -- get it
1278 WalkState = AcpiDsGetCurrentWalkState (Thread);
1281 else if (Status == AE_CTRL_TERMINATE)
1285 else if (Status != AE_OK)
1287 ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1288 WalkState->MethodNode, NULL, Status);
1291 /* We are done with this walk, move on to the parent if any */
1293 WalkState = AcpiDsPopWalkState (Thread);
1294 /* Save the last effective return value */
1296 if (CallerReturnDesc && WalkState->ReturnDesc)
1298 AcpiUtRemoveReference (EffectiveReturnDesc);
1299 EffectiveReturnDesc = WalkState->ReturnDesc;
1300 AcpiUtAddReference (EffectiveReturnDesc);
1303 /* Reset the current scope to the beginning of scope stack */
1305 AcpiDsScopeStackClear (WalkState);
1308 * If we just returned from the execution of a control method,
1309 * there's lots of cleanup to do
1311 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE)
1313 TerminateStatus = AcpiDsTerminateControlMethod (WalkState);
1314 if (ACPI_FAILURE (TerminateStatus))
1316 ACPI_REPORT_ERROR ((
1317 "Could not terminate control method properly\n"));
1319 /* Ignore error and continue */
1323 /* Delete this walk state and all linked control states */
1325 AcpiPsCleanupScope (&WalkState->ParserState);
1327 PreviousWalkState = WalkState;
1329 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "ReturnValue=%p, State=%p\n",
1330 WalkState->ReturnDesc, WalkState));
1332 /* Check if we have restarted a preempted walk */
1334 WalkState = AcpiDsGetCurrentWalkState (Thread);
1337 if (ACPI_SUCCESS (Status))
1340 * There is another walk state, restart it.
1341 * If the method return value is not used by the parent,
1342 * The object is deleted
1344 Status = AcpiDsRestartControlMethod (WalkState,
1345 PreviousWalkState->ReturnDesc);
1346 if (ACPI_SUCCESS (Status))
1348 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
1353 /* On error, delete any return object */
1355 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1360 * Just completed a 1st-level method, save the final internal return
1363 else if (PreviousWalkState->CallerReturnDesc)
1366 * Some AML code expects return value w/o ReturnOp.
1367 * Return the saved effective return value instead.
1370 if (PreviousWalkState->ReturnDesc == NULL && EffectiveReturnDesc != NULL)
1372 PreviousWalkState->ReturnDesc = EffectiveReturnDesc;
1373 AcpiUtAddReference (PreviousWalkState->ReturnDesc);
1376 *(PreviousWalkState->CallerReturnDesc) = PreviousWalkState->ReturnDesc; /* NULL if no return value */
1378 else if (PreviousWalkState->ReturnDesc)
1380 /* Caller doesn't want it, must delete it */
1382 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1385 AcpiDsDeleteWalkState (PreviousWalkState);
1390 AcpiUtRemoveReference (EffectiveReturnDesc);
1391 AcpiExReleaseAllMutexes (Thread);
1392 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
1393 AcpiGbl_CurrentWalkList = PrevWalkList;
1394 return_ACPI_STATUS (Status);