1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
48 #include "acconvert.h"
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME ("psobject")
54 /* Local prototypes */
58 ACPI_WALK_STATE
*WalkState
);
61 /*******************************************************************************
63 * FUNCTION: AcpiPsGetAmlOpcode
65 * PARAMETERS: WalkState - Current state
69 * DESCRIPTION: Extract the next AML opcode from the input stream.
71 ******************************************************************************/
75 ACPI_WALK_STATE
*WalkState
)
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
90 * 3) An unknown/invalid opcode
92 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (WalkState
->Opcode
);
94 switch (WalkState
->OpInfo
->Class
)
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
;
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",
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.
128 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
130 (UINT32
) (AmlOffset
+ sizeof (ACPI_TABLE_HEADER
)));
132 /* Dump the context surrounding the invalid opcode */
134 AcpiUtDumpBuffer (((UINT8
*) WalkState
->ParserState
.Aml
- 16),
136 (AmlOffset
+ sizeof (ACPI_TABLE_HEADER
) - 16));
137 AcpiOsPrintf (" */\n");
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
);
153 /* Found opcode info, this is a normal opcode */
155 WalkState
->ParserState
.Aml
+=
156 AcpiPsGetOpcodeSize (WalkState
->Opcode
);
157 WalkState
->ArgTypes
= WalkState
->OpInfo
->ParseArgs
;
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)
176 * DESCRIPTION: Parse a named Op
178 ******************************************************************************/
182 ACPI_WALK_STATE
*WalkState
,
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
;
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
);
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
;
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
327 * DESCRIPTION: Get Op from AML
329 ******************************************************************************/
333 ACPI_WALK_STATE
*WalkState
,
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
;
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
);
360 return_ACPI_STATUS (AE_NO_MEMORY
);
363 if (WalkState
->OpInfo
->Flags
& AML_NAMED
)
365 Status
= AcpiPsBuildNamedOp (WalkState
, AmlOpStart
, Op
, &NamedOp
);
367 if (ACPI_FAILURE (Status
))
369 return_ACPI_STATUS (Status
);
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
);
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
450 * Status - Parse status before complete Op
454 * DESCRIPTION: Complete Op
456 ******************************************************************************/
460 ACPI_WALK_STATE
*WalkState
,
461 ACPI_PARSE_OBJECT
**Op
,
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
);
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
);
501 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
502 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
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
);
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 */
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
);
552 case AE_CTRL_TERMINATE
:
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
);
574 return_ACPI_STATUS (AE_OK
);
576 default: /* All other non-AE_OK status */
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
);
597 * TBD: Cleanup parse ops on error
601 AcpiPsPopScope (ParserState
, Op
,
602 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
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
));
623 return_ACPI_STATUS (AE_OK
);
627 /*******************************************************************************
629 * FUNCTION: AcpiPsCompleteFinalOp
631 * PARAMETERS: WalkState - Current state
633 * Status - Current parse status before complete last
638 * DESCRIPTION: Complete last Op.
640 ******************************************************************************/
643 AcpiPsCompleteFinalOp (
644 ACPI_WALK_STATE
*WalkState
,
645 ACPI_PARSE_OBJECT
*Op
,
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
));
665 if (WalkState
->AscendingCallback
!= NULL
)
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
)
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
);
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
);
727 return_ACPI_STATUS (Status
);