2 /******************************************************************************
4 * Module Name: aslopcode - AML opcode generation
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2011, 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"
122 #define _COMPONENT ACPI_COMPILER
123 ACPI_MODULE_NAME ("aslopcodes")
126 /* Local prototypes */
130 ACPI_PARSE_OBJECT *Op);
134 ACPI_PARSE_OBJECT *Op);
138 ACPI_PARSE_OBJECT *Op);
142 ACPI_PARSE_OBJECT *Op);
145 /*******************************************************************************
147 * FUNCTION: OpcAmlOpcodeUpdateWalk
149 * PARAMETERS: ASL_WALK_CALLBACK
153 * DESCRIPTION: Opcode update walk, ascending callback
155 ******************************************************************************/
158 OpcAmlOpcodeUpdateWalk (
159 ACPI_PARSE_OBJECT *Op,
165 * Handle the Package() case where the actual opcode cannot be determined
166 * until the PackageLength operand has been folded and minimized.
167 * (PackageOp versus VarPackageOp)
169 * This is (as of ACPI 3.0) the only case where the AML opcode can change
170 * based upon the value of a parameter.
172 * The parser always inserts a VarPackage opcode, which can possibly be
173 * optimized to a Package opcode.
175 if (Op->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE)
184 /*******************************************************************************
186 * FUNCTION: OpcAmlOpcodeWalk
188 * PARAMETERS: ASL_WALK_CALLBACK
192 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
195 ******************************************************************************/
199 ACPI_PARSE_OBJECT *Op,
206 OpcGenerateAmlOpcode (Op);
207 OpnGenerateAmlOperands (Op);
212 /*******************************************************************************
214 * FUNCTION: OpcGetIntegerWidth
216 * PARAMETERS: Op - DEFINITION BLOCK op
220 * DESCRIPTION: Extract integer width from the table revision
222 ******************************************************************************/
226 ACPI_PARSE_OBJECT *Op)
228 ACPI_PARSE_OBJECT *Child;
236 if (Gbl_RevisionOverride)
238 AcpiUtSetIntegerWidth (Gbl_RevisionOverride);
242 Child = Op->Asl.Child;
243 Child = Child->Asl.Next;
244 Child = Child->Asl.Next;
246 /* Use the revision to set the integer width */
248 AcpiUtSetIntegerWidth ((UINT8) Child->Asl.Value.Integer);
253 /*******************************************************************************
255 * FUNCTION: OpcSetOptimalIntegerSize
257 * PARAMETERS: Op - A parse tree node
259 * RETURN: Integer width, in bytes. Also sets the node AML opcode to the
260 * optimal integer AML prefix opcode.
262 * DESCRIPTION: Determine the optimal AML encoding of an integer. All leading
263 * zeros can be truncated to squeeze the integer into the
264 * minimal number of AML bytes.
266 ******************************************************************************/
269 OpcSetOptimalIntegerSize (
270 ACPI_PARSE_OBJECT *Op)
275 * TBD: - we don't want to optimize integers in the block header, but the
276 * code below does not work correctly.
278 if (Op->Asl.Parent &&
279 Op->Asl.Parent->Asl.Parent &&
280 (Op->Asl.Parent->Asl.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK))
287 * Check for the special AML integers first - Zero, One, Ones.
288 * These are single-byte opcodes that are the smallest possible
289 * representation of an integer.
291 * This optimization is optional.
293 if (Gbl_IntegerOptimizationFlag)
295 switch (Op->Asl.Value.Integer)
299 Op->Asl.AmlOpcode = AML_ZERO_OP;
300 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
306 Op->Asl.AmlOpcode = AML_ONE_OP;
307 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
311 case ACPI_UINT32_MAX:
313 /* Check for table integer width (32 or 64) */
315 if (AcpiGbl_IntegerByteWidth == 4)
317 Op->Asl.AmlOpcode = AML_ONES_OP;
318 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
324 case ACPI_UINT64_MAX:
326 /* Check for table integer width (32 or 64) */
328 if (AcpiGbl_IntegerByteWidth == 8)
330 Op->Asl.AmlOpcode = AML_ONES_OP;
331 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
342 /* Find the best fit using the various AML integer prefixes */
344 if (Op->Asl.Value.Integer <= ACPI_UINT8_MAX)
346 Op->Asl.AmlOpcode = AML_BYTE_OP;
349 if (Op->Asl.Value.Integer <= ACPI_UINT16_MAX)
351 Op->Asl.AmlOpcode = AML_WORD_OP;
354 if (Op->Asl.Value.Integer <= ACPI_UINT32_MAX)
356 Op->Asl.AmlOpcode = AML_DWORD_OP;
361 if (AcpiGbl_IntegerByteWidth == 4)
363 AslError (ASL_WARNING, ASL_MSG_INTEGER_LENGTH,
366 if (!Gbl_IgnoreErrors)
368 /* Truncate the integer to 32-bit */
369 Op->Asl.AmlOpcode = AML_DWORD_OP;
374 Op->Asl.AmlOpcode = AML_QWORD_OP;
380 /*******************************************************************************
382 * FUNCTION: OpcDoAccessAs
384 * PARAMETERS: Op - Parse node
388 * DESCRIPTION: Implement the ACCESS_AS ASL keyword.
390 ******************************************************************************/
394 ACPI_PARSE_OBJECT *Op)
396 ACPI_PARSE_OBJECT *Next;
399 Op->Asl.AmlOpcodeLength = 1;
400 Next = Op->Asl.Child;
402 /* First child is the access type */
404 Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
405 Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
407 /* Second child is the optional access attribute */
409 Next = Next->Asl.Next;
410 if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
412 Next->Asl.Value.Integer = 0;
414 Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
415 Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
419 /*******************************************************************************
421 * FUNCTION: OpcDoUnicode
423 * PARAMETERS: Op - Parse node
427 * DESCRIPTION: Implement the UNICODE ASL "macro". Convert the input string
428 * to a unicode buffer. There is no Unicode AML opcode.
430 * Note: The Unicode string is 16 bits per character, no leading signature,
431 * with a 16-bit terminating NULL.
433 ******************************************************************************/
437 ACPI_PARSE_OBJECT *Op)
439 ACPI_PARSE_OBJECT *InitializerOp;
444 UINT16 *UnicodeString;
445 ACPI_PARSE_OBJECT *BufferLengthOp;
448 /* Change op into a buffer object */
450 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
451 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
452 UtSetParseOpName (Op);
454 /* Buffer Length is first, followed by the string */
456 BufferLengthOp = Op->Asl.Child;
457 InitializerOp = BufferLengthOp->Asl.Next;
459 AsciiString = (UINT8 *) InitializerOp->Asl.Value.String;
461 /* Create a new buffer for the Unicode string */
463 Count = strlen (InitializerOp->Asl.Value.String) + 1;
464 Length = Count * sizeof (UINT16);
465 UnicodeString = UtLocalCalloc (Length);
467 /* Convert to Unicode string (including null terminator) */
469 for (i = 0; i < Count; i++)
471 UnicodeString[i] = (UINT16) AsciiString[i];
475 * Just set the buffer size node to be the buffer length, regardless
476 * of whether it was previously an integer or a default_arg placeholder
478 BufferLengthOp->Asl.ParseOpcode = PARSEOP_INTEGER;
479 BufferLengthOp->Asl.AmlOpcode = AML_DWORD_OP;
480 BufferLengthOp->Asl.Value.Integer = Length;
481 UtSetParseOpName (BufferLengthOp);
483 (void) OpcSetOptimalIntegerSize (BufferLengthOp);
485 /* The Unicode string is a raw data buffer */
487 InitializerOp->Asl.Value.Buffer = (UINT8 *) UnicodeString;
488 InitializerOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
489 InitializerOp->Asl.AmlLength = Length;
490 InitializerOp->Asl.ParseOpcode = PARSEOP_RAW_DATA;
491 InitializerOp->Asl.Child = NULL;
492 UtSetParseOpName (InitializerOp);
496 /*******************************************************************************
498 * FUNCTION: OpcDoEisaId
500 * PARAMETERS: Op - Parse node
504 * DESCRIPTION: Convert a string EISA ID to numeric representation. See the
505 * Pnp BIOS Specification for details. Here is an excerpt:
507 * A seven character ASCII representation of the product
508 * identifier compressed into a 32-bit identifier. The seven
509 * character ID consists of a three character manufacturer code,
510 * a three character hexadecimal product identifier, and a one
511 * character hexadecimal revision number. The manufacturer code
512 * is a 3 uppercase character code that is compressed into 3 5-bit
514 * 1) Find hex ASCII value for each letter
515 * 2) Subtract 40h from each ASCII value
516 * 3) Retain 5 least signficant bits for each letter by
517 * discarding upper 3 bits because they are always 0.
518 * 4) Compressed code = concatenate 0 and the 3 5-bit values
520 * The format of the compressed product identifier is as follows:
521 * Byte 0: Bit 7 - Reserved (0)
522 * Bits 6-2: - 1st character of compressed mfg code
523 * Bits 1-0 - Upper 2 bits of 2nd character of mfg code
524 * Byte 1: Bits 7-5 - Lower 3 bits of 2nd character of mfg code
525 * Bits 4-0 - 3rd character of mfg code
526 * Byte 2: Bits 7-4 - 1st hex digit of product number
527 * Bits 3-0 - 2nd hex digit of product number
528 * Byte 3: Bits 7-4 - 3st hex digit of product number
529 * Bits 3-0 - Hex digit of the revision number
531 ******************************************************************************/
535 ACPI_PARSE_OBJECT *Op)
540 ACPI_STATUS Status = AE_OK;
544 InString = (char *) Op->Asl.Value.String;
547 * The EISAID string must be exactly 7 characters and of the form
548 * "UUUXXXX" -- 3 uppercase letters and 4 hex digits (e.g., "PNP0001")
550 if (ACPI_STRLEN (InString) != 7)
552 Status = AE_BAD_PARAMETER;
556 /* Check all 7 characters for correct format */
558 for (i = 0; i < 7; i++)
560 /* First 3 characters must be uppercase letters */
564 if (!isupper ((int) InString[i]))
566 Status = AE_BAD_PARAMETER;
570 /* Last 4 characters must be hex digits */
572 else if (!isxdigit ((int) InString[i]))
574 Status = AE_BAD_PARAMETER;
579 if (ACPI_FAILURE (Status))
581 AslError (ASL_ERROR, ASL_MSG_INVALID_EISAID, Op, Op->Asl.Value.String);
585 /* Create ID big-endian first (bits are contiguous) */
588 (UINT32) ((UINT8) (InString[0] - 0x40)) << 26 |
589 (UINT32) ((UINT8) (InString[1] - 0x40)) << 21 |
590 (UINT32) ((UINT8) (InString[2] - 0x40)) << 16 |
592 (UtHexCharToValue (InString[3])) << 12 |
593 (UtHexCharToValue (InString[4])) << 8 |
594 (UtHexCharToValue (InString[5])) << 4 |
595 UtHexCharToValue (InString[6]);
597 /* Swap to little-endian to get final ID (see function header) */
599 EisaId = AcpiUtDwordByteSwap (BigEndianId);
603 * Morph the Op into an integer, regardless of whether there
604 * was an error in the EISAID string
606 Op->Asl.Value.Integer = EisaId;
608 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
609 Op->Asl.ParseOpcode = PARSEOP_INTEGER;
610 (void) OpcSetOptimalIntegerSize (Op);
612 /* Op is now an integer */
614 UtSetParseOpName (Op);
618 /*******************************************************************************
620 * FUNCTION: OpcDoUiId
622 * PARAMETERS: Op - Parse node
626 * DESCRIPTION: Convert UUID string to 16-byte buffer
628 ******************************************************************************/
632 ACPI_PARSE_OBJECT *Op)
636 ACPI_STATUS Status = AE_OK;
637 ACPI_PARSE_OBJECT *NewOp;
640 InString = (char *) Op->Asl.Value.String;
641 Buffer = UtLocalCalloc (16);
643 Status = AuValidateUuid (InString);
644 if (ACPI_FAILURE (Status))
646 AslError (ASL_ERROR, ASL_MSG_INVALID_UUID, Op, Op->Asl.Value.String);
650 (void) AuConvertStringToUuid (InString, Buffer);
653 /* Change Op to a Buffer */
655 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
656 Op->Common.AmlOpcode = AML_BUFFER_OP;
658 /* Disable further optimization */
660 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
661 UtSetParseOpName (Op);
663 /* Child node is the buffer length */
665 NewOp = TrAllocateNode (PARSEOP_INTEGER);
667 NewOp->Asl.AmlOpcode = AML_BYTE_OP;
668 NewOp->Asl.Value.Integer = 16;
669 NewOp->Asl.Parent = Op;
671 Op->Asl.Child = NewOp;
674 /* Peer to the child is the raw buffer data */
676 NewOp = TrAllocateNode (PARSEOP_RAW_DATA);
677 NewOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
678 NewOp->Asl.AmlLength = 16;
679 NewOp->Asl.Value.String = (char *) Buffer;
680 NewOp->Asl.Parent = Op->Asl.Parent;
682 Op->Asl.Next = NewOp;
686 /*******************************************************************************
688 * FUNCTION: OpcGenerateAmlOpcode
690 * PARAMETERS: Op - Parse node
694 * DESCRIPTION: Generate the AML opcode associated with the node and its
695 * parse (lex/flex) keyword opcode. Essentially implements
696 * a mapping between the parse opcodes and the actual AML opcodes.
698 ******************************************************************************/
701 OpcGenerateAmlOpcode (
702 ACPI_PARSE_OBJECT *Op)
708 Index = (UINT16) (Op->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
710 Op->Asl.AmlOpcode = AslKeywordMapping[Index].AmlOpcode;
711 Op->Asl.AcpiBtype = AslKeywordMapping[Index].AcpiBtype;
712 Op->Asl.CompileFlags |= AslKeywordMapping[Index].Flags;
714 if (!Op->Asl.Value.Integer)
716 Op->Asl.Value.Integer = AslKeywordMapping[Index].Value;
719 /* Special handling for some opcodes */
721 switch (Op->Asl.ParseOpcode)
723 case PARSEOP_INTEGER:
725 * Set the opcode based on the size of the integer
727 (void) OpcSetOptimalIntegerSize (Op);
732 Op->Asl.AmlOpcodeLength = 1;
735 case PARSEOP_ACCESSAS:
750 case PARSEOP_UNICODE:
755 case PARSEOP_INCLUDE:
757 Op->Asl.Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
758 Gbl_HasIncludeFiles = TRUE;
761 case PARSEOP_EXTERNAL:
763 Op->Asl.Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
764 Op->Asl.Child->Asl.Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
768 /* Nothing to do for other opcodes */