xref: /freebsd/sys/contrib/dev/acpica/compiler/aslutils.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 
2 /******************************************************************************
3  *
4  * Module Name: aslutils -- compiler utilities
5  *              $Revision: 1.72 $
6  *
7  *****************************************************************************/
8 
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2007, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117 
118 
119 #include <contrib/dev/acpica/compiler/aslcompiler.h>
120 #include "aslcompiler.y.h"
121 #include <contrib/dev/acpica/acnamesp.h>
122 #include <contrib/dev/acpica/amlcode.h>
123 
124 #define _COMPONENT          ACPI_COMPILER
125         ACPI_MODULE_NAME    ("aslutils")
126 
127 #ifdef _USE_BERKELEY_YACC
128 extern const char * const       AslCompilername[];
129 static const char * const       *yytname = &AslCompilername[254];
130 #else
131 extern const char * const       yytname[];
132 #endif
133 
134 /* Local prototypes */
135 
136 static ACPI_STATUS
137 UtStrtoul64 (
138     char                    *String,
139     UINT32                  Base,
140     ACPI_INTEGER            *RetInteger);
141 
142 static void
143 UtPadNameWithUnderscores (
144     char                    *NameSeg,
145     char                    *PaddedNameSeg);
146 
147 static void
148 UtAttachNameseg (
149     ACPI_PARSE_OBJECT       *Op,
150     char                    *Name);
151 
152 
153 /*******************************************************************************
154  *
155  * FUNCTION:    AcpiPsDisplayConstantOpcodes
156  *
157  * PARAMETERS:  None
158  *
159  * RETURN:      None
160  *
161  * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
162  *
163  ******************************************************************************/
164 
165 void
166 UtDisplayConstantOpcodes (
167     void)
168 {
169     UINT32                  i;
170 
171 
172     printf ("Constant expression opcode information\n\n");
173 
174     for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
175     {
176         if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
177         {
178             printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
179         }
180     }
181 }
182 
183 
184 /*******************************************************************************
185  *
186  * FUNCTION:    UtLocalCalloc
187  *
188  * PARAMETERS:  Size        - Bytes to be allocated
189  *
190  * RETURN:      Pointer to the allocated memory.  Guaranteed to be valid.
191  *
192  * DESCRIPTION: Allocate zero-initialized memory.  Aborts the compile on an
193  *              allocation failure, on the assumption that nothing more can be
194  *              accomplished.
195  *
196  ******************************************************************************/
197 
198 void *
199 UtLocalCalloc (
200     UINT32                  Size)
201 {
202     void                    *Allocated;
203 
204 
205     Allocated = ACPI_ALLOCATE_ZEROED (Size);
206     if (!Allocated)
207     {
208         AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
209             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
210             Gbl_InputByteCount, Gbl_CurrentColumn,
211             Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
212         exit (1);
213     }
214 
215     TotalAllocations++;
216     TotalAllocated += Size;
217     return (Allocated);
218 }
219 
220 
221 /*******************************************************************************
222  *
223  * FUNCTION:    UtBeginEvent
224  *
225  * PARAMETERS:  Name        - Ascii name of this event
226  *
227  * RETURN:      Event       - Event number (integer index)
228  *
229  * DESCRIPTION: Saves the current time with this event
230  *
231  ******************************************************************************/
232 
233 UINT8
234 UtBeginEvent (
235     char                    *Name)
236 {
237 
238     if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
239     {
240         AcpiOsPrintf ("Ran out of compiler event structs!\n");
241         return (AslGbl_NextEvent);
242     }
243 
244     /* Init event with current (start) time */
245 
246     AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
247     AslGbl_Events[AslGbl_NextEvent].EventName = Name;
248     AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
249 
250     return (AslGbl_NextEvent++);
251 }
252 
253 
254 /*******************************************************************************
255  *
256  * FUNCTION:    UtEndEvent
257  *
258  * PARAMETERS:  Event       - Event number (integer index)
259  *
260  * RETURN:      None
261  *
262  * DESCRIPTION: Saves the current time (end time) with this event
263  *
264  ******************************************************************************/
265 
266 void
267 UtEndEvent (
268     UINT8                  Event)
269 {
270 
271     if (Event >= ASL_NUM_EVENTS)
272     {
273         return;
274     }
275 
276     /* Insert end time for event */
277 
278     AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
279 }
280 
281 
282 /*******************************************************************************
283  *
284  * FUNCTION:    UtHexCharToValue
285  *
286  * PARAMETERS:  HexChar         - Hex character in Ascii
287  *
288  * RETURN:      The binary value of the hex character
289  *
290  * DESCRIPTION: Perform ascii-to-hex translation
291  *
292  ******************************************************************************/
293 
294 UINT8
295 UtHexCharToValue (
296     int                     HexChar)
297 {
298 
299     if (HexChar <= 0x39)
300     {
301         return ((UINT8) (HexChar - 0x30));
302     }
303 
304     if (HexChar <= 0x46)
305     {
306         return ((UINT8) (HexChar - 0x37));
307     }
308 
309     return ((UINT8) (HexChar - 0x57));
310 }
311 
312 
313 /*******************************************************************************
314  *
315  * FUNCTION:    UtConvertByteToHex
316  *
317  * PARAMETERS:  RawByte         - Binary data
318  *              Buffer          - Pointer to where the hex bytes will be stored
319  *
320  * RETURN:      Ascii hex byte is stored in Buffer.
321  *
322  * DESCRIPTION: Perform hex-to-ascii translation.  The return data is prefixed
323  *              with "0x"
324  *
325  ******************************************************************************/
326 
327 void
328 UtConvertByteToHex (
329     UINT8                   RawByte,
330     UINT8                   *Buffer)
331 {
332 
333     Buffer[0] = '0';
334     Buffer[1] = 'x';
335 
336     Buffer[2] = (UINT8) hex[(RawByte >> 4) & 0xF];
337     Buffer[3] = (UINT8) hex[RawByte & 0xF];
338 }
339 
340 
341 /*******************************************************************************
342  *
343  * FUNCTION:    UtConvertByteToAsmHex
344  *
345  * PARAMETERS:  RawByte         - Binary data
346  *              Buffer          - Pointer to where the hex bytes will be stored
347  *
348  * RETURN:      Ascii hex byte is stored in Buffer.
349  *
350  * DESCRIPTION: Perform hex-to-ascii translation.  The return data is prefixed
351  *              with "0x"
352  *
353  ******************************************************************************/
354 
355 void
356 UtConvertByteToAsmHex (
357     UINT8                   RawByte,
358     UINT8                   *Buffer)
359 {
360 
361     Buffer[0] = '0';
362     Buffer[1] = (UINT8) hex[(RawByte >> 4) & 0xF];
363     Buffer[2] = (UINT8) hex[RawByte & 0xF];
364     Buffer[3] = 'h';
365 }
366 
367 
368 /*******************************************************************************
369  *
370  * FUNCTION:    DbgPrint
371  *
372  * PARAMETERS:  Type            - Type of output
373  *              Fmt             - Printf format string
374  *              ...             - variable printf list
375  *
376  * RETURN:      None
377  *
378  * DESCRIPTION: Conditional print statement.  Prints to stderr only if the
379  *              debug flag is set.
380  *
381  ******************************************************************************/
382 
383 void
384 DbgPrint (
385     UINT32                  Type,
386     char                    *Fmt,
387     ...)
388 {
389     va_list                 Args;
390 
391 
392     va_start (Args, Fmt);
393 
394     if (!Gbl_DebugFlag)
395     {
396         return;
397     }
398 
399     if ((Type == ASL_PARSE_OUTPUT) &&
400         (!(AslCompilerdebug)))
401     {
402         return;
403     }
404 
405     (void) vfprintf (stderr, Fmt, Args);
406     va_end (Args);
407     return;
408 }
409 
410 
411 /*******************************************************************************
412  *
413  * FUNCTION:    UtPrintFormattedName
414  *
415  * PARAMETERS:  ParseOpcode         - Parser keyword ID
416  *              Level               - Indentation level
417  *
418  * RETURN:      None
419  *
420  * DESCRIPTION: Print the ascii name of the parse opcode.
421  *
422  ******************************************************************************/
423 
424 #define TEXT_OFFSET 10
425 
426 void
427 UtPrintFormattedName (
428     UINT16                  ParseOpcode,
429     UINT32                  Level)
430 {
431 
432     if (Level)
433     {
434         DbgPrint (ASL_TREE_OUTPUT,
435             "%*s", (3 * Level), " ");
436     }
437     DbgPrint (ASL_TREE_OUTPUT,
438         " %-20.20s", UtGetOpName (ParseOpcode));
439 
440     if (Level < TEXT_OFFSET)
441     {
442         DbgPrint (ASL_TREE_OUTPUT,
443             "%*s", (TEXT_OFFSET - Level) * 3, " ");
444     }
445 }
446 
447 
448 /*******************************************************************************
449  *
450  * FUNCTION:    UtSetParseOpName
451  *
452  * PARAMETERS:  Op
453  *
454  * RETURN:      None
455  *
456  * DESCRIPTION: Insert the ascii name of the parse opcode
457  *
458  ******************************************************************************/
459 
460 void
461 UtSetParseOpName (
462     ACPI_PARSE_OBJECT       *Op)
463 {
464 
465     strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
466         ACPI_MAX_PARSEOP_NAME);
467 }
468 
469 
470 /*******************************************************************************
471  *
472  * FUNCTION:    UtGetOpName
473  *
474  * PARAMETERS:  ParseOpcode         - Parser keyword ID
475  *
476  * RETURN:      Pointer to the opcode name
477  *
478  * DESCRIPTION: Get the ascii name of the parse opcode
479  *
480  ******************************************************************************/
481 
482 char *
483 UtGetOpName (
484     UINT32                  ParseOpcode)
485 {
486 
487     /*
488      * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
489      * Ignore first 8 characters of the name
490      */
491     return ((char *) yytname
492         [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8);
493 }
494 
495 
496 /*******************************************************************************
497  *
498  * FUNCTION:    UtDisplaySummary
499  *
500  * PARAMETERS:  FileID          - ID of outpout file
501  *
502  * RETURN:      None
503  *
504  * DESCRIPTION: Display compilation statistics
505  *
506  ******************************************************************************/
507 
508 void
509 UtDisplaySummary (
510     UINT32                  FileId)
511 {
512 
513     if (FileId != ASL_FILE_STDOUT)
514     {
515         /* Compiler name and version number */
516 
517         FlPrintFile (FileId, "%s version %X [%s]\n",
518             CompilerId, (UINT32) ACPI_CA_VERSION, __DATE__);
519     }
520 
521     /* Input/Output summary */
522 
523     FlPrintFile (FileId,
524         "ASL Input:  %s - %d lines, %d bytes, %d keywords\n",
525         Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
526         Gbl_InputByteCount, TotalKeywords);
527 
528     /* AML summary */
529 
530     if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
531     {
532         FlPrintFile (FileId,
533             "AML Output: %s - %d bytes %d named objects %d executable opcodes\n\n",
534             Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
535             TotalNamedObjects, TotalExecutableOpcodes);
536     }
537 
538     /* Error summary */
539 
540     FlPrintFile (FileId,
541         "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n",
542         Gbl_ExceptionCount[ASL_ERROR],
543         Gbl_ExceptionCount[ASL_WARNING] +
544             Gbl_ExceptionCount[ASL_WARNING2] +
545             Gbl_ExceptionCount[ASL_WARNING3],
546         Gbl_ExceptionCount[ASL_REMARK],
547         Gbl_ExceptionCount[ASL_OPTIMIZATION]);
548 }
549 
550 
551 /*******************************************************************************
552  *
553  * FUNCTION:    UtDisplaySummary
554  *
555  * PARAMETERS:  Op              - Integer parse node
556  *              LowValue        - Smallest allowed value
557  *              HighValue       - Largest allowed value
558  *
559  * RETURN:      Op if OK, otherwise NULL
560  *
561  * DESCRIPTION: Check integer for an allowable range
562  *
563  ******************************************************************************/
564 
565 ACPI_PARSE_OBJECT *
566 UtCheckIntegerRange (
567     ACPI_PARSE_OBJECT       *Op,
568     UINT32                  LowValue,
569     UINT32                  HighValue)
570 {
571     char                    *ParseError = NULL;
572     char                    Buffer[64];
573 
574 
575     if (!Op)
576     {
577         return NULL;
578     }
579 
580     if (Op->Asl.Value.Integer < LowValue)
581     {
582         ParseError = "Value below valid range";
583         Op->Asl.Value.Integer = LowValue;
584     }
585 
586     if (Op->Asl.Value.Integer > HighValue)
587     {
588         ParseError = "Value above valid range";
589         Op->Asl.Value.Integer = HighValue;
590     }
591 
592     if (ParseError)
593     {
594         sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
595         AslCompilererror (Buffer);
596 
597         return NULL;
598     }
599 
600     return Op;
601 }
602 
603 
604 /*******************************************************************************
605  *
606  * FUNCTION:    UtGetStringBuffer
607  *
608  * PARAMETERS:  Length          - Size of buffer requested
609  *
610  * RETURN:      Pointer to the buffer.  Aborts on allocation failure
611  *
612  * DESCRIPTION: Allocate a string buffer.  Bypass the local
613  *              dynamic memory manager for performance reasons (This has a
614  *              major impact on the speed of the compiler.)
615  *
616  ******************************************************************************/
617 
618 char *
619 UtGetStringBuffer (
620     UINT32                  Length)
621 {
622     char                    *Buffer;
623 
624 
625     if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
626     {
627         Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
628         Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
629                                 Length;
630     }
631 
632     Buffer = Gbl_StringCacheNext;
633     Gbl_StringCacheNext += Length;
634 
635     return (Buffer);
636 }
637 
638 
639 /*******************************************************************************
640  *
641  * FUNCTION:    UtInternalizeName
642  *
643  * PARAMETERS:  ExternalName            - Name to convert
644  *              ConvertedName           - Where the converted name is returned
645  *
646  * RETURN:      Status
647  *
648  * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
649  *
650  ******************************************************************************/
651 
652 ACPI_STATUS
653 UtInternalizeName (
654     char                    *ExternalName,
655     char                    **ConvertedName)
656 {
657     ACPI_NAMESTRING_INFO    Info;
658     ACPI_STATUS             Status;
659 
660 
661     if (!ExternalName)
662     {
663         return (AE_OK);
664     }
665 
666     /* Get the length of the new internal name */
667 
668     Info.ExternalName = ExternalName;
669     AcpiNsGetInternalNameLength (&Info);
670 
671     /* We need a segment to store the internal  name */
672 
673     Info.InternalName = UtGetStringBuffer (Info.Length);
674     if (!Info.InternalName)
675     {
676         return (AE_NO_MEMORY);
677     }
678 
679     /* Build the name */
680 
681     Status = AcpiNsBuildInternalName (&Info);
682     if (ACPI_FAILURE (Status))
683     {
684         return (Status);
685     }
686 
687     *ConvertedName = Info.InternalName;
688     return (AE_OK);
689 }
690 
691 
692 /*******************************************************************************
693  *
694  * FUNCTION:    UtPadNameWithUnderscores
695  *
696  * PARAMETERS:  NameSeg         - Input nameseg
697  *              PaddedNameSeg   - Output padded nameseg
698  *
699  * RETURN:      Padded nameseg.
700  *
701  * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
702  *              ACPI_NAME.
703  *
704  ******************************************************************************/
705 
706 static void
707 UtPadNameWithUnderscores (
708     char                    *NameSeg,
709     char                    *PaddedNameSeg)
710 {
711     UINT32                  i;
712 
713 
714     for (i = 0; (i < ACPI_NAME_SIZE); i++)
715     {
716         if (*NameSeg)
717         {
718             *PaddedNameSeg = *NameSeg;
719             NameSeg++;
720         }
721         else
722         {
723             *PaddedNameSeg = '_';
724         }
725         PaddedNameSeg++;
726     }
727 }
728 
729 
730 /*******************************************************************************
731  *
732  * FUNCTION:    UtAttachNameseg
733  *
734  * PARAMETERS:  Op              - Parent parse node
735  *              Name            - Full ExternalName
736  *
737  * RETURN:      None; Sets the NameSeg field in parent node
738  *
739  * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
740  *              in the NameSeg field of the Op.
741  *
742  ******************************************************************************/
743 
744 static void
745 UtAttachNameseg (
746     ACPI_PARSE_OBJECT       *Op,
747     char                    *Name)
748 {
749     char                    *NameSeg;
750     char                    PaddedNameSeg[4];
751 
752 
753     if (!Name)
754     {
755         return;
756     }
757 
758     /* Look for the last dot in the namepath */
759 
760     NameSeg = strrchr (Name, '.');
761     if (NameSeg)
762     {
763         /* Found last dot, we have also found the final nameseg */
764 
765         NameSeg++;
766         UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
767     }
768     else
769     {
770         /* No dots in the namepath, there is only a single nameseg. */
771         /* Handle prefixes */
772 
773         while ((*Name == '\\') || (*Name == '^'))
774         {
775             Name++;
776         }
777 
778         /* Remaing string should be one single nameseg */
779 
780         UtPadNameWithUnderscores (Name, PaddedNameSeg);
781     }
782 
783     strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
784 }
785 
786 
787 /*******************************************************************************
788  *
789  * FUNCTION:    UtAttachNamepathToOwner
790  *
791  * PARAMETERS:  Op            - Parent parse node
792  *              NameOp        - Node that contains the name
793  *
794  * RETURN:      Sets the ExternalName and Namepath in the parent node
795  *
796  * DESCRIPTION: Store the name in two forms in the parent node:  The original
797  *              (external) name, and the internalized name that is used within
798  *              the ACPI namespace manager.
799  *
800  ******************************************************************************/
801 
802 void
803 UtAttachNamepathToOwner (
804     ACPI_PARSE_OBJECT       *Op,
805     ACPI_PARSE_OBJECT       *NameOp)
806 {
807     ACPI_STATUS             Status;
808 
809 
810     /* Full external path */
811 
812     Op->Asl.ExternalName = NameOp->Asl.Value.String;
813 
814     /* Save the NameOp for possible error reporting later */
815 
816     Op->Asl.ParentMethod = (void *) NameOp;
817 
818     /* Last nameseg of the path */
819 
820     UtAttachNameseg (Op, Op->Asl.ExternalName);
821 
822     /* Create internalized path */
823 
824     Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
825     if (ACPI_FAILURE (Status))
826     {
827         /* TBD: abort on no memory */
828     }
829 }
830 
831 
832 /*******************************************************************************
833  *
834  * FUNCTION:    UtDoConstant
835  *
836  * PARAMETERS:  String      - Hex, Octal, or Decimal string
837  *
838  * RETURN:      Converted Integer
839  *
840  * DESCRIPTION: Convert a string to an integer.  With error checking.
841  *
842  ******************************************************************************/
843 
844 ACPI_INTEGER
845 UtDoConstant (
846     char                    *String)
847 {
848     ACPI_STATUS             Status;
849     ACPI_INTEGER            Converted;
850     char                    ErrBuf[64];
851 
852 
853     Status = UtStrtoul64 (String, 0, &Converted);
854     if (ACPI_FAILURE (Status))
855     {
856         sprintf (ErrBuf, "%s %s\n", "Conversion error:",
857             AcpiFormatException (Status));
858         AslCompilererror (ErrBuf);
859     }
860 
861     return (Converted);
862 }
863 
864 
865 /* TBD: use version in ACPI CA main code base? */
866 
867 /*******************************************************************************
868  *
869  * FUNCTION:    UtStrtoul64
870  *
871  * PARAMETERS:  String          - Null terminated string
872  *              Terminater      - Where a pointer to the terminating byte is
873  *                                returned
874  *              Base            - Radix of the string
875  *
876  * RETURN:      Converted value
877  *
878  * DESCRIPTION: Convert a string into an unsigned value.
879  *
880  ******************************************************************************/
881 
882 static ACPI_STATUS
883 UtStrtoul64 (
884     char                    *String,
885     UINT32                  Base,
886     ACPI_INTEGER            *RetInteger)
887 {
888     UINT32                  Index;
889     UINT32                  Sign;
890     ACPI_INTEGER            ReturnValue = 0;
891     ACPI_STATUS             Status = AE_OK;
892 
893 
894     *RetInteger = 0;
895 
896     switch (Base)
897     {
898     case 0:
899     case 8:
900     case 10:
901     case 16:
902         break;
903 
904     default:
905         /*
906          * The specified Base parameter is not in the domain of
907          * this function:
908          */
909         return (AE_BAD_PARAMETER);
910     }
911 
912     /* Skip over any white space in the buffer: */
913 
914     while (isspace (*String) || *String == '\t')
915     {
916         ++String;
917     }
918 
919     /*
920      * The buffer may contain an optional plus or minus sign.
921      * If it does, then skip over it but remember what is was:
922      */
923     if (*String == '-')
924     {
925         Sign = NEGATIVE;
926         ++String;
927     }
928     else if (*String == '+')
929     {
930         ++String;
931         Sign = POSITIVE;
932     }
933     else
934     {
935         Sign = POSITIVE;
936     }
937 
938     /*
939      * If the input parameter Base is zero, then we need to
940      * determine if it is octal, decimal, or hexadecimal:
941      */
942     if (Base == 0)
943     {
944         if (*String == '0')
945         {
946             if (tolower (*(++String)) == 'x')
947             {
948                 Base = 16;
949                 ++String;
950             }
951             else
952             {
953                 Base = 8;
954             }
955         }
956         else
957         {
958             Base = 10;
959         }
960     }
961 
962     /*
963      * For octal and hexadecimal bases, skip over the leading
964      * 0 or 0x, if they are present.
965      */
966     if (Base == 8 && *String == '0')
967     {
968         String++;
969     }
970 
971     if (Base == 16 &&
972         *String == '0' &&
973         tolower (*(++String)) == 'x')
974     {
975         String++;
976     }
977 
978     /* Main loop: convert the string to an unsigned long */
979 
980     while (*String)
981     {
982         if (isdigit (*String))
983         {
984             Index = ((UINT8) *String) - '0';
985         }
986         else
987         {
988             Index = (UINT8) toupper (*String);
989             if (isupper ((char) Index))
990             {
991                 Index = Index - 'A' + 10;
992             }
993             else
994             {
995                 goto ErrorExit;
996             }
997         }
998 
999         if (Index >= Base)
1000         {
1001             goto ErrorExit;
1002         }
1003 
1004         /* Check to see if value is out of range: */
1005 
1006         if (ReturnValue > ((ACPI_INTEGER_MAX - (ACPI_INTEGER) Index) /
1007                             (ACPI_INTEGER) Base))
1008         {
1009             goto ErrorExit;
1010         }
1011         else
1012         {
1013             ReturnValue *= Base;
1014             ReturnValue += Index;
1015         }
1016 
1017         ++String;
1018     }
1019 
1020 
1021     /* If a minus sign was present, then "the conversion is negated": */
1022 
1023     if (Sign == NEGATIVE)
1024     {
1025         ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1026     }
1027 
1028     *RetInteger = ReturnValue;
1029     return (Status);
1030 
1031 
1032 ErrorExit:
1033     switch (Base)
1034     {
1035     case 8:
1036         Status = AE_BAD_OCTAL_CONSTANT;
1037         break;
1038 
1039     case 10:
1040         Status = AE_BAD_DECIMAL_CONSTANT;
1041         break;
1042 
1043     case 16:
1044         Status = AE_BAD_HEX_CONSTANT;
1045         break;
1046 
1047     default:
1048         /* Base validated above */
1049         break;
1050     }
1051 
1052     return (Status);
1053 }
1054 
1055 
1056