315c9fadfd1528337b09471905f7479438aa2503
[repair.git] / Repair / RepairCompiler / MCC / IR / OpExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class OpExpr extends Expr {
6
7     Expr left;
8     Expr right;
9     Opcode opcode;
10
11     public OpExpr(Opcode opcode, Expr left, Expr right) {
12         this.opcode = opcode;
13         this.left = left;
14         this.right = right;
15
16         assert (right == null && opcode == Opcode.NOT) || (right != null);
17     }
18
19     public Set getInversedRelations() {
20         Set set = left.getInversedRelations();
21         if (right != null) {
22             set.addAll(right.getInversedRelations());
23         }
24         return set;
25     }
26
27     public Set getRequiredDescriptors() {
28         Set v = left.getRequiredDescriptors();
29      
30         if (right != null) {
31             v.addAll(right.getRequiredDescriptors());
32         }
33
34         return v;
35     }   
36
37     public void generate(CodeWriter writer, VarDescriptor dest) {
38         VarDescriptor ld = VarDescriptor.makeNew("leftop");
39         left.generate(writer, ld);        
40         VarDescriptor rd = null;
41
42         if (right != null) {
43             rd = VarDescriptor.makeNew("rightop");
44             right.generate(writer, rd);
45         }
46
47         String code;
48         if (opcode != Opcode.NOT) { /* two operands */
49             assert rd != null;
50             writer.outputline("int " + dest.getSafeSymbol() + " = " + 
51                               ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
52         } else {
53             writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
54         }
55     }
56
57     public void prettyPrint(PrettyPrinter pp) {
58         pp.output("(");
59         if (opcode == Opcode.NOT) {
60             left.prettyPrint(pp);
61         } else {           
62             left.prettyPrint(pp);
63             pp.output(" " + opcode.toString() + " ");
64             assert right != null;
65             right.prettyPrint(pp);
66         }
67         pp.output(")");
68     }
69
70     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
71         TypeDescriptor lt = left.typecheck(sa);
72         TypeDescriptor rt = right == null ? null : right.typecheck(sa);
73
74         if (lt == null) {
75             return null;
76         } else if (right != null && rt == null) {
77             return null;
78         }
79
80         boolean ok = true;
81
82         // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
83         // either that or we use a isvalid clause to check for null
84
85         /*
86         if (lt != ReservedTypeDescriptor.INT) {
87             sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
88             ok = false;
89         }
90
91         if (right != null) {
92             if (rt != ReservedTypeDescriptor.INT) {
93                 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
94                 ok = false;
95             }
96         }
97         */
98
99         if (!ok) {
100             return null;
101         }
102
103         this.td = ReservedTypeDescriptor.INT;
104         return this.td;
105     }
106
107 }
108
109
110
111
112