aslmain.c revision 138287
1
2/******************************************************************************
3 *
4 * Module Name: aslmain - compiler main and utilities
5 *              $Revision: 77 $
6 *
7 *****************************************************************************/
8
9/******************************************************************************
10 *
11 * 1. Copyright Notice
12 *
13 * Some or all of this work - Copyright (c) 1999 - 2004, 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
119#define _DECLARE_GLOBALS
120
121#include "aslcompiler.h"
122#include "acnamesp.h"
123#include "acapps.h"
124
125#ifdef _DEBUG
126#include <crtdbg.h>
127#endif
128
129#define _COMPONENT          ACPI_COMPILER
130        ACPI_MODULE_NAME    ("aslmain")
131
132BOOLEAN                 AslToFile = TRUE;
133BOOLEAN                 DoCompile = TRUE;
134BOOLEAN                 DoSignon = TRUE;
135
136char                    hex[] = {'0','1','2','3','4','5','6','7',
137                                 '8','9','A','B','C','D','E','F'};
138
139
140/*******************************************************************************
141 *
142 * FUNCTION:    Options
143 *
144 * PARAMETERS:  None
145 *
146 * RETURN:      None
147 *
148 * DESCRIPTION: Display option help message
149 *
150 ******************************************************************************/
151
152void
153Options (
154    void)
155{
156
157    printf ("General Output:\n");
158    printf ("  -p <prefix>    Specify filename prefix for all output files (including .aml)\n");
159    printf ("  -vi            Less verbose errors and warnings for use with IDEs\n");
160    printf ("  -vo            Enable optimization comments\n");
161    printf ("  -vr            Disable remarks\n");
162    printf ("  -vs            Disable signon\n");
163
164    printf ("\nAML Output Files:\n");
165    printf ("  -s<a|c>        Create AML in assembler or C source file (*.asm or *.c)\n");
166    printf ("  -i<a|c>        Create assembler or C include file (*.inc or *.h)\n");
167    printf ("  -t<a|c>        Create AML in assembler or C hex table (*.hex)\n");
168
169    printf ("\nAML Code Generation:\n");
170    printf ("  -oa            Disable all optimizations (compatibility mode)\n");
171    printf ("  -of            Disable constant folding\n");
172    printf ("  -oi            Disable integer optimization to Zero/One/Ones\n");
173    printf ("  -on            Disable named reference string optimization\n");
174    printf ("  -r<Revision>   Override table header Revision (1-255)\n");
175
176    printf ("\nListings:\n");
177    printf ("  -l             Create mixed listing file (ASL source and AML) (*.lst)\n");
178    printf ("  -ln            Create namespace file (*.nsp)\n");
179    printf ("  -ls            Create combined source file (expanded includes) (*.src)\n");
180
181    printf ("\nAML Disassembler:\n");
182    printf ("  -d  [file]     Disassemble AML to ASL source code file (*.dsl)\n");
183    printf ("  -dc [file]     Disassemble AML and immediately compile it\n");
184    printf ("                 (Obtain DSDT from current system if no input file)\n");
185    printf ("  -e             Generate External() statements for unresolved symbols\n");
186    printf ("  -g             Get ACPI tables and write to files (*.dat)\n");
187
188    printf ("\nHelp:\n");
189    printf ("  -h             Additional help and compiler debug options\n");
190    printf ("  -hc            Display operators allowed in constant expressions\n");
191    printf ("  -hr            Display ACPI reserved method names\n");
192}
193
194
195/*******************************************************************************
196 *
197 * FUNCTION:    Usage
198 *
199 * PARAMETERS:  None
200 *
201 * RETURN:      None
202 *
203 * DESCRIPTION: Display help message
204 *
205 ******************************************************************************/
206
207void
208HelpMessage (
209    void)
210{
211
212    printf ("AML output filename generation:\n");
213    printf ("  Output filenames are generated by appending an extension to a common\n");
214    printf ("  filename prefix.  The filename prefix is obtained via one of the\n");
215    printf ("  following methods (in priority order):\n");
216    printf ("    1) The -p option specifies the prefix\n");
217    printf ("    2) The prefix of the AMLFileName in the ASL Definition Block\n");
218    printf ("    3) The prefix of the input filename\n");
219    printf ("\n");
220
221    Options ();
222
223    printf ("\nCompiler Debug Options:\n");
224    printf ("  -b<p|t|b>      Create compiler debug/trace file (*.txt)\n");
225    printf ("                   Types: Parse/Tree/Both\n");
226    printf ("  -f             Ignore errors, force creation of AML output file(s)\n");
227    printf ("  -c             Parse only, no output generation\n");
228    printf ("  -ot            Display compile times\n");
229    printf ("  -x<level>      Set debug level for trace output\n");
230}
231
232
233/*******************************************************************************
234 *
235 * FUNCTION:    Usage
236 *
237 * PARAMETERS:  None
238 *
239 * RETURN:      None
240 *
241 * DESCRIPTION: Display usage and option message
242 *
243 ******************************************************************************/
244
245void
246Usage (
247    void)
248{
249
250    printf ("Usage:    %s [Options] [InputFile]\n\n", CompilerName);
251    Options ();
252}
253
254
255/*******************************************************************************
256 *
257 * FUNCTION:    AslInitialize
258 *
259 * PARAMETERS:  None
260 *
261 * RETURN:      None
262 *
263 * DESCRIPTION: Initialize compiler globals
264 *
265 ******************************************************************************/
266
267void
268AslInitialize (void)
269{
270    UINT32                  i;
271
272
273#ifdef _DEBUG
274    _CrtSetDbgFlag (_CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(0));
275#endif
276
277    AcpiDbgLevel = 0;
278
279    for (i = 0; i < ASL_NUM_FILES; i++)
280    {
281        Gbl_Files[i].Handle = NULL;
282        Gbl_Files[i].Filename = NULL;
283    }
284
285    Gbl_Files[ASL_FILE_STDOUT].Handle   = stdout;
286    Gbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";
287
288    Gbl_Files[ASL_FILE_STDERR].Handle   = stderr;
289    Gbl_Files[ASL_FILE_STDERR].Filename = "STDERR";
290}
291
292
293/*******************************************************************************
294 *
295 * FUNCTION:    AslCommandLine
296 *
297 * PARAMETERS:  argc/argv
298 *
299 * RETURN:      None
300 *
301 * DESCRIPTION: Command line processing
302 *
303 ******************************************************************************/
304
305void
306AslCommandLine (
307    int                     argc,
308    char                    **argv)
309{
310    BOOLEAN                 BadCommandLine = FALSE;
311    ACPI_NATIVE_UINT        j;
312
313
314    /* Minimum command line contains at least one option or an input file */
315
316    if (argc < 2)
317    {
318        AslCompilerSignon (ASL_FILE_STDOUT);
319        Usage ();
320        exit (1);
321    }
322
323    /* Get the command line options */
324
325    while ((j = AcpiGetopt (argc, argv, "ab:cd^efgh^i^l^o:p:r:s:t:v:x:")) != EOF) switch (j)
326    {
327    case 'a':
328        AslToFile = FALSE;
329        break;
330
331
332    case 'b':
333
334        switch (AcpiGbl_Optarg[0])
335        {
336        case 'b':
337            AslCompilerdebug = 1; /* same as yydebug */
338            break;
339
340        case 'p':
341            AslCompilerdebug = 1; /* same as yydebug */
342            break;
343
344        case 't':
345            break;
346
347        default:
348            printf ("Unknown option: -b%s\n", AcpiGbl_Optarg);
349            BadCommandLine = TRUE;
350            break;
351        }
352
353        /* Produce debug output file */
354
355        Gbl_DebugFlag = TRUE;
356        break;
357
358
359    case 'c':
360
361        /* Parse only */
362
363        Gbl_ParseOnlyFlag = TRUE;
364        break;
365
366
367    case 'd':
368        switch (AcpiGbl_Optarg[0])
369        {
370        case '^':
371            DoCompile = FALSE;
372            break;
373
374        case 'c':
375            break;
376
377        default:
378            printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
379            BadCommandLine = TRUE;
380            break;
381        }
382
383        Gbl_DisasmFlag = TRUE;
384        break;
385
386
387    case 'e':
388
389        /* Generate external statements for unresolved symbols */
390
391        Gbl_GenerateExternals = TRUE;
392        break;
393
394
395    case 'f':
396
397        /* Ignore errors and force creation of aml file */
398
399        Gbl_IgnoreErrors = TRUE;
400        break;
401
402
403    case 'g':
404
405        /* Get all ACPI tables */
406
407        Gbl_GetAllTables = TRUE;
408        DoCompile = FALSE;
409        break;
410
411
412    case 'h':
413
414        switch (AcpiGbl_Optarg[0])
415        {
416        case '^':
417            HelpMessage ();
418            exit (0);
419
420        case 'c':
421            UtDisplayConstantOpcodes ();
422            exit (0);
423
424        case 'r':
425            /* reserved names */
426
427            MpDisplayReservedNames ();
428            exit (0);
429
430        default:
431            printf ("Unknown option: -h%s\n", AcpiGbl_Optarg);
432            BadCommandLine = TRUE;
433            break;
434        }
435        break;
436
437
438    case 'i':
439
440        switch (AcpiGbl_Optarg[0])
441        {
442        case 'a':
443
444            /* Produce assembly code include file */
445
446            Gbl_AsmIncludeOutputFlag = TRUE;
447            break;
448
449        case 'c':
450
451            /* Produce C include file */
452
453            Gbl_C_IncludeOutputFlag = TRUE;
454            break;
455
456        default:
457            printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
458            BadCommandLine = TRUE;
459            break;
460        }
461        break;
462
463
464    case 'l':
465
466        switch (AcpiGbl_Optarg[0])
467        {
468        case '^':
469            /* Produce listing file (Mixed source/aml) */
470
471            Gbl_ListingFlag = TRUE;
472            break;
473
474        case 'n':
475            /* Produce namespace file */
476
477            Gbl_NsOutputFlag = TRUE;
478            break;
479
480        case 's':
481            /* Produce combined source file */
482
483            Gbl_SourceOutputFlag = TRUE;
484            break;
485
486        default:
487            printf ("Unknown option: -l%s\n", AcpiGbl_Optarg);
488            BadCommandLine = TRUE;
489            break;
490        }
491        break;
492
493
494    case 'o':
495
496        switch (AcpiGbl_Optarg[0])
497        {
498        case 'a':
499
500            /* Disable all optimizations */
501
502            Gbl_FoldConstants = FALSE;
503            Gbl_IntegerOptimizationFlag = FALSE;
504            Gbl_ReferenceOptimizationFlag = FALSE;
505            break;
506
507        case 'f':
508
509            /* Disable folding on "normal" expressions */
510
511            Gbl_FoldConstants = FALSE;
512            break;
513
514        case 'i':
515
516            /* Disable integer optimization to constants */
517
518            Gbl_IntegerOptimizationFlag = FALSE;
519            break;
520
521        case 'n':
522
523            /* Disable named reference optimization */
524
525            Gbl_ReferenceOptimizationFlag = FALSE;
526            break;
527
528        case 't':
529
530            /* Display compile time(s) */
531
532            Gbl_CompileTimesFlag = TRUE;
533            break;
534
535        default:
536            printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
537            BadCommandLine = TRUE;
538            break;
539        }
540        break;
541
542
543    case 'p':
544
545        /* Override default AML output filename */
546
547        Gbl_OutputFilenamePrefix = AcpiGbl_Optarg;
548        Gbl_UseDefaultAmlFilename = FALSE;
549        break;
550
551
552    case 'r':
553        Gbl_RevisionOverride = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
554        break;
555
556
557    case 's':
558
559        switch (AcpiGbl_Optarg[0])
560        {
561        case 'a':
562
563            /* Produce assembly code output file */
564
565            Gbl_AsmOutputFlag = TRUE;
566            break;
567
568        case 'c':
569
570            /* Produce C hex output file */
571
572            Gbl_C_OutputFlag = TRUE;
573            break;
574
575        default:
576            printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
577            BadCommandLine = TRUE;
578            break;
579        }
580        break;
581
582
583    case 't':
584
585        /* Produce hex table output file */
586
587        switch (AcpiGbl_Optarg[0])
588        {
589        case 'a':
590            Gbl_HexOutputFlag = HEX_OUTPUT_ASM;
591            break;
592
593        case 'c':
594            Gbl_HexOutputFlag = HEX_OUTPUT_C;
595            break;
596
597        default:
598            printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
599            BadCommandLine = TRUE;
600            break;
601        }
602        break;
603
604
605    case 'v':
606
607        switch (AcpiGbl_Optarg[0])
608        {
609        case 'i':
610            /* Less verbose error messages */
611
612            Gbl_VerboseErrors = FALSE;
613            break;
614
615        case 'o':
616            Gbl_DisplayOptimizations = TRUE;
617            break;
618
619        case 'r':
620            Gbl_DisplayRemarks = FALSE;
621            break;
622
623        case 's':
624            DoSignon = FALSE;
625            break;
626
627        default:
628            printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
629            BadCommandLine = TRUE;
630            break;
631        }
632        break;
633
634
635    case 'x':
636
637        AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 16);
638        break;
639
640
641    default:
642
643        BadCommandLine = TRUE;
644        break;
645    }
646
647    /* Next parameter must be the input filename */
648
649    Gbl_Files[ASL_FILE_INPUT].Filename = argv[AcpiGbl_Optind];
650    if (!Gbl_Files[ASL_FILE_INPUT].Filename && !Gbl_DisasmFlag && !Gbl_GetAllTables)
651    {
652        printf ("Missing input filename\n");
653        BadCommandLine = TRUE;
654    }
655
656    if (DoSignon)
657    {
658        AslCompilerSignon (ASL_FILE_STDOUT);
659    }
660
661    /* Abort if anything went wrong on the command line */
662
663    if (BadCommandLine)
664    {
665        printf ("\n");
666        Usage ();
667        exit (1);
668    }
669
670    if ((AcpiGbl_Optind + 1) < argc)
671    {
672        printf ("Warning: extra arguments (%d) after input filename are ignored\n\n",
673            argc - AcpiGbl_Optind - 1);
674    }
675}
676
677
678/*******************************************************************************
679 *
680 * FUNCTION:    main
681 *
682 * PARAMETERS:  Standard argc/argv
683 *
684 * RETURN:      Program termination code
685 *
686 * DESCRIPTION: C main routine for the Asl Compiler.  Handle command line
687 *              options and begin the compile.
688 *
689 ******************************************************************************/
690
691int ACPI_SYSTEM_XFACE
692main (
693    int                     argc,
694    char                    **argv)
695{
696    ACPI_STATUS             Status;
697    char                    *Prefix;
698
699
700    /* Init and command line */
701
702    AslInitialize ();
703    AslCommandLine (argc, argv);
704
705    /*
706     * If -p not specified, we will use the input filename as the
707     * output filename prefix
708     */
709    FlSplitInputPathname (Gbl_Files[ASL_FILE_INPUT].Filename,
710        &Gbl_DirectoryPath, &Prefix);
711
712    if (Gbl_UseDefaultAmlFilename)
713    {
714        Gbl_OutputFilenamePrefix = Prefix;
715    }
716
717    /*
718     * AML Disassembly (Optional)
719     */
720    if (Gbl_DisasmFlag || Gbl_GetAllTables)
721    {
722        /* ACPI CA subsystem initialization */
723
724        Status = AcpiOsInitialize ();
725        AcpiUtInitGlobals ();
726        Status = AcpiUtMutexInitialize ();
727        if (ACPI_FAILURE (Status))
728        {
729            return -1;
730        }
731
732        Status = AcpiNsRootInitialize ();
733        if (ACPI_FAILURE (Status))
734        {
735            return -1;
736        }
737
738        /* This is where the disassembly happens */
739
740        AcpiGbl_DbOpt_disasm = TRUE;
741        Status = AdAmlDisassemble (AslToFile,
742                        Gbl_Files[ASL_FILE_INPUT].Filename,
743                        Gbl_OutputFilenamePrefix,
744                        &Gbl_Files[ASL_FILE_INPUT].Filename,
745                        Gbl_GetAllTables);
746        if (ACPI_FAILURE (Status))
747        {
748            return -1;
749        }
750
751        /*
752         * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
753         * .DSL disassembly file, which can now be compiled if requested
754         */
755        if (DoCompile)
756        {
757            AcpiOsPrintf ("\nCompiling \"%s\"\n",
758                Gbl_Files[ASL_FILE_INPUT].Filename);
759        }
760    }
761
762    /*
763     * ASL Compilation (Optional)
764     */
765    if (DoCompile)
766    {
767        /*
768         * If -p not specified, we will use the input filename as the
769         * output filename prefix
770         */
771        FlSplitInputPathname (Gbl_Files[ASL_FILE_INPUT].Filename,
772            &Gbl_DirectoryPath, &Prefix);
773
774        if (Gbl_UseDefaultAmlFilename)
775        {
776            Gbl_OutputFilenamePrefix = Prefix;
777        }
778
779        /* ACPI CA subsystem initialization (Must be re-initialized) */
780
781        Status = AcpiOsInitialize ();
782        AcpiUtInitGlobals ();
783        Status = AcpiUtMutexInitialize ();
784        if (ACPI_FAILURE (Status))
785        {
786            return -1;
787        }
788
789        Status = AcpiNsRootInitialize ();
790        if (ACPI_FAILURE (Status))
791        {
792            return -1;
793        }
794        Status = CmDoCompile ();
795    }
796
797    return (0);
798}
799
800
801