5 public class DotExpr extends Expr {
11 static boolean DOMEMCHECKS=true;
12 static boolean DOTYPECHECKS=false;
13 static boolean DONULL=false;
15 public void findmatch(Descriptor d, Set s) {
23 public Set freeVars() {
24 Set lset=left.freeVars();
27 iset=index.freeVars();
36 static int memoryindents = 0;
38 public static void generate_memory_endblocks(CodeWriter cr) {
39 while (memoryindents > 0) {
48 TypeDescriptor fieldtype;
51 public String name() {
52 String name=left.name()+"."+field;
54 name+="["+index.name()+"]";
58 public boolean usesDescriptor(Descriptor d) {
61 return left.usesDescriptor(d)||((intindex!=null)&&intindex.usesDescriptor(d));
64 public boolean equals(Map remap, Expr e) {
65 if (e==null||!(e instanceof DotExpr))
67 DotExpr de=(DotExpr)e;
68 if (!de.field.equals(field))
73 } else if (!index.equals(remap,de.index))
75 if (!left.equals(remap,de.left))
81 public DotExpr(Expr left, String field, Expr index) {
87 public Set getRequiredDescriptors() {
88 Set v = left.getRequiredDescriptors();
91 v.addAll(index.getRequiredDescriptors());
96 public Expr getExpr() {
100 public FieldDescriptor getField() {
104 public Expr getIndex() {
108 public void generate(CodeWriter writer, VarDescriptor dest) {
109 VarDescriptor leftd = VarDescriptor.makeNew("left");
111 writer.output("// " + leftd.getSafeSymbol() + " <-- ");
112 left.prettyPrint(writer);
113 writer.outputline("");
115 left.generate(writer, leftd);
117 writer.output("// " + leftd.getSafeSymbol() + " = ");
118 left.prettyPrint(writer);
119 writer.outputline("");
121 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
122 Expr intindex = index;
125 // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor obect that is in teh vector list
126 // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array
127 // descriptor not the underlying field descriptor
129 /* we calculate the offset in bits */
130 offsetbits = struct.getOffsetExpr(fd);
132 if (fd instanceof ArrayDescriptor) {
133 fd = ((ArrayDescriptor) fd).getField();
136 if (intindex != null) {
137 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
138 /* short circuit for constant 0 */
140 Expr basesize = fd.getBaseSizeExpr();
141 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
145 final SymbolTable st = writer.getSymbolTable();
146 TypeDescriptor td = offsetbits.typecheck(new SemanticAnalyzer() {
147 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
148 public SymbolTable getSymbolTable() { return st; }
152 throw new IRException();
153 } else if (td != ReservedTypeDescriptor.INT) {
154 throw new IRException();
157 boolean dotypecheck = false;
159 if (offsetbits instanceof IntegerLiteralExpr) {
160 int offsetinbits = ((IntegerLiteralExpr) offsetbits).getValue();
161 int offset = offsetinbits >> 3; /* offset in bytes */
163 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
164 int shift = offsetinbits - (offset << 3);
165 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
167 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
168 writer.outputline(getType().getGenerateType() + " "+dest.getSafeSymbol()+"=0;");
169 writer.outputline("if ("+leftd.getSafeSymbol()+")");
170 writer.outputline(dest.getSafeSymbol() + " = ((*(int *)" +
171 "(" + leftd.getSafeSymbol() + " + " + offset + ")) " +
172 " >> " + shift + ") & 0x" + Integer.toHexString(mask) + ";");
173 writer.outputline("else maybe=1;");
174 } else { /* a structure address or a ptr! */
175 String ptr = fd.getPtr() ? "*(int *)" : "";
176 /* type var = [*(int *)] (base + offset) */
177 writer.outputline("int " + dest.getSafeSymbol()+"=0;");
178 writer.outputline("if ("+leftd.getSafeSymbol()+")");
180 writer.outputline(dest.getSafeSymbol() +
181 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ");");
183 writer.outputline("if ("+dest.getSafeSymbol()+")");
185 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
186 if (DOMEMCHECKS&&(!DOTYPECHECKS)) {
187 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
189 } else if (DOTYPECHECKS) {
190 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
192 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
194 writer.outputline(dest.getSafeSymbol()+"=0;");
196 writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ")=0;");
201 writer.outputline("else maybe=1;");
203 } else { /* offset in bits is an expression that must be generated */
204 VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
205 writer.output("// " + ob.getSafeSymbol() + " <-- ");
206 offsetbits.prettyPrint(writer);
207 writer.outputline("");
208 offsetbits.generate(writer, ob);
209 writer.output("// " + ob.getSafeSymbol() + " = ");
210 offsetbits.prettyPrint(writer);
211 writer.outputline("");
213 /* derive offset in bytes */
214 VarDescriptor offset = VarDescriptor.makeNew("offset");
215 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
217 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
218 VarDescriptor shift = VarDescriptor.makeNew("shift");
219 writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() +
220 " - (" + offset.getSafeSymbol() + " << 3);");
221 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
223 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
224 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;");
225 writer.outputline("if ("+leftd.getSafeSymbol()+")");
226 writer.outputline(dest.getSafeSymbol() +
228 "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " +
229 " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");
230 writer.outputline("else maybe=1;");
231 } else { /* a structure address or a ptr */
232 String ptr = fd.getPtr() ? "*(int *)" : "";
233 /* type var = [*(int *)] (base + offset) */
234 writer.outputline("int " + dest.getSafeSymbol() +"=0;");
235 writer.outputline("if ("+leftd.getSafeSymbol()+")");
237 writer.outputline(dest.getSafeSymbol() +
238 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
240 writer.outputline("if ("+dest.getSafeSymbol()+")");
242 VarDescriptor typevar=VarDescriptor.makeNew("typechecks");
243 if (DOMEMCHECKS&&(!DOTYPECHECKS)) {
244 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
246 } else if (DOTYPECHECKS) {
247 writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");");
249 writer.outputline("if (!"+typevar.getSafeSymbol()+")");
251 writer.outputline(dest.getSafeSymbol()+"=0;");
253 writer.outputline(ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")=0;");
258 writer.outputline("else maybe=1;");
263 private int bitmask(int bits) {
266 for (int i = 0; i < bits; i++) {
274 public void prettyPrint(PrettyPrinter pp) {
275 left.prettyPrint(pp);
276 pp.output("." + field);
279 index.prettyPrint(pp);
284 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
285 TypeDescriptor lefttype = left.typecheck(sa);
286 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
289 /* finished typechecking...so we can fill the fields in */
290 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
291 FieldDescriptor fd = struct.getField(field);
292 LabelDescriptor ld = struct.getLabel(field);
293 if (ld != null) { /* label */
295 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
298 assert intindex == null;
299 intindex = ld.getIndex();
301 fieldtype = fd.getType();
307 if ((lefttype == null) || (index != null && indextype == null)) {
311 if (indextype != null) {
312 if (indextype != ReservedTypeDescriptor.INT) {
313 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
318 if (lefttype instanceof StructureTypeDescriptor) {
319 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
320 FieldDescriptor fd = struct.getField(field);
321 LabelDescriptor ld = struct.getLabel(field);
323 if (fd != null) { /* field */
326 if (indextype == null && fd instanceof ArrayDescriptor) {
327 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
329 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
330 sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
334 this.td = fd.getType();
335 } else if (ld != null) { /* label */
339 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
343 this.td = ld.getType();
345 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
349 /* we promote bit, byte and short to integer types */
350 if (this.td == ReservedTypeDescriptor.BIT ||
351 this.td == ReservedTypeDescriptor.BYTE ||
352 this.td == ReservedTypeDescriptor.SHORT) {
353 this.td = ReservedTypeDescriptor.INT;
358 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");