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