isa_parser.py (6987:31ba8b062d08) | isa_parser.py (6988:55b7e9ba3b4f) |
---|---|
1# Copyright (c) 2003-2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 1181 unchanged lines hidden (view full) --- 1190 list.__init__(self, items) 1191 1192 def push(self, item): 1193 self.append(item); 1194 1195 def top(self): 1196 return self[-1] 1197 | 1# Copyright (c) 2003-2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 1181 unchanged lines hidden (view full) --- 1190 list.__init__(self, items) 1191 1192 def push(self, item): 1193 self.append(item); 1194 1195 def top(self): 1196 return self[-1] 1197 |
1198# The global format stack. 1199formatStack = Stack(NoFormat()) 1200 1201# The global default case stack. 1202defaultStack = Stack(None) 1203 | |
1204# Global stack that tracks current file and line number. 1205# Each element is a tuple (filename, lineno) that records the 1206# *current* filename and the line number in the *previous* file where 1207# it was included. 1208fileNameStack = Stack() 1209 1210 1211####################### --- 39 unchanged lines hidden (view full) --- 1251 1252class ISAParser(Grammar): 1253 def __init__(self, output_dir): 1254 super(ISAParser, self).__init__() 1255 self.output_dir = output_dir 1256 1257 self.templateMap = {} 1258 | 1198# Global stack that tracks current file and line number. 1199# Each element is a tuple (filename, lineno) that records the 1200# *current* filename and the line number in the *previous* file where 1201# it was included. 1202fileNameStack = Stack() 1203 1204 1205####################### --- 39 unchanged lines hidden (view full) --- 1245 1246class ISAParser(Grammar): 1247 def __init__(self, output_dir): 1248 super(ISAParser, self).__init__() 1249 self.output_dir = output_dir 1250 1251 self.templateMap = {} 1252 |
1253 # The format stack. 1254 self.formatStack = Stack(NoFormat()) 1255 1256 # The default case stack. 1257 self.defaultStack = Stack(None) 1258 |
|
1259 ##################################################################### 1260 # 1261 # Lexer 1262 # 1263 # The PLY lexer module takes two things as input: 1264 # - A list of token names (the string list 'tokens') 1265 # - A regular expression describing a match for each token. The 1266 # regexp for token FOO can be provided in two ways: --- 418 unchanged lines hidden (view full) --- 1685 ############## 1686 1687 # 1688 # A decode block looks like: 1689 # decode <field1> [, <field2>]* [default <inst>] { ... } 1690 # 1691 def p_decode_block(self, t): 1692 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE' | 1259 ##################################################################### 1260 # 1261 # Lexer 1262 # 1263 # The PLY lexer module takes two things as input: 1264 # - A list of token names (the string list 'tokens') 1265 # - A regular expression describing a match for each token. The 1266 # regexp for token FOO can be provided in two ways: --- 418 unchanged lines hidden (view full) --- 1685 ############## 1686 1687 # 1688 # A decode block looks like: 1689 # decode <field1> [, <field2>]* [default <inst>] { ... } 1690 # 1691 def p_decode_block(self, t): 1692 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE' |
1693 default_defaults = defaultStack.pop() | 1693 default_defaults = self.defaultStack.pop() |
1694 codeObj = t[5] 1695 # use the "default defaults" only if there was no explicit 1696 # default statement in decode_stmt_list 1697 if not codeObj.has_decode_default: 1698 codeObj += default_defaults 1699 codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n') 1700 t[0] = codeObj 1701 1702 # The opt_default statement serves only to push the "default 1703 # defaults" onto defaultStack. This value will be used by nested 1704 # decode blocks, and used and popped off when the current 1705 # decode_block is processed (in p_decode_block() above). 1706 def p_opt_default_0(self, t): 1707 'opt_default : empty' 1708 # no default specified: reuse the one currently at the top of 1709 # the stack | 1694 codeObj = t[5] 1695 # use the "default defaults" only if there was no explicit 1696 # default statement in decode_stmt_list 1697 if not codeObj.has_decode_default: 1698 codeObj += default_defaults 1699 codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n') 1700 t[0] = codeObj 1701 1702 # The opt_default statement serves only to push the "default 1703 # defaults" onto defaultStack. This value will be used by nested 1704 # decode blocks, and used and popped off when the current 1705 # decode_block is processed (in p_decode_block() above). 1706 def p_opt_default_0(self, t): 1707 'opt_default : empty' 1708 # no default specified: reuse the one currently at the top of 1709 # the stack |
1710 defaultStack.push(defaultStack.top()) | 1710 self.defaultStack.push(self.defaultStack.top()) |
1711 # no meaningful value returned 1712 t[0] = None 1713 1714 def p_opt_default_1(self, t): 1715 'opt_default : DEFAULT inst' 1716 # push the new default 1717 codeObj = t[2] 1718 codeObj.wrap_decode_block('\ndefault:\n', 'break;\n') | 1711 # no meaningful value returned 1712 t[0] = None 1713 1714 def p_opt_default_1(self, t): 1715 'opt_default : DEFAULT inst' 1716 # push the new default 1717 codeObj = t[2] 1718 codeObj.wrap_decode_block('\ndefault:\n', 'break;\n') |
1719 defaultStack.push(codeObj) | 1719 self.defaultStack.push(codeObj) |
1720 # no meaningful value returned 1721 t[0] = None 1722 1723 def p_decode_stmt_list_0(self, t): 1724 'decode_stmt_list : decode_stmt' 1725 t[0] = t[1] 1726 1727 def p_decode_stmt_list_1(self, t): --- 29 unchanged lines hidden (view full) --- 1757 # format on the instruction definition or with a nested format 1758 # block. 1759 def p_decode_stmt_format(self, t): 1760 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE' 1761 # The format will be pushed on the stack when 'push_format_id' 1762 # is processed (see below). Once the parser has recognized 1763 # the full production (though the right brace), we're done 1764 # with the format, so now we can pop it. | 1720 # no meaningful value returned 1721 t[0] = None 1722 1723 def p_decode_stmt_list_0(self, t): 1724 'decode_stmt_list : decode_stmt' 1725 t[0] = t[1] 1726 1727 def p_decode_stmt_list_1(self, t): --- 29 unchanged lines hidden (view full) --- 1757 # format on the instruction definition or with a nested format 1758 # block. 1759 def p_decode_stmt_format(self, t): 1760 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE' 1761 # The format will be pushed on the stack when 'push_format_id' 1762 # is processed (see below). Once the parser has recognized 1763 # the full production (though the right brace), we're done 1764 # with the format, so now we can pop it. |
1765 formatStack.pop() | 1765 self.formatStack.pop() |
1766 t[0] = t[4] 1767 1768 # This rule exists so we can set the current format (& push the 1769 # stack) when we recognize the format name part of the format 1770 # block. 1771 def p_push_format_id(self, t): 1772 'push_format_id : ID' 1773 try: | 1766 t[0] = t[4] 1767 1768 # This rule exists so we can set the current format (& push the 1769 # stack) when we recognize the format name part of the format 1770 # block. 1771 def p_push_format_id(self, t): 1772 'push_format_id : ID' 1773 try: |
1774 formatStack.push(formatMap[t[1]]) | 1774 self.formatStack.push(formatMap[t[1]]) |
1775 t[0] = ('', '// format %s' % t[1]) 1776 except KeyError: 1777 error(t, 'instruction format "%s" not defined.' % t[1]) 1778 1779 # Nested decode block: if the value of the current field matches 1780 # the specified constant, do a nested decode on some other field. 1781 def p_decode_stmt_decode(self, t): 1782 'decode_stmt : case_label COLON decode_block' --- 43 unchanged lines hidden (view full) --- 1826 t[0].append(t[3]) 1827 1828 # Define an instruction using the current instruction format 1829 # (specified by an enclosing format block). 1830 # "<mnemonic>(<args>)" 1831 def p_inst_0(self, t): 1832 'inst : ID LPAREN arg_list RPAREN' 1833 # Pass the ID and arg list to the current format class to deal with. | 1775 t[0] = ('', '// format %s' % t[1]) 1776 except KeyError: 1777 error(t, 'instruction format "%s" not defined.' % t[1]) 1778 1779 # Nested decode block: if the value of the current field matches 1780 # the specified constant, do a nested decode on some other field. 1781 def p_decode_stmt_decode(self, t): 1782 'decode_stmt : case_label COLON decode_block' --- 43 unchanged lines hidden (view full) --- 1826 t[0].append(t[3]) 1827 1828 # Define an instruction using the current instruction format 1829 # (specified by an enclosing format block). 1830 # "<mnemonic>(<args>)" 1831 def p_inst_0(self, t): 1832 'inst : ID LPAREN arg_list RPAREN' 1833 # Pass the ID and arg list to the current format class to deal with. |
1834 currentFormat = formatStack.top() | 1834 currentFormat = self.formatStack.top() |
1835 codeObj = currentFormat.defineInst(t[1], t[3], t.lexer.lineno) 1836 args = ','.join(map(str, t[3])) 1837 args = re.sub('(?m)^', '//', args) 1838 args = re.sub('^//', '', args) 1839 comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args) 1840 codeObj.prepend_all(comment) 1841 t[0] = codeObj 1842 --- 236 unchanged lines hidden --- | 1835 codeObj = currentFormat.defineInst(t[1], t[3], t.lexer.lineno) 1836 args = ','.join(map(str, t[3])) 1837 args = re.sub('(?m)^', '//', args) 1838 args = re.sub('^//', '', args) 1839 comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args) 1840 codeObj.prepend_all(comment) 1841 t[0] = codeObj 1842 --- 236 unchanged lines hidden --- |