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