i386/identcpu.c: Add VIA Nano support
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / compiler / aslopt.c
blob502f0245ab69c262127492d876fd4e06797ae497
1 /******************************************************************************
3 * Module Name: aslopt- Compiler optimizations
4 * $Revision: 1.25 $
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #include "aslcompiler.h"
119 #include "aslcompiler.y.h"
121 #include "acparser.h"
122 #include "amlcode.h"
123 #include "acnamesp.h"
126 #define _COMPONENT ACPI_COMPILER
127 ACPI_MODULE_NAME ("aslopt")
130 static UINT32 OptTotal = 0;
132 /* Local prototypes */
134 static ACPI_STATUS
135 OptSearchToRoot (
136 ACPI_PARSE_OBJECT *Op,
137 ACPI_WALK_STATE *WalkState,
138 ACPI_NAMESPACE_NODE *CurrentNode,
139 ACPI_NAMESPACE_NODE *TargetNode,
140 ACPI_BUFFER *TargetPath,
141 char **NewPath);
143 static ACPI_STATUS
144 OptBuildShortestPath (
145 ACPI_PARSE_OBJECT *Op,
146 ACPI_WALK_STATE *WalkState,
147 ACPI_NAMESPACE_NODE *CurrentNode,
148 ACPI_NAMESPACE_NODE *TargetNode,
149 ACPI_BUFFER *CurrentPath,
150 ACPI_BUFFER *TargetPath,
151 ACPI_SIZE AmlNameStringLength,
152 UINT8 IsDeclaration,
153 char **ReturnNewPath);
155 static ACPI_STATUS
156 OptOptimizeNameDeclaration (
157 ACPI_PARSE_OBJECT *Op,
158 ACPI_WALK_STATE *WalkState,
159 ACPI_NAMESPACE_NODE *CurrentNode,
160 ACPI_NAMESPACE_NODE *TargetNode,
161 char *AmlNameString,
162 char **NewPath);
165 /*******************************************************************************
167 * FUNCTION: OptSearchToRoot
169 * PARAMETERS: Op - Current parser op
170 * WalkState - Current state
171 * CurrentNode - Where we are in the namespace
172 * TargetNode - Node to which we are referring
173 * TargetPath - External full path to the target node
174 * NewPath - Where the optimized path is returned
176 * RETURN: Status
178 * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
179 * name utilizing the search-to-root name resolution algorithm
180 * that is used by AML interpreters.
182 ******************************************************************************/
184 static ACPI_STATUS
185 OptSearchToRoot (
186 ACPI_PARSE_OBJECT *Op,
187 ACPI_WALK_STATE *WalkState,
188 ACPI_NAMESPACE_NODE *CurrentNode,
189 ACPI_NAMESPACE_NODE *TargetNode,
190 ACPI_BUFFER *TargetPath,
191 char **NewPath)
193 ACPI_NAMESPACE_NODE *Node;
194 ACPI_GENERIC_STATE ScopeInfo;
195 ACPI_STATUS Status;
196 char *Path;
199 ACPI_FUNCTION_NAME (OptSearchToRoot);
203 * Check if search-to-root can be utilized. Use the last NameSeg of
204 * the NamePath and 1) See if can be found and 2) If found, make
205 * sure that it is the same node that we want. If there is another
206 * name in the search path before the one we want, the nodes will
207 * not match, and we cannot use this optimization.
209 Path = &(((char *) TargetPath->Pointer)[TargetPath->Length -
210 ACPI_NAME_SIZE]),
211 ScopeInfo.Scope.Node = CurrentNode;
213 /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
215 Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
216 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
217 WalkState, &(Node));
218 if (ACPI_FAILURE (Status))
220 return (Status);
224 * We found the name, but we must check to make sure that the node
225 * matches. Otherwise, there is another identical name in the search
226 * path that precludes the use of this optimization.
228 if (Node != TargetNode)
231 * This means that another object with the same name was found first,
232 * and we cannot use this optimization.
234 return (AE_NOT_FOUND);
237 /* Found the node, we can use this optimization */
239 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
240 "NAMESEG: %-24s", Path));
242 /* We must allocate a new string for the name (TargetPath gets deleted) */
244 *NewPath = ACPI_ALLOCATE_ZEROED (ACPI_NAME_SIZE + 1);
245 ACPI_STRCPY (*NewPath, Path);
247 if (ACPI_STRNCMP (*NewPath, "_T_", 3))
249 AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION, Op,
250 *NewPath);
253 return (AE_OK);
257 /*******************************************************************************
259 * FUNCTION: OptBuildShortestPath
261 * PARAMETERS: Op - Current parser op
262 * WalkState - Current state
263 * CurrentNode - Where we are in the namespace
264 * TargetNode - Node to which we are referring
265 * CurrentPath - External full path to the current node
266 * TargetPath - External full path to the target node
267 * AmlNameStringLength - Length of the original namepath
268 * IsDeclaration - TRUE for declaration, FALSE for reference
269 * ReturnNewPath - Where the optimized path is returned
271 * RETURN: Status
273 * DESCRIPTION: Build an optimal NamePath using carats
275 ******************************************************************************/
277 static ACPI_STATUS
278 OptBuildShortestPath (
279 ACPI_PARSE_OBJECT *Op,
280 ACPI_WALK_STATE *WalkState,
281 ACPI_NAMESPACE_NODE *CurrentNode,
282 ACPI_NAMESPACE_NODE *TargetNode,
283 ACPI_BUFFER *CurrentPath,
284 ACPI_BUFFER *TargetPath,
285 ACPI_SIZE AmlNameStringLength,
286 UINT8 IsDeclaration,
287 char **ReturnNewPath)
289 UINT32 NumCommonSegments;
290 UINT32 MaxCommonSegments;
291 ACPI_NATIVE_UINT Index;
292 UINT32 NumCarats;
293 ACPI_NATIVE_UINT i;
294 char *NewPath;
295 char *NewPathExternal;
296 ACPI_NAMESPACE_NODE *Node;
297 ACPI_GENERIC_STATE ScopeInfo;
298 ACPI_STATUS Status;
299 BOOLEAN SubPath = FALSE;
302 ACPI_FUNCTION_NAME (OptBuildShortestPath);
305 ScopeInfo.Scope.Node = CurrentNode;
308 * Determine the maximum number of NameSegs that the Target and Current paths
309 * can possibly have in common. (To optimize, we have to have at least 1)
311 * Note: The external NamePath string lengths are always a multiple of 5
312 * (ACPI_NAME_SIZE + separator)
314 MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
315 if (CurrentPath->Length < TargetPath->Length)
317 MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
321 * Determine how many NameSegs the two paths have in common.
322 * (Starting from the root)
324 for (NumCommonSegments = 0;
325 NumCommonSegments < MaxCommonSegments;
326 NumCommonSegments++)
328 /* Compare two single NameSegs */
330 if (ACPI_STRNCMP (
331 &((char *) TargetPath->Pointer)[(NumCommonSegments *
332 ACPI_PATH_SEGMENT_LENGTH) + 1],
333 &((char *) CurrentPath->Pointer)[(NumCommonSegments *
334 ACPI_PATH_SEGMENT_LENGTH) + 1],
335 ACPI_NAME_SIZE))
337 /* Mismatch */
339 break;
343 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %d",
344 NumCommonSegments));
346 /* There must be at least 1 common NameSeg in order to optimize */
348 if (NumCommonSegments == 0)
350 return (AE_NOT_FOUND);
353 if (NumCommonSegments == MaxCommonSegments)
355 if (CurrentPath->Length == TargetPath->Length)
357 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
358 return (AE_NOT_FOUND);
360 else
362 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
363 SubPath = TRUE;
367 /* Determine how many prefix Carats are required */
369 NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
370 NumCommonSegments;
373 * Construct a new target string
375 NewPathExternal = ACPI_ALLOCATE_ZEROED (
376 TargetPath->Length + NumCarats + 1);
378 /* Insert the Carats into the Target string */
380 for (i = 0; i < NumCarats; i++)
382 NewPathExternal[i] = '^';
386 * Copy only the necessary (optimal) segments from the original
387 * target string
389 Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
391 /* Special handling for exact subpath in a name declaration */
393 if (IsDeclaration && SubPath && (CurrentPath->Length > TargetPath->Length))
396 * The current path is longer than the target, and the target is a
397 * subpath of the current path. We must include one more NameSeg of
398 * the target path
400 Index -= ACPI_PATH_SEGMENT_LENGTH;
402 /* Special handling for Scope() operator */
404 if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
406 NewPathExternal[i] = '^';
407 i++;
408 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
412 /* Make sure we haven't gone off the end of the target path */
414 if (Index > TargetPath->Length)
416 Index = TargetPath->Length;
419 ACPI_STRCPY (&NewPathExternal[i], &((char *) TargetPath->Pointer)[Index]);
420 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
423 * Internalize the new target string and check it against the original
424 * string to make sure that this is in fact an optimization. If the
425 * original string is already optimal, there is no point in continuing.
427 Status = AcpiNsInternalizeName (NewPathExternal, &NewPath);
428 if (ACPI_FAILURE (Status))
430 AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
431 ASL_NO_ABORT);
432 ACPI_FREE (NewPathExternal);
433 return (Status);
436 if (ACPI_STRLEN (NewPath) >= AmlNameStringLength)
438 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
439 " NOT SHORTER (New %u old %u)",
440 ACPI_STRLEN (NewPath), AmlNameStringLength));
441 ACPI_FREE (NewPathExternal);
442 return (AE_NOT_FOUND);
446 * Check to make sure that the optimization finds the node we are
447 * looking for. This is simply a sanity check on the new
448 * path that has been created.
450 Status = AcpiNsLookup (&ScopeInfo, NewPath,
451 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
452 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
453 if (ACPI_SUCCESS (Status))
455 /* Found the namepath, but make sure the node is correct */
457 if (Node == TargetNode)
459 /* The lookup matched the node, accept this optimization */
461 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
462 Op, NewPathExternal);
463 *ReturnNewPath = NewPath;
465 else
467 /* Node is not correct, do not use this optimization */
469 Status = AE_NOT_FOUND;
470 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
471 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
472 "Not using optimized name - found wrong node");
475 else
477 /* The lookup failed, we obviously cannot use this optimization */
479 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
480 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
481 "Not using optimized name - did not find node");
484 ACPI_FREE (NewPathExternal);
485 return (Status);
489 /*******************************************************************************
491 * FUNCTION: OptOptimizeNameDeclaration
493 * PARAMETERS: Op - Current parser op
494 * WalkState - Current state
495 * CurrentNode - Where we are in the namespace
496 * AmlNameString - Unoptimized namepath
497 * NewPath - Where the optimized path is returned
499 * RETURN: Status. AE_OK If path is optimized
501 * DESCRIPTION: Perform a simple optimization of removing an extraneous
502 * backslash prefix if we are already at the root scope.
504 ******************************************************************************/
506 static ACPI_STATUS
507 OptOptimizeNameDeclaration (
508 ACPI_PARSE_OBJECT *Op,
509 ACPI_WALK_STATE *WalkState,
510 ACPI_NAMESPACE_NODE *CurrentNode,
511 ACPI_NAMESPACE_NODE *TargetNode,
512 char *AmlNameString,
513 char **NewPath)
515 ACPI_STATUS Status;
516 char *NewPathExternal;
517 ACPI_GENERIC_STATE ScopeInfo;
518 ACPI_NAMESPACE_NODE *Node;
521 ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
524 if (((CurrentNode == AcpiGbl_RootNode) ||
525 (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK)) &&
526 (AmlNameString[0] == '\\'))
529 * The current scope is the root, and the namepath has a root prefix
530 * that is therefore extraneous. Remove it.
532 *NewPath = &AmlNameString[1];
534 /* Debug output */
536 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
537 NULL, &NewPathExternal);
538 if (ACPI_FAILURE (Status))
540 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
541 ASL_NO_ABORT);
542 return (Status);
546 * Check to make sure that the optimization finds the node we are
547 * looking for. This is simply a sanity check on the new
548 * path that has been created.
550 ScopeInfo.Scope.Node = CurrentNode;
551 Status = AcpiNsLookup (&ScopeInfo, *NewPath,
552 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
553 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
554 if (ACPI_SUCCESS (Status))
556 /* Found the namepath, but make sure the node is correct */
558 if (Node == TargetNode)
560 /* The lookup matched the node, accept this optimization */
562 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
563 Op, NewPathExternal);
565 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
566 "AT ROOT: %-24s", NewPathExternal));
568 else
570 /* Node is not correct, do not use this optimization */
572 Status = AE_NOT_FOUND;
573 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
574 " ***** WRONG NODE"));
575 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
576 "Not using optimized name - found wrong node");
579 else
581 /* The lookup failed, we obviously cannot use this optimization */
583 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
584 " ***** NOT FOUND"));
585 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
586 "Not using optimized name - did not find node");
589 ACPI_FREE (NewPathExternal);
590 return (Status);
593 /* Could not optimize */
595 return (AE_NOT_FOUND);
599 /*******************************************************************************
601 * FUNCTION: OptOptimizeNamePath
603 * PARAMETERS: Op - Current parser op
604 * Flags - Opcode info flags
605 * WalkState - Current state
606 * AmlNameString - Unoptimized namepath
607 * TargetNode - Node to which AmlNameString refers
609 * RETURN: None. If path is optimized, the Op is updated with new path
611 * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
612 * Must take into account both the current location in the
613 * namespace and the actual reference path.
615 ******************************************************************************/
617 void
618 OptOptimizeNamePath (
619 ACPI_PARSE_OBJECT *Op,
620 UINT32 Flags,
621 ACPI_WALK_STATE *WalkState,
622 char *AmlNameString,
623 ACPI_NAMESPACE_NODE *TargetNode)
625 ACPI_STATUS Status;
626 ACPI_BUFFER TargetPath;
627 ACPI_BUFFER CurrentPath;
628 ACPI_SIZE AmlNameStringLength;
629 ACPI_NAMESPACE_NODE *CurrentNode;
630 char *ExternalNameString;
631 char *NewPath = NULL;
632 ACPI_SIZE HowMuchShorter;
633 ACPI_PARSE_OBJECT *NextOp;
636 ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
639 /* This is an optional optimization */
641 if (!Gbl_ReferenceOptimizationFlag)
643 return_VOID;
646 /* Various required items */
648 if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
650 return_VOID;
653 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "%5d [%12.12s] [%12.12s] ",
654 Op->Asl.LogicalLineNumber,
655 AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
656 AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
658 if (!(Flags & (AML_NAMED | AML_CREATE)))
660 if (Op->Asl.CompileFlags & NODE_IS_NAME_DECLARATION)
662 /* We don't want to fuss with actual name declaration nodes here */
664 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
665 "******* NAME DECLARATION\n"));
666 return_VOID;
671 * The original path must be longer than one NameSeg (4 chars) for there
672 * to be any possibility that it can be optimized to a shorter string
674 AmlNameStringLength = ACPI_STRLEN (AmlNameString);
675 if (AmlNameStringLength <= ACPI_NAME_SIZE)
677 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
678 "NAMESEG %4.4s\n", AmlNameString));
679 return_VOID;
683 * We need to obtain the node that represents the current scope -- where
684 * we are right now in the namespace. We will compare this path
685 * against the Namepath, looking for commonality.
687 CurrentNode = AcpiGbl_RootNode;
688 if (WalkState->ScopeInfo)
690 CurrentNode = WalkState->ScopeInfo->Scope.Node;
693 if (Flags & (AML_NAMED | AML_CREATE))
695 /* This is the declaration of a new name */
697 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME"));
700 * The node of interest is the parent of this node
701 * (the containing scope)
703 CurrentNode = Op->Asl.Parent->Asl.Node;
704 if (!CurrentNode)
706 CurrentNode = AcpiGbl_RootNode;
709 else
711 /* This is a reference to an existing named object */
713 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REF "));
717 * Obtain the full paths to the two nodes that we are interested in
718 * (Target and current namespace location) in external
719 * format -- something we can easily manipulate
721 TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
722 Status = AcpiNsHandleToPathname (TargetNode, &TargetPath);
723 if (ACPI_FAILURE (Status))
725 AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
726 ASL_NO_ABORT);
727 return_VOID;
729 TargetPath.Length--; /* Subtract one for null terminator */
731 /* CurrentPath is the path to this scope (where we are in the namespace) */
733 CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
734 Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath);
735 if (ACPI_FAILURE (Status))
737 AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
738 ASL_NO_ABORT);
739 return_VOID;
741 CurrentPath.Length--; /* Subtract one for null terminator */
743 /* Debug output only */
745 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
746 NULL, &ExternalNameString);
747 if (ACPI_FAILURE (Status))
749 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
750 ASL_NO_ABORT);
751 return_VOID;
754 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
755 "%37s (%2u) ==> %-32s(%2u) %-32s",
756 (char *) CurrentPath.Pointer, CurrentPath.Length,
757 (char *) TargetPath.Pointer, TargetPath.Length, ExternalNameString));
759 ACPI_FREE (ExternalNameString);
762 * Attempt an optmization depending on the type of namepath
764 if (Flags & (AML_NAMED | AML_CREATE))
767 * This is a named opcode and the namepath is a name declaration, not
768 * a reference.
770 Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
771 TargetNode, AmlNameString, &NewPath);
772 if (ACPI_FAILURE (Status))
775 * 2) now attempt to
776 * optimize the namestring with carats (up-arrow)
778 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
779 TargetNode, &CurrentPath, &TargetPath,
780 AmlNameStringLength, 1, &NewPath);
783 else
786 * This is a reference to an existing named object
788 * 1) Check if search-to-root can be utilized using the last
789 * NameSeg of the NamePath
791 Status = OptSearchToRoot (Op, WalkState, CurrentNode,
792 TargetNode, &TargetPath, &NewPath);
793 if (ACPI_FAILURE (Status))
796 * 2) Search-to-root could not be used, now attempt to
797 * optimize the namestring with carats (up-arrow)
799 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
800 TargetNode, &CurrentPath, &TargetPath,
801 AmlNameStringLength, 0, &NewPath);
806 * Success from above indicates that the NamePath was successfully
807 * optimized. We need to update the parse op with the new name
809 if (ACPI_SUCCESS (Status))
811 HowMuchShorter = (AmlNameStringLength - ACPI_STRLEN (NewPath));
812 OptTotal += HowMuchShorter;
814 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " REDUCED %2u (%u)",
815 HowMuchShorter, OptTotal));
817 if (Flags & AML_NAMED)
819 if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
822 * ALIAS is the only oddball opcode, the name declaration
823 * (alias name) is the second operand
825 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
826 Op->Asl.Child->Asl.Next->Asl.AmlLength = ACPI_STRLEN (NewPath);
828 else
830 Op->Asl.Child->Asl.Value.String = NewPath;
831 Op->Asl.Child->Asl.AmlLength = ACPI_STRLEN (NewPath);
834 else if (Flags & AML_CREATE)
836 /* Name must appear as the last parameter */
838 NextOp = Op->Asl.Child;
839 while (!(NextOp->Asl.CompileFlags & NODE_IS_NAME_DECLARATION))
841 NextOp = NextOp->Asl.Next;
843 /* Update the parse node with the new NamePath */
845 NextOp->Asl.Value.String = NewPath;
846 NextOp->Asl.AmlLength = ACPI_STRLEN (NewPath);
848 else
850 /* Update the parse node with the new NamePath */
852 Op->Asl.Value.String = NewPath;
853 Op->Asl.AmlLength = ACPI_STRLEN (NewPath);
856 else
858 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
861 /* Cleanup path buffers */
863 ACPI_FREE (TargetPath.Pointer);
864 ACPI_FREE (CurrentPath.Pointer);
866 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
867 return_VOID;