xref: /freebsd/sys/contrib/dev/acpica/compiler/aslcodegen.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
1 
2 /******************************************************************************
3  *
4  * Module Name: aslcodegen - AML code generation
5  *              $Revision: 1.62 $
6  *
7  *****************************************************************************/
8 
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2007, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117 
118 
119 #include <contrib/dev/acpica/compiler/aslcompiler.h>
120 #include "aslcompiler.y.h"
121 #include <contrib/dev/acpica/amlcode.h>
122 
123 #define _COMPONENT          ACPI_COMPILER
124         ACPI_MODULE_NAME    ("aslcodegen")
125 
126 /* Local prototypes */
127 
128 static ACPI_STATUS
129 CgAmlWriteWalk (
130     ACPI_PARSE_OBJECT       *Op,
131     UINT32                  Level,
132     void                    *Context);
133 
134 static void
135 CgLocalWriteAmlData (
136     ACPI_PARSE_OBJECT       *Op,
137     void                    *Buffer,
138     UINT32                  Length);
139 
140 static void
141 CgWriteAmlOpcode (
142     ACPI_PARSE_OBJECT       *Op);
143 
144 static void
145 CgWriteTableHeader (
146     ACPI_PARSE_OBJECT       *Op);
147 
148 static void
149 CgCloseTable (
150     void);
151 
152 static void
153 CgWriteNode (
154     ACPI_PARSE_OBJECT       *Op);
155 
156 
157 /*******************************************************************************
158  *
159  * FUNCTION:    CgGenerateAmlOutput
160  *
161  * PARAMETERS:  None.
162  *
163  * RETURN:      None
164  *
165  * DESCRIPTION: Generate AML code.  Currently generates the listing file
166  *              simultaneously.
167  *
168  ******************************************************************************/
169 
170 void
171 CgGenerateAmlOutput (
172     void)
173 {
174 
175     DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
176 
177     /* Generate the AML output file */
178 
179     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
180     Gbl_SourceLine = 0;
181     Gbl_NextError = Gbl_ErrorLog;
182 
183     TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
184         CgAmlWriteWalk, NULL, NULL);
185     CgCloseTable ();
186 }
187 
188 
189 /*******************************************************************************
190  *
191  * FUNCTION:    CgAmlWriteWalk
192  *
193  * PARAMETERS:  ASL_WALK_CALLBACK
194  *
195  * RETURN:      Status
196  *
197  * DESCRIPTION: Parse tree walk to generate the AML code.
198  *
199  ******************************************************************************/
200 
201 static ACPI_STATUS
202 CgAmlWriteWalk (
203     ACPI_PARSE_OBJECT       *Op,
204     UINT32                  Level,
205     void                    *Context)
206 {
207 
208     /*
209      * Print header at level 0. Alignment assumes 32-bit pointers
210      */
211     if (!Level)
212     {
213         DbgPrint (ASL_TREE_OUTPUT,
214             "Final parse tree used for AML output:\n");
215         DbgPrint (ASL_TREE_OUTPUT,
216             "%*s Value    P_Op A_Op OpLen PByts Len  SubLen PSubLen OpPtr    Child    Parent   Flags    AcTyp    Final Col L\n",
217             76, " ");
218     }
219 
220     /* Debug output */
221 
222     DbgPrint (ASL_TREE_OUTPUT,
223         "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
224     UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
225 
226     if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG    ||
227         Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
228         Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
229     {
230         DbgPrint (ASL_TREE_OUTPUT,
231             "%10.32s      ", Op->Asl.ExternalName);
232     }
233     else
234     {
235         DbgPrint (ASL_TREE_OUTPUT, "                ");
236     }
237 
238         DbgPrint (ASL_TREE_OUTPUT,
239         "%08X %04X %04X %01X     %04X  %04X %04X   %04X    %08X %08X %08X %08X %08X %04X  %02d  %02d\n",
240                 /* 1  */ (UINT32) Op->Asl.Value.Integer,
241                 /* 2  */ Op->Asl.ParseOpcode,
242                 /* 3  */ Op->Asl.AmlOpcode,
243                 /* 4  */ Op->Asl.AmlOpcodeLength,
244                 /* 5  */ Op->Asl.AmlPkgLenBytes,
245                 /* 6  */ Op->Asl.AmlLength,
246                 /* 7  */ Op->Asl.AmlSubtreeLength,
247                 /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
248                 /* 9  */ Op,
249                 /* 10 */ Op->Asl.Child,
250                 /* 11 */ Op->Asl.Parent,
251                 /* 12 */ Op->Asl.CompileFlags,
252                 /* 13 */ Op->Asl.AcpiBtype,
253                 /* 14 */ Op->Asl.FinalAmlLength,
254                 /* 15 */ Op->Asl.Column,
255                 /* 16 */ Op->Asl.LineNumber);
256 
257     /* Generate the AML for this node */
258 
259     CgWriteNode (Op);
260     return (AE_OK);
261 }
262 
263 
264 /*******************************************************************************
265  *
266  * FUNCTION:    CgLocalWriteAmlData
267  *
268  * PARAMETERS:  Op              - Current parse op
269  *              Buffer          - Buffer to write
270  *              Length          - Size of data in buffer
271  *
272  * RETURN:      None
273  *
274  * DESCRIPTION: Write a buffer of AML data to the AML output file.
275  *
276  ******************************************************************************/
277 
278 static void
279 CgLocalWriteAmlData (
280     ACPI_PARSE_OBJECT       *Op,
281     void                    *Buffer,
282     UINT32                  Length)
283 {
284 
285     /* Write the raw data to the AML file */
286 
287     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
288 
289     /* Update the final AML length for this node (used for listings) */
290 
291     if (Op)
292     {
293         Op->Asl.FinalAmlLength += Length;
294     }
295 }
296 
297 
298 /*******************************************************************************
299  *
300  * FUNCTION:    CgWriteAmlOpcode
301  *
302  * PARAMETERS:  Op            - Parse node with an AML opcode
303  *
304  * RETURN:      None.
305  *
306  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
307  *
308  ******************************************************************************/
309 
310 static void
311 CgWriteAmlOpcode (
312     ACPI_PARSE_OBJECT       *Op)
313 {
314     UINT8                   PkgLenFirstByte;
315     UINT32                  i;
316     union {
317         UINT16                  Opcode;
318         UINT8                   OpcodeBytes[2];
319     } Aml;
320     union {
321         UINT32                  Len;
322         UINT8                   LenBytes[4];
323     } PkgLen;
324 
325 
326     /* We expect some DEFAULT_ARGs, just ignore them */
327 
328     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
329     {
330         return;
331     }
332 
333     switch (Op->Asl.AmlOpcode)
334     {
335     case AML_UNASSIGNED_OPCODE:
336 
337         /* These opcodes should not get here */
338 
339         printf ("Found a node with an unassigned AML opcode\n");
340         fprintf (stderr, "Found a node with an unassigned AML opcode\n");
341         return;
342 
343     case AML_INT_RESERVEDFIELD_OP:
344 
345         /* Special opcodes for within a field definition */
346 
347         Aml.Opcode = 0x00;
348         break;
349 
350     case AML_INT_ACCESSFIELD_OP:
351 
352         Aml.Opcode = 0x01;
353         break;
354 
355     default:
356         Aml.Opcode = Op->Asl.AmlOpcode;
357         break;
358     }
359 
360 
361     switch (Aml.Opcode)
362     {
363     case AML_PACKAGE_LENGTH:
364 
365         /* Value is the length to be encoded (Used in field definitions) */
366 
367         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
368         break;
369 
370     default:
371 
372         /* Check for two-byte opcode */
373 
374         if (Aml.Opcode > 0x00FF)
375         {
376             /* Write the high byte first */
377 
378             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
379         }
380 
381         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
382 
383         /* Subtreelength doesn't include length of package length bytes */
384 
385         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
386         break;
387     }
388 
389     /* Does this opcode have an associated "PackageLength" field? */
390 
391     if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
392     {
393         if (Op->Asl.AmlPkgLenBytes == 1)
394         {
395             /* Simplest case -- no bytes to follow, just write the count */
396 
397             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
398         }
399         else if (Op->Asl.AmlPkgLenBytes != 0)
400         {
401             /*
402              * Encode the "bytes to follow" in the first byte, top two bits.
403              * The low-order nybble of the length is in the bottom 4 bits
404              */
405             PkgLenFirstByte = (UINT8)
406                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
407                 (PkgLen.LenBytes[0] & 0x0F));
408 
409             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
410 
411             /*
412              * Shift the length over by the 4 bits we just stuffed
413              * in the first byte
414              */
415             PkgLen.Len >>= 4;
416 
417             /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
418 
419             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
420             {
421                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
422             }
423         }
424     }
425 
426     switch (Aml.Opcode)
427     {
428     case AML_BYTE_OP:
429 
430         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
431         break;
432 
433     case AML_WORD_OP:
434 
435         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
436        break;
437 
438     case AML_DWORD_OP:
439 
440         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
441         break;
442 
443     case AML_QWORD_OP:
444 
445         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
446         break;
447 
448     case AML_STRING_OP:
449 
450         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
451         break;
452 
453     default:
454         /* All data opcodes must appear above */
455         break;
456     }
457 }
458 
459 
460 /*******************************************************************************
461  *
462  * FUNCTION:    CgWriteTableHeader
463  *
464  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
465  *
466  * RETURN:      None
467  *
468  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
469  *
470  ******************************************************************************/
471 
472 static void
473 CgWriteTableHeader (
474     ACPI_PARSE_OBJECT       *Op)
475 {
476     ACPI_PARSE_OBJECT       *Child;
477 
478 
479     /* AML filename */
480 
481     Child = Op->Asl.Child;
482 
483     /* Signature */
484 
485     Child = Child->Asl.Next;
486     strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
487 
488     /* Revision */
489 
490     Child = Child->Asl.Next;
491     TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
492 
493     /* Command-line Revision override */
494 
495     if (Gbl_RevisionOverride)
496     {
497         TableHeader.Revision = Gbl_RevisionOverride;
498     }
499 
500     /* OEMID */
501 
502     Child = Child->Asl.Next;
503     strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
504 
505     /* OEM TableID */
506 
507     Child = Child->Asl.Next;
508     strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
509 
510     /* OEM Revision */
511 
512     Child = Child->Asl.Next;
513     TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
514 
515     /* Compiler ID */
516 
517     strncpy (TableHeader.AslCompilerId, CompilerCreatorId, 4);
518 
519     /* Compiler version */
520 
521     TableHeader.AslCompilerRevision = CompilerCreatorRevision;
522 
523     /* Table length. Checksum zero for now, will rewrite later */
524 
525     TableHeader.Length   = Gbl_TableLength;
526     TableHeader.Checksum = 0;
527 
528     CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
529 }
530 
531 
532 /*******************************************************************************
533  *
534  * FUNCTION:    CgCloseTable
535  *
536  * PARAMETERS:  None.
537  *
538  * RETURN:      None.
539  *
540  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
541  *              re-writing the header.
542  *
543  ******************************************************************************/
544 
545 static void
546 CgCloseTable (
547     void)
548 {
549     signed char         Sum;
550     UINT8               FileByte;
551 
552 
553     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
554     Sum = 0;
555 
556     /* Calculate the checksum over the entire file */
557 
558     while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
559     {
560         Sum = (signed char) (Sum + FileByte);
561     }
562 
563     /* Re-write the table header with the checksum */
564 
565     TableHeader.Checksum = (UINT8) (0 - Sum);
566 
567     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
568     CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
569 }
570 
571 
572 /*******************************************************************************
573  *
574  * FUNCTION:    CgWriteNode
575  *
576  * PARAMETERS:  Op            - Parse node to write.
577  *
578  * RETURN:      None.
579  *
580  * DESCRIPTION: Write the AML that corresponds to a parse node.
581  *
582  ******************************************************************************/
583 
584 static void
585 CgWriteNode (
586     ACPI_PARSE_OBJECT       *Op)
587 {
588     ASL_RESOURCE_NODE       *Rnode;
589 
590 
591     /* Always check for DEFAULT_ARG and other "Noop" nodes */
592     /* TBD: this may not be the best place for this check */
593 
594     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
595         (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL)     ||
596         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
597         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
598     {
599         return;
600     }
601 
602     Op->Asl.FinalAmlLength = 0;
603 
604     switch (Op->Asl.AmlOpcode)
605     {
606     case AML_RAW_DATA_BYTE:
607     case AML_RAW_DATA_WORD:
608     case AML_RAW_DATA_DWORD:
609     case AML_RAW_DATA_QWORD:
610 
611         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
612         return;
613 
614 
615     case AML_RAW_DATA_BUFFER:
616 
617         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
618         return;
619 
620 
621     case AML_RAW_DATA_CHAIN:
622 
623         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
624         while (Rnode)
625         {
626             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
627             Rnode = Rnode->Next;
628         }
629         return;
630 
631     default:
632         /* Internal data opcodes must all appear above */
633         break;
634     }
635 
636     switch (Op->Asl.ParseOpcode)
637     {
638     case PARSEOP_DEFAULT_ARG:
639 
640         break;
641 
642     case PARSEOP_DEFINITIONBLOCK:
643 
644         CgWriteTableHeader (Op);
645         break;
646 
647     case PARSEOP_NAMESEG:
648     case PARSEOP_NAMESTRING:
649     case PARSEOP_METHODCALL:
650 
651         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
652         break;
653 
654     default:
655 
656         CgWriteAmlOpcode (Op);
657         break;
658     }
659 }
660 
661 
662