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