Home | History | Annotate | Line # | Download | only in compiler
aslutils.c revision 1.1.1.12
      1 /******************************************************************************
      2  *
      3  * Module Name: aslutils -- compiler utilities
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2017, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #include "aslcompiler.h"
     45 #include "aslcompiler.y.h"
     46 #include "acdisasm.h"
     47 #include "acnamesp.h"
     48 #include "amlcode.h"
     49 #include "acapps.h"
     50 #include <sys/stat.h>
     51 
     52 
     53 #define _COMPONENT          ACPI_COMPILER
     54         ACPI_MODULE_NAME    ("aslutils")
     55 
     56 
     57 /* Local prototypes */
     58 
     59 static void
     60 UtPadNameWithUnderscores (
     61     char                    *NameSeg,
     62     char                    *PaddedNameSeg);
     63 
     64 static void
     65 UtAttachNameseg (
     66     ACPI_PARSE_OBJECT       *Op,
     67     char                    *Name);
     68 
     69 static void
     70 UtReallocLineBuffers (
     71     char                    **Buffer,
     72     UINT32                  OldSize,
     73     UINT32                  NewSize);
     74 
     75 
     76 /*******************************************************************************
     77  *
     78  * FUNCTION:    UtIsBigEndianMachine
     79  *
     80  * PARAMETERS:  None
     81  *
     82  * RETURN:      TRUE if machine is big endian
     83  *              FALSE if machine is little endian
     84  *
     85  * DESCRIPTION: Detect whether machine is little endian or big endian.
     86  *
     87  ******************************************************************************/
     88 
     89 UINT8
     90 UtIsBigEndianMachine (
     91     void)
     92 {
     93     union {
     94         UINT32              Integer;
     95         UINT8               Bytes[4];
     96     } Overlay =                 {0xFF000000};
     97 
     98 
     99     return (Overlay.Bytes[0]); /* Returns 0xFF (TRUE) for big endian */
    100 }
    101 
    102 
    103 /******************************************************************************
    104  *
    105  * FUNCTION:    UtQueryForOverwrite
    106  *
    107  * PARAMETERS:  Pathname            - Output filename
    108  *
    109  * RETURN:      TRUE if file does not exist or overwrite is authorized
    110  *
    111  * DESCRIPTION: Query for file overwrite if it already exists.
    112  *
    113  ******************************************************************************/
    114 
    115 BOOLEAN
    116 UtQueryForOverwrite (
    117     char                    *Pathname)
    118 {
    119     struct stat             StatInfo;
    120 
    121 
    122     if (!stat (Pathname, &StatInfo))
    123     {
    124         fprintf (stderr, "Target file \"%s\" already exists, overwrite? [y|n] ",
    125             Pathname);
    126 
    127         if (getchar () != 'y')
    128         {
    129             return (FALSE);
    130         }
    131     }
    132 
    133     return (TRUE);
    134 }
    135 
    136 
    137 /*******************************************************************************
    138  *
    139  * FUNCTION:    UtDisplaySupportedTables
    140  *
    141  * PARAMETERS:  None
    142  *
    143  * RETURN:      None
    144  *
    145  * DESCRIPTION: Print all supported ACPI table names.
    146  *
    147  ******************************************************************************/
    148 
    149 void
    150 UtDisplaySupportedTables (
    151     void)
    152 {
    153     const AH_TABLE          *TableData;
    154     UINT32                  i;
    155 
    156 
    157     printf ("\nACPI tables supported by iASL version %8.8X:\n"
    158         "  (Compiler, Disassembler, Template Generator)\n\n",
    159         ACPI_CA_VERSION);
    160 
    161     /* All ACPI tables with the common table header */
    162 
    163     printf ("\n  Supported ACPI tables:\n");
    164     for (TableData = Gbl_AcpiSupportedTables, i = 1;
    165          TableData->Signature; TableData++, i++)
    166     {
    167         printf ("%8u) %s    %s\n", i,
    168             TableData->Signature, TableData->Description);
    169     }
    170 }
    171 
    172 
    173 /*******************************************************************************
    174  *
    175  * FUNCTION:    UtDisplayConstantOpcodes
    176  *
    177  * PARAMETERS:  None
    178  *
    179  * RETURN:      None
    180  *
    181  * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
    182  *
    183  ******************************************************************************/
    184 
    185 void
    186 UtDisplayConstantOpcodes (
    187     void)
    188 {
    189     UINT32                  i;
    190 
    191 
    192     printf ("Constant expression opcode information\n\n");
    193 
    194     for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
    195     {
    196         if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
    197         {
    198             printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
    199         }
    200     }
    201 }
    202 
    203 
    204 /*******************************************************************************
    205  *
    206  * FUNCTION:    UtLocalCalloc
    207  *
    208  * PARAMETERS:  Size                - Bytes to be allocated
    209  *
    210  * RETURN:      Pointer to the allocated memory. Guaranteed to be valid.
    211  *
    212  * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
    213  *              allocation failure, on the assumption that nothing more can be
    214  *              accomplished.
    215  *
    216  ******************************************************************************/
    217 
    218 void *
    219 UtLocalCalloc (
    220     UINT32                  Size)
    221 {
    222     void                    *Allocated;
    223 
    224 
    225     Allocated = ACPI_ALLOCATE_ZEROED (Size);
    226     if (!Allocated)
    227     {
    228         AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
    229             Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
    230             Gbl_InputByteCount, Gbl_CurrentColumn,
    231             Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
    232 
    233         CmCleanupAndExit ();
    234         exit (1);
    235     }
    236 
    237     TotalAllocations++;
    238     TotalAllocated += Size;
    239     return (Allocated);
    240 }
    241 
    242 
    243 /*******************************************************************************
    244  *
    245  * FUNCTION:    UtBeginEvent
    246  *
    247  * PARAMETERS:  Name                - Ascii name of this event
    248  *
    249  * RETURN:      Event number (integer index)
    250  *
    251  * DESCRIPTION: Saves the current time with this event
    252  *
    253  ******************************************************************************/
    254 
    255 UINT8
    256 UtBeginEvent (
    257     char                    *Name)
    258 {
    259 
    260     if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
    261     {
    262         AcpiOsPrintf ("Ran out of compiler event structs!\n");
    263         return (AslGbl_NextEvent);
    264     }
    265 
    266     /* Init event with current (start) time */
    267 
    268     AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
    269     AslGbl_Events[AslGbl_NextEvent].EventName = Name;
    270     AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
    271     return (AslGbl_NextEvent++);
    272 }
    273 
    274 
    275 /*******************************************************************************
    276  *
    277  * FUNCTION:    UtEndEvent
    278  *
    279  * PARAMETERS:  Event               - Event number (integer index)
    280  *
    281  * RETURN:      None
    282  *
    283  * DESCRIPTION: Saves the current time (end time) with this event
    284  *
    285  ******************************************************************************/
    286 
    287 void
    288 UtEndEvent (
    289     UINT8                   Event)
    290 {
    291 
    292     if (Event >= ASL_NUM_EVENTS)
    293     {
    294         return;
    295     }
    296 
    297     /* Insert end time for event */
    298 
    299     AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
    300 }
    301 
    302 
    303 /*******************************************************************************
    304  *
    305  * FUNCTION:    DbgPrint
    306  *
    307  * PARAMETERS:  Type                - Type of output
    308  *              Fmt                 - Printf format string
    309  *              ...                 - variable printf list
    310  *
    311  * RETURN:      None
    312  *
    313  * DESCRIPTION: Conditional print statement. Prints to stderr only if the
    314  *              debug flag is set.
    315  *
    316  ******************************************************************************/
    317 
    318 void
    319 DbgPrint (
    320     UINT32                  Type,
    321     char                    *Fmt,
    322     ...)
    323 {
    324     va_list                 Args;
    325 
    326 
    327     if (!Gbl_DebugFlag)
    328     {
    329         return;
    330     }
    331 
    332     if ((Type == ASL_PARSE_OUTPUT) &&
    333         (!(AslCompilerdebug)))
    334     {
    335         return;
    336     }
    337 
    338     va_start (Args, Fmt);
    339     (void) vfprintf (stderr, Fmt, Args);
    340     va_end (Args);
    341     return;
    342 }
    343 
    344 
    345 /*******************************************************************************
    346  *
    347  * FUNCTION:    UtSetParseOpName
    348  *
    349  * PARAMETERS:  Op                  - Parse op to be named.
    350  *
    351  * RETURN:      None
    352  *
    353  * DESCRIPTION: Insert the ascii name of the parse opcode
    354  *
    355  ******************************************************************************/
    356 
    357 void
    358 UtSetParseOpName (
    359     ACPI_PARSE_OBJECT       *Op)
    360 {
    361 
    362     strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
    363         ACPI_MAX_PARSEOP_NAME);
    364 }
    365 
    366 
    367 /*******************************************************************************
    368  *
    369  * FUNCTION:    UtDisplaySummary
    370  *
    371  * PARAMETERS:  FileID              - ID of outpout file
    372  *
    373  * RETURN:      None
    374  *
    375  * DESCRIPTION: Display compilation statistics
    376  *
    377  ******************************************************************************/
    378 
    379 void
    380 UtDisplaySummary (
    381     UINT32                  FileId)
    382 {
    383     UINT32                  i;
    384 
    385 
    386     if (FileId != ASL_FILE_STDOUT)
    387     {
    388         /* Compiler name and version number */
    389 
    390         FlPrintFile (FileId, "%s version %X [%s]\n\n",
    391             ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, __DATE__);
    392     }
    393 
    394     /* Summary of main input and output files */
    395 
    396     if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
    397     {
    398         FlPrintFile (FileId,
    399             "%-14s %s - %u lines, %u bytes, %u fields\n",
    400             "Table Input:",
    401             Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
    402             Gbl_InputByteCount, Gbl_InputFieldCount);
    403 
    404         if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
    405         {
    406             FlPrintFile (FileId,
    407                 "%-14s %s - %u bytes\n",
    408                 "Binary Output:",
    409                 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
    410         }
    411     }
    412     else
    413     {
    414         FlPrintFile (FileId,
    415             "%-14s %s - %u lines, %u bytes, %u keywords\n",
    416             "ASL Input:",
    417             Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
    418             Gbl_OriginalInputFileSize, TotalKeywords);
    419 
    420         /* AML summary */
    421 
    422         if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
    423         {
    424             if (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
    425             {
    426                 FlPrintFile (FileId,
    427                     "%-14s %s - %u bytes, %u named objects, "
    428                     "%u executable opcodes\n",
    429                     "AML Output:",
    430                     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename,
    431                     FlGetFileSize (ASL_FILE_AML_OUTPUT),
    432                     TotalNamedObjects, TotalExecutableOpcodes);
    433             }
    434         }
    435     }
    436 
    437     /* Display summary of any optional files */
    438 
    439     for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
    440     {
    441         if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
    442         {
    443             continue;
    444         }
    445 
    446         /* .SRC is a temp file unless specifically requested */
    447 
    448         if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
    449         {
    450             continue;
    451         }
    452 
    453         /* .PRE is the preprocessor intermediate file */
    454 
    455         if ((i == ASL_FILE_PREPROCESSOR)  && (!Gbl_KeepPreprocessorTempFile))
    456         {
    457             continue;
    458         }
    459 
    460         FlPrintFile (FileId, "%14s %s - %u bytes\n",
    461             Gbl_Files[i].ShortDescription,
    462             Gbl_Files[i].Filename, FlGetFileSize (i));
    463     }
    464 
    465     /* Error summary */
    466 
    467     FlPrintFile (FileId,
    468         "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
    469         Gbl_ExceptionCount[ASL_ERROR],
    470         Gbl_ExceptionCount[ASL_WARNING] +
    471             Gbl_ExceptionCount[ASL_WARNING2] +
    472             Gbl_ExceptionCount[ASL_WARNING3],
    473         Gbl_ExceptionCount[ASL_REMARK]);
    474 
    475     if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
    476     {
    477         FlPrintFile (FileId, ", %u Optimizations",
    478             Gbl_ExceptionCount[ASL_OPTIMIZATION]);
    479 
    480         if (TotalFolds)
    481         {
    482             FlPrintFile (FileId, ", %u Constants Folded", TotalFolds);
    483         }
    484     }
    485 
    486     FlPrintFile (FileId, "\n");
    487 }
    488 
    489 
    490 /*******************************************************************************
    491  *
    492  * FUNCTION:    UtCheckIntegerRange
    493  *
    494  * PARAMETERS:  Op                  - Integer parse node
    495  *              LowValue            - Smallest allowed value
    496  *              HighValue           - Largest allowed value
    497  *
    498  * RETURN:      Op if OK, otherwise NULL
    499  *
    500  * DESCRIPTION: Check integer for an allowable range
    501  *
    502  ******************************************************************************/
    503 
    504 ACPI_PARSE_OBJECT *
    505 UtCheckIntegerRange (
    506     ACPI_PARSE_OBJECT       *Op,
    507     UINT32                  LowValue,
    508     UINT32                  HighValue)
    509 {
    510 
    511     if (!Op)
    512     {
    513         return (NULL);
    514     }
    515 
    516     if ((Op->Asl.Value.Integer < LowValue) ||
    517         (Op->Asl.Value.Integer > HighValue))
    518     {
    519         sprintf (MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
    520             (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
    521 
    522         AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer);
    523         return (NULL);
    524     }
    525 
    526     return (Op);
    527 }
    528 
    529 
    530 /*******************************************************************************
    531  *
    532  * FUNCTION:    UtStringCacheCalloc
    533  *
    534  * PARAMETERS:  Length              - Size of buffer requested
    535  *
    536  * RETURN:      Pointer to the buffer. Aborts compiler on allocation failure
    537  *
    538  * DESCRIPTION: Allocate a string buffer. Bypass the local
    539  *              dynamic memory manager for performance reasons (This has a
    540  *              major impact on the speed of the compiler.)
    541  *
    542  ******************************************************************************/
    543 
    544 char *
    545 UtStringCacheCalloc (
    546     UINT32                  Length)
    547 {
    548     char                    *Buffer;
    549     ASL_CACHE_INFO          *Cache;
    550     UINT32                  CacheSize = ASL_STRING_CACHE_SIZE;
    551 
    552 
    553     if (Length > CacheSize)
    554     {
    555         CacheSize = Length;
    556 
    557         if (Gbl_StringCacheList)
    558         {
    559             Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
    560 
    561             /* Link new cache buffer just following head of list */
    562 
    563             Cache->Next = Gbl_StringCacheList->Next;
    564             Gbl_StringCacheList->Next = Cache;
    565 
    566             /* Leave cache management pointers alone as they pertain to head */
    567 
    568             Gbl_StringCount++;
    569             Gbl_StringSize += Length;
    570 
    571             return (Cache->Buffer);
    572         }
    573     }
    574 
    575     if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
    576     {
    577         /* Allocate a new buffer */
    578 
    579         Cache = UtLocalCalloc (sizeof (Cache->Next) + CacheSize);
    580 
    581         /* Link new cache buffer to head of list */
    582 
    583         Cache->Next = Gbl_StringCacheList;
    584         Gbl_StringCacheList = Cache;
    585 
    586         /* Setup cache management pointers */
    587 
    588         Gbl_StringCacheNext = Cache->Buffer;
    589         Gbl_StringCacheLast = Gbl_StringCacheNext + CacheSize;
    590     }
    591 
    592     Gbl_StringCount++;
    593     Gbl_StringSize += Length;
    594 
    595     Buffer = Gbl_StringCacheNext;
    596     Gbl_StringCacheNext += Length;
    597     return (Buffer);
    598 }
    599 
    600 
    601 /******************************************************************************
    602  *
    603  * FUNCTION:    UtExpandLineBuffers
    604  *
    605  * PARAMETERS:  None. Updates global line buffer pointers.
    606  *
    607  * RETURN:      None. Reallocates the global line buffers
    608  *
    609  * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
    610  *              all global line buffers and updates Gbl_LineBufferSize. NOTE:
    611  *              Also used for the initial allocation of the buffers, when
    612  *              all of the buffer pointers are NULL. Initial allocations are
    613  *              of size ASL_DEFAULT_LINE_BUFFER_SIZE
    614  *
    615  *****************************************************************************/
    616 
    617 void
    618 UtExpandLineBuffers (
    619     void)
    620 {
    621     UINT32                  NewSize;
    622 
    623 
    624     /* Attempt to double the size of all line buffers */
    625 
    626     NewSize = Gbl_LineBufferSize * 2;
    627     if (Gbl_CurrentLineBuffer)
    628     {
    629         DbgPrint (ASL_DEBUG_OUTPUT,
    630             "Increasing line buffer size from %u to %u\n",
    631             Gbl_LineBufferSize, NewSize);
    632     }
    633 
    634     UtReallocLineBuffers (&Gbl_CurrentLineBuffer, Gbl_LineBufferSize, NewSize);
    635     UtReallocLineBuffers (&Gbl_MainTokenBuffer, Gbl_LineBufferSize, NewSize);
    636     UtReallocLineBuffers (&Gbl_MacroTokenBuffer, Gbl_LineBufferSize, NewSize);
    637     UtReallocLineBuffers (&Gbl_ExpressionTokenBuffer, Gbl_LineBufferSize, NewSize);
    638 
    639     Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
    640     Gbl_LineBufferSize = NewSize;
    641 }
    642 
    643 
    644 /******************************************************************************
    645  *
    646  * FUNCTION:    UtReallocLineBuffers
    647  *
    648  * PARAMETERS:  Buffer              - Buffer to realloc
    649  *              OldSize             - Old size of Buffer
    650  *              NewSize             - New size of Buffer
    651  *
    652  * RETURN:      none
    653  *
    654  * DESCRIPTION: Reallocate and initialize Buffer
    655  *
    656  *****************************************************************************/
    657 
    658 static void
    659 UtReallocLineBuffers (
    660     char                    **Buffer,
    661     UINT32                  OldSize,
    662     UINT32                  NewSize)
    663 {
    664 
    665     *Buffer = realloc (*Buffer, NewSize);
    666     if (*Buffer)
    667     {
    668         memset (*Buffer + OldSize, 0, NewSize - OldSize);
    669         return;
    670     }
    671 
    672     printf ("Could not increase line buffer size from %u to %u\n",
    673         OldSize, NewSize);
    674 
    675     AslError (ASL_ERROR, ASL_MSG_BUFFER_ALLOCATION, NULL, NULL);
    676     AslAbort ();
    677 }
    678 
    679 
    680 /******************************************************************************
    681  *
    682  * FUNCTION:    UtFreeLineBuffers
    683  *
    684  * PARAMETERS:  None
    685  *
    686  * RETURN:      None
    687  *
    688  * DESCRIPTION: Free all line buffers
    689  *
    690  *****************************************************************************/
    691 
    692 void
    693 UtFreeLineBuffers (
    694     void)
    695 {
    696 
    697     free (Gbl_CurrentLineBuffer);
    698     free (Gbl_MainTokenBuffer);
    699     free (Gbl_MacroTokenBuffer);
    700     free (Gbl_ExpressionTokenBuffer);
    701 }
    702 
    703 
    704 /*******************************************************************************
    705  *
    706  * FUNCTION:    UtInternalizeName
    707  *
    708  * PARAMETERS:  ExternalName        - Name to convert
    709  *              ConvertedName       - Where the converted name is returned
    710  *
    711  * RETURN:      Status
    712  *
    713  * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
    714  *
    715  ******************************************************************************/
    716 
    717 ACPI_STATUS
    718 UtInternalizeName (
    719     char                    *ExternalName,
    720     char                    **ConvertedName)
    721 {
    722     ACPI_NAMESTRING_INFO    Info;
    723     ACPI_STATUS             Status;
    724 
    725 
    726     if (!ExternalName)
    727     {
    728         return (AE_OK);
    729     }
    730 
    731     /* Get the length of the new internal name */
    732 
    733     Info.ExternalName = ExternalName;
    734     AcpiNsGetInternalNameLength (&Info);
    735 
    736     /* We need a segment to store the internal name */
    737 
    738     Info.InternalName = UtStringCacheCalloc (Info.Length);
    739 
    740     /* Build the name */
    741 
    742     Status = AcpiNsBuildInternalName (&Info);
    743     if (ACPI_FAILURE (Status))
    744     {
    745         return (Status);
    746     }
    747 
    748     *ConvertedName = Info.InternalName;
    749     return (AE_OK);
    750 }
    751 
    752 
    753 /*******************************************************************************
    754  *
    755  * FUNCTION:    UtPadNameWithUnderscores
    756  *
    757  * PARAMETERS:  NameSeg             - Input nameseg
    758  *              PaddedNameSeg       - Output padded nameseg
    759  *
    760  * RETURN:      Padded nameseg.
    761  *
    762  * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
    763  *              ACPI_NAME.
    764  *
    765  ******************************************************************************/
    766 
    767 static void
    768 UtPadNameWithUnderscores (
    769     char                    *NameSeg,
    770     char                    *PaddedNameSeg)
    771 {
    772     UINT32                  i;
    773 
    774 
    775     for (i = 0; (i < ACPI_NAME_SIZE); i++)
    776     {
    777         if (*NameSeg)
    778         {
    779             *PaddedNameSeg = *NameSeg;
    780             NameSeg++;
    781         }
    782         else
    783         {
    784             *PaddedNameSeg = '_';
    785         }
    786 
    787         PaddedNameSeg++;
    788     }
    789 }
    790 
    791 
    792 /*******************************************************************************
    793  *
    794  * FUNCTION:    UtAttachNameseg
    795  *
    796  * PARAMETERS:  Op                  - Parent parse node
    797  *              Name                - Full ExternalName
    798  *
    799  * RETURN:      None; Sets the NameSeg field in parent node
    800  *
    801  * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
    802  *              in the NameSeg field of the Op.
    803  *
    804  ******************************************************************************/
    805 
    806 static void
    807 UtAttachNameseg (
    808     ACPI_PARSE_OBJECT       *Op,
    809     char                    *Name)
    810 {
    811     char                    *NameSeg;
    812     char                    PaddedNameSeg[4];
    813 
    814 
    815     if (!Name)
    816     {
    817         return;
    818     }
    819 
    820     /* Look for the last dot in the namepath */
    821 
    822     NameSeg = strrchr (Name, '.');
    823     if (NameSeg)
    824     {
    825         /* Found last dot, we have also found the final nameseg */
    826 
    827         NameSeg++;
    828         UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
    829     }
    830     else
    831     {
    832         /* No dots in the namepath, there is only a single nameseg. */
    833         /* Handle prefixes */
    834 
    835         while (ACPI_IS_ROOT_PREFIX (*Name) ||
    836                ACPI_IS_PARENT_PREFIX (*Name))
    837         {
    838             Name++;
    839         }
    840 
    841         /* Remaining string should be one single nameseg */
    842 
    843         UtPadNameWithUnderscores (Name, PaddedNameSeg);
    844     }
    845 
    846     ACPI_MOVE_NAME (Op->Asl.NameSeg, PaddedNameSeg);
    847 }
    848 
    849 
    850 /*******************************************************************************
    851  *
    852  * FUNCTION:    UtAttachNamepathToOwner
    853  *
    854  * PARAMETERS:  Op                  - Parent parse node
    855  *              NameOp              - Node that contains the name
    856  *
    857  * RETURN:      Sets the ExternalName and Namepath in the parent node
    858  *
    859  * DESCRIPTION: Store the name in two forms in the parent node: The original
    860  *              (external) name, and the internalized name that is used within
    861  *              the ACPI namespace manager.
    862  *
    863  ******************************************************************************/
    864 
    865 void
    866 UtAttachNamepathToOwner (
    867     ACPI_PARSE_OBJECT       *Op,
    868     ACPI_PARSE_OBJECT       *NameOp)
    869 {
    870     ACPI_STATUS             Status;
    871 
    872 
    873     /* Full external path */
    874 
    875     Op->Asl.ExternalName = NameOp->Asl.Value.String;
    876 
    877     /* Save the NameOp for possible error reporting later */
    878 
    879     Op->Asl.ParentMethod = (void *) NameOp;
    880 
    881     /* Last nameseg of the path */
    882 
    883     UtAttachNameseg (Op, Op->Asl.ExternalName);
    884 
    885     /* Create internalized path */
    886 
    887     Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
    888     if (ACPI_FAILURE (Status))
    889     {
    890         /* TBD: abort on no memory */
    891     }
    892 }
    893 
    894 
    895 /*******************************************************************************
    896  *
    897  * FUNCTION:    UtDoConstant
    898  *
    899  * PARAMETERS:  String              - Hex/Decimal/Octal
    900  *
    901  * RETURN:      Converted Integer
    902  *
    903  * DESCRIPTION: Convert a string to an integer, with overflow/error checking.
    904  *
    905  ******************************************************************************/
    906 
    907 UINT64
    908 UtDoConstant (
    909     char                    *String)
    910 {
    911     ACPI_STATUS             Status;
    912     UINT64                  ConvertedInteger;
    913     char                    ErrBuf[64];
    914 
    915 
    916     Status = AcpiUtStrtoul64 (String, &ConvertedInteger);
    917     if (ACPI_FAILURE (Status))
    918     {
    919         sprintf (ErrBuf, "While creating 64-bit constant: %s\n",
    920             AcpiFormatException (Status));
    921 
    922         AslCommonError (ASL_ERROR, ASL_MSG_SYNTAX, Gbl_CurrentLineNumber,
    923             Gbl_LogicalLineNumber, Gbl_CurrentLineOffset,
    924             Gbl_CurrentColumn, Gbl_Files[ASL_FILE_INPUT].Filename, ErrBuf);
    925     }
    926 
    927     return (ConvertedInteger);
    928 }
    929