OperatorExprAST.py revision 10965:6f433e7f9767
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 slicc.ast.ExprAST import ExprAST
29from slicc.symbols import Type
30
31class InfixOperatorExprAST(ExprAST):
32    def __init__(self, slicc, left, op, right):
33        super(InfixOperatorExprAST, self).__init__(slicc)
34
35        self.left = left
36        self.op = op
37        self.right = right
38
39    def __repr__(self):
40        return "[InfixExpr: %r %s %r]" % (self.left, self.op, self.right)
41
42    def generate(self, code):
43        lcode = self.slicc.codeFormatter()
44        rcode = self.slicc.codeFormatter()
45
46        ltype = self.left.generate(lcode)
47        rtype = self.right.generate(rcode)
48
49        # Figure out what the input and output types should be
50        if self.op in ("==", "!=", ">=", "<=", ">", "<"):
51            output = "bool"
52            if (ltype != rtype):
53                self.error("Type mismatch: left and right operands of " +
54                           "operator '%s' must be the same type. " +
55                           "left: '%s', right: '%s'",
56                           self.op, ltype, rtype)
57        else:
58            expected_types = []
59            output = None
60
61            if self.op in ("&&", "||"):
62                # boolean inputs and output
63                expected_types = [("bool", "bool", "bool")]
64            elif self.op in ("<<", ">>"):
65                expected_types = [("int", "int", "int"),
66                                  ("Cycles", "int", "Cycles")]
67            elif self.op in ("+", "-", "*", "/"):
68                expected_types = [("int", "int", "int"),
69                                  ("Cycles", "Cycles", "Cycles"),
70                                  ("Cycles", "int", "Cycles"),
71                                  ("Scalar", "int", "Scalar"),
72                                  ("int", "bool", "int"),
73                                  ("bool", "int", "int"),
74                                  ("int", "Cycles", "Cycles")]
75            else:
76                self.error("No operator matched with {0}!" .format(self.op))
77
78            for expected_type in expected_types:
79                left_input_type = self.symtab.find(expected_type[0], Type)
80                right_input_type = self.symtab.find(expected_type[1], Type)
81
82                if (left_input_type == ltype) and (right_input_type == rtype):
83                    output = expected_type[2]
84
85            if output == None:
86                self.error("Type mismatch: operands ({0}, {1}) for operator " \
87                           "'{2}' failed to match with the expected types" .
88                           format(ltype, rtype, self.op))
89
90        # All is well
91        fix = code.nofix()
92        code("($lcode ${{self.op}} $rcode)")
93        code.fix(fix)
94        return self.symtab.find(output, Type)
95
96class PrefixOperatorExprAST(ExprAST):
97    def __init__(self, slicc, op, operand):
98        super(PrefixOperatorExprAST, self).__init__(slicc)
99
100        self.op = op
101        self.operand = operand
102
103    def __repr__(self):
104        return "[PrefixExpr: %s %r]" % (self.op, self.operand)
105
106    def generate(self, code):
107        opcode = self.slicc.codeFormatter()
108        optype = self.operand.generate(opcode)
109
110        # Figure out what the input and output types should be
111        opmap = {"!": "bool", "-": "int", "++": "Scalar"}
112        if self.op in opmap:
113            output = opmap[self.op]
114            type_in_symtab = self.symtab.find(opmap[self.op], Type)
115            if (optype != type_in_symtab):
116                self.error("Type mismatch: right operand of " +
117                           "unary operator '%s' must be of type '%s'. ",
118                           self.op, type_in_symtab)
119        else:
120            self.error("Invalid prefix operator '%s'",
121                       self.op)
122
123        # All is well
124        fix = code.nofix()
125        code("(${{self.op}} $opcode)")
126        code.fix(fix)
127
128        return self.symtab.find(output, Type)
129