EnqueueStatementAST.py revision 10472:399f35ed5cca
14519Sgblack@eecs.umich.edu# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 24519Sgblack@eecs.umich.edu# Copyright (c) 2009 The Hewlett-Packard Development Company 34519Sgblack@eecs.umich.edu# All rights reserved. 44519Sgblack@eecs.umich.edu# 54519Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without 64519Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are 74519Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright 84519Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer; 94519Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright 104519Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the 114519Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution; 124519Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its 134519Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from 144519Sgblack@eecs.umich.edu# this software without specific prior written permission. 154519Sgblack@eecs.umich.edu# 164519Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 174519Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 184519Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 194519Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 204519Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214519Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224519Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 234519Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244519Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254519Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264519Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274519Sgblack@eecs.umich.edu 284519Sgblack@eecs.umich.edufrom slicc.ast.StatementAST import StatementAST 294519Sgblack@eecs.umich.edufrom slicc.symbols import Var 304519Sgblack@eecs.umich.edu 314519Sgblack@eecs.umich.educlass EnqueueStatementAST(StatementAST): 324519Sgblack@eecs.umich.edu def __init__(self, slicc, queue_name, type_ast, lexpr, statements): 334519Sgblack@eecs.umich.edu super(EnqueueStatementAST, self).__init__(slicc) 344519Sgblack@eecs.umich.edu 354519Sgblack@eecs.umich.edu self.queue_name = queue_name 364519Sgblack@eecs.umich.edu self.type_ast = type_ast 374519Sgblack@eecs.umich.edu self.latexpr = lexpr 384519Sgblack@eecs.umich.edu self.statements = statements 394519Sgblack@eecs.umich.edu 404519Sgblack@eecs.umich.edu def __repr__(self): 414519Sgblack@eecs.umich.edu return "[EnqueueStatementAst: %s %s %s]" % \ 424519Sgblack@eecs.umich.edu (self.queue_name, self.type_ast.ident, self.statements) 434519Sgblack@eecs.umich.edu 444519Sgblack@eecs.umich.edu def generate(self, code, return_type): 454519Sgblack@eecs.umich.edu code("{") 464519Sgblack@eecs.umich.edu code.indent() 474519Sgblack@eecs.umich.edu self.symtab.pushFrame() 484519Sgblack@eecs.umich.edu 494519Sgblack@eecs.umich.edu msg_type = self.type_ast.type 504519Sgblack@eecs.umich.edu 514519Sgblack@eecs.umich.edu # Add new local var to symbol table 524519Sgblack@eecs.umich.edu v = Var(self.symtab, "out_msg", self.location, msg_type, "*out_msg", 534519Sgblack@eecs.umich.edu self.pairs) 544519Sgblack@eecs.umich.edu self.symtab.newSymbol(v) 554519Sgblack@eecs.umich.edu 564519Sgblack@eecs.umich.edu # Declare message 574519Sgblack@eecs.umich.edu code("std::shared_ptr<${{msg_type.ident}}> out_msg = "\ 584519Sgblack@eecs.umich.edu "std::make_shared<${{msg_type.ident}}>(clockEdge());") 594519Sgblack@eecs.umich.edu 604519Sgblack@eecs.umich.edu # The other statements 614519Sgblack@eecs.umich.edu t = self.statements.generate(code, None) 624519Sgblack@eecs.umich.edu self.queue_name.assertType("OutPort") 634519Sgblack@eecs.umich.edu 644519Sgblack@eecs.umich.edu if self.latexpr != None: 654519Sgblack@eecs.umich.edu ret_type, rcode = self.latexpr.inline(True) 664519Sgblack@eecs.umich.edu code("(${{self.queue_name.var.code}}).enqueue(" \ 674519Sgblack@eecs.umich.edu "out_msg, Cycles($rcode));") 684809Sgblack@eecs.umich.edu else: 694519Sgblack@eecs.umich.edu code("(${{self.queue_name.var.code}}).enqueue(out_msg);") 704519Sgblack@eecs.umich.edu 714688Sgblack@eecs.umich.edu # End scope 724688Sgblack@eecs.umich.edu self.symtab.popFrame() 734688Sgblack@eecs.umich.edu code.dedent() 744688Sgblack@eecs.umich.edu code("}") 754688Sgblack@eecs.umich.edu 764688Sgblack@eecs.umich.edu def findResources(self, resources): 774708Sgblack@eecs.umich.edu var = self.queue_name.var 784708Sgblack@eecs.umich.edu res_count = int(resources.get(var, 0)) 794708Sgblack@eecs.umich.edu resources[var] = str(res_count + 1) 804708Sgblack@eecs.umich.edu