Subversion Repositories Kolibri OS

Rev

Rev 1498 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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