1118611Snjl/******************************************************************************
2118611Snjl *
3118611Snjl * Module Name: aslmain - compiler main and utilities
4118611Snjl *
5118611Snjl *****************************************************************************/
6118611Snjl
7217365Sjkim/*
8281075Sdim * Copyright (C) 2000 - 2015, Intel Corp.
9118611Snjl * All rights reserved.
10118611Snjl *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
25118611Snjl *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
29118611Snjl *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
43118611Snjl
44118611Snjl#define _DECLARE_GLOBALS
45118611Snjl
46151937Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
47193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
48210976Sjkim#include <contrib/dev/acpica/include/acdisasm.h>
49240716Sjkim#include <signal.h>
50118611Snjl
51118611Snjl#define _COMPONENT          ACPI_COMPILER
52118611Snjl        ACPI_MODULE_NAME    ("aslmain")
53118611Snjl
54281075Sdim/*
55281075Sdim * Main routine for the iASL compiler.
56281075Sdim *
57281075Sdim * Portability note: The compiler depends upon the host for command-line
58281075Sdim * wildcard support - it is not implemented locally. For example:
59281075Sdim *
60281075Sdim * Linux/Unix systems: Shell expands wildcards automatically.
61281075Sdim *
62281075Sdim * Windows: The setargv.obj module must be linked in to automatically
63281075Sdim * expand wildcards.
64281075Sdim */
65250838Sjkim
66151937Sjkim/* Local prototypes */
67118611Snjl
68240716Sjkimstatic void ACPI_SYSTEM_XFACE
69240716SjkimAslSignalHandler (
70240716Sjkim    int                     Sig);
71240716Sjkim
72151937Sjkimstatic void
73151937SjkimAslInitialize (
74151937Sjkim    void);
75151937Sjkim
76281075SdimUINT8
77281075SdimAcpiIsBigEndianMachine (
78281075Sdim    void);
79151937Sjkim
80281075Sdim
81118611Snjl/*******************************************************************************
82118611Snjl *
83281075Sdim * FUNCTION:    AcpiIsBigEndianMachine
84281075Sdim *
85281075Sdim * PARAMETERS:  None
86281075Sdim *
87281075Sdim * RETURN:      TRUE if machine is big endian
88281075Sdim *              FALSE if machine is little endian
89281075Sdim *
90281075Sdim * DESCRIPTION: Detect whether machine is little endian or big endian.
91281075Sdim *
92281075Sdim ******************************************************************************/
93281075Sdim
94281075SdimUINT8
95281075SdimAcpiIsBigEndianMachine (
96281075Sdim    void)
97281075Sdim{
98281075Sdim    union {
99281075Sdim        UINT32              Integer;
100281075Sdim        UINT8               Bytes[4];
101281075Sdim    } Overlay = {0xFF000000};
102281075Sdim
103281075Sdim    return (Overlay.Bytes[0]); /* Returns 0xFF (TRUE) for big endian */
104281075Sdim}
105281075Sdim
106281075Sdim
107281075Sdim/*******************************************************************************
108281075Sdim *
109250838Sjkim * FUNCTION:    Usage
110118611Snjl *
111118611Snjl * PARAMETERS:  None
112118611Snjl *
113118611Snjl * RETURN:      None
114118611Snjl *
115246849Sjkim * DESCRIPTION: Display option help message.
116246849Sjkim *              Optional items in square brackets.
117118611Snjl *
118118611Snjl ******************************************************************************/
119118611Snjl
120250838Sjkimvoid
121250838SjkimUsage (
122118611Snjl    void)
123118611Snjl{
124250838Sjkim    printf ("%s\n\n", ASL_COMPLIANCE);
125250838Sjkim    ACPI_USAGE_HEADER ("iasl [Options] [Files]");
126118611Snjl
127281075Sdim    printf ("\nGeneral:\n");
128228110Sjkim    ACPI_OPTION ("-@ <file>",       "Specify command file");
129228110Sjkim    ACPI_OPTION ("-I <dir>",        "Specify additional include directory");
130241973Sjkim    ACPI_OPTION ("-T <sig>|ALL|*",  "Create table template file for ACPI <Sig>");
131281075Sdim    ACPI_OPTION ("-p <prefix>",     "Specify path/filename prefix for all output files");
132238381Sjkim    ACPI_OPTION ("-v",              "Display compiler version");
133281075Sdim    ACPI_OPTION ("-vo",             "Enable optimization comments");
134281075Sdim    ACPI_OPTION ("-vs",             "Disable signon");
135197104Sjkim
136281075Sdim    printf ("\nHelp:\n");
137281075Sdim    ACPI_OPTION ("-h",              "This message");
138281075Sdim    ACPI_OPTION ("-hc",             "Display operators allowed in constant expressions");
139281075Sdim    ACPI_OPTION ("-hf",             "Display help for output filename generation");
140281075Sdim    ACPI_OPTION ("-hr",             "Display ACPI reserved method names");
141281075Sdim    ACPI_OPTION ("-ht",             "Display currently supported ACPI table names");
142281075Sdim
143233250Sjkim    printf ("\nPreprocessor:\n");
144233250Sjkim    ACPI_OPTION ("-D <symbol>",     "Define symbol for preprocessor use");
145233250Sjkim    ACPI_OPTION ("-li",             "Create preprocessed output file (*.i)");
146233250Sjkim    ACPI_OPTION ("-P",              "Preprocess only and create preprocessor output file (*.i)");
147234623Sjkim    ACPI_OPTION ("-Pn",             "Disable preprocessor");
148233250Sjkim
149281075Sdim    printf ("\nErrors, Warnings, and Remarks:\n");
150281075Sdim    ACPI_OPTION ("-va",             "Disable all errors/warnings/remarks");
151281075Sdim    ACPI_OPTION ("-ve",             "Report only errors (ignore warnings and remarks)");
152228110Sjkim    ACPI_OPTION ("-vi",             "Less verbose errors and warnings for use with IDEs");
153228110Sjkim    ACPI_OPTION ("-vr",             "Disable remarks");
154250838Sjkim    ACPI_OPTION ("-vw <messageid>", "Disable specific warning or remark");
155233250Sjkim    ACPI_OPTION ("-w1 -w2 -w3",     "Set warning reporting level");
156234623Sjkim    ACPI_OPTION ("-we",             "Report warnings as errors");
157118611Snjl
158246849Sjkim    printf ("\nAML Code Generation (*.aml):\n");
159228110Sjkim    ACPI_OPTION ("-oa",             "Disable all optimizations (compatibility mode)");
160228110Sjkim    ACPI_OPTION ("-of",             "Disable constant folding");
161228110Sjkim    ACPI_OPTION ("-oi",             "Disable integer optimization to Zero/One/Ones");
162228110Sjkim    ACPI_OPTION ("-on",             "Disable named reference string optimization");
163228110Sjkim    ACPI_OPTION ("-cr",             "Disable Resource Descriptor error checking");
164245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp operators");
165228110Sjkim    ACPI_OPTION ("-r <revision>",   "Override table header Revision (1-255)");
166118611Snjl
167246849Sjkim    printf ("\nOptional Source Code Output Files:\n");
168246849Sjkim    ACPI_OPTION ("-sc -sa",         "Create source file in C or assembler (*.c or *.asm)");
169246849Sjkim    ACPI_OPTION ("-ic -ia",         "Create include file in C or assembler (*.h or *.inc)");
170246849Sjkim    ACPI_OPTION ("-tc -ta -ts",     "Create hex AML table in C, assembler, or ASL (*.hex)");
171249112Sjkim    ACPI_OPTION ("-so",             "Create offset table in C (*.offset.h)");
172246849Sjkim
173246849Sjkim    printf ("\nOptional Listing Files:\n");
174228110Sjkim    ACPI_OPTION ("-l",              "Create mixed listing file (ASL source and AML) (*.lst)");
175281075Sdim    ACPI_OPTION ("-lm",             "Create hardware summary map file (*.map)");
176228110Sjkim    ACPI_OPTION ("-ln",             "Create namespace file (*.nsp)");
177228110Sjkim    ACPI_OPTION ("-ls",             "Create combined source file (expanded includes) (*.src)");
178118611Snjl
179246849Sjkim    printf ("\nData Table Compiler:\n");
180246849Sjkim    ACPI_OPTION ("-G",              "Compile custom table that contains generic operators");
181246849Sjkim    ACPI_OPTION ("-vt",             "Create verbose template files (full disassembly)");
182209746Sjkim
183118611Snjl    printf ("\nAML Disassembler:\n");
184281075Sdim    ACPI_OPTION ("-d  <f1 f2 ...>", "Disassemble or decode binary ACPI tables to file (*.dsl)");
185246849Sjkim    ACPI_OPTION ("",                "  (Optional, file type is automatically detected)");
186281075Sdim    ACPI_OPTION ("-da <f1 f2 ...>", "Disassemble multiple tables from single namespace");
187240716Sjkim    ACPI_OPTION ("-db",             "Do not translate Buffers to Resource Templates");
188281075Sdim    ACPI_OPTION ("-dc <f1 f2 ...>", "Disassemble AML and immediately compile it");
189246849Sjkim    ACPI_OPTION ("",                "  (Obtain DSDT from current system if no input file)");
190281687Sjkim    ACPI_OPTION ("-df",             "Force disassembler to assume table contains valid AML");
191281075Sdim    ACPI_OPTION ("-dl",             "Emit legacy ASL code only (no C-style operators)");
192281075Sdim    ACPI_OPTION ("-e  <f1 f2 ...>", "Include ACPI table(s) for external symbol resolution");
193254745Sjkim    ACPI_OPTION ("-fe <file>",      "Specify external symbol declaration file");
194245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp opcodes");
195241973Sjkim    ACPI_OPTION ("-vt",             "Dump binary table data in hex format within output file");
196118611Snjl
197237412Sjkim    printf ("\nDebug Options:\n");
198281075Sdim    ACPI_OPTION ("-bf",             "Create debug file (full output) (*.txt)");
199281075Sdim    ACPI_OPTION ("-bs",             "Create debug file (parse tree only) (*.txt)");
200281075Sdim    ACPI_OPTION ("-bp <depth>",     "Prune ASL parse tree");
201281075Sdim    ACPI_OPTION ("-bt <type>",      "Object type to be pruned from the parse tree");
202237412Sjkim    ACPI_OPTION ("-f",              "Ignore errors, force creation of AML output file(s)");
203246849Sjkim    ACPI_OPTION ("-m <size>",       "Set internal line buffer size (in Kbytes)");
204237412Sjkim    ACPI_OPTION ("-n",              "Parse only, no output generation");
205237412Sjkim    ACPI_OPTION ("-ot",             "Display compile times and statistics");
206237412Sjkim    ACPI_OPTION ("-x <level>",      "Set debug level for trace output");
207237412Sjkim    ACPI_OPTION ("-z",              "Do not insert new compiler ID for DataTables");
208118611Snjl}
209118611Snjl
210118611Snjl
211118611Snjl/*******************************************************************************
212118611Snjl *
213237412Sjkim * FUNCTION:    FilenameHelp
214118611Snjl *
215118611Snjl * PARAMETERS:  None
216118611Snjl *
217118611Snjl * RETURN:      None
218118611Snjl *
219237412Sjkim * DESCRIPTION: Display help message for output filename generation
220118611Snjl *
221118611Snjl ******************************************************************************/
222118611Snjl
223250838Sjkimvoid
224250838SjkimAslFilenameHelp (
225118611Snjl    void)
226118611Snjl{
227118611Snjl
228228110Sjkim    printf ("\nAML output filename generation:\n");
229118611Snjl    printf ("  Output filenames are generated by appending an extension to a common\n");
230241973Sjkim    printf ("  filename prefix. The filename prefix is obtained via one of the\n");
231118611Snjl    printf ("  following methods (in priority order):\n");
232118611Snjl    printf ("    1) The -p option specifies the prefix\n");
233118611Snjl    printf ("    2) The prefix of the AMLFileName in the ASL Definition Block\n");
234118611Snjl    printf ("    3) The prefix of the input filename\n");
235118611Snjl    printf ("\n");
236118611Snjl}
237118611Snjl
238118611Snjl
239240716Sjkim/******************************************************************************
240240716Sjkim *
241240716Sjkim * FUNCTION:    AslSignalHandler
242240716Sjkim *
243240716Sjkim * PARAMETERS:  Sig                 - Signal that invoked this handler
244240716Sjkim *
245240716Sjkim * RETURN:      None
246240716Sjkim *
247240716Sjkim * DESCRIPTION: Control-C handler. Delete any intermediate files and any
248240716Sjkim *              output files that may be left in an indeterminate state.
249240716Sjkim *
250240716Sjkim *****************************************************************************/
251240716Sjkim
252240716Sjkimstatic void ACPI_SYSTEM_XFACE
253240716SjkimAslSignalHandler (
254240716Sjkim    int                     Sig)
255240716Sjkim{
256240716Sjkim    UINT32                  i;
257240716Sjkim
258240716Sjkim
259240716Sjkim    signal (Sig, SIG_IGN);
260240716Sjkim    printf ("Aborting\n\n");
261240716Sjkim
262240716Sjkim    /* Close all open files */
263240716Sjkim
264240716Sjkim    Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL; /* the .i file is same as source file */
265240716Sjkim
266240716Sjkim    for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
267240716Sjkim    {
268240716Sjkim        FlCloseFile (i);
269240716Sjkim    }
270240716Sjkim
271240716Sjkim    /* Delete any output files */
272240716Sjkim
273240716Sjkim    for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
274240716Sjkim    {
275240716Sjkim        FlDeleteFile (i);
276240716Sjkim    }
277240716Sjkim
278240716Sjkim    exit (0);
279240716Sjkim}
280240716Sjkim
281240716Sjkim
282118611Snjl/*******************************************************************************
283118611Snjl *
284118611Snjl * FUNCTION:    AslInitialize
285118611Snjl *
286118611Snjl * PARAMETERS:  None
287118611Snjl *
288118611Snjl * RETURN:      None
289118611Snjl *
290118611Snjl * DESCRIPTION: Initialize compiler globals
291118611Snjl *
292118611Snjl ******************************************************************************/
293118611Snjl
294151937Sjkimstatic void
295151937SjkimAslInitialize (
296151937Sjkim    void)
297118611Snjl{
298118611Snjl    UINT32                  i;
299118611Snjl
300118611Snjl
301118611Snjl    for (i = 0; i < ASL_NUM_FILES; i++)
302118611Snjl    {
303118611Snjl        Gbl_Files[i].Handle = NULL;
304118611Snjl        Gbl_Files[i].Filename = NULL;
305118611Snjl    }
306118611Snjl
307118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Handle   = stdout;
308118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";
309118611Snjl
310118611Snjl    Gbl_Files[ASL_FILE_STDERR].Handle   = stderr;
311118611Snjl    Gbl_Files[ASL_FILE_STDERR].Filename = "STDERR";
312118611Snjl}
313118611Snjl
314118611Snjl
315118611Snjl/*******************************************************************************
316118611Snjl *
317118611Snjl * FUNCTION:    main
318118611Snjl *
319118611Snjl * PARAMETERS:  Standard argc/argv
320118611Snjl *
321118611Snjl * RETURN:      Program termination code
322118611Snjl *
323193529Sjkim * DESCRIPTION: C main routine for the Asl Compiler. Handle command line
324193529Sjkim *              options and begin the compile for each file on the command line
325118611Snjl *
326118611Snjl ******************************************************************************/
327118611Snjl
328118611Snjlint ACPI_SYSTEM_XFACE
329118611Snjlmain (
330118611Snjl    int                     argc,
331118611Snjl    char                    **argv)
332118611Snjl{
333118611Snjl    ACPI_STATUS             Status;
334210976Sjkim    int                     Index1;
335210976Sjkim    int                     Index2;
336281075Sdim    int                     ReturnStatus = 0;
337118611Snjl
338118611Snjl
339281075Sdim    /*
340281075Sdim     * Big-endian machines are not currently supported. ACPI tables must
341281075Sdim     * be little-endian, and support for big-endian machines needs to
342281075Sdim     * be implemented.
343281075Sdim     */
344281075Sdim    if (AcpiIsBigEndianMachine ())
345281075Sdim    {
346281075Sdim        fprintf (stderr,
347281075Sdim            "iASL is not currently supported on big-endian machines.\n");
348281075Sdim        return (-1);
349281075Sdim    }
350281075Sdim
351281075Sdim    AcpiOsInitialize ();
352250838Sjkim    ACPI_DEBUG_INITIALIZE (); /* For debug version only */
353250838Sjkim
354250838Sjkim    /* Initialize preprocessor and compiler before command line processing */
355250838Sjkim
356240716Sjkim    signal (SIGINT, AslSignalHandler);
357210976Sjkim    AcpiGbl_ExternalFileList = NULL;
358240716Sjkim    AcpiDbgLevel = 0;
359250838Sjkim    PrInitializePreprocessor ();
360250838Sjkim    AslInitialize ();
361210976Sjkim
362250838Sjkim    Index1 = Index2 = AslCommandLine (argc, argv);
363151937Sjkim
364250838Sjkim    /* Allocate the line buffer(s), must be after command line */
365118611Snjl
366250838Sjkim    Gbl_LineBufferSize /= 2;
367250838Sjkim    UtExpandLineBuffers ();
368240716Sjkim
369250838Sjkim    /* Perform global actions first/only */
370118611Snjl
371210976Sjkim    if (Gbl_DisassembleAll)
372210976Sjkim    {
373210976Sjkim        while (argv[Index1])
374210976Sjkim        {
375281075Sdim            Status = AcpiDmAddToExternalFileList (argv[Index1]);
376210976Sjkim            if (ACPI_FAILURE (Status))
377210976Sjkim            {
378210976Sjkim                return (-1);
379210976Sjkim            }
380210976Sjkim
381210976Sjkim            Index1++;
382210976Sjkim        }
383210976Sjkim    }
384210976Sjkim
385193529Sjkim    /* Process each pathname/filename in the list, with possible wildcards */
386118611Snjl
387210976Sjkim    while (argv[Index2])
388118611Snjl    {
389281075Sdim        /*
390281075Sdim         * If -p not specified, we will use the input filename as the
391281075Sdim         * output filename prefix
392281075Sdim         */
393281075Sdim        if (Gbl_UseDefaultAmlFilename)
394281075Sdim        {
395281075Sdim            Gbl_OutputFilenamePrefix = argv[Index2];
396281075Sdim            UtConvertBackslashes (Gbl_OutputFilenamePrefix);
397281075Sdim        }
398281075Sdim
399281075Sdim        Status = AslDoOneFile (argv[Index2]);
400151937Sjkim        if (ACPI_FAILURE (Status))
401151937Sjkim        {
402281075Sdim            ReturnStatus = -1;
403281075Sdim            goto CleanupAndExit;
404151937Sjkim        }
405118611Snjl
406210976Sjkim        Index2++;
407118611Snjl    }
408118611Snjl
409281075Sdim
410281075SdimCleanupAndExit:
411281075Sdim
412281075Sdim    UtFreeLineBuffers ();
413281075Sdim    AslParserCleanup ();
414281075Sdim
415210976Sjkim    if (AcpiGbl_ExternalFileList)
416210976Sjkim    {
417210976Sjkim        AcpiDmClearExternalFileList();
418210976Sjkim    }
419210976Sjkim
420281075Sdim    return (ReturnStatus);
421118611Snjl}
422