aslopt.c revision 1.1.1.6 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.5 christos * Copyright (C) 2000 - 2015, 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 #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.1 jruoho 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.1.1.3 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.1.1.3 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.1 jruoho Path = &(((char *) TargetPath->Pointer)[TargetPath->Length -
136 1.1 jruoho 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.1 jruoho ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
143 1.1 jruoho 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.1.1.3 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.1.1.4 christos *NewPath = UtStringCacheCalloc (ACPI_NAME_SIZE + 1);
171 1.1.1.6 christos strcpy (*NewPath, Path);
172 1.1 jruoho
173 1.1.1.6 christos if (strncmp (*NewPath, "_T_", 3))
174 1.1 jruoho {
175 1.1 jruoho AslError (ASL_OPTIMIZATION, ASL_MSG_SINGLE_NAME_OPTIMIZATION, Op,
176 1.1 jruoho *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.1 jruoho char *NewPath;
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.1.1.3 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.1.1.3 christos if (!ACPI_COMPARE_NAME (
257 1.1.1.3 christos &((char *) TargetPath->Pointer)[
258 1.1.1.3 christos (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1],
259 1.1.1.3 christos &((char *) CurrentPath->Pointer)[
260 1.1.1.3 christos (NumCommonSegments * ACPI_PATH_SEGMENT_LENGTH) + 1]))
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.1 jruoho NumCommonSegments;
296 1.1 jruoho
297 1.1 jruoho /*
298 1.1 jruoho * Construct a new target string
299 1.1 jruoho */
300 1.1 jruoho NewPathExternal = ACPI_ALLOCATE_ZEROED (
301 1.1 jruoho 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.1.1.3 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.1 jruoho if (IsDeclaration && SubPath && (CurrentPath->Length > TargetPath->Length))
319 1.1 jruoho {
320 1.1 jruoho /*
321 1.1 jruoho * The current path is longer than the target, and the target is a
322 1.1 jruoho * subpath of the current path. We must include one more NameSeg of
323 1.1 jruoho * the target path
324 1.1 jruoho */
325 1.1 jruoho Index -= ACPI_PATH_SEGMENT_LENGTH;
326 1.1 jruoho
327 1.1 jruoho /* Special handling for Scope() operator */
328 1.1 jruoho
329 1.1 jruoho if (Op->Asl.AmlOpcode == AML_SCOPE_OP)
330 1.1 jruoho {
331 1.1.1.3 christos NewPathExternal[i] = AML_PARENT_PREFIX;
332 1.1 jruoho i++;
333 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "(EXTRA ^)"));
334 1.1 jruoho }
335 1.1 jruoho }
336 1.1 jruoho
337 1.1 jruoho /* Make sure we haven't gone off the end of the target path */
338 1.1 jruoho
339 1.1 jruoho if (Index > TargetPath->Length)
340 1.1 jruoho {
341 1.1 jruoho Index = TargetPath->Length;
342 1.1 jruoho }
343 1.1 jruoho
344 1.1.1.6 christos strcpy (&NewPathExternal[i], &((char *) TargetPath->Pointer)[Index]);
345 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " %-24s", NewPathExternal));
346 1.1 jruoho
347 1.1 jruoho /*
348 1.1 jruoho * Internalize the new target string and check it against the original
349 1.1 jruoho * string to make sure that this is in fact an optimization. If the
350 1.1 jruoho * original string is already optimal, there is no point in continuing.
351 1.1 jruoho */
352 1.1 jruoho Status = AcpiNsInternalizeName (NewPathExternal, &NewPath);
353 1.1 jruoho if (ACPI_FAILURE (Status))
354 1.1 jruoho {
355 1.1 jruoho AslCoreSubsystemError (Op, Status, "Internalizing new NamePath",
356 1.1 jruoho ASL_NO_ABORT);
357 1.1 jruoho ACPI_FREE (NewPathExternal);
358 1.1 jruoho return (Status);
359 1.1 jruoho }
360 1.1 jruoho
361 1.1.1.6 christos if (strlen (NewPath) >= AmlNameStringLength)
362 1.1 jruoho {
363 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
364 1.1 jruoho " NOT SHORTER (New %u old %u)",
365 1.1.1.6 christos (UINT32) strlen (NewPath), (UINT32) AmlNameStringLength));
366 1.1 jruoho ACPI_FREE (NewPathExternal);
367 1.1 jruoho return (AE_NOT_FOUND);
368 1.1 jruoho }
369 1.1 jruoho
370 1.1 jruoho /*
371 1.1 jruoho * Check to make sure that the optimization finds the node we are
372 1.1.1.3 christos * looking for. This is simply a sanity check on the new
373 1.1 jruoho * path that has been created.
374 1.1 jruoho */
375 1.1 jruoho Status = AcpiNsLookup (&ScopeInfo, NewPath,
376 1.1 jruoho ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
377 1.1 jruoho ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
378 1.1 jruoho if (ACPI_SUCCESS (Status))
379 1.1 jruoho {
380 1.1 jruoho /* Found the namepath, but make sure the node is correct */
381 1.1 jruoho
382 1.1 jruoho if (Node == TargetNode)
383 1.1 jruoho {
384 1.1 jruoho /* The lookup matched the node, accept this optimization */
385 1.1 jruoho
386 1.1 jruoho AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
387 1.1 jruoho Op, NewPathExternal);
388 1.1 jruoho *ReturnNewPath = NewPath;
389 1.1 jruoho }
390 1.1 jruoho else
391 1.1 jruoho {
392 1.1 jruoho /* Node is not correct, do not use this optimization */
393 1.1 jruoho
394 1.1 jruoho Status = AE_NOT_FOUND;
395 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** WRONG NODE"));
396 1.1 jruoho AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
397 1.1 jruoho "Not using optimized name - found wrong node");
398 1.1 jruoho }
399 1.1 jruoho }
400 1.1 jruoho else
401 1.1 jruoho {
402 1.1 jruoho /* The lookup failed, we obviously cannot use this optimization */
403 1.1 jruoho
404 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ***** NOT FOUND"));
405 1.1 jruoho AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
406 1.1 jruoho "Not using optimized name - did not find node");
407 1.1 jruoho }
408 1.1 jruoho
409 1.1 jruoho ACPI_FREE (NewPathExternal);
410 1.1 jruoho return (Status);
411 1.1 jruoho }
412 1.1 jruoho
413 1.1 jruoho
414 1.1 jruoho /*******************************************************************************
415 1.1 jruoho *
416 1.1 jruoho * FUNCTION: OptOptimizeNameDeclaration
417 1.1 jruoho *
418 1.1 jruoho * PARAMETERS: Op - Current parser op
419 1.1 jruoho * WalkState - Current state
420 1.1 jruoho * CurrentNode - Where we are in the namespace
421 1.1 jruoho * AmlNameString - Unoptimized namepath
422 1.1 jruoho * NewPath - Where the optimized path is returned
423 1.1 jruoho *
424 1.1 jruoho * RETURN: Status. AE_OK If path is optimized
425 1.1 jruoho *
426 1.1 jruoho * DESCRIPTION: Perform a simple optimization of removing an extraneous
427 1.1 jruoho * backslash prefix if we are already at the root scope.
428 1.1 jruoho *
429 1.1 jruoho ******************************************************************************/
430 1.1 jruoho
431 1.1 jruoho static ACPI_STATUS
432 1.1 jruoho OptOptimizeNameDeclaration (
433 1.1 jruoho ACPI_PARSE_OBJECT *Op,
434 1.1 jruoho ACPI_WALK_STATE *WalkState,
435 1.1 jruoho ACPI_NAMESPACE_NODE *CurrentNode,
436 1.1 jruoho ACPI_NAMESPACE_NODE *TargetNode,
437 1.1 jruoho char *AmlNameString,
438 1.1 jruoho char **NewPath)
439 1.1 jruoho {
440 1.1 jruoho ACPI_STATUS Status;
441 1.1 jruoho char *NewPathExternal;
442 1.1 jruoho ACPI_NAMESPACE_NODE *Node;
443 1.1 jruoho
444 1.1 jruoho
445 1.1 jruoho ACPI_FUNCTION_TRACE (OptOptimizeNameDeclaration);
446 1.1 jruoho
447 1.1 jruoho
448 1.1 jruoho if (((CurrentNode == AcpiGbl_RootNode) ||
449 1.1 jruoho (Op->Common.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK)) &&
450 1.1.1.3 christos (ACPI_IS_ROOT_PREFIX (AmlNameString[0])))
451 1.1 jruoho {
452 1.1 jruoho /*
453 1.1 jruoho * The current scope is the root, and the namepath has a root prefix
454 1.1.1.3 christos * that is therefore extraneous. Remove it.
455 1.1 jruoho */
456 1.1 jruoho *NewPath = &AmlNameString[1];
457 1.1 jruoho
458 1.1 jruoho /* Debug output */
459 1.1 jruoho
460 1.1 jruoho Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, *NewPath,
461 1.1 jruoho NULL, &NewPathExternal);
462 1.1 jruoho if (ACPI_FAILURE (Status))
463 1.1 jruoho {
464 1.1 jruoho AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
465 1.1 jruoho ASL_NO_ABORT);
466 1.1 jruoho return (Status);
467 1.1 jruoho }
468 1.1 jruoho
469 1.1 jruoho /*
470 1.1 jruoho * Check to make sure that the optimization finds the node we are
471 1.1.1.3 christos * looking for. This is simply a sanity check on the new
472 1.1 jruoho * path that has been created.
473 1.1.1.3 christos *
474 1.1.1.3 christos * We know that we are at the root, so NULL is used for the scope.
475 1.1 jruoho */
476 1.1.1.3 christos Status = AcpiNsLookup (NULL, *NewPath,
477 1.1 jruoho ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
478 1.1 jruoho ACPI_NS_DONT_OPEN_SCOPE, WalkState, &(Node));
479 1.1 jruoho if (ACPI_SUCCESS (Status))
480 1.1 jruoho {
481 1.1 jruoho /* Found the namepath, but make sure the node is correct */
482 1.1 jruoho
483 1.1 jruoho if (Node == TargetNode)
484 1.1 jruoho {
485 1.1 jruoho /* The lookup matched the node, accept this optimization */
486 1.1 jruoho
487 1.1 jruoho AslError (ASL_OPTIMIZATION, ASL_MSG_NAME_OPTIMIZATION,
488 1.1 jruoho Op, NewPathExternal);
489 1.1 jruoho
490 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
491 1.1 jruoho "AT ROOT: %-24s", NewPathExternal));
492 1.1 jruoho }
493 1.1 jruoho else
494 1.1 jruoho {
495 1.1 jruoho /* Node is not correct, do not use this optimization */
496 1.1 jruoho
497 1.1 jruoho Status = AE_NOT_FOUND;
498 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
499 1.1 jruoho " ***** WRONG NODE"));
500 1.1 jruoho AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
501 1.1 jruoho "Not using optimized name - found wrong node");
502 1.1 jruoho }
503 1.1 jruoho }
504 1.1 jruoho else
505 1.1 jruoho {
506 1.1 jruoho /* The lookup failed, we obviously cannot use this optimization */
507 1.1 jruoho
508 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
509 1.1 jruoho " ***** NOT FOUND"));
510 1.1 jruoho AslError (ASL_WARNING, ASL_MSG_COMPILER_INTERNAL, Op,
511 1.1 jruoho "Not using optimized name - did not find node");
512 1.1 jruoho }
513 1.1 jruoho
514 1.1 jruoho ACPI_FREE (NewPathExternal);
515 1.1 jruoho return (Status);
516 1.1 jruoho }
517 1.1 jruoho
518 1.1 jruoho /* Could not optimize */
519 1.1 jruoho
520 1.1 jruoho return (AE_NOT_FOUND);
521 1.1 jruoho }
522 1.1 jruoho
523 1.1 jruoho
524 1.1 jruoho /*******************************************************************************
525 1.1 jruoho *
526 1.1 jruoho * FUNCTION: OptOptimizeNamePath
527 1.1 jruoho *
528 1.1 jruoho * PARAMETERS: Op - Current parser op
529 1.1 jruoho * Flags - Opcode info flags
530 1.1 jruoho * WalkState - Current state
531 1.1 jruoho * AmlNameString - Unoptimized namepath
532 1.1 jruoho * TargetNode - Node to which AmlNameString refers
533 1.1 jruoho *
534 1.1.1.3 christos * RETURN: None. If path is optimized, the Op is updated with new path
535 1.1 jruoho *
536 1.1 jruoho * DESCRIPTION: Optimize a Named Declaration or Reference to the minimal length.
537 1.1 jruoho * Must take into account both the current location in the
538 1.1 jruoho * namespace and the actual reference path.
539 1.1 jruoho *
540 1.1 jruoho ******************************************************************************/
541 1.1 jruoho
542 1.1 jruoho void
543 1.1 jruoho OptOptimizeNamePath (
544 1.1 jruoho ACPI_PARSE_OBJECT *Op,
545 1.1 jruoho UINT32 Flags,
546 1.1 jruoho ACPI_WALK_STATE *WalkState,
547 1.1 jruoho char *AmlNameString,
548 1.1 jruoho ACPI_NAMESPACE_NODE *TargetNode)
549 1.1 jruoho {
550 1.1 jruoho ACPI_STATUS Status;
551 1.1 jruoho ACPI_BUFFER TargetPath;
552 1.1 jruoho ACPI_BUFFER CurrentPath;
553 1.1 jruoho ACPI_SIZE AmlNameStringLength;
554 1.1 jruoho ACPI_NAMESPACE_NODE *CurrentNode;
555 1.1 jruoho char *ExternalNameString;
556 1.1 jruoho char *NewPath = NULL;
557 1.1 jruoho ACPI_SIZE HowMuchShorter;
558 1.1 jruoho ACPI_PARSE_OBJECT *NextOp;
559 1.1 jruoho
560 1.1 jruoho
561 1.1 jruoho ACPI_FUNCTION_TRACE (OptOptimizeNamePath);
562 1.1 jruoho
563 1.1 jruoho
564 1.1 jruoho /* This is an optional optimization */
565 1.1 jruoho
566 1.1 jruoho if (!Gbl_ReferenceOptimizationFlag)
567 1.1 jruoho {
568 1.1 jruoho return_VOID;
569 1.1 jruoho }
570 1.1 jruoho
571 1.1 jruoho /* Various required items */
572 1.1 jruoho
573 1.1 jruoho if (!TargetNode || !WalkState || !AmlNameString || !Op->Common.Parent)
574 1.1 jruoho {
575 1.1 jruoho return_VOID;
576 1.1 jruoho }
577 1.1 jruoho
578 1.1.1.3 christos ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
579 1.1.1.3 christos "PATH OPTIMIZE: Line %5d ParentOp [%12.12s] ThisOp [%12.12s] ",
580 1.1 jruoho Op->Asl.LogicalLineNumber,
581 1.1 jruoho AcpiPsGetOpcodeName (Op->Common.Parent->Common.AmlOpcode),
582 1.1 jruoho AcpiPsGetOpcodeName (Op->Common.AmlOpcode)));
583 1.1 jruoho
584 1.1 jruoho if (!(Flags & (AML_NAMED | AML_CREATE)))
585 1.1 jruoho {
586 1.1 jruoho if (Op->Asl.CompileFlags & NODE_IS_NAME_DECLARATION)
587 1.1 jruoho {
588 1.1 jruoho /* We don't want to fuss with actual name declaration nodes here */
589 1.1 jruoho
590 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
591 1.1 jruoho "******* NAME DECLARATION\n"));
592 1.1 jruoho return_VOID;
593 1.1 jruoho }
594 1.1 jruoho }
595 1.1 jruoho
596 1.1 jruoho /*
597 1.1 jruoho * The original path must be longer than one NameSeg (4 chars) for there
598 1.1 jruoho * to be any possibility that it can be optimized to a shorter string
599 1.1 jruoho */
600 1.1.1.6 christos AmlNameStringLength = strlen (AmlNameString);
601 1.1 jruoho if (AmlNameStringLength <= ACPI_NAME_SIZE)
602 1.1 jruoho {
603 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
604 1.1 jruoho "NAMESEG %4.4s\n", AmlNameString));
605 1.1 jruoho return_VOID;
606 1.1 jruoho }
607 1.1 jruoho
608 1.1 jruoho /*
609 1.1 jruoho * We need to obtain the node that represents the current scope -- where
610 1.1.1.3 christos * we are right now in the namespace. We will compare this path
611 1.1 jruoho * against the Namepath, looking for commonality.
612 1.1 jruoho */
613 1.1 jruoho CurrentNode = AcpiGbl_RootNode;
614 1.1 jruoho if (WalkState->ScopeInfo)
615 1.1 jruoho {
616 1.1 jruoho CurrentNode = WalkState->ScopeInfo->Scope.Node;
617 1.1 jruoho }
618 1.1 jruoho
619 1.1 jruoho if (Flags & (AML_NAMED | AML_CREATE))
620 1.1 jruoho {
621 1.1 jruoho /* This is the declaration of a new name */
622 1.1 jruoho
623 1.1.1.3 christos ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "NAME\n"));
624 1.1 jruoho
625 1.1 jruoho /*
626 1.1.1.3 christos * The node of interest is the parent of this node (the containing
627 1.1.1.3 christos * scope). The actual namespace node may be up more than one level
628 1.1.1.3 christos * of parse op or it may not exist at all (if we traverse back
629 1.1.1.3 christos * up to the root.)
630 1.1 jruoho */
631 1.1.1.3 christos NextOp = Op->Asl.Parent;
632 1.1.1.3 christos while (NextOp && (!NextOp->Asl.Node))
633 1.1.1.3 christos {
634 1.1.1.3 christos NextOp = NextOp->Asl.Parent;
635 1.1.1.3 christos }
636 1.1.1.3 christos if (NextOp && NextOp->Asl.Node)
637 1.1.1.3 christos {
638 1.1.1.3 christos CurrentNode = NextOp->Asl.Node;
639 1.1.1.3 christos }
640 1.1.1.3 christos else
641 1.1 jruoho {
642 1.1 jruoho CurrentNode = AcpiGbl_RootNode;
643 1.1 jruoho }
644 1.1 jruoho }
645 1.1 jruoho else
646 1.1 jruoho {
647 1.1 jruoho /* This is a reference to an existing named object */
648 1.1 jruoho
649 1.1.1.3 christos ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "REFERENCE\n"));
650 1.1 jruoho }
651 1.1 jruoho
652 1.1 jruoho /*
653 1.1 jruoho * Obtain the full paths to the two nodes that we are interested in
654 1.1 jruoho * (Target and current namespace location) in external
655 1.1 jruoho * format -- something we can easily manipulate
656 1.1 jruoho */
657 1.1 jruoho TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
658 1.1.1.6 christos Status = AcpiNsHandleToPathname (TargetNode, &TargetPath, FALSE);
659 1.1 jruoho if (ACPI_FAILURE (Status))
660 1.1 jruoho {
661 1.1 jruoho AslCoreSubsystemError (Op, Status, "Getting Target NamePath",
662 1.1 jruoho ASL_NO_ABORT);
663 1.1 jruoho return_VOID;
664 1.1 jruoho }
665 1.1 jruoho TargetPath.Length--; /* Subtract one for null terminator */
666 1.1 jruoho
667 1.1 jruoho /* CurrentPath is the path to this scope (where we are in the namespace) */
668 1.1 jruoho
669 1.1 jruoho CurrentPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
670 1.1.1.6 christos Status = AcpiNsHandleToPathname (CurrentNode, &CurrentPath, FALSE);
671 1.1 jruoho if (ACPI_FAILURE (Status))
672 1.1 jruoho {
673 1.1 jruoho AslCoreSubsystemError (Op, Status, "Getting Current NamePath",
674 1.1 jruoho ASL_NO_ABORT);
675 1.1 jruoho return_VOID;
676 1.1 jruoho }
677 1.1 jruoho CurrentPath.Length--; /* Subtract one for null terminator */
678 1.1 jruoho
679 1.1 jruoho /* Debug output only */
680 1.1 jruoho
681 1.1 jruoho Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, AmlNameString,
682 1.1 jruoho NULL, &ExternalNameString);
683 1.1 jruoho if (ACPI_FAILURE (Status))
684 1.1 jruoho {
685 1.1 jruoho AslCoreSubsystemError (Op, Status, "Externalizing NamePath",
686 1.1 jruoho ASL_NO_ABORT);
687 1.1 jruoho return_VOID;
688 1.1 jruoho }
689 1.1 jruoho
690 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
691 1.1.1.3 christos "CURRENT SCOPE: (%2u) %-37s FULL PATH TO NAME: (%2u) %-32s ACTUAL AML:%-32s\n",
692 1.1.1.3 christos (UINT32) CurrentPath.Length, (char *) CurrentPath.Pointer,
693 1.1.1.3 christos (UINT32) TargetPath.Length, (char *) TargetPath.Pointer,
694 1.1.1.3 christos ExternalNameString));
695 1.1 jruoho
696 1.1 jruoho ACPI_FREE (ExternalNameString);
697 1.1 jruoho
698 1.1 jruoho /*
699 1.1 jruoho * Attempt an optmization depending on the type of namepath
700 1.1 jruoho */
701 1.1 jruoho if (Flags & (AML_NAMED | AML_CREATE))
702 1.1 jruoho {
703 1.1 jruoho /*
704 1.1 jruoho * This is a named opcode and the namepath is a name declaration, not
705 1.1 jruoho * a reference.
706 1.1 jruoho */
707 1.1 jruoho Status = OptOptimizeNameDeclaration (Op, WalkState, CurrentNode,
708 1.1 jruoho TargetNode, AmlNameString, &NewPath);
709 1.1 jruoho if (ACPI_FAILURE (Status))
710 1.1 jruoho {
711 1.1 jruoho /*
712 1.1 jruoho * 2) now attempt to
713 1.1 jruoho * optimize the namestring with carats (up-arrow)
714 1.1 jruoho */
715 1.1 jruoho Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
716 1.1 jruoho TargetNode, &CurrentPath, &TargetPath,
717 1.1 jruoho AmlNameStringLength, 1, &NewPath);
718 1.1 jruoho }
719 1.1 jruoho }
720 1.1 jruoho else
721 1.1 jruoho {
722 1.1 jruoho /*
723 1.1 jruoho * This is a reference to an existing named object
724 1.1 jruoho *
725 1.1 jruoho * 1) Check if search-to-root can be utilized using the last
726 1.1 jruoho * NameSeg of the NamePath
727 1.1 jruoho */
728 1.1 jruoho Status = OptSearchToRoot (Op, WalkState, CurrentNode,
729 1.1 jruoho TargetNode, &TargetPath, &NewPath);
730 1.1 jruoho if (ACPI_FAILURE (Status))
731 1.1 jruoho {
732 1.1 jruoho /*
733 1.1 jruoho * 2) Search-to-root could not be used, now attempt to
734 1.1 jruoho * optimize the namestring with carats (up-arrow)
735 1.1 jruoho */
736 1.1 jruoho Status = OptBuildShortestPath (Op, WalkState, CurrentNode,
737 1.1 jruoho TargetNode, &CurrentPath, &TargetPath,
738 1.1 jruoho AmlNameStringLength, 0, &NewPath);
739 1.1 jruoho }
740 1.1 jruoho }
741 1.1 jruoho
742 1.1 jruoho /*
743 1.1 jruoho * Success from above indicates that the NamePath was successfully
744 1.1.1.3 christos * optimized. We need to update the parse op with the new name
745 1.1 jruoho */
746 1.1 jruoho if (ACPI_SUCCESS (Status))
747 1.1 jruoho {
748 1.1.1.6 christos HowMuchShorter = (AmlNameStringLength - strlen (NewPath));
749 1.1 jruoho OptTotal += HowMuchShorter;
750 1.1 jruoho
751 1.1.1.4 christos ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS,
752 1.1.1.3 christos " REDUCED BY %2u (TOTAL SAVED %2u)",
753 1.1 jruoho (UINT32) HowMuchShorter, OptTotal));
754 1.1 jruoho
755 1.1 jruoho if (Flags & AML_NAMED)
756 1.1 jruoho {
757 1.1 jruoho if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
758 1.1 jruoho {
759 1.1 jruoho /*
760 1.1 jruoho * ALIAS is the only oddball opcode, the name declaration
761 1.1 jruoho * (alias name) is the second operand
762 1.1 jruoho */
763 1.1 jruoho Op->Asl.Child->Asl.Next->Asl.Value.String = NewPath;
764 1.1.1.6 christos Op->Asl.Child->Asl.Next->Asl.AmlLength = strlen (NewPath);
765 1.1 jruoho }
766 1.1 jruoho else
767 1.1 jruoho {
768 1.1 jruoho Op->Asl.Child->Asl.Value.String = NewPath;
769 1.1.1.6 christos Op->Asl.Child->Asl.AmlLength = strlen (NewPath);
770 1.1 jruoho }
771 1.1 jruoho }
772 1.1 jruoho else if (Flags & AML_CREATE)
773 1.1 jruoho {
774 1.1 jruoho /* Name must appear as the last parameter */
775 1.1 jruoho
776 1.1 jruoho NextOp = Op->Asl.Child;
777 1.1 jruoho while (!(NextOp->Asl.CompileFlags & NODE_IS_NAME_DECLARATION))
778 1.1 jruoho {
779 1.1 jruoho NextOp = NextOp->Asl.Next;
780 1.1 jruoho }
781 1.1 jruoho /* Update the parse node with the new NamePath */
782 1.1 jruoho
783 1.1 jruoho NextOp->Asl.Value.String = NewPath;
784 1.1.1.6 christos NextOp->Asl.AmlLength = strlen (NewPath);
785 1.1 jruoho }
786 1.1 jruoho else
787 1.1 jruoho {
788 1.1 jruoho /* Update the parse node with the new NamePath */
789 1.1 jruoho
790 1.1 jruoho Op->Asl.Value.String = NewPath;
791 1.1.1.6 christos Op->Asl.AmlLength = strlen (NewPath);
792 1.1 jruoho }
793 1.1 jruoho }
794 1.1 jruoho else
795 1.1 jruoho {
796 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, " ALREADY OPTIMAL"));
797 1.1 jruoho }
798 1.1 jruoho
799 1.1 jruoho /* Cleanup path buffers */
800 1.1 jruoho
801 1.1 jruoho ACPI_FREE (TargetPath.Pointer);
802 1.1 jruoho ACPI_FREE (CurrentPath.Pointer);
803 1.1 jruoho
804 1.1 jruoho ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OPTIMIZATIONS, "\n"));
805 1.1 jruoho return_VOID;
806 1.1 jruoho }
807