Func.py revision 7055
11689SN/A# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
22325SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
31689SN/A# All rights reserved.
41689SN/A#
51689SN/A# Redistribution and use in source and binary forms, with or without
61689SN/A# modification, are permitted provided that the following conditions are
71689SN/A# met: redistributions of source code must retain the above copyright
81689SN/A# notice, this list of conditions and the following disclaimer;
91689SN/A# redistributions in binary form must reproduce the above copyright
101689SN/A# notice, this list of conditions and the following disclaimer in the
111689SN/A# documentation and/or other materials provided with the distribution;
121689SN/A# neither the name of the copyright holders nor the names of its
131689SN/A# contributors may be used to endorse or promote products derived from
141689SN/A# this software without specific prior written permission.
151689SN/A#
161689SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/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
292756Sksewell@umich.edufrom slicc.symbols.Type import Type
301689SN/A
311689SN/Aclass Func(Symbol):
321858SN/A    def __init__(self, table, ident, location, return_type, param_types,
332733Sktlim@umich.edu                 param_strings, body, pairs, machine):
341858SN/A        super(Func, self).__init__(table, ident, location, pairs)
351858SN/A        self.return_type = return_type
362356SN/A        self.param_types = param_types
371060SN/A        self.param_strings = param_strings
381060SN/A        self.body = body
391060SN/A        self.isInternalMachineFunc = False
401060SN/A
411060SN/A        if machine is None:
422325SN/A            self.c_ident = ident
432683Sktlim@umich.edu        elif "external" in self or "primitive" in self:
442680Sktlim@umich.edu            self.c_ident = ident
452817Sksewell@umich.edu        else:
461717SN/A            self.machineStr = str(machine)
471060SN/A            # Append with machine name
482325SN/A            self.c_ident = "%s_%s" % (self.machineStr, ident)
492292SN/A            self.isInternalMachineFunc = True
502292SN/A
512794Sktlim@umich.edu    def __repr__(self):
522794Sktlim@umich.edu        return ""
532794Sktlim@umich.edu
542794Sktlim@umich.edu    @property
551060SN/A    def prototype(self):
562669Sktlim@umich.edu        if "external" in self:
571060SN/A            return ""
582733Sktlim@umich.edu
592292SN/A        return_type = self.return_type.c_ident
601060SN/A        void_type = self.symtab.find("void", Type)
611060SN/A        if "return_by_ref" in self and self.return_type != void_type:
621060SN/A            return_type += "&"
632292SN/A
642733Sktlim@umich.edu        return "%s %s(%s);" % (return_type, self.c_ident,
652292SN/A                               ", ".join(self.param_strings))
662292SN/A
672292SN/A    def writeCodeFiles(self, path):
682292SN/A        '''This write a function of object Chip'''
691060SN/A        if "external" in self:
701755SN/A            return
711060SN/A
721060SN/A        code = self.symtab.codeFormatter()
731060SN/A
741060SN/A        # Header
751060SN/A        code('''
761060SN/A/** Auto generated C++ code started by $__file__:$__line__ */
771755SN/A
781060SN/A#include "mem/protocol/Types.hh"
791060SN/A''')
801060SN/A
811060SN/A        if self.isInternalMachineFunc:
821060SN/A            code('#include "mem/protocol/${{self.machineStr}}_Controller.hh"')
831060SN/A
841755SN/A        code('using namespace std;')
851060SN/A        # Generate function header
861755SN/A        void_type = self.symtab.find("void", Type)
871060SN/A        return_type = self.return_type.c_ident
881060SN/A        if "return_by_ref" in self and self.return_type != void_type:
891060SN/A            return_type += "&"
902829Sksewell@umich.edu
913221Sktlim@umich.edu        if self.isInternalMachineFunc:
922829Sksewell@umich.edu            klass = "%s_Controller" % self.machineStr
932829Sksewell@umich.edu        else:
942829Sksewell@umich.edu            klass = "Chip"
952829Sksewell@umich.edu
962829Sksewell@umich.edu        params = ', '.join(self.param_strings)
972829Sksewell@umich.edu
982829Sksewell@umich.edu        code('''
992829Sksewell@umich.edu$return_type
1002829Sksewell@umich.edu${klass}::${{self.c_ident}}($params)
1012829Sksewell@umich.edu{
1022829Sksewell@umich.edu${{self.body}}
1032829Sksewell@umich.edu}
1042829Sksewell@umich.edu''')
1052829Sksewell@umich.edu        code.write(path, "%s.cc" % self.c_ident)
1062829Sksewell@umich.edu
1072829Sksewell@umich.edu__all__ = [ "Func" ]
1082829Sksewell@umich.edu