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