1 /******************************************************************************
3 * Module Name: aslmethod.c - Control method analysis walk
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
45 #include "aslcompiler.h"
46 #include "aslcompiler.y.h"
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslmethod")
55 /* Local prototypes */
58 MtCheckNamedObjectInMethod (
59 ACPI_PARSE_OBJECT
*Op
,
60 ASL_METHOD_INFO
*MethodInfo
);
63 /*******************************************************************************
65 * FUNCTION: MtMethodAnalysisWalkBegin
67 * PARAMETERS: ASL_WALK_CALLBACK
71 * DESCRIPTION: Descending callback for the analysis walk. Check methods for:
72 * 1) Initialized local variables
76 ******************************************************************************/
79 MtMethodAnalysisWalkBegin (
80 ACPI_PARSE_OBJECT
*Op
,
84 ASL_ANALYSIS_WALK_INFO
*WalkInfo
= (ASL_ANALYSIS_WALK_INFO
*) Context
;
85 ASL_METHOD_INFO
*MethodInfo
= WalkInfo
->MethodStack
;
86 ACPI_PARSE_OBJECT
*Next
;
87 UINT32 RegisterNumber
;
89 char LocalName
[] = "Local0";
90 char ArgName
[] = "Arg0";
91 ACPI_PARSE_OBJECT
*ArgNode
;
92 ACPI_PARSE_OBJECT
*NextType
;
93 ACPI_PARSE_OBJECT
*NextParamType
;
97 switch (Op
->Asl
.ParseOpcode
)
103 /* Create and init method info */
105 MethodInfo
= UtLocalCalloc (sizeof (ASL_METHOD_INFO
));
106 MethodInfo
->Next
= WalkInfo
->MethodStack
;
109 WalkInfo
->MethodStack
= MethodInfo
;
111 /* Get the name node, ignored here */
113 Next
= Op
->Asl
.Child
;
115 /* Get the NumArguments node */
117 Next
= Next
->Asl
.Next
;
118 MethodInfo
->NumArguments
= (UINT8
)
119 (((UINT8
) Next
->Asl
.Value
.Integer
) & 0x07);
121 /* Get the SerializeRule and SyncLevel nodes, ignored here */
123 Next
= Next
->Asl
.Next
;
124 MethodInfo
->ShouldBeSerialized
= (UINT8
) Next
->Asl
.Value
.Integer
;
126 Next
= Next
->Asl
.Next
;
129 /* Get the ReturnType node */
131 Next
= Next
->Asl
.Next
;
133 NextType
= Next
->Asl
.Child
;
136 /* Get and map each of the ReturnTypes */
138 MethodInfo
->ValidReturnTypes
|= AnMapObjTypeToBtype (NextType
);
139 NextType
->Asl
.ParseOpcode
= PARSEOP_DEFAULT_ARG
;
140 NextType
= NextType
->Asl
.Next
;
143 /* Get the ParameterType node */
145 Next
= Next
->Asl
.Next
;
147 NextType
= Next
->Asl
.Child
;
150 if (NextType
->Asl
.ParseOpcode
== PARSEOP_DEFAULT_ARG
)
152 NextParamType
= NextType
->Asl
.Child
;
153 while (NextParamType
)
155 MethodInfo
->ValidArgTypes
[ActualArgs
] |= AnMapObjTypeToBtype (NextParamType
);
156 NextParamType
->Asl
.ParseOpcode
= PARSEOP_DEFAULT_ARG
;
157 NextParamType
= NextParamType
->Asl
.Next
;
162 MethodInfo
->ValidArgTypes
[ActualArgs
] =
163 AnMapObjTypeToBtype (NextType
);
164 NextType
->Asl
.ParseOpcode
= PARSEOP_DEFAULT_ARG
;
168 NextType
= NextType
->Asl
.Next
;
171 if ((MethodInfo
->NumArguments
) &&
172 (MethodInfo
->NumArguments
!= ActualArgs
))
174 /* error: Param list did not match number of args */
177 /* Allow numarguments == 0 for Function() */
179 if ((!MethodInfo
->NumArguments
) && (ActualArgs
))
181 MethodInfo
->NumArguments
= ActualArgs
;
182 ArgNode
->Asl
.Value
.Integer
|= ActualArgs
;
186 * Actual arguments are initialized at method entry.
187 * All other ArgX "registers" can be used as locals, so we
188 * track their initialization.
190 for (i
= 0; i
< MethodInfo
->NumArguments
; i
++)
192 MethodInfo
->ArgInitialized
[i
] = TRUE
;
196 case PARSEOP_METHODCALL
:
199 (Op
->Asl
.Node
== MethodInfo
->Op
->Asl
.Node
))
201 AslError (ASL_REMARK
, ASL_MSG_RECURSION
, Op
, Op
->Asl
.ExternalName
);
217 * Local was used outside a control method, or there was an error
218 * in the method declaration.
220 AslError (ASL_REMARK
, ASL_MSG_LOCAL_OUTSIDE_METHOD
, Op
, Op
->Asl
.ExternalName
);
224 RegisterNumber
= (Op
->Asl
.AmlOpcode
& 0x000F);
227 * If the local is being used as a target, mark the local
230 if (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
)
232 MethodInfo
->LocalInitialized
[RegisterNumber
] = TRUE
;
236 * Otherwise, this is a reference, check if the local
237 * has been previously initialized.
239 * The only operator that accepts an uninitialized value is ObjectType()
241 else if ((!MethodInfo
->LocalInitialized
[RegisterNumber
]) &&
242 (Op
->Asl
.Parent
->Asl
.ParseOpcode
!= PARSEOP_OBJECTTYPE
))
244 LocalName
[strlen (LocalName
) -1] = (char) (RegisterNumber
+ 0x30);
245 AslError (ASL_ERROR
, ASL_MSG_LOCAL_INIT
, Op
, LocalName
);
260 * Arg was used outside a control method, or there was an error
261 * in the method declaration.
263 AslError (ASL_REMARK
, ASL_MSG_LOCAL_OUTSIDE_METHOD
, Op
, Op
->Asl
.ExternalName
);
267 RegisterNumber
= (Op
->Asl
.AmlOpcode
& 0x000F) - 8;
268 ArgName
[strlen (ArgName
) -1] = (char) (RegisterNumber
+ 0x30);
271 * If the Arg is being used as a target, mark the local
274 if (Op
->Asl
.CompileFlags
& NODE_IS_TARGET
)
276 MethodInfo
->ArgInitialized
[RegisterNumber
] = TRUE
;
280 * Otherwise, this is a reference, check if the Arg
281 * has been previously initialized.
283 * The only operator that accepts an uninitialized value is ObjectType()
285 else if ((!MethodInfo
->ArgInitialized
[RegisterNumber
]) &&
286 (Op
->Asl
.Parent
->Asl
.ParseOpcode
!= PARSEOP_OBJECTTYPE
))
288 AslError (ASL_ERROR
, ASL_MSG_ARG_INIT
, Op
, ArgName
);
291 /* Flag this arg if it is not a "real" argument to the method */
293 if (RegisterNumber
>= MethodInfo
->NumArguments
)
295 AslError (ASL_REMARK
, ASL_MSG_NOT_PARAMETER
, Op
, ArgName
);
304 * Probably was an error in the method declaration,
305 * no additional error here
307 ACPI_WARNING ((AE_INFO
, "%p, No parent method", Op
));
312 * A child indicates a possible return value. A simple Return or
313 * Return() is marked with NODE_IS_NULL_RETURN by the parser so
314 * that it is not counted as a "real" return-with-value, although
315 * the AML code that is actually emitted is Return(0). The AML
316 * definition of Return has a required parameter, so we are
317 * forced to convert a null return to Return(0).
319 if ((Op
->Asl
.Child
) &&
320 (Op
->Asl
.Child
->Asl
.ParseOpcode
!= PARSEOP_DEFAULT_ARG
) &&
321 (!(Op
->Asl
.Child
->Asl
.CompileFlags
& NODE_IS_NULL_RETURN
)))
323 MethodInfo
->NumReturnWithValue
++;
327 MethodInfo
->NumReturnNoValue
++;
332 case PARSEOP_CONTINUE
:
334 Next
= Op
->Asl
.Parent
;
337 if (Next
->Asl
.ParseOpcode
== PARSEOP_WHILE
)
341 Next
= Next
->Asl
.Parent
;
346 AslError (ASL_ERROR
, ASL_MSG_NO_WHILE
, Op
, NULL
);
352 /* We can range check if the argument is an integer */
354 if ((Op
->Asl
.Child
->Asl
.ParseOpcode
== PARSEOP_INTEGER
) &&
355 (Op
->Asl
.Child
->Asl
.Value
.Integer
> ACPI_UINT8_MAX
))
357 AslError (ASL_ERROR
, ASL_MSG_INVALID_TIME
, Op
, NULL
);
364 case PARSEOP_OPERATIONREGION
:
365 case PARSEOP_POWERRESOURCE
:
366 case PARSEOP_PROCESSOR
:
367 case PARSEOP_THERMALZONE
:
370 * The first operand is a name to be created in the namespace.
371 * Check against the reserved list.
373 i
= ApCheckForPredefinedName (Op
, Op
->Asl
.NameSeg
);
374 if (i
< ACPI_VALID_RESERVED_NAME_MAX
)
376 AslError (ASL_ERROR
, ASL_MSG_RESERVED_USE
, Op
, Op
->Asl
.ExternalName
);
382 /* Typecheck any predefined names statically defined with Name() */
384 ApCheckForPredefinedObject (Op
, Op
->Asl
.NameSeg
);
386 /* Special typechecking for _HID */
388 if (!ACPI_STRCMP (METHOD_NAME__HID
, Op
->Asl
.NameSeg
))
390 Next
= Op
->Asl
.Child
->Asl
.Next
;
391 AnCheckId (Next
, ASL_TYPE_HID
);
394 /* Special typechecking for _CID */
396 else if (!ACPI_STRCMP (METHOD_NAME__CID
, Op
->Asl
.NameSeg
))
398 Next
= Op
->Asl
.Child
->Asl
.Next
;
400 if ((Next
->Asl
.ParseOpcode
== PARSEOP_PACKAGE
) ||
401 (Next
->Asl
.ParseOpcode
== PARSEOP_VAR_PACKAGE
))
403 Next
= Next
->Asl
.Child
;
406 AnCheckId (Next
, ASL_TYPE_CID
);
407 Next
= Next
->Asl
.Next
;
412 AnCheckId (Next
, ASL_TYPE_CID
);
422 /* Check for named object creation within a non-serialized method */
424 MtCheckNamedObjectInMethod (Op
, MethodInfo
);
429 /*******************************************************************************
431 * FUNCTION: MtCheckNamedObjectInMethod
433 * PARAMETERS: Op - Current parser op
434 * MethodInfo - Info for method being parsed
438 * DESCRIPTION: Detect if a non-serialized method is creating a named object,
439 * which could possibly cause problems if two threads execute
440 * the method concurrently. Emit a remark in this case.
442 ******************************************************************************/
445 MtCheckNamedObjectInMethod (
446 ACPI_PARSE_OBJECT
*Op
,
447 ASL_METHOD_INFO
*MethodInfo
)
449 const ACPI_OPCODE_INFO
*OpInfo
;
452 /* We don't care about actual method declarations */
454 if (Op
->Asl
.AmlOpcode
== AML_METHOD_OP
)
459 /* Determine if we are creating a named object */
461 OpInfo
= AcpiPsGetOpcodeInfo (Op
->Asl
.AmlOpcode
);
462 if (OpInfo
->Class
== AML_CLASS_NAMED_OBJECT
)
465 * If we have a named object created within a non-serialized method,
466 * emit a remark that the method should be serialized.
468 * Reason: If a thread blocks within the method for any reason, and
469 * another thread enters the method, the method will fail because an
470 * attempt will be made to create the same object twice.
472 if (MethodInfo
&& !MethodInfo
->ShouldBeSerialized
)
474 AslError (ASL_REMARK
, ASL_MSG_SERIALIZED_REQUIRED
, MethodInfo
->Op
,
475 "due to creation of named objects within");
477 /* Emit message only ONCE per method */
479 MethodInfo
->ShouldBeSerialized
= TRUE
;
485 /*******************************************************************************
487 * FUNCTION: MtMethodAnalysisWalkEnd
489 * PARAMETERS: ASL_WALK_CALLBACK
493 * DESCRIPTION: Ascending callback for analysis walk. Complete method
496 ******************************************************************************/
499 MtMethodAnalysisWalkEnd (
500 ACPI_PARSE_OBJECT
*Op
,
504 ASL_ANALYSIS_WALK_INFO
*WalkInfo
= (ASL_ANALYSIS_WALK_INFO
*) Context
;
505 ASL_METHOD_INFO
*MethodInfo
= WalkInfo
->MethodStack
;
508 switch (Op
->Asl
.ParseOpcode
)
515 printf ("No method info for method! [%s]\n", Op
->Asl
.Namepath
);
516 AslError (ASL_ERROR
, ASL_MSG_COMPILER_INTERNAL
, Op
,
517 "No method info for this method");
520 return (AE_AML_INTERNAL
);
529 switch (Op
->Asl
.ParseOpcode
)
533 WalkInfo
->MethodStack
= MethodInfo
->Next
;
536 * Check if there is no return statement at the end of the
537 * method AND we can actually get there -- i.e., the execution
538 * of the method can possibly terminate without a return statement.
540 if ((!AnLastStatementIsReturn (Op
)) &&
541 (!(Op
->Asl
.CompileFlags
& NODE_HAS_NO_EXIT
)))
544 * No return statement, and execution can possibly exit
545 * via this path. This is equivalent to Return ()
547 MethodInfo
->NumReturnNoValue
++;
551 * Check for case where some return statements have a return value
552 * and some do not. Exit without a return statement is a return with
555 if (MethodInfo
->NumReturnNoValue
&&
556 MethodInfo
->NumReturnWithValue
)
558 AslError (ASL_WARNING
, ASL_MSG_RETURN_TYPES
, Op
,
559 Op
->Asl
.ExternalName
);
563 * If there are any RETURN() statements with no value, or there is a
564 * control path that allows the method to exit without a return value,
565 * we mark the method as a method that does not return a value. This
566 * knowledge can be used to check method invocations that expect a
569 if (MethodInfo
->NumReturnNoValue
)
571 if (MethodInfo
->NumReturnWithValue
)
573 Op
->Asl
.CompileFlags
|= NODE_METHOD_SOME_NO_RETVAL
;
577 Op
->Asl
.CompileFlags
|= NODE_METHOD_NO_RETVAL
;
582 * Check predefined method names for correct return behavior
583 * and correct number of arguments. Also, some special checks
584 * For GPE and _REG methods.
586 if (ApCheckForPredefinedMethod (Op
, MethodInfo
))
588 /* Special check for two names like _L01 and _E01 in same scope */
590 ApCheckForGpeNameConflict (Op
);
593 * Special check for _REG: Must have an operation region definition
594 * within the same scope!
596 ApCheckRegMethod (Op
);
599 ACPI_FREE (MethodInfo
);
604 /* Special check for two names like _L01 and _E01 in same scope */
606 ApCheckForGpeNameConflict (Op
);
612 * If the parent is a predefined method name, attempt to typecheck
613 * the return value. Only static types can be validated.
615 ApCheckPredefinedReturnValue (Op
, MethodInfo
);
618 * The parent block does not "exit" and continue execution -- the
619 * method is terminated here with the Return() statement.
621 Op
->Asl
.Parent
->Asl
.CompileFlags
|= NODE_HAS_NO_EXIT
;
623 /* Used in the "typing" pass later */
625 Op
->Asl
.ParentMethod
= MethodInfo
->Op
;
628 * If there is a peer node after the return statement, then this
629 * node is unreachable code -- i.e., it won't be executed because of
630 * the preceding Return() statement.
634 AslError (ASL_WARNING
, ASL_MSG_UNREACHABLE_CODE
, Op
->Asl
.Next
, NULL
);
640 if ((Op
->Asl
.CompileFlags
& NODE_HAS_NO_EXIT
) &&
642 (Op
->Asl
.Next
->Asl
.ParseOpcode
== PARSEOP_ELSE
))
645 * This IF has a corresponding ELSE. The IF block has no exit,
646 * (it contains an unconditional Return)
647 * mark the ELSE block to remember this fact.
649 Op
->Asl
.Next
->Asl
.CompileFlags
|= NODE_IF_HAS_NO_EXIT
;
655 if ((Op
->Asl
.CompileFlags
& NODE_HAS_NO_EXIT
) &&
656 (Op
->Asl
.CompileFlags
& NODE_IF_HAS_NO_EXIT
))
659 * This ELSE block has no exit and the corresponding IF block
660 * has no exit either. Therefore, the parent node has no exit.
662 Op
->Asl
.Parent
->Asl
.CompileFlags
|= NODE_HAS_NO_EXIT
;
669 if ((Op
->Asl
.CompileFlags
& NODE_HAS_NO_EXIT
) &&
672 /* If this node has no exit, then the parent has no exit either */
674 Op
->Asl
.Parent
->Asl
.CompileFlags
|= NODE_HAS_NO_EXIT
;