1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
46 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acapps.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 parser-detected syntax errors */
125 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
130 /* Did the parse tree get successfully constructed? */
135 * If there are no errors, then we have some sort of
138 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
139 NULL, "- Could not resolve parse tree root node");
144 /* Flush out any remaining source after parse tree is complete */
146 Event = UtBeginEvent ("Flush source input");
147 CmFlushSourceCode ();
149 /* Prune the parse tree if requested (debug purposes only) */
151 if (Gbl_PruneParseTree)
153 AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
156 /* Optional parse tree dump, compiler debug output only */
160 OpcGetIntegerWidth (RootNode);
163 /* Pre-process parse tree for any operator transforms */
165 Event = UtBeginEvent ("Parse tree transforms");
166 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
167 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
168 TrAmlTransformWalk, NULL, NULL);
171 /* Generate AML opcodes corresponding to the parse tokens */
173 Event = UtBeginEvent ("Generate AML opcodes");
174 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
175 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
176 OpcAmlOpcodeWalk, NULL);
180 * Now that the input is parsed, we can open the AML output file.
181 * Note: by default, the name of this file comes from the table descriptor
182 * within the input file.
184 Event = UtBeginEvent ("Open AML output file");
185 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
187 if (ACPI_FAILURE (Status))
189 AePrintErrorLog (ASL_FILE_STDERR);
193 /* Interpret and generate all compile-time constants */
195 Event = UtBeginEvent ("Constant folding via AML interpreter");
196 DbgPrint (ASL_DEBUG_OUTPUT,
197 "\nInterpreting compile-time constant expressions\n\n");
199 if (Gbl_FoldConstants)
201 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
202 OpcAmlConstantWalk, NULL, NULL);
206 DbgPrint (ASL_PARSE_OUTPUT, " Optional folding disabled\n");
210 /* Update AML opcodes if necessary, after constant folding */
212 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
213 DbgPrint (ASL_DEBUG_OUTPUT,
214 "\nUpdating AML opcodes after constant folding\n\n");
215 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
216 NULL, OpcAmlOpcodeUpdateWalk, NULL);
219 /* Calculate all AML package lengths */
221 Event = UtBeginEvent ("Generate AML package lengths");
222 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
223 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
224 LnPackageLengthWalk, NULL);
227 if (Gbl_ParseOnlyFlag)
229 AePrintErrorLog (ASL_FILE_STDERR);
230 UtDisplaySummary (ASL_FILE_STDERR);
233 /* Print error summary to the stdout also */
235 AePrintErrorLog (ASL_FILE_STDOUT);
236 UtDisplaySummary (ASL_FILE_STDOUT);
238 UtEndEvent (FullCompile);
243 * Create an internal namespace and use it as a symbol table
246 /* Namespace loading */
248 Event = UtBeginEvent ("Create ACPI Namespace");
249 Status = LdLoadNamespace (RootNode);
251 if (ACPI_FAILURE (Status))
256 /* Namespace cross-reference */
258 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
259 Status = XfCrossReferenceNamespace ();
260 if (ACPI_FAILURE (Status))
265 /* Namespace - Check for non-referenced objects */
267 LkFindUnreferencedObjects ();
268 UtEndEvent (AslGbl_NamespaceEvent);
271 * Semantic analysis. This can happen only after the
272 * namespace has been loaded and cross-referenced.
274 * part one - check control methods
276 Event = UtBeginEvent ("Analyze control method return types");
277 AnalysisWalkInfo.MethodStack = NULL;
279 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
280 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
281 MtMethodAnalysisWalkBegin,
282 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
285 /* Semantic error checking part two - typing of method returns */
287 Event = UtBeginEvent ("Determine object types returned by methods");
288 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
289 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
290 NULL, AnMethodTypingWalkEnd, NULL);
293 /* Semantic error checking part three - operand type checking */
295 Event = UtBeginEvent ("Analyze AML operand types");
296 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
297 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
298 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
301 /* Semantic error checking part four - other miscellaneous checks */
303 Event = UtBeginEvent ("Miscellaneous analysis");
304 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
305 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
306 AnOtherSemanticAnalysisWalkBegin,
307 NULL, &AnalysisWalkInfo);
310 /* Calculate all AML package lengths */
312 Event = UtBeginEvent ("Finish AML package length generation");
313 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
314 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
315 LnInitLengthsWalk, NULL);
316 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
317 LnPackageLengthWalk, NULL);
320 /* Code generation - emit the AML */
322 Event = UtBeginEvent ("Generate AML code and write output files");
323 CgGenerateAmlOutput ();
326 Event = UtBeginEvent ("Write optional output files");
330 UtEndEvent (FullCompile);
335 UtEndEvent (FullCompile);
341 /*******************************************************************************
343 * FUNCTION: AslCompilerSignon
345 * PARAMETERS: FileId - ID of the output file
349 * DESCRIPTION: Display compiler signon
351 ******************************************************************************/
361 /* Set line prefix depending on the destination file type */
365 case ASL_FILE_ASM_SOURCE_OUTPUT:
366 case ASL_FILE_ASM_INCLUDE_OUTPUT:
371 case ASL_FILE_HEX_OUTPUT:
373 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
377 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
378 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
380 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
385 case ASL_FILE_C_SOURCE_OUTPUT:
386 case ASL_FILE_C_OFFSET_OUTPUT:
387 case ASL_FILE_C_INCLUDE_OUTPUT:
394 /* No other output types supported */
399 /* Running compiler or disassembler? */
403 UtilityName = AML_DISASSEMBLER_NAME;
407 UtilityName = ASL_COMPILER_NAME;
410 /* Compiler signon with copyright */
412 FlPrintFile (FileId, "%s\n", Prefix);
413 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
417 /*******************************************************************************
419 * FUNCTION: AslCompilerFileHeader
421 * PARAMETERS: FileId - ID of the output file
425 * DESCRIPTION: Header used at the beginning of output files
427 ******************************************************************************/
430 AslCompilerFileHeader (
438 /* Set line prefix depending on the destination file type */
442 case ASL_FILE_ASM_SOURCE_OUTPUT:
443 case ASL_FILE_ASM_INCLUDE_OUTPUT:
448 case ASL_FILE_HEX_OUTPUT:
450 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
454 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
455 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
461 case ASL_FILE_C_SOURCE_OUTPUT:
462 case ASL_FILE_C_OFFSET_OUTPUT:
463 case ASL_FILE_C_INCLUDE_OUTPUT:
470 /* No other output types supported */
475 /* Compilation header with timestamp */
477 (void) time (&Aclock);
478 NewTime = localtime (&Aclock);
481 "%sCompilation of \"%s\" - %s%s\n",
482 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
487 case ASL_FILE_C_SOURCE_OUTPUT:
488 case ASL_FILE_C_OFFSET_OUTPUT:
489 case ASL_FILE_C_INCLUDE_OUTPUT:
491 FlPrintFile (FileId, " */\n");
496 /* Nothing to do for other output types */
503 /*******************************************************************************
505 * FUNCTION: CmFlushSourceCode
511 * DESCRIPTION: Read in any remaining source code after the parse tree
512 * has been constructed.
514 ******************************************************************************/
523 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
525 AslInsertLineBuffer ((int) Buffer);
528 AslResetCurrentLineBuffer ();
532 /*******************************************************************************
534 * FUNCTION: CmDoOutputFiles
540 * DESCRIPTION: Create all "listing" type files
542 ******************************************************************************/
549 /* Create listings and hex files */
554 /* Dump the namespace to the .nsp file if requested */
556 (void) NsDisplayNamespace ();
558 /* Dump the device mapping file */
560 MpEmitMappingInfo ();
564 /*******************************************************************************
566 * FUNCTION: CmDumpAllEvents
572 * DESCRIPTION: Dump all compiler events
574 ******************************************************************************/
580 ASL_EVENT_INFO *Event;
587 Event = AslGbl_Events;
589 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
590 if (Gbl_CompileTimesFlag)
592 printf ("\nElapsed time for major events\n\n");
595 for (i = 0; i < AslGbl_NextEvent; i++)
599 /* Delta will be in 100-nanosecond units */
601 Delta = (UINT32) (Event->EndTime - Event->StartTime);
603 USec = Delta / ACPI_100NSEC_PER_USEC;
604 MSec = Delta / ACPI_100NSEC_PER_MSEC;
606 /* Round milliseconds up */
608 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
613 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
614 USec, MSec, Event->EventName);
616 if (Gbl_CompileTimesFlag)
618 printf ("%8u usec %8u msec - %s\n",
619 USec, MSec, Event->EventName);
628 /*******************************************************************************
630 * FUNCTION: CmCleanupAndExit
636 * DESCRIPTION: Close all open files and exit the compiler
638 ******************************************************************************/
645 BOOLEAN DeleteAmlFile = FALSE;
648 AePrintErrorLog (ASL_FILE_STDERR);
651 /* Print error summary to stdout also */
653 AePrintErrorLog (ASL_FILE_STDOUT);
656 /* Emit compile times if enabled */
660 if (Gbl_CompileTimesFlag)
662 printf ("\nMiscellaneous compile statistics\n\n");
663 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
664 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
665 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
666 printf ("%11u : %s\n", TotalMethods, "Control methods");
667 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
668 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
669 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
673 if (Gbl_NsLookupCount)
675 DbgPrint (ASL_DEBUG_OUTPUT,
676 "\n\nMiscellaneous compile statistics\n\n");
678 DbgPrint (ASL_DEBUG_OUTPUT,
679 "%32s : %u\n", "Total Namespace searches",
682 DbgPrint (ASL_DEBUG_OUTPUT,
683 "%32s : %u usec\n", "Time per search", ((UINT32)
684 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
685 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
689 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
691 printf ("\nMaximum error count (%u) exceeded\n",
692 ASL_MAX_ERROR_COUNT);
695 UtDisplaySummary (ASL_FILE_STDOUT);
698 * We will delete the AML file if there are errors and the
699 * force AML output option has not been used.
701 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
702 (!Gbl_IgnoreErrors) &&
703 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
705 DeleteAmlFile = TRUE;
708 /* Close all open files */
711 * Take care with the preprocessor file (.i), it might be the same
712 * as the "input" file, depending on where the compiler has terminated
713 * or aborted. Prevent attempt to close the same file twice in
716 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
717 Gbl_Files[ASL_FILE_INPUT].Handle)
719 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
722 /* Close the standard I/O files */
724 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
729 /* Delete AML file if there are errors */
733 FlDeleteFile (ASL_FILE_AML_OUTPUT);
736 /* Delete the preprocessor output file (.i) unless -li flag is set */
738 if (!Gbl_PreprocessorOutputFlag &&
741 FlDeleteFile (ASL_FILE_PREPROCESSOR);
745 * Delete intermediate ("combined") source file (if -ls flag not set)
746 * This file is created during normal ASL/AML compiles. It is not
747 * created by the data table compiler.
749 * If the -ls flag is set, then the .SRC file should not be deleted.
750 * In this case, Gbl_SourceOutputFlag is set to TRUE.
752 * Note: Handles are cleared by FlCloseFile above, so we look at the
753 * filename instead, to determine if the .SRC file was actually
756 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
758 if (!Gbl_SourceOutputFlag)
760 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
763 /* Final cleanup after compiling one file */
769 /*******************************************************************************
771 * FUNCTION: CmDeleteCaches
777 * DESCRIPTION: Delete all local cache buffer blocks
779 ******************************************************************************/
786 ASL_CACHE_INFO *Next;
792 while (Gbl_ParseOpCacheList)
794 Next = Gbl_ParseOpCacheList->Next;
795 ACPI_FREE (Gbl_ParseOpCacheList);
796 Gbl_ParseOpCacheList = Next;
800 DbgPrint (ASL_DEBUG_OUTPUT,
801 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
802 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
803 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
805 Gbl_ParseOpCount = 0;
806 Gbl_ParseOpCacheNext = NULL;
807 Gbl_ParseOpCacheLast = NULL;
810 /* Generic string cache */
813 while (Gbl_StringCacheList)
815 Next = Gbl_StringCacheList->Next;
816 ACPI_FREE (Gbl_StringCacheList);
817 Gbl_StringCacheList = Next;
821 DbgPrint (ASL_DEBUG_OUTPUT,
822 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
823 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
827 Gbl_StringCacheNext = NULL;
828 Gbl_StringCacheLast = NULL;