1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "aslcompiler.h"
45 #include "dtcompiler.h"
52 #define _COMPONENT ACPI_COMPILER
53 ACPI_MODULE_NAME ("aslcompile")
57 * External is here in case the parser emits the same external in the
58 * generated header. (Newer versions of Bison)
64 /* Local prototypes */
75 /*******************************************************************************
77 * FUNCTION: CmDoCompile
81 * RETURN: Status (0 = OK)
83 * DESCRIPTION: This procedure performs the entire compile
85 ******************************************************************************/
96 FullCompile = UtBeginEvent ("*** Total Compile time ***");
97 Event = UtBeginEvent ("Open input and output files");
100 Event = UtBeginEvent ("Preprocess input file");
101 if (Gbl_PreprocessFlag)
106 if (Gbl_PreprocessOnly)
115 /* Build the parse tree */
117 Event = UtBeginEvent ("Parse source code and build parse tree");
121 /* Check for parse errors */
123 Status = AslCheckForErrorExit ();
124 if (ACPI_FAILURE (Status))
126 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
131 /* Did the parse tree get successfully constructed? */
136 * If there are no errors, then we have some sort of
139 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
140 NULL, "- Could not resolve parse tree root node");
145 /* Flush out any remaining source after parse tree is complete */
147 Event = UtBeginEvent ("Flush source input");
148 CmFlushSourceCode ();
150 /* Prune the parse tree if requested (debug purposes only) */
152 if (Gbl_PruneParseTree)
154 AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
157 /* Optional parse tree dump, compiler debug output only */
161 OpcGetIntegerWidth (RootNode);
164 /* Pre-process parse tree for any operator transforms */
166 Event = UtBeginEvent ("Parse tree transforms");
167 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
168 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
169 TrAmlTransformWalk, NULL, NULL);
172 /* Generate AML opcodes corresponding to the parse tokens */
174 Event = UtBeginEvent ("Generate AML opcodes");
175 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
176 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
177 OpcAmlOpcodeWalk, NULL);
181 * Now that the input is parsed, we can open the AML output file.
182 * Note: by default, the name of this file comes from the table descriptor
183 * within the input file.
185 Event = UtBeginEvent ("Open AML output file");
186 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
188 if (ACPI_FAILURE (Status))
190 AePrintErrorLog (ASL_FILE_STDERR);
194 /* Interpret and generate all compile-time constants */
196 Event = UtBeginEvent ("Constant folding via AML interpreter");
197 DbgPrint (ASL_DEBUG_OUTPUT,
198 "\nInterpreting compile-time constant expressions\n\n");
199 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
200 OpcAmlConstantWalk, NULL, NULL);
203 /* Update AML opcodes if necessary, after constant folding */
205 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
206 DbgPrint (ASL_DEBUG_OUTPUT,
207 "\nUpdating AML opcodes after constant folding\n\n");
208 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
209 NULL, OpcAmlOpcodeUpdateWalk, NULL);
212 /* Calculate all AML package lengths */
214 Event = UtBeginEvent ("Generate AML package lengths");
215 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
216 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
217 LnPackageLengthWalk, NULL);
220 if (Gbl_ParseOnlyFlag)
222 AePrintErrorLog (ASL_FILE_STDERR);
223 UtDisplaySummary (ASL_FILE_STDERR);
226 /* Print error summary to the stdout also */
228 AePrintErrorLog (ASL_FILE_STDOUT);
229 UtDisplaySummary (ASL_FILE_STDOUT);
231 UtEndEvent (FullCompile);
236 * Create an internal namespace and use it as a symbol table
239 /* Namespace loading */
241 Event = UtBeginEvent ("Create ACPI Namespace");
242 Status = LdLoadNamespace (RootNode);
244 if (ACPI_FAILURE (Status))
249 /* Namespace cross-reference */
251 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
252 Status = XfCrossReferenceNamespace ();
253 if (ACPI_FAILURE (Status))
258 /* Namespace - Check for non-referenced objects */
260 LkFindUnreferencedObjects ();
261 UtEndEvent (AslGbl_NamespaceEvent);
264 * Semantic analysis. This can happen only after the
265 * namespace has been loaded and cross-referenced.
267 * part one - check control methods
269 Event = UtBeginEvent ("Analyze control method return types");
270 AnalysisWalkInfo.MethodStack = NULL;
272 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
273 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
274 MtMethodAnalysisWalkBegin,
275 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
278 /* Semantic error checking part two - typing of method returns */
280 Event = UtBeginEvent ("Determine object types returned by methods");
281 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
282 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
283 NULL, AnMethodTypingWalkEnd, NULL);
286 /* Semantic error checking part three - operand type checking */
288 Event = UtBeginEvent ("Analyze AML operand types");
289 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
290 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
291 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
294 /* Semantic error checking part four - other miscellaneous checks */
296 Event = UtBeginEvent ("Miscellaneous analysis");
297 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
298 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
299 AnOtherSemanticAnalysisWalkBegin,
300 NULL, &AnalysisWalkInfo);
303 /* Calculate all AML package lengths */
305 Event = UtBeginEvent ("Finish AML package length generation");
306 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
307 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
308 LnInitLengthsWalk, NULL);
309 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
310 LnPackageLengthWalk, NULL);
313 /* Code generation - emit the AML */
315 Event = UtBeginEvent ("Generate AML code and write output files");
316 CgGenerateAmlOutput ();
319 Event = UtBeginEvent ("Write optional output files");
323 UtEndEvent (FullCompile);
328 UtEndEvent (FullCompile);
334 /*******************************************************************************
336 * FUNCTION: AslCompilerSignon
338 * PARAMETERS: FileId - ID of the output file
342 * DESCRIPTION: Display compiler signon
344 ******************************************************************************/
354 /* Set line prefix depending on the destination file type */
358 case ASL_FILE_ASM_SOURCE_OUTPUT:
359 case ASL_FILE_ASM_INCLUDE_OUTPUT:
364 case ASL_FILE_HEX_OUTPUT:
366 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
370 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
371 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
373 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
378 case ASL_FILE_C_SOURCE_OUTPUT:
379 case ASL_FILE_C_OFFSET_OUTPUT:
380 case ASL_FILE_C_INCLUDE_OUTPUT:
387 /* No other output types supported */
392 /* Running compiler or disassembler? */
396 UtilityName = AML_DISASSEMBLER_NAME;
400 UtilityName = ASL_COMPILER_NAME;
403 /* Compiler signon with copyright */
405 FlPrintFile (FileId, "%s\n", Prefix);
406 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
410 /*******************************************************************************
412 * FUNCTION: AslCompilerFileHeader
414 * PARAMETERS: FileId - ID of the output file
418 * DESCRIPTION: Header used at the beginning of output files
420 ******************************************************************************/
423 AslCompilerFileHeader (
431 /* Set line prefix depending on the destination file type */
435 case ASL_FILE_ASM_SOURCE_OUTPUT:
436 case ASL_FILE_ASM_INCLUDE_OUTPUT:
441 case ASL_FILE_HEX_OUTPUT:
443 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
447 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
448 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
454 case ASL_FILE_C_SOURCE_OUTPUT:
455 case ASL_FILE_C_OFFSET_OUTPUT:
456 case ASL_FILE_C_INCLUDE_OUTPUT:
463 /* No other output types supported */
468 /* Compilation header with timestamp */
470 (void) time (&Aclock);
471 NewTime = localtime (&Aclock);
474 "%sCompilation of \"%s\" - %s%s\n",
475 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
480 case ASL_FILE_C_SOURCE_OUTPUT:
481 case ASL_FILE_C_OFFSET_OUTPUT:
482 case ASL_FILE_C_INCLUDE_OUTPUT:
484 FlPrintFile (FileId, " */\n");
489 /* Nothing to do for other output types */
496 /*******************************************************************************
498 * FUNCTION: CmFlushSourceCode
504 * DESCRIPTION: Read in any remaining source code after the parse tree
505 * has been constructed.
507 ******************************************************************************/
516 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
518 AslInsertLineBuffer ((int) Buffer);
521 AslResetCurrentLineBuffer ();
525 /*******************************************************************************
527 * FUNCTION: CmDoOutputFiles
533 * DESCRIPTION: Create all "listing" type files
535 ******************************************************************************/
542 /* Create listings and hex files */
547 /* Dump the namespace to the .nsp file if requested */
549 (void) NsDisplayNamespace ();
551 /* Dump the device mapping file */
553 MpEmitMappingInfo ();
557 /*******************************************************************************
559 * FUNCTION: CmDumpAllEvents
565 * DESCRIPTION: Dump all compiler events
567 ******************************************************************************/
573 ASL_EVENT_INFO *Event;
580 Event = AslGbl_Events;
582 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
583 if (Gbl_CompileTimesFlag)
585 printf ("\nElapsed time for major events\n\n");
588 for (i = 0; i < AslGbl_NextEvent; i++)
592 /* Delta will be in 100-nanosecond units */
594 Delta = (UINT32) (Event->EndTime - Event->StartTime);
596 USec = Delta / ACPI_100NSEC_PER_USEC;
597 MSec = Delta / ACPI_100NSEC_PER_MSEC;
599 /* Round milliseconds up */
601 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
606 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
607 USec, MSec, Event->EventName);
609 if (Gbl_CompileTimesFlag)
611 printf ("%8u usec %8u msec - %s\n",
612 USec, MSec, Event->EventName);
621 /*******************************************************************************
623 * FUNCTION: CmCleanupAndExit
629 * DESCRIPTION: Close all open files and exit the compiler
631 ******************************************************************************/
638 BOOLEAN DeleteAmlFile = FALSE;
641 AePrintErrorLog (ASL_FILE_STDERR);
644 /* Print error summary to stdout also */
646 AePrintErrorLog (ASL_FILE_STDOUT);
649 /* Emit compile times if enabled */
653 if (Gbl_CompileTimesFlag)
655 printf ("\nMiscellaneous compile statistics\n\n");
656 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
657 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
658 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
659 printf ("%11u : %s\n", TotalMethods, "Control methods");
660 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
661 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
662 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
666 if (Gbl_NsLookupCount)
668 DbgPrint (ASL_DEBUG_OUTPUT,
669 "\n\nMiscellaneous compile statistics\n\n");
671 DbgPrint (ASL_DEBUG_OUTPUT,
672 "%32s : %u\n", "Total Namespace searches",
675 DbgPrint (ASL_DEBUG_OUTPUT,
676 "%32s : %u usec\n", "Time per search", ((UINT32)
677 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
678 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
682 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
684 printf ("\nMaximum error count (%u) exceeded\n",
685 ASL_MAX_ERROR_COUNT);
688 UtDisplaySummary (ASL_FILE_STDOUT);
691 * We will delete the AML file if there are errors and the
692 * force AML output option has not been used.
694 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
695 (!Gbl_IgnoreErrors) &&
696 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
698 DeleteAmlFile = TRUE;
701 /* Close all open files */
704 * Take care with the preprocessor file (.i), it might be the same
705 * as the "input" file, depending on where the compiler has terminated
706 * or aborted. Prevent attempt to close the same file twice in
709 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
710 Gbl_Files[ASL_FILE_INPUT].Handle)
712 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
715 /* Close the standard I/O files */
717 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
722 /* Delete AML file if there are errors */
726 FlDeleteFile (ASL_FILE_AML_OUTPUT);
729 /* Delete the preprocessor output file (.i) unless -li flag is set */
731 if (!Gbl_PreprocessorOutputFlag &&
734 FlDeleteFile (ASL_FILE_PREPROCESSOR);
738 * Delete intermediate ("combined") source file (if -ls flag not set)
739 * This file is created during normal ASL/AML compiles. It is not
740 * created by the data table compiler.
742 * If the -ls flag is set, then the .SRC file should not be deleted.
743 * In this case, Gbl_SourceOutputFlag is set to TRUE.
745 * Note: Handles are cleared by FlCloseFile above, so we look at the
746 * filename instead, to determine if the .SRC file was actually
749 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
751 if (!Gbl_SourceOutputFlag)
753 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
756 /* Final cleanup after compiling one file */
762 /*******************************************************************************
764 * FUNCTION: CmDeleteCaches
770 * DESCRIPTION: Delete all local cache buffer blocks
772 ******************************************************************************/
779 ASL_CACHE_INFO *Next;
785 while (Gbl_ParseOpCacheList)
787 Next = Gbl_ParseOpCacheList->Next;
788 ACPI_FREE (Gbl_ParseOpCacheList);
789 Gbl_ParseOpCacheList = Next;
793 DbgPrint (ASL_DEBUG_OUTPUT,
794 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
795 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
796 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
798 Gbl_ParseOpCount = 0;
799 Gbl_ParseOpCacheNext = NULL;
800 Gbl_ParseOpCacheLast = NULL;
803 /* Generic string cache */
806 while (Gbl_StringCacheList)
808 Next = Gbl_StringCacheList->Next;
809 ACPI_FREE (Gbl_StringCacheList);
810 Gbl_StringCacheList = Next;
814 DbgPrint (ASL_DEBUG_OUTPUT,
815 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
816 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
820 Gbl_StringCacheNext = NULL;
821 Gbl_StringCacheLast = NULL;