xref: /freebsd/sys/contrib/dev/acpica/compiler/aslutils.c (revision 8b238f4126d32df3e70056bc32536b7248ebffa0)
1 /******************************************************************************
2  *
3  * Module Name: aslutils -- compiler utilities
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2019, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/include/acdisasm.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 #include <contrib/dev/acpica/include/amlcode.h>
157 #include <contrib/dev/acpica/include/acapps.h>
158 #include <sys/stat.h>
159 
160 
161 #define _COMPONENT          ACPI_COMPILER
162         ACPI_MODULE_NAME    ("aslutils")
163 
164 
165 /* Local prototypes */
166 
167 static void
168 UtPadNameWithUnderscores (
169     char                    *NameSeg,
170     char                    *PaddedNameSeg);
171 
172 static void
173 UtAttachNameseg (
174     ACPI_PARSE_OBJECT       *Op,
175     char                    *Name);
176 
177 static void
178 UtDisplayErrorSummary (
179     UINT32                  FileId);
180 
181 
182 /*******************************************************************************
183  *
184  * FUNCTION:    UtIsBigEndianMachine
185  *
186  * PARAMETERS:  None
187  *
188  * RETURN:      TRUE if machine is big endian
189  *              FALSE if machine is little endian
190  *
191  * DESCRIPTION: Detect whether machine is little endian or big endian.
192  *
193  ******************************************************************************/
194 
195 UINT8
196 UtIsBigEndianMachine (
197     void)
198 {
199     union {
200         UINT32              Integer;
201         UINT8               Bytes[4];
202     } Overlay =                 {0xFF000000};
203 
204 
205     return (Overlay.Bytes[0]); /* Returns 0xFF (TRUE) for big endian */
206 }
207 
208 
209 /******************************************************************************
210  *
211  * FUNCTION:    UtQueryForOverwrite
212  *
213  * PARAMETERS:  Pathname            - Output filename
214  *
215  * RETURN:      TRUE if file does not exist or overwrite is authorized
216  *
217  * DESCRIPTION: Query for file overwrite if it already exists.
218  *
219  ******************************************************************************/
220 
221 BOOLEAN
222 UtQueryForOverwrite (
223     char                    *Pathname)
224 {
225     struct stat             StatInfo;
226     int                     InChar;
227 
228 
229     if (!stat (Pathname, &StatInfo))
230     {
231         fprintf (stderr, "Target file \"%s\" already exists, overwrite? [y|n] ",
232             Pathname);
233 
234         InChar = fgetc (stdin);
235         if (InChar == '\n')
236         {
237             InChar = fgetc (stdin);
238         }
239 
240         if ((InChar != 'y') && (InChar != 'Y'))
241         {
242             return (FALSE);
243         }
244     }
245 
246     return (TRUE);
247 }
248 
249 
250 /*******************************************************************************
251  *
252  * FUNCTION:    UtNodeIsDescendantOf
253  *
254  * PARAMETERS:  Node1                   - Child node
255  *              Node2                   - Possible parent node
256  *
257  * RETURN:      Boolean
258  *
259  * DESCRIPTION: Returns TRUE if Node1 is a descendant of Node2. Otherwise,
260  *              return FALSE. Note, we assume a NULL Node2 element to be the
261  *              topmost (root) scope. All nodes are descendants of the root.
262  *              Note: Nodes at the same level (siblings) are not considered
263  *              descendants.
264  *
265  ******************************************************************************/
266 
267 BOOLEAN
268 UtNodeIsDescendantOf (
269     ACPI_NAMESPACE_NODE     *Node1,
270     ACPI_NAMESPACE_NODE     *Node2)
271 {
272 
273     if (Node1 == Node2)
274     {
275         return (FALSE);
276     }
277 
278     if (!Node2)
279     {
280         return (TRUE); /* All nodes descend from the root */
281     }
282 
283     /* Walk upward until the root is reached or parent is found */
284 
285     while (Node1)
286     {
287         if (Node1 == Node2)
288         {
289             return (TRUE);
290         }
291 
292         Node1 = Node1->Parent;
293     }
294 
295     return (FALSE);
296 }
297 
298 
299 /*******************************************************************************
300  *
301  * FUNCTION:    UtGetParentMethod
302  *
303  * PARAMETERS:  Node                    - Namespace node for any object
304  *
305  * RETURN:      Namespace node for the parent method
306  *              NULL - object is not within a method
307  *
308  * DESCRIPTION: Find the parent (owning) method node for a namespace object
309  *
310  ******************************************************************************/
311 
312 void *
313 UtGetParentMethod (
314     ACPI_NAMESPACE_NODE     *Node)
315 {
316     ACPI_NAMESPACE_NODE     *ParentNode;
317 
318 
319     if (!Node)
320     {
321         return (NULL);
322     }
323 
324     /* Walk upward until a method is found, or the root is reached */
325 
326     ParentNode = Node->Parent;
327     while (ParentNode)
328     {
329         if (ParentNode->Type == ACPI_TYPE_METHOD)
330         {
331             return (ParentNode);
332         }
333 
334         ParentNode = ParentNode->Parent;
335     }
336 
337     return (NULL); /* Object is not within a control method */
338 }
339 
340 
341 /*******************************************************************************
342  *
343  * FUNCTION:    UtDisplaySupportedTables
344  *
345  * PARAMETERS:  None
346  *
347  * RETURN:      None
348  *
349  * DESCRIPTION: Print all supported ACPI table names.
350  *
351  ******************************************************************************/
352 
353 void
354 UtDisplaySupportedTables (
355     void)
356 {
357     const AH_TABLE          *TableData;
358     UINT32                  i;
359 
360 
361     printf ("\nACPI tables supported by iASL version %8.8X:\n"
362         "  (Compiler, Disassembler, Template Generator)\n\n",
363         ACPI_CA_VERSION);
364 
365     /* All ACPI tables with the common table header */
366 
367     printf ("\n  Supported ACPI tables:\n");
368     for (TableData = AcpiGbl_SupportedTables, i = 1;
369          TableData->Signature; TableData++, i++)
370     {
371         printf ("%8u) %s    %s\n", i,
372             TableData->Signature, TableData->Description);
373     }
374 }
375 
376 
377 /*******************************************************************************
378  *
379  * FUNCTION:    UtDisplayConstantOpcodes
380  *
381  * PARAMETERS:  None
382  *
383  * RETURN:      None
384  *
385  * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
386  *
387  ******************************************************************************/
388 
389 void
390 UtDisplayConstantOpcodes (
391     void)
392 {
393     UINT32                  i;
394 
395 
396     printf ("Constant expression opcode information\n\n");
397 
398     for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
399     {
400         if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
401         {
402             printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
403         }
404     }
405 }
406 
407 
408 /*******************************************************************************
409  *
410  * FUNCTION:    UtBeginEvent
411  *
412  * PARAMETERS:  Name                - Ascii name of this event
413  *
414  * RETURN:      Event number (integer index)
415  *
416  * DESCRIPTION: Saves the current time with this event
417  *
418  ******************************************************************************/
419 
420 UINT8
421 UtBeginEvent (
422     char                    *Name)
423 {
424 
425     if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
426     {
427         AcpiOsPrintf ("Ran out of compiler event structs!\n");
428         return (AslGbl_NextEvent);
429     }
430 
431     /* Init event with current (start) time */
432 
433     AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
434     AslGbl_Events[AslGbl_NextEvent].EventName = Name;
435     AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
436     return (AslGbl_NextEvent++);
437 }
438 
439 
440 /*******************************************************************************
441  *
442  * FUNCTION:    UtEndEvent
443  *
444  * PARAMETERS:  Event               - Event number (integer index)
445  *
446  * RETURN:      None
447  *
448  * DESCRIPTION: Saves the current time (end time) with this event
449  *
450  ******************************************************************************/
451 
452 void
453 UtEndEvent (
454     UINT8                   Event)
455 {
456 
457     if (Event >= ASL_NUM_EVENTS)
458     {
459         return;
460     }
461 
462     /* Insert end time for event */
463 
464     AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
465 }
466 
467 
468 /*******************************************************************************
469  *
470  * FUNCTION:    DbgPrint
471  *
472  * PARAMETERS:  Type                - Type of output
473  *              Fmt                 - Printf format string
474  *              ...                 - variable printf list
475  *
476  * RETURN:      None
477  *
478  * DESCRIPTION: Conditional print statement. Prints to stderr only if the
479  *              debug flag is set.
480  *
481  ******************************************************************************/
482 
483 void
484 DbgPrint (
485     UINT32                  Type,
486     char                    *Fmt,
487     ...)
488 {
489     va_list                 Args;
490 
491 
492     if (!AslGbl_DebugFlag)
493     {
494         return;
495     }
496 
497     if ((Type == ASL_PARSE_OUTPUT) &&
498         (!(AslCompilerdebug)))
499     {
500         return;
501     }
502 
503     va_start (Args, Fmt);
504     (void) vfprintf (stderr, Fmt, Args);
505     va_end (Args);
506     return;
507 }
508 
509 
510 /*******************************************************************************
511  *
512  * FUNCTION:    UtSetParseOpName
513  *
514  * PARAMETERS:  Op                  - Parse op to be named.
515  *
516  * RETURN:      None
517  *
518  * DESCRIPTION: Insert the ascii name of the parse opcode
519  *
520  ******************************************************************************/
521 
522 void
523 UtSetParseOpName (
524     ACPI_PARSE_OBJECT       *Op)
525 {
526 
527     AcpiUtSafeStrncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
528         ACPI_MAX_PARSEOP_NAME);
529 }
530 
531 
532 /*******************************************************************************
533  *
534  * FUNCTION:    UtDisplayOneSummary
535  *
536  * PARAMETERS:  FileID              - ID of outpout file
537  *
538  * RETURN:      None
539  *
540  * DESCRIPTION: Display compilation statistics for one input file
541  *
542  ******************************************************************************/
543 
544 void
545 UtDisplayOneSummary (
546     UINT32                  FileId,
547     BOOLEAN                 DisplayErrorSummary)
548 {
549     UINT32                  i;
550     ASL_GLOBAL_FILE_NODE    *FileNode;
551     BOOLEAN                 DisplayAMLSummary;
552 
553 
554     DisplayAMLSummary =
555         !AslGbl_PreprocessOnly && !AslGbl_ParserErrorDetected &&
556         ((AslGbl_ExceptionCount[ASL_ERROR] == 0) || AslGbl_IgnoreErrors) &&
557         AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle;
558 
559     if (FileId != ASL_FILE_STDOUT)
560     {
561         /* Compiler name and version number */
562 
563         FlPrintFile (FileId, "%s version %X\n\n",
564             ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION);
565     }
566 
567     /* Summary of main input and output files */
568 
569     FileNode = FlGetCurrentFileNode ();
570     if (!FileNode)
571     {
572         fprintf (stderr, "Summary could not be generated");
573         return;
574     }
575 
576     if (FileNode->ParserErrorDetected)
577     {
578         FlPrintFile (FileId,
579             "%-14s %s - Compilation aborted due to parser-detected syntax error(s)\n",
580             "Input file:", AslGbl_Files[ASL_FILE_INPUT].Filename);
581     }
582     else if (FileNode->FileType == ASL_INPUT_TYPE_ASCII_DATA)
583     {
584         FlPrintFile (FileId,
585             "%-14s %s - %7u bytes %6u fields %8u source lines\n",
586             "Table Input:",
587             AslGbl_Files[ASL_FILE_INPUT].Filename,
588             FileNode->OriginalInputFileSize, FileNode->TotalFields,
589             FileNode->TotalLineCount);
590 
591         FlPrintFile (FileId,
592             "%-14s %s - %7u bytes\n",
593             "Binary Output:",
594             AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename, FileNode->OutputByteLength);
595     }
596     else if (FileNode->FileType == ASL_INPUT_TYPE_ASCII_ASL)
597     {
598         FlPrintFile (FileId,
599             "%-14s %s - %7u bytes %6u keywords %6u source lines\n",
600             "ASL Input:",
601             AslGbl_Files[ASL_FILE_INPUT].Filename,
602             FileNode->OriginalInputFileSize,
603             FileNode->TotalKeywords,
604             FileNode->TotalLineCount);
605 
606         /* AML summary */
607 
608         if (DisplayAMLSummary)
609         {
610             FlPrintFile (FileId,
611                 "%-14s %s - %7u bytes %6u opcodes  %6u named objects\n",
612                 "AML Output:",
613                 AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename,
614                 FlGetFileSize (ASL_FILE_AML_OUTPUT),
615                 FileNode->TotalExecutableOpcodes,
616                 FileNode->TotalNamedObjects);
617         }
618     }
619 
620     /* Display summary of any optional files */
621 
622     for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
623     {
624         if (!AslGbl_Files[i].Filename || !AslGbl_Files[i].Handle)
625         {
626             continue;
627         }
628 
629         /* .SRC is a temp file unless specifically requested */
630 
631         if ((i == ASL_FILE_SOURCE_OUTPUT) && (!AslGbl_SourceOutputFlag))
632         {
633             continue;
634         }
635 
636         /* .PRE is the preprocessor intermediate file */
637 
638         if ((i == ASL_FILE_PREPROCESSOR)  && (!AslGbl_KeepPreprocessorTempFile))
639         {
640             continue;
641         }
642 
643         FlPrintFile (FileId, "%-14s %s - %7u bytes\n",
644             AslGbl_FileDescs[i].ShortDescription,
645             AslGbl_Files[i].Filename, FlGetFileSize (i));
646     }
647 
648 
649     /*
650      * Optionally emit an error summary for a file. This is used to enhance the
651      * appearance of listing files.
652      */
653     if (DisplayErrorSummary)
654     {
655         UtDisplayErrorSummary (FileId);
656     }
657 }
658 
659 
660 /*******************************************************************************
661  *
662  * FUNCTION:    UtDisplayErrorSummary
663  *
664  * PARAMETERS:  FileID              - ID of outpout file
665  *
666  * RETURN:      None
667  *
668  * DESCRIPTION: Display compilation statistics for all input files
669  *
670  ******************************************************************************/
671 
672 static void
673 UtDisplayErrorSummary (
674     UINT32                  FileId)
675 {
676     BOOLEAN                 ErrorDetected;
677 
678 
679     ErrorDetected = AslGbl_ParserErrorDetected ||
680         ((AslGbl_ExceptionCount[ASL_ERROR] > 0) && !AslGbl_IgnoreErrors);
681 
682     if (ErrorDetected)
683     {
684         FlPrintFile (FileId, "\nCompilation failed. ");
685     }
686     else
687     {
688         FlPrintFile (FileId, "\nCompilation successful. ");
689     }
690 
691     FlPrintFile (FileId,
692         "%u Errors, %u Warnings, %u Remarks",
693         AslGbl_ExceptionCount[ASL_ERROR],
694         AslGbl_ExceptionCount[ASL_WARNING] +
695             AslGbl_ExceptionCount[ASL_WARNING2] +
696             AslGbl_ExceptionCount[ASL_WARNING3],
697         AslGbl_ExceptionCount[ASL_REMARK]);
698 
699     if (AslGbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
700     {
701         if (AslGbl_ParserErrorDetected)
702         {
703             FlPrintFile (FileId,
704                 "\nNo AML files were generated due to syntax error(s)\n");
705             return;
706         }
707         else if (ErrorDetected)
708         {
709             FlPrintFile (FileId,
710                 "\nNo AML files were generated due to compiler error(s)\n");
711             return;
712         }
713 
714         FlPrintFile (FileId, ", %u Optimizations",
715             AslGbl_ExceptionCount[ASL_OPTIMIZATION]);
716 
717         if (AslGbl_TotalFolds)
718         {
719             FlPrintFile (FileId, ", %u Constants Folded", AslGbl_TotalFolds);
720         }
721     }
722 
723     FlPrintFile (FileId, "\n");
724 }
725 
726 
727 /*******************************************************************************
728  *
729  * FUNCTION:    UtDisplaySummary
730  *
731  * PARAMETERS:  FileID              - ID of outpout file
732  *
733  * RETURN:      None
734  *
735  * DESCRIPTION: Display compilation statistics for all input files
736  *
737  ******************************************************************************/
738 
739 void
740 UtDisplaySummary (
741     UINT32                  FileId)
742 {
743     ASL_GLOBAL_FILE_NODE    *Current = AslGbl_FilesList;
744 
745 
746     while (Current)
747     {
748         switch  (FlSwitchFileSet(Current->Files[ASL_FILE_INPUT].Filename))
749         {
750             case SWITCH_TO_SAME_FILE:
751             case SWITCH_TO_DIFFERENT_FILE:
752 
753                 UtDisplayOneSummary (FileId, FALSE);
754                 Current = Current->Next;
755                 break;
756 
757             case FILE_NOT_FOUND:
758             default:
759 
760                 Current = NULL;
761                 break;
762         }
763     }
764     UtDisplayErrorSummary (FileId);
765 }
766 
767 /*******************************************************************************
768  *
769  * FUNCTION:    UtCheckIntegerRange
770  *
771  * PARAMETERS:  Op                  - Integer parse node
772  *              LowValue            - Smallest allowed value
773  *              HighValue           - Largest allowed value
774  *
775  * RETURN:      Op if OK, otherwise NULL
776  *
777  * DESCRIPTION: Check integer for an allowable range
778  *
779  ******************************************************************************/
780 
781 ACPI_PARSE_OBJECT *
782 UtCheckIntegerRange (
783     ACPI_PARSE_OBJECT       *Op,
784     UINT32                  LowValue,
785     UINT32                  HighValue)
786 {
787 
788     if (!Op)
789     {
790         return (NULL);
791     }
792 
793     if ((Op->Asl.Value.Integer < LowValue) ||
794         (Op->Asl.Value.Integer > HighValue))
795     {
796         sprintf (AslGbl_MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
797             (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
798 
799         AslError (ASL_ERROR, ASL_MSG_RANGE, Op, AslGbl_MsgBuffer);
800         return (NULL);
801     }
802 
803     return (Op);
804 }
805 
806 
807 /*******************************************************************************
808  *
809  * FUNCTION:    UtInternalizeName
810  *
811  * PARAMETERS:  ExternalName        - Name to convert
812  *              ConvertedName       - Where the converted name is returned
813  *
814  * RETURN:      Status
815  *
816  * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
817  *
818  ******************************************************************************/
819 
820 ACPI_STATUS
821 UtInternalizeName (
822     char                    *ExternalName,
823     char                    **ConvertedName)
824 {
825     ACPI_NAMESTRING_INFO    Info;
826     ACPI_STATUS             Status;
827 
828 
829     if (!ExternalName)
830     {
831         return (AE_OK);
832     }
833 
834     /* Get the length of the new internal name */
835 
836     Info.ExternalName = ExternalName;
837     AcpiNsGetInternalNameLength (&Info);
838 
839     /* We need a segment to store the internal name */
840 
841     Info.InternalName = UtLocalCacheCalloc (Info.Length);
842 
843     /* Build the name */
844 
845     Status = AcpiNsBuildInternalName (&Info);
846     if (ACPI_FAILURE (Status))
847     {
848         return (Status);
849     }
850 
851     *ConvertedName = Info.InternalName;
852     return (AE_OK);
853 }
854 
855 
856 /*******************************************************************************
857  *
858  * FUNCTION:    UtPadNameWithUnderscores
859  *
860  * PARAMETERS:  NameSeg             - Input nameseg
861  *              PaddedNameSeg       - Output padded nameseg
862  *
863  * RETURN:      Padded nameseg.
864  *
865  * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
866  *              ACPI_NAME.
867  *
868  ******************************************************************************/
869 
870 static void
871 UtPadNameWithUnderscores (
872     char                    *NameSeg,
873     char                    *PaddedNameSeg)
874 {
875     UINT32                  i;
876 
877 
878     for (i = 0; (i < ACPI_NAMESEG_SIZE); i++)
879     {
880         if (*NameSeg)
881         {
882             *PaddedNameSeg = *NameSeg;
883             NameSeg++;
884         }
885         else
886         {
887             *PaddedNameSeg = '_';
888         }
889 
890         PaddedNameSeg++;
891     }
892 }
893 
894 
895 /*******************************************************************************
896  *
897  * FUNCTION:    UtAttachNameseg
898  *
899  * PARAMETERS:  Op                  - Parent parse node
900  *              Name                - Full ExternalName
901  *
902  * RETURN:      None; Sets the NameSeg field in parent node
903  *
904  * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
905  *              in the NameSeg field of the Op.
906  *
907  ******************************************************************************/
908 
909 static void
910 UtAttachNameseg (
911     ACPI_PARSE_OBJECT       *Op,
912     char                    *Name)
913 {
914     char                    *NameSeg;
915     char                    PaddedNameSeg[4];
916 
917 
918     if (!Name)
919     {
920         return;
921     }
922 
923     /* Look for the last dot in the namepath */
924 
925     NameSeg = strrchr (Name, '.');
926     if (NameSeg)
927     {
928         /* Found last dot, we have also found the final nameseg */
929 
930         NameSeg++;
931         UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
932     }
933     else
934     {
935         /* No dots in the namepath, there is only a single nameseg. */
936         /* Handle prefixes */
937 
938         while (ACPI_IS_ROOT_PREFIX (*Name) ||
939                ACPI_IS_PARENT_PREFIX (*Name))
940         {
941             Name++;
942         }
943 
944         /* Remaining string should be one single nameseg */
945 
946         UtPadNameWithUnderscores (Name, PaddedNameSeg);
947     }
948 
949     ACPI_COPY_NAMESEG (Op->Asl.NameSeg, PaddedNameSeg);
950 }
951 
952 
953 /*******************************************************************************
954  *
955  * FUNCTION:    UtAttachNamepathToOwner
956  *
957  * PARAMETERS:  Op                  - Parent parse node
958  *              NameOp              - Node that contains the name
959  *
960  * RETURN:      Sets the ExternalName and Namepath in the parent node
961  *
962  * DESCRIPTION: Store the name in two forms in the parent node: The original
963  *              (external) name, and the internalized name that is used within
964  *              the ACPI namespace manager.
965  *
966  ******************************************************************************/
967 
968 void
969 UtAttachNamepathToOwner (
970     ACPI_PARSE_OBJECT       *Op,
971     ACPI_PARSE_OBJECT       *NameOp)
972 {
973     ACPI_STATUS             Status;
974 
975 
976     /* Full external path */
977 
978     Op->Asl.ExternalName = NameOp->Asl.Value.String;
979 
980     /* Save the NameOp for possible error reporting later */
981 
982     Op->Asl.ParentMethod = (void *) NameOp;
983 
984     /* Last nameseg of the path */
985 
986     UtAttachNameseg (Op, Op->Asl.ExternalName);
987 
988     /* Create internalized path */
989 
990     Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
991     if (ACPI_FAILURE (Status))
992     {
993         /* TBD: abort on no memory */
994     }
995 }
996 
997 
998 /*******************************************************************************
999  *
1000  * FUNCTION:    UtNameContainsAllPrefix
1001  *
1002  * PARAMETERS:  Op                  - Op containing NameString
1003  *
1004  * RETURN:      NameString consists of all ^ characters
1005  *
1006  * DESCRIPTION: Determine if this Op contains a name segment that consists of
1007  *              all '^' characters.
1008  *
1009  ******************************************************************************/
1010 
1011 BOOLEAN
1012 UtNameContainsAllPrefix (
1013     ACPI_PARSE_OBJECT       *Op)
1014 {
1015     UINT32                  Length = Op->Asl.AmlLength;
1016     UINT32                  i;
1017 
1018     for (i = 0; i < Length; i++)
1019     {
1020         if (Op->Asl.Value.String[i] != '^')
1021         {
1022             return (FALSE);
1023         }
1024     }
1025 
1026     return (TRUE);
1027 }
1028 
1029 /*******************************************************************************
1030  *
1031  * FUNCTION:    UtDoConstant
1032  *
1033  * PARAMETERS:  String              - Hex/Decimal/Octal
1034  *
1035  * RETURN:      Converted Integer
1036  *
1037  * DESCRIPTION: Convert a string to an integer, with overflow/error checking.
1038  *
1039  ******************************************************************************/
1040 
1041 UINT64
1042 UtDoConstant (
1043     char                    *String)
1044 {
1045     ACPI_STATUS             Status;
1046     UINT64                  ConvertedInteger;
1047     char                    ErrBuf[64];
1048 
1049 
1050     Status = AcpiUtStrtoul64 (String, &ConvertedInteger);
1051     if (ACPI_FAILURE (Status))
1052     {
1053         sprintf (ErrBuf, "While creating 64-bit constant: %s\n",
1054             AcpiFormatException (Status));
1055 
1056         AslCommonError (ASL_ERROR, ASL_MSG_SYNTAX, AslGbl_CurrentLineNumber,
1057             AslGbl_LogicalLineNumber, AslGbl_CurrentLineOffset,
1058             AslGbl_CurrentColumn, AslGbl_Files[ASL_FILE_INPUT].Filename, ErrBuf);
1059     }
1060 
1061     return (ConvertedInteger);
1062 }
1063 
1064 
1065 /******************************************************************************
1066  *
1067  * FUNCTION:    AcpiUtStrdup
1068  *
1069  * PARAMETERS:  String1             - string to duplicate
1070  *
1071  * RETURN:      int that signifies string relationship. Zero means strings
1072  *              are equal.
1073  *
1074  * DESCRIPTION: Duplicate the string using UtCacheAlloc to avoid manual memory
1075  *              reclamation.
1076  *
1077  ******************************************************************************/
1078 
1079 char *
1080 AcpiUtStrdup (
1081     char                    *String)
1082 {
1083     char                    *NewString = (char *) UtLocalCalloc (strlen (String) + 1);
1084 
1085 
1086     strcpy (NewString, String);
1087     return (NewString);
1088 }
1089 
1090 
1091 /******************************************************************************
1092  *
1093  * FUNCTION:    AcpiUtStrcat
1094  *
1095  * PARAMETERS:  String1
1096  *              String2
1097  *
1098  * RETURN:      New string with String1 concatenated with String2
1099  *
1100  * DESCRIPTION: Concatenate string1 and string2
1101  *
1102  ******************************************************************************/
1103 
1104 char *
1105 AcpiUtStrcat (
1106     char                    *String1,
1107     char                    *String2)
1108 {
1109     UINT32                  String1Length = strlen (String1);
1110     char                    *NewString = (char *) UtLocalCalloc (strlen (String1) + strlen (String2) + 1);
1111 
1112     strcpy (NewString, String1);
1113     strcpy (NewString + String1Length, String2);
1114     return (NewString);
1115 }
1116