1 2 /****************************************************************************** 3 * 4 * Module Name: aslcodegen - AML code generation 5 * $Revision: 1.57 $ 6 * 7 *****************************************************************************/ 8 9 /****************************************************************************** 10 * 11 * 1. Copyright Notice 12 * 13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp. 14 * All rights reserved. 15 * 16 * 2. License 17 * 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 21 * property rights. 22 * 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 29 * 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; 38 * 39 * The above copyright and patent license is granted only if the following 40 * conditions are met: 41 * 42 * 3. Conditions 43 * 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. 55 * 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 66 * make. 67 * 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 72 * distribution. 73 * 74 * 3.4. Intel retains all right, title, and interest in and to the Original 75 * Intel Code. 76 * 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. 81 * 82 * 4. Disclaimer and Export Compliance 83 * 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 90 * PARTICULAR PURPOSE. 91 * 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 99 * LIMITED REMEDY. 100 * 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. 115 * 116 *****************************************************************************/ 117 118 119 #include <contrib/dev/acpica/compiler/aslcompiler.h> 120 #include "aslcompiler.y.h" 121 #include <contrib/dev/acpica/amlcode.h> 122 123 #define _COMPONENT ACPI_COMPILER 124 ACPI_MODULE_NAME ("aslcodegen") 125 126 /* Local prototypes */ 127 128 static ACPI_STATUS 129 CgAmlWriteWalk ( 130 ACPI_PARSE_OBJECT *Op, 131 UINT32 Level, 132 void *Context); 133 134 static void 135 CgLocalWriteAmlData ( 136 ACPI_PARSE_OBJECT *Op, 137 void *Buffer, 138 UINT32 Length); 139 140 static void 141 CgWriteAmlOpcode ( 142 ACPI_PARSE_OBJECT *Op); 143 144 static void 145 CgWriteTableHeader ( 146 ACPI_PARSE_OBJECT *Op); 147 148 static void 149 CgCloseTable ( 150 void); 151 152 static void 153 CgWriteNode ( 154 ACPI_PARSE_OBJECT *Op); 155 156 157 /******************************************************************************* 158 * 159 * FUNCTION: CgGenerateAmlOutput 160 * 161 * PARAMETERS: None. 162 * 163 * RETURN: None 164 * 165 * DESCRIPTION: Generate AML code. Currently generates the listing file 166 * simultaneously. 167 * 168 ******************************************************************************/ 169 170 void 171 CgGenerateAmlOutput ( 172 void) 173 { 174 175 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n"); 176 177 /* Generate the AML output file */ 178 179 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0); 180 Gbl_SourceLine = 0; 181 Gbl_NextError = Gbl_ErrorLog; 182 183 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD, 184 CgAmlWriteWalk, NULL, NULL); 185 CgCloseTable (); 186 } 187 188 189 /******************************************************************************* 190 * 191 * FUNCTION: CgAmlWriteWalk 192 * 193 * PARAMETERS: ASL_WALK_CALLBACK 194 * 195 * RETURN: Status 196 * 197 * DESCRIPTION: Parse tree walk to generate the AML code. 198 * 199 ******************************************************************************/ 200 201 static ACPI_STATUS 202 CgAmlWriteWalk ( 203 ACPI_PARSE_OBJECT *Op, 204 UINT32 Level, 205 void *Context) 206 { 207 208 /* Debug output */ 209 210 DbgPrint (ASL_TREE_OUTPUT, 211 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level); 212 UtPrintFormattedName (Op->Asl.ParseOpcode, Level); 213 214 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG || 215 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING || 216 Op->Asl.ParseOpcode == PARSEOP_METHODCALL) 217 { 218 DbgPrint (ASL_TREE_OUTPUT, 219 "%10.32s ", Op->Asl.ExternalName); 220 } 221 else 222 { 223 DbgPrint (ASL_TREE_OUTPUT, " "); 224 } 225 226 DbgPrint (ASL_TREE_OUTPUT, 227 "Val-%08X POp-%04X AOp-%04X OpLen-%01X PByts-%01X Len-%04X SubLen-%04X PSubLen-%04X Op-%08X Chld-%08X Paren-%08X Flags-%04X AcTyp-%08X C-%2d L-%d\n", 228 (UINT32) Op->Asl.Value.Integer, 229 Op->Asl.ParseOpcode, 230 Op->Asl.AmlOpcode, 231 Op->Asl.AmlOpcodeLength, 232 Op->Asl.AmlPkgLenBytes, 233 Op->Asl.AmlLength, 234 Op->Asl.AmlSubtreeLength, 235 Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0, 236 Op, 237 Op->Asl.Child, 238 Op->Asl.Parent, 239 Op->Asl.CompileFlags, 240 Op->Asl.AcpiBtype, 241 Op->Asl.Column, 242 Op->Asl.LineNumber); 243 244 /* Generate the AML for this node */ 245 246 CgWriteNode (Op); 247 return (AE_OK); 248 } 249 250 251 /******************************************************************************* 252 * 253 * FUNCTION: CgLocalWriteAmlData 254 * 255 * PARAMETERS: Op - Current parse op 256 * Buffer - Buffer to write 257 * Length - Size of data in buffer 258 * 259 * RETURN: None 260 * 261 * DESCRIPTION: Write a buffer of AML data to the AML output file. 262 * 263 ******************************************************************************/ 264 265 static void 266 CgLocalWriteAmlData ( 267 ACPI_PARSE_OBJECT *Op, 268 void *Buffer, 269 UINT32 Length) 270 { 271 272 /* Write the raw data to the AML file */ 273 274 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length); 275 276 /* Update the final AML length for this node (used for listings) */ 277 278 if (Op) 279 { 280 Op->Asl.FinalAmlLength += Length; 281 } 282 } 283 284 285 /******************************************************************************* 286 * 287 * FUNCTION: CgWriteAmlOpcode 288 * 289 * PARAMETERS: Op - Parse node with an AML opcode 290 * 291 * RETURN: None. 292 * 293 * DESCRIPTION: Write the AML opcode corresponding to a parse node. 294 * 295 ******************************************************************************/ 296 297 static void 298 CgWriteAmlOpcode ( 299 ACPI_PARSE_OBJECT *Op) 300 { 301 UINT8 PkgLenFirstByte; 302 UINT32 i; 303 union { 304 UINT16 Opcode; 305 UINT8 OpcodeBytes[2]; 306 } Aml; 307 union { 308 UINT32 Len; 309 UINT8 LenBytes[4]; 310 } PkgLen; 311 312 313 /* We expect some DEFAULT_ARGs, just ignore them */ 314 315 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) 316 { 317 return; 318 } 319 320 switch (Op->Asl.AmlOpcode) 321 { 322 case AML_UNASSIGNED_OPCODE: 323 324 /* These opcodes should not get here */ 325 326 printf ("Found a node with an unassigned AML opcode\n"); 327 fprintf (stderr, "Found a node with an unassigned AML opcode\n"); 328 return; 329 330 case AML_INT_RESERVEDFIELD_OP: 331 332 /* Special opcodes for within a field definition */ 333 334 Aml.Opcode = 0x00; 335 break; 336 337 case AML_INT_ACCESSFIELD_OP: 338 339 Aml.Opcode = 0x01; 340 break; 341 342 default: 343 Aml.Opcode = Op->Asl.AmlOpcode; 344 break; 345 } 346 347 348 switch (Aml.Opcode) 349 { 350 case AML_PACKAGE_LENGTH: 351 352 /* Value is the length to be encoded (Used in field definitions) */ 353 354 PkgLen.Len = (UINT32) Op->Asl.Value.Integer; 355 break; 356 357 default: 358 359 /* Check for two-byte opcode */ 360 361 if (Aml.Opcode > 0x00FF) 362 { 363 /* Write the high byte first */ 364 365 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1); 366 } 367 368 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1); 369 370 /* Subtreelength doesn't include length of package length bytes */ 371 372 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes; 373 break; 374 } 375 376 /* Does this opcode have an associated "PackageLength" field? */ 377 378 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE) 379 { 380 if (Op->Asl.AmlPkgLenBytes == 1) 381 { 382 /* Simplest case -- no bytes to follow, just write the count */ 383 384 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1); 385 } 386 else 387 { 388 /* 389 * Encode the "bytes to follow" in the first byte, top two bits. 390 * The low-order nybble of the length is in the bottom 4 bits 391 */ 392 PkgLenFirstByte = (UINT8) 393 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) | 394 (PkgLen.LenBytes[0] & 0x0F)); 395 396 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1); 397 398 /* 399 * Shift the length over by the 4 bits we just stuffed 400 * in the first byte 401 */ 402 PkgLen.Len >>= 4; 403 404 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */ 405 406 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++) 407 { 408 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1); 409 } 410 } 411 } 412 413 switch (Aml.Opcode) 414 { 415 case AML_BYTE_OP: 416 417 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1); 418 break; 419 420 case AML_WORD_OP: 421 422 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2); 423 break; 424 425 case AML_DWORD_OP: 426 427 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4); 428 break; 429 430 case AML_QWORD_OP: 431 432 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8); 433 break; 434 435 case AML_STRING_OP: 436 437 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength); 438 break; 439 440 default: 441 /* All data opcodes must appear above */ 442 break; 443 } 444 } 445 446 447 /******************************************************************************* 448 * 449 * FUNCTION: CgWriteTableHeader 450 * 451 * PARAMETERS: Op - The DEFINITIONBLOCK node 452 * 453 * RETURN: None 454 * 455 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK 456 * 457 ******************************************************************************/ 458 459 static void 460 CgWriteTableHeader ( 461 ACPI_PARSE_OBJECT *Op) 462 { 463 ACPI_PARSE_OBJECT *Child; 464 465 466 /* AML filename */ 467 468 Child = Op->Asl.Child; 469 470 /* Signature */ 471 472 Child = Child->Asl.Next; 473 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4); 474 475 /* Revision */ 476 477 Child = Child->Asl.Next; 478 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer; 479 480 /* Command-line Revision override */ 481 482 if (Gbl_RevisionOverride) 483 { 484 TableHeader.Revision = Gbl_RevisionOverride; 485 } 486 487 /* OEMID */ 488 489 Child = Child->Asl.Next; 490 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6); 491 492 /* OEM TableID */ 493 494 Child = Child->Asl.Next; 495 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8); 496 497 /* OEM Revision */ 498 499 Child = Child->Asl.Next; 500 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer; 501 502 /* Compiler ID */ 503 504 strncpy (TableHeader.AslCompilerId, CompilerCreatorId, 4); 505 506 /* Compiler version */ 507 508 TableHeader.AslCompilerRevision = CompilerCreatorRevision; 509 510 /* Table length. Checksum zero for now, will rewrite later */ 511 512 TableHeader.Length = Gbl_TableLength; 513 TableHeader.Checksum = 0; 514 515 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER)); 516 } 517 518 519 /******************************************************************************* 520 * 521 * FUNCTION: CgCloseTable 522 * 523 * PARAMETERS: None. 524 * 525 * RETURN: None. 526 * 527 * DESCRIPTION: Complete the ACPI table by calculating the checksum and 528 * re-writing the header. 529 * 530 ******************************************************************************/ 531 532 static void 533 CgCloseTable ( 534 void) 535 { 536 signed char Sum; 537 UINT8 FileByte; 538 539 540 FlSeekFile (ASL_FILE_AML_OUTPUT, 0); 541 Sum = 0; 542 543 /* Calculate the checksum over the entire file */ 544 545 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK) 546 { 547 Sum = (signed char) (Sum + FileByte); 548 } 549 550 /* Re-write the table header with the checksum */ 551 552 TableHeader.Checksum = (UINT8) (0 - Sum); 553 554 FlSeekFile (ASL_FILE_AML_OUTPUT, 0); 555 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER)); 556 } 557 558 559 /******************************************************************************* 560 * 561 * FUNCTION: CgWriteNode 562 * 563 * PARAMETERS: Op - Parse node to write. 564 * 565 * RETURN: None. 566 * 567 * DESCRIPTION: Write the AML that corresponds to a parse node. 568 * 569 ******************************************************************************/ 570 571 static void 572 CgWriteNode ( 573 ACPI_PARSE_OBJECT *Op) 574 { 575 ASL_RESOURCE_NODE *Rnode; 576 577 578 Op->Asl.FinalAmlLength = 0; 579 580 /* Always check for DEFAULT_ARG and other "Noop" nodes */ 581 /* TBD: this may not be the best place for this check */ 582 583 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) || 584 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) || 585 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) || 586 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END)) 587 { 588 return; 589 } 590 591 switch (Op->Asl.AmlOpcode) 592 { 593 case AML_RAW_DATA_BYTE: 594 case AML_RAW_DATA_WORD: 595 case AML_RAW_DATA_DWORD: 596 case AML_RAW_DATA_QWORD: 597 598 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength); 599 return; 600 601 602 case AML_RAW_DATA_BUFFER: 603 604 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength); 605 return; 606 607 608 case AML_RAW_DATA_CHAIN: 609 610 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer); 611 while (Rnode) 612 { 613 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength); 614 Rnode = Rnode->Next; 615 } 616 return; 617 618 default: 619 /* Internal data opcodes must all appear above */ 620 break; 621 } 622 623 switch (Op->Asl.ParseOpcode) 624 { 625 case PARSEOP_DEFAULT_ARG: 626 627 break; 628 629 case PARSEOP_DEFINITIONBLOCK: 630 631 CgWriteTableHeader (Op); 632 break; 633 634 case PARSEOP_NAMESEG: 635 case PARSEOP_NAMESTRING: 636 case PARSEOP_METHODCALL: 637 638 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength); 639 break; 640 641 default: 642 643 CgWriteAmlOpcode (Op); 644 break; 645 } 646 } 647 648 649