X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=b62efe6f9be681427f33d95d6503ed3c592e62c0;hb=0f6eccdbf75885631221388d362417eb7b873ce0;hp=e398f7a034319fa9765f75aa8898f9f5fa54ea28;hpb=03c36c36a3aaa9e8a6975ebdec35a9533d947ef5;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index e398f7a034..b62efe6f9b 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -2,13 +2,7 @@ # This file is used to parse and evaluate expression in directive or PCD value. # # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
-# This program and the accompanying materials -# are licensed and made available under the terms and conditions of the BSD License -# which accompanies this distribution. The full text of the license may be found at -# http://opensource.org/licenses/bsd-license.php -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# SPDX-License-Identifier: BSD-2-Clause-Patent ## Import Modules # @@ -17,11 +11,13 @@ from __future__ import absolute_import from Common.GlobalData import * from CommonDataClass.Exceptions import BadExpression from CommonDataClass.Exceptions import WrnExpression -from .Misc import GuidStringToGuidStructureString, ParseFieldValue +from .Misc import GuidStringToGuidStructureString, ParseFieldValue,CopyDict import Common.EdkLogger as EdkLogger import copy from Common.DataType import * import sys +from random import sample +import string ERR_STRING_EXPR = 'This operator cannot be used in string expression: [%s].' ERR_SNYTAX = 'Syntax error, the rest of expression cannot be evaluated: [%s].' @@ -47,7 +43,7 @@ ERR_IN_OPERAND = 'Macro after IN operator can only be: $(FAMILY), $(ARC __ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$') _ReLabel = re.compile('LABEL\((\w+)\)') _ReOffset = re.compile('OFFSET_OF\((\w+)\)') -PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$') +PcdPattern = re.compile(r'^[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$') ## SplitString # Split string to list according double quote @@ -55,6 +51,8 @@ PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$') # def SplitString(String): # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi' + RanStr = ''.join(sample(string.ascii_letters + string.digits, 8)) + String = String.replace('\\\\', RanStr).strip() RetList = [] InSingleQuote = False InDoubleQuote = False @@ -87,11 +85,16 @@ def SplitString(String): raise BadExpression(ERR_STRING_TOKEN % Item) if Item: RetList.append(Item) + for i, ch in enumerate(RetList): + if RanStr in ch: + RetList[i] = ch.replace(RanStr,'\\\\') return RetList def SplitPcdValueString(String): # There might be escaped comma in GUID() or DEVICE_PATH() or " " # or ' ' or L' ' or L" " + RanStr = ''.join(sample(string.ascii_letters + string.digits, 8)) + String = String.replace('\\\\', RanStr).strip() RetList = [] InParenthesis = 0 InSingleQuote = False @@ -124,6 +127,9 @@ def SplitPcdValueString(String): raise BadExpression(ERR_STRING_TOKEN % Item) if Item: RetList.append(Item) + for i, ch in enumerate(RetList): + if RanStr in ch: + RetList[i] = ch.replace(RanStr,'\\\\') return RetList def IsValidCName(Str): @@ -232,7 +238,7 @@ class ValueExpression(BaseExpression): 'IN' : 'in' } - NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] + NonLetterOpLst = ['+', '-', TAB_STAR, '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] SymbolPattern = re.compile("(" @@ -297,8 +303,8 @@ class ValueExpression(BaseExpression): else: raise BadExpression(ERR_EXPR_TYPE) if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')): - if ((Oprand1.startswith('L"') or Oprand1.startswith('L')) and (not Oprand2.startswith('L"')) and (not Oprand2.startswith("L'"))) or \ - (((not Oprand1.startswith('L"')) and (not Oprand1.startswith("L'"))) and (Oprand2.startswith('L"') or Oprand2.startswith('L'))): + if ((Oprand1.startswith('L"') or Oprand1.startswith("L'")) and (not Oprand2.startswith('L"')) and (not Oprand2.startswith("L'"))) or \ + (((not Oprand1.startswith('L"')) and (not Oprand1.startswith("L'"))) and (Oprand2.startswith('L"') or Oprand2.startswith("L'"))): raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2)) if 'in' in Operator and isinstance(Oprand2, type('')): Oprand2 = Oprand2.split() @@ -343,7 +349,7 @@ class ValueExpression(BaseExpression): # # The symbol table including PCD and macro mapping # - self._Symb = copy.deepcopy(SymbolTable) + self._Symb = CopyDict(SymbolTable) self._Symb.update(self.LogicalOperators) self._Idx = 0 self._Len = len(self._Expr) @@ -390,7 +396,7 @@ class ValueExpression(BaseExpression): elif not Val: Val = False RealVal = '""' - elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"): + elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'") and not Val.startswith("'"): Val = True RealVal = '"' + RealVal + '"' @@ -427,6 +433,13 @@ class ValueExpression(BaseExpression): else: Val = Val3 continue + # + # PEP 238 -- Changing the Division Operator + # x/y to return a reasonable approximation of the mathematical result of the division ("true division") + # x//y to return the floor ("floor division") + # + if Op == '/': + Op = '//' try: Val = self.Eval(Op, Val, EvalFunc()) except WrnExpression as Warn: @@ -486,7 +499,7 @@ class ValueExpression(BaseExpression): # A [ * B]* def _MulExpr(self): - return self._ExprFuncTemplate(self._UnaryExpr, {"*", "/", "%"}) + return self._ExprFuncTemplate(self._UnaryExpr, {TAB_STAR, "/", "%"}) # [!]*A def _UnaryExpr(self): @@ -808,221 +821,219 @@ class ValueExpressionEx(ValueExpression): def __call__(self, RealValue=False, Depth=0): PcdValue = self.PcdValue - try: - PcdValue = ValueExpression.__call__(self, RealValue, Depth) - if self.PcdType == TAB_VOID and (PcdValue.startswith("'") or PcdValue.startswith("L'")): - PcdValue, Size = ParseFieldValue(PcdValue) - PcdValueList = [] - for I in range(Size): - PcdValueList.append('0x%02X'%(PcdValue & 0xff)) - PcdValue = PcdValue >> 8 - PcdValue = '{' + ','.join(PcdValueList) + '}' - elif self.PcdType in TAB_PCD_NUMERIC_TYPES and (PcdValue.startswith("'") or \ - PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')): - raise BadExpression - except WrnExpression as Value: - PcdValue = Value.result - except BadExpression as Value: - if self.PcdType in TAB_PCD_NUMERIC_TYPES: - PcdValue = PcdValue.strip() - if PcdValue.startswith('{') and PcdValue.endswith('}'): - PcdValue = SplitPcdValueString(PcdValue[1:-1]) - if ERR_STRING_CMP.split(':')[0] in Value.message: - raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value)) - if isinstance(PcdValue, type([])): - TmpValue = 0 - Size = 0 - ValueType = '' - for Item in PcdValue: - Item = Item.strip() - if Item.startswith(TAB_UINT8): - ItemSize = 1 - ValueType = TAB_UINT8 - elif Item.startswith(TAB_UINT16): - ItemSize = 2 - ValueType = TAB_UINT16 - elif Item.startswith(TAB_UINT32): - ItemSize = 4 - ValueType = TAB_UINT32 - elif Item.startswith(TAB_UINT64): - ItemSize = 8 - ValueType = TAB_UINT64 - elif Item[0] in {'"', "'", 'L'}: - ItemSize = 0 - ValueType = TAB_VOID - else: - ItemSize = 0 - ValueType = TAB_UINT8 - Item = ValueExpressionEx(Item, ValueType, self._Symb)(True) - - if ItemSize == 0: - try: - tmpValue = int(Item, 0) - if tmpValue > 255: - raise BadExpression("Byte array number %s should less than 0xFF." % Item) - except BadExpression as Value: - raise BadExpression(Value) - except ValueError: - pass - ItemValue, ItemSize = ParseFieldValue(Item) - else: - ItemValue = ParseFieldValue(Item)[0] - - if isinstance(ItemValue, type('')): - ItemValue = int(ItemValue, 0) - - TmpValue = (ItemValue << (Size * 8)) | TmpValue - Size = Size + ItemSize - else: - try: - TmpValue, Size = ParseFieldValue(PcdValue) - except BadExpression as Value: - raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value)) - if isinstance(TmpValue, type('')): - try: - TmpValue = int(TmpValue) - except: - raise BadExpression(Value) - else: - PcdValue = '0x%0{}X'.format(Size) % (TmpValue) - if TmpValue < 0: - raise BadExpression('Type %s PCD Value is negative' % self.PcdType) - if self.PcdType == TAB_UINT8 and Size > 1: - raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType) - if self.PcdType == TAB_UINT16 and Size > 2: - raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType) - if self.PcdType == TAB_UINT32 and Size > 4: - raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType) - if self.PcdType == TAB_UINT64 and Size > 8: - raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType) - else: - try: - TmpValue = int(PcdValue) - TmpList = [] - if TmpValue.bit_length() == 0: - PcdValue = '{0x00}' - else: - for I in range((TmpValue.bit_length() + 7) / 8): - TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff)) - PcdValue = '{' + ', '.join(TmpList) + '}' - except: - if PcdValue.strip().startswith('{'): - PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1]) - LabelDict = {} - NewPcdValueList = [] - LabelOffset = 0 - for Item in PcdValueList: - # compute byte offset of every LABEL - LabelList = _ReLabel.findall(Item) - Item = _ReLabel.sub('', Item) + if "{CODE(" not in PcdValue: + try: + PcdValue = ValueExpression.__call__(self, RealValue, Depth) + if self.PcdType == TAB_VOID and (PcdValue.startswith("'") or PcdValue.startswith("L'")): + PcdValue, Size = ParseFieldValue(PcdValue) + PcdValueList = [] + for I in range(Size): + PcdValueList.append('0x%02X'%(PcdValue & 0xff)) + PcdValue = PcdValue >> 8 + PcdValue = '{' + ','.join(PcdValueList) + '}' + elif self.PcdType in TAB_PCD_NUMERIC_TYPES and (PcdValue.startswith("'") or \ + PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')): + raise BadExpression + except WrnExpression as Value: + PcdValue = Value.result + except BadExpression as Value: + if self.PcdType in TAB_PCD_NUMERIC_TYPES: + PcdValue = PcdValue.strip() + if PcdValue.startswith('{') and PcdValue.endswith('}'): + PcdValue = SplitPcdValueString(PcdValue[1:-1]) + if isinstance(PcdValue, type([])): + TmpValue = 0 + Size = 0 + ValueType = '' + for Item in PcdValue: Item = Item.strip() - if LabelList: - for Label in LabelList: - if not IsValidCName(Label): - raise BadExpression('%s is not a valid c variable name' % Label) - if Label not in LabelDict: - LabelDict[Label] = str(LabelOffset) if Item.startswith(TAB_UINT8): - LabelOffset = LabelOffset + 1 + ItemSize = 1 + ValueType = TAB_UINT8 elif Item.startswith(TAB_UINT16): - LabelOffset = LabelOffset + 2 + ItemSize = 2 + ValueType = TAB_UINT16 elif Item.startswith(TAB_UINT32): - LabelOffset = LabelOffset + 4 + ItemSize = 4 + ValueType = TAB_UINT32 elif Item.startswith(TAB_UINT64): - LabelOffset = LabelOffset + 8 + ItemSize = 8 + ValueType = TAB_UINT64 + elif Item[0] in {'"', "'", 'L'}: + ItemSize = 0 + ValueType = TAB_VOID else: + ItemSize = 0 + ValueType = TAB_UINT8 + Item = ValueExpressionEx(Item, ValueType, self._Symb)(True) + if ItemSize == 0: try: - ItemValue, ItemSize = ParseFieldValue(Item) - LabelOffset = LabelOffset + ItemSize - except: - LabelOffset = LabelOffset + 1 - - for Item in PcdValueList: - # for LABEL parse - Item = Item.strip() - try: - Item = _ReLabel.sub('', Item) - except: - pass - try: - OffsetList = _ReOffset.findall(Item) - except: - pass - # replace each offset, except errors - for Offset in OffsetList: - try: - Item = Item.replace('OFFSET_OF({})'.format(Offset), LabelDict[Offset]) - except: - raise BadExpression('%s not defined' % Offset) - - NewPcdValueList.append(Item) - - AllPcdValueList = [] - for Item in NewPcdValueList: - Size = 0 - ValueStr = '' - TokenSpaceGuidName = '' - if Item.startswith(TAB_GUID) and Item.endswith(')'): - try: - TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1) - except: + tmpValue = int(Item, 0) + if tmpValue > 255: + raise BadExpression("Byte array number %s should less than 0xFF." % Item) + except BadExpression as Value: + raise BadExpression(Value) + except ValueError: pass - if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb: - Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')' - elif TokenSpaceGuidName: - raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName) - Item, Size = ParseFieldValue(Item) - for Index in range(0, Size): - ValueStr = '0x%02X' % (int(Item) & 255) - Item >>= 8 - AllPcdValueList.append(ValueStr) - continue - elif Item.startswith('DEVICE_PATH') and Item.endswith(')'): - Item, Size = ParseFieldValue(Item) - AllPcdValueList.append(Item[1:-1]) - continue + ItemValue, ItemSize = ParseFieldValue(Item) else: - ValueType = "" + ItemValue = ParseFieldValue(Item)[0] + + if isinstance(ItemValue, type('')): + ItemValue = int(ItemValue, 0) + + TmpValue = (ItemValue << (Size * 8)) | TmpValue + Size = Size + ItemSize + else: + try: + TmpValue, Size = ParseFieldValue(PcdValue) + except BadExpression as Value: + raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value)) + if isinstance(TmpValue, type('')): + try: + TmpValue = int(TmpValue) + except: + raise BadExpression(Value) + else: + PcdValue = '0x%0{}X'.format(Size) % (TmpValue) + if TmpValue < 0: + raise BadExpression('Type %s PCD Value is negative' % self.PcdType) + if self.PcdType == TAB_UINT8 and Size > 1: + raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType) + if self.PcdType == TAB_UINT16 and Size > 2: + raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType) + if self.PcdType == TAB_UINT32 and Size > 4: + raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType) + if self.PcdType == TAB_UINT64 and Size > 8: + raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType) + else: + try: + TmpValue = int(PcdValue) + TmpList = [] + if TmpValue.bit_length() == 0: + PcdValue = '{0x00}' + else: + for I in range((TmpValue.bit_length() + 7) // 8): + TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff)) + PcdValue = '{' + ', '.join(TmpList) + '}' + except: + if PcdValue.strip().startswith('{'): + PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1]) + LabelDict = {} + NewPcdValueList = [] + LabelOffset = 0 + for Item in PcdValueList: + # compute byte offset of every LABEL + LabelList = _ReLabel.findall(Item) + Item = _ReLabel.sub('', Item) + Item = Item.strip() + if LabelList: + for Label in LabelList: + if not IsValidCName(Label): + raise BadExpression('%s is not a valid c variable name' % Label) + if Label not in LabelDict: + LabelDict[Label] = str(LabelOffset) if Item.startswith(TAB_UINT8): - ItemSize = 1 - ValueType = TAB_UINT8 + LabelOffset = LabelOffset + 1 elif Item.startswith(TAB_UINT16): - ItemSize = 2 - ValueType = TAB_UINT16 + LabelOffset = LabelOffset + 2 elif Item.startswith(TAB_UINT32): - ItemSize = 4 - ValueType = TAB_UINT32 + LabelOffset = LabelOffset + 4 elif Item.startswith(TAB_UINT64): - ItemSize = 8 - ValueType = TAB_UINT64 - else: - ItemSize = 0 - if ValueType: - TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True) + LabelOffset = LabelOffset + 8 else: - TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) - Item = '0x%x' % TmpValue if not isinstance(TmpValue, type('')) else TmpValue - if ItemSize == 0: - ItemValue, ItemSize = ParseFieldValue(Item) - if Item[0] not in {'"', 'L', '{'} and ItemSize > 1: - raise BadExpression("Byte array number %s should less than 0xFF." % Item) + try: + ItemValue, ItemSize = ParseFieldValue(Item) + LabelOffset = LabelOffset + ItemSize + except: + LabelOffset = LabelOffset + 1 + + for Item in PcdValueList: + # for LABEL parse + Item = Item.strip() + try: + Item = _ReLabel.sub('', Item) + except: + pass + try: + OffsetList = _ReOffset.findall(Item) + except: + pass + # replace each offset, except errors + for Offset in OffsetList: + try: + Item = Item.replace('OFFSET_OF({})'.format(Offset), LabelDict[Offset]) + except: + raise BadExpression('%s not defined' % Offset) + + NewPcdValueList.append(Item) + + AllPcdValueList = [] + for Item in NewPcdValueList: + Size = 0 + ValueStr = '' + TokenSpaceGuidName = '' + if Item.startswith(TAB_GUID) and Item.endswith(')'): + try: + TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1) + except: + pass + if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb: + Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')' + elif TokenSpaceGuidName: + raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName) + Item, Size = ParseFieldValue(Item) + for Index in range(0, Size): + ValueStr = '0x%02X' % (int(Item) & 255) + Item >>= 8 + AllPcdValueList.append(ValueStr) + continue + elif Item.startswith('DEVICE_PATH') and Item.endswith(')'): + Item, Size = ParseFieldValue(Item) + AllPcdValueList.append(Item[1:-1]) + continue else: - ItemValue = ParseFieldValue(Item)[0] - for I in range(0, ItemSize): - ValueStr = '0x%02X' % (int(ItemValue) & 255) - ItemValue >>= 8 - AllPcdValueList.append(ValueStr) - Size += ItemSize - - if Size > 0: - PcdValue = '{' + ','.join(AllPcdValueList) + '}' - else: - raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value)) + ValueType = "" + if Item.startswith(TAB_UINT8): + ItemSize = 1 + ValueType = TAB_UINT8 + elif Item.startswith(TAB_UINT16): + ItemSize = 2 + ValueType = TAB_UINT16 + elif Item.startswith(TAB_UINT32): + ItemSize = 4 + ValueType = TAB_UINT32 + elif Item.startswith(TAB_UINT64): + ItemSize = 8 + ValueType = TAB_UINT64 + else: + ItemSize = 0 + if ValueType: + TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True) + else: + TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) + Item = '0x%x' % TmpValue if not isinstance(TmpValue, type('')) else TmpValue + if ItemSize == 0: + ItemValue, ItemSize = ParseFieldValue(Item) + if Item[0] not in {'"', 'L', '{'} and ItemSize > 1: + raise BadExpression("Byte array number %s should less than 0xFF." % Item) + else: + ItemValue = ParseFieldValue(Item)[0] + for I in range(0, ItemSize): + ValueStr = '0x%02X' % (int(ItemValue) & 255) + ItemValue >>= 8 + AllPcdValueList.append(ValueStr) + Size += ItemSize + + if Size > 0: + PcdValue = '{' + ', '.join(AllPcdValueList) + '}' + else: + raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value)) - if PcdValue == 'True': - PcdValue = '1' - if PcdValue == 'False': - PcdValue = '0' + if PcdValue == 'True': + PcdValue = '1' + if PcdValue == 'False': + PcdValue = '0' if RealValue: return PcdValue