Merge from vendor branch TCPDUMP:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20031203 / interpreter / parser / psparse.c
1 /******************************************************************************
2  *
3  * Module Name: psparse - Parser top level AML parse routines
4  *              $Revision: 142 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
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
20  * property rights.
21  *
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
28  *
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;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
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.
54  *
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
65  * make.
66  *
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
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
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.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
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
89  * PARTICULAR PURPOSE.
90  *
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
98  * LIMITED REMEDY.
99  *
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.
114  *
115  *****************************************************************************/
116
117
118 /*
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[]
125  */
126
127 #include "acpi.h"
128 #include "acparser.h"
129 #include "acdispat.h"
130 #include "amlcode.h"
131 #include "acnamesp.h"
132 #include "acinterp.h"
133
134 #define _COMPONENT          ACPI_PARSER
135         ACPI_MODULE_NAME    ("psparse")
136
137
138 static UINT32               AcpiGbl_Depth = 0;
139
140
141 /*******************************************************************************
142  *
143  * FUNCTION:    AcpiPsGetOpcodeSize
144  *
145  * PARAMETERS:  Opcode          - An AML opcode
146  *
147  * RETURN:      Size of the opcode, in bytes (1 or 2)
148  *
149  * DESCRIPTION: Get the size of the current opcode.
150  *
151  ******************************************************************************/
152
153 UINT32
154 AcpiPsGetOpcodeSize (
155     UINT32                  Opcode)
156 {
157
158     /* Extended (2-byte) opcode if > 255 */
159
160     if (Opcode > 0x00FF)
161     {
162         return (2);
163     }
164
165     /* Otherwise, just a single byte opcode */
166
167     return (1);
168 }
169
170
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiPsPeekOpcode
174  *
175  * PARAMETERS:  ParserState         - A parser state object
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
180  *
181  ******************************************************************************/
182
183 UINT16
184 AcpiPsPeekOpcode (
185     ACPI_PARSE_STATE        *ParserState)
186 {
187     UINT8                   *Aml;
188     UINT16                  Opcode;
189
190
191     Aml = ParserState->Aml;
192     Opcode = (UINT16) ACPI_GET8 (Aml);
193
194
195     if (Opcode == AML_EXTOP)
196     {
197         /* Extended opcode */
198
199         Aml++;
200         Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
201     }
202
203     return (Opcode);
204 }
205
206
207 /*******************************************************************************
208  *
209  * FUNCTION:    AcpiPsCompleteThisOp
210  *
211  * PARAMETERS:  WalkState       - Current State
212  *              Op              - Op to complete
213  *
214  * RETURN:      None.
215  *
216  * DESCRIPTION: Perform any cleanup at the completion of an Op.
217  *
218  ******************************************************************************/
219
220 void
221 AcpiPsCompleteThisOp (
222     ACPI_WALK_STATE         *WalkState,
223     ACPI_PARSE_OBJECT       *Op)
224 {
225     ACPI_PARSE_OBJECT       *Prev;
226     ACPI_PARSE_OBJECT       *Next;
227     const ACPI_OPCODE_INFO  *ParentInfo;
228     ACPI_PARSE_OBJECT       *ReplacementOp = NULL;
229
230
231     ACPI_FUNCTION_TRACE_PTR ("PsCompleteThisOp", Op);
232
233
234     /* Check for null Op, can happen if AML code is corrupt */
235
236     if (!Op)
237     {
238         return_VOID;
239     }
240
241     /* Delete this op and the subtree below it if asked to */
242
243     if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) == ACPI_PARSE_DELETE_TREE) &&
244         (WalkState->OpInfo->Class != AML_CLASS_ARGUMENT))
245     {
246         /* Make sure that we only delete this subtree */
247
248         if (Op->Common.Parent)
249         {
250             /*
251              * Check if we need to replace the operator and its subtree
252              * with a return value op (placeholder op)
253              */
254             ParentInfo  = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
255
256             switch (ParentInfo->Class)
257             {
258             case AML_CLASS_CONTROL:
259                 break;
260
261             case AML_CLASS_CREATE:
262
263                 /*
264                  * These opcodes contain TermArg operands.  The current
265                  * op must be replaced by a placeholder return op
266                  */
267                 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
268                 if (!ReplacementOp)
269                 {
270                     return_VOID;
271                 }
272                 break;
273
274             case AML_CLASS_NAMED_OBJECT:
275
276                 /*
277                  * These opcodes contain TermArg operands.  The current
278                  * op must be replaced by a placeholder return op
279                  */
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))
285                 {
286                     ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
287                     if (!ReplacementOp)
288                     {
289                         return_VOID;
290                     }
291                 }
292
293                 if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
294                     (WalkState->DescendingCallback != AcpiDsExecBeginOp))
295
296                 {
297                     if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
298                         (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
299                         (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
300                     {
301                         ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
302                         if (!ReplacementOp)
303                         {
304                             return_VOID;
305                         }
306
307                         ReplacementOp->Named.Data = Op->Named.Data;
308                         ReplacementOp->Named.Length = Op->Named.Length;
309                     }
310                 }
311                 break;
312
313             default:
314                 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
315                 if (!ReplacementOp)
316                 {
317                     return_VOID;
318                 }
319             }
320
321             /* We must unlink this op from the parent tree */
322
323             Prev = Op->Common.Parent->Common.Value.Arg;
324             if (Prev == Op)
325             {
326                 /* This op is the first in the list */
327
328                 if (ReplacementOp)
329                 {
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;
335                 }
336                 else
337                 {
338                     Op->Common.Parent->Common.Value.Arg    = Op->Common.Next;
339                 }
340             }
341
342             /* Search the parent list */
343
344             else while (Prev)
345             {
346                 /* Traverse all siblings in the parent's argument list */
347
348                 Next = Prev->Common.Next;
349                 if (Next == Op)
350                 {
351                     if (ReplacementOp)
352                     {
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;
358                         Next = NULL;
359                     }
360                     else
361                     {
362                         Prev->Common.Next = Op->Common.Next;
363                         Next = NULL;
364                     }
365                 }
366
367                 Prev = Next;
368             }
369         }
370
371         /* Now we can actually delete the subtree rooted at op */
372
373         AcpiPsDeleteParseTree (Op);
374
375         return_VOID;
376     }
377
378     return_VOID;
379 }
380
381
382 /*******************************************************************************
383  *
384  * FUNCTION:    AcpiPsNextParseState
385  *
386  * PARAMETERS:  ParserState         - Current parser state object
387  *
388  * RETURN:      Status
389  *
390  * DESCRIPTION: Update the parser state based upon the return exception from
391  *              the parser callback.
392  *
393  ******************************************************************************/
394
395 ACPI_STATUS
396 AcpiPsNextParseState (
397     ACPI_WALK_STATE         *WalkState,
398     ACPI_PARSE_OBJECT       *Op,
399     ACPI_STATUS             CallbackStatus)
400 {
401     ACPI_PARSE_STATE        *ParserState = &WalkState->ParserState;
402     ACPI_STATUS             Status = AE_CTRL_PENDING;
403
404
405     ACPI_FUNCTION_TRACE_PTR ("PsNextParseState", Op);
406
407
408     switch (CallbackStatus)
409     {
410     case AE_CTRL_TERMINATE:
411
412         /*
413          * A control method was terminated via a RETURN statement.
414          * The walk of this method is complete.
415          */
416         ParserState->Aml = ParserState->AmlEnd;
417         Status = AE_CTRL_TERMINATE;
418         break;
419
420
421     case AE_CTRL_BREAK:
422
423         ParserState->Aml = WalkState->AmlLastWhile;
424         WalkState->ControlState->Common.Value = FALSE;
425         Status = AE_CTRL_BREAK;
426         break;
427
428     case AE_CTRL_CONTINUE:
429
430
431         ParserState->Aml = WalkState->AmlLastWhile;
432         Status = AE_CTRL_CONTINUE;
433         break;
434
435     case AE_CTRL_PENDING:
436
437         ParserState->Aml = WalkState->AmlLastWhile;
438         break;
439
440 #if 0
441     case AE_CTRL_SKIP:
442
443         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
444         Status = AE_OK;
445         break;
446 #endif
447
448     case AE_CTRL_TRUE:
449
450         /*
451          * Predicate of an IF was true, and we are at the matching ELSE.
452          * Just close out this package
453          */
454         ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
455         break;
456
457
458     case AE_CTRL_FALSE:
459
460         /*
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
465          * level.
466          */
467         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
468
469         /* In the case of a BREAK, just force a predicate (if any) to FALSE */
470
471         WalkState->ControlState->Common.Value = FALSE;
472         Status = AE_CTRL_END;
473         break;
474
475
476     case AE_CTRL_TRANSFER:
477
478         /*
479          * A method call (invocation) -- transfer control
480          */
481         Status = AE_CTRL_TRANSFER;
482         WalkState->PrevOp = Op;
483         WalkState->MethodCallOp = Op;
484         WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
485
486         /* Will return value (if any) be used by the caller? */
487
488         WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
489         break;
490
491
492     default:
493         Status = CallbackStatus;
494         if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
495         {
496             Status = AE_OK;
497         }
498         break;
499     }
500
501     return_ACPI_STATUS (Status);
502 }
503
504
505 /*******************************************************************************
506  *
507  * FUNCTION:    AcpiPsParseLoop
508  *
509  * PARAMETERS:  ParserState         - Current parser state object
510  *
511  * RETURN:      Status
512  *
513  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
514  *              a tree of ops.
515  *
516  ******************************************************************************/
517
518 ACPI_STATUS
519 AcpiPsParseLoop (
520     ACPI_WALK_STATE         *WalkState)
521 {
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;
528
529
530     ACPI_FUNCTION_TRACE_PTR ("PsParseLoop", WalkState);
531
532     if (WalkState->DescendingCallback == NULL)
533     {
534         return_ACPI_STATUS (AE_BAD_PARAMETER);
535     }
536
537     ParserState = &WalkState->ParserState;
538     WalkState->ArgTypes = 0;
539
540 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
541     if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
542     {
543         /* We are restarting a preempted control method */
544
545         if (AcpiPsHasCompletedScope (ParserState))
546         {
547             /*
548              * We must check if a predicate to an IF or WHILE statement
549              * was just completed
550              */
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))
557             {
558                 /*
559                  * A predicate was just completed, get the value of the
560                  * predicate and branch based on that value
561                  */
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))
566                 {
567                     if (Status == AE_AML_NO_RETURN_VALUE)
568                     {
569                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
570                             "Invoked method did not return a value, %s\n",
571                             AcpiFormatException (Status)));
572
573                     }
574                     ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "GetPredicate Failed, %s\n",
575                         AcpiFormatException (Status)));
576                     return_ACPI_STATUS (Status);
577                 }
578
579                 Status = AcpiPsNextParseState (WalkState, Op, Status);
580             }
581
582             AcpiPsPopScope (ParserState, &Op,
583                 &WalkState->ArgTypes, &WalkState->ArgCount);
584             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
585         }
586         else if (WalkState->PrevOp)
587         {
588             /* We were in the middle of an op */
589
590             Op = WalkState->PrevOp;
591             WalkState->ArgTypes = WalkState->PrevArgTypes;
592         }
593     }
594 #endif
595
596     /*
597      * Iterative parsing loop, while there is more aml to process:
598      */
599     while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
600     {
601         AmlOpStart = ParserState->Aml;
602         if (!Op)
603         {
604             /* Get the next opcode from the AML stream */
605
606             WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (ParserState->Aml,
607                                                            ParserState->AmlStart);
608             WalkState->Opcode    = AcpiPsPeekOpcode (ParserState);
609
610             /*
611              * First cut to determine what we have found:
612              * 1) A valid AML opcode
613              * 2) A name string
614              * 3) An unknown/invalid opcode
615              */
616             WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
617             switch (WalkState->OpInfo->Class)
618             {
619             case AML_CLASS_ASCII:
620             case AML_CLASS_PREFIX:
621                 /*
622                  * Starts with a valid prefix or ASCII char, this is a name
623                  * string.  Convert the bare name string to a namepath.
624                  */
625                 WalkState->Opcode = AML_INT_NAMEPATH_OP;
626                 WalkState->ArgTypes = ARGP_NAMESTRING;
627                 break;
628
629             case AML_CLASS_UNKNOWN:
630
631                 /* The opcode is unrecognized.  Just skip unknown opcodes */
632
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));
636
637                 ACPI_DUMP_BUFFER (ParserState->Aml, 128);
638
639                 /* Assume one-byte bad opcode */
640
641                 ParserState->Aml++;
642                 continue;
643
644             default:
645
646                 /* Found opcode info, this is a normal opcode */
647
648                 ParserState->Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
649                 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
650                 break;
651             }
652
653             /* Create Op structure and append to parent's argument list */
654
655             if (WalkState->OpInfo->Flags & AML_NAMED)
656             {
657                 PreOp.Common.Value.Arg = NULL;
658                 PreOp.Common.AmlOpcode = WalkState->Opcode;
659
660                 /*
661                  * Get and append arguments until we find the node that contains
662                  * the name (the type ARGP_NAME).
663                  */
664                 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
665                       (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
666                 {
667                     Status = AcpiPsGetNextArg (WalkState, ParserState,
668                                 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
669                     if (ACPI_FAILURE (Status))
670                     {
671                         goto CloseThisOp;
672                     }
673
674                     AcpiPsAppendArg (&PreOp, Arg);
675                     INCREMENT_ARG_LIST (WalkState->ArgTypes);
676                 }
677
678                 /* Make sure that we found a NAME and didn't run out of arguments */
679
680                 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
681                 {
682                     return_ACPI_STATUS (AE_AML_NO_OPERAND);
683                 }
684
685                 /* We know that this arg is a name, move to next arg */
686
687                 INCREMENT_ARG_LIST (WalkState->ArgTypes);
688
689                 /*
690                  * Find the object.  This will either insert the object into
691                  * the namespace or simply look it up
692                  */
693                 WalkState->Op = NULL;
694
695                 Status = WalkState->DescendingCallback (WalkState, &Op);
696                 if (ACPI_FAILURE (Status))
697                 {
698                     ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "During name lookup/catalog, %s\n",
699                             AcpiFormatException (Status)));
700                     goto CloseThisOp;
701                 }
702
703                 if (Op == NULL)
704                 {
705                     continue;
706                 }
707
708                 Status = AcpiPsNextParseState (WalkState, Op, Status);
709                 if (Status == AE_CTRL_PENDING)
710                 {
711                     Status = AE_OK;
712                     goto CloseThisOp;
713                 }
714
715                 if (ACPI_FAILURE (Status))
716                 {
717                     goto CloseThisOp;
718                 }
719
720                 AcpiPsAppendArg (Op, PreOp.Common.Value.Arg);
721                 AcpiGbl_Depth++;
722
723                 if (Op->Common.AmlOpcode == AML_REGION_OP)
724                 {
725                     /*
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.)
730                      *
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.
734                      *
735                      * (Length is unknown until parse of the body complete)
736                      */
737                     Op->Named.Data    = AmlOpStart;
738                     Op->Named.Length  = 0;
739                 }
740             }
741             else
742             {
743                 /* Not a named opcode, just allocate Op and append to parent */
744
745                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
746                 Op = AcpiPsAllocOp (WalkState->Opcode);
747                 if (!Op)
748                 {
749                     return_ACPI_STATUS (AE_NO_MEMORY);
750                 }
751
752                 if (WalkState->OpInfo->Flags & AML_CREATE)
753                 {
754                     /*
755                      * Backup to beginning of CreateXXXfield declaration
756                      * BodyLength is unknown until we parse the body
757                      */
758                     Op->Named.Data    = AmlOpStart;
759                     Op->Named.Length  = 0;
760                 }
761
762                 AcpiPsAppendArg (AcpiPsGetParentScope (ParserState), Op);
763
764                 if ((WalkState->DescendingCallback != NULL))
765                 {
766                     /*
767                      * Find the object.  This will either insert the object into
768                      * the namespace or simply look it up
769                      */
770                     WalkState->Op = Op;
771
772                     Status = WalkState->DescendingCallback (WalkState, &Op);
773                     Status = AcpiPsNextParseState (WalkState, Op, Status);
774                     if (Status == AE_CTRL_PENDING)
775                     {
776                         Status = AE_OK;
777                         goto CloseThisOp;
778                     }
779
780                     if (ACPI_FAILURE (Status))
781                     {
782                         goto CloseThisOp;
783                     }
784                 }
785             }
786
787             Op->Common.AmlOffset = WalkState->AmlOffset;
788
789             if (WalkState->OpInfo)
790             {
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));
795             }
796         }
797
798
799         /* Start ArgCount at zero because we don't know if there are any args yet */
800
801         WalkState->ArgCount  = 0;
802
803         if (WalkState->ArgTypes)   /* Are there any arguments that must be processed? */
804         {
805             /* Get arguments */
806
807             switch (Op->Common.AmlOpcode)
808             {
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 */
814
815                 /* Fill in constant or string argument directly */
816
817                 AcpiPsGetNextSimpleArg (ParserState,
818                     GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
819                 break;
820
821             case AML_INT_NAMEPATH_OP:   /* AML_NAMESTRING_ARG */
822
823                 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Op, 1);
824                 if (ACPI_FAILURE (Status))
825                 {
826                     goto CloseThisOp;
827                 }
828
829                 WalkState->ArgTypes = 0;
830                 break;
831
832             default:
833
834                 /* Op is not a constant or string, append each argument to the Op */
835
836                 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
837                         !WalkState->ArgCount)
838                 {
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))
844                     {
845                         goto CloseThisOp;
846                     }
847
848                     if (Arg)
849                     {
850                         Arg->Common.AmlOffset = WalkState->AmlOffset;
851                         AcpiPsAppendArg (Op, Arg);
852                     }
853                     INCREMENT_ARG_LIST (WalkState->ArgTypes);
854                 }
855
856                 /* Special processing for certain opcodes */
857
858                 switch (Op->Common.AmlOpcode)
859                 {
860                 case AML_METHOD_OP:
861
862                     /*
863                      * Skip parsing of control method
864                      * because we don't have enough info in the first pass
865                      * to parse it correctly.
866                      *
867                      * Save the length and address of the body
868                      */
869                     Op->Named.Data   = ParserState->Aml;
870                     Op->Named.Length = (UINT32) (ParserState->PkgEnd - ParserState->Aml);
871
872                     /* Skip body of method */
873
874                     ParserState->Aml    = ParserState->PkgEnd;
875                     WalkState->ArgCount = 0;
876                     break;
877
878                 case AML_BUFFER_OP:
879                 case AML_PACKAGE_OP:
880                 case AML_VAR_PACKAGE_OP:
881
882                     if ((Op->Common.Parent) &&
883                         (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
884                         (WalkState->DescendingCallback != AcpiDsExecBeginOp))
885                     {
886                         /*
887                          * Skip parsing of Buffers and Packages
888                          * because we don't have enough info in the first pass
889                          * to parse them correctly.
890                          */
891                         Op->Named.Data   = AmlOpStart;
892                         Op->Named.Length = (UINT32) (ParserState->PkgEnd - AmlOpStart);
893
894                         /* Skip body */
895
896                         ParserState->Aml    = ParserState->PkgEnd;
897                         WalkState->ArgCount = 0;
898                     }
899                     break;
900
901                 case AML_WHILE_OP:
902
903                     if (WalkState->ControlState)
904                     {
905                         WalkState->ControlState->Control.PackageEnd = ParserState->PkgEnd;
906                     }
907                     break;
908
909                 default:
910
911                     /* No action for all other opcodes */
912                     break;
913                 }
914                 break;
915             }
916         }
917
918         /* Check for arguments that need to be processed */
919
920         if (WalkState->ArgCount)
921         {
922             /* There are arguments (complex ones), push Op and prepare for argument */
923
924             Status = AcpiPsPushScope (ParserState, Op,
925                         WalkState->ArgTypes, WalkState->ArgCount);
926             if (ACPI_FAILURE (Status))
927             {
928                 return_ACPI_STATUS (Status);
929             }
930             Op = NULL;
931             continue;
932         }
933
934         /* All arguments have been processed -- Op is complete, prepare for next */
935
936         WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
937         if (WalkState->OpInfo->Flags & AML_NAMED)
938         {
939             if (AcpiGbl_Depth)
940             {
941                 AcpiGbl_Depth--;
942             }
943
944             if (Op->Common.AmlOpcode == AML_REGION_OP)
945             {
946                 /*
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.
950                  *
951                  * Completed parsing an OpRegion declaration, we now
952                  * know the length.
953                  */
954                 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
955             }
956         }
957
958         if (WalkState->OpInfo->Flags & AML_CREATE)
959         {
960             /*
961              * Backup to beginning of CreateXXXfield declaration (1 for
962              * Opcode)
963              *
964              * BodyLength is unknown until we parse the body
965              */
966             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
967         }
968
969         /* This op complete, notify the dispatcher */
970
971         if (WalkState->AscendingCallback != NULL)
972         {
973             WalkState->Op     = Op;
974             WalkState->Opcode = Op->Common.AmlOpcode;
975
976             Status = WalkState->AscendingCallback (WalkState);
977             Status = AcpiPsNextParseState (WalkState, Op, Status);
978             if (Status == AE_CTRL_PENDING)
979             {
980                 Status = AE_OK;
981                 goto CloseThisOp;
982             }
983         }
984
985
986 CloseThisOp:
987         /*
988          * Finished one argument of the containing scope
989          */
990         ParserState->Scope->ParseScope.ArgCount--;
991
992         /* Close this Op (will result in parse subtree deletion) */
993
994         AcpiPsCompleteThisOp (WalkState, Op);
995         Op = NULL;
996
997         switch (Status)
998         {
999         case AE_OK:
1000             break;
1001
1002
1003         case AE_CTRL_TRANSFER:
1004
1005             /*
1006              * We are about to transfer to a called method.
1007              */
1008             WalkState->PrevOp = Op;
1009             WalkState->PrevArgTypes = WalkState->ArgTypes;
1010             return_ACPI_STATUS (Status);
1011
1012
1013         case AE_CTRL_END:
1014
1015             AcpiPsPopScope (ParserState, &Op,
1016                 &WalkState->ArgTypes, &WalkState->ArgCount);
1017
1018             if (Op)
1019             {
1020                 WalkState->Op     = Op;
1021                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1022                 WalkState->Opcode = Op->Common.AmlOpcode;
1023
1024                 Status = WalkState->AscendingCallback (WalkState);
1025                 Status = AcpiPsNextParseState (WalkState, Op, Status);
1026
1027                 AcpiPsCompleteThisOp (WalkState, Op);
1028                 Op = NULL;
1029             }
1030             Status = AE_OK;
1031             break;
1032
1033
1034         case AE_CTRL_BREAK:
1035         case AE_CTRL_CONTINUE:
1036
1037             /* Pop off scopes until we find the While */
1038
1039             while (!Op || (Op->Common.AmlOpcode != AML_WHILE_OP))
1040             {
1041                 AcpiPsPopScope (ParserState, &Op,
1042                     &WalkState->ArgTypes, &WalkState->ArgCount);
1043             }
1044
1045             /* Close this iteration of the While loop */
1046
1047             WalkState->Op     = Op;
1048             WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1049             WalkState->Opcode = Op->Common.AmlOpcode;
1050
1051             Status = WalkState->AscendingCallback (WalkState);
1052             Status = AcpiPsNextParseState (WalkState, Op, Status);
1053
1054             AcpiPsCompleteThisOp (WalkState, Op);
1055             Op = NULL;
1056
1057             Status = AE_OK;
1058             break;
1059
1060
1061         case AE_CTRL_TERMINATE:
1062
1063             Status = AE_OK;
1064
1065             /* Clean up */
1066             do
1067             {
1068                 if (Op)
1069                 {
1070                     AcpiPsCompleteThisOp (WalkState, Op);
1071                 }
1072                 AcpiPsPopScope (ParserState, &Op,
1073                     &WalkState->ArgTypes, &WalkState->ArgCount);
1074
1075             } while (Op);
1076
1077             return_ACPI_STATUS (Status);
1078
1079
1080         default:  /* All other non-AE_OK status */
1081
1082             do
1083             {
1084                 if (Op)
1085                 {
1086                     AcpiPsCompleteThisOp (WalkState, Op);
1087                 }
1088                 AcpiPsPopScope (ParserState, &Op,
1089                     &WalkState->ArgTypes, &WalkState->ArgCount);
1090
1091             } while (Op);
1092
1093
1094             /*
1095              * TBD: Cleanup parse ops on error
1096              */
1097 #if 0
1098             if (Op == NULL)
1099             {
1100                 AcpiPsPopScope (ParserState, &Op,
1101                     &WalkState->ArgTypes, &WalkState->ArgCount);
1102             }
1103 #endif
1104             WalkState->PrevOp = Op;
1105             WalkState->PrevArgTypes = WalkState->ArgTypes;
1106             return_ACPI_STATUS (Status);
1107         }
1108
1109         /* This scope complete? */
1110
1111         if (AcpiPsHasCompletedScope (ParserState))
1112         {
1113             AcpiPsPopScope (ParserState, &Op,
1114                 &WalkState->ArgTypes, &WalkState->ArgCount);
1115             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
1116         }
1117         else
1118         {
1119             Op = NULL;
1120         }
1121
1122     } /* while ParserState->Aml */
1123
1124
1125     /*
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.
1130      */
1131     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
1132     do
1133     {
1134         if (Op)
1135         {
1136             if (WalkState->AscendingCallback != NULL)
1137             {
1138                 WalkState->Op     = Op;
1139                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1140                 WalkState->Opcode = Op->Common.AmlOpcode;
1141
1142                 Status = WalkState->AscendingCallback (WalkState);
1143                 Status = AcpiPsNextParseState (WalkState, Op, Status);
1144                 if (Status == AE_CTRL_PENDING)
1145                 {
1146                     Status = AE_OK;
1147                     goto CloseThisOp;
1148                 }
1149
1150                 if (Status == AE_CTRL_TERMINATE)
1151                 {
1152                     Status = AE_OK;
1153
1154                     /* Clean up */
1155                     do
1156                     {
1157                         if (Op)
1158                         {
1159                             AcpiPsCompleteThisOp (WalkState, Op);
1160                         }
1161
1162                         AcpiPsPopScope (ParserState, &Op,
1163                             &WalkState->ArgTypes, &WalkState->ArgCount);
1164
1165                     } while (Op);
1166
1167                     return_ACPI_STATUS (Status);
1168                 }
1169
1170                 else if (ACPI_FAILURE (Status))
1171                 {
1172                     AcpiPsCompleteThisOp (WalkState, Op);
1173                     return_ACPI_STATUS (Status);
1174                 }
1175             }
1176
1177             AcpiPsCompleteThisOp (WalkState, Op);
1178         }
1179
1180         AcpiPsPopScope (ParserState, &Op, &WalkState->ArgTypes,
1181             &WalkState->ArgCount);
1182
1183     } while (Op);
1184
1185     return_ACPI_STATUS (Status);
1186 }
1187
1188
1189 /*******************************************************************************
1190  *
1191  * FUNCTION:    AcpiPsParseAml
1192  *
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
1197  *
1198  *
1199  * RETURN:      Status
1200  *
1201  * DESCRIPTION: Parse raw AML and return a tree of ops
1202  *
1203  ******************************************************************************/
1204
1205 ACPI_STATUS
1206 AcpiPsParseAml (
1207     ACPI_WALK_STATE         *WalkState)
1208 {
1209     ACPI_STATUS             Status;
1210     ACPI_STATUS             TerminateStatus;
1211     ACPI_THREAD_STATE       *Thread;
1212     ACPI_THREAD_STATE       *PrevWalkList = AcpiGbl_CurrentWalkList;
1213     ACPI_WALK_STATE         *PreviousWalkState;
1214
1215
1216     ACPI_FUNCTION_TRACE ("PsParseAml");
1217
1218     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Entered with WalkState=%p Aml=%p size=%X\n",
1219         WalkState, WalkState->ParserState.Aml, WalkState->ParserState.AmlSize));
1220
1221
1222     /* Create and initialize a new thread state */
1223
1224     Thread = AcpiUtCreateThreadState ();
1225     if (!Thread)
1226     {
1227         return_ACPI_STATUS (AE_NO_MEMORY);
1228     }
1229
1230     WalkState->Thread = Thread;
1231     AcpiDsPushWalkState (WalkState, Thread);
1232
1233     /*
1234      * This global allows the AML debugger to get a handle to the currently
1235      * executing control method.
1236      */
1237     AcpiGbl_CurrentWalkList = Thread;
1238
1239     /*
1240      * Execute the walk loop as long as there is a valid Walk State.  This
1241      * handles nested control method invocations without recursion.
1242      */
1243     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
1244
1245     Status = AE_OK;
1246     while (WalkState)
1247     {
1248         if (ACPI_SUCCESS (Status))
1249         {
1250             /*
1251              * The ParseLoop executes AML until the method terminates
1252              * or calls another method.
1253              */
1254             Status = AcpiPsParseLoop (WalkState);
1255         }
1256
1257         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1258             "Completed one call to walk loop, %s State=%p\n",
1259             AcpiFormatException (Status), WalkState));
1260
1261         if (Status == AE_CTRL_TRANSFER)
1262         {
1263             /*
1264              * A method call was detected.
1265              * Transfer control to the called control method
1266              */
1267             Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
1268
1269             /*
1270              * If the transfer to the new method method call worked, a new walk
1271              * state was created -- get it
1272              */
1273             WalkState = AcpiDsGetCurrentWalkState (Thread);
1274             continue;
1275         }
1276         else if (Status == AE_CTRL_TERMINATE)
1277         {
1278             Status = AE_OK;
1279         }
1280         else if (Status != AE_OK)
1281         {
1282             ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1283                 WalkState->MethodNode, NULL, Status);
1284         }
1285
1286         /* We are done with this walk, move on to the parent if any */
1287
1288         WalkState = AcpiDsPopWalkState (Thread);
1289
1290         /* Reset the current scope to the beginning of scope stack */
1291
1292         AcpiDsScopeStackClear (WalkState);
1293
1294         /*
1295          * If we just returned from the execution of a control method,
1296          * there's lots of cleanup to do
1297          */
1298         if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE)
1299         {
1300             TerminateStatus = AcpiDsTerminateControlMethod (WalkState);
1301             if (ACPI_FAILURE (TerminateStatus))
1302             {
1303                 ACPI_REPORT_ERROR ((
1304                     "Could not terminate control method properly\n"));
1305
1306                 /* Ignore error and continue */
1307             }
1308         }
1309
1310         /* Delete this walk state and all linked control states */
1311
1312         AcpiPsCleanupScope (&WalkState->ParserState);
1313
1314         PreviousWalkState = WalkState;
1315
1316         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "ReturnValue=%p, State=%p\n",
1317             WalkState->ReturnDesc, WalkState));
1318
1319         /* Check if we have restarted a preempted walk */
1320
1321         WalkState = AcpiDsGetCurrentWalkState (Thread);
1322         if (WalkState)
1323         {
1324             if (ACPI_SUCCESS (Status))
1325             {
1326                 /*
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
1330                  */
1331                 Status = AcpiDsRestartControlMethod (WalkState,
1332                             PreviousWalkState->ReturnDesc);
1333                 if (ACPI_SUCCESS (Status))
1334                 {
1335                     WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
1336                 }
1337             }
1338             else
1339             {
1340                 /* On error, delete any return object */
1341
1342                 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1343             }
1344         }
1345
1346         /*
1347          * Just completed a 1st-level method, save the final internal return
1348          * value (if any)
1349          */
1350         else if (PreviousWalkState->CallerReturnDesc)
1351         {
1352             *(PreviousWalkState->CallerReturnDesc) = PreviousWalkState->ReturnDesc; /* NULL if no return value */
1353         }
1354         else if (PreviousWalkState->ReturnDesc)
1355         {
1356             /* Caller doesn't want it, must delete it */
1357
1358             AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1359         }
1360
1361         AcpiDsDeleteWalkState (PreviousWalkState);
1362     }
1363
1364     /* Normal exit */
1365
1366     AcpiExReleaseAllMutexes (Thread);
1367     AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
1368     AcpiGbl_CurrentWalkList = PrevWalkList;
1369     return_ACPI_STATUS (Status);
1370 }
1371
1372