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)
103 /* Enter compiler name as a #define */
105 PrAddDefine (ASL_DEFINE, "", FALSE);
110 Gbl_CurrentLineNumber = 1;
111 Gbl_LogicalLineNumber = 1;
113 if (Gbl_PreprocessOnly)
123 /* Build the parse tree */
125 Event = UtBeginEvent ("Parse source code and build parse tree");
129 /* Check for parser-detected syntax errors */
133 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
138 /* Did the parse tree get successfully constructed? */
143 * If there are no errors, then we have some sort of
146 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
147 NULL, "- Could not resolve parse tree root node");
152 /* Flush out any remaining source after parse tree is complete */
154 Event = UtBeginEvent ("Flush source input");
155 CmFlushSourceCode ();
157 /* Prune the parse tree if requested (debug purposes only) */
159 if (Gbl_PruneParseTree)
161 AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
164 /* Optional parse tree dump, compiler debug output only */
168 OpcGetIntegerWidth (RootNode);
171 /* Pre-process parse tree for any operator transforms */
173 Event = UtBeginEvent ("Parse tree transforms");
174 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
175 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
176 TrAmlTransformWalk, NULL, NULL);
179 /* Generate AML opcodes corresponding to the parse tokens */
181 Event = UtBeginEvent ("Generate AML opcodes");
182 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
183 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
184 OpcAmlOpcodeWalk, NULL);
188 * Now that the input is parsed, we can open the AML output file.
189 * Note: by default, the name of this file comes from the table descriptor
190 * within the input file.
192 Event = UtBeginEvent ("Open AML output file");
193 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
195 if (ACPI_FAILURE (Status))
197 AePrintErrorLog (ASL_FILE_STDERR);
201 /* Interpret and generate all compile-time constants */
203 Event = UtBeginEvent ("Constant folding via AML interpreter");
204 DbgPrint (ASL_DEBUG_OUTPUT,
205 "\nInterpreting compile-time constant expressions\n\n");
207 if (Gbl_FoldConstants)
209 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
210 OpcAmlConstantWalk, NULL, NULL);
214 DbgPrint (ASL_PARSE_OUTPUT, " Optional folding disabled\n");
218 /* Update AML opcodes if necessary, after constant folding */
220 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
221 DbgPrint (ASL_DEBUG_OUTPUT,
222 "\nUpdating AML opcodes after constant folding\n\n");
223 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
224 NULL, OpcAmlOpcodeUpdateWalk, NULL);
227 /* Calculate all AML package lengths */
229 Event = UtBeginEvent ("Generate AML package lengths");
230 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
231 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
232 LnPackageLengthWalk, NULL);
235 if (Gbl_ParseOnlyFlag)
237 AePrintErrorLog (ASL_FILE_STDERR);
238 UtDisplaySummary (ASL_FILE_STDERR);
241 /* Print error summary to the stdout also */
243 AePrintErrorLog (ASL_FILE_STDOUT);
244 UtDisplaySummary (ASL_FILE_STDOUT);
246 UtEndEvent (FullCompile);
251 * Create an internal namespace and use it as a symbol table
254 /* Namespace loading */
256 Event = UtBeginEvent ("Create ACPI Namespace");
257 Status = LdLoadNamespace (RootNode);
259 if (ACPI_FAILURE (Status))
264 /* Namespace cross-reference */
266 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
267 Status = XfCrossReferenceNamespace ();
268 if (ACPI_FAILURE (Status))
273 /* Namespace - Check for non-referenced objects */
275 LkFindUnreferencedObjects ();
276 UtEndEvent (AslGbl_NamespaceEvent);
279 * Semantic analysis. This can happen only after the
280 * namespace has been loaded and cross-referenced.
282 * part one - check control methods
284 Event = UtBeginEvent ("Analyze control method return types");
285 AnalysisWalkInfo.MethodStack = NULL;
287 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
288 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
289 MtMethodAnalysisWalkBegin,
290 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
293 /* Semantic error checking part two - typing of method returns */
295 Event = UtBeginEvent ("Determine object types returned by methods");
296 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
297 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
298 NULL, AnMethodTypingWalkEnd, NULL);
301 /* Semantic error checking part three - operand type checking */
303 Event = UtBeginEvent ("Analyze AML operand types");
304 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
305 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
306 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
309 /* Semantic error checking part four - other miscellaneous checks */
311 Event = UtBeginEvent ("Miscellaneous analysis");
312 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
313 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
314 AnOtherSemanticAnalysisWalkBegin,
315 NULL, &AnalysisWalkInfo);
318 /* Calculate all AML package lengths */
320 Event = UtBeginEvent ("Finish AML package length generation");
321 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
322 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
323 LnInitLengthsWalk, NULL);
324 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
325 LnPackageLengthWalk, NULL);
328 /* Code generation - emit the AML */
330 Event = UtBeginEvent ("Generate AML code and write output files");
331 CgGenerateAmlOutput ();
334 Event = UtBeginEvent ("Write optional output files");
338 UtEndEvent (FullCompile);
343 UtEndEvent (FullCompile);
349 /*******************************************************************************
351 * FUNCTION: AslCompilerSignon
353 * PARAMETERS: FileId - ID of the output file
357 * DESCRIPTION: Display compiler signon
359 ******************************************************************************/
369 /* Set line prefix depending on the destination file type */
373 case ASL_FILE_ASM_SOURCE_OUTPUT:
374 case ASL_FILE_ASM_INCLUDE_OUTPUT:
379 case ASL_FILE_HEX_OUTPUT:
381 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
385 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
386 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
388 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
393 case ASL_FILE_C_SOURCE_OUTPUT:
394 case ASL_FILE_C_OFFSET_OUTPUT:
395 case ASL_FILE_C_INCLUDE_OUTPUT:
402 /* No other output types supported */
407 /* Running compiler or disassembler? */
411 UtilityName = AML_DISASSEMBLER_NAME;
415 UtilityName = ASL_COMPILER_NAME;
418 /* Compiler signon with copyright */
420 FlPrintFile (FileId, "%s\n", Prefix);
421 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
425 /*******************************************************************************
427 * FUNCTION: AslCompilerFileHeader
429 * PARAMETERS: FileId - ID of the output file
433 * DESCRIPTION: Header used at the beginning of output files
435 ******************************************************************************/
438 AslCompilerFileHeader (
446 /* Set line prefix depending on the destination file type */
450 case ASL_FILE_ASM_SOURCE_OUTPUT:
451 case ASL_FILE_ASM_INCLUDE_OUTPUT:
456 case ASL_FILE_HEX_OUTPUT:
458 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
462 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
463 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
469 case ASL_FILE_C_SOURCE_OUTPUT:
470 case ASL_FILE_C_OFFSET_OUTPUT:
471 case ASL_FILE_C_INCLUDE_OUTPUT:
478 /* No other output types supported */
483 /* Compilation header with timestamp */
485 (void) time (&Aclock);
486 NewTime = localtime (&Aclock);
489 "%sCompilation of \"%s\" - %s%s\n",
490 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
495 case ASL_FILE_C_SOURCE_OUTPUT:
496 case ASL_FILE_C_OFFSET_OUTPUT:
497 case ASL_FILE_C_INCLUDE_OUTPUT:
499 FlPrintFile (FileId, " */\n");
504 /* Nothing to do for other output types */
511 /*******************************************************************************
513 * FUNCTION: CmFlushSourceCode
519 * DESCRIPTION: Read in any remaining source code after the parse tree
520 * has been constructed.
522 ******************************************************************************/
531 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
533 AslInsertLineBuffer ((int) Buffer);
536 AslResetCurrentLineBuffer ();
540 /*******************************************************************************
542 * FUNCTION: CmDoOutputFiles
548 * DESCRIPTION: Create all "listing" type files
550 ******************************************************************************/
557 /* Create listings and hex files */
562 /* Dump the namespace to the .nsp file if requested */
564 (void) NsDisplayNamespace ();
566 /* Dump the device mapping file */
568 MpEmitMappingInfo ();
572 /*******************************************************************************
574 * FUNCTION: CmDumpAllEvents
580 * DESCRIPTION: Dump all compiler events
582 ******************************************************************************/
588 ASL_EVENT_INFO *Event;
595 Event = AslGbl_Events;
597 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
598 if (Gbl_CompileTimesFlag)
600 printf ("\nElapsed time for major events\n\n");
603 for (i = 0; i < AslGbl_NextEvent; i++)
607 /* Delta will be in 100-nanosecond units */
609 Delta = (UINT32) (Event->EndTime - Event->StartTime);
611 USec = Delta / ACPI_100NSEC_PER_USEC;
612 MSec = Delta / ACPI_100NSEC_PER_MSEC;
614 /* Round milliseconds up */
616 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
621 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
622 USec, MSec, Event->EventName);
624 if (Gbl_CompileTimesFlag)
626 printf ("%8u usec %8u msec - %s\n",
627 USec, MSec, Event->EventName);
636 /*******************************************************************************
638 * FUNCTION: CmCleanupAndExit
644 * DESCRIPTION: Close all open files and exit the compiler
646 ******************************************************************************/
653 BOOLEAN DeleteAmlFile = FALSE;
656 AePrintErrorLog (ASL_FILE_STDERR);
659 /* Print error summary to stdout also */
661 AePrintErrorLog (ASL_FILE_STDOUT);
664 /* Emit compile times if enabled */
668 if (Gbl_CompileTimesFlag)
670 printf ("\nMiscellaneous compile statistics\n\n");
671 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
672 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
673 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
674 printf ("%11u : %s\n", TotalMethods, "Control methods");
675 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
676 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
677 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
681 if (Gbl_NsLookupCount)
683 DbgPrint (ASL_DEBUG_OUTPUT,
684 "\n\nMiscellaneous compile statistics\n\n");
686 DbgPrint (ASL_DEBUG_OUTPUT,
687 "%32s : %u\n", "Total Namespace searches",
690 DbgPrint (ASL_DEBUG_OUTPUT,
691 "%32s : %u usec\n", "Time per search", ((UINT32)
692 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
693 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
697 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
699 printf ("\nMaximum error count (%u) exceeded\n",
700 ASL_MAX_ERROR_COUNT);
703 UtDisplaySummary (ASL_FILE_STDOUT);
706 * We will delete the AML file if there are errors and the
707 * force AML output option has not been used.
709 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
710 (!Gbl_IgnoreErrors) &&
711 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
713 DeleteAmlFile = TRUE;
716 /* Close all open files */
719 * Take care with the preprocessor file (.pre), it might be the same
720 * as the "input" file, depending on where the compiler has terminated
721 * or aborted. Prevent attempt to close the same file twice in
724 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
725 Gbl_Files[ASL_FILE_INPUT].Handle)
727 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
730 /* Close the standard I/O files */
732 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
737 /* Delete AML file if there are errors */
741 FlDeleteFile (ASL_FILE_AML_OUTPUT);
744 /* Delete the preprocessor temp file unless full debug was specified */
746 if (Gbl_PreprocessFlag && !Gbl_KeepPreprocessorTempFile)
748 FlDeleteFile (ASL_FILE_PREPROCESSOR);
752 * Delete intermediate ("combined") source file (if -ls flag not set)
753 * This file is created during normal ASL/AML compiles. It is not
754 * created by the data table compiler.
756 * If the -ls flag is set, then the .SRC file should not be deleted.
757 * In this case, Gbl_SourceOutputFlag is set to TRUE.
759 * Note: Handles are cleared by FlCloseFile above, so we look at the
760 * filename instead, to determine if the .SRC file was actually
763 if (!Gbl_SourceOutputFlag)
765 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
768 /* Final cleanup after compiling one file */
774 /*******************************************************************************
776 * FUNCTION: CmDeleteCaches
782 * DESCRIPTION: Delete all local cache buffer blocks
784 ******************************************************************************/
791 ASL_CACHE_INFO *Next;
797 while (Gbl_ParseOpCacheList)
799 Next = Gbl_ParseOpCacheList->Next;
800 ACPI_FREE (Gbl_ParseOpCacheList);
801 Gbl_ParseOpCacheList = Next;
805 DbgPrint (ASL_DEBUG_OUTPUT,
806 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
807 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
808 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
810 Gbl_ParseOpCount = 0;
811 Gbl_ParseOpCacheNext = NULL;
812 Gbl_ParseOpCacheLast = NULL;
815 /* Generic string cache */
818 while (Gbl_StringCacheList)
820 Next = Gbl_StringCacheList->Next;
821 ACPI_FREE (Gbl_StringCacheList);
822 Gbl_StringCacheList = Next;
826 DbgPrint (ASL_DEBUG_OUTPUT,
827 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
828 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
832 Gbl_StringCacheNext = NULL;
833 Gbl_StringCacheLast = NULL;