1 /******************************************************************************
3 * Module Name: aslanalyze.c - Support functions for parse tree walks
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include "aslcompiler.y.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("aslanalyze")
53 /* Local Prototypes */
57 ACPI_PARSE_OBJECT *Op,
62 /*******************************************************************************
64 * FUNCTION: AnIsInternalMethod
66 * PARAMETERS: Op - Current op
70 * DESCRIPTION: Check for an internal control method.
72 ******************************************************************************/
76 ACPI_PARSE_OBJECT *Op)
79 if ((!strcmp (Op->Asl.ExternalName, "\\_OSI")) ||
80 (!strcmp (Op->Asl.ExternalName, "_OSI")))
89 /*******************************************************************************
91 * FUNCTION: AnGetInternalMethodReturnType
93 * PARAMETERS: Op - Current op
97 * DESCRIPTION: Get the return type of an internal method
99 ******************************************************************************/
102 AnGetInternalMethodReturnType (
103 ACPI_PARSE_OBJECT *Op)
106 if ((!strcmp (Op->Asl.ExternalName, "\\_OSI")) ||
107 (!strcmp (Op->Asl.ExternalName, "_OSI")))
109 return (ACPI_BTYPE_STRING);
116 /*******************************************************************************
118 * FUNCTION: AnCheckId
120 * PARAMETERS: Op - Current parse op
125 * DESCRIPTION: Perform various checks on _HID and _CID strings. Only limited
126 * checks can be performed on _CID strings.
128 ******************************************************************************/
132 ACPI_PARSE_OBJECT *Op,
139 /* Only care about string versions of _HID/_CID (integers are legal) */
141 if (Op->Asl.ParseOpcode != PARSEOP_STRING_LITERAL)
146 /* For both _HID and _CID, the string must be non-null */
148 Length = strlen (Op->Asl.Value.String);
151 AslError (ASL_ERROR, ASL_MSG_NULL_STRING, Op, NULL);
156 * One of the things we want to catch here is the use of a leading
157 * asterisk in the string -- an odd construct that certain platform
158 * manufacturers are fond of. Technically, a leading asterisk is OK
159 * for _CID, but a valid use of this has not been seen.
161 if (*Op->Asl.Value.String == '*')
163 AslError (ASL_ERROR, ASL_MSG_LEADING_ASTERISK,
164 Op, Op->Asl.Value.String);
168 /* _CID strings are bus-specific, no more checks can be performed */
170 if (Type == ASL_TYPE_CID)
175 /* For _HID, all characters must be alphanumeric */
177 for (i = 0; Op->Asl.Value.String[i]; i++)
179 if (!isalnum ((int) Op->Asl.Value.String[i]))
181 AslError (ASL_ERROR, ASL_MSG_ALPHANUMERIC_STRING,
182 Op, Op->Asl.Value.String);
188 * _HID String must be one of these forms:
190 * "AAA####" A is an uppercase letter and # is a hex digit
191 * "ACPI####" # is a hex digit
192 * "NNNN####" N is an uppercase letter or decimal digit (0-9)
193 * # is a hex digit (ACPI 5.0)
195 if ((Length < 7) || (Length > 8))
197 AslError (ASL_ERROR, ASL_MSG_HID_LENGTH,
198 Op, Op->Asl.Value.String);
202 /* _HID Length is valid (7 or 8), now check prefix (first 3 or 4 chars) */
206 /* AAA####: Ensure the alphabetic prefix is all uppercase */
208 for (i = 0; i < 3; i++)
210 if (!isupper ((int) Op->Asl.Value.String[i]))
212 AslError (ASL_ERROR, ASL_MSG_UPPER_CASE,
213 Op, &Op->Asl.Value.String[i]);
218 else /* Length == 8 */
221 * ACPI#### or NNNN####:
222 * Ensure the prefix contains only uppercase alpha or decimal digits
224 for (i = 0; i < 4; i++)
226 if (!isupper ((int) Op->Asl.Value.String[i]) &&
227 !isdigit ((int) Op->Asl.Value.String[i]))
229 AslError (ASL_ERROR, ASL_MSG_HID_PREFIX,
230 Op, &Op->Asl.Value.String[i]);
236 /* Remaining characters (suffix) must be hex digits */
238 for (; i < Length; i++)
240 if (!isxdigit ((int) Op->Asl.Value.String[i]))
242 AslError (ASL_ERROR, ASL_MSG_HID_SUFFIX,
243 Op, &Op->Asl.Value.String[i]);
250 /*******************************************************************************
252 * FUNCTION: AnLastStatementIsReturn
254 * PARAMETERS: Op - A method parse node
256 * RETURN: TRUE if last statement is an ASL RETURN. False otherwise
258 * DESCRIPTION: Walk down the list of top level statements within a method
259 * to find the last one. Check if that last statement is in
260 * fact a RETURN statement.
262 ******************************************************************************/
265 AnLastStatementIsReturn (
266 ACPI_PARSE_OBJECT *Op)
268 ACPI_PARSE_OBJECT *Next;
271 /* Check if last statement is a return */
273 Next = ASL_GET_CHILD_NODE (Op);
276 if ((!Next->Asl.Next) &&
277 (Next->Asl.ParseOpcode == PARSEOP_RETURN))
282 Next = ASL_GET_PEER_NODE (Next);
289 /*******************************************************************************
291 * FUNCTION: AnCheckMethodReturnValue
293 * PARAMETERS: Op - Parent
294 * OpInfo - Parent info
295 * ArgOp - Method invocation op
296 * RequiredBtypes - What caller requires
297 * ThisNodeBtype - What this node returns (if anything)
301 * DESCRIPTION: Check a method invocation for 1) A return value and if it does
302 * in fact return a value, 2) check the type of the return value.
304 ******************************************************************************/
307 AnCheckMethodReturnValue (
308 ACPI_PARSE_OBJECT *Op,
309 const ACPI_OPCODE_INFO *OpInfo,
310 ACPI_PARSE_OBJECT *ArgOp,
311 UINT32 RequiredBtypes,
312 UINT32 ThisNodeBtype)
314 ACPI_PARSE_OBJECT *OwningOp;
315 ACPI_NAMESPACE_NODE *Node;
318 Node = ArgOp->Asl.Node;
322 /* No error message, this can happen and is OK */
327 /* Examine the parent op of this method */
330 if (OwningOp->Asl.CompileFlags & NODE_METHOD_NO_RETVAL)
332 /* Method NEVER returns a value */
334 AslError (ASL_ERROR, ASL_MSG_NO_RETVAL, Op, Op->Asl.ExternalName);
336 else if (OwningOp->Asl.CompileFlags & NODE_METHOD_SOME_NO_RETVAL)
338 /* Method SOMETIMES returns a value, SOMETIMES not */
340 AslError (ASL_WARNING, ASL_MSG_SOME_NO_RETVAL,
341 Op, Op->Asl.ExternalName);
343 else if (!(ThisNodeBtype & RequiredBtypes))
345 /* Method returns a value, but the type is wrong */
347 AnFormatBtype (StringBuffer, ThisNodeBtype);
348 AnFormatBtype (StringBuffer2, RequiredBtypes);
351 * The case where the method does not return any value at all
352 * was already handled in the namespace cross reference
353 * -- Only issue an error if the method in fact returns a value,
354 * but it is of the wrong type
356 if (ThisNodeBtype != 0)
359 "Method returns [%s], %s operator requires [%s]",
360 StringBuffer, OpInfo->Name, StringBuffer2);
362 AslError (ASL_ERROR, ASL_MSG_INVALID_TYPE, ArgOp, MsgBuffer);
368 /*******************************************************************************
370 * FUNCTION: AnIsResultUsed
372 * PARAMETERS: Op - Parent op for the operator
374 * RETURN: TRUE if result from this operation is actually consumed
376 * DESCRIPTION: Determine if the function result value from an operator is
379 ******************************************************************************/
383 ACPI_PARSE_OBJECT *Op)
385 ACPI_PARSE_OBJECT *Parent;
388 switch (Op->Asl.ParseOpcode)
390 case PARSEOP_INCREMENT:
391 case PARSEOP_DECREMENT:
393 /* These are standalone operators, no return value */
402 /* Examine parent to determine if the return value is used */
404 Parent = Op->Asl.Parent;
405 switch (Parent->Asl.ParseOpcode)
407 /* If/While - check if the operator is the predicate */
412 /* First child is the predicate */
414 if (Parent->Asl.Child == Op)
421 /* Not used if one of these is the parent */
424 case PARSEOP_DEFINITION_BLOCK:
431 /* Any other type of parent means that the result is used */
438 /*******************************************************************************
440 * FUNCTION: ApCheckForGpeNameConflict
442 * PARAMETERS: Op - Current parse op
446 * DESCRIPTION: Check for a conflict between GPE names within this scope.
447 * Conflict means two GPE names with the same GPE number, but
448 * different types -- such as _L1C and _E1C.
450 ******************************************************************************/
453 ApCheckForGpeNameConflict (
454 ACPI_PARSE_OBJECT *Op)
456 ACPI_PARSE_OBJECT *NextOp;
458 char Name[ACPI_NAME_SIZE + 1];
459 char Target[ACPI_NAME_SIZE];
462 /* Need a null-terminated string version of NameSeg */
464 ACPI_MOVE_32_TO_32 (Name, &Op->Asl.NameSeg);
465 Name[ACPI_NAME_SIZE] = 0;
469 * 1st char must be underscore
470 * 2nd char must be L or E
471 * 3rd/4th chars must be a hex number
473 if ((Name[0] != '_') ||
474 ((Name[1] != 'L') && (Name[1] != 'E')))
479 /* Verify 3rd/4th chars are a valid hex value */
481 GpeNumber = strtoul (&Name[2], NULL, 16);
482 if (GpeNumber == ACPI_UINT32_MAX)
488 * We are now sure we have an _Lxx or _Exx.
489 * Create the target name that would cause collision (Flip E/L)
491 ACPI_MOVE_32_TO_32 (Target, Name);
493 /* Inject opposite letter ("L" versus "E") */
499 else /* Name[1] == 'E' */
504 /* Search all peers (objects within this scope) for target match */
506 NextOp = Op->Asl.Next;
510 * We mostly care about methods, but check Name() constructs also,
511 * even though they will get another error for not being a method.
512 * All GPE names must be defined as control methods.
514 if ((NextOp->Asl.ParseOpcode == PARSEOP_METHOD) ||
515 (NextOp->Asl.ParseOpcode == PARSEOP_NAME))
517 if (ACPI_COMPARE_NAME (Target, NextOp->Asl.NameSeg))
519 /* Found both _Exy and _Lxy in the same scope, error */
521 AslError (ASL_ERROR, ASL_MSG_GPE_NAME_CONFLICT, NextOp,
527 NextOp = NextOp->Asl.Next;
530 /* OK, no conflict found */
536 /*******************************************************************************
538 * FUNCTION: ApCheckRegMethod
540 * PARAMETERS: Op - Current parse op
544 * DESCRIPTION: Ensure that a _REG method has a corresponding Operation
545 * Region declaration within the same scope. Note: _REG is defined
546 * to have two arguments and must therefore be defined as a
549 ******************************************************************************/
553 ACPI_PARSE_OBJECT *Op)
555 ACPI_PARSE_OBJECT *Next;
556 ACPI_PARSE_OBJECT *Parent;
559 /* We are only interested in _REG methods */
561 if (!ACPI_COMPARE_NAME (METHOD_NAME__REG, &Op->Asl.NameSeg))
566 /* Get the start of the current scope */
568 Parent = Op->Asl.Parent;
569 Next = Parent->Asl.Child;
571 /* Search entire scope for an operation region declaration */
575 if (Next->Asl.ParseOpcode == PARSEOP_OPERATIONREGION)
577 return; /* Found region, OK */
580 Next = Next->Asl.Next;
583 /* No region found, issue warning */
585 AslError (ASL_WARNING, ASL_MSG_NO_REGION, Op, NULL);
589 /*******************************************************************************
591 * FUNCTION: ApFindNameInDeviceTree
593 * PARAMETERS: Name - Name to search for
594 * Op - Current parse op
596 * RETURN: TRUE if name found in the same scope as Op.
598 * DESCRIPTION: Determine if a name appears in the same scope as Op, as either
599 * a Method() or a Name(). "Same scope" can mean under an If or
602 * NOTE: Detects _HID/_ADR in this type of construct (legal in ACPI 6.1+)
609 * Name (_HID, "INT3442")
615 ******************************************************************************/
618 ApFindNameInDeviceTree (
620 ACPI_PARSE_OBJECT *Op)
625 Status = TrWalkParseTree (Op, ASL_WALK_VISIT_DOWNWARD,
626 ApDeviceSubtreeWalk, NULL, Name);
628 if (Status == AE_CTRL_TRUE)
630 return (TRUE); /* Found a match */
637 /* Callback function for interface above */
640 ApDeviceSubtreeWalk (
641 ACPI_PARSE_OBJECT *Op,
645 char *Name = ACPI_CAST_PTR (char, Context);
648 switch (Op->Asl.ParseOpcode)
652 /* Level 0 is the starting device, ignore it */
656 /* Ignore sub-devices */
658 return (AE_CTRL_DEPTH);
665 /* These are what we are looking for */
667 if (ACPI_COMPARE_NAME (Name, Op->Asl.NameSeg))
669 return (AE_CTRL_TRUE);
671 return (AE_CTRL_DEPTH);
675 case PARSEOP_OPERATIONREGION:
678 * We want to ignore these, because either they can be large
679 * subtrees or open a scope to somewhere else.
681 return (AE_CTRL_DEPTH);
691 /*******************************************************************************
693 * FUNCTION: ApFindNameInScope
695 * PARAMETERS: Name - Name to search for
696 * Op - Current parse op
698 * RETURN: TRUE if name found in the same scope as Op.
700 * DESCRIPTION: Determine if a name appears in the same scope as Op, as either
701 * a Method() or a Name().
703 ******************************************************************************/
708 ACPI_PARSE_OBJECT *Op)
710 ACPI_PARSE_OBJECT *Next;
711 ACPI_PARSE_OBJECT *Parent;
714 /* Get the start of the current scope */
716 Parent = Op->Asl.Parent;
717 Next = Parent->Asl.Child;
719 /* Search entire scope for a match to the name */
723 if ((Next->Asl.ParseOpcode == PARSEOP_METHOD) ||
724 (Next->Asl.ParseOpcode == PARSEOP_NAME))
726 if (ACPI_COMPARE_NAME (Name, Next->Asl.NameSeg))
732 Next = Next->Asl.Next;