]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/compiler/asloptions.c
Merge libc++ r291274, and update the library Makefile.
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / compiler / asloptions.c
1 /******************************************************************************
2  *
3  * Module Name: asloptions - compiler command line processing
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2016, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/acapps.h>
46 #include <contrib/dev/acpica/include/acdisasm.h>
47
48 #define _COMPONENT          ACPI_COMPILER
49         ACPI_MODULE_NAME    ("asloption")
50
51
52 /* Local prototypes */
53
54 static int
55 AslDoOptions (
56     int                     argc,
57     char                    **argv,
58     BOOLEAN                 IsResponseFile);
59
60 static void
61 AslMergeOptionTokens (
62     char                    *InBuffer,
63     char                    *OutBuffer);
64
65 static int
66 AslDoResponseFile (
67     char                    *Filename);
68
69
70 #define ASL_TOKEN_SEPARATORS    " \t\n"
71 #define ASL_SUPPORTED_OPTIONS   "@:a:b|c|d^D:e:f^gh^i|I:l^m:no|p:P^r:s|t|T+G^v^w|x:z"
72
73
74 /*******************************************************************************
75  *
76  * FUNCTION:    AslCommandLine
77  *
78  * PARAMETERS:  argc/argv
79  *
80  * RETURN:      Last argv index
81  *
82  * DESCRIPTION: Command line processing
83  *
84  ******************************************************************************/
85
86 int
87 AslCommandLine (
88     int                     argc,
89     char                    **argv)
90 {
91     int                     BadCommandLine = 0;
92     ACPI_STATUS             Status;
93
94
95     /* Minimum command line contains at least the command and an input file */
96
97     if (argc < 2)
98     {
99         printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
100         Usage ();
101         exit (1);
102     }
103
104     /* Process all command line options */
105
106     BadCommandLine = AslDoOptions (argc, argv, FALSE);
107
108     if (Gbl_DoTemplates)
109     {
110         Status = DtCreateTemplates (argv);
111         if (ACPI_FAILURE (Status))
112         {
113             exit (-1);
114         }
115         exit (1);
116     }
117
118     /* Next parameter must be the input filename */
119
120     if (!argv[AcpiGbl_Optind] &&
121         !Gbl_DisasmFlag)
122     {
123         printf ("Missing input filename\n");
124         BadCommandLine = TRUE;
125     }
126
127     if (Gbl_DoSignon)
128     {
129         printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
130         if (Gbl_IgnoreErrors)
131         {
132             printf ("Ignoring all errors, forcing AML file generation\n\n");
133         }
134     }
135
136     if (BadCommandLine)
137     {
138         printf ("Use -h option for help information\n");
139         exit (1);
140     }
141
142     return (AcpiGbl_Optind);
143 }
144
145
146 /*******************************************************************************
147  *
148  * FUNCTION:    AslDoOptions
149  *
150  * PARAMETERS:  argc/argv           - Standard argc/argv
151  *              IsResponseFile      - TRUE if executing a response file.
152  *
153  * RETURN:      Status
154  *
155  * DESCRIPTION: Command line option processing
156  *
157  ******************************************************************************/
158
159 static int
160 AslDoOptions (
161     int                     argc,
162     char                    **argv,
163     BOOLEAN                 IsResponseFile)
164 {
165     ACPI_STATUS             Status;
166     UINT32                  j;
167
168
169     /* Get the command line options */
170
171     while ((j = AcpiGetopt (argc, argv, ASL_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
172     {
173     case '@':   /* Begin a response file */
174
175         if (IsResponseFile)
176         {
177             printf ("Nested command files are not supported\n");
178             return (-1);
179         }
180
181         if (AslDoResponseFile (AcpiGbl_Optarg))
182         {
183             return (-1);
184         }
185         break;
186
187     case 'a':   /* Debug options */
188
189         switch (AcpiGbl_Optarg[0])
190         {
191         case 'r':
192
193             Gbl_EnableReferenceTypechecking = TRUE;
194             break;
195
196         default:
197
198             printf ("Unknown option: -a%s\n", AcpiGbl_Optarg);
199             return (-1);
200         }
201
202         break;
203
204
205     case 'b':   /* Debug options */
206
207         switch (AcpiGbl_Optarg[0])
208         {
209         case 'f':
210
211             AslCompilerdebug = 1; /* same as yydebug */
212             DtParserdebug = 1;
213             PrParserdebug = 1;
214             Gbl_DebugFlag = TRUE;
215             Gbl_KeepPreprocessorTempFile = TRUE;
216             break;
217
218         case 'p':   /* Prune ASL parse tree */
219
220             /* Get the required argument */
221
222             if (AcpiGetoptArgument (argc, argv))
223             {
224                 return (-1);
225             }
226
227             Gbl_PruneParseTree = TRUE;
228             Gbl_PruneDepth = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
229             break;
230
231         case 's':
232
233             Gbl_DebugFlag = TRUE;
234             break;
235
236         case 't':
237
238             /* Get the required argument */
239
240             if (AcpiGetoptArgument (argc, argv))
241             {
242                 return (-1);
243             }
244
245             Gbl_PruneType = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
246             break;
247
248         default:
249
250             printf ("Unknown option: -b%s\n", AcpiGbl_Optarg);
251             return (-1);
252         }
253
254         break;
255
256     case 'c':
257
258         switch (AcpiGbl_Optarg[0])
259         {
260         case 'r':
261
262             Gbl_NoResourceChecking = TRUE;
263             break;
264
265         default:
266
267             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
268             return (-1);
269         }
270         break;
271
272     case 'd':   /* Disassembler */
273
274         switch (AcpiGbl_Optarg[0])
275         {
276         case '^':
277
278             /* Get the required argument */
279
280             if (AcpiGetoptArgument (argc, argv))
281             {
282                 return (-1);
283             }
284
285             Gbl_DoCompile = FALSE;
286             break;
287
288         case 'a':
289
290             /* Get the required argument */
291
292             if (AcpiGetoptArgument (argc, argv))
293             {
294                 return (-1);
295             }
296
297             Gbl_DoCompile = FALSE;
298             Gbl_DisassembleAll = TRUE;
299             break;
300
301         case 'b':   /* Do not convert buffers to resource descriptors */
302
303             AcpiGbl_NoResourceDisassembly = TRUE;
304             break;
305
306         case 'c':
307
308             break;
309
310         case 'f':
311
312             AcpiGbl_ForceAmlDisassembly = TRUE;
313             break;
314
315         case 'l':   /* Use legacy ASL code (not ASL+) for disassembly */
316
317             Gbl_DoCompile = FALSE;
318             AcpiGbl_CstyleDisassembly = FALSE;
319             break;
320
321         default:
322
323             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
324             return (-1);
325         }
326
327         Gbl_DisasmFlag = TRUE;
328         break;
329
330     case 'D':   /* Define a symbol */
331
332         PrAddDefine (AcpiGbl_Optarg, NULL, TRUE);
333         break;
334
335     case 'e':   /* External files for disassembler */
336
337         /* Get entire list of external files */
338
339         AcpiGbl_Optind--;
340         argv[AcpiGbl_Optind] = AcpiGbl_Optarg;
341
342         while (argv[AcpiGbl_Optind] &&
343               (argv[AcpiGbl_Optind][0] != '-'))
344         {
345             Status = AcpiDmAddToExternalFileList (argv[AcpiGbl_Optind]);
346             if (ACPI_FAILURE (Status))
347             {
348                 printf ("Could not add %s to external list\n",
349                     argv[AcpiGbl_Optind]);
350                 return (-1);
351             }
352
353             AcpiGbl_Optind++;
354         }
355         break;
356
357     case 'f':
358
359         switch (AcpiGbl_Optarg[0])
360         {
361         case '^':   /* Ignore errors and force creation of aml file */
362
363             Gbl_IgnoreErrors = TRUE;
364             break;
365
366         case 'e':   /* Disassembler: Get external declaration file */
367
368             if (AcpiGetoptArgument (argc, argv))
369             {
370                 return (-1);
371             }
372
373             Gbl_ExternalRefFilename = AcpiGbl_Optarg;
374             break;
375
376         default:
377
378             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
379             return (-1);
380         }
381         break;
382
383     case 'G':
384
385         Gbl_CompileGeneric = TRUE;
386         break;
387
388     case 'g':   /* Get all ACPI tables */
389
390         printf ("-g option is deprecated, use acpidump utility instead\n");
391         exit (1);
392
393     case 'h':
394
395         switch (AcpiGbl_Optarg[0])
396         {
397         case '^':
398
399             Usage ();
400             exit (0);
401
402         case 'c':
403
404             UtDisplayConstantOpcodes ();
405             exit (0);
406
407         case 'd':
408
409             AslDisassemblyHelp ();
410             exit (0);
411
412         case 'f':
413
414             AslFilenameHelp ();
415             exit (0);
416
417         case 'r':
418
419             /* reserved names */
420
421             ApDisplayReservedNames ();
422             exit (0);
423
424         case 't':
425
426             UtDisplaySupportedTables ();
427             exit (0);
428
429         default:
430
431             printf ("Unknown option: -h%s\n", AcpiGbl_Optarg);
432             return (-1);
433         }
434
435     case 'I':   /* Add an include file search directory */
436
437         FlAddIncludeDirectory (AcpiGbl_Optarg);
438         break;
439
440     case 'i':   /* Output AML as an include file */
441
442         switch (AcpiGbl_Optarg[0])
443         {
444         case 'a':
445
446             /* Produce assembly code include file */
447
448             Gbl_AsmIncludeOutputFlag = TRUE;
449             break;
450
451         case 'c':
452
453             /* Produce C include file */
454
455             Gbl_C_IncludeOutputFlag = TRUE;
456             break;
457
458         case 'n':
459
460             /* Compiler/Disassembler: Ignore the NOOP operator */
461
462             AcpiGbl_IgnoreNoopOperator = TRUE;
463             break;
464
465         default:
466
467             printf ("Unknown option: -i%s\n", AcpiGbl_Optarg);
468             return (-1);
469         }
470         break;
471
472     case 'l':   /* Listing files */
473
474         switch (AcpiGbl_Optarg[0])
475         {
476         case '^':
477
478             /* Produce listing file (Mixed source/aml) */
479
480             Gbl_ListingFlag = TRUE;
481             AcpiGbl_DmOpt_Listing = TRUE;
482             break;
483
484         case 'i':
485
486             /* Produce preprocessor output file */
487
488             Gbl_PreprocessorOutputFlag = TRUE;
489             break;
490
491         case 'm':
492
493             /* Produce hardware map summary file */
494
495             Gbl_MapfileFlag = TRUE;
496             break;
497
498         case 'n':
499
500             /* Produce namespace file */
501
502             Gbl_NsOutputFlag = TRUE;
503             break;
504
505         case 's':
506
507             /* Produce combined source file */
508
509             Gbl_SourceOutputFlag = TRUE;
510             break;
511
512         case 'x':
513
514             /* Produce cross-reference file */
515
516             Gbl_CrossReferenceOutput = TRUE;
517             break;
518
519         default:
520
521             printf ("Unknown option: -l%s\n", AcpiGbl_Optarg);
522             return (-1);
523         }
524         break;
525
526     case 'm':   /* Set line buffer size */
527
528         Gbl_LineBufferSize = (UINT32) strtoul (AcpiGbl_Optarg, NULL, 0) * 1024;
529         if (Gbl_LineBufferSize < ASL_DEFAULT_LINE_BUFFER_SIZE)
530         {
531             Gbl_LineBufferSize = ASL_DEFAULT_LINE_BUFFER_SIZE;
532         }
533         printf ("Line Buffer Size: %u\n", Gbl_LineBufferSize);
534         break;
535
536     case 'n':   /* Parse only */
537
538         Gbl_ParseOnlyFlag = TRUE;
539         break;
540
541     case 'o':   /* Control compiler AML optimizations */
542
543         switch (AcpiGbl_Optarg[0])
544         {
545         case 'a':
546
547             /* Disable all optimizations */
548
549             Gbl_FoldConstants = FALSE;
550             Gbl_IntegerOptimizationFlag = FALSE;
551             Gbl_ReferenceOptimizationFlag = FALSE;
552             break;
553
554         case 'c':
555
556             /* Display compile time(s) */
557
558             Gbl_CompileTimesFlag = TRUE;
559             break;
560
561         case 'd':
562
563             /* Disable disassembler code optimizations */
564
565             AcpiGbl_DoDisassemblerOptimizations = FALSE;
566             break;
567
568         case 'e':
569
570             /* iASL: Disable External opcode generation */
571
572             Gbl_DoExternals = FALSE;
573
574             /* Disassembler: Emit embedded external operators */
575
576             AcpiGbl_DmEmitExternalOpcodes = TRUE;
577             break;
578
579         case 'f':
580
581             /* Disable folding on "normal" expressions */
582
583             Gbl_FoldConstants = FALSE;
584             break;
585
586         case 'i':
587
588             /* Disable integer optimization to constants */
589
590             Gbl_IntegerOptimizationFlag = FALSE;
591             break;
592
593         case 'n':
594
595             /* Disable named reference optimization */
596
597             Gbl_ReferenceOptimizationFlag = FALSE;
598             break;
599
600         case 't':
601
602             /* Disable heavy typechecking */
603
604             Gbl_DoTypechecking = FALSE;
605             break;
606
607         default:
608
609             printf ("Unknown option: -c%s\n", AcpiGbl_Optarg);
610             return (-1);
611         }
612         break;
613
614     case 'P':   /* Preprocessor options */
615
616         switch (AcpiGbl_Optarg[0])
617         {
618         case '^':   /* Proprocess only, emit (.i) file */
619
620             Gbl_PreprocessOnly = TRUE;
621             Gbl_PreprocessorOutputFlag = TRUE;
622             break;
623
624         case 'n':   /* Disable preprocessor */
625
626             Gbl_PreprocessFlag = FALSE;
627             break;
628
629         default:
630
631             printf ("Unknown option: -P%s\n", AcpiGbl_Optarg);
632             return (-1);
633         }
634         break;
635
636     case 'p':   /* Override default AML output filename */
637
638         Gbl_OutputFilenamePrefix = AcpiGbl_Optarg;
639         UtConvertBackslashes (Gbl_OutputFilenamePrefix);
640         Gbl_UseDefaultAmlFilename = FALSE;
641         break;
642
643     case 'r':   /* Override revision found in table header */
644
645         Gbl_RevisionOverride = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
646         break;
647
648     case 's':   /* Create AML in a source code file */
649
650         switch (AcpiGbl_Optarg[0])
651         {
652         case 'a':
653
654             /* Produce assembly code output file */
655
656             Gbl_AsmOutputFlag = TRUE;
657             break;
658
659         case 'c':
660
661             /* Produce C hex output file */
662
663             Gbl_C_OutputFlag = TRUE;
664             break;
665
666         case 'o':
667
668             /* Produce AML offset table in C */
669
670             Gbl_C_OffsetTableFlag = TRUE;
671             break;
672
673         default:
674
675             printf ("Unknown option: -s%s\n", AcpiGbl_Optarg);
676             return (-1);
677         }
678         break;
679
680     case 't':   /* Produce hex table output file */
681
682         switch (AcpiGbl_Optarg[0])
683         {
684         case 'a':
685
686             Gbl_HexOutputFlag = HEX_OUTPUT_ASM;
687             break;
688
689         case 'c':
690
691             Gbl_HexOutputFlag = HEX_OUTPUT_C;
692             break;
693
694         case 's':
695
696             Gbl_HexOutputFlag = HEX_OUTPUT_ASL;
697             break;
698
699         default:
700
701             printf ("Unknown option: -t%s\n", AcpiGbl_Optarg);
702             return (-1);
703         }
704         break;
705
706     case 'T':   /* Create a ACPI table template file */
707
708         Gbl_DoTemplates = TRUE;
709         break;
710
711     case 'v':   /* Version and verbosity settings */
712
713         switch (AcpiGbl_Optarg[0])
714         {
715         case '^':
716
717             printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
718             exit (0);
719
720         case 'a':
721
722             /* Disable all error/warning/remark messages */
723
724             Gbl_NoErrors = TRUE;
725             break;
726
727         case 'e':
728
729             /* Disable all warning/remark messages (errors only) */
730
731             Gbl_DisplayRemarks = FALSE;
732             Gbl_DisplayWarnings = FALSE;
733             break;
734
735         case 'i':
736             /*
737              * Support for integrated development environment(s).
738              *
739              * 1) No compiler signon
740              * 2) Send stderr messages to stdout
741              * 3) Less verbose error messages (single line only for each)
742              * 4) Error/warning messages are formatted appropriately to
743              *    be recognized by MS Visual Studio
744              */
745             Gbl_VerboseErrors = FALSE;
746             Gbl_DoSignon = FALSE;
747             Gbl_Files[ASL_FILE_STDERR].Handle = stdout;
748             break;
749
750         case 'o':
751
752             Gbl_DisplayOptimizations = TRUE;
753             break;
754
755         case 'r':
756
757             Gbl_DisplayRemarks = FALSE;
758             break;
759
760         case 's':
761
762             Gbl_DoSignon = FALSE;
763             break;
764
765         case 't':
766
767             Gbl_VerboseTemplates = TRUE;
768             break;
769
770         case 'w':
771
772             /* Get the required argument */
773
774             if (AcpiGetoptArgument (argc, argv))
775             {
776                 return (-1);
777             }
778
779             Status = AslDisableException (AcpiGbl_Optarg);
780             if (ACPI_FAILURE (Status))
781             {
782                 return (-1);
783             }
784             break;
785
786         default:
787
788             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
789             return (-1);
790         }
791         break;
792
793     case 'w': /* Set warning levels */
794
795         switch (AcpiGbl_Optarg[0])
796         {
797         case '1':
798
799             Gbl_WarningLevel = ASL_WARNING;
800             break;
801
802         case '2':
803
804             Gbl_WarningLevel = ASL_WARNING2;
805             break;
806
807         case '3':
808
809             Gbl_WarningLevel = ASL_WARNING3;
810             break;
811
812         case 'e':
813
814             Gbl_WarningsAsErrors = TRUE;
815             break;
816
817         default:
818
819             printf ("Unknown option: -w%s\n", AcpiGbl_Optarg);
820             return (-1);
821         }
822         break;
823
824     case 'x':   /* Set debug print output level */
825
826         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 16);
827         break;
828
829     case 'z':
830
831         Gbl_UseOriginalCompilerId = TRUE;
832         break;
833
834     default:
835
836         return (-1);
837     }
838
839     return (0);
840 }
841
842
843 /*******************************************************************************
844  *
845  * FUNCTION:    AslMergeOptionTokens
846  *
847  * PARAMETERS:  InBuffer            - Input containing an option string
848  *              OutBuffer           - Merged output buffer
849  *
850  * RETURN:      None
851  *
852  * DESCRIPTION: Remove all whitespace from an option string.
853  *
854  ******************************************************************************/
855
856 static void
857 AslMergeOptionTokens (
858     char                    *InBuffer,
859     char                    *OutBuffer)
860 {
861     char                    *Token;
862
863
864     *OutBuffer = 0;
865
866     Token = strtok (InBuffer, ASL_TOKEN_SEPARATORS);
867     while (Token)
868     {
869         strcat (OutBuffer, Token);
870         Token = strtok (NULL, ASL_TOKEN_SEPARATORS);
871     }
872 }
873
874
875 /*******************************************************************************
876  *
877  * FUNCTION:    AslDoResponseFile
878  *
879  * PARAMETERS:  Filename        - Name of the response file
880  *
881  * RETURN:      Status
882  *
883  * DESCRIPTION: Open a response file and process all options within.
884  *
885  ******************************************************************************/
886
887 static int
888 AslDoResponseFile (
889     char                    *Filename)
890 {
891     char                    *argv = StringBuffer2;
892     FILE                    *ResponseFile;
893     int                     OptStatus = 0;
894     int                     Opterr;
895     int                     Optind;
896
897
898     ResponseFile = fopen (Filename, "r");
899     if (!ResponseFile)
900     {
901         printf ("Could not open command file %s, %s\n",
902             Filename, strerror (errno));
903         return (-1);
904     }
905
906     /* Must save the current GetOpt globals */
907
908     Opterr = AcpiGbl_Opterr;
909     Optind = AcpiGbl_Optind;
910
911     /*
912      * Process all lines in the response file. There must be one complete
913      * option per line
914      */
915     while (fgets (StringBuffer, ASL_MSG_BUFFER_SIZE, ResponseFile))
916     {
917         /* Compress all tokens, allowing us to use a single argv entry */
918
919         AslMergeOptionTokens (StringBuffer, StringBuffer2);
920
921         /* Process the option */
922
923         AcpiGbl_Opterr = 0;
924         AcpiGbl_Optind = 0;
925
926         OptStatus = AslDoOptions (1, &argv, TRUE);
927         if (OptStatus)
928         {
929             printf ("Invalid option in command file %s: %s\n",
930                 Filename, StringBuffer);
931             break;
932         }
933     }
934
935     /* Restore the GetOpt globals */
936
937     AcpiGbl_Opterr = Opterr;
938     AcpiGbl_Optind = Optind;
939
940     fclose (ResponseFile);
941     return (OptStatus);
942 }