8edcb668af4abf03db8c63a101a9ab50f5115138
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslcodegen.c
1 /******************************************************************************
2  *
3  * Module Name: aslcodegen - AML code generation
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2019, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151
152 #include "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "amlcode.h"
155 #include "acconvert.h"
156
157 #define _COMPONENT          ACPI_COMPILER
158         ACPI_MODULE_NAME    ("aslcodegen")
159
160 /* Local prototypes */
161
162 static ACPI_STATUS
163 CgAmlWriteWalk (
164     ACPI_PARSE_OBJECT       *Op,
165     UINT32                  Level,
166     void                    *Context);
167
168 static void
169 CgWriteAmlOpcode (
170     ACPI_PARSE_OBJECT       *Op);
171
172 static void
173 CgWriteTableHeader (
174     ACPI_PARSE_OBJECT       *Op);
175
176 static void
177 CgCloseTable (
178     void);
179
180 static void
181 CgWriteNode (
182     ACPI_PARSE_OBJECT       *Op);
183
184
185 /*******************************************************************************
186  *
187  * FUNCTION:    CgGenerateAmlOutput
188  *
189  * PARAMETERS:  None.
190  *
191  * RETURN:      None
192  *
193  * DESCRIPTION: Generate AML code. Currently generates the listing file
194  *              simultaneously.
195  *
196  ******************************************************************************/
197
198 void
199 CgGenerateAmlOutput (
200     void)
201 {
202
203     /* Generate the AML output file */
204
205     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
206     AslGbl_SourceLine = 0;
207     AslGbl_NextError = AslGbl_ErrorLog;
208
209     TrWalkParseTree (AslGbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
210         CgAmlWriteWalk, NULL, NULL);
211
212     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
213     CgCloseTable ();
214 }
215
216
217 /*******************************************************************************
218  *
219  * FUNCTION:    CgAmlWriteWalk
220  *
221  * PARAMETERS:  ASL_WALK_CALLBACK
222  *
223  * RETURN:      Status
224  *
225  * DESCRIPTION: Parse tree walk to generate the AML code.
226  *
227  ******************************************************************************/
228
229 static ACPI_STATUS
230 CgAmlWriteWalk (
231     ACPI_PARSE_OBJECT       *Op,
232     UINT32                  Level,
233     void                    *Context)
234 {
235
236     /* Generate the AML for this node */
237
238     CgWriteNode (Op);
239
240     if (!AslGbl_DebugFlag)
241     {
242         return (AE_OK);
243     }
244
245     /* Print header at level 0. Alignment assumes 32-bit pointers */
246
247     if (!Level)
248     {
249         DbgPrint (ASL_TREE_OUTPUT,
250             "\nFinal parse tree used for AML output:\n");
251         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
252     }
253
254     /* Dump ParseOp name and possible value */
255
256     switch (Op->Asl.ParseOpcode)
257     {
258     case PARSEOP_NAMESEG:
259     case PARSEOP_NAMESTRING:
260     case PARSEOP_METHODCALL:
261     case PARSEOP_STRING_LITERAL:
262
263         UtDumpStringOp (Op, Level);
264         break;
265
266     default:
267
268         UtDumpBasicOp (Op, Level);
269         break;
270     }
271
272     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
273         /* 1  */ (UINT32) Op->Asl.Value.Integer,
274         /* 2  */ Op->Asl.ParseOpcode,
275         /* 3  */ Op->Asl.AmlOpcode,
276         /* 4  */ Op->Asl.AmlOpcodeLength,
277         /* 5  */ Op->Asl.AmlPkgLenBytes,
278         /* 6  */ Op->Asl.AmlLength,
279         /* 7  */ Op->Asl.AmlSubtreeLength,
280         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
281         /* 9  */ Op,
282         /* 10 */ Op->Asl.Parent,
283         /* 11 */ Op->Asl.Child,
284         /* 12 */ Op->Asl.Next,
285         /* 13 */ Op->Asl.CompileFlags,
286         /* 14 */ Op->Asl.AcpiBtype,
287         /* 15 */ Op->Asl.FinalAmlLength,
288         /* 16 */ Op->Asl.Column,
289         /* 17 */ Op->Asl.LineNumber,
290         /* 18 */ Op->Asl.EndLine,
291         /* 19 */ Op->Asl.LogicalLineNumber,
292         /* 20 */ Op->Asl.EndLogicalLine);
293
294     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_TREE_OUTPUT);
295     DbgPrint (ASL_TREE_OUTPUT, "\n");
296     return (AE_OK);
297 }
298
299
300 /*******************************************************************************
301  *
302  * FUNCTION:    CgLocalWriteAmlData
303  *
304  * PARAMETERS:  Op              - Current parse op
305  *              Buffer          - Buffer to write
306  *              Length          - Size of data in buffer
307  *
308  * RETURN:      None
309  *
310  * DESCRIPTION: Write a buffer of AML data to the AML output file.
311  *
312  ******************************************************************************/
313
314 void
315 CgLocalWriteAmlData (
316     ACPI_PARSE_OBJECT       *Op,
317     void                    *Buffer,
318     UINT32                  Length)
319 {
320
321     /* Write the raw data to the AML file */
322
323     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
324
325     /* Update the final AML length for this node (used for listings) */
326
327     if (Op)
328     {
329         Op->Asl.FinalAmlLength += Length;
330     }
331 }
332
333
334 /*******************************************************************************
335  *
336  * FUNCTION:    CgWriteAmlOpcode
337  *
338  * PARAMETERS:  Op            - Parse node with an AML opcode
339  *
340  * RETURN:      None.
341  *
342  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
343  *
344  ******************************************************************************/
345
346 static void
347 CgWriteAmlOpcode (
348     ACPI_PARSE_OBJECT       *Op)
349 {
350     UINT8                   PkgLenFirstByte;
351     UINT32                  i;
352     union {
353         UINT16                  Opcode;
354         UINT8                   OpcodeBytes[2];
355     } Aml;
356     union {
357         UINT32                  Len;
358         UINT8                   LenBytes[4];
359     } PkgLen;
360
361
362     /* We expect some DEFAULT_ARGs, just ignore them */
363
364     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
365     {
366         return;
367     }
368
369     /*
370      * Before printing the bytecode, generate comment byte codes
371      * associated with this node.
372      */
373     if (AcpiGbl_CaptureComments)
374     {
375         CgWriteAmlComment(Op);
376     }
377
378     switch (Op->Asl.AmlOpcode)
379     {
380     case AML_UNASSIGNED_OPCODE:
381
382         /* These opcodes should not get here */
383
384         printf ("Found a node with an unassigned AML opcode\n");
385         FlPrintFile (ASL_FILE_STDERR,
386             "Found a node with an unassigned AML opcode\n");
387         return;
388
389     case AML_INT_RESERVEDFIELD_OP:
390
391         /* Special opcodes for within a field definition */
392
393         Aml.Opcode = AML_FIELD_OFFSET_OP;
394         break;
395
396     case AML_INT_ACCESSFIELD_OP:
397
398         Aml.Opcode = AML_FIELD_ACCESS_OP;
399         break;
400
401     case AML_INT_CONNECTION_OP:
402
403         Aml.Opcode = AML_FIELD_CONNECTION_OP;
404         break;
405
406     default:
407
408         Aml.Opcode = Op->Asl.AmlOpcode;
409         break;
410     }
411
412
413     switch (Aml.Opcode)
414     {
415     case AML_PACKAGE_LENGTH:
416
417         /* Value is the length to be encoded (Used in field definitions) */
418
419         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
420         break;
421
422     default:
423
424         /* Check for two-byte opcode */
425
426         if (Aml.Opcode > 0x00FF)
427         {
428             /* Write the high byte first */
429
430             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
431         }
432
433         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
434
435         /* Subtreelength doesn't include length of package length bytes */
436
437         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
438         break;
439     }
440
441     /* Does this opcode have an associated "PackageLength" field? */
442
443     if (Op->Asl.CompileFlags & OP_AML_PACKAGE)
444     {
445         if (Op->Asl.AmlPkgLenBytes == 1)
446         {
447             /* Simplest case -- no bytes to follow, just write the count */
448
449             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
450         }
451         else if (Op->Asl.AmlPkgLenBytes != 0)
452         {
453             /*
454              * Encode the "bytes to follow" in the first byte, top two bits.
455              * The low-order nybble of the length is in the bottom 4 bits
456              */
457             PkgLenFirstByte = (UINT8)
458                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
459                 (PkgLen.LenBytes[0] & 0x0F));
460
461             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
462
463             /*
464              * Shift the length over by the 4 bits we just stuffed
465              * in the first byte
466              */
467             PkgLen.Len >>= 4;
468
469             /*
470              * Now we can write the remaining bytes -
471              * either 1, 2, or 3 bytes
472              */
473             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
474             {
475                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
476             }
477         }
478     }
479
480     switch (Aml.Opcode)
481     {
482     case AML_BYTE_OP:
483
484         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
485         break;
486
487     case AML_WORD_OP:
488
489         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
490        break;
491
492     case AML_DWORD_OP:
493
494         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
495         break;
496
497     case AML_QWORD_OP:
498
499         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
500         break;
501
502     case AML_STRING_OP:
503
504         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
505         break;
506
507     default:
508
509         /* All data opcodes must appear above */
510
511         break;
512     }
513 }
514
515
516 /*******************************************************************************
517  *
518  * FUNCTION:    CgWriteTableHeader
519  *
520  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
521  *
522  * RETURN:      None
523  *
524  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
525  *
526  * NOTE: Input strings should be validated before this function is invoked.
527  *
528  ******************************************************************************/
529
530 static void
531 CgWriteTableHeader (
532     ACPI_PARSE_OBJECT       *Op)
533 {
534     ACPI_PARSE_OBJECT       *Child;
535     UINT32                  CommentLength;
536     ACPI_COMMENT_NODE       *Current;
537
538
539     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
540
541     /* AML filename */
542
543     Child = Op->Asl.Child;
544
545     /* Signature */
546
547     Child = Child->Asl.Next;
548
549     /*
550      * For ASL-/ASL+ converter: replace the table signature with
551      * "XXXX" and save the original table signature. This results in an AML
552      * file with the signature "XXXX". The converter should remove this AML
553      * file. In the event where this AML file does not get deleted, the
554      * "XXXX" table signature prevents this AML file from running on the AML
555      * interpreter.
556      */
557     if (AcpiGbl_CaptureComments)
558     {
559         ACPI_COPY_NAMESEG (AcpiGbl_TableSig, Child->Asl.Value.String);
560         Child->Asl.Value.String = ACPI_SIG_XXXX;
561     }
562
563     ACPI_COPY_NAMESEG (AslGbl_TableHeader.Signature, Child->Asl.Value.String);
564
565     /* Revision */
566
567     Child = Child->Asl.Next;
568     AslGbl_TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
569
570     /* Command-line Revision override */
571
572     if (AslGbl_RevisionOverride)
573     {
574         AslGbl_TableHeader.Revision = AslGbl_RevisionOverride;
575     }
576
577     /* OEMID */
578
579     Child = Child->Asl.Next;
580     memcpy (AslGbl_TableHeader.OemId, Child->Asl.Value.String,
581         strlen (Child->Asl.Value.String));
582
583     /* OEM TableID */
584
585     Child = Child->Asl.Next;
586     memcpy (AslGbl_TableHeader.OemTableId, Child->Asl.Value.String,
587         strlen (Child->Asl.Value.String));
588
589     /* OEM Revision */
590
591     Child = Child->Asl.Next;
592     AslGbl_TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
593
594     /* Compiler ID */
595
596     ACPI_COPY_NAMESEG (AslGbl_TableHeader.AslCompilerId, ASL_CREATOR_ID);
597
598     /* Compiler version */
599
600     AslGbl_TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
601
602     /* Table length. Checksum zero for now, will rewrite later */
603
604     AslGbl_TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
605         Op->Asl.AmlSubtreeLength;
606
607     /* Calculate the comment lengths for this definition block parseOp */
608
609     if (AcpiGbl_CaptureComments)
610     {
611         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
612             Op->Asl.ParseOpName);
613
614         /*
615          * Take the filename without extensions, add 3 for the new extension
616          * and another 3 for the a908 bytecode and null terminator.
617          */
618         AslGbl_TableHeader.Length += strrchr (AslGbl_ParseTreeRoot->Asl.Filename, '.')
619             - AslGbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
620
621         Op->Asl.AmlSubtreeLength +=
622             strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3;
623
624         CvDbgPrint ("     Length: %lu\n",
625             strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3);
626
627         if (Op->Asl.CommentList)
628         {
629             Current = Op->Asl.CommentList;
630             while (Current)
631             {
632                 CommentLength = strlen (Current->Comment)+3;
633                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
634                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
635                 AslGbl_TableHeader.Length += CommentLength;
636                 Op->Asl.AmlSubtreeLength += CommentLength;
637                 Current = Current->Next;
638                 CvDbgPrint ("    Length: %u\n", CommentLength);
639             }
640         }
641         if (Op->Asl.CloseBraceComment)
642         {
643             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
644             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
645             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
646             AslGbl_TableHeader.Length += CommentLength;
647             Op->Asl.AmlSubtreeLength += CommentLength;
648             CvDbgPrint ("    Length: %u\n", CommentLength);
649         }
650     }
651
652     AslGbl_TableHeader.Checksum = 0;
653     Op->Asl.FinalAmlOffset = ftell (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);
654
655     /* Write entire header and clear the table header global */
656
657     CgLocalWriteAmlData (Op, &AslGbl_TableHeader, sizeof (ACPI_TABLE_HEADER));
658     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
659 }
660
661
662 /*******************************************************************************
663  *
664  * FUNCTION:    CgUpdateHeader
665  *
666  * PARAMETERS:  Op                  - Op for the Definition Block
667  *
668  * RETURN:      None.
669  *
670  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
671  *              re-writing the header for the input definition block
672  *
673  ******************************************************************************/
674
675 static void
676 CgUpdateHeader (
677     ACPI_PARSE_OBJECT       *Op)
678 {
679     signed char             Sum;
680     UINT32                  i;
681     UINT32                  Length;
682     UINT8                   FileByte;
683     UINT8                   Checksum;
684
685
686     /* Calculate the checksum over the entire definition block */
687
688     Sum = 0;
689     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
690     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
691
692     for (i = 0; i < Length; i++)
693     {
694         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
695         {
696             printf ("EOF while reading checksum bytes\n");
697             return;
698         }
699
700         Sum = (signed char) (Sum + FileByte);
701     }
702
703     Checksum = (UINT8) (0 - Sum);
704
705     /* Re-write the the checksum byte */
706
707     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
708         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
709
710     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
711 }
712
713
714 /*******************************************************************************
715  *
716  * FUNCTION:    CgCloseTable
717  *
718  * PARAMETERS:  None.
719  *
720  * RETURN:      None.
721  *
722  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
723  *              re-writing each table header. This allows support for
724  *              multiple definition blocks in a single source file.
725  *
726  ******************************************************************************/
727
728 static void
729 CgCloseTable (
730     void)
731 {
732     ACPI_PARSE_OBJECT   *Op;
733
734
735     /* Process all definition blocks */
736
737     Op = AslGbl_ParseTreeRoot->Asl.Child;
738     while (Op)
739     {
740         CgUpdateHeader (Op);
741         Op = Op->Asl.Next;
742     }
743 }
744
745
746 /*******************************************************************************
747  *
748  * FUNCTION:    CgWriteNode
749  *
750  * PARAMETERS:  Op            - Parse node to write.
751  *
752  * RETURN:      None.
753  *
754  * DESCRIPTION: Write the AML that corresponds to a parse node.
755  *
756  ******************************************************************************/
757
758 static void
759 CgWriteNode (
760     ACPI_PARSE_OBJECT       *Op)
761 {
762     ASL_RESOURCE_NODE       *Rnode;
763
764
765     /* Write all comments here. */
766
767     if (AcpiGbl_CaptureComments)
768     {
769         CgWriteAmlComment(Op);
770     }
771
772     /* Always check for DEFAULT_ARG and other "Noop" nodes */
773     /* TBD: this may not be the best place for this check */
774
775     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
776         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
777         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
778     {
779         return;
780     }
781
782     Op->Asl.FinalAmlLength = 0;
783
784     switch (Op->Asl.AmlOpcode)
785     {
786     case AML_RAW_DATA_BYTE:
787     case AML_RAW_DATA_WORD:
788     case AML_RAW_DATA_DWORD:
789     case AML_RAW_DATA_QWORD:
790
791         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
792         return;
793
794
795     case AML_RAW_DATA_BUFFER:
796
797         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
798         return;
799
800
801     case AML_RAW_DATA_CHAIN:
802
803         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
804         while (Rnode)
805         {
806             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
807             Rnode = Rnode->Next;
808         }
809         return;
810
811     default:
812
813         /* Internal data opcodes must all appear above */
814
815         break;
816     }
817
818     switch (Op->Asl.ParseOpcode)
819     {
820     case PARSEOP_DEFAULT_ARG:
821
822         break;
823
824     case PARSEOP_DEFINITION_BLOCK:
825
826         CgWriteTableHeader (Op);
827         if (AcpiGbl_CaptureComments)
828         {
829             CgWriteAmlDefBlockComment (Op);
830         }
831         break;
832
833     case PARSEOP_NAMESEG:
834     case PARSEOP_NAMESTRING:
835     case PARSEOP_METHODCALL:
836
837         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
838         break;
839
840     default:
841
842         CgWriteAmlOpcode (Op);
843         break;
844     }
845 }