Func.py revision 7055
12SN/A# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
21762SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
32SN/A# All rights reserved.
42SN/A#
52SN/A# Redistribution and use in source and binary forms, with or without
62SN/A# modification, are permitted provided that the following conditions are
72SN/A# met: redistributions of source code must retain the above copyright
82SN/A# notice, this list of conditions and the following disclaimer;
92SN/A# redistributions in binary form must reproduce the above copyright
102SN/A# notice, this list of conditions and the following disclaimer in the
112SN/A# documentation and/or other materials provided with the distribution;
122SN/A# neither the name of the copyright holders nor the names of its
132SN/A# contributors may be used to endorse or promote products derived from
142SN/A# this software without specific prior written permission.
152SN/A#
162SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/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
312SN/Aclass Func(Symbol):
322SN/A    def __init__(self, table, ident, location, return_type, param_types,
332SN/A                 param_strings, body, pairs, machine):
342SN/A        super(Func, self).__init__(table, ident, location, pairs)
352SN/A        self.return_type = return_type
362SN/A        self.param_types = param_types
3775SN/A        self.param_strings = param_strings
382SN/A        self.body = body
392439SN/A        self.isInternalMachineFunc = False
402439SN/A
41603SN/A        if machine is None:
422986Sgblack@eecs.umich.edu            self.c_ident = ident
43603SN/A        elif "external" in self or "primitive" in self:
444762Snate@binkert.org            self.c_ident = ident
452520SN/A        else:
464762Snate@binkert.org            self.machineStr = str(machine)
472378SN/A            # Append with machine name
482378SN/A            self.c_ident = "%s_%s" % (self.machineStr, ident)
49722SN/A            self.isInternalMachineFunc = True
502521SN/A
512378SN/A    def __repr__(self):
52312SN/A        return ""
531634SN/A
542680Sktlim@umich.edu    @property
551634SN/A    def prototype(self):
562521SN/A        if "external" in self:
572378SN/A            return ""
582378SN/A
59803SN/A        return_type = self.return_type.c_ident
603960Sgblack@eecs.umich.edu        void_type = self.symtab.find("void", Type)
612378SN/A        if "return_by_ref" in self and self.return_type != void_type:
623536Sgblack@eecs.umich.edu            return_type += "&"
633536Sgblack@eecs.umich.edu
643536Sgblack@eecs.umich.edu        return "%s %s(%s);" % (return_type, self.c_ident,
653536Sgblack@eecs.umich.edu                               ", ".join(self.param_strings))
662SN/A
672SN/A    def writeCodeFiles(self, path):
682SN/A        '''This write a function of object Chip'''
69603SN/A        if "external" in self:
702901Ssaidi@eecs.umich.edu            return
712902Ssaidi@eecs.umich.edu
722902Ssaidi@eecs.umich.edu        code = self.symtab.codeFormatter()
734762Snate@binkert.org
744762Snate@binkert.org        # Header
754762Snate@binkert.org        code('''
764762Snate@binkert.org/** Auto generated C++ code started by $__file__:$__line__ */
774762Snate@binkert.org
784762Snate@binkert.org#include "mem/protocol/Types.hh"
792901Ssaidi@eecs.umich.edu''')
802901Ssaidi@eecs.umich.edu
812901Ssaidi@eecs.umich.edu        if self.isInternalMachineFunc:
822901Ssaidi@eecs.umich.edu            code('#include "mem/protocol/${{self.machineStr}}_Controller.hh"')
832901Ssaidi@eecs.umich.edu
844762Snate@binkert.org        code('using namespace std;')
852901Ssaidi@eecs.umich.edu        # Generate function header
862521SN/A        void_type = self.symtab.find("void", Type)
872SN/A        return_type = self.return_type.c_ident
882SN/A        if "return_by_ref" in self and self.return_type != void_type:
892680Sktlim@umich.edu            return_type += "&"
905714Shsul@eecs.umich.edu
911806SN/A        if self.isInternalMachineFunc:
925713Shsul@eecs.umich.edu            klass = "%s_Controller" % self.machineStr
935713Shsul@eecs.umich.edu        else:
945713Shsul@eecs.umich.edu            klass = "Chip"
955713Shsul@eecs.umich.edu
965713Shsul@eecs.umich.edu        params = ', '.join(self.param_strings)
975714Shsul@eecs.umich.edu
981806SN/A        code('''
995714Shsul@eecs.umich.edu$return_type
1001806SN/A${klass}::${{self.c_ident}}($params)
1011806SN/A{
1025714Shsul@eecs.umich.edu${{self.body}}
1031806SN/A}
104180SN/A''')
1052378SN/A        code.write(path, "%s.cc" % self.c_ident)
1062378SN/A
1072378SN/A__all__ = [ "Func" ]
1082378SN/A