OperatorExprAST.py revision 6657
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.ExprAST import ExprAST
31from slicc.symbols import Type
32
33class InfixOperatorExprAST(ExprAST):
34    def __init__(self, slicc, left, op, right):
35        super(InfixOperatorExprAST, self).__init__(slicc)
36
37        self.left = left
38        self.op = op
39        self.right = right
40
41    def __repr__(self):
42        return "[InfixExpr: %r %s %r]" % (self.left, self.op, self.right)
43
44    def generate(self, code):
45        lcode = code_formatter()
46        rcode = code_formatter()
47
48        ltype = self.left.generate(lcode)
49        rtype = self.right.generate(rcode)
50
51        # Figure out what the input and output types should be
52        if self.op in ("==", "!="):
53            output = "bool"
54            if (ltype != rtype):
55                self.error("Type mismatch: left and right operands of " +
56                           "operator '%s' must be the same type. " +
57                           "left: '%s', right: '%s'",
58                           self.op, ltype, rtype)
59        else:
60            if self.op in ("&&", "||"):
61                # boolean inputs and output
62                inputs = "bool"
63                output = "bool"
64            elif self.op in ("==", "!=", ">=", "<=", ">", "<"):
65                # Integer inputs, boolean output
66                inputs = "int"
67                output = "bool"
68            else:
69                # integer inputs and output
70                inputs = "int"
71                output = "int"
72
73            inputs_type = self.symtab.find(inputs, Type)
74
75            if inputs_type != ltype:
76                self.left.error("Type mismatch: left operand of operator " +
77                                "'%s' expects type '%s', actual was '%s'",
78                                self.op, inputs, ltype)
79
80            if inputs_type != rtype:
81                self.right.error("Type mismatch: right operand of operator " +
82                                 "'%s' expects type '%s', actual was '%s'",
83                                 self.op, inputs, rtype)
84
85        # All is well
86        fix = code.nofix()
87        code("($lcode ${{self.op}} $rcode)")
88        code.fix(fix)
89        return self.symtab.find(output, Type)
90