xref: /freebsd/sys/contrib/dev/acpica/compiler/aslexternal.c (revision f425b8be7e7a38e47677d7bd68819dccbdcc3134)
1f8146b88SJung-uk Kim /******************************************************************************
2f8146b88SJung-uk Kim  *
3f8146b88SJung-uk Kim  * Module Name: aslexternal - ASL External opcode compiler support
4f8146b88SJung-uk Kim  *
5f8146b88SJung-uk Kim  *****************************************************************************/
6f8146b88SJung-uk Kim 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
11*f425b8beSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2020, Intel Corp.
12f8146b88SJung-uk Kim  * All rights reserved.
13f8146b88SJung-uk Kim  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-uk Kim  *
119f8146b88SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120f8146b88SJung-uk Kim  * modification, are permitted provided that the following conditions
121f8146b88SJung-uk Kim  * are met:
122f8146b88SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123f8146b88SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124f8146b88SJung-uk Kim  *    without modification.
125f8146b88SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126f8146b88SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127f8146b88SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128f8146b88SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129f8146b88SJung-uk Kim  *    binary redistribution.
130f8146b88SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131f8146b88SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132f8146b88SJung-uk Kim  *    from this software without specific prior written permission.
133f8146b88SJung-uk Kim  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147f8146b88SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148f8146b88SJung-uk Kim  * Software Foundation.
149f8146b88SJung-uk Kim  *
1500d84335fSJung-uk Kim  *****************************************************************************/
151f8146b88SJung-uk Kim 
152f8146b88SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
153f8146b88SJung-uk Kim #include "aslcompiler.y.h"
154f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/acparser.h>
155f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/amlcode.h>
156f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
157f8146b88SJung-uk Kim 
158f8146b88SJung-uk Kim 
159f8146b88SJung-uk Kim #define _COMPONENT          ACPI_COMPILER
160f8146b88SJung-uk Kim         ACPI_MODULE_NAME    ("aslexternal")
161f8146b88SJung-uk Kim 
162f8146b88SJung-uk Kim 
163f8146b88SJung-uk Kim /* Local prototypes */
164f8146b88SJung-uk Kim 
165f8146b88SJung-uk Kim static void
166f8146b88SJung-uk Kim ExInsertArgCount (
167f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
168f8146b88SJung-uk Kim 
169f8146b88SJung-uk Kim static void
170f8146b88SJung-uk Kim ExMoveExternals (
171f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *DefinitionBlockOp);
172f8146b88SJung-uk Kim 
173f8146b88SJung-uk Kim 
174f8146b88SJung-uk Kim /*******************************************************************************
175f8146b88SJung-uk Kim  *
176f8146b88SJung-uk Kim  * FUNCTION:    ExDoExternal
177f8146b88SJung-uk Kim  *
178f8146b88SJung-uk Kim  * PARAMETERS:  Op                  - Current Parse node
179f8146b88SJung-uk Kim  *
180f8146b88SJung-uk Kim  * RETURN:      None
181f8146b88SJung-uk Kim  *
182f8146b88SJung-uk Kim  * DESCRIPTION: Add an External() definition to the global list. This list
183f8146b88SJung-uk Kim  *              is used to generate External opcodes.
184f8146b88SJung-uk Kim  *
185f8146b88SJung-uk Kim  ******************************************************************************/
186f8146b88SJung-uk Kim 
187f8146b88SJung-uk Kim void
188f8146b88SJung-uk Kim ExDoExternal (
189f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op)
190f8146b88SJung-uk Kim {
191f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *ListOp;
192f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Prev;
193f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Next;
194f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *ArgCountOp;
195f8146b88SJung-uk Kim 
196f8146b88SJung-uk Kim 
197f8146b88SJung-uk Kim     ArgCountOp = Op->Asl.Child->Asl.Next->Asl.Next;
198f8146b88SJung-uk Kim     ArgCountOp->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
199f8146b88SJung-uk Kim     ArgCountOp->Asl.ParseOpcode = PARSEOP_BYTECONST;
200f8146b88SJung-uk Kim     ArgCountOp->Asl.Value.Integer = 0;
201f8146b88SJung-uk Kim     UtSetParseOpName (ArgCountOp);
202f8146b88SJung-uk Kim 
203f8146b88SJung-uk Kim     /* Create new list node of arbitrary type */
204f8146b88SJung-uk Kim 
2055f9b24faSJung-uk Kim     ListOp = TrAllocateOp (PARSEOP_DEFAULT_ARG);
206f8146b88SJung-uk Kim 
207f8146b88SJung-uk Kim     /* Store External node as child */
208f8146b88SJung-uk Kim 
209f8146b88SJung-uk Kim     ListOp->Asl.Child = Op;
210f8146b88SJung-uk Kim     ListOp->Asl.Next = NULL;
211f8146b88SJung-uk Kim 
2126f1f1a63SJung-uk Kim     if (AslGbl_ExternalsListHead)
213f8146b88SJung-uk Kim     {
214f8146b88SJung-uk Kim         /* Link new External to end of list */
215f8146b88SJung-uk Kim 
2166f1f1a63SJung-uk Kim         Prev = AslGbl_ExternalsListHead;
217f8146b88SJung-uk Kim         Next = Prev;
218f8146b88SJung-uk Kim         while (Next)
219f8146b88SJung-uk Kim         {
220f8146b88SJung-uk Kim             Prev = Next;
221f8146b88SJung-uk Kim             Next = Next->Asl.Next;
222f8146b88SJung-uk Kim         }
223f8146b88SJung-uk Kim 
224f8146b88SJung-uk Kim         Prev->Asl.Next = ListOp;
225f8146b88SJung-uk Kim     }
226f8146b88SJung-uk Kim     else
227f8146b88SJung-uk Kim     {
2286f1f1a63SJung-uk Kim         AslGbl_ExternalsListHead = ListOp;
229f8146b88SJung-uk Kim     }
230f8146b88SJung-uk Kim }
231f8146b88SJung-uk Kim 
232f8146b88SJung-uk Kim 
233f8146b88SJung-uk Kim /*******************************************************************************
234f8146b88SJung-uk Kim  *
235f8146b88SJung-uk Kim  * FUNCTION:    ExInsertArgCount
236f8146b88SJung-uk Kim  *
237f8146b88SJung-uk Kim  * PARAMETERS:  Op              - Op for a method invocation
238f8146b88SJung-uk Kim  *
239f8146b88SJung-uk Kim  * RETURN:      None
240f8146b88SJung-uk Kim  *
241f8146b88SJung-uk Kim  * DESCRIPTION: Obtain the number of arguments for a control method -- from
242f8146b88SJung-uk Kim  *              the actual invocation.
243f8146b88SJung-uk Kim  *
244f8146b88SJung-uk Kim  ******************************************************************************/
245f8146b88SJung-uk Kim 
246f8146b88SJung-uk Kim static void
247f8146b88SJung-uk Kim ExInsertArgCount (
248f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op)
249f8146b88SJung-uk Kim {
250f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Next;
251f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *NameOp;
252f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Child;
253f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *ArgCountOp;
254f8146b88SJung-uk Kim     char *                  ExternalName;
255f8146b88SJung-uk Kim     char *                  CallName;
256f8146b88SJung-uk Kim     UINT16                  ArgCount = 0;
257f8146b88SJung-uk Kim     ACPI_STATUS             Status;
258f8146b88SJung-uk Kim 
259f8146b88SJung-uk Kim 
260f8146b88SJung-uk Kim     CallName = AcpiNsGetNormalizedPathname (Op->Asl.Node, TRUE);
261f8146b88SJung-uk Kim 
2626f1f1a63SJung-uk Kim     Next = AslGbl_ExternalsListHead;
263f8146b88SJung-uk Kim     while (Next)
264f8146b88SJung-uk Kim     {
265f8146b88SJung-uk Kim         ArgCount = 0;
266f8146b88SJung-uk Kim 
267f8146b88SJung-uk Kim         /* Skip if External node already handled */
268f8146b88SJung-uk Kim 
2695f9b24faSJung-uk Kim         if (Next->Asl.Child->Asl.CompileFlags & OP_VISITED)
270f8146b88SJung-uk Kim         {
271f8146b88SJung-uk Kim             Next = Next->Asl.Next;
272f8146b88SJung-uk Kim             continue;
273f8146b88SJung-uk Kim         }
274f8146b88SJung-uk Kim 
275f8146b88SJung-uk Kim         NameOp = Next->Asl.Child->Asl.Child;
276f8146b88SJung-uk Kim         ExternalName = AcpiNsGetNormalizedPathname (NameOp->Asl.Node, TRUE);
277f8146b88SJung-uk Kim 
278f8146b88SJung-uk Kim         if (strcmp (CallName, ExternalName))
279f8146b88SJung-uk Kim         {
280f8146b88SJung-uk Kim             ACPI_FREE (ExternalName);
281f8146b88SJung-uk Kim             Next = Next->Asl.Next;
282f8146b88SJung-uk Kim             continue;
283f8146b88SJung-uk Kim         }
284f8146b88SJung-uk Kim 
2855f9b24faSJung-uk Kim         Next->Asl.Child->Asl.CompileFlags |= OP_VISITED;
286f8146b88SJung-uk Kim 
287f8146b88SJung-uk Kim         /*
288f8146b88SJung-uk Kim          * Since we will reposition Externals to the Root, set Namepath
289f8146b88SJung-uk Kim          * to the fully qualified name and recalculate the aml length
290f8146b88SJung-uk Kim          */
291f8146b88SJung-uk Kim         Status = UtInternalizeName (ExternalName,
292f8146b88SJung-uk Kim             &NameOp->Asl.Value.String);
293f8146b88SJung-uk Kim 
294f8146b88SJung-uk Kim         ACPI_FREE (ExternalName);
295f8146b88SJung-uk Kim         if (ACPI_FAILURE (Status))
296f8146b88SJung-uk Kim         {
297f8146b88SJung-uk Kim             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
298f8146b88SJung-uk Kim                 NULL, "- Could not Internalize External");
299f8146b88SJung-uk Kim             break;
300f8146b88SJung-uk Kim         }
301f8146b88SJung-uk Kim 
302f8146b88SJung-uk Kim         NameOp->Asl.AmlLength = strlen (NameOp->Asl.Value.String);
303f8146b88SJung-uk Kim 
304f8146b88SJung-uk Kim         /* Get argument count */
305f8146b88SJung-uk Kim 
306f8146b88SJung-uk Kim         Child = Op->Asl.Child;
307f8146b88SJung-uk Kim         while (Child)
308f8146b88SJung-uk Kim         {
309f8146b88SJung-uk Kim             ArgCount++;
310f8146b88SJung-uk Kim             Child = Child->Asl.Next;
311f8146b88SJung-uk Kim         }
312f8146b88SJung-uk Kim 
313f8146b88SJung-uk Kim         /* Setup ArgCount operand */
314f8146b88SJung-uk Kim 
315f8146b88SJung-uk Kim         ArgCountOp = Next->Asl.Child->Asl.Child->Asl.Next->Asl.Next;
316f8146b88SJung-uk Kim         ArgCountOp->Asl.Value.Integer = ArgCount;
317f8146b88SJung-uk Kim         break;
318f8146b88SJung-uk Kim     }
319f8146b88SJung-uk Kim 
320f8146b88SJung-uk Kim     ACPI_FREE (CallName);
321f8146b88SJung-uk Kim }
322f8146b88SJung-uk Kim 
323f8146b88SJung-uk Kim 
324f8146b88SJung-uk Kim /*******************************************************************************
325f8146b88SJung-uk Kim  *
326f8146b88SJung-uk Kim  * FUNCTION:    ExAmlExternalWalkBegin
327f8146b88SJung-uk Kim  *
328f8146b88SJung-uk Kim  * PARAMETERS:  ASL_WALK_CALLBACK
329f8146b88SJung-uk Kim  *
330f8146b88SJung-uk Kim  * RETURN:      None
331f8146b88SJung-uk Kim  *
332f8146b88SJung-uk Kim  * DESCRIPTION: Parse tree walk to create external opcode list for methods.
333f8146b88SJung-uk Kim  *
334f8146b88SJung-uk Kim  ******************************************************************************/
335f8146b88SJung-uk Kim 
336f8146b88SJung-uk Kim ACPI_STATUS
337f8146b88SJung-uk Kim ExAmlExternalWalkBegin (
338f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
339f8146b88SJung-uk Kim     UINT32                  Level,
340f8146b88SJung-uk Kim     void                    *Context)
341f8146b88SJung-uk Kim {
342f8146b88SJung-uk Kim 
343f8146b88SJung-uk Kim     /* External list head saved in the definition block op */
344f8146b88SJung-uk Kim 
345f8146b88SJung-uk Kim     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
346f8146b88SJung-uk Kim     {
3476f1f1a63SJung-uk Kim         AslGbl_ExternalsListHead = Op->Asl.Value.Arg;
348f8146b88SJung-uk Kim     }
349f8146b88SJung-uk Kim 
3506f1f1a63SJung-uk Kim     if (!AslGbl_ExternalsListHead)
351f8146b88SJung-uk Kim     {
352f8146b88SJung-uk Kim         return (AE_OK);
353f8146b88SJung-uk Kim     }
354f8146b88SJung-uk Kim 
355f8146b88SJung-uk Kim     if (Op->Asl.ParseOpcode != PARSEOP_METHODCALL)
356f8146b88SJung-uk Kim     {
357f8146b88SJung-uk Kim         return (AE_OK);
358f8146b88SJung-uk Kim     }
359f8146b88SJung-uk Kim 
360f8146b88SJung-uk Kim     /*
361f8146b88SJung-uk Kim      * The NameOp child under an ExternalOp gets turned into PARSE_METHODCALL
362f8146b88SJung-uk Kim      * by XfNamespaceLocateBegin(). Ignore these.
363f8146b88SJung-uk Kim      */
364f8146b88SJung-uk Kim     if (Op->Asl.Parent &&
365f8146b88SJung-uk Kim         Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_EXTERNAL)
366f8146b88SJung-uk Kim     {
367f8146b88SJung-uk Kim         return (AE_OK);
368f8146b88SJung-uk Kim     }
369f8146b88SJung-uk Kim 
370f8146b88SJung-uk Kim     ExInsertArgCount (Op);
371f8146b88SJung-uk Kim     return (AE_OK);
372f8146b88SJung-uk Kim }
373f8146b88SJung-uk Kim 
374f8146b88SJung-uk Kim 
375f8146b88SJung-uk Kim /*******************************************************************************
376f8146b88SJung-uk Kim  *
377f8146b88SJung-uk Kim  * FUNCTION:    ExAmlExternalWalkEnd
378f8146b88SJung-uk Kim  *
379f8146b88SJung-uk Kim  * PARAMETERS:  ASL_WALK_CALLBACK
380f8146b88SJung-uk Kim  *
381f8146b88SJung-uk Kim  * RETURN:      None
382f8146b88SJung-uk Kim  *
383f8146b88SJung-uk Kim  * DESCRIPTION: Parse tree walk to create external opcode list for methods.
384f8146b88SJung-uk Kim  *              Here, we just want to catch the case where a definition block
385f8146b88SJung-uk Kim  *              has been completed. Then we move all of the externals into
386f8146b88SJung-uk Kim  *              a single block in the parse tree and thus the AML code.
387f8146b88SJung-uk Kim  *
388f8146b88SJung-uk Kim  ******************************************************************************/
389f8146b88SJung-uk Kim 
390f8146b88SJung-uk Kim ACPI_STATUS
391f8146b88SJung-uk Kim ExAmlExternalWalkEnd (
392f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
393f8146b88SJung-uk Kim     UINT32                  Level,
394f8146b88SJung-uk Kim     void                    *Context)
395f8146b88SJung-uk Kim {
396f8146b88SJung-uk Kim 
397f8146b88SJung-uk Kim     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
398f8146b88SJung-uk Kim     {
399f8146b88SJung-uk Kim         /*
400f8146b88SJung-uk Kim          * Process any existing external list. (Support for
401f8146b88SJung-uk Kim          * multiple definition blocks in a single file/compile)
402f8146b88SJung-uk Kim          */
403f8146b88SJung-uk Kim         ExMoveExternals (Op);
4046f1f1a63SJung-uk Kim         AslGbl_ExternalsListHead = NULL;
405f8146b88SJung-uk Kim     }
406f8146b88SJung-uk Kim 
407f8146b88SJung-uk Kim     return (AE_OK);
408f8146b88SJung-uk Kim }
409f8146b88SJung-uk Kim 
410f8146b88SJung-uk Kim 
411f8146b88SJung-uk Kim /*******************************************************************************
412f8146b88SJung-uk Kim  *
413f8146b88SJung-uk Kim  * FUNCTION:    ExMoveExternals
414f8146b88SJung-uk Kim  *
415f8146b88SJung-uk Kim  * PARAMETERS:  DefinitionBlockOp       - Op for current definition block
416f8146b88SJung-uk Kim  *
417f8146b88SJung-uk Kim  * RETURN:      None
418f8146b88SJung-uk Kim  *
419f8146b88SJung-uk Kim  * DESCRIPTION: Move all externals present in the source file into a single
420f8146b88SJung-uk Kim  *              block of AML code, surrounded by an "If (0)" to prevent
421f8146b88SJung-uk Kim  *              AML interpreters from attempting to execute the External
422f8146b88SJung-uk Kim  *              opcodes.
423f8146b88SJung-uk Kim  *
424f8146b88SJung-uk Kim  ******************************************************************************/
425f8146b88SJung-uk Kim 
426f8146b88SJung-uk Kim static void
427f8146b88SJung-uk Kim ExMoveExternals (
428f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *DefinitionBlockOp)
429f8146b88SJung-uk Kim {
430f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *ParentOp;
431f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *ExternalOp;
432f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *PredicateOp;
433f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *NextOp;
434f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Prev;
435f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Next;
4361cc50d6bSJung-uk Kim     char                    *ExternalName;
437f8146b88SJung-uk Kim     ACPI_OBJECT_TYPE        ObjType;
438af051161SJung-uk Kim     ACPI_STATUS             Status;
439f8146b88SJung-uk Kim     UINT32                  i;
440f8146b88SJung-uk Kim 
441f8146b88SJung-uk Kim 
4426f1f1a63SJung-uk Kim     if (!AslGbl_ExternalsListHead)
443f8146b88SJung-uk Kim     {
444f8146b88SJung-uk Kim         return;
445f8146b88SJung-uk Kim     }
446f8146b88SJung-uk Kim 
447f8146b88SJung-uk Kim     /* Remove the External nodes from the tree */
448f8146b88SJung-uk Kim 
4496f1f1a63SJung-uk Kim     NextOp = AslGbl_ExternalsListHead;
450f8146b88SJung-uk Kim     while (NextOp)
451f8146b88SJung-uk Kim     {
452f8146b88SJung-uk Kim         /*
453f8146b88SJung-uk Kim          * The External is stored in child pointer of each node in the
454f8146b88SJung-uk Kim          * list
455f8146b88SJung-uk Kim          */
456f8146b88SJung-uk Kim         ExternalOp = NextOp->Asl.Child;
457f8146b88SJung-uk Kim 
4581cc50d6bSJung-uk Kim         /* Get/set the fully qualified name */
4591cc50d6bSJung-uk Kim 
4601cc50d6bSJung-uk Kim         ExternalName = AcpiNsGetNormalizedPathname (ExternalOp->Asl.Node, TRUE);
4611cc50d6bSJung-uk Kim         ExternalOp->Asl.ExternalName = ExternalName;
4621cc50d6bSJung-uk Kim         ExternalOp->Asl.Namepath = ExternalName;
4631cc50d6bSJung-uk Kim 
464f8146b88SJung-uk Kim         /* Set line numbers (for listings, etc.) */
465f8146b88SJung-uk Kim 
466f8146b88SJung-uk Kim         ExternalOp->Asl.LineNumber = 0;
467f8146b88SJung-uk Kim         ExternalOp->Asl.LogicalLineNumber = 0;
468f8146b88SJung-uk Kim 
469f8146b88SJung-uk Kim         Next = ExternalOp->Asl.Child;
470f8146b88SJung-uk Kim         Next->Asl.LineNumber = 0;
471f8146b88SJung-uk Kim         Next->Asl.LogicalLineNumber = 0;
472f8146b88SJung-uk Kim 
4731cc50d6bSJung-uk Kim         if (Next->Asl.ParseOpcode == PARSEOP_NAMESEG)
4741cc50d6bSJung-uk Kim         {
4751cc50d6bSJung-uk Kim             Next->Asl.ParseOpcode = PARSEOP_NAMESTRING;
4761cc50d6bSJung-uk Kim         }
477af051161SJung-uk Kim 
4781cc50d6bSJung-uk Kim         Next->Asl.ExternalName = ExternalName;
479af051161SJung-uk Kim         Status = UtInternalizeName (ExternalName, &Next->Asl.Value.String);
480af051161SJung-uk Kim         if (ACPI_FAILURE (Status))
481af051161SJung-uk Kim         {
482af051161SJung-uk Kim             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
483af051161SJung-uk Kim                 Next, "Could not internalize namestring");
484af051161SJung-uk Kim             return;
485af051161SJung-uk Kim         }
486af051161SJung-uk Kim 
4871cc50d6bSJung-uk Kim         Next->Asl.AmlLength = strlen (Next->Asl.Value.String);
4881cc50d6bSJung-uk Kim 
489f8146b88SJung-uk Kim         Next = Next->Asl.Next;
490f8146b88SJung-uk Kim         Next->Asl.LineNumber = 0;
491f8146b88SJung-uk Kim         Next->Asl.LogicalLineNumber = 0;
492f8146b88SJung-uk Kim 
493f8146b88SJung-uk Kim         Next = Next->Asl.Next;
494f8146b88SJung-uk Kim         Next->Asl.LineNumber = 0;
495f8146b88SJung-uk Kim         Next->Asl.LogicalLineNumber = 0;
496f8146b88SJung-uk Kim 
497f8146b88SJung-uk Kim         Next = Next->Asl.Next;
498f8146b88SJung-uk Kim         Next->Asl.LineNumber = 0;
499f8146b88SJung-uk Kim         Next->Asl.LogicalLineNumber = 0;
500f8146b88SJung-uk Kim 
501f8146b88SJung-uk Kim         ParentOp = ExternalOp->Asl.Parent;
502f8146b88SJung-uk Kim         Prev = Next = ParentOp->Asl.Child;
503f8146b88SJung-uk Kim 
504f8146b88SJung-uk Kim         /* Now find the External node's position in parse tree */
505f8146b88SJung-uk Kim 
506f8146b88SJung-uk Kim         while (Next != ExternalOp)
507f8146b88SJung-uk Kim         {
508f8146b88SJung-uk Kim             Prev = Next;
509f8146b88SJung-uk Kim             Next = Next->Asl.Next;
510f8146b88SJung-uk Kim         }
511f8146b88SJung-uk Kim 
512f8146b88SJung-uk Kim         /* Remove the External from the parse tree */
513f8146b88SJung-uk Kim 
514f8146b88SJung-uk Kim         if (Prev == ExternalOp)
515f8146b88SJung-uk Kim         {
516f8146b88SJung-uk Kim             /* External was the first child node */
517f8146b88SJung-uk Kim 
518f8146b88SJung-uk Kim             ParentOp->Asl.Child = ExternalOp->Asl.Next;
519f8146b88SJung-uk Kim         }
520f8146b88SJung-uk Kim 
521f8146b88SJung-uk Kim         Prev->Asl.Next = ExternalOp->Asl.Next;
522f8146b88SJung-uk Kim         ExternalOp->Asl.Next = NULL;
5236f1f1a63SJung-uk Kim         ExternalOp->Asl.Parent = AslGbl_ExternalsListHead;
524f8146b88SJung-uk Kim 
525f8146b88SJung-uk Kim         /* Point the External to the next in the list */
526f8146b88SJung-uk Kim 
527f8146b88SJung-uk Kim         if (NextOp->Asl.Next)
528f8146b88SJung-uk Kim         {
529f8146b88SJung-uk Kim             ExternalOp->Asl.Next = NextOp->Asl.Next->Asl.Child;
530f8146b88SJung-uk Kim         }
531f8146b88SJung-uk Kim 
532f8146b88SJung-uk Kim         NextOp = NextOp->Asl.Next;
533f8146b88SJung-uk Kim     }
534f8146b88SJung-uk Kim 
535f8146b88SJung-uk Kim     /*
536f8146b88SJung-uk Kim      * Loop again to remove MethodObj Externals for which
537f8146b88SJung-uk Kim      * a MethodCall was not found (dead external reference)
538f8146b88SJung-uk Kim      */
5396f1f1a63SJung-uk Kim     Prev = AslGbl_ExternalsListHead->Asl.Child;
540f8146b88SJung-uk Kim     Next = Prev;
541f8146b88SJung-uk Kim     while (Next)
542f8146b88SJung-uk Kim     {
543f8146b88SJung-uk Kim         ObjType = (ACPI_OBJECT_TYPE)
544f8146b88SJung-uk Kim             Next->Asl.Child->Asl.Next->Asl.Value.Integer;
545f8146b88SJung-uk Kim 
546f8146b88SJung-uk Kim         if (ObjType == ACPI_TYPE_METHOD &&
5475f9b24faSJung-uk Kim             !(Next->Asl.CompileFlags & OP_VISITED))
548f8146b88SJung-uk Kim         {
549f8146b88SJung-uk Kim             if (Next == Prev)
550f8146b88SJung-uk Kim             {
5516f1f1a63SJung-uk Kim                 AslGbl_ExternalsListHead->Asl.Child = Next->Asl.Next;
552f8146b88SJung-uk Kim                 Next->Asl.Next = NULL;
5536f1f1a63SJung-uk Kim                 Prev = AslGbl_ExternalsListHead->Asl.Child;
554f8146b88SJung-uk Kim                 Next = Prev;
555f8146b88SJung-uk Kim                 continue;
556f8146b88SJung-uk Kim             }
557f8146b88SJung-uk Kim             else
558f8146b88SJung-uk Kim             {
559f8146b88SJung-uk Kim                 Prev->Asl.Next = Next->Asl.Next;
560f8146b88SJung-uk Kim                 Next->Asl.Next = NULL;
561f8146b88SJung-uk Kim                 Next = Prev->Asl.Next;
562f8146b88SJung-uk Kim                 continue;
563f8146b88SJung-uk Kim             }
564f8146b88SJung-uk Kim         }
565f8146b88SJung-uk Kim 
566f8146b88SJung-uk Kim         Prev = Next;
567f8146b88SJung-uk Kim         Next = Next->Asl.Next;
568f8146b88SJung-uk Kim     }
569f8146b88SJung-uk Kim 
570f8146b88SJung-uk Kim     /* If list is now empty, don't bother to make If (0) block */
571f8146b88SJung-uk Kim 
5726f1f1a63SJung-uk Kim     if (!AslGbl_ExternalsListHead->Asl.Child)
573f8146b88SJung-uk Kim     {
574f8146b88SJung-uk Kim         return;
575f8146b88SJung-uk Kim     }
576f8146b88SJung-uk Kim 
577f8146b88SJung-uk Kim     /* Convert Gbl_ExternalsListHead parent to If(). */
578f8146b88SJung-uk Kim 
5796f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.ParseOpcode = PARSEOP_IF;
5806f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.AmlOpcode = AML_IF_OP;
5816f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.CompileFlags = OP_AML_PACKAGE;
5826f1f1a63SJung-uk Kim     UtSetParseOpName (AslGbl_ExternalsListHead);
583f8146b88SJung-uk Kim 
584f8146b88SJung-uk Kim     /* Create a Zero op for the If predicate */
585f8146b88SJung-uk Kim 
5865f9b24faSJung-uk Kim     PredicateOp = TrAllocateOp (PARSEOP_ZERO);
587f8146b88SJung-uk Kim     PredicateOp->Asl.AmlOpcode = AML_ZERO_OP;
588f8146b88SJung-uk Kim 
5896f1f1a63SJung-uk Kim     PredicateOp->Asl.Parent = AslGbl_ExternalsListHead;
590f8146b88SJung-uk Kim     PredicateOp->Asl.Child = NULL;
5916f1f1a63SJung-uk Kim     PredicateOp->Asl.Next = AslGbl_ExternalsListHead->Asl.Child;
5926f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.Child = PredicateOp;
593f8146b88SJung-uk Kim 
594f8146b88SJung-uk Kim     /* Set line numbers (for listings, etc.) */
595f8146b88SJung-uk Kim 
5966f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.LineNumber = 0;
5976f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.LogicalLineNumber = 0;
598f8146b88SJung-uk Kim 
599f8146b88SJung-uk Kim     PredicateOp->Asl.LineNumber = 0;
600f8146b88SJung-uk Kim     PredicateOp->Asl.LogicalLineNumber = 0;
601f8146b88SJung-uk Kim 
602f8146b88SJung-uk Kim     /* Insert block back in the list */
603f8146b88SJung-uk Kim 
604f8146b88SJung-uk Kim     Prev = DefinitionBlockOp->Asl.Child;
605f8146b88SJung-uk Kim     Next = Prev;
606f8146b88SJung-uk Kim 
607f8146b88SJung-uk Kim     /* Find last default arg */
608f8146b88SJung-uk Kim 
609f8146b88SJung-uk Kim     for (i = 0; i < 6; i++)
610f8146b88SJung-uk Kim     {
611f8146b88SJung-uk Kim         Prev = Next;
612f8146b88SJung-uk Kim         Next = Prev->Asl.Next;
613f8146b88SJung-uk Kim     }
614f8146b88SJung-uk Kim 
615f8146b88SJung-uk Kim     if (Next)
616f8146b88SJung-uk Kim     {
617f8146b88SJung-uk Kim         /* Definition Block is not empty */
618f8146b88SJung-uk Kim 
6196f1f1a63SJung-uk Kim         AslGbl_ExternalsListHead->Asl.Next = Next;
620f8146b88SJung-uk Kim     }
621f8146b88SJung-uk Kim     else
622f8146b88SJung-uk Kim     {
623f8146b88SJung-uk Kim         /* Definition Block is empty. */
624f8146b88SJung-uk Kim 
6256f1f1a63SJung-uk Kim         AslGbl_ExternalsListHead->Asl.Next = NULL;
626f8146b88SJung-uk Kim     }
627f8146b88SJung-uk Kim 
6286f1f1a63SJung-uk Kim     Prev->Asl.Next = AslGbl_ExternalsListHead;
6296f1f1a63SJung-uk Kim     AslGbl_ExternalsListHead->Asl.Parent = Prev->Asl.Parent;
630f8146b88SJung-uk Kim }
631