1 /******************************************************************************
2 *
3 * Module Name: aslopt- Compiler optimizations
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2023, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
46
47 #include "acparser.h"
48 #include "amlcode.h"
49 #include "acnamesp.h"
50
51
52 #define _COMPONENT ACPI_COMPILER
53 ACPI_MODULE_NAME ("aslopt")
54
55
56 static UINT32 OptTotal = 0;
57
58 /* Local prototypes */
59
60 static ACPI_STATUS
61 OptSearchToRoot (
62 ACPI_PARSE_OBJECT *Op,
63 ACPI_WALK_STATE *WalkState,
64 ACPI_NAMESPACE_NODE *CurrentNode,
65 ACPI_NAMESPACE_NODE *TargetNode,
66 ACPI_BUFFER *TargetPath,
67 char **NewPath);
68
69 static ACPI_STATUS
70 OptBuildShortestPath (
71 ACPI_PARSE_OBJECT *Op,
72 ACPI_WALK_STATE *WalkState,
73 ACPI_NAMESPACE_NODE *CurrentNode,
74 ACPI_NAMESPACE_NODE *TargetNode,
75 ACPI_BUFFER *CurrentPath,
76 ACPI_BUFFER *TargetPath,
77 ACPI_SIZE AmlNameStringLength,
78 UINT8 IsDeclaration,
79 char **ReturnNewPath);
80
81 static ACPI_STATUS
82 OptOptimizeNameDeclaration (
83 ACPI_PARSE_OBJECT *Op,
84 ACPI_WALK_STATE *WalkState,
85 ACPI_NAMESPACE_NODE *CurrentNode,
86 ACPI_NAMESPACE_NODE *TargetNode,
87 char *AmlNameString,
88 char **NewPath);
89
90
91 /*******************************************************************************
92 *
93 * FUNCTION: OptSearchToRoot
94 *
95 * PARAMETERS: Op - Current parser op
96 * WalkState - Current state
97 * CurrentNode - Where we are in the namespace
98 * TargetNode - Node to which we are referring
99 * TargetPath - External full path to the target node
100 * NewPath - Where the optimized path is returned
101 *
102 * RETURN: Status
103 *
104 * DESCRIPTION: Attempt to optimize a reference to a single 4-character ACPI
105 * name utilizing the search-to-root name resolution algorithm
106 * that is used by AML interpreters.
107 *
108 ******************************************************************************/
109
110 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)111 OptSearchToRoot (
112 ACPI_PARSE_OBJECT *Op,
113 ACPI_WALK_STATE *WalkState,
114 ACPI_NAMESPACE_NODE *CurrentNode,
115 ACPI_NAMESPACE_NODE *TargetNode,
116 ACPI_BUFFER *TargetPath,
117 char **NewPath)
118 {
119 ACPI_NAMESPACE_NODE *Node;
120 ACPI_GENERIC_STATE ScopeInfo;
121 ACPI_STATUS Status;
122 char *Path;
123
124
125 ACPI_FUNCTION_NAME (OptSearchToRoot);
126
127
128 /*
129 * Check if search-to-root can be utilized. Use the last NameSeg of
130 * the NamePath and 1) See if can be found and 2) If found, make
131 * sure that it is the same node that we want. If there is another
132 * name in the search path before the one we want, the nodes will
133 * not match, and we cannot use this optimization.
134 */
135 Path = &(((char *) TargetPath->Pointer)[
136 TargetPath->Length - ACPI_NAMESEG_SIZE]);
137 ScopeInfo.Scope.Node = CurrentNode;
138
139 /* Lookup the NameSeg using SEARCH_PARENT (search-to-root) */
140
141 Status = AcpiNsLookup (&ScopeInfo, Path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
142 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
143 WalkState, &(Node));
144 if (ACPI_FAILURE (Status))
145 {
146 return (Status);
147 }
148
149 /*
150 * We found the name, but we must check to make sure that the node
151 * matches. Otherwise, there is another identical name in the search
152 * path that precludes the use of this optimization.
153 */
154 if (Node != TargetNode)
155 {
156 /*
157 * This means that another object with the same name was found first,
158 * and we cannot use this optimization.
159 */
160 return (AE_NOT_FOUND);
161 }
162
163 /* Found the node, we can use this optimization */
164
165 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
166 "NAMESEG: %-24s", Path));
167
168 /* We must allocate a new string for the name (TargetPath gets deleted) */
169
170 *NewPath = UtLocalCacheCalloc (ACPI_NAMESEG_SIZE + 1);
171 strcpy (*NewPath, Path);
172
173 if (strncmp (*NewPath, "_T_", 3))
174 {
175 AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION,
176 Op, *NewPath);
177 }
178
179 return (AE_OK);
180 }
181
182
183 /*******************************************************************************
184 *
185 * FUNCTION: OptBuildShortestPath
186 *
187 * PARAMETERS: Op - Current parser op
188 * WalkState - Current state
189 * CurrentNode - Where we are in the namespace
190 * TargetNode - Node to which we are referring
191 * CurrentPath - External full path to the current node
192 * TargetPath - External full path to the target node
193 * AmlNameStringLength - Length of the original namepath
194 * IsDeclaration - TRUE for declaration, FALSE for reference
195 * ReturnNewPath - Where the optimized path is returned
196 *
197 * RETURN: Status
198 *
199 * DESCRIPTION: Build an optimal NamePath using carats
200 *
201 ******************************************************************************/
202
203 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)204 OptBuildShortestPath (
205 ACPI_PARSE_OBJECT *Op,
206 ACPI_WALK_STATE *WalkState,
207 ACPI_NAMESPACE_NODE *CurrentNode,
208 ACPI_NAMESPACE_NODE *TargetNode,
209 ACPI_BUFFER *CurrentPath,
210 ACPI_BUFFER *TargetPath,
211 ACPI_SIZE AmlNameStringLength,
212 UINT8 IsDeclaration,
213 char **ReturnNewPath)
214 {
215 UINT32 NumCommonSegments;
216 UINT32 MaxCommonSegments;
217 UINT32 Index;
218 UINT32 NumCarats;
219 UINT32 i;
220 char *NewPathInternal;
221 char *NewPathExternal;
222 ACPI_NAMESPACE_NODE *Node;
223 ACPI_GENERIC_STATE ScopeInfo;
224 ACPI_STATUS Status;
225 BOOLEAN SubPath = FALSE;
226
227
228 ACPI_FUNCTION_NAME (OptBuildShortestPath);
229
230
231 ScopeInfo.Scope.Node = CurrentNode;
232
233 /*
234 * Determine the maximum number of NameSegs that the Target and Current paths
235 * can possibly have in common. (To optimize, we have to have at least 1)
236 *
237 * Note: The external NamePath string lengths are always a multiple of 5
238 * (ACPI_NAMESEG_SIZE + separator)
239 */
240 MaxCommonSegments = TargetPath->Length / ACPI_PATH_SEGMENT_LENGTH;
241 if (CurrentPath->Length < TargetPath->Length)
242 {
243 MaxCommonSegments = CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH;
244 }
245
246 /*
247 * Determine how many NameSegs the two paths have in common.
248 * (Starting from the root)
249 */
250 for (NumCommonSegments = 0;
251 NumCommonSegments < MaxCommonSegments;
252 NumCommonSegments++)
253 {
254 /* Compare two single NameSegs */
255
256 Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
257
258 if (!ACPI_COMPARE_NAMESEG (
259 &(ACPI_CAST_PTR (char, TargetPath->Pointer)) [Index],
260 &(ACPI_CAST_PTR (char, CurrentPath->Pointer)) [Index]))
261 {
262 /* Mismatch */
263
264 break;
265 }
266 }
267
268 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " COMMON: %u",
269 NumCommonSegments));
270
271 /* There must be at least 1 common NameSeg in order to optimize */
272
273 if (NumCommonSegments == 0)
274 {
275 return (AE_NOT_FOUND);
276 }
277
278 if (NumCommonSegments == MaxCommonSegments)
279 {
280 if (CurrentPath->Length == TargetPath->Length)
281 {
282 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SAME PATH"));
283 return (AE_NOT_FOUND);
284 }
285 else
286 {
287 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " SUBPATH"));
288 SubPath = TRUE;
289 }
290 }
291
292 /* Determine how many prefix Carats are required */
293
294 NumCarats = (CurrentPath->Length / ACPI_PATH_SEGMENT_LENGTH) -
295 NumCommonSegments;
296
297 /*
298 * Construct a new target string
299 */
300 NewPathExternal =
301 UtLocalCacheCalloc (TargetPath->Length + NumCarats + 1);
302
303 /* Insert the Carats into the Target string */
304
305 for (i = 0; i < NumCarats; i++)
306 {
307 NewPathExternal[i] = AML_PARENT_PREFIX;
308 }
309
310 /*
311 * Copy only the necessary (optimal) segments from the original
312 * target string
313 */
314 Index = (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1;
315
316 /* Special handling for exact subpath in a name declaration */
317
318 if (IsDeclaration && SubPath &&
319 (CurrentPath->Length > TargetPath->Length))
320 {
321 /*
322 * The current path is longer than the target, and the target is a
323 * subpath of the current path. We must include one more NameSeg of
324 * the target path
325 */
326 Index -= ACPI_PATH_SEGMENT_LENGTH;
327
328 /* Special handling for Scope() operator */
329
330 if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
331 {
332 NewPathExternal[i] = AML_PARENT_PREFIX;
333 i++;
334 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
335 }
336 }
337
338 /* Make sure we haven't gone off the end of the target path */
339
340 if (Index > TargetPath->Length)
341 {
342 Index = TargetPath->Length;
343 }
344
345 strcpy (&NewPathExternal[i],
346 &(ACPI_CAST_PTR (char, TargetPath->Pointer))[Index]);
347 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
348
349 /*
350 * Internalize the new target string and check it against the original
351 * string to make sure that this is in fact an optimization. If the
352 * original string is already optimal, there is no point in continuing.
353 */
354 Status = AcpiNsInternalizeName (NewPathExternal, &NewPathInternal);
355 if (ACPI_FAILURE (Status))
356 {
357 AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
358 ASL_NO_ABORT);
359 goto Cleanup;
360 }
361
362 if (strlen (NewPathInternal) >= AmlNameStringLength)
363 {
364 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
365 " NOT SHORTER (New %u old %u)",
366 (UINT32) strlen (NewPathInternal),
367 (UINT32) AmlNameStringLength));
368
369 ACPI_FREE (NewPathInternal);
370 Status = AE_NOT_FOUND;
371 goto Cleanup;
372 }
373
374 /*
375 * Check to make sure that the optimization finds the node we are
376 * looking for. This is simply a sanity check on the new
377 * path that has been created.
378 */
379 Status = AcpiNsLookup (&ScopeInfo, NewPathInternal,
380 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
381 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
382 if (ACPI_SUCCESS (Status))
383 {
384 /* Found the namepath, but make sure the node is correct */
385
386 if (Node == TargetNode)
387 {
388 /* The lookup matched the node, accept this optimization */
389
390 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
391 Op, NewPathExternal);
392 *ReturnNewPath = NewPathInternal;
393 }
394 else
395 {
396 /* Node is not correct, do not use this optimization */
397
398 Status = AE_NOT_FOUND;
399 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
400 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
401 "Not using optimized name - found wrong node");
402 }
403 }
404 else
405 {
406 /* The lookup failed, we obviously cannot use this optimization */
407
408 ACPI_FREE (NewPathInternal);
409
410 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
411 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
412 "Not using optimized name - did not find node");
413 }
414
415 Cleanup:
416
417 return (Status);
418 }
419
420
421 /*******************************************************************************
422 *
423 * FUNCTION: OptOptimizeNameDeclaration
424 *
425 * PARAMETERS: Op - Current parser op
426 * WalkState - Current state
427 * CurrentNode - Where we are in the namespace
428 * AmlNameString - Unoptimized namepath
429 * NewPath - Where the optimized path is returned
430 *
431 * RETURN: Status. AE_OK If path is optimized
432 *
433 * DESCRIPTION: Perform a simple optimization of removing an extraneous
434 * backslash prefix if we are already at the root scope.
435 *
436 ******************************************************************************/
437
438 static ACPI_STATUS
OptOptimizeNameDeclaration(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState,ACPI_NAMESPACE_NODE * CurrentNode,ACPI_NAMESPACE_NODE * TargetNode,char * AmlNameString,char ** NewPath)439 OptOptimizeNameDeclaration (
440 ACPI_PARSE_OBJECT *Op,
441 ACPI_WALK_STATE *WalkState,
442 ACPI_NAMESPACE_NODE *CurrentNode,
443 ACPI_NAMESPACE_NODE *TargetNode,
444 char *AmlNameString,
445 char **NewPath)
446 {
447 ACPI_STATUS Status;
448 char *NewPathExternal;
449 ACPI_NAMESPACE_NODE *Node;
450
451
452 ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
453
454
455 if (((CurrentNode == AcpiGbl_RootNode) ||
456 (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)) &&
457 (ACPI_IS_ROOT_PREFIX (AmlNameString[0])))
458 {
459 /*
460 * The current scope is the root, and the namepath has a root prefix
461 * that is therefore extraneous. Remove it.
462 */
463 *NewPath = &AmlNameString[1];
464
465 /* Debug output */
466
467 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
468 NULL, &NewPathExternal);
469 if (ACPI_FAILURE (Status))
470 {
471 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
472 ASL_NO_ABORT);
473 return (Status);
474 }
475
476 /*
477 * Check to make sure that the optimization finds the node we are
478 * looking for. This is simply a sanity check on the new
479 * path that has been created.
480 *
481 * We know that we are at the root, so NULL is used for the scope.
482 */
483 Status = AcpiNsLookup (NULL, *NewPath,
484 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
485 ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
486 if (ACPI_SUCCESS (Status))
487 {
488 /* Found the namepath, but make sure the node is correct */
489
490 if (Node == TargetNode)
491 {
492 /* The lookup matched the node, accept this optimization */
493
494 AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
495 Op, NewPathExternal);
496
497 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
498 "AT ROOT: %-24s", NewPathExternal));
499 }
500 else
501 {
502 /* Node is not correct, do not use this optimization */
503
504 Status = AE_NOT_FOUND;
505 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
506 " ***** WRONG NODE"));
507 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
508 "Not using optimized name - found wrong node");
509 }
510 }
511 else
512 {
513 /* The lookup failed, we obviously cannot use this optimization */
514
515 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
516 " ***** NOT FOUND"));
517 AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
518 "Not using optimized name - did not find node");
519 }
520
521 ACPI_FREE (NewPathExternal);
522 return (Status);
523 }
524
525 /* Could not optimize */
526
527 return (AE_NOT_FOUND);
528 }
529
530
531 /*******************************************************************************
532 *
533 * FUNCTION: OptOptimizeNamePath
534 *
535 * PARAMETERS: Op - Current parser op
536 * Flags - Opcode info flags
537 * WalkState - Current state
538 * AmlNameString - Unoptimized namepath
539 * TargetNode - Node to which AmlNameString refers
540 *
541 * RETURN: None. If path is optimized, the Op is updated with new path
542 *
543 * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
544 * Must take into account both the current location in the
545 * namespace and the actual reference path.
546 *
547 ******************************************************************************/
548
549 void
OptOptimizeNamePath(ACPI_PARSE_OBJECT * Op,UINT32 Flags,ACPI_WALK_STATE * WalkState,char * AmlNameString,ACPI_NAMESPACE_NODE * TargetNode)550 OptOptimizeNamePath (
551 ACPI_PARSE_OBJECT *Op,
552 UINT32 Flags,
553 ACPI_WALK_STATE *WalkState,
554 char *AmlNameString,
555 ACPI_NAMESPACE_NODE *TargetNode)
556 {
557 ACPI_STATUS Status;
558 ACPI_BUFFER TargetPath;
559 ACPI_BUFFER CurrentPath;
560 ACPI_SIZE AmlNameStringLength;
561 ACPI_NAMESPACE_NODE *CurrentNode;
562 char *ExternalNameString;
563 char *NewPath = NULL;
564 ACPI_SIZE HowMuchShorter;
565 ACPI_PARSE_OBJECT *NextOp;
566
567
568 ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
569
570
571 /* This is an optional optimization */
572
573 if (!AslGbl_ReferenceOptimizationFlag)
574 {
575 return_VOID;
576 }
577
578 /* Various required items */
579
580 if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
581 {
582 return_VOID;
583 }
584
585 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
586 "PATH OPTIMIZE: Line %5d ParentOp [%12.12s] ThisOp [%12.12s] ",
587 Op->Asl.LogicalLineNumber,
588 AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
589 AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
590
591 if (!(Flags & (AML_NAMED | AML_CREATE)))
592 {
593 if (Op->Asl.CompileFlags & OP_IS_NAME_DECLARATION)
594 {
595 /* We don't want to fuss with actual name declaration nodes here */
596
597 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
598 "******* NAME DECLARATION\n"));
599 return_VOID;
600 }
601 }
602
603 /*
604 * The original path must be longer than one NameSeg (4 chars) for there
605 * to be any possibility that it can be optimized to a shorter string
606 */
607 AmlNameStringLength = strlen (AmlNameString);
608 if (AmlNameStringLength <= ACPI_NAMESEG_SIZE)
609 {
610 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
611 "NAMESEG %4.4s\n", AmlNameString));
612 return_VOID;
613 }
614
615 /*
616 * We need to obtain the node that represents the current scope -- where
617 * we are right now in the namespace. We will compare this path
618 * against the Namepath, looking for commonality.
619 */
620 CurrentNode = AcpiGbl_RootNode;
621 if (WalkState->ScopeInfo)
622 {
623 CurrentNode = WalkState->ScopeInfo->Scope.Node;
624 }
625
626 if (Flags & (AML_NAMED | AML_CREATE))
627 {
628 /* This is the declaration of a new name */
629
630 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME\n"));
631
632 /*
633 * The node of interest is the parent of this node (the containing
634 * scope). The actual namespace node may be up more than one level
635 * of parse op or it may not exist at all (if we traverse back
636 * up to the root.)
637 */
638 NextOp = Op->Asl.Parent;
639 while (NextOp && (!NextOp->Asl.Node))
640 {
641 NextOp = NextOp->Asl.Parent;
642 }
643
644 if (NextOp && NextOp->Asl.Node)
645 {
646 CurrentNode = NextOp->Asl.Node;
647 }
648 else
649 {
650 CurrentNode = AcpiGbl_RootNode;
651 }
652 }
653 else
654 {
655 /* This is a reference to an existing named object */
656
657 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REFERENCE\n"));
658 }
659
660 /*
661 * Obtain the full paths to the two nodes that we are interested in
662 * (Target and current namespace location) in external
663 * format -- something we can easily manipulate
664 */
665 TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
666 Status = AcpiNsHandleToPathname (TargetNode, &TargetPath, FALSE);
667 if (ACPI_FAILURE (Status))
668 {
669 AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
670 ASL_NO_ABORT);
671 return_VOID;
672 }
673
674 TargetPath.Length--; /* Subtract one for null terminator */
675
676 /* CurrentPath is the path to this scope (where we are in the namespace) */
677
678 CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
679 Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath, FALSE);
680 if (ACPI_FAILURE (Status))
681 {
682 AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
683 ASL_NO_ABORT);
684 return_VOID;
685 }
686
687 CurrentPath.Length--; /* Subtract one for null terminator */
688
689 /* Debug output only */
690
691 Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
692 NULL, &ExternalNameString);
693 if (ACPI_FAILURE (Status))
694 {
695 AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
696 ASL_NO_ABORT);
697 return_VOID;
698 }
699
700 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
701 "CURRENT SCOPE: (%2u) %-37s FULL PATH TO NAME: (%2u) %-32s ACTUAL AML:%-32s\n",
702 (UINT32) CurrentPath.Length, (char *) CurrentPath.Pointer,
703 (UINT32) TargetPath.Length, (char *) TargetPath.Pointer,
704 ExternalNameString));
705
706 ACPI_FREE (ExternalNameString);
707
708 /*
709 * Attempt an optimization depending on the type of namepath
710 */
711 if (Flags & (AML_NAMED | AML_CREATE))
712 {
713 /*
714 * This is a named opcode and the namepath is a name declaration, not
715 * a reference.
716 */
717 Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
718 TargetNode, AmlNameString, &NewPath);
719 if (ACPI_FAILURE (Status))
720 {
721 /*
722 * 2) now attempt to
723 * optimize the namestring with carats (up-arrow)
724 */
725 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
726 TargetNode, &CurrentPath, &TargetPath,
727 AmlNameStringLength, 1, &NewPath);
728 }
729 }
730 else
731 {
732 /*
733 * This is a reference to an existing named object
734 *
735 * 1) Check if search-to-root can be utilized using the last
736 * NameSeg of the NamePath
737 */
738 Status = OptSearchToRoot (Op, WalkState, CurrentNode,
739 TargetNode, &TargetPath, &NewPath);
740 if (ACPI_FAILURE (Status))
741 {
742 /*
743 * 2) Search-to-root could not be used, now attempt to
744 * optimize the namestring with carats (up-arrow)
745 */
746 Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
747 TargetNode, &CurrentPath, &TargetPath,
748 AmlNameStringLength, 0, &NewPath);
749 }
750 }
751
752 /*
753 * Success from above indicates that the NamePath was successfully
754 * optimized. We need to update the parse op with the new name
755 */
756 if (ACPI_SUCCESS (Status))
757 {
758 HowMuchShorter = (AmlNameStringLength - strlen (NewPath));
759 OptTotal += HowMuchShorter;
760
761 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
762 " REDUCED BY %2u (TOTAL SAVED %2u)",
763 (UINT32) HowMuchShorter, OptTotal));
764
765 if (Flags & AML_NAMED)
766 {
767 if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
768 {
769 /*
770 * ALIAS is the only oddball opcode, the name declaration
771 * (alias name) is the second operand
772 */
773 Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
774 Op->Asl.Child->Asl.Next->Asl.AmlLength = strlen (NewPath);
775 }
776 else
777 {
778 Op->Asl.Child->Asl.Value.String = NewPath;
779 Op->Asl.Child->Asl.AmlLength = strlen (NewPath);
780 }
781 }
782 else if (Flags & AML_CREATE)
783 {
784 /* Name must appear as the last parameter */
785
786 NextOp = Op->Asl.Child;
787 while (!(NextOp->Asl.CompileFlags & OP_IS_NAME_DECLARATION))
788 {
789 NextOp = NextOp->Asl.Next;
790 }
791 /* Update the parse node with the new NamePath */
792
793 NextOp->Asl.Value.String = NewPath;
794 NextOp->Asl.AmlLength = strlen (NewPath);
795 }
796 else
797 {
798 /* Update the parse node with the new NamePath */
799
800 Op->Asl.Value.String = NewPath;
801 Op->Asl.AmlLength = strlen (NewPath);
802 }
803 }
804 else
805 {
806 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
807 }
808
809 /* Cleanup path buffers */
810
811 ACPI_FREE (TargetPath.Pointer);
812 ACPI_FREE (CurrentPath.Pointer);
813
814 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
815 return_VOID;
816 }
817