isa_parser.py revision 10934:5af8f40d8f2c
1# Copyright (c) 2014 ARM Limited 2# All rights reserved 3# 4# The license below extends only to copyright in the software and shall 5# not be construed as granting a license to any other intellectual 6# property including but not limited to intellectual property relating 7# to a hardware implementation of the functionality of the software 8# licensed hereunder. You may use the software subject to the license 9# terms below provided that you ensure that this notice is replicated 10# unmodified and in its entirety in all distributions of the software, 11# modified or unmodified, in source code or in binary form. 12# 13# Copyright (c) 2003-2005 The Regents of The University of Michigan 14# Copyright (c) 2013 Advanced Micro Devices, Inc. 15# All rights reserved. 16# 17# Redistribution and use in source and binary forms, with or without 18# modification, are permitted provided that the following conditions are 19# met: redistributions of source code must retain the above copyright 20# notice, this list of conditions and the following disclaimer; 21# redistributions in binary form must reproduce the above copyright 22# notice, this list of conditions and the following disclaimer in the 23# documentation and/or other materials provided with the distribution; 24# neither the name of the copyright holders nor the names of its 25# contributors may be used to endorse or promote products derived from 26# this software without specific prior written permission. 27# 28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39# 40# Authors: Steve Reinhardt 41 42from __future__ import with_statement 43import os 44import sys 45import re 46import string 47import inspect, traceback 48# get type names 49from types import * 50 51from m5.util.grammar import Grammar 52 53debug=False 54 55################### 56# Utility functions 57 58# 59# Indent every line in string 's' by two spaces 60# (except preprocessor directives). 61# Used to make nested code blocks look pretty. 62# 63def indent(s): 64 return re.sub(r'(?m)^(?!#)', ' ', s) 65 66# 67# Munge a somewhat arbitrarily formatted piece of Python code 68# (e.g. from a format 'let' block) into something whose indentation 69# will get by the Python parser. 70# 71# The two keys here are that Python will give a syntax error if 72# there's any whitespace at the beginning of the first line, and that 73# all lines at the same lexical nesting level must have identical 74# indentation. Unfortunately the way code literals work, an entire 75# let block tends to have some initial indentation. Rather than 76# trying to figure out what that is and strip it off, we prepend 'if 77# 1:' to make the let code the nested block inside the if (and have 78# the parser automatically deal with the indentation for us). 79# 80# We don't want to do this if (1) the code block is empty or (2) the 81# first line of the block doesn't have any whitespace at the front. 82 83def fixPythonIndentation(s): 84 # get rid of blank lines first 85 s = re.sub(r'(?m)^\s*\n', '', s); 86 if (s != '' and re.match(r'[ \t]', s[0])): 87 s = 'if 1:\n' + s 88 return s 89 90class ISAParserError(Exception): 91 """Error handler for parser errors""" 92 def __init__(self, first, second=None): 93 if second is None: 94 self.lineno = 0 95 self.string = first 96 else: 97 if hasattr(first, 'lexer'): 98 first = first.lexer.lineno 99 self.lineno = first 100 self.string = second 101 102 def display(self, filename_stack, print_traceback=debug): 103 # Output formatted to work under Emacs compile-mode. Optional 104 # 'print_traceback' arg, if set to True, prints a Python stack 105 # backtrace too (can be handy when trying to debug the parser 106 # itself). 107 108 spaces = "" 109 for (filename, line) in filename_stack[:-1]: 110 print "%sIn file included from %s:" % (spaces, filename) 111 spaces += " " 112 113 # Print a Python stack backtrace if requested. 114 if print_traceback or not self.lineno: 115 traceback.print_exc() 116 117 line_str = "%s:" % (filename_stack[-1][0], ) 118 if self.lineno: 119 line_str += "%d:" % (self.lineno, ) 120 121 return "%s%s %s" % (spaces, line_str, self.string) 122 123 def exit(self, filename_stack, print_traceback=debug): 124 # Just call exit. 125 126 sys.exit(self.display(filename_stack, print_traceback)) 127 128def error(*args): 129 raise ISAParserError(*args) 130 131#################### 132# Template objects. 133# 134# Template objects are format strings that allow substitution from 135# the attribute spaces of other objects (e.g. InstObjParams instances). 136 137labelRE = re.compile(r'(?<!%)%\(([^\)]+)\)[sd]') 138 139class Template(object): 140 def __init__(self, parser, t): 141 self.parser = parser 142 self.template = t 143 144 def subst(self, d): 145 myDict = None 146 147 # Protect non-Python-dict substitutions (e.g. if there's a printf 148 # in the templated C++ code) 149 template = self.parser.protectNonSubstPercents(self.template) 150 # CPU-model-specific substitutions are handled later (in GenCode). 151 template = self.parser.protectCpuSymbols(template) 152 153 # Build a dict ('myDict') to use for the template substitution. 154 # Start with the template namespace. Make a copy since we're 155 # going to modify it. 156 myDict = self.parser.templateMap.copy() 157 158 if isinstance(d, InstObjParams): 159 # If we're dealing with an InstObjParams object, we need 160 # to be a little more sophisticated. The instruction-wide 161 # parameters are already formed, but the parameters which 162 # are only function wide still need to be generated. 163 compositeCode = '' 164 165 myDict.update(d.__dict__) 166 # The "operands" and "snippets" attributes of the InstObjParams 167 # objects are for internal use and not substitution. 168 del myDict['operands'] 169 del myDict['snippets'] 170 171 snippetLabels = [l for l in labelRE.findall(template) 172 if d.snippets.has_key(l)] 173 174 snippets = dict([(s, self.parser.mungeSnippet(d.snippets[s])) 175 for s in snippetLabels]) 176 177 myDict.update(snippets) 178 179 compositeCode = ' '.join(map(str, snippets.values())) 180 181 # Add in template itself in case it references any 182 # operands explicitly (like Mem) 183 compositeCode += ' ' + template 184 185 operands = SubOperandList(self.parser, compositeCode, d.operands) 186 187 myDict['op_decl'] = operands.concatAttrStrings('op_decl') 188 if operands.readPC or operands.setPC: 189 myDict['op_decl'] += 'TheISA::PCState __parserAutoPCState;\n' 190 191 # In case there are predicated register reads and write, declare 192 # the variables for register indicies. It is being assumed that 193 # all the operands in the OperandList are also in the 194 # SubOperandList and in the same order. Otherwise, it is 195 # expected that predication would not be used for the operands. 196 if operands.predRead: 197 myDict['op_decl'] += 'uint8_t _sourceIndex = 0;\n' 198 if operands.predWrite: 199 myDict['op_decl'] += 'uint8_t M5_VAR_USED _destIndex = 0;\n' 200 201 is_src = lambda op: op.is_src 202 is_dest = lambda op: op.is_dest 203 204 myDict['op_src_decl'] = \ 205 operands.concatSomeAttrStrings(is_src, 'op_src_decl') 206 myDict['op_dest_decl'] = \ 207 operands.concatSomeAttrStrings(is_dest, 'op_dest_decl') 208 if operands.readPC: 209 myDict['op_src_decl'] += \ 210 'TheISA::PCState __parserAutoPCState;\n' 211 if operands.setPC: 212 myDict['op_dest_decl'] += \ 213 'TheISA::PCState __parserAutoPCState;\n' 214 215 myDict['op_rd'] = operands.concatAttrStrings('op_rd') 216 if operands.readPC: 217 myDict['op_rd'] = '__parserAutoPCState = xc->pcState();\n' + \ 218 myDict['op_rd'] 219 220 # Compose the op_wb string. If we're going to write back the 221 # PC state because we changed some of its elements, we'll need to 222 # do that as early as possible. That allows later uncoordinated 223 # modifications to the PC to layer appropriately. 224 reordered = list(operands.items) 225 reordered.reverse() 226 op_wb_str = '' 227 pcWbStr = 'xc->pcState(__parserAutoPCState);\n' 228 for op_desc in reordered: 229 if op_desc.isPCPart() and op_desc.is_dest: 230 op_wb_str = op_desc.op_wb + pcWbStr + op_wb_str 231 pcWbStr = '' 232 else: 233 op_wb_str = op_desc.op_wb + op_wb_str 234 myDict['op_wb'] = op_wb_str 235 236 elif isinstance(d, dict): 237 # if the argument is a dictionary, we just use it. 238 myDict.update(d) 239 elif hasattr(d, '__dict__'): 240 # if the argument is an object, we use its attribute map. 241 myDict.update(d.__dict__) 242 else: 243 raise TypeError, "Template.subst() arg must be or have dictionary" 244 return template % myDict 245 246 # Convert to string. This handles the case when a template with a 247 # CPU-specific term gets interpolated into another template or into 248 # an output block. 249 def __str__(self): 250 return self.parser.expandCpuSymbolsToString(self.template) 251 252################ 253# Format object. 254# 255# A format object encapsulates an instruction format. It must provide 256# a defineInst() method that generates the code for an instruction 257# definition. 258 259class Format(object): 260 def __init__(self, id, params, code): 261 self.id = id 262 self.params = params 263 label = 'def format ' + id 264 self.user_code = compile(fixPythonIndentation(code), label, 'exec') 265 param_list = string.join(params, ", ") 266 f = '''def defInst(_code, _context, %s): 267 my_locals = vars().copy() 268 exec _code in _context, my_locals 269 return my_locals\n''' % param_list 270 c = compile(f, label + ' wrapper', 'exec') 271 exec c 272 self.func = defInst 273 274 def defineInst(self, parser, name, args, lineno): 275 parser.updateExportContext() 276 context = parser.exportContext.copy() 277 if len(name): 278 Name = name[0].upper() 279 if len(name) > 1: 280 Name += name[1:] 281 context.update({ 'name' : name, 'Name' : Name }) 282 try: 283 vars = self.func(self.user_code, context, *args[0], **args[1]) 284 except Exception, exc: 285 if debug: 286 raise 287 error(lineno, 'error defining "%s": %s.' % (name, exc)) 288 for k in vars.keys(): 289 if k not in ('header_output', 'decoder_output', 290 'exec_output', 'decode_block'): 291 del vars[k] 292 return GenCode(parser, **vars) 293 294# Special null format to catch an implicit-format instruction 295# definition outside of any format block. 296class NoFormat(object): 297 def __init__(self): 298 self.defaultInst = '' 299 300 def defineInst(self, parser, name, args, lineno): 301 error(lineno, 302 'instruction definition "%s" with no active format!' % name) 303 304############### 305# GenCode class 306# 307# The GenCode class encapsulates generated code destined for various 308# output files. The header_output and decoder_output attributes are 309# strings containing code destined for decoder.hh and decoder.cc 310# respectively. The decode_block attribute contains code to be 311# incorporated in the decode function itself (that will also end up in 312# decoder.cc). The exec_output attribute is a dictionary with a key 313# for each CPU model name; the value associated with a particular key 314# is the string of code for that CPU model's exec.cc file. The 315# has_decode_default attribute is used in the decode block to allow 316# explicit default clauses to override default default clauses. 317 318class GenCode(object): 319 # Constructor. At this point we substitute out all CPU-specific 320 # symbols. For the exec output, these go into the per-model 321 # dictionary. For all other output types they get collapsed into 322 # a single string. 323 def __init__(self, parser, 324 header_output = '', decoder_output = '', exec_output = '', 325 decode_block = '', has_decode_default = False): 326 self.parser = parser 327 self.header_output = parser.expandCpuSymbolsToString(header_output) 328 self.decoder_output = parser.expandCpuSymbolsToString(decoder_output) 329 self.exec_output = exec_output 330 self.decode_block = decode_block 331 self.has_decode_default = has_decode_default 332 333 # Write these code chunks out to the filesystem. They will be properly 334 # interwoven by the write_top_level_files(). 335 def emit(self): 336 if self.header_output: 337 self.parser.get_file('header').write(self.header_output) 338 if self.decoder_output: 339 self.parser.get_file('decoder').write(self.decoder_output) 340 if self.exec_output: 341 self.parser.get_file('exec').write(self.exec_output) 342 if self.decode_block: 343 self.parser.get_file('decode_block').write(self.decode_block) 344 345 # Override '+' operator: generate a new GenCode object that 346 # concatenates all the individual strings in the operands. 347 def __add__(self, other): 348 return GenCode(self.parser, 349 self.header_output + other.header_output, 350 self.decoder_output + other.decoder_output, 351 self.exec_output + other.exec_output, 352 self.decode_block + other.decode_block, 353 self.has_decode_default or other.has_decode_default) 354 355 # Prepend a string (typically a comment) to all the strings. 356 def prepend_all(self, pre): 357 self.header_output = pre + self.header_output 358 self.decoder_output = pre + self.decoder_output 359 self.decode_block = pre + self.decode_block 360 self.exec_output = pre + self.exec_output 361 362 # Wrap the decode block in a pair of strings (e.g., 'case foo:' 363 # and 'break;'). Used to build the big nested switch statement. 364 def wrap_decode_block(self, pre, post = ''): 365 self.decode_block = pre + indent(self.decode_block) + post 366 367##################################################################### 368# 369# Bitfield Operator Support 370# 371##################################################################### 372 373bitOp1ArgRE = re.compile(r'<\s*(\w+)\s*:\s*>') 374 375bitOpWordRE = re.compile(r'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>') 376bitOpExprRE = re.compile(r'\)<\s*(\w+)\s*:\s*(\w+)\s*>') 377 378def substBitOps(code): 379 # first convert single-bit selectors to two-index form 380 # i.e., <n> --> <n:n> 381 code = bitOp1ArgRE.sub(r'<\1:\1>', code) 382 # simple case: selector applied to ID (name) 383 # i.e., foo<a:b> --> bits(foo, a, b) 384 code = bitOpWordRE.sub(r'bits(\1, \2, \3)', code) 385 # if selector is applied to expression (ending in ')'), 386 # we need to search backward for matching '(' 387 match = bitOpExprRE.search(code) 388 while match: 389 exprEnd = match.start() 390 here = exprEnd - 1 391 nestLevel = 1 392 while nestLevel > 0: 393 if code[here] == '(': 394 nestLevel -= 1 395 elif code[here] == ')': 396 nestLevel += 1 397 here -= 1 398 if here < 0: 399 sys.exit("Didn't find '('!") 400 exprStart = here+1 401 newExpr = r'bits(%s, %s, %s)' % (code[exprStart:exprEnd+1], 402 match.group(1), match.group(2)) 403 code = code[:exprStart] + newExpr + code[match.end():] 404 match = bitOpExprRE.search(code) 405 return code 406 407 408##################################################################### 409# 410# Code Parser 411# 412# The remaining code is the support for automatically extracting 413# instruction characteristics from pseudocode. 414# 415##################################################################### 416 417# Force the argument to be a list. Useful for flags, where a caller 418# can specify a singleton flag or a list of flags. Also usful for 419# converting tuples to lists so they can be modified. 420def makeList(arg): 421 if isinstance(arg, list): 422 return arg 423 elif isinstance(arg, tuple): 424 return list(arg) 425 elif not arg: 426 return [] 427 else: 428 return [ arg ] 429 430class Operand(object): 431 '''Base class for operand descriptors. An instance of this class 432 (or actually a class derived from this one) represents a specific 433 operand for a code block (e.g, "Rc.sq" as a dest). Intermediate 434 derived classes encapsulates the traits of a particular operand 435 type (e.g., "32-bit integer register").''' 436 437 def buildReadCode(self, func = None): 438 subst_dict = {"name": self.base_name, 439 "func": func, 440 "reg_idx": self.reg_spec, 441 "ctype": self.ctype} 442 if hasattr(self, 'src_reg_idx'): 443 subst_dict['op_idx'] = self.src_reg_idx 444 code = self.read_code % subst_dict 445 return '%s = %s;\n' % (self.base_name, code) 446 447 def buildWriteCode(self, func = None): 448 subst_dict = {"name": self.base_name, 449 "func": func, 450 "reg_idx": self.reg_spec, 451 "ctype": self.ctype, 452 "final_val": self.base_name} 453 if hasattr(self, 'dest_reg_idx'): 454 subst_dict['op_idx'] = self.dest_reg_idx 455 code = self.write_code % subst_dict 456 return ''' 457 { 458 %s final_val = %s; 459 %s; 460 if (traceData) { traceData->setData(final_val); } 461 }''' % (self.dflt_ctype, self.base_name, code) 462 463 def __init__(self, parser, full_name, ext, is_src, is_dest): 464 self.full_name = full_name 465 self.ext = ext 466 self.is_src = is_src 467 self.is_dest = is_dest 468 # The 'effective extension' (eff_ext) is either the actual 469 # extension, if one was explicitly provided, or the default. 470 if ext: 471 self.eff_ext = ext 472 elif hasattr(self, 'dflt_ext'): 473 self.eff_ext = self.dflt_ext 474 475 if hasattr(self, 'eff_ext'): 476 self.ctype = parser.operandTypeMap[self.eff_ext] 477 478 # Finalize additional fields (primarily code fields). This step 479 # is done separately since some of these fields may depend on the 480 # register index enumeration that hasn't been performed yet at the 481 # time of __init__(). The register index enumeration is affected 482 # by predicated register reads/writes. Hence, we forward the flags 483 # that indicate whether or not predication is in use. 484 def finalize(self, predRead, predWrite): 485 self.flags = self.getFlags() 486 self.constructor = self.makeConstructor(predRead, predWrite) 487 self.op_decl = self.makeDecl() 488 489 if self.is_src: 490 self.op_rd = self.makeRead(predRead) 491 self.op_src_decl = self.makeDecl() 492 else: 493 self.op_rd = '' 494 self.op_src_decl = '' 495 496 if self.is_dest: 497 self.op_wb = self.makeWrite(predWrite) 498 self.op_dest_decl = self.makeDecl() 499 else: 500 self.op_wb = '' 501 self.op_dest_decl = '' 502 503 def isMem(self): 504 return 0 505 506 def isReg(self): 507 return 0 508 509 def isFloatReg(self): 510 return 0 511 512 def isIntReg(self): 513 return 0 514 515 def isCCReg(self): 516 return 0 517 518 def isVectorReg(self): 519 return 0 520 521 def isControlReg(self): 522 return 0 523 524 def isPCState(self): 525 return 0 526 527 def isPCPart(self): 528 return self.isPCState() and self.reg_spec 529 530 def hasReadPred(self): 531 return self.read_predicate != None 532 533 def hasWritePred(self): 534 return self.write_predicate != None 535 536 def getFlags(self): 537 # note the empty slice '[:]' gives us a copy of self.flags[0] 538 # instead of a reference to it 539 my_flags = self.flags[0][:] 540 if self.is_src: 541 my_flags += self.flags[1] 542 if self.is_dest: 543 my_flags += self.flags[2] 544 return my_flags 545 546 def makeDecl(self): 547 # Note that initializations in the declarations are solely 548 # to avoid 'uninitialized variable' errors from the compiler. 549 return self.ctype + ' ' + self.base_name + ' = 0;\n'; 550 551class IntRegOperand(Operand): 552 def isReg(self): 553 return 1 554 555 def isIntReg(self): 556 return 1 557 558 def makeConstructor(self, predRead, predWrite): 559 c_src = '' 560 c_dest = '' 561 562 if self.is_src: 563 c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s;' % (self.reg_spec) 564 if self.hasReadPred(): 565 c_src = '\n\tif (%s) {%s\n\t}' % \ 566 (self.read_predicate, c_src) 567 568 if self.is_dest: 569 c_dest = '\n\t_destRegIdx[_numDestRegs++] = %s;' % \ 570 (self.reg_spec) 571 c_dest += '\n\t_numIntDestRegs++;' 572 if self.hasWritePred(): 573 c_dest = '\n\tif (%s) {%s\n\t}' % \ 574 (self.write_predicate, c_dest) 575 576 return c_src + c_dest 577 578 def makeRead(self, predRead): 579 if (self.ctype == 'float' or self.ctype == 'double'): 580 error('Attempt to read integer register as FP') 581 if self.read_code != None: 582 return self.buildReadCode('readIntRegOperand') 583 584 int_reg_val = '' 585 if predRead: 586 int_reg_val = 'xc->readIntRegOperand(this, _sourceIndex++)' 587 if self.hasReadPred(): 588 int_reg_val = '(%s) ? %s : 0' % \ 589 (self.read_predicate, int_reg_val) 590 else: 591 int_reg_val = 'xc->readIntRegOperand(this, %d)' % self.src_reg_idx 592 593 return '%s = %s;\n' % (self.base_name, int_reg_val) 594 595 def makeWrite(self, predWrite): 596 if (self.ctype == 'float' or self.ctype == 'double'): 597 error('Attempt to write integer register as FP') 598 if self.write_code != None: 599 return self.buildWriteCode('setIntRegOperand') 600 601 if predWrite: 602 wp = 'true' 603 if self.hasWritePred(): 604 wp = self.write_predicate 605 606 wcond = 'if (%s)' % (wp) 607 windex = '_destIndex++' 608 else: 609 wcond = '' 610 windex = '%d' % self.dest_reg_idx 611 612 wb = ''' 613 %s 614 { 615 %s final_val = %s; 616 xc->setIntRegOperand(this, %s, final_val);\n 617 if (traceData) { traceData->setData(final_val); } 618 }''' % (wcond, self.ctype, self.base_name, windex) 619 620 return wb 621 622class FloatRegOperand(Operand): 623 def isReg(self): 624 return 1 625 626 def isFloatReg(self): 627 return 1 628 629 def makeConstructor(self, predRead, predWrite): 630 c_src = '' 631 c_dest = '' 632 633 if self.is_src: 634 c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s + FP_Reg_Base;' % \ 635 (self.reg_spec) 636 637 if self.is_dest: 638 c_dest = \ 639 '\n\t_destRegIdx[_numDestRegs++] = %s + FP_Reg_Base;' % \ 640 (self.reg_spec) 641 c_dest += '\n\t_numFPDestRegs++;' 642 643 return c_src + c_dest 644 645 def makeRead(self, predRead): 646 bit_select = 0 647 if (self.ctype == 'float' or self.ctype == 'double'): 648 func = 'readFloatRegOperand' 649 else: 650 func = 'readFloatRegOperandBits' 651 if self.read_code != None: 652 return self.buildReadCode(func) 653 654 if predRead: 655 rindex = '_sourceIndex++' 656 else: 657 rindex = '%d' % self.src_reg_idx 658 659 return '%s = xc->%s(this, %s);\n' % \ 660 (self.base_name, func, rindex) 661 662 def makeWrite(self, predWrite): 663 if (self.ctype == 'float' or self.ctype == 'double'): 664 func = 'setFloatRegOperand' 665 else: 666 func = 'setFloatRegOperandBits' 667 if self.write_code != None: 668 return self.buildWriteCode(func) 669 670 if predWrite: 671 wp = '_destIndex++' 672 else: 673 wp = '%d' % self.dest_reg_idx 674 wp = 'xc->%s(this, %s, final_val);' % (func, wp) 675 676 wb = ''' 677 { 678 %s final_val = %s; 679 %s\n 680 if (traceData) { traceData->setData(final_val); } 681 }''' % (self.ctype, self.base_name, wp) 682 return wb 683 684class CCRegOperand(Operand): 685 def isReg(self): 686 return 1 687 688 def isCCReg(self): 689 return 1 690 691 def makeConstructor(self, predRead, predWrite): 692 c_src = '' 693 c_dest = '' 694 695 if self.is_src: 696 c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s + CC_Reg_Base;' % \ 697 (self.reg_spec) 698 if self.hasReadPred(): 699 c_src = '\n\tif (%s) {%s\n\t}' % \ 700 (self.read_predicate, c_src) 701 702 if self.is_dest: 703 c_dest = \ 704 '\n\t_destRegIdx[_numDestRegs++] = %s + CC_Reg_Base;' % \ 705 (self.reg_spec) 706 c_dest += '\n\t_numCCDestRegs++;' 707 if self.hasWritePred(): 708 c_dest = '\n\tif (%s) {%s\n\t}' % \ 709 (self.write_predicate, c_dest) 710 711 return c_src + c_dest 712 713 def makeRead(self, predRead): 714 if (self.ctype == 'float' or self.ctype == 'double'): 715 error('Attempt to read condition-code register as FP') 716 if self.read_code != None: 717 return self.buildReadCode('readCCRegOperand') 718 719 int_reg_val = '' 720 if predRead: 721 int_reg_val = 'xc->readCCRegOperand(this, _sourceIndex++)' 722 if self.hasReadPred(): 723 int_reg_val = '(%s) ? %s : 0' % \ 724 (self.read_predicate, int_reg_val) 725 else: 726 int_reg_val = 'xc->readCCRegOperand(this, %d)' % self.src_reg_idx 727 728 return '%s = %s;\n' % (self.base_name, int_reg_val) 729 730 def makeWrite(self, predWrite): 731 if (self.ctype == 'float' or self.ctype == 'double'): 732 error('Attempt to write condition-code register as FP') 733 if self.write_code != None: 734 return self.buildWriteCode('setCCRegOperand') 735 736 if predWrite: 737 wp = 'true' 738 if self.hasWritePred(): 739 wp = self.write_predicate 740 741 wcond = 'if (%s)' % (wp) 742 windex = '_destIndex++' 743 else: 744 wcond = '' 745 windex = '%d' % self.dest_reg_idx 746 747 wb = ''' 748 %s 749 { 750 %s final_val = %s; 751 xc->setCCRegOperand(this, %s, final_val);\n 752 if (traceData) { traceData->setData(final_val); } 753 }''' % (wcond, self.ctype, self.base_name, windex) 754 755 return wb 756 757class VectorRegOperand(Operand): 758 def isReg(self): 759 return 1 760 761 def isVectorReg(self): 762 return 1 763 764 def __init__(self, parser, full_name, ext, is_src, is_dest): 765 ## Vector registers are always treated as source registers since 766 ## not the whole of them might be written, in which case we need 767 ## to retain the earlier value. 768 super(VectorRegOperand, self).__init__(parser, full_name, ext, 769 True, is_dest) 770 self.size = 0 771 772 def finalize(self, predRead, predWrite): 773 self.flags = self.getFlags() 774 self.constructor = self.makeConstructor(predRead, predWrite) 775 self.op_decl = self.makeDecl() 776 777 if self.is_src: 778 self.op_rd = self.makeRead(predRead) 779 self.op_src_decl = self.makeDecl() 780 else: 781 self.op_rd = '' 782 self.op_src_decl = '' 783 784 if self.is_dest: 785 self.op_wb = self.makeWrite(predWrite) 786 self.op_dest_decl = self.makeDecl() 787 else: 788 self.op_wb = '' 789 self.op_dest_decl = '' 790 791 def makeConstructor(self, predRead, predWrite): 792 c_src = '' 793 c_dest = '' 794 795 if self.is_src: 796 c_src = '\n\t_srcRegIdx[_numSrcRegs++] = %s + Vector_Reg_Base;' % \ 797 (self.reg_spec) 798 if self.hasReadPred(): 799 c_src = '\n\tif (%s) {%s\n\t}' % \ 800 (self.read_predicate, c_src) 801 802 if self.is_dest: 803 c_dest = '\n\t_destRegIdx[_numDestRegs++] = %s + Vector_Reg_Base;' % \ 804 (self.reg_spec) 805 c_dest += '\n\t_numVectorDestRegs++;' 806 if self.hasWritePred(): 807 c_dest = '\n\tif (%s) {%s\n\t}' % \ 808 (self.write_predicate, c_dest) 809 810 return c_src + c_dest 811 812 def makeRead(self, predRead): 813 if self.read_code != None: 814 return self.buildReadCode('readVectorRegOperand') 815 816 vector_reg_val = '' 817 if predRead: 818 vector_reg_val = 'xc->readVectorRegOperand(this, _sourceIndex++)' 819 if self.hasReadPred(): 820 vector_reg_val = '(%s) ? %s : 0' % \ 821 (self.read_predicate, vector_reg_val) 822 else: 823 vector_reg_val = 'xc->readVectorRegOperand(this, %d)' % \ 824 self.src_reg_idx 825 826 return '%s = %s;\n' % (self.base_name, vector_reg_val) 827 828 def makeWrite(self, predWrite): 829 if self.write_code != None: 830 return self.buildWriteCode('setVectorRegOperand') 831 832 if predWrite: 833 wp = 'true' 834 if self.hasWritePred(): 835 wp = self.write_predicate 836 837 wcond = 'if (%s)' % (wp) 838 windex = '_destIndex++' 839 else: 840 wcond = '' 841 windex = '%d' % self.dest_reg_idx 842 843 wb = ''' 844 %s 845 { 846 TheISA::VectorReg final_val = %s; 847 xc->setVectorRegOperand(this, %s, final_val);\n 848 if (traceData) { traceData->setData(final_val); } 849 }''' % (wcond, self.base_name, windex) 850 851 return wb 852 853 def makeDecl(self): 854 ctype = 'TheISA::VectorReg' 855 return '%s %s;\n' % (ctype, self.base_name) 856 857class ControlRegOperand(Operand): 858 def isReg(self): 859 return 1 860 861 def isControlReg(self): 862 return 1 863 864 def makeConstructor(self, predRead, predWrite): 865 c_src = '' 866 c_dest = '' 867 868 if self.is_src: 869 c_src = \ 870 '\n\t_srcRegIdx[_numSrcRegs++] = %s + Misc_Reg_Base;' % \ 871 (self.reg_spec) 872 873 if self.is_dest: 874 c_dest = \ 875 '\n\t_destRegIdx[_numDestRegs++] = %s + Misc_Reg_Base;' % \ 876 (self.reg_spec) 877 878 return c_src + c_dest 879 880 def makeRead(self, predRead): 881 bit_select = 0 882 if (self.ctype == 'float' or self.ctype == 'double'): 883 error('Attempt to read control register as FP') 884 if self.read_code != None: 885 return self.buildReadCode('readMiscRegOperand') 886 887 if predRead: 888 rindex = '_sourceIndex++' 889 else: 890 rindex = '%d' % self.src_reg_idx 891 892 return '%s = xc->readMiscRegOperand(this, %s);\n' % \ 893 (self.base_name, rindex) 894 895 def makeWrite(self, predWrite): 896 if (self.ctype == 'float' or self.ctype == 'double'): 897 error('Attempt to write control register as FP') 898 if self.write_code != None: 899 return self.buildWriteCode('setMiscRegOperand') 900 901 if predWrite: 902 windex = '_destIndex++' 903 else: 904 windex = '%d' % self.dest_reg_idx 905 906 wb = 'xc->setMiscRegOperand(this, %s, %s);\n' % \ 907 (windex, self.base_name) 908 wb += 'if (traceData) { traceData->setData(%s); }' % \ 909 self.base_name 910 911 return wb 912 913class MemOperand(Operand): 914 def isMem(self): 915 return 1 916 917 def makeConstructor(self, predRead, predWrite): 918 return '' 919 920 def makeDecl(self): 921 # Note that initializations in the declarations are solely 922 # to avoid 'uninitialized variable' errors from the compiler. 923 # Declare memory data variable. 924 if 'IsVector' in self.flags: 925 return 'TheISA::VectorReg %s;\n' % self.base_name 926 else: 927 return '%s %s = 0;\n' % (self.ctype, self.base_name) 928 929 def makeRead(self, predRead): 930 if self.read_code != None: 931 return self.buildReadCode() 932 return '' 933 934 def makeWrite(self, predWrite): 935 if self.write_code != None: 936 return self.buildWriteCode() 937 return '' 938 939class PCStateOperand(Operand): 940 def makeConstructor(self, predRead, predWrite): 941 return '' 942 943 def makeRead(self, predRead): 944 if self.reg_spec: 945 # A component of the PC state. 946 return '%s = __parserAutoPCState.%s();\n' % \ 947 (self.base_name, self.reg_spec) 948 else: 949 # The whole PC state itself. 950 return '%s = xc->pcState();\n' % self.base_name 951 952 def makeWrite(self, predWrite): 953 if self.reg_spec: 954 # A component of the PC state. 955 return '__parserAutoPCState.%s(%s);\n' % \ 956 (self.reg_spec, self.base_name) 957 else: 958 # The whole PC state itself. 959 return 'xc->pcState(%s);\n' % self.base_name 960 961 def makeDecl(self): 962 ctype = 'TheISA::PCState' 963 if self.isPCPart(): 964 ctype = self.ctype 965 # Note that initializations in the declarations are solely 966 # to avoid 'uninitialized variable' errors from the compiler. 967 return '%s %s = 0;\n' % (ctype, self.base_name) 968 969 def isPCState(self): 970 return 1 971 972class OperandList(object): 973 '''Find all the operands in the given code block. Returns an operand 974 descriptor list (instance of class OperandList).''' 975 def __init__(self, parser, code): 976 self.items = [] 977 self.bases = {} 978 # delete strings and comments so we don't match on operands inside 979 for regEx in (stringRE, commentRE): 980 code = regEx.sub('', code) 981 # search for operands 982 next_pos = 0 983 while 1: 984 match = parser.operandsRE.search(code, next_pos) 985 if not match: 986 # no more matches: we're done 987 break 988 op = match.groups() 989 # regexp groups are operand full name, base, and extension 990 (op_full, op_base, op_ext) = op 991 # if the token following the operand is an assignment, this is 992 # a destination (LHS), else it's a source (RHS) 993 is_dest = (assignRE.match(code, match.end()) != None) 994 is_src = not is_dest 995 # see if we've already seen this one 996 op_desc = self.find_base(op_base) 997 if op_desc: 998 if op_desc.ext != op_ext: 999 error('Inconsistent extensions for operand %s' % \ 1000 op_base) 1001 op_desc.is_src = op_desc.is_src or is_src 1002 op_desc.is_dest = op_desc.is_dest or is_dest 1003 else: 1004 # new operand: create new descriptor 1005 op_desc = parser.operandNameMap[op_base](parser, 1006 op_full, op_ext, is_src, is_dest) 1007 self.append(op_desc) 1008 # start next search after end of current match 1009 next_pos = match.end() 1010 self.sort() 1011 # enumerate source & dest register operands... used in building 1012 # constructor later 1013 self.numSrcRegs = 0 1014 self.numDestRegs = 0 1015 self.numFPDestRegs = 0 1016 self.numIntDestRegs = 0 1017 self.numCCDestRegs = 0 1018 self.numVectorDestRegs = 0 1019 self.numMiscDestRegs = 0 1020 self.memOperand = None 1021 1022 # Flags to keep track if one or more operands are to be read/written 1023 # conditionally. 1024 self.predRead = False 1025 self.predWrite = False 1026 1027 for op_desc in self.items: 1028 if op_desc.isReg(): 1029 if op_desc.is_src: 1030 op_desc.src_reg_idx = self.numSrcRegs 1031 self.numSrcRegs += 1 1032 if op_desc.is_dest: 1033 op_desc.dest_reg_idx = self.numDestRegs 1034 self.numDestRegs += 1 1035 if op_desc.isFloatReg(): 1036 self.numFPDestRegs += 1 1037 elif op_desc.isIntReg(): 1038 self.numIntDestRegs += 1 1039 elif op_desc.isCCReg(): 1040 self.numCCDestRegs += 1 1041 elif op_desc.isVectorReg(): 1042 self.numVectorDestRegs += 1 1043 elif op_desc.isControlReg(): 1044 self.numMiscDestRegs += 1 1045 elif op_desc.isMem(): 1046 if self.memOperand: 1047 error("Code block has more than one memory operand.") 1048 self.memOperand = op_desc 1049 1050 # Check if this operand has read/write predication. If true, then 1051 # the microop will dynamically index source/dest registers. 1052 self.predRead = self.predRead or op_desc.hasReadPred() 1053 self.predWrite = self.predWrite or op_desc.hasWritePred() 1054 1055 if parser.maxInstSrcRegs < self.numSrcRegs: 1056 parser.maxInstSrcRegs = self.numSrcRegs 1057 if parser.maxInstDestRegs < self.numDestRegs: 1058 parser.maxInstDestRegs = self.numDestRegs 1059 if parser.maxMiscDestRegs < self.numMiscDestRegs: 1060 parser.maxMiscDestRegs = self.numMiscDestRegs 1061 1062 # now make a final pass to finalize op_desc fields that may depend 1063 # on the register enumeration 1064 for op_desc in self.items: 1065 op_desc.finalize(self.predRead, self.predWrite) 1066 1067 def __len__(self): 1068 return len(self.items) 1069 1070 def __getitem__(self, index): 1071 return self.items[index] 1072 1073 def append(self, op_desc): 1074 self.items.append(op_desc) 1075 self.bases[op_desc.base_name] = op_desc 1076 1077 def find_base(self, base_name): 1078 # like self.bases[base_name], but returns None if not found 1079 # (rather than raising exception) 1080 return self.bases.get(base_name) 1081 1082 # internal helper function for concat[Some]Attr{Strings|Lists} 1083 def __internalConcatAttrs(self, attr_name, filter, result): 1084 for op_desc in self.items: 1085 if filter(op_desc): 1086 result += getattr(op_desc, attr_name) 1087 return result 1088 1089 # return a single string that is the concatenation of the (string) 1090 # values of the specified attribute for all operands 1091 def concatAttrStrings(self, attr_name): 1092 return self.__internalConcatAttrs(attr_name, lambda x: 1, '') 1093 1094 # like concatAttrStrings, but only include the values for the operands 1095 # for which the provided filter function returns true 1096 def concatSomeAttrStrings(self, filter, attr_name): 1097 return self.__internalConcatAttrs(attr_name, filter, '') 1098 1099 # return a single list that is the concatenation of the (list) 1100 # values of the specified attribute for all operands 1101 def concatAttrLists(self, attr_name): 1102 return self.__internalConcatAttrs(attr_name, lambda x: 1, []) 1103 1104 # like concatAttrLists, but only include the values for the operands 1105 # for which the provided filter function returns true 1106 def concatSomeAttrLists(self, filter, attr_name): 1107 return self.__internalConcatAttrs(attr_name, filter, []) 1108 1109 def sort(self): 1110 self.items.sort(lambda a, b: a.sort_pri - b.sort_pri) 1111 1112class SubOperandList(OperandList): 1113 '''Find all the operands in the given code block. Returns an operand 1114 descriptor list (instance of class OperandList).''' 1115 def __init__(self, parser, code, master_list): 1116 self.items = [] 1117 self.bases = {} 1118 # delete strings and comments so we don't match on operands inside 1119 for regEx in (stringRE, commentRE): 1120 code = regEx.sub('', code) 1121 # search for operands 1122 next_pos = 0 1123 while 1: 1124 match = parser.operandsRE.search(code, next_pos) 1125 if not match: 1126 # no more matches: we're done 1127 break 1128 op = match.groups() 1129 # regexp groups are operand full name, base, and extension 1130 (op_full, op_base, op_ext) = op 1131 # find this op in the master list 1132 op_desc = master_list.find_base(op_base) 1133 if not op_desc: 1134 error('Found operand %s which is not in the master list!' \ 1135 ' This is an internal error' % op_base) 1136 else: 1137 # See if we've already found this operand 1138 op_desc = self.find_base(op_base) 1139 if not op_desc: 1140 # if not, add a reference to it to this sub list 1141 self.append(master_list.bases[op_base]) 1142 1143 # start next search after end of current match 1144 next_pos = match.end() 1145 self.sort() 1146 self.memOperand = None 1147 # Whether the whole PC needs to be read so parts of it can be accessed 1148 self.readPC = False 1149 # Whether the whole PC needs to be written after parts of it were 1150 # changed 1151 self.setPC = False 1152 # Whether this instruction manipulates the whole PC or parts of it. 1153 # Mixing the two is a bad idea and flagged as an error. 1154 self.pcPart = None 1155 1156 # Flags to keep track if one or more operands are to be read/written 1157 # conditionally. 1158 self.predRead = False 1159 self.predWrite = False 1160 1161 for op_desc in self.items: 1162 if op_desc.isPCPart(): 1163 self.readPC = True 1164 if op_desc.is_dest: 1165 self.setPC = True 1166 1167 if op_desc.isPCState(): 1168 if self.pcPart is not None: 1169 if self.pcPart and not op_desc.isPCPart() or \ 1170 not self.pcPart and op_desc.isPCPart(): 1171 error("Mixed whole and partial PC state operands.") 1172 self.pcPart = op_desc.isPCPart() 1173 1174 if op_desc.isMem(): 1175 if self.memOperand: 1176 error("Code block has more than one memory operand.") 1177 self.memOperand = op_desc 1178 1179 # Check if this operand has read/write predication. If true, then 1180 # the microop will dynamically index source/dest registers. 1181 self.predRead = self.predRead or op_desc.hasReadPred() 1182 self.predWrite = self.predWrite or op_desc.hasWritePred() 1183 1184# Regular expression object to match C++ strings 1185stringRE = re.compile(r'"([^"\\]|\\.)*"') 1186 1187# Regular expression object to match C++ comments 1188# (used in findOperands()) 1189commentRE = re.compile(r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', 1190 re.DOTALL | re.MULTILINE) 1191 1192# Regular expression object to match assignment statements 1193# (used in findOperands()) 1194assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE) 1195 1196def makeFlagConstructor(flag_list): 1197 if len(flag_list) == 0: 1198 return '' 1199 # filter out repeated flags 1200 flag_list.sort() 1201 i = 1 1202 while i < len(flag_list): 1203 if flag_list[i] == flag_list[i-1]: 1204 del flag_list[i] 1205 else: 1206 i += 1 1207 pre = '\n\tflags[' 1208 post = '] = true;' 1209 code = pre + string.join(flag_list, post + pre) + post 1210 return code 1211 1212# Assume all instruction flags are of the form 'IsFoo' 1213instFlagRE = re.compile(r'Is.*') 1214 1215# OpClass constants end in 'Op' except No_OpClass 1216opClassRE = re.compile(r'.*Op|No_OpClass') 1217 1218class InstObjParams(object): 1219 def __init__(self, parser, mnem, class_name, base_class = '', 1220 snippets = {}, opt_args = []): 1221 self.mnemonic = mnem 1222 self.class_name = class_name 1223 self.base_class = base_class 1224 if not isinstance(snippets, dict): 1225 snippets = {'code' : snippets} 1226 compositeCode = ' '.join(map(str, snippets.values())) 1227 self.snippets = snippets 1228 1229 self.operands = OperandList(parser, compositeCode) 1230 1231 # The header of the constructor declares the variables to be used 1232 # in the body of the constructor. 1233 header = '' 1234 header += '\n\t_numSrcRegs = 0;' 1235 header += '\n\t_numDestRegs = 0;' 1236 header += '\n\t_numFPDestRegs = 0;' 1237 header += '\n\t_numIntDestRegs = 0;' 1238 header += '\n\t_numCCDestRegs = 0;' 1239 header += '\n\t_numVectorDestRegs = 0;' 1240 1241 self.constructor = header + \ 1242 self.operands.concatAttrStrings('constructor') 1243 1244 self.flags = self.operands.concatAttrLists('flags') 1245 1246 self.op_class = None 1247 1248 # Optional arguments are assumed to be either StaticInst flags 1249 # or an OpClass value. To avoid having to import a complete 1250 # list of these values to match against, we do it ad-hoc 1251 # with regexps. 1252 for oa in opt_args: 1253 if instFlagRE.match(oa): 1254 self.flags.append(oa) 1255 elif opClassRE.match(oa): 1256 self.op_class = oa 1257 else: 1258 error('InstObjParams: optional arg "%s" not recognized ' 1259 'as StaticInst::Flag or OpClass.' % oa) 1260 1261 # Make a basic guess on the operand class if not set. 1262 # These are good enough for most cases. 1263 if not self.op_class: 1264 if 'IsStore' in self.flags: 1265 self.op_class = 'MemWriteOp' 1266 elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags: 1267 self.op_class = 'MemReadOp' 1268 elif 'IsFloating' in self.flags: 1269 self.op_class = 'FloatAddOp' 1270 else: 1271 self.op_class = 'IntAluOp' 1272 1273 # add flag initialization to contructor here to include 1274 # any flags added via opt_args 1275 self.constructor += makeFlagConstructor(self.flags) 1276 1277 # if 'IsFloating' is set, add call to the FP enable check 1278 # function (which should be provided by isa_desc via a declare) 1279 if 'IsFloating' in self.flags: 1280 self.fp_enable_check = 'fault = checkFpEnableFault(xc);' 1281 else: 1282 self.fp_enable_check = '' 1283 1284############## 1285# Stack: a simple stack object. Used for both formats (formatStack) 1286# and default cases (defaultStack). Simply wraps a list to give more 1287# stack-like syntax and enable initialization with an argument list 1288# (as opposed to an argument that's a list). 1289 1290class Stack(list): 1291 def __init__(self, *items): 1292 list.__init__(self, items) 1293 1294 def push(self, item): 1295 self.append(item); 1296 1297 def top(self): 1298 return self[-1] 1299 1300####################### 1301# 1302# ISA Parser 1303# parses ISA DSL and emits C++ headers and source 1304# 1305 1306class ISAParser(Grammar): 1307 class CpuModel(object): 1308 def __init__(self, name, filename, includes, strings): 1309 self.name = name 1310 self.filename = filename 1311 self.includes = includes 1312 self.strings = strings 1313 1314 def __init__(self, output_dir): 1315 super(ISAParser, self).__init__() 1316 self.output_dir = output_dir 1317 1318 self.filename = None # for output file watermarking/scaremongering 1319 1320 self.cpuModels = [ 1321 ISAParser.CpuModel('ExecContext', 1322 'generic_cpu_exec.cc', 1323 '#include "cpu/exec_context.hh"', 1324 { "CPU_exec_context" : "ExecContext" }), 1325 ] 1326 1327 # variable to hold templates 1328 self.templateMap = {} 1329 1330 # This dictionary maps format name strings to Format objects. 1331 self.formatMap = {} 1332 1333 # Track open files and, if applicable, how many chunks it has been 1334 # split into so far. 1335 self.files = {} 1336 self.splits = {} 1337 1338 # isa_name / namespace identifier from namespace declaration. 1339 # before the namespace declaration, None. 1340 self.isa_name = None 1341 self.namespace = None 1342 1343 # The format stack. 1344 self.formatStack = Stack(NoFormat()) 1345 1346 # The default case stack. 1347 self.defaultStack = Stack(None) 1348 1349 # Stack that tracks current file and line number. Each 1350 # element is a tuple (filename, lineno) that records the 1351 # *current* filename and the line number in the *previous* 1352 # file where it was included. 1353 self.fileNameStack = Stack() 1354 1355 symbols = ('makeList', 're', 'string') 1356 self.exportContext = dict([(s, eval(s)) for s in symbols]) 1357 1358 self.maxInstSrcRegs = 0 1359 self.maxInstDestRegs = 0 1360 self.maxMiscDestRegs = 0 1361 1362 def __getitem__(self, i): # Allow object (self) to be 1363 return getattr(self, i) # passed to %-substitutions 1364 1365 # Change the file suffix of a base filename: 1366 # (e.g.) decoder.cc -> decoder-g.cc.inc for 'global' outputs 1367 def suffixize(self, s, sec): 1368 extn = re.compile('(\.[^\.]+)$') # isolate extension 1369 if self.namespace: 1370 return extn.sub(r'-ns\1.inc', s) # insert some text on either side 1371 else: 1372 return extn.sub(r'-g\1.inc', s) 1373 1374 # Get the file object for emitting code into the specified section 1375 # (header, decoder, exec, decode_block). 1376 def get_file(self, section): 1377 if section == 'decode_block': 1378 filename = 'decode-method.cc.inc' 1379 else: 1380 if section == 'header': 1381 file = 'decoder.hh' 1382 else: 1383 file = '%s.cc' % section 1384 filename = self.suffixize(file, section) 1385 try: 1386 return self.files[filename] 1387 except KeyError: pass 1388 1389 f = self.open(filename) 1390 self.files[filename] = f 1391 1392 # The splittable files are the ones with many independent 1393 # per-instruction functions - the decoder's instruction constructors 1394 # and the instruction execution (execute()) methods. These both have 1395 # the suffix -ns.cc.inc, meaning they are within the namespace part 1396 # of the ISA, contain object-emitting C++ source, and are included 1397 # into other top-level files. These are the files that need special 1398 # #define's to allow parts of them to be compiled separately. Rather 1399 # than splitting the emissions into separate files, the monolithic 1400 # output of the ISA parser is maintained, but the value (or lack 1401 # thereof) of the __SPLIT definition during C preprocessing will 1402 # select the different chunks. If no 'split' directives are used, 1403 # the cpp emissions have no effect. 1404 if re.search('-ns.cc.inc$', filename): 1405 print >>f, '#if !defined(__SPLIT) || (__SPLIT == 1)' 1406 self.splits[f] = 1 1407 # ensure requisite #include's 1408 elif filename in ['decoder-g.cc.inc', 'exec-g.cc.inc']: 1409 print >>f, '#include "decoder.hh"' 1410 elif filename == 'decoder-g.hh.inc': 1411 print >>f, '#include "base/bitfield.hh"' 1412 1413 return f 1414 1415 # Weave together the parts of the different output sections by 1416 # #include'ing them into some very short top-level .cc/.hh files. 1417 # These small files make it much clearer how this tool works, since 1418 # you directly see the chunks emitted as files that are #include'd. 1419 def write_top_level_files(self): 1420 dep = self.open('inc.d', bare=True) 1421 1422 # decoder header - everything depends on this 1423 file = 'decoder.hh' 1424 with self.open(file) as f: 1425 inc = [] 1426 1427 fn = 'decoder-g.hh.inc' 1428 assert(fn in self.files) 1429 f.write('#include "%s"\n' % fn) 1430 inc.append(fn) 1431 1432 fn = 'decoder-ns.hh.inc' 1433 assert(fn in self.files) 1434 f.write('namespace %s {\n#include "%s"\n}\n' 1435 % (self.namespace, fn)) 1436 inc.append(fn) 1437 1438 print >>dep, file+':', ' '.join(inc) 1439 1440 # decoder method - cannot be split 1441 file = 'decoder.cc' 1442 with self.open(file) as f: 1443 inc = [] 1444 1445 fn = 'decoder-g.cc.inc' 1446 assert(fn in self.files) 1447 f.write('#include "%s"\n' % fn) 1448 inc.append(fn) 1449 1450 fn = 'decode-method.cc.inc' 1451 # is guaranteed to have been written for parse to complete 1452 f.write('#include "%s"\n' % fn) 1453 inc.append(fn) 1454 1455 inc.append("decoder.hh") 1456 print >>dep, file+':', ' '.join(inc) 1457 1458 extn = re.compile('(\.[^\.]+)$') 1459 1460 # instruction constructors 1461 splits = self.splits[self.get_file('decoder')] 1462 file_ = 'inst-constrs.cc' 1463 for i in range(1, splits+1): 1464 if splits > 1: 1465 file = extn.sub(r'-%d\1' % i, file_) 1466 else: 1467 file = file_ 1468 with self.open(file) as f: 1469 inc = [] 1470 1471 fn = 'decoder-g.cc.inc' 1472 assert(fn in self.files) 1473 f.write('#include "%s"\n' % fn) 1474 inc.append(fn) 1475 1476 fn = 'decoder-ns.cc.inc' 1477 assert(fn in self.files) 1478 print >>f, 'namespace %s {' % self.namespace 1479 if splits > 1: 1480 print >>f, '#define __SPLIT %u' % i 1481 print >>f, '#include "%s"' % fn 1482 print >>f, '}' 1483 inc.append(fn) 1484 1485 inc.append("decoder.hh") 1486 print >>dep, file+':', ' '.join(inc) 1487 1488 # instruction execution per-CPU model 1489 splits = self.splits[self.get_file('exec')] 1490 for cpu in self.cpuModels: 1491 for i in range(1, splits+1): 1492 if splits > 1: 1493 file = extn.sub(r'_%d\1' % i, cpu.filename) 1494 else: 1495 file = cpu.filename 1496 with self.open(file) as f: 1497 inc = [] 1498 1499 fn = 'exec-g.cc.inc' 1500 assert(fn in self.files) 1501 f.write('#include "%s"\n' % fn) 1502 inc.append(fn) 1503 1504 f.write(cpu.includes+"\n") 1505 1506 fn = 'exec-ns.cc.inc' 1507 assert(fn in self.files) 1508 print >>f, 'namespace %s {' % self.namespace 1509 print >>f, '#define CPU_EXEC_CONTEXT %s' \ 1510 % cpu.strings['CPU_exec_context'] 1511 if splits > 1: 1512 print >>f, '#define __SPLIT %u' % i 1513 print >>f, '#include "%s"' % fn 1514 print >>f, '}' 1515 inc.append(fn) 1516 1517 inc.append("decoder.hh") 1518 print >>dep, file+':', ' '.join(inc) 1519 1520 # max_inst_regs.hh 1521 self.update('max_inst_regs.hh', 1522 '''namespace %(namespace)s { 1523 const int MaxInstSrcRegs = %(maxInstSrcRegs)d; 1524 const int MaxInstDestRegs = %(maxInstDestRegs)d; 1525 const int MaxMiscDestRegs = %(maxMiscDestRegs)d;\n}\n''' % self) 1526 print >>dep, 'max_inst_regs.hh:' 1527 1528 dep.close() 1529 1530 1531 scaremonger_template ='''// DO NOT EDIT 1532// This file was automatically generated from an ISA description: 1533// %(filename)s 1534 1535'''; 1536 1537 ##################################################################### 1538 # 1539 # Lexer 1540 # 1541 # The PLY lexer module takes two things as input: 1542 # - A list of token names (the string list 'tokens') 1543 # - A regular expression describing a match for each token. The 1544 # regexp for token FOO can be provided in two ways: 1545 # - as a string variable named t_FOO 1546 # - as the doc string for a function named t_FOO. In this case, 1547 # the function is also executed, allowing an action to be 1548 # associated with each token match. 1549 # 1550 ##################################################################### 1551 1552 # Reserved words. These are listed separately as they are matched 1553 # using the same regexp as generic IDs, but distinguished in the 1554 # t_ID() function. The PLY documentation suggests this approach. 1555 reserved = ( 1556 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT', 1557 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS', 1558 'OUTPUT', 'SIGNED', 'SPLIT', 'TEMPLATE' 1559 ) 1560 1561 # List of tokens. The lex module requires this. 1562 tokens = reserved + ( 1563 # identifier 1564 'ID', 1565 1566 # integer literal 1567 'INTLIT', 1568 1569 # string literal 1570 'STRLIT', 1571 1572 # code literal 1573 'CODELIT', 1574 1575 # ( ) [ ] { } < > , ; . : :: * 1576 'LPAREN', 'RPAREN', 1577 'LBRACKET', 'RBRACKET', 1578 'LBRACE', 'RBRACE', 1579 'LESS', 'GREATER', 'EQUALS', 1580 'COMMA', 'SEMI', 'DOT', 'COLON', 'DBLCOLON', 1581 'ASTERISK', 1582 1583 # C preprocessor directives 1584 'CPPDIRECTIVE' 1585 1586 # The following are matched but never returned. commented out to 1587 # suppress PLY warning 1588 # newfile directive 1589 # 'NEWFILE', 1590 1591 # endfile directive 1592 # 'ENDFILE' 1593 ) 1594 1595 # Regular expressions for token matching 1596 t_LPAREN = r'\(' 1597 t_RPAREN = r'\)' 1598 t_LBRACKET = r'\[' 1599 t_RBRACKET = r'\]' 1600 t_LBRACE = r'\{' 1601 t_RBRACE = r'\}' 1602 t_LESS = r'\<' 1603 t_GREATER = r'\>' 1604 t_EQUALS = r'=' 1605 t_COMMA = r',' 1606 t_SEMI = r';' 1607 t_DOT = r'\.' 1608 t_COLON = r':' 1609 t_DBLCOLON = r'::' 1610 t_ASTERISK = r'\*' 1611 1612 # Identifiers and reserved words 1613 reserved_map = { } 1614 for r in reserved: 1615 reserved_map[r.lower()] = r 1616 1617 def t_ID(self, t): 1618 r'[A-Za-z_]\w*' 1619 t.type = self.reserved_map.get(t.value, 'ID') 1620 return t 1621 1622 # Integer literal 1623 def t_INTLIT(self, t): 1624 r'-?(0x[\da-fA-F]+)|\d+' 1625 try: 1626 t.value = int(t.value,0) 1627 except ValueError: 1628 error(t, 'Integer value "%s" too large' % t.value) 1629 t.value = 0 1630 return t 1631 1632 # String literal. Note that these use only single quotes, and 1633 # can span multiple lines. 1634 def t_STRLIT(self, t): 1635 r"(?m)'([^'])+'" 1636 # strip off quotes 1637 t.value = t.value[1:-1] 1638 t.lexer.lineno += t.value.count('\n') 1639 return t 1640 1641 1642 # "Code literal"... like a string literal, but delimiters are 1643 # '{{' and '}}' so they get formatted nicely under emacs c-mode 1644 def t_CODELIT(self, t): 1645 r"(?m)\{\{([^\}]|}(?!\}))+\}\}" 1646 # strip off {{ & }} 1647 t.value = t.value[2:-2] 1648 t.lexer.lineno += t.value.count('\n') 1649 return t 1650 1651 def t_CPPDIRECTIVE(self, t): 1652 r'^\#[^\#].*\n' 1653 t.lexer.lineno += t.value.count('\n') 1654 return t 1655 1656 def t_NEWFILE(self, t): 1657 r'^\#\#newfile\s+"[^"]*"' 1658 self.fileNameStack.push((t.value[11:-1], t.lexer.lineno)) 1659 t.lexer.lineno = 0 1660 1661 def t_ENDFILE(self, t): 1662 r'^\#\#endfile' 1663 (old_filename, t.lexer.lineno) = self.fileNameStack.pop() 1664 1665 # 1666 # The functions t_NEWLINE, t_ignore, and t_error are 1667 # special for the lex module. 1668 # 1669 1670 # Newlines 1671 def t_NEWLINE(self, t): 1672 r'\n+' 1673 t.lexer.lineno += t.value.count('\n') 1674 1675 # Comments 1676 def t_comment(self, t): 1677 r'//.*' 1678 1679 # Completely ignored characters 1680 t_ignore = ' \t\x0c' 1681 1682 # Error handler 1683 def t_error(self, t): 1684 error(t, "illegal character '%s'" % t.value[0]) 1685 t.skip(1) 1686 1687 ##################################################################### 1688 # 1689 # Parser 1690 # 1691 # Every function whose name starts with 'p_' defines a grammar 1692 # rule. The rule is encoded in the function's doc string, while 1693 # the function body provides the action taken when the rule is 1694 # matched. The argument to each function is a list of the values 1695 # of the rule's symbols: t[0] for the LHS, and t[1..n] for the 1696 # symbols on the RHS. For tokens, the value is copied from the 1697 # t.value attribute provided by the lexer. For non-terminals, the 1698 # value is assigned by the producing rule; i.e., the job of the 1699 # grammar rule function is to set the value for the non-terminal 1700 # on the LHS (by assigning to t[0]). 1701 ##################################################################### 1702 1703 # The LHS of the first grammar rule is used as the start symbol 1704 # (in this case, 'specification'). Note that this rule enforces 1705 # that there will be exactly one namespace declaration, with 0 or 1706 # more global defs/decls before and after it. The defs & decls 1707 # before the namespace decl will be outside the namespace; those 1708 # after will be inside. The decoder function is always inside the 1709 # namespace. 1710 def p_specification(self, t): 1711 'specification : opt_defs_and_outputs top_level_decode_block' 1712 1713 for f in self.splits.iterkeys(): 1714 f.write('\n#endif\n') 1715 1716 for f in self.files.itervalues(): # close ALL the files; 1717 f.close() # not doing so can cause compilation to fail 1718 1719 self.write_top_level_files() 1720 1721 t[0] = True 1722 1723 # 'opt_defs_and_outputs' is a possibly empty sequence of def and/or 1724 # output statements. Its productions do the hard work of eventually 1725 # instantiating a GenCode, which are generally emitted (written to disk) 1726 # as soon as possible, except for the decode_block, which has to be 1727 # accumulated into one large function of nested switch/case blocks. 1728 def p_opt_defs_and_outputs_0(self, t): 1729 'opt_defs_and_outputs : empty' 1730 1731 def p_opt_defs_and_outputs_1(self, t): 1732 'opt_defs_and_outputs : defs_and_outputs' 1733 1734 def p_defs_and_outputs_0(self, t): 1735 'defs_and_outputs : def_or_output' 1736 1737 def p_defs_and_outputs_1(self, t): 1738 'defs_and_outputs : defs_and_outputs def_or_output' 1739 1740 # The list of possible definition/output statements. 1741 # They are all processed as they are seen. 1742 def p_def_or_output(self, t): 1743 '''def_or_output : name_decl 1744 | def_format 1745 | def_bitfield 1746 | def_bitfield_struct 1747 | def_template 1748 | def_operand_types 1749 | def_operands 1750 | output 1751 | global_let 1752 | split''' 1753 1754 # Utility function used by both invocations of splitting - explicit 1755 # 'split' keyword and split() function inside "let {{ }};" blocks. 1756 def split(self, sec, write=False): 1757 assert(sec != 'header' and "header cannot be split") 1758 1759 f = self.get_file(sec) 1760 self.splits[f] += 1 1761 s = '\n#endif\n#if __SPLIT == %u\n' % self.splits[f] 1762 if write: 1763 f.write(s) 1764 else: 1765 return s 1766 1767 # split output file to reduce compilation time 1768 def p_split(self, t): 1769 'split : SPLIT output_type SEMI' 1770 assert(self.isa_name and "'split' not allowed before namespace decl") 1771 1772 self.split(t[2], True) 1773 1774 def p_output_type(self, t): 1775 '''output_type : DECODER 1776 | HEADER 1777 | EXEC''' 1778 t[0] = t[1] 1779 1780 # ISA name declaration looks like "namespace <foo>;" 1781 def p_name_decl(self, t): 1782 'name_decl : NAMESPACE ID SEMI' 1783 assert(self.isa_name == None and "Only 1 namespace decl permitted") 1784 self.isa_name = t[2] 1785 self.namespace = t[2] + 'Inst' 1786 1787 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied 1788 # directly to the appropriate output section. 1789 1790 # Massage output block by substituting in template definitions and 1791 # bit operators. We handle '%'s embedded in the string that don't 1792 # indicate template substitutions (or CPU-specific symbols, which 1793 # get handled in GenCode) by doubling them first so that the 1794 # format operation will reduce them back to single '%'s. 1795 def process_output(self, s): 1796 s = self.protectNonSubstPercents(s) 1797 # protects cpu-specific symbols too 1798 s = self.protectCpuSymbols(s) 1799 return substBitOps(s % self.templateMap) 1800 1801 def p_output(self, t): 1802 'output : OUTPUT output_type CODELIT SEMI' 1803 kwargs = { t[2]+'_output' : self.process_output(t[3]) } 1804 GenCode(self, **kwargs).emit() 1805 1806 # global let blocks 'let {{...}}' (Python code blocks) are 1807 # executed directly when seen. Note that these execute in a 1808 # special variable context 'exportContext' to prevent the code 1809 # from polluting this script's namespace. 1810 def p_global_let(self, t): 1811 'global_let : LET CODELIT SEMI' 1812 def _split(sec): 1813 return self.split(sec) 1814 self.updateExportContext() 1815 self.exportContext["header_output"] = '' 1816 self.exportContext["decoder_output"] = '' 1817 self.exportContext["exec_output"] = '' 1818 self.exportContext["decode_block"] = '' 1819 self.exportContext["split"] = _split 1820 split_setup = ''' 1821def wrap(func): 1822 def split(sec): 1823 globals()[sec + '_output'] += func(sec) 1824 return split 1825split = wrap(split) 1826del wrap 1827''' 1828 # This tricky setup (immediately above) allows us to just write 1829 # (e.g.) "split('exec')" in the Python code and the split #ifdef's 1830 # will automatically be added to the exec_output variable. The inner 1831 # Python execution environment doesn't know about the split points, 1832 # so we carefully inject and wrap a closure that can retrieve the 1833 # next split's #define from the parser and add it to the current 1834 # emission-in-progress. 1835 try: 1836 exec split_setup+fixPythonIndentation(t[2]) in self.exportContext 1837 except Exception, exc: 1838 if debug: 1839 raise 1840 error(t, 'error: %s in global let block "%s".' % (exc, t[2])) 1841 GenCode(self, 1842 header_output=self.exportContext["header_output"], 1843 decoder_output=self.exportContext["decoder_output"], 1844 exec_output=self.exportContext["exec_output"], 1845 decode_block=self.exportContext["decode_block"]).emit() 1846 1847 # Define the mapping from operand type extensions to C++ types and 1848 # bit widths (stored in operandTypeMap). 1849 def p_def_operand_types(self, t): 1850 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI' 1851 try: 1852 self.operandTypeMap = eval('{' + t[3] + '}') 1853 except Exception, exc: 1854 if debug: 1855 raise 1856 error(t, 1857 'error: %s in def operand_types block "%s".' % (exc, t[3])) 1858 1859 # Define the mapping from operand names to operand classes and 1860 # other traits. Stored in operandNameMap. 1861 def p_def_operands(self, t): 1862 'def_operands : DEF OPERANDS CODELIT SEMI' 1863 if not hasattr(self, 'operandTypeMap'): 1864 error(t, 'error: operand types must be defined before operands') 1865 try: 1866 user_dict = eval('{' + t[3] + '}', self.exportContext) 1867 except Exception, exc: 1868 if debug: 1869 raise 1870 error(t, 'error: %s in def operands block "%s".' % (exc, t[3])) 1871 self.buildOperandNameMap(user_dict, t.lexer.lineno) 1872 1873 # A bitfield definition looks like: 1874 # 'def [signed] bitfield <ID> [<first>:<last>]' 1875 # This generates a preprocessor macro in the output file. 1876 def p_def_bitfield_0(self, t): 1877 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI' 1878 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[8]) 1879 if (t[2] == 'signed'): 1880 expr = 'sext<%d>(%s)' % (t[6] - t[8] + 1, expr) 1881 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr) 1882 GenCode(self, header_output=hash_define).emit() 1883 1884 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]' 1885 def p_def_bitfield_1(self, t): 1886 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI' 1887 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[6]) 1888 if (t[2] == 'signed'): 1889 expr = 'sext<%d>(%s)' % (1, expr) 1890 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr) 1891 GenCode(self, header_output=hash_define).emit() 1892 1893 # alternate form for structure member: 'def bitfield <ID> <ID>' 1894 def p_def_bitfield_struct(self, t): 1895 'def_bitfield_struct : DEF opt_signed BITFIELD ID id_with_dot SEMI' 1896 if (t[2] != ''): 1897 error(t, 'error: structure bitfields are always unsigned.') 1898 expr = 'machInst.%s' % t[5] 1899 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr) 1900 GenCode(self, header_output=hash_define).emit() 1901 1902 def p_id_with_dot_0(self, t): 1903 'id_with_dot : ID' 1904 t[0] = t[1] 1905 1906 def p_id_with_dot_1(self, t): 1907 'id_with_dot : ID DOT id_with_dot' 1908 t[0] = t[1] + t[2] + t[3] 1909 1910 def p_opt_signed_0(self, t): 1911 'opt_signed : SIGNED' 1912 t[0] = t[1] 1913 1914 def p_opt_signed_1(self, t): 1915 'opt_signed : empty' 1916 t[0] = '' 1917 1918 def p_def_template(self, t): 1919 'def_template : DEF TEMPLATE ID CODELIT SEMI' 1920 if t[3] in self.templateMap: 1921 print "warning: template %s already defined" % t[3] 1922 self.templateMap[t[3]] = Template(self, t[4]) 1923 1924 # An instruction format definition looks like 1925 # "def format <fmt>(<params>) {{...}};" 1926 def p_def_format(self, t): 1927 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI' 1928 (id, params, code) = (t[3], t[5], t[7]) 1929 self.defFormat(id, params, code, t.lexer.lineno) 1930 1931 # The formal parameter list for an instruction format is a 1932 # possibly empty list of comma-separated parameters. Positional 1933 # (standard, non-keyword) parameters must come first, followed by 1934 # keyword parameters, followed by a '*foo' parameter that gets 1935 # excess positional arguments (as in Python). Each of these three 1936 # parameter categories is optional. 1937 # 1938 # Note that we do not support the '**foo' parameter for collecting 1939 # otherwise undefined keyword args. Otherwise the parameter list 1940 # is (I believe) identical to what is supported in Python. 1941 # 1942 # The param list generates a tuple, where the first element is a 1943 # list of the positional params and the second element is a dict 1944 # containing the keyword params. 1945 def p_param_list_0(self, t): 1946 'param_list : positional_param_list COMMA nonpositional_param_list' 1947 t[0] = t[1] + t[3] 1948 1949 def p_param_list_1(self, t): 1950 '''param_list : positional_param_list 1951 | nonpositional_param_list''' 1952 t[0] = t[1] 1953 1954 def p_positional_param_list_0(self, t): 1955 'positional_param_list : empty' 1956 t[0] = [] 1957 1958 def p_positional_param_list_1(self, t): 1959 'positional_param_list : ID' 1960 t[0] = [t[1]] 1961 1962 def p_positional_param_list_2(self, t): 1963 'positional_param_list : positional_param_list COMMA ID' 1964 t[0] = t[1] + [t[3]] 1965 1966 def p_nonpositional_param_list_0(self, t): 1967 'nonpositional_param_list : keyword_param_list COMMA excess_args_param' 1968 t[0] = t[1] + t[3] 1969 1970 def p_nonpositional_param_list_1(self, t): 1971 '''nonpositional_param_list : keyword_param_list 1972 | excess_args_param''' 1973 t[0] = t[1] 1974 1975 def p_keyword_param_list_0(self, t): 1976 'keyword_param_list : keyword_param' 1977 t[0] = [t[1]] 1978 1979 def p_keyword_param_list_1(self, t): 1980 'keyword_param_list : keyword_param_list COMMA keyword_param' 1981 t[0] = t[1] + [t[3]] 1982 1983 def p_keyword_param(self, t): 1984 'keyword_param : ID EQUALS expr' 1985 t[0] = t[1] + ' = ' + t[3].__repr__() 1986 1987 def p_excess_args_param(self, t): 1988 'excess_args_param : ASTERISK ID' 1989 # Just concatenate them: '*ID'. Wrap in list to be consistent 1990 # with positional_param_list and keyword_param_list. 1991 t[0] = [t[1] + t[2]] 1992 1993 # End of format definition-related rules. 1994 ############## 1995 1996 # 1997 # A decode block looks like: 1998 # decode <field1> [, <field2>]* [default <inst>] { ... } 1999 # 2000 def p_top_level_decode_block(self, t): 2001 'top_level_decode_block : decode_block' 2002 codeObj = t[1] 2003 codeObj.wrap_decode_block(''' 2004StaticInstPtr 2005%(isa_name)s::Decoder::decodeInst(%(isa_name)s::ExtMachInst machInst) 2006{ 2007 using namespace %(namespace)s; 2008''' % self, '}') 2009 2010 codeObj.emit() 2011 2012 def p_decode_block(self, t): 2013 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE' 2014 default_defaults = self.defaultStack.pop() 2015 codeObj = t[5] 2016 # use the "default defaults" only if there was no explicit 2017 # default statement in decode_stmt_list 2018 if not codeObj.has_decode_default: 2019 codeObj += default_defaults 2020 codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n') 2021 t[0] = codeObj 2022 2023 # The opt_default statement serves only to push the "default 2024 # defaults" onto defaultStack. This value will be used by nested 2025 # decode blocks, and used and popped off when the current 2026 # decode_block is processed (in p_decode_block() above). 2027 def p_opt_default_0(self, t): 2028 'opt_default : empty' 2029 # no default specified: reuse the one currently at the top of 2030 # the stack 2031 self.defaultStack.push(self.defaultStack.top()) 2032 # no meaningful value returned 2033 t[0] = None 2034 2035 def p_opt_default_1(self, t): 2036 'opt_default : DEFAULT inst' 2037 # push the new default 2038 codeObj = t[2] 2039 codeObj.wrap_decode_block('\ndefault:\n', 'break;\n') 2040 self.defaultStack.push(codeObj) 2041 # no meaningful value returned 2042 t[0] = None 2043 2044 def p_decode_stmt_list_0(self, t): 2045 'decode_stmt_list : decode_stmt' 2046 t[0] = t[1] 2047 2048 def p_decode_stmt_list_1(self, t): 2049 'decode_stmt_list : decode_stmt decode_stmt_list' 2050 if (t[1].has_decode_default and t[2].has_decode_default): 2051 error(t, 'Two default cases in decode block') 2052 t[0] = t[1] + t[2] 2053 2054 # 2055 # Decode statement rules 2056 # 2057 # There are four types of statements allowed in a decode block: 2058 # 1. Format blocks 'format <foo> { ... }' 2059 # 2. Nested decode blocks 2060 # 3. Instruction definitions. 2061 # 4. C preprocessor directives. 2062 2063 2064 # Preprocessor directives found in a decode statement list are 2065 # passed through to the output, replicated to all of the output 2066 # code streams. This works well for ifdefs, so we can ifdef out 2067 # both the declarations and the decode cases generated by an 2068 # instruction definition. Handling them as part of the grammar 2069 # makes it easy to keep them in the right place with respect to 2070 # the code generated by the other statements. 2071 def p_decode_stmt_cpp(self, t): 2072 'decode_stmt : CPPDIRECTIVE' 2073 t[0] = GenCode(self, t[1], t[1], t[1], t[1]) 2074 2075 # A format block 'format <foo> { ... }' sets the default 2076 # instruction format used to handle instruction definitions inside 2077 # the block. This format can be overridden by using an explicit 2078 # format on the instruction definition or with a nested format 2079 # block. 2080 def p_decode_stmt_format(self, t): 2081 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE' 2082 # The format will be pushed on the stack when 'push_format_id' 2083 # is processed (see below). Once the parser has recognized 2084 # the full production (though the right brace), we're done 2085 # with the format, so now we can pop it. 2086 self.formatStack.pop() 2087 t[0] = t[4] 2088 2089 # This rule exists so we can set the current format (& push the 2090 # stack) when we recognize the format name part of the format 2091 # block. 2092 def p_push_format_id(self, t): 2093 'push_format_id : ID' 2094 try: 2095 self.formatStack.push(self.formatMap[t[1]]) 2096 t[0] = ('', '// format %s' % t[1]) 2097 except KeyError: 2098 error(t, 'instruction format "%s" not defined.' % t[1]) 2099 2100 # Nested decode block: if the value of the current field matches 2101 # the specified constant(s), do a nested decode on some other field. 2102 def p_decode_stmt_decode(self, t): 2103 'decode_stmt : case_list COLON decode_block' 2104 case_list = t[1] 2105 codeObj = t[3] 2106 # just wrap the decoding code from the block as a case in the 2107 # outer switch statement. 2108 codeObj.wrap_decode_block('\n%s\n' % ''.join(case_list)) 2109 codeObj.has_decode_default = (case_list == ['default:']) 2110 t[0] = codeObj 2111 2112 # Instruction definition (finally!). 2113 def p_decode_stmt_inst(self, t): 2114 'decode_stmt : case_list COLON inst SEMI' 2115 case_list = t[1] 2116 codeObj = t[3] 2117 codeObj.wrap_decode_block('\n%s' % ''.join(case_list), 'break;\n') 2118 codeObj.has_decode_default = (case_list == ['default:']) 2119 t[0] = codeObj 2120 2121 # The constant list for a decode case label must be non-empty, and must 2122 # either be the keyword 'default', or made up of one or more 2123 # comma-separated integer literals or strings which evaluate to 2124 # constants when compiled as C++. 2125 def p_case_list_0(self, t): 2126 'case_list : DEFAULT' 2127 t[0] = ['default:'] 2128 2129 def prep_int_lit_case_label(self, lit): 2130 if lit >= 2**32: 2131 return 'case ULL(%#x): ' % lit 2132 else: 2133 return 'case %#x: ' % lit 2134 2135 def prep_str_lit_case_label(self, lit): 2136 return 'case %s: ' % lit 2137 2138 def p_case_list_1(self, t): 2139 'case_list : INTLIT' 2140 t[0] = [self.prep_int_lit_case_label(t[1])] 2141 2142 def p_case_list_2(self, t): 2143 'case_list : STRLIT' 2144 t[0] = [self.prep_str_lit_case_label(t[1])] 2145 2146 def p_case_list_3(self, t): 2147 'case_list : case_list COMMA INTLIT' 2148 t[0] = t[1] 2149 t[0].append(self.prep_int_lit_case_label(t[3])) 2150 2151 def p_case_list_4(self, t): 2152 'case_list : case_list COMMA STRLIT' 2153 t[0] = t[1] 2154 t[0].append(self.prep_str_lit_case_label(t[3])) 2155 2156 # Define an instruction using the current instruction format 2157 # (specified by an enclosing format block). 2158 # "<mnemonic>(<args>)" 2159 def p_inst_0(self, t): 2160 'inst : ID LPAREN arg_list RPAREN' 2161 # Pass the ID and arg list to the current format class to deal with. 2162 currentFormat = self.formatStack.top() 2163 codeObj = currentFormat.defineInst(self, t[1], t[3], t.lexer.lineno) 2164 args = ','.join(map(str, t[3])) 2165 args = re.sub('(?m)^', '//', args) 2166 args = re.sub('^//', '', args) 2167 comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args) 2168 codeObj.prepend_all(comment) 2169 t[0] = codeObj 2170 2171 # Define an instruction using an explicitly specified format: 2172 # "<fmt>::<mnemonic>(<args>)" 2173 def p_inst_1(self, t): 2174 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN' 2175 try: 2176 format = self.formatMap[t[1]] 2177 except KeyError: 2178 error(t, 'instruction format "%s" not defined.' % t[1]) 2179 2180 codeObj = format.defineInst(self, t[3], t[5], t.lexer.lineno) 2181 comment = '\n// %s::%s(%s)\n' % (t[1], t[3], t[5]) 2182 codeObj.prepend_all(comment) 2183 t[0] = codeObj 2184 2185 # The arg list generates a tuple, where the first element is a 2186 # list of the positional args and the second element is a dict 2187 # containing the keyword args. 2188 def p_arg_list_0(self, t): 2189 'arg_list : positional_arg_list COMMA keyword_arg_list' 2190 t[0] = ( t[1], t[3] ) 2191 2192 def p_arg_list_1(self, t): 2193 'arg_list : positional_arg_list' 2194 t[0] = ( t[1], {} ) 2195 2196 def p_arg_list_2(self, t): 2197 'arg_list : keyword_arg_list' 2198 t[0] = ( [], t[1] ) 2199 2200 def p_positional_arg_list_0(self, t): 2201 'positional_arg_list : empty' 2202 t[0] = [] 2203 2204 def p_positional_arg_list_1(self, t): 2205 'positional_arg_list : expr' 2206 t[0] = [t[1]] 2207 2208 def p_positional_arg_list_2(self, t): 2209 'positional_arg_list : positional_arg_list COMMA expr' 2210 t[0] = t[1] + [t[3]] 2211 2212 def p_keyword_arg_list_0(self, t): 2213 'keyword_arg_list : keyword_arg' 2214 t[0] = t[1] 2215 2216 def p_keyword_arg_list_1(self, t): 2217 'keyword_arg_list : keyword_arg_list COMMA keyword_arg' 2218 t[0] = t[1] 2219 t[0].update(t[3]) 2220 2221 def p_keyword_arg(self, t): 2222 'keyword_arg : ID EQUALS expr' 2223 t[0] = { t[1] : t[3] } 2224 2225 # 2226 # Basic expressions. These constitute the argument values of 2227 # "function calls" (i.e. instruction definitions in the decode 2228 # block) and default values for formal parameters of format 2229 # functions. 2230 # 2231 # Right now, these are either strings, integers, or (recursively) 2232 # lists of exprs (using Python square-bracket list syntax). Note 2233 # that bare identifiers are trated as string constants here (since 2234 # there isn't really a variable namespace to refer to). 2235 # 2236 def p_expr_0(self, t): 2237 '''expr : ID 2238 | INTLIT 2239 | STRLIT 2240 | CODELIT''' 2241 t[0] = t[1] 2242 2243 def p_expr_1(self, t): 2244 '''expr : LBRACKET list_expr RBRACKET''' 2245 t[0] = t[2] 2246 2247 def p_list_expr_0(self, t): 2248 'list_expr : expr' 2249 t[0] = [t[1]] 2250 2251 def p_list_expr_1(self, t): 2252 'list_expr : list_expr COMMA expr' 2253 t[0] = t[1] + [t[3]] 2254 2255 def p_list_expr_2(self, t): 2256 'list_expr : empty' 2257 t[0] = [] 2258 2259 # 2260 # Empty production... use in other rules for readability. 2261 # 2262 def p_empty(self, t): 2263 'empty :' 2264 pass 2265 2266 # Parse error handler. Note that the argument here is the 2267 # offending *token*, not a grammar symbol (hence the need to use 2268 # t.value) 2269 def p_error(self, t): 2270 if t: 2271 error(t, "syntax error at '%s'" % t.value) 2272 else: 2273 error("unknown syntax error") 2274 2275 # END OF GRAMMAR RULES 2276 2277 def updateExportContext(self): 2278 2279 # create a continuation that allows us to grab the current parser 2280 def wrapInstObjParams(*args): 2281 return InstObjParams(self, *args) 2282 self.exportContext['InstObjParams'] = wrapInstObjParams 2283 self.exportContext.update(self.templateMap) 2284 2285 def defFormat(self, id, params, code, lineno): 2286 '''Define a new format''' 2287 2288 # make sure we haven't already defined this one 2289 if id in self.formatMap: 2290 error(lineno, 'format %s redefined.' % id) 2291 2292 # create new object and store in global map 2293 self.formatMap[id] = Format(id, params, code) 2294 2295 def expandCpuSymbolsToDict(self, template): 2296 '''Expand template with CPU-specific references into a 2297 dictionary with an entry for each CPU model name. The entry 2298 key is the model name and the corresponding value is the 2299 template with the CPU-specific refs substituted for that 2300 model.''' 2301 2302 # Protect '%'s that don't go with CPU-specific terms 2303 t = re.sub(r'%(?!\(CPU_)', '%%', template) 2304 result = {} 2305 for cpu in self.cpuModels: 2306 result[cpu.name] = t % cpu.strings 2307 return result 2308 2309 def expandCpuSymbolsToString(self, template): 2310 '''*If* the template has CPU-specific references, return a 2311 single string containing a copy of the template for each CPU 2312 model with the corresponding values substituted in. If the 2313 template has no CPU-specific references, it is returned 2314 unmodified.''' 2315 2316 if template.find('%(CPU_') != -1: 2317 return reduce(lambda x,y: x+y, 2318 self.expandCpuSymbolsToDict(template).values()) 2319 else: 2320 return template 2321 2322 def protectCpuSymbols(self, template): 2323 '''Protect CPU-specific references by doubling the 2324 corresponding '%'s (in preparation for substituting a different 2325 set of references into the template).''' 2326 2327 return re.sub(r'%(?=\(CPU_)', '%%', template) 2328 2329 def protectNonSubstPercents(self, s): 2330 '''Protect any non-dict-substitution '%'s in a format string 2331 (i.e. those not followed by '(')''' 2332 2333 return re.sub(r'%(?!\()', '%%', s) 2334 2335 def buildOperandNameMap(self, user_dict, lineno): 2336 operand_name = {} 2337 for op_name, val in user_dict.iteritems(): 2338 2339 # Check if extra attributes have been specified. 2340 if len(val) > 9: 2341 error(lineno, 'error: too many attributes for operand "%s"' % 2342 base_cls_name) 2343 2344 # Pad val with None in case optional args are missing 2345 val += (None, None, None, None) 2346 base_cls_name, dflt_ext, reg_spec, flags, sort_pri, \ 2347 read_code, write_code, read_predicate, write_predicate = val[:9] 2348 2349 # Canonical flag structure is a triple of lists, where each list 2350 # indicates the set of flags implied by this operand always, when 2351 # used as a source, and when used as a dest, respectively. 2352 # For simplicity this can be initialized using a variety of fairly 2353 # obvious shortcuts; we convert these to canonical form here. 2354 if not flags: 2355 # no flags specified (e.g., 'None') 2356 flags = ( [], [], [] ) 2357 elif isinstance(flags, str): 2358 # a single flag: assumed to be unconditional 2359 flags = ( [ flags ], [], [] ) 2360 elif isinstance(flags, list): 2361 # a list of flags: also assumed to be unconditional 2362 flags = ( flags, [], [] ) 2363 elif isinstance(flags, tuple): 2364 # it's a tuple: it should be a triple, 2365 # but each item could be a single string or a list 2366 (uncond_flags, src_flags, dest_flags) = flags 2367 flags = (makeList(uncond_flags), 2368 makeList(src_flags), makeList(dest_flags)) 2369 2370 # Accumulate attributes of new operand class in tmp_dict 2371 tmp_dict = {} 2372 attrList = ['reg_spec', 'flags', 'sort_pri', 2373 'read_code', 'write_code', 2374 'read_predicate', 'write_predicate'] 2375 if dflt_ext: 2376 dflt_ctype = self.operandTypeMap[dflt_ext] 2377 attrList.extend(['dflt_ctype', 'dflt_ext']) 2378 for attr in attrList: 2379 tmp_dict[attr] = eval(attr) 2380 tmp_dict['base_name'] = op_name 2381 2382 # New class name will be e.g. "IntReg_Ra" 2383 cls_name = base_cls_name + '_' + op_name 2384 # Evaluate string arg to get class object. Note that the 2385 # actual base class for "IntReg" is "IntRegOperand", i.e. we 2386 # have to append "Operand". 2387 try: 2388 base_cls = eval(base_cls_name + 'Operand') 2389 except NameError: 2390 error(lineno, 2391 'error: unknown operand base class "%s"' % base_cls_name) 2392 # The following statement creates a new class called 2393 # <cls_name> as a subclass of <base_cls> with the attributes 2394 # in tmp_dict, just as if we evaluated a class declaration. 2395 operand_name[op_name] = type(cls_name, (base_cls,), tmp_dict) 2396 2397 self.operandNameMap = operand_name 2398 2399 # Define operand variables. 2400 operands = user_dict.keys() 2401 extensions = self.operandTypeMap.keys() 2402 2403 operandsREString = r''' 2404 (?<!\w) # neg. lookbehind assertion: prevent partial matches 2405 ((%s)(?:_(%s))?(?:\[\w+\])?) # match: operand with optional '_' 2406 # then suffix, and then an optional array index. 2407 (?!\w) # neg. lookahead assertion: prevent partial matches 2408 ''' % (string.join(operands, '|'), string.join(extensions, '|')) 2409 2410 self.operandsRE = re.compile(operandsREString, re.MULTILINE|re.VERBOSE) 2411 2412 # Same as operandsREString, but extension is mandatory, and only two 2413 # groups are returned (base and ext, not full name as above). 2414 # Used for subtituting '_' for '.' to make C++ identifiers. 2415 operandsWithExtREString = r'(?<!\w)(%s)_(%s)(?!\w)' \ 2416 % (string.join(operands, '|'), string.join(extensions, '|')) 2417 2418 self.operandsWithExtRE = \ 2419 re.compile(operandsWithExtREString, re.MULTILINE) 2420 2421 def substMungedOpNames(self, code): 2422 '''Munge operand names in code string to make legal C++ 2423 variable names. This means getting rid of the type extension 2424 if any. Will match base_name attribute of Operand object.)''' 2425 return self.operandsWithExtRE.sub(r'\1', code) 2426 2427 def mungeSnippet(self, s): 2428 '''Fix up code snippets for final substitution in templates.''' 2429 if isinstance(s, str): 2430 return self.substMungedOpNames(substBitOps(s)) 2431 else: 2432 return s 2433 2434 def open(self, name, bare=False): 2435 '''Open the output file for writing and include scary warning.''' 2436 filename = os.path.join(self.output_dir, name) 2437 f = open(filename, 'w') 2438 if f: 2439 if not bare: 2440 f.write(ISAParser.scaremonger_template % self) 2441 return f 2442 2443 def update(self, file, contents): 2444 '''Update the output file only. Scons should handle the case when 2445 the new contents are unchanged using its built-in hash feature.''' 2446 f = self.open(file) 2447 f.write(contents) 2448 f.close() 2449 2450 # This regular expression matches '##include' directives 2451 includeRE = re.compile(r'^\s*##include\s+"(?P<filename>[^"]*)".*$', 2452 re.MULTILINE) 2453 2454 def replace_include(self, matchobj, dirname): 2455 """Function to replace a matched '##include' directive with the 2456 contents of the specified file (with nested ##includes 2457 replaced recursively). 'matchobj' is an re match object 2458 (from a match of includeRE) and 'dirname' is the directory 2459 relative to which the file path should be resolved.""" 2460 2461 fname = matchobj.group('filename') 2462 full_fname = os.path.normpath(os.path.join(dirname, fname)) 2463 contents = '##newfile "%s"\n%s\n##endfile\n' % \ 2464 (full_fname, self.read_and_flatten(full_fname)) 2465 return contents 2466 2467 def read_and_flatten(self, filename): 2468 """Read a file and recursively flatten nested '##include' files.""" 2469 2470 current_dir = os.path.dirname(filename) 2471 try: 2472 contents = open(filename).read() 2473 except IOError: 2474 error('Error including file "%s"' % filename) 2475 2476 self.fileNameStack.push((filename, 0)) 2477 2478 # Find any includes and include them 2479 def replace(matchobj): 2480 return self.replace_include(matchobj, current_dir) 2481 contents = self.includeRE.sub(replace, contents) 2482 2483 self.fileNameStack.pop() 2484 return contents 2485 2486 AlreadyGenerated = {} 2487 2488 def _parse_isa_desc(self, isa_desc_file): 2489 '''Read in and parse the ISA description.''' 2490 2491 # The build system can end up running the ISA parser twice: once to 2492 # finalize the build dependencies, and then to actually generate 2493 # the files it expects (in src/arch/$ARCH/generated). This code 2494 # doesn't do anything different either time, however; the SCons 2495 # invocations just expect different things. Since this code runs 2496 # within SCons, we can just remember that we've already run and 2497 # not perform a completely unnecessary run, since the ISA parser's 2498 # effect is idempotent. 2499 if isa_desc_file in ISAParser.AlreadyGenerated: 2500 return 2501 2502 # grab the last three path components of isa_desc_file 2503 self.filename = '/'.join(isa_desc_file.split('/')[-3:]) 2504 2505 # Read file and (recursively) all included files into a string. 2506 # PLY requires that the input be in a single string so we have to 2507 # do this up front. 2508 isa_desc = self.read_and_flatten(isa_desc_file) 2509 2510 # Initialize filename stack with outer file. 2511 self.fileNameStack.push((isa_desc_file, 0)) 2512 2513 # Parse. 2514 self.parse_string(isa_desc) 2515 2516 ISAParser.AlreadyGenerated[isa_desc_file] = None 2517 2518 def parse_isa_desc(self, *args, **kwargs): 2519 try: 2520 self._parse_isa_desc(*args, **kwargs) 2521 except ISAParserError, e: 2522 e.exit(self.fileNameStack) 2523 2524# Called as script: get args from command line. 2525# Args are: <isa desc file> <output dir> 2526if __name__ == '__main__': 2527 ISAParser(sys.argv[2]).parse_isa_desc(sys.argv[1]) 2528