Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / parser / psobject.c
blobf507e8eb260073c27f7d65852f3a98b2611cfe0c
1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include "acpi.h"
46 #include "accommon.h"
47 #include "acparser.h"
48 #include "amlcode.h"
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME ("psobject")
54 /* Local prototypes */
56 static ACPI_STATUS
57 AcpiPsGetAmlOpcode (
58 ACPI_WALK_STATE *WalkState);
61 /*******************************************************************************
63 * FUNCTION: AcpiPsGetAmlOpcode
65 * PARAMETERS: WalkState - Current state
67 * RETURN: Status
69 * DESCRIPTION: Extract the next AML opcode from the input stream.
71 ******************************************************************************/
73 static ACPI_STATUS
74 AcpiPsGetAmlOpcode (
75 ACPI_WALK_STATE *WalkState)
78 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
81 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
82 WalkState->ParserState.AmlStart);
83 WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
86 * First cut to determine what we have found:
87 * 1) A valid AML opcode
88 * 2) A name string
89 * 3) An unknown/invalid opcode
91 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
93 switch (WalkState->OpInfo->Class)
95 case AML_CLASS_ASCII:
96 case AML_CLASS_PREFIX:
98 * Starts with a valid prefix or ASCII char, this is a name
99 * string. Convert the bare name string to a namepath.
101 WalkState->Opcode = AML_INT_NAMEPATH_OP;
102 WalkState->ArgTypes = ARGP_NAMESTRING;
103 break;
105 case AML_CLASS_UNKNOWN:
107 /* The opcode is unrecognized. Complain and skip unknown opcodes */
109 if (WalkState->PassNumber == 2)
111 ACPI_ERROR ((AE_INFO,
112 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
113 WalkState->Opcode,
114 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER))));
116 ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
118 #ifdef ACPI_ASL_COMPILER
120 * This is executed for the disassembler only. Output goes
121 * to the disassembled ASL output file.
123 AcpiOsPrintf (
124 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
125 WalkState->Opcode,
126 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER)));
128 /* Dump the context surrounding the invalid opcode */
130 AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
131 48, DB_BYTE_DISPLAY,
132 (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
133 AcpiOsPrintf (" */\n");
134 #endif
137 /* Increment past one-byte or two-byte opcode */
139 WalkState->ParserState.Aml++;
140 if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
142 WalkState->ParserState.Aml++;
145 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
147 default:
149 /* Found opcode info, this is a normal opcode */
151 WalkState->ParserState.Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
152 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
153 break;
156 return_ACPI_STATUS (AE_OK);
160 /*******************************************************************************
162 * FUNCTION: AcpiPsBuildNamedOp
164 * PARAMETERS: WalkState - Current state
165 * AmlOpStart - Begin of named Op in AML
166 * UnnamedOp - Early Op (not a named Op)
167 * Op - Returned Op
169 * RETURN: Status
171 * DESCRIPTION: Parse a named Op
173 ******************************************************************************/
175 ACPI_STATUS
176 AcpiPsBuildNamedOp (
177 ACPI_WALK_STATE *WalkState,
178 UINT8 *AmlOpStart,
179 ACPI_PARSE_OBJECT *UnnamedOp,
180 ACPI_PARSE_OBJECT **Op)
182 ACPI_STATUS Status = AE_OK;
183 ACPI_PARSE_OBJECT *Arg = NULL;
186 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
189 UnnamedOp->Common.Value.Arg = NULL;
190 UnnamedOp->Common.ArgListLength = 0;
191 UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
194 * Get and append arguments until we find the node that contains
195 * the name (the type ARGP_NAME).
197 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
198 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
200 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
201 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
202 if (ACPI_FAILURE (Status))
204 return_ACPI_STATUS (Status);
207 AcpiPsAppendArg (UnnamedOp, Arg);
208 INCREMENT_ARG_LIST (WalkState->ArgTypes);
212 * Make sure that we found a NAME and didn't run out of arguments
214 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
216 return_ACPI_STATUS (AE_AML_NO_OPERAND);
219 /* We know that this arg is a name, move to next arg */
221 INCREMENT_ARG_LIST (WalkState->ArgTypes);
224 * Find the object. This will either insert the object into
225 * the namespace or simply look it up
227 WalkState->Op = NULL;
229 Status = WalkState->DescendingCallback (WalkState, Op);
230 if (ACPI_FAILURE (Status))
232 ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
233 return_ACPI_STATUS (Status);
236 if (!*Op)
238 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
241 Status = AcpiPsNextParseState (WalkState, *Op, Status);
242 if (ACPI_FAILURE (Status))
244 if (Status == AE_CTRL_PENDING)
246 return_ACPI_STATUS (AE_CTRL_PARSE_PENDING);
248 return_ACPI_STATUS (Status);
251 AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
253 if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
254 (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
257 * Defer final parsing of an OperationRegion body, because we don't
258 * have enough info in the first pass to parse it correctly (i.e.,
259 * there may be method calls within the TermArg elements of the body.)
261 * However, we must continue parsing because the opregion is not a
262 * standalone package -- we don't know where the end is at this point.
264 * (Length is unknown until parse of the body complete)
266 (*Op)->Named.Data = AmlOpStart;
267 (*Op)->Named.Length = 0;
270 return_ACPI_STATUS (AE_OK);
274 /*******************************************************************************
276 * FUNCTION: AcpiPsCreateOp
278 * PARAMETERS: WalkState - Current state
279 * AmlOpStart - Op start in AML
280 * NewOp - Returned Op
282 * RETURN: Status
284 * DESCRIPTION: Get Op from AML
286 ******************************************************************************/
288 ACPI_STATUS
289 AcpiPsCreateOp (
290 ACPI_WALK_STATE *WalkState,
291 UINT8 *AmlOpStart,
292 ACPI_PARSE_OBJECT **NewOp)
294 ACPI_STATUS Status = AE_OK;
295 ACPI_PARSE_OBJECT *Op;
296 ACPI_PARSE_OBJECT *NamedOp = NULL;
297 ACPI_PARSE_OBJECT *ParentScope;
298 UINT8 ArgumentCount;
299 const ACPI_OPCODE_INFO *OpInfo;
302 ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
305 Status = AcpiPsGetAmlOpcode (WalkState);
306 if (Status == AE_CTRL_PARSE_CONTINUE)
308 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
311 /* Create Op structure and append to parent's argument list */
313 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
314 Op = AcpiPsAllocOp (WalkState->Opcode);
315 if (!Op)
317 return_ACPI_STATUS (AE_NO_MEMORY);
320 if (WalkState->OpInfo->Flags & AML_NAMED)
322 Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
323 AcpiPsFreeOp (Op);
324 if (ACPI_FAILURE (Status))
326 return_ACPI_STATUS (Status);
329 *NewOp = NamedOp;
330 return_ACPI_STATUS (AE_OK);
333 /* Not a named opcode, just allocate Op and append to parent */
335 if (WalkState->OpInfo->Flags & AML_CREATE)
338 * Backup to beginning of CreateXXXfield declaration
339 * BodyLength is unknown until we parse the body
341 Op->Named.Data = AmlOpStart;
342 Op->Named.Length = 0;
345 if (WalkState->Opcode == AML_BANK_FIELD_OP)
348 * Backup to beginning of BankField declaration
349 * BodyLength is unknown until we parse the body
351 Op->Named.Data = AmlOpStart;
352 Op->Named.Length = 0;
355 ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
356 AcpiPsAppendArg (ParentScope, Op);
358 if (ParentScope)
360 OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
361 if (OpInfo->Flags & AML_HAS_TARGET)
363 ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
364 if (ParentScope->Common.ArgListLength > ArgumentCount)
366 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
369 else if (ParentScope->Common.AmlOpcode == AML_INCREMENT_OP)
371 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
375 if (WalkState->DescendingCallback != NULL)
378 * Find the object. This will either insert the object into
379 * the namespace or simply look it up
381 WalkState->Op = *NewOp = Op;
383 Status = WalkState->DescendingCallback (WalkState, &Op);
384 Status = AcpiPsNextParseState (WalkState, Op, Status);
385 if (Status == AE_CTRL_PENDING)
387 Status = AE_CTRL_PARSE_PENDING;
391 return_ACPI_STATUS (Status);
395 /*******************************************************************************
397 * FUNCTION: AcpiPsCompleteOp
399 * PARAMETERS: WalkState - Current state
400 * Op - Returned Op
401 * Status - Parse status before complete Op
403 * RETURN: Status
405 * DESCRIPTION: Complete Op
407 ******************************************************************************/
409 ACPI_STATUS
410 AcpiPsCompleteOp (
411 ACPI_WALK_STATE *WalkState,
412 ACPI_PARSE_OBJECT **Op,
413 ACPI_STATUS Status)
415 ACPI_STATUS Status2;
418 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
422 * Finished one argument of the containing scope
424 WalkState->ParserState.Scope->ParseScope.ArgCount--;
426 /* Close this Op (will result in parse subtree deletion) */
428 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
429 if (ACPI_FAILURE (Status2))
431 return_ACPI_STATUS (Status2);
434 *Op = NULL;
436 switch (Status)
438 case AE_OK:
440 break;
442 case AE_CTRL_TRANSFER:
444 /* We are about to transfer to a called method */
446 WalkState->PrevOp = NULL;
447 WalkState->PrevArgTypes = WalkState->ArgTypes;
448 return_ACPI_STATUS (Status);
450 case AE_CTRL_END:
452 AcpiPsPopScope (&(WalkState->ParserState), Op,
453 &WalkState->ArgTypes, &WalkState->ArgCount);
455 if (*Op)
457 WalkState->Op = *Op;
458 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
459 WalkState->Opcode = (*Op)->Common.AmlOpcode;
461 Status = WalkState->AscendingCallback (WalkState);
462 Status = AcpiPsNextParseState (WalkState, *Op, Status);
464 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
465 if (ACPI_FAILURE (Status2))
467 return_ACPI_STATUS (Status2);
471 Status = AE_OK;
472 break;
474 case AE_CTRL_BREAK:
475 case AE_CTRL_CONTINUE:
477 /* Pop off scopes until we find the While */
479 while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
481 AcpiPsPopScope (&(WalkState->ParserState), Op,
482 &WalkState->ArgTypes, &WalkState->ArgCount);
485 /* Close this iteration of the While loop */
487 WalkState->Op = *Op;
488 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
489 WalkState->Opcode = (*Op)->Common.AmlOpcode;
491 Status = WalkState->AscendingCallback (WalkState);
492 Status = AcpiPsNextParseState (WalkState, *Op, Status);
494 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
495 if (ACPI_FAILURE (Status2))
497 return_ACPI_STATUS (Status2);
500 Status = AE_OK;
501 break;
503 case AE_CTRL_TERMINATE:
505 /* Clean up */
508 if (*Op)
510 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
511 if (ACPI_FAILURE (Status2))
513 return_ACPI_STATUS (Status2);
516 AcpiUtDeleteGenericState (
517 AcpiUtPopGenericState (&WalkState->ControlState));
520 AcpiPsPopScope (&(WalkState->ParserState), Op,
521 &WalkState->ArgTypes, &WalkState->ArgCount);
523 } while (*Op);
525 return_ACPI_STATUS (AE_OK);
527 default: /* All other non-AE_OK status */
531 if (*Op)
533 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
534 if (ACPI_FAILURE (Status2))
536 return_ACPI_STATUS (Status2);
540 AcpiPsPopScope (&(WalkState->ParserState), Op,
541 &WalkState->ArgTypes, &WalkState->ArgCount);
543 } while (*Op);
546 #if 0
548 * TBD: Cleanup parse ops on error
550 if (*Op == NULL)
552 AcpiPsPopScope (ParserState, Op,
553 &WalkState->ArgTypes, &WalkState->ArgCount);
555 #endif
556 WalkState->PrevOp = NULL;
557 WalkState->PrevArgTypes = WalkState->ArgTypes;
558 return_ACPI_STATUS (Status);
561 /* This scope complete? */
563 if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
565 AcpiPsPopScope (&(WalkState->ParserState), Op,
566 &WalkState->ArgTypes, &WalkState->ArgCount);
567 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
569 else
571 *Op = NULL;
574 return_ACPI_STATUS (AE_OK);
578 /*******************************************************************************
580 * FUNCTION: AcpiPsCompleteFinalOp
582 * PARAMETERS: WalkState - Current state
583 * Op - Current Op
584 * Status - Current parse status before complete last
585 * Op
587 * RETURN: Status
589 * DESCRIPTION: Complete last Op.
591 ******************************************************************************/
593 ACPI_STATUS
594 AcpiPsCompleteFinalOp (
595 ACPI_WALK_STATE *WalkState,
596 ACPI_PARSE_OBJECT *Op,
597 ACPI_STATUS Status)
599 ACPI_STATUS Status2;
602 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
606 * Complete the last Op (if not completed), and clear the scope stack.
607 * It is easily possible to end an AML "package" with an unbounded number
608 * of open scopes (such as when several ASL blocks are closed with
609 * sequential closing braces). We want to terminate each one cleanly.
611 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
614 if (Op)
616 if (WalkState->AscendingCallback != NULL)
618 WalkState->Op = Op;
619 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
620 WalkState->Opcode = Op->Common.AmlOpcode;
622 Status = WalkState->AscendingCallback (WalkState);
623 Status = AcpiPsNextParseState (WalkState, Op, Status);
624 if (Status == AE_CTRL_PENDING)
626 Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
627 if (ACPI_FAILURE (Status))
629 return_ACPI_STATUS (Status);
633 if (Status == AE_CTRL_TERMINATE)
635 Status = AE_OK;
637 /* Clean up */
640 if (Op)
642 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
643 if (ACPI_FAILURE (Status2))
645 return_ACPI_STATUS (Status2);
649 AcpiPsPopScope (&(WalkState->ParserState), &Op,
650 &WalkState->ArgTypes, &WalkState->ArgCount);
652 } while (Op);
654 return_ACPI_STATUS (Status);
657 else if (ACPI_FAILURE (Status))
659 /* First error is most important */
661 (void) AcpiPsCompleteThisOp (WalkState, Op);
662 return_ACPI_STATUS (Status);
666 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
667 if (ACPI_FAILURE (Status2))
669 return_ACPI_STATUS (Status2);
673 AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
674 &WalkState->ArgCount);
676 } while (Op);
678 return_ACPI_STATUS (Status);