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 - 2017, 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 "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "acdisasm.h"
155 #include "acnamesp.h"
156 #include "amlcode.h"
157 #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 UtReallocLineBuffers (
179     char                    **Buffer,
180     UINT32                  OldSize,
181     UINT32                  NewSize);
182 
183 
184 /*******************************************************************************
185  *
186  * FUNCTION:    UtIsBigEndianMachine
187  *
188  * PARAMETERS:  None
189  *
190  * RETURN:      TRUE if machine is big endian
191  *              FALSE if machine is little endian
192  *
193  * DESCRIPTION: Detect whether machine is little endian or big endian.
194  *
195  ******************************************************************************/
196 
197 UINT8
198 UtIsBigEndianMachine (
199     void)
200 {
201     union {
202         UINT32              Integer;
203         UINT8               Bytes[4];
204     } Overlay =                 {0xFF000000};
205 
206 
207     return (Overlay.Bytes[0]); /* Returns 0xFF (TRUE) for big endian */
208 }
209 
210 
211 /******************************************************************************
212  *
213  * FUNCTION:    UtQueryForOverwrite
214  *
215  * PARAMETERS:  Pathname            - Output filename
216  *
217  * RETURN:      TRUE if file does not exist or overwrite is authorized
218  *
219  * DESCRIPTION: Query for file overwrite if it already exists.
220  *
221  ******************************************************************************/
222 
223 BOOLEAN
224 UtQueryForOverwrite (
225     char                    *Pathname)
226 {
227     struct stat             StatInfo;
228 
229 
230     if (!stat (Pathname, &StatInfo))
231     {
232         fprintf (stderr, "Target file \"%s\" already exists, overwrite? [y|n] ",
233             Pathname);
234 
235         if (getchar () != 'y')
236         {
237             return (FALSE);
238         }
239     }
240 
241     return (TRUE);
242 }
243 
244 
245 /*******************************************************************************
246  *
247  * FUNCTION:    UtDisplaySupportedTables
248  *
249  * PARAMETERS:  None
250  *
251  * RETURN:      None
252  *
253  * DESCRIPTION: Print all supported ACPI table names.
254  *
255  ******************************************************************************/
256 
257 void
258 UtDisplaySupportedTables (
259     void)
260 {
261     const AH_TABLE          *TableData;
262     UINT32                  i;
263 
264 
265     printf ("\nACPI tables supported by iASL version %8.8X:\n"
266         "  (Compiler, Disassembler, Template Generator)\n\n",
267         ACPI_CA_VERSION);
268 
269     /* All ACPI tables with the common table header */
270 
271     printf ("\n  Supported ACPI tables:\n");
272     for (TableData = Gbl_AcpiSupportedTables, i = 1;
273          TableData->Signature; TableData++, i++)
274     {
275         printf ("%8u) %s    %s\n", i,
276             TableData->Signature, TableData->Description);
277     }
278 }
279 
280 
281 /*******************************************************************************
282  *
283  * FUNCTION:    UtDisplayConstantOpcodes
284  *
285  * PARAMETERS:  None
286  *
287  * RETURN:      None
288  *
289  * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
290  *
291  ******************************************************************************/
292 
293 void
294 UtDisplayConstantOpcodes (
295     void)
296 {
297     UINT32                  i;
298 
299 
300     printf ("Constant expression opcode information\n\n");
301 
302     for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
303     {
304         if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
305         {
306             printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
307         }
308     }
309 }
310 
311 
312 /*******************************************************************************
313  *
314  * FUNCTION:    UtLocalCalloc
315  *
316  * PARAMETERS:  Size                - Bytes to be allocated
317  *
318  * RETURN:      Pointer to the allocated memory. Guaranteed to be valid.
319  *
320  * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
321  *              allocation failure, on the assumption that nothing more can be
322  *              accomplished.
323  *
324  ******************************************************************************/
325 
326 void *
327 UtLocalCalloc (
328     UINT32                  Size)
329 {
330     void                    *Allocated;
331 
332 
333     Allocated = ACPI_ALLOCATE_ZEROED (Size);
334     if (!Allocated)
335     {
336         AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
337             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
338             Gbl_InputByteCount, Gbl_CurrentColumn,
339             Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
340 
341         CmCleanupAndExit ();
342         exit (1);
343     }
344 
345     TotalAllocations++;
346     TotalAllocated += Size;
347     return (Allocated);
348 }
349 
350 
351 /*******************************************************************************
352  *
353  * FUNCTION:    UtBeginEvent
354  *
355  * PARAMETERS:  Name                - Ascii name of this event
356  *
357  * RETURN:      Event number (integer index)
358  *
359  * DESCRIPTION: Saves the current time with this event
360  *
361  ******************************************************************************/
362 
363 UINT8
364 UtBeginEvent (
365     char                    *Name)
366 {
367 
368     if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
369     {
370         AcpiOsPrintf ("Ran out of compiler event structs!\n");
371         return (AslGbl_NextEvent);
372     }
373 
374     /* Init event with current (start) time */
375 
376     AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
377     AslGbl_Events[AslGbl_NextEvent].EventName = Name;
378     AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
379     return (AslGbl_NextEvent++);
380 }
381 
382 
383 /*******************************************************************************
384  *
385  * FUNCTION:    UtEndEvent
386  *
387  * PARAMETERS:  Event               - Event number (integer index)
388  *
389  * RETURN:      None
390  *
391  * DESCRIPTION: Saves the current time (end time) with this event
392  *
393  ******************************************************************************/
394 
395 void
396 UtEndEvent (
397     UINT8                   Event)
398 {
399 
400     if (Event >= ASL_NUM_EVENTS)
401     {
402         return;
403     }
404 
405     /* Insert end time for event */
406 
407     AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
408 }
409 
410 
411 /*******************************************************************************
412  *
413  * FUNCTION:    DbgPrint
414  *
415  * PARAMETERS:  Type                - Type of output
416  *              Fmt                 - Printf format string
417  *              ...                 - variable printf list
418  *
419  * RETURN:      None
420  *
421  * DESCRIPTION: Conditional print statement. Prints to stderr only if the
422  *              debug flag is set.
423  *
424  ******************************************************************************/
425 
426 void
427 DbgPrint (
428     UINT32                  Type,
429     char                    *Fmt,
430     ...)
431 {
432     va_list                 Args;
433 
434 
435     if (!Gbl_DebugFlag)
436     {
437         return;
438     }
439 
440     if ((Type == ASL_PARSE_OUTPUT) &&
441         (!(AslCompilerdebug)))
442     {
443         return;
444     }
445 
446     va_start (Args, Fmt);
447     (void) vfprintf (stderr, Fmt, Args);
448     va_end (Args);
449     return;
450 }
451 
452 
453 /*******************************************************************************
454  *
455  * FUNCTION:    UtSetParseOpName
456  *
457  * PARAMETERS:  Op                  - Parse op to be named.
458  *
459  * RETURN:      None
460  *
461  * DESCRIPTION: Insert the ascii name of the parse opcode
462  *
463  ******************************************************************************/
464 
465 void
466 UtSetParseOpName (
467     ACPI_PARSE_OBJECT       *Op)
468 {
469 
470     strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
471         ACPI_MAX_PARSEOP_NAME);
472 }
473 
474 
475 /*******************************************************************************
476  *
477  * FUNCTION:    UtDisplaySummary
478  *
479  * PARAMETERS:  FileID              - ID of outpout file
480  *
481  * RETURN:      None
482  *
483  * DESCRIPTION: Display compilation statistics
484  *
485  ******************************************************************************/
486 
487 void
488 UtDisplaySummary (
489     UINT32                  FileId)
490 {
491     UINT32                  i;
492 
493 
494     if (FileId != ASL_FILE_STDOUT)
495     {
496         /* Compiler name and version number */
497 
498         FlPrintFile (FileId, "%s version %X [%s]\n\n",
499             ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, __DATE__);
500     }
501 
502     /* Summary of main input and output files */
503 
504     if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
505     {
506         FlPrintFile (FileId,
507             "%-14s %s - %u lines, %u bytes, %u fields\n",
508             "Table Input:",
509             Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
510             Gbl_InputByteCount, Gbl_InputFieldCount);
511 
512         if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
513         {
514             FlPrintFile (FileId,
515                 "%-14s %s - %u bytes\n",
516                 "Binary Output:",
517                 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
518         }
519     }
520     else
521     {
522         FlPrintFile (FileId,
523             "%-14s %s - %u lines, %u bytes, %u keywords\n",
524             "ASL Input:",
525             Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
526             Gbl_OriginalInputFileSize, TotalKeywords);
527 
528         /* AML summary */
529 
530         if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
531         {
532             if (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
533             {
534                 FlPrintFile (FileId,
535                     "%-14s %s - %u bytes, %u named objects, "
536                     "%u executable opcodes\n",
537                     "AML Output:",
538                     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename,
539                     FlGetFileSize (ASL_FILE_AML_OUTPUT),
540                     TotalNamedObjects, TotalExecutableOpcodes);
541             }
542         }
543     }
544 
545     /* Display summary of any optional files */
546 
547     for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
548     {
549         if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
550         {
551             continue;
552         }
553 
554         /* .SRC is a temp file unless specifically requested */
555 
556         if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
557         {
558             continue;
559         }
560 
561         /* .PRE is the preprocessor intermediate file */
562 
563         if ((i == ASL_FILE_PREPROCESSOR)  && (!Gbl_KeepPreprocessorTempFile))
564         {
565             continue;
566         }
567 
568         FlPrintFile (FileId, "%14s %s - %u bytes\n",
569             Gbl_Files[i].ShortDescription,
570             Gbl_Files[i].Filename, FlGetFileSize (i));
571     }
572 
573     /* Error summary */
574 
575     FlPrintFile (FileId,
576         "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
577         Gbl_ExceptionCount[ASL_ERROR],
578         Gbl_ExceptionCount[ASL_WARNING] +
579             Gbl_ExceptionCount[ASL_WARNING2] +
580             Gbl_ExceptionCount[ASL_WARNING3],
581         Gbl_ExceptionCount[ASL_REMARK]);
582 
583     if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
584     {
585         FlPrintFile (FileId, ", %u Optimizations",
586             Gbl_ExceptionCount[ASL_OPTIMIZATION]);
587 
588         if (TotalFolds)
589         {
590             FlPrintFile (FileId, ", %u Constants Folded", TotalFolds);
591         }
592     }
593 
594     FlPrintFile (FileId, "\n");
595 }
596 
597 
598 /*******************************************************************************
599  *
600  * FUNCTION:    UtCheckIntegerRange
601  *
602  * PARAMETERS:  Op                  - Integer parse node
603  *              LowValue            - Smallest allowed value
604  *              HighValue           - Largest allowed value
605  *
606  * RETURN:      Op if OK, otherwise NULL
607  *
608  * DESCRIPTION: Check integer for an allowable range
609  *
610  ******************************************************************************/
611 
612 ACPI_PARSE_OBJECT *
613 UtCheckIntegerRange (
614     ACPI_PARSE_OBJECT       *Op,
615     UINT32                  LowValue,
616     UINT32                  HighValue)
617 {
618 
619     if (!Op)
620     {
621         return (NULL);
622     }
623 
624     if ((Op->Asl.Value.Integer < LowValue) ||
625         (Op->Asl.Value.Integer > HighValue))
626     {
627         sprintf (MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
628             (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
629 
630         AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer);
631         return (NULL);
632     }
633 
634     return (Op);
635 }
636 
637 
638 /*******************************************************************************
639  *
640  * FUNCTION:    UtStringCacheCalloc
641  *
642  * PARAMETERS:  Length              - Size of buffer requested
643  *
644  * RETURN:      Pointer to the buffer. Aborts compiler on allocation failure
645  *
646  * DESCRIPTION: Allocate a string buffer. Bypass the local
647  *              dynamic memory manager for performance reasons (This has a
648  *              major impact on the speed of the compiler.)
649  *
650  ******************************************************************************/
651 
652 char *
653 UtStringCacheCalloc (
654     UINT32                  Length)
655 {
656     char                    *Buffer;
657     ASL_CACHE_INFO          *Cache;
658     UINT32                  CacheSize = ASL_STRING_CACHE_SIZE;
659 
660 
661     if (Length > CacheSize)
662     {
663         CacheSize = Length;
664 
665         if (Gbl_StringCacheList)
666         {
667             Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
668 
669             /* Link new cache buffer just following head of list */
670 
671             Cache->Next = Gbl_StringCacheList->Next;
672             Gbl_StringCacheList->Next = Cache;
673 
674             /* Leave cache management pointers alone as they pertain to head */
675 
676             Gbl_StringCount++;
677             Gbl_StringSize += Length;
678 
679             return (Cache->Buffer);
680         }
681     }
682 
683     if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
684     {
685         /* Allocate a new buffer */
686 
687         Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
688 
689         /* Link new cache buffer to head of list */
690 
691         Cache->Next = Gbl_StringCacheList;
692         Gbl_StringCacheList = Cache;
693 
694         /* Setup cache management pointers */
695 
696         Gbl_StringCacheNext = Cache->Buffer;
697         Gbl_StringCacheLast = Gbl_StringCacheNext + CacheSize;
698     }
699 
700     Gbl_StringCount++;
701     Gbl_StringSize += Length;
702 
703     Buffer = Gbl_StringCacheNext;
704     Gbl_StringCacheNext += Length;
705     return (Buffer);
706 }
707 
708 
709 /******************************************************************************
710  *
711  * FUNCTION:    UtExpandLineBuffers
712  *
713  * PARAMETERS:  None. Updates global line buffer pointers.
714  *
715  * RETURN:      None. Reallocates the global line buffers
716  *
717  * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
718  *              all global line buffers and updates Gbl_LineBufferSize. NOTE:
719  *              Also used for the initial allocation of the buffers, when
720  *              all of the buffer pointers are NULL. Initial allocations are
721  *              of size ASL_DEFAULT_LINE_BUFFER_SIZE
722  *
723  *****************************************************************************/
724 
725 void
726 UtExpandLineBuffers (
727     void)
728 {
729     UINT32                  NewSize;
730 
731 
732     /* Attempt to double the size of all line buffers */
733 
734     NewSize = Gbl_LineBufferSize * 2;
735     if (Gbl_CurrentLineBuffer)
736     {
737         DbgPrint (ASL_DEBUG_OUTPUT,
738             "Increasing line buffer size from %u to %u\n",
739             Gbl_LineBufferSize, NewSize);
740     }
741 
742     UtReallocLineBuffers (&Gbl_CurrentLineBuffer, Gbl_LineBufferSize, NewSize);
743     UtReallocLineBuffers (&Gbl_MainTokenBuffer, Gbl_LineBufferSize, NewSize);
744     UtReallocLineBuffers (&Gbl_MacroTokenBuffer, Gbl_LineBufferSize, NewSize);
745     UtReallocLineBuffers (&Gbl_ExpressionTokenBuffer, Gbl_LineBufferSize, NewSize);
746 
747     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
748     Gbl_LineBufferSize = NewSize;
749 }
750 
751 
752 /******************************************************************************
753  *
754  * FUNCTION:    UtReallocLineBuffers
755  *
756  * PARAMETERS:  Buffer              - Buffer to realloc
757  *              OldSize             - Old size of Buffer
758  *              NewSize             - New size of Buffer
759  *
760  * RETURN:      none
761  *
762  * DESCRIPTION: Reallocate and initialize Buffer
763  *
764  *****************************************************************************/
765 
766 static void
767 UtReallocLineBuffers (
768     char                    **Buffer,
769     UINT32                  OldSize,
770     UINT32                  NewSize)
771 {
772 
773     *Buffer = realloc (*Buffer, NewSize);
774     if (*Buffer)
775     {
776         memset (*Buffer + OldSize, 0, NewSize - OldSize);
777         return;
778     }
779 
780     printf ("Could not increase line buffer size from %u to %u\n",
781         OldSize, NewSize);
782 
783     AslError (ASL_ERROR, ASL_MSG_BUFFER_ALLOCATION, NULL, NULL);
784     AslAbort ();
785 }
786 
787 
788 /******************************************************************************
789  *
790  * FUNCTION:    UtFreeLineBuffers
791  *
792  * PARAMETERS:  None
793  *
794  * RETURN:      None
795  *
796  * DESCRIPTION: Free all line buffers
797  *
798  *****************************************************************************/
799 
800 void
801 UtFreeLineBuffers (
802     void)
803 {
804 
805     free (Gbl_CurrentLineBuffer);
806     free (Gbl_MainTokenBuffer);
807     free (Gbl_MacroTokenBuffer);
808     free (Gbl_ExpressionTokenBuffer);
809 }
810 
811 
812 /*******************************************************************************
813  *
814  * FUNCTION:    UtInternalizeName
815  *
816  * PARAMETERS:  ExternalName        - Name to convert
817  *              ConvertedName       - Where the converted name is returned
818  *
819  * RETURN:      Status
820  *
821  * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
822  *
823  ******************************************************************************/
824 
825 ACPI_STATUS
826 UtInternalizeName (
827     char                    *ExternalName,
828     char                    **ConvertedName)
829 {
830     ACPI_NAMESTRING_INFO    Info;
831     ACPI_STATUS             Status;
832 
833 
834     if (!ExternalName)
835     {
836         return (AE_OK);
837     }
838 
839     /* Get the length of the new internal name */
840 
841     Info.ExternalName = ExternalName;
842     AcpiNsGetInternalNameLength (&Info);
843 
844     /* We need a segment to store the internal name */
845 
846     Info.InternalName = UtStringCacheCalloc (Info.Length);
847 
848     /* Build the name */
849 
850     Status = AcpiNsBuildInternalName (&Info);
851     if (ACPI_FAILURE (Status))
852     {
853         return (Status);
854     }
855 
856     *ConvertedName = Info.InternalName;
857     return (AE_OK);
858 }
859 
860 
861 /*******************************************************************************
862  *
863  * FUNCTION:    UtPadNameWithUnderscores
864  *
865  * PARAMETERS:  NameSeg             - Input nameseg
866  *              PaddedNameSeg       - Output padded nameseg
867  *
868  * RETURN:      Padded nameseg.
869  *
870  * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
871  *              ACPI_NAME.
872  *
873  ******************************************************************************/
874 
875 static void
876 UtPadNameWithUnderscores (
877     char                    *NameSeg,
878     char                    *PaddedNameSeg)
879 {
880     UINT32                  i;
881 
882 
883     for (i = 0; (i < ACPI_NAME_SIZE); i++)
884     {
885         if (*NameSeg)
886         {
887             *PaddedNameSeg = *NameSeg;
888             NameSeg++;
889         }
890         else
891         {
892             *PaddedNameSeg = '_';
893         }
894 
895         PaddedNameSeg++;
896     }
897 }
898 
899 
900 /*******************************************************************************
901  *
902  * FUNCTION:    UtAttachNameseg
903  *
904  * PARAMETERS:  Op                  - Parent parse node
905  *              Name                - Full ExternalName
906  *
907  * RETURN:      None; Sets the NameSeg field in parent node
908  *
909  * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
910  *              in the NameSeg field of the Op.
911  *
912  ******************************************************************************/
913 
914 static void
915 UtAttachNameseg (
916     ACPI_PARSE_OBJECT       *Op,
917     char                    *Name)
918 {
919     char                    *NameSeg;
920     char                    PaddedNameSeg[4];
921 
922 
923     if (!Name)
924     {
925         return;
926     }
927 
928     /* Look for the last dot in the namepath */
929 
930     NameSeg = strrchr (Name, '.');
931     if (NameSeg)
932     {
933         /* Found last dot, we have also found the final nameseg */
934 
935         NameSeg++;
936         UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
937     }
938     else
939     {
940         /* No dots in the namepath, there is only a single nameseg. */
941         /* Handle prefixes */
942 
943         while (ACPI_IS_ROOT_PREFIX (*Name) ||
944                ACPI_IS_PARENT_PREFIX (*Name))
945         {
946             Name++;
947         }
948 
949         /* Remaining string should be one single nameseg */
950 
951         UtPadNameWithUnderscores (Name, PaddedNameSeg);
952     }
953 
954     ACPI_MOVE_NAME (Op->Asl.NameSeg, PaddedNameSeg);
955 }
956 
957 
958 /*******************************************************************************
959  *
960  * FUNCTION:    UtAttachNamepathToOwner
961  *
962  * PARAMETERS:  Op                  - Parent parse node
963  *              NameOp              - Node that contains the name
964  *
965  * RETURN:      Sets the ExternalName and Namepath in the parent node
966  *
967  * DESCRIPTION: Store the name in two forms in the parent node: The original
968  *              (external) name, and the internalized name that is used within
969  *              the ACPI namespace manager.
970  *
971  ******************************************************************************/
972 
973 void
974 UtAttachNamepathToOwner (
975     ACPI_PARSE_OBJECT       *Op,
976     ACPI_PARSE_OBJECT       *NameOp)
977 {
978     ACPI_STATUS             Status;
979 
980 
981     /* Full external path */
982 
983     Op->Asl.ExternalName = NameOp->Asl.Value.String;
984 
985     /* Save the NameOp for possible error reporting later */
986 
987     Op->Asl.ParentMethod = (void *) NameOp;
988 
989     /* Last nameseg of the path */
990 
991     UtAttachNameseg (Op, Op->Asl.ExternalName);
992 
993     /* Create internalized path */
994 
995     Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
996     if (ACPI_FAILURE (Status))
997     {
998         /* TBD: abort on no memory */
999     }
1000 }
1001 
1002 
1003 /*******************************************************************************
1004  *
1005  * FUNCTION:    UtDoConstant
1006  *
1007  * PARAMETERS:  String              - Hex/Decimal/Octal
1008  *
1009  * RETURN:      Converted Integer
1010  *
1011  * DESCRIPTION: Convert a string to an integer, with overflow/error checking.
1012  *
1013  ******************************************************************************/
1014 
1015 UINT64
1016 UtDoConstant (
1017     char                    *String)
1018 {
1019     ACPI_STATUS             Status;
1020     UINT64                  ConvertedInteger;
1021     char                    ErrBuf[64];
1022 
1023 
1024     Status = AcpiUtStrtoul64 (String, &ConvertedInteger);
1025     if (ACPI_FAILURE (Status))
1026     {
1027         sprintf (ErrBuf, "While creating 64-bit constant: %s\n",
1028             AcpiFormatException (Status));
1029 
1030         AslCommonError (ASL_ERROR, ASL_MSG_SYNTAX, Gbl_CurrentLineNumber,
1031             Gbl_LogicalLineNumber, Gbl_CurrentLineOffset,
1032             Gbl_CurrentColumn, Gbl_Files[ASL_FILE_INPUT].Filename, ErrBuf);
1033     }
1034 
1035     return (ConvertedInteger);
1036 }
1037