6 public class DotExpr extends Expr {
12 static boolean DOMEMCHECKS=false;
13 static boolean DOTYPECHECKS=false;
14 static boolean DONULL=false;
17 public DotExpr(Expr left, String field, Expr index) {
23 public boolean isInvariant(Set vars) {
24 if (!left.isInvariant(vars))
27 return intindex.isInvariant(vars);
32 public Set findInvariants(Set vars) {
33 if (isInvariant(vars)) {
38 Set ls=left.findInvariants(vars);
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));
57 public boolean isSafe() {
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
68 public Set freeVars() {
69 Set lset=left.freeVars();
72 iset=intindex.freeVars();
81 static int memoryindents = 0;
83 public static void generate_memory_endblocks(CodeWriter cr) {
84 while (memoryindents > 0) {
93 TypeDescriptor fieldtype;
96 public String name() {
97 String name=left.name()+"."+field;
99 name+="["+index.name()+"]";
103 public void findmatch(Descriptor d, Set s) {
108 intindex.findmatch(d,s);
111 public Set useDescriptor(Descriptor d) {
112 HashSet newset=new HashSet();
115 newset.addAll(left.useDescriptor(d));
117 newset.addAll(intindex.useDescriptor(d));
121 public boolean usesDescriptor(Descriptor d) {
124 return left.usesDescriptor(d)||((intindex!=null)&&intindex.usesDescriptor(d));
127 public boolean equals(Map remap, Expr e) {
128 if (e==null||!(e instanceof DotExpr))
130 DotExpr de=(DotExpr)e;
131 if (!de.field.equals(field))
136 } else if (!index.equals(remap,de.index))
138 if (!left.equals(remap,de.left))
144 public Set getRequiredDescriptors() {
145 Set v = left.getRequiredDescriptors();
147 if (intindex != null) {
148 v.addAll(intindex.getRequiredDescriptors());
153 public Expr getExpr() {
157 public FieldDescriptor getField() {
161 public Expr getIndex() {
165 public void generate(CodeWriter writer, VarDescriptor dest) {
166 VarDescriptor leftd = VarDescriptor.makeNew("left");
168 if (writer.getInvariantValue()!=null&&
169 writer.getInvariantValue().isInvariant(this)) {
170 writer.outputline(getType().getGenerateType().getSafeSymbol()+
171 " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";");
172 writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";");
176 writer.output("/* " + leftd.getSafeSymbol() + " <-- ");
177 left.prettyPrint(writer);
178 writer.outputline("*/");
180 left.generate(writer, leftd);
182 writer.output("/* " + leftd.getSafeSymbol() + " = ");
183 left.prettyPrint(writer);
184 writer.outputline("*/");
186 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
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
193 /* we calculate the offset in bits */
195 offsetbits = struct.getOffsetExpr(fd);
197 FieldDescriptor fd=this.fd;
198 if (fd instanceof ArrayDescriptor)
199 fd=((ArrayDescriptor)fd).getField();
200 boolean doboundscheck=true;
201 boolean performedboundscheck=false;
203 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;");
205 if (intindex != null) {
206 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
207 /* short circuit for constant 0 */
209 Expr basesize = fd.getBaseSizeExpr();
211 VarDescriptor indexvd=VarDescriptor.makeNew("index");
212 indexvd.setType(ReservedTypeDescriptor.INT);
213 writer.getSymbolTable().add(indexvd);
215 writer.output("/* " + indexvd.getSafeSymbol() + " <-- ");
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");
226 indexbound.generate(writer,indexboundvd);
228 writer.outputline("if ("+indexvd.getSafeSymbol()+">=0 &&"+indexvd.getSafeSymbol()+"<"+indexboundvd.getSafeSymbol()+")");
230 VarExpr indexve=new VarExpr(indexvd);
231 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, indexve));
233 performedboundscheck=true;
235 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
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; }
246 throw new IRException();
247 } else if (td2 != ReservedTypeDescriptor.INT) {
248 throw new IRException();
251 boolean dotypecheck = false;
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("*/");
262 /* derive offset in bytes */
263 VarDescriptor offset = VarDescriptor.makeNew("offset");
264 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
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());
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()+")");
283 writer.outputline(dest.getSafeSymbol() +
284 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
286 writer.outputline("if ("+dest.getSafeSymbol()+")");
288 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
289 if (DOMEMCHECKS&&(!DOTYPECHECKS)) {
290 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
292 } else if (DOTYPECHECKS) {
293 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
296 if (DOTYPECHECKS||DOMEMCHECKS) {
297 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
299 writer.outputline(dest.getSafeSymbol()+"=0;");
301 writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")=0;");
308 writer.outputline("else maybe=1;");
310 if (performedboundscheck) {
312 writer.outputline(" else ");
314 writer.outputline(dest.getSafeSymbol()+"=0;");
315 writer.outputline("maybe=1;");
316 if (!Compiler.REPAIR)
317 writer.outputline("printf(\"Array Index Out of Bounds\");");
322 private int bitmask(int bits) {
325 for (int i = 0; i < bits; i++) {
333 public void prettyPrint(PrettyPrinter pp) {
334 left.prettyPrint(pp);
335 pp.output("." + field);
338 index.prettyPrint(pp);
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));
350 public boolean isPtr() {
351 FieldDescriptor tmpfd=fd;
352 if (tmpfd instanceof ArrayDescriptor)
353 tmpfd=((ArrayDescriptor)tmpfd).getField();
354 return tmpfd.getPtr();
357 boolean typechecked=false;
358 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
361 else typechecked=true;
362 TypeDescriptor lefttype = left.typecheck(sa);
363 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
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 */
372 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
375 assert intindex == null;
376 intindex = ld.getIndex();
378 fieldtype = fd.getType();
382 if (fieldtype instanceof MissingTypeDescriptor)
383 throw new Error(fieldtype.getSymbol()+" type undefined!");
386 if ((lefttype == null) || (index != null && indextype == null)) {
390 if (indextype != null) {
391 if (indextype != ReservedTypeDescriptor.INT) {
392 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
397 if (lefttype instanceof StructureTypeDescriptor) {
398 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
399 FieldDescriptor fd = struct.getField(field);
400 LabelDescriptor ld = struct.getLabel(field);
402 if (fd != null) { /* field */
405 if (indextype == null && fd instanceof ArrayDescriptor) {
406 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
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() + "'");
413 this.td = fd.getType();
414 } else if (ld != null) { /* label */
418 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
422 this.td = ld.getType();
424 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
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;
437 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");