No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / compiler / aslcodegen.c
blobaf3f292e3f23bd6319087471782af3f77cecc0dc
2 /******************************************************************************
4 * Module Name: aslcodegen - AML code generation
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #include "aslcompiler.h"
119 #include "aslcompiler.y.h"
120 #include "amlcode.h"
122 #define _COMPONENT ACPI_COMPILER
123 ACPI_MODULE_NAME ("aslcodegen")
125 /* Local prototypes */
127 static ACPI_STATUS
128 CgAmlWriteWalk (
129 ACPI_PARSE_OBJECT *Op,
130 UINT32 Level,
131 void *Context);
133 static void
134 CgLocalWriteAmlData (
135 ACPI_PARSE_OBJECT *Op,
136 void *Buffer,
137 UINT32 Length);
139 static void
140 CgWriteAmlOpcode (
141 ACPI_PARSE_OBJECT *Op);
143 static void
144 CgWriteTableHeader (
145 ACPI_PARSE_OBJECT *Op);
147 static void
148 CgCloseTable (
149 void);
151 static void
152 CgWriteNode (
153 ACPI_PARSE_OBJECT *Op);
156 /*******************************************************************************
158 * FUNCTION: CgGenerateAmlOutput
160 * PARAMETERS: None.
162 * RETURN: None
164 * DESCRIPTION: Generate AML code. Currently generates the listing file
165 * simultaneously.
167 ******************************************************************************/
169 void
170 CgGenerateAmlOutput (
171 void)
174 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
176 /* Generate the AML output file */
178 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
179 Gbl_SourceLine = 0;
180 Gbl_NextError = Gbl_ErrorLog;
182 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
183 CgAmlWriteWalk, NULL, NULL);
184 CgCloseTable ();
188 /*******************************************************************************
190 * FUNCTION: CgAmlWriteWalk
192 * PARAMETERS: ASL_WALK_CALLBACK
194 * RETURN: Status
196 * DESCRIPTION: Parse tree walk to generate the AML code.
198 ******************************************************************************/
200 static ACPI_STATUS
201 CgAmlWriteWalk (
202 ACPI_PARSE_OBJECT *Op,
203 UINT32 Level,
204 void *Context)
208 * Print header at level 0. Alignment assumes 32-bit pointers
210 if (!Level)
212 DbgPrint (ASL_TREE_OUTPUT,
213 "Final parse tree used for AML output:\n");
214 DbgPrint (ASL_TREE_OUTPUT,
215 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr Child Parent Flags AcTyp Final Col L\n",
216 76, " ");
219 /* Debug output */
221 DbgPrint (ASL_TREE_OUTPUT,
222 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
223 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
225 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
226 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
227 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
229 DbgPrint (ASL_TREE_OUTPUT,
230 "%10.32s ", Op->Asl.ExternalName);
232 else
234 DbgPrint (ASL_TREE_OUTPUT, " ");
237 DbgPrint (ASL_TREE_OUTPUT,
238 "%08X %04X %04X %01X %04X %04X %04X %04X %08X %08X %08X %08X %08X %04X %02d %02d\n",
239 /* 1 */ (UINT32) Op->Asl.Value.Integer,
240 /* 2 */ Op->Asl.ParseOpcode,
241 /* 3 */ Op->Asl.AmlOpcode,
242 /* 4 */ Op->Asl.AmlOpcodeLength,
243 /* 5 */ Op->Asl.AmlPkgLenBytes,
244 /* 6 */ Op->Asl.AmlLength,
245 /* 7 */ Op->Asl.AmlSubtreeLength,
246 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
247 /* 9 */ Op,
248 /* 10 */ Op->Asl.Child,
249 /* 11 */ Op->Asl.Parent,
250 /* 12 */ Op->Asl.CompileFlags,
251 /* 13 */ Op->Asl.AcpiBtype,
252 /* 14 */ Op->Asl.FinalAmlLength,
253 /* 15 */ Op->Asl.Column,
254 /* 16 */ Op->Asl.LineNumber);
256 /* Generate the AML for this node */
258 CgWriteNode (Op);
259 return (AE_OK);
263 /*******************************************************************************
265 * FUNCTION: CgLocalWriteAmlData
267 * PARAMETERS: Op - Current parse op
268 * Buffer - Buffer to write
269 * Length - Size of data in buffer
271 * RETURN: None
273 * DESCRIPTION: Write a buffer of AML data to the AML output file.
275 ******************************************************************************/
277 static void
278 CgLocalWriteAmlData (
279 ACPI_PARSE_OBJECT *Op,
280 void *Buffer,
281 UINT32 Length)
284 /* Write the raw data to the AML file */
286 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
288 /* Update the final AML length for this node (used for listings) */
290 if (Op)
292 Op->Asl.FinalAmlLength += Length;
297 /*******************************************************************************
299 * FUNCTION: CgWriteAmlOpcode
301 * PARAMETERS: Op - Parse node with an AML opcode
303 * RETURN: None.
305 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
307 ******************************************************************************/
309 static void
310 CgWriteAmlOpcode (
311 ACPI_PARSE_OBJECT *Op)
313 UINT8 PkgLenFirstByte;
314 UINT32 i;
315 union {
316 UINT16 Opcode;
317 UINT8 OpcodeBytes[2];
318 } Aml;
319 union {
320 UINT32 Len;
321 UINT8 LenBytes[4];
322 } PkgLen;
325 /* We expect some DEFAULT_ARGs, just ignore them */
327 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
329 return;
332 switch (Op->Asl.AmlOpcode)
334 case AML_UNASSIGNED_OPCODE:
336 /* These opcodes should not get here */
338 printf ("Found a node with an unassigned AML opcode\n");
339 fprintf (stderr, "Found a node with an unassigned AML opcode\n");
340 return;
342 case AML_INT_RESERVEDFIELD_OP:
344 /* Special opcodes for within a field definition */
346 Aml.Opcode = 0x00;
347 break;
349 case AML_INT_ACCESSFIELD_OP:
351 Aml.Opcode = 0x01;
352 break;
354 default:
355 Aml.Opcode = Op->Asl.AmlOpcode;
356 break;
360 switch (Aml.Opcode)
362 case AML_PACKAGE_LENGTH:
364 /* Value is the length to be encoded (Used in field definitions) */
366 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
367 break;
369 default:
371 /* Check for two-byte opcode */
373 if (Aml.Opcode > 0x00FF)
375 /* Write the high byte first */
377 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
380 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
382 /* Subtreelength doesn't include length of package length bytes */
384 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
385 break;
388 /* Does this opcode have an associated "PackageLength" field? */
390 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
392 if (Op->Asl.AmlPkgLenBytes == 1)
394 /* Simplest case -- no bytes to follow, just write the count */
396 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
398 else if (Op->Asl.AmlPkgLenBytes != 0)
401 * Encode the "bytes to follow" in the first byte, top two bits.
402 * The low-order nybble of the length is in the bottom 4 bits
404 PkgLenFirstByte = (UINT8)
405 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
406 (PkgLen.LenBytes[0] & 0x0F));
408 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
411 * Shift the length over by the 4 bits we just stuffed
412 * in the first byte
414 PkgLen.Len >>= 4;
416 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
418 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
420 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
425 switch (Aml.Opcode)
427 case AML_BYTE_OP:
429 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
430 break;
432 case AML_WORD_OP:
434 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
435 break;
437 case AML_DWORD_OP:
439 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
440 break;
442 case AML_QWORD_OP:
444 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
445 break;
447 case AML_STRING_OP:
449 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
450 break;
452 default:
453 /* All data opcodes must appear above */
454 break;
459 /*******************************************************************************
461 * FUNCTION: CgWriteTableHeader
463 * PARAMETERS: Op - The DEFINITIONBLOCK node
465 * RETURN: None
467 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
469 ******************************************************************************/
471 static void
472 CgWriteTableHeader (
473 ACPI_PARSE_OBJECT *Op)
475 ACPI_PARSE_OBJECT *Child;
478 /* AML filename */
480 Child = Op->Asl.Child;
482 /* Signature */
484 Child = Child->Asl.Next;
485 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
487 /* Revision */
489 Child = Child->Asl.Next;
490 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
492 /* Command-line Revision override */
494 if (Gbl_RevisionOverride)
496 TableHeader.Revision = Gbl_RevisionOverride;
499 /* OEMID */
501 Child = Child->Asl.Next;
502 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
504 /* OEM TableID */
506 Child = Child->Asl.Next;
507 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
509 /* OEM Revision */
511 Child = Child->Asl.Next;
512 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
514 /* Compiler ID */
516 strncpy (TableHeader.AslCompilerId, CompilerCreatorId, 4);
518 /* Compiler version */
520 TableHeader.AslCompilerRevision = CompilerCreatorRevision;
522 /* Table length. Checksum zero for now, will rewrite later */
524 TableHeader.Length = Gbl_TableLength;
525 TableHeader.Checksum = 0;
527 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
531 /*******************************************************************************
533 * FUNCTION: CgCloseTable
535 * PARAMETERS: None.
537 * RETURN: None.
539 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
540 * re-writing the header.
542 ******************************************************************************/
544 static void
545 CgCloseTable (
546 void)
548 signed char Sum;
549 UINT8 FileByte;
552 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
553 Sum = 0;
555 /* Calculate the checksum over the entire file */
557 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
559 Sum = (signed char) (Sum + FileByte);
562 /* Re-write the table header with the checksum */
564 TableHeader.Checksum = (UINT8) (0 - Sum);
566 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
567 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
571 /*******************************************************************************
573 * FUNCTION: CgWriteNode
575 * PARAMETERS: Op - Parse node to write.
577 * RETURN: None.
579 * DESCRIPTION: Write the AML that corresponds to a parse node.
581 ******************************************************************************/
583 static void
584 CgWriteNode (
585 ACPI_PARSE_OBJECT *Op)
587 ASL_RESOURCE_NODE *Rnode;
590 /* Always check for DEFAULT_ARG and other "Noop" nodes */
591 /* TBD: this may not be the best place for this check */
593 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
594 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
595 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
596 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
598 return;
601 Op->Asl.FinalAmlLength = 0;
603 switch (Op->Asl.AmlOpcode)
605 case AML_RAW_DATA_BYTE:
606 case AML_RAW_DATA_WORD:
607 case AML_RAW_DATA_DWORD:
608 case AML_RAW_DATA_QWORD:
610 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
611 return;
614 case AML_RAW_DATA_BUFFER:
616 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
617 return;
620 case AML_RAW_DATA_CHAIN:
622 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
623 while (Rnode)
625 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
626 Rnode = Rnode->Next;
628 return;
630 default:
631 /* Internal data opcodes must all appear above */
632 break;
635 switch (Op->Asl.ParseOpcode)
637 case PARSEOP_DEFAULT_ARG:
639 break;
641 case PARSEOP_DEFINITIONBLOCK:
643 CgWriteTableHeader (Op);
644 break;
646 case PARSEOP_NAMESEG:
647 case PARSEOP_NAMESTRING:
648 case PARSEOP_METHODCALL:
650 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
651 break;
653 default:
655 CgWriteAmlOpcode (Op);
656 break;