IfStatementAST.py revision 7839
16019Shines@cs.fsu.edu# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
212509Schuan.zhu@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company
37093Sgblack@eecs.umich.edu# All rights reserved.
47093Sgblack@eecs.umich.edu#
57093Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
67093Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
77093Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
87093Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
97093Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
107093Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
117093Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
127093Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
137093Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
146019Shines@cs.fsu.edu# this software without specific prior written permission.
156019Shines@cs.fsu.edu#
166019Shines@cs.fsu.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176019Shines@cs.fsu.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186019Shines@cs.fsu.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196019Shines@cs.fsu.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206019Shines@cs.fsu.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216019Shines@cs.fsu.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226019Shines@cs.fsu.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236019Shines@cs.fsu.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246019Shines@cs.fsu.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256019Shines@cs.fsu.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266019Shines@cs.fsu.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276019Shines@cs.fsu.edu
286019Shines@cs.fsu.edufrom slicc.ast.StatementAST import StatementAST
296019Shines@cs.fsu.edufrom slicc.symbols import Type
306019Shines@cs.fsu.edu
316019Shines@cs.fsu.educlass IfStatementAST(StatementAST):
326019Shines@cs.fsu.edu    def __init__(self, slicc, cond, then, else_):
336019Shines@cs.fsu.edu        super(IfStatementAST, self).__init__(slicc)
346019Shines@cs.fsu.edu
356019Shines@cs.fsu.edu        assert cond is not None
366019Shines@cs.fsu.edu        assert then is not None
376019Shines@cs.fsu.edu
386019Shines@cs.fsu.edu        self.cond = cond
396019Shines@cs.fsu.edu        self.then = then
406019Shines@cs.fsu.edu        self.else_ = else_
416735Sgblack@eecs.umich.edu
426735Sgblack@eecs.umich.edu    def __repr__(self):
4310037SARM gem5 Developers        return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
4410037SARM gem5 Developers
456019Shines@cs.fsu.edu    def generate(self, code, return_type):
466019Shines@cs.fsu.edu        cond_code = self.slicc.codeFormatter()
476019Shines@cs.fsu.edu        cond_type = self.cond.generate(cond_code)
4811793Sbrandon.potter@amd.com
4911793Sbrandon.potter@amd.com        if cond_type != self.symtab.find("bool", Type):
5010037SARM gem5 Developers            self.cond.error("Condition of if stmt must be bool, type was '%s'",
5110037SARM gem5 Developers                            ctype)
5210037SARM gem5 Developers
538229Snate@binkert.org        # Conditional
548229Snate@binkert.org        code.indent()
556019Shines@cs.fsu.edu        code('if ($cond_code) {')
568232Snate@binkert.org        # Then part
578782Sgblack@eecs.umich.edu        code.indent()
586019Shines@cs.fsu.edu        self.symtab.pushFrame()
596019Shines@cs.fsu.edu        self.then.generate(code, return_type)
606019Shines@cs.fsu.edu        self.symtab.popFrame()
616019Shines@cs.fsu.edu        code.dedent()
6210037SARM gem5 Developers        # Else part
6310037SARM gem5 Developers        if self.else_:
6410037SARM gem5 Developers            code('} else {')
6510037SARM gem5 Developers            code.indent()
6610037SARM gem5 Developers            self.symtab.pushFrame()
6710037SARM gem5 Developers            self.else_.generate(code, return_type)
6810037SARM gem5 Developers            self.symtab.popFrame()
6910037SARM gem5 Developers            code.dedent()
7010037SARM gem5 Developers        code('}') # End scope
7110037SARM gem5 Developers
7210037SARM gem5 Developers    def findResources(self, resources):
7310037SARM gem5 Developers        # Take a worse case look at both paths
7410037SARM gem5 Developers        self.then.findResources(resources)
7510037SARM gem5 Developers        if self.else_ is not None:
7610037SARM gem5 Developers            self.else_.findResources(resources)
7710037SARM gem5 Developers