aslcodegen.c revision 202771
1
2/******************************************************************************
3 *
4 * Module Name: aslcodegen - AML code generation
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
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.
21 *
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
28 *
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;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
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.
54 *
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.
66 *
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.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
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.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
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.
90 *
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.
99 *
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.
114 *
115 *****************************************************************************/
116
117
118#include <contrib/dev/acpica/compiler/aslcompiler.h>
119#include "aslcompiler.y.h"
120#include <contrib/dev/acpica/include/amlcode.h>
121
122#define _COMPONENT          ACPI_COMPILER
123        ACPI_MODULE_NAME    ("aslcodegen")
124
125/* Local prototypes */
126
127static ACPI_STATUS
128CgAmlWriteWalk (
129    ACPI_PARSE_OBJECT       *Op,
130    UINT32                  Level,
131    void                    *Context);
132
133static void
134CgLocalWriteAmlData (
135    ACPI_PARSE_OBJECT       *Op,
136    void                    *Buffer,
137    UINT32                  Length);
138
139static void
140CgWriteAmlOpcode (
141    ACPI_PARSE_OBJECT       *Op);
142
143static void
144CgWriteTableHeader (
145    ACPI_PARSE_OBJECT       *Op);
146
147static void
148CgCloseTable (
149    void);
150
151static void
152CgWriteNode (
153    ACPI_PARSE_OBJECT       *Op);
154
155
156/*******************************************************************************
157 *
158 * FUNCTION:    CgGenerateAmlOutput
159 *
160 * PARAMETERS:  None.
161 *
162 * RETURN:      None
163 *
164 * DESCRIPTION: Generate AML code.  Currently generates the listing file
165 *              simultaneously.
166 *
167 ******************************************************************************/
168
169void
170CgGenerateAmlOutput (
171    void)
172{
173
174    DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
175
176    /* Generate the AML output file */
177
178    FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
179    Gbl_SourceLine = 0;
180    Gbl_NextError = Gbl_ErrorLog;
181
182    TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
183        CgAmlWriteWalk, NULL, NULL);
184    CgCloseTable ();
185}
186
187
188/*******************************************************************************
189 *
190 * FUNCTION:    CgAmlWriteWalk
191 *
192 * PARAMETERS:  ASL_WALK_CALLBACK
193 *
194 * RETURN:      Status
195 *
196 * DESCRIPTION: Parse tree walk to generate the AML code.
197 *
198 ******************************************************************************/
199
200static ACPI_STATUS
201CgAmlWriteWalk (
202    ACPI_PARSE_OBJECT       *Op,
203    UINT32                  Level,
204    void                    *Context)
205{
206
207    /*
208     * Print header at level 0. Alignment assumes 32-bit pointers
209     */
210    if (!Level)
211    {
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, " ");
217    }
218
219    /* Debug output */
220
221    DbgPrint (ASL_TREE_OUTPUT,
222        "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
223    UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
224
225    if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG    ||
226        Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
227        Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
228    {
229        DbgPrint (ASL_TREE_OUTPUT,
230            "%10.32s      ", Op->Asl.ExternalName);
231    }
232    else
233    {
234        DbgPrint (ASL_TREE_OUTPUT, "                ");
235    }
236
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);
255
256    /* Generate the AML for this node */
257
258    CgWriteNode (Op);
259    return (AE_OK);
260}
261
262
263/*******************************************************************************
264 *
265 * FUNCTION:    CgLocalWriteAmlData
266 *
267 * PARAMETERS:  Op              - Current parse op
268 *              Buffer          - Buffer to write
269 *              Length          - Size of data in buffer
270 *
271 * RETURN:      None
272 *
273 * DESCRIPTION: Write a buffer of AML data to the AML output file.
274 *
275 ******************************************************************************/
276
277static void
278CgLocalWriteAmlData (
279    ACPI_PARSE_OBJECT       *Op,
280    void                    *Buffer,
281    UINT32                  Length)
282{
283
284    /* Write the raw data to the AML file */
285
286    FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
287
288    /* Update the final AML length for this node (used for listings) */
289
290    if (Op)
291    {
292        Op->Asl.FinalAmlLength += Length;
293    }
294}
295
296
297/*******************************************************************************
298 *
299 * FUNCTION:    CgWriteAmlOpcode
300 *
301 * PARAMETERS:  Op            - Parse node with an AML opcode
302 *
303 * RETURN:      None.
304 *
305 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
306 *
307 ******************************************************************************/
308
309static void
310CgWriteAmlOpcode (
311    ACPI_PARSE_OBJECT       *Op)
312{
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;
323
324
325    /* We expect some DEFAULT_ARGs, just ignore them */
326
327    if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
328    {
329        return;
330    }
331
332    switch (Op->Asl.AmlOpcode)
333    {
334    case AML_UNASSIGNED_OPCODE:
335
336        /* These opcodes should not get here */
337
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;
341
342    case AML_INT_RESERVEDFIELD_OP:
343
344        /* Special opcodes for within a field definition */
345
346        Aml.Opcode = 0x00;
347        break;
348
349    case AML_INT_ACCESSFIELD_OP:
350
351        Aml.Opcode = 0x01;
352        break;
353
354    default:
355        Aml.Opcode = Op->Asl.AmlOpcode;
356        break;
357    }
358
359
360    switch (Aml.Opcode)
361    {
362    case AML_PACKAGE_LENGTH:
363
364        /* Value is the length to be encoded (Used in field definitions) */
365
366        PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
367        break;
368
369    default:
370
371        /* Check for two-byte opcode */
372
373        if (Aml.Opcode > 0x00FF)
374        {
375            /* Write the high byte first */
376
377            CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
378        }
379
380        CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
381
382        /* Subtreelength doesn't include length of package length bytes */
383
384        PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
385        break;
386    }
387
388    /* Does this opcode have an associated "PackageLength" field? */
389
390    if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
391    {
392        if (Op->Asl.AmlPkgLenBytes == 1)
393        {
394            /* Simplest case -- no bytes to follow, just write the count */
395
396            CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
397        }
398        else if (Op->Asl.AmlPkgLenBytes != 0)
399        {
400            /*
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
403             */
404            PkgLenFirstByte = (UINT8)
405                (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
406                (PkgLen.LenBytes[0] & 0x0F));
407
408            CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
409
410            /*
411             * Shift the length over by the 4 bits we just stuffed
412             * in the first byte
413             */
414            PkgLen.Len >>= 4;
415
416            /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
417
418            for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
419            {
420                CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
421            }
422        }
423    }
424
425    switch (Aml.Opcode)
426    {
427    case AML_BYTE_OP:
428
429        CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
430        break;
431
432    case AML_WORD_OP:
433
434        CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
435       break;
436
437    case AML_DWORD_OP:
438
439        CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
440        break;
441
442    case AML_QWORD_OP:
443
444        CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
445        break;
446
447    case AML_STRING_OP:
448
449        CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
450        break;
451
452    default:
453        /* All data opcodes must appear above */
454        break;
455    }
456}
457
458
459/*******************************************************************************
460 *
461 * FUNCTION:    CgWriteTableHeader
462 *
463 * PARAMETERS:  Op        - The DEFINITIONBLOCK node
464 *
465 * RETURN:      None
466 *
467 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
468 *
469 ******************************************************************************/
470
471static void
472CgWriteTableHeader (
473    ACPI_PARSE_OBJECT       *Op)
474{
475    ACPI_PARSE_OBJECT       *Child;
476
477
478    /* AML filename */
479
480    Child = Op->Asl.Child;
481
482    /* Signature */
483
484    Child = Child->Asl.Next;
485    strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
486
487    /* Revision */
488
489    Child = Child->Asl.Next;
490    TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
491
492    /* Command-line Revision override */
493
494    if (Gbl_RevisionOverride)
495    {
496        TableHeader.Revision = Gbl_RevisionOverride;
497    }
498
499    /* OEMID */
500
501    Child = Child->Asl.Next;
502    strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
503
504    /* OEM TableID */
505
506    Child = Child->Asl.Next;
507    strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
508
509    /* OEM Revision */
510
511    Child = Child->Asl.Next;
512    TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
513
514    /* Compiler ID */
515
516    strncpy (TableHeader.AslCompilerId, CompilerCreatorId, 4);
517
518    /* Compiler version */
519
520    TableHeader.AslCompilerRevision = CompilerCreatorRevision;
521
522    /* Table length. Checksum zero for now, will rewrite later */
523
524    TableHeader.Length   = Gbl_TableLength;
525    TableHeader.Checksum = 0;
526
527    CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
528}
529
530
531/*******************************************************************************
532 *
533 * FUNCTION:    CgCloseTable
534 *
535 * PARAMETERS:  None.
536 *
537 * RETURN:      None.
538 *
539 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
540 *              re-writing the header.
541 *
542 ******************************************************************************/
543
544static void
545CgCloseTable (
546    void)
547{
548    signed char         Sum;
549    UINT8               FileByte;
550
551
552    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
553    Sum = 0;
554
555    /* Calculate the checksum over the entire file */
556
557    while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
558    {
559        Sum = (signed char) (Sum + FileByte);
560    }
561
562    /* Re-write the table header with the checksum */
563
564    TableHeader.Checksum = (UINT8) (0 - Sum);
565
566    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
567    CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
568}
569
570
571/*******************************************************************************
572 *
573 * FUNCTION:    CgWriteNode
574 *
575 * PARAMETERS:  Op            - Parse node to write.
576 *
577 * RETURN:      None.
578 *
579 * DESCRIPTION: Write the AML that corresponds to a parse node.
580 *
581 ******************************************************************************/
582
583static void
584CgWriteNode (
585    ACPI_PARSE_OBJECT       *Op)
586{
587    ASL_RESOURCE_NODE       *Rnode;
588
589
590    /* Always check for DEFAULT_ARG and other "Noop" nodes */
591    /* TBD: this may not be the best place for this check */
592
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))
597    {
598        return;
599    }
600
601    Op->Asl.FinalAmlLength = 0;
602
603    switch (Op->Asl.AmlOpcode)
604    {
605    case AML_RAW_DATA_BYTE:
606    case AML_RAW_DATA_WORD:
607    case AML_RAW_DATA_DWORD:
608    case AML_RAW_DATA_QWORD:
609
610        CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
611        return;
612
613
614    case AML_RAW_DATA_BUFFER:
615
616        CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
617        return;
618
619
620    case AML_RAW_DATA_CHAIN:
621
622        Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
623        while (Rnode)
624        {
625            CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
626            Rnode = Rnode->Next;
627        }
628        return;
629
630    default:
631        /* Internal data opcodes must all appear above */
632        break;
633    }
634
635    switch (Op->Asl.ParseOpcode)
636    {
637    case PARSEOP_DEFAULT_ARG:
638
639        break;
640
641    case PARSEOP_DEFINITIONBLOCK:
642
643        CgWriteTableHeader (Op);
644        break;
645
646    case PARSEOP_NAMESEG:
647    case PARSEOP_NAMESTRING:
648    case PARSEOP_METHODCALL:
649
650        CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
651        break;
652
653    default:
654
655        CgWriteAmlOpcode (Op);
656        break;
657    }
658}
659
660
661