1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 2# Copyright (c) 2009 The Hewlett-Packard Development Company 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer; 9# redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution; 12# neither the name of the copyright holders nor the names of its 13# contributors may be used to endorse or promote products derived from 14# this software without specific prior written permission. 15# 16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28from m5.util import orderdict 29 30from slicc.util import PairContainer 31from slicc.symbols.Symbol import Symbol 32 33class DataMember(PairContainer): 34 def __init__(self, ident, type, pairs, init_code): 35 super(DataMember, self).__init__(pairs) 36 self.ident = ident 37 self.type = type 38 self.init_code = init_code 39 40class Enumeration(PairContainer): 41 def __init__(self, ident, pairs): 42 super(Enumeration, self).__init__(pairs) 43 self.ident = ident 44 45class Method(object): 46 def __init__(self, return_type, param_types): 47 self.return_type = return_type 48 self.param_types = param_types 49 50class Type(Symbol): 51 def __init__(self, table, ident, location, pairs, machine=None): 52 super(Type, self).__init__(table, ident, location, pairs) 53 self.c_ident = ident 54 self.abstract_ident = "" 55 if machine: 56 if self.isExternal or self.isPrimitive: 57 if "external_name" in self: 58 self.c_ident = self["external_name"] 59 else: 60 # Append with machine name 61 self.c_ident = "%s_%s" % (machine, ident) 62 63 self.pairs.setdefault("desc", "No description avaliable") 64 65 # check for interface that this Type implements 66 if "interface" in self: 67 interface = self["interface"] 68 if interface in ("Message", "NetworkMessage"): 69 self["message"] = "yes" 70 if interface == "NetworkMessage": 71 self["networkmessage"] = "yes" 72 73 # FIXME - all of the following id comparisons are fragile hacks 74 if self.ident in ("CacheMemory", "NewCacheMemory", 75 "TLCCacheMemory", "DNUCACacheMemory", 76 "DNUCABankCacheMemory", "L2BankCacheMemory", 77 "CompressedCacheMemory", "PrefetchCacheMemory"): 78 self["cache"] = "yes" 79 80 if self.ident in ("TBETable", "DNUCATBETable", "DNUCAStopTable"): 81 self["tbe"] = "yes" 82 83 if self.ident == "NewTBETable": 84 self["newtbe"] = "yes" 85 86 if self.ident == "TimerTable": 87 self["timer"] = "yes" 88 89 if self.ident == "DirectoryMemory": 90 self["dir"] = "yes" 91 92 if self.ident == "PersistentTable": 93 self["persistent"] = "yes" 94 95 if self.ident == "Prefetcher": 96 self["prefetcher"] = "yes" 97 98 if self.ident == "DNUCA_Movement": 99 self["mover"] = "yes" 100 101 self.isMachineType = (ident == "MachineType") 102 103 self.data_members = orderdict() 104 105 # Methods 106 self.methods = {} 107 108 # Enums 109 self.enums = orderdict() 110 111 @property 112 def isPrimitive(self): 113 return "primitive" in self 114 @property 115 def isNetworkMessage(self): 116 return "networkmessage" in self 117 @property 118 def isMessage(self): 119 return "message" in self 120 @property 121 def isBuffer(self): 122 return "buffer" in self 123 @property 124 def isInPort(self): 125 return "inport" in self 126 @property 127 def isOutPort(self): 128 return "outport" in self 129 @property 130 def isEnumeration(self): 131 return "enumeration" in self 132 @property 133 def isExternal(self): 134 return "external" in self 135 @property 136 def isGlobal(self): 137 return "global" in self 138 @property 139 def isInterface(self): 140 return "interface" in self 141 142 # Return false on error 143 def dataMemberAdd(self, ident, type, pairs, init_code): 144 if ident in self.data_members: 145 return False 146 147 member = DataMember(ident, type, pairs, init_code) 148 self.data_members[ident] = member 149 150 return True 151 152 def dataMemberType(self, ident): 153 return self.data_members[ident].type 154 155 def methodId(self, name, param_type_vec): 156 return '_'.join([name] + [ pt.c_ident for pt in param_type_vec ]) 157 158 def methodIdAbstract(self, name, param_type_vec): 159 return '_'.join([name] + [ pt.abstract_ident for pt in param_type_vec ]) 160 161 def methodAdd(self, name, return_type, param_type_vec): 162 ident = self.methodId(name, param_type_vec) 163 if ident in self.methods: 164 return False 165 166 self.methods[ident] = Method(return_type, param_type_vec) 167 return True 168 169 def enumAdd(self, ident, pairs): 170 if ident in self.enums: 171 return False 172 173 self.enums[ident] = Enumeration(ident, pairs) 174 175 # Add default 176 if "default" not in self: 177 self["default"] = "%s_NUM" % self.c_ident 178 179 return True 180 181 def writeCodeFiles(self, path): 182 if self.isExternal: 183 # Do nothing 184 pass 185 elif self.isEnumeration: 186 self.printEnumHH(path) 187 self.printEnumCC(path) 188 else: 189 # User defined structs and messages 190 self.printTypeHH(path) 191 self.printTypeCC(path) 192 193 def printTypeHH(self, path): 194 code = self.symtab.codeFormatter() 195 code(''' 196/** \\file ${{self.c_ident}}.hh 197 * 198 * 199 * Auto generated C++ code started by $__file__:$__line__ 200 */ 201 202#ifndef __${{self.c_ident}}_HH__ 203#define __${{self.c_ident}}_HH__ 204 205#include <iostream> 206 207#include "mem/ruby/common/Global.hh"
|
410 code(''' 411/** \\brief Print the state of this object */ 412void 413${{self.c_ident}}::print(ostream& out) const 414{ 415 out << "[${{self.c_ident}}: "; 416''') 417 418 # For each field 419 code.indent() 420 for dm in self.data_members.values(): 421 code('out << "${{dm.ident}} = " << m_${{dm.ident}} << " ";''') 422 423 if self.isMessage: 424 code('out << "Time = " << getTime() << " ";') 425 code.dedent() 426 427 # Trailer 428 code(''' 429 out << "]"; 430}''') 431 432 code.write(path, "%s.cc" % self.c_ident) 433 434 def printEnumHH(self, path): 435 code = self.symtab.codeFormatter() 436 code(''' 437/** \\file ${{self.c_ident}}.hh 438 * 439 * Auto generated C++ code started by $__file__:$__line__ 440 */ 441 442#ifndef __${{self.c_ident}}_HH__ 443#define __${{self.c_ident}}_HH__ 444 445#include <iostream> 446#include <string> 447 448#include "mem/ruby/common/Global.hh" 449 450// Class definition 451/** \\enum ${{self.c_ident}} 452 * \\brief ${{self.desc}} 453 */ 454enum ${{self.c_ident}} { 455 ${{self.c_ident}}_FIRST, 456''') 457 458 code.indent() 459 # For each field 460 for i,(ident,enum) in enumerate(self.enums.iteritems()): 461 desc = enum.get("desc", "No description avaliable") 462 if i == 0: 463 init = ' = %s_FIRST' % self.c_ident 464 else: 465 init = '' 466 code('${{self.c_ident}}_${{enum.ident}}$init, /**< $desc */') 467 code.dedent() 468 code(''' 469 ${{self.c_ident}}_NUM 470}; 471 472// Code to convert from a string to the enumeration 473${{self.c_ident}} string_to_${{self.c_ident}}(const std::string& str); 474 475// Code to convert state to a string 476std::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj); 477 478// Code to increment an enumeration type 479${{self.c_ident}} &operator++(${{self.c_ident}} &e); 480''') 481 482 # MachineType hack used to set the base component id for each Machine 483 if self.isMachineType: 484 code(''' 485int ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj); 486MachineType ${{self.c_ident}}_from_base_level(int); 487int ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj); 488int ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj); 489''') 490 491 for enum in self.enums.itervalues(): 492 code('#define MACHINETYPE_${{enum.ident}} 1') 493 494 # Trailer 495 code(''' 496std::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj); 497 498#endif // __${{self.c_ident}}_HH__ 499''') 500 501 code.write(path, "%s.hh" % self.c_ident) 502 503 def printEnumCC(self, path): 504 code = self.symtab.codeFormatter() 505 code(''' 506/** \\file ${{self.c_ident}}.hh 507 * 508 * Auto generated C++ code started by $__file__:$__line__ 509 */ 510 511#include <iostream> 512#include <string> 513 514#include "mem/protocol/${{self.c_ident}}.hh" 515 516using namespace std; 517 518''') 519 520 if self.isMachineType: 521 for enum in self.enums.itervalues(): 522 code('#include "mem/protocol/${{enum.ident}}_Controller.hh"') 523 524 code(''' 525// Code for output operator 526ostream& 527operator<<(ostream& out, const ${{self.c_ident}}& obj) 528{ 529 out << ${{self.c_ident}}_to_string(obj); 530 out << flush; 531 return out; 532} 533 534// Code to convert state to a string 535string 536${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj) 537{ 538 switch(obj) { 539''') 540 541 # For each field 542 code.indent() 543 for enum in self.enums.itervalues(): 544 code(' case ${{self.c_ident}}_${{enum.ident}}:') 545 code(' return "${{enum.ident}}";') 546 code.dedent() 547 548 # Trailer 549 code(''' 550 default: 551 ERROR_MSG("Invalid range for type ${{self.c_ident}}"); 552 return ""; 553 } 554} 555 556// Code to convert from a string to the enumeration 557${{self.c_ident}} 558string_to_${{self.c_ident}}(const string& str) 559{ 560''') 561 562 # For each field 563 start = "" 564 code.indent() 565 for enum in self.enums.itervalues(): 566 code('${start}if (str == "${{enum.ident}}") {') 567 code(' return ${{self.c_ident}}_${{enum.ident}};') 568 start = "} else " 569 code.dedent() 570 571 code(''' 572 } else { 573 WARN_EXPR(str); 574 ERROR_MSG("Invalid string conversion for type ${{self.c_ident}}"); 575 } 576} 577 578// Code to increment an enumeration type 579${{self.c_ident}}& 580operator++(${{self.c_ident}}& e) 581{ 582 assert(e < ${{self.c_ident}}_NUM); 583 return e = ${{self.c_ident}}(e+1); 584} 585''') 586 587 # MachineType hack used to set the base level and number of 588 # components for each Machine 589 if self.isMachineType: 590 code(''' 591/** \\brief returns the base vector index for each machine type to be 592 * used by NetDest 593 * 594 * \\return the base vector index for each machine type to be used by NetDest 595 * \\see NetDest.hh 596 */ 597int 598${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj) 599{ 600 switch(obj) { 601''') 602 603 # For each field 604 code.indent() 605 for i,enum in enumerate(self.enums.itervalues()): 606 code(' case ${{self.c_ident}}_${{enum.ident}}:') 607 code(' return $i;') 608 code.dedent() 609 610 # total num 611 code(''' 612 case ${{self.c_ident}}_NUM: 613 return ${{len(self.enums)}}; 614 615 default: 616 ERROR_MSG("Invalid range for type ${{self.c_ident}}"); 617 return -1; 618 } 619} 620 621/** \\brief returns the machine type for each base vector index used by NetDest 622 * 623 * \\return the MachineType 624 */ 625MachineType 626${{self.c_ident}}_from_base_level(int type) 627{ 628 switch(type) { 629''') 630 631 # For each field 632 code.indent() 633 for i,enum in enumerate(self.enums.itervalues()): 634 code(' case $i:') 635 code(' return ${{self.c_ident}}_${{enum.ident}};') 636 code.dedent() 637 638 # Trailer 639 code(''' 640 default: 641 ERROR_MSG("Invalid range for type ${{self.c_ident}}"); 642 return MachineType_NUM; 643 } 644} 645 646/** \\brief The return value indicates the number of components created 647 * before a particular machine\'s components 648 * 649 * \\return the base number of components for each machine 650 */ 651int 652${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) 653{ 654 int base = 0; 655 switch(obj) { 656''') 657 658 # For each field 659 code.indent() 660 code(' case ${{self.c_ident}}_NUM:') 661 for enum in reversed(self.enums.values()): 662 code(' base += ${{enum.ident}}_Controller::getNumControllers();') 663 code(' case ${{self.c_ident}}_${{enum.ident}}:') 664 code(' break;') 665 code.dedent() 666 667 code(''' 668 default: 669 ERROR_MSG("Invalid range for type ${{self.c_ident}}"); 670 return -1; 671 } 672 673 return base; 674} 675 676/** \\brief returns the total number of components for each machine 677 * \\return the total number of components for each machine 678 */ 679int 680${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj) 681{ 682 switch(obj) { 683''') 684 685 # For each field 686 for enum in self.enums.itervalues(): 687 code(''' 688 case ${{self.c_ident}}_${{enum.ident}}: 689 return ${{enum.ident}}_Controller::getNumControllers(); 690''') 691 692 # total num 693 code(''' 694 case ${{self.c_ident}}_NUM: 695 default: 696 ERROR_MSG("Invalid range for type ${{self.c_ident}}"); 697 return -1; 698 } 699} 700''') 701 702 # Write the file 703 code.write(path, "%s.cc" % self.c_ident) 704 705__all__ = [ "Type" ]
|