Add updates...
[repair.git] / Repair / RepairCompiler / MCC / IR / DotExpr.java
1 package MCC.IR;
2
3 import java.util.*;
4 import MCC.Compiler;
5
6 public class DotExpr extends Expr {
7     
8     Expr left;
9     String field;
10     Expr index;
11     
12     static boolean DOMEMCHECKS=false;
13     static boolean DOTYPECHECKS=false;
14     static boolean DONULL=false;
15
16     
17     public DotExpr(Expr left, String field, Expr index) {
18         this.left = left;
19         this.field = field;
20         this.index = index;
21     }
22
23     public boolean isInvariant(Set vars) {
24         if (!left.isInvariant(vars))
25             return false;
26         if (intindex!=null)
27             return intindex.isInvariant(vars);
28         else
29             return true;
30     }
31
32     public Set findInvariants(Set vars) {
33         if (isInvariant(vars)) {
34             Set s=new HashSet();
35             s.add(this);
36             return s;
37         } else {
38             Set ls=left.findInvariants(vars);
39             if (intindex!=null) {
40                 ls.addAll(intindex.findInvariants(vars));
41                 Expr indexbound=((ArrayDescriptor)this.fd).getIndexBound();
42                 ls.addAll(indexbound.findInvariants(vars));
43                 if ((!(intindex instanceof IntegerLiteralExpr))||
44                     ((IntegerLiteralExpr) intindex).getValue() != 0) {
45                     FieldDescriptor fd=this.fd;
46                     if (fd instanceof ArrayDescriptor)
47                         fd=((ArrayDescriptor)fd).getField();
48                     Expr basesize = fd.getBaseSizeExpr();
49                     ls.addAll(basesize.findInvariants(vars));
50                 }
51             }
52             return ls;
53         }
54     }
55
56
57     public boolean isSafe() {
58         if (!left.isSafe())
59             return false;
60         FieldDescriptor tmpfd=fd;
61         if (tmpfd instanceof ArrayDescriptor)
62             return false; // Arrays could be out of bounds
63         if (tmpfd.getPtr()) // Pointers cound be invalid
64             return false;
65         return true;
66     }
67
68     public Set freeVars() {
69         Set lset=left.freeVars();
70         Set iset=null;
71         if (intindex!=null)
72             iset=intindex.freeVars();
73         if (lset==null)
74             return iset;
75         if (iset!=null)
76             lset.addAll(iset);
77         return lset;
78     }
79
80     /*
81     static int memoryindents = 0;
82
83     public static void generate_memory_endblocks(CodeWriter cr) {
84         while (memoryindents > 0) {
85             memoryindents --;
86             cr.endblock();
87         }
88         memoryindents = 0;
89     }
90     */
91
92     FieldDescriptor fd;
93     TypeDescriptor fieldtype;
94     Expr intindex;
95
96     public String name() {
97         String name=left.name()+"."+field;
98         if (index!=null)
99             name+="["+index.name()+"]";
100         return name;
101     }
102     
103     public void findmatch(Descriptor d, Set s) {
104         if (d==fd)
105             s.add(this);
106         left.findmatch(d,s);
107         if (intindex!=null)
108             intindex.findmatch(d,s);
109     }
110
111     public Set useDescriptor(Descriptor d) {
112         HashSet newset=new HashSet();
113         if (d==fd)
114             newset.add(this);
115         newset.addAll(left.useDescriptor(d));
116         if (intindex!=null)
117             newset.addAll(intindex.useDescriptor(d));
118         return newset;
119     }
120
121     public boolean usesDescriptor(Descriptor d) {
122         if (d==fd)
123             return true;
124         return left.usesDescriptor(d)||((intindex!=null)&&intindex.usesDescriptor(d));
125     }
126
127     public boolean equals(Map remap, Expr e) {
128         if (e==null||!(e instanceof DotExpr))
129             return false;
130         DotExpr de=(DotExpr)e;
131         if (!de.field.equals(field))
132             return false;
133         if (index==null) {
134             if (de.index!=null)
135                 return false;
136         } else if (!index.equals(remap,de.index))
137             return false;
138         if (!left.equals(remap,de.left))
139             return false;
140         return true;
141     }
142
143
144     public Set getRequiredDescriptors() {
145         Set v = left.getRequiredDescriptors();
146         
147         if (intindex != null) {
148             v.addAll(intindex.getRequiredDescriptors());
149         }
150         return v;
151     }
152
153     public Expr getExpr() {
154         return left;
155     }
156
157     public FieldDescriptor getField() {
158         return fd;
159     }
160
161     public Expr getIndex() {
162         return intindex;
163     }
164
165     public void generate(CodeWriter writer, VarDescriptor dest) {
166         VarDescriptor leftd = VarDescriptor.makeNew("left");
167
168         if (writer.getInvariantValue()!=null&&
169             writer.getInvariantValue().isInvariant(this)) {
170             writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
171             writer.outputline(getType().getGenerateType().getSafeSymbol()+
172                               " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
173             return;
174         }
175
176         writer.output("// " +  leftd.getSafeSymbol() + " <-- ");
177         left.prettyPrint(writer);
178         writer.outputline("");
179
180         left.generate(writer, leftd);
181
182         writer.output("// " +  leftd.getSafeSymbol() + " = ");
183         left.prettyPrint(writer);
184         writer.outputline("");
185       
186         StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
187         Expr offsetbits;
188
189         // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor object that is in the vector list
190         // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array 
191         // descriptor not the underlying field descriptor
192
193         /* we calculate the offset in bits */
194         
195         offsetbits = struct.getOffsetExpr(fd);
196
197         FieldDescriptor fd=this.fd;
198         if (fd instanceof ArrayDescriptor)
199             fd=((ArrayDescriptor)fd).getField();
200         boolean doboundscheck=true;
201         boolean performedboundscheck=false;
202
203         writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;");
204
205         if (intindex != null) {
206             if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
207                 /* short circuit for constant 0 */
208             } else {
209                 Expr basesize = fd.getBaseSizeExpr();
210                 if (doboundscheck) {
211                     VarDescriptor indexvd=VarDescriptor.makeNew("index");
212                     indexvd.setType(ReservedTypeDescriptor.INT);
213                     writer.getSymbolTable().add(indexvd);
214
215                     writer.output("// " + indexvd.getSafeSymbol() + " <-- ");
216
217                     intindex.prettyPrint(writer);
218                     writer.outputline("");
219                     intindex.generate(writer, indexvd);
220                     writer.output("// " + indexvd.getSafeSymbol() + " = ");
221                     intindex.prettyPrint(writer);
222                     writer.outputline("");
223                     Expr indexbound=((ArrayDescriptor)this.fd).getIndexBound();
224                     VarDescriptor indexboundvd=VarDescriptor.makeNew("indexbound");
225
226                     indexbound.generate(writer,indexboundvd);
227                     
228                     writer.outputline("if ("+indexvd.getSafeSymbol()+">=0 &&"+indexvd.getSafeSymbol()+"<"+indexboundvd.getSafeSymbol()+")");
229                     writer.startblock();
230                     VarExpr indexve=new VarExpr(indexvd);
231                     offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, indexve));
232                     
233                     performedboundscheck=true;
234                 } else
235                     offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
236             }
237         }
238
239         final SymbolTable st = writer.getSymbolTable();
240         TypeDescriptor td2 = offsetbits.typecheck(new SemanticAnalyzer() {
241                 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
242                 public SymbolTable getSymbolTable() { return st; }
243             });
244         
245         if (td2 == null) {
246             throw new IRException();
247         } else if (td2 != ReservedTypeDescriptor.INT) {
248             throw new IRException();
249         }
250                
251         boolean dotypecheck = false;
252
253         VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
254         writer.output("// " + ob.getSafeSymbol() + " <-- ");
255         offsetbits.prettyPrint(writer);
256         writer.outputline("");
257         offsetbits.generate(writer, ob);
258         writer.output("// " + ob.getSafeSymbol() + " = ");
259         offsetbits.prettyPrint(writer);
260         writer.outputline("");
261         
262         /* derive offset in bytes */
263         VarDescriptor offset = VarDescriptor.makeNew("offset");
264         writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
265         
266         if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
267             VarDescriptor shift = VarDescriptor.makeNew("shift");
268             writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() + 
269                               " - (" + offset.getSafeSymbol() + " << 3);");
270             int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
271             
272             /* type var = ((*(int *) (base + offset)) >> shift) & mask */
273             writer.outputline("if ("+leftd.getSafeSymbol()+")");
274             writer.outputline(dest.getSafeSymbol() + " = ((*(int *)" + 
275                               "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " + 
276                               " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");  
277             writer.outputline("else maybe=1;");
278         } else { /* a structure address or a ptr */
279             String ptr = fd.getPtr() ? "*(int *)" : "";
280             /* type var = [*(int *)] (base + offset) */
281             writer.outputline("if ("+leftd.getSafeSymbol()+")");
282             writer.startblock();
283             writer.outputline(dest.getSafeSymbol() + 
284                               " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");  
285             if (fd.getPtr()) {
286                 writer.outputline("if ("+dest.getSafeSymbol()+")");
287                 writer.startblock();
288                 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
289                 if (DOMEMCHECKS&&(!DOTYPECHECKS)) {
290                     writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
291                     dotypecheck = true;
292                 } else if (DOTYPECHECKS) {
293                     writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
294                 }
295
296                 if (DOTYPECHECKS||DOMEMCHECKS) {
297                     writer.outputline("if (!"+typevar.getSafeSymbol()+")");
298                     writer.startblock();
299                     writer.outputline(dest.getSafeSymbol()+"=0;");
300                     if (DONULL)
301                         writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")=0;");
302                     writer.endblock();
303                 }
304
305                 writer.endblock();
306             }
307             writer.endblock();
308             writer.outputline("else maybe=1;");
309         }
310         if (performedboundscheck) {
311             writer.endblock();
312             writer.outputline(" else ");
313             writer.startblock();
314             writer.outputline(dest.getSafeSymbol()+"=0;");
315             writer.outputline("maybe=1;");
316             if (!Compiler.REPAIR)
317                 writer.outputline("printf(\"Array Index Out of Bounds\");");
318             writer.endblock();
319         }
320     }
321
322     private int bitmask(int bits) {
323         int mask = 0;
324         
325         for (int i = 0; i < bits; i++) {
326             mask <<= 1;
327             mask += 1;
328         }
329
330         return mask;            
331     }
332
333     public void prettyPrint(PrettyPrinter pp) {
334         left.prettyPrint(pp);
335         pp.output("." + field);
336         if (index != null) {
337             pp.output("[");
338             index.prettyPrint(pp);
339             pp.output("]");
340         }
341     }
342
343     public boolean isValue(TypeDescriptor td) {
344         FieldDescriptor tmpfd=fd;
345         if (tmpfd instanceof ArrayDescriptor)
346             tmpfd=((ArrayDescriptor)tmpfd).getField();
347         return (tmpfd.getPtr()||(tmpfd.getType() instanceof ReservedTypeDescriptor));
348     }
349     
350     public boolean isPtr() {
351         FieldDescriptor tmpfd=fd;
352         if (tmpfd instanceof ArrayDescriptor)
353             tmpfd=((ArrayDescriptor)tmpfd).getField();
354         return tmpfd.getPtr();
355     }
356
357     boolean typechecked=false;
358     public TypeDescriptor typecheck(SemanticAnalyzer sa) {
359         if (typechecked)
360             return this.td;
361         else typechecked=true;
362         TypeDescriptor lefttype = left.typecheck(sa);
363         TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
364         
365         {
366             /* finished typechecking...so we can fill the fields in */
367             StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
368             FieldDescriptor fd = struct.getField(field);
369             LabelDescriptor ld = struct.getLabel(field);
370             if (ld != null) { /* label */
371                 assert fd == null;
372                 fieldtype = ld.getType(); // d.s ==> Superblock, while,  d.b ==> Block
373                 fd = ld.getField();
374                 assert fd != null;
375                 assert intindex == null;
376                 intindex = ld.getIndex();
377             } else {
378                 fieldtype = fd.getType();
379                 intindex=index;
380             }
381             this.fd=fd;
382             if (fieldtype instanceof MissingTypeDescriptor)
383                 throw new Error(fieldtype.getSymbol()+" type undefined!");
384         }
385
386         if ((lefttype == null) || (index != null && indextype == null)) {
387             return null;
388         }
389
390         if (indextype != null) {
391             if (indextype != ReservedTypeDescriptor.INT) {
392                 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
393                 return null;
394             }
395         }
396
397         if (lefttype instanceof StructureTypeDescriptor) {            
398             StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
399             FieldDescriptor fd = struct.getField(field);
400             LabelDescriptor ld = struct.getLabel(field);
401
402             if (fd != null) { /* field */
403                 assert ld == null;
404
405                 if (indextype == null && fd instanceof ArrayDescriptor) {
406                     sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
407                     return null;                
408                 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
409                     sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
410                     return null;
411                 }
412                 
413                 this.td = fd.getType();
414             } else if (ld != null) { /* label */
415                 assert fd == null;
416
417                 if (index != null) { 
418                     sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
419                     return null;
420                 }
421                 
422                 this.td = ld.getType();
423             } else {
424                 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
425                 return null;
426             }
427
428             /* we promote bit, byte and short to integer types */
429             if (this.td == ReservedTypeDescriptor.BIT ||
430                 this.td == ReservedTypeDescriptor.BYTE ||
431                 this.td == ReservedTypeDescriptor.SHORT) {
432                 this.td = ReservedTypeDescriptor.INT;
433             }
434
435             return this.td;
436         } else {
437             sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");
438             return null;
439         }
440     }
441 }
442