isa_parser.py revision 3823:1c8f87aa103e
12623SN/A# Copyright (c) 2003-2005 The Regents of The University of Michigan 22623SN/A# All rights reserved. 32623SN/A# 42623SN/A# Redistribution and use in source and binary forms, with or without 52623SN/A# modification, are permitted provided that the following conditions are 62623SN/A# met: redistributions of source code must retain the above copyright 72623SN/A# notice, this list of conditions and the following disclaimer; 82623SN/A# redistributions in binary form must reproduce the above copyright 92623SN/A# notice, this list of conditions and the following disclaimer in the 102623SN/A# documentation and/or other materials provided with the distribution; 112623SN/A# neither the name of the copyright holders nor the names of its 122623SN/A# contributors may be used to endorse or promote products derived from 132623SN/A# this software without specific prior written permission. 142623SN/A# 152623SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 162623SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 172623SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 182623SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 192623SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 202623SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 212623SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222623SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232623SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242623SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 252623SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262623SN/A# 272665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt 282665Ssaidi@eecs.umich.edu# Korey Sewell 292623SN/A 302623SN/Aimport os 313170Sstever@eecs.umich.eduimport sys 322623SN/Aimport re 334040Ssaidi@eecs.umich.eduimport string 342623SN/Aimport traceback 352623SN/A# get type names 363348Sbinkertn@umich.edufrom types import * 373348Sbinkertn@umich.edu 384762Snate@binkert.org# Prepend the directory where the PLY lex & yacc modules are found 392901Ssaidi@eecs.umich.edu# to the search path. Assumes we're compiling in a subdirectory 402623SN/A# of 'build' in the current tree. 412623SN/Asys.path[0:0] = [os.environ['M5_PLY']] 422623SN/A 432623SN/Aimport lex 442856Srdreslin@umich.eduimport yacc 452856Srdreslin@umich.edu 462856Srdreslin@umich.edu##################################################################### 472856Srdreslin@umich.edu# 482856Srdreslin@umich.edu# Lexer 492856Srdreslin@umich.edu# 502856Srdreslin@umich.edu# The PLY lexer module takes two things as input: 512856Srdreslin@umich.edu# - A list of token names (the string list 'tokens') 522856Srdreslin@umich.edu# - A regular expression describing a match for each token. The 532856Srdreslin@umich.edu# regexp for token FOO can be provided in two ways: 542623SN/A# - as a string variable named t_FOO 552623SN/A# - as the doc string for a function named t_FOO. In this case, 562623SN/A# the function is also executed, allowing an action to be 572623SN/A# associated with each token match. 582623SN/A# 592623SN/A##################################################################### 602680Sktlim@umich.edu 612680Sktlim@umich.edu# Reserved words. These are listed separately as they are matched 622623SN/A# using the same regexp as generic IDs, but distinguished in the 632623SN/A# t_ID() function. The PLY documentation suggests this approach. 642680Sktlim@umich.edureserved = ( 652623SN/A 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT', 662623SN/A 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS', 672623SN/A 'OUTPUT', 'SIGNED', 'TEMPLATE' 682623SN/A ) 692623SN/A 703349Sbinkertn@umich.edu# List of tokens. The lex module requires this. 712623SN/Atokens = reserved + ( 722623SN/A # identifier 732623SN/A 'ID', 742623SN/A 752623SN/A # integer literal 762623SN/A 'INTLIT', 773349Sbinkertn@umich.edu 782623SN/A # string literal 793184Srdreslin@umich.edu 'STRLIT', 803184Srdreslin@umich.edu 812623SN/A # code literal 822623SN/A 'CODELIT', 832623SN/A 842623SN/A # ( ) [ ] { } < > , ; : :: * 852623SN/A 'LPAREN', 'RPAREN', 863647Srdreslin@umich.edu 'LBRACKET', 'RBRACKET', 873647Srdreslin@umich.edu 'LBRACE', 'RBRACE', 883647Srdreslin@umich.edu 'LESS', 'GREATER', 'EQUALS', 893647Srdreslin@umich.edu 'COMMA', 'SEMI', 'COLON', 'DBLCOLON', 903647Srdreslin@umich.edu 'ASTERISK', 912631SN/A 923647Srdreslin@umich.edu # C preprocessor directives 932631SN/A 'CPPDIRECTIVE' 942623SN/A 952623SN/A# The following are matched but never returned. commented out to 962623SN/A# suppress PLY warning 972948Ssaidi@eecs.umich.edu # newfile directive 982948Ssaidi@eecs.umich.edu# 'NEWFILE', 993349Sbinkertn@umich.edu 1002948Ssaidi@eecs.umich.edu # endfile directive 1012948Ssaidi@eecs.umich.edu# 'ENDFILE' 1022948Ssaidi@eecs.umich.edu) 1032948Ssaidi@eecs.umich.edu 1042948Ssaidi@eecs.umich.edu# Regular expressions for token matching 1052623SN/At_LPAREN = r'\(' 1063170Sstever@eecs.umich.edut_RPAREN = r'\)' 1073170Sstever@eecs.umich.edut_LBRACKET = r'\[' 1082623SN/At_RBRACKET = r'\]' 1092623SN/At_LBRACE = r'\{' 1103647Srdreslin@umich.edut_RBRACE = r'\}' 1113647Srdreslin@umich.edut_LESS = r'\<' 1123647Srdreslin@umich.edut_GREATER = r'\>' 1133647Srdreslin@umich.edut_EQUALS = r'=' 1142623SN/At_COMMA = r',' 1152839Sktlim@umich.edut_SEMI = r';' 1162867Sktlim@umich.edut_COLON = r':' 1173222Sktlim@umich.edut_DBLCOLON = r'::' 1182901Ssaidi@eecs.umich.edut_ASTERISK = r'\*' 1192623SN/A 1202623SN/A# Identifiers and reserved words 1212623SN/Areserved_map = { } 1222623SN/Afor r in reserved: 1232623SN/A reserved_map[r.lower()] = r 1242623SN/A 1252623SN/Adef t_ID(t): 1262623SN/A r'[A-Za-z_]\w*' 1272623SN/A t.type = reserved_map.get(t.value,'ID') 1282623SN/A return t 1292915Sktlim@umich.edu 1302915Sktlim@umich.edu# Integer literal 1312623SN/Adef t_INTLIT(t): 1322623SN/A r'(0x[\da-fA-F]+)|\d+' 1332623SN/A try: 1342623SN/A t.value = int(t.value,0) 1352623SN/A except ValueError: 1362623SN/A error(t.lineno, 'Integer value "%s" too large' % t.value) 1372915Sktlim@umich.edu t.value = 0 1382915Sktlim@umich.edu return t 1392623SN/A 1402798Sktlim@umich.edu# String literal. Note that these use only single quotes, and 1412798Sktlim@umich.edu# can span multiple lines. 1422901Ssaidi@eecs.umich.edudef t_STRLIT(t): 1432839Sktlim@umich.edu r"(?m)'([^'])+'" 1442798Sktlim@umich.edu # strip off quotes 1452839Sktlim@umich.edu t.value = t.value[1:-1] 1462798Sktlim@umich.edu t.lineno += t.value.count('\n') 1472798Sktlim@umich.edu return t 1482901Ssaidi@eecs.umich.edu 1492901Ssaidi@eecs.umich.edu 1502798Sktlim@umich.edu# "Code literal"... like a string literal, but delimiters are 1512839Sktlim@umich.edu# '{{' and '}}' so they get formatted nicely under emacs c-mode 1522839Sktlim@umich.edudef t_CODELIT(t): 1532901Ssaidi@eecs.umich.edu r"(?m)\{\{([^\}]|}(?!\}))+\}\}" 1542798Sktlim@umich.edu # strip off {{ & }} 1552623SN/A t.value = t.value[2:-2] 1562623SN/A t.lineno += t.value.count('\n') 1572623SN/A return t 1582798Sktlim@umich.edu 1592623SN/Adef t_CPPDIRECTIVE(t): 1602798Sktlim@umich.edu r'^\#[^\#].*\n' 1614762Snate@binkert.org t.lineno += t.value.count('\n') 1623201Shsul@eecs.umich.edu return t 1632867Sktlim@umich.edu 1642867Sktlim@umich.edudef t_NEWFILE(t): 1652915Sktlim@umich.edu r'^\#\#newfile\s+"[\w/.-]*"' 1662915Sktlim@umich.edu fileNameStack.push((t.value[11:-1], t.lineno)) 1672915Sktlim@umich.edu t.lineno = 0 1682867Sktlim@umich.edu 1692867Sktlim@umich.edudef t_ENDFILE(t): 1702867Sktlim@umich.edu r'^\#\#endfile' 1714471Sstever@eecs.umich.edu (old_filename, t.lineno) = fileNameStack.pop() 1722623SN/A 1732798Sktlim@umich.edu# 1742901Ssaidi@eecs.umich.edu# The functions t_NEWLINE, t_ignore, and t_error are 1753222Sktlim@umich.edu# special for the lex module. 1762798Sktlim@umich.edu# 1772798Sktlim@umich.edu 1782798Sktlim@umich.edu# Newlines 1792798Sktlim@umich.edudef t_NEWLINE(t): 1802798Sktlim@umich.edu r'\n+' 1812798Sktlim@umich.edu t.lineno += t.value.count('\n') 1822798Sktlim@umich.edu 1833222Sktlim@umich.edu# Comments 1842867Sktlim@umich.edudef t_comment(t): 1852867Sktlim@umich.edu r'//.*' 1862867Sktlim@umich.edu 1872867Sktlim@umich.edu# Completely ignored characters 1882867Sktlim@umich.edut_ignore = ' \t\x0c' 1892623SN/A 1902623SN/A# Error handler 1912623SN/Adef t_error(t): 1922623SN/A error(t.lineno, "illegal character '%s'" % t.value[0]) 1932623SN/A t.skip(1) 1942623SN/A 1954192Sktlim@umich.edu# Build the lexer 1962623SN/Alex.lex() 1972680Sktlim@umich.edu 1982623SN/A##################################################################### 1992680Sktlim@umich.edu# 2002680Sktlim@umich.edu# Parser 2012680Sktlim@umich.edu# 2022623SN/A# Every function whose name starts with 'p_' defines a grammar rule. 2032623SN/A# The rule is encoded in the function's doc string, while the 2042623SN/A# function body provides the action taken when the rule is matched. 2052623SN/A# The argument to each function is a list of the values of the 2063201Shsul@eecs.umich.edu# rule's symbols: t[0] for the LHS, and t[1..n] for the symbols 2073201Shsul@eecs.umich.edu# on the RHS. For tokens, the value is copied from the t.value 2083201Shsul@eecs.umich.edu# attribute provided by the lexer. For non-terminals, the value 2093201Shsul@eecs.umich.edu# is assigned by the producing rule; i.e., the job of the grammar 2102623SN/A# rule function is to set the value for the non-terminal on the LHS 2112623SN/A# (by assigning to t[0]). 2122623SN/A##################################################################### 2132623SN/A 2142623SN/A# The LHS of the first grammar rule is used as the start symbol 2152623SN/A# (in this case, 'specification'). Note that this rule enforces 2162623SN/A# that there will be exactly one namespace declaration, with 0 or more 2172683Sktlim@umich.edu# global defs/decls before and after it. The defs & decls before 2182623SN/A# the namespace decl will be outside the namespace; those after 2192623SN/A# will be inside. The decoder function is always inside the namespace. 2202623SN/Adef p_specification(t): 2212623SN/A 'specification : opt_defs_and_outputs name_decl opt_defs_and_outputs decode_block' 2222623SN/A global_code = t[1] 2233686Sktlim@umich.edu isa_name = t[2] 2242623SN/A namespace = isa_name + "Inst" 2254471Sstever@eecs.umich.edu # wrap the decode block as a function definition 2262623SN/A t[4].wrap_decode_block(''' 2272623SN/AStaticInstPtr 2282623SN/A%(isa_name)s::decodeInst(%(isa_name)s::ExtMachInst machInst) 2292623SN/A{ 2302623SN/A using namespace %(namespace)s; 2312623SN/A''' % vars(), '}') 2322623SN/A # both the latter output blocks and the decode block are in the namespace 2332683Sktlim@umich.edu namespace_code = t[3] + t[4] 2342623SN/A # pass it all back to the caller of yacc.parse() 2352644Sstever@eecs.umich.edu t[0] = (isa_name, namespace, global_code, namespace_code) 2362623SN/A 2372644Sstever@eecs.umich.edu# ISA name declaration looks like "namespace <foo>;" 2382644Sstever@eecs.umich.edudef p_name_decl(t): 2392623SN/A 'name_decl : NAMESPACE ID SEMI' 2402623SN/A t[0] = t[2] 2412623SN/A 2422623SN/A# 'opt_defs_and_outputs' is a possibly empty sequence of 2432623SN/A# def and/or output statements. 2442623SN/Adef p_opt_defs_and_outputs_0(t): 2452623SN/A 'opt_defs_and_outputs : empty' 2462623SN/A t[0] = GenCode() 2472623SN/A 2482623SN/Adef p_opt_defs_and_outputs_1(t): 2493169Sstever@eecs.umich.edu 'opt_defs_and_outputs : defs_and_outputs' 2503169Sstever@eecs.umich.edu t[0] = t[1] 2513170Sstever@eecs.umich.edu 2522623SN/Adef p_defs_and_outputs_0(t): 2532623SN/A 'defs_and_outputs : def_or_output' 2543169Sstever@eecs.umich.edu t[0] = t[1] 2552623SN/A 2562623SN/Adef p_defs_and_outputs_1(t): 2572623SN/A 'defs_and_outputs : defs_and_outputs def_or_output' 2583169Sstever@eecs.umich.edu t[0] = t[1] + t[2] 2592623SN/A 2602623SN/A# The list of possible definition/output statements. 2612623SN/Adef p_def_or_output(t): 2623349Sbinkertn@umich.edu '''def_or_output : def_format 2634022Sstever@eecs.umich.edu | def_bitfield 2643169Sstever@eecs.umich.edu | def_template 2652623SN/A | def_operand_types 2663169Sstever@eecs.umich.edu | def_operands 2672623SN/A | output_header 2683169Sstever@eecs.umich.edu | output_decoder 2692623SN/A | output_exec 2702623SN/A | global_let''' 2713169Sstever@eecs.umich.edu t[0] = t[1] 2722623SN/A 2732623SN/A# Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied 2744200Ssaidi@eecs.umich.edu# directly to the appropriate output section. 2754200Ssaidi@eecs.umich.edu 2764200Ssaidi@eecs.umich.edu 2774200Ssaidi@eecs.umich.edu# Protect any non-dict-substitution '%'s in a format string 2783658Sktlim@umich.edu# (i.e. those not followed by '(') 2793658Sktlim@umich.edudef protect_non_subst_percents(s): 2802623SN/A return re.sub(r'%(?!\()', '%%', s) 2812623SN/A 2822623SN/A# Massage output block by substituting in template definitions and bit 2832623SN/A# operators. We handle '%'s embedded in the string that don't 2842623SN/A# indicate template substitutions (or CPU-specific symbols, which get 2852623SN/A# handled in GenCode) by doubling them first so that the format 2862623SN/A# operation will reduce them back to single '%'s. 2872623SN/Adef process_output(s): 2882623SN/A s = protect_non_subst_percents(s) 2894040Ssaidi@eecs.umich.edu # protects cpu-specific symbols too 2904040Ssaidi@eecs.umich.edu s = protect_cpu_symbols(s) 2914040Ssaidi@eecs.umich.edu return substBitOps(s % templateMap) 2924040Ssaidi@eecs.umich.edu 2934115Ssaidi@eecs.umich.edudef p_output_header(t): 2944115Ssaidi@eecs.umich.edu 'output_header : OUTPUT HEADER CODELIT SEMI' 2954115Ssaidi@eecs.umich.edu t[0] = GenCode(header_output = process_output(t[3])) 2964115Ssaidi@eecs.umich.edu 2972623SN/Adef p_output_decoder(t): 2982623SN/A 'output_decoder : OUTPUT DECODER CODELIT SEMI' 2992623SN/A t[0] = GenCode(decoder_output = process_output(t[3])) 3002623SN/A 3012623SN/Adef p_output_exec(t): 3022623SN/A 'output_exec : OUTPUT EXEC CODELIT SEMI' 3032623SN/A t[0] = GenCode(exec_output = process_output(t[3])) 3042623SN/A 3052623SN/A# global let blocks 'let {{...}}' (Python code blocks) are executed 3062623SN/A# directly when seen. Note that these execute in a special variable 3072623SN/A# context 'exportContext' to prevent the code from polluting this 3082623SN/A# script's namespace. 3092623SN/Adef p_global_let(t): 3102623SN/A 'global_let : LET CODELIT SEMI' 3112623SN/A updateExportContext() 3122623SN/A try: 3132623SN/A exec fixPythonIndentation(t[2]) in exportContext 3142623SN/A except Exception, exc: 3152623SN/A error(t.lineno(1), 3162623SN/A 'error: %s in global let block "%s".' % (exc, t[2])) 3172623SN/A t[0] = GenCode() # contributes nothing to the output C++ file 3182623SN/A 3192623SN/A# Define the mapping from operand type extensions to C++ types and bit 3202623SN/A# widths (stored in operandTypeMap). 3212623SN/Adef p_def_operand_types(t): 3222623SN/A 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI' 3232623SN/A try: 3242623SN/A userDict = eval('{' + t[3] + '}') 3252623SN/A except Exception, exc: 3262623SN/A error(t.lineno(1), 3272623SN/A 'error: %s in def operand_types block "%s".' % (exc, t[3])) 3282623SN/A buildOperandTypeMap(userDict, t.lineno(1)) 3292623SN/A t[0] = GenCode() # contributes nothing to the output C++ file 3302623SN/A 3312623SN/A# Define the mapping from operand names to operand classes and other 3322623SN/A# traits. Stored in operandNameMap. 3332623SN/Adef p_def_operands(t): 3342623SN/A 'def_operands : DEF OPERANDS CODELIT SEMI' 3352623SN/A if not globals().has_key('operandTypeMap'): 3362623SN/A error(t.lineno(1), 3372623SN/A 'error: operand types must be defined before operands') 3382623SN/A try: 3392623SN/A userDict = eval('{' + t[3] + '}') 3403169Sstever@eecs.umich.edu except Exception, exc: 3413169Sstever@eecs.umich.edu error(t.lineno(1), 3423170Sstever@eecs.umich.edu 'error: %s in def operands block "%s".' % (exc, t[3])) 3432623SN/A buildOperandNameMap(userDict, t.lineno(1)) 3444040Ssaidi@eecs.umich.edu t[0] = GenCode() # contributes nothing to the output C++ file 3454040Ssaidi@eecs.umich.edu 3464040Ssaidi@eecs.umich.edu# A bitfield definition looks like: 3474040Ssaidi@eecs.umich.edu# 'def [signed] bitfield <ID> [<first>:<last>]' 3482623SN/A# This generates a preprocessor macro in the output file. 3493169Sstever@eecs.umich.edudef p_def_bitfield_0(t): 3503169Sstever@eecs.umich.edu 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI' 3512623SN/A expr = 'bits(machInst, %2d, %2d)' % (t[6], t[8]) 3522623SN/A if (t[2] == 'signed'): 3533169Sstever@eecs.umich.edu expr = 'sext<%d>(%s)' % (t[6] - t[8] + 1, expr) 3544040Ssaidi@eecs.umich.edu hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr) 3554040Ssaidi@eecs.umich.edu t[0] = GenCode(header_output = hash_define) 3564040Ssaidi@eecs.umich.edu 3574040Ssaidi@eecs.umich.edu# alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]' 3583169Sstever@eecs.umich.edudef p_def_bitfield_1(t): 3593169Sstever@eecs.umich.edu 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI' 3602623SN/A expr = 'bits(machInst, %2d, %2d)' % (t[6], t[6]) 3613170Sstever@eecs.umich.edu if (t[2] == 'signed'): 3623170Sstever@eecs.umich.edu expr = 'sext<%d>(%s)' % (1, expr) 3633170Sstever@eecs.umich.edu hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr) 3643170Sstever@eecs.umich.edu t[0] = GenCode(header_output = hash_define) 3653170Sstever@eecs.umich.edu 3664040Ssaidi@eecs.umich.edudef p_opt_signed_0(t): 3674040Ssaidi@eecs.umich.edu 'opt_signed : SIGNED' 3684040Ssaidi@eecs.umich.edu t[0] = t[1] 3694040Ssaidi@eecs.umich.edu 3703170Sstever@eecs.umich.edudef p_opt_signed_1(t): 3713170Sstever@eecs.umich.edu 'opt_signed : empty' 3723170Sstever@eecs.umich.edu t[0] = '' 3733170Sstever@eecs.umich.edu 3743170Sstever@eecs.umich.edu# Global map variable to hold templates 3753170Sstever@eecs.umich.edutemplateMap = {} 3763170Sstever@eecs.umich.edu 3773170Sstever@eecs.umich.edudef p_def_template(t): 3783170Sstever@eecs.umich.edu 'def_template : DEF TEMPLATE ID CODELIT SEMI' 3792623SN/A templateMap[t[3]] = Template(t[4]) 3804200Ssaidi@eecs.umich.edu t[0] = GenCode() 3814200Ssaidi@eecs.umich.edu 3824200Ssaidi@eecs.umich.edu# An instruction format definition looks like 3833658Sktlim@umich.edu# "def format <fmt>(<params>) {{...}};" 3843658Sktlim@umich.edudef p_def_format(t): 3852623SN/A 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI' 3862623SN/A (id, params, code) = (t[3], t[5], t[7]) 3872623SN/A defFormat(id, params, code, t.lineno(1)) 3882623SN/A t[0] = GenCode() 3892623SN/A 3902623SN/A# The formal parameter list for an instruction format is a possibly 3912623SN/A# empty list of comma-separated parameters. Positional (standard, 3922623SN/A# non-keyword) parameters must come first, followed by keyword 3932623SN/A# parameters, followed by a '*foo' parameter that gets excess 3942623SN/A# positional arguments (as in Python). Each of these three parameter 3952623SN/A# categories is optional. 3962623SN/A# 3974224Sgblack@eecs.umich.edu# Note that we do not support the '**foo' parameter for collecting 3984224Sgblack@eecs.umich.edu# otherwise undefined keyword args. Otherwise the parameter list is 3994224Sgblack@eecs.umich.edu# (I believe) identical to what is supported in Python. 4004224Sgblack@eecs.umich.edu# 4014224Sgblack@eecs.umich.edu# The param list generates a tuple, where the first element is a list of 4024224Sgblack@eecs.umich.edu# the positional params and the second element is a dict containing the 4034224Sgblack@eecs.umich.edu# keyword params. 4044224Sgblack@eecs.umich.edudef p_param_list_0(t): 4054224Sgblack@eecs.umich.edu 'param_list : positional_param_list COMMA nonpositional_param_list' 4064224Sgblack@eecs.umich.edu t[0] = t[1] + t[3] 4072623SN/A 4082623SN/Adef p_param_list_1(t): 4092623SN/A '''param_list : positional_param_list 4102623SN/A | nonpositional_param_list''' 4112623SN/A t[0] = t[1] 4122623SN/A 4132623SN/Adef p_positional_param_list_0(t): 4142623SN/A 'positional_param_list : empty' 4152623SN/A t[0] = [] 4162623SN/A 4172623SN/Adef p_positional_param_list_1(t): 4182623SN/A 'positional_param_list : ID' 4192623SN/A t[0] = [t[1]] 4202623SN/A 4212623SN/Adef p_positional_param_list_2(t): 4222623SN/A 'positional_param_list : positional_param_list COMMA ID' 4232623SN/A t[0] = t[1] + [t[3]] 4242623SN/A 4252623SN/Adef p_nonpositional_param_list_0(t): 4262623SN/A 'nonpositional_param_list : keyword_param_list COMMA excess_args_param' 4272623SN/A t[0] = t[1] + t[3] 4282623SN/A 4292623SN/Adef p_nonpositional_param_list_1(t): 4302623SN/A '''nonpositional_param_list : keyword_param_list 4312623SN/A | excess_args_param''' 4322623SN/A t[0] = t[1] 4332623SN/A 4342623SN/Adef p_keyword_param_list_0(t): 4352623SN/A 'keyword_param_list : keyword_param' 4362623SN/A t[0] = [t[1]] 4372623SN/A 4382623SN/Adef p_keyword_param_list_1(t): 4392623SN/A 'keyword_param_list : keyword_param_list COMMA keyword_param' 4402623SN/A t[0] = t[1] + [t[3]] 4412623SN/A 4422623SN/Adef p_keyword_param(t): 4432623SN/A 'keyword_param : ID EQUALS expr' 4442623SN/A t[0] = t[1] + ' = ' + t[3].__repr__() 4452623SN/A 4462623SN/Adef p_excess_args_param(t): 4472623SN/A 'excess_args_param : ASTERISK ID' 4482623SN/A # Just concatenate them: '*ID'. Wrap in list to be consistent 4492623SN/A # with positional_param_list and keyword_param_list. 4502623SN/A t[0] = [t[1] + t[2]] 4512623SN/A 4522623SN/A# End of format definition-related rules. 4533387Sgblack@eecs.umich.edu############## 4543387Sgblack@eecs.umich.edu 4552631SN/A# 4562663Sstever@eecs.umich.edu# A decode block looks like: 4573170Sstever@eecs.umich.edu# decode <field1> [, <field2>]* [default <inst>] { ... } 4582662Sstever@eecs.umich.edu# 4592623SN/Adef p_decode_block(t): 4604022Sstever@eecs.umich.edu 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE' 4612623SN/A default_defaults = defaultStack.pop() 4622623SN/A codeObj = t[5] 4632623SN/A # use the "default defaults" only if there was no explicit 4642630SN/A # default statement in decode_stmt_list 4652623SN/A if not codeObj.has_decode_default: 4662623SN/A codeObj += default_defaults 4672623SN/A codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n') 4682623SN/A t[0] = codeObj 4692623SN/A 4702623SN/A# The opt_default statement serves only to push the "default defaults" 4712623SN/A# onto defaultStack. This value will be used by nested decode blocks, 4722623SN/A# and used and popped off when the current decode_block is processed 4732623SN/A# (in p_decode_block() above). 4743658Sktlim@umich.edudef p_opt_default_0(t): 4753658Sktlim@umich.edu 'opt_default : empty' 4762644Sstever@eecs.umich.edu # no default specified: reuse the one currently at the top of the stack 4772644Sstever@eecs.umich.edu defaultStack.push(defaultStack.top()) 4782623SN/A # no meaningful value returned 4793222Sktlim@umich.edu t[0] = None 4803222Sktlim@umich.edu 4813222Sktlim@umich.edudef p_opt_default_1(t): 4822623SN/A 'opt_default : DEFAULT inst' 4832623SN/A # push the new default 4842623SN/A codeObj = t[2] 4852623SN/A codeObj.wrap_decode_block('\ndefault:\n', 'break;\n') 4862644Sstever@eecs.umich.edu defaultStack.push(codeObj) 4872623SN/A # no meaningful value returned 4882623SN/A t[0] = None 4892623SN/A 4902631SN/Adef p_decode_stmt_list_0(t): 4912631SN/A 'decode_stmt_list : decode_stmt' 4922631SN/A t[0] = t[1] 4932631SN/A 4942631SN/Adef p_decode_stmt_list_1(t): 4952631SN/A 'decode_stmt_list : decode_stmt decode_stmt_list' 4962623SN/A if (t[1].has_decode_default and t[2].has_decode_default): 4972623SN/A error(t.lineno(1), 'Two default cases in decode block') 4982623SN/A t[0] = t[1] + t[2] 4992623SN/A 5003349Sbinkertn@umich.edu# 5012623SN/A# Decode statement rules 5022623SN/A# 5032623SN/A# There are four types of statements allowed in a decode block: 5042644Sstever@eecs.umich.edu# 1. Format blocks 'format <foo> { ... }' 5052623SN/A# 2. Nested decode blocks 5062798Sktlim@umich.edu# 3. Instruction definitions. 5072623SN/A# 4. C preprocessor directives. 5082644Sstever@eecs.umich.edu 5093222Sktlim@umich.edu 5103222Sktlim@umich.edu# Preprocessor directives found in a decode statement list are passed 5113222Sktlim@umich.edu# through to the output, replicated to all of the output code 5122839Sktlim@umich.edu# streams. This works well for ifdefs, so we can ifdef out both the 5133658Sktlim@umich.edu# declarations and the decode cases generated by an instruction 5143658Sktlim@umich.edu# definition. Handling them as part of the grammar makes it easy to 5153658Sktlim@umich.edu# keep them in the right place with respect to the code generated by 5162839Sktlim@umich.edu# the other statements. 5172798Sktlim@umich.edudef p_decode_stmt_cpp(t): 5182798Sktlim@umich.edu 'decode_stmt : CPPDIRECTIVE' 5192798Sktlim@umich.edu t[0] = GenCode(t[1], t[1], t[1], t[1]) 5202623SN/A 5212644Sstever@eecs.umich.edu# A format block 'format <foo> { ... }' sets the default instruction 5222623SN/A# format used to handle instruction definitions inside the block. 5232623SN/A# This format can be overridden by using an explicit format on the 5243170Sstever@eecs.umich.edu# instruction definition or with a nested format block. 5253170Sstever@eecs.umich.edudef p_decode_stmt_format(t): 5263170Sstever@eecs.umich.edu 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE' 5273170Sstever@eecs.umich.edu # The format will be pushed on the stack when 'push_format_id' is 5282644Sstever@eecs.umich.edu # processed (see below). Once the parser has recognized the full 5293170Sstever@eecs.umich.edu # production (though the right brace), we're done with the format, 5303170Sstever@eecs.umich.edu # so now we can pop it. 5313170Sstever@eecs.umich.edu formatStack.pop() 5323170Sstever@eecs.umich.edu t[0] = t[4] 5333170Sstever@eecs.umich.edu 5343170Sstever@eecs.umich.edu# This rule exists so we can set the current format (& push the stack) 5353170Sstever@eecs.umich.edu# when we recognize the format name part of the format block. 5363170Sstever@eecs.umich.edudef p_push_format_id(t): 5373170Sstever@eecs.umich.edu 'push_format_id : ID' 5382644Sstever@eecs.umich.edu try: 5392644Sstever@eecs.umich.edu formatStack.push(formatMap[t[1]]) 5402644Sstever@eecs.umich.edu t[0] = ('', '// format %s' % t[1]) 5412623SN/A except KeyError: 5422623SN/A error(t.lineno(1), 'instruction format "%s" not defined.' % t[1]) 5432623SN/A 5442644Sstever@eecs.umich.edu# Nested decode block: if the value of the current field matches the 5452644Sstever@eecs.umich.edu# specified constant, do a nested decode on some other field. 5462623SN/Adef p_decode_stmt_decode(t): 5473658Sktlim@umich.edu 'decode_stmt : case_label COLON decode_block' 5483658Sktlim@umich.edu label = t[1] 5493658Sktlim@umich.edu codeObj = t[3] 5502623SN/A # just wrap the decoding code from the block as a case in the 5512623SN/A # outer switch statement. 5522948Ssaidi@eecs.umich.edu codeObj.wrap_decode_block('\n%s:\n' % label) 5532948Ssaidi@eecs.umich.edu codeObj.has_decode_default = (label == 'default') 5542948Ssaidi@eecs.umich.edu t[0] = codeObj 5552948Ssaidi@eecs.umich.edu 5562948Ssaidi@eecs.umich.edu# Instruction definition (finally!). 5572623SN/Adef p_decode_stmt_inst(t): 5582623SN/A 'decode_stmt : case_label COLON inst SEMI' 5593349Sbinkertn@umich.edu label = t[1] 5602623SN/A codeObj = t[3] 5613310Srdreslin@umich.edu codeObj.wrap_decode_block('\n%s:' % label, 'break;\n') 5623310Srdreslin@umich.edu codeObj.has_decode_default = (label == 'default') 5634584Ssaidi@eecs.umich.edu t[0] = codeObj 5642948Ssaidi@eecs.umich.edu 5653495Sktlim@umich.edu# The case label is either a list of one or more constants or 'default' 5663310Srdreslin@umich.edudef p_case_label_0(t): 5673310Srdreslin@umich.edu 'case_label : intlit_list' 5683495Sktlim@umich.edu t[0] = ': '.join(map(lambda a: 'case %#x' % a, t[1])) 5692948Ssaidi@eecs.umich.edu 5703310Srdreslin@umich.edudef p_case_label_1(t): 5713310Srdreslin@umich.edu 'case_label : DEFAULT' 5724433Ssaidi@eecs.umich.edu t[0] = 'default' 5734433Ssaidi@eecs.umich.edu 5744433Ssaidi@eecs.umich.edu# 5754433Ssaidi@eecs.umich.edu# The constant list for a decode case label must be non-empty, but may have 5764433Ssaidi@eecs.umich.edu# one or more comma-separated integer literals in it. 5774433Ssaidi@eecs.umich.edu# 5784433Ssaidi@eecs.umich.edudef p_intlit_list_0(t): 5793310Srdreslin@umich.edu 'intlit_list : INTLIT' 5804433Ssaidi@eecs.umich.edu t[0] = [t[1]] 5814433Ssaidi@eecs.umich.edu 5822623SN/Adef p_intlit_list_1(t): 5832623SN/A 'intlit_list : intlit_list COMMA INTLIT' 5842657Ssaidi@eecs.umich.edu t[0] = t[1] 5852623SN/A t[0].append(t[3]) 5862623SN/A 5872623SN/A# Define an instruction using the current instruction format (specified 5882623SN/A# by an enclosing format block). 5892623SN/A# "<mnemonic>(<args>)" 5902623SN/Adef p_inst_0(t): 5913349Sbinkertn@umich.edu 'inst : ID LPAREN arg_list RPAREN' 5922657Ssaidi@eecs.umich.edu # Pass the ID and arg list to the current format class to deal with. 5932657Ssaidi@eecs.umich.edu currentFormat = formatStack.top() 5942657Ssaidi@eecs.umich.edu codeObj = currentFormat.defineInst(t[1], t[3], t.lineno(1)) 5952657Ssaidi@eecs.umich.edu args = ','.join(map(str, t[3])) 5962623SN/A args = re.sub('(?m)^', '//', args) 5972623SN/A args = re.sub('^//', '', args) 5982623SN/A comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args) 5993349Sbinkertn@umich.edu codeObj.prepend_all(comment) 6002623SN/A t[0] = codeObj 6012623SN/A 6022623SN/A# Define an instruction using an explicitly specified format: 6032641Sstever@eecs.umich.edu# "<fmt>::<mnemonic>(<args>)" 6042623SN/Adef p_inst_1(t): 6052623SN/A 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN' 6062623SN/A try: 6073222Sktlim@umich.edu format = formatMap[t[1]] 6083222Sktlim@umich.edu except KeyError: 6093184Srdreslin@umich.edu error(t.lineno(1), 'instruction format "%s" not defined.' % t[1]) 6102623SN/A codeObj = format.defineInst(t[3], t[5], t.lineno(1)) 6112623SN/A comment = '\n// %s::%s(%s)\n' % (t[1], t[3], t[5]) 6123170Sstever@eecs.umich.edu codeObj.prepend_all(comment) 6133170Sstever@eecs.umich.edu t[0] = codeObj 6143170Sstever@eecs.umich.edu 6153170Sstever@eecs.umich.edu# The arg list generates a tuple, where the first element is a list of 6162644Sstever@eecs.umich.edu# the positional args and the second element is a dict containing the 6172644Sstever@eecs.umich.edu# keyword args. 6182644Sstever@eecs.umich.edudef p_arg_list_0(t): 6193184Srdreslin@umich.edu 'arg_list : positional_arg_list COMMA keyword_arg_list' 6203227Sktlim@umich.edu t[0] = ( t[1], t[3] ) 6213201Shsul@eecs.umich.edu 6223201Shsul@eecs.umich.edudef p_arg_list_1(t): 6233201Shsul@eecs.umich.edu 'arg_list : positional_arg_list' 6243201Shsul@eecs.umich.edu t[0] = ( t[1], {} ) 6253201Shsul@eecs.umich.edu 6263201Shsul@eecs.umich.edudef p_arg_list_2(t): 6273201Shsul@eecs.umich.edu 'arg_list : keyword_arg_list' 6282644Sstever@eecs.umich.edu t[0] = ( [], t[1] ) 6292623SN/A 6302623SN/Adef p_positional_arg_list_0(t): 6312623SN/A 'positional_arg_list : empty' 6322798Sktlim@umich.edu t[0] = [] 6332839Sktlim@umich.edu 6342798Sktlim@umich.edudef p_positional_arg_list_1(t): 6352839Sktlim@umich.edu 'positional_arg_list : expr' 6362901Ssaidi@eecs.umich.edu t[0] = [t[1]] 6372839Sktlim@umich.edu 6382798Sktlim@umich.edudef p_positional_arg_list_2(t): 6392623SN/A 'positional_arg_list : positional_arg_list COMMA expr' 6404192Sktlim@umich.edu t[0] = t[1] + [t[3]] 6414192Sktlim@umich.edu 6424192Sktlim@umich.edudef p_keyword_arg_list_0(t): 6434192Sktlim@umich.edu 'keyword_arg_list : keyword_arg' 6444192Sktlim@umich.edu t[0] = t[1] 6454192Sktlim@umich.edu 6464192Sktlim@umich.edudef p_keyword_arg_list_1(t): 6474192Sktlim@umich.edu 'keyword_arg_list : keyword_arg_list COMMA keyword_arg' 6484192Sktlim@umich.edu t[0] = t[1] 6494192Sktlim@umich.edu t[0].update(t[3]) 6504192Sktlim@umich.edu 6514192Sktlim@umich.edudef p_keyword_arg(t): 6522623SN/A 'keyword_arg : ID EQUALS expr' 6533349Sbinkertn@umich.edu t[0] = { t[1] : t[3] } 6542623SN/A 6553310Srdreslin@umich.edu# 6563310Srdreslin@umich.edu# Basic expressions. These constitute the argument values of 6574584Ssaidi@eecs.umich.edu# "function calls" (i.e. instruction definitions in the decode block) 6582948Ssaidi@eecs.umich.edu# and default values for formal parameters of format functions. 6593495Sktlim@umich.edu# 6603310Srdreslin@umich.edu# Right now, these are either strings, integers, or (recursively) 6613310Srdreslin@umich.edu# lists of exprs (using Python square-bracket list syntax). Note that 6623495Sktlim@umich.edu# bare identifiers are trated as string constants here (since there 6632948Ssaidi@eecs.umich.edu# isn't really a variable namespace to refer to). 6643310Srdreslin@umich.edu# 6653310Srdreslin@umich.edudef p_expr_0(t): 6664433Ssaidi@eecs.umich.edu '''expr : ID 6674433Ssaidi@eecs.umich.edu | INTLIT 6684433Ssaidi@eecs.umich.edu | STRLIT 6694433Ssaidi@eecs.umich.edu | CODELIT''' 6704433Ssaidi@eecs.umich.edu t[0] = t[1] 6714433Ssaidi@eecs.umich.edu 6724433Ssaidi@eecs.umich.edudef p_expr_1(t): 6733310Srdreslin@umich.edu '''expr : LBRACKET list_expr RBRACKET''' 6744433Ssaidi@eecs.umich.edu t[0] = t[2] 6754433Ssaidi@eecs.umich.edu 6762948Ssaidi@eecs.umich.edudef p_list_expr_0(t): 6772948Ssaidi@eecs.umich.edu 'list_expr : expr' 6782948Ssaidi@eecs.umich.edu t[0] = [t[1]] 6792948Ssaidi@eecs.umich.edu 6802948Ssaidi@eecs.umich.edudef p_list_expr_1(t): 6812630SN/A 'list_expr : list_expr COMMA expr' 6822623SN/A t[0] = t[1] + [t[3]] 6832623SN/A 6842657Ssaidi@eecs.umich.edudef p_list_expr_2(t): 6852623SN/A 'list_expr : empty' 6862623SN/A t[0] = [] 6872623SN/A 6882623SN/A# 6892623SN/A# Empty production... use in other rules for readability. 6902623SN/A# 6913349Sbinkertn@umich.edudef p_empty(t): 6922657Ssaidi@eecs.umich.edu 'empty :' 6932657Ssaidi@eecs.umich.edu pass 6943170Sstever@eecs.umich.edu 6952657Ssaidi@eecs.umich.edu# Parse error handler. Note that the argument here is the offending 6962657Ssaidi@eecs.umich.edu# *token*, not a grammar symbol (hence the need to use t.value) 6972623SN/Adef p_error(t): 6982623SN/A if t: 6992623SN/A error(t.lineno, "syntax error at '%s'" % t.value) 7002623SN/A else: 7012623SN/A error(0, "unknown syntax error", True) 7022623SN/A 7032623SN/A# END OF GRAMMAR RULES 7044762Snate@binkert.org# 7054762Snate@binkert.org# Now build the parser. 7062623SN/Ayacc.yacc() 7072623SN/A 7084762Snate@binkert.org 7092623SN/A##################################################################### 7102623SN/A# 7112623SN/A# Support Classes 7122623SN/A# 7132623SN/A##################################################################### 7143119Sktlim@umich.edu 7152623SN/A# Expand template with CPU-specific references into a dictionary with 7162623SN/A# an entry for each CPU model name. The entry key is the model name 7173661Srdreslin@umich.edu# and the corresponding value is the template with the CPU-specific 7182623SN/A# refs substituted for that model. 7192623SN/Adef expand_cpu_symbols_to_dict(template): 7202901Ssaidi@eecs.umich.edu # Protect '%'s that don't go with CPU-specific terms 7213170Sstever@eecs.umich.edu t = re.sub(r'%(?!\(CPU_)', '%%', template) 7222623SN/A result = {} 7232623SN/A for cpu in cpu_models: 7242623SN/A result[cpu.name] = t % cpu.strings 7252623SN/A return result 7262623SN/A 7273617Sbinkertn@umich.edu# *If* the template has CPU-specific references, return a single 7283617Sbinkertn@umich.edu# string containing a copy of the template for each CPU model with the 7293617Sbinkertn@umich.edu# corresponding values substituted in. If the template has no 7302623SN/A# CPU-specific references, it is returned unmodified. 7314762Snate@binkert.orgdef expand_cpu_symbols_to_string(template): 7324762Snate@binkert.org if template.find('%(CPU_') != -1: 7334762Snate@binkert.org return reduce(lambda x,y: x+y, 7342623SN/A expand_cpu_symbols_to_dict(template).values()) 7352623SN/A else: 7362623SN/A return template 7372623SN/A 7382623SN/A# Protect CPU-specific references by doubling the corresponding '%'s 739# (in preparation for substituting a different set of references into 740# the template). 741def protect_cpu_symbols(template): 742 return re.sub(r'%(?=\(CPU_)', '%%', template) 743 744############### 745# GenCode class 746# 747# The GenCode class encapsulates generated code destined for various 748# output files. The header_output and decoder_output attributes are 749# strings containing code destined for decoder.hh and decoder.cc 750# respectively. The decode_block attribute contains code to be 751# incorporated in the decode function itself (that will also end up in 752# decoder.cc). The exec_output attribute is a dictionary with a key 753# for each CPU model name; the value associated with a particular key 754# is the string of code for that CPU model's exec.cc file. The 755# has_decode_default attribute is used in the decode block to allow 756# explicit default clauses to override default default clauses. 757 758class GenCode: 759 # Constructor. At this point we substitute out all CPU-specific 760 # symbols. For the exec output, these go into the per-model 761 # dictionary. For all other output types they get collapsed into 762 # a single string. 763 def __init__(self, 764 header_output = '', decoder_output = '', exec_output = '', 765 decode_block = '', has_decode_default = False): 766 self.header_output = expand_cpu_symbols_to_string(header_output) 767 self.decoder_output = expand_cpu_symbols_to_string(decoder_output) 768 if isinstance(exec_output, dict): 769 self.exec_output = exec_output 770 elif isinstance(exec_output, str): 771 # If the exec_output arg is a single string, we replicate 772 # it for each of the CPU models, substituting and 773 # %(CPU_foo)s params appropriately. 774 self.exec_output = expand_cpu_symbols_to_dict(exec_output) 775 self.decode_block = expand_cpu_symbols_to_string(decode_block) 776 self.has_decode_default = has_decode_default 777 778 # Override '+' operator: generate a new GenCode object that 779 # concatenates all the individual strings in the operands. 780 def __add__(self, other): 781 exec_output = {} 782 for cpu in cpu_models: 783 n = cpu.name 784 exec_output[n] = self.exec_output[n] + other.exec_output[n] 785 return GenCode(self.header_output + other.header_output, 786 self.decoder_output + other.decoder_output, 787 exec_output, 788 self.decode_block + other.decode_block, 789 self.has_decode_default or other.has_decode_default) 790 791 # Prepend a string (typically a comment) to all the strings. 792 def prepend_all(self, pre): 793 self.header_output = pre + self.header_output 794 self.decoder_output = pre + self.decoder_output 795 self.decode_block = pre + self.decode_block 796 for cpu in cpu_models: 797 self.exec_output[cpu.name] = pre + self.exec_output[cpu.name] 798 799 # Wrap the decode block in a pair of strings (e.g., 'case foo:' 800 # and 'break;'). Used to build the big nested switch statement. 801 def wrap_decode_block(self, pre, post = ''): 802 self.decode_block = pre + indent(self.decode_block) + post 803 804################ 805# Format object. 806# 807# A format object encapsulates an instruction format. It must provide 808# a defineInst() method that generates the code for an instruction 809# definition. 810 811exportContextSymbols = ('InstObjParams', 'CodeBlock', 812 'makeList', 're', 'string') 813 814exportContext = {} 815 816def updateExportContext(): 817 exportContext.update(exportDict(*exportContextSymbols)) 818 exportContext.update(templateMap) 819 820def exportDict(*symNames): 821 return dict([(s, eval(s)) for s in symNames]) 822 823 824class Format: 825 def __init__(self, id, params, code): 826 # constructor: just save away arguments 827 self.id = id 828 self.params = params 829 label = 'def format ' + id 830 self.user_code = compile(fixPythonIndentation(code), label, 'exec') 831 param_list = string.join(params, ", ") 832 f = '''def defInst(_code, _context, %s): 833 my_locals = vars().copy() 834 exec _code in _context, my_locals 835 return my_locals\n''' % param_list 836 c = compile(f, label + ' wrapper', 'exec') 837 exec c 838 self.func = defInst 839 840 def defineInst(self, name, args, lineno): 841 context = {} 842 updateExportContext() 843 context.update(exportContext) 844 context.update({ 'name': name, 'Name': string.capitalize(name) }) 845 try: 846 vars = self.func(self.user_code, context, *args[0], **args[1]) 847 except Exception, exc: 848 error(lineno, 'error defining "%s": %s.' % (name, exc)) 849 for k in vars.keys(): 850 if k not in ('header_output', 'decoder_output', 851 'exec_output', 'decode_block'): 852 del vars[k] 853 return GenCode(**vars) 854 855# Special null format to catch an implicit-format instruction 856# definition outside of any format block. 857class NoFormat: 858 def __init__(self): 859 self.defaultInst = '' 860 861 def defineInst(self, name, args, lineno): 862 error(lineno, 863 'instruction definition "%s" with no active format!' % name) 864 865# This dictionary maps format name strings to Format objects. 866formatMap = {} 867 868# Define a new format 869def defFormat(id, params, code, lineno): 870 # make sure we haven't already defined this one 871 if formatMap.get(id, None) != None: 872 error(lineno, 'format %s redefined.' % id) 873 # create new object and store in global map 874 formatMap[id] = Format(id, params, code) 875 876 877############## 878# Stack: a simple stack object. Used for both formats (formatStack) 879# and default cases (defaultStack). Simply wraps a list to give more 880# stack-like syntax and enable initialization with an argument list 881# (as opposed to an argument that's a list). 882 883class Stack(list): 884 def __init__(self, *items): 885 list.__init__(self, items) 886 887 def push(self, item): 888 self.append(item); 889 890 def top(self): 891 return self[-1] 892 893# The global format stack. 894formatStack = Stack(NoFormat()) 895 896# The global default case stack. 897defaultStack = Stack( None ) 898 899# Global stack that tracks current file and line number. 900# Each element is a tuple (filename, lineno) that records the 901# *current* filename and the line number in the *previous* file where 902# it was included. 903fileNameStack = Stack() 904 905################### 906# Utility functions 907 908# 909# Indent every line in string 's' by two spaces 910# (except preprocessor directives). 911# Used to make nested code blocks look pretty. 912# 913def indent(s): 914 return re.sub(r'(?m)^(?!#)', ' ', s) 915 916# 917# Munge a somewhat arbitrarily formatted piece of Python code 918# (e.g. from a format 'let' block) into something whose indentation 919# will get by the Python parser. 920# 921# The two keys here are that Python will give a syntax error if 922# there's any whitespace at the beginning of the first line, and that 923# all lines at the same lexical nesting level must have identical 924# indentation. Unfortunately the way code literals work, an entire 925# let block tends to have some initial indentation. Rather than 926# trying to figure out what that is and strip it off, we prepend 'if 927# 1:' to make the let code the nested block inside the if (and have 928# the parser automatically deal with the indentation for us). 929# 930# We don't want to do this if (1) the code block is empty or (2) the 931# first line of the block doesn't have any whitespace at the front. 932 933def fixPythonIndentation(s): 934 # get rid of blank lines first 935 s = re.sub(r'(?m)^\s*\n', '', s); 936 if (s != '' and re.match(r'[ \t]', s[0])): 937 s = 'if 1:\n' + s 938 return s 939 940# Error handler. Just call exit. Output formatted to work under 941# Emacs compile-mode. Optional 'print_traceback' arg, if set to True, 942# prints a Python stack backtrace too (can be handy when trying to 943# debug the parser itself). 944def error(lineno, string, print_traceback = False): 945 spaces = "" 946 for (filename, line) in fileNameStack[0:-1]: 947 print spaces + "In file included from " + filename + ":" 948 spaces += " " 949 # Print a Python stack backtrace if requested. 950 if (print_traceback): 951 traceback.print_exc() 952 if lineno != 0: 953 line_str = "%d:" % lineno 954 else: 955 line_str = "" 956 sys.exit(spaces + "%s:%s %s" % (fileNameStack[-1][0], line_str, string)) 957 958 959##################################################################### 960# 961# Bitfield Operator Support 962# 963##################################################################### 964 965bitOp1ArgRE = re.compile(r'<\s*(\w+)\s*:\s*>') 966 967bitOpWordRE = re.compile(r'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>') 968bitOpExprRE = re.compile(r'\)<\s*(\w+)\s*:\s*(\w+)\s*>') 969 970def substBitOps(code): 971 # first convert single-bit selectors to two-index form 972 # i.e., <n> --> <n:n> 973 code = bitOp1ArgRE.sub(r'<\1:\1>', code) 974 # simple case: selector applied to ID (name) 975 # i.e., foo<a:b> --> bits(foo, a, b) 976 code = bitOpWordRE.sub(r'bits(\1, \2, \3)', code) 977 # if selector is applied to expression (ending in ')'), 978 # we need to search backward for matching '(' 979 match = bitOpExprRE.search(code) 980 while match: 981 exprEnd = match.start() 982 here = exprEnd - 1 983 nestLevel = 1 984 while nestLevel > 0: 985 if code[here] == '(': 986 nestLevel -= 1 987 elif code[here] == ')': 988 nestLevel += 1 989 here -= 1 990 if here < 0: 991 sys.exit("Didn't find '('!") 992 exprStart = here+1 993 newExpr = r'bits(%s, %s, %s)' % (code[exprStart:exprEnd+1], 994 match.group(1), match.group(2)) 995 code = code[:exprStart] + newExpr + code[match.end():] 996 match = bitOpExprRE.search(code) 997 return code 998 999 1000#################### 1001# Template objects. 1002# 1003# Template objects are format strings that allow substitution from 1004# the attribute spaces of other objects (e.g. InstObjParams instances). 1005 1006class Template: 1007 def __init__(self, t): 1008 self.template = t 1009 1010 def subst(self, d): 1011 # Start with the template namespace. Make a copy since we're 1012 # going to modify it. 1013 myDict = templateMap.copy() 1014 # if the argument is a dictionary, we just use it. 1015 if isinstance(d, dict): 1016 myDict.update(d) 1017 # if the argument is an object, we use its attribute map. 1018 elif hasattr(d, '__dict__'): 1019 myDict.update(d.__dict__) 1020 else: 1021 raise TypeError, "Template.subst() arg must be or have dictionary" 1022 # Protect non-Python-dict substitutions (e.g. if there's a printf 1023 # in the templated C++ code) 1024 template = protect_non_subst_percents(self.template) 1025 # CPU-model-specific substitutions are handled later (in GenCode). 1026 template = protect_cpu_symbols(template) 1027 return template % myDict 1028 1029 # Convert to string. This handles the case when a template with a 1030 # CPU-specific term gets interpolated into another template or into 1031 # an output block. 1032 def __str__(self): 1033 return expand_cpu_symbols_to_string(self.template) 1034 1035##################################################################### 1036# 1037# Code Parser 1038# 1039# The remaining code is the support for automatically extracting 1040# instruction characteristics from pseudocode. 1041# 1042##################################################################### 1043 1044# Force the argument to be a list. Useful for flags, where a caller 1045# can specify a singleton flag or a list of flags. Also usful for 1046# converting tuples to lists so they can be modified. 1047def makeList(arg): 1048 if isinstance(arg, list): 1049 return arg 1050 elif isinstance(arg, tuple): 1051 return list(arg) 1052 elif not arg: 1053 return [] 1054 else: 1055 return [ arg ] 1056 1057# Generate operandTypeMap from the user's 'def operand_types' 1058# statement. 1059def buildOperandTypeMap(userDict, lineno): 1060 global operandTypeMap 1061 operandTypeMap = {} 1062 for (ext, (desc, size)) in userDict.iteritems(): 1063 if desc == 'signed int': 1064 ctype = 'int%d_t' % size 1065 is_signed = 1 1066 elif desc == 'unsigned int': 1067 ctype = 'uint%d_t' % size 1068 is_signed = 0 1069 elif desc == 'float': 1070 is_signed = 1 # shouldn't really matter 1071 if size == 32: 1072 ctype = 'float' 1073 elif size == 64: 1074 ctype = 'double' 1075 if ctype == '': 1076 error(lineno, 'Unrecognized type description "%s" in userDict') 1077 operandTypeMap[ext] = (size, ctype, is_signed) 1078 1079# 1080# 1081# 1082# Base class for operand descriptors. An instance of this class (or 1083# actually a class derived from this one) represents a specific 1084# operand for a code block (e.g, "Rc.sq" as a dest). Intermediate 1085# derived classes encapsulates the traits of a particular operand type 1086# (e.g., "32-bit integer register"). 1087# 1088class Operand(object): 1089 def __init__(self, full_name, ext, is_src, is_dest): 1090 self.full_name = full_name 1091 self.ext = ext 1092 self.is_src = is_src 1093 self.is_dest = is_dest 1094 # The 'effective extension' (eff_ext) is either the actual 1095 # extension, if one was explicitly provided, or the default. 1096 if ext: 1097 self.eff_ext = ext 1098 else: 1099 self.eff_ext = self.dflt_ext 1100 1101 (self.size, self.ctype, self.is_signed) = operandTypeMap[self.eff_ext] 1102 1103 # note that mem_acc_size is undefined for non-mem operands... 1104 # template must be careful not to use it if it doesn't apply. 1105 if self.isMem(): 1106 self.mem_acc_size = self.makeAccSize() 1107 self.mem_acc_type = self.ctype 1108 1109 # Finalize additional fields (primarily code fields). This step 1110 # is done separately since some of these fields may depend on the 1111 # register index enumeration that hasn't been performed yet at the 1112 # time of __init__(). 1113 def finalize(self): 1114 self.flags = self.getFlags() 1115 self.constructor = self.makeConstructor() 1116 self.op_decl = self.makeDecl() 1117 1118 if self.is_src: 1119 self.op_rd = self.makeRead() 1120 self.op_src_decl = self.makeDecl() 1121 else: 1122 self.op_rd = '' 1123 self.op_src_decl = '' 1124 1125 if self.is_dest: 1126 self.op_wb = self.makeWrite() 1127 self.op_dest_decl = self.makeDecl() 1128 else: 1129 self.op_wb = '' 1130 self.op_dest_decl = '' 1131 1132 def isMem(self): 1133 return 0 1134 1135 def isReg(self): 1136 return 0 1137 1138 def isFloatReg(self): 1139 return 0 1140 1141 def isIntReg(self): 1142 return 0 1143 1144 def isControlReg(self): 1145 return 0 1146 1147 def getFlags(self): 1148 # note the empty slice '[:]' gives us a copy of self.flags[0] 1149 # instead of a reference to it 1150 my_flags = self.flags[0][:] 1151 if self.is_src: 1152 my_flags += self.flags[1] 1153 if self.is_dest: 1154 my_flags += self.flags[2] 1155 return my_flags 1156 1157 def makeDecl(self): 1158 # Note that initializations in the declarations are solely 1159 # to avoid 'uninitialized variable' errors from the compiler. 1160 return self.ctype + ' ' + self.base_name + ' = 0;\n'; 1161 1162class IntRegOperand(Operand): 1163 def isReg(self): 1164 return 1 1165 1166 def isIntReg(self): 1167 return 1 1168 1169 def makeConstructor(self): 1170 c = '' 1171 if self.is_src: 1172 c += '\n\t_srcRegIdx[%d] = %s;' % \ 1173 (self.src_reg_idx, self.reg_spec) 1174 if self.is_dest: 1175 c += '\n\t_destRegIdx[%d] = %s;' % \ 1176 (self.dest_reg_idx, self.reg_spec) 1177 return c 1178 1179 def makeRead(self): 1180 if (self.ctype == 'float' or self.ctype == 'double'): 1181 error(0, 'Attempt to read integer register as FP') 1182 if (self.size == self.dflt_size): 1183 return '%s = xc->readIntReg(this, %d);\n' % \ 1184 (self.base_name, self.src_reg_idx) 1185 elif (self.size > self.dflt_size): 1186 int_reg_val = 'xc->readIntReg(this, %d)' % (self.src_reg_idx) 1187 if (self.is_signed): 1188 int_reg_val = 'sext<%d>(%s)' % (self.dflt_size, int_reg_val) 1189 return '%s = %s;\n' % (self.base_name, int_reg_val) 1190 else: 1191 return '%s = bits(xc->readIntReg(this, %d), %d, 0);\n' % \ 1192 (self.base_name, self.src_reg_idx, self.size-1) 1193 1194 def makeWrite(self): 1195 if (self.ctype == 'float' or self.ctype == 'double'): 1196 error(0, 'Attempt to write integer register as FP') 1197 if (self.size != self.dflt_size and self.is_signed): 1198 final_val = 'sext<%d>(%s)' % (self.size, self.base_name) 1199 else: 1200 final_val = self.base_name 1201 wb = ''' 1202 { 1203 %s final_val = %s; 1204 xc->setIntReg(this, %d, final_val);\n 1205 if (traceData) { traceData->setData(final_val); } 1206 }''' % (self.dflt_ctype, final_val, self.dest_reg_idx) 1207 return wb 1208 1209class FloatRegOperand(Operand): 1210 def isReg(self): 1211 return 1 1212 1213 def isFloatReg(self): 1214 return 1 1215 1216 def makeConstructor(self): 1217 c = '' 1218 if self.is_src: 1219 c += '\n\t_srcRegIdx[%d] = %s + FP_Base_DepTag;' % \ 1220 (self.src_reg_idx, self.reg_spec) 1221 if self.is_dest: 1222 c += '\n\t_destRegIdx[%d] = %s + FP_Base_DepTag;' % \ 1223 (self.dest_reg_idx, self.reg_spec) 1224 return c 1225 1226 def makeRead(self): 1227 bit_select = 0 1228 width = 0; 1229 if (self.ctype == 'float'): 1230 func = 'readFloatReg' 1231 width = 32; 1232 elif (self.ctype == 'double'): 1233 func = 'readFloatReg' 1234 width = 64; 1235 else: 1236 func = 'readFloatRegBits' 1237 if (self.ctype == 'uint32_t'): 1238 width = 32; 1239 elif (self.ctype == 'uint64_t'): 1240 width = 64; 1241 if (self.size != self.dflt_size): 1242 bit_select = 1 1243 if width: 1244 base = 'xc->%s(this, %d, %d)' % \ 1245 (func, self.src_reg_idx, width) 1246 else: 1247 base = 'xc->%s(this, %d)' % \ 1248 (func, self.src_reg_idx) 1249 if bit_select: 1250 return '%s = bits(%s, %d, 0);\n' % \ 1251 (self.base_name, base, self.size-1) 1252 else: 1253 return '%s = %s;\n' % (self.base_name, base) 1254 1255 def makeWrite(self): 1256 final_val = self.base_name 1257 final_ctype = self.ctype 1258 widthSpecifier = '' 1259 width = 0 1260 if (self.ctype == 'float'): 1261 width = 32 1262 func = 'setFloatReg' 1263 elif (self.ctype == 'double'): 1264 width = 64 1265 func = 'setFloatReg' 1266 elif (self.ctype == 'uint32_t'): 1267 func = 'setFloatRegBits' 1268 width = 32 1269 elif (self.ctype == 'uint64_t'): 1270 func = 'setFloatRegBits' 1271 width = 64 1272 else: 1273 func = 'setFloatRegBits' 1274 final_ctype = 'uint%d_t' % self.dflt_size 1275 if (self.size != self.dflt_size and self.is_signed): 1276 final_val = 'sext<%d>(%s)' % (self.size, self.base_name) 1277 if width: 1278 widthSpecifier = ', %d' % width 1279 wb = ''' 1280 { 1281 %s final_val = %s; 1282 xc->%s(this, %d, final_val%s);\n 1283 if (traceData) { traceData->setData(final_val); } 1284 }''' % (final_ctype, final_val, func, self.dest_reg_idx, 1285 widthSpecifier) 1286 return wb 1287 1288class ControlRegOperand(Operand): 1289 def isReg(self): 1290 return 1 1291 1292 def isControlReg(self): 1293 return 1 1294 1295 def makeConstructor(self): 1296 c = '' 1297 if self.is_src: 1298 c += '\n\t_srcRegIdx[%d] = %s;' % \ 1299 (self.src_reg_idx, self.reg_spec) 1300 if self.is_dest: 1301 c += '\n\t_destRegIdx[%d] = %s;' % \ 1302 (self.dest_reg_idx, self.reg_spec) 1303 return c 1304 1305 def makeRead(self): 1306 bit_select = 0 1307 if (self.ctype == 'float' or self.ctype == 'double'): 1308 error(0, 'Attempt to read control register as FP') 1309 base = 'xc->readMiscRegWithEffect(%s)' % self.reg_spec 1310 if self.size == self.dflt_size: 1311 return '%s = %s;\n' % (self.base_name, base) 1312 else: 1313 return '%s = bits(%s, %d, 0);\n' % \ 1314 (self.base_name, base, self.size-1) 1315 1316 def makeWrite(self): 1317 if (self.ctype == 'float' or self.ctype == 'double'): 1318 error(0, 'Attempt to write control register as FP') 1319 wb = 'xc->setMiscRegWithEffect(%s, %s);\n' % (self.reg_spec, self.base_name) 1320 wb += 'if (traceData) { traceData->setData(%s); }' % \ 1321 self.base_name 1322 return wb 1323 1324class MemOperand(Operand): 1325 def isMem(self): 1326 return 1 1327 1328 def makeConstructor(self): 1329 return '' 1330 1331 def makeDecl(self): 1332 # Note that initializations in the declarations are solely 1333 # to avoid 'uninitialized variable' errors from the compiler. 1334 # Declare memory data variable. 1335 c = '%s %s = 0;\n' % (self.ctype, self.base_name) 1336 return c 1337 1338 def makeRead(self): 1339 return '' 1340 1341 def makeWrite(self): 1342 return '' 1343 1344 # Return the memory access size *in bits*, suitable for 1345 # forming a type via "uint%d_t". Divide by 8 if you want bytes. 1346 def makeAccSize(self): 1347 return self.size 1348 1349 1350class NPCOperand(Operand): 1351 def makeConstructor(self): 1352 return '' 1353 1354 def makeRead(self): 1355 return '%s = xc->readNextPC();\n' % self.base_name 1356 1357 def makeWrite(self): 1358 return 'xc->setNextPC(%s);\n' % self.base_name 1359 1360class NNPCOperand(Operand): 1361 def makeConstructor(self): 1362 return '' 1363 1364 def makeRead(self): 1365 return '%s = xc->readNextNPC();\n' % self.base_name 1366 1367 def makeWrite(self): 1368 return 'xc->setNextNPC(%s);\n' % self.base_name 1369 1370def buildOperandNameMap(userDict, lineno): 1371 global operandNameMap 1372 operandNameMap = {} 1373 for (op_name, val) in userDict.iteritems(): 1374 (base_cls_name, dflt_ext, reg_spec, flags, sort_pri) = val 1375 (dflt_size, dflt_ctype, dflt_is_signed) = operandTypeMap[dflt_ext] 1376 # Canonical flag structure is a triple of lists, where each list 1377 # indicates the set of flags implied by this operand always, when 1378 # used as a source, and when used as a dest, respectively. 1379 # For simplicity this can be initialized using a variety of fairly 1380 # obvious shortcuts; we convert these to canonical form here. 1381 if not flags: 1382 # no flags specified (e.g., 'None') 1383 flags = ( [], [], [] ) 1384 elif isinstance(flags, str): 1385 # a single flag: assumed to be unconditional 1386 flags = ( [ flags ], [], [] ) 1387 elif isinstance(flags, list): 1388 # a list of flags: also assumed to be unconditional 1389 flags = ( flags, [], [] ) 1390 elif isinstance(flags, tuple): 1391 # it's a tuple: it should be a triple, 1392 # but each item could be a single string or a list 1393 (uncond_flags, src_flags, dest_flags) = flags 1394 flags = (makeList(uncond_flags), 1395 makeList(src_flags), makeList(dest_flags)) 1396 # Accumulate attributes of new operand class in tmp_dict 1397 tmp_dict = {} 1398 for attr in ('dflt_ext', 'reg_spec', 'flags', 'sort_pri', 1399 'dflt_size', 'dflt_ctype', 'dflt_is_signed'): 1400 tmp_dict[attr] = eval(attr) 1401 tmp_dict['base_name'] = op_name 1402 # New class name will be e.g. "IntReg_Ra" 1403 cls_name = base_cls_name + '_' + op_name 1404 # Evaluate string arg to get class object. Note that the 1405 # actual base class for "IntReg" is "IntRegOperand", i.e. we 1406 # have to append "Operand". 1407 try: 1408 base_cls = eval(base_cls_name + 'Operand') 1409 except NameError: 1410 error(lineno, 1411 'error: unknown operand base class "%s"' % base_cls_name) 1412 # The following statement creates a new class called 1413 # <cls_name> as a subclass of <base_cls> with the attributes 1414 # in tmp_dict, just as if we evaluated a class declaration. 1415 operandNameMap[op_name] = type(cls_name, (base_cls,), tmp_dict) 1416 1417 # Define operand variables. 1418 operands = userDict.keys() 1419 1420 operandsREString = (r''' 1421 (?<![\w\.]) # neg. lookbehind assertion: prevent partial matches 1422 ((%s)(?:\.(\w+))?) # match: operand with optional '.' then suffix 1423 (?![\w\.]) # neg. lookahead assertion: prevent partial matches 1424 ''' 1425 % string.join(operands, '|')) 1426 1427 global operandsRE 1428 operandsRE = re.compile(operandsREString, re.MULTILINE|re.VERBOSE) 1429 1430 # Same as operandsREString, but extension is mandatory, and only two 1431 # groups are returned (base and ext, not full name as above). 1432 # Used for subtituting '_' for '.' to make C++ identifiers. 1433 operandsWithExtREString = (r'(?<![\w\.])(%s)\.(\w+)(?![\w\.])' 1434 % string.join(operands, '|')) 1435 1436 global operandsWithExtRE 1437 operandsWithExtRE = re.compile(operandsWithExtREString, re.MULTILINE) 1438 1439 1440class OperandList: 1441 1442 # Find all the operands in the given code block. Returns an operand 1443 # descriptor list (instance of class OperandList). 1444 def __init__(self, code): 1445 self.items = [] 1446 self.bases = {} 1447 # delete comments so we don't match on reg specifiers inside 1448 code = commentRE.sub('', code) 1449 # search for operands 1450 next_pos = 0 1451 while 1: 1452 match = operandsRE.search(code, next_pos) 1453 if not match: 1454 # no more matches: we're done 1455 break 1456 op = match.groups() 1457 # regexp groups are operand full name, base, and extension 1458 (op_full, op_base, op_ext) = op 1459 # if the token following the operand is an assignment, this is 1460 # a destination (LHS), else it's a source (RHS) 1461 is_dest = (assignRE.match(code, match.end()) != None) 1462 is_src = not is_dest 1463 # see if we've already seen this one 1464 op_desc = self.find_base(op_base) 1465 if op_desc: 1466 if op_desc.ext != op_ext: 1467 error(0, 'Inconsistent extensions for operand %s' % \ 1468 op_base) 1469 op_desc.is_src = op_desc.is_src or is_src 1470 op_desc.is_dest = op_desc.is_dest or is_dest 1471 else: 1472 # new operand: create new descriptor 1473 op_desc = operandNameMap[op_base](op_full, op_ext, 1474 is_src, is_dest) 1475 self.append(op_desc) 1476 # start next search after end of current match 1477 next_pos = match.end() 1478 self.sort() 1479 # enumerate source & dest register operands... used in building 1480 # constructor later 1481 self.numSrcRegs = 0 1482 self.numDestRegs = 0 1483 self.numFPDestRegs = 0 1484 self.numIntDestRegs = 0 1485 self.memOperand = None 1486 for op_desc in self.items: 1487 if op_desc.isReg(): 1488 if op_desc.is_src: 1489 op_desc.src_reg_idx = self.numSrcRegs 1490 self.numSrcRegs += 1 1491 if op_desc.is_dest: 1492 op_desc.dest_reg_idx = self.numDestRegs 1493 self.numDestRegs += 1 1494 if op_desc.isFloatReg(): 1495 self.numFPDestRegs += 1 1496 elif op_desc.isIntReg(): 1497 self.numIntDestRegs += 1 1498 elif op_desc.isMem(): 1499 if self.memOperand: 1500 error(0, "Code block has more than one memory operand.") 1501 self.memOperand = op_desc 1502 # now make a final pass to finalize op_desc fields that may depend 1503 # on the register enumeration 1504 for op_desc in self.items: 1505 op_desc.finalize() 1506 1507 def __len__(self): 1508 return len(self.items) 1509 1510 def __getitem__(self, index): 1511 return self.items[index] 1512 1513 def append(self, op_desc): 1514 self.items.append(op_desc) 1515 self.bases[op_desc.base_name] = op_desc 1516 1517 def find_base(self, base_name): 1518 # like self.bases[base_name], but returns None if not found 1519 # (rather than raising exception) 1520 return self.bases.get(base_name) 1521 1522 # internal helper function for concat[Some]Attr{Strings|Lists} 1523 def __internalConcatAttrs(self, attr_name, filter, result): 1524 for op_desc in self.items: 1525 if filter(op_desc): 1526 result += getattr(op_desc, attr_name) 1527 return result 1528 1529 # return a single string that is the concatenation of the (string) 1530 # values of the specified attribute for all operands 1531 def concatAttrStrings(self, attr_name): 1532 return self.__internalConcatAttrs(attr_name, lambda x: 1, '') 1533 1534 # like concatAttrStrings, but only include the values for the operands 1535 # for which the provided filter function returns true 1536 def concatSomeAttrStrings(self, filter, attr_name): 1537 return self.__internalConcatAttrs(attr_name, filter, '') 1538 1539 # return a single list that is the concatenation of the (list) 1540 # values of the specified attribute for all operands 1541 def concatAttrLists(self, attr_name): 1542 return self.__internalConcatAttrs(attr_name, lambda x: 1, []) 1543 1544 # like concatAttrLists, but only include the values for the operands 1545 # for which the provided filter function returns true 1546 def concatSomeAttrLists(self, filter, attr_name): 1547 return self.__internalConcatAttrs(attr_name, filter, []) 1548 1549 def sort(self): 1550 self.items.sort(lambda a, b: a.sort_pri - b.sort_pri) 1551 1552# Regular expression object to match C++ comments 1553# (used in findOperands()) 1554commentRE = re.compile(r'//.*\n') 1555 1556# Regular expression object to match assignment statements 1557# (used in findOperands()) 1558assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE) 1559 1560# Munge operand names in code string to make legal C++ variable names. 1561# This means getting rid of the type extension if any. 1562# (Will match base_name attribute of Operand object.) 1563def substMungedOpNames(code): 1564 return operandsWithExtRE.sub(r'\1', code) 1565 1566def joinLists(t): 1567 return map(string.join, t) 1568 1569def makeFlagConstructor(flag_list): 1570 if len(flag_list) == 0: 1571 return '' 1572 # filter out repeated flags 1573 flag_list.sort() 1574 i = 1 1575 while i < len(flag_list): 1576 if flag_list[i] == flag_list[i-1]: 1577 del flag_list[i] 1578 else: 1579 i += 1 1580 pre = '\n\tflags[' 1581 post = '] = true;' 1582 code = pre + string.join(flag_list, post + pre) + post 1583 return code 1584 1585class CodeBlock: 1586 def __init__(self, code): 1587 self.orig_code = code 1588 self.operands = OperandList(code) 1589 self.code = substMungedOpNames(substBitOps(code)) 1590 self.constructor = self.operands.concatAttrStrings('constructor') 1591 self.constructor += \ 1592 '\n\t_numSrcRegs = %d;' % self.operands.numSrcRegs 1593 self.constructor += \ 1594 '\n\t_numDestRegs = %d;' % self.operands.numDestRegs 1595 self.constructor += \ 1596 '\n\t_numFPDestRegs = %d;' % self.operands.numFPDestRegs 1597 self.constructor += \ 1598 '\n\t_numIntDestRegs = %d;' % self.operands.numIntDestRegs 1599 1600 self.op_decl = self.operands.concatAttrStrings('op_decl') 1601 1602 is_src = lambda op: op.is_src 1603 is_dest = lambda op: op.is_dest 1604 1605 self.op_src_decl = \ 1606 self.operands.concatSomeAttrStrings(is_src, 'op_src_decl') 1607 self.op_dest_decl = \ 1608 self.operands.concatSomeAttrStrings(is_dest, 'op_dest_decl') 1609 1610 self.op_rd = self.operands.concatAttrStrings('op_rd') 1611 self.op_wb = self.operands.concatAttrStrings('op_wb') 1612 1613 self.flags = self.operands.concatAttrLists('flags') 1614 1615 if self.operands.memOperand: 1616 self.mem_acc_size = self.operands.memOperand.mem_acc_size 1617 self.mem_acc_type = self.operands.memOperand.mem_acc_type 1618 1619 # Make a basic guess on the operand class (function unit type). 1620 # These are good enough for most cases, and will be overridden 1621 # later otherwise. 1622 if 'IsStore' in self.flags: 1623 self.op_class = 'MemWriteOp' 1624 elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags: 1625 self.op_class = 'MemReadOp' 1626 elif 'IsFloating' in self.flags: 1627 self.op_class = 'FloatAddOp' 1628 else: 1629 self.op_class = 'IntAluOp' 1630 1631# Assume all instruction flags are of the form 'IsFoo' 1632instFlagRE = re.compile(r'Is.*') 1633 1634# OpClass constants end in 'Op' except No_OpClass 1635opClassRE = re.compile(r'.*Op|No_OpClass') 1636 1637class InstObjParams: 1638 def __init__(self, mnem, class_name, base_class = '', 1639 code = None, opt_args = [], extras = {}): 1640 self.mnemonic = mnem 1641 self.class_name = class_name 1642 self.base_class = base_class 1643 if code: 1644 #If the user already made a CodeBlock, pick the parts from it 1645 if isinstance(code, CodeBlock): 1646 origCode = code.orig_code 1647 codeBlock = code 1648 else: 1649 origCode = code 1650 codeBlock = CodeBlock(code) 1651 stringExtras = {} 1652 otherExtras = {} 1653 for (k, v) in extras.items(): 1654 if type(v) == str: 1655 stringExtras[k] = v 1656 else: 1657 otherExtras[k] = v 1658 compositeCode = "\n".join([origCode] + stringExtras.values()) 1659 # compositeCode = '\n'.join([origCode] + 1660 # [pair[1] for pair in extras]) 1661 compositeBlock = CodeBlock(compositeCode) 1662 for code_attr in compositeBlock.__dict__.keys(): 1663 setattr(self, code_attr, getattr(compositeBlock, code_attr)) 1664 for (key, snippet) in stringExtras.items(): 1665 setattr(self, key, CodeBlock(snippet).code) 1666 for (key, item) in otherExtras.items(): 1667 setattr(self, key, item) 1668 self.code = codeBlock.code 1669 self.orig_code = origCode 1670 else: 1671 self.constructor = '' 1672 self.flags = [] 1673 # Optional arguments are assumed to be either StaticInst flags 1674 # or an OpClass value. To avoid having to import a complete 1675 # list of these values to match against, we do it ad-hoc 1676 # with regexps. 1677 for oa in opt_args: 1678 if instFlagRE.match(oa): 1679 self.flags.append(oa) 1680 elif opClassRE.match(oa): 1681 self.op_class = oa 1682 else: 1683 error(0, 'InstObjParams: optional arg "%s" not recognized ' 1684 'as StaticInst::Flag or OpClass.' % oa) 1685 1686 # add flag initialization to contructor here to include 1687 # any flags added via opt_args 1688 self.constructor += makeFlagConstructor(self.flags) 1689 1690 # if 'IsFloating' is set, add call to the FP enable check 1691 # function (which should be provided by isa_desc via a declare) 1692 if 'IsFloating' in self.flags: 1693 self.fp_enable_check = 'fault = checkFpEnableFault(xc);' 1694 else: 1695 self.fp_enable_check = '' 1696 1697####################### 1698# 1699# Output file template 1700# 1701 1702file_template = ''' 1703/* 1704 * DO NOT EDIT THIS FILE!!! 1705 * 1706 * It was automatically generated from the ISA description in %(filename)s 1707 */ 1708 1709%(includes)s 1710 1711%(global_output)s 1712 1713namespace %(namespace)s { 1714 1715%(namespace_output)s 1716 1717} // namespace %(namespace)s 1718 1719%(decode_function)s 1720''' 1721 1722 1723# Update the output file only if the new contents are different from 1724# the current contents. Minimizes the files that need to be rebuilt 1725# after minor changes. 1726def update_if_needed(file, contents): 1727 update = False 1728 if os.access(file, os.R_OK): 1729 f = open(file, 'r') 1730 old_contents = f.read() 1731 f.close() 1732 if contents != old_contents: 1733 print 'Updating', file 1734 os.remove(file) # in case it's write-protected 1735 update = True 1736 else: 1737 print 'File', file, 'is unchanged' 1738 else: 1739 print 'Generating', file 1740 update = True 1741 if update: 1742 f = open(file, 'w') 1743 f.write(contents) 1744 f.close() 1745 1746# This regular expression matches '##include' directives 1747includeRE = re.compile(r'^\s*##include\s+"(?P<filename>[\w/.-]*)".*$', 1748 re.MULTILINE) 1749 1750# Function to replace a matched '##include' directive with the 1751# contents of the specified file (with nested ##includes replaced 1752# recursively). 'matchobj' is an re match object (from a match of 1753# includeRE) and 'dirname' is the directory relative to which the file 1754# path should be resolved. 1755def replace_include(matchobj, dirname): 1756 fname = matchobj.group('filename') 1757 full_fname = os.path.normpath(os.path.join(dirname, fname)) 1758 contents = '##newfile "%s"\n%s\n##endfile\n' % \ 1759 (full_fname, read_and_flatten(full_fname)) 1760 return contents 1761 1762# Read a file and recursively flatten nested '##include' files. 1763def read_and_flatten(filename): 1764 current_dir = os.path.dirname(filename) 1765 try: 1766 contents = open(filename).read() 1767 except IOError: 1768 error(0, 'Error including file "%s"' % filename) 1769 fileNameStack.push((filename, 0)) 1770 # Find any includes and include them 1771 contents = includeRE.sub(lambda m: replace_include(m, current_dir), 1772 contents) 1773 fileNameStack.pop() 1774 return contents 1775 1776# 1777# Read in and parse the ISA description. 1778# 1779def parse_isa_desc(isa_desc_file, output_dir): 1780 # Read file and (recursively) all included files into a string. 1781 # PLY requires that the input be in a single string so we have to 1782 # do this up front. 1783 isa_desc = read_and_flatten(isa_desc_file) 1784 1785 # Initialize filename stack with outer file. 1786 fileNameStack.push((isa_desc_file, 0)) 1787 1788 # Parse it. 1789 (isa_name, namespace, global_code, namespace_code) = yacc.parse(isa_desc) 1790 1791 # grab the last three path components of isa_desc_file to put in 1792 # the output 1793 filename = '/'.join(isa_desc_file.split('/')[-3:]) 1794 1795 # generate decoder.hh 1796 includes = '#include "base/bitfield.hh" // for bitfield support' 1797 global_output = global_code.header_output 1798 namespace_output = namespace_code.header_output 1799 decode_function = '' 1800 update_if_needed(output_dir + '/decoder.hh', file_template % vars()) 1801 1802 # generate decoder.cc 1803 includes = '#include "decoder.hh"' 1804 global_output = global_code.decoder_output 1805 namespace_output = namespace_code.decoder_output 1806 # namespace_output += namespace_code.decode_block 1807 decode_function = namespace_code.decode_block 1808 update_if_needed(output_dir + '/decoder.cc', file_template % vars()) 1809 1810 # generate per-cpu exec files 1811 for cpu in cpu_models: 1812 includes = '#include "decoder.hh"\n' 1813 includes += cpu.includes 1814 global_output = global_code.exec_output[cpu.name] 1815 namespace_output = namespace_code.exec_output[cpu.name] 1816 decode_function = '' 1817 update_if_needed(output_dir + '/' + cpu.filename, 1818 file_template % vars()) 1819 1820# global list of CpuModel objects (see cpu_models.py) 1821cpu_models = [] 1822 1823# Called as script: get args from command line. 1824# Args are: <path to cpu_models.py> <isa desc file> <output dir> <cpu models> 1825if __name__ == '__main__': 1826 execfile(sys.argv[1]) # read in CpuModel definitions 1827 cpu_models = [CpuModel.dict[cpu] for cpu in sys.argv[4:]] 1828 parse_isa_desc(sys.argv[2], sys.argv[3]) 1829