xref: /illumos-gate/usr/src/cmd/acpi/iasl/aslcompile.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1 /******************************************************************************
2  *
3  * Module Name: aslcompile - top level compile module
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2018, 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 "acnamesp.h"
154 
155 #include <stdio.h>
156 #include <time.h>
157 #include <acapps.h>
158 
159 #define _COMPONENT          ACPI_COMPILER
160         ACPI_MODULE_NAME    ("aslcompile")
161 
162 /*
163  * Main parser entry
164  * External is here in case the parser emits the same external in the
165  * generated header. (Newer versions of Bison)
166  */
167 int
168 AslCompilerparse(
169     void);
170 
171 /* Local prototypes */
172 
173 static void
174 CmFlushSourceCode (
175     void);
176 
177 static void
178 CmDumpAllEvents (
179     void);
180 
181 
182 /*******************************************************************************
183  *
184  * FUNCTION:    CmDoCompile
185  *
186  * PARAMETERS:  None
187  *
188  * RETURN:      Status (0 = OK)
189  *
190  * DESCRIPTION: This procedure performs the entire compile
191  *
192  ******************************************************************************/
193 
194 int
195 CmDoCompile (
196     void)
197 {
198     ACPI_STATUS             Status;
199     UINT8                   FullCompile;
200     UINT8                   Event;
201 
202 
203     FullCompile = UtBeginEvent ("*** Total Compile time ***");
204     Event = UtBeginEvent ("Open input and output files");
205     UtEndEvent (Event);
206 
207     Event = UtBeginEvent ("Preprocess input file");
208     if (Gbl_PreprocessFlag)
209     {
210         /* Enter compiler name as a #define */
211 
212         PrAddDefine (ASL_DEFINE, "", FALSE);
213 
214         /* Preprocessor */
215 
216         PrDoPreprocess ();
217         Gbl_CurrentLineNumber = 1;
218         Gbl_LogicalLineNumber = 1;
219 
220         if (Gbl_PreprocessOnly)
221         {
222             UtEndEvent (Event);
223             CmCleanupAndExit ();
224             return (0);
225         }
226     }
227     UtEndEvent (Event);
228 
229 
230     /* Build the parse tree */
231 
232     Event = UtBeginEvent ("Parse source code and build parse tree");
233     AslCompilerparse();
234     UtEndEvent (Event);
235 
236     /* Check for parser-detected syntax errors */
237 
238     if (Gbl_SyntaxError)
239     {
240         fprintf (stderr,
241             "Compiler aborting due to parser-detected syntax error(s)\n");
242         LsDumpParseTree ();
243         goto ErrorExit;
244     }
245 
246     /* Did the parse tree get successfully constructed? */
247 
248     if (!Gbl_ParseTreeRoot)
249     {
250         /*
251          * If there are no errors, then we have some sort of
252          * internal problem.
253          */
254         AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
255             NULL, "- Could not resolve parse tree root node");
256 
257         goto ErrorExit;
258     }
259 
260     /* Flush out any remaining source after parse tree is complete */
261 
262     Event = UtBeginEvent ("Flush source input");
263     CmFlushSourceCode ();
264 
265     /* Prune the parse tree if requested (debug purposes only) */
266 
267     if (Gbl_PruneParseTree)
268     {
269         AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
270     }
271 
272     /* Optional parse tree dump, compiler debug output only */
273 
274     LsDumpParseTree ();
275 
276     OpcGetIntegerWidth (Gbl_ParseTreeRoot->Asl.Child);
277     UtEndEvent (Event);
278 
279     /* Pre-process parse tree for any operator transforms */
280 
281     Event = UtBeginEvent ("Parse tree transforms");
282     DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
283     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
284         TrAmlTransformWalkBegin, TrAmlTransformWalkEnd, NULL);
285     UtEndEvent (Event);
286 
287     /* Generate AML opcodes corresponding to the parse tokens */
288 
289     Event = UtBeginEvent ("Generate AML opcodes");
290     DbgPrint (ASL_DEBUG_OUTPUT, "Generating AML opcodes\n\n");
291     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
292         OpcAmlOpcodeWalk, NULL);
293     UtEndEvent (Event);
294 
295     /*
296      * Now that the input is parsed, we can open the AML output file.
297      * Note: by default, the name of this file comes from the table
298      * descriptor within the input file.
299      */
300     Event = UtBeginEvent ("Open AML output file");
301     Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
302     UtEndEvent (Event);
303     if (ACPI_FAILURE (Status))
304     {
305         AePrintErrorLog (ASL_FILE_STDERR);
306         return (-1);
307     }
308 
309     /* Interpret and generate all compile-time constants */
310 
311     Event = UtBeginEvent ("Constant folding via AML interpreter");
312     DbgPrint (ASL_DEBUG_OUTPUT,
313         "Interpreting compile-time constant expressions\n\n");
314 
315     if (Gbl_FoldConstants)
316     {
317         TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
318             NULL, OpcAmlConstantWalk, NULL);
319     }
320     else
321     {
322         DbgPrint (ASL_PARSE_OUTPUT, "    Optional folding disabled\n");
323     }
324     UtEndEvent (Event);
325 
326     /* Update AML opcodes if necessary, after constant folding */
327 
328     Event = UtBeginEvent ("Updating AML opcodes after constant folding");
329     DbgPrint (ASL_DEBUG_OUTPUT,
330         "Updating AML opcodes after constant folding\n\n");
331     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
332         NULL, OpcAmlOpcodeUpdateWalk, NULL);
333     UtEndEvent (Event);
334 
335     /* Calculate all AML package lengths */
336 
337     Event = UtBeginEvent ("Generate AML package lengths");
338     DbgPrint (ASL_DEBUG_OUTPUT, "Generating Package lengths\n\n");
339     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
340         LnPackageLengthWalk, NULL);
341     UtEndEvent (Event);
342 
343     if (Gbl_ParseOnlyFlag)
344     {
345         AePrintErrorLog (ASL_FILE_STDERR);
346         UtDisplaySummary (ASL_FILE_STDERR);
347         if (Gbl_DebugFlag)
348         {
349             /* Print error summary to the stdout also */
350 
351             AePrintErrorLog (ASL_FILE_STDOUT);
352             UtDisplaySummary (ASL_FILE_STDOUT);
353         }
354         UtEndEvent (FullCompile);
355         return (0);
356     }
357 
358     /*
359      * Create an internal namespace and use it as a symbol table
360      */
361 
362     /* Namespace loading */
363 
364     Event = UtBeginEvent ("Create ACPI Namespace");
365     DbgPrint (ASL_DEBUG_OUTPUT, "Creating ACPI Namespace\n\n");
366     Status = LdLoadNamespace (Gbl_ParseTreeRoot);
367     UtEndEvent (Event);
368     if (ACPI_FAILURE (Status))
369     {
370         goto ErrorExit;
371     }
372 
373     /* Namespace cross-reference */
374 
375     AslGbl_NamespaceEvent = UtBeginEvent (
376         "Cross reference parse tree and Namespace");
377     DbgPrint (ASL_DEBUG_OUTPUT, "Cross referencing namespace\n\n");
378     Status = XfCrossReferenceNamespace ();
379     if (ACPI_FAILURE (Status))
380     {
381         goto ErrorExit;
382     }
383 
384     /* Namespace - Check for non-referenced objects */
385 
386     LkFindUnreferencedObjects ();
387     UtEndEvent (AslGbl_NamespaceEvent);
388 
389     /* Resolve External Declarations */
390 
391     if (Gbl_DoExternals)
392     {
393         Event = UtBeginEvent ("Resolve all Externals");
394         DbgPrint (ASL_DEBUG_OUTPUT, "\nResolve Externals\n\n");
395 
396         if (Gbl_DoExternalsInPlace)
397         {
398             TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
399                 ExAmlExternalWalkBegin, NULL, NULL);
400         }
401         else
402         {
403             TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
404                 ExAmlExternalWalkBegin, ExAmlExternalWalkEnd, NULL);
405         }
406         UtEndEvent (Event);
407     }
408 
409     /*
410      * Semantic analysis. This can happen only after the
411      * namespace has been loaded and cross-referenced.
412      *
413      * part one - check control methods
414      */
415     Event = UtBeginEvent ("Analyze control method return types");
416     AnalysisWalkInfo.MethodStack = NULL;
417 
418     DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - Method analysis\n\n");
419 
420     if (Gbl_CrossReferenceOutput)
421     {
422         OtPrintHeaders ("Part 1: Object Reference Map "
423             "(Object references from within each control method)");
424     }
425 
426     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
427         MtMethodAnalysisWalkBegin,
428         MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
429     UtEndEvent (Event);
430 
431     /* Generate the object cross-reference file if requested */
432 
433     Event = UtBeginEvent ("Generate cross-reference file");
434     OtCreateXrefFile ();
435     UtEndEvent (Event);
436 
437     /* Semantic error checking part two - typing of method returns */
438 
439     Event = UtBeginEvent ("Determine object types returned by methods");
440     DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - Method typing\n\n");
441     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
442         NULL, AnMethodTypingWalkEnd, NULL);
443     UtEndEvent (Event);
444 
445     /* Semantic error checking part three - operand type checking */
446 
447     Event = UtBeginEvent ("Analyze AML operand types");
448     DbgPrint (ASL_DEBUG_OUTPUT,
449         "Semantic analysis - Operand type checking\n\n");
450     if (Gbl_DoTypechecking)
451     {
452         TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
453             NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
454     }
455     UtEndEvent (Event);
456 
457     /* Semantic error checking part four - other miscellaneous checks */
458 
459     Event = UtBeginEvent ("Miscellaneous analysis");
460     DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - miscellaneous\n\n");
461     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
462         AnOtherSemanticAnalysisWalkBegin,
463         NULL, &AnalysisWalkInfo);
464     UtEndEvent (Event);
465 
466     /*
467      * ASL-/ASL+ converter: Gbl_ParseTreeRoot->CommentList contains the
468      * very last comment of a given ASL file because it's the last constructed
469      * node during compilation. We take the very last comment and save it in a
470      * global for it to be used by the disassembler.
471      */
472     if (AcpiGbl_CaptureComments)
473     {
474         AcpiGbl_LastListHead = Gbl_ParseTreeRoot->Asl.CommentList;
475         Gbl_ParseTreeRoot->Asl.CommentList = NULL;
476     }
477 
478     /* Calculate all AML package lengths */
479 
480     Event = UtBeginEvent ("Finish AML package length generation");
481     DbgPrint (ASL_DEBUG_OUTPUT, "Generating Package lengths\n\n");
482     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
483         LnInitLengthsWalk, NULL);
484     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
485         LnPackageLengthWalk, NULL);
486     UtEndEvent (Event);
487 
488     /* Code generation - emit the AML */
489 
490     Event = UtBeginEvent ("Generate AML code and write output files");
491     DbgPrint (ASL_DEBUG_OUTPUT, "Writing AML byte code\n\n");
492     CgGenerateAmlOutput ();
493     UtEndEvent (Event);
494 
495     Event = UtBeginEvent ("Write optional output files");
496     CmDoOutputFiles ();
497     UtEndEvent (Event);
498 
499     UtEndEvent (FullCompile);
500     CmCleanupAndExit ();
501     return (0);
502 
503 ErrorExit:
504     UtEndEvent (FullCompile);
505     CmCleanupAndExit ();
506     return (-1);
507 }
508 
509 
510 /*******************************************************************************
511  *
512  * FUNCTION:    AslCompilerSignon
513  *
514  * PARAMETERS:  FileId      - ID of the output file
515  *
516  * RETURN:      None
517  *
518  * DESCRIPTION: Display compiler signon
519  *
520  ******************************************************************************/
521 
522 void
523 AslCompilerSignon (
524     UINT32                  FileId)
525 {
526     char                    *Prefix = "";
527     char                    *UtilityName;
528 
529 
530     /* Set line prefix depending on the destination file type */
531 
532     switch (FileId)
533     {
534     case ASL_FILE_ASM_SOURCE_OUTPUT:
535     case ASL_FILE_ASM_INCLUDE_OUTPUT:
536 
537         Prefix = "; ";
538         break;
539 
540     case ASL_FILE_HEX_OUTPUT:
541 
542         if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
543         {
544             Prefix = "; ";
545         }
546         else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
547                  (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
548         {
549             FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
550             Prefix = " * ";
551         }
552         break;
553 
554     case ASL_FILE_C_SOURCE_OUTPUT:
555     case ASL_FILE_C_OFFSET_OUTPUT:
556     case ASL_FILE_C_INCLUDE_OUTPUT:
557 
558         Prefix = " * ";
559         break;
560 
561     default:
562 
563         /* No other output types supported */
564 
565         break;
566     }
567 
568     /* Running compiler or disassembler? */
569 
570     if (AcpiGbl_DisasmFlag)
571     {
572         UtilityName = AML_DISASSEMBLER_NAME;
573     }
574     else
575     {
576         UtilityName = ASL_COMPILER_NAME;
577     }
578 
579     /* Compiler signon with copyright */
580 
581     FlPrintFile (FileId, "%s\n", Prefix);
582     FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
583 }
584 
585 
586 /*******************************************************************************
587  *
588  * FUNCTION:    AslCompilerFileHeader
589  *
590  * PARAMETERS:  FileId      - ID of the output file
591  *
592  * RETURN:      None
593  *
594  * DESCRIPTION: Header used at the beginning of output files
595  *
596  ******************************************************************************/
597 
598 void
599 AslCompilerFileHeader (
600     UINT32                  FileId)
601 {
602     struct tm               *NewTime;
603     time_t                  Aclock;
604     char                    *Prefix = "";
605 
606 
607     /* Set line prefix depending on the destination file type */
608 
609     switch (FileId)
610     {
611     case ASL_FILE_ASM_SOURCE_OUTPUT:
612     case ASL_FILE_ASM_INCLUDE_OUTPUT:
613 
614         Prefix = "; ";
615         break;
616 
617     case ASL_FILE_HEX_OUTPUT:
618 
619         if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
620         {
621             Prefix = "; ";
622         }
623         else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
624                  (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
625         {
626             Prefix = " * ";
627         }
628         break;
629 
630     case ASL_FILE_C_SOURCE_OUTPUT:
631     case ASL_FILE_C_OFFSET_OUTPUT:
632     case ASL_FILE_C_INCLUDE_OUTPUT:
633 
634         Prefix = " * ";
635         break;
636 
637     default:
638 
639         /* No other output types supported */
640 
641         break;
642     }
643 
644     /* Compilation header with timestamp */
645 
646     (void) time (&Aclock);
647     NewTime = localtime (&Aclock);
648 
649     FlPrintFile (FileId,
650         "%sCompilation of \"%s\" - %s%s\n",
651         Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
652         Prefix);
653 
654     switch (FileId)
655     {
656     case ASL_FILE_C_SOURCE_OUTPUT:
657     case ASL_FILE_C_OFFSET_OUTPUT:
658     case ASL_FILE_C_INCLUDE_OUTPUT:
659 
660         FlPrintFile (FileId, " */\n");
661         break;
662 
663     default:
664 
665         /* Nothing to do for other output types */
666 
667         break;
668     }
669 }
670 
671 
672 /*******************************************************************************
673  *
674  * FUNCTION:    CmFlushSourceCode
675  *
676  * PARAMETERS:  None
677  *
678  * RETURN:      None
679  *
680  * DESCRIPTION: Read in any remaining source code after the parse tree
681  *              has been constructed.
682  *
683  ******************************************************************************/
684 
685 static void
686 CmFlushSourceCode (
687     void)
688 {
689     char                    Buffer;
690 
691 
692     while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
693     {
694         AslInsertLineBuffer ((int) Buffer);
695     }
696 
697     AslResetCurrentLineBuffer ();
698 }
699 
700 
701 /*******************************************************************************
702  *
703  * FUNCTION:    CmDoOutputFiles
704  *
705  * PARAMETERS:  None
706  *
707  * RETURN:      None.
708  *
709  * DESCRIPTION: Create all "listing" type files
710  *
711  ******************************************************************************/
712 
713 void
714 CmDoOutputFiles (
715     void)
716 {
717 
718     /* Create listings and hex files */
719 
720     LsDoListings ();
721     HxDoHexOutput ();
722 
723     /* Dump the namespace to the .nsp file if requested */
724 
725     (void) NsDisplayNamespace ();
726 
727     /* Dump the device mapping file */
728 
729     MpEmitMappingInfo ();
730 }
731 
732 
733 /*******************************************************************************
734  *
735  * FUNCTION:    CmDumpAllEvents
736  *
737  * PARAMETERS:  None
738  *
739  * RETURN:      None.
740  *
741  * DESCRIPTION: Dump all compiler events
742  *
743  ******************************************************************************/
744 
745 static void
746 CmDumpAllEvents (
747     void)
748 {
749     ASL_EVENT_INFO          *Event;
750     UINT32                  Delta;
751     UINT32                  MicroSeconds;
752     UINT32                  MilliSeconds;
753     UINT32                  i;
754 
755 
756     Event = AslGbl_Events;
757 
758     DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
759     if (Gbl_CompileTimesFlag)
760     {
761         printf ("\nElapsed time for major events\n\n");
762     }
763 
764     for (i = 0; i < AslGbl_NextEvent; i++)
765     {
766         if (Event->Valid)
767         {
768             /* Delta will be in 100-nanosecond units */
769 
770             Delta = (UINT32) (Event->EndTime - Event->StartTime);
771 
772             MicroSeconds = Delta / ACPI_100NSEC_PER_USEC;
773             MilliSeconds = Delta / ACPI_100NSEC_PER_MSEC;
774 
775             /* Round milliseconds up */
776 
777             if ((MicroSeconds - (MilliSeconds * ACPI_USEC_PER_MSEC)) >= 500)
778             {
779                 MilliSeconds++;
780             }
781 
782             DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
783                 MicroSeconds, MilliSeconds, Event->EventName);
784 
785             if (Gbl_CompileTimesFlag)
786             {
787                 printf ("%8u usec %8u msec - %s\n",
788                     MicroSeconds, MilliSeconds, Event->EventName);
789             }
790         }
791 
792         Event++;
793     }
794 }
795 
796 
797 /*******************************************************************************
798  *
799  * FUNCTION:    CmCleanupAndExit
800  *
801  * PARAMETERS:  None
802  *
803  * RETURN:      None.
804  *
805  * DESCRIPTION: Close all open files and exit the compiler
806  *
807  ******************************************************************************/
808 
809 void
810 CmCleanupAndExit (
811     void)
812 {
813     UINT32                  i;
814     BOOLEAN                 DeleteAmlFile = FALSE;
815 
816 
817     AslCheckExpectedExceptions ();
818     AePrintErrorLog (ASL_FILE_STDERR);
819     if (Gbl_DebugFlag)
820     {
821         /* Print error summary to stdout also */
822 
823         AePrintErrorLog (ASL_FILE_STDOUT);
824     }
825 
826     /* Emit compile times if enabled */
827 
828     CmDumpAllEvents ();
829 
830     if (Gbl_CompileTimesFlag)
831     {
832         printf ("\nMiscellaneous compile statistics\n\n");
833         printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
834         printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
835         printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
836         printf ("%11u : %s\n", TotalMethods, "Control methods");
837         printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
838         printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
839         printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
840         printf ("\n");
841     }
842 
843     if (Gbl_NsLookupCount)
844     {
845         DbgPrint (ASL_DEBUG_OUTPUT,
846             "\n\nMiscellaneous compile statistics\n\n");
847 
848         DbgPrint (ASL_DEBUG_OUTPUT,
849             "%32s : %u\n", "Total Namespace searches",
850             Gbl_NsLookupCount);
851 
852         DbgPrint (ASL_DEBUG_OUTPUT,
853             "%32s : %u usec\n", "Time per search", ((UINT32)
854             (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
855                 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
856                 Gbl_NsLookupCount);
857     }
858 
859     if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
860     {
861         printf ("\nMaximum error count (%u) exceeded\n",
862             ASL_MAX_ERROR_COUNT);
863     }
864 
865     UtDisplaySummary (ASL_FILE_STDOUT);
866 
867     /*
868      * We will delete the AML file if there are errors and the
869      * force AML output option has not been used.
870      */
871     if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
872         (!Gbl_IgnoreErrors) &&
873         Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
874     {
875         DeleteAmlFile = TRUE;
876     }
877 
878     /* Close all open files */
879 
880     /*
881      * Take care with the preprocessor file (.pre), it might be the same
882      * as the "input" file, depending on where the compiler has terminated
883      * or aborted. Prevent attempt to close the same file twice in
884      * loop below.
885      */
886     if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
887         Gbl_Files[ASL_FILE_INPUT].Handle)
888     {
889         Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
890     }
891 
892     /* Close the standard I/O files */
893 
894     for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
895     {
896         FlCloseFile (i);
897     }
898 
899     /* Delete AML file if there are errors */
900 
901     if (DeleteAmlFile)
902     {
903         FlDeleteFile (ASL_FILE_AML_OUTPUT);
904     }
905 
906     /* Delete the preprocessor temp file unless full debug was specified */
907 
908     if (Gbl_PreprocessFlag && !Gbl_KeepPreprocessorTempFile)
909     {
910         FlDeleteFile (ASL_FILE_PREPROCESSOR);
911     }
912 
913     /*
914      * Delete intermediate ("combined") source file (if -ls flag not set)
915      * This file is created during normal ASL/AML compiles. It is not
916      * created by the data table compiler.
917      *
918      * If the -ls flag is set, then the .SRC file should not be deleted.
919      * In this case, Gbl_SourceOutputFlag is set to TRUE.
920      *
921      * Note: Handles are cleared by FlCloseFile above, so we look at the
922      * filename instead, to determine if the .SRC file was actually
923      * created.
924      */
925     if (!Gbl_SourceOutputFlag)
926     {
927         FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
928     }
929 
930     /* Final cleanup after compiling one file */
931 
932     if (!Gbl_DoAslConversion)
933     {
934         UtDeleteLocalCaches ();
935     }
936 
937 }
938