aslcompile.c revision 123315
1
2/******************************************************************************
3 *
4 * Module Name: aslcompile - top level compile module
5 *              $Revision: 72 $
6 *
7 *****************************************************************************/
8
9/******************************************************************************
10 *
11 * 1. Copyright Notice
12 *
13 * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
14 * All rights reserved.
15 *
16 * 2. License
17 *
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights.  You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
22 *
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
29 *
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code.  No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
38 *
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
41 *
42 * 3. Conditions
43 *
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision.  In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change.  Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee.  Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
55 *
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution.  In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
67 *
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
73 *
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
76 *
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
81 *
82 * 4. Disclaimer and Export Compliance
83 *
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
91 *
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
100 *
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government.  In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
115 *
116 *****************************************************************************/
117
118#include <stdio.h>
119#include "aslcompiler.h"
120
121#define _COMPONENT          ACPI_COMPILER
122        ACPI_MODULE_NAME    ("aslcompile")
123
124
125/*******************************************************************************
126 *
127 * FUNCTION:    AslCompilerSignon
128 *
129 * PARAMETERS:  FileId      - ID of the output file
130 *
131 * RETURN:      None
132 *
133 * DESCRIPTION: Display compiler signon
134 *
135 ******************************************************************************/
136
137void
138AslCompilerSignon (
139    UINT32                  FileId)
140{
141    char                    *Prefix = "";
142
143
144    /*
145     * Set line prefix depending on the destination file type
146     */
147    switch (FileId)
148    {
149    case ASL_FILE_ASM_SOURCE_OUTPUT:
150    case ASL_FILE_ASM_INCLUDE_OUTPUT:
151
152        Prefix = "; ";
153        break;
154
155    case ASL_FILE_HEX_OUTPUT:
156
157        if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
158        {
159            Prefix = "; ";
160        }
161        else if (Gbl_HexOutputFlag == HEX_OUTPUT_C)
162        {
163            FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
164            Prefix = " * ";
165        }
166        break;
167
168    case ASL_FILE_C_SOURCE_OUTPUT:
169    case ASL_FILE_C_INCLUDE_OUTPUT:
170
171        Prefix = " * ";
172        break;
173
174    default:
175        /* No other output types supported */
176        break;
177    }
178
179    /* Compiler signon with copyright */
180
181    FlPrintFile (FileId,
182        "%s\n%s%s\n%s%s version %X [%s]\n%s%s\n%sSupports ACPI Specification Revision 2.0c\n%s\n",
183        Prefix,
184        Prefix, IntelAcpiCA,
185        Prefix, CompilerId, ACPI_CA_VERSION, __DATE__,
186        Prefix, CompilerCopyright,
187        Prefix,
188        Prefix);
189}
190
191
192/*******************************************************************************
193 *
194 * FUNCTION:    AslCompilerFileHeader
195 *
196 * PARAMETERS:  FileId      - ID of the output file
197 *
198 * RETURN:      None
199 *
200 * DESCRIPTION: Header used at the beginning of output files
201 *
202 ******************************************************************************/
203
204void
205AslCompilerFileHeader (
206    UINT32                  FileId)
207{
208    struct tm               *NewTime;
209    time_t                  Aclock;
210    char                    *Prefix = "";
211
212
213    /*
214     * Set line prefix depending on the destination file type
215     */
216    switch (FileId)
217    {
218    case ASL_FILE_ASM_SOURCE_OUTPUT:
219    case ASL_FILE_ASM_INCLUDE_OUTPUT:
220
221        Prefix = "; ";
222        break;
223
224    case ASL_FILE_HEX_OUTPUT:
225
226        if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
227        {
228            Prefix = "; ";
229        }
230        else if (Gbl_HexOutputFlag == HEX_OUTPUT_C)
231        {
232            Prefix = " * ";
233        }
234        break;
235
236    case ASL_FILE_C_SOURCE_OUTPUT:
237    case ASL_FILE_C_INCLUDE_OUTPUT:
238
239        Prefix = " * ";
240        break;
241
242    default:
243        /* No other output types supported */
244        break;
245    }
246
247    /* Compilation header with timestamp */
248
249    (void) time (&Aclock);
250    NewTime = localtime (&Aclock);
251
252    FlPrintFile (FileId,
253        "%sCompilation of \"%s\" - %s%s\n",
254        Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
255        Prefix);
256
257    switch (FileId)
258    {
259    case ASL_FILE_C_SOURCE_OUTPUT:
260    case ASL_FILE_C_INCLUDE_OUTPUT:
261        FlPrintFile (FileId, " */\n");
262        break;
263
264    default:
265        /* Nothing to do for other output types */
266        break;
267    }
268}
269
270
271/*******************************************************************************
272 *
273 * FUNCTION:    CmFlushSourceCode
274 *
275 * PARAMETERS:  None
276 *
277 * RETURN:      None
278 *
279 * DESCRIPTION: Read in any remaining source code after the parse tree
280 *              has been constructed.
281 *
282 ******************************************************************************/
283
284void
285CmFlushSourceCode (void)
286{
287    char                    Buffer;
288
289
290    while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
291    {
292        InsertLineBuffer ((int) Buffer);
293    }
294
295    ResetCurrentLineBuffer ();
296}
297
298
299/*******************************************************************************
300 *
301 * FUNCTION:    FlCheckForAscii
302 *
303 * PARAMETERS:  FileInfo        - Points to an open input file
304 *
305 * RETURN:      Status (0 = OK)
306 *
307 * DESCRIPTION: Verify that the input file is entirely ASCII.
308 *
309 ******************************************************************************/
310
311ACPI_STATUS
312FlCheckForAscii (
313    ASL_FILE_INFO           *FileInfo)
314{
315    UINT8                   Byte;
316    ACPI_SIZE               BadBytes = 0;
317    ACPI_SIZE               Offset = 0;
318
319
320    /* Read the entire file */
321
322    while (fread (&Byte, 1, 1, FileInfo->Handle))
323    {
324        /* Check for an ASCII character */
325
326        if (!isascii (Byte))
327        {
328            if (BadBytes < 10)
329            {
330                AcpiOsPrintf ("Non-ASCII character: 0x%2.2X at offset 0x%X\n", Byte, Offset);
331            }
332            BadBytes++;
333        }
334        Offset++;
335    }
336
337    /* Were there any non-ASCII characters in the file? */
338
339    if (BadBytes)
340    {
341        AcpiOsPrintf ("%d non-ASCII characters found in input file, appears to be binary\n", BadBytes);
342        AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, FileInfo->Filename);
343        return (AE_BAD_CHARACTER);
344    }
345
346    /* File is OK, seek back to the beginning */
347
348    fseek (FileInfo->Handle, 0, SEEK_SET);
349    return (AE_OK);
350}
351
352
353/*******************************************************************************
354 *
355 * FUNCTION:    CmDoCompile
356 *
357 * PARAMETERS:  None
358 *
359 * RETURN:      Status (0 = OK)
360 *
361 * DESCRIPTION: This procedure performs the entire compile
362 *
363 ******************************************************************************/
364
365int
366CmDoCompile (void)
367{
368    ACPI_STATUS             Status;
369    UINT32                  i = 0;
370
371
372    UtBeginEvent (12, "Total Compile time");
373    UtBeginEvent (i, "Initialize");
374
375    /* Open the required input and output files */
376
377    Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
378    if (ACPI_FAILURE (Status))
379    {
380        AePrintErrorLog (ASL_FILE_STDERR);
381        return -1;
382    }
383
384    /* Ensure that the input file is 100% ASCII text */
385
386    Status = FlCheckForAscii (&Gbl_Files[ASL_FILE_INPUT]);
387    if (ACPI_FAILURE (Status))
388    {
389        AePrintErrorLog (ASL_FILE_STDERR);
390        return -1;
391    }
392
393    Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
394    if (ACPI_FAILURE (Status))
395    {
396        AePrintErrorLog (ASL_FILE_STDERR);
397        return -1;
398    }
399
400    UtEndEvent (i++);
401
402    /* Build the parse tree */
403
404    UtBeginEvent (i, "Parse source code and build parse tree");
405    AslCompilerparse();
406    UtEndEvent (i++);
407
408    /* Flush out any remaining source after parse tree is complete */
409
410    CmFlushSourceCode ();
411
412    /* Did the parse tree get successfully constructed? */
413
414    if (!RootNode)
415    {
416        CmCleanupAndExit ();
417        return -1;
418    }
419
420    OpcGetIntegerWidth (RootNode);
421
422    /* Pre-process parse tree for any operator transforms */
423
424    UtBeginEvent (i, "Generate AML opcodes");
425    DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
426    TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD, TrAmlTransformWalk, NULL, NULL);
427
428    /* Generate AML opcodes corresponding to the parse tokens */
429
430    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
431    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL, OpcAmlOpcodeWalk, NULL);
432    UtEndEvent (i++);
433
434    /*
435     * Now that the input is parsed, we can open the AML output file.
436     * Note: by default, the name of this file comes from the table descriptor
437     * within the input file.
438     */
439    Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
440    if (ACPI_FAILURE (Status))
441    {
442        AePrintErrorLog (ASL_FILE_STDERR);
443        return -1;
444    }
445
446    /* Interpret and generate all compile-time constants */
447
448    UtBeginEvent (i, "Constant folding via AML interpreter");
449    DbgPrint (ASL_DEBUG_OUTPUT, "\nInterpreting compile-time constant expressions\n\n");
450    TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD, OpcAmlConstantWalk, NULL, NULL);
451    UtEndEvent (i++);
452
453    /* Calculate all AML package lengths */
454
455    UtBeginEvent (i, "Generate AML package lengths");
456    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
457    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL, LnPackageLengthWalk, NULL);
458    UtEndEvent (i++);
459
460    if (Gbl_ParseOnlyFlag)
461    {
462        AePrintErrorLog (ASL_FILE_STDOUT);
463        UtDisplaySummary (ASL_FILE_STDOUT);
464        if (Gbl_DebugFlag)
465        {
466            /* Print error summary to the debug file */
467
468            AePrintErrorLog (ASL_FILE_STDERR);
469            UtDisplaySummary (ASL_FILE_STDERR);
470        }
471        return 0;
472    }
473
474    /*
475     * Create an internal namespace and use it as a symbol table
476     */
477
478    /* Namespace loading */
479
480    UtBeginEvent (i, "Create ACPI Namespace");
481    Status = LdLoadNamespace (RootNode);
482    UtEndEvent (i++);
483    if (ACPI_FAILURE (Status))
484    {
485        return -1;
486    }
487
488    /* Namespace lookup */
489
490    UtBeginEvent (i, "Cross reference parse tree and Namespace");
491    Status = LkCrossReferenceNamespace ();
492    UtEndEvent (i++);
493    UtEndEvent (i++);
494    if (ACPI_FAILURE (Status))
495    {
496        return -1;
497    }
498
499    /*
500     * Semantic analysis.  This can happen only after the
501     * namespace has been loaded and cross-referenced.
502     *
503     * part one - check control methods
504     */
505    UtBeginEvent (i, "Analyze control method return types");
506    AnalysisWalkInfo.MethodStack = NULL;
507
508    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
509    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE, AnMethodAnalysisWalkBegin,
510                        AnMethodAnalysisWalkEnd, &AnalysisWalkInfo);
511    UtEndEvent (i++);
512
513    /* Semantic error checking part two - typing of method returns */
514
515    UtBeginEvent (i, "Determine object types returned by methods");
516    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing \n\n");
517    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE, AnMethodTypingWalkBegin,
518                        AnMethodTypingWalkEnd, NULL);
519    UtEndEvent (i++);
520
521    /* Semantic error checking part three - operand type checking */
522
523    UtBeginEvent (i, "Analyze AML operand types");
524    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking \n\n");
525    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE, AnOperandTypecheckWalkBegin,
526                        AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
527    UtEndEvent (i++);
528
529    /* Semantic error checking part four - other miscellaneous checks */
530
531    UtBeginEvent (i, "Miscellaneous analysis");
532    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous \n\n");
533    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE, AnOtherSemanticAnalysisWalkBegin,
534                        AnOtherSemanticAnalysisWalkEnd, &AnalysisWalkInfo);
535    UtEndEvent (i++);
536
537    /* Calculate all AML package lengths */
538
539    UtBeginEvent (i, "Finish AML package length generation");
540    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
541    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL, LnInitLengthsWalk, NULL);
542    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL, LnPackageLengthWalk, NULL);
543    UtEndEvent (i++);
544
545
546    /* Code generation - emit the AML */
547
548    UtBeginEvent (i, "Generate AML code and write output files");
549    CgGenerateAmlOutput ();
550    UtEndEvent (i++);
551
552    UtBeginEvent (i, "Write optional output files");
553    CmDoOutputFiles ();
554    UtEndEvent (i++);
555
556    UtEndEvent (13);
557    CmCleanupAndExit ();
558    return 0;
559}
560
561void
562CmDoOutputFiles (void)
563{
564
565    /* Create listings and hex files */
566
567    LsDoListings ();
568    LsDoHexOutput ();
569
570    /* Dump the namespace to the .nsp file if requested */
571
572    LsDisplayNamespace ();
573}
574
575
576/*******************************************************************************
577 *
578 * FUNCTION:    CmCleanupAndExit
579 *
580 * PARAMETERS:  None
581 *
582 * RETURN:      None.
583 *
584 * DESCRIPTION: Close all open files and exit the compiler
585 *
586 ******************************************************************************/
587
588void
589CmCleanupAndExit (void)
590{
591    UINT32                  i;
592
593
594    AePrintErrorLog (ASL_FILE_STDOUT);
595    if (Gbl_DebugFlag)
596    {
597        /* Print error summary to the debug file */
598
599        AePrintErrorLog (ASL_FILE_STDERR);
600    }
601
602    DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
603    for (i = 0; i < 13; i++)
604    {
605        if (AslGbl_Events[i].Valid)
606        {
607            DbgPrint (ASL_DEBUG_OUTPUT, "%8lu ms - %s\n",
608                AslGbl_Events[i].EndTime -
609                AslGbl_Events[i].StartTime,
610                AslGbl_Events[i].EventName);
611        }
612    }
613
614    if (Gbl_CompileTimesFlag)
615    {
616        printf ("\nElapsed time for major events\n\n");
617        for (i = 0; i < 13; i++)
618        {
619            if (AslGbl_Events[i].Valid)
620            {
621                printf ("%8lu ms : %s\n",
622                    AslGbl_Events[i].EndTime -
623                    AslGbl_Events[i].StartTime,
624                    AslGbl_Events[i].EventName);
625            }
626        }
627        printf ("\nMiscellaneous compile statistics\n\n");
628        printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
629        printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
630        printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
631        printf ("%11u : %s\n", TotalMethods, "Control methods");
632        printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
633        printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
634        printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
635        printf ("\n");
636    }
637
638    if (Gbl_NsLookupCount)
639    {
640        DbgPrint (ASL_DEBUG_OUTPUT, "\n\nMiscellaneous compile statistics\n\n");
641        DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d\n", "Total Namespace searches", Gbl_NsLookupCount);
642        DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d\n", "Time per search",
643                        ((UINT32) (AslGbl_Events[7].EndTime - AslGbl_Events[7].StartTime) * 1000) /
644                        Gbl_NsLookupCount);
645    }
646
647    /* Close all open files */
648
649    for (i = 2; i < ASL_MAX_FILE_TYPE; i++)
650    {
651        FlCloseFile (i);
652    }
653
654    /*
655     * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
656     */
657    if (!Gbl_SourceOutputFlag)
658    {
659        unlink (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);
660    }
661
662    /* Delete AML file if there are errors */
663
664    if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors))
665    {
666        unlink (Gbl_Files[ASL_FILE_AML_OUTPUT].Filename);
667    }
668
669    if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
670    {
671        printf ("\nMaximum error count (%d) exceeded.\n", ASL_MAX_ERROR_COUNT);
672    }
673
674    UtDisplaySummary (ASL_FILE_STDOUT);
675    exit (0);
676}
677
678
679