xref: /freebsd/sys/contrib/dev/acpica/compiler/aslopt.c (revision 9a179dd8bec7b25fb577262c606fddfb5da50ffc)
153289f6aSNate Lawson /******************************************************************************
253289f6aSNate Lawson  *
353289f6aSNate Lawson  * Module Name: aslopt- Compiler optimizations
453289f6aSNate Lawson  *
553289f6aSNate Lawson  *****************************************************************************/
653289f6aSNate Lawson 
753289f6aSNate Lawson /******************************************************************************
853289f6aSNate Lawson  *
953289f6aSNate Lawson  * 1. Copyright Notice
1053289f6aSNate Lawson  *
119a179dd8SJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
1253289f6aSNate Lawson  * All rights reserved.
1353289f6aSNate Lawson  *
1453289f6aSNate Lawson  * 2. License
1553289f6aSNate Lawson  *
1653289f6aSNate Lawson  * 2.1. This is your license from Intel Corp. under its intellectual property
1753289f6aSNate Lawson  * rights.  You may have additional license terms from the party that provided
1853289f6aSNate Lawson  * you this software, covering your right to use that party's intellectual
1953289f6aSNate Lawson  * property rights.
2053289f6aSNate Lawson  *
2153289f6aSNate Lawson  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2253289f6aSNate Lawson  * copy of the source code appearing in this file ("Covered Code") an
2353289f6aSNate Lawson  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2453289f6aSNate Lawson  * base code distributed originally by Intel ("Original Intel Code") to copy,
2553289f6aSNate Lawson  * make derivatives, distribute, use and display any portion of the Covered
2653289f6aSNate Lawson  * Code in any form, with the right to sublicense such rights; and
2753289f6aSNate Lawson  *
2853289f6aSNate Lawson  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
2953289f6aSNate Lawson  * license (with the right to sublicense), under only those claims of Intel
3053289f6aSNate Lawson  * patents that are infringed by the Original Intel Code, to make, use, sell,
3153289f6aSNate Lawson  * offer to sell, and import the Covered Code and derivative works thereof
3253289f6aSNate Lawson  * solely to the minimum extent necessary to exercise the above copyright
3353289f6aSNate Lawson  * license, and in no event shall the patent license extend to any additions
3453289f6aSNate Lawson  * to or modifications of the Original Intel Code.  No other license or right
3553289f6aSNate Lawson  * is granted directly or by implication, estoppel or otherwise;
3653289f6aSNate Lawson  *
3753289f6aSNate Lawson  * The above copyright and patent license is granted only if the following
3853289f6aSNate Lawson  * conditions are met:
3953289f6aSNate Lawson  *
4053289f6aSNate Lawson  * 3. Conditions
4153289f6aSNate Lawson  *
4253289f6aSNate Lawson  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4353289f6aSNate Lawson  * Redistribution of source code of any substantial portion of the Covered
4453289f6aSNate Lawson  * Code or modification with rights to further distribute source must include
4553289f6aSNate Lawson  * the above Copyright Notice, the above License, this list of Conditions,
4653289f6aSNate Lawson  * and the following Disclaimer and Export Compliance provision.  In addition,
4753289f6aSNate Lawson  * Licensee must cause all Covered Code to which Licensee contributes to
4853289f6aSNate Lawson  * contain a file documenting the changes Licensee made to create that Covered
4953289f6aSNate Lawson  * Code and the date of any change.  Licensee must include in that file the
5053289f6aSNate Lawson  * documentation of any changes made by any predecessor Licensee.  Licensee
5153289f6aSNate Lawson  * must include a prominent statement that the modification is derived,
5253289f6aSNate Lawson  * directly or indirectly, from Original Intel Code.
5353289f6aSNate Lawson  *
5453289f6aSNate Lawson  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5553289f6aSNate Lawson  * Redistribution of source code of any substantial portion of the Covered
5653289f6aSNate Lawson  * Code or modification without rights to further distribute source must
5753289f6aSNate Lawson  * include the following Disclaimer and Export Compliance provision in the
5853289f6aSNate Lawson  * documentation and/or other materials provided with distribution.  In
5953289f6aSNate Lawson  * addition, Licensee may not authorize further sublicense of source of any
6053289f6aSNate Lawson  * portion of the Covered Code, and must include terms to the effect that the
6153289f6aSNate Lawson  * license from Licensee to its licensee is limited to the intellectual
6253289f6aSNate Lawson  * property embodied in the software Licensee provides to its licensee, and
6353289f6aSNate Lawson  * not to intellectual property embodied in modifications its licensee may
6453289f6aSNate Lawson  * make.
6553289f6aSNate Lawson  *
6653289f6aSNate Lawson  * 3.3. Redistribution of Executable. Redistribution in executable form of any
6753289f6aSNate Lawson  * substantial portion of the Covered Code or modification must reproduce the
6853289f6aSNate Lawson  * above Copyright Notice, and the following Disclaimer and Export Compliance
6953289f6aSNate Lawson  * provision in the documentation and/or other materials provided with the
7053289f6aSNate Lawson  * distribution.
7153289f6aSNate Lawson  *
7253289f6aSNate Lawson  * 3.4. Intel retains all right, title, and interest in and to the Original
7353289f6aSNate Lawson  * Intel Code.
7453289f6aSNate Lawson  *
7553289f6aSNate Lawson  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7653289f6aSNate Lawson  * Intel shall be used in advertising or otherwise to promote the sale, use or
7753289f6aSNate Lawson  * other dealings in products derived from or relating to the Covered Code
7853289f6aSNate Lawson  * without prior written authorization from Intel.
7953289f6aSNate Lawson  *
8053289f6aSNate Lawson  * 4. Disclaimer and Export Compliance
8153289f6aSNate Lawson  *
8253289f6aSNate Lawson  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8353289f6aSNate Lawson  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8453289f6aSNate Lawson  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8553289f6aSNate Lawson  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8653289f6aSNate Lawson  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8753289f6aSNate Lawson  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
8853289f6aSNate Lawson  * PARTICULAR PURPOSE.
8953289f6aSNate Lawson  *
9053289f6aSNate Lawson  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9153289f6aSNate Lawson  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9253289f6aSNate Lawson  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9353289f6aSNate Lawson  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9453289f6aSNate Lawson  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9553289f6aSNate Lawson  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9653289f6aSNate Lawson  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9753289f6aSNate Lawson  * LIMITED REMEDY.
9853289f6aSNate Lawson  *
9953289f6aSNate Lawson  * 4.3. Licensee shall not export, either directly or indirectly, any of this
10053289f6aSNate Lawson  * software or system incorporating such software without first obtaining any
10153289f6aSNate Lawson  * required license or other approval from the U. S. Department of Commerce or
10253289f6aSNate Lawson  * any other agency or department of the United States Government.  In the
10353289f6aSNate Lawson  * event Licensee exports any such software from the United States or
10453289f6aSNate Lawson  * re-exports any such software from a foreign destination, Licensee shall
10553289f6aSNate Lawson  * ensure that the distribution and export/re-export of the software is in
10653289f6aSNate Lawson  * compliance with all laws, regulations, orders, or other restrictions of the
10753289f6aSNate Lawson  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10853289f6aSNate Lawson  * any of its subsidiaries will export/re-export any technical data, process,
10953289f6aSNate Lawson  * software, or service, directly or indirectly, to any country for which the
11053289f6aSNate Lawson  * United States government or any agency thereof requires an export license,
11153289f6aSNate Lawson  * other governmental approval, or letter of assurance, without first obtaining
11253289f6aSNate Lawson  * such license, approval or letter.
11353289f6aSNate Lawson  *
11453289f6aSNate Lawson  *****************************************************************************/
11553289f6aSNate Lawson 
11653289f6aSNate Lawson 
117ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
11853289f6aSNate Lawson #include "aslcompiler.y.h"
11953289f6aSNate Lawson 
120ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/acparser.h>
121ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/amlcode.h>
122ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/acnamesp.h>
12353289f6aSNate Lawson 
12453289f6aSNate Lawson 
12553289f6aSNate Lawson #define _COMPONENT          ACPI_COMPILER
12653289f6aSNate Lawson         ACPI_MODULE_NAME    ("aslopt")
12753289f6aSNate Lawson 
12853289f6aSNate Lawson 
129fba7fc7eSJung-uk Kim static UINT32 OptTotal = 0;
130fba7fc7eSJung-uk Kim 
131fba7fc7eSJung-uk Kim /* Local prototypes */
132fba7fc7eSJung-uk Kim 
133fba7fc7eSJung-uk Kim static ACPI_STATUS
134fba7fc7eSJung-uk Kim OptSearchToRoot (
135fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
136fba7fc7eSJung-uk Kim     ACPI_WALK_STATE         *WalkState,
137fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *CurrentNode,
138fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *TargetNode,
139fba7fc7eSJung-uk Kim     ACPI_BUFFER             *TargetPath,
140fba7fc7eSJung-uk Kim     char                    **NewPath);
141fba7fc7eSJung-uk Kim 
142fba7fc7eSJung-uk Kim static ACPI_STATUS
143fba7fc7eSJung-uk Kim OptBuildShortestPath (
144fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
145fba7fc7eSJung-uk Kim     ACPI_WALK_STATE         *WalkState,
146fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *CurrentNode,
147fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *TargetNode,
148fba7fc7eSJung-uk Kim     ACPI_BUFFER             *CurrentPath,
149fba7fc7eSJung-uk Kim     ACPI_BUFFER             *TargetPath,
150fba7fc7eSJung-uk Kim     ACPI_SIZE               AmlNameStringLength,
151fba7fc7eSJung-uk Kim     UINT8                   IsDeclaration,
152fba7fc7eSJung-uk Kim     char                    **ReturnNewPath);
153fba7fc7eSJung-uk Kim 
154fba7fc7eSJung-uk Kim static ACPI_STATUS
155fba7fc7eSJung-uk Kim OptOptimizeNameDeclaration (
156fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
157fba7fc7eSJung-uk Kim     ACPI_WALK_STATE         *WalkState,
158fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *CurrentNode,
159fba7fc7eSJung-uk Kim     ACPI_NAMESPACE_NODE     *TargetNode,
160fba7fc7eSJung-uk Kim     char                    *AmlNameString,
161fba7fc7eSJung-uk Kim     char                    **NewPath);
16253289f6aSNate Lawson 
16353289f6aSNate Lawson 
16453289f6aSNate Lawson /*******************************************************************************
16553289f6aSNate Lawson  *
16653289f6aSNate Lawson  * FUNCTION:    OptSearchToRoot
16753289f6aSNate Lawson  *
16853289f6aSNate Lawson  * PARAMETERS:  Op                  - Current parser op
16953289f6aSNate Lawson  *              WalkState           - Current state
17053289f6aSNate Lawson  *              CurrentNode         - Where we are in the namespace
17153289f6aSNate Lawson  *              TargetNode          - Node to which we are referring
17253289f6aSNate Lawson  *              TargetPath          - External full path to the target node
17353289f6aSNate Lawson  *              NewPath             - Where the optimized path is returned
17453289f6aSNate Lawson  *
17553289f6aSNate Lawson  * RETURN:      Status
17653289f6aSNate Lawson  *
17753289f6aSNate Lawson  * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
17853289f6aSNate Lawson  *              name utilizing the search-to-root name resolution algorithm
17953289f6aSNate Lawson  *              that is used by AML interpreters.
18053289f6aSNate Lawson  *
18153289f6aSNate Lawson  ******************************************************************************/
18253289f6aSNate Lawson 
183fba7fc7eSJung-uk Kim static ACPI_STATUS
18453289f6aSNate Lawson OptSearchToRoot (
18553289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
18653289f6aSNate Lawson     ACPI_WALK_STATE         *WalkState,
18753289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *CurrentNode,
18853289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *TargetNode,
18953289f6aSNate Lawson     ACPI_BUFFER             *TargetPath,
19053289f6aSNate Lawson     char                    **NewPath)
19153289f6aSNate Lawson {
19253289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *Node;
19353289f6aSNate Lawson     ACPI_GENERIC_STATE      ScopeInfo;
19453289f6aSNate Lawson     ACPI_STATUS             Status;
19553289f6aSNate Lawson     char                    *Path;
19653289f6aSNate Lawson 
19753289f6aSNate Lawson 
1981a39cfb0SJung-uk Kim     ACPI_FUNCTION_NAME (OptSearchToRoot);
19953289f6aSNate Lawson 
20053289f6aSNate Lawson 
20153289f6aSNate Lawson     /*
20253289f6aSNate Lawson      * Check if search-to-root can be utilized.  Use the last NameSeg of
20353289f6aSNate Lawson      * the NamePath and 1) See if can be found and 2) If found, make
20453289f6aSNate Lawson      * sure that it is the same node that we want.  If there is another
20553289f6aSNate Lawson      * name in the search path before the one we want, the nodes will
20653289f6aSNate Lawson      * not match, and we cannot use this optimization.
20753289f6aSNate Lawson      */
208fba7fc7eSJung-uk Kim     Path = &(((char *) TargetPath->Pointer)[TargetPath->Length -
209fba7fc7eSJung-uk Kim                                             ACPI_NAME_SIZE]),
21053289f6aSNate Lawson     ScopeInfo.Scope.Node = CurrentNode;
21153289f6aSNate Lawson 
21253289f6aSNate Lawson     /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
21353289f6aSNate Lawson 
21453289f6aSNate Lawson     Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
21553289f6aSNate Lawson                     ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
21653289f6aSNate Lawson                     WalkState, &(Node));
21753289f6aSNate Lawson     if (ACPI_FAILURE (Status))
21853289f6aSNate Lawson     {
21953289f6aSNate Lawson         return (Status);
22053289f6aSNate Lawson     }
22153289f6aSNate Lawson 
22253289f6aSNate Lawson     /*
22353289f6aSNate Lawson      * We found the name, but we must check to make sure that the node
22453289f6aSNate Lawson      * matches.  Otherwise, there is another identical name in the search
22553289f6aSNate Lawson      * path that precludes the use of this optimization.
22653289f6aSNate Lawson      */
22753289f6aSNate Lawson     if (Node != TargetNode)
22853289f6aSNate Lawson     {
22953289f6aSNate Lawson         /*
23053289f6aSNate Lawson          * This means that another object with the same name was found first,
23153289f6aSNate Lawson          * and we cannot use this optimization.
23253289f6aSNate Lawson          */
23353289f6aSNate Lawson         return (AE_NOT_FOUND);
23453289f6aSNate Lawson     }
23553289f6aSNate Lawson 
23653289f6aSNate Lawson     /* Found the node, we can use this optimization */
23753289f6aSNate Lawson 
23853289f6aSNate Lawson     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
23953289f6aSNate Lawson         "NAMESEG:   %-24s", Path));
24053289f6aSNate Lawson 
24153289f6aSNate Lawson     /* We must allocate a new string for the name (TargetPath gets deleted) */
24253289f6aSNate Lawson 
2431a39cfb0SJung-uk Kim     *NewPath = ACPI_ALLOCATE_ZEROED (ACPI_NAME_SIZE + 1);
24453289f6aSNate Lawson     ACPI_STRCPY (*NewPath, Path);
24553289f6aSNate Lawson 
246c8466860SMark Santcroos     if (ACPI_STRNCMP (*NewPath, "_T_", 3))
247c8466860SMark Santcroos     {
24853289f6aSNate Lawson         AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION, Op,
24953289f6aSNate Lawson                 *NewPath);
250c8466860SMark Santcroos     }
25153289f6aSNate Lawson 
25253289f6aSNate Lawson     return (AE_OK);
25353289f6aSNate Lawson }
25453289f6aSNate Lawson 
25553289f6aSNate Lawson 
25653289f6aSNate Lawson /*******************************************************************************
25753289f6aSNate Lawson  *
25853289f6aSNate Lawson  * FUNCTION:    OptBuildShortestPath
25953289f6aSNate Lawson  *
26053289f6aSNate Lawson  * PARAMETERS:  Op                  - Current parser op
26153289f6aSNate Lawson  *              WalkState           - Current state
26253289f6aSNate Lawson  *              CurrentNode         - Where we are in the namespace
26353289f6aSNate Lawson  *              TargetNode          - Node to which we are referring
26453289f6aSNate Lawson  *              CurrentPath         - External full path to the current node
26553289f6aSNate Lawson  *              TargetPath          - External full path to the target node
26653289f6aSNate Lawson  *              AmlNameStringLength - Length of the original namepath
26753289f6aSNate Lawson  *              IsDeclaration       - TRUE for declaration, FALSE for reference
26853289f6aSNate Lawson  *              ReturnNewPath       - Where the optimized path is returned
26953289f6aSNate Lawson  *
27053289f6aSNate Lawson  * RETURN:      Status
27153289f6aSNate Lawson  *
27253289f6aSNate Lawson  * DESCRIPTION: Build an optimal NamePath using carats
27353289f6aSNate Lawson  *
27453289f6aSNate Lawson  ******************************************************************************/
27553289f6aSNate Lawson 
276fba7fc7eSJung-uk Kim static ACPI_STATUS
27753289f6aSNate Lawson OptBuildShortestPath (
27853289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
27953289f6aSNate Lawson     ACPI_WALK_STATE         *WalkState,
28053289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *CurrentNode,
28153289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *TargetNode,
28253289f6aSNate Lawson     ACPI_BUFFER             *CurrentPath,
28353289f6aSNate Lawson     ACPI_BUFFER             *TargetPath,
28453289f6aSNate Lawson     ACPI_SIZE               AmlNameStringLength,
28553289f6aSNate Lawson     UINT8                   IsDeclaration,
28653289f6aSNate Lawson     char                    **ReturnNewPath)
28753289f6aSNate Lawson {
28853289f6aSNate Lawson     UINT32                  NumCommonSegments;
28953289f6aSNate Lawson     UINT32                  MaxCommonSegments;
290a9f12690SJung-uk Kim     UINT32                  Index;
29153289f6aSNate Lawson     UINT32                  NumCarats;
292a9f12690SJung-uk Kim     UINT32                  i;
29353289f6aSNate Lawson     char                    *NewPath;
29453289f6aSNate Lawson     char                    *NewPathExternal;
29553289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *Node;
29653289f6aSNate Lawson     ACPI_GENERIC_STATE      ScopeInfo;
29753289f6aSNate Lawson     ACPI_STATUS             Status;
29853289f6aSNate Lawson     BOOLEAN                 SubPath = FALSE;
29953289f6aSNate Lawson 
30053289f6aSNate Lawson 
3011a39cfb0SJung-uk Kim     ACPI_FUNCTION_NAME (OptBuildShortestPath);
30253289f6aSNate Lawson 
30353289f6aSNate Lawson 
30453289f6aSNate Lawson     ScopeInfo.Scope.Node = CurrentNode;
30553289f6aSNate Lawson 
30653289f6aSNate Lawson     /*
30753289f6aSNate Lawson      * Determine the maximum number of NameSegs that the Target and Current paths
30853289f6aSNate Lawson      * can possibly have in common.  (To optimize, we have to have at least 1)
30953289f6aSNate Lawson      *
31053289f6aSNate Lawson      * Note: The external NamePath string lengths are always a multiple of 5
31153289f6aSNate Lawson      * (ACPI_NAME_SIZE + separator)
31253289f6aSNate Lawson      */
31353289f6aSNate Lawson     MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
31453289f6aSNate Lawson     if (CurrentPath->Length < TargetPath->Length)
31553289f6aSNate Lawson     {
31653289f6aSNate Lawson         MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
31753289f6aSNate Lawson     }
31853289f6aSNate Lawson 
31953289f6aSNate Lawson     /*
32053289f6aSNate Lawson      * Determine how many NameSegs the two paths have in common.
32153289f6aSNate Lawson      * (Starting from the root)
32253289f6aSNate Lawson      */
32353289f6aSNate Lawson     for (NumCommonSegments = 0;
32453289f6aSNate Lawson          NumCommonSegments < MaxCommonSegments;
32553289f6aSNate Lawson          NumCommonSegments++)
32653289f6aSNate Lawson     {
32753289f6aSNate Lawson         /* Compare two single NameSegs */
32853289f6aSNate Lawson 
32953289f6aSNate Lawson         if (ACPI_STRNCMP (
330fba7fc7eSJung-uk Kim             &((char *) TargetPath->Pointer)[(NumCommonSegments *
331fba7fc7eSJung-uk Kim                                              ACPI_PATH_SEGMENT_LENGTH) + 1],
332fba7fc7eSJung-uk Kim             &((char *) CurrentPath->Pointer)[(NumCommonSegments *
333fba7fc7eSJung-uk Kim                                               ACPI_PATH_SEGMENT_LENGTH) + 1],
33453289f6aSNate Lawson             ACPI_NAME_SIZE))
33553289f6aSNate Lawson         {
33653289f6aSNate Lawson             /* Mismatch */
33753289f6aSNate Lawson 
33853289f6aSNate Lawson             break;
33953289f6aSNate Lawson         }
34053289f6aSNate Lawson     }
34153289f6aSNate Lawson 
342fba7fc7eSJung-uk Kim     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %d",
343fba7fc7eSJung-uk Kim         NumCommonSegments));
34453289f6aSNate Lawson 
34553289f6aSNate Lawson     /* There must be at least 1 common NameSeg in order to optimize */
34653289f6aSNate Lawson 
34753289f6aSNate Lawson     if (NumCommonSegments == 0)
34853289f6aSNate Lawson     {
34953289f6aSNate Lawson         return (AE_NOT_FOUND);
35053289f6aSNate Lawson     }
35153289f6aSNate Lawson 
35253289f6aSNate Lawson     if (NumCommonSegments == MaxCommonSegments)
35353289f6aSNate Lawson     {
35453289f6aSNate Lawson         if (CurrentPath->Length == TargetPath->Length)
35553289f6aSNate Lawson         {
35653289f6aSNate Lawson             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
35753289f6aSNate Lawson             return (AE_NOT_FOUND);
35853289f6aSNate Lawson         }
35953289f6aSNate Lawson         else
36053289f6aSNate Lawson         {
36153289f6aSNate Lawson             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
36253289f6aSNate Lawson             SubPath = TRUE;
36353289f6aSNate Lawson         }
36453289f6aSNate Lawson     }
36553289f6aSNate Lawson 
36653289f6aSNate Lawson     /* Determine how many prefix Carats are required */
36753289f6aSNate Lawson 
368fba7fc7eSJung-uk Kim     NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
369fba7fc7eSJung-uk Kim                 NumCommonSegments;
37053289f6aSNate Lawson 
37153289f6aSNate Lawson     /*
37253289f6aSNate Lawson      * Construct a new target string
37353289f6aSNate Lawson      */
3741a39cfb0SJung-uk Kim     NewPathExternal = ACPI_ALLOCATE_ZEROED (
3751a39cfb0SJung-uk Kim         TargetPath->Length + NumCarats + 1);
37653289f6aSNate Lawson 
37753289f6aSNate Lawson     /* Insert the Carats into the Target string */
37853289f6aSNate Lawson 
37953289f6aSNate Lawson     for (i = 0; i < NumCarats; i++)
38053289f6aSNate Lawson     {
38153289f6aSNate Lawson         NewPathExternal[i] = '^';
38253289f6aSNate Lawson     }
38353289f6aSNate Lawson 
384fba7fc7eSJung-uk Kim     /*
385fba7fc7eSJung-uk Kim      * Copy only the necessary (optimal) segments from the original
386fba7fc7eSJung-uk Kim      * target string
387fba7fc7eSJung-uk Kim      */
38853289f6aSNate Lawson     Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
38953289f6aSNate Lawson 
39053289f6aSNate Lawson     /* Special handling for exact subpath in a name declaration */
39153289f6aSNate Lawson 
39253289f6aSNate Lawson     if (IsDeclaration && SubPath && (CurrentPath->Length > TargetPath->Length))
39353289f6aSNate Lawson     {
39453289f6aSNate Lawson         /*
395fba7fc7eSJung-uk Kim          * The current path is longer than the target, and the target is a
396fba7fc7eSJung-uk Kim          * subpath of the current path. We must include one more NameSeg of
397fba7fc7eSJung-uk Kim          * the target path
39853289f6aSNate Lawson          */
39953289f6aSNate Lawson         Index -= ACPI_PATH_SEGMENT_LENGTH;
400c8466860SMark Santcroos 
401c8466860SMark Santcroos         /* Special handling for Scope() operator */
402c8466860SMark Santcroos 
403c8466860SMark Santcroos         if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
404c8466860SMark Santcroos         {
405c8466860SMark Santcroos             NewPathExternal[i] = '^';
406c8466860SMark Santcroos             i++;
407c8466860SMark Santcroos             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
408c8466860SMark Santcroos         }
40953289f6aSNate Lawson     }
41053289f6aSNate Lawson 
411fba7fc7eSJung-uk Kim     /* Make sure we haven't gone off the end of the target path */
412fba7fc7eSJung-uk Kim 
413fba7fc7eSJung-uk Kim     if (Index > TargetPath->Length)
414fba7fc7eSJung-uk Kim     {
415fba7fc7eSJung-uk Kim         Index = TargetPath->Length;
416fba7fc7eSJung-uk Kim     }
417fba7fc7eSJung-uk Kim 
41853289f6aSNate Lawson     ACPI_STRCPY (&NewPathExternal[i], &((char *) TargetPath->Pointer)[Index]);
41953289f6aSNate Lawson     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
42053289f6aSNate Lawson 
42153289f6aSNate Lawson     /*
422fba7fc7eSJung-uk Kim      * Internalize the new target string and check it against the original
423fba7fc7eSJung-uk Kim      * string to make sure that this is in fact an optimization. If the
424fba7fc7eSJung-uk Kim      * original string is already optimal, there is no point in continuing.
42553289f6aSNate Lawson      */
42653289f6aSNate Lawson     Status = AcpiNsInternalizeName (NewPathExternal, &NewPath);
42753289f6aSNate Lawson     if (ACPI_FAILURE (Status))
42853289f6aSNate Lawson     {
429fba7fc7eSJung-uk Kim         AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
430fba7fc7eSJung-uk Kim             ASL_NO_ABORT);
4311a39cfb0SJung-uk Kim         ACPI_FREE (NewPathExternal);
43253289f6aSNate Lawson         return (Status);
43353289f6aSNate Lawson     }
43453289f6aSNate Lawson 
43553289f6aSNate Lawson     if (ACPI_STRLEN (NewPath) >= AmlNameStringLength)
43653289f6aSNate Lawson     {
437fba7fc7eSJung-uk Kim         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
4381a39cfb0SJung-uk Kim             " NOT SHORTER (New %u old %u)",
439a9f12690SJung-uk Kim             (UINT32) ACPI_STRLEN (NewPath), (UINT32) AmlNameStringLength));
4401a39cfb0SJung-uk Kim         ACPI_FREE (NewPathExternal);
44153289f6aSNate Lawson         return (AE_NOT_FOUND);
44253289f6aSNate Lawson     }
44353289f6aSNate Lawson 
44453289f6aSNate Lawson     /*
44553289f6aSNate Lawson      * Check to make sure that the optimization finds the node we are
44653289f6aSNate Lawson      * looking for.  This is simply a sanity check on the new
44753289f6aSNate Lawson      * path that has been created.
44853289f6aSNate Lawson      */
44953289f6aSNate Lawson     Status = AcpiNsLookup (&ScopeInfo,  NewPath,
45053289f6aSNate Lawson                     ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
45153289f6aSNate Lawson                     ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
45253289f6aSNate Lawson     if (ACPI_SUCCESS (Status))
45353289f6aSNate Lawson     {
45453289f6aSNate Lawson         /* Found the namepath, but make sure the node is correct */
45553289f6aSNate Lawson 
45653289f6aSNate Lawson         if (Node == TargetNode)
45753289f6aSNate Lawson         {
45853289f6aSNate Lawson             /* The lookup matched the node, accept this optimization */
45953289f6aSNate Lawson 
46053289f6aSNate Lawson             AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
46153289f6aSNate Lawson                 Op, NewPathExternal);
46253289f6aSNate Lawson             *ReturnNewPath = NewPath;
46353289f6aSNate Lawson         }
46453289f6aSNate Lawson         else
46553289f6aSNate Lawson         {
46653289f6aSNate Lawson             /* Node is not correct, do not use this optimization */
46753289f6aSNate Lawson 
46853289f6aSNate Lawson             Status = AE_NOT_FOUND;
46953289f6aSNate Lawson             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
47053289f6aSNate Lawson             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
47153289f6aSNate Lawson                 "Not using optimized name - found wrong node");
47253289f6aSNate Lawson         }
47353289f6aSNate Lawson     }
47453289f6aSNate Lawson     else
47553289f6aSNate Lawson     {
47653289f6aSNate Lawson         /* The lookup failed, we obviously cannot use this optimization */
47753289f6aSNate Lawson 
47853289f6aSNate Lawson         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
47953289f6aSNate Lawson         AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
48053289f6aSNate Lawson             "Not using optimized name - did not find node");
48153289f6aSNate Lawson     }
48253289f6aSNate Lawson 
4831a39cfb0SJung-uk Kim     ACPI_FREE (NewPathExternal);
48453289f6aSNate Lawson     return (Status);
48553289f6aSNate Lawson }
48653289f6aSNate Lawson 
48753289f6aSNate Lawson 
48853289f6aSNate Lawson /*******************************************************************************
48953289f6aSNate Lawson  *
49053289f6aSNate Lawson  * FUNCTION:    OptOptimizeNameDeclaration
49153289f6aSNate Lawson  *
49253289f6aSNate Lawson  * PARAMETERS:  Op                  - Current parser op
49353289f6aSNate Lawson  *              WalkState           - Current state
49453289f6aSNate Lawson  *              CurrentNode         - Where we are in the namespace
49553289f6aSNate Lawson  *              AmlNameString       - Unoptimized namepath
49653289f6aSNate Lawson  *              NewPath             - Where the optimized path is returned
49753289f6aSNate Lawson  *
49853289f6aSNate Lawson  * RETURN:      Status. AE_OK If path is optimized
49953289f6aSNate Lawson  *
50053289f6aSNate Lawson  * DESCRIPTION: Perform a simple optimization of removing an extraneous
50153289f6aSNate Lawson  *              backslash prefix if we are already at the root scope.
50253289f6aSNate Lawson  *
50353289f6aSNate Lawson  ******************************************************************************/
50453289f6aSNate Lawson 
505fba7fc7eSJung-uk Kim static ACPI_STATUS
50653289f6aSNate Lawson OptOptimizeNameDeclaration (
50753289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
50853289f6aSNate Lawson     ACPI_WALK_STATE         *WalkState,
50953289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *CurrentNode,
51053289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *TargetNode,
51153289f6aSNate Lawson     char                    *AmlNameString,
51253289f6aSNate Lawson     char                    **NewPath)
51353289f6aSNate Lawson {
51453289f6aSNate Lawson     ACPI_STATUS             Status;
51553289f6aSNate Lawson     char                    *NewPathExternal;
51653289f6aSNate Lawson     ACPI_GENERIC_STATE      ScopeInfo;
51753289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *Node;
51853289f6aSNate Lawson 
51953289f6aSNate Lawson 
5201a39cfb0SJung-uk Kim     ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
52153289f6aSNate Lawson 
52253289f6aSNate Lawson 
52353289f6aSNate Lawson     if (((CurrentNode == AcpiGbl_RootNode) ||
52453289f6aSNate Lawson         (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK)) &&
52553289f6aSNate Lawson             (AmlNameString[0] == '\\'))
52653289f6aSNate Lawson     {
52753289f6aSNate Lawson         /*
52853289f6aSNate Lawson          * The current scope is the root, and the namepath has a root prefix
52953289f6aSNate Lawson          * that is therefore extraneous.  Remove it.
53053289f6aSNate Lawson          */
53153289f6aSNate Lawson         *NewPath = &AmlNameString[1];
53253289f6aSNate Lawson 
53353289f6aSNate Lawson         /* Debug output */
53453289f6aSNate Lawson 
53553289f6aSNate Lawson         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
53653289f6aSNate Lawson                     NULL, &NewPathExternal);
53753289f6aSNate Lawson         if (ACPI_FAILURE (Status))
53853289f6aSNate Lawson         {
539fba7fc7eSJung-uk Kim             AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
540fba7fc7eSJung-uk Kim                 ASL_NO_ABORT);
54153289f6aSNate Lawson             return (Status);
54253289f6aSNate Lawson         }
54353289f6aSNate Lawson 
54453289f6aSNate Lawson         /*
54553289f6aSNate Lawson          * Check to make sure that the optimization finds the node we are
54653289f6aSNate Lawson          * looking for.  This is simply a sanity check on the new
54753289f6aSNate Lawson          * path that has been created.
54853289f6aSNate Lawson          */
54953289f6aSNate Lawson         ScopeInfo.Scope.Node = CurrentNode;
55053289f6aSNate Lawson         Status = AcpiNsLookup (&ScopeInfo, *NewPath,
55153289f6aSNate Lawson                         ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
55253289f6aSNate Lawson                         ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
55353289f6aSNate Lawson         if (ACPI_SUCCESS (Status))
55453289f6aSNate Lawson         {
55553289f6aSNate Lawson             /* Found the namepath, but make sure the node is correct */
55653289f6aSNate Lawson 
55753289f6aSNate Lawson             if (Node == TargetNode)
55853289f6aSNate Lawson             {
55953289f6aSNate Lawson                 /* The lookup matched the node, accept this optimization */
56053289f6aSNate Lawson 
56153289f6aSNate Lawson                 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
56253289f6aSNate Lawson                     Op, NewPathExternal);
56353289f6aSNate Lawson 
56453289f6aSNate Lawson                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
56553289f6aSNate Lawson                     "AT ROOT:   %-24s", NewPathExternal));
56653289f6aSNate Lawson             }
56753289f6aSNate Lawson             else
56853289f6aSNate Lawson             {
56953289f6aSNate Lawson                 /* Node is not correct, do not use this optimization */
57053289f6aSNate Lawson 
57153289f6aSNate Lawson                 Status = AE_NOT_FOUND;
572fba7fc7eSJung-uk Kim                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
573fba7fc7eSJung-uk Kim                     " ***** WRONG NODE"));
57453289f6aSNate Lawson                 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
57553289f6aSNate Lawson                     "Not using optimized name - found wrong node");
57653289f6aSNate Lawson             }
57753289f6aSNate Lawson         }
57853289f6aSNate Lawson         else
57953289f6aSNate Lawson         {
58053289f6aSNate Lawson             /* The lookup failed, we obviously cannot use this optimization */
58153289f6aSNate Lawson 
582fba7fc7eSJung-uk Kim             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
583fba7fc7eSJung-uk Kim                 " ***** NOT FOUND"));
58453289f6aSNate Lawson             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
58553289f6aSNate Lawson                 "Not using optimized name - did not find node");
58653289f6aSNate Lawson         }
58753289f6aSNate Lawson 
5881a39cfb0SJung-uk Kim         ACPI_FREE (NewPathExternal);
58953289f6aSNate Lawson         return (Status);
59053289f6aSNate Lawson     }
59153289f6aSNate Lawson 
59253289f6aSNate Lawson     /* Could not optimize */
59353289f6aSNate Lawson 
59453289f6aSNate Lawson     return (AE_NOT_FOUND);
59553289f6aSNate Lawson }
59653289f6aSNate Lawson 
59753289f6aSNate Lawson 
59853289f6aSNate Lawson /*******************************************************************************
59953289f6aSNate Lawson  *
60053289f6aSNate Lawson  * FUNCTION:    OptOptimizeNamePath
60153289f6aSNate Lawson  *
60253289f6aSNate Lawson  * PARAMETERS:  Op                  - Current parser op
60353289f6aSNate Lawson  *              Flags               - Opcode info flags
60453289f6aSNate Lawson  *              WalkState           - Current state
60553289f6aSNate Lawson  *              AmlNameString       - Unoptimized namepath
60653289f6aSNate Lawson  *              TargetNode          - Node to which AmlNameString refers
60753289f6aSNate Lawson  *
60853289f6aSNate Lawson  * RETURN:      None.  If path is optimized, the Op is updated with new path
60953289f6aSNate Lawson  *
61053289f6aSNate Lawson  * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
61153289f6aSNate Lawson  *              Must take into account both the current location in the
61253289f6aSNate Lawson  *              namespace and the actual reference path.
61353289f6aSNate Lawson  *
61453289f6aSNate Lawson  ******************************************************************************/
61553289f6aSNate Lawson 
61653289f6aSNate Lawson void
61753289f6aSNate Lawson OptOptimizeNamePath (
61853289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
61953289f6aSNate Lawson     UINT32                  Flags,
62053289f6aSNate Lawson     ACPI_WALK_STATE         *WalkState,
62153289f6aSNate Lawson     char                    *AmlNameString,
62253289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *TargetNode)
62353289f6aSNate Lawson {
62453289f6aSNate Lawson     ACPI_STATUS             Status;
62553289f6aSNate Lawson     ACPI_BUFFER             TargetPath;
62653289f6aSNate Lawson     ACPI_BUFFER             CurrentPath;
62753289f6aSNate Lawson     ACPI_SIZE               AmlNameStringLength;
62853289f6aSNate Lawson     ACPI_NAMESPACE_NODE     *CurrentNode;
62953289f6aSNate Lawson     char                    *ExternalNameString;
63053289f6aSNate Lawson     char                    *NewPath = NULL;
63153289f6aSNate Lawson     ACPI_SIZE               HowMuchShorter;
63253289f6aSNate Lawson     ACPI_PARSE_OBJECT       *NextOp;
63353289f6aSNate Lawson 
63453289f6aSNate Lawson 
6351a39cfb0SJung-uk Kim     ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
63653289f6aSNate Lawson 
63753289f6aSNate Lawson 
63853289f6aSNate Lawson     /* This is an optional optimization */
63953289f6aSNate Lawson 
64053289f6aSNate Lawson     if (!Gbl_ReferenceOptimizationFlag)
64153289f6aSNate Lawson     {
64253289f6aSNate Lawson         return_VOID;
64353289f6aSNate Lawson     }
64453289f6aSNate Lawson 
64553289f6aSNate Lawson     /* Various required items */
64653289f6aSNate Lawson 
647c8466860SMark Santcroos     if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
64853289f6aSNate Lawson     {
64953289f6aSNate Lawson         return_VOID;
65053289f6aSNate Lawson     }
65153289f6aSNate Lawson 
65253289f6aSNate Lawson     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "%5d [%12.12s] [%12.12s] ",
65353289f6aSNate Lawson         Op->Asl.LogicalLineNumber,
65453289f6aSNate Lawson         AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
65553289f6aSNate Lawson         AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
65653289f6aSNate Lawson 
65753289f6aSNate Lawson     if (!(Flags & (AML_NAMED | AML_CREATE)))
65853289f6aSNate Lawson     {
65953289f6aSNate Lawson         if (Op->Asl.CompileFlags & NODE_IS_NAME_DECLARATION)
66053289f6aSNate Lawson         {
66153289f6aSNate Lawson             /* We don't want to fuss with actual name declaration nodes here */
66253289f6aSNate Lawson 
66353289f6aSNate Lawson             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
66453289f6aSNate Lawson                 "******* NAME DECLARATION\n"));
66553289f6aSNate Lawson             return_VOID;
66653289f6aSNate Lawson         }
66753289f6aSNate Lawson     }
66853289f6aSNate Lawson 
66953289f6aSNate Lawson     /*
67053289f6aSNate Lawson      * The original path must be longer than one NameSeg (4 chars) for there
67153289f6aSNate Lawson      * to be any possibility that it can be optimized to a shorter string
67253289f6aSNate Lawson      */
67353289f6aSNate Lawson     AmlNameStringLength = ACPI_STRLEN (AmlNameString);
67453289f6aSNate Lawson     if (AmlNameStringLength <= ACPI_NAME_SIZE)
67553289f6aSNate Lawson     {
67653289f6aSNate Lawson         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
67753289f6aSNate Lawson             "NAMESEG %4.4s\n", AmlNameString));
67853289f6aSNate Lawson         return_VOID;
67953289f6aSNate Lawson     }
68053289f6aSNate Lawson 
68153289f6aSNate Lawson     /*
68253289f6aSNate Lawson      * We need to obtain the node that represents the current scope -- where
68353289f6aSNate Lawson      * we are right now in the namespace.  We will compare this path
68453289f6aSNate Lawson      * against the Namepath, looking for commonality.
68553289f6aSNate Lawson      */
68653289f6aSNate Lawson     CurrentNode = AcpiGbl_RootNode;
68753289f6aSNate Lawson     if (WalkState->ScopeInfo)
68853289f6aSNate Lawson     {
68953289f6aSNate Lawson         CurrentNode = WalkState->ScopeInfo->Scope.Node;
69053289f6aSNate Lawson     }
69153289f6aSNate Lawson 
69253289f6aSNate Lawson     if (Flags & (AML_NAMED | AML_CREATE))
69353289f6aSNate Lawson     {
69453289f6aSNate Lawson         /* This is the declaration of a new name */
69553289f6aSNate Lawson 
69653289f6aSNate Lawson         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME"));
69753289f6aSNate Lawson 
698fba7fc7eSJung-uk Kim         /*
699fba7fc7eSJung-uk Kim          * The node of interest is the parent of this node
700fba7fc7eSJung-uk Kim          * (the containing scope)
701fba7fc7eSJung-uk Kim          */
70253289f6aSNate Lawson         CurrentNode = Op->Asl.Parent->Asl.Node;
70353289f6aSNate Lawson         if (!CurrentNode)
70453289f6aSNate Lawson         {
70553289f6aSNate Lawson             CurrentNode = AcpiGbl_RootNode;
70653289f6aSNate Lawson         }
70753289f6aSNate Lawson     }
70853289f6aSNate Lawson     else
70953289f6aSNate Lawson     {
71053289f6aSNate Lawson         /* This is a reference to an existing named object */
71153289f6aSNate Lawson 
71253289f6aSNate Lawson         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REF "));
71353289f6aSNate Lawson     }
71453289f6aSNate Lawson 
71553289f6aSNate Lawson     /*
71653289f6aSNate Lawson      * Obtain the full paths to the two nodes that we are interested in
71753289f6aSNate Lawson      * (Target and current namespace location) in external
71853289f6aSNate Lawson      * format -- something we can easily manipulate
71953289f6aSNate Lawson      */
72053289f6aSNate Lawson     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
72153289f6aSNate Lawson     Status = AcpiNsHandleToPathname (TargetNode, &TargetPath);
72253289f6aSNate Lawson     if (ACPI_FAILURE (Status))
72353289f6aSNate Lawson     {
724fba7fc7eSJung-uk Kim         AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
725fba7fc7eSJung-uk Kim             ASL_NO_ABORT);
72653289f6aSNate Lawson         return_VOID;
72753289f6aSNate Lawson     }
72853289f6aSNate Lawson     TargetPath.Length--;    /* Subtract one for null terminator */
72953289f6aSNate Lawson 
73053289f6aSNate Lawson     /* CurrentPath is the path to this scope (where we are in the namespace) */
73153289f6aSNate Lawson 
73253289f6aSNate Lawson     CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
73353289f6aSNate Lawson     Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath);
73453289f6aSNate Lawson     if (ACPI_FAILURE (Status))
73553289f6aSNate Lawson     {
736fba7fc7eSJung-uk Kim         AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
737fba7fc7eSJung-uk Kim             ASL_NO_ABORT);
73853289f6aSNate Lawson         return_VOID;
73953289f6aSNate Lawson     }
74053289f6aSNate Lawson     CurrentPath.Length--;   /* Subtract one for null terminator */
74153289f6aSNate Lawson 
74253289f6aSNate Lawson     /* Debug output only */
74353289f6aSNate Lawson 
74453289f6aSNate Lawson     Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
74553289f6aSNate Lawson                 NULL, &ExternalNameString);
74653289f6aSNate Lawson     if (ACPI_FAILURE (Status))
74753289f6aSNate Lawson     {
748fba7fc7eSJung-uk Kim         AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
749fba7fc7eSJung-uk Kim             ASL_NO_ABORT);
75053289f6aSNate Lawson         return_VOID;
75153289f6aSNate Lawson     }
75253289f6aSNate Lawson 
75353289f6aSNate Lawson     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
7541a39cfb0SJung-uk Kim         "%37s (%2u) ==> %-32s(%2u) %-32s",
755a9f12690SJung-uk Kim         (char *) CurrentPath.Pointer, (UINT32) CurrentPath.Length,
756a9f12690SJung-uk Kim         (char *) TargetPath.Pointer, (UINT32) TargetPath.Length, ExternalNameString));
75753289f6aSNate Lawson 
7581a39cfb0SJung-uk Kim     ACPI_FREE (ExternalNameString);
75953289f6aSNate Lawson 
76053289f6aSNate Lawson     /*
76153289f6aSNate Lawson      * Attempt an optmization depending on the type of namepath
76253289f6aSNate Lawson      */
76353289f6aSNate Lawson     if (Flags & (AML_NAMED | AML_CREATE))
76453289f6aSNate Lawson     {
76553289f6aSNate Lawson         /*
76653289f6aSNate Lawson          * This is a named opcode and the namepath is a name declaration, not
76753289f6aSNate Lawson          * a reference.
76853289f6aSNate Lawson          */
76953289f6aSNate Lawson         Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
77053289f6aSNate Lawson                     TargetNode, AmlNameString, &NewPath);
77153289f6aSNate Lawson         if (ACPI_FAILURE (Status))
77253289f6aSNate Lawson         {
77353289f6aSNate Lawson             /*
77453289f6aSNate Lawson              * 2) now attempt to
77553289f6aSNate Lawson              *    optimize the namestring with carats (up-arrow)
77653289f6aSNate Lawson              */
77753289f6aSNate Lawson             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
77853289f6aSNate Lawson                             TargetNode, &CurrentPath, &TargetPath,
77953289f6aSNate Lawson                             AmlNameStringLength, 1, &NewPath);
78053289f6aSNate Lawson         }
78153289f6aSNate Lawson     }
78253289f6aSNate Lawson     else
78353289f6aSNate Lawson     {
78453289f6aSNate Lawson         /*
78553289f6aSNate Lawson          * This is a reference to an existing named object
78653289f6aSNate Lawson          *
78753289f6aSNate Lawson          * 1) Check if search-to-root can be utilized using the last
78853289f6aSNate Lawson          *    NameSeg of the NamePath
78953289f6aSNate Lawson          */
79053289f6aSNate Lawson         Status = OptSearchToRoot (Op, WalkState, CurrentNode,
79153289f6aSNate Lawson                         TargetNode, &TargetPath, &NewPath);
79253289f6aSNate Lawson         if (ACPI_FAILURE (Status))
79353289f6aSNate Lawson         {
79453289f6aSNate Lawson             /*
79553289f6aSNate Lawson              * 2) Search-to-root could not be used, now attempt to
79653289f6aSNate Lawson              *    optimize the namestring with carats (up-arrow)
79753289f6aSNate Lawson              */
79853289f6aSNate Lawson             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
79953289f6aSNate Lawson                             TargetNode, &CurrentPath, &TargetPath,
80053289f6aSNate Lawson                             AmlNameStringLength, 0, &NewPath);
80153289f6aSNate Lawson         }
80253289f6aSNate Lawson     }
80353289f6aSNate Lawson 
80453289f6aSNate Lawson     /*
80553289f6aSNate Lawson      * Success from above indicates that the NamePath was successfully
80653289f6aSNate Lawson      * optimized.  We need to update the parse op with the new name
80753289f6aSNate Lawson      */
80853289f6aSNate Lawson     if (ACPI_SUCCESS (Status))
80953289f6aSNate Lawson     {
81053289f6aSNate Lawson         HowMuchShorter = (AmlNameStringLength - ACPI_STRLEN (NewPath));
81153289f6aSNate Lawson         OptTotal += HowMuchShorter;
81253289f6aSNate Lawson 
8131a39cfb0SJung-uk Kim         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " REDUCED %2u (%u)",
814a9f12690SJung-uk Kim             (UINT32) HowMuchShorter, OptTotal));
81553289f6aSNate Lawson 
81653289f6aSNate Lawson         if (Flags & AML_NAMED)
81753289f6aSNate Lawson         {
81853289f6aSNate Lawson             if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
81953289f6aSNate Lawson             {
82053289f6aSNate Lawson                 /*
82153289f6aSNate Lawson                  * ALIAS is the only oddball opcode, the name declaration
82253289f6aSNate Lawson                  * (alias name) is the second operand
82353289f6aSNate Lawson                  */
82453289f6aSNate Lawson                 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
82553289f6aSNate Lawson                 Op->Asl.Child->Asl.Next->Asl.AmlLength = ACPI_STRLEN (NewPath);
82653289f6aSNate Lawson             }
82753289f6aSNate Lawson             else
82853289f6aSNate Lawson             {
82953289f6aSNate Lawson                 Op->Asl.Child->Asl.Value.String = NewPath;
83053289f6aSNate Lawson                 Op->Asl.Child->Asl.AmlLength = ACPI_STRLEN (NewPath);
83153289f6aSNate Lawson             }
83253289f6aSNate Lawson         }
83353289f6aSNate Lawson         else if (Flags & AML_CREATE)
83453289f6aSNate Lawson         {
83553289f6aSNate Lawson             /* Name must appear as the last parameter */
83653289f6aSNate Lawson 
83753289f6aSNate Lawson             NextOp = Op->Asl.Child;
83853289f6aSNate Lawson             while (!(NextOp->Asl.CompileFlags & NODE_IS_NAME_DECLARATION))
83953289f6aSNate Lawson             {
84053289f6aSNate Lawson                 NextOp = NextOp->Asl.Next;
84153289f6aSNate Lawson             }
84253289f6aSNate Lawson             /* Update the parse node with the new NamePath */
84353289f6aSNate Lawson 
84453289f6aSNate Lawson             NextOp->Asl.Value.String = NewPath;
84553289f6aSNate Lawson             NextOp->Asl.AmlLength = ACPI_STRLEN (NewPath);
84653289f6aSNate Lawson         }
84753289f6aSNate Lawson         else
84853289f6aSNate Lawson         {
84953289f6aSNate Lawson             /* Update the parse node with the new NamePath */
85053289f6aSNate Lawson 
85153289f6aSNate Lawson             Op->Asl.Value.String = NewPath;
85253289f6aSNate Lawson             Op->Asl.AmlLength = ACPI_STRLEN (NewPath);
85353289f6aSNate Lawson         }
85453289f6aSNate Lawson     }
85553289f6aSNate Lawson     else
85653289f6aSNate Lawson     {
85753289f6aSNate Lawson         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
85853289f6aSNate Lawson     }
85953289f6aSNate Lawson 
86053289f6aSNate Lawson     /* Cleanup path buffers */
86153289f6aSNate Lawson 
8621a39cfb0SJung-uk Kim     ACPI_FREE (TargetPath.Pointer);
8631a39cfb0SJung-uk Kim     ACPI_FREE (CurrentPath.Pointer);
86453289f6aSNate Lawson 
86553289f6aSNate Lawson     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
86653289f6aSNate Lawson     return_VOID;
86753289f6aSNate Lawson }
86853289f6aSNate Lawson 
869