xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/aslopt.c (revision 2c7d7e3ca2e4f0b675c6c58e614f6aede66c678e)
128c506b8Sjruoho /******************************************************************************
228c506b8Sjruoho  *
328c506b8Sjruoho  * Module Name: aslopt- Compiler optimizations
428c506b8Sjruoho  *
528c506b8Sjruoho  *****************************************************************************/
628c506b8Sjruoho 
7124f4c82Sjruoho /*
8*2c7d7e3cSchristos  * Copyright (C) 2000 - 2023, Intel Corp.
928c506b8Sjruoho  * All rights reserved.
1028c506b8Sjruoho  *
11124f4c82Sjruoho  * Redistribution and use in source and binary forms, with or without
12124f4c82Sjruoho  * modification, are permitted provided that the following conditions
13124f4c82Sjruoho  * are met:
14124f4c82Sjruoho  * 1. Redistributions of source code must retain the above copyright
15124f4c82Sjruoho  *    notice, this list of conditions, and the following disclaimer,
16124f4c82Sjruoho  *    without modification.
17124f4c82Sjruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18124f4c82Sjruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
19124f4c82Sjruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
20124f4c82Sjruoho  *    including a substantially similar Disclaimer requirement for further
21124f4c82Sjruoho  *    binary redistribution.
22124f4c82Sjruoho  * 3. Neither the names of the above-listed copyright holders nor the names
23124f4c82Sjruoho  *    of any contributors may be used to endorse or promote products derived
24124f4c82Sjruoho  *    from this software without specific prior written permission.
2528c506b8Sjruoho  *
26124f4c82Sjruoho  * Alternatively, this software may be distributed under the terms of the
27124f4c82Sjruoho  * GNU General Public License ("GPL") version 2 as published by the Free
28124f4c82Sjruoho  * Software Foundation.
2928c506b8Sjruoho  *
30124f4c82Sjruoho  * NO WARRANTY
31124f4c82Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32124f4c82Sjruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3398244dcfSchristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34124f4c82Sjruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35124f4c82Sjruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36124f4c82Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37124f4c82Sjruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38124f4c82Sjruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39124f4c82Sjruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40124f4c82Sjruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41124f4c82Sjruoho  * POSSIBILITY OF SUCH DAMAGES.
42124f4c82Sjruoho  */
4328c506b8Sjruoho 
4428c506b8Sjruoho #include "aslcompiler.h"
4528c506b8Sjruoho #include "aslcompiler.y.h"
4628c506b8Sjruoho 
4728c506b8Sjruoho #include "acparser.h"
4828c506b8Sjruoho #include "amlcode.h"
4928c506b8Sjruoho #include "acnamesp.h"
5028c506b8Sjruoho 
5128c506b8Sjruoho 
5228c506b8Sjruoho #define _COMPONENT          ACPI_COMPILER
5328c506b8Sjruoho         ACPI_MODULE_NAME    ("aslopt")
5428c506b8Sjruoho 
5528c506b8Sjruoho 
5628c506b8Sjruoho static UINT32               OptTotal = 0;
5728c506b8Sjruoho 
5828c506b8Sjruoho /* Local prototypes */
5928c506b8Sjruoho 
6028c506b8Sjruoho static ACPI_STATUS
6128c506b8Sjruoho OptSearchToRoot (
6228c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
6328c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
6428c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
6528c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
6628c506b8Sjruoho     ACPI_BUFFER             *TargetPath,
6728c506b8Sjruoho     char                    **NewPath);
6828c506b8Sjruoho 
6928c506b8Sjruoho static ACPI_STATUS
7028c506b8Sjruoho OptBuildShortestPath (
7128c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
7228c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
7328c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
7428c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
7528c506b8Sjruoho     ACPI_BUFFER             *CurrentPath,
7628c506b8Sjruoho     ACPI_BUFFER             *TargetPath,
7728c506b8Sjruoho     ACPI_SIZE               AmlNameStringLength,
7828c506b8Sjruoho     UINT8                   IsDeclaration,
7928c506b8Sjruoho     char                    **ReturnNewPath);
8028c506b8Sjruoho 
8128c506b8Sjruoho static ACPI_STATUS
8228c506b8Sjruoho OptOptimizeNameDeclaration (
8328c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
8428c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
8528c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
8628c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
8728c506b8Sjruoho     char                    *AmlNameString,
8828c506b8Sjruoho     char                    **NewPath);
8928c506b8Sjruoho 
9028c506b8Sjruoho 
9128c506b8Sjruoho /*******************************************************************************
9228c506b8Sjruoho  *
9328c506b8Sjruoho  * FUNCTION:    OptSearchToRoot
9428c506b8Sjruoho  *
9528c506b8Sjruoho  * PARAMETERS:  Op                  - Current parser op
9628c506b8Sjruoho  *              WalkState           - Current state
9728c506b8Sjruoho  *              CurrentNode         - Where we are in the namespace
9828c506b8Sjruoho  *              TargetNode          - Node to which we are referring
9928c506b8Sjruoho  *              TargetPath          - External full path to the target node
10028c506b8Sjruoho  *              NewPath             - Where the optimized path is returned
10128c506b8Sjruoho  *
10228c506b8Sjruoho  * RETURN:      Status
10328c506b8Sjruoho  *
10428c506b8Sjruoho  * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
10528c506b8Sjruoho  *              name utilizing the search-to-root name resolution algorithm
10628c506b8Sjruoho  *              that is used by AML interpreters.
10728c506b8Sjruoho  *
10828c506b8Sjruoho  ******************************************************************************/
10928c506b8Sjruoho 
11028c506b8Sjruoho static ACPI_STATUS
OptSearchToRoot(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,ACPI_BUFFER * TargetPath,char ** NewPath)11128c506b8Sjruoho OptSearchToRoot (
11228c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
11328c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
11428c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
11528c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
11628c506b8Sjruoho     ACPI_BUFFER             *TargetPath,
11728c506b8Sjruoho     char                    **NewPath)
11828c506b8Sjruoho {
11928c506b8Sjruoho     ACPI_NAMESPACE_NODE     *Node;
12028c506b8Sjruoho     ACPI_GENERIC_STATE      ScopeInfo;
12128c506b8Sjruoho     ACPI_STATUS             Status;
12228c506b8Sjruoho     char                    *Path;
12328c506b8Sjruoho 
12428c506b8Sjruoho 
12528c506b8Sjruoho     ACPI_FUNCTION_NAME (OptSearchToRoot);
12628c506b8Sjruoho 
12728c506b8Sjruoho 
12828c506b8Sjruoho     /*
12928c506b8Sjruoho      * Check if search-to-root can be utilized. Use the last NameSeg of
13028c506b8Sjruoho      * the NamePath and 1) See if can be found and 2) If found, make
13128c506b8Sjruoho      * sure that it is the same node that we want. If there is another
13228c506b8Sjruoho      * name in the search path before the one we want, the nodes will
13328c506b8Sjruoho      * not match, and we cannot use this optimization.
13428c506b8Sjruoho      */
13571e38f1dSchristos     Path = &(((char *) TargetPath->Pointer)[
1365b948c02Schristos         TargetPath->Length - ACPI_NAMESEG_SIZE]);
13728c506b8Sjruoho     ScopeInfo.Scope.Node = CurrentNode;
13828c506b8Sjruoho 
13928c506b8Sjruoho     /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
14028c506b8Sjruoho 
14128c506b8Sjruoho     Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
14228c506b8Sjruoho         ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
14328c506b8Sjruoho         WalkState, &(Node));
14428c506b8Sjruoho     if (ACPI_FAILURE (Status))
14528c506b8Sjruoho     {
14628c506b8Sjruoho         return (Status);
14728c506b8Sjruoho     }
14828c506b8Sjruoho 
14928c506b8Sjruoho     /*
15028c506b8Sjruoho      * We found the name, but we must check to make sure that the node
15128c506b8Sjruoho      * matches. Otherwise, there is another identical name in the search
15228c506b8Sjruoho      * path that precludes the use of this optimization.
15328c506b8Sjruoho      */
15428c506b8Sjruoho     if (Node != TargetNode)
15528c506b8Sjruoho     {
15628c506b8Sjruoho         /*
15728c506b8Sjruoho          * This means that another object with the same name was found first,
15828c506b8Sjruoho          * and we cannot use this optimization.
15928c506b8Sjruoho          */
16028c506b8Sjruoho         return (AE_NOT_FOUND);
16128c506b8Sjruoho     }
16228c506b8Sjruoho 
16328c506b8Sjruoho     /* Found the node, we can use this optimization */
16428c506b8Sjruoho 
16528c506b8Sjruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
16628c506b8Sjruoho         "NAMESEG:   %-24s", Path));
16728c506b8Sjruoho 
16828c506b8Sjruoho     /* We must allocate a new string for the name (TargetPath gets deleted) */
16928c506b8Sjruoho 
1705b948c02Schristos     *NewPath = UtLocalCacheCalloc (ACPI_NAMESEG_SIZE + 1);
171c72da027Schristos     strcpy (*NewPath, Path);
17228c506b8Sjruoho 
173c72da027Schristos     if (strncmp (*NewPath, "_T_", 3))
17428c506b8Sjruoho     {
17571e38f1dSchristos         AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION,
17671e38f1dSchristos             Op, *NewPath);
17728c506b8Sjruoho     }
17828c506b8Sjruoho 
17928c506b8Sjruoho     return (AE_OK);
18028c506b8Sjruoho }
18128c506b8Sjruoho 
18228c506b8Sjruoho 
18328c506b8Sjruoho /*******************************************************************************
18428c506b8Sjruoho  *
18528c506b8Sjruoho  * FUNCTION:    OptBuildShortestPath
18628c506b8Sjruoho  *
18728c506b8Sjruoho  * PARAMETERS:  Op                  - Current parser op
18828c506b8Sjruoho  *              WalkState           - Current state
18928c506b8Sjruoho  *              CurrentNode         - Where we are in the namespace
19028c506b8Sjruoho  *              TargetNode          - Node to which we are referring
19128c506b8Sjruoho  *              CurrentPath         - External full path to the current node
19228c506b8Sjruoho  *              TargetPath          - External full path to the target node
19328c506b8Sjruoho  *              AmlNameStringLength - Length of the original namepath
19428c506b8Sjruoho  *              IsDeclaration       - TRUE for declaration, FALSE for reference
19528c506b8Sjruoho  *              ReturnNewPath       - Where the optimized path is returned
19628c506b8Sjruoho  *
19728c506b8Sjruoho  * RETURN:      Status
19828c506b8Sjruoho  *
19928c506b8Sjruoho  * DESCRIPTION: Build an optimal NamePath using carats
20028c506b8Sjruoho  *
20128c506b8Sjruoho  ******************************************************************************/
20228c506b8Sjruoho 
20328c506b8Sjruoho static ACPI_STATUS
OptBuildShortestPath(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,ACPI_BUFFER * CurrentPath,ACPI_BUFFER * TargetPath,ACPI_SIZE AmlNameStringLength,UINT8 IsDeclaration,char ** ReturnNewPath)20428c506b8Sjruoho OptBuildShortestPath (
20528c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
20628c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
20728c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
20828c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
20928c506b8Sjruoho     ACPI_BUFFER             *CurrentPath,
21028c506b8Sjruoho     ACPI_BUFFER             *TargetPath,
21128c506b8Sjruoho     ACPI_SIZE               AmlNameStringLength,
21228c506b8Sjruoho     UINT8                   IsDeclaration,
21328c506b8Sjruoho     char                    **ReturnNewPath)
21428c506b8Sjruoho {
21528c506b8Sjruoho     UINT32                  NumCommonSegments;
21628c506b8Sjruoho     UINT32                  MaxCommonSegments;
21728c506b8Sjruoho     UINT32                  Index;
21828c506b8Sjruoho     UINT32                  NumCarats;
21928c506b8Sjruoho     UINT32                  i;
220cfbb7280Schristos     char                    *NewPathInternal;
22128c506b8Sjruoho     char                    *NewPathExternal;
22228c506b8Sjruoho     ACPI_NAMESPACE_NODE     *Node;
22328c506b8Sjruoho     ACPI_GENERIC_STATE      ScopeInfo;
22428c506b8Sjruoho     ACPI_STATUS             Status;
22528c506b8Sjruoho     BOOLEAN                 SubPath = FALSE;
22628c506b8Sjruoho 
22728c506b8Sjruoho 
22828c506b8Sjruoho     ACPI_FUNCTION_NAME (OptBuildShortestPath);
22928c506b8Sjruoho 
23028c506b8Sjruoho 
23128c506b8Sjruoho     ScopeInfo.Scope.Node = CurrentNode;
23228c506b8Sjruoho 
23328c506b8Sjruoho     /*
23428c506b8Sjruoho      * Determine the maximum number of NameSegs that the Target and Current paths
23528c506b8Sjruoho      * can possibly have in common. (To optimize, we have to have at least 1)
23628c506b8Sjruoho      *
23728c506b8Sjruoho      * Note: The external NamePath string lengths are always a multiple of 5
2385b948c02Schristos      * (ACPI_NAMESEG_SIZE + separator)
23928c506b8Sjruoho      */
24028c506b8Sjruoho     MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
24128c506b8Sjruoho     if (CurrentPath->Length < TargetPath->Length)
24228c506b8Sjruoho     {
24328c506b8Sjruoho         MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
24428c506b8Sjruoho     }
24528c506b8Sjruoho 
24628c506b8Sjruoho     /*
24728c506b8Sjruoho      * Determine how many NameSegs the two paths have in common.
24828c506b8Sjruoho      * (Starting from the root)
24928c506b8Sjruoho      */
25028c506b8Sjruoho     for (NumCommonSegments = 0;
25128c506b8Sjruoho          NumCommonSegments < MaxCommonSegments;
25228c506b8Sjruoho          NumCommonSegments++)
25328c506b8Sjruoho     {
25428c506b8Sjruoho         /* Compare two single NameSegs */
25528c506b8Sjruoho 
256cfbb7280Schristos         Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
257cfbb7280Schristos 
2585b948c02Schristos         if (!ACPI_COMPARE_NAMESEG (
259cfbb7280Schristos             &(ACPI_CAST_PTR (char, TargetPath->Pointer)) [Index],
260cfbb7280Schristos             &(ACPI_CAST_PTR (char, CurrentPath->Pointer)) [Index]))
26128c506b8Sjruoho         {
26228c506b8Sjruoho             /* Mismatch */
26328c506b8Sjruoho 
26428c506b8Sjruoho             break;
26528c506b8Sjruoho         }
26628c506b8Sjruoho     }
26728c506b8Sjruoho 
26828c506b8Sjruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %u",
26928c506b8Sjruoho         NumCommonSegments));
27028c506b8Sjruoho 
27128c506b8Sjruoho     /* There must be at least 1 common NameSeg in order to optimize */
27228c506b8Sjruoho 
27328c506b8Sjruoho     if (NumCommonSegments == 0)
27428c506b8Sjruoho     {
27528c506b8Sjruoho         return (AE_NOT_FOUND);
27628c506b8Sjruoho     }
27728c506b8Sjruoho 
27828c506b8Sjruoho     if (NumCommonSegments == MaxCommonSegments)
27928c506b8Sjruoho     {
28028c506b8Sjruoho         if (CurrentPath->Length == TargetPath->Length)
28128c506b8Sjruoho         {
28228c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
28328c506b8Sjruoho             return (AE_NOT_FOUND);
28428c506b8Sjruoho         }
28528c506b8Sjruoho         else
28628c506b8Sjruoho         {
28728c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
28828c506b8Sjruoho             SubPath = TRUE;
28928c506b8Sjruoho         }
29028c506b8Sjruoho     }
29128c506b8Sjruoho 
29228c506b8Sjruoho     /* Determine how many prefix Carats are required */
29328c506b8Sjruoho 
29428c506b8Sjruoho     NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
29528c506b8Sjruoho         NumCommonSegments;
29628c506b8Sjruoho 
29728c506b8Sjruoho     /*
29828c506b8Sjruoho      * Construct a new target string
29928c506b8Sjruoho      */
300cfbb7280Schristos     NewPathExternal =
3017efa3256Schristos         UtLocalCacheCalloc (TargetPath->Length + NumCarats + 1);
30228c506b8Sjruoho 
30328c506b8Sjruoho     /* Insert the Carats into the Target string */
30428c506b8Sjruoho 
30528c506b8Sjruoho     for (i = 0; i < NumCarats; i++)
30628c506b8Sjruoho     {
307ff4a156dSchristos         NewPathExternal[i] = AML_PARENT_PREFIX;
30828c506b8Sjruoho     }
30928c506b8Sjruoho 
31028c506b8Sjruoho     /*
31128c506b8Sjruoho      * Copy only the necessary (optimal) segments from the original
31228c506b8Sjruoho      * target string
31328c506b8Sjruoho      */
31428c506b8Sjruoho     Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
31528c506b8Sjruoho 
31628c506b8Sjruoho     /* Special handling for exact subpath in a name declaration */
31728c506b8Sjruoho 
318cfbb7280Schristos     if (IsDeclaration && SubPath &&
319cfbb7280Schristos         (CurrentPath->Length > TargetPath->Length))
32028c506b8Sjruoho     {
32128c506b8Sjruoho         /*
32228c506b8Sjruoho          * The current path is longer than the target, and the target is a
32328c506b8Sjruoho          * subpath of the current path. We must include one more NameSeg of
32428c506b8Sjruoho          * the target path
32528c506b8Sjruoho          */
32628c506b8Sjruoho         Index -= ACPI_PATH_SEGMENT_LENGTH;
32728c506b8Sjruoho 
32828c506b8Sjruoho         /* Special handling for Scope() operator */
32928c506b8Sjruoho 
33028c506b8Sjruoho         if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
33128c506b8Sjruoho         {
332ff4a156dSchristos             NewPathExternal[i] = AML_PARENT_PREFIX;
33328c506b8Sjruoho             i++;
33428c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
33528c506b8Sjruoho         }
33628c506b8Sjruoho     }
33728c506b8Sjruoho 
33828c506b8Sjruoho     /* Make sure we haven't gone off the end of the target path */
33928c506b8Sjruoho 
34028c506b8Sjruoho     if (Index > TargetPath->Length)
34128c506b8Sjruoho     {
34228c506b8Sjruoho         Index = TargetPath->Length;
34328c506b8Sjruoho     }
34428c506b8Sjruoho 
345cfbb7280Schristos     strcpy (&NewPathExternal[i],
346cfbb7280Schristos         &(ACPI_CAST_PTR (char, TargetPath->Pointer))[Index]);
34728c506b8Sjruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
34828c506b8Sjruoho 
34928c506b8Sjruoho     /*
35028c506b8Sjruoho      * Internalize the new target string and check it against the original
35128c506b8Sjruoho      * string to make sure that this is in fact an optimization. If the
35228c506b8Sjruoho      * original string is already optimal, there is no point in continuing.
35328c506b8Sjruoho      */
354cfbb7280Schristos     Status = AcpiNsInternalizeName (NewPathExternal, &NewPathInternal);
35528c506b8Sjruoho     if (ACPI_FAILURE (Status))
35628c506b8Sjruoho     {
35728c506b8Sjruoho         AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
35828c506b8Sjruoho             ASL_NO_ABORT);
359cfbb7280Schristos         goto Cleanup;
36028c506b8Sjruoho     }
36128c506b8Sjruoho 
362cfbb7280Schristos     if (strlen (NewPathInternal) >= AmlNameStringLength)
36328c506b8Sjruoho     {
36428c506b8Sjruoho         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
36528c506b8Sjruoho             " NOT SHORTER (New %u old %u)",
366cfbb7280Schristos             (UINT32) strlen (NewPathInternal),
367cfbb7280Schristos             (UINT32) AmlNameStringLength));
368cfbb7280Schristos 
369cfbb7280Schristos         ACPI_FREE (NewPathInternal);
370cfbb7280Schristos         Status = AE_NOT_FOUND;
371cfbb7280Schristos         goto Cleanup;
37228c506b8Sjruoho     }
37328c506b8Sjruoho 
37428c506b8Sjruoho     /*
37528c506b8Sjruoho      * Check to make sure that the optimization finds the node we are
37628c506b8Sjruoho      * looking for. This is simply a sanity check on the new
37728c506b8Sjruoho      * path that has been created.
37828c506b8Sjruoho      */
379cfbb7280Schristos     Status = AcpiNsLookup (&ScopeInfo, NewPathInternal,
38028c506b8Sjruoho         ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
38128c506b8Sjruoho         ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
38228c506b8Sjruoho     if (ACPI_SUCCESS (Status))
38328c506b8Sjruoho     {
38428c506b8Sjruoho         /* Found the namepath, but make sure the node is correct */
38528c506b8Sjruoho 
38628c506b8Sjruoho         if (Node == TargetNode)
38728c506b8Sjruoho         {
38828c506b8Sjruoho             /* The lookup matched the node, accept this optimization */
38928c506b8Sjruoho 
39028c506b8Sjruoho             AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
39128c506b8Sjruoho                 Op, NewPathExternal);
392cfbb7280Schristos             *ReturnNewPath = NewPathInternal;
39328c506b8Sjruoho         }
39428c506b8Sjruoho         else
39528c506b8Sjruoho         {
39628c506b8Sjruoho             /* Node is not correct, do not use this optimization */
39728c506b8Sjruoho 
39828c506b8Sjruoho             Status = AE_NOT_FOUND;
39928c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
40028c506b8Sjruoho             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
40128c506b8Sjruoho                 "Not using optimized name - found wrong node");
40228c506b8Sjruoho         }
40328c506b8Sjruoho     }
40428c506b8Sjruoho     else
40528c506b8Sjruoho     {
40628c506b8Sjruoho         /* The lookup failed, we obviously cannot use this optimization */
40728c506b8Sjruoho 
408cfbb7280Schristos         ACPI_FREE (NewPathInternal);
409cfbb7280Schristos 
41028c506b8Sjruoho         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
41128c506b8Sjruoho         AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
41228c506b8Sjruoho             "Not using optimized name - did not find node");
41328c506b8Sjruoho     }
41428c506b8Sjruoho 
415cfbb7280Schristos Cleanup:
416cfbb7280Schristos 
41728c506b8Sjruoho     return (Status);
41828c506b8Sjruoho }
41928c506b8Sjruoho 
42028c506b8Sjruoho 
42128c506b8Sjruoho /*******************************************************************************
42228c506b8Sjruoho  *
42328c506b8Sjruoho  * FUNCTION:    OptOptimizeNameDeclaration
42428c506b8Sjruoho  *
42528c506b8Sjruoho  * PARAMETERS:  Op                  - Current parser op
42628c506b8Sjruoho  *              WalkState           - Current state
42728c506b8Sjruoho  *              CurrentNode         - Where we are in the namespace
42828c506b8Sjruoho  *              AmlNameString       - Unoptimized namepath
42928c506b8Sjruoho  *              NewPath             - Where the optimized path is returned
43028c506b8Sjruoho  *
43128c506b8Sjruoho  * RETURN:      Status. AE_OK If path is optimized
43228c506b8Sjruoho  *
43328c506b8Sjruoho  * DESCRIPTION: Perform a simple optimization of removing an extraneous
43428c506b8Sjruoho  *              backslash prefix if we are already at the root scope.
43528c506b8Sjruoho  *
43628c506b8Sjruoho  ******************************************************************************/
43728c506b8Sjruoho 
43828c506b8Sjruoho static ACPI_STATUS
OptOptimizeNameDeclaration(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,char * AmlNameString,char ** NewPath)43928c506b8Sjruoho OptOptimizeNameDeclaration (
44028c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
44128c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
44228c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode,
44328c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode,
44428c506b8Sjruoho     char                    *AmlNameString,
44528c506b8Sjruoho     char                    **NewPath)
44628c506b8Sjruoho {
44728c506b8Sjruoho     ACPI_STATUS             Status;
44828c506b8Sjruoho     char                    *NewPathExternal;
44928c506b8Sjruoho     ACPI_NAMESPACE_NODE     *Node;
45028c506b8Sjruoho 
45128c506b8Sjruoho 
45228c506b8Sjruoho     ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
45328c506b8Sjruoho 
45428c506b8Sjruoho 
45528c506b8Sjruoho     if (((CurrentNode == AcpiGbl_RootNode) ||
45671e38f1dSchristos         (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)) &&
457ff4a156dSchristos             (ACPI_IS_ROOT_PREFIX (AmlNameString[0])))
45828c506b8Sjruoho     {
45928c506b8Sjruoho         /*
46028c506b8Sjruoho          * The current scope is the root, and the namepath has a root prefix
46128c506b8Sjruoho          * that is therefore extraneous. Remove it.
46228c506b8Sjruoho          */
46328c506b8Sjruoho         *NewPath = &AmlNameString[1];
46428c506b8Sjruoho 
46528c506b8Sjruoho         /* Debug output */
46628c506b8Sjruoho 
46728c506b8Sjruoho         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
46828c506b8Sjruoho             NULL, &NewPathExternal);
46928c506b8Sjruoho         if (ACPI_FAILURE (Status))
47028c506b8Sjruoho         {
47128c506b8Sjruoho             AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
47228c506b8Sjruoho                 ASL_NO_ABORT);
47328c506b8Sjruoho             return (Status);
47428c506b8Sjruoho         }
47528c506b8Sjruoho 
47628c506b8Sjruoho         /*
47728c506b8Sjruoho          * Check to make sure that the optimization finds the node we are
47828c506b8Sjruoho          * looking for. This is simply a sanity check on the new
47928c506b8Sjruoho          * path that has been created.
480ff4a156dSchristos          *
481ff4a156dSchristos          * We know that we are at the root, so NULL is used for the scope.
48228c506b8Sjruoho          */
483ff4a156dSchristos         Status = AcpiNsLookup (NULL, *NewPath,
48428c506b8Sjruoho             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
48528c506b8Sjruoho             ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
48628c506b8Sjruoho         if (ACPI_SUCCESS (Status))
48728c506b8Sjruoho         {
48828c506b8Sjruoho             /* Found the namepath, but make sure the node is correct */
48928c506b8Sjruoho 
49028c506b8Sjruoho             if (Node == TargetNode)
49128c506b8Sjruoho             {
49228c506b8Sjruoho                 /* The lookup matched the node, accept this optimization */
49328c506b8Sjruoho 
49428c506b8Sjruoho                 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
49528c506b8Sjruoho                     Op, NewPathExternal);
49628c506b8Sjruoho 
49728c506b8Sjruoho                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
49828c506b8Sjruoho                     "AT ROOT:   %-24s", NewPathExternal));
49928c506b8Sjruoho             }
50028c506b8Sjruoho             else
50128c506b8Sjruoho             {
50228c506b8Sjruoho                 /* Node is not correct, do not use this optimization */
50328c506b8Sjruoho 
50428c506b8Sjruoho                 Status = AE_NOT_FOUND;
50528c506b8Sjruoho                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
50628c506b8Sjruoho                     " ***** WRONG NODE"));
50728c506b8Sjruoho                 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
50828c506b8Sjruoho                     "Not using optimized name - found wrong node");
50928c506b8Sjruoho             }
51028c506b8Sjruoho         }
51128c506b8Sjruoho         else
51228c506b8Sjruoho         {
51328c506b8Sjruoho             /* The lookup failed, we obviously cannot use this optimization */
51428c506b8Sjruoho 
51528c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
51628c506b8Sjruoho                 " ***** NOT FOUND"));
51728c506b8Sjruoho             AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
51828c506b8Sjruoho                 "Not using optimized name - did not find node");
51928c506b8Sjruoho         }
52028c506b8Sjruoho 
52128c506b8Sjruoho         ACPI_FREE (NewPathExternal);
52228c506b8Sjruoho         return (Status);
52328c506b8Sjruoho     }
52428c506b8Sjruoho 
52528c506b8Sjruoho     /* Could not optimize */
52628c506b8Sjruoho 
52728c506b8Sjruoho     return (AE_NOT_FOUND);
52828c506b8Sjruoho }
52928c506b8Sjruoho 
53028c506b8Sjruoho 
53128c506b8Sjruoho /*******************************************************************************
53228c506b8Sjruoho  *
53328c506b8Sjruoho  * FUNCTION:    OptOptimizeNamePath
53428c506b8Sjruoho  *
53528c506b8Sjruoho  * PARAMETERS:  Op                  - Current parser op
53628c506b8Sjruoho  *              Flags               - Opcode info flags
53728c506b8Sjruoho  *              WalkState           - Current state
53828c506b8Sjruoho  *              AmlNameString       - Unoptimized namepath
53928c506b8Sjruoho  *              TargetNode          - Node to which AmlNameString refers
54028c506b8Sjruoho  *
54128c506b8Sjruoho  * RETURN:      None. If path is optimized, the Op is updated with new path
54228c506b8Sjruoho  *
54328c506b8Sjruoho  * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
54428c506b8Sjruoho  *              Must take into account both the current location in the
54528c506b8Sjruoho  *              namespace and the actual reference path.
54628c506b8Sjruoho  *
54728c506b8Sjruoho  ******************************************************************************/
54828c506b8Sjruoho 
54928c506b8Sjruoho void
OptOptimizeNamePath(ACPI_PARSE_OBJECT * Op,UINT32 Flags,ACPI_WALK_STATE * WalkState,char * AmlNameString,ACPI_NAMESPACE_NODE * TargetNode)55028c506b8Sjruoho OptOptimizeNamePath (
55128c506b8Sjruoho     ACPI_PARSE_OBJECT       *Op,
55228c506b8Sjruoho     UINT32                  Flags,
55328c506b8Sjruoho     ACPI_WALK_STATE         *WalkState,
55428c506b8Sjruoho     char                    *AmlNameString,
55528c506b8Sjruoho     ACPI_NAMESPACE_NODE     *TargetNode)
55628c506b8Sjruoho {
55728c506b8Sjruoho     ACPI_STATUS             Status;
55828c506b8Sjruoho     ACPI_BUFFER             TargetPath;
55928c506b8Sjruoho     ACPI_BUFFER             CurrentPath;
56028c506b8Sjruoho     ACPI_SIZE               AmlNameStringLength;
56128c506b8Sjruoho     ACPI_NAMESPACE_NODE     *CurrentNode;
56228c506b8Sjruoho     char                    *ExternalNameString;
56328c506b8Sjruoho     char                    *NewPath = NULL;
56428c506b8Sjruoho     ACPI_SIZE               HowMuchShorter;
56528c506b8Sjruoho     ACPI_PARSE_OBJECT       *NextOp;
56628c506b8Sjruoho 
56728c506b8Sjruoho 
56828c506b8Sjruoho     ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
56928c506b8Sjruoho 
57028c506b8Sjruoho 
57128c506b8Sjruoho     /* This is an optional optimization */
57228c506b8Sjruoho 
5737efa3256Schristos     if (!AslGbl_ReferenceOptimizationFlag)
57428c506b8Sjruoho     {
57528c506b8Sjruoho         return_VOID;
57628c506b8Sjruoho     }
57728c506b8Sjruoho 
57828c506b8Sjruoho     /* Various required items */
57928c506b8Sjruoho 
58028c506b8Sjruoho     if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
58128c506b8Sjruoho     {
58228c506b8Sjruoho         return_VOID;
58328c506b8Sjruoho     }
58428c506b8Sjruoho 
585ff4a156dSchristos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
586ff4a156dSchristos         "PATH OPTIMIZE: Line %5d ParentOp [%12.12s] ThisOp [%12.12s] ",
58728c506b8Sjruoho         Op->Asl.LogicalLineNumber,
58828c506b8Sjruoho         AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
58928c506b8Sjruoho         AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
59028c506b8Sjruoho 
59128c506b8Sjruoho     if (!(Flags & (AML_NAMED | AML_CREATE)))
59228c506b8Sjruoho     {
593ae01dbf5Schristos         if (Op->Asl.CompileFlags & OP_IS_NAME_DECLARATION)
59428c506b8Sjruoho         {
59528c506b8Sjruoho             /* We don't want to fuss with actual name declaration nodes here */
59628c506b8Sjruoho 
59728c506b8Sjruoho             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
59828c506b8Sjruoho                 "******* NAME DECLARATION\n"));
59928c506b8Sjruoho             return_VOID;
60028c506b8Sjruoho         }
60128c506b8Sjruoho     }
60228c506b8Sjruoho 
60328c506b8Sjruoho     /*
60428c506b8Sjruoho      * The original path must be longer than one NameSeg (4 chars) for there
60528c506b8Sjruoho      * to be any possibility that it can be optimized to a shorter string
60628c506b8Sjruoho      */
607c72da027Schristos     AmlNameStringLength = strlen (AmlNameString);
6085b948c02Schristos     if (AmlNameStringLength <= ACPI_NAMESEG_SIZE)
60928c506b8Sjruoho     {
61028c506b8Sjruoho         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
61128c506b8Sjruoho             "NAMESEG %4.4s\n", AmlNameString));
61228c506b8Sjruoho         return_VOID;
61328c506b8Sjruoho     }
61428c506b8Sjruoho 
61528c506b8Sjruoho     /*
61628c506b8Sjruoho      * We need to obtain the node that represents the current scope -- where
61728c506b8Sjruoho      * we are right now in the namespace. We will compare this path
61828c506b8Sjruoho      * against the Namepath, looking for commonality.
61928c506b8Sjruoho      */
62028c506b8Sjruoho     CurrentNode = AcpiGbl_RootNode;
62128c506b8Sjruoho     if (WalkState->ScopeInfo)
62228c506b8Sjruoho     {
62328c506b8Sjruoho         CurrentNode = WalkState->ScopeInfo->Scope.Node;
62428c506b8Sjruoho     }
62528c506b8Sjruoho 
62628c506b8Sjruoho     if (Flags & (AML_NAMED | AML_CREATE))
62728c506b8Sjruoho     {
62828c506b8Sjruoho         /* This is the declaration of a new name */
62928c506b8Sjruoho 
630ff4a156dSchristos         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME\n"));
63128c506b8Sjruoho 
63228c506b8Sjruoho         /*
633ff4a156dSchristos          * The node of interest is the parent of this node (the containing
634ff4a156dSchristos          * scope). The actual namespace node may be up more than one level
635ff4a156dSchristos          * of parse op or it may not exist at all (if we traverse back
636ff4a156dSchristos          * up to the root.)
63728c506b8Sjruoho          */
638ff4a156dSchristos         NextOp = Op->Asl.Parent;
639ff4a156dSchristos         while (NextOp && (!NextOp->Asl.Node))
640ff4a156dSchristos         {
641ff4a156dSchristos             NextOp = NextOp->Asl.Parent;
642ff4a156dSchristos         }
64371e38f1dSchristos 
644ff4a156dSchristos         if (NextOp && NextOp->Asl.Node)
645ff4a156dSchristos         {
646ff4a156dSchristos             CurrentNode = NextOp->Asl.Node;
647ff4a156dSchristos         }
648ff4a156dSchristos         else
64928c506b8Sjruoho         {
65028c506b8Sjruoho             CurrentNode = AcpiGbl_RootNode;
65128c506b8Sjruoho         }
65228c506b8Sjruoho     }
65328c506b8Sjruoho     else
65428c506b8Sjruoho     {
65528c506b8Sjruoho         /* This is a reference to an existing named object */
65628c506b8Sjruoho 
657ff4a156dSchristos         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REFERENCE\n"));
65828c506b8Sjruoho     }
65928c506b8Sjruoho 
66028c506b8Sjruoho     /*
66128c506b8Sjruoho      * Obtain the full paths to the two nodes that we are interested in
66228c506b8Sjruoho      * (Target and current namespace location) in external
66328c506b8Sjruoho      * format -- something we can easily manipulate
66428c506b8Sjruoho      */
66528c506b8Sjruoho     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
666c72da027Schristos     Status = AcpiNsHandleToPathname (TargetNode, &TargetPath, FALSE);
66728c506b8Sjruoho     if (ACPI_FAILURE (Status))
66828c506b8Sjruoho     {
66928c506b8Sjruoho         AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
67028c506b8Sjruoho             ASL_NO_ABORT);
67128c506b8Sjruoho         return_VOID;
67228c506b8Sjruoho     }
67371e38f1dSchristos 
67428c506b8Sjruoho     TargetPath.Length--;    /* Subtract one for null terminator */
67528c506b8Sjruoho 
67628c506b8Sjruoho     /* CurrentPath is the path to this scope (where we are in the namespace) */
67728c506b8Sjruoho 
67828c506b8Sjruoho     CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
679c72da027Schristos     Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath, FALSE);
68028c506b8Sjruoho     if (ACPI_FAILURE (Status))
68128c506b8Sjruoho     {
68228c506b8Sjruoho         AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
68328c506b8Sjruoho             ASL_NO_ABORT);
68428c506b8Sjruoho         return_VOID;
68528c506b8Sjruoho     }
68671e38f1dSchristos 
68728c506b8Sjruoho     CurrentPath.Length--;   /* Subtract one for null terminator */
68828c506b8Sjruoho 
68928c506b8Sjruoho     /* Debug output only */
69028c506b8Sjruoho 
69128c506b8Sjruoho     Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
69228c506b8Sjruoho         NULL, &ExternalNameString);
69328c506b8Sjruoho     if (ACPI_FAILURE (Status))
69428c506b8Sjruoho     {
69528c506b8Sjruoho         AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
69628c506b8Sjruoho             ASL_NO_ABORT);
69728c506b8Sjruoho         return_VOID;
69828c506b8Sjruoho     }
69928c506b8Sjruoho 
70028c506b8Sjruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
701ff4a156dSchristos         "CURRENT SCOPE: (%2u) %-37s FULL PATH TO NAME: (%2u) %-32s ACTUAL AML:%-32s\n",
702ff4a156dSchristos         (UINT32) CurrentPath.Length, (char *) CurrentPath.Pointer,
703ff4a156dSchristos         (UINT32) TargetPath.Length, (char *) TargetPath.Pointer,
704ff4a156dSchristos         ExternalNameString));
70528c506b8Sjruoho 
70628c506b8Sjruoho     ACPI_FREE (ExternalNameString);
70728c506b8Sjruoho 
70828c506b8Sjruoho     /*
7095b948c02Schristos      * Attempt an optimization depending on the type of namepath
71028c506b8Sjruoho      */
71128c506b8Sjruoho     if (Flags & (AML_NAMED | AML_CREATE))
71228c506b8Sjruoho     {
71328c506b8Sjruoho         /*
71428c506b8Sjruoho          * This is a named opcode and the namepath is a name declaration, not
71528c506b8Sjruoho          * a reference.
71628c506b8Sjruoho          */
71728c506b8Sjruoho         Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
71828c506b8Sjruoho             TargetNode, AmlNameString, &NewPath);
71928c506b8Sjruoho         if (ACPI_FAILURE (Status))
72028c506b8Sjruoho         {
72128c506b8Sjruoho             /*
72228c506b8Sjruoho              * 2) now attempt to
72328c506b8Sjruoho              *    optimize the namestring with carats (up-arrow)
72428c506b8Sjruoho              */
72528c506b8Sjruoho             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
72628c506b8Sjruoho                 TargetNode, &CurrentPath, &TargetPath,
72728c506b8Sjruoho                 AmlNameStringLength, 1, &NewPath);
72828c506b8Sjruoho         }
72928c506b8Sjruoho     }
73028c506b8Sjruoho     else
73128c506b8Sjruoho     {
73228c506b8Sjruoho         /*
73328c506b8Sjruoho          * This is a reference to an existing named object
73428c506b8Sjruoho          *
73528c506b8Sjruoho          * 1) Check if search-to-root can be utilized using the last
73628c506b8Sjruoho          *    NameSeg of the NamePath
73728c506b8Sjruoho          */
73828c506b8Sjruoho         Status = OptSearchToRoot (Op, WalkState, CurrentNode,
73928c506b8Sjruoho             TargetNode, &TargetPath, &NewPath);
74028c506b8Sjruoho         if (ACPI_FAILURE (Status))
74128c506b8Sjruoho         {
74228c506b8Sjruoho             /*
74328c506b8Sjruoho              * 2) Search-to-root could not be used, now attempt to
74428c506b8Sjruoho              *    optimize the namestring with carats (up-arrow)
74528c506b8Sjruoho              */
74628c506b8Sjruoho             Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
74728c506b8Sjruoho                 TargetNode, &CurrentPath, &TargetPath,
74828c506b8Sjruoho                 AmlNameStringLength, 0, &NewPath);
74928c506b8Sjruoho         }
75028c506b8Sjruoho     }
75128c506b8Sjruoho 
75228c506b8Sjruoho     /*
75328c506b8Sjruoho      * Success from above indicates that the NamePath was successfully
75428c506b8Sjruoho      * optimized. We need to update the parse op with the new name
75528c506b8Sjruoho      */
75628c506b8Sjruoho     if (ACPI_SUCCESS (Status))
75728c506b8Sjruoho     {
758c72da027Schristos         HowMuchShorter = (AmlNameStringLength - strlen (NewPath));
75928c506b8Sjruoho         OptTotal += HowMuchShorter;
76028c506b8Sjruoho 
761ff4a156dSchristos         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
762ff4a156dSchristos             " REDUCED BY %2u (TOTAL SAVED %2u)",
76328c506b8Sjruoho             (UINT32) HowMuchShorter, OptTotal));
76428c506b8Sjruoho 
76528c506b8Sjruoho         if (Flags & AML_NAMED)
76628c506b8Sjruoho         {
76728c506b8Sjruoho             if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
76828c506b8Sjruoho             {
76928c506b8Sjruoho                 /*
77028c506b8Sjruoho                  * ALIAS is the only oddball opcode, the name declaration
77128c506b8Sjruoho                  * (alias name) is the second operand
77228c506b8Sjruoho                  */
77328c506b8Sjruoho                 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
774c72da027Schristos                 Op->Asl.Child->Asl.Next->Asl.AmlLength = strlen (NewPath);
77528c506b8Sjruoho             }
77628c506b8Sjruoho             else
77728c506b8Sjruoho             {
77828c506b8Sjruoho                 Op->Asl.Child->Asl.Value.String = NewPath;
779c72da027Schristos                 Op->Asl.Child->Asl.AmlLength = strlen (NewPath);
78028c506b8Sjruoho             }
78128c506b8Sjruoho         }
78228c506b8Sjruoho         else if (Flags & AML_CREATE)
78328c506b8Sjruoho         {
78428c506b8Sjruoho             /* Name must appear as the last parameter */
78528c506b8Sjruoho 
78628c506b8Sjruoho             NextOp = Op->Asl.Child;
787ae01dbf5Schristos             while (!(NextOp->Asl.CompileFlags & OP_IS_NAME_DECLARATION))
78828c506b8Sjruoho             {
78928c506b8Sjruoho                 NextOp = NextOp->Asl.Next;
79028c506b8Sjruoho             }
79128c506b8Sjruoho             /* Update the parse node with the new NamePath */
79228c506b8Sjruoho 
79328c506b8Sjruoho             NextOp->Asl.Value.String = NewPath;
794c72da027Schristos             NextOp->Asl.AmlLength = strlen (NewPath);
79528c506b8Sjruoho         }
79628c506b8Sjruoho         else
79728c506b8Sjruoho         {
79828c506b8Sjruoho             /* Update the parse node with the new NamePath */
79928c506b8Sjruoho 
80028c506b8Sjruoho             Op->Asl.Value.String = NewPath;
801c72da027Schristos             Op->Asl.AmlLength = strlen (NewPath);
80228c506b8Sjruoho         }
80328c506b8Sjruoho     }
80428c506b8Sjruoho     else
80528c506b8Sjruoho     {
80628c506b8Sjruoho         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
80728c506b8Sjruoho     }
80828c506b8Sjruoho 
80928c506b8Sjruoho     /* Cleanup path buffers */
81028c506b8Sjruoho 
81128c506b8Sjruoho     ACPI_FREE (TargetPath.Pointer);
81228c506b8Sjruoho     ACPI_FREE (CurrentPath.Pointer);
81328c506b8Sjruoho 
81428c506b8Sjruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
81528c506b8Sjruoho     return_VOID;
81628c506b8Sjruoho }
817