5 public class DotExpr extends Expr {
12 static int memoryindents = 0;
14 public static void generate_memory_endblocks(CodeWriter cr) {
15 while (memoryindents > 0) {
24 TypeDescriptor fieldtype;
27 public String name() {
28 String name=left.name()+"."+field;
30 name+="["+index.name()+"]";
34 public boolean usesDescriptor(Descriptor d) {
37 return left.usesDescriptor(d)||((intindex!=null)&&intindex.usesDescriptor(d));
40 public boolean equals(Map remap, Expr e) {
41 if (e==null||!(e instanceof DotExpr))
43 DotExpr de=(DotExpr)e;
44 if (!de.field.equals(field))
49 } else if (!index.equals(remap,de.index))
51 if (!left.equals(remap,de.left))
57 public DotExpr(Expr left, String field, Expr index) {
63 public Set getRequiredDescriptors() {
64 Set v = left.getRequiredDescriptors();
67 v.addAll(index.getRequiredDescriptors());
72 public Expr getExpr() {
76 public FieldDescriptor getField() {
80 public Expr getIndex() {
84 public void generate(CodeWriter writer, VarDescriptor dest) {
85 VarDescriptor leftd = VarDescriptor.makeNew("left");
87 writer.output("// " + leftd.getSafeSymbol() + " <-- ");
88 left.prettyPrint(writer);
89 writer.outputline("");
91 left.generate(writer, leftd);
93 writer.output("// " + leftd.getSafeSymbol() + " = ");
94 left.prettyPrint(writer);
95 writer.outputline("");
97 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
98 Expr intindex = index;
101 // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor obect that is in teh vector list
102 // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array
103 // descriptor not the underlying field descriptor
105 /* we calculate the offset in bits */
106 offsetbits = struct.getOffsetExpr(fd);
108 if (fd instanceof ArrayDescriptor) {
109 fd = ((ArrayDescriptor) fd).getField();
112 if (intindex != null) {
113 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
114 /* short circuit for constant 0 */
116 Expr basesize = fd.getBaseSizeExpr();
117 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
121 final SymbolTable st = writer.getSymbolTable();
122 TypeDescriptor td = offsetbits.typecheck(new SemanticAnalyzer() {
123 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
124 public SymbolTable getSymbolTable() { return st; }
128 throw new IRException();
129 } else if (td != ReservedTypeDescriptor.INT) {
130 throw new IRException();
133 // #TBD#: ptr's to bits and byte's and stuff are a little iffy...
134 // right now, a bit* is the same as a int* = short* = byte* (that is there
135 // is no post-derefernce mask)
137 // #ATTN#: do we handle int* correctly? what is the correct behavior? we automatically
138 // dereference pointers, but for structures that means that if we have a nested structure
139 // we return an integer address to that nested structure. if we have a pointer to a
140 // structure else where we want that base address ... yeah so we *(int *) it... ok we are
143 boolean dotypecheck = false;
145 if (offsetbits instanceof IntegerLiteralExpr) {
146 int offsetinbits = ((IntegerLiteralExpr) offsetbits).getValue();
147 int offset = offsetinbits >> 3; /* offset in bytes */
149 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
150 int shift = offsetinbits - (offset << 3);
151 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
153 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
154 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
156 "(" + leftd.getSafeSymbol() + " + " + offset + ")) " +
157 " >> " + shift + ") & 0x" + Integer.toHexString(mask) + ";");
158 } else { /* a structure address or a ptr! */
159 String ptr = fd.getPtr() ? "*(int *)" : "";
160 /* type var = [*(int *)] (base + offset) */
162 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
164 writer.outputline("int " + dest.getSafeSymbol() +
165 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ");");
169 } else { /* offset in bits is an expression that must be generated */
170 VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
171 writer.output("// " + ob.getSafeSymbol() + " <-- ");
172 offsetbits.prettyPrint(writer);
173 writer.outputline("");
174 offsetbits.generate(writer, ob);
175 writer.output("// " + ob.getSafeSymbol() + " = ");
176 offsetbits.prettyPrint(writer);
177 writer.outputline("");
179 /* derive offset in bytes */
180 VarDescriptor offset = VarDescriptor.makeNew("offset");
181 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
183 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
184 VarDescriptor shift = VarDescriptor.makeNew("shift");
185 writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() +
186 " - (" + offset.getSafeSymbol() + " << 3);");
187 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
189 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
190 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
192 "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " +
193 " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");
194 } else { /* a structure address or a ptr */
195 String ptr = fd.getPtr() ? "*(int *)" : "";
196 /* type var = [*(int *)] (base + offset) */
198 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
200 writer.outputline("int " + dest.getSafeSymbol() +
201 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
207 if (dotypecheck) { /* typemap checks! */
209 // high is 'low' + sizeof(fd.getDataStructure) <<< can be cached!!
211 // #ATTN#: we need to get the size of the fieldtype (if its a label the type of the label, not the
212 // underlying field's type
214 Expr sizeofexpr = fieldtype.getSizeExpr();
215 VarDescriptor sizeof = VarDescriptor.makeNew("sizeof");
216 sizeofexpr.generate(writer, sizeof);
218 String low = dest.getSafeSymbol();
219 String high = VarDescriptor.makeNew("high").getSafeSymbol();
220 writer.outputline("int " + high + " = " + low + " + " + sizeof.getSafeSymbol() + ";");
221 writer.outputline("assertvalidmemory(" + low + ", " + high + ");");
223 // we need to null value check and conditionalize the rest of the rule... we'll use a hack
224 // here where we store the number of indents in this class... and then provide a static
225 // method to unwind...
226 //writer.outputline("// assertvalidmemory ");
227 //DotExpr.memoryindents++;
228 //writer.outputline("if (" + dest.getSafeSymbol() + " != NULL)");
229 //writer.startblock();
234 private int bitmask(int bits) {
237 for (int i = 0; i < bits; i++) {
245 public void prettyPrint(PrettyPrinter pp) {
246 left.prettyPrint(pp);
247 pp.output("." + field);
250 index.prettyPrint(pp);
255 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
256 TypeDescriptor lefttype = left.typecheck(sa);
257 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
260 /* finished typechecking...so we can fill the fields in */
261 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
262 FieldDescriptor fd = struct.getField(field);
263 LabelDescriptor ld = struct.getLabel(field);
264 if (ld != null) { /* label */
266 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
269 assert intindex == null;
270 intindex = ld.getIndex();
272 fieldtype = fd.getType();
278 if ((lefttype == null) || (index != null && indextype == null)) {
282 if (indextype != null) {
283 if (indextype != ReservedTypeDescriptor.INT) {
284 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
289 if (lefttype instanceof StructureTypeDescriptor) {
290 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
291 FieldDescriptor fd = struct.getField(field);
292 LabelDescriptor ld = struct.getLabel(field);
294 if (fd != null) { /* field */
297 if (indextype == null && fd instanceof ArrayDescriptor) {
298 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
300 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
301 sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
305 this.td = fd.getType();
306 } else if (ld != null) { /* label */
310 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
314 this.td = ld.getType();
316 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
320 /* we promote bit, byte and short to integer types */
321 if (this.td == ReservedTypeDescriptor.BIT ||
322 this.td == ReservedTypeDescriptor.BYTE ||
323 this.td == ReservedTypeDescriptor.SHORT) {
324 this.td = ReservedTypeDescriptor.INT;
329 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");