2 /******************************************************************************
4 * Module Name: aslopcode - AML opcode generation
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
48 #include <contrib/dev/acpica/include/amlcode.h>
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslopcodes")
54 /* Local prototypes */
58 ACPI_PARSE_OBJECT *Op);
62 ACPI_PARSE_OBJECT *Op);
66 ACPI_PARSE_OBJECT *Op);
70 ACPI_PARSE_OBJECT *Op);
73 /*******************************************************************************
75 * FUNCTION: OpcAmlOpcodeUpdateWalk
77 * PARAMETERS: ASL_WALK_CALLBACK
81 * DESCRIPTION: Opcode update walk, ascending callback
83 ******************************************************************************/
86 OpcAmlOpcodeUpdateWalk (
87 ACPI_PARSE_OBJECT *Op,
93 * Handle the Package() case where the actual opcode cannot be determined
94 * until the PackageLength operand has been folded and minimized.
95 * (PackageOp versus VarPackageOp)
97 * This is (as of ACPI 3.0) the only case where the AML opcode can change
98 * based upon the value of a parameter.
100 * The parser always inserts a VarPackage opcode, which can possibly be
101 * optimized to a Package opcode.
103 if (Op->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE)
112 /*******************************************************************************
114 * FUNCTION: OpcAmlOpcodeWalk
116 * PARAMETERS: ASL_WALK_CALLBACK
120 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
123 ******************************************************************************/
127 ACPI_PARSE_OBJECT *Op,
134 OpcGenerateAmlOpcode (Op);
135 OpnGenerateAmlOperands (Op);
140 /*******************************************************************************
142 * FUNCTION: OpcGetIntegerWidth
144 * PARAMETERS: Op - DEFINITION BLOCK op
148 * DESCRIPTION: Extract integer width from the table revision
150 ******************************************************************************/
154 ACPI_PARSE_OBJECT *Op)
156 ACPI_PARSE_OBJECT *Child;
164 if (Gbl_RevisionOverride)
166 AcpiUtSetIntegerWidth (Gbl_RevisionOverride);
170 Child = Op->Asl.Child;
171 Child = Child->Asl.Next;
172 Child = Child->Asl.Next;
174 /* Use the revision to set the integer width */
176 AcpiUtSetIntegerWidth ((UINT8) Child->Asl.Value.Integer);
181 /*******************************************************************************
183 * FUNCTION: OpcSetOptimalIntegerSize
185 * PARAMETERS: Op - A parse tree node
187 * RETURN: Integer width, in bytes. Also sets the node AML opcode to the
188 * optimal integer AML prefix opcode.
190 * DESCRIPTION: Determine the optimal AML encoding of an integer. All leading
191 * zeros can be truncated to squeeze the integer into the
192 * minimal number of AML bytes.
194 ******************************************************************************/
197 OpcSetOptimalIntegerSize (
198 ACPI_PARSE_OBJECT *Op)
203 * TBD: - we don't want to optimize integers in the block header, but the
204 * code below does not work correctly.
206 if (Op->Asl.Parent &&
207 Op->Asl.Parent->Asl.Parent &&
208 (Op->Asl.Parent->Asl.Parent->Asl.ParseOpcode == PARSEOP_DEFINITIONBLOCK))
215 * Check for the special AML integers first - Zero, One, Ones.
216 * These are single-byte opcodes that are the smallest possible
217 * representation of an integer.
219 * This optimization is optional.
221 if (Gbl_IntegerOptimizationFlag)
223 switch (Op->Asl.Value.Integer)
227 Op->Asl.AmlOpcode = AML_ZERO_OP;
228 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
234 Op->Asl.AmlOpcode = AML_ONE_OP;
235 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
239 case ACPI_UINT32_MAX:
241 /* Check for table integer width (32 or 64) */
243 if (AcpiGbl_IntegerByteWidth == 4)
245 Op->Asl.AmlOpcode = AML_ONES_OP;
246 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
252 case ACPI_UINT64_MAX:
254 /* Check for table integer width (32 or 64) */
256 if (AcpiGbl_IntegerByteWidth == 8)
258 Op->Asl.AmlOpcode = AML_ONES_OP;
259 AslError (ASL_OPTIMIZATION, ASL_MSG_INTEGER_OPTIMIZATION,
270 /* Find the best fit using the various AML integer prefixes */
272 if (Op->Asl.Value.Integer <= ACPI_UINT8_MAX)
274 Op->Asl.AmlOpcode = AML_BYTE_OP;
277 if (Op->Asl.Value.Integer <= ACPI_UINT16_MAX)
279 Op->Asl.AmlOpcode = AML_WORD_OP;
282 if (Op->Asl.Value.Integer <= ACPI_UINT32_MAX)
284 Op->Asl.AmlOpcode = AML_DWORD_OP;
289 if (AcpiGbl_IntegerByteWidth == 4)
291 AslError (ASL_WARNING, ASL_MSG_INTEGER_LENGTH,
294 if (!Gbl_IgnoreErrors)
296 /* Truncate the integer to 32-bit */
297 Op->Asl.AmlOpcode = AML_DWORD_OP;
302 Op->Asl.AmlOpcode = AML_QWORD_OP;
308 /*******************************************************************************
310 * FUNCTION: OpcDoAccessAs
312 * PARAMETERS: Op - Parse node
316 * DESCRIPTION: Implement the ACCESS_AS ASL keyword.
318 ******************************************************************************/
322 ACPI_PARSE_OBJECT *Op)
324 ACPI_PARSE_OBJECT *Next;
327 Op->Asl.AmlOpcodeLength = 1;
328 Next = Op->Asl.Child;
330 /* First child is the access type */
332 Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
333 Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
335 /* Second child is the optional access attribute */
337 Next = Next->Asl.Next;
338 if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
340 Next->Asl.Value.Integer = 0;
342 Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
343 Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
347 /*******************************************************************************
349 * FUNCTION: OpcDoUnicode
351 * PARAMETERS: Op - Parse node
355 * DESCRIPTION: Implement the UNICODE ASL "macro". Convert the input string
356 * to a unicode buffer. There is no Unicode AML opcode.
358 * Note: The Unicode string is 16 bits per character, no leading signature,
359 * with a 16-bit terminating NULL.
361 ******************************************************************************/
365 ACPI_PARSE_OBJECT *Op)
367 ACPI_PARSE_OBJECT *InitializerOp;
372 UINT16 *UnicodeString;
373 ACPI_PARSE_OBJECT *BufferLengthOp;
376 /* Change op into a buffer object */
378 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
379 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
380 UtSetParseOpName (Op);
382 /* Buffer Length is first, followed by the string */
384 BufferLengthOp = Op->Asl.Child;
385 InitializerOp = BufferLengthOp->Asl.Next;
387 AsciiString = (UINT8 *) InitializerOp->Asl.Value.String;
389 /* Create a new buffer for the Unicode string */
391 Count = strlen (InitializerOp->Asl.Value.String) + 1;
392 Length = Count * sizeof (UINT16);
393 UnicodeString = UtLocalCalloc (Length);
395 /* Convert to Unicode string (including null terminator) */
397 for (i = 0; i < Count; i++)
399 UnicodeString[i] = (UINT16) AsciiString[i];
403 * Just set the buffer size node to be the buffer length, regardless
404 * of whether it was previously an integer or a default_arg placeholder
406 BufferLengthOp->Asl.ParseOpcode = PARSEOP_INTEGER;
407 BufferLengthOp->Asl.AmlOpcode = AML_DWORD_OP;
408 BufferLengthOp->Asl.Value.Integer = Length;
409 UtSetParseOpName (BufferLengthOp);
411 (void) OpcSetOptimalIntegerSize (BufferLengthOp);
413 /* The Unicode string is a raw data buffer */
415 InitializerOp->Asl.Value.Buffer = (UINT8 *) UnicodeString;
416 InitializerOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
417 InitializerOp->Asl.AmlLength = Length;
418 InitializerOp->Asl.ParseOpcode = PARSEOP_RAW_DATA;
419 InitializerOp->Asl.Child = NULL;
420 UtSetParseOpName (InitializerOp);
424 /*******************************************************************************
426 * FUNCTION: OpcDoEisaId
428 * PARAMETERS: Op - Parse node
432 * DESCRIPTION: Convert a string EISA ID to numeric representation. See the
433 * Pnp BIOS Specification for details. Here is an excerpt:
435 * A seven character ASCII representation of the product
436 * identifier compressed into a 32-bit identifier. The seven
437 * character ID consists of a three character manufacturer code,
438 * a three character hexadecimal product identifier, and a one
439 * character hexadecimal revision number. The manufacturer code
440 * is a 3 uppercase character code that is compressed into 3 5-bit
442 * 1) Find hex ASCII value for each letter
443 * 2) Subtract 40h from each ASCII value
444 * 3) Retain 5 least signficant bits for each letter by
445 * discarding upper 3 bits because they are always 0.
446 * 4) Compressed code = concatenate 0 and the 3 5-bit values
448 * The format of the compressed product identifier is as follows:
449 * Byte 0: Bit 7 - Reserved (0)
450 * Bits 6-2: - 1st character of compressed mfg code
451 * Bits 1-0 - Upper 2 bits of 2nd character of mfg code
452 * Byte 1: Bits 7-5 - Lower 3 bits of 2nd character of mfg code
453 * Bits 4-0 - 3rd character of mfg code
454 * Byte 2: Bits 7-4 - 1st hex digit of product number
455 * Bits 3-0 - 2nd hex digit of product number
456 * Byte 3: Bits 7-4 - 3st hex digit of product number
457 * Bits 3-0 - Hex digit of the revision number
459 ******************************************************************************/
463 ACPI_PARSE_OBJECT *Op)
468 ACPI_STATUS Status = AE_OK;
472 InString = (char *) Op->Asl.Value.String;
475 * The EISAID string must be exactly 7 characters and of the form
476 * "UUUXXXX" -- 3 uppercase letters and 4 hex digits (e.g., "PNP0001")
478 if (ACPI_STRLEN (InString) != 7)
480 Status = AE_BAD_PARAMETER;
484 /* Check all 7 characters for correct format */
486 for (i = 0; i < 7; i++)
488 /* First 3 characters must be uppercase letters */
492 if (!isupper ((int) InString[i]))
494 Status = AE_BAD_PARAMETER;
498 /* Last 4 characters must be hex digits */
500 else if (!isxdigit ((int) InString[i]))
502 Status = AE_BAD_PARAMETER;
507 if (ACPI_FAILURE (Status))
509 AslError (ASL_ERROR, ASL_MSG_INVALID_EISAID, Op, Op->Asl.Value.String);
513 /* Create ID big-endian first (bits are contiguous) */
516 (UINT32) ((UINT8) (InString[0] - 0x40)) << 26 |
517 (UINT32) ((UINT8) (InString[1] - 0x40)) << 21 |
518 (UINT32) ((UINT8) (InString[2] - 0x40)) << 16 |
520 (UtHexCharToValue (InString[3])) << 12 |
521 (UtHexCharToValue (InString[4])) << 8 |
522 (UtHexCharToValue (InString[5])) << 4 |
523 UtHexCharToValue (InString[6]);
525 /* Swap to little-endian to get final ID (see function header) */
527 EisaId = AcpiUtDwordByteSwap (BigEndianId);
531 * Morph the Op into an integer, regardless of whether there
532 * was an error in the EISAID string
534 Op->Asl.Value.Integer = EisaId;
536 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
537 Op->Asl.ParseOpcode = PARSEOP_INTEGER;
538 (void) OpcSetOptimalIntegerSize (Op);
540 /* Op is now an integer */
542 UtSetParseOpName (Op);
546 /*******************************************************************************
548 * FUNCTION: OpcDoUiId
550 * PARAMETERS: Op - Parse node
554 * DESCRIPTION: Convert UUID string to 16-byte buffer
556 ******************************************************************************/
560 ACPI_PARSE_OBJECT *Op)
564 ACPI_STATUS Status = AE_OK;
565 ACPI_PARSE_OBJECT *NewOp;
568 InString = (char *) Op->Asl.Value.String;
569 Buffer = UtLocalCalloc (16);
571 Status = AuValidateUuid (InString);
572 if (ACPI_FAILURE (Status))
574 AslError (ASL_ERROR, ASL_MSG_INVALID_UUID, Op, Op->Asl.Value.String);
578 (void) AuConvertStringToUuid (InString, Buffer);
581 /* Change Op to a Buffer */
583 Op->Asl.ParseOpcode = PARSEOP_BUFFER;
584 Op->Common.AmlOpcode = AML_BUFFER_OP;
586 /* Disable further optimization */
588 Op->Asl.CompileFlags &= ~NODE_COMPILE_TIME_CONST;
589 UtSetParseOpName (Op);
591 /* Child node is the buffer length */
593 NewOp = TrAllocateNode (PARSEOP_INTEGER);
595 NewOp->Asl.AmlOpcode = AML_BYTE_OP;
596 NewOp->Asl.Value.Integer = 16;
597 NewOp->Asl.Parent = Op;
599 Op->Asl.Child = NewOp;
602 /* Peer to the child is the raw buffer data */
604 NewOp = TrAllocateNode (PARSEOP_RAW_DATA);
605 NewOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
606 NewOp->Asl.AmlLength = 16;
607 NewOp->Asl.Value.String = (char *) Buffer;
608 NewOp->Asl.Parent = Op->Asl.Parent;
610 Op->Asl.Next = NewOp;
614 /*******************************************************************************
616 * FUNCTION: OpcGenerateAmlOpcode
618 * PARAMETERS: Op - Parse node
622 * DESCRIPTION: Generate the AML opcode associated with the node and its
623 * parse (lex/flex) keyword opcode. Essentially implements
624 * a mapping between the parse opcodes and the actual AML opcodes.
626 ******************************************************************************/
629 OpcGenerateAmlOpcode (
630 ACPI_PARSE_OBJECT *Op)
636 Index = (UINT16) (Op->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
638 Op->Asl.AmlOpcode = AslKeywordMapping[Index].AmlOpcode;
639 Op->Asl.AcpiBtype = AslKeywordMapping[Index].AcpiBtype;
640 Op->Asl.CompileFlags |= AslKeywordMapping[Index].Flags;
642 if (!Op->Asl.Value.Integer)
644 Op->Asl.Value.Integer = AslKeywordMapping[Index].Value;
647 /* Special handling for some opcodes */
649 switch (Op->Asl.ParseOpcode)
651 case PARSEOP_INTEGER:
653 * Set the opcode based on the size of the integer
655 (void) OpcSetOptimalIntegerSize (Op);
660 Op->Asl.AmlOpcodeLength = 1;
663 case PARSEOP_ACCESSAS:
678 case PARSEOP_UNICODE:
683 case PARSEOP_INCLUDE:
685 Op->Asl.Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
686 Gbl_HasIncludeFiles = TRUE;
689 case PARSEOP_EXTERNAL:
691 Op->Asl.Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
692 Op->Asl.Child->Asl.Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
696 /* Nothing to do for other opcodes */