5 public class OpExpr extends Expr {
11 public boolean isSafe() {
14 return left.isSafe()&&right.isSafe();
17 public Set getfunctions() {
18 Set leftfunctions=left.getfunctions();
19 Set rightfunctions=right.getfunctions();
20 if (leftfunctions!=null&&rightfunctions!=null) {
21 HashSet functions=new HashSet();
22 functions.addAll(leftfunctions);
23 functions.addAll(rightfunctions);
26 if (leftfunctions!=null)
28 return rightfunctions;
31 public void findmatch(Descriptor d, Set s) {
37 public static boolean isInt(Expr e) {
40 if ((e instanceof IntegerLiteralExpr)||
41 ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
46 public static int getInt(Expr e) {
47 if (e instanceof IntegerLiteralExpr)
48 return ((IntegerLiteralExpr)e).getValue();
49 else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
50 return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
51 else throw new Error();
54 public OpExpr(Opcode opcode, Expr left, Expr right) {
55 if ((isInt(left)&&isInt(right))||
56 (isInt(left)&&(opcode==Opcode.NOT))||
57 (isInt(left)&&(opcode==Opcode.RND))) {
58 this.opcode=Opcode.NOP;
60 int lint=isInt(left)?getInt(left):0;
61 int rint=isInt(right)?getInt(right):0;
63 if (opcode==Opcode.ADD) {
65 } else if (opcode==Opcode.SUB) {
67 } else if (opcode==Opcode.SHL) {
69 } else if (opcode==Opcode.SHR) {
71 } else if (opcode==Opcode.MULT) {
73 } else if (opcode==Opcode.DIV) {
75 } else if (opcode==Opcode.GT) {
78 } else if (opcode==Opcode.GE) {
81 } else if (opcode==Opcode.LT) {
84 } else if (opcode==Opcode.LE) {
87 } else if (opcode==Opcode.EQ) {
90 } else if (opcode==Opcode.NE) {
93 } else if (opcode==Opcode.AND) {
94 if ((lint!=0)&&(rint!=0))
96 } else if (opcode==Opcode.OR) {
97 if ((lint!=0)||(rint!=0))
99 } else if (opcode==Opcode.NOT) {
102 } else if (opcode==Opcode.RND) {
103 value=((lint>>3)<<3);
106 } else throw new Error("Unrecognized Opcode");
107 this.left=new IntegerLiteralExpr(value);
108 } else if ((opcode==Opcode.MULT)&&
109 ((isInt(left)&&(getInt(left)==0))
110 ||(isInt(right)&&(getInt(right)==0)))) {
111 this.opcode=Opcode.NOP;
113 this.left=new IntegerLiteralExpr(0);
115 this.opcode = opcode;
118 assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
122 public Expr getRightExpr() {
126 public Expr getLeftExpr() {
130 public Set freeVars() {
131 Set lset=left.freeVars();
134 rset=right.freeVars();
142 public String name() {
143 if (opcode==Opcode.NOT)
144 return "!("+left.name()+")";
145 if (opcode==Opcode.NOP)
147 if (opcode==Opcode.RND)
148 return "Round("+left.name()+")";
149 String name=left.name()+opcode.toString();
155 public Opcode getOpcode() {
162 public boolean equals(Map remap, Expr e) {
163 if (e==null||!(e instanceof OpExpr))
166 if (opcode!=oe.opcode)
168 if (!left.equals(remap,oe.left))
170 if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
171 if (!right.equals(remap,oe.right))
176 public DNFRule constructDNF() {
177 if (opcode==Opcode.AND) {
178 DNFRule leftd=left.constructDNF();
179 DNFRule rightd=right.constructDNF();
180 return leftd.and(rightd);
181 } else if (opcode==Opcode.OR) {
182 DNFRule leftd=left.constructDNF();
183 DNFRule rightd=right.constructDNF();
184 return leftd.or(rightd);
185 } else if (opcode==Opcode.NOT) {
186 DNFRule leftd=left.constructDNF();
188 } else return new DNFRule(this);
191 public boolean usesDescriptor(Descriptor d) {
192 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
195 public Set useDescriptor(Descriptor d) {
196 HashSet newset=new HashSet();
197 newset.addAll(left.useDescriptor(d));
199 newset.addAll(right.useDescriptor(d));
203 public int[] getRepairs(boolean negated, Termination t) {
204 if (left instanceof RelationExpr)
205 return new int[] {AbstractRepair.MODIFYRELATION};
206 if (left instanceof SizeofExpr) {
209 /* remove negation through opcode translation */
212 else if (op==Opcode.GE)
214 else if (op==Opcode.EQ)
216 else if (op==Opcode.NE)
218 else if (op==Opcode.LT)
220 else if (op==Opcode.LE)
224 int maxsize=t.maxsize.getsize(getDescriptor());
225 int size=getInt(right);
228 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
232 return new int[] {AbstractRepair.REMOVEFROMRELATION};
234 if ((maxsize!=-1)&&maxsize<=size)
235 return new int[] {AbstractRepair.ADDTORELATION};
236 return new int[] {AbstractRepair.ADDTORELATION,
237 AbstractRepair.REMOVEFROMRELATION};
239 } else if (op==Opcode.GE||op==Opcode.GT) {
240 return new int[]{AbstractRepair.ADDTORELATION};
241 } else if (op==Opcode.LE||op==Opcode.LT) {
242 if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
244 return new int[]{AbstractRepair.REMOVEFROMRELATION};
245 } else if (op==Opcode.NE) {
246 if (maxsize<size&&maxsize!=-1)
248 return new int[]{AbstractRepair.ADDTORELATION};
249 } else throw new Error();
253 return new int[] {AbstractRepair.REMOVEFROMSET};
255 if (maxsize<=size&&maxsize!=-1)
256 return new int[] {AbstractRepair.ADDTOSET};
257 return new int[] {AbstractRepair.ADDTOSET,
258 AbstractRepair.REMOVEFROMSET};
260 } else if (op==Opcode.GE||op==Opcode.GT) {
261 return new int[] {AbstractRepair.ADDTOSET};
262 } else if (op==Opcode.LE||op==Opcode.LT) {
263 if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
265 return new int[] {AbstractRepair.REMOVEFROMSET};
266 } else if (op==Opcode.NE) {
267 if (maxsize<size&&maxsize!=-1)
269 return new int[] {AbstractRepair.ADDTOSET};
270 } else throw new Error();
273 throw new Error("BAD");
276 public Descriptor getDescriptor() {
277 return left.getDescriptor();
280 public boolean inverted() {
281 return left.inverted();
284 public Set getInversedRelations() {
285 Set set = left.getInversedRelations();
287 set.addAll(right.getInversedRelations());
292 public Set getRequiredDescriptors() {
293 Set v = left.getRequiredDescriptors();
296 v.addAll(right.getRequiredDescriptors());
302 public void generate(CodeWriter writer, VarDescriptor dest) {
303 VarDescriptor ld = VarDescriptor.makeNew("leftop");
304 left.generate(writer, ld);
305 VarDescriptor rd = null;
306 VarDescriptor lm=VarDescriptor.makeNew("lm");
307 VarDescriptor rm=VarDescriptor.makeNew("rm");
310 if ((opcode==Opcode.OR)||
311 (opcode==Opcode.AND)) {
312 writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
313 writer.outputline("int maybe=0;");
316 rd = VarDescriptor.makeNew("rightop");
317 right.generate(writer, rd);
321 if (opcode == Opcode.RND) {
322 writer.outputline("int " +dest.getSafeSymbol() + " = (" +
323 ld.getSafeSymbol() + ">>3)<<3; ");
324 writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
325 } else if (opcode == Opcode.NOP) {
326 writer.outputline("int " +dest.getSafeSymbol() + " = " +
327 ld.getSafeSymbol() +"; ");
328 } else if (opcode == Opcode.AND) {
329 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
330 writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
331 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
332 } else if (opcode == Opcode.OR) {
333 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
334 writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
335 " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
336 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
338 } else if (opcode != Opcode.NOT) { /* two operands */
340 writer.outputline("int " + dest.getSafeSymbol() + " = " +
341 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
342 } else if (opcode == Opcode.NOT) {
343 writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
347 public void prettyPrint(PrettyPrinter pp) {
349 if (opcode == Opcode.NOT) {
351 left.prettyPrint(pp);
352 } else if (opcode == Opcode.NOP) {
353 left.prettyPrint(pp);
354 } else if (opcode == Opcode.RND) {
356 left.prettyPrint(pp);
358 left.prettyPrint(pp);
359 pp.output(" " + opcode.toString() + " ");
360 assert right != null;
361 right.prettyPrint(pp);
366 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
367 TypeDescriptor lt = left.typecheck(sa);
368 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
372 } else if (right != null && rt == null) {
378 // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
379 // either that or we use a isvalid clause to check for null
382 if (lt != ReservedTypeDescriptor.INT) {
383 sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
388 if (rt != ReservedTypeDescriptor.INT) {
389 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
399 this.td = ReservedTypeDescriptor.INT;