Path: blob/main/sys/contrib/dev/acpica/compiler/aslopt.c
48373 views
/******************************************************************************1*2* Module Name: aslopt- Compiler optimizations3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/compiler/aslcompiler.h>152#include "aslcompiler.y.h"153154#include <contrib/dev/acpica/include/acparser.h>155#include <contrib/dev/acpica/include/amlcode.h>156#include <contrib/dev/acpica/include/acnamesp.h>157158159#define _COMPONENT ACPI_COMPILER160ACPI_MODULE_NAME ("aslopt")161162163static UINT32 OptTotal = 0;164165/* Local prototypes */166167static ACPI_STATUS168OptSearchToRoot (169ACPI_PARSE_OBJECT *Op,170ACPI_WALK_STATE *WalkState,171ACPI_NAMESPACE_NODE *CurrentNode,172ACPI_NAMESPACE_NODE *TargetNode,173ACPI_BUFFER *TargetPath,174char **NewPath);175176static ACPI_STATUS177OptBuildShortestPath (178ACPI_PARSE_OBJECT *Op,179ACPI_WALK_STATE *WalkState,180ACPI_NAMESPACE_NODE *CurrentNode,181ACPI_NAMESPACE_NODE *TargetNode,182ACPI_BUFFER *CurrentPath,183ACPI_BUFFER *TargetPath,184ACPI_SIZE AmlNameStringLength,185UINT8 IsDeclaration,186char **ReturnNewPath);187188static ACPI_STATUS189OptOptimizeNameDeclaration (190ACPI_PARSE_OBJECT *Op,191ACPI_WALK_STATE *WalkState,192ACPI_NAMESPACE_NODE *CurrentNode,193ACPI_NAMESPACE_NODE *TargetNode,194char *AmlNameString,195char **NewPath);196197198/*******************************************************************************199*200* FUNCTION: OptSearchToRoot201*202* PARAMETERS: Op - Current parser op203* WalkState - Current state204* CurrentNode - Where we are in the namespace205* TargetNode - Node to which we are referring206* TargetPath - External full path to the target node207* NewPath - Where the optimized path is returned208*209* RETURN: Status210*211* DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI212* name utilizing the search-to-root name resolution algorithm213* that is used by AML interpreters.214*215******************************************************************************/216217static ACPI_STATUS218OptSearchToRoot (219ACPI_PARSE_OBJECT *Op,220ACPI_WALK_STATE *WalkState,221ACPI_NAMESPACE_NODE *CurrentNode,222ACPI_NAMESPACE_NODE *TargetNode,223ACPI_BUFFER *TargetPath,224char **NewPath)225{226ACPI_NAMESPACE_NODE *Node;227ACPI_GENERIC_STATE ScopeInfo;228ACPI_STATUS Status;229char *Path;230231232ACPI_FUNCTION_NAME (OptSearchToRoot);233234235/*236* Check if search-to-root can be utilized. Use the last NameSeg of237* the NamePath and 1) See if can be found and 2) If found, make238* sure that it is the same node that we want. If there is another239* name in the search path before the one we want, the nodes will240* not match, and we cannot use this optimization.241*/242Path = &(((char *) TargetPath->Pointer)[243TargetPath->Length - ACPI_NAMESEG_SIZE]);244ScopeInfo.Scope.Node = CurrentNode;245246/* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */247248Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,249ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,250WalkState, &(Node));251if (ACPI_FAILURE (Status))252{253return (Status);254}255256/*257* We found the name, but we must check to make sure that the node258* matches. Otherwise, there is another identical name in the search259* path that precludes the use of this optimization.260*/261if (Node != TargetNode)262{263/*264* This means that another object with the same name was found first,265* and we cannot use this optimization.266*/267return (AE_NOT_FOUND);268}269270/* Found the node, we can use this optimization */271272ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,273"NAMESEG: %-24s", Path));274275/* We must allocate a new string for the name (TargetPath gets deleted) */276277*NewPath = UtLocalCacheCalloc (ACPI_NAMESEG_SIZE + 1);278strcpy (*NewPath, Path);279280if (strncmp (*NewPath, "_T_", 3))281{282AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION,283Op, *NewPath);284}285286return (AE_OK);287}288289290/*******************************************************************************291*292* FUNCTION: OptBuildShortestPath293*294* PARAMETERS: Op - Current parser op295* WalkState - Current state296* CurrentNode - Where we are in the namespace297* TargetNode - Node to which we are referring298* CurrentPath - External full path to the current node299* TargetPath - External full path to the target node300* AmlNameStringLength - Length of the original namepath301* IsDeclaration - TRUE for declaration, FALSE for reference302* ReturnNewPath - Where the optimized path is returned303*304* RETURN: Status305*306* DESCRIPTION: Build an optimal NamePath using carats307*308******************************************************************************/309310static ACPI_STATUS311OptBuildShortestPath (312ACPI_PARSE_OBJECT *Op,313ACPI_WALK_STATE *WalkState,314ACPI_NAMESPACE_NODE *CurrentNode,315ACPI_NAMESPACE_NODE *TargetNode,316ACPI_BUFFER *CurrentPath,317ACPI_BUFFER *TargetPath,318ACPI_SIZE AmlNameStringLength,319UINT8 IsDeclaration,320char **ReturnNewPath)321{322UINT32 NumCommonSegments;323UINT32 MaxCommonSegments;324UINT32 Index;325UINT32 NumCarats;326UINT32 i;327char *NewPathInternal;328char *NewPathExternal;329ACPI_NAMESPACE_NODE *Node;330ACPI_GENERIC_STATE ScopeInfo;331ACPI_STATUS Status;332BOOLEAN SubPath = FALSE;333334335ACPI_FUNCTION_NAME (OptBuildShortestPath);336337338ScopeInfo.Scope.Node = CurrentNode;339340/*341* Determine the maximum number of NameSegs that the Target and Current paths342* can possibly have in common. (To optimize, we have to have at least 1)343*344* Note: The external NamePath string lengths are always a multiple of 5345* (ACPI_NAMESEG_SIZE + separator)346*/347MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;348if (CurrentPath->Length < TargetPath->Length)349{350MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;351}352353/*354* Determine how many NameSegs the two paths have in common.355* (Starting from the root)356*/357for (NumCommonSegments = 0;358NumCommonSegments < MaxCommonSegments;359NumCommonSegments++)360{361/* Compare two single NameSegs */362363Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;364365if (!ACPI_COMPARE_NAMESEG (366&(ACPI_CAST_PTR (char, TargetPath->Pointer)) [Index],367&(ACPI_CAST_PTR (char, CurrentPath->Pointer)) [Index]))368{369/* Mismatch */370371break;372}373}374375ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %u",376NumCommonSegments));377378/* There must be at least 1 common NameSeg in order to optimize */379380if (NumCommonSegments == 0)381{382return (AE_NOT_FOUND);383}384385if (NumCommonSegments == MaxCommonSegments)386{387if (CurrentPath->Length == TargetPath->Length)388{389ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));390return (AE_NOT_FOUND);391}392else393{394ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));395SubPath = TRUE;396}397}398399/* Determine how many prefix Carats are required */400401NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -402NumCommonSegments;403404/*405* Construct a new target string406*/407NewPathExternal =408UtLocalCacheCalloc (TargetPath->Length + NumCarats + 1);409410/* Insert the Carats into the Target string */411412for (i = 0; i < NumCarats; i++)413{414NewPathExternal[i] = AML_PARENT_PREFIX;415}416417/*418* Copy only the necessary (optimal) segments from the original419* target string420*/421Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;422423/* Special handling for exact subpath in a name declaration */424425if (IsDeclaration && SubPath &&426(CurrentPath->Length > TargetPath->Length))427{428/*429* The current path is longer than the target, and the target is a430* subpath of the current path. We must include one more NameSeg of431* the target path432*/433Index -= ACPI_PATH_SEGMENT_LENGTH;434435/* Special handling for Scope() operator */436437if (Op->Asl.AmlOpcode == AML_SCOPE_OP)438{439NewPathExternal[i] = AML_PARENT_PREFIX;440i++;441ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));442}443}444445/* Make sure we haven't gone off the end of the target path */446447if (Index > TargetPath->Length)448{449Index = TargetPath->Length;450}451452strcpy (&NewPathExternal[i],453&(ACPI_CAST_PTR (char, TargetPath->Pointer))[Index]);454ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));455456/*457* Internalize the new target string and check it against the original458* string to make sure that this is in fact an optimization. If the459* original string is already optimal, there is no point in continuing.460*/461Status = AcpiNsInternalizeName (NewPathExternal, &NewPathInternal);462if (ACPI_FAILURE (Status))463{464AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",465ASL_NO_ABORT);466goto Cleanup;467}468469if (strlen (NewPathInternal) >= AmlNameStringLength)470{471ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,472" NOT SHORTER (New %u old %u)",473(UINT32) strlen (NewPathInternal),474(UINT32) AmlNameStringLength));475476ACPI_FREE (NewPathInternal);477Status = AE_NOT_FOUND;478goto Cleanup;479}480481/*482* Check to make sure that the optimization finds the node we are483* looking for. This is simply a sanity check on the new484* path that has been created.485*/486Status = AcpiNsLookup (&ScopeInfo, NewPathInternal,487ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,488ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));489if (ACPI_SUCCESS (Status))490{491/* Found the namepath, but make sure the node is correct */492493if (Node == TargetNode)494{495/* The lookup matched the node, accept this optimization */496497AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,498Op, NewPathExternal);499*ReturnNewPath = NewPathInternal;500}501else502{503/* Node is not correct, do not use this optimization */504505Status = AE_NOT_FOUND;506ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));507AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,508"Not using optimized name - found wrong node");509}510}511else512{513/* The lookup failed, we obviously cannot use this optimization */514515ACPI_FREE (NewPathInternal);516517ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));518AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,519"Not using optimized name - did not find node");520}521522Cleanup:523524return (Status);525}526527528/*******************************************************************************529*530* FUNCTION: OptOptimizeNameDeclaration531*532* PARAMETERS: Op - Current parser op533* WalkState - Current state534* CurrentNode - Where we are in the namespace535* AmlNameString - Unoptimized namepath536* NewPath - Where the optimized path is returned537*538* RETURN: Status. AE_OK If path is optimized539*540* DESCRIPTION: Perform a simple optimization of removing an extraneous541* backslash prefix if we are already at the root scope.542*543******************************************************************************/544545static ACPI_STATUS546OptOptimizeNameDeclaration (547ACPI_PARSE_OBJECT *Op,548ACPI_WALK_STATE *WalkState,549ACPI_NAMESPACE_NODE *CurrentNode,550ACPI_NAMESPACE_NODE *TargetNode,551char *AmlNameString,552char **NewPath)553{554ACPI_STATUS Status;555char *NewPathExternal;556ACPI_NAMESPACE_NODE *Node;557558559ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);560561562if (((CurrentNode == AcpiGbl_RootNode) ||563(Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)) &&564(ACPI_IS_ROOT_PREFIX (AmlNameString[0])))565{566/*567* The current scope is the root, and the namepath has a root prefix568* that is therefore extraneous. Remove it.569*/570*NewPath = &AmlNameString[1];571572/* Debug output */573574Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,575NULL, &NewPathExternal);576if (ACPI_FAILURE (Status))577{578AslCoreSubsystemError (Op, Status, "Externalizing NamePath",579ASL_NO_ABORT);580return (Status);581}582583/*584* Check to make sure that the optimization finds the node we are585* looking for. This is simply a sanity check on the new586* path that has been created.587*588* We know that we are at the root, so NULL is used for the scope.589*/590Status = AcpiNsLookup (NULL, *NewPath,591ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,592ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));593if (ACPI_SUCCESS (Status))594{595/* Found the namepath, but make sure the node is correct */596597if (Node == TargetNode)598{599/* The lookup matched the node, accept this optimization */600601AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,602Op, NewPathExternal);603604ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,605"AT ROOT: %-24s", NewPathExternal));606}607else608{609/* Node is not correct, do not use this optimization */610611Status = AE_NOT_FOUND;612ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,613" ***** WRONG NODE"));614AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,615"Not using optimized name - found wrong node");616}617}618else619{620/* The lookup failed, we obviously cannot use this optimization */621622ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,623" ***** NOT FOUND"));624AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,625"Not using optimized name - did not find node");626}627628ACPI_FREE (NewPathExternal);629return (Status);630}631632/* Could not optimize */633634return (AE_NOT_FOUND);635}636637638/*******************************************************************************639*640* FUNCTION: OptOptimizeNamePath641*642* PARAMETERS: Op - Current parser op643* Flags - Opcode info flags644* WalkState - Current state645* AmlNameString - Unoptimized namepath646* TargetNode - Node to which AmlNameString refers647*648* RETURN: None. If path is optimized, the Op is updated with new path649*650* DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.651* Must take into account both the current location in the652* namespace and the actual reference path.653*654******************************************************************************/655656void657OptOptimizeNamePath (658ACPI_PARSE_OBJECT *Op,659UINT32 Flags,660ACPI_WALK_STATE *WalkState,661char *AmlNameString,662ACPI_NAMESPACE_NODE *TargetNode)663{664ACPI_STATUS Status;665ACPI_BUFFER TargetPath;666ACPI_BUFFER CurrentPath;667ACPI_SIZE AmlNameStringLength;668ACPI_NAMESPACE_NODE *CurrentNode;669char *ExternalNameString;670char *NewPath = NULL;671ACPI_SIZE HowMuchShorter;672ACPI_PARSE_OBJECT *NextOp;673674675ACPI_FUNCTION_TRACE (OptOptimizeNamePath);676677678/* This is an optional optimization */679680if (!AslGbl_ReferenceOptimizationFlag)681{682return_VOID;683}684685/* Various required items */686687if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)688{689return_VOID;690}691692ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,693"PATH OPTIMIZE: Line %5d ParentOp [%12.12s] ThisOp [%12.12s] ",694Op->Asl.LogicalLineNumber,695AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),696AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));697698if (!(Flags & (AML_NAMED | AML_CREATE)))699{700if (Op->Asl.CompileFlags & OP_IS_NAME_DECLARATION)701{702/* We don't want to fuss with actual name declaration nodes here */703704ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,705"******* NAME DECLARATION\n"));706return_VOID;707}708}709710/*711* The original path must be longer than one NameSeg (4 chars) for there712* to be any possibility that it can be optimized to a shorter string713*/714AmlNameStringLength = strlen (AmlNameString);715if (AmlNameStringLength <= ACPI_NAMESEG_SIZE)716{717ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,718"NAMESEG %4.4s\n", AmlNameString));719return_VOID;720}721722/*723* We need to obtain the node that represents the current scope -- where724* we are right now in the namespace. We will compare this path725* against the Namepath, looking for commonality.726*/727CurrentNode = AcpiGbl_RootNode;728if (WalkState->ScopeInfo)729{730CurrentNode = WalkState->ScopeInfo->Scope.Node;731}732733if (Flags & (AML_NAMED | AML_CREATE))734{735/* This is the declaration of a new name */736737ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME\n"));738739/*740* The node of interest is the parent of this node (the containing741* scope). The actual namespace node may be up more than one level742* of parse op or it may not exist at all (if we traverse back743* up to the root.)744*/745NextOp = Op->Asl.Parent;746while (NextOp && (!NextOp->Asl.Node))747{748NextOp = NextOp->Asl.Parent;749}750751if (NextOp && NextOp->Asl.Node)752{753CurrentNode = NextOp->Asl.Node;754}755else756{757CurrentNode = AcpiGbl_RootNode;758}759}760else761{762/* This is a reference to an existing named object */763764ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REFERENCE\n"));765}766767/*768* Obtain the full paths to the two nodes that we are interested in769* (Target and current namespace location) in external770* format -- something we can easily manipulate771*/772TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;773Status = AcpiNsHandleToPathname (TargetNode, &TargetPath, FALSE);774if (ACPI_FAILURE (Status))775{776AslCoreSubsystemError (Op, Status, "Getting Target NamePath",777ASL_NO_ABORT);778return_VOID;779}780781TargetPath.Length--; /* Subtract one for null terminator */782783/* CurrentPath is the path to this scope (where we are in the namespace) */784785CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;786Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath, FALSE);787if (ACPI_FAILURE (Status))788{789AslCoreSubsystemError (Op, Status, "Getting Current NamePath",790ASL_NO_ABORT);791return_VOID;792}793794CurrentPath.Length--; /* Subtract one for null terminator */795796/* Debug output only */797798Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,799NULL, &ExternalNameString);800if (ACPI_FAILURE (Status))801{802AslCoreSubsystemError (Op, Status, "Externalizing NamePath",803ASL_NO_ABORT);804return_VOID;805}806807ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,808"CURRENT SCOPE: (%2u) %-37s FULL PATH TO NAME: (%2u) %-32s ACTUAL AML:%-32s\n",809(UINT32) CurrentPath.Length, (char *) CurrentPath.Pointer,810(UINT32) TargetPath.Length, (char *) TargetPath.Pointer,811ExternalNameString));812813ACPI_FREE (ExternalNameString);814815/*816* Attempt an optimization depending on the type of namepath817*/818if (Flags & (AML_NAMED | AML_CREATE))819{820/*821* This is a named opcode and the namepath is a name declaration, not822* a reference.823*/824Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,825TargetNode, AmlNameString, &NewPath);826if (ACPI_FAILURE (Status))827{828/*829* 2) now attempt to830* optimize the namestring with carats (up-arrow)831*/832Status = OptBuildShortestPath (Op, WalkState, CurrentNode,833TargetNode, &CurrentPath, &TargetPath,834AmlNameStringLength, 1, &NewPath);835}836}837else838{839/*840* This is a reference to an existing named object841*842* 1) Check if search-to-root can be utilized using the last843* NameSeg of the NamePath844*/845Status = OptSearchToRoot (Op, WalkState, CurrentNode,846TargetNode, &TargetPath, &NewPath);847if (ACPI_FAILURE (Status))848{849/*850* 2) Search-to-root could not be used, now attempt to851* optimize the namestring with carats (up-arrow)852*/853Status = OptBuildShortestPath (Op, WalkState, CurrentNode,854TargetNode, &CurrentPath, &TargetPath,855AmlNameStringLength, 0, &NewPath);856}857}858859/*860* Success from above indicates that the NamePath was successfully861* optimized. We need to update the parse op with the new name862*/863if (ACPI_SUCCESS (Status))864{865HowMuchShorter = (AmlNameStringLength - strlen (NewPath));866OptTotal += HowMuchShorter;867868ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,869" REDUCED BY %2u (TOTAL SAVED %2u)",870(UINT32) HowMuchShorter, OptTotal));871872if (Flags & AML_NAMED)873{874if (Op->Asl.AmlOpcode == AML_ALIAS_OP)875{876/*877* ALIAS is the only oddball opcode, the name declaration878* (alias name) is the second operand879*/880Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;881Op->Asl.Child->Asl.Next->Asl.AmlLength = strlen (NewPath);882}883else884{885Op->Asl.Child->Asl.Value.String = NewPath;886Op->Asl.Child->Asl.AmlLength = strlen (NewPath);887}888}889else if (Flags & AML_CREATE)890{891/* Name must appear as the last parameter */892893NextOp = Op->Asl.Child;894while (!(NextOp->Asl.CompileFlags & OP_IS_NAME_DECLARATION))895{896NextOp = NextOp->Asl.Next;897}898/* Update the parse node with the new NamePath */899900NextOp->Asl.Value.String = NewPath;901NextOp->Asl.AmlLength = strlen (NewPath);902}903else904{905/* Update the parse node with the new NamePath */906907Op->Asl.Value.String = NewPath;908Op->Asl.AmlLength = strlen (NewPath);909}910}911else912{913ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));914}915916/* Cleanup path buffers */917918ACPI_FREE (TargetPath.Pointer);919ACPI_FREE (CurrentPath.Pointer);920921ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));922return_VOID;923}924925926