Sync ACPICA with Intel's version 20170224.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / parser / psobject.c
blobbcc2aa2686dd48e179291bed25de3c66b2273875
1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2017, 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.
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "amlcode.h"
48 #include "acconvert.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)
77 UINT32 AmlOffset;
80 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
83 WalkState->Aml = WalkState->ParserState.Aml;
84 WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
87 * First cut to determine what we have found:
88 * 1) A valid AML opcode
89 * 2) A name string
90 * 3) An unknown/invalid opcode
92 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
94 switch (WalkState->OpInfo->Class)
96 case AML_CLASS_ASCII:
97 case AML_CLASS_PREFIX:
99 * Starts with a valid prefix or ASCII char, this is a name
100 * string. Convert the bare name string to a namepath.
102 WalkState->Opcode = AML_INT_NAMEPATH_OP;
103 WalkState->ArgTypes = ARGP_NAMESTRING;
104 break;
106 case AML_CLASS_UNKNOWN:
108 /* The opcode is unrecognized. Complain and skip unknown opcodes */
110 if (WalkState->PassNumber == 2)
112 AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
113 WalkState->ParserState.AmlStart);
115 ACPI_ERROR ((AE_INFO,
116 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
117 WalkState->Opcode,
118 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER))));
120 ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
122 #ifdef ACPI_ASL_COMPILER
124 * This is executed for the disassembler only. Output goes
125 * to the disassembled ASL output file.
127 AcpiOsPrintf (
128 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
129 WalkState->Opcode,
130 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER)));
132 /* Dump the context surrounding the invalid opcode */
134 AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
135 48, DB_BYTE_DISPLAY,
136 (AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
137 AcpiOsPrintf (" */\n");
138 #endif
141 /* Increment past one-byte or two-byte opcode */
143 WalkState->ParserState.Aml++;
144 if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
146 WalkState->ParserState.Aml++;
149 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
151 default:
153 /* Found opcode info, this is a normal opcode */
155 WalkState->ParserState.Aml +=
156 AcpiPsGetOpcodeSize (WalkState->Opcode);
157 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
158 break;
161 return_ACPI_STATUS (AE_OK);
165 /*******************************************************************************
167 * FUNCTION: AcpiPsBuildNamedOp
169 * PARAMETERS: WalkState - Current state
170 * AmlOpStart - Begin of named Op in AML
171 * UnnamedOp - Early Op (not a named Op)
172 * Op - Returned Op
174 * RETURN: Status
176 * DESCRIPTION: Parse a named Op
178 ******************************************************************************/
180 ACPI_STATUS
181 AcpiPsBuildNamedOp (
182 ACPI_WALK_STATE *WalkState,
183 UINT8 *AmlOpStart,
184 ACPI_PARSE_OBJECT *UnnamedOp,
185 ACPI_PARSE_OBJECT **Op)
187 ACPI_STATUS Status = AE_OK;
188 ACPI_PARSE_OBJECT *Arg = NULL;
191 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
194 UnnamedOp->Common.Value.Arg = NULL;
195 UnnamedOp->Common.ArgListLength = 0;
196 UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
199 * Get and append arguments until we find the node that contains
200 * the name (the type ARGP_NAME).
202 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
203 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
205 ASL_CV_CAPTURE_COMMENTS (WalkState);
206 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
207 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
208 if (ACPI_FAILURE (Status))
210 return_ACPI_STATUS (Status);
213 AcpiPsAppendArg (UnnamedOp, Arg);
214 INCREMENT_ARG_LIST (WalkState->ArgTypes);
217 /* are there any inline comments associated with the NameSeg?? If so, save this. */
219 ASL_CV_CAPTURE_COMMENTS (WalkState);
221 #ifdef ACPI_ASL_COMPILER
222 if (AcpiGbl_CurrentInlineComment != NULL)
224 UnnamedOp->Common.NameComment = AcpiGbl_CurrentInlineComment;
225 AcpiGbl_CurrentInlineComment = NULL;
227 #endif
230 * Make sure that we found a NAME and didn't run out of arguments
232 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
234 return_ACPI_STATUS (AE_AML_NO_OPERAND);
237 /* We know that this arg is a name, move to next arg */
239 INCREMENT_ARG_LIST (WalkState->ArgTypes);
242 * Find the object. This will either insert the object into
243 * the namespace or simply look it up
245 WalkState->Op = NULL;
247 Status = WalkState->DescendingCallback (WalkState, Op);
248 if (ACPI_FAILURE (Status))
250 if (Status != AE_CTRL_TERMINATE)
252 ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
254 return_ACPI_STATUS (Status);
257 if (!*Op)
259 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
262 Status = AcpiPsNextParseState (WalkState, *Op, Status);
263 if (ACPI_FAILURE (Status))
265 if (Status == AE_CTRL_PENDING)
267 Status = AE_CTRL_PARSE_PENDING;
269 return_ACPI_STATUS (Status);
272 AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
274 #ifdef ACPI_ASL_COMPILER
276 /* save any comments that might be associated with UnnamedOp. */
278 (*Op)->Common.InlineComment = UnnamedOp->Common.InlineComment;
279 (*Op)->Common.EndNodeComment = UnnamedOp->Common.EndNodeComment;
280 (*Op)->Common.CloseBraceComment = UnnamedOp->Common.CloseBraceComment;
281 (*Op)->Common.NameComment = UnnamedOp->Common.NameComment;
282 (*Op)->Common.CommentList = UnnamedOp->Common.CommentList;
283 (*Op)->Common.EndBlkComment = UnnamedOp->Common.EndBlkComment;
284 (*Op)->Common.CvFilename = UnnamedOp->Common.CvFilename;
285 (*Op)->Common.CvParentFilename = UnnamedOp->Common.CvParentFilename;
286 (*Op)->Named.Aml = UnnamedOp->Common.Aml;
288 UnnamedOp->Common.InlineComment = NULL;
289 UnnamedOp->Common.EndNodeComment = NULL;
290 UnnamedOp->Common.CloseBraceComment = NULL;
291 UnnamedOp->Common.NameComment = NULL;
292 UnnamedOp->Common.CommentList = NULL;
293 UnnamedOp->Common.EndBlkComment = NULL;
294 #endif
296 if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
297 (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
300 * Defer final parsing of an OperationRegion body, because we don't
301 * have enough info in the first pass to parse it correctly (i.e.,
302 * there may be method calls within the TermArg elements of the body.)
304 * However, we must continue parsing because the opregion is not a
305 * standalone package -- we don't know where the end is at this point.
307 * (Length is unknown until parse of the body complete)
309 (*Op)->Named.Data = AmlOpStart;
310 (*Op)->Named.Length = 0;
313 return_ACPI_STATUS (AE_OK);
317 /*******************************************************************************
319 * FUNCTION: AcpiPsCreateOp
321 * PARAMETERS: WalkState - Current state
322 * AmlOpStart - Op start in AML
323 * NewOp - Returned Op
325 * RETURN: Status
327 * DESCRIPTION: Get Op from AML
329 ******************************************************************************/
331 ACPI_STATUS
332 AcpiPsCreateOp (
333 ACPI_WALK_STATE *WalkState,
334 UINT8 *AmlOpStart,
335 ACPI_PARSE_OBJECT **NewOp)
337 ACPI_STATUS Status = AE_OK;
338 ACPI_PARSE_OBJECT *Op;
339 ACPI_PARSE_OBJECT *NamedOp = NULL;
340 ACPI_PARSE_OBJECT *ParentScope;
341 UINT8 ArgumentCount;
342 const ACPI_OPCODE_INFO *OpInfo;
345 ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
348 Status = AcpiPsGetAmlOpcode (WalkState);
349 if (Status == AE_CTRL_PARSE_CONTINUE)
351 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
354 /* Create Op structure and append to parent's argument list */
356 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
357 Op = AcpiPsAllocOp (WalkState->Opcode, AmlOpStart);
358 if (!Op)
360 return_ACPI_STATUS (AE_NO_MEMORY);
363 if (WalkState->OpInfo->Flags & AML_NAMED)
365 Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
366 AcpiPsFreeOp (Op);
367 if (ACPI_FAILURE (Status))
369 return_ACPI_STATUS (Status);
372 *NewOp = NamedOp;
373 return_ACPI_STATUS (AE_OK);
376 /* Not a named opcode, just allocate Op and append to parent */
378 if (WalkState->OpInfo->Flags & AML_CREATE)
381 * Backup to beginning of CreateXXXfield declaration
382 * BodyLength is unknown until we parse the body
384 Op->Named.Data = AmlOpStart;
385 Op->Named.Length = 0;
388 if (WalkState->Opcode == AML_BANK_FIELD_OP)
391 * Backup to beginning of BankField declaration
392 * BodyLength is unknown until we parse the body
394 Op->Named.Data = AmlOpStart;
395 Op->Named.Length = 0;
398 ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
399 AcpiPsAppendArg (ParentScope, Op);
401 if (ParentScope)
403 OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
404 if (OpInfo->Flags & AML_HAS_TARGET)
406 ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
407 if (ParentScope->Common.ArgListLength > ArgumentCount)
409 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
414 * Special case for both Increment() and Decrement(), where
415 * the lone argument is both a source and a target.
417 else if ((ParentScope->Common.AmlOpcode == AML_INCREMENT_OP) ||
418 (ParentScope->Common.AmlOpcode == AML_DECREMENT_OP))
420 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
424 if (WalkState->DescendingCallback != NULL)
427 * Find the object. This will either insert the object into
428 * the namespace or simply look it up
430 WalkState->Op = *NewOp = Op;
432 Status = WalkState->DescendingCallback (WalkState, &Op);
433 Status = AcpiPsNextParseState (WalkState, Op, Status);
434 if (Status == AE_CTRL_PENDING)
436 Status = AE_CTRL_PARSE_PENDING;
440 return_ACPI_STATUS (Status);
444 /*******************************************************************************
446 * FUNCTION: AcpiPsCompleteOp
448 * PARAMETERS: WalkState - Current state
449 * Op - Returned Op
450 * Status - Parse status before complete Op
452 * RETURN: Status
454 * DESCRIPTION: Complete Op
456 ******************************************************************************/
458 ACPI_STATUS
459 AcpiPsCompleteOp (
460 ACPI_WALK_STATE *WalkState,
461 ACPI_PARSE_OBJECT **Op,
462 ACPI_STATUS Status)
464 ACPI_STATUS Status2;
467 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
471 * Finished one argument of the containing scope
473 WalkState->ParserState.Scope->ParseScope.ArgCount--;
475 /* Close this Op (will result in parse subtree deletion) */
477 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
478 if (ACPI_FAILURE (Status2))
480 return_ACPI_STATUS (Status2);
483 *Op = NULL;
485 switch (Status)
487 case AE_OK:
489 break;
491 case AE_CTRL_TRANSFER:
493 /* We are about to transfer to a called method */
495 WalkState->PrevOp = NULL;
496 WalkState->PrevArgTypes = WalkState->ArgTypes;
497 return_ACPI_STATUS (Status);
499 case AE_CTRL_END:
501 AcpiPsPopScope (&(WalkState->ParserState), Op,
502 &WalkState->ArgTypes, &WalkState->ArgCount);
504 if (*Op)
506 WalkState->Op = *Op;
507 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
508 WalkState->Opcode = (*Op)->Common.AmlOpcode;
510 Status = WalkState->AscendingCallback (WalkState);
511 Status = AcpiPsNextParseState (WalkState, *Op, Status);
513 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
514 if (ACPI_FAILURE (Status2))
516 return_ACPI_STATUS (Status2);
520 Status = AE_OK;
521 break;
523 case AE_CTRL_BREAK:
524 case AE_CTRL_CONTINUE:
526 /* Pop off scopes until we find the While */
528 while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
530 AcpiPsPopScope (&(WalkState->ParserState), Op,
531 &WalkState->ArgTypes, &WalkState->ArgCount);
534 /* Close this iteration of the While loop */
536 WalkState->Op = *Op;
537 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
538 WalkState->Opcode = (*Op)->Common.AmlOpcode;
540 Status = WalkState->AscendingCallback (WalkState);
541 Status = AcpiPsNextParseState (WalkState, *Op, Status);
543 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
544 if (ACPI_FAILURE (Status2))
546 return_ACPI_STATUS (Status2);
549 Status = AE_OK;
550 break;
552 case AE_CTRL_TERMINATE:
554 /* Clean up */
557 if (*Op)
559 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
560 if (ACPI_FAILURE (Status2))
562 return_ACPI_STATUS (Status2);
565 AcpiUtDeleteGenericState (
566 AcpiUtPopGenericState (&WalkState->ControlState));
569 AcpiPsPopScope (&(WalkState->ParserState), Op,
570 &WalkState->ArgTypes, &WalkState->ArgCount);
572 } while (*Op);
574 return_ACPI_STATUS (AE_OK);
576 default: /* All other non-AE_OK status */
580 if (*Op)
582 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
583 if (ACPI_FAILURE (Status2))
585 return_ACPI_STATUS (Status2);
589 AcpiPsPopScope (&(WalkState->ParserState), Op,
590 &WalkState->ArgTypes, &WalkState->ArgCount);
592 } while (*Op);
595 #if 0
597 * TBD: Cleanup parse ops on error
599 if (*Op == NULL)
601 AcpiPsPopScope (ParserState, Op,
602 &WalkState->ArgTypes, &WalkState->ArgCount);
604 #endif
605 WalkState->PrevOp = NULL;
606 WalkState->PrevArgTypes = WalkState->ArgTypes;
607 return_ACPI_STATUS (Status);
610 /* This scope complete? */
612 if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
614 AcpiPsPopScope (&(WalkState->ParserState), Op,
615 &WalkState->ArgTypes, &WalkState->ArgCount);
616 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
618 else
620 *Op = NULL;
623 return_ACPI_STATUS (AE_OK);
627 /*******************************************************************************
629 * FUNCTION: AcpiPsCompleteFinalOp
631 * PARAMETERS: WalkState - Current state
632 * Op - Current Op
633 * Status - Current parse status before complete last
634 * Op
636 * RETURN: Status
638 * DESCRIPTION: Complete last Op.
640 ******************************************************************************/
642 ACPI_STATUS
643 AcpiPsCompleteFinalOp (
644 ACPI_WALK_STATE *WalkState,
645 ACPI_PARSE_OBJECT *Op,
646 ACPI_STATUS Status)
648 ACPI_STATUS Status2;
651 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
655 * Complete the last Op (if not completed), and clear the scope stack.
656 * It is easily possible to end an AML "package" with an unbounded number
657 * of open scopes (such as when several ASL blocks are closed with
658 * sequential closing braces). We want to terminate each one cleanly.
660 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
663 if (Op)
665 if (WalkState->AscendingCallback != NULL)
667 WalkState->Op = Op;
668 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
669 WalkState->Opcode = Op->Common.AmlOpcode;
671 Status = WalkState->AscendingCallback (WalkState);
672 Status = AcpiPsNextParseState (WalkState, Op, Status);
673 if (Status == AE_CTRL_PENDING)
675 Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
676 if (ACPI_FAILURE (Status))
678 return_ACPI_STATUS (Status);
682 if (Status == AE_CTRL_TERMINATE)
684 Status = AE_OK;
686 /* Clean up */
689 if (Op)
691 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
692 if (ACPI_FAILURE (Status2))
694 return_ACPI_STATUS (Status2);
698 AcpiPsPopScope (&(WalkState->ParserState), &Op,
699 &WalkState->ArgTypes, &WalkState->ArgCount);
701 } while (Op);
703 return_ACPI_STATUS (Status);
706 else if (ACPI_FAILURE (Status))
708 /* First error is most important */
710 (void) AcpiPsCompleteThisOp (WalkState, Op);
711 return_ACPI_STATUS (Status);
715 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
716 if (ACPI_FAILURE (Status2))
718 return_ACPI_STATUS (Status2);
722 AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
723 &WalkState->ArgCount);
725 } while (Op);
727 return_ACPI_STATUS (Status);