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