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