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