aslcompile.c revision 207344
1118611Snjl
2118611Snjl/******************************************************************************
3118611Snjl *
4118611Snjl * Module Name: aslcompile - top level compile module
5118611Snjl *
6118611Snjl *****************************************************************************/
7118611Snjl
8118611Snjl/******************************************************************************
9118611Snjl *
10118611Snjl * 1. Copyright Notice
11118611Snjl *
12202771Sjkim * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13118611Snjl * All rights reserved.
14118611Snjl *
15118611Snjl * 2. License
16118611Snjl *
17118611Snjl * 2.1. This is your license from Intel Corp. under its intellectual property
18118611Snjl * rights.  You may have additional license terms from the party that provided
19118611Snjl * you this software, covering your right to use that party's intellectual
20118611Snjl * property rights.
21118611Snjl *
22118611Snjl * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23118611Snjl * copy of the source code appearing in this file ("Covered Code") an
24118611Snjl * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25118611Snjl * base code distributed originally by Intel ("Original Intel Code") to copy,
26118611Snjl * make derivatives, distribute, use and display any portion of the Covered
27118611Snjl * Code in any form, with the right to sublicense such rights; and
28118611Snjl *
29118611Snjl * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30118611Snjl * license (with the right to sublicense), under only those claims of Intel
31118611Snjl * patents that are infringed by the Original Intel Code, to make, use, sell,
32118611Snjl * offer to sell, and import the Covered Code and derivative works thereof
33118611Snjl * solely to the minimum extent necessary to exercise the above copyright
34118611Snjl * license, and in no event shall the patent license extend to any additions
35118611Snjl * to or modifications of the Original Intel Code.  No other license or right
36118611Snjl * is granted directly or by implication, estoppel or otherwise;
37118611Snjl *
38118611Snjl * The above copyright and patent license is granted only if the following
39118611Snjl * conditions are met:
40118611Snjl *
41118611Snjl * 3. Conditions
42118611Snjl *
43118611Snjl * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44118611Snjl * Redistribution of source code of any substantial portion of the Covered
45118611Snjl * Code or modification with rights to further distribute source must include
46118611Snjl * the above Copyright Notice, the above License, this list of Conditions,
47118611Snjl * and the following Disclaimer and Export Compliance provision.  In addition,
48118611Snjl * Licensee must cause all Covered Code to which Licensee contributes to
49118611Snjl * contain a file documenting the changes Licensee made to create that Covered
50118611Snjl * Code and the date of any change.  Licensee must include in that file the
51118611Snjl * documentation of any changes made by any predecessor Licensee.  Licensee
52118611Snjl * must include a prominent statement that the modification is derived,
53118611Snjl * directly or indirectly, from Original Intel Code.
54118611Snjl *
55118611Snjl * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56118611Snjl * Redistribution of source code of any substantial portion of the Covered
57118611Snjl * Code or modification without rights to further distribute source must
58118611Snjl * include the following Disclaimer and Export Compliance provision in the
59118611Snjl * documentation and/or other materials provided with distribution.  In
60118611Snjl * addition, Licensee may not authorize further sublicense of source of any
61118611Snjl * portion of the Covered Code, and must include terms to the effect that the
62118611Snjl * license from Licensee to its licensee is limited to the intellectual
63118611Snjl * property embodied in the software Licensee provides to its licensee, and
64118611Snjl * not to intellectual property embodied in modifications its licensee may
65118611Snjl * make.
66118611Snjl *
67118611Snjl * 3.3. Redistribution of Executable. Redistribution in executable form of any
68118611Snjl * substantial portion of the Covered Code or modification must reproduce the
69118611Snjl * above Copyright Notice, and the following Disclaimer and Export Compliance
70118611Snjl * provision in the documentation and/or other materials provided with the
71118611Snjl * distribution.
72118611Snjl *
73118611Snjl * 3.4. Intel retains all right, title, and interest in and to the Original
74118611Snjl * Intel Code.
75118611Snjl *
76118611Snjl * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77118611Snjl * Intel shall be used in advertising or otherwise to promote the sale, use or
78118611Snjl * other dealings in products derived from or relating to the Covered Code
79118611Snjl * without prior written authorization from Intel.
80118611Snjl *
81118611Snjl * 4. Disclaimer and Export Compliance
82118611Snjl *
83118611Snjl * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84118611Snjl * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85118611Snjl * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86118611Snjl * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87118611Snjl * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88118611Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89118611Snjl * PARTICULAR PURPOSE.
90118611Snjl *
91118611Snjl * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92118611Snjl * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93118611Snjl * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94118611Snjl * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95118611Snjl * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96118611Snjl * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97118611Snjl * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98118611Snjl * LIMITED REMEDY.
99118611Snjl *
100118611Snjl * 4.3. Licensee shall not export, either directly or indirectly, any of this
101118611Snjl * software or system incorporating such software without first obtaining any
102118611Snjl * required license or other approval from the U. S. Department of Commerce or
103118611Snjl * any other agency or department of the United States Government.  In the
104118611Snjl * event Licensee exports any such software from the United States or
105118611Snjl * re-exports any such software from a foreign destination, Licensee shall
106118611Snjl * ensure that the distribution and export/re-export of the software is in
107118611Snjl * compliance with all laws, regulations, orders, or other restrictions of the
108118611Snjl * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109118611Snjl * any of its subsidiaries will export/re-export any technical data, process,
110118611Snjl * software, or service, directly or indirectly, to any country for which the
111118611Snjl * United States government or any agency thereof requires an export license,
112118611Snjl * other governmental approval, or letter of assurance, without first obtaining
113118611Snjl * such license, approval or letter.
114118611Snjl *
115118611Snjl *****************************************************************************/
116118611Snjl
117118611Snjl#include <stdio.h>
118151937Sjkim#include <time.h>
119151937Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
120118611Snjl
121118611Snjl#define _COMPONENT          ACPI_COMPILER
122118611Snjl        ACPI_MODULE_NAME    ("aslcompile")
123118611Snjl
124151937Sjkim/* Local prototypes */
125118611Snjl
126151937Sjkimstatic void
127151937SjkimCmFlushSourceCode (
128151937Sjkim    void);
129151937Sjkim
130151937Sjkimstatic ACPI_STATUS
131151937SjkimFlCheckForAscii (
132151937Sjkim    ASL_FILE_INFO           *FileInfo);
133151937Sjkim
134193529Sjkimvoid
135193529SjkimFlConsumeAnsiComment (
136193529Sjkim    ASL_FILE_INFO           *FileInfo,
137193529Sjkim    ASL_FILE_STATUS         *Status);
138151937Sjkim
139193529Sjkimvoid
140193529SjkimFlConsumeNewComment (
141193529Sjkim    ASL_FILE_INFO           *FileInfo,
142193529Sjkim    ASL_FILE_STATUS         *Status);
143193529Sjkim
144193529Sjkim
145118611Snjl/*******************************************************************************
146118611Snjl *
147118611Snjl * FUNCTION:    AslCompilerSignon
148118611Snjl *
149118611Snjl * PARAMETERS:  FileId      - ID of the output file
150118611Snjl *
151118611Snjl * RETURN:      None
152118611Snjl *
153118611Snjl * DESCRIPTION: Display compiler signon
154118611Snjl *
155118611Snjl ******************************************************************************/
156118611Snjl
157118611Snjlvoid
158118611SnjlAslCompilerSignon (
159118611Snjl    UINT32                  FileId)
160118611Snjl{
161118611Snjl    char                    *Prefix = "";
162118611Snjl
163118611Snjl
164151937Sjkim    /* Set line prefix depending on the destination file type */
165151937Sjkim
166118611Snjl    switch (FileId)
167118611Snjl    {
168118611Snjl    case ASL_FILE_ASM_SOURCE_OUTPUT:
169118611Snjl    case ASL_FILE_ASM_INCLUDE_OUTPUT:
170118611Snjl
171118611Snjl        Prefix = "; ";
172118611Snjl        break;
173118611Snjl
174118611Snjl    case ASL_FILE_HEX_OUTPUT:
175118611Snjl
176118611Snjl        if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
177118611Snjl        {
178118611Snjl            Prefix = "; ";
179118611Snjl        }
180207344Sjkim        else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
181207344Sjkim                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
182118611Snjl        {
183118611Snjl            FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
184118611Snjl            Prefix = " * ";
185118611Snjl        }
186118611Snjl        break;
187118611Snjl
188118611Snjl    case ASL_FILE_C_SOURCE_OUTPUT:
189118611Snjl    case ASL_FILE_C_INCLUDE_OUTPUT:
190118611Snjl
191118611Snjl        Prefix = " * ";
192118611Snjl        break;
193118611Snjl
194118611Snjl    default:
195118611Snjl        /* No other output types supported */
196118611Snjl        break;
197118611Snjl    }
198118611Snjl
199151937Sjkim    /*
200151937Sjkim     * Compiler signon with copyright
201151937Sjkim     */
202118611Snjl    FlPrintFile (FileId,
203151937Sjkim        "%s\n%s%s\n%s",
204118611Snjl        Prefix,
205118611Snjl        Prefix, IntelAcpiCA,
206151937Sjkim        Prefix);
207151937Sjkim
208151937Sjkim    /* Running compiler or disassembler? */
209151937Sjkim
210151937Sjkim    if (Gbl_DisasmFlag)
211151937Sjkim    {
212151937Sjkim        FlPrintFile (FileId,
213151937Sjkim            "%s", DisassemblerId);
214151937Sjkim    }
215151937Sjkim    else
216151937Sjkim    {
217151937Sjkim        FlPrintFile (FileId,
218151937Sjkim            "%s", CompilerId);
219151937Sjkim    }
220151937Sjkim
221195626Scperciva    /* Version, copyright, compliance */
222151937Sjkim
223151937Sjkim    FlPrintFile (FileId,
224195626Scperciva        " version %X\n%s%s\n%s%s\n%s\n",
225195626Scperciva        (UINT32) ACPI_CA_VERSION,
226118611Snjl        Prefix, CompilerCopyright,
227151937Sjkim        Prefix, CompilerCompliance,
228118611Snjl        Prefix);
229118611Snjl}
230118611Snjl
231118611Snjl
232118611Snjl/*******************************************************************************
233118611Snjl *
234118611Snjl * FUNCTION:    AslCompilerFileHeader
235118611Snjl *
236118611Snjl * PARAMETERS:  FileId      - ID of the output file
237118611Snjl *
238118611Snjl * RETURN:      None
239118611Snjl *
240118611Snjl * DESCRIPTION: Header used at the beginning of output files
241118611Snjl *
242118611Snjl ******************************************************************************/
243118611Snjl
244118611Snjlvoid
245118611SnjlAslCompilerFileHeader (
246118611Snjl    UINT32                  FileId)
247118611Snjl{
248118611Snjl    struct tm               *NewTime;
249118611Snjl    time_t                  Aclock;
250118611Snjl    char                    *Prefix = "";
251118611Snjl
252118611Snjl
253151937Sjkim    /* Set line prefix depending on the destination file type */
254151937Sjkim
255118611Snjl    switch (FileId)
256118611Snjl    {
257118611Snjl    case ASL_FILE_ASM_SOURCE_OUTPUT:
258118611Snjl    case ASL_FILE_ASM_INCLUDE_OUTPUT:
259118611Snjl
260118611Snjl        Prefix = "; ";
261118611Snjl        break;
262118611Snjl
263118611Snjl    case ASL_FILE_HEX_OUTPUT:
264118611Snjl
265118611Snjl        if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
266118611Snjl        {
267118611Snjl            Prefix = "; ";
268118611Snjl        }
269207344Sjkim        else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
270207344Sjkim                 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
271118611Snjl        {
272118611Snjl            Prefix = " * ";
273118611Snjl        }
274118611Snjl        break;
275118611Snjl
276118611Snjl    case ASL_FILE_C_SOURCE_OUTPUT:
277118611Snjl    case ASL_FILE_C_INCLUDE_OUTPUT:
278118611Snjl
279118611Snjl        Prefix = " * ";
280118611Snjl        break;
281118611Snjl
282118611Snjl    default:
283118611Snjl        /* No other output types supported */
284118611Snjl        break;
285118611Snjl    }
286118611Snjl
287118611Snjl    /* Compilation header with timestamp */
288118611Snjl
289118611Snjl    (void) time (&Aclock);
290118611Snjl    NewTime = localtime (&Aclock);
291118611Snjl
292118611Snjl    FlPrintFile (FileId,
293118611Snjl        "%sCompilation of \"%s\" - %s%s\n",
294118611Snjl        Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
295118611Snjl        Prefix);
296118611Snjl
297118611Snjl    switch (FileId)
298118611Snjl    {
299118611Snjl    case ASL_FILE_C_SOURCE_OUTPUT:
300118611Snjl    case ASL_FILE_C_INCLUDE_OUTPUT:
301118611Snjl        FlPrintFile (FileId, " */\n");
302118611Snjl        break;
303118611Snjl
304118611Snjl    default:
305118611Snjl        /* Nothing to do for other output types */
306118611Snjl        break;
307118611Snjl    }
308118611Snjl}
309118611Snjl
310118611Snjl
311118611Snjl/*******************************************************************************
312118611Snjl *
313118611Snjl * FUNCTION:    CmFlushSourceCode
314118611Snjl *
315118611Snjl * PARAMETERS:  None
316118611Snjl *
317118611Snjl * RETURN:      None
318118611Snjl *
319118611Snjl * DESCRIPTION: Read in any remaining source code after the parse tree
320118611Snjl *              has been constructed.
321118611Snjl *
322118611Snjl ******************************************************************************/
323118611Snjl
324151937Sjkimstatic void
325151937SjkimCmFlushSourceCode (
326151937Sjkim    void)
327118611Snjl{
328118611Snjl    char                    Buffer;
329118611Snjl
330118611Snjl
331118611Snjl    while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
332118611Snjl    {
333118611Snjl        InsertLineBuffer ((int) Buffer);
334118611Snjl    }
335118611Snjl
336118611Snjl    ResetCurrentLineBuffer ();
337118611Snjl}
338118611Snjl
339118611Snjl
340118611Snjl/*******************************************************************************
341118611Snjl *
342167802Sjkim * FUNCTION:    FlConsume*
343167802Sjkim *
344167802Sjkim * PARAMETERS:  FileInfo        - Points to an open input file
345167802Sjkim *
346167802Sjkim * RETURN:      Number of lines consumed
347167802Sjkim *
348167802Sjkim * DESCRIPTION: Step over both types of comment during check for ascii chars
349167802Sjkim *
350167802Sjkim ******************************************************************************/
351167802Sjkim
352167802Sjkimvoid
353167802SjkimFlConsumeAnsiComment (
354167802Sjkim    ASL_FILE_INFO           *FileInfo,
355167802Sjkim    ASL_FILE_STATUS         *Status)
356167802Sjkim{
357167802Sjkim    UINT8                   Byte;
358167802Sjkim    BOOLEAN                 ClosingComment = FALSE;
359167802Sjkim
360167802Sjkim
361167802Sjkim    while (fread (&Byte, 1, 1, FileInfo->Handle))
362167802Sjkim    {
363167802Sjkim        /* Scan until comment close is found */
364167802Sjkim
365167802Sjkim        if (ClosingComment)
366167802Sjkim        {
367167802Sjkim            if (Byte == '/')
368167802Sjkim            {
369167802Sjkim                return;
370167802Sjkim            }
371167802Sjkim
372167802Sjkim            if (Byte != '*')
373167802Sjkim            {
374167802Sjkim                /* Reset */
375167802Sjkim
376167802Sjkim                ClosingComment = FALSE;
377167802Sjkim            }
378167802Sjkim        }
379167802Sjkim        else if (Byte == '*')
380167802Sjkim        {
381167802Sjkim            ClosingComment = TRUE;
382167802Sjkim        }
383167802Sjkim
384167802Sjkim        /* Maintain line count */
385167802Sjkim
386167802Sjkim        if (Byte == 0x0A)
387167802Sjkim        {
388167802Sjkim            Status->Line++;
389167802Sjkim        }
390167802Sjkim
391167802Sjkim        Status->Offset++;
392167802Sjkim    }
393167802Sjkim}
394167802Sjkim
395167802Sjkim
396167802Sjkimvoid
397167802SjkimFlConsumeNewComment (
398167802Sjkim    ASL_FILE_INFO           *FileInfo,
399167802Sjkim    ASL_FILE_STATUS         *Status)
400167802Sjkim{
401167802Sjkim    UINT8                   Byte;
402167802Sjkim
403167802Sjkim
404167802Sjkim    while (fread (&Byte, 1, 1, FileInfo->Handle))
405167802Sjkim    {
406167802Sjkim        Status->Offset++;
407167802Sjkim
408167802Sjkim        /* Comment ends at newline */
409167802Sjkim
410167802Sjkim        if (Byte == 0x0A)
411167802Sjkim        {
412167802Sjkim            Status->Line++;
413167802Sjkim            return;
414167802Sjkim        }
415167802Sjkim    }
416167802Sjkim}
417167802Sjkim
418167802Sjkim
419167802Sjkim/*******************************************************************************
420167802Sjkim *
421123315Snjl * FUNCTION:    FlCheckForAscii
422123315Snjl *
423123315Snjl * PARAMETERS:  FileInfo        - Points to an open input file
424123315Snjl *
425151937Sjkim * RETURN:      Status
426123315Snjl *
427167802Sjkim * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
428167802Sjkim *              within comments. Note: does not handle nested comments and does
429167802Sjkim *              not handle comment delimiters within string literals. However,
430167802Sjkim *              on the rare chance this happens and an invalid character is
431167802Sjkim *              missed, the parser will catch the error by failing in some
432167802Sjkim *              spectactular manner.
433123315Snjl *
434123315Snjl ******************************************************************************/
435123315Snjl
436151937Sjkimstatic ACPI_STATUS
437123315SnjlFlCheckForAscii (
438123315Snjl    ASL_FILE_INFO           *FileInfo)
439123315Snjl{
440123315Snjl    UINT8                   Byte;
441123315Snjl    ACPI_SIZE               BadBytes = 0;
442167802Sjkim    BOOLEAN                 OpeningComment = FALSE;
443167802Sjkim    ASL_FILE_STATUS         Status;
444123315Snjl
445123315Snjl
446167802Sjkim    Status.Line = 1;
447167802Sjkim    Status.Offset = 0;
448167802Sjkim
449123315Snjl    /* Read the entire file */
450123315Snjl
451123315Snjl    while (fread (&Byte, 1, 1, FileInfo->Handle))
452123315Snjl    {
453167802Sjkim        /* Ignore comment fields (allow non-ascii within) */
454167802Sjkim
455167802Sjkim        if (OpeningComment)
456167802Sjkim        {
457167802Sjkim            /* Check for second comment open delimiter */
458167802Sjkim
459167802Sjkim            if (Byte == '*')
460167802Sjkim            {
461167802Sjkim                FlConsumeAnsiComment (FileInfo, &Status);
462167802Sjkim            }
463167802Sjkim
464167802Sjkim            if (Byte == '/')
465167802Sjkim            {
466167802Sjkim                FlConsumeNewComment (FileInfo, &Status);
467167802Sjkim            }
468167802Sjkim
469167802Sjkim            /* Reset */
470167802Sjkim
471167802Sjkim            OpeningComment = FALSE;
472167802Sjkim        }
473167802Sjkim        else if (Byte == '/')
474167802Sjkim        {
475167802Sjkim            OpeningComment = TRUE;
476167802Sjkim        }
477167802Sjkim
478123315Snjl        /* Check for an ASCII character */
479123315Snjl
480193529Sjkim        if (!ACPI_IS_ASCII (Byte))
481123315Snjl        {
482123315Snjl            if (BadBytes < 10)
483123315Snjl            {
484151937Sjkim                AcpiOsPrintf (
485167802Sjkim                    "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
486167802Sjkim                    Byte, Status.Line, Status.Offset);
487123315Snjl            }
488167802Sjkim
489123315Snjl            BadBytes++;
490123315Snjl        }
491167802Sjkim
492167802Sjkim        /* Update line counter */
493167802Sjkim
494167802Sjkim        else if (Byte == 0x0A)
495167802Sjkim        {
496167802Sjkim            Status.Line++;
497167802Sjkim        }
498167802Sjkim
499167802Sjkim        Status.Offset++;
500123315Snjl    }
501123315Snjl
502151937Sjkim    /* Seek back to the beginning of the source file */
503151937Sjkim
504151937Sjkim    fseek (FileInfo->Handle, 0, SEEK_SET);
505151937Sjkim
506123315Snjl    /* Were there any non-ASCII characters in the file? */
507123315Snjl
508123315Snjl    if (BadBytes)
509123315Snjl    {
510151937Sjkim        AcpiOsPrintf (
511167802Sjkim            "%u non-ASCII characters found in input source text, could be a binary file\n",
512151937Sjkim            BadBytes);
513167802Sjkim        AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, FileInfo->Filename);
514123315Snjl        return (AE_BAD_CHARACTER);
515123315Snjl    }
516123315Snjl
517151937Sjkim    /* File is OK */
518123315Snjl
519123315Snjl    return (AE_OK);
520123315Snjl}
521123315Snjl
522123315Snjl
523123315Snjl/*******************************************************************************
524123315Snjl *
525118611Snjl * FUNCTION:    CmDoCompile
526118611Snjl *
527118611Snjl * PARAMETERS:  None
528118611Snjl *
529118611Snjl * RETURN:      Status (0 = OK)
530118611Snjl *
531118611Snjl * DESCRIPTION: This procedure performs the entire compile
532118611Snjl *
533118611Snjl ******************************************************************************/
534118611Snjl
535118611Snjlint
536151937SjkimCmDoCompile (
537151937Sjkim    void)
538118611Snjl{
539118611Snjl    ACPI_STATUS             Status;
540151937Sjkim    UINT8                   FullCompile;
541151937Sjkim    UINT8                   Event;
542118611Snjl
543118611Snjl
544151937Sjkim    FullCompile = UtBeginEvent ("*** Total Compile time ***");
545151937Sjkim    Event = UtBeginEvent ("Open input and output files");
546118611Snjl
547118611Snjl    /* Open the required input and output files */
548118611Snjl
549118611Snjl    Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
550118611Snjl    if (ACPI_FAILURE (Status))
551118611Snjl    {
552118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
553118611Snjl        return -1;
554118611Snjl    }
555118611Snjl
556167802Sjkim    /* Check for 100% ASCII source file (comments are ignored) */
557123315Snjl
558167802Sjkim    Status = FlCheckForAscii (&Gbl_Files[ASL_FILE_INPUT]);
559167802Sjkim    if (ACPI_FAILURE (Status))
560123315Snjl    {
561167802Sjkim        AePrintErrorLog (ASL_FILE_STDERR);
562167802Sjkim        return -1;
563123315Snjl    }
564123315Snjl
565118611Snjl    Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
566118611Snjl    if (ACPI_FAILURE (Status))
567118611Snjl    {
568118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
569118611Snjl        return -1;
570118611Snjl    }
571151937Sjkim    UtEndEvent (Event);
572118611Snjl
573118611Snjl    /* Build the parse tree */
574118611Snjl
575151937Sjkim    Event = UtBeginEvent ("Parse source code and build parse tree");
576118611Snjl    AslCompilerparse();
577151937Sjkim    UtEndEvent (Event);
578118611Snjl
579118611Snjl    /* Flush out any remaining source after parse tree is complete */
580118611Snjl
581151937Sjkim    Event = UtBeginEvent ("Flush source input");
582118611Snjl    CmFlushSourceCode ();
583118611Snjl
584118611Snjl    /* Did the parse tree get successfully constructed? */
585118611Snjl
586118611Snjl    if (!RootNode)
587118611Snjl    {
588118611Snjl        CmCleanupAndExit ();
589118611Snjl        return -1;
590118611Snjl    }
591118611Snjl
592167802Sjkim    /* Optional parse tree dump, compiler debug output only */
593167802Sjkim
594167802Sjkim    LsDumpParseTree ();
595167802Sjkim
596118611Snjl    OpcGetIntegerWidth (RootNode);
597151937Sjkim    UtEndEvent (Event);
598118611Snjl
599118611Snjl    /* Pre-process parse tree for any operator transforms */
600118611Snjl
601151937Sjkim    Event = UtBeginEvent ("Parse tree transforms");
602118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
603151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
604151937Sjkim        TrAmlTransformWalk, NULL, NULL);
605151937Sjkim    UtEndEvent (Event);
606118611Snjl
607118611Snjl    /* Generate AML opcodes corresponding to the parse tokens */
608118611Snjl
609151937Sjkim    Event = UtBeginEvent ("Generate AML opcodes");
610118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
611151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
612151937Sjkim        OpcAmlOpcodeWalk, NULL);
613151937Sjkim    UtEndEvent (Event);
614118611Snjl
615118611Snjl    /*
616118611Snjl     * Now that the input is parsed, we can open the AML output file.
617118611Snjl     * Note: by default, the name of this file comes from the table descriptor
618118611Snjl     * within the input file.
619118611Snjl     */
620151937Sjkim    Event = UtBeginEvent ("Open AML output file");
621118611Snjl    Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
622118611Snjl    if (ACPI_FAILURE (Status))
623118611Snjl    {
624118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
625118611Snjl        return -1;
626118611Snjl    }
627151937Sjkim    UtEndEvent (Event);
628118611Snjl
629118611Snjl    /* Interpret and generate all compile-time constants */
630118611Snjl
631151937Sjkim    Event = UtBeginEvent ("Constant folding via AML interpreter");
632151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
633151937Sjkim        "\nInterpreting compile-time constant expressions\n\n");
634151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
635151937Sjkim        OpcAmlConstantWalk, NULL, NULL);
636151937Sjkim    UtEndEvent (Event);
637118611Snjl
638151937Sjkim    /* Update AML opcodes if necessary, after constant folding */
639151937Sjkim
640151937Sjkim    Event = UtBeginEvent ("Updating AML opcodes after constant folding");
641151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT,
642151937Sjkim        "\nUpdating AML opcodes after constant folding\n\n");
643151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
644151937Sjkim        NULL, OpcAmlOpcodeUpdateWalk, NULL);
645151937Sjkim    UtEndEvent (Event);
646151937Sjkim
647118611Snjl    /* Calculate all AML package lengths */
648118611Snjl
649151937Sjkim    Event = UtBeginEvent ("Generate AML package lengths");
650118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
651151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
652151937Sjkim        LnPackageLengthWalk, NULL);
653151937Sjkim    UtEndEvent (Event);
654118611Snjl
655118611Snjl    if (Gbl_ParseOnlyFlag)
656118611Snjl    {
657118611Snjl        AePrintErrorLog (ASL_FILE_STDOUT);
658118611Snjl        UtDisplaySummary (ASL_FILE_STDOUT);
659118611Snjl        if (Gbl_DebugFlag)
660118611Snjl        {
661118611Snjl            /* Print error summary to the debug file */
662118611Snjl
663118611Snjl            AePrintErrorLog (ASL_FILE_STDERR);
664118611Snjl            UtDisplaySummary (ASL_FILE_STDERR);
665118611Snjl        }
666118611Snjl        return 0;
667118611Snjl    }
668118611Snjl
669118611Snjl    /*
670118611Snjl     * Create an internal namespace and use it as a symbol table
671118611Snjl     */
672118611Snjl
673118611Snjl    /* Namespace loading */
674118611Snjl
675151937Sjkim    Event = UtBeginEvent ("Create ACPI Namespace");
676118611Snjl    Status = LdLoadNamespace (RootNode);
677151937Sjkim    UtEndEvent (Event);
678118611Snjl    if (ACPI_FAILURE (Status))
679118611Snjl    {
680118611Snjl        return -1;
681118611Snjl    }
682118611Snjl
683167802Sjkim    /* Namespace cross-reference */
684118611Snjl
685151937Sjkim    AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
686118611Snjl    Status = LkCrossReferenceNamespace ();
687118611Snjl    if (ACPI_FAILURE (Status))
688118611Snjl    {
689118611Snjl        return -1;
690118611Snjl    }
691118611Snjl
692167802Sjkim    /* Namespace - Check for non-referenced objects */
693167802Sjkim
694167802Sjkim    LkFindUnreferencedObjects ();
695167802Sjkim    UtEndEvent (AslGbl_NamespaceEvent);
696167802Sjkim
697118611Snjl    /*
698118611Snjl     * Semantic analysis.  This can happen only after the
699118611Snjl     * namespace has been loaded and cross-referenced.
700118611Snjl     *
701118611Snjl     * part one - check control methods
702118611Snjl     */
703151937Sjkim    Event = UtBeginEvent ("Analyze control method return types");
704118611Snjl    AnalysisWalkInfo.MethodStack = NULL;
705118611Snjl
706118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
707151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
708151937Sjkim        AnMethodAnalysisWalkBegin,
709151937Sjkim        AnMethodAnalysisWalkEnd, &AnalysisWalkInfo);
710151937Sjkim    UtEndEvent (Event);
711118611Snjl
712118611Snjl    /* Semantic error checking part two - typing of method returns */
713118611Snjl
714151937Sjkim    Event = UtBeginEvent ("Determine object types returned by methods");
715151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
716151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
717151937Sjkim        AnMethodTypingWalkBegin,
718151937Sjkim        AnMethodTypingWalkEnd, NULL);
719151937Sjkim    UtEndEvent (Event);
720118611Snjl
721118611Snjl    /* Semantic error checking part three - operand type checking */
722118611Snjl
723151937Sjkim    Event = UtBeginEvent ("Analyze AML operand types");
724151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
725151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
726151937Sjkim        AnOperandTypecheckWalkBegin,
727151937Sjkim        AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
728151937Sjkim    UtEndEvent (Event);
729118611Snjl
730118611Snjl    /* Semantic error checking part four - other miscellaneous checks */
731118611Snjl
732151937Sjkim    Event = UtBeginEvent ("Miscellaneous analysis");
733151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
734151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
735151937Sjkim        AnOtherSemanticAnalysisWalkBegin,
736151937Sjkim        AnOtherSemanticAnalysisWalkEnd, &AnalysisWalkInfo);
737151937Sjkim    UtEndEvent (Event);
738118611Snjl
739118611Snjl    /* Calculate all AML package lengths */
740118611Snjl
741151937Sjkim    Event = UtBeginEvent ("Finish AML package length generation");
742118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
743151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
744151937Sjkim        LnInitLengthsWalk, NULL);
745151937Sjkim    TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
746151937Sjkim        LnPackageLengthWalk, NULL);
747151937Sjkim    UtEndEvent (Event);
748118611Snjl
749118611Snjl    /* Code generation - emit the AML */
750118611Snjl
751151937Sjkim    Event = UtBeginEvent ("Generate AML code and write output files");
752118611Snjl    CgGenerateAmlOutput ();
753151937Sjkim    UtEndEvent (Event);
754118611Snjl
755151937Sjkim    Event = UtBeginEvent ("Write optional output files");
756118611Snjl    CmDoOutputFiles ();
757151937Sjkim    UtEndEvent (Event);
758118611Snjl
759151937Sjkim    UtEndEvent (FullCompile);
760118611Snjl    CmCleanupAndExit ();
761118611Snjl    return 0;
762118611Snjl}
763118611Snjl
764151937Sjkim
765151937Sjkim/*******************************************************************************
766151937Sjkim *
767151937Sjkim * FUNCTION:    CmDoOutputFiles
768151937Sjkim *
769151937Sjkim * PARAMETERS:  None
770151937Sjkim *
771151937Sjkim * RETURN:      None.
772151937Sjkim *
773151937Sjkim * DESCRIPTION: Create all "listing" type files
774151937Sjkim *
775151937Sjkim ******************************************************************************/
776151937Sjkim
777118611Snjlvoid
778151937SjkimCmDoOutputFiles (
779151937Sjkim    void)
780118611Snjl{
781118611Snjl
782118611Snjl    /* Create listings and hex files */
783118611Snjl
784118611Snjl    LsDoListings ();
785118611Snjl    LsDoHexOutput ();
786118611Snjl
787118611Snjl    /* Dump the namespace to the .nsp file if requested */
788118611Snjl
789151937Sjkim    (void) LsDisplayNamespace ();
790118611Snjl}
791118611Snjl
792118611Snjl
793118611Snjl/*******************************************************************************
794118611Snjl *
795151937Sjkim * FUNCTION:    CmDumpEvent
796151937Sjkim *
797151937Sjkim * PARAMETERS:  Event           - A compiler event struct
798151937Sjkim *
799151937Sjkim * RETURN:      None.
800151937Sjkim *
801151937Sjkim * DESCRIPTION: Dump a compiler event struct
802151937Sjkim *
803151937Sjkim ******************************************************************************/
804151937Sjkim
805151937Sjkimstatic void
806151937SjkimCmDumpEvent (
807151937Sjkim    ASL_EVENT_INFO          *Event)
808151937Sjkim{
809151937Sjkim    UINT32                  Delta;
810151937Sjkim    UINT32                  USec;
811151937Sjkim    UINT32                  MSec;
812151937Sjkim
813151937Sjkim    if (!Event->Valid)
814151937Sjkim    {
815151937Sjkim        return;
816151937Sjkim    }
817151937Sjkim
818151937Sjkim    /* Delta will be in 100-nanosecond units */
819151937Sjkim
820151937Sjkim    Delta = (UINT32) (Event->EndTime - Event->StartTime);
821151937Sjkim
822151937Sjkim    USec = Delta / 10;
823151937Sjkim    MSec = Delta / 10000;
824151937Sjkim
825151937Sjkim    /* Round milliseconds up */
826151937Sjkim
827151937Sjkim    if ((USec - (MSec * 1000)) >= 500)
828151937Sjkim    {
829151937Sjkim        MSec++;
830151937Sjkim    }
831151937Sjkim
832151937Sjkim    DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
833151937Sjkim        USec, MSec, Event->EventName);
834151937Sjkim}
835151937Sjkim
836151937Sjkim
837151937Sjkim/*******************************************************************************
838151937Sjkim *
839118611Snjl * FUNCTION:    CmCleanupAndExit
840118611Snjl *
841118611Snjl * PARAMETERS:  None
842118611Snjl *
843118611Snjl * RETURN:      None.
844118611Snjl *
845118611Snjl * DESCRIPTION: Close all open files and exit the compiler
846118611Snjl *
847118611Snjl ******************************************************************************/
848118611Snjl
849118611Snjlvoid
850151937SjkimCmCleanupAndExit (
851151937Sjkim    void)
852118611Snjl{
853118611Snjl    UINT32                  i;
854118611Snjl
855118611Snjl
856118611Snjl    AePrintErrorLog (ASL_FILE_STDOUT);
857118611Snjl    if (Gbl_DebugFlag)
858118611Snjl    {
859118611Snjl        /* Print error summary to the debug file */
860118611Snjl
861118611Snjl        AePrintErrorLog (ASL_FILE_STDERR);
862118611Snjl    }
863118611Snjl
864118611Snjl    DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
865151937Sjkim    for (i = 0; i < AslGbl_NextEvent; i++)
866118611Snjl    {
867151937Sjkim        CmDumpEvent (&AslGbl_Events[i]);
868118611Snjl    }
869118611Snjl
870118611Snjl    if (Gbl_CompileTimesFlag)
871118611Snjl    {
872118611Snjl        printf ("\nElapsed time for major events\n\n");
873151937Sjkim        for (i = 0; i < AslGbl_NextEvent; i++)
874118611Snjl        {
875151937Sjkim            CmDumpEvent (&AslGbl_Events[i]);
876118611Snjl        }
877151937Sjkim
878118611Snjl        printf ("\nMiscellaneous compile statistics\n\n");
879118611Snjl        printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
880118611Snjl        printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
881118611Snjl        printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
882118611Snjl        printf ("%11u : %s\n", TotalMethods, "Control methods");
883118611Snjl        printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
884118611Snjl        printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
885118611Snjl        printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
886118611Snjl        printf ("\n");
887118611Snjl    }
888118611Snjl
889118611Snjl    if (Gbl_NsLookupCount)
890118611Snjl    {
891118611Snjl        DbgPrint (ASL_DEBUG_OUTPUT, "\n\nMiscellaneous compile statistics\n\n");
892151937Sjkim        DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d\n", "Total Namespace searches",
893151937Sjkim            Gbl_NsLookupCount);
894151937Sjkim        DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d usec\n", "Time per search",
895151937Sjkim            ((UINT32) (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
896151937Sjkim                        AslGbl_Events[AslGbl_NamespaceEvent].StartTime) /
897151937Sjkim                        10) / Gbl_NsLookupCount);
898118611Snjl    }
899118611Snjl
900118611Snjl
901118611Snjl    if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
902118611Snjl    {
903167802Sjkim        printf ("\nMaximum error count (%d) exceeded\n", ASL_MAX_ERROR_COUNT);
904118611Snjl    }
905118611Snjl
906118611Snjl    UtDisplaySummary (ASL_FILE_STDOUT);
907199337Sjkim
908199337Sjkim    /* Close all open files */
909199337Sjkim
910199337Sjkim    for (i = 2; i < ASL_MAX_FILE_TYPE; i++)
911199337Sjkim    {
912199337Sjkim        FlCloseFile (i);
913199337Sjkim    }
914200553Sjkim
915200553Sjkim    /* Delete AML file if there are errors */
916200553Sjkim
917200553Sjkim    if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors))
918200553Sjkim    {
919200553Sjkim        remove (Gbl_Files[ASL_FILE_AML_OUTPUT].Filename);
920200553Sjkim    }
921200553Sjkim
922200553Sjkim    /*
923200553Sjkim     * Delete intermediate ("combined") source file (if -ls flag not set)
924200553Sjkim     *
925200553Sjkim     * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
926200553Sjkim     */
927200553Sjkim    if (!Gbl_SourceOutputFlag)
928200553Sjkim    {
929200553Sjkim        if (remove (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename))
930200553Sjkim        {
931200553Sjkim            printf ("Could not remove SRC file, %s\n",
932200553Sjkim                Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);
933200553Sjkim        }
934200553Sjkim    }
935118611Snjl}
936118611Snjl
937118611Snjl
938