6 public class DotExpr extends Expr {
12 static boolean DOMEMCHECKS=true;
13 static boolean DOTYPECHECKS=false;
14 static boolean DONULL=false;
16 public boolean isSafe() {
19 FieldDescriptor tmpfd=fd;
20 if (tmpfd instanceof ArrayDescriptor)
21 return false; // Arrays could be out of bounds
22 if (tmpfd.getPtr()) // Pointers cound be invalid
27 public void findmatch(Descriptor d, Set s) {
35 public Set freeVars() {
36 Set lset=left.freeVars();
39 iset=index.freeVars();
48 static int memoryindents = 0;
50 public static void generate_memory_endblocks(CodeWriter cr) {
51 while (memoryindents > 0) {
60 TypeDescriptor fieldtype;
63 public String name() {
64 String name=left.name()+"."+field;
66 name+="["+index.name()+"]";
70 public Set useDescriptor(Descriptor d) {
71 HashSet newset=new HashSet();
74 newset.addAll(left.useDescriptor(d));
76 newset.addAll(intindex.useDescriptor(d));
80 public boolean usesDescriptor(Descriptor d) {
83 return left.usesDescriptor(d)||((intindex!=null)&&intindex.usesDescriptor(d));
86 public boolean equals(Map remap, Expr e) {
87 if (e==null||!(e instanceof DotExpr))
89 DotExpr de=(DotExpr)e;
90 if (!de.field.equals(field))
95 } else if (!index.equals(remap,de.index))
97 if (!left.equals(remap,de.left))
103 public DotExpr(Expr left, String field, Expr index) {
109 public Set getRequiredDescriptors() {
110 Set v = left.getRequiredDescriptors();
113 v.addAll(index.getRequiredDescriptors());
118 public Expr getExpr() {
122 public FieldDescriptor getField() {
126 public Expr getIndex() {
130 public void generate(CodeWriter writer, VarDescriptor dest) {
131 VarDescriptor leftd = VarDescriptor.makeNew("left");
133 writer.output("// " + leftd.getSafeSymbol() + " <-- ");
134 left.prettyPrint(writer);
135 writer.outputline("");
137 left.generate(writer, leftd);
139 writer.output("// " + leftd.getSafeSymbol() + " = ");
140 left.prettyPrint(writer);
141 writer.outputline("");
143 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
144 Expr intindex = index;
147 // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor object that is in the vector list
148 // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array
149 // descriptor not the underlying field descriptor
151 /* we calculate the offset in bits */
153 offsetbits = struct.getOffsetExpr(fd);
155 FieldDescriptor fd=this.fd;
156 if (fd instanceof ArrayDescriptor)
157 fd=((ArrayDescriptor)fd).getField();
158 boolean doboundscheck=true;
159 boolean performedboundscheck=false;
161 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;");
163 if (intindex != null) {
164 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
165 /* short circuit for constant 0 */
167 Expr basesize = fd.getBaseSizeExpr();
169 VarDescriptor indexvd=VarDescriptor.makeNew("index");
170 indexvd.setType(ReservedTypeDescriptor.INT);
171 writer.getSymbolTable().add(indexvd);
173 writer.output("// " + indexvd.getSafeSymbol() + " <-- ");
175 intindex.prettyPrint(writer);
176 writer.outputline("");
177 intindex.generate(writer, indexvd);
178 writer.output("// " + indexvd.getSafeSymbol() + " = ");
179 intindex.prettyPrint(writer);
180 writer.outputline("");
181 Expr indexbound=((ArrayDescriptor)this.fd).getIndexBound();
182 VarDescriptor indexboundvd=VarDescriptor.makeNew("indexbound");
184 indexbound.generate(writer,indexboundvd);
186 writer.outputline("if ("+indexvd.getSafeSymbol()+">=0 &&"+indexvd.getSafeSymbol()+"<"+indexboundvd.getSafeSymbol()+")");
188 VarExpr indexve=new VarExpr(indexvd);
189 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, indexve));
191 performedboundscheck=true;
193 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
197 final SymbolTable st = writer.getSymbolTable();
198 TypeDescriptor td2 = offsetbits.typecheck(new SemanticAnalyzer() {
199 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
200 public SymbolTable getSymbolTable() { return st; }
204 throw new IRException();
205 } else if (td2 != ReservedTypeDescriptor.INT) {
206 throw new IRException();
209 boolean dotypecheck = false;
211 VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
212 writer.output("// " + ob.getSafeSymbol() + " <-- ");
213 offsetbits.prettyPrint(writer);
214 writer.outputline("");
215 offsetbits.generate(writer, ob);
216 writer.output("// " + ob.getSafeSymbol() + " = ");
217 offsetbits.prettyPrint(writer);
218 writer.outputline("");
220 /* derive offset in bytes */
221 VarDescriptor offset = VarDescriptor.makeNew("offset");
222 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
224 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
225 VarDescriptor shift = VarDescriptor.makeNew("shift");
226 writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() +
227 " - (" + offset.getSafeSymbol() + " << 3);");
228 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
230 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
231 writer.outputline("if ("+leftd.getSafeSymbol()+")");
232 writer.outputline(dest.getSafeSymbol() + " = ((*(int *)" +
233 "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " +
234 " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");
235 writer.outputline("else maybe=1;");
236 } else { /* a structure address or a ptr */
237 String ptr = fd.getPtr() ? "*(int *)" : "";
238 /* type var = [*(int *)] (base + offset) */
239 writer.outputline("if ("+leftd.getSafeSymbol()+")");
241 writer.outputline(dest.getSafeSymbol() +
242 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
244 writer.outputline("if ("+dest.getSafeSymbol()+")");
246 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
247 if (DOMEMCHECKS&&(!DOTYPECHECKS)) {
248 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
250 } else if (DOTYPECHECKS) {
251 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
253 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
255 writer.outputline(dest.getSafeSymbol()+"=0;");
257 writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")=0;");
262 writer.outputline("else maybe=1;");
264 if (performedboundscheck) {
266 writer.outputline(" else ");
268 writer.outputline(dest.getSafeSymbol()+"=0;");
269 writer.outputline("maybe=1;");
270 if (!Compiler.REPAIR)
271 writer.outputline("printf(\"Array Index Out of Bounds\");");
276 private int bitmask(int bits) {
279 for (int i = 0; i < bits; i++) {
287 public void prettyPrint(PrettyPrinter pp) {
288 left.prettyPrint(pp);
289 pp.output("." + field);
292 index.prettyPrint(pp);
297 public boolean isValue() {
298 FieldDescriptor tmpfd=fd;
299 if (tmpfd instanceof ArrayDescriptor)
300 tmpfd=((ArrayDescriptor)tmpfd).getField();
301 return (tmpfd.getPtr()||(tmpfd.getType() instanceof ReservedTypeDescriptor));
304 boolean typechecked=false;
305 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
308 else typechecked=true;
309 TypeDescriptor lefttype = left.typecheck(sa);
310 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
313 /* finished typechecking...so we can fill the fields in */
314 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
315 FieldDescriptor fd = struct.getField(field);
316 LabelDescriptor ld = struct.getLabel(field);
317 if (ld != null) { /* label */
319 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
322 assert intindex == null;
323 intindex = ld.getIndex();
325 fieldtype = fd.getType();
331 if ((lefttype == null) || (index != null && indextype == null)) {
335 if (indextype != null) {
336 if (indextype != ReservedTypeDescriptor.INT) {
337 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
342 if (lefttype instanceof StructureTypeDescriptor) {
343 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
344 FieldDescriptor fd = struct.getField(field);
345 LabelDescriptor ld = struct.getLabel(field);
347 if (fd != null) { /* field */
350 if (indextype == null && fd instanceof ArrayDescriptor) {
351 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
353 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
354 sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
358 this.td = fd.getType();
359 } else if (ld != null) { /* label */
363 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
367 this.td = ld.getType();
369 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
373 /* we promote bit, byte and short to integer types */
374 if (this.td == ReservedTypeDescriptor.BIT ||
375 this.td == ReservedTypeDescriptor.BYTE ||
376 this.td == ReservedTypeDescriptor.SHORT) {
377 this.td = ReservedTypeDescriptor.INT;
382 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");