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