Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / aslcodegen.c
blob1230f59faf70cd71909cf4d890761064c106d0bb
1 /******************************************************************************
3 * Module Name: aslcodegen - AML code generation
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include "aslcompiler.h"
46 #include "aslcompiler.y.h"
47 #include "amlcode.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("aslcodegen")
52 /* Local prototypes */
54 static ACPI_STATUS
55 CgAmlWriteWalk (
56 ACPI_PARSE_OBJECT *Op,
57 UINT32 Level,
58 void *Context);
60 static void
61 CgLocalWriteAmlData (
62 ACPI_PARSE_OBJECT *Op,
63 void *Buffer,
64 UINT32 Length);
66 static void
67 CgWriteAmlOpcode (
68 ACPI_PARSE_OBJECT *Op);
70 static void
71 CgWriteTableHeader (
72 ACPI_PARSE_OBJECT *Op);
74 static void
75 CgCloseTable (
76 void);
78 static void
79 CgWriteNode (
80 ACPI_PARSE_OBJECT *Op);
83 /*******************************************************************************
85 * FUNCTION: CgGenerateAmlOutput
87 * PARAMETERS: None.
89 * RETURN: None
91 * DESCRIPTION: Generate AML code. Currently generates the listing file
92 * simultaneously.
94 ******************************************************************************/
96 void
97 CgGenerateAmlOutput (
98 void)
101 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
103 /* Generate the AML output file */
105 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
106 Gbl_SourceLine = 0;
107 Gbl_NextError = Gbl_ErrorLog;
109 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
110 CgAmlWriteWalk, NULL, NULL);
111 CgCloseTable ();
115 /*******************************************************************************
117 * FUNCTION: CgAmlWriteWalk
119 * PARAMETERS: ASL_WALK_CALLBACK
121 * RETURN: Status
123 * DESCRIPTION: Parse tree walk to generate the AML code.
125 ******************************************************************************/
127 static ACPI_STATUS
128 CgAmlWriteWalk (
129 ACPI_PARSE_OBJECT *Op,
130 UINT32 Level,
131 void *Context)
135 * Print header at level 0. Alignment assumes 32-bit pointers
137 if (!Level)
139 DbgPrint (ASL_TREE_OUTPUT,
140 "Final parse tree used for AML output:\n");
141 DbgPrint (ASL_TREE_OUTPUT,
142 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr Child Parent Flags AcTyp Final Col L\n",
143 76, " ");
146 /* Debug output */
148 DbgPrint (ASL_TREE_OUTPUT,
149 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
150 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
152 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
153 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
154 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
156 DbgPrint (ASL_TREE_OUTPUT,
157 "%10.32s ", Op->Asl.ExternalName);
159 else
161 DbgPrint (ASL_TREE_OUTPUT, " ");
164 DbgPrint (ASL_TREE_OUTPUT,
165 "%08X %04X %04X %01X %04X %04X %04X %04X %08X %08X %08X %08X %08X %04X %02d %02d\n",
166 /* 1 */ (UINT32) Op->Asl.Value.Integer,
167 /* 2 */ Op->Asl.ParseOpcode,
168 /* 3 */ Op->Asl.AmlOpcode,
169 /* 4 */ Op->Asl.AmlOpcodeLength,
170 /* 5 */ Op->Asl.AmlPkgLenBytes,
171 /* 6 */ Op->Asl.AmlLength,
172 /* 7 */ Op->Asl.AmlSubtreeLength,
173 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
174 /* 9 */ Op,
175 /* 10 */ Op->Asl.Child,
176 /* 11 */ Op->Asl.Parent,
177 /* 12 */ Op->Asl.CompileFlags,
178 /* 13 */ Op->Asl.AcpiBtype,
179 /* 14 */ Op->Asl.FinalAmlLength,
180 /* 15 */ Op->Asl.Column,
181 /* 16 */ Op->Asl.LineNumber);
183 /* Generate the AML for this node */
185 CgWriteNode (Op);
186 return (AE_OK);
190 /*******************************************************************************
192 * FUNCTION: CgLocalWriteAmlData
194 * PARAMETERS: Op - Current parse op
195 * Buffer - Buffer to write
196 * Length - Size of data in buffer
198 * RETURN: None
200 * DESCRIPTION: Write a buffer of AML data to the AML output file.
202 ******************************************************************************/
204 static void
205 CgLocalWriteAmlData (
206 ACPI_PARSE_OBJECT *Op,
207 void *Buffer,
208 UINT32 Length)
211 /* Write the raw data to the AML file */
213 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
215 /* Update the final AML length for this node (used for listings) */
217 if (Op)
219 Op->Asl.FinalAmlLength += Length;
224 /*******************************************************************************
226 * FUNCTION: CgWriteAmlOpcode
228 * PARAMETERS: Op - Parse node with an AML opcode
230 * RETURN: None.
232 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
234 ******************************************************************************/
236 static void
237 CgWriteAmlOpcode (
238 ACPI_PARSE_OBJECT *Op)
240 UINT8 PkgLenFirstByte;
241 UINT32 i;
242 union {
243 UINT16 Opcode;
244 UINT8 OpcodeBytes[2];
245 } Aml;
246 union {
247 UINT32 Len;
248 UINT8 LenBytes[4];
249 } PkgLen;
252 /* We expect some DEFAULT_ARGs, just ignore them */
254 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
256 return;
259 switch (Op->Asl.AmlOpcode)
261 case AML_UNASSIGNED_OPCODE:
263 /* These opcodes should not get here */
265 printf ("Found a node with an unassigned AML opcode\n");
266 FlPrintFile (ASL_FILE_STDERR, "Found a node with an unassigned AML opcode\n");
267 return;
269 case AML_INT_RESERVEDFIELD_OP:
271 /* Special opcodes for within a field definition */
273 Aml.Opcode = AML_FIELD_OFFSET_OP;
274 break;
276 case AML_INT_ACCESSFIELD_OP:
278 Aml.Opcode = AML_FIELD_ACCESS_OP;
279 break;
281 case AML_INT_CONNECTION_OP:
283 Aml.Opcode = AML_FIELD_CONNECTION_OP;
284 break;
286 default:
288 Aml.Opcode = Op->Asl.AmlOpcode;
289 break;
293 switch (Aml.Opcode)
295 case AML_PACKAGE_LENGTH:
297 /* Value is the length to be encoded (Used in field definitions) */
299 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
300 break;
302 default:
304 /* Check for two-byte opcode */
306 if (Aml.Opcode > 0x00FF)
308 /* Write the high byte first */
310 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
313 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
315 /* Subtreelength doesn't include length of package length bytes */
317 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
318 break;
321 /* Does this opcode have an associated "PackageLength" field? */
323 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
325 if (Op->Asl.AmlPkgLenBytes == 1)
327 /* Simplest case -- no bytes to follow, just write the count */
329 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
331 else if (Op->Asl.AmlPkgLenBytes != 0)
334 * Encode the "bytes to follow" in the first byte, top two bits.
335 * The low-order nybble of the length is in the bottom 4 bits
337 PkgLenFirstByte = (UINT8)
338 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
339 (PkgLen.LenBytes[0] & 0x0F));
341 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
344 * Shift the length over by the 4 bits we just stuffed
345 * in the first byte
347 PkgLen.Len >>= 4;
349 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
351 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
353 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
358 switch (Aml.Opcode)
360 case AML_BYTE_OP:
362 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
363 break;
365 case AML_WORD_OP:
367 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
368 break;
370 case AML_DWORD_OP:
372 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
373 break;
375 case AML_QWORD_OP:
377 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
378 break;
380 case AML_STRING_OP:
382 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
383 break;
385 default:
387 /* All data opcodes must appear above */
389 break;
394 /*******************************************************************************
396 * FUNCTION: CgWriteTableHeader
398 * PARAMETERS: Op - The DEFINITIONBLOCK node
400 * RETURN: None
402 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
404 ******************************************************************************/
406 static void
407 CgWriteTableHeader (
408 ACPI_PARSE_OBJECT *Op)
410 ACPI_PARSE_OBJECT *Child;
413 /* AML filename */
415 Child = Op->Asl.Child;
417 /* Signature */
419 Child = Child->Asl.Next;
420 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
422 /* Revision */
424 Child = Child->Asl.Next;
425 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
427 /* Command-line Revision override */
429 if (Gbl_RevisionOverride)
431 TableHeader.Revision = Gbl_RevisionOverride;
434 /* OEMID */
436 Child = Child->Asl.Next;
437 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
439 /* OEM TableID */
441 Child = Child->Asl.Next;
442 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
444 /* OEM Revision */
446 Child = Child->Asl.Next;
447 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
449 /* Compiler ID */
451 ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
453 /* Compiler version */
455 TableHeader.AslCompilerRevision = ASL_REVISION;
457 /* Table length. Checksum zero for now, will rewrite later */
459 TableHeader.Length = Gbl_TableLength;
460 TableHeader.Checksum = 0;
462 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
466 /*******************************************************************************
468 * FUNCTION: CgCloseTable
470 * PARAMETERS: None.
472 * RETURN: None.
474 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
475 * re-writing the header.
477 ******************************************************************************/
479 static void
480 CgCloseTable (
481 void)
483 signed char Sum;
484 UINT8 FileByte;
487 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
488 Sum = 0;
490 /* Calculate the checksum over the entire file */
492 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
494 Sum = (signed char) (Sum + FileByte);
497 /* Re-write the table header with the checksum */
499 TableHeader.Checksum = (UINT8) (0 - Sum);
501 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
502 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
506 /*******************************************************************************
508 * FUNCTION: CgWriteNode
510 * PARAMETERS: Op - Parse node to write.
512 * RETURN: None.
514 * DESCRIPTION: Write the AML that corresponds to a parse node.
516 ******************************************************************************/
518 static void
519 CgWriteNode (
520 ACPI_PARSE_OBJECT *Op)
522 ASL_RESOURCE_NODE *Rnode;
525 /* Always check for DEFAULT_ARG and other "Noop" nodes */
526 /* TBD: this may not be the best place for this check */
528 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
529 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
530 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
531 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
533 return;
536 Op->Asl.FinalAmlLength = 0;
538 switch (Op->Asl.AmlOpcode)
540 case AML_RAW_DATA_BYTE:
541 case AML_RAW_DATA_WORD:
542 case AML_RAW_DATA_DWORD:
543 case AML_RAW_DATA_QWORD:
545 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
546 return;
549 case AML_RAW_DATA_BUFFER:
551 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
552 return;
555 case AML_RAW_DATA_CHAIN:
557 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
558 while (Rnode)
560 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
561 Rnode = Rnode->Next;
563 return;
565 default:
567 /* Internal data opcodes must all appear above */
569 break;
572 switch (Op->Asl.ParseOpcode)
574 case PARSEOP_DEFAULT_ARG:
576 break;
578 case PARSEOP_DEFINITIONBLOCK:
580 CgWriteTableHeader (Op);
581 break;
583 case PARSEOP_NAMESEG:
584 case PARSEOP_NAMESTRING:
585 case PARSEOP_METHODCALL:
587 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
588 break;
590 default:
592 CgWriteAmlOpcode (Op);
593 break;