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