2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2006, 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 "aslcompiler.h"
120 #include "aslcompiler.y.h"
121 #include "acnamesp.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_ALLOCATE_ZEROED (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 (
362 Buffer
[1] = (UINT8
) hex
[(RawByte
>> 4) & 0xF];
363 Buffer
[2] = (UINT8
) hex
[RawByte
& 0xF];
368 /*******************************************************************************
372 * PARAMETERS: Type - Type of output
373 * Fmt - Printf format string
374 * ... - variable printf list
378 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
381 ******************************************************************************/
392 va_start (Args
, Fmt
);
399 if ((Type
== ASL_PARSE_OUTPUT
) &&
400 (!(AslCompilerdebug
)))
405 (void) vfprintf (stderr
, Fmt
, Args
);
411 /*******************************************************************************
413 * FUNCTION: UtPrintFormattedName
415 * PARAMETERS: ParseOpcode - Parser keyword ID
416 * Level - Indentation level
420 * DESCRIPTION: Print the ascii name of the parse opcode.
422 ******************************************************************************/
424 #define TEXT_OFFSET 10
427 UtPrintFormattedName (
434 DbgPrint (ASL_TREE_OUTPUT
,
435 "%*s", (3 * Level
), " ");
437 DbgPrint (ASL_TREE_OUTPUT
,
438 " %-20.20s", UtGetOpName (ParseOpcode
));
440 if (Level
< TEXT_OFFSET
)
442 DbgPrint (ASL_TREE_OUTPUT
,
443 "%*s", (TEXT_OFFSET
- Level
) * 3, " ");
448 /*******************************************************************************
450 * FUNCTION: UtSetParseOpName
456 * DESCRIPTION: Insert the ascii name of the parse opcode
458 ******************************************************************************/
462 ACPI_PARSE_OBJECT
*Op
)
465 strncpy (Op
->Asl
.ParseOpName
, UtGetOpName (Op
->Asl
.ParseOpcode
),
466 ACPI_MAX_PARSEOP_NAME
);
470 /*******************************************************************************
472 * FUNCTION: UtGetOpName
474 * PARAMETERS: ParseOpcode - Parser keyword ID
476 * RETURN: Pointer to the opcode name
478 * DESCRIPTION: Get the ascii name of the parse opcode
480 ******************************************************************************/
488 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
489 * Ignore first 8 characters of the name
491 return ((char *) yytname
492 [(ParseOpcode
- ASL_FIRST_PARSE_OPCODE
) + ASL_YYTNAME_START
] + 8);
496 /*******************************************************************************
498 * FUNCTION: UtDisplaySummary
500 * PARAMETERS: FileID - ID of outpout file
504 * DESCRIPTION: Display compilation statistics
506 ******************************************************************************/
513 if (FileId
!= ASL_FILE_STDOUT
)
515 /* Compiler name and version number */
517 FlPrintFile (FileId
, "%s version %X [%s]\n",
518 CompilerId
, (UINT32
) ACPI_CA_VERSION
, __DATE__
);
521 /* Input/Output summary */
524 "ASL Input: %s - %d lines, %d bytes, %d keywords\n",
525 Gbl_Files
[ASL_FILE_INPUT
].Filename
, Gbl_CurrentLineNumber
,
526 Gbl_InputByteCount
, TotalKeywords
);
530 if ((Gbl_ExceptionCount
[ASL_ERROR
] == 0) || (Gbl_IgnoreErrors
))
533 "AML Output: %s - %d bytes %d named objects %d executable opcodes\n\n",
534 Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
, Gbl_TableLength
,
535 TotalNamedObjects
, TotalExecutableOpcodes
);
541 "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n",
542 Gbl_ExceptionCount
[ASL_ERROR
],
543 Gbl_ExceptionCount
[ASL_WARNING
] +
544 Gbl_ExceptionCount
[ASL_WARNING2
] +
545 Gbl_ExceptionCount
[ASL_WARNING3
],
546 Gbl_ExceptionCount
[ASL_REMARK
],
547 Gbl_ExceptionCount
[ASL_OPTIMIZATION
]);
551 /*******************************************************************************
553 * FUNCTION: UtDisplaySummary
555 * PARAMETERS: Op - Integer parse node
556 * LowValue - Smallest allowed value
557 * HighValue - Largest allowed value
559 * RETURN: Op if OK, otherwise NULL
561 * DESCRIPTION: Check integer for an allowable range
563 ******************************************************************************/
566 UtCheckIntegerRange (
567 ACPI_PARSE_OBJECT
*Op
,
571 char *ParseError
= NULL
;
580 if (Op
->Asl
.Value
.Integer
< LowValue
)
582 ParseError
= "Value below valid range";
583 Op
->Asl
.Value
.Integer
= LowValue
;
586 if (Op
->Asl
.Value
.Integer
> HighValue
)
588 ParseError
= "Value above valid range";
589 Op
->Asl
.Value
.Integer
= HighValue
;
594 sprintf (Buffer
, "%s 0x%X-0x%X", ParseError
, LowValue
, HighValue
);
595 AslCompilererror (Buffer
);
604 /*******************************************************************************
606 * FUNCTION: UtGetStringBuffer
608 * PARAMETERS: Length - Size of buffer requested
610 * RETURN: Pointer to the buffer. Aborts on allocation failure
612 * DESCRIPTION: Allocate a string buffer. Bypass the local
613 * dynamic memory manager for performance reasons (This has a
614 * major impact on the speed of the compiler.)
616 ******************************************************************************/
625 if ((Gbl_StringCacheNext
+ Length
) >= Gbl_StringCacheLast
)
627 Gbl_StringCacheNext
= UtLocalCalloc (ASL_STRING_CACHE_SIZE
+ Length
);
628 Gbl_StringCacheLast
= Gbl_StringCacheNext
+ ASL_STRING_CACHE_SIZE
+
632 Buffer
= Gbl_StringCacheNext
;
633 Gbl_StringCacheNext
+= Length
;
639 /*******************************************************************************
641 * FUNCTION: UtInternalizeName
643 * PARAMETERS: ExternalName - Name to convert
644 * ConvertedName - Where the converted name is returned
648 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
650 ******************************************************************************/
655 char **ConvertedName
)
657 ACPI_NAMESTRING_INFO Info
;
666 /* Get the length of the new internal name */
668 Info
.ExternalName
= ExternalName
;
669 AcpiNsGetInternalNameLength (&Info
);
671 /* We need a segment to store the internal name */
673 Info
.InternalName
= UtGetStringBuffer (Info
.Length
);
674 if (!Info
.InternalName
)
676 return (AE_NO_MEMORY
);
681 Status
= AcpiNsBuildInternalName (&Info
);
682 if (ACPI_FAILURE (Status
))
687 *ConvertedName
= Info
.InternalName
;
692 /*******************************************************************************
694 * FUNCTION: UtPadNameWithUnderscores
696 * PARAMETERS: NameSeg - Input nameseg
697 * PaddedNameSeg - Output padded nameseg
699 * RETURN: Padded nameseg.
701 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
704 ******************************************************************************/
707 UtPadNameWithUnderscores (
714 for (i
= 0; (i
< ACPI_NAME_SIZE
); i
++)
718 *PaddedNameSeg
= *NameSeg
;
723 *PaddedNameSeg
= '_';
730 /*******************************************************************************
732 * FUNCTION: UtAttachNameseg
734 * PARAMETERS: Op - Parent parse node
735 * Name - Full ExternalName
737 * RETURN: None; Sets the NameSeg field in parent node
739 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
740 * in the NameSeg field of the Op.
742 ******************************************************************************/
746 ACPI_PARSE_OBJECT
*Op
,
750 char PaddedNameSeg
[4];
758 /* Look for the last dot in the namepath */
760 NameSeg
= strrchr (Name
, '.');
763 /* Found last dot, we have also found the final nameseg */
766 UtPadNameWithUnderscores (NameSeg
, PaddedNameSeg
);
770 /* No dots in the namepath, there is only a single nameseg. */
771 /* Handle prefixes */
773 while ((*Name
== '\\') || (*Name
== '^'))
778 /* Remaing string should be one single nameseg */
780 UtPadNameWithUnderscores (Name
, PaddedNameSeg
);
783 strncpy (Op
->Asl
.NameSeg
, PaddedNameSeg
, 4);
787 /*******************************************************************************
789 * FUNCTION: UtAttachNamepathToOwner
791 * PARAMETERS: Op - Parent parse node
792 * NameOp - Node that contains the name
794 * RETURN: Sets the ExternalName and Namepath in the parent node
796 * DESCRIPTION: Store the name in two forms in the parent node: The original
797 * (external) name, and the internalized name that is used within
798 * the ACPI namespace manager.
800 ******************************************************************************/
803 UtAttachNamepathToOwner (
804 ACPI_PARSE_OBJECT
*Op
,
805 ACPI_PARSE_OBJECT
*NameOp
)
810 /* Full external path */
812 Op
->Asl
.ExternalName
= NameOp
->Asl
.Value
.String
;
814 /* Save the NameOp for possible error reporting later */
816 Op
->Asl
.ParentMethod
= (void *) NameOp
;
818 /* Last nameseg of the path */
820 UtAttachNameseg (Op
, Op
->Asl
.ExternalName
);
822 /* Create internalized path */
824 Status
= UtInternalizeName (NameOp
->Asl
.Value
.String
, &Op
->Asl
.Namepath
);
825 if (ACPI_FAILURE (Status
))
827 /* TBD: abort on no memory */
832 /*******************************************************************************
834 * FUNCTION: UtDoConstant
836 * PARAMETERS: String - Hex, Octal, or Decimal string
838 * RETURN: Converted Integer
840 * DESCRIPTION: Convert a string to an integer. With error checking.
842 ******************************************************************************/
849 ACPI_INTEGER Converted
;
853 Status
= UtStrtoul64 (String
, 0, &Converted
);
854 if (ACPI_FAILURE (Status
))
856 sprintf (ErrBuf
, "%s %s\n", "Conversion error:",
857 AcpiFormatException (Status
));
858 AslCompilererror (ErrBuf
);
865 /* TBD: use version in ACPI CA main code base? */
867 /*******************************************************************************
869 * FUNCTION: UtStrtoul64
871 * PARAMETERS: String - Null terminated string
872 * Terminater - Where a pointer to the terminating byte is
874 * Base - Radix of the string
876 * RETURN: Converted value
878 * DESCRIPTION: Convert a string into an unsigned value.
880 ******************************************************************************/
886 ACPI_INTEGER
*RetInteger
)
890 ACPI_INTEGER ReturnValue
= 0;
891 ACPI_STATUS Status
= AE_OK
;
906 * The specified Base parameter is not in the domain of
909 return (AE_BAD_PARAMETER
);
912 /* Skip over any white space in the buffer: */
914 while (isspace (*String
) || *String
== '\t')
920 * The buffer may contain an optional plus or minus sign.
921 * If it does, then skip over it but remember what is was:
928 else if (*String
== '+')
939 * If the input parameter Base is zero, then we need to
940 * determine if it is octal, decimal, or hexadecimal:
946 if (tolower (*(++String
)) == 'x')
963 * For octal and hexadecimal bases, skip over the leading
964 * 0 or 0x, if they are present.
966 if (Base
== 8 && *String
== '0')
973 tolower (*(++String
)) == 'x')
978 /* Main loop: convert the string to an unsigned long */
982 if (isdigit (*String
))
984 Index
= ((UINT8
) *String
) - '0';
988 Index
= (UINT8
) toupper (*String
);
989 if (isupper ((char) Index
))
991 Index
= Index
- 'A' + 10;
1004 /* Check to see if value is out of range: */
1006 if (ReturnValue
> ((ACPI_INTEGER_MAX
- (ACPI_INTEGER
) Index
) /
1007 (ACPI_INTEGER
) Base
))
1013 ReturnValue
*= Base
;
1014 ReturnValue
+= Index
;
1021 /* If a minus sign was present, then "the conversion is negated": */
1023 if (Sign
== NEGATIVE
)
1025 ReturnValue
= (ACPI_UINT32_MAX
- ReturnValue
) + 1;
1028 *RetInteger
= ReturnValue
;
1036 Status
= AE_BAD_OCTAL_CONSTANT
;
1040 Status
= AE_BAD_DECIMAL_CONSTANT
;
1044 Status
= AE_BAD_HEX_CONSTANT
;
1048 /* Base validated above */