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