6ca7d6520cfad7014cc8b84bc5fdcd87990d1c2a
[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 getRequiredDescriptors() {
20         Set v = left.getRequiredDescriptors();
21      
22         if (right != null) {
23             v.addAll(right.getRequiredDescriptors());
24         }
25
26         return v;
27     }   
28
29     public void generate(CodeWriter writer, VarDescriptor dest) {
30         VarDescriptor ld = VarDescriptor.makeNew("leftop");
31         left.generate(writer, ld);        
32         VarDescriptor rd = null;
33
34         if (right != null) {
35             rd = VarDescriptor.makeNew("rightop");
36             right.generate(writer, rd);
37         }
38
39         String code;
40         if (opcode != Opcode.NOT) { /* two operands */
41             assert rd != null;
42             writer.outputline("int " + dest.getSafeSymbol() + " = " + 
43                               ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
44         } else {
45             writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
46         }
47     }
48
49     public void prettyPrint(PrettyPrinter pp) {
50         if (opcode == Opcode.NOT) {
51             pp.output("!");
52             left.prettyPrint(pp);
53         } else {            
54             left.prettyPrint(pp);
55             pp.output(" " + opcode.toString() + " ");
56             assert right != null;
57             right.prettyPrint(pp);
58         }
59     }
60
61     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
62         TypeDescriptor lt = left.typecheck(sa);
63         TypeDescriptor rt = right == null ? null : right.typecheck(sa);
64
65         if (lt == null) {
66             return null;
67         } else if (right != null && rt == null) {
68             return null;
69         }
70
71         boolean ok = true;
72
73         if (lt != ReservedTypeDescriptor.INT) {
74             sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
75             ok = false;
76         }
77
78         if (right != null) {
79             if (rt != ReservedTypeDescriptor.INT) {
80                 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
81                 ok = false;
82             }
83         }
84
85         if (!ok) {
86             return null;
87         }
88
89         this.td = ReservedTypeDescriptor.INT;
90         return this.td;
91     }
92
93 }
94
95
96
97
98