2 /******************************************************************************
4 * Module Name: asllength - Tree walk to determine package and opcode lengths
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
48 #include <contrib/dev/acpica/include/amlcode.h>
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("asllength")
54 /* Local prototypes */
57 CgGetPackageLenByteCount (
58 ACPI_PARSE_OBJECT *Op,
59 UINT32 PackageLength);
62 CgGenerateAmlOpcodeLength (
63 ACPI_PARSE_OBJECT *Op);
66 #ifdef ACPI_OBSOLETE_FUNCTIONS
68 LnAdjustLengthToRoot (
69 ACPI_PARSE_OBJECT *Op,
74 /*******************************************************************************
76 * FUNCTION: LnInitLengthsWalk
78 * PARAMETERS: ASL_WALK_CALLBACK
82 * DESCRIPTION: Walk callback to initialize (and re-initialize) the node
83 * subtree length(s) to zero. The Subtree lengths are bubbled
84 * up to the root node in order to get a total AML length.
86 ******************************************************************************/
90 ACPI_PARSE_OBJECT *Op,
95 Op->Asl.AmlSubtreeLength = 0;
100 /*******************************************************************************
102 * FUNCTION: LnPackageLengthWalk
104 * PARAMETERS: ASL_WALK_CALLBACK
108 * DESCRIPTION: Walk callback to calculate the total AML length.
109 * 1) Calculate the AML lengths (opcode, package length, etc.) for
111 * 2) Bubbble up all of these lengths to the parent node by summing
112 * them all into the parent subtree length.
114 * Note: The SubtreeLength represents the total AML length of all child nodes
115 * in all subtrees under a given node. Therefore, once this walk is
116 * complete, the Root Node subtree length is the AML length of the entire
117 * tree (and thus, the entire ACPI table)
119 ******************************************************************************/
122 LnPackageLengthWalk (
123 ACPI_PARSE_OBJECT *Op,
128 /* Generate the AML lengths for this node */
130 CgGenerateAmlLengths (Op);
132 /* Bubble up all lengths (this node and all below it) to the parent */
134 if ((Op->Asl.Parent) &&
135 (Op->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG))
137 Op->Asl.Parent->Asl.AmlSubtreeLength += (Op->Asl.AmlLength +
138 Op->Asl.AmlOpcodeLength +
139 Op->Asl.AmlPkgLenBytes +
140 Op->Asl.AmlSubtreeLength);
146 /*******************************************************************************
148 * FUNCTION: CgGetPackageLenByteCount
150 * PARAMETERS: Op - Parse node
151 * PackageLength - Length to be encoded
153 * RETURN: Required length of the package length encoding
155 * DESCRIPTION: Calculate the number of bytes required to encode the given
158 ******************************************************************************/
161 CgGetPackageLenByteCount (
162 ACPI_PARSE_OBJECT *Op,
163 UINT32 PackageLength)
167 * Determine the number of bytes required to encode the package length
168 * Note: the package length includes the number of bytes used to encode
169 * the package length, so we must account for this also.
171 if (PackageLength <= (0x0000003F - 1))
175 else if (PackageLength <= (0x00000FFF - 2))
179 else if (PackageLength <= (0x000FFFFF - 3))
183 else if (PackageLength <= (0x0FFFFFFF - 4))
189 /* Fatal error - the package length is too large to encode */
191 AslError (ASL_ERROR, ASL_MSG_ENCODING_LENGTH, Op, NULL);
198 /*******************************************************************************
200 * FUNCTION: CgGenerateAmlOpcodeLength
202 * PARAMETERS: Op - Parse node whose AML opcode lengths will be
207 * DESCRIPTION: Calculate the AmlOpcodeLength, AmlPkgLenBytes, and AmlLength
208 * fields for this node.
210 ******************************************************************************/
213 CgGenerateAmlOpcodeLength (
214 ACPI_PARSE_OBJECT *Op)
217 /* Check for two-byte opcode */
219 if (Op->Asl.AmlOpcode > 0x00FF)
221 Op->Asl.AmlOpcodeLength = 2;
225 Op->Asl.AmlOpcodeLength = 1;
228 /* Does this opcode have an associated "PackageLength" field? */
230 Op->Asl.AmlPkgLenBytes = 0;
231 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
233 Op->Asl.AmlPkgLenBytes = CgGetPackageLenByteCount (
234 Op, Op->Asl.AmlSubtreeLength);
237 /* Data opcode lengths are easy */
239 switch (Op->Asl.AmlOpcode)
243 Op->Asl.AmlLength = 1;
248 Op->Asl.AmlLength = 2;
253 Op->Asl.AmlLength = 4;
258 Op->Asl.AmlLength = 8;
262 /* All data opcodes must be above */
268 /*******************************************************************************
270 * FUNCTION: CgGenerateAmlLengths
272 * PARAMETERS: Op - Parse node
276 * DESCRIPTION: Generate internal length fields based on the AML opcode or
279 ******************************************************************************/
282 CgGenerateAmlLengths (
283 ACPI_PARSE_OBJECT *Op)
289 switch (Op->Asl.AmlOpcode)
291 case AML_RAW_DATA_BYTE:
293 Op->Asl.AmlOpcodeLength = 0;
294 Op->Asl.AmlLength = 1;
297 case AML_RAW_DATA_WORD:
299 Op->Asl.AmlOpcodeLength = 0;
300 Op->Asl.AmlLength = 2;
303 case AML_RAW_DATA_DWORD:
305 Op->Asl.AmlOpcodeLength = 0;
306 Op->Asl.AmlLength = 4;
309 case AML_RAW_DATA_QWORD:
311 Op->Asl.AmlOpcodeLength = 0;
312 Op->Asl.AmlLength = 8;
315 case AML_RAW_DATA_BUFFER:
317 /* Aml length is/was set by creator */
319 Op->Asl.AmlOpcodeLength = 0;
322 case AML_RAW_DATA_CHAIN:
324 /* Aml length is/was set by creator */
326 Op->Asl.AmlOpcodeLength = 0;
333 switch (Op->Asl.ParseOpcode)
335 case PARSEOP_DEFINITIONBLOCK:
337 Gbl_TableLength = sizeof (ACPI_TABLE_HEADER) +
338 Op->Asl.AmlSubtreeLength;
341 case PARSEOP_NAMESEG:
343 Op->Asl.AmlOpcodeLength = 0;
344 Op->Asl.AmlLength = 4;
345 Op->Asl.ExternalName = Op->Asl.Value.String;
348 case PARSEOP_NAMESTRING:
349 case PARSEOP_METHODCALL:
351 if (Op->Asl.CompileFlags & NODE_NAME_INTERNALIZED)
356 Op->Asl.AmlOpcodeLength = 0;
357 Status = UtInternalizeName (Op->Asl.Value.String, &Buffer);
358 if (ACPI_FAILURE (Status))
360 DbgPrint (ASL_DEBUG_OUTPUT,
361 "Failure from internalize name %X\n", Status);
365 Op->Asl.ExternalName = Op->Asl.Value.String;
366 Op->Asl.Value.String = Buffer;
367 Op->Asl.CompileFlags |= NODE_NAME_INTERNALIZED;
369 Op->Asl.AmlLength = strlen (Buffer);
372 * Check for single backslash reference to root,
373 * make it a null terminated string in the AML
375 if (Op->Asl.AmlLength == 1)
377 Op->Asl.AmlLength = 2;
381 case PARSEOP_STRING_LITERAL:
383 Op->Asl.AmlOpcodeLength = 1;
385 /* Get null terminator */
387 Op->Asl.AmlLength = strlen (Op->Asl.Value.String) + 1;
390 case PARSEOP_PACKAGE_LENGTH:
392 Op->Asl.AmlOpcodeLength = 0;
393 Op->Asl.AmlPkgLenBytes = CgGetPackageLenByteCount (Op,
394 (UINT32) Op->Asl.Value.Integer);
397 case PARSEOP_RAW_DATA:
399 Op->Asl.AmlOpcodeLength = 0;
402 case PARSEOP_DEFAULT_ARG:
403 case PARSEOP_EXTERNAL:
404 case PARSEOP_INCLUDE:
405 case PARSEOP_INCLUDE_END:
407 /* Ignore the "default arg" nodes, they are extraneous at this point */
413 CgGenerateAmlOpcodeLength (Op);
419 #ifdef ACPI_OBSOLETE_FUNCTIONS
420 /*******************************************************************************
422 * FUNCTION: LnAdjustLengthToRoot
424 * PARAMETERS: Op - Node whose Length was changed
428 * DESCRIPTION: Change the Subtree length of the given node, and bubble the
429 * change all the way up to the root node. This allows for
430 * last second changes to a package length (for example, if the
431 * package length encoding gets shorter or longer.)
433 ******************************************************************************/
436 LnAdjustLengthToRoot (
437 ACPI_PARSE_OBJECT *SubtreeOp,
440 ACPI_PARSE_OBJECT *Op;
443 /* Adjust all subtree lengths up to the root */
445 Op = SubtreeOp->Asl.Parent;
448 Op->Asl.AmlSubtreeLength -= LengthDelta;
452 /* Adjust the global table length */
454 Gbl_TableLength -= LengthDelta;