2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
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
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
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;
38 * The above copyright and patent license is granted only if the following
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.
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
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
73 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
81 * 4. Disclaimer and Export Compliance
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
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
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.
115 *****************************************************************************/
118 #include "aslcompiler.h"
119 #include "aslcompiler.y.h"
120 #include "acnamesp.h"
123 #define _COMPONENT ACPI_COMPILER
124 ACPI_MODULE_NAME ("aslutils")
126 #ifdef _USE_BERKELEY_YACC
127 extern const char * const AslCompilername
[];
128 static const char * const *yytname
= &AslCompilername
[254];
130 extern const char * const yytname
[];
135 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
139 /* Local prototypes */
145 ACPI_INTEGER
*RetInteger
);
148 UtPadNameWithUnderscores (
150 char *PaddedNameSeg
);
154 ACPI_PARSE_OBJECT
*Op
,
158 /*******************************************************************************
160 * FUNCTION: AcpiPsDisplayConstantOpcodes
166 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
168 ******************************************************************************/
171 UtDisplayConstantOpcodes (
177 printf ("Constant expression opcode information\n\n");
179 for (i
= 0; i
< sizeof (AcpiGbl_AmlOpInfo
) / sizeof (ACPI_OPCODE_INFO
); i
++)
181 if (AcpiGbl_AmlOpInfo
[i
].Flags
& AML_CONSTANT
)
183 printf ("%s\n", AcpiGbl_AmlOpInfo
[i
].Name
);
189 /*******************************************************************************
191 * FUNCTION: UtLocalCalloc
193 * PARAMETERS: Size - Bytes to be allocated
195 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
197 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
198 * allocation failure, on the assumption that nothing more can be
201 ******************************************************************************/
210 Allocated
= ACPI_ALLOCATE_ZEROED (Size
);
213 AslCommonError (ASL_ERROR
, ASL_MSG_MEMORY_ALLOCATION
,
214 Gbl_CurrentLineNumber
, Gbl_LogicalLineNumber
,
215 Gbl_InputByteCount
, Gbl_CurrentColumn
,
216 Gbl_Files
[ASL_FILE_INPUT
].Filename
, NULL
);
221 TotalAllocated
+= Size
;
226 /*******************************************************************************
228 * FUNCTION: UtBeginEvent
230 * PARAMETERS: Name - Ascii name of this event
232 * RETURN: Event - Event number (integer index)
234 * DESCRIPTION: Saves the current time with this event
236 ******************************************************************************/
243 if (AslGbl_NextEvent
>= ASL_NUM_EVENTS
)
245 AcpiOsPrintf ("Ran out of compiler event structs!\n");
246 return (AslGbl_NextEvent
);
249 /* Init event with current (start) time */
251 AslGbl_Events
[AslGbl_NextEvent
].StartTime
= AcpiOsGetTimer ();
252 AslGbl_Events
[AslGbl_NextEvent
].EventName
= Name
;
253 AslGbl_Events
[AslGbl_NextEvent
].Valid
= TRUE
;
255 return (AslGbl_NextEvent
++);
259 /*******************************************************************************
261 * FUNCTION: UtEndEvent
263 * PARAMETERS: Event - Event number (integer index)
267 * DESCRIPTION: Saves the current time (end time) with this event
269 ******************************************************************************/
276 if (Event
>= ASL_NUM_EVENTS
)
281 /* Insert end time for event */
283 AslGbl_Events
[Event
].EndTime
= AcpiOsGetTimer ();
287 /*******************************************************************************
289 * FUNCTION: UtHexCharToValue
291 * PARAMETERS: HexChar - Hex character in Ascii
293 * RETURN: The binary value of the hex character
295 * DESCRIPTION: Perform ascii-to-hex translation
297 ******************************************************************************/
306 return ((UINT8
) (HexChar
- 0x30));
311 return ((UINT8
) (HexChar
- 0x37));
314 return ((UINT8
) (HexChar
- 0x57));
318 /*******************************************************************************
320 * FUNCTION: UtConvertByteToHex
322 * PARAMETERS: RawByte - Binary data
323 * Buffer - Pointer to where the hex bytes will be stored
325 * RETURN: Ascii hex byte is stored in Buffer.
327 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
330 ******************************************************************************/
341 Buffer
[2] = (UINT8
) HexLookup
[(RawByte
>> 4) & 0xF];
342 Buffer
[3] = (UINT8
) HexLookup
[RawByte
& 0xF];
346 /*******************************************************************************
348 * FUNCTION: UtConvertByteToAsmHex
350 * PARAMETERS: RawByte - Binary data
351 * Buffer - Pointer to where the hex bytes will be stored
353 * RETURN: Ascii hex byte is stored in Buffer.
355 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
358 ******************************************************************************/
361 UtConvertByteToAsmHex (
367 Buffer
[1] = (UINT8
) HexLookup
[(RawByte
>> 4) & 0xF];
368 Buffer
[2] = (UINT8
) HexLookup
[RawByte
& 0xF];
373 /*******************************************************************************
377 * PARAMETERS: Type - Type of output
378 * Fmt - Printf format string
379 * ... - variable printf list
383 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
386 ******************************************************************************/
397 va_start (Args
, Fmt
);
404 if ((Type
== ASL_PARSE_OUTPUT
) &&
405 (!(AslCompilerdebug
)))
410 (void) vfprintf (stderr
, Fmt
, Args
);
416 /*******************************************************************************
418 * FUNCTION: UtPrintFormattedName
420 * PARAMETERS: ParseOpcode - Parser keyword ID
421 * Level - Indentation level
425 * DESCRIPTION: Print the ascii name of the parse opcode.
427 ******************************************************************************/
429 #define TEXT_OFFSET 10
432 UtPrintFormattedName (
439 DbgPrint (ASL_TREE_OUTPUT
,
440 "%*s", (3 * Level
), " ");
442 DbgPrint (ASL_TREE_OUTPUT
,
443 " %-20.20s", UtGetOpName (ParseOpcode
));
445 if (Level
< TEXT_OFFSET
)
447 DbgPrint (ASL_TREE_OUTPUT
,
448 "%*s", (TEXT_OFFSET
- Level
) * 3, " ");
453 /*******************************************************************************
455 * FUNCTION: UtSetParseOpName
461 * DESCRIPTION: Insert the ascii name of the parse opcode
463 ******************************************************************************/
467 ACPI_PARSE_OBJECT
*Op
)
470 strncpy (Op
->Asl
.ParseOpName
, UtGetOpName (Op
->Asl
.ParseOpcode
),
471 ACPI_MAX_PARSEOP_NAME
);
475 /*******************************************************************************
477 * FUNCTION: UtGetOpName
479 * PARAMETERS: ParseOpcode - Parser keyword ID
481 * RETURN: Pointer to the opcode name
483 * DESCRIPTION: Get the ascii name of the parse opcode
485 ******************************************************************************/
493 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
494 * Ignore first 8 characters of the name
496 return ((char *) yytname
497 [(ParseOpcode
- ASL_FIRST_PARSE_OPCODE
) + ASL_YYTNAME_START
] + 8);
501 /*******************************************************************************
503 * FUNCTION: UtDisplaySummary
505 * PARAMETERS: FileID - ID of outpout file
509 * DESCRIPTION: Display compilation statistics
511 ******************************************************************************/
518 if (FileId
!= ASL_FILE_STDOUT
)
520 /* Compiler name and version number */
522 FlPrintFile (FileId
, "%s version %X [%s]\n",
523 CompilerId
, (UINT32
) ACPI_CA_VERSION
, __DATE__
);
526 /* Input/Output summary */
529 "ASL Input: %s - %d lines, %d bytes, %d keywords\n",
530 Gbl_Files
[ASL_FILE_INPUT
].Filename
, Gbl_CurrentLineNumber
,
531 Gbl_InputByteCount
, TotalKeywords
);
535 if ((Gbl_ExceptionCount
[ASL_ERROR
] == 0) || (Gbl_IgnoreErrors
))
538 "AML Output: %s - %d bytes, %d named objects, %d executable opcodes\n\n",
539 Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
, Gbl_TableLength
,
540 TotalNamedObjects
, TotalExecutableOpcodes
);
546 "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n",
547 Gbl_ExceptionCount
[ASL_ERROR
],
548 Gbl_ExceptionCount
[ASL_WARNING
] +
549 Gbl_ExceptionCount
[ASL_WARNING2
] +
550 Gbl_ExceptionCount
[ASL_WARNING3
],
551 Gbl_ExceptionCount
[ASL_REMARK
],
552 Gbl_ExceptionCount
[ASL_OPTIMIZATION
]);
556 /*******************************************************************************
558 * FUNCTION: UtDisplaySummary
560 * PARAMETERS: Op - Integer parse node
561 * LowValue - Smallest allowed value
562 * HighValue - Largest allowed value
564 * RETURN: Op if OK, otherwise NULL
566 * DESCRIPTION: Check integer for an allowable range
568 ******************************************************************************/
571 UtCheckIntegerRange (
572 ACPI_PARSE_OBJECT
*Op
,
576 char *ParseError
= NULL
;
585 if (Op
->Asl
.Value
.Integer
< LowValue
)
587 ParseError
= "Value below valid range";
588 Op
->Asl
.Value
.Integer
= LowValue
;
591 if (Op
->Asl
.Value
.Integer
> HighValue
)
593 ParseError
= "Value above valid range";
594 Op
->Asl
.Value
.Integer
= HighValue
;
599 sprintf (Buffer
, "%s 0x%X-0x%X", ParseError
, LowValue
, HighValue
);
600 AslCompilererror (Buffer
);
609 /*******************************************************************************
611 * FUNCTION: UtGetStringBuffer
613 * PARAMETERS: Length - Size of buffer requested
615 * RETURN: Pointer to the buffer. Aborts on allocation failure
617 * DESCRIPTION: Allocate a string buffer. Bypass the local
618 * dynamic memory manager for performance reasons (This has a
619 * major impact on the speed of the compiler.)
621 ******************************************************************************/
630 if ((Gbl_StringCacheNext
+ Length
) >= Gbl_StringCacheLast
)
632 Gbl_StringCacheNext
= UtLocalCalloc (ASL_STRING_CACHE_SIZE
+ Length
);
633 Gbl_StringCacheLast
= Gbl_StringCacheNext
+ ASL_STRING_CACHE_SIZE
+
637 Buffer
= Gbl_StringCacheNext
;
638 Gbl_StringCacheNext
+= Length
;
644 /*******************************************************************************
646 * FUNCTION: UtInternalizeName
648 * PARAMETERS: ExternalName - Name to convert
649 * ConvertedName - Where the converted name is returned
653 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
655 ******************************************************************************/
660 char **ConvertedName
)
662 ACPI_NAMESTRING_INFO Info
;
671 /* Get the length of the new internal name */
673 Info
.ExternalName
= ExternalName
;
674 AcpiNsGetInternalNameLength (&Info
);
676 /* We need a segment to store the internal name */
678 Info
.InternalName
= UtGetStringBuffer (Info
.Length
);
679 if (!Info
.InternalName
)
681 return (AE_NO_MEMORY
);
686 Status
= AcpiNsBuildInternalName (&Info
);
687 if (ACPI_FAILURE (Status
))
692 *ConvertedName
= Info
.InternalName
;
697 /*******************************************************************************
699 * FUNCTION: UtPadNameWithUnderscores
701 * PARAMETERS: NameSeg - Input nameseg
702 * PaddedNameSeg - Output padded nameseg
704 * RETURN: Padded nameseg.
706 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
709 ******************************************************************************/
712 UtPadNameWithUnderscores (
719 for (i
= 0; (i
< ACPI_NAME_SIZE
); i
++)
723 *PaddedNameSeg
= *NameSeg
;
728 *PaddedNameSeg
= '_';
735 /*******************************************************************************
737 * FUNCTION: UtAttachNameseg
739 * PARAMETERS: Op - Parent parse node
740 * Name - Full ExternalName
742 * RETURN: None; Sets the NameSeg field in parent node
744 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
745 * in the NameSeg field of the Op.
747 ******************************************************************************/
751 ACPI_PARSE_OBJECT
*Op
,
755 char PaddedNameSeg
[4];
763 /* Look for the last dot in the namepath */
765 NameSeg
= strrchr (Name
, '.');
768 /* Found last dot, we have also found the final nameseg */
771 UtPadNameWithUnderscores (NameSeg
, PaddedNameSeg
);
775 /* No dots in the namepath, there is only a single nameseg. */
776 /* Handle prefixes */
778 while ((*Name
== '\\') || (*Name
== '^'))
783 /* Remaing string should be one single nameseg */
785 UtPadNameWithUnderscores (Name
, PaddedNameSeg
);
788 strncpy (Op
->Asl
.NameSeg
, PaddedNameSeg
, 4);
792 /*******************************************************************************
794 * FUNCTION: UtAttachNamepathToOwner
796 * PARAMETERS: Op - Parent parse node
797 * NameOp - Node that contains the name
799 * RETURN: Sets the ExternalName and Namepath in the parent node
801 * DESCRIPTION: Store the name in two forms in the parent node: The original
802 * (external) name, and the internalized name that is used within
803 * the ACPI namespace manager.
805 ******************************************************************************/
808 UtAttachNamepathToOwner (
809 ACPI_PARSE_OBJECT
*Op
,
810 ACPI_PARSE_OBJECT
*NameOp
)
815 /* Full external path */
817 Op
->Asl
.ExternalName
= NameOp
->Asl
.Value
.String
;
819 /* Save the NameOp for possible error reporting later */
821 Op
->Asl
.ParentMethod
= (void *) NameOp
;
823 /* Last nameseg of the path */
825 UtAttachNameseg (Op
, Op
->Asl
.ExternalName
);
827 /* Create internalized path */
829 Status
= UtInternalizeName (NameOp
->Asl
.Value
.String
, &Op
->Asl
.Namepath
);
830 if (ACPI_FAILURE (Status
))
832 /* TBD: abort on no memory */
837 /*******************************************************************************
839 * FUNCTION: UtDoConstant
841 * PARAMETERS: String - Hex, Octal, or Decimal string
843 * RETURN: Converted Integer
845 * DESCRIPTION: Convert a string to an integer. With error checking.
847 ******************************************************************************/
854 ACPI_INTEGER Converted
;
858 Status
= UtStrtoul64 (String
, 0, &Converted
);
859 if (ACPI_FAILURE (Status
))
861 sprintf (ErrBuf
, "%s %s\n", "Conversion error:",
862 AcpiFormatException (Status
));
863 AslCompilererror (ErrBuf
);
870 /* TBD: use version in ACPI CA main code base? */
872 /*******************************************************************************
874 * FUNCTION: UtStrtoul64
876 * PARAMETERS: String - Null terminated string
877 * Terminater - Where a pointer to the terminating byte is
879 * Base - Radix of the string
881 * RETURN: Converted value
883 * DESCRIPTION: Convert a string into an unsigned value.
885 ******************************************************************************/
891 ACPI_INTEGER
*RetInteger
)
895 ACPI_INTEGER ReturnValue
= 0;
896 ACPI_STATUS Status
= AE_OK
;
911 * The specified Base parameter is not in the domain of
914 return (AE_BAD_PARAMETER
);
917 /* Skip over any white space in the buffer: */
919 while (isspace (*String
) || *String
== '\t')
925 * The buffer may contain an optional plus or minus sign.
926 * If it does, then skip over it but remember what is was:
933 else if (*String
== '+')
944 * If the input parameter Base is zero, then we need to
945 * determine if it is octal, decimal, or hexadecimal:
951 if (tolower (*(++String
)) == 'x')
968 * For octal and hexadecimal bases, skip over the leading
969 * 0 or 0x, if they are present.
971 if (Base
== 8 && *String
== '0')
978 tolower (*(++String
)) == 'x')
983 /* Main loop: convert the string to an unsigned long */
987 if (isdigit (*String
))
989 Index
= ((UINT8
) *String
) - '0';
993 Index
= (UINT8
) toupper (*String
);
994 if (isupper ((char) Index
))
996 Index
= Index
- 'A' + 10;
1009 /* Check to see if value is out of range: */
1011 if (ReturnValue
> ((ACPI_INTEGER_MAX
- (ACPI_INTEGER
) Index
) /
1012 (ACPI_INTEGER
) Base
))
1018 ReturnValue
*= Base
;
1019 ReturnValue
+= Index
;
1026 /* If a minus sign was present, then "the conversion is negated": */
1028 if (Sign
== NEGATIVE
)
1030 ReturnValue
= (ACPI_UINT32_MAX
- ReturnValue
) + 1;
1033 *RetInteger
= ReturnValue
;
1041 Status
= AE_BAD_OCTAL_CONSTANT
;
1045 Status
= AE_BAD_DECIMAL_CONSTANT
;
1049 Status
= AE_BAD_HEX_CONSTANT
;
1053 /* Base validated above */