2 /******************************************************************************
4 * Module Name: aslcompile - top level compile module
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <contrib/dev/acpica/compiler/aslcompiler.h>
49 #include <contrib/dev/acpica/include/acapps.h>
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslcompile")
54 /* Local prototypes */
61 FlConsumeAnsiComment (
62 ASL_FILE_INFO *FileInfo,
63 ASL_FILE_STATUS *Status);
67 ASL_FILE_INFO *FileInfo,
68 ASL_FILE_STATUS *Status);
71 /*******************************************************************************
73 * FUNCTION: AslCompilerSignon
75 * PARAMETERS: FileId - ID of the output file
79 * DESCRIPTION: Display compiler signon
81 ******************************************************************************/
91 /* Set line prefix depending on the destination file type */
95 case ASL_FILE_ASM_SOURCE_OUTPUT:
96 case ASL_FILE_ASM_INCLUDE_OUTPUT:
101 case ASL_FILE_HEX_OUTPUT:
103 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
107 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
108 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
110 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
115 case ASL_FILE_C_SOURCE_OUTPUT:
116 case ASL_FILE_C_INCLUDE_OUTPUT:
122 /* No other output types supported */
126 /* Running compiler or disassembler? */
130 UtilityName = AML_DISASSEMBLER_NAME;
134 UtilityName = ASL_COMPILER_NAME;
137 /* Compiler signon with copyright */
139 FlPrintFile (FileId, "%s\n", Prefix);
140 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
144 /*******************************************************************************
146 * FUNCTION: AslCompilerFileHeader
148 * PARAMETERS: FileId - ID of the output file
152 * DESCRIPTION: Header used at the beginning of output files
154 ******************************************************************************/
157 AslCompilerFileHeader (
165 /* Set line prefix depending on the destination file type */
169 case ASL_FILE_ASM_SOURCE_OUTPUT:
170 case ASL_FILE_ASM_INCLUDE_OUTPUT:
175 case ASL_FILE_HEX_OUTPUT:
177 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
181 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
182 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
188 case ASL_FILE_C_SOURCE_OUTPUT:
189 case ASL_FILE_C_INCLUDE_OUTPUT:
195 /* No other output types supported */
199 /* Compilation header with timestamp */
201 (void) time (&Aclock);
202 NewTime = localtime (&Aclock);
205 "%sCompilation of \"%s\" - %s%s\n",
206 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
211 case ASL_FILE_C_SOURCE_OUTPUT:
212 case ASL_FILE_C_INCLUDE_OUTPUT:
213 FlPrintFile (FileId, " */\n");
217 /* Nothing to do for other output types */
223 /*******************************************************************************
225 * FUNCTION: CmFlushSourceCode
231 * DESCRIPTION: Read in any remaining source code after the parse tree
232 * has been constructed.
234 ******************************************************************************/
243 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
245 InsertLineBuffer ((int) Buffer);
248 ResetCurrentLineBuffer ();
252 /*******************************************************************************
254 * FUNCTION: FlConsume*
256 * PARAMETERS: FileInfo - Points to an open input file
258 * RETURN: Number of lines consumed
260 * DESCRIPTION: Step over both types of comment during check for ascii chars
262 ******************************************************************************/
265 FlConsumeAnsiComment (
266 ASL_FILE_INFO *FileInfo,
267 ASL_FILE_STATUS *Status)
270 BOOLEAN ClosingComment = FALSE;
273 while (fread (&Byte, 1, 1, FileInfo->Handle))
275 /* Scan until comment close is found */
288 ClosingComment = FALSE;
291 else if (Byte == '*')
293 ClosingComment = TRUE;
296 /* Maintain line count */
309 FlConsumeNewComment (
310 ASL_FILE_INFO *FileInfo,
311 ASL_FILE_STATUS *Status)
316 while (fread (&Byte, 1, 1, FileInfo->Handle))
320 /* Comment ends at newline */
331 /*******************************************************************************
333 * FUNCTION: FlCheckForAscii
335 * PARAMETERS: FileInfo - Points to an open input file
339 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
340 * within comments. Note: does not handle nested comments and does
341 * not handle comment delimiters within string literals. However,
342 * on the rare chance this happens and an invalid character is
343 * missed, the parser will catch the error by failing in some
344 * spectactular manner.
346 ******************************************************************************/
350 ASL_FILE_INFO *FileInfo)
353 ACPI_SIZE BadBytes = 0;
354 BOOLEAN OpeningComment = FALSE;
355 ASL_FILE_STATUS Status;
361 /* Read the entire file */
363 while (fread (&Byte, 1, 1, FileInfo->Handle))
365 /* Ignore comment fields (allow non-ascii within) */
369 /* Check for second comment open delimiter */
373 FlConsumeAnsiComment (FileInfo, &Status);
378 FlConsumeNewComment (FileInfo, &Status);
383 OpeningComment = FALSE;
385 else if (Byte == '/')
387 OpeningComment = TRUE;
390 /* Check for an ASCII character */
392 if (!ACPI_IS_ASCII (Byte))
397 "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
398 Byte, Status.Line, Status.Offset);
404 /* Update line counter */
406 else if (Byte == 0x0A)
414 /* Seek back to the beginning of the source file */
416 fseek (FileInfo->Handle, 0, SEEK_SET);
418 /* Were there any non-ASCII characters in the file? */
423 "%u non-ASCII characters found in input source text, could be a binary file\n",
425 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, FileInfo->Filename);
426 return (AE_BAD_CHARACTER);
435 /*******************************************************************************
437 * FUNCTION: CmDoCompile
441 * RETURN: Status (0 = OK)
443 * DESCRIPTION: This procedure performs the entire compile
445 ******************************************************************************/
456 FullCompile = UtBeginEvent ("*** Total Compile time ***");
457 Event = UtBeginEvent ("Open input and output files");
460 /* Build the parse tree */
462 Event = UtBeginEvent ("Parse source code and build parse tree");
466 /* Flush out any remaining source after parse tree is complete */
468 Event = UtBeginEvent ("Flush source input");
469 CmFlushSourceCode ();
471 /* Did the parse tree get successfully constructed? */
479 /* Optional parse tree dump, compiler debug output only */
483 OpcGetIntegerWidth (RootNode);
486 /* Pre-process parse tree for any operator transforms */
488 Event = UtBeginEvent ("Parse tree transforms");
489 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
490 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
491 TrAmlTransformWalk, NULL, NULL);
494 /* Generate AML opcodes corresponding to the parse tokens */
496 Event = UtBeginEvent ("Generate AML opcodes");
497 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
498 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
499 OpcAmlOpcodeWalk, NULL);
503 * Now that the input is parsed, we can open the AML output file.
504 * Note: by default, the name of this file comes from the table descriptor
505 * within the input file.
507 Event = UtBeginEvent ("Open AML output file");
508 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
509 if (ACPI_FAILURE (Status))
511 AePrintErrorLog (ASL_FILE_STDERR);
516 /* Interpret and generate all compile-time constants */
518 Event = UtBeginEvent ("Constant folding via AML interpreter");
519 DbgPrint (ASL_DEBUG_OUTPUT,
520 "\nInterpreting compile-time constant expressions\n\n");
521 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
522 OpcAmlConstantWalk, NULL, NULL);
525 /* Update AML opcodes if necessary, after constant folding */
527 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
528 DbgPrint (ASL_DEBUG_OUTPUT,
529 "\nUpdating AML opcodes after constant folding\n\n");
530 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
531 NULL, OpcAmlOpcodeUpdateWalk, NULL);
534 /* Calculate all AML package lengths */
536 Event = UtBeginEvent ("Generate AML package lengths");
537 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
538 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
539 LnPackageLengthWalk, NULL);
542 if (Gbl_ParseOnlyFlag)
544 AePrintErrorLog (ASL_FILE_STDOUT);
545 UtDisplaySummary (ASL_FILE_STDOUT);
548 /* Print error summary to the debug file */
550 AePrintErrorLog (ASL_FILE_STDERR);
551 UtDisplaySummary (ASL_FILE_STDERR);
557 * Create an internal namespace and use it as a symbol table
560 /* Namespace loading */
562 Event = UtBeginEvent ("Create ACPI Namespace");
563 Status = LdLoadNamespace (RootNode);
565 if (ACPI_FAILURE (Status))
570 /* Namespace cross-reference */
572 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
573 Status = LkCrossReferenceNamespace ();
574 if (ACPI_FAILURE (Status))
579 /* Namespace - Check for non-referenced objects */
581 LkFindUnreferencedObjects ();
582 UtEndEvent (AslGbl_NamespaceEvent);
585 * Semantic analysis. This can happen only after the
586 * namespace has been loaded and cross-referenced.
588 * part one - check control methods
590 Event = UtBeginEvent ("Analyze control method return types");
591 AnalysisWalkInfo.MethodStack = NULL;
593 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
594 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
595 AnMethodAnalysisWalkBegin,
596 AnMethodAnalysisWalkEnd, &AnalysisWalkInfo);
599 /* Semantic error checking part two - typing of method returns */
601 Event = UtBeginEvent ("Determine object types returned by methods");
602 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
603 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
604 NULL, AnMethodTypingWalkEnd, NULL);
607 /* Semantic error checking part three - operand type checking */
609 Event = UtBeginEvent ("Analyze AML operand types");
610 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
611 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
612 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
615 /* Semantic error checking part four - other miscellaneous checks */
617 Event = UtBeginEvent ("Miscellaneous analysis");
618 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
619 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
620 AnOtherSemanticAnalysisWalkBegin,
621 NULL, &AnalysisWalkInfo);
624 /* Calculate all AML package lengths */
626 Event = UtBeginEvent ("Finish AML package length generation");
627 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
628 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
629 LnInitLengthsWalk, NULL);
630 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
631 LnPackageLengthWalk, NULL);
634 /* Code generation - emit the AML */
636 Event = UtBeginEvent ("Generate AML code and write output files");
637 CgGenerateAmlOutput ();
640 Event = UtBeginEvent ("Write optional output files");
644 UtEndEvent (FullCompile);
650 /*******************************************************************************
652 * FUNCTION: CmDoOutputFiles
658 * DESCRIPTION: Create all "listing" type files
660 ******************************************************************************/
667 /* Create listings and hex files */
672 /* Dump the namespace to the .nsp file if requested */
674 (void) LsDisplayNamespace ();
678 /*******************************************************************************
680 * FUNCTION: CmDumpEvent
682 * PARAMETERS: Event - A compiler event struct
686 * DESCRIPTION: Dump a compiler event struct
688 ******************************************************************************/
692 ASL_EVENT_INFO *Event)
703 /* Delta will be in 100-nanosecond units */
705 Delta = (UINT32) (Event->EndTime - Event->StartTime);
708 MSec = Delta / 10000;
710 /* Round milliseconds up */
712 if ((USec - (MSec * 1000)) >= 500)
717 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
718 USec, MSec, Event->EventName);
722 /*******************************************************************************
724 * FUNCTION: CmCleanupAndExit
730 * DESCRIPTION: Close all open files and exit the compiler
732 ******************************************************************************/
741 AePrintErrorLog (ASL_FILE_STDOUT);
744 /* Print error summary to the debug file */
746 AePrintErrorLog (ASL_FILE_STDERR);
749 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
750 for (i = 0; i < AslGbl_NextEvent; i++)
752 CmDumpEvent (&AslGbl_Events[i]);
755 if (Gbl_CompileTimesFlag)
757 printf ("\nElapsed time for major events\n\n");
758 for (i = 0; i < AslGbl_NextEvent; i++)
760 CmDumpEvent (&AslGbl_Events[i]);
763 printf ("\nMiscellaneous compile statistics\n\n");
764 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
765 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
766 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
767 printf ("%11u : %s\n", TotalMethods, "Control methods");
768 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
769 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
770 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
774 if (Gbl_NsLookupCount)
776 DbgPrint (ASL_DEBUG_OUTPUT,
777 "\n\nMiscellaneous compile statistics\n\n");
779 DbgPrint (ASL_DEBUG_OUTPUT,
780 "%32s : %u\n", "Total Namespace searches",
783 DbgPrint (ASL_DEBUG_OUTPUT,
784 "%32s : %u usec\n", "Time per search", ((UINT32)
785 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
786 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
790 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
792 printf ("\nMaximum error count (%u) exceeded\n",
793 ASL_MAX_ERROR_COUNT);
796 UtDisplaySummary (ASL_FILE_STDOUT);
798 /* Close all open files */
800 for (i = 2; i < ASL_MAX_FILE_TYPE; i++)
805 /* Delete AML file if there are errors */
807 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors) &&
808 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
810 if (remove (Gbl_Files[ASL_FILE_AML_OUTPUT].Filename))
813 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename);
814 perror ("Could not delete AML file");
819 * Delete intermediate ("combined") source file (if -ls flag not set)
820 * This file is created during normal ASL/AML compiles. It is not
821 * created by the data table compiler.
823 * If the -ls flag is set, then the .SRC file should not be deleted.
824 * In this case, Gbl_SourceOutputFlag is set to TRUE.
826 * Note: Handles are cleared by FlCloseFile above, so we look at the
827 * filename instead, to determine if the .SRC file was actually
830 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
832 if (!Gbl_SourceOutputFlag && Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename)
834 if (remove (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename))
837 Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);
838 perror ("Could not delete SRC file");