Home | History | Annotate | Line # | Download | only in compiler
aslprintf.c revision 1.1.1.10.4.1
      1           1.1  christos /******************************************************************************
      2           1.1  christos  *
      3           1.1  christos  * Module Name: aslprintf - ASL Printf/Fprintf macro support
      4           1.1  christos  *
      5           1.1  christos  *****************************************************************************/
      6           1.1  christos 
      7           1.1  christos /*
      8  1.1.1.10.4.1   thorpej  * Copyright (C) 2000 - 2021, Intel Corp.
      9           1.1  christos  * All rights reserved.
     10           1.1  christos  *
     11           1.1  christos  * Redistribution and use in source and binary forms, with or without
     12           1.1  christos  * modification, are permitted provided that the following conditions
     13           1.1  christos  * are met:
     14           1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15           1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16           1.1  christos  *    without modification.
     17           1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18           1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19           1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20           1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21           1.1  christos  *    binary redistribution.
     22           1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23           1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24           1.1  christos  *    from this software without specific prior written permission.
     25           1.1  christos  *
     26           1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27           1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28           1.1  christos  * Software Foundation.
     29           1.1  christos  *
     30           1.1  christos  * NO WARRANTY
     31           1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32           1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.10.4.1   thorpej  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34           1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35           1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36           1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37           1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38           1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39           1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40           1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41           1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42           1.1  christos  */
     43           1.1  christos 
     44           1.1  christos #include "aslcompiler.h"
     45           1.1  christos #include "aslcompiler.y.h"
     46           1.1  christos #include "amlcode.h"
     47           1.1  christos 
     48           1.1  christos #define _COMPONENT          ACPI_COMPILER
     49           1.1  christos         ACPI_MODULE_NAME    ("aslprintf")
     50           1.1  christos 
     51           1.1  christos 
     52           1.1  christos /* Local prototypes */
     53           1.1  christos 
     54           1.1  christos static void
     55           1.1  christos OpcCreateConcatenateNode (
     56           1.1  christos     ACPI_PARSE_OBJECT       *Op,
     57           1.1  christos     ACPI_PARSE_OBJECT       *Node);
     58           1.1  christos 
     59           1.1  christos static void
     60           1.1  christos OpcParsePrintf (
     61           1.1  christos     ACPI_PARSE_OBJECT       *Op,
     62           1.1  christos     ACPI_PARSE_OBJECT       *DestOp);
     63           1.1  christos 
     64           1.1  christos 
     65           1.1  christos /*******************************************************************************
     66           1.1  christos  *
     67           1.1  christos  * FUNCTION:    OpcDoPrintf
     68           1.1  christos  *
     69           1.1  christos  * PARAMETERS:  Op                  - printf parse node
     70           1.1  christos  *
     71           1.1  christos  * RETURN:      None
     72           1.1  christos  *
     73           1.1  christos  * DESCRIPTION: Convert printf macro to a Store(..., Debug) AML operation.
     74           1.1  christos  *
     75           1.1  christos  ******************************************************************************/
     76           1.1  christos 
     77           1.1  christos void
     78           1.1  christos OpcDoPrintf (
     79           1.1  christos     ACPI_PARSE_OBJECT       *Op)
     80           1.1  christos {
     81           1.1  christos     ACPI_PARSE_OBJECT       *DestOp;
     82           1.1  christos 
     83           1.1  christos 
     84           1.1  christos     /* Store destination is the Debug op */
     85           1.1  christos 
     86       1.1.1.6  christos     DestOp = TrAllocateOp (PARSEOP_DEBUG);
     87           1.1  christos     DestOp->Asl.AmlOpcode = AML_DEBUG_OP;
     88           1.1  christos     DestOp->Asl.Parent = Op;
     89           1.1  christos     DestOp->Asl.LogicalLineNumber = Op->Asl.LogicalLineNumber;
     90           1.1  christos 
     91           1.1  christos     OpcParsePrintf (Op, DestOp);
     92           1.1  christos }
     93           1.1  christos 
     94           1.1  christos 
     95           1.1  christos /*******************************************************************************
     96           1.1  christos  *
     97           1.1  christos  * FUNCTION:    OpcDoFprintf
     98           1.1  christos  *
     99           1.1  christos  * PARAMETERS:  Op                  - fprintf parse node
    100           1.1  christos  *
    101           1.1  christos  * RETURN:      None
    102           1.1  christos  *
    103           1.1  christos  * DESCRIPTION: Convert fprintf macro to a Store AML operation.
    104           1.1  christos  *
    105           1.1  christos  ******************************************************************************/
    106           1.1  christos 
    107           1.1  christos void
    108           1.1  christos OpcDoFprintf (
    109           1.1  christos     ACPI_PARSE_OBJECT       *Op)
    110           1.1  christos {
    111           1.1  christos     ACPI_PARSE_OBJECT       *DestOp;
    112           1.1  christos 
    113           1.1  christos 
    114           1.1  christos     /* Store destination is the first argument of fprintf */
    115           1.1  christos 
    116           1.1  christos     DestOp = Op->Asl.Child;
    117           1.1  christos     Op->Asl.Child = DestOp->Asl.Next;
    118           1.1  christos     DestOp->Asl.Next = NULL;
    119           1.1  christos 
    120           1.1  christos     OpcParsePrintf (Op, DestOp);
    121           1.1  christos }
    122           1.1  christos 
    123           1.1  christos 
    124           1.1  christos /*******************************************************************************
    125           1.1  christos  *
    126           1.1  christos  * FUNCTION:    OpcParsePrintf
    127           1.1  christos  *
    128           1.1  christos  * PARAMETERS:  Op                  - Printf parse node
    129           1.1  christos  *              DestOp              - Destination of Store operation
    130           1.1  christos  *
    131           1.1  christos  * RETURN:      None
    132           1.1  christos  *
    133           1.1  christos  * DESCRIPTION: Convert printf macro to a Store AML operation. The printf
    134       1.1.1.9  christos  *              macro parse tree is laid out as follows:
    135           1.1  christos  *
    136           1.1  christos  *              Op        - printf parse op
    137           1.1  christos  *              Op->Child - Format string
    138           1.1  christos  *              Op->Next  - Format string arguments
    139           1.1  christos  *
    140           1.1  christos  ******************************************************************************/
    141           1.1  christos 
    142           1.1  christos static void
    143           1.1  christos OpcParsePrintf (
    144           1.1  christos     ACPI_PARSE_OBJECT       *Op,
    145           1.1  christos     ACPI_PARSE_OBJECT       *DestOp)
    146           1.1  christos {
    147           1.1  christos     char                    *Format;
    148           1.1  christos     char                    *StartPosition = NULL;
    149           1.1  christos     ACPI_PARSE_OBJECT       *ArgNode;
    150           1.1  christos     ACPI_PARSE_OBJECT       *NextNode;
    151           1.1  christos     UINT32                  StringLength = 0;
    152           1.1  christos     char                    *NewString;
    153           1.1  christos     BOOLEAN                 StringToProcess = FALSE;
    154           1.1  christos     ACPI_PARSE_OBJECT       *NewOp;
    155           1.1  christos 
    156           1.1  christos 
    157           1.1  christos     /* Get format string */
    158           1.1  christos 
    159           1.1  christos     Format = ACPI_CAST_PTR (char, Op->Asl.Child->Asl.Value.String);
    160           1.1  christos     ArgNode = Op->Asl.Child->Asl.Next;
    161           1.1  christos 
    162           1.1  christos     /*
    163           1.1  christos      * Detach argument list so that we can use a NULL check to distinguish
    164           1.1  christos      * the first concatenation operation we need to make
    165           1.1  christos      */
    166           1.1  christos     Op->Asl.Child = NULL;
    167           1.1  christos 
    168           1.1  christos     for (; *Format; ++Format)
    169           1.1  christos     {
    170           1.1  christos         if (*Format != '%')
    171           1.1  christos         {
    172           1.1  christos             if (!StringToProcess)
    173           1.1  christos             {
    174           1.1  christos                 /* Mark the beginning of a string */
    175           1.1  christos 
    176           1.1  christos                 StartPosition = Format;
    177           1.1  christos                 StringToProcess = TRUE;
    178           1.1  christos             }
    179           1.1  christos 
    180           1.1  christos             ++StringLength;
    181           1.1  christos             continue;
    182           1.1  christos         }
    183           1.1  christos 
    184           1.1  christos         /* Save string, if any, to new string object and concat it */
    185           1.1  christos 
    186           1.1  christos         if (StringToProcess)
    187           1.1  christos         {
    188       1.1.1.7  christos             NewString = UtLocalCacheCalloc (StringLength + 1);
    189       1.1.1.2  christos             strncpy (NewString, StartPosition, StringLength);
    190           1.1  christos 
    191       1.1.1.6  christos             NewOp = TrAllocateOp (PARSEOP_STRING_LITERAL);
    192           1.1  christos             NewOp->Asl.Value.String = NewString;
    193           1.1  christos             NewOp->Asl.AmlOpcode = AML_STRING_OP;
    194           1.1  christos             NewOp->Asl.AcpiBtype = ACPI_BTYPE_STRING;
    195           1.1  christos             NewOp->Asl.LogicalLineNumber = Op->Asl.LogicalLineNumber;
    196           1.1  christos 
    197           1.1  christos             OpcCreateConcatenateNode(Op, NewOp);
    198           1.1  christos 
    199           1.1  christos             StringLength = 0;
    200           1.1  christos             StringToProcess = FALSE;
    201           1.1  christos         }
    202           1.1  christos 
    203           1.1  christos         ++Format;
    204           1.1  christos 
    205           1.1  christos         /*
    206           1.1  christos          * We have a format parameter and will need an argument to go
    207           1.1  christos          * with it
    208           1.1  christos          */
    209           1.1  christos         if (!ArgNode ||
    210           1.1  christos             ArgNode->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
    211           1.1  christos         {
    212           1.1  christos             AslError(ASL_ERROR, ASL_MSG_ARG_COUNT_LO, Op, NULL);
    213           1.1  christos             return;
    214           1.1  christos         }
    215           1.1  christos 
    216           1.1  christos         /*
    217           1.1  christos          * We do not support sub-specifiers of printf (flags, width,
    218           1.1  christos          * precision, length). For specifiers we only support %x/%X for
    219           1.1  christos          * hex or %s for strings. Also, %o for generic "acpi object".
    220           1.1  christos          */
    221           1.1  christos         switch (*Format)
    222           1.1  christos         {
    223           1.1  christos         case 's':
    224           1.1  christos 
    225           1.1  christos             if (ArgNode->Asl.ParseOpcode != PARSEOP_STRING_LITERAL)
    226           1.1  christos             {
    227           1.1  christos                 AslError(ASL_ERROR, ASL_MSG_INVALID_TYPE, ArgNode,
    228           1.1  christos                     "String required");
    229           1.1  christos                 return;
    230           1.1  christos             }
    231           1.1  christos 
    232           1.1  christos             NextNode = ArgNode->Asl.Next;
    233           1.1  christos             ArgNode->Asl.Next = NULL;
    234           1.1  christos             OpcCreateConcatenateNode(Op, ArgNode);
    235           1.1  christos             ArgNode = NextNode;
    236           1.1  christos             continue;
    237           1.1  christos 
    238           1.1  christos         case 'X':
    239           1.1  christos         case 'x':
    240           1.1  christos         case 'o':
    241           1.1  christos 
    242           1.1  christos             NextNode = ArgNode->Asl.Next;
    243           1.1  christos             ArgNode->Asl.Next = NULL;
    244           1.1  christos 
    245           1.1  christos             /*
    246           1.1  christos              * Append an empty string if the first argument is
    247  1.1.1.10.4.1   thorpej              * not a string. This will implicitly convert the 2nd
    248           1.1  christos              * concat source to a string per the ACPI specification.
    249           1.1  christos              */
    250           1.1  christos             if (!Op->Asl.Child)
    251           1.1  christos             {
    252       1.1.1.6  christos                 NewOp = TrAllocateOp (PARSEOP_STRING_LITERAL);
    253           1.1  christos                 NewOp->Asl.Value.String = "";
    254           1.1  christos                 NewOp->Asl.AmlOpcode = AML_STRING_OP;
    255           1.1  christos                 NewOp->Asl.AcpiBtype = ACPI_BTYPE_STRING;
    256           1.1  christos                 NewOp->Asl.LogicalLineNumber = Op->Asl.LogicalLineNumber;
    257           1.1  christos 
    258           1.1  christos                 OpcCreateConcatenateNode(Op, NewOp);
    259           1.1  christos             }
    260           1.1  christos 
    261           1.1  christos             OpcCreateConcatenateNode(Op, ArgNode);
    262           1.1  christos             ArgNode = NextNode;
    263           1.1  christos             break;
    264           1.1  christos 
    265           1.1  christos         default:
    266           1.1  christos 
    267           1.1  christos             AslError(ASL_ERROR, ASL_MSG_INVALID_OPERAND, Op,
    268           1.1  christos                 "Unrecognized format specifier");
    269           1.1  christos             continue;
    270           1.1  christos         }
    271           1.1  christos     }
    272           1.1  christos 
    273           1.1  christos     /* Process any remaining string */
    274           1.1  christos 
    275           1.1  christos     if (StringToProcess)
    276           1.1  christos     {
    277       1.1.1.7  christos         NewString = UtLocalCacheCalloc (StringLength + 1);
    278       1.1.1.2  christos         strncpy (NewString, StartPosition, StringLength);
    279           1.1  christos 
    280       1.1.1.6  christos         NewOp = TrAllocateOp (PARSEOP_STRING_LITERAL);
    281           1.1  christos         NewOp->Asl.Value.String = NewString;
    282           1.1  christos         NewOp->Asl.AcpiBtype = ACPI_BTYPE_STRING;
    283           1.1  christos         NewOp->Asl.AmlOpcode = AML_STRING_OP;
    284           1.1  christos         NewOp->Asl.LogicalLineNumber = Op->Asl.LogicalLineNumber;
    285           1.1  christos 
    286           1.1  christos         OpcCreateConcatenateNode(Op, NewOp);
    287           1.1  christos     }
    288           1.1  christos 
    289           1.1  christos     /*
    290           1.1  christos      * If we get here and there's no child node then Format
    291           1.1  christos      * was an empty string. Just make a no op.
    292           1.1  christos      */
    293           1.1  christos     if (!Op->Asl.Child)
    294           1.1  christos     {
    295           1.1  christos         Op->Asl.ParseOpcode = PARSEOP_NOOP;
    296           1.1  christos         AslError(ASL_WARNING, ASL_MSG_NULL_STRING, Op,
    297           1.1  christos             "Converted to NOOP");
    298           1.1  christos         return;
    299           1.1  christos     }
    300           1.1  christos 
    301           1.1  christos      /* Check for erroneous extra arguments */
    302           1.1  christos 
    303           1.1  christos     if (ArgNode &&
    304           1.1  christos         ArgNode->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
    305           1.1  christos     {
    306           1.1  christos         AslError(ASL_WARNING, ASL_MSG_ARG_COUNT_HI, ArgNode,
    307           1.1  christos             "Extra arguments ignored");
    308           1.1  christos     }
    309           1.1  christos 
    310           1.1  christos     /* Change Op to a Store */
    311           1.1  christos 
    312           1.1  christos     Op->Asl.ParseOpcode = PARSEOP_STORE;
    313           1.1  christos     Op->Common.AmlOpcode = AML_STORE_OP;
    314           1.1  christos     Op->Asl.CompileFlags  = 0;
    315           1.1  christos 
    316           1.1  christos     /* Disable further optimization */
    317           1.1  christos 
    318       1.1.1.6  christos     Op->Asl.CompileFlags &= ~OP_COMPILE_TIME_CONST;
    319           1.1  christos     UtSetParseOpName (Op);
    320           1.1  christos 
    321           1.1  christos     /* Set Store destination */
    322           1.1  christos 
    323           1.1  christos     Op->Asl.Child->Asl.Next = DestOp;
    324           1.1  christos }
    325           1.1  christos 
    326           1.1  christos 
    327           1.1  christos /*******************************************************************************
    328           1.1  christos  *
    329           1.1  christos  * FUNCTION:    OpcCreateConcatenateNode
    330           1.1  christos  *
    331           1.1  christos  * PARAMETERS:  Op                  - Parse node
    332           1.1  christos  *              Node                - Parse node to be concatenated
    333           1.1  christos  *
    334           1.1  christos  * RETURN:      None
    335           1.1  christos  *
    336           1.1  christos  * DESCRIPTION: Make Node the child of Op. If child node already exists, then
    337           1.1  christos  *              concat child with Node and makes concat node the child of Op.
    338           1.1  christos  *
    339           1.1  christos  ******************************************************************************/
    340           1.1  christos 
    341           1.1  christos static void
    342           1.1  christos OpcCreateConcatenateNode (
    343           1.1  christos     ACPI_PARSE_OBJECT       *Op,
    344           1.1  christos     ACPI_PARSE_OBJECT       *Node)
    345           1.1  christos {
    346           1.1  christos     ACPI_PARSE_OBJECT       *NewConcatOp;
    347           1.1  christos 
    348           1.1  christos 
    349           1.1  christos     if (!Op->Asl.Child)
    350           1.1  christos     {
    351           1.1  christos         Op->Asl.Child = Node;
    352           1.1  christos         Node->Asl.Parent = Op;
    353           1.1  christos         return;
    354           1.1  christos     }
    355           1.1  christos 
    356       1.1.1.6  christos     NewConcatOp = TrAllocateOp (PARSEOP_CONCATENATE);
    357       1.1.1.5  christos     NewConcatOp->Asl.AmlOpcode = AML_CONCATENATE_OP;
    358           1.1  christos     NewConcatOp->Asl.AcpiBtype = 0x7;
    359           1.1  christos     NewConcatOp->Asl.LogicalLineNumber = Op->Asl.LogicalLineNumber;
    360           1.1  christos 
    361           1.1  christos     /* First arg is child of Op*/
    362           1.1  christos 
    363           1.1  christos     NewConcatOp->Asl.Child = Op->Asl.Child;
    364           1.1  christos     Op->Asl.Child->Asl.Parent = NewConcatOp;
    365           1.1  christos 
    366           1.1  christos     /* Second arg is Node */
    367           1.1  christos 
    368           1.1  christos     NewConcatOp->Asl.Child->Asl.Next = Node;
    369           1.1  christos     Node->Asl.Parent = NewConcatOp;
    370           1.1  christos 
    371           1.1  christos     /* Third arg is Zero (not used) */
    372           1.1  christos 
    373           1.1  christos     NewConcatOp->Asl.Child->Asl.Next->Asl.Next =
    374       1.1.1.6  christos         TrAllocateOp (PARSEOP_ZERO);
    375           1.1  christos     NewConcatOp->Asl.Child->Asl.Next->Asl.Next->Asl.Parent =
    376           1.1  christos         NewConcatOp;
    377           1.1  christos 
    378           1.1  christos     Op->Asl.Child = NewConcatOp;
    379           1.1  christos     NewConcatOp->Asl.Parent = Op;
    380           1.1  christos }
    381