Func.py revision 11062
14604Sgblack@eecs.umich.edu# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 24604Sgblack@eecs.umich.edu# Copyright (c) 2009 The Hewlett-Packard Development Company 34604Sgblack@eecs.umich.edu# All rights reserved. 44604Sgblack@eecs.umich.edu# 57087Snate@binkert.org# Redistribution and use in source and binary forms, with or without 67087Snate@binkert.org# modification, are permitted provided that the following conditions are 77087Snate@binkert.org# met: redistributions of source code must retain the above copyright 87087Snate@binkert.org# notice, this list of conditions and the following disclaimer; 97087Snate@binkert.org# redistributions in binary form must reproduce the above copyright 107087Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 117087Snate@binkert.org# documentation and/or other materials provided with the distribution; 127087Snate@binkert.org# neither the name of the copyright holders nor the names of its 134604Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from 147087Snate@binkert.org# this software without specific prior written permission. 157087Snate@binkert.org# 167087Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 177087Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 187087Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 197087Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 207087Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 217087Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224604Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237087Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244604Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254604Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264604Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274604Sgblack@eecs.umich.edu 284604Sgblack@eecs.umich.edufrom slicc.symbols.Symbol import Symbol 294604Sgblack@eecs.umich.edufrom slicc.symbols.Type import Type 304604Sgblack@eecs.umich.edu 314604Sgblack@eecs.umich.educlass Func(Symbol): 324604Sgblack@eecs.umich.edu def __init__(self, table, ident, name, location, return_type, param_types, 334604Sgblack@eecs.umich.edu param_strings, body, pairs): 344604Sgblack@eecs.umich.edu super(Func, self).__init__(table, ident, location, pairs) 354604Sgblack@eecs.umich.edu self.return_type = return_type 364604Sgblack@eecs.umich.edu self.param_types = param_types 374604Sgblack@eecs.umich.edu self.param_strings = param_strings 384604Sgblack@eecs.umich.edu self.body = body 394604Sgblack@eecs.umich.edu self.isInternalMachineFunc = False 405616Snate@binkert.org self.c_ident = ident 415616Snate@binkert.org self.c_name = name 424604Sgblack@eecs.umich.edu self.class_name = "" 434604Sgblack@eecs.umich.edu 444604Sgblack@eecs.umich.edu def __repr__(self): 454604Sgblack@eecs.umich.edu return "" 464604Sgblack@eecs.umich.edu 474604Sgblack@eecs.umich.edu @property 484604Sgblack@eecs.umich.edu def prototype(self): 494604Sgblack@eecs.umich.edu if "external" in self: 504604Sgblack@eecs.umich.edu return "" 514604Sgblack@eecs.umich.edu 524604Sgblack@eecs.umich.edu return_type = self.return_type.c_ident 534712Sgblack@eecs.umich.edu void_type = self.symtab.find("void", Type) 544712Sgblack@eecs.umich.edu if "return_by_ref" in self and self.return_type != void_type: 554604Sgblack@eecs.umich.edu return_type += "&" 564604Sgblack@eecs.umich.edu elif "return_by_pointer" in self and self.return_type != void_type: 574604Sgblack@eecs.umich.edu return_type += "*" 584604Sgblack@eecs.umich.edu 594604Sgblack@eecs.umich.edu return "%s %s(%s);" % (return_type, self.c_name, 604848Sgblack@eecs.umich.edu ", ".join(self.param_strings)) 614604Sgblack@eecs.umich.edu 624604Sgblack@eecs.umich.edu def writeCodeFiles(self, path, includes): 634604Sgblack@eecs.umich.edu return 646071Sgblack@eecs.umich.edu 656071Sgblack@eecs.umich.edu def checkArguments(self, args): 666071Sgblack@eecs.umich.edu if len(args) != len(self.param_types): 676071Sgblack@eecs.umich.edu self.error("Wrong number of arguments passed to function : '%s'" +\ 686071Sgblack@eecs.umich.edu " Expected %d, got %d", self.c_ident, 696071Sgblack@eecs.umich.edu len(self.param_types), len(args)) 706071Sgblack@eecs.umich.edu 716071Sgblack@eecs.umich.edu cvec = [] 726071Sgblack@eecs.umich.edu type_vec = [] 736071Sgblack@eecs.umich.edu for expr,expected_type in zip(args, self.param_types): 746071Sgblack@eecs.umich.edu # Check the types of the parameter 756071Sgblack@eecs.umich.edu actual_type,param_code = expr.inline(True) 766071Sgblack@eecs.umich.edu if str(actual_type) != 'OOD' and \ 776071Sgblack@eecs.umich.edu str(actual_type) != str(expected_type) and \ 786071Sgblack@eecs.umich.edu str(actual_type["interface"]) != str(expected_type): 796071Sgblack@eecs.umich.edu expr.error("Type mismatch: expected: %s actual: %s" % \ 806071Sgblack@eecs.umich.edu (expected_type, actual_type)) 816071Sgblack@eecs.umich.edu cvec.append(param_code) 826071Sgblack@eecs.umich.edu type_vec.append(expected_type) 836071Sgblack@eecs.umich.edu 846071Sgblack@eecs.umich.edu return cvec, type_vec 856071Sgblack@eecs.umich.edu 866071Sgblack@eecs.umich.edu def generateCode(self): 876071Sgblack@eecs.umich.edu '''This write a function of object Chip''' 886071Sgblack@eecs.umich.edu if "external" in self: 896071Sgblack@eecs.umich.edu return "" 904604Sgblack@eecs.umich.edu 914604Sgblack@eecs.umich.edu code = self.symtab.codeFormatter() 924712Sgblack@eecs.umich.edu 934604Sgblack@eecs.umich.edu # Generate function header 944712Sgblack@eecs.umich.edu void_type = self.symtab.find("void", Type) 954712Sgblack@eecs.umich.edu return_type = self.return_type.c_ident 964848Sgblack@eecs.umich.edu if "return_by_ref" in self and self.return_type != void_type: 974604Sgblack@eecs.umich.edu return_type += "&" 984604Sgblack@eecs.umich.edu if "return_by_pointer" in self and self.return_type != void_type: 994604Sgblack@eecs.umich.edu return_type += "*" 1004863Sgblack@eecs.umich.edu 1014863Sgblack@eecs.umich.edu params = ', '.join(self.param_strings) 1024863Sgblack@eecs.umich.edu 1036437Sgblack@eecs.umich.edu code(''' 1044863Sgblack@eecs.umich.edu$return_type 1054863Sgblack@eecs.umich.edu${{self.class_name}}::${{self.c_name}}($params) 1064863Sgblack@eecs.umich.edu{ 1074863Sgblack@eecs.umich.edu${{self.body}} 1084863Sgblack@eecs.umich.edu} 1094863Sgblack@eecs.umich.edu''') 1104863Sgblack@eecs.umich.edu return str(code) 1114863Sgblack@eecs.umich.edu 1124604Sgblack@eecs.umich.edu__all__ = [ "Func" ] 1134604Sgblack@eecs.umich.edu