5 public class OpExpr extends Expr {
11 public Expr getUpper() {
12 Expr lupper=left.getUpper();
16 Expr rupper=right.getUpper();
19 OpExpr oe=new OpExpr(this.opcode,lupper,rupper);
20 oe.td = ReservedTypeDescriptor.INT;
25 public Expr getLower() {
26 Expr llower=left.getLower();
30 Expr rlower=right.getLower();
33 OpExpr oe=new OpExpr(this.opcode,llower,rlower);
34 oe.td = ReservedTypeDescriptor.INT;
40 public boolean isSafe() {
43 return left.isSafe()&&right.isSafe();
46 public boolean isInvariant(Set vars) {
47 return left.isInvariant(vars)&&((right==null)||right.isInvariant(vars));
50 public Set findInvariants(Set vars) {
52 /* Don't hoist ints */
54 } else if (isInvariant(vars)) {
59 Set ls=left.findInvariants(vars);
61 ls.addAll(right.findInvariants(vars));
66 public Set getfunctions() {
67 Set leftfunctions=left.getfunctions();
68 Set rightfunctions=null;
69 if (right!=null) rightfunctions=right.getfunctions();
70 if (leftfunctions!=null&&rightfunctions!=null) {
71 HashSet functions=new HashSet();
72 functions.addAll(leftfunctions);
73 functions.addAll(rightfunctions);
76 if (leftfunctions!=null)
78 return rightfunctions;
81 public void findmatch(Descriptor d, Set s) {
87 public static boolean isInt(Expr e) {
90 if ((e instanceof IntegerLiteralExpr)||
91 ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
96 public static int getInt(Expr e) {
97 if (e instanceof IntegerLiteralExpr)
98 return ((IntegerLiteralExpr)e).getValue();
99 else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
100 return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
101 else throw new Error();
104 public OpExpr(Opcode opcode, Expr left, Expr right) {
105 if ((isInt(left)&&isInt(right))||
106 (isInt(left)&&(opcode==Opcode.NOT))||
107 (isInt(left)&&(opcode==Opcode.RND))) {
108 this.opcode=Opcode.NOP;
110 int lint=isInt(left)?getInt(left):0;
111 int rint=isInt(right)?getInt(right):0;
113 if (opcode==Opcode.ADD) {
115 } else if (opcode==Opcode.SUB) {
117 } else if (opcode==Opcode.SHL) {
119 } else if (opcode==Opcode.SHR) {
121 } else if (opcode==Opcode.MULT) {
123 } else if (opcode==Opcode.DIV) {
125 } else if (opcode==Opcode.GT) {
128 } else if (opcode==Opcode.GE) {
131 } else if (opcode==Opcode.LT) {
134 } else if (opcode==Opcode.LE) {
137 } else if (opcode==Opcode.EQ) {
140 } else if (opcode==Opcode.NE) {
143 } else if (opcode==Opcode.AND) {
144 if ((lint!=0)&&(rint!=0))
146 } else if (opcode==Opcode.OR) {
147 if ((lint!=0)||(rint!=0))
149 } else if (opcode==Opcode.NOT) {
152 } else if (opcode==Opcode.RND) {
153 value=((lint>>3)<<3);
156 } else throw new Error("Unrecognized Opcode");
157 this.left=new IntegerLiteralExpr(value);
158 } else if ((opcode==Opcode.MULT)&&
159 ((isInt(left)&&(getInt(left)==0))
160 ||(isInt(right)&&(getInt(right)==0)))) {
161 this.opcode=Opcode.NOP;
163 this.left=new IntegerLiteralExpr(0);
165 this.opcode = opcode;
168 assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
172 public Expr getRightExpr() {
176 public Expr getLeftExpr() {
180 public Set freeVars() {
181 Set lset=left.freeVars();
184 rset=right.freeVars();
192 public String name() {
193 if (opcode==Opcode.NOT)
194 return "!("+left.name()+")";
195 if (opcode==Opcode.NOP)
197 if (opcode==Opcode.RND)
198 return "Round("+left.name()+")";
199 String name=left.name()+opcode.toString();
205 public Opcode getOpcode() {
209 public boolean equals(Map remap, Expr e) {
210 if (e==null||!(e instanceof OpExpr))
213 if (opcode!=oe.opcode)
215 if (!left.equals(remap,oe.left))
217 if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
218 if (!right.equals(remap,oe.right))
223 public DNFRule constructDNF() {
224 if (opcode==Opcode.AND) {
225 DNFRule leftd=left.constructDNF();
226 DNFRule rightd=right.constructDNF();
227 return leftd.and(rightd);
228 } else if (opcode==Opcode.OR) {
229 DNFRule leftd=left.constructDNF();
230 DNFRule rightd=right.constructDNF();
231 return leftd.or(rightd);
232 } else if (opcode==Opcode.NOT) {
233 DNFRule leftd=left.constructDNF();
235 } else return new DNFRule(this);
238 public boolean usesDescriptor(Descriptor d) {
239 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
242 public Set useDescriptor(Descriptor d) {
243 HashSet newset=new HashSet();
244 newset.addAll(left.useDescriptor(d));
246 newset.addAll(right.useDescriptor(d));
250 public int[] getRepairs(boolean negated, Termination t) {
251 if (left instanceof RelationExpr)
252 return new int[] {AbstractRepair.MODIFYRELATION};
253 if (left instanceof SizeofExpr) {
256 /* remove negation through opcode translation */
259 else if (op==Opcode.GE)
261 else if (op==Opcode.EQ)
263 else if (op==Opcode.NE)
265 else if (op==Opcode.LT)
267 else if (op==Opcode.LE)
271 int maxsize=t.maxsize.getsize(getDescriptor());
272 int size=getInt(right);
275 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
279 return new int[] {AbstractRepair.REMOVEFROMRELATION};
281 if ((maxsize!=-1)&&maxsize<=size)
282 return new int[] {AbstractRepair.ADDTORELATION};
283 return new int[] {AbstractRepair.ADDTORELATION,
284 AbstractRepair.REMOVEFROMRELATION};
286 } else if (op==Opcode.GE||op==Opcode.GT) {
287 return new int[]{AbstractRepair.ADDTORELATION};
288 } else if (op==Opcode.LE||op==Opcode.LT) {
289 if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
291 return new int[]{AbstractRepair.REMOVEFROMRELATION};
292 } else if (op==Opcode.NE) {
293 if (maxsize<size&&maxsize!=-1)
295 return new int[]{AbstractRepair.ADDTORELATION};
296 } else throw new Error();
300 return new int[] {AbstractRepair.REMOVEFROMSET};
302 if (maxsize<=size&&maxsize!=-1)
303 return new int[] {AbstractRepair.ADDTOSET};
304 return new int[] {AbstractRepair.ADDTOSET,
305 AbstractRepair.REMOVEFROMSET};
307 } else if (op==Opcode.GE||op==Opcode.GT) {
308 return new int[] {AbstractRepair.ADDTOSET};
309 } else if (op==Opcode.LE||op==Opcode.LT) {
310 if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
312 return new int[] {AbstractRepair.REMOVEFROMSET};
313 } else if (op==Opcode.NE) {
314 if (maxsize<size&&maxsize!=-1)
316 return new int[] {AbstractRepair.ADDTOSET};
317 } else throw new Error();
320 throw new Error("BAD");
323 public Descriptor getDescriptor() {
324 return left.getDescriptor();
327 public boolean inverted() {
328 return left.inverted();
331 public Set getInversedRelations() {
332 Set set = left.getInversedRelations();
334 set.addAll(right.getInversedRelations());
339 public Set getRequiredDescriptors() {
340 Set v = left.getRequiredDescriptors();
343 v.addAll(right.getRequiredDescriptors());
349 public void generate(CodeWriter writer, VarDescriptor dest) {
350 VarDescriptor ld = VarDescriptor.makeNew("leftop");
351 if (writer.getInvariantValue()!=null&&
352 writer.getInvariantValue().isInvariant(this)) {
353 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
354 writer.outputline("int "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
358 left.generate(writer, ld);
359 VarDescriptor rd = null;
360 VarDescriptor lm=VarDescriptor.makeNew("lm");
361 VarDescriptor rm=VarDescriptor.makeNew("rm");
364 if ((opcode==Opcode.OR)||
365 (opcode==Opcode.AND)) {
366 writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
367 writer.outputline("maybe=0;");
370 rd = VarDescriptor.makeNew("rightop");
371 right.generate(writer, rd);
375 if (opcode == Opcode.RND) {
376 writer.outputline("int " +dest.getSafeSymbol() + " = (" +
377 ld.getSafeSymbol() + ">>3)<<3; ");
378 writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
379 } else if (opcode == Opcode.NOP) {
380 writer.outputline("int " +dest.getSafeSymbol() + " = " +
381 ld.getSafeSymbol() +"; ");
382 } else if (opcode == Opcode.AND) {
383 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
384 writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
385 writer.outputline("int "+dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
386 } else if (opcode == Opcode.OR) {
387 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
388 writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
389 " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
390 writer.outputline("int "+dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";");
391 } else if (opcode != Opcode.NOT) { /* two operands */
393 writer.outputline("int " + dest.getSafeSymbol() + " = " +
394 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
395 } else if (opcode == Opcode.NOT) {
396 writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
400 public void prettyPrint(PrettyPrinter pp) {
402 if (opcode == Opcode.NOT) {
404 left.prettyPrint(pp);
405 } else if (opcode == Opcode.NOP) {
406 left.prettyPrint(pp);
407 } else if (opcode == Opcode.RND) {
409 left.prettyPrint(pp);
411 left.prettyPrint(pp);
412 pp.output(" " + opcode.toString() + " ");
413 assert right != null;
414 right.prettyPrint(pp);
419 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
420 TypeDescriptor lt = left.typecheck(sa);
421 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
425 } else if (right != null && rt == null) {
435 this.td = ReservedTypeDescriptor.INT;