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;
39 public boolean isSafe() {
42 return left.isSafe()&&right.isSafe();
45 public Set getfunctions() {
46 Set leftfunctions=left.getfunctions();
47 Set rightfunctions=right.getfunctions();
48 if (leftfunctions!=null&&rightfunctions!=null) {
49 HashSet functions=new HashSet();
50 functions.addAll(leftfunctions);
51 functions.addAll(rightfunctions);
54 if (leftfunctions!=null)
56 return rightfunctions;
59 public void findmatch(Descriptor d, Set s) {
65 public static boolean isInt(Expr e) {
68 if ((e instanceof IntegerLiteralExpr)||
69 ((e instanceof OpExpr)&&(((OpExpr)e).opcode==Opcode.NOP)&&(((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr)))
74 public static int getInt(Expr e) {
75 if (e instanceof IntegerLiteralExpr)
76 return ((IntegerLiteralExpr)e).getValue();
77 else if ((e instanceof OpExpr) && (((OpExpr)e).getLeftExpr() instanceof IntegerLiteralExpr))
78 return ((IntegerLiteralExpr)((OpExpr)e).getLeftExpr()).getValue();
79 else throw new Error();
82 public OpExpr(Opcode opcode, Expr left, Expr right) {
83 if ((isInt(left)&&isInt(right))||
84 (isInt(left)&&(opcode==Opcode.NOT))||
85 (isInt(left)&&(opcode==Opcode.RND))) {
86 this.opcode=Opcode.NOP;
88 int lint=isInt(left)?getInt(left):0;
89 int rint=isInt(right)?getInt(right):0;
91 if (opcode==Opcode.ADD) {
93 } else if (opcode==Opcode.SUB) {
95 } else if (opcode==Opcode.SHL) {
97 } else if (opcode==Opcode.SHR) {
99 } else if (opcode==Opcode.MULT) {
101 } else if (opcode==Opcode.DIV) {
103 } else if (opcode==Opcode.GT) {
106 } else if (opcode==Opcode.GE) {
109 } else if (opcode==Opcode.LT) {
112 } else if (opcode==Opcode.LE) {
115 } else if (opcode==Opcode.EQ) {
118 } else if (opcode==Opcode.NE) {
121 } else if (opcode==Opcode.AND) {
122 if ((lint!=0)&&(rint!=0))
124 } else if (opcode==Opcode.OR) {
125 if ((lint!=0)||(rint!=0))
127 } else if (opcode==Opcode.NOT) {
130 } else if (opcode==Opcode.RND) {
131 value=((lint>>3)<<3);
134 } else throw new Error("Unrecognized Opcode");
135 this.left=new IntegerLiteralExpr(value);
136 } else if ((opcode==Opcode.MULT)&&
137 ((isInt(left)&&(getInt(left)==0))
138 ||(isInt(right)&&(getInt(right)==0)))) {
139 this.opcode=Opcode.NOP;
141 this.left=new IntegerLiteralExpr(0);
143 this.opcode = opcode;
146 assert (right == null && (opcode == Opcode.NOT||opcode==Opcode.RND)) || (right != null);
150 public Expr getRightExpr() {
154 public Expr getLeftExpr() {
158 public Set freeVars() {
159 Set lset=left.freeVars();
162 rset=right.freeVars();
170 public String name() {
171 if (opcode==Opcode.NOT)
172 return "!("+left.name()+")";
173 if (opcode==Opcode.NOP)
175 if (opcode==Opcode.RND)
176 return "Round("+left.name()+")";
177 String name=left.name()+opcode.toString();
183 public Opcode getOpcode() {
190 public boolean equals(Map remap, Expr e) {
191 if (e==null||!(e instanceof OpExpr))
194 if (opcode!=oe.opcode)
196 if (!left.equals(remap,oe.left))
198 if ((opcode!=Opcode.NOT)&&(opcode!=Opcode.RND)&&(opcode!=Opcode.NOP))
199 if (!right.equals(remap,oe.right))
204 public DNFRule constructDNF() {
205 if (opcode==Opcode.AND) {
206 DNFRule leftd=left.constructDNF();
207 DNFRule rightd=right.constructDNF();
208 return leftd.and(rightd);
209 } else if (opcode==Opcode.OR) {
210 DNFRule leftd=left.constructDNF();
211 DNFRule rightd=right.constructDNF();
212 return leftd.or(rightd);
213 } else if (opcode==Opcode.NOT) {
214 DNFRule leftd=left.constructDNF();
216 } else return new DNFRule(this);
219 public boolean usesDescriptor(Descriptor d) {
220 return left.usesDescriptor(d)||(right!=null&&right.usesDescriptor(d));
223 public Set useDescriptor(Descriptor d) {
224 HashSet newset=new HashSet();
225 newset.addAll(left.useDescriptor(d));
227 newset.addAll(right.useDescriptor(d));
231 public int[] getRepairs(boolean negated, Termination t) {
232 if (left instanceof RelationExpr)
233 return new int[] {AbstractRepair.MODIFYRELATION};
234 if (left instanceof SizeofExpr) {
237 /* remove negation through opcode translation */
240 else if (op==Opcode.GE)
242 else if (op==Opcode.EQ)
244 else if (op==Opcode.NE)
246 else if (op==Opcode.LT)
248 else if (op==Opcode.LE)
252 int maxsize=t.maxsize.getsize(getDescriptor());
253 int size=getInt(right);
256 boolean isRelation=((SizeofExpr)left).setexpr instanceof ImageSetExpr;
260 return new int[] {AbstractRepair.REMOVEFROMRELATION};
262 if ((maxsize!=-1)&&maxsize<=size)
263 return new int[] {AbstractRepair.ADDTORELATION};
264 return new int[] {AbstractRepair.ADDTORELATION,
265 AbstractRepair.REMOVEFROMRELATION};
267 } else if (op==Opcode.GE||op==Opcode.GT) {
268 return new int[]{AbstractRepair.ADDTORELATION};
269 } else if (op==Opcode.LE||op==Opcode.LT) {
270 if ((op==Opcode.LT&&maxsize!=-1&&maxsize<size)||(op==Opcode.LE&&maxsize!=-1&&maxsize<=size))
272 return new int[]{AbstractRepair.REMOVEFROMRELATION};
273 } else if (op==Opcode.NE) {
274 if (maxsize<size&&maxsize!=-1)
276 return new int[]{AbstractRepair.ADDTORELATION};
277 } else throw new Error();
281 return new int[] {AbstractRepair.REMOVEFROMSET};
283 if (maxsize<=size&&maxsize!=-1)
284 return new int[] {AbstractRepair.ADDTOSET};
285 return new int[] {AbstractRepair.ADDTOSET,
286 AbstractRepair.REMOVEFROMSET};
288 } else if (op==Opcode.GE||op==Opcode.GT) {
289 return new int[] {AbstractRepair.ADDTOSET};
290 } else if (op==Opcode.LE||op==Opcode.LT) {
291 if ((op==Opcode.LT&&maxsize<size&&maxsize!=-1)||(op==Opcode.LE&&maxsize<=size&&maxsize!=-1))
293 return new int[] {AbstractRepair.REMOVEFROMSET};
294 } else if (op==Opcode.NE) {
295 if (maxsize<size&&maxsize!=-1)
297 return new int[] {AbstractRepair.ADDTOSET};
298 } else throw new Error();
301 throw new Error("BAD");
304 public Descriptor getDescriptor() {
305 return left.getDescriptor();
308 public boolean inverted() {
309 return left.inverted();
312 public Set getInversedRelations() {
313 Set set = left.getInversedRelations();
315 set.addAll(right.getInversedRelations());
320 public Set getRequiredDescriptors() {
321 Set v = left.getRequiredDescriptors();
324 v.addAll(right.getRequiredDescriptors());
330 public void generate(CodeWriter writer, VarDescriptor dest) {
331 VarDescriptor ld = VarDescriptor.makeNew("leftop");
332 left.generate(writer, ld);
333 VarDescriptor rd = null;
334 VarDescriptor lm=VarDescriptor.makeNew("lm");
335 VarDescriptor rm=VarDescriptor.makeNew("rm");
338 if ((opcode==Opcode.OR)||
339 (opcode==Opcode.AND)) {
340 writer.outputline("int "+lm.getSafeSymbol()+"=maybe;");
341 writer.outputline("int maybe=0;");
344 rd = VarDescriptor.makeNew("rightop");
345 right.generate(writer, rd);
349 if (opcode == Opcode.RND) {
350 writer.outputline("int " +dest.getSafeSymbol() + " = (" +
351 ld.getSafeSymbol() + ">>3)<<3; ");
352 writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;");
353 } else if (opcode == Opcode.NOP) {
354 writer.outputline("int " +dest.getSafeSymbol() + " = " +
355 ld.getSafeSymbol() +"; ");
356 } else if (opcode == Opcode.AND) {
357 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
358 writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
359 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";");
360 } else if (opcode == Opcode.OR) {
361 writer.outputline("int "+rm.getSafeSymbol()+"=maybe;");
362 writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() +
363 " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");");
364 writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() +
366 } else if (opcode != Opcode.NOT) { /* two operands */
368 writer.outputline("int " + dest.getSafeSymbol() + " = " +
369 ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";");
370 } else if (opcode == Opcode.NOT) {
371 writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";");
375 public void prettyPrint(PrettyPrinter pp) {
377 if (opcode == Opcode.NOT) {
379 left.prettyPrint(pp);
380 } else if (opcode == Opcode.NOP) {
381 left.prettyPrint(pp);
382 } else if (opcode == Opcode.RND) {
384 left.prettyPrint(pp);
386 left.prettyPrint(pp);
387 pp.output(" " + opcode.toString() + " ");
388 assert right != null;
389 right.prettyPrint(pp);
394 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
395 TypeDescriptor lt = left.typecheck(sa);
396 TypeDescriptor rt = right == null ? null : right.typecheck(sa);
400 } else if (right != null && rt == null) {
406 // #ATTN#: if we want node.next != literal(0) to represent a null check than we need to allow ptr arithmetic
407 // either that or we use a isvalid clause to check for null
410 if (lt != ReservedTypeDescriptor.INT) {
411 sa.getErrorReporter().report(null, "Left hand side of expression is of type '" + lt.getSymbol() + "' but must be type 'int'");
416 if (rt != ReservedTypeDescriptor.INT) {
417 sa.getErrorReporter().report(null, "Right hand side of expression is of type '" + rt.getSymbol() + "' but must be type 'int'");
427 this.td = ReservedTypeDescriptor.INT;