Func.py revision 9298
1451SN/A# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 22212SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company 3451SN/A# All rights reserved. 4451SN/A# 5451SN/A# Redistribution and use in source and binary forms, with or without 6451SN/A# modification, are permitted provided that the following conditions are 7451SN/A# met: redistributions of source code must retain the above copyright 8451SN/A# notice, this list of conditions and the following disclaimer; 9451SN/A# redistributions in binary form must reproduce the above copyright 10451SN/A# notice, this list of conditions and the following disclaimer in the 11451SN/A# documentation and/or other materials provided with the distribution; 12451SN/A# neither the name of the copyright holders nor the names of its 13451SN/A# contributors may be used to endorse or promote products derived from 14451SN/A# this software without specific prior written permission. 15451SN/A# 16451SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17451SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18451SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19451SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20451SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21451SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22451SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23451SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24451SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25451SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26451SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu 282665Ssaidi@eecs.umich.edufrom slicc.symbols.Symbol import Symbol 292665Ssaidi@eecs.umich.edufrom slicc.symbols.Type import Type 302665Ssaidi@eecs.umich.edu 31451SN/Aclass Func(Symbol): 32451SN/A def __init__(self, table, ident, location, return_type, param_types, 332212SN/A param_strings, body, pairs): 342212SN/A super(Func, self).__init__(table, ident, location, pairs) 35451SN/A self.return_type = return_type 362680Sktlim@umich.edu self.param_types = param_types 371070SN/A self.param_strings = param_strings 381070SN/A self.body = body 391070SN/A self.isInternalMachineFunc = False 402212SN/A self.c_ident = ident 413565Sgblack@eecs.umich.edu self.class_name = "" 422212SN/A 432212SN/A def __repr__(self): 444762Snate@binkert.org return "" 452212SN/A 462212SN/A @property 472212SN/A def prototype(self): 48451SN/A if "external" in self: 49885SN/A return "" 502343SN/A 51885SN/A return_type = self.return_type.c_ident 52885SN/A void_type = self.symtab.find("void", Type) 53885SN/A if "return_by_ref" in self and self.return_type != void_type: 542212SN/A return_type += "&" 55451SN/A elif "return_by_pointer" in self and self.return_type != void_type: 56451SN/A return_type += "*" 575569Snate@binkert.org 581885SN/A return "%s %s(%s);" % (return_type, self.c_ident, 591885SN/A ", ".join(self.param_strings)) 601885SN/A 612680Sktlim@umich.edu def writeCodeFiles(self, path, includes): 621885SN/A return 631885SN/A 645569Snate@binkert.org def generateCode(self): 651885SN/A '''This write a function of object Chip''' 661885SN/A if "external" in self: 671885SN/A return "" 682680Sktlim@umich.edu 691885SN/A code = self.symtab.codeFormatter() 701885SN/A 711855SN/A # Generate function header 721855SN/A void_type = self.symtab.find("void", Type) 731855SN/A return_type = self.return_type.c_ident 741855SN/A if "return_by_ref" in self and self.return_type != void_type: 751855SN/A return_type += "&" 761855SN/A if "return_by_pointer" in self and self.return_type != void_type: 771855SN/A return_type += "*" 781855SN/A 791855SN/A params = ', '.join(self.param_strings) 801855SN/A 811855SN/A code(''' 821855SN/A$return_type 831855SN/A${{self.class_name}}::${{self.c_ident}}($params) 841855SN/A{ 851855SN/A${{self.body}} 861855SN/A} 871855SN/A''') 881855SN/A return str(code) 891855SN/A 901855SN/A__all__ = [ "Func" ] 911492SN/A