Func.py revision 7839
113172Sgiacomo.travaglini@arm.com# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
210259SAndrew.Bardsley@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company
310259SAndrew.Bardsley@arm.com# All rights reserved.
410259SAndrew.Bardsley@arm.com#
510259SAndrew.Bardsley@arm.com# Redistribution and use in source and binary forms, with or without
610259SAndrew.Bardsley@arm.com# modification, are permitted provided that the following conditions are
710259SAndrew.Bardsley@arm.com# met: redistributions of source code must retain the above copyright
810259SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer;
910259SAndrew.Bardsley@arm.com# redistributions in binary form must reproduce the above copyright
1010259SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer in the
1110259SAndrew.Bardsley@arm.com# documentation and/or other materials provided with the distribution;
1210259SAndrew.Bardsley@arm.com# neither the name of the copyright holders nor the names of its
1310259SAndrew.Bardsley@arm.com# contributors may be used to endorse or promote products derived from
1410259SAndrew.Bardsley@arm.com# this software without specific prior written permission.
1510259SAndrew.Bardsley@arm.com#
1610259SAndrew.Bardsley@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710259SAndrew.Bardsley@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810259SAndrew.Bardsley@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910259SAndrew.Bardsley@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010259SAndrew.Bardsley@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110259SAndrew.Bardsley@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210259SAndrew.Bardsley@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310259SAndrew.Bardsley@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410259SAndrew.Bardsley@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510259SAndrew.Bardsley@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610259SAndrew.Bardsley@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710259SAndrew.Bardsley@arm.com
2810259SAndrew.Bardsley@arm.comfrom slicc.symbols.Symbol import Symbol
2910259SAndrew.Bardsley@arm.comfrom slicc.symbols.Type import Type
3010259SAndrew.Bardsley@arm.com
3110259SAndrew.Bardsley@arm.comclass Func(Symbol):
3210259SAndrew.Bardsley@arm.com    def __init__(self, table, ident, location, return_type, param_types,
3310259SAndrew.Bardsley@arm.com                 param_strings, body, pairs, machine):
3410259SAndrew.Bardsley@arm.com        super(Func, self).__init__(table, ident, location, pairs)
3510259SAndrew.Bardsley@arm.com        self.return_type = return_type
3610259SAndrew.Bardsley@arm.com        self.param_types = param_types
3710259SAndrew.Bardsley@arm.com        self.param_strings = param_strings
3810259SAndrew.Bardsley@arm.com        self.body = body
3910259SAndrew.Bardsley@arm.com        self.isInternalMachineFunc = False
4010259SAndrew.Bardsley@arm.com
4110259SAndrew.Bardsley@arm.com        if machine is None:
4210259SAndrew.Bardsley@arm.com            self.c_ident = ident
4312563Sgabeblack@google.com        elif "external" in self or "primitive" in self:
4412563Sgabeblack@google.com            self.c_ident = ident
4510259SAndrew.Bardsley@arm.com        else:
4610259SAndrew.Bardsley@arm.com            self.machineStr = str(machine)
4710259SAndrew.Bardsley@arm.com            # Append with machine name
4810259SAndrew.Bardsley@arm.com            self.c_ident = "%s_%s" % (self.machineStr, ident)
4913665Sandreas.sandberg@arm.com            self.isInternalMachineFunc = True
5013665Sandreas.sandberg@arm.com
5113665Sandreas.sandberg@arm.com    def __repr__(self):
5213665Sandreas.sandberg@arm.com        return ""
5310259SAndrew.Bardsley@arm.com
5413665Sandreas.sandberg@arm.com    @property
5510259SAndrew.Bardsley@arm.com    def prototype(self):
5610259SAndrew.Bardsley@arm.com        if "external" in self:
5710259SAndrew.Bardsley@arm.com            return ""
5810259SAndrew.Bardsley@arm.com
5910259SAndrew.Bardsley@arm.com        return_type = self.return_type.c_ident
6010259SAndrew.Bardsley@arm.com        void_type = self.symtab.find("void", Type)
6110259SAndrew.Bardsley@arm.com        if "return_by_ref" in self and self.return_type != void_type:
6210259SAndrew.Bardsley@arm.com            return_type += "&"
6310259SAndrew.Bardsley@arm.com        elif "return_by_pointer" in self and self.return_type != void_type:
6410259SAndrew.Bardsley@arm.com            return_type += "*"
6510259SAndrew.Bardsley@arm.com
6610259SAndrew.Bardsley@arm.com        return "%s %s(%s);" % (return_type, self.c_ident,
6710259SAndrew.Bardsley@arm.com                               ", ".join(self.param_strings))
6810259SAndrew.Bardsley@arm.com
6910259SAndrew.Bardsley@arm.com    def writeCodeFiles(self, path):
7010259SAndrew.Bardsley@arm.com        '''This write a function of object Chip'''
7110259SAndrew.Bardsley@arm.com        if "external" in self:
7210259SAndrew.Bardsley@arm.com            return
7310259SAndrew.Bardsley@arm.com
7410259SAndrew.Bardsley@arm.com        code = self.symtab.codeFormatter()
7510259SAndrew.Bardsley@arm.com
7610259SAndrew.Bardsley@arm.com        # Header
7710259SAndrew.Bardsley@arm.com        code('''
7810259SAndrew.Bardsley@arm.com/** Auto generated C++ code started by $__file__:$__line__ */
7910259SAndrew.Bardsley@arm.com
8010259SAndrew.Bardsley@arm.com#include "mem/protocol/Types.hh"
8110259SAndrew.Bardsley@arm.com''')
8210259SAndrew.Bardsley@arm.com
8310259SAndrew.Bardsley@arm.com        if self.isInternalMachineFunc:
8410259SAndrew.Bardsley@arm.com            code('#include "mem/protocol/${{self.machineStr}}_Controller.hh"')
8510259SAndrew.Bardsley@arm.com
8610259SAndrew.Bardsley@arm.com        code('using namespace std;')
8710259SAndrew.Bardsley@arm.com        # Generate function header
8810259SAndrew.Bardsley@arm.com        void_type = self.symtab.find("void", Type)
8910259SAndrew.Bardsley@arm.com        return_type = self.return_type.c_ident
9010259SAndrew.Bardsley@arm.com        if "return_by_ref" in self and self.return_type != void_type:
9110259SAndrew.Bardsley@arm.com            return_type += "&"
9210259SAndrew.Bardsley@arm.com        if "return_by_pointer" in self and self.return_type != void_type:
9310259SAndrew.Bardsley@arm.com            return_type += "*"
9410259SAndrew.Bardsley@arm.com
9510259SAndrew.Bardsley@arm.com        if self.isInternalMachineFunc:
9610259SAndrew.Bardsley@arm.com            klass = "%s_Controller" % self.machineStr
9710259SAndrew.Bardsley@arm.com        else:
9810259SAndrew.Bardsley@arm.com            klass = "Chip"
9910259SAndrew.Bardsley@arm.com
10010259SAndrew.Bardsley@arm.com        params = ', '.join(self.param_strings)
10110259SAndrew.Bardsley@arm.com
10210259SAndrew.Bardsley@arm.com        code('''
10310259SAndrew.Bardsley@arm.com$return_type
10410259SAndrew.Bardsley@arm.com${klass}::${{self.c_ident}}($params)
10513709Sandreas.sandberg@arm.com{
10610259SAndrew.Bardsley@arm.com${{self.body}}
10710259SAndrew.Bardsley@arm.com}
10810259SAndrew.Bardsley@arm.com''')
10910259SAndrew.Bardsley@arm.com        code.write(path, "%s.cc" % self.c_ident)
11010259SAndrew.Bardsley@arm.com
11110259SAndrew.Bardsley@arm.com__all__ = [ "Func" ]
11210259SAndrew.Bardsley@arm.com