BaseTools: Update ValueExpressionEx for flexible PCD
authorFeng, YunhuaX </o=Intel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Feng, YunhuaX4e1>
Fri, 23 Feb 2018 11:47:30 +0000 (19:47 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Sun, 25 Feb 2018 08:04:23 +0000 (16:04 +0800)
1. Byte  array number should less than 0xFF.
2. Add SplitPcdValueString for PCD split

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/Common/Expression.py

index 28320d78a931fb802686d65cc64edf44d81ed150..edb0a60de6007a367bdf80245fc061d665a3e469 100644 (file)
@@ -68,6 +68,44 @@ def SplitString(String):
         RetList.append(Item)\r
     return RetList\r
 \r
+def SplitPcdValueString(String):\r
+    # There might be escaped comma in GUID() or DEVICE_PATH() or " "\r
+    # or ' ' or L' ' or L" "\r
+    Str = String\r
+    RetList = []\r
+    InParenthesis = 0\r
+    InSingleQuote = False\r
+    InDoubleQuote = False\r
+    Item = ''\r
+    for i, ch in enumerate(Str):\r
+        if ch == '(':\r
+            InParenthesis += 1\r
+        if ch == ')':\r
+            if InParenthesis:\r
+                InParenthesis -= 1\r
+            else:\r
+                raise BadExpression(ERR_STRING_TOKEN % Item)\r
+        if ch == '"' and not InSingleQuote:\r
+            if String[i-1] != '\\':\r
+                InDoubleQuote = not InDoubleQuote\r
+        if ch == "'" and not InDoubleQuote:\r
+            if String[i-1] != '\\':\r
+                InSingleQuote = not InSingleQuote\r
+        if ch == ',':\r
+            if InParenthesis or InSingleQuote or InDoubleQuote:\r
+                Item += String[i]\r
+                continue\r
+            elif Item:\r
+                RetList.append(Item)\r
+                Item = ''\r
+            continue\r
+        Item += String[i]\r
+    if InSingleQuote or InDoubleQuote or InParenthesis:\r
+        raise BadExpression(ERR_STRING_TOKEN % Item)\r
+    if Item:\r
+        RetList.append(Item)\r
+    return RetList\r
+\r
 ## ReplaceExprMacro\r
 #\r
 def ReplaceExprMacro(String, Macros, ExceptionList = None):\r
@@ -733,24 +771,42 @@ class ValueExpressionEx(ValueExpression):
             if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:\r
                 PcdValue = PcdValue.strip()\r
                 if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'):\r
-                    PcdValue = PcdValue[1:-1].split(',')\r
+                    PcdValue = SplitPcdValueString(PcdValue[1:-1])\r
                 if type(PcdValue) == type([]):\r
                     TmpValue = 0\r
                     Size = 0\r
+                    ValueType = ''\r
                     for Item in PcdValue:\r
+                        Item = Item.strip()\r
                         if Item.startswith('UINT8'):\r
                             ItemSize = 1\r
-                        if Item.startswith('UINT16'):\r
+                            ValueType = 'UINT8'\r
+                        elif Item.startswith('UINT16'):\r
                             ItemSize = 2\r
+                            ValueType = 'UINT16'\r
                         elif Item.startswith('UINT32'):\r
                             ItemSize = 4\r
+                            ValueType = 'UINT32'\r
                         elif Item.startswith('UINT64'):\r
                             ItemSize = 8\r
+                            ValueType = 'UINT64'\r
+                        elif Item.startswith('"') or Item.startswith("'") or Item.startswith('L'):\r
+                            ItemSize = 0\r
+                            ValueType = 'VOID*'\r
                         else:\r
                             ItemSize = 0\r
-                        Item = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)\r
+                            ValueType = 'UINT8'\r
+                        Item = ValueExpressionEx(Item, ValueType, self._Symb)(True)\r
 \r
                         if ItemSize == 0:\r
+                            try:\r
+                                tmpValue = int(Item, 16) if Item.upper().startswith('0X') else int(Item, 0)\r
+                                if tmpValue > 255:\r
+                                    raise BadExpression("Byte  array number %s should less than 0xFF." % Item)\r
+                            except BadExpression, Value:\r
+                                raise BadExpression(Value)\r
+                            except ValueError:\r
+                                pass\r
                             ItemValue, ItemSize = ParseFieldValue(Item)\r
                         else:\r
                             ItemValue = ParseFieldValue(Item)[0]\r
@@ -794,84 +850,81 @@ class ValueExpressionEx(ValueExpression):
                         PcdValue = '{' + ', '.join(TmpList) + '}'\r
                 except:\r
                     if PcdValue.strip().startswith('{'):\r
-                        PcdValue = PcdValue.strip()[1:-1].strip()\r
-                        Size = 0\r
-                        ValueStr = ''\r
-                        TokenSpaceGuidName = ''\r
-                        if PcdValue.startswith('GUID') and PcdValue.endswith(')'):\r
+                        PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1])\r
+                        LabelDict = {}\r
+                        NewPcdValueList = []\r
+                        ReLabel = re.compile('LABEL\((\w+)\)')\r
+                        ReOffset = re.compile('OFFSET_OF\((\w+)\)')\r
+                        LabelOffset = 0\r
+                        for Index, Item in enumerate(PcdValueList):\r
+                            # compute byte offset of every LABEL\r
+                            Item = Item.strip()\r
                             try:\r
-                                TokenSpaceGuidName = re.search('GUID\((\w+)\)', PcdValue).group(1)\r
+                                LabelList = ReLabel.findall(Item)\r
+                                for Label in LabelList:\r
+                                    if Label not in LabelDict.keys():\r
+                                        LabelDict[Label] = str(LabelOffset)\r
                             except:\r
                                 pass\r
-                            if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:\r
-                                PcdValue = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'\r
-                            elif TokenSpaceGuidName:\r
-                                raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)\r
-\r
-                            ListItem, Size = ParseFieldValue(PcdValue)\r
-                        elif PcdValue.startswith('DEVICE_PATH') and PcdValue.endswith(')'):\r
-                            ListItem, Size = ParseFieldValue(PcdValue)\r
-                        else:\r
-                            ListItem = PcdValue.split(',')\r
-\r
-                        if type(ListItem) == type(0) or type(ListItem) == type(0L):\r
-                            for Index in range(0, Size):\r
-                                ValueStr += '0x%02X' % (int(ListItem) & 255)\r
-                                ListItem >>= 8\r
-                                ValueStr += ', '\r
-                                PcdValue = '{' + ValueStr[:-2] + '}'\r
-                        elif type(ListItem) == type(''):\r
-                            if ListItem.startswith('{') and ListItem.endswith('}'):\r
-                                PcdValue = ListItem\r
-                        else:\r
-                            LabelDict = {}\r
-                            ReLabel = re.compile('LABEL\((\w+)\)')\r
-                            ReOffset = re.compile('OFFSET_OF\((\w+)\)')\r
-                            LabelOffset = 0\r
-                            for Index, Item in enumerate(ListItem):\r
-                                # compute byte offset of every LABEL\r
-                                Item = Item.strip()\r
+                            if Item.startswith('UINT8'):\r
+                                LabelOffset = LabelOffset + 1\r
+                            elif Item.startswith('UINT16'):\r
+                                LabelOffset = LabelOffset + 2\r
+                            elif Item.startswith('UINT32'):\r
+                                LabelOffset = LabelOffset + 4\r
+                            elif Item.startswith('UINT64'):\r
+                                LabelOffset = LabelOffset + 8\r
+                            else:\r
                                 try:\r
-                                    LabelList = ReLabel.findall(Item)\r
-                                    for Label in LabelList:\r
-                                        if Label not in LabelDict.keys():\r
-                                            LabelDict[Label] = str(LabelOffset)\r
-                                    Item = ReLabel.sub('', Item)\r
-                                except:\r
-                                    pass\r
-                                if Item.startswith('UINT8'):\r
-                                    LabelOffset = LabelOffset + 1\r
-                                elif Item.startswith('UINT16'):\r
-                                    LabelOffset = LabelOffset + 2\r
-                                elif Item.startswith('UINT32'):\r
-                                    LabelOffset = LabelOffset + 4\r
-                                elif Item.startswith('UINT64'):\r
-                                    LabelOffset = LabelOffset + 8\r
-                                else:\r
                                     ItemValue, ItemSize = ParseFieldValue(Item)\r
                                     LabelOffset = LabelOffset + ItemSize\r
-\r
-                            for Index, Item in enumerate(ListItem):\r
-                                # for LABEL parse\r
-                                Item = Item.strip()\r
-                                try:\r
-                                    LabelList = ReLabel.findall(Item)\r
-                                    for Label in LabelList:\r
-                                        if Label not in LabelDict.keys():\r
-                                            LabelDict[Label] = str(Index)\r
-                                    Item = ReLabel.sub('', Item)\r
                                 except:\r
-                                    pass\r
+                                    LabelOffset = LabelOffset + 1\r
+\r
+                        for Index, Item in enumerate(PcdValueList):\r
+                            # for LABEL parse\r
+                            Item = Item.strip()\r
+                            try:\r
+                                Item = ReLabel.sub('', Item)\r
+                            except:\r
+                                pass\r
+                            try:\r
+                                OffsetList = ReOffset.findall(Item)\r
+                            except:\r
+                                pass\r
+                            for Offset in OffsetList:\r
+                                if Offset in LabelDict.keys():\r
+                                    Re = re.compile('OFFSET_OF\(%s\)' % Offset)\r
+                                    Item = Re.sub(LabelDict[Offset], Item)\r
+                                else:\r
+                                    raise BadExpression('%s not defined' % Offset)\r
+                            NewPcdValueList.append(Item)\r
+\r
+                        AllPcdValueList = []\r
+                        for Item in NewPcdValueList:\r
+                            Size = 0\r
+                            ValueStr = ''\r
+                            TokenSpaceGuidName = ''\r
+                            if Item.startswith('GUID') and Item.endswith(')'):\r
                                 try:\r
-                                    OffsetList = ReOffset.findall(Item)\r
+                                    TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1)\r
                                 except:\r
                                     pass\r
-                                for Offset in OffsetList:\r
-                                    if Offset in LabelDict.keys():\r
-                                        Re = re.compile('OFFSET_OF\(%s\)'% Offset)\r
-                                        Item = Re.sub(LabelDict[Offset], Item)\r
-                                    else:\r
-                                        raise BadExpression('%s not defined' % Offset)\r
+                                if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:\r
+                                    Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'\r
+                                elif TokenSpaceGuidName:\r
+                                    raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)\r
+                                Item, Size = ParseFieldValue(Item)\r
+                                for Index in range(0, Size):\r
+                                    ValueStr = '0x%02X' % (int(Item) & 255)\r
+                                    Item >>= 8\r
+                                    AllPcdValueList.append(ValueStr)\r
+                                continue\r
+                            elif Item.startswith('DEVICE_PATH') and Item.endswith(')'):\r
+                                Item, Size = ParseFieldValue(Item)\r
+                                AllPcdValueList.append(Item[1:-1])\r
+                                continue\r
+                            else:\r
                                 ValueType = ""\r
                                 if Item.startswith('UINT8'):\r
                                     ItemSize = 1\r
@@ -894,16 +947,18 @@ class ValueExpressionEx(ValueExpression):
                                 Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue\r
                                 if ItemSize == 0:\r
                                     ItemValue, ItemSize = ParseFieldValue(Item)\r
+                                    if not (Item.startswith('"') or Item.startswith('L') or Item.startswith('{')) and ItemSize > 1:\r
+                                        raise BadExpression("Byte  array number %s should less than 0xFF." % Item)\r
                                 else:\r
                                     ItemValue = ParseFieldValue(Item)[0]\r
                                 for I in range(0, ItemSize):\r
-                                    ValueStr += '0x%02X' % (int(ItemValue) & 255)\r
+                                    ValueStr = '0x%02X' % (int(ItemValue) & 255)\r
                                     ItemValue >>= 8\r
-                                    ValueStr += ', '\r
+                                    AllPcdValueList.append(ValueStr)\r
                                 Size += ItemSize\r
 \r
-                            if Size > 0:\r
-                                PcdValue = '{' + ValueStr[:-2] + '}'\r
+                        if Size > 0:\r
+                            PcdValue = '{' + ','.join(AllPcdValueList) + '}'\r
                     else:\r
                         raise  BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value))\r
 \r