From 726c501c2c9a1ef103fab7846e2d1a34506715d8 Mon Sep 17 00:00:00 2001 From: Yonghong Zhu Date: Tue, 26 Dec 2017 16:17:13 +0800 Subject: BaseTools: Support PCD flexible values format https://bugzilla.tianocore.org/show_bug.cgi?id=541 Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng Signed-off-by: Yonghong Zhu Reviewed-by: Liming Gao --- BaseTools/Source/Python/Common/Expression.py | 246 ++++++++++++++++++++++++++- 1 file changed, 237 insertions(+), 9 deletions(-) (limited to 'BaseTools/Source/Python/Common/Expression.py') diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index ba83e02f75..953a412e80 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -15,7 +15,9 @@ from Common.GlobalData import * from CommonDataClass.Exceptions import BadExpression from CommonDataClass.Exceptions import WrnExpression -from Misc import GuidStringToGuidStructureString +from Misc import GuidStringToGuidStructureString, ParseFieldValue +import Common.EdkLogger as EdkLogger +import copy ERR_STRING_EXPR = 'This operator cannot be used in string expression: [%s].' ERR_SNYTAX = 'Syntax error, the rest of expression cannot be evaluated: [%s].' @@ -114,6 +116,15 @@ def ReplaceExprMacro(String, Macros, ExceptionList = None): StrList[i] = RetStr return ''.join(StrList) +# transfer int to string for in/not in expression +def IntToStr(Value): + StrList = [] + while Value > 0: + StrList.append(chr(Value & 0xff)) + Value = Value >> 8 + Value = '"' + ''.join(StrList) + '"' + return Value + SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY'] class ValueExpression(object): @@ -146,10 +157,24 @@ class ValueExpression(object): def Eval(Operator, Oprand1, Oprand2 = None): WrnExp = None - if Operator not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \ - (type(Oprand1) == type('') or type(Oprand2) == type('')): - raise BadExpression(ERR_STRING_EXPR % Operator) - + if Operator not in ["in", "not in"] and (type(Oprand1) == type('') or type(Oprand2) == type('')): + if type(Oprand1) == type(''): + if Oprand1[0] in ['"', "'"] or Oprand1.startswith('L"') or Oprand1.startswith("L'")or Oprand1.startswith('UINT'): + Oprand1, Size = ParseFieldValue(Oprand1) + else: + Oprand1,Size = ParseFieldValue('"' + Oprand1 + '"') + if type(Oprand2) == type(''): + if Oprand2[0] in ['"', "'", 'L'] or Oprand2.startswith('UINT'): + Oprand2, Size = ParseFieldValue(Oprand2) + else: + Oprand2, Size = ParseFieldValue('"' + Oprand2 + '"') + if type(Oprand1) == type('') or type(Oprand2) == type(''): + raise BadExpression(ERR_STRING_EXPR % Operator) + if Operator in ['in', 'not in']: + if type(Oprand1) != type(''): + Oprand1 = IntToStr(Oprand1) + if type(Oprand2) != type(''): + Oprand2 = IntToStr(Oprand2) TypeDict = { type(0) : 0, type(0L) : 0, @@ -226,6 +251,9 @@ class ValueExpression(object): self._Expr = Expression self._NoProcess = True return + if Expression.strip().startswith('{') and Expression.strip().endswith('}'): + self._Expr = Expression + self._NoProcess = True self._Expr = ReplaceExprMacro(Expression.strip(), SymbolTable, @@ -237,7 +265,7 @@ class ValueExpression(object): # # The symbol table including PCD and macro mapping # - self._Symb = SymbolTable + self._Symb = copy.deepcopy(SymbolTable) self._Symb.update(self.LogicalOperators) self._Idx = 0 self._Len = len(self._Expr) @@ -284,7 +312,7 @@ class ValueExpression(object): elif not Val: Val = False RealVal = '""' - elif not Val.startswith('L"') and not Val.startswith('{'): + elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"): Val = True RealVal = '"' + RealVal + '"' @@ -426,6 +454,15 @@ class ValueExpression(object): Radix = 10 if self._Token.lower()[0:2] == '0x' and len(self._Token) > 2: Radix = 16 + if self._Token.startswith('"') or self._Token.startswith("'")\ + or self._Token.startswith("L'") or self._Token.startswith('L"'): + Flag = 0 + for Index in range(len(self._Token)): + if self._Token[Index] in ['"', "'"]: + Flag += 1 + if Flag == 2: + self._Token = ParseFieldValue(self._Token)[0] + return True try: self._Token = int(self._Token, Radix) return True @@ -473,10 +510,12 @@ class ValueExpression(object): Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'') for Ch in Expr: self._Idx += 1 - if Ch == '"': + if Ch == '"' or Ch == "'": break self._Token = self._LiteralToken = self._Expr[Idx:self._Idx] - if not self._Token.endswith('"'): + if self._Token.startswith('"') and not self._Token.endswith('"'): + raise BadExpression(ERR_STRING_TOKEN % self._Token) + if self._Token.startswith("'") and not self._Token.endswith("'"): raise BadExpression(ERR_STRING_TOKEN % self._Token) self._Token = self._Token[1:-1] return self._Token @@ -578,7 +617,44 @@ class ValueExpression(object): self._Idx += 1 UStr = self.__GetString() self._Token = 'L"' + UStr + '"' + self._Token, Size = ParseFieldValue(self._Token) + return self._Token + elif Expr.startswith("L'"): + # Skip L + self._Idx += 1 + UStr = self.__GetString() + self._Token = "L'" + UStr + "'" + self._Token, Size = ParseFieldValue(self._Token) + return self._Token + elif Expr.startswith('"'): + UStr = self.__GetString() + self._Token = '"' + UStr + '"' + self._Token, Size = ParseFieldValue(self._Token) return self._Token + elif Expr.startswith("'"): + UStr = self.__GetString() + self._Token = "'" + UStr + "'" + self._Token, Size = ParseFieldValue(self._Token) + return self._Token + elif Expr.startswith('UINT'): + Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)') + try: + RetValue = Re.search(Expr).group(1) + except: + raise BadExpression('Invalid Expression %s' % Expr) + Idx = self._Idx + for Ch in Expr: + self._Idx += 1 + if Ch == '(': + Prefix = self._Expr[Idx:self._Idx - 1] + Idx = self._Idx + if Ch == ')': + TmpValue = self._Expr[Idx :self._Idx - 1] + TmpValue = ValueExpression(TmpValue)(True) + TmpValue = '0x%x' % int(TmpValue) if type(TmpValue) != type('') else TmpValue + break + self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')') + return self._Token self._Token = '' if Expr: @@ -648,6 +724,158 @@ class ValueExpression(object): self._Idx = Idx return False +class ValueExpressionEx(ValueExpression): + def __init__(self, PcdValue, PcdType, SymbolTable={}): + ValueExpression.__init__(self, PcdValue, SymbolTable) + self.PcdValue = PcdValue + self.PcdType = PcdType + + def __call__(self, RealValue=False, Depth=0): + PcdValue = self.PcdValue + try: + PcdValue = ValueExpression.__call__(self, RealValue, Depth) + except WrnExpression, Value: + PcdValue = Value.result + + if PcdValue == 'True': + PcdValue = '1' + if PcdValue == 'False': + PcdValue = '0' + if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']: + PcdValue = PcdValue.strip() + if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'): + PcdValue = PcdValue[1:-1].split(',') + if type(PcdValue) == type([]): + TmpValue = 0 + Size = 0 + for Item in PcdValue: + if Item.startswith('UINT16'): + ItemSize = 2 + elif Item.startswith('UINT32'): + ItemSize = 4 + elif Item.startswith('UINT64'): + ItemSize = 8 + else: + ItemSize = 0 + Item = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) + + if ItemSize == 0: + ItemValue, ItemSize = ParseFieldValue(Item) + else: + ItemValue = ParseFieldValue(Item)[0] + + if type(ItemValue) == type(''): + ItemValue = int(ItemValue, 16) if ItemValue.startswith('0x') else int(ItemValue) + + TmpValue = (ItemValue << (Size * 8)) | TmpValue + Size = Size + ItemSize + else: + TmpValue, Size = ParseFieldValue(PcdValue) + if type(TmpValue) == type(''): + TmpValue = int(TmpValue) + else: + PcdValue = '0x%0{}X'.format(Size) % (TmpValue) + if TmpValue < 0: + raise BadExpression('Type %s PCD Value is negative' % self.PcdType) + if self.PcdType == 'UINT8' and Size > 1: + raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType) + if self.PcdType == 'UINT16' and Size > 2: + raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType) + if self.PcdType == 'UINT32' and Size > 4: + raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType) + if self.PcdType == 'UINT64' and Size > 8: + raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType) + if self.PcdType in ['VOID*']: + try: + TmpValue = long(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('{'): + PcdValue = PcdValue.strip()[1:-1].strip() + Size = 0 + ValueStr = '' + TokenSpaceGuidName = '' + if PcdValue.startswith('GUID') and PcdValue.endswith(')'): + try: + TokenSpaceGuidName = re.search('GUID\((\w+)\)', PcdValue).group(1) + except: + pass + if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb: + PcdValue = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')' + elif TokenSpaceGuidName: + raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName) + + ListItem, Size = ParseFieldValue(PcdValue) + elif PcdValue.startswith('DEVICE_PATH') and PcdValue.endswith(')'): + ListItem, Size = ParseFieldValue(PcdValue) + else: + ListItem = PcdValue.split(',') + + if type(ListItem) == type(0) or type(ListItem) == type(0L): + for Index in range(0, Size): + ValueStr += '0x%02X' % (int(ListItem) & 255) + ListItem >>= 8 + ValueStr += ', ' + PcdValue = '{' + ValueStr[:-2] + '}' + elif type(ListItem) == type(''): + if ListItem.startswith('{') and ListItem.endswith('}'): + PcdValue = ListItem + else: + LabelDict = {} + ReLabel = re.compile('LABEL\((\w+)\)') + ReOffset = re.compile('OFFSET_OF\((\w+)\)') + for Index, Item in enumerate(ListItem): + # for LABEL parse + Item = Item.strip() + try: + LabelList = ReLabel.findall(Item) + for Label in LabelList: + if Label not in LabelDict.keys(): + LabelDict[Label] = str(Index) + Item = ReLabel.sub('', Item) + except: + pass + try: + OffsetList = ReOffset.findall(Item) + except: + pass + for Offset in OffsetList: + if Offset in LabelDict.keys(): + Re = re.compile('OFFSET_OF\(%s\)'% Offset) + Item = Re.sub(LabelDict[Offset], Item) + else: + raise BadExpression('%s not defined before use' % Offset) + if Item.startswith('UINT16'): + ItemSize = 2 + elif Item.startswith('UINT32'): + ItemSize = 4 + elif Item.startswith('UINT64'): + ItemSize = 8 + else: + ItemSize = 0 + TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) + Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue + if ItemSize == 0: + ItemValue, ItemSize = ParseFieldValue(Item) + else: + ItemValue = ParseFieldValue(Item)[0] + for I in range(0, ItemSize): + ValueStr += '0x%02X' % (int(ItemValue) & 255) + ItemValue >>= 8 + ValueStr += ', ' + Size += ItemSize + + if Size > 0: + PcdValue = '{' + ValueStr[:-2] + '}' + if RealValue: + return PcdValue + if __name__ == '__main__': pass while True: -- cgit v1.2.3