FuncDeclAST.py revision 6657
19288Sandreas.hansson@arm.com# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 29288Sandreas.hansson@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company 39288Sandreas.hansson@arm.com# All rights reserved. 49288Sandreas.hansson@arm.com# 59288Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without 69288Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are 79288Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright 89288Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer; 99288Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright 109288Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the 119288Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution; 129288Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its 139288Sandreas.hansson@arm.com# contributors may be used to endorse or promote products derived from 149288Sandreas.hansson@arm.com# this software without specific prior written permission. 159288Sandreas.hansson@arm.com# 169288Sandreas.hansson@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179288Sandreas.hansson@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189288Sandreas.hansson@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199288Sandreas.hansson@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209288Sandreas.hansson@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219288Sandreas.hansson@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229288Sandreas.hansson@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239288Sandreas.hansson@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249288Sandreas.hansson@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259288Sandreas.hansson@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269288Sandreas.hansson@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279288Sandreas.hansson@arm.com 289288Sandreas.hansson@arm.comfrom m5.util.code_formatter import code_formatter 299288Sandreas.hansson@arm.com 309288Sandreas.hansson@arm.comfrom slicc.ast.DeclAST import DeclAST 319288Sandreas.hansson@arm.comfrom slicc.symbols import Func, Type 329288Sandreas.hansson@arm.com 339288Sandreas.hansson@arm.comclass FuncDeclAST(DeclAST): 349288Sandreas.hansson@arm.com def __init__(self, slicc, return_type, ident, formals, pairs, statements): 359288Sandreas.hansson@arm.com super(FuncDeclAST, self).__init__(slicc, pairs) 369288Sandreas.hansson@arm.com 379288Sandreas.hansson@arm.com self.return_type = return_type 389288Sandreas.hansson@arm.com self.ident = ident 399288Sandreas.hansson@arm.com self.formals = formals 409288Sandreas.hansson@arm.com self.statements = statements 419288Sandreas.hansson@arm.com 428831Smrinmoy.ghosh@arm.com def __repr__(self): 438832SAli.Saidi@ARM.com return "[FuncDecl: %s]" % self.ident 448832SAli.Saidi@ARM.com 459288Sandreas.hansson@arm.com def files(self, hh, cc, parent=None): 468831Smrinmoy.ghosh@arm.com if "external" in self or self.statements is None: 478831Smrinmoy.ghosh@arm.com return 489338SAndreas.Sandberg@arm.com 498831Smrinmoy.ghosh@arm.com if parent: 508831Smrinmoy.ghosh@arm.com ident = "%s_%s" % (parent, self.ident) 518831Smrinmoy.ghosh@arm.com else: 528831Smrinmoy.ghosh@arm.com ident = self.ident 538831Smrinmoy.ghosh@arm.com cc.add("%s.cc" % ident) 548831Smrinmoy.ghosh@arm.com 558831Smrinmoy.ghosh@arm.com def generate(self): 568831Smrinmoy.ghosh@arm.com types = [] 579288Sandreas.hansson@arm.com params = [] 588832SAli.Saidi@ARM.com void_type = self.symtab.find("void", Type) 598832SAli.Saidi@ARM.com 608831Smrinmoy.ghosh@arm.com # Generate definition code 618831Smrinmoy.ghosh@arm.com self.symtab.pushFrame() 628832SAli.Saidi@ARM.com 638831Smrinmoy.ghosh@arm.com # Lookup return type 648831Smrinmoy.ghosh@arm.com return_type = self.return_type.type 658831Smrinmoy.ghosh@arm.com 668831Smrinmoy.ghosh@arm.com # Generate function header 679338SAndreas.Sandberg@arm.com for formal in self.formals: 688831Smrinmoy.ghosh@arm.com # Lookup parameter types 698831Smrinmoy.ghosh@arm.com type, ident = formal.generate() 708831Smrinmoy.ghosh@arm.com types.append(type) 718831Smrinmoy.ghosh@arm.com params.append(ident) 729338SAndreas.Sandberg@arm.com 738831Smrinmoy.ghosh@arm.com body = code_formatter() 748831Smrinmoy.ghosh@arm.com if self.statements is None: 758831Smrinmoy.ghosh@arm.com self["external"] = "yes" 768831Smrinmoy.ghosh@arm.com else: 779338SAndreas.Sandberg@arm.com rtype = self.statements.generate(body, return_type) 788831Smrinmoy.ghosh@arm.com 798831Smrinmoy.ghosh@arm.com self.symtab.popFrame() 808831Smrinmoy.ghosh@arm.com 818831Smrinmoy.ghosh@arm.com machine = self.state_machine 82 func = Func(self.symtab, self.ident, self.location, return_type, 83 types, params, str(body), self.pairs, machine) 84 85 if machine is not None: 86 machine.addFunc(func) 87 else: 88 self.symtab.newSymbol(func) 89