2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
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
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
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;
39 * The above copyright and patent license is granted only if the following
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.
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
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
74 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
82 * 4. Disclaimer and Export Compliance
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
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
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.
116 *****************************************************************************/
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>
124 #define _COMPONENT ACPI_COMPILER
125 ACPI_MODULE_NAME ("aslutils")
127 #ifdef _USE_BERKELEY_YACC
128 extern const char * const AslCompilername[];
129 static const char * const *yytname = &AslCompilername[254];
131 extern const char * const yytname[];
134 /* Local prototypes */
140 ACPI_INTEGER *RetInteger);
143 UtPadNameWithUnderscores (
145 char *PaddedNameSeg);
149 ACPI_PARSE_OBJECT *Op,
153 /*******************************************************************************
155 * FUNCTION: AcpiPsDisplayConstantOpcodes
161 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
163 ******************************************************************************/
166 UtDisplayConstantOpcodes (
172 printf ("Constant expression opcode information\n\n");
174 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
176 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
178 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
184 /*******************************************************************************
186 * FUNCTION: UtLocalCalloc
188 * PARAMETERS: Size - Bytes to be allocated
190 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
192 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
193 * allocation failure, on the assumption that nothing more can be
196 ******************************************************************************/
205 Allocated = ACPI_MEM_CALLOCATE (Size);
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);
216 TotalAllocated += Size;
221 /*******************************************************************************
223 * FUNCTION: UtBeginEvent
225 * PARAMETERS: Name - Ascii name of this event
227 * RETURN: Event - Event number (integer index)
229 * DESCRIPTION: Saves the current time with this event
231 ******************************************************************************/
238 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
240 AcpiOsPrintf ("Ran out of compiler event structs!\n");
241 return (AslGbl_NextEvent);
244 /* Init event with current (start) time */
246 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
247 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
248 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
250 return (AslGbl_NextEvent++);
254 /*******************************************************************************
256 * FUNCTION: UtEndEvent
258 * PARAMETERS: Event - Event number (integer index)
262 * DESCRIPTION: Saves the current time (end time) with this event
264 ******************************************************************************/
271 if (Event >= ASL_NUM_EVENTS)
276 /* Insert end time for event */
278 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
282 /*******************************************************************************
284 * FUNCTION: UtHexCharToValue
286 * PARAMETERS: HexChar - Hex character in Ascii
288 * RETURN: The binary value of the hex character
290 * DESCRIPTION: Perform ascii-to-hex translation
292 ******************************************************************************/
301 return ((UINT8) (HexChar - 0x30));
306 return ((UINT8) (HexChar - 0x37));
309 return ((UINT8) (HexChar - 0x57));
313 /*******************************************************************************
315 * FUNCTION: UtConvertByteToHex
317 * PARAMETERS: RawByte - Binary data
318 * Buffer - Pointer to where the hex bytes will be stored
320 * RETURN: Ascii hex byte is stored in Buffer.
322 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
325 ******************************************************************************/
336 Buffer[2] = (UINT8) hex[(RawByte >> 4) & 0xF];
337 Buffer[3] = (UINT8) hex[RawByte & 0xF];
341 /*******************************************************************************
343 * FUNCTION: UtConvertByteToAsmHex
345 * PARAMETERS: RawByte - Binary data
346 * Buffer - Pointer to where the hex bytes will be stored
348 * RETURN: Ascii hex byte is stored in Buffer.
350 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
353 ******************************************************************************/
356 UtConvertByteToAsmHex (
363 Buffer[1] = (UINT8) hex[(RawByte >> 4) & 0xF];
364 Buffer[2] = (UINT8) hex[RawByte & 0xF];
369 /*******************************************************************************
373 * PARAMETERS: Type - Type of output
374 * Fmt - Printf format string
375 * ... - variable printf list
379 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
382 ******************************************************************************/
393 va_start (Args, Fmt);
400 if ((Type == ASL_PARSE_OUTPUT) &&
401 (!(AslCompilerdebug)))
406 (void) vfprintf (stderr, Fmt, Args);
412 /*******************************************************************************
414 * FUNCTION: UtPrintFormattedName
416 * PARAMETERS: ParseOpcode - Parser keyword ID
417 * Level - Indentation level
421 * DESCRIPTION: Print the ascii name of the parse opcode.
423 ******************************************************************************/
425 #define TEXT_OFFSET 10
428 UtPrintFormattedName (
433 DbgPrint (ASL_TREE_OUTPUT,
434 "%*s %-16.16s", (3 * Level), " ",
435 UtGetOpName (ParseOpcode));
437 if (Level < TEXT_OFFSET)
439 DbgPrint (ASL_TREE_OUTPUT,
440 "%*s", (TEXT_OFFSET - Level) * 3, " ");
445 /*******************************************************************************
447 * FUNCTION: UtSetParseOpName
453 * DESCRIPTION: Insert the ascii name of the parse opcode
455 ******************************************************************************/
459 ACPI_PARSE_OBJECT *Op)
462 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode), 12);
466 /*******************************************************************************
468 * FUNCTION: UtGetOpName
470 * PARAMETERS: ParseOpcode - Parser keyword ID
472 * RETURN: Pointer to the opcode name
474 * DESCRIPTION: Get the ascii name of the parse opcode
476 ******************************************************************************/
484 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
485 * Ignore first 8 characters of the name
487 return ((char *) yytname [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8);
491 /*******************************************************************************
493 * FUNCTION: UtDisplaySummary
495 * PARAMETERS: FileID - ID of outpout file
499 * DESCRIPTION: Display compilation statistics
501 ******************************************************************************/
508 if (FileId != ASL_FILE_STDOUT)
510 /* Compiler name and version number */
512 FlPrintFile (FileId, "%s version %X [%s]\n",
513 CompilerId, (UINT32) ACPI_CA_VERSION, __DATE__);
516 /* Input/Output summary */
519 "ASL Input: %s - %d lines, %d bytes, %d keywords\n",
520 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
521 Gbl_InputByteCount, TotalKeywords);
525 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
528 "AML Output: %s - %d bytes %d named objects %d executable opcodes\n\n",
529 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
530 TotalNamedObjects, TotalExecutableOpcodes);
536 "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n",
537 Gbl_ExceptionCount[ASL_ERROR],
538 Gbl_ExceptionCount[ASL_WARNING],
539 Gbl_ExceptionCount[ASL_REMARK],
540 Gbl_ExceptionCount[ASL_OPTIMIZATION]);
544 /*******************************************************************************
546 * FUNCTION: UtDisplaySummary
548 * PARAMETERS: Op - Integer parse node
549 * LowValue - Smallest allowed value
550 * HighValue - Largest allowed value
552 * RETURN: Op if OK, otherwise NULL
554 * DESCRIPTION: Check integer for an allowable range
556 ******************************************************************************/
559 UtCheckIntegerRange (
560 ACPI_PARSE_OBJECT *Op,
564 char *ParseError = NULL;
573 if (Op->Asl.Value.Integer < LowValue)
575 ParseError = "Value below valid range";
576 Op->Asl.Value.Integer = LowValue;
579 if (Op->Asl.Value.Integer > HighValue)
581 ParseError = "Value above valid range";
582 Op->Asl.Value.Integer = HighValue;
587 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
588 AslCompilererror (Buffer);
597 /*******************************************************************************
599 * FUNCTION: UtGetStringBuffer
601 * PARAMETERS: Length - Size of buffer requested
603 * RETURN: Pointer to the buffer. Aborts on allocation failure
605 * DESCRIPTION: Allocate a string buffer. Bypass the local
606 * dynamic memory manager for performance reasons (This has a
607 * major impact on the speed of the compiler.)
609 ******************************************************************************/
618 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
620 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
621 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
625 Buffer = Gbl_StringCacheNext;
626 Gbl_StringCacheNext += Length;
632 /*******************************************************************************
634 * FUNCTION: UtInternalizeName
636 * PARAMETERS: ExternalName - Name to convert
637 * ConvertedName - Where the converted name is returned
641 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
643 ******************************************************************************/
648 char **ConvertedName)
650 ACPI_NAMESTRING_INFO Info;
659 /* Get the length of the new internal name */
661 Info.ExternalName = ExternalName;
662 AcpiNsGetInternalNameLength (&Info);
664 /* We need a segment to store the internal name */
666 Info.InternalName = UtGetStringBuffer (Info.Length);
667 if (!Info.InternalName)
669 return (AE_NO_MEMORY);
674 Status = AcpiNsBuildInternalName (&Info);
675 if (ACPI_FAILURE (Status))
680 *ConvertedName = Info.InternalName;
685 /*******************************************************************************
687 * FUNCTION: UtPadNameWithUnderscores
689 * PARAMETERS: NameSeg - Input nameseg
690 * PaddedNameSeg - Output padded nameseg
692 * RETURN: Padded nameseg.
694 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
697 ******************************************************************************/
700 UtPadNameWithUnderscores (
707 for (i = 0; (i < ACPI_NAME_SIZE); i++)
711 *PaddedNameSeg = *NameSeg;
716 *PaddedNameSeg = '_';
723 /*******************************************************************************
725 * FUNCTION: UtAttachNameseg
727 * PARAMETERS: Op - Parent parse node
728 * Name - Full ExternalName
730 * RETURN: None; Sets the NameSeg field in parent node
732 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
733 * in the NameSeg field of the Op.
735 ******************************************************************************/
739 ACPI_PARSE_OBJECT *Op,
743 char PaddedNameSeg[4];
751 /* Look for the last dot in the namepath */
753 NameSeg = strrchr (Name, '.');
756 /* Found last dot, we have also found the final nameseg */
759 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
763 /* No dots in the namepath, there is only a single nameseg. */
764 /* Handle prefixes */
766 while ((*Name == '\\') || (*Name == '^'))
771 /* Remaing string should be one single nameseg */
773 UtPadNameWithUnderscores (Name, PaddedNameSeg);
776 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
780 /*******************************************************************************
782 * FUNCTION: UtAttachNamepathToOwner
784 * PARAMETERS: Op - Parent parse node
785 * NameOp - Node that contains the name
787 * RETURN: Sets the ExternalName and Namepath in the parent node
789 * DESCRIPTION: Store the name in two forms in the parent node: The original
790 * (external) name, and the internalized name that is used within
791 * the ACPI namespace manager.
793 ******************************************************************************/
796 UtAttachNamepathToOwner (
797 ACPI_PARSE_OBJECT *Op,
798 ACPI_PARSE_OBJECT *NameOp)
803 /* Full external path */
805 Op->Asl.ExternalName = NameOp->Asl.Value.String;
807 /* Save the NameOp for possible error reporting later */
809 Op->Asl.ParentMethod = (void *) NameOp;
811 /* Last nameseg of the path */
813 UtAttachNameseg (Op, Op->Asl.ExternalName);
815 /* Create internalized path */
817 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
818 if (ACPI_FAILURE (Status))
820 /* TBD: abort on no memory */
825 /*******************************************************************************
827 * FUNCTION: UtDoConstant
829 * PARAMETERS: String - Hex, Octal, or Decimal string
831 * RETURN: Converted Integer
833 * DESCRIPTION: Convert a string to an integer. With error checking.
835 ******************************************************************************/
842 ACPI_INTEGER Converted;
846 Status = UtStrtoul64 (String, 0, &Converted);
847 if (ACPI_FAILURE (Status))
849 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
850 AcpiFormatException (Status));
851 AslCompilererror (ErrBuf);
858 /* TBD: use version in ACPI CA main code base? */
860 /*******************************************************************************
862 * FUNCTION: UtStrtoul64
864 * PARAMETERS: String - Null terminated string
865 * Terminater - Where a pointer to the terminating byte is
867 * Base - Radix of the string
869 * RETURN: Converted value
871 * DESCRIPTION: Convert a string into an unsigned value.
873 ******************************************************************************/
879 ACPI_INTEGER *RetInteger)
883 ACPI_INTEGER ReturnValue = 0;
884 ACPI_STATUS Status = AE_OK;
899 * The specified Base parameter is not in the domain of
902 return (AE_BAD_PARAMETER);
905 /* Skip over any white space in the buffer: */
907 while (isspace (*String) || *String == '\t')
913 * The buffer may contain an optional plus or minus sign.
914 * If it does, then skip over it but remember what is was:
921 else if (*String == '+')
932 * If the input parameter Base is zero, then we need to
933 * determine if it is octal, decimal, or hexadecimal:
939 if (tolower (*(++String)) == 'x')
956 * For octal and hexadecimal bases, skip over the leading
957 * 0 or 0x, if they are present.
959 if (Base == 8 && *String == '0')
966 tolower (*(++String)) == 'x')
971 /* Main loop: convert the string to an unsigned long */
975 if (isdigit (*String))
977 Index = ((UINT8) *String) - '0';
981 Index = (UINT8) toupper (*String);
982 if (isupper ((char) Index))
984 Index = Index - 'A' + 10;
997 /* Check to see if value is out of range: */
999 if (ReturnValue > ((ACPI_INTEGER_MAX - (ACPI_INTEGER) Index) /
1000 (ACPI_INTEGER) Base))
1006 ReturnValue *= Base;
1007 ReturnValue += Index;
1014 /* If a minus sign was present, then "the conversion is negated": */
1016 if (Sign == NEGATIVE)
1018 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1021 *RetInteger = ReturnValue;
1029 Status = AE_BAD_OCTAL_CONSTANT;
1033 Status = AE_BAD_DECIMAL_CONSTANT;
1037 Status = AE_BAD_HEX_CONSTANT;
1041 /* Base validated above */