11a3716ccSchristos /******************************************************************************
21a3716ccSchristos *
31a3716ccSchristos * Module Name: cvcompiler - ASL-/ASL+ converter functions
41a3716ccSchristos *
51a3716ccSchristos *****************************************************************************/
61a3716ccSchristos
71a3716ccSchristos /*
8*e5631441Schristos * Copyright (C) 2000 - 2022, Intel Corp.
91a3716ccSchristos * All rights reserved.
101a3716ccSchristos *
111a3716ccSchristos * Redistribution and use in source and binary forms, with or without
121a3716ccSchristos * modification, are permitted provided that the following conditions
131a3716ccSchristos * are met:
141a3716ccSchristos * 1. Redistributions of source code must retain the above copyright
151a3716ccSchristos * notice, this list of conditions, and the following disclaimer,
161a3716ccSchristos * without modification.
171a3716ccSchristos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
181a3716ccSchristos * substantially similar to the "NO WARRANTY" disclaimer below
191a3716ccSchristos * ("Disclaimer") and any redistribution must be conditioned upon
201a3716ccSchristos * including a substantially similar Disclaimer requirement for further
211a3716ccSchristos * binary redistribution.
221a3716ccSchristos * 3. Neither the names of the above-listed copyright holders nor the names
231a3716ccSchristos * of any contributors may be used to endorse or promote products derived
241a3716ccSchristos * from this software without specific prior written permission.
251a3716ccSchristos *
261a3716ccSchristos * Alternatively, this software may be distributed under the terms of the
271a3716ccSchristos * GNU General Public License ("GPL") version 2 as published by the Free
281a3716ccSchristos * Software Foundation.
291a3716ccSchristos *
301a3716ccSchristos * NO WARRANTY
311a3716ccSchristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
321a3716ccSchristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33d4291bf2Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
341a3716ccSchristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
351a3716ccSchristos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
361a3716ccSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
371a3716ccSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
381a3716ccSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
391a3716ccSchristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
401a3716ccSchristos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
411a3716ccSchristos * POSSIBILITY OF SUCH DAMAGES.
421a3716ccSchristos */
431a3716ccSchristos
441a3716ccSchristos #include "aslcompiler.h"
451a3716ccSchristos #include "acparser.h"
461a3716ccSchristos #include "amlcode.h"
471a3716ccSchristos #include "acdebug.h"
481a3716ccSchristos #include "acconvert.h"
491a3716ccSchristos
501a3716ccSchristos
51d382fe7fSchristos /* Local prototypes */
52d382fe7fSchristos
531a3716ccSchristos static void
541a3716ccSchristos CvPrintInclude(
551a3716ccSchristos ACPI_FILE_NODE *FNode,
561a3716ccSchristos UINT32 Level);
571a3716ccSchristos
581a3716ccSchristos static BOOLEAN
591a3716ccSchristos CvListIsSingleton (
601a3716ccSchristos ACPI_COMMENT_NODE *CommentList);
611a3716ccSchristos
621a3716ccSchristos
631a3716ccSchristos /*******************************************************************************
641a3716ccSchristos *
651a3716ccSchristos * FUNCTION: CvPrintOneCommentList
661a3716ccSchristos *
671a3716ccSchristos * PARAMETERS: CommentList
681a3716ccSchristos * Level
691a3716ccSchristos *
701a3716ccSchristos * RETURN: None
711a3716ccSchristos *
721a3716ccSchristos * DESCRIPTION: Prints all comments within the given list.
731a3716ccSchristos * This is referred as ASL_CV_PRINT_ONE_COMMENT_LIST.
741a3716ccSchristos *
751a3716ccSchristos ******************************************************************************/
761a3716ccSchristos
771a3716ccSchristos void
CvPrintOneCommentList(ACPI_COMMENT_NODE * CommentList,UINT32 Level)781a3716ccSchristos CvPrintOneCommentList (
791a3716ccSchristos ACPI_COMMENT_NODE *CommentList,
801a3716ccSchristos UINT32 Level)
811a3716ccSchristos {
821a3716ccSchristos ACPI_COMMENT_NODE *Current = CommentList;
831a3716ccSchristos ACPI_COMMENT_NODE *Previous;
841a3716ccSchristos
851a3716ccSchristos
861a3716ccSchristos while (Current)
871a3716ccSchristos {
881a3716ccSchristos Previous = Current;
891a3716ccSchristos if (Current->Comment)
901a3716ccSchristos {
911a3716ccSchristos AcpiDmIndent(Level);
921a3716ccSchristos AcpiOsPrintf("%s\n", Current->Comment);
931a3716ccSchristos Current->Comment = NULL;
941a3716ccSchristos }
95d382fe7fSchristos
961a3716ccSchristos Current = Current->Next;
971a3716ccSchristos AcpiOsReleaseObject(AcpiGbl_RegCommentCache, Previous);
981a3716ccSchristos }
991a3716ccSchristos }
1001a3716ccSchristos
1011a3716ccSchristos
1021a3716ccSchristos /*******************************************************************************
1031a3716ccSchristos *
1041a3716ccSchristos * FUNCTION: CvListIsSingleton
1051a3716ccSchristos *
106d382fe7fSchristos * PARAMETERS: CommentList - check to see if this is a single item list.
1071a3716ccSchristos *
1081a3716ccSchristos * RETURN: BOOLEAN
1091a3716ccSchristos *
1101a3716ccSchristos * DESCRIPTION: Returns TRUE if CommentList only contains 1 node.
1111a3716ccSchristos *
1121a3716ccSchristos ******************************************************************************/
1131a3716ccSchristos
1141a3716ccSchristos static BOOLEAN
CvListIsSingleton(ACPI_COMMENT_NODE * CommentList)1151a3716ccSchristos CvListIsSingleton (
1161a3716ccSchristos ACPI_COMMENT_NODE *CommentList)
1171a3716ccSchristos
1181a3716ccSchristos {
119d382fe7fSchristos
1201a3716ccSchristos if (!CommentList)
1211a3716ccSchristos {
122d382fe7fSchristos return (FALSE);
1231a3716ccSchristos }
1241a3716ccSchristos else if (CommentList->Next)
1251a3716ccSchristos {
126d382fe7fSchristos return (FALSE);
1271a3716ccSchristos }
1281a3716ccSchristos
129d382fe7fSchristos return (TRUE);
1301a3716ccSchristos }
1311a3716ccSchristos
1321a3716ccSchristos
1331a3716ccSchristos /*******************************************************************************
1341a3716ccSchristos *
1351a3716ccSchristos * FUNCTION: CvPrintOneCommentType
1361a3716ccSchristos *
1371a3716ccSchristos * PARAMETERS: Op
1381a3716ccSchristos * CommentType
1391a3716ccSchristos * EndStr - String to print after printing the comment
1401a3716ccSchristos * Level - indentation level for comment lists.
1411a3716ccSchristos *
1421a3716ccSchristos * RETURN: None
1431a3716ccSchristos *
1441a3716ccSchristos * DESCRIPTION: Prints all comments of CommentType within the given Op and
1451a3716ccSchristos * clears the printed comment from the Op.
1461a3716ccSchristos * This is referred as ASL_CV_PRINT_ONE_COMMENT.
1471a3716ccSchristos *
1481a3716ccSchristos ******************************************************************************/
1491a3716ccSchristos
1501a3716ccSchristos void
CvPrintOneCommentType(ACPI_PARSE_OBJECT * Op,UINT8 CommentType,char * EndStr,UINT32 Level)1511a3716ccSchristos CvPrintOneCommentType (
1521a3716ccSchristos ACPI_PARSE_OBJECT *Op,
1531a3716ccSchristos UINT8 CommentType,
1541a3716ccSchristos char* EndStr,
1551a3716ccSchristos UINT32 Level)
1561a3716ccSchristos {
1571a3716ccSchristos BOOLEAN CommentExists = FALSE;
1581a3716ccSchristos char **CommentToPrint = NULL;
1591a3716ccSchristos
1601a3716ccSchristos
1611a3716ccSchristos switch (CommentType)
1621a3716ccSchristos {
1631a3716ccSchristos case AML_COMMENT_STANDARD:
1641a3716ccSchristos
1651a3716ccSchristos if (CvListIsSingleton (Op->Common.CommentList))
1661a3716ccSchristos {
1671a3716ccSchristos CvPrintOneCommentList (Op->Common.CommentList, Level);
1681a3716ccSchristos AcpiOsPrintf ("\n");
1691a3716ccSchristos }
1701a3716ccSchristos else
1711a3716ccSchristos {
1721a3716ccSchristos CvPrintOneCommentList (Op->Common.CommentList, Level);
1731a3716ccSchristos }
174d382fe7fSchristos
1751a3716ccSchristos Op->Common.CommentList = NULL;
1761a3716ccSchristos return;
1771a3716ccSchristos
1781a3716ccSchristos case AML_COMMENT_ENDBLK:
1791a3716ccSchristos
1801a3716ccSchristos if (Op->Common.EndBlkComment)
1811a3716ccSchristos {
1821a3716ccSchristos CvPrintOneCommentList (Op->Common.EndBlkComment, Level);
1831a3716ccSchristos Op->Common.EndBlkComment = NULL;
1841a3716ccSchristos AcpiDmIndent(Level);
1851a3716ccSchristos }
1861a3716ccSchristos return;
1871a3716ccSchristos
1881a3716ccSchristos case AMLCOMMENT_INLINE:
1891a3716ccSchristos
1901a3716ccSchristos CommentToPrint = &Op->Common.InlineComment;
1911a3716ccSchristos break;
1921a3716ccSchristos
1931a3716ccSchristos case AML_COMMENT_END_NODE:
1941a3716ccSchristos
1951a3716ccSchristos CommentToPrint = &Op->Common.EndNodeComment;
1961a3716ccSchristos break;
1971a3716ccSchristos
1981a3716ccSchristos case AML_NAMECOMMENT:
1991a3716ccSchristos
2001a3716ccSchristos CommentToPrint = &Op->Common.NameComment;
2011a3716ccSchristos break;
2021a3716ccSchristos
2031a3716ccSchristos case AML_COMMENT_CLOSE_BRACE:
2041a3716ccSchristos
2051a3716ccSchristos CommentToPrint = &Op->Common.CloseBraceComment;
2061a3716ccSchristos break;
2071a3716ccSchristos
2081a3716ccSchristos default:
2091a3716ccSchristos return;
2101a3716ccSchristos }
2111a3716ccSchristos
2121a3716ccSchristos if (*CommentToPrint)
2131a3716ccSchristos {
214d382fe7fSchristos CommentExists = TRUE;
2151a3716ccSchristos AcpiOsPrintf ("%s", *CommentToPrint);
2161a3716ccSchristos *CommentToPrint = NULL;
2171a3716ccSchristos }
2181a3716ccSchristos
2191a3716ccSchristos if (CommentExists && EndStr)
2201a3716ccSchristos {
2211a3716ccSchristos AcpiOsPrintf ("%s", EndStr);
2221a3716ccSchristos }
2231a3716ccSchristos }
2241a3716ccSchristos
2251a3716ccSchristos
2261a3716ccSchristos /*******************************************************************************
2271a3716ccSchristos *
2281a3716ccSchristos * FUNCTION: CvCloseBraceWriteComment
2291a3716ccSchristos *
2301a3716ccSchristos * PARAMETERS: Op
2311a3716ccSchristos * Level
2321a3716ccSchristos *
233d382fe7fSchristos * RETURN: None
2341a3716ccSchristos *
2351a3716ccSchristos * DESCRIPTION: Print a close brace } and any open brace comments associated
2361a3716ccSchristos * with this parse object.
2371a3716ccSchristos * This is referred as ASL_CV_CLOSE_BRACE.
2381a3716ccSchristos *
2391a3716ccSchristos ******************************************************************************/
2401a3716ccSchristos
2411a3716ccSchristos void
CvCloseBraceWriteComment(ACPI_PARSE_OBJECT * Op,UINT32 Level)2421a3716ccSchristos CvCloseBraceWriteComment(
2431a3716ccSchristos ACPI_PARSE_OBJECT *Op,
2441a3716ccSchristos UINT32 Level)
2451a3716ccSchristos {
246d382fe7fSchristos
247e32744fcSchristos if (!AcpiGbl_CaptureComments)
2481a3716ccSchristos {
2491a3716ccSchristos AcpiOsPrintf ("}");
2501a3716ccSchristos return;
2511a3716ccSchristos }
2521a3716ccSchristos
2531a3716ccSchristos CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
2541a3716ccSchristos AcpiOsPrintf ("}");
2551a3716ccSchristos CvPrintOneCommentType (Op, AML_COMMENT_CLOSE_BRACE, NULL, Level);
2561a3716ccSchristos }
2571a3716ccSchristos
2581a3716ccSchristos
2591a3716ccSchristos /*******************************************************************************
2601a3716ccSchristos *
2611a3716ccSchristos * FUNCTION: CvCloseParenWriteComment
2621a3716ccSchristos *
2631a3716ccSchristos * PARAMETERS: Op
2641a3716ccSchristos * Level
2651a3716ccSchristos *
266d382fe7fSchristos * RETURN: None
2671a3716ccSchristos *
2681a3716ccSchristos * DESCRIPTION: Print a closing paren ) and any end node comments associated
2691a3716ccSchristos * with this parse object.
2701a3716ccSchristos * This is referred as ASL_CV_CLOSE_PAREN.
2711a3716ccSchristos *
2721a3716ccSchristos ******************************************************************************/
2731a3716ccSchristos
2741a3716ccSchristos void
CvCloseParenWriteComment(ACPI_PARSE_OBJECT * Op,UINT32 Level)2751a3716ccSchristos CvCloseParenWriteComment(
2761a3716ccSchristos ACPI_PARSE_OBJECT *Op,
2771a3716ccSchristos UINT32 Level)
2781a3716ccSchristos {
279d382fe7fSchristos
280e32744fcSchristos if (!AcpiGbl_CaptureComments)
2811a3716ccSchristos {
2821a3716ccSchristos AcpiOsPrintf (")");
2831a3716ccSchristos return;
2841a3716ccSchristos }
2851a3716ccSchristos
2861a3716ccSchristos /*
2871a3716ccSchristos * If this op has a BLOCK_BRACE, then output the comment when the
2881a3716ccSchristos * disassembler calls CvCloseBraceWriteComment
2891a3716ccSchristos */
2901a3716ccSchristos if (AcpiDmBlockType (Op) == BLOCK_PAREN)
2911a3716ccSchristos {
2921a3716ccSchristos CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
2931a3716ccSchristos }
2941a3716ccSchristos
2951a3716ccSchristos AcpiOsPrintf (")");
2961a3716ccSchristos
2971a3716ccSchristos if (Op->Common.EndNodeComment)
2981a3716ccSchristos {
2991a3716ccSchristos CvPrintOneCommentType (Op, AML_COMMENT_END_NODE, NULL, Level);
3001a3716ccSchristos }
3011a3716ccSchristos else if ((Op->Common.Parent->Common.AmlOpcode == AML_IF_OP) &&
3021a3716ccSchristos Op->Common.Parent->Common.EndNodeComment)
3031a3716ccSchristos {
3041a3716ccSchristos CvPrintOneCommentType (Op->Common.Parent,
3051a3716ccSchristos AML_COMMENT_END_NODE, NULL, Level);
3061a3716ccSchristos }
3071a3716ccSchristos }
3081a3716ccSchristos
3091a3716ccSchristos
3101a3716ccSchristos /*******************************************************************************
3111a3716ccSchristos *
3121a3716ccSchristos * FUNCTION: CvFileHasSwitched
3131a3716ccSchristos *
3141a3716ccSchristos * PARAMETERS: Op
3151a3716ccSchristos *
3161a3716ccSchristos * RETURN: BOOLEAN
3171a3716ccSchristos *
3181a3716ccSchristos * DESCRIPTION: Determine whether if a file has switched.
3191a3716ccSchristos * TRUE - file has switched.
3201a3716ccSchristos * FALSE - file has not switched.
3211a3716ccSchristos * This is referred as ASL_CV_FILE_HAS_SWITCHED.
3221a3716ccSchristos *
3231a3716ccSchristos ******************************************************************************/
3241a3716ccSchristos
3251a3716ccSchristos BOOLEAN
CvFileHasSwitched(ACPI_PARSE_OBJECT * Op)3261a3716ccSchristos CvFileHasSwitched(
3271a3716ccSchristos ACPI_PARSE_OBJECT *Op)
3281a3716ccSchristos {
329d382fe7fSchristos
3301a3716ccSchristos if (Op->Common.CvFilename &&
3311a3716ccSchristos AcpiGbl_CurrentFilename &&
3321a3716ccSchristos AcpiUtStricmp(Op->Common.CvFilename, AcpiGbl_CurrentFilename))
3331a3716ccSchristos {
334d382fe7fSchristos return (TRUE);
3351a3716ccSchristos }
336d382fe7fSchristos
337d382fe7fSchristos return (FALSE);
3381a3716ccSchristos }
3391a3716ccSchristos
3401a3716ccSchristos
3411a3716ccSchristos /*******************************************************************************
3421a3716ccSchristos *
3431a3716ccSchristos * FUNCTION: CvPrintInclude
3441a3716ccSchristos *
3451a3716ccSchristos * PARAMETERS: FNode - Write an Include statement for the file that is pointed
3461a3716ccSchristos * by FNode->File.
3471a3716ccSchristos * Level - indentation level
3481a3716ccSchristos *
3491a3716ccSchristos * RETURN: None
3501a3716ccSchristos *
3511a3716ccSchristos * DESCRIPTION: Write the ASL Include statement for FNode->File in the file
3521a3716ccSchristos * indicated by FNode->Parent->File. Note this function emits
3531a3716ccSchristos * actual ASL code rather than comments. This switches the output
3541a3716ccSchristos * file to FNode->Parent->File.
3551a3716ccSchristos *
3561a3716ccSchristos ******************************************************************************/
3571a3716ccSchristos
3581a3716ccSchristos static void
CvPrintInclude(ACPI_FILE_NODE * FNode,UINT32 Level)3591a3716ccSchristos CvPrintInclude(
3601a3716ccSchristos ACPI_FILE_NODE *FNode,
3611a3716ccSchristos UINT32 Level)
3621a3716ccSchristos {
363d382fe7fSchristos
3641a3716ccSchristos if (!FNode || FNode->IncludeWritten)
3651a3716ccSchristos {
3661a3716ccSchristos return;
3671a3716ccSchristos }
3681a3716ccSchristos
369d382fe7fSchristos CvDbgPrint ("Writing include for %s within %s\n",
370d382fe7fSchristos FNode->Filename, FNode->Parent->Filename);
3711a3716ccSchristos AcpiOsRedirectOutput (FNode->Parent->File);
3721a3716ccSchristos CvPrintOneCommentList (FNode->IncludeComment, Level);
373d382fe7fSchristos
3741a3716ccSchristos AcpiDmIndent (Level);
3751a3716ccSchristos AcpiOsPrintf ("Include (\"%s\")\n", FNode->Filename);
376d382fe7fSchristos CvDbgPrint ("emitted the following: Include (\"%s\")\n",
377d382fe7fSchristos FNode->Filename);
3781a3716ccSchristos FNode->IncludeWritten = TRUE;
3791a3716ccSchristos }
3801a3716ccSchristos
3811a3716ccSchristos
3821a3716ccSchristos /*******************************************************************************
3831a3716ccSchristos *
3841a3716ccSchristos * FUNCTION: CvSwitchFiles
3851a3716ccSchristos *
3861a3716ccSchristos * PARAMETERS: Level - indentation level
3871a3716ccSchristos * Op
3881a3716ccSchristos *
3891a3716ccSchristos * RETURN: None
3901a3716ccSchristos *
3911a3716ccSchristos * DESCRIPTION: Switch the outputfile and write ASL Include statement. Note,
3921a3716ccSchristos * this function emits actual ASL code rather than comments.
3931a3716ccSchristos * This is referred as ASL_CV_SWITCH_FILES.
3941a3716ccSchristos *
3951a3716ccSchristos ******************************************************************************/
3961a3716ccSchristos
3971a3716ccSchristos void
CvSwitchFiles(UINT32 Level,ACPI_PARSE_OBJECT * Op)3981a3716ccSchristos CvSwitchFiles(
3991a3716ccSchristos UINT32 Level,
4001a3716ccSchristos ACPI_PARSE_OBJECT *Op)
4011a3716ccSchristos {
4021a3716ccSchristos char *Filename = Op->Common.CvFilename;
4031a3716ccSchristos ACPI_FILE_NODE *FNode;
404d382fe7fSchristos ACPI_FILE_NODE *Current;
4051a3716ccSchristos
406d382fe7fSchristos
407d382fe7fSchristos CvDbgPrint ("Switching from %s to %s\n", AcpiGbl_CurrentFilename,
408d382fe7fSchristos Filename);
4091a3716ccSchristos FNode = CvFilenameExists (Filename, AcpiGbl_FileTreeRoot);
4101a3716ccSchristos if (!FNode)
4111a3716ccSchristos {
4121a3716ccSchristos /*
4131a3716ccSchristos * At this point, each Filename should exist in AcpiGbl_FileTreeRoot
4141a3716ccSchristos * if it does not exist, then abort.
4151a3716ccSchristos */
4161a3716ccSchristos FlDeleteFile (ASL_FILE_AML_OUTPUT);
4174e4e259fSchristos sprintf (AslGbl_MsgBuffer, "\"Cannot find %s\" - %s",
418d382fe7fSchristos Filename, strerror (errno));
419d382fe7fSchristos AslCommonError (ASL_ERROR, ASL_MSG_OPEN, 0, 0, 0, 0,
4204e4e259fSchristos NULL, AslGbl_MsgBuffer);
4211a3716ccSchristos AslAbort ();
4221a3716ccSchristos }
4231a3716ccSchristos
424d382fe7fSchristos Current = FNode;
425d382fe7fSchristos
4261a3716ccSchristos /*
4271a3716ccSchristos * If the previous file is a descendent of the current file,
4281a3716ccSchristos * make sure that Include statements from the current file
4291a3716ccSchristos * to the previous have been emitted.
4301a3716ccSchristos */
431d382fe7fSchristos while (Current &&
432d382fe7fSchristos Current->Parent &&
433d382fe7fSchristos AcpiUtStricmp (Current->Filename, AcpiGbl_CurrentFilename))
4341a3716ccSchristos {
435d382fe7fSchristos CvPrintInclude (Current, Level);
436d382fe7fSchristos Current = Current->Parent;
4371a3716ccSchristos }
4381a3716ccSchristos
439553634feSchristos if (FNode)
440553634feSchristos {
441d382fe7fSchristos /* Redirect output to Op->Common.CvFilename */
4421a3716ccSchristos
4431a3716ccSchristos AcpiOsRedirectOutput (FNode->File);
4441a3716ccSchristos AcpiGbl_CurrentFilename = FNode->Filename;
4451a3716ccSchristos }
446553634feSchristos }
447