Path: blob/main/sys/contrib/dev/acpica/compiler/asllistsup.c
48373 views
/******************************************************************************1*2* Module Name: asllistsup - Listing file support utilities3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/compiler/aslcompiler.h>152#include "aslcompiler.y.h"153154155#define _COMPONENT ACPI_COMPILER156ACPI_MODULE_NAME ("aslistsup")157158159/*******************************************************************************160*161* FUNCTION: LsDumpAscii162*163* PARAMETERS: FileId - ID of current listing file164* Count - Number of bytes to convert165* Buffer - Buffer of bytes to convert166*167* RETURN: None168*169* DESCRIPTION: Convert hex bytes to ascii170*171******************************************************************************/172173void174LsDumpAscii (175UINT32 FileId,176UINT32 Count,177UINT8 *Buffer)178{179UINT8 BufChar;180UINT32 i;181182183FlPrintFile (FileId, " \"");184for (i = 0; i < Count; i++)185{186BufChar = Buffer[i];187if (isprint (BufChar))188{189FlPrintFile (FileId, "%c", BufChar);190}191else192{193/* Not a printable character, just put out a dot */194195FlPrintFile (FileId, ".");196}197}198199FlPrintFile (FileId, "\"");200}201202203/*******************************************************************************204*205* FUNCTION: LsDumpAsciiInComment206*207* PARAMETERS: FileId - ID of current listing file208* Count - Number of bytes to convert209* Buffer - Buffer of bytes to convert210*211* RETURN: None212*213* DESCRIPTION: Convert hex bytes to ascii214*215******************************************************************************/216217void218LsDumpAsciiInComment (219UINT32 FileId,220UINT32 Count,221UINT8 *Buffer)222{223UINT8 BufChar = 0;224UINT8 LastChar;225UINT32 i;226227228FlPrintFile (FileId, " \"");229for (i = 0; i < Count; i++)230{231LastChar = BufChar;232BufChar = Buffer[i];233234if (isprint (BufChar))235{236/* Handle embedded C comment sequences */237238if (((LastChar == '*') && (BufChar == '/')) ||239((LastChar == '/') && (BufChar == '*')))240{241/* Insert a space to break the sequence */242243FlPrintFile (FileId, " ");244}245246FlPrintFile (FileId, "%c", BufChar);247}248else249{250/* Not a printable character, just put out a dot */251252FlPrintFile (FileId, ".");253}254}255256FlPrintFile (FileId, "\"");257}258259260/*******************************************************************************261*262* FUNCTION: LsCheckException263*264* PARAMETERS: LineNumber - Current logical (cumulative) line #265* FileId - ID of output listing file266*267* RETURN: None268*269* DESCRIPTION: Check if there is an exception for this line, and if there is,270* put it in the listing immediately. Handles multiple errors271* per line. AslGbl_NextError points to the next error in the272* sorted (by line #) list of compile errors/warnings.273*274******************************************************************************/275276void277LsCheckException (278UINT32 LineNumber,279UINT32 FileId)280{281282if ((!AslGbl_NextError) ||283(LineNumber < AslGbl_NextError->LogicalLineNumber ))284{285return;286}287288/* Handle multiple errors per line */289290if (FileId == ASL_FILE_LISTING_OUTPUT)291{292while (AslGbl_NextError &&293(LineNumber >= AslGbl_NextError->LogicalLineNumber))294{295AePrintException (FileId, AslGbl_NextError, "\n[****iasl****]\n");296AslGbl_NextError = AslGbl_NextError->Next;297}298299FlPrintFile (FileId, "\n");300}301}302303304/*******************************************************************************305*306* FUNCTION: LsWriteListingHexBytes307*308* PARAMETERS: Buffer - AML code buffer309* Length - Number of AML bytes to write310* FileId - ID of current listing file.311*312* RETURN: None313*314* DESCRIPTION: Write the contents of the AML buffer to the listing file via315* the listing buffer. The listing buffer is flushed every 16316* AML bytes.317*318******************************************************************************/319320void321LsWriteListingHexBytes (322UINT8 *Buffer,323UINT32 Length,324UINT32 FileId)325{326UINT32 i;327328329/* Transfer all requested bytes */330331for (i = 0; i < Length; i++)332{333/* Print line header when buffer is empty */334335if (AslGbl_CurrentHexColumn == 0)336{337if (AslGbl_HasIncludeFiles)338{339FlPrintFile (FileId, "%*s", 10, " ");340}341342switch (FileId)343{344case ASL_FILE_LISTING_OUTPUT:345346FlPrintFile (FileId, "%8.8X%s", AslGbl_CurrentAmlOffset,347ASL_LISTING_LINE_PREFIX);348break;349350case ASL_FILE_ASM_SOURCE_OUTPUT:351352FlPrintFile (FileId, " db ");353break;354355case ASL_FILE_C_SOURCE_OUTPUT:356357FlPrintFile (FileId, " ");358break;359360default:361362/* No other types supported */363364return;365}366}367368/* Transfer AML byte and update counts */369370AslGbl_AmlBuffer[AslGbl_CurrentHexColumn] = Buffer[i];371372AslGbl_CurrentHexColumn++;373AslGbl_CurrentAmlOffset++;374375/* Flush buffer when it is full */376377if (AslGbl_CurrentHexColumn >= HEX_LISTING_LINE_SIZE)378{379LsFlushListingBuffer (FileId);380}381}382}383384385/*******************************************************************************386*387* FUNCTION: LsWriteSourceLines388*389* PARAMETERS: ToLineNumber -390* ToLogicalLineNumber - Write up to this source line number391* FileId - ID of current listing file392*393* RETURN: None394*395* DESCRIPTION: Read then write source lines to the listing file until we have396* reached the specified logical (cumulative) line number. This397* automatically echos out comment blocks and other non-AML398* generating text until we get to the actual AML-generating line399* of ASL code specified by the logical line number.400*401******************************************************************************/402403void404LsWriteSourceLines (405UINT32 ToLineNumber,406UINT32 ToLogicalLineNumber,407UINT32 FileId)408{409410/* Nothing to do for these file types */411412if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||413(FileId == ASL_FILE_C_INCLUDE_OUTPUT))414{415return;416}417418AslGbl_CurrentLine = ToLogicalLineNumber;419420/* Flush any hex bytes remaining from the last opcode */421422LsFlushListingBuffer (FileId);423424/* Read lines and write them as long as we are not caught up */425426if (AslGbl_SourceLine < AslGbl_CurrentLine)427{428/*429* If we just completed writing some AML hex bytes, output a linefeed430* to add some whitespace for readability.431*/432if (AslGbl_HexBytesWereWritten)433{434FlPrintFile (FileId, "\n");435AslGbl_HexBytesWereWritten = FALSE;436}437438if (FileId == ASL_FILE_C_SOURCE_OUTPUT)439{440FlPrintFile (FileId, " /*\n");441}442443/* Write one line at a time until we have reached the target line # */444445while ((AslGbl_SourceLine < AslGbl_CurrentLine) &&446LsWriteOneSourceLine (FileId))447{ ; }448449if (FileId == ASL_FILE_C_SOURCE_OUTPUT)450{451FlPrintFile (FileId, " */");452}453454FlPrintFile (FileId, "\n");455}456}457458459/*******************************************************************************460*461* FUNCTION: LsWriteOneSourceLine462*463* PARAMETERS: FileId - ID of current listing file464*465* RETURN: FALSE on EOF (input source file), TRUE otherwise466*467* DESCRIPTION: Read one line from the input source file and echo it to the468* listing file, prefixed with the line number, and if the source469* file contains include files, prefixed with the current filename470*471******************************************************************************/472473UINT32474LsWriteOneSourceLine (475UINT32 FileId)476{477UINT8 FileByte;478UINT32 Column = 0;479UINT32 Index = 16;480BOOLEAN StartOfLine = FALSE;481BOOLEAN ProcessLongLine = FALSE;482483484AslGbl_SourceLine++;485AslGbl_ListingNode->LineNumber++;486487/* Ignore lines that are completely blank (but count the line above) */488489if (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) != AE_OK)490{491return (0);492}493if (FileByte == '\n')494{495return (1);496}497498/*499* This is a non-empty line, we will print the entire line with500* the line number and possibly other prefixes and transforms.501*/502503/* Line prefixes for special files, C and ASM output */504505if (FileId == ASL_FILE_C_SOURCE_OUTPUT)506{507FlPrintFile (FileId, " *");508}509if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)510{511FlPrintFile (FileId, "; ");512}513514if (AslGbl_HasIncludeFiles)515{516/*517* This file contains "include" statements, print the current518* filename and line number within the current file519*/520FlPrintFile (FileId, "%12s %5d%s",521AslGbl_ListingNode->Filename, AslGbl_ListingNode->LineNumber,522ASL_LISTING_LINE_PREFIX);523}524else525{526/* No include files, just print the line number */527528FlPrintFile (FileId, "%8u%s", AslGbl_SourceLine,529ASL_LISTING_LINE_PREFIX);530}531532/* Read the rest of this line (up to a newline or EOF) */533534do535{536if (FileId == ASL_FILE_C_SOURCE_OUTPUT)537{538if (FileByte == '/')539{540FileByte = '*';541}542}543544/* Split long input lines for readability in the listing */545546Column++;547if (Column >= 128)548{549if (!ProcessLongLine)550{551if ((FileByte != '}') &&552(FileByte != '{'))553{554goto WriteByte;555}556557ProcessLongLine = TRUE;558}559560if (FileByte == '{')561{562FlPrintFile (FileId, "\n%*s{\n", Index, " ");563StartOfLine = TRUE;564Index += 4;565continue;566}567568else if (FileByte == '}')569{570if (!StartOfLine)571{572FlPrintFile (FileId, "\n");573}574575StartOfLine = TRUE;576Index -= 4;577FlPrintFile (FileId, "%*s}\n", Index, " ");578continue;579}580581/* Ignore spaces/tabs at the start of line */582583else if ((FileByte == ' ') && StartOfLine)584{585continue;586}587588else if (StartOfLine)589{590StartOfLine = FALSE;591FlPrintFile (FileId, "%*s", Index, " ");592}593594WriteByte:595FlWriteFile (FileId, &FileByte, 1);596if (FileByte == '\n')597{598/*599* This line has been completed.600* Check if an error occurred on this source line during the compile.601* If so, we print the error message after the source line.602*/603LsCheckException (AslGbl_SourceLine, FileId);604return (1);605}606}607else608{609FlWriteFile (FileId, &FileByte, 1);610if (FileByte == '\n')611{612/*613* This line has been completed.614* Check if an error occurred on this source line during the compile.615* If so, we print the error message after the source line.616*/617LsCheckException (AslGbl_SourceLine, FileId);618return (1);619}620}621622} while (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) == AE_OK);623624/* EOF on the input file was reached */625626return (0);627}628629630/*******************************************************************************631*632* FUNCTION: LsFlushListingBuffer633*634* PARAMETERS: FileId - ID of the listing file635*636* RETURN: None637*638* DESCRIPTION: Flush out the current contents of the 16-byte hex AML code639* buffer. Usually called at the termination of a single line640* of source code or when the buffer is full.641*642******************************************************************************/643644void645LsFlushListingBuffer (646UINT32 FileId)647{648UINT32 i;649650651if (AslGbl_CurrentHexColumn == 0)652{653return;654}655656/* Write the hex bytes */657658switch (FileId)659{660case ASL_FILE_LISTING_OUTPUT:661662for (i = 0; i < AslGbl_CurrentHexColumn; i++)663{664FlPrintFile (FileId, "%2.2X ", AslGbl_AmlBuffer[i]);665}666667for (i = 0; i < ((HEX_LISTING_LINE_SIZE - AslGbl_CurrentHexColumn) * 3); i++)668{669FlWriteFile (FileId, ".", 1);670}671672/* Write the ASCII character associated with each of the bytes */673674LsDumpAscii (FileId, AslGbl_CurrentHexColumn, AslGbl_AmlBuffer);675break;676677678case ASL_FILE_ASM_SOURCE_OUTPUT:679680for (i = 0; i < AslGbl_CurrentHexColumn; i++)681{682if (i > 0)683{684FlPrintFile (FileId, ",");685}686687FlPrintFile (FileId, "0%2.2Xh", AslGbl_AmlBuffer[i]);688}689690for (i = 0; i < ((HEX_LISTING_LINE_SIZE - AslGbl_CurrentHexColumn) * 5); i++)691{692FlWriteFile (FileId, " ", 1);693}694695FlPrintFile (FileId, " ;%8.8X",696AslGbl_CurrentAmlOffset - HEX_LISTING_LINE_SIZE);697698/* Write the ASCII character associated with each of the bytes */699700LsDumpAscii (FileId, AslGbl_CurrentHexColumn, AslGbl_AmlBuffer);701break;702703704case ASL_FILE_C_SOURCE_OUTPUT:705706for (i = 0; i < AslGbl_CurrentHexColumn; i++)707{708FlPrintFile (FileId, "0x%2.2X,", AslGbl_AmlBuffer[i]);709}710711/* Pad hex output with spaces if line is shorter than max line size */712713for (i = 0; i < ((HEX_LISTING_LINE_SIZE - AslGbl_CurrentHexColumn) * 5); i++)714{715FlWriteFile (FileId, " ", 1);716}717718/* AML offset for the start of the line */719720FlPrintFile (FileId, " /* %8.8X",721AslGbl_CurrentAmlOffset - AslGbl_CurrentHexColumn);722723/* Write the ASCII character associated with each of the bytes */724725LsDumpAsciiInComment (FileId, AslGbl_CurrentHexColumn, AslGbl_AmlBuffer);726FlPrintFile (FileId, " */");727break;728729default:730731/* No other types supported */732733return;734}735736FlPrintFile (FileId, "\n");737738AslGbl_CurrentHexColumn = 0;739AslGbl_HexBytesWereWritten = TRUE;740}741742743/*******************************************************************************744*745* FUNCTION: LsPushNode746*747* PARAMETERS: Filename - Pointer to the include filename748*749* RETURN: None750*751* DESCRIPTION: Push a listing node on the listing/include file stack. This752* stack enables tracking of include files (infinitely nested)753* and resumption of the listing of the parent file when the754* include file is finished.755*756******************************************************************************/757758void759LsPushNode (760char *Filename)761{762ASL_LISTING_NODE *Lnode;763764765/* Create a new node */766767Lnode = UtLocalCalloc (sizeof (ASL_LISTING_NODE));768769/* Initialize */770771Lnode->Filename = Filename;772Lnode->LineNumber = 0;773774/* Link (push) */775776Lnode->Next = AslGbl_ListingNode;777AslGbl_ListingNode = Lnode;778}779780781/*******************************************************************************782*783* FUNCTION: LsPopNode784*785* PARAMETERS: None786*787* RETURN: List head after current head is popped off788*789* DESCRIPTION: Pop the current head of the list, free it, and return the790* next node on the stack (the new current node).791*792******************************************************************************/793794ASL_LISTING_NODE *795LsPopNode (796void)797{798ASL_LISTING_NODE *Lnode;799800801/* Just grab the node at the head of the list */802803Lnode = AslGbl_ListingNode;804if ((!Lnode) ||805(!Lnode->Next))806{807AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,808"Could not pop empty listing stack");809return (AslGbl_ListingNode);810}811812AslGbl_ListingNode = Lnode->Next;813ACPI_FREE (Lnode);814815/* New "Current" node is the new head */816817return (AslGbl_ListingNode);818}819820821