xref: /illumos-gate/usr/src/cmd/acpi/iasl/aslopt.c (revision 35786f68)
1bc36eafdSMike Gerdts /******************************************************************************
2bc36eafdSMike Gerdts  *
3bc36eafdSMike Gerdts  * Module Name: aslopt- Compiler optimizations
4bc36eafdSMike Gerdts  *
5bc36eafdSMike Gerdts  *****************************************************************************/
6bc36eafdSMike Gerdts 
7*35786f68SRobert Mustacchi /******************************************************************************
8*35786f68SRobert Mustacchi  *
9*35786f68SRobert Mustacchi  * 1. Copyright Notice
10*35786f68SRobert Mustacchi  *
11*35786f68SRobert Mustacchi  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12bc36eafdSMike Gerdts  * All rights reserved.
13bc36eafdSMike Gerdts  *
14*35786f68SRobert Mustacchi  * 2. License
15*35786f68SRobert Mustacchi  *
16*35786f68SRobert Mustacchi  * 2.1. This is your license from Intel Corp. under its intellectual property
17*35786f68SRobert Mustacchi  * rights. You may have additional license terms from the party that provided
18*35786f68SRobert Mustacchi  * you this software, covering your right to use that party's intellectual
19*35786f68SRobert Mustacchi  * property rights.
20*35786f68SRobert Mustacchi  *
21*35786f68SRobert Mustacchi  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22*35786f68SRobert Mustacchi  * copy of the source code appearing in this file ("Covered Code") an
23*35786f68SRobert Mustacchi  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24*35786f68SRobert Mustacchi  * base code distributed originally by Intel ("Original Intel Code") to copy,
25*35786f68SRobert Mustacchi  * make derivatives, distribute, use and display any portion of the Covered
26*35786f68SRobert Mustacchi  * Code in any form, with the right to sublicense such rights; and
27*35786f68SRobert Mustacchi  *
28*35786f68SRobert Mustacchi  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29*35786f68SRobert Mustacchi  * license (with the right to sublicense), under only those claims of Intel
30*35786f68SRobert Mustacchi  * patents that are infringed by the Original Intel Code, to make, use, sell,
31*35786f68SRobert Mustacchi  * offer to sell, and import the Covered Code and derivative works thereof
32*35786f68SRobert Mustacchi  * solely to the minimum extent necessary to exercise the above copyright
33*35786f68SRobert Mustacchi  * license, and in no event shall the patent license extend to any additions
34*35786f68SRobert Mustacchi  * to or modifications of the Original Intel Code. No other license or right
35*35786f68SRobert Mustacchi  * is granted directly or by implication, estoppel or otherwise;
36*35786f68SRobert Mustacchi  *
37*35786f68SRobert Mustacchi  * The above copyright and patent license is granted only if the following
38*35786f68SRobert Mustacchi  * conditions are met:
39*35786f68SRobert Mustacchi  *
40*35786f68SRobert Mustacchi  * 3. Conditions
41*35786f68SRobert Mustacchi  *
42*35786f68SRobert Mustacchi  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43*35786f68SRobert Mustacchi  * Redistribution of source code of any substantial portion of the Covered
44*35786f68SRobert Mustacchi  * Code or modification with rights to further distribute source must include
45*35786f68SRobert Mustacchi  * the above Copyright Notice, the above License, this list of Conditions,
46*35786f68SRobert Mustacchi  * and the following Disclaimer and Export Compliance provision. In addition,
47*35786f68SRobert Mustacchi  * Licensee must cause all Covered Code to which Licensee contributes to
48*35786f68SRobert Mustacchi  * contain a file documenting the changes Licensee made to create that Covered
49*35786f68SRobert Mustacchi  * Code and the date of any change. Licensee must include in that file the
50*35786f68SRobert Mustacchi  * documentation of any changes made by any predecessor Licensee. Licensee
51*35786f68SRobert Mustacchi  * must include a prominent statement that the modification is derived,
52*35786f68SRobert Mustacchi  * directly or indirectly, from Original Intel Code.
53*35786f68SRobert Mustacchi  *
54*35786f68SRobert Mustacchi  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55*35786f68SRobert Mustacchi  * Redistribution of source code of any substantial portion of the Covered
56*35786f68SRobert Mustacchi  * Code or modification without rights to further distribute source must
57*35786f68SRobert Mustacchi  * include the following Disclaimer and Export Compliance provision in the
58*35786f68SRobert Mustacchi  * documentation and/or other materials provided with distribution. In
59*35786f68SRobert Mustacchi  * addition, Licensee may not authorize further sublicense of source of any
60*35786f68SRobert Mustacchi  * portion of the Covered Code, and must include terms to the effect that the
61*35786f68SRobert Mustacchi  * license from Licensee to its licensee is limited to the intellectual
62*35786f68SRobert Mustacchi  * property embodied in the software Licensee provides to its licensee, and
63*35786f68SRobert Mustacchi  * not to intellectual property embodied in modifications its licensee may
64*35786f68SRobert Mustacchi  * make.
65*35786f68SRobert Mustacchi  *
66*35786f68SRobert Mustacchi  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67*35786f68SRobert Mustacchi  * substantial portion of the Covered Code or modification must reproduce the
68*35786f68SRobert Mustacchi  * above Copyright Notice, and the following Disclaimer and Export Compliance
69*35786f68SRobert Mustacchi  * provision in the documentation and/or other materials provided with the
70*35786f68SRobert Mustacchi  * distribution.
71*35786f68SRobert Mustacchi  *
72*35786f68SRobert Mustacchi  * 3.4. Intel retains all right, title, and interest in and to the Original
73*35786f68SRobert Mustacchi  * Intel Code.
74*35786f68SRobert Mustacchi  *
75*35786f68SRobert Mustacchi  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76*35786f68SRobert Mustacchi  * Intel shall be used in advertising or otherwise to promote the sale, use or
77*35786f68SRobert Mustacchi  * other dealings in products derived from or relating to the Covered Code
78*35786f68SRobert Mustacchi  * without prior written authorization from Intel.
79*35786f68SRobert Mustacchi  *
80*35786f68SRobert Mustacchi  * 4. Disclaimer and Export Compliance
81*35786f68SRobert Mustacchi  *
82*35786f68SRobert Mustacchi  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83*35786f68SRobert Mustacchi  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84*35786f68SRobert Mustacchi  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85*35786f68SRobert Mustacchi  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86*35786f68SRobert Mustacchi  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87*35786f68SRobert Mustacchi  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88*35786f68SRobert Mustacchi  * PARTICULAR PURPOSE.
89*35786f68SRobert Mustacchi  *
90*35786f68SRobert Mustacchi  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91*35786f68SRobert Mustacchi  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92*35786f68SRobert Mustacchi  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93*35786f68SRobert Mustacchi  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94*35786f68SRobert Mustacchi  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95*35786f68SRobert Mustacchi  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96*35786f68SRobert Mustacchi  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97*35786f68SRobert Mustacchi  * LIMITED REMEDY.
98*35786f68SRobert Mustacchi  *
99*35786f68SRobert Mustacchi  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100*35786f68SRobert Mustacchi  * software or system incorporating such software without first obtaining any
101*35786f68SRobert Mustacchi  * required license or other approval from the U. S. Department of Commerce or
102*35786f68SRobert Mustacchi  * any other agency or department of the United States Government. In the
103*35786f68SRobert Mustacchi  * event Licensee exports any such software from the United States or
104*35786f68SRobert Mustacchi  * re-exports any such software from a foreign destination, Licensee shall
105*35786f68SRobert Mustacchi  * ensure that the distribution and export/re-export of the software is in
106*35786f68SRobert Mustacchi  * compliance with all laws, regulations, orders, or other restrictions of the
107*35786f68SRobert Mustacchi  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108*35786f68SRobert Mustacchi  * any of its subsidiaries will export/re-export any technical data, process,
109*35786f68SRobert Mustacchi  * software, or service, directly or indirectly, to any country for which the
110*35786f68SRobert Mustacchi  * United States government or any agency thereof requires an export license,
111*35786f68SRobert Mustacchi  * other governmental approval, or letter of assurance, without first obtaining
112*35786f68SRobert Mustacchi  * such license, approval or letter.
113*35786f68SRobert Mustacchi  *
114*35786f68SRobert Mustacchi  *****************************************************************************
115*35786f68SRobert Mustacchi  *
116*35786f68SRobert Mustacchi  * Alternatively, you may choose to be licensed under the terms of the
117*35786f68SRobert Mustacchi  * following license:
118*35786f68SRobert Mustacchi  *
119bc36eafdSMike Gerdts  * Redistribution and use in source and binary forms, with or without
120bc36eafdSMike Gerdts  * modification, are permitted provided that the following conditions
121bc36eafdSMike Gerdts  * are met:
122bc36eafdSMike Gerdts  * 1. Redistributions of source code must retain the above copyright
123bc36eafdSMike Gerdts  *    notice, this list of conditions, and the following disclaimer,
124bc36eafdSMike Gerdts  *    without modification.
125bc36eafdSMike Gerdts  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126bc36eafdSMike Gerdts  *    substantially similar to the "NO WARRANTY" disclaimer below
127bc36eafdSMike Gerdts  *    ("Disclaimer") and any redistribution must be conditioned upon
128bc36eafdSMike Gerdts  *    including a substantially similar Disclaimer requirement for further
129bc36eafdSMike Gerdts  *    binary redistribution.
130bc36eafdSMike Gerdts  * 3. Neither the names of the above-listed copyright holders nor the names
131bc36eafdSMike Gerdts  *    of any contributors may be used to endorse or promote products derived
132bc36eafdSMike Gerdts  *    from this software without specific prior written permission.
133bc36eafdSMike Gerdts  *
134bc36eafdSMike Gerdts  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135bc36eafdSMike Gerdts  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136*35786f68SRobert Mustacchi  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137bc36eafdSMike Gerdts  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138*35786f68SRobert Mustacchi  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139*35786f68SRobert Mustacchi  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140*35786f68SRobert Mustacchi  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141*35786f68SRobert Mustacchi  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142*35786f68SRobert Mustacchi  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143*35786f68SRobert Mustacchi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144*35786f68SRobert Mustacchi  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145*35786f68SRobert Mustacchi  *
146*35786f68SRobert Mustacchi  * Alternatively, you may choose to be licensed under the terms of the
147*35786f68SRobert Mustacchi  * GNU General Public License ("GPL") version 2 as published by the Free
148*35786f68SRobert Mustacchi  * Software Foundation.
149*35786f68SRobert Mustacchi  *
150*35786f68SRobert Mustacchi  *****************************************************************************/
151bc36eafdSMike Gerdts 
152bc36eafdSMike Gerdts #include "aslcompiler.h"
153bc36eafdSMike Gerdts #include "aslcompiler.y.h"
154bc36eafdSMike Gerdts 
155bc36eafdSMike Gerdts #include "acparser.h"
156bc36eafdSMike Gerdts #include "amlcode.h"
157bc36eafdSMike Gerdts #include "acnamesp.h"
158bc36eafdSMike Gerdts 
159bc36eafdSMike Gerdts 
160bc36eafdSMike Gerdts #define _COMPONENT          ACPI_COMPILER
161bc36eafdSMike Gerdts         ACPI_MODULE_NAME    ("aslopt")
162bc36eafdSMike Gerdts 
163bc36eafdSMike Gerdts 
164bc36eafdSMike Gerdts static UINT32               OptTotal = 0;
165bc36eafdSMike Gerdts 
166bc36eafdSMike Gerdts /* Local prototypes */
167bc36eafdSMike Gerdts 
168bc36eafdSMike Gerdts static ACPI_STATUS
169bc36eafdSMike Gerdts OptSearchToRoot (
170bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
171bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
172bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
173bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
174bc36eafdSMike Gerdts     ACPI_BUFFER             *TargetPath,
175bc36eafdSMike Gerdts     char                    **NewPath);
176bc36eafdSMike Gerdts 
177bc36eafdSMike Gerdts static ACPI_STATUS
178bc36eafdSMike Gerdts OptBuildShortestPath (
179bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
180bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
181bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
182bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
183bc36eafdSMike Gerdts     ACPI_BUFFER             *CurrentPath,
184bc36eafdSMike Gerdts     ACPI_BUFFER             *TargetPath,
185bc36eafdSMike Gerdts     ACPI_SIZE               AmlNameStringLength,
186bc36eafdSMike Gerdts     UINT8                   IsDeclaration,
187bc36eafdSMike Gerdts     char                    **ReturnNewPath);
188bc36eafdSMike Gerdts 
189bc36eafdSMike Gerdts static ACPI_STATUS
190bc36eafdSMike Gerdts OptOptimizeNameDeclaration (
191bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
192bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
193bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
194bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
195bc36eafdSMike Gerdts     char                    *AmlNameString,
196bc36eafdSMike Gerdts     char                    **NewPath);
197bc36eafdSMike Gerdts 
198bc36eafdSMike Gerdts 
199bc36eafdSMike Gerdts /*******************************************************************************
200bc36eafdSMike Gerdts  *
201bc36eafdSMike Gerdts  * FUNCTION:    OptSearchToRoot
202bc36eafdSMike Gerdts  *
203bc36eafdSMike Gerdts  * PARAMETERS:  Op                  - Current parser op
204bc36eafdSMike Gerdts  *              WalkState           - Current state
205bc36eafdSMike Gerdts  *              CurrentNode         - Where we are in the namespace
206bc36eafdSMike Gerdts  *              TargetNode          - Node to which we are referring
207bc36eafdSMike Gerdts  *              TargetPath          - External full path to the target node
208bc36eafdSMike Gerdts  *              NewPath             - Where the optimized path is returned
209bc36eafdSMike Gerdts  *
210bc36eafdSMike Gerdts  * RETURN:      Status
211bc36eafdSMike Gerdts  *
212bc36eafdSMike Gerdts  * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
213bc36eafdSMike Gerdts  *              name utilizing the search-to-root name resolution algorithm
214bc36eafdSMike Gerdts  *              that is used by AML interpreters.
215bc36eafdSMike Gerdts  *
216bc36eafdSMike Gerdts  ******************************************************************************/
217bc36eafdSMike Gerdts 
218bc36eafdSMike Gerdts static ACPI_STATUS
OptSearchToRoot(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,ACPI_BUFFER * TargetPath,char ** NewPath)219bc36eafdSMike Gerdts OptSearchToRoot (
220bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
221bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
222bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
223bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
224bc36eafdSMike Gerdts     ACPI_BUFFER             *TargetPath,
225bc36eafdSMike Gerdts     char                    **NewPath)
226bc36eafdSMike Gerdts {
227bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *Node;
228bc36eafdSMike Gerdts     ACPI_GENERIC_STATE      ScopeInfo;
229bc36eafdSMike Gerdts     ACPI_STATUS             Status;
230bc36eafdSMike Gerdts     char                    *Path;
231bc36eafdSMike Gerdts 
232bc36eafdSMike Gerdts 
233bc36eafdSMike Gerdts     ACPI_FUNCTION_NAME (OptSearchToRoot);
234bc36eafdSMike Gerdts 
235bc36eafdSMike Gerdts 
236bc36eafdSMike Gerdts     /*
237bc36eafdSMike Gerdts      * Check if search-to-root can be utilized. Use the last NameSeg of
238bc36eafdSMike Gerdts      * the NamePath and 1) See if can be found and 2) If found, make
239bc36eafdSMike Gerdts      * sure that it is the same node that we want. If there is another
240bc36eafdSMike Gerdts      * name in the search path before the one we want, the nodes will
241bc36eafdSMike Gerdts      * not match, and we cannot use this optimization.
242bc36eafdSMike Gerdts      */
243bc36eafdSMike Gerdts     Path = &(((char *) TargetPath->Pointer)[
244*35786f68SRobert Mustacchi         TargetPath->Length - ACPI_NAME_SIZE]);
245bc36eafdSMike Gerdts     ScopeInfo.Scope.Node = CurrentNode;
246bc36eafdSMike Gerdts 
247bc36eafdSMike Gerdts     /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
248bc36eafdSMike Gerdts 
249bc36eafdSMike Gerdts     Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
250bc36eafdSMike Gerdts         ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
251bc36eafdSMike Gerdts         WalkState, &(Node));
252bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
253bc36eafdSMike Gerdts     {
254bc36eafdSMike Gerdts         return (Status);
255bc36eafdSMike Gerdts     }
256bc36eafdSMike Gerdts 
257bc36eafdSMike Gerdts     /*
258bc36eafdSMike Gerdts      * We found the name, but we must check to make sure that the node
259bc36eafdSMike Gerdts      * matches. Otherwise, there is another identical name in the search
260bc36eafdSMike Gerdts      * path that precludes the use of this optimization.
261bc36eafdSMike Gerdts      */
262bc36eafdSMike Gerdts     if (Node != TargetNode)
263bc36eafdSMike Gerdts     {
264bc36eafdSMike Gerdts         /*
265bc36eafdSMike Gerdts          * This means that another object with the same name was found first,
266bc36eafdSMike Gerdts          * and we cannot use this optimization.
267bc36eafdSMike Gerdts          */
268bc36eafdSMike Gerdts         return (AE_NOT_FOUND);
269bc36eafdSMike Gerdts     }
270bc36eafdSMike Gerdts 
271bc36eafdSMike Gerdts     /* Found the node, we can use this optimization */
272bc36eafdSMike Gerdts 
273bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
274bc36eafdSMike Gerdts         "NAMESEG:   %-24s", Path));
275bc36eafdSMike Gerdts 
276bc36eafdSMike Gerdts     /* We must allocate a new string for the name (TargetPath gets deleted) */
277bc36eafdSMike Gerdts 
278*35786f68SRobert Mustacchi     *NewPath = UtLocalCacheCalloc (ACPI_NAME_SIZE + 1);
279bc36eafdSMike Gerdts     strcpy (*NewPath, Path);
280bc36eafdSMike Gerdts 
281bc36eafdSMike Gerdts     if (strncmp (*NewPath, "_T_", 3))
282bc36eafdSMike Gerdts     {
283bc36eafdSMike Gerdts         AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION,
284bc36eafdSMike Gerdts             Op, *NewPath);
285bc36eafdSMike Gerdts     }
286bc36eafdSMike Gerdts 
287bc36eafdSMike Gerdts     return (AE_OK);
288bc36eafdSMike Gerdts }
289bc36eafdSMike Gerdts 
290bc36eafdSMike Gerdts 
291bc36eafdSMike Gerdts /*******************************************************************************
292bc36eafdSMike Gerdts  *
293bc36eafdSMike Gerdts  * FUNCTION:    OptBuildShortestPath
294bc36eafdSMike Gerdts  *
295bc36eafdSMike Gerdts  * PARAMETERS:  Op                  - Current parser op
296bc36eafdSMike Gerdts  *              WalkState           - Current state
297bc36eafdSMike Gerdts  *              CurrentNode         - Where we are in the namespace
298bc36eafdSMike Gerdts  *              TargetNode          - Node to which we are referring
299bc36eafdSMike Gerdts  *              CurrentPath         - External full path to the current node
300bc36eafdSMike Gerdts  *              TargetPath          - External full path to the target node
301bc36eafdSMike Gerdts  *              AmlNameStringLength - Length of the original namepath
302bc36eafdSMike Gerdts  *              IsDeclaration       - TRUE for declaration, FALSE for reference
303bc36eafdSMike Gerdts  *              ReturnNewPath       - Where the optimized path is returned
304bc36eafdSMike Gerdts  *
305bc36eafdSMike Gerdts  * RETURN:      Status
306bc36eafdSMike Gerdts  *
307bc36eafdSMike Gerdts  * DESCRIPTION: Build an optimal NamePath using carats
308bc36eafdSMike Gerdts  *
309bc36eafdSMike Gerdts  ******************************************************************************/
310bc36eafdSMike Gerdts 
311bc36eafdSMike Gerdts static ACPI_STATUS
OptBuildShortestPath(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,ACPI_BUFFER * CurrentPath,ACPI_BUFFER * TargetPath,ACPI_SIZE AmlNameStringLength,UINT8 IsDeclaration,char ** ReturnNewPath)312bc36eafdSMike Gerdts OptBuildShortestPath (
313bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
314bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
315bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
316bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
317bc36eafdSMike Gerdts     ACPI_BUFFER             *CurrentPath,
318bc36eafdSMike Gerdts     ACPI_BUFFER             *TargetPath,
319bc36eafdSMike Gerdts     ACPI_SIZE               AmlNameStringLength,
320bc36eafdSMike Gerdts     UINT8                   IsDeclaration,
321bc36eafdSMike Gerdts     char                    **ReturnNewPath)
322bc36eafdSMike Gerdts {
323bc36eafdSMike Gerdts     UINT32                  NumCommonSegments;
324bc36eafdSMike Gerdts     UINT32                  MaxCommonSegments;
325bc36eafdSMike Gerdts     UINT32                  Index;
326bc36eafdSMike Gerdts     UINT32                  NumCarats;
327bc36eafdSMike Gerdts     UINT32                  i;
328bc36eafdSMike Gerdts     char                    *NewPathInternal;
329bc36eafdSMike Gerdts     char                    *NewPathExternal;
330bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *Node;
331bc36eafdSMike Gerdts     ACPI_GENERIC_STATE      ScopeInfo;
332bc36eafdSMike Gerdts     ACPI_STATUS             Status;
333bc36eafdSMike Gerdts     BOOLEAN                 SubPath = FALSE;
334bc36eafdSMike Gerdts 
335bc36eafdSMike Gerdts 
336bc36eafdSMike Gerdts     ACPI_FUNCTION_NAME (OptBuildShortestPath);
337bc36eafdSMike Gerdts 
338bc36eafdSMike Gerdts 
339bc36eafdSMike Gerdts     ScopeInfo.Scope.Node = CurrentNode;
340bc36eafdSMike Gerdts 
341bc36eafdSMike Gerdts     /*
342bc36eafdSMike Gerdts      * Determine the maximum number of NameSegs that the Target and Current paths
343bc36eafdSMike Gerdts      * can possibly have in common. (To optimize, we have to have at least 1)
344bc36eafdSMike Gerdts      *
345bc36eafdSMike Gerdts      * Note: The external NamePath string lengths are always a multiple of 5
346bc36eafdSMike Gerdts      * (ACPI_NAME_SIZE + separator)
347bc36eafdSMike Gerdts      */
348bc36eafdSMike Gerdts     MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
349bc36eafdSMike Gerdts     if (CurrentPath->Length < TargetPath->Length)
350bc36eafdSMike Gerdts     {
351bc36eafdSMike Gerdts         MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
352bc36eafdSMike Gerdts     }
353bc36eafdSMike Gerdts 
354bc36eafdSMike Gerdts     /*
355bc36eafdSMike Gerdts      * Determine how many NameSegs the two paths have in common.
356bc36eafdSMike Gerdts      * (Starting from the root)
357bc36eafdSMike Gerdts      */
358bc36eafdSMike Gerdts     for (NumCommonSegments = 0;
359bc36eafdSMike Gerdts          NumCommonSegments < MaxCommonSegments;
360bc36eafdSMike Gerdts          NumCommonSegments++)
361bc36eafdSMike Gerdts     {
362bc36eafdSMike Gerdts         /* Compare two single NameSegs */
363bc36eafdSMike Gerdts 
364bc36eafdSMike Gerdts         Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
365bc36eafdSMike Gerdts 
366bc36eafdSMike Gerdts         if (!ACPI_COMPARE_NAME (
367bc36eafdSMike Gerdts             &(ACPI_CAST_PTR (char, TargetPath->Pointer)) [Index],
368bc36eafdSMike Gerdts             &(ACPI_CAST_PTR (char, CurrentPath->Pointer)) [Index]))
369bc36eafdSMike Gerdts         {
370bc36eafdSMike Gerdts             /* Mismatch */
371bc36eafdSMike Gerdts 
372bc36eafdSMike Gerdts             break;
373bc36eafdSMike Gerdts         }
374bc36eafdSMike Gerdts     }
375bc36eafdSMike Gerdts 
376bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %u",
377bc36eafdSMike Gerdts         NumCommonSegments));
378bc36eafdSMike Gerdts 
379bc36eafdSMike Gerdts     /* There must be at least 1 common NameSeg in order to optimize */
380bc36eafdSMike Gerdts 
381bc36eafdSMike Gerdts     if (NumCommonSegments == 0)
382bc36eafdSMike Gerdts     {
383bc36eafdSMike Gerdts         return (AE_NOT_FOUND);
384bc36eafdSMike Gerdts     }
385bc36eafdSMike Gerdts 
386bc36eafdSMike Gerdts     if (NumCommonSegments == MaxCommonSegments)
387bc36eafdSMike Gerdts     {
388bc36eafdSMike Gerdts         if (CurrentPath->Length == TargetPath->Length)
389bc36eafdSMike Gerdts         {
390bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
391bc36eafdSMike Gerdts             return (AE_NOT_FOUND);
392bc36eafdSMike Gerdts         }
393bc36eafdSMike Gerdts         else
394bc36eafdSMike Gerdts         {
395bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
396bc36eafdSMike Gerdts             SubPath = TRUE;
397bc36eafdSMike Gerdts         }
398bc36eafdSMike Gerdts     }
399bc36eafdSMike Gerdts 
400bc36eafdSMike Gerdts     /* Determine how many prefix Carats are required */
401bc36eafdSMike Gerdts 
402bc36eafdSMike Gerdts     NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
403bc36eafdSMike Gerdts         NumCommonSegments;
404bc36eafdSMike Gerdts 
405bc36eafdSMike Gerdts     /*
406bc36eafdSMike Gerdts      * Construct a new target string
407bc36eafdSMike Gerdts      */
408bc36eafdSMike Gerdts     NewPathExternal =
409bc36eafdSMike Gerdts         ACPI_ALLOCATE_ZEROED (TargetPath->Length + NumCarats + 1);
410bc36eafdSMike Gerdts 
411bc36eafdSMike Gerdts     /* Insert the Carats into the Target string */
412bc36eafdSMike Gerdts 
413bc36eafdSMike Gerdts     for (i = 0; i < NumCarats; i++)
414bc36eafdSMike Gerdts     {
415bc36eafdSMike Gerdts         NewPathExternal[i] = AML_PARENT_PREFIX;
416bc36eafdSMike Gerdts     }
417bc36eafdSMike Gerdts 
418bc36eafdSMike Gerdts     /*
419bc36eafdSMike Gerdts      * Copy only the necessary (optimal) segments from the original
420bc36eafdSMike Gerdts      * target string
421bc36eafdSMike Gerdts      */
422bc36eafdSMike Gerdts     Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
423bc36eafdSMike Gerdts 
424bc36eafdSMike Gerdts     /* Special handling for exact subpath in a name declaration */
425bc36eafdSMike Gerdts 
426bc36eafdSMike Gerdts     if (IsDeclaration && SubPath &&
427bc36eafdSMike Gerdts         (CurrentPath->Length > TargetPath->Length))
428bc36eafdSMike Gerdts     {
429bc36eafdSMike Gerdts         /*
430bc36eafdSMike Gerdts          * The current path is longer than the target, and the target is a
431bc36eafdSMike Gerdts          * subpath of the current path. We must include one more NameSeg of
432bc36eafdSMike Gerdts          * the target path
433bc36eafdSMike Gerdts          */
434bc36eafdSMike Gerdts         Index -= ACPI_PATH_SEGMENT_LENGTH;
435bc36eafdSMike Gerdts 
436bc36eafdSMike Gerdts         /* Special handling for Scope() operator */
437bc36eafdSMike Gerdts 
438bc36eafdSMike Gerdts         if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
439bc36eafdSMike Gerdts         {
440bc36eafdSMike Gerdts             NewPathExternal[i] = AML_PARENT_PREFIX;
441bc36eafdSMike Gerdts             i++;
442bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
443bc36eafdSMike Gerdts         }
444bc36eafdSMike Gerdts     }
445bc36eafdSMike Gerdts 
446bc36eafdSMike Gerdts     /* Make sure we haven't gone off the end of the target path */
447bc36eafdSMike Gerdts 
448bc36eafdSMike Gerdts     if (Index > TargetPath->Length)
449bc36eafdSMike Gerdts     {
450bc36eafdSMike Gerdts         Index = TargetPath->Length;
451bc36eafdSMike Gerdts     }
452bc36eafdSMike Gerdts 
453bc36eafdSMike Gerdts     strcpy (&NewPathExternal[i],
454bc36eafdSMike Gerdts         &(ACPI_CAST_PTR (char, TargetPath->Pointer))[Index]);
455bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
456bc36eafdSMike Gerdts 
457bc36eafdSMike Gerdts     /*
458bc36eafdSMike Gerdts      * Internalize the new target string and check it against the original
459bc36eafdSMike Gerdts      * string to make sure that this is in fact an optimization. If the
460bc36eafdSMike Gerdts      * original string is already optimal, there is no point in continuing.
461bc36eafdSMike Gerdts      */
462bc36eafdSMike Gerdts     Status = AcpiNsInternalizeName (NewPathExternal, &NewPathInternal);
463bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
464bc36eafdSMike Gerdts     {
465bc36eafdSMike Gerdts         AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
466bc36eafdSMike Gerdts             ASL_NO_ABORT);
467bc36eafdSMike Gerdts         goto Cleanup;
468bc36eafdSMike Gerdts     }
469bc36eafdSMike Gerdts 
470bc36eafdSMike Gerdts     if (strlen (NewPathInternal) >= AmlNameStringLength)
471bc36eafdSMike Gerdts     {
472bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
473bc36eafdSMike Gerdts             " NOT SHORTER (New %u old %u)",
474bc36eafdSMike Gerdts             (UINT32) strlen (NewPathInternal),
475bc36eafdSMike Gerdts             (UINT32) AmlNameStringLength));
476bc36eafdSMike Gerdts 
477bc36eafdSMike Gerdts         ACPI_FREE (NewPathInternal);
478bc36eafdSMike Gerdts         Status = AE_NOT_FOUND;
479bc36eafdSMike Gerdts         goto Cleanup;
480bc36eafdSMike Gerdts     }
481bc36eafdSMike Gerdts 
482bc36eafdSMike Gerdts     /*
483bc36eafdSMike Gerdts      * Check to make sure that the optimization finds the node we are
484bc36eafdSMike Gerdts      * looking for. This is simply a sanity check on the new
485bc36eafdSMike Gerdts      * path that has been created.
486bc36eafdSMike Gerdts      */
487bc36eafdSMike Gerdts     Status = AcpiNsLookup (&ScopeInfo, NewPathInternal,
488bc36eafdSMike Gerdts         ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
489bc36eafdSMike Gerdts         ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
490bc36eafdSMike Gerdts     if (ACPI_SUCCESS (Status))
491bc36eafdSMike Gerdts     {
492bc36eafdSMike Gerdts         /* Found the namepath, but make sure the node is correct */
493bc36eafdSMike Gerdts 
494bc36eafdSMike Gerdts         if (Node == TargetNode)
495bc36eafdSMike Gerdts         {
496bc36eafdSMike Gerdts             /* The lookup matched the node, accept this optimization */
497bc36eafdSMike Gerdts 
498bc36eafdSMike Gerdts             AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
499bc36eafdSMike Gerdts                 Op, NewPathExternal);
500bc36eafdSMike Gerdts             *ReturnNewPath = NewPathInternal;
501bc36eafdSMike Gerdts         }
502bc36eafdSMike Gerdts         else
503bc36eafdSMike Gerdts         {
504bc36eafdSMike Gerdts             /* Node is not correct, do not use this optimization */
505bc36eafdSMike Gerdts 
506bc36eafdSMike Gerdts             Status = AE_NOT_FOUND;
507bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
508bc36eafdSMike Gerdts             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
509bc36eafdSMike Gerdts                 "Not using optimized name - found wrong node");
510bc36eafdSMike Gerdts         }
511bc36eafdSMike Gerdts     }
512bc36eafdSMike Gerdts     else
513bc36eafdSMike Gerdts     {
514bc36eafdSMike Gerdts         /* The lookup failed, we obviously cannot use this optimization */
515bc36eafdSMike Gerdts 
516bc36eafdSMike Gerdts         ACPI_FREE (NewPathInternal);
517bc36eafdSMike Gerdts 
518bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
519bc36eafdSMike Gerdts         AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
520bc36eafdSMike Gerdts             "Not using optimized name - did not find node");
521bc36eafdSMike Gerdts     }
522bc36eafdSMike Gerdts 
523bc36eafdSMike Gerdts Cleanup:
524bc36eafdSMike Gerdts 
525bc36eafdSMike Gerdts     ACPI_FREE (NewPathExternal);
526bc36eafdSMike Gerdts     return (Status);
527bc36eafdSMike Gerdts }
528bc36eafdSMike Gerdts 
529bc36eafdSMike Gerdts 
530bc36eafdSMike Gerdts /*******************************************************************************
531bc36eafdSMike Gerdts  *
532bc36eafdSMike Gerdts  * FUNCTION:    OptOptimizeNameDeclaration
533bc36eafdSMike Gerdts  *
534bc36eafdSMike Gerdts  * PARAMETERS:  Op                  - Current parser op
535bc36eafdSMike Gerdts  *              WalkState           - Current state
536bc36eafdSMike Gerdts  *              CurrentNode         - Where we are in the namespace
537bc36eafdSMike Gerdts  *              AmlNameString       - Unoptimized namepath
538bc36eafdSMike Gerdts  *              NewPath             - Where the optimized path is returned
539bc36eafdSMike Gerdts  *
540bc36eafdSMike Gerdts  * RETURN:      Status. AE_OK If path is optimized
541bc36eafdSMike Gerdts  *
542bc36eafdSMike Gerdts  * DESCRIPTION: Perform a simple optimization of removing an extraneous
543bc36eafdSMike Gerdts  *              backslash prefix if we are already at the root scope.
544bc36eafdSMike Gerdts  *
545bc36eafdSMike Gerdts  ******************************************************************************/
546bc36eafdSMike Gerdts 
547bc36eafdSMike Gerdts static ACPI_STATUS
OptOptimizeNameDeclaration(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,char * AmlNameString,char ** NewPath)548bc36eafdSMike Gerdts OptOptimizeNameDeclaration (
549bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
550bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
551bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode,
552bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode,
553bc36eafdSMike Gerdts     char                    *AmlNameString,
554bc36eafdSMike Gerdts     char                    **NewPath)
555bc36eafdSMike Gerdts {
556bc36eafdSMike Gerdts     ACPI_STATUS             Status;
557bc36eafdSMike Gerdts     char                    *NewPathExternal;
558bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *Node;
559bc36eafdSMike Gerdts 
560bc36eafdSMike Gerdts 
561bc36eafdSMike Gerdts     ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
562bc36eafdSMike Gerdts 
563bc36eafdSMike Gerdts 
564bc36eafdSMike Gerdts     if (((CurrentNode == AcpiGbl_RootNode) ||
565bc36eafdSMike Gerdts         (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)) &&
566bc36eafdSMike Gerdts             (ACPI_IS_ROOT_PREFIX (AmlNameString[0])))
567bc36eafdSMike Gerdts     {
568bc36eafdSMike Gerdts         /*
569bc36eafdSMike Gerdts          * The current scope is the root, and the namepath has a root prefix
570bc36eafdSMike Gerdts          * that is therefore extraneous. Remove it.
571bc36eafdSMike Gerdts          */
572bc36eafdSMike Gerdts         *NewPath = &AmlNameString[1];
573bc36eafdSMike Gerdts 
574bc36eafdSMike Gerdts         /* Debug output */
575bc36eafdSMike Gerdts 
576bc36eafdSMike Gerdts         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
577bc36eafdSMike Gerdts             NULL, &NewPathExternal);
578bc36eafdSMike Gerdts         if (ACPI_FAILURE (Status))
579bc36eafdSMike Gerdts         {
580bc36eafdSMike Gerdts             AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
581bc36eafdSMike Gerdts                 ASL_NO_ABORT);
582bc36eafdSMike Gerdts             return (Status);
583bc36eafdSMike Gerdts         }
584bc36eafdSMike Gerdts 
585bc36eafdSMike Gerdts         /*
586bc36eafdSMike Gerdts          * Check to make sure that the optimization finds the node we are
587bc36eafdSMike Gerdts          * looking for. This is simply a sanity check on the new
588bc36eafdSMike Gerdts          * path that has been created.
589bc36eafdSMike Gerdts          *
590bc36eafdSMike Gerdts          * We know that we are at the root, so NULL is used for the scope.
591bc36eafdSMike Gerdts          */
592bc36eafdSMike Gerdts         Status = AcpiNsLookup (NULL, *NewPath,
593bc36eafdSMike Gerdts             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
594bc36eafdSMike Gerdts             ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
595bc36eafdSMike Gerdts         if (ACPI_SUCCESS (Status))
596bc36eafdSMike Gerdts         {
597bc36eafdSMike Gerdts             /* Found the namepath, but make sure the node is correct */
598bc36eafdSMike Gerdts 
599bc36eafdSMike Gerdts             if (Node == TargetNode)
600bc36eafdSMike Gerdts             {
601bc36eafdSMike Gerdts                 /* The lookup matched the node, accept this optimization */
602bc36eafdSMike Gerdts 
603bc36eafdSMike Gerdts                 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
604bc36eafdSMike Gerdts                     Op, NewPathExternal);
605bc36eafdSMike Gerdts 
606bc36eafdSMike Gerdts                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
607bc36eafdSMike Gerdts                     "AT ROOT:   %-24s", NewPathExternal));
608bc36eafdSMike Gerdts             }
609bc36eafdSMike Gerdts             else
610bc36eafdSMike Gerdts             {
611bc36eafdSMike Gerdts                 /* Node is not correct, do not use this optimization */
612bc36eafdSMike Gerdts 
613bc36eafdSMike Gerdts                 Status = AE_NOT_FOUND;
614bc36eafdSMike Gerdts                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
615bc36eafdSMike Gerdts                     " ***** WRONG NODE"));
616bc36eafdSMike Gerdts                 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
617bc36eafdSMike Gerdts                     "Not using optimized name - found wrong node");
618bc36eafdSMike Gerdts             }
619bc36eafdSMike Gerdts         }
620bc36eafdSMike Gerdts         else
621bc36eafdSMike Gerdts         {
622bc36eafdSMike Gerdts             /* The lookup failed, we obviously cannot use this optimization */
623bc36eafdSMike Gerdts 
624bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
625bc36eafdSMike Gerdts                 " ***** NOT FOUND"));
626bc36eafdSMike Gerdts             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
627bc36eafdSMike Gerdts                 "Not using optimized name - did not find node");
628bc36eafdSMike Gerdts         }
629bc36eafdSMike Gerdts 
630bc36eafdSMike Gerdts         ACPI_FREE (NewPathExternal);
631bc36eafdSMike Gerdts         return (Status);
632bc36eafdSMike Gerdts     }
633bc36eafdSMike Gerdts 
634bc36eafdSMike Gerdts     /* Could not optimize */
635bc36eafdSMike Gerdts 
636bc36eafdSMike Gerdts     return (AE_NOT_FOUND);
637bc36eafdSMike Gerdts }
638bc36eafdSMike Gerdts 
639bc36eafdSMike Gerdts 
640bc36eafdSMike Gerdts /*******************************************************************************
641bc36eafdSMike Gerdts  *
642bc36eafdSMike Gerdts  * FUNCTION:    OptOptimizeNamePath
643bc36eafdSMike Gerdts  *
644bc36eafdSMike Gerdts  * PARAMETERS:  Op                  - Current parser op
645bc36eafdSMike Gerdts  *              Flags               - Opcode info flags
646bc36eafdSMike Gerdts  *              WalkState           - Current state
647bc36eafdSMike Gerdts  *              AmlNameString       - Unoptimized namepath
648bc36eafdSMike Gerdts  *              TargetNode          - Node to which AmlNameString refers
649bc36eafdSMike Gerdts  *
650bc36eafdSMike Gerdts  * RETURN:      None. If path is optimized, the Op is updated with new path
651bc36eafdSMike Gerdts  *
652bc36eafdSMike Gerdts  * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
653bc36eafdSMike Gerdts  *              Must take into account both the current location in the
654bc36eafdSMike Gerdts  *              namespace and the actual reference path.
655bc36eafdSMike Gerdts  *
656bc36eafdSMike Gerdts  ******************************************************************************/
657bc36eafdSMike Gerdts 
658bc36eafdSMike Gerdts void
OptOptimizeNamePath(ACPI_PARSE_OBJECT * Op,UINT32 Flags,ACPI_WALK_STATE * WalkState,char * AmlNameString,ACPI_NAMESPACE_NODE * TargetNode)659bc36eafdSMike Gerdts OptOptimizeNamePath (
660bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *Op,
661bc36eafdSMike Gerdts     UINT32                  Flags,
662bc36eafdSMike Gerdts     ACPI_WALK_STATE         *WalkState,
663bc36eafdSMike Gerdts     char                    *AmlNameString,
664bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *TargetNode)
665bc36eafdSMike Gerdts {
666bc36eafdSMike Gerdts     ACPI_STATUS             Status;
667bc36eafdSMike Gerdts     ACPI_BUFFER             TargetPath;
668bc36eafdSMike Gerdts     ACPI_BUFFER             CurrentPath;
669bc36eafdSMike Gerdts     ACPI_SIZE               AmlNameStringLength;
670bc36eafdSMike Gerdts     ACPI_NAMESPACE_NODE     *CurrentNode;
671bc36eafdSMike Gerdts     char                    *ExternalNameString;
672bc36eafdSMike Gerdts     char                    *NewPath = NULL;
673bc36eafdSMike Gerdts     ACPI_SIZE               HowMuchShorter;
674bc36eafdSMike Gerdts     ACPI_PARSE_OBJECT       *NextOp;
675bc36eafdSMike Gerdts 
676bc36eafdSMike Gerdts 
677bc36eafdSMike Gerdts     ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
678bc36eafdSMike Gerdts 
679bc36eafdSMike Gerdts 
680bc36eafdSMike Gerdts     /* This is an optional optimization */
681bc36eafdSMike Gerdts 
682bc36eafdSMike Gerdts     if (!Gbl_ReferenceOptimizationFlag)
683bc36eafdSMike Gerdts     {
684bc36eafdSMike Gerdts         return_VOID;
685bc36eafdSMike Gerdts     }
686bc36eafdSMike Gerdts 
687bc36eafdSMike Gerdts     /* Various required items */
688bc36eafdSMike Gerdts 
689bc36eafdSMike Gerdts     if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
690bc36eafdSMike Gerdts     {
691bc36eafdSMike Gerdts         return_VOID;
692bc36eafdSMike Gerdts     }
693bc36eafdSMike Gerdts 
694bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
695bc36eafdSMike Gerdts         "PATH OPTIMIZE: Line %5d ParentOp [%12.12s] ThisOp [%12.12s] ",
696bc36eafdSMike Gerdts         Op->Asl.LogicalLineNumber,
697bc36eafdSMike Gerdts         AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
698bc36eafdSMike Gerdts         AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
699bc36eafdSMike Gerdts 
700bc36eafdSMike Gerdts     if (!(Flags & (AML_NAMED | AML_CREATE)))
701bc36eafdSMike Gerdts     {
702*35786f68SRobert Mustacchi         if (Op->Asl.CompileFlags & OP_IS_NAME_DECLARATION)
703bc36eafdSMike Gerdts         {
704bc36eafdSMike Gerdts             /* We don't want to fuss with actual name declaration nodes here */
705bc36eafdSMike Gerdts 
706bc36eafdSMike Gerdts             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
707bc36eafdSMike Gerdts                 "******* NAME DECLARATION\n"));
708bc36eafdSMike Gerdts             return_VOID;
709bc36eafdSMike Gerdts         }
710bc36eafdSMike Gerdts     }
711bc36eafdSMike Gerdts 
712bc36eafdSMike Gerdts     /*
713bc36eafdSMike Gerdts      * The original path must be longer than one NameSeg (4 chars) for there
714bc36eafdSMike Gerdts      * to be any possibility that it can be optimized to a shorter string
715bc36eafdSMike Gerdts      */
716bc36eafdSMike Gerdts     AmlNameStringLength = strlen (AmlNameString);
717bc36eafdSMike Gerdts     if (AmlNameStringLength <= ACPI_NAME_SIZE)
718bc36eafdSMike Gerdts     {
719bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
720bc36eafdSMike Gerdts             "NAMESEG %4.4s\n", AmlNameString));
721bc36eafdSMike Gerdts         return_VOID;
722bc36eafdSMike Gerdts     }
723bc36eafdSMike Gerdts 
724bc36eafdSMike Gerdts     /*
725bc36eafdSMike Gerdts      * We need to obtain the node that represents the current scope -- where
726bc36eafdSMike Gerdts      * we are right now in the namespace. We will compare this path
727bc36eafdSMike Gerdts      * against the Namepath, looking for commonality.
728bc36eafdSMike Gerdts      */
729bc36eafdSMike Gerdts     CurrentNode = AcpiGbl_RootNode;
730bc36eafdSMike Gerdts     if (WalkState->ScopeInfo)
731bc36eafdSMike Gerdts     {
732bc36eafdSMike Gerdts         CurrentNode = WalkState->ScopeInfo->Scope.Node;
733bc36eafdSMike Gerdts     }
734bc36eafdSMike Gerdts 
735bc36eafdSMike Gerdts     if (Flags & (AML_NAMED | AML_CREATE))
736bc36eafdSMike Gerdts     {
737bc36eafdSMike Gerdts         /* This is the declaration of a new name */
738bc36eafdSMike Gerdts 
739bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME\n"));
740bc36eafdSMike Gerdts 
741bc36eafdSMike Gerdts         /*
742bc36eafdSMike Gerdts          * The node of interest is the parent of this node (the containing
743bc36eafdSMike Gerdts          * scope). The actual namespace node may be up more than one level
744bc36eafdSMike Gerdts          * of parse op or it may not exist at all (if we traverse back
745bc36eafdSMike Gerdts          * up to the root.)
746bc36eafdSMike Gerdts          */
747bc36eafdSMike Gerdts         NextOp = Op->Asl.Parent;
748bc36eafdSMike Gerdts         while (NextOp && (!NextOp->Asl.Node))
749bc36eafdSMike Gerdts         {
750bc36eafdSMike Gerdts             NextOp = NextOp->Asl.Parent;
751bc36eafdSMike Gerdts         }
752bc36eafdSMike Gerdts 
753bc36eafdSMike Gerdts         if (NextOp && NextOp->Asl.Node)
754bc36eafdSMike Gerdts         {
755bc36eafdSMike Gerdts             CurrentNode = NextOp->Asl.Node;
756bc36eafdSMike Gerdts         }
757bc36eafdSMike Gerdts         else
758bc36eafdSMike Gerdts         {
759bc36eafdSMike Gerdts             CurrentNode = AcpiGbl_RootNode;
760bc36eafdSMike Gerdts         }
761bc36eafdSMike Gerdts     }
762bc36eafdSMike Gerdts     else
763bc36eafdSMike Gerdts     {
764bc36eafdSMike Gerdts         /* This is a reference to an existing named object */
765bc36eafdSMike Gerdts 
766bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REFERENCE\n"));
767bc36eafdSMike Gerdts     }
768bc36eafdSMike Gerdts 
769bc36eafdSMike Gerdts     /*
770bc36eafdSMike Gerdts      * Obtain the full paths to the two nodes that we are interested in
771bc36eafdSMike Gerdts      * (Target and current namespace location) in external
772bc36eafdSMike Gerdts      * format -- something we can easily manipulate
773bc36eafdSMike Gerdts      */
774bc36eafdSMike Gerdts     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
775bc36eafdSMike Gerdts     Status = AcpiNsHandleToPathname (TargetNode, &TargetPath, FALSE);
776bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
777bc36eafdSMike Gerdts     {
778bc36eafdSMike Gerdts         AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
779bc36eafdSMike Gerdts             ASL_NO_ABORT);
780bc36eafdSMike Gerdts         return_VOID;
781bc36eafdSMike Gerdts     }
782bc36eafdSMike Gerdts 
783bc36eafdSMike Gerdts     TargetPath.Length--;    /* Subtract one for null terminator */
784bc36eafdSMike Gerdts 
785bc36eafdSMike Gerdts     /* CurrentPath is the path to this scope (where we are in the namespace) */
786bc36eafdSMike Gerdts 
787bc36eafdSMike Gerdts     CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
788bc36eafdSMike Gerdts     Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath, FALSE);
789bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
790bc36eafdSMike Gerdts     {
791bc36eafdSMike Gerdts         AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
792bc36eafdSMike Gerdts             ASL_NO_ABORT);
793bc36eafdSMike Gerdts         return_VOID;
794bc36eafdSMike Gerdts     }
795bc36eafdSMike Gerdts 
796bc36eafdSMike Gerdts     CurrentPath.Length--;   /* Subtract one for null terminator */
797bc36eafdSMike Gerdts 
798bc36eafdSMike Gerdts     /* Debug output only */
799bc36eafdSMike Gerdts 
800bc36eafdSMike Gerdts     Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
801bc36eafdSMike Gerdts         NULL, &ExternalNameString);
802bc36eafdSMike Gerdts     if (ACPI_FAILURE (Status))
803bc36eafdSMike Gerdts     {
804bc36eafdSMike Gerdts         AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
805bc36eafdSMike Gerdts             ASL_NO_ABORT);
806bc36eafdSMike Gerdts         return_VOID;
807bc36eafdSMike Gerdts     }
808bc36eafdSMike Gerdts 
809bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
810bc36eafdSMike Gerdts         "CURRENT SCOPE: (%2u) %-37s FULL PATH TO NAME: (%2u) %-32s ACTUAL AML:%-32s\n",
811bc36eafdSMike Gerdts         (UINT32) CurrentPath.Length, (char *) CurrentPath.Pointer,
812bc36eafdSMike Gerdts         (UINT32) TargetPath.Length, (char *) TargetPath.Pointer,
813bc36eafdSMike Gerdts         ExternalNameString));
814bc36eafdSMike Gerdts 
815bc36eafdSMike Gerdts     ACPI_FREE (ExternalNameString);
816bc36eafdSMike Gerdts 
817bc36eafdSMike Gerdts     /*
818bc36eafdSMike Gerdts      * Attempt an optmization depending on the type of namepath
819bc36eafdSMike Gerdts      */
820bc36eafdSMike Gerdts     if (Flags & (AML_NAMED | AML_CREATE))
821bc36eafdSMike Gerdts     {
822bc36eafdSMike Gerdts         /*
823bc36eafdSMike Gerdts          * This is a named opcode and the namepath is a name declaration, not
824bc36eafdSMike Gerdts          * a reference.
825bc36eafdSMike Gerdts          */
826bc36eafdSMike Gerdts         Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
827bc36eafdSMike Gerdts             TargetNode, AmlNameString, &NewPath);
828bc36eafdSMike Gerdts         if (ACPI_FAILURE (Status))
829bc36eafdSMike Gerdts         {
830bc36eafdSMike Gerdts             /*
831bc36eafdSMike Gerdts              * 2) now attempt to
832bc36eafdSMike Gerdts              *    optimize the namestring with carats (up-arrow)
833bc36eafdSMike Gerdts              */
834bc36eafdSMike Gerdts             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
835bc36eafdSMike Gerdts                 TargetNode, &CurrentPath, &TargetPath,
836bc36eafdSMike Gerdts                 AmlNameStringLength, 1, &NewPath);
837bc36eafdSMike Gerdts         }
838bc36eafdSMike Gerdts     }
839bc36eafdSMike Gerdts     else
840bc36eafdSMike Gerdts     {
841bc36eafdSMike Gerdts         /*
842bc36eafdSMike Gerdts          * This is a reference to an existing named object
843bc36eafdSMike Gerdts          *
844bc36eafdSMike Gerdts          * 1) Check if search-to-root can be utilized using the last
845bc36eafdSMike Gerdts          *    NameSeg of the NamePath
846bc36eafdSMike Gerdts          */
847bc36eafdSMike Gerdts         Status = OptSearchToRoot (Op, WalkState, CurrentNode,
848bc36eafdSMike Gerdts             TargetNode, &TargetPath, &NewPath);
849bc36eafdSMike Gerdts         if (ACPI_FAILURE (Status))
850bc36eafdSMike Gerdts         {
851bc36eafdSMike Gerdts             /*
852bc36eafdSMike Gerdts              * 2) Search-to-root could not be used, now attempt to
853bc36eafdSMike Gerdts              *    optimize the namestring with carats (up-arrow)
854bc36eafdSMike Gerdts              */
855bc36eafdSMike Gerdts             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
856bc36eafdSMike Gerdts                 TargetNode, &CurrentPath, &TargetPath,
857bc36eafdSMike Gerdts                 AmlNameStringLength, 0, &NewPath);
858bc36eafdSMike Gerdts         }
859bc36eafdSMike Gerdts     }
860bc36eafdSMike Gerdts 
861bc36eafdSMike Gerdts     /*
862bc36eafdSMike Gerdts      * Success from above indicates that the NamePath was successfully
863bc36eafdSMike Gerdts      * optimized. We need to update the parse op with the new name
864bc36eafdSMike Gerdts      */
865bc36eafdSMike Gerdts     if (ACPI_SUCCESS (Status))
866bc36eafdSMike Gerdts     {
867bc36eafdSMike Gerdts         HowMuchShorter = (AmlNameStringLength - strlen (NewPath));
868bc36eafdSMike Gerdts         OptTotal += HowMuchShorter;
869bc36eafdSMike Gerdts 
870bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
871bc36eafdSMike Gerdts             " REDUCED BY %2u (TOTAL SAVED %2u)",
872bc36eafdSMike Gerdts             (UINT32) HowMuchShorter, OptTotal));
873bc36eafdSMike Gerdts 
874bc36eafdSMike Gerdts         if (Flags & AML_NAMED)
875bc36eafdSMike Gerdts         {
876bc36eafdSMike Gerdts             if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
877bc36eafdSMike Gerdts             {
878bc36eafdSMike Gerdts                 /*
879bc36eafdSMike Gerdts                  * ALIAS is the only oddball opcode, the name declaration
880bc36eafdSMike Gerdts                  * (alias name) is the second operand
881bc36eafdSMike Gerdts                  */
882bc36eafdSMike Gerdts                 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
883bc36eafdSMike Gerdts                 Op->Asl.Child->Asl.Next->Asl.AmlLength = strlen (NewPath);
884bc36eafdSMike Gerdts             }
885bc36eafdSMike Gerdts             else
886bc36eafdSMike Gerdts             {
887bc36eafdSMike Gerdts                 Op->Asl.Child->Asl.Value.String = NewPath;
888bc36eafdSMike Gerdts                 Op->Asl.Child->Asl.AmlLength = strlen (NewPath);
889bc36eafdSMike Gerdts             }
890bc36eafdSMike Gerdts         }
891bc36eafdSMike Gerdts         else if (Flags & AML_CREATE)
892bc36eafdSMike Gerdts         {
893bc36eafdSMike Gerdts             /* Name must appear as the last parameter */
894bc36eafdSMike Gerdts 
895bc36eafdSMike Gerdts             NextOp = Op->Asl.Child;
896*35786f68SRobert Mustacchi             while (!(NextOp->Asl.CompileFlags & OP_IS_NAME_DECLARATION))
897bc36eafdSMike Gerdts             {
898bc36eafdSMike Gerdts                 NextOp = NextOp->Asl.Next;
899bc36eafdSMike Gerdts             }
900bc36eafdSMike Gerdts             /* Update the parse node with the new NamePath */
901bc36eafdSMike Gerdts 
902bc36eafdSMike Gerdts             NextOp->Asl.Value.String = NewPath;
903bc36eafdSMike Gerdts             NextOp->Asl.AmlLength = strlen (NewPath);
904bc36eafdSMike Gerdts         }
905bc36eafdSMike Gerdts         else
906bc36eafdSMike Gerdts         {
907bc36eafdSMike Gerdts             /* Update the parse node with the new NamePath */
908bc36eafdSMike Gerdts 
909bc36eafdSMike Gerdts             Op->Asl.Value.String = NewPath;
910bc36eafdSMike Gerdts             Op->Asl.AmlLength = strlen (NewPath);
911bc36eafdSMike Gerdts         }
912bc36eafdSMike Gerdts     }
913bc36eafdSMike Gerdts     else
914bc36eafdSMike Gerdts     {
915bc36eafdSMike Gerdts         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
916bc36eafdSMike Gerdts     }
917bc36eafdSMike Gerdts 
918bc36eafdSMike Gerdts     /* Cleanup path buffers */
919bc36eafdSMike Gerdts 
920bc36eafdSMike Gerdts     ACPI_FREE (TargetPath.Pointer);
921bc36eafdSMike Gerdts     ACPI_FREE (CurrentPath.Pointer);
922bc36eafdSMike Gerdts 
923bc36eafdSMike Gerdts     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
924bc36eafdSMike Gerdts     return_VOID;
925bc36eafdSMike Gerdts }
926