No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / compiler / aslopt.c
blob3013f4cbd240871df33a772f96901e761f852726
1 /******************************************************************************
3 * Module Name: aslopt- Compiler optimizations
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
117 #include "aslcompiler.h"
118 #include "aslcompiler.y.h"
120 #include "acparser.h"
121 #include "amlcode.h"
122 #include "acnamesp.h"
125 #define _COMPONENT ACPI_COMPILER
126 ACPI_MODULE_NAME ("aslopt")
129 static UINT32 OptTotal = 0;
131 /* Local prototypes */
133 static ACPI_STATUS
134 OptSearchToRoot (
135 ACPI_PARSE_OBJECT *Op,
136 ACPI_WALK_STATE *WalkState,
137 ACPI_NAMESPACE_NODE *CurrentNode,
138 ACPI_NAMESPACE_NODE *TargetNode,
139 ACPI_BUFFER *TargetPath,
140 char **NewPath);
142 static ACPI_STATUS
143 OptBuildShortestPath (
144 ACPI_PARSE_OBJECT *Op,
145 ACPI_WALK_STATE *WalkState,
146 ACPI_NAMESPACE_NODE *CurrentNode,
147 ACPI_NAMESPACE_NODE *TargetNode,
148 ACPI_BUFFER *CurrentPath,
149 ACPI_BUFFER *TargetPath,
150 ACPI_SIZE AmlNameStringLength,
151 UINT8 IsDeclaration,
152 char **ReturnNewPath);
154 static ACPI_STATUS
155 OptOptimizeNameDeclaration (
156 ACPI_PARSE_OBJECT *Op,
157 ACPI_WALK_STATE *WalkState,
158 ACPI_NAMESPACE_NODE *CurrentNode,
159 ACPI_NAMESPACE_NODE *TargetNode,
160 char *AmlNameString,
161 char **NewPath);
164 /*******************************************************************************
166 * FUNCTION: OptSearchToRoot
168 * PARAMETERS: Op - Current parser op
169 * WalkState - Current state
170 * CurrentNode - Where we are in the namespace
171 * TargetNode - Node to which we are referring
172 * TargetPath - External full path to the target node
173 * NewPath - Where the optimized path is returned
175 * RETURN: Status
177 * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
178 * name utilizing the search-to-root name resolution algorithm
179 * that is used by AML interpreters.
181 ******************************************************************************/
183 static ACPI_STATUS
184 OptSearchToRoot (
185 ACPI_PARSE_OBJECT *Op,
186 ACPI_WALK_STATE *WalkState,
187 ACPI_NAMESPACE_NODE *CurrentNode,
188 ACPI_NAMESPACE_NODE *TargetNode,
189 ACPI_BUFFER *TargetPath,
190 char **NewPath)
192 ACPI_NAMESPACE_NODE *Node;
193 ACPI_GENERIC_STATE ScopeInfo;
194 ACPI_STATUS Status;
195 char *Path;
198 ACPI_FUNCTION_NAME (OptSearchToRoot);
202 * Check if search-to-root can be utilized. Use the last NameSeg of
203 * the NamePath and 1) See if can be found and 2) If found, make
204 * sure that it is the same node that we want. If there is another
205 * name in the search path before the one we want, the nodes will
206 * not match, and we cannot use this optimization.
208 Path = &(((char *) TargetPath->Pointer)[TargetPath->Length -
209 ACPI_NAME_SIZE]),
210 ScopeInfo.Scope.Node = CurrentNode;
212 /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
214 Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
215 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
216 WalkState, &(Node));
217 if (ACPI_FAILURE (Status))
219 return (Status);
223 * We found the name, but we must check to make sure that the node
224 * matches. Otherwise, there is another identical name in the search
225 * path that precludes the use of this optimization.
227 if (Node != TargetNode)
230 * This means that another object with the same name was found first,
231 * and we cannot use this optimization.
233 return (AE_NOT_FOUND);
236 /* Found the node, we can use this optimization */
238 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
239 "NAMESEG: %-24s", Path));
241 /* We must allocate a new string for the name (TargetPath gets deleted) */
243 *NewPath = ACPI_ALLOCATE_ZEROED (ACPI_NAME_SIZE + 1);
244 ACPI_STRCPY (*NewPath, Path);
246 if (ACPI_STRNCMP (*NewPath, "_T_", 3))
248 AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION, Op,
249 *NewPath);
252 return (AE_OK);
256 /*******************************************************************************
258 * FUNCTION: OptBuildShortestPath
260 * PARAMETERS: Op - Current parser op
261 * WalkState - Current state
262 * CurrentNode - Where we are in the namespace
263 * TargetNode - Node to which we are referring
264 * CurrentPath - External full path to the current node
265 * TargetPath - External full path to the target node
266 * AmlNameStringLength - Length of the original namepath
267 * IsDeclaration - TRUE for declaration, FALSE for reference
268 * ReturnNewPath - Where the optimized path is returned
270 * RETURN: Status
272 * DESCRIPTION: Build an optimal NamePath using carats
274 ******************************************************************************/
276 static ACPI_STATUS
277 OptBuildShortestPath (
278 ACPI_PARSE_OBJECT *Op,
279 ACPI_WALK_STATE *WalkState,
280 ACPI_NAMESPACE_NODE *CurrentNode,
281 ACPI_NAMESPACE_NODE *TargetNode,
282 ACPI_BUFFER *CurrentPath,
283 ACPI_BUFFER *TargetPath,
284 ACPI_SIZE AmlNameStringLength,
285 UINT8 IsDeclaration,
286 char **ReturnNewPath)
288 UINT32 NumCommonSegments;
289 UINT32 MaxCommonSegments;
290 UINT32 Index;
291 UINT32 NumCarats;
292 UINT32 i;
293 char *NewPath;
294 char *NewPathExternal;
295 ACPI_NAMESPACE_NODE *Node;
296 ACPI_GENERIC_STATE ScopeInfo;
297 ACPI_STATUS Status;
298 BOOLEAN SubPath = FALSE;
301 ACPI_FUNCTION_NAME (OptBuildShortestPath);
304 ScopeInfo.Scope.Node = CurrentNode;
307 * Determine the maximum number of NameSegs that the Target and Current paths
308 * can possibly have in common. (To optimize, we have to have at least 1)
310 * Note: The external NamePath string lengths are always a multiple of 5
311 * (ACPI_NAME_SIZE + separator)
313 MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
314 if (CurrentPath->Length < TargetPath->Length)
316 MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
320 * Determine how many NameSegs the two paths have in common.
321 * (Starting from the root)
323 for (NumCommonSegments = 0;
324 NumCommonSegments < MaxCommonSegments;
325 NumCommonSegments++)
327 /* Compare two single NameSegs */
329 if (ACPI_STRNCMP (
330 &((char *) TargetPath->Pointer)[(NumCommonSegments *
331 ACPI_PATH_SEGMENT_LENGTH) + 1],
332 &((char *) CurrentPath->Pointer)[(NumCommonSegments *
333 ACPI_PATH_SEGMENT_LENGTH) + 1],
334 ACPI_NAME_SIZE))
336 /* Mismatch */
338 break;
342 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %d",
343 NumCommonSegments));
345 /* There must be at least 1 common NameSeg in order to optimize */
347 if (NumCommonSegments == 0)
349 return (AE_NOT_FOUND);
352 if (NumCommonSegments == MaxCommonSegments)
354 if (CurrentPath->Length == TargetPath->Length)
356 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
357 return (AE_NOT_FOUND);
359 else
361 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
362 SubPath = TRUE;
366 /* Determine how many prefix Carats are required */
368 NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
369 NumCommonSegments;
372 * Construct a new target string
374 NewPathExternal = ACPI_ALLOCATE_ZEROED (
375 TargetPath->Length + NumCarats + 1);
377 /* Insert the Carats into the Target string */
379 for (i = 0; i < NumCarats; i++)
381 NewPathExternal[i] = '^';
385 * Copy only the necessary (optimal) segments from the original
386 * target string
388 Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
390 /* Special handling for exact subpath in a name declaration */
392 if (IsDeclaration && SubPath && (CurrentPath->Length > TargetPath->Length))
395 * The current path is longer than the target, and the target is a
396 * subpath of the current path. We must include one more NameSeg of
397 * the target path
399 Index -= ACPI_PATH_SEGMENT_LENGTH;
401 /* Special handling for Scope() operator */
403 if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
405 NewPathExternal[i] = '^';
406 i++;
407 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
411 /* Make sure we haven't gone off the end of the target path */
413 if (Index > TargetPath->Length)
415 Index = TargetPath->Length;
418 ACPI_STRCPY (&NewPathExternal[i], &((char *) TargetPath->Pointer)[Index]);
419 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
422 * Internalize the new target string and check it against the original
423 * string to make sure that this is in fact an optimization. If the
424 * original string is already optimal, there is no point in continuing.
426 Status = AcpiNsInternalizeName (NewPathExternal, &NewPath);
427 if (ACPI_FAILURE (Status))
429 AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
430 ASL_NO_ABORT);
431 ACPI_FREE (NewPathExternal);
432 return (Status);
435 if (ACPI_STRLEN (NewPath) >= AmlNameStringLength)
437 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
438 " NOT SHORTER (New %u old %u)",
439 (UINT32) ACPI_STRLEN (NewPath), (UINT32) AmlNameStringLength));
440 ACPI_FREE (NewPathExternal);
441 return (AE_NOT_FOUND);
445 * Check to make sure that the optimization finds the node we are
446 * looking for. This is simply a sanity check on the new
447 * path that has been created.
449 Status = AcpiNsLookup (&ScopeInfo, NewPath,
450 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
451 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
452 if (ACPI_SUCCESS (Status))
454 /* Found the namepath, but make sure the node is correct */
456 if (Node == TargetNode)
458 /* The lookup matched the node, accept this optimization */
460 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
461 Op, NewPathExternal);
462 *ReturnNewPath = NewPath;
464 else
466 /* Node is not correct, do not use this optimization */
468 Status = AE_NOT_FOUND;
469 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
470 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
471 "Not using optimized name - found wrong node");
474 else
476 /* The lookup failed, we obviously cannot use this optimization */
478 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
479 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
480 "Not using optimized name - did not find node");
483 ACPI_FREE (NewPathExternal);
484 return (Status);
488 /*******************************************************************************
490 * FUNCTION: OptOptimizeNameDeclaration
492 * PARAMETERS: Op - Current parser op
493 * WalkState - Current state
494 * CurrentNode - Where we are in the namespace
495 * AmlNameString - Unoptimized namepath
496 * NewPath - Where the optimized path is returned
498 * RETURN: Status. AE_OK If path is optimized
500 * DESCRIPTION: Perform a simple optimization of removing an extraneous
501 * backslash prefix if we are already at the root scope.
503 ******************************************************************************/
505 static ACPI_STATUS
506 OptOptimizeNameDeclaration (
507 ACPI_PARSE_OBJECT *Op,
508 ACPI_WALK_STATE *WalkState,
509 ACPI_NAMESPACE_NODE *CurrentNode,
510 ACPI_NAMESPACE_NODE *TargetNode,
511 char *AmlNameString,
512 char **NewPath)
514 ACPI_STATUS Status;
515 char *NewPathExternal;
516 ACPI_GENERIC_STATE ScopeInfo;
517 ACPI_NAMESPACE_NODE *Node;
520 ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
523 if (((CurrentNode == AcpiGbl_RootNode) ||
524 (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK)) &&
525 (AmlNameString[0] == '\\'))
528 * The current scope is the root, and the namepath has a root prefix
529 * that is therefore extraneous. Remove it.
531 *NewPath = &AmlNameString[1];
533 /* Debug output */
535 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
536 NULL, &NewPathExternal);
537 if (ACPI_FAILURE (Status))
539 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
540 ASL_NO_ABORT);
541 return (Status);
545 * Check to make sure that the optimization finds the node we are
546 * looking for. This is simply a sanity check on the new
547 * path that has been created.
549 ScopeInfo.Scope.Node = CurrentNode;
550 Status = AcpiNsLookup (&ScopeInfo, *NewPath,
551 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
552 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
553 if (ACPI_SUCCESS (Status))
555 /* Found the namepath, but make sure the node is correct */
557 if (Node == TargetNode)
559 /* The lookup matched the node, accept this optimization */
561 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
562 Op, NewPathExternal);
564 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
565 "AT ROOT: %-24s", NewPathExternal));
567 else
569 /* Node is not correct, do not use this optimization */
571 Status = AE_NOT_FOUND;
572 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
573 " ***** WRONG NODE"));
574 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
575 "Not using optimized name - found wrong node");
578 else
580 /* The lookup failed, we obviously cannot use this optimization */
582 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
583 " ***** NOT FOUND"));
584 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
585 "Not using optimized name - did not find node");
588 ACPI_FREE (NewPathExternal);
589 return (Status);
592 /* Could not optimize */
594 return (AE_NOT_FOUND);
598 /*******************************************************************************
600 * FUNCTION: OptOptimizeNamePath
602 * PARAMETERS: Op - Current parser op
603 * Flags - Opcode info flags
604 * WalkState - Current state
605 * AmlNameString - Unoptimized namepath
606 * TargetNode - Node to which AmlNameString refers
608 * RETURN: None. If path is optimized, the Op is updated with new path
610 * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
611 * Must take into account both the current location in the
612 * namespace and the actual reference path.
614 ******************************************************************************/
616 void
617 OptOptimizeNamePath (
618 ACPI_PARSE_OBJECT *Op,
619 UINT32 Flags,
620 ACPI_WALK_STATE *WalkState,
621 char *AmlNameString,
622 ACPI_NAMESPACE_NODE *TargetNode)
624 ACPI_STATUS Status;
625 ACPI_BUFFER TargetPath;
626 ACPI_BUFFER CurrentPath;
627 ACPI_SIZE AmlNameStringLength;
628 ACPI_NAMESPACE_NODE *CurrentNode;
629 char *ExternalNameString;
630 char *NewPath = NULL;
631 ACPI_SIZE HowMuchShorter;
632 ACPI_PARSE_OBJECT *NextOp;
635 ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
638 /* This is an optional optimization */
640 if (!Gbl_ReferenceOptimizationFlag)
642 return_VOID;
645 /* Various required items */
647 if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
649 return_VOID;
652 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "%5d [%12.12s] [%12.12s] ",
653 Op->Asl.LogicalLineNumber,
654 AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
655 AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
657 if (!(Flags & (AML_NAMED | AML_CREATE)))
659 if (Op->Asl.CompileFlags & NODE_IS_NAME_DECLARATION)
661 /* We don't want to fuss with actual name declaration nodes here */
663 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
664 "******* NAME DECLARATION\n"));
665 return_VOID;
670 * The original path must be longer than one NameSeg (4 chars) for there
671 * to be any possibility that it can be optimized to a shorter string
673 AmlNameStringLength = ACPI_STRLEN (AmlNameString);
674 if (AmlNameStringLength <= ACPI_NAME_SIZE)
676 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
677 "NAMESEG %4.4s\n", AmlNameString));
678 return_VOID;
682 * We need to obtain the node that represents the current scope -- where
683 * we are right now in the namespace. We will compare this path
684 * against the Namepath, looking for commonality.
686 CurrentNode = AcpiGbl_RootNode;
687 if (WalkState->ScopeInfo)
689 CurrentNode = WalkState->ScopeInfo->Scope.Node;
692 if (Flags & (AML_NAMED | AML_CREATE))
694 /* This is the declaration of a new name */
696 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME"));
699 * The node of interest is the parent of this node
700 * (the containing scope)
702 CurrentNode = Op->Asl.Parent->Asl.Node;
703 if (!CurrentNode)
705 CurrentNode = AcpiGbl_RootNode;
708 else
710 /* This is a reference to an existing named object */
712 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REF "));
716 * Obtain the full paths to the two nodes that we are interested in
717 * (Target and current namespace location) in external
718 * format -- something we can easily manipulate
720 TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
721 Status = AcpiNsHandleToPathname (TargetNode, &TargetPath);
722 if (ACPI_FAILURE (Status))
724 AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
725 ASL_NO_ABORT);
726 return_VOID;
728 TargetPath.Length--; /* Subtract one for null terminator */
730 /* CurrentPath is the path to this scope (where we are in the namespace) */
732 CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
733 Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath);
734 if (ACPI_FAILURE (Status))
736 AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
737 ASL_NO_ABORT);
738 return_VOID;
740 CurrentPath.Length--; /* Subtract one for null terminator */
742 /* Debug output only */
744 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
745 NULL, &ExternalNameString);
746 if (ACPI_FAILURE (Status))
748 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
749 ASL_NO_ABORT);
750 return_VOID;
753 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
754 "%37s (%2u) ==> %-32s(%2u) %-32s",
755 (char *) CurrentPath.Pointer, (UINT32) CurrentPath.Length,
756 (char *) TargetPath.Pointer, (UINT32) TargetPath.Length, ExternalNameString));
758 ACPI_FREE (ExternalNameString);
761 * Attempt an optmization depending on the type of namepath
763 if (Flags & (AML_NAMED | AML_CREATE))
766 * This is a named opcode and the namepath is a name declaration, not
767 * a reference.
769 Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
770 TargetNode, AmlNameString, &NewPath);
771 if (ACPI_FAILURE (Status))
774 * 2) now attempt to
775 * optimize the namestring with carats (up-arrow)
777 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
778 TargetNode, &CurrentPath, &TargetPath,
779 AmlNameStringLength, 1, &NewPath);
782 else
785 * This is a reference to an existing named object
787 * 1) Check if search-to-root can be utilized using the last
788 * NameSeg of the NamePath
790 Status = OptSearchToRoot (Op, WalkState, CurrentNode,
791 TargetNode, &TargetPath, &NewPath);
792 if (ACPI_FAILURE (Status))
795 * 2) Search-to-root could not be used, now attempt to
796 * optimize the namestring with carats (up-arrow)
798 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
799 TargetNode, &CurrentPath, &TargetPath,
800 AmlNameStringLength, 0, &NewPath);
805 * Success from above indicates that the NamePath was successfully
806 * optimized. We need to update the parse op with the new name
808 if (ACPI_SUCCESS (Status))
810 HowMuchShorter = (AmlNameStringLength - ACPI_STRLEN (NewPath));
811 OptTotal += HowMuchShorter;
813 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " REDUCED %2u (%u)",
814 (UINT32) HowMuchShorter, OptTotal));
816 if (Flags & AML_NAMED)
818 if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
821 * ALIAS is the only oddball opcode, the name declaration
822 * (alias name) is the second operand
824 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
825 Op->Asl.Child->Asl.Next->Asl.AmlLength = ACPI_STRLEN (NewPath);
827 else
829 Op->Asl.Child->Asl.Value.String = NewPath;
830 Op->Asl.Child->Asl.AmlLength = ACPI_STRLEN (NewPath);
833 else if (Flags & AML_CREATE)
835 /* Name must appear as the last parameter */
837 NextOp = Op->Asl.Child;
838 while (!(NextOp->Asl.CompileFlags & NODE_IS_NAME_DECLARATION))
840 NextOp = NextOp->Asl.Next;
842 /* Update the parse node with the new NamePath */
844 NextOp->Asl.Value.String = NewPath;
845 NextOp->Asl.AmlLength = ACPI_STRLEN (NewPath);
847 else
849 /* Update the parse node with the new NamePath */
851 Op->Asl.Value.String = NewPath;
852 Op->Asl.AmlLength = ACPI_STRLEN (NewPath);
855 else
857 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
860 /* Cleanup path buffers */
862 ACPI_FREE (TargetPath.Pointer);
863 ACPI_FREE (CurrentPath.Pointer);
865 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
866 return_VOID;