IfStatementAST.py revision 6657:ef5fae93a3b2
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2# Copyright (c) 2009 The Hewlett-Packard Development Company
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28from m5.util import code_formatter
29
30from slicc.ast.StatementAST import StatementAST
31from slicc.symbols import Type
32
33class IfStatementAST(StatementAST):
34    def __init__(self, slicc, cond, then, else_):
35        super(IfStatementAST, self).__init__(slicc)
36
37        assert cond is not None
38        assert then is not None
39
40        self.cond = cond
41        self.then = then
42        self.else_ = else_
43
44    def __repr__(self):
45        return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
46
47    def generate(self, code, return_type):
48        cond_code = code_formatter()
49        cond_type = self.cond.generate(cond_code)
50
51        if cond_type != self.symtab.find("bool", Type):
52            self.cond.error("Condition of if stmt must be bool, type was '%s'",
53                            ctype)
54
55        # Conditional
56        code.indent()
57        code('if ($cond_code) {')
58        # Then part
59        code.indent()
60        self.then.generate(code, return_type)
61        code.dedent()
62        # Else part
63        if self.else_:
64            code('} else {')
65            code.indent()
66            self.else_.generate(code, return_type)
67            code.dedent()
68        code('}') # End scope
69
70    def findResources(self, resources):
71        # Take a worse case look at both paths
72        self.then.findResources(resources)
73        if self.else_ is not None:
74            self.else_.findResources(resources)
75