FuncDeclAST.py revision 10307
12SN/A# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
210298Salexandru.dutu@amd.com# Copyright (c) 2009 The Hewlett-Packard Development Company
38852Sandreas.hansson@arm.com# All rights reserved.
48852Sandreas.hansson@arm.com#
58852Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
68852Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
78852Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
88852Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
98852Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
108852Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
118852Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
128852Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
138852Sandreas.hansson@arm.com# contributors may be used to endorse or promote products derived from
148852Sandreas.hansson@arm.com# this software without specific prior written permission.
151762SN/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.
272SN/A
282SN/Afrom slicc.ast.DeclAST import DeclAST
292SN/Afrom slicc.symbols import Func, Type
302SN/A
312SN/Aclass FuncDeclAST(DeclAST):
322SN/A    def __init__(self, slicc, return_type, ident, formals, pairs, statements):
332SN/A        super(FuncDeclAST, self).__init__(slicc, pairs)
342SN/A
352SN/A        self.return_type = return_type
362SN/A        self.ident = ident
372SN/A        self.formals = formals
382SN/A        self.statements = statements
392SN/A
402665Ssaidi@eecs.umich.edu    def __repr__(self):
412665Ssaidi@eecs.umich.edu        return "[FuncDecl: %s]" % self.ident
422665Ssaidi@eecs.umich.edu
432665Ssaidi@eecs.umich.edu    def files(self, parent=None):
442SN/A        return set()
452SN/A
4611793Sbrandon.potter@amd.com    def generate(self, parent = None):
4711793Sbrandon.potter@amd.com        types = []
488229Snate@binkert.org        params = []
492SN/A        void_type = self.symtab.find("void", Type)
506712Snate@binkert.org
5111800Sbrandon.potter@amd.com        # Generate definition code
5210930Sbrandon.potter@amd.com        self.symtab.pushFrame()
532SN/A
5411800Sbrandon.potter@amd.com        # Lookup return type
552SN/A        return_type = self.return_type.type
5611793Sbrandon.potter@amd.com
5756SN/A        # Generate function header
581158SN/A        for formal in self.formals:
59146SN/A            # Lookup parameter types
606658Snate@binkert.org            try:
612680Sktlim@umich.edu                type, ident = formal.generate()
622378SN/A                types.append(type)
638706Sandreas.hansson@arm.com                params.append(ident)
648229Snate@binkert.org
655154Sgblack@eecs.umich.edu            except AttributeError:
6611800Sbrandon.potter@amd.com                types.append(formal.type)
6711794Sbrandon.potter@amd.com                params.append(None)
682378SN/A
692SN/A        body = self.slicc.codeFormatter()
702715Sstever@eecs.umich.edu        if self.statements is None:
712715Sstever@eecs.umich.edu            self["external"] = "yes"
722715Sstever@eecs.umich.edu        else:
732715Sstever@eecs.umich.edu            rtype = self.statements.generate(body, return_type)
742715Sstever@eecs.umich.edu
752715Sstever@eecs.umich.edu        self.symtab.popFrame()
762715Sstever@eecs.umich.edu
775335Shines@cs.fsu.edu        machine = self.state_machine
785335Shines@cs.fsu.edu        func = Func(self.symtab, self.ident, self.location, return_type,
7910810Sbr@bsdpad.com                    types, params, str(body), self.pairs)
804157Sgblack@eecs.umich.edu
814166Sgblack@eecs.umich.edu        if parent is not None:
826691Stjones1@inf.ed.ac.uk            if not parent.addFunc(func):
836691Stjones1@inf.ed.ac.uk                self.error("Duplicate method: %s:%s()" % (parent, self.ident))
8411723Sar4jc@virginia.edu            func.class_name = parent.c_ident
8511723Sar4jc@virginia.edu
862715Sstever@eecs.umich.edu        elif machine is not None:
872715Sstever@eecs.umich.edu            machine.addFunc(func)
882715Sstever@eecs.umich.edu            func.isInternalMachineFunc = True
892715Sstever@eecs.umich.edu            func.class_name = "%s_Controller" % machine
902715Sstever@eecs.umich.edu        else:
912SN/A            self.symtab.newSymbol(func)
922107SN/A