5 public class OpExpr extends Expr {
11 public void findmatch(Descriptor d, Set s) {
17 public static boolean isInt(Expr e) {
18 if ((e instanceof IntegerLiteralExpr)||
19 ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
24 public static int getInt(Expr e) {
25 if (e instanceof IntegerLiteralExpr)
26 return ((IntegerLiteralExpr)e).getValue();
27 else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
28 return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
29 else throw new Error();
32 public OpExpr(Opcode opcode, Expr left, Expr right) {
33 if ((isInt(left)&&isInt(right))||
34 (isInt(left)&&(opcode==Opcode.NOT))||
35 (isInt(left)&&(opcode==Opcode.RND))) {
36 this.opcode=Opcode.NOP;
38 int lint=getInt(left);
39 int rint=getInt(right);
41 if (opcode==Opcode.ADD) {
43 } else if (opcode==Opcode.SUB) {
45 } else if (opcode==Opcode.SHL) {
47 } else if (opcode==Opcode.SHR) {
49 } else if (opcode==Opcode.MULT) {
51 } else if (opcode==Opcode.DIV) {
53 } else if (opcode==Opcode.GT) {
56 } else if (opcode==Opcode.GE) {
59 } else if (opcode==Opcode.LT) {
62 } else if (opcode==Opcode.LE) {
65 } else if (opcode==Opcode.EQ) {
68 } else if (opcode==Opcode.NE) {
71 } else if (opcode==Opcode.AND) {
72 if ((lint!=0)&&(rint!=0))
74 } else if (opcode==Opcode.OR) {
75 if ((lint!=0)||(rint!=0))
77 } else if (opcode==Opcode.NOT) {
80 } else if (opcode==Opcode.RND) {
84 } else throw new Error("Unrecognized Opcode");
85 this.left=new IntegerLiteralExpr(value);
86 } else if ((opcode==Opcode.MULT)&&
87 ((isInt(left)&&(getInt(left)==0))
88 ||(isInt(right)&&(getInt(right)==0)))) {
89 this.opcode=Opcode.NOP;
91 this.left=new IntegerLiteralExpr(0);
96 assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
100 public Expr getRightExpr() {
104 public Expr getLeftExpr() {
108 public Set freeVars() {
109 Set lset=left.freeVars();
112 rset=right.freeVars();
120 public String name() {
121 if (opcode==Opcode.NOT)
122 return "!("+left.name()+")";
123 if (opcode==Opcode.NOP)
125 if (opcode==Opcode.RND)
126 return "Round("+left.name()+")";
127 String name=left.name()+opcode.toString();
133 public Opcode getOpcode() {
140 public boolean equals(Map remap, Expr e) {
141 if (e==null||!(e instanceof OpExpr))
144 if (opcode!=oe.opcode)
146 if (!left.equals(remap,oe.left))
148 if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
149 if (!right.equals(remap,oe.right))
154 public DNFRule constructDNF() {
155 if (opcode==Opcode.AND) {
156 DNFRule leftd=left.constructDNF();
157 DNFRule rightd=right.constructDNF();
158 return leftd.and(rightd);
159 } else if (opcode==Opcode.OR) {
160 DNFRule leftd=left.constructDNF();
161 DNFRule rightd=right.constructDNF();
162 return leftd.or(rightd);
163 } else if (opcode==Opcode.NOT) {
164 DNFRule leftd=left.constructDNF();
166 } else return new DNFRule(this);
169 public boolean usesDescriptor(Descriptor d) {
170 if (opcode==Opcode.GT||opcode==Opcode.GE||opcode==Opcode.LT||
171 opcode==Opcode.LE||opcode==Opcode.EQ||opcode==Opcode.NE)
172 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
173 // return right.usesDescriptor(d);
175 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
179 public int[] getRepairs(boolean negated) {
180 if (left instanceof RelationExpr)
181 return new int[] {AbstractRepair.MODIFYRELATION};
182 if (left instanceof SizeofExpr) {
185 /* remove negation through opcode translation */
188 else if (op==Opcode.GE)
190 else if (op==Opcode.EQ)
192 else if (op==Opcode.NE)
194 else if (op==Opcode.LT)
196 else if (op==Opcode.LE)
202 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
205 if (((IntegerLiteralExpr)right).getValue()==0)
206 return new int[] {AbstractRepair.REMOVEFROMRELATION};
208 return new int[] {AbstractRepair.ADDTORELATION,
209 AbstractRepair.REMOVEFROMRELATION};
210 } else if (op==Opcode.GE||op==Opcode.GT) {
211 return new int[]{AbstractRepair.ADDTORELATION};
212 } else if (op==Opcode.LE||op==Opcode.LT) {
213 return new int[]{AbstractRepair.REMOVEFROMRELATION};
214 } else if (op==Opcode.NE) {
215 return new int[]{AbstractRepair.ADDTORELATION};
216 } else throw new Error();
219 if (((IntegerLiteralExpr)right).getValue()==0)
220 return new int[] {AbstractRepair.REMOVEFROMSET};
222 return new int[] {AbstractRepair.ADDTOSET,
223 AbstractRepair.REMOVEFROMSET};
224 } else if (op==Opcode.GE||op==Opcode.GT) {
225 return new int[] {AbstractRepair.ADDTOSET};
226 } else if (op==Opcode.LE||op==Opcode.LT) {
227 return new int[] {AbstractRepair.REMOVEFROMSET};
228 } else if (op==Opcode.NE) {
229 return new int[] {AbstractRepair.ADDTOSET};
230 } else throw new Error();
233 throw new Error("BAD");
236 public Descriptor getDescriptor() {
237 return left.getDescriptor();
240 public boolean inverted() {
241 return left.inverted();
244 public Set getInversedRelations() {
245 Set set = left.getInversedRelations();
247 set.addAll(right.getInversedRelations());
252 public Set getRequiredDescriptors() {
253 Set v = left.getRequiredDescriptors();
256 v.addAll(right.getRequiredDescriptors());
262 public void generate(CodeWriter writer, VarDescriptor dest) {
263 VarDescriptor ld = VarDescriptor.makeNew("leftop");
264 left.generate(writer, ld);
265 VarDescriptor rd = null;
266 VarDescriptor lm=VarDescriptor.makeNew("lm");
267 VarDescriptor rm=VarDescriptor.makeNew("rm");
270 if ((opcode==Opcode.OR)||
271 (opcode==Opcode.AND)) {
272 writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
273 writer.outputline("int maybe=0;");
276 rd = VarDescriptor.makeNew("rightop");
277 right.generate(writer, rd);
281 if (opcode == Opcode.RND) {
282 writer.outputline("int " +dest.getSafeSymbol() + " = (" +
283 ld.getSafeSymbol() + ">>3)<<3; ");
284 writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
285 } else if (opcode == Opcode.NOP) {
286 writer.outputline("int " +dest.getSafeSymbol() + " = " +
287 ld.getSafeSymbol() +"; ");
288 } else if (opcode == Opcode.AND) {
289 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
290 writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
291 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
292 } else if (opcode == Opcode.OR) {
293 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
294 writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
295 " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
296 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
298 } else if (opcode != Opcode.NOT) { /* two operands */
300 writer.outputline("int " + dest.getSafeSymbol() + " = " +
301 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
302 } else if (opcode == Opcode.NOT) {
303 writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
307 public void prettyPrint(PrettyPrinter pp) {
309 if (opcode == Opcode.NOT) {
311 left.prettyPrint(pp);
312 } else if (opcode == Opcode.NOP) {
313 left.prettyPrint(pp);
314 } else if (opcode == Opcode.RND) {
316 left.prettyPrint(pp);
318 left.prettyPrint(pp);
319 pp.output(" " + opcode.toString() + " ");
320 assert right != null;
321 right.prettyPrint(pp);
326 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
327 TypeDescriptor lt = left.typecheck(sa);
328 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
332 } else if (right != null && rt == null) {
338 // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
339 // either that or we use a isvalid clause to check for null
342 if (lt != ReservedTypeDescriptor.INT) {
343 sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
348 if (rt != ReservedTypeDescriptor.INT) {
349 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
359 this.td = ReservedTypeDescriptor.INT;