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