Func.py revision 9219
110355SGeoffrey.Blake@arm.com# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
28839Sandreas.hansson@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company
38839Sandreas.hansson@arm.com# All rights reserved.
48839Sandreas.hansson@arm.com#
58839Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
68839Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
78839Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
88839Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
98839Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
108839Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
118839Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
128839Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
133101Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
148579Ssteve.reinhardt@amd.com# this software without specific prior written permission.
153101Sstever@eecs.umich.edu#
163101Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173101Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183101Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193101Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203101Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213101Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223101Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233101Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243101Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253101Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263101Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273101Sstever@eecs.umich.edu
283101Sstever@eecs.umich.edufrom slicc.symbols.Symbol import Symbol
293101Sstever@eecs.umich.edufrom slicc.symbols.Type import Type
303101Sstever@eecs.umich.edu
313101Sstever@eecs.umich.educlass Func(Symbol):
323101Sstever@eecs.umich.edu    def __init__(self, table, ident, location, return_type, param_types,
333101Sstever@eecs.umich.edu                 param_strings, body, pairs, machine):
343101Sstever@eecs.umich.edu        super(Func, self).__init__(table, ident, location, pairs)
353101Sstever@eecs.umich.edu        self.return_type = return_type
363101Sstever@eecs.umich.edu        self.param_types = param_types
373101Sstever@eecs.umich.edu        self.param_strings = param_strings
383101Sstever@eecs.umich.edu        self.body = body
393101Sstever@eecs.umich.edu        self.isInternalMachineFunc = False
403101Sstever@eecs.umich.edu        self.c_ident = ident
413101Sstever@eecs.umich.edu
427778Sgblack@eecs.umich.edu        if machine is None or "external" in self or "primitive" in self:
438839Sandreas.hansson@arm.com            pass
443101Sstever@eecs.umich.edu        else:
453101Sstever@eecs.umich.edu            self.machineStr = str(machine)
463101Sstever@eecs.umich.edu            self.isInternalMachineFunc = True
473101Sstever@eecs.umich.edu
483101Sstever@eecs.umich.edu    def __repr__(self):
493101Sstever@eecs.umich.edu        return ""
503101Sstever@eecs.umich.edu
513101Sstever@eecs.umich.edu    @property
523101Sstever@eecs.umich.edu    def prototype(self):
533101Sstever@eecs.umich.edu        if "external" in self:
543101Sstever@eecs.umich.edu            return ""
553101Sstever@eecs.umich.edu
563101Sstever@eecs.umich.edu        return_type = self.return_type.c_ident
573101Sstever@eecs.umich.edu        void_type = self.symtab.find("void", Type)
583101Sstever@eecs.umich.edu        if "return_by_ref" in self and self.return_type != void_type:
593101Sstever@eecs.umich.edu            return_type += "&"
603101Sstever@eecs.umich.edu        elif "return_by_pointer" in self and self.return_type != void_type:
613101Sstever@eecs.umich.edu            return_type += "*"
623885Sbinkertn@umich.edu
633885Sbinkertn@umich.edu        return "%s %s(%s);" % (return_type, self.c_ident,
644762Snate@binkert.org                               ", ".join(self.param_strings))
653885Sbinkertn@umich.edu
663885Sbinkertn@umich.edu    def writeCodeFiles(self, path, includes):
677528Ssteve.reinhardt@amd.com        return
683885Sbinkertn@umich.edu
694380Sbinkertn@umich.edu    def generateCode(self):
704167Sbinkertn@umich.edu        '''This write a function of object Chip'''
713102Sstever@eecs.umich.edu        if "external" in self:
723101Sstever@eecs.umich.edu            return ""
734762Snate@binkert.org
744762Snate@binkert.org        code = self.symtab.codeFormatter()
754762Snate@binkert.org
764762Snate@binkert.org        # Generate function header
774762Snate@binkert.org        void_type = self.symtab.find("void", Type)
784762Snate@binkert.org        return_type = self.return_type.c_ident
794762Snate@binkert.org        if "return_by_ref" in self and self.return_type != void_type:
804762Snate@binkert.org            return_type += "&"
814762Snate@binkert.org        if "return_by_pointer" in self and self.return_type != void_type:
825033Smilesck@eecs.umich.edu            return_type += "*"
835033Smilesck@eecs.umich.edu
845033Smilesck@eecs.umich.edu        if self.isInternalMachineFunc:
855033Smilesck@eecs.umich.edu            klass = "%s_Controller" % self.machineStr
865033Smilesck@eecs.umich.edu        else:
875033Smilesck@eecs.umich.edu            klass = "Chip"
885033Smilesck@eecs.umich.edu
895033Smilesck@eecs.umich.edu        params = ', '.join(self.param_strings)
905033Smilesck@eecs.umich.edu
915033Smilesck@eecs.umich.edu        code('''
923101Sstever@eecs.umich.edu$return_type
933101Sstever@eecs.umich.edu${klass}::${{self.c_ident}}($params)
943101Sstever@eecs.umich.edu{
955033Smilesck@eecs.umich.edu${{self.body}}
9610267SGeoffrey.Blake@arm.com}
978596Ssteve.reinhardt@amd.com''')
988596Ssteve.reinhardt@amd.com        return str(code)
998596Ssteve.reinhardt@amd.com
1008596Ssteve.reinhardt@amd.com__all__ = [ "Func" ]
1017673Snate@binkert.org