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 DotExpr(Expr left, String field, Expr index) {
32 FieldDescriptor fd = struct.getField(field);
33 LabelDescriptor ld = struct.getLabel(field);
34 if (ld != null) { /* label */
36 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
39 assert intindex == null;
40 intindex = ld.getIndex();
42 fieldtype = fd.getType();
48 public Set getRequiredDescriptors() {
49 Set v = left.getRequiredDescriptors();
52 v.addAll(index.getRequiredDescriptors());
57 public Expr getExpr() {
61 public FieldDescriptor getField() {
65 public Expr getIndex() {
69 public void generate(CodeWriter writer, VarDescriptor dest) {
70 VarDescriptor leftd = VarDescriptor.makeNew("left");
72 writer.output("// " + leftd.getSafeSymbol() + " <-- ");
73 left.prettyPrint(writer);
74 writer.outputline("");
76 left.generate(writer, leftd);
78 writer.output("// " + leftd.getSafeSymbol() + " = ");
79 left.prettyPrint(writer);
80 writer.outputline("");
82 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
83 Expr intindex = index;
86 // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor obect that is in teh vector list
87 // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array
88 // descriptor not the underlying field descriptor
90 /* we calculate the offset in bits */
91 offsetbits = struct.getOffsetExpr(fd);
93 if (fd instanceof ArrayDescriptor) {
94 fd = ((ArrayDescriptor) fd).getField();
97 if (intindex != null) {
98 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
99 /* short circuit for constant 0 */
101 Expr basesize = fd.getBaseSizeExpr();
102 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
106 final SymbolTable st = writer.getSymbolTable();
107 TypeDescriptor td = offsetbits.typecheck(new SemanticAnalyzer() {
108 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
109 public SymbolTable getSymbolTable() { return st; }
113 throw new IRException();
114 } else if (td != ReservedTypeDescriptor.INT) {
115 throw new IRException();
118 // #TBD#: ptr's to bits and byte's and stuff are a little iffy...
119 // right now, a bit* is the same as a int* = short* = byte* (that is there
120 // is no post-derefernce mask)
122 // #ATTN#: do we handle int* correctly? what is the correct behavior? we automatically
123 // dereference pointers, but for structures that means that if we have a nested structure
124 // we return an integer address to that nested structure. if we have a pointer to a
125 // structure else where we want that base address ... yeah so we *(int *) it... ok we are
128 boolean dotypecheck = false;
130 if (offsetbits instanceof IntegerLiteralExpr) {
131 int offsetinbits = ((IntegerLiteralExpr) offsetbits).getValue();
132 int offset = offsetinbits >> 3; /* offset in bytes */
134 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
135 int shift = offsetinbits - (offset << 3);
136 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
138 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
139 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
141 "(" + leftd.getSafeSymbol() + " + " + offset + ")) " +
142 " >> " + shift + ") & 0x" + Integer.toHexString(mask) + ";");
143 } else { /* a structure address or a ptr! */
144 String ptr = fd.getPtr() ? "*(int *)" : "";
145 /* type var = [*(int *)] (base + offset) */
147 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
149 writer.outputline("int " + dest.getSafeSymbol() +
150 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ");");
154 } else { /* offset in bits is an expression that must be generated */
155 VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
156 writer.output("// " + ob.getSafeSymbol() + " <-- ");
157 offsetbits.prettyPrint(writer);
158 writer.outputline("");
159 offsetbits.generate(writer, ob);
160 writer.output("// " + ob.getSafeSymbol() + " = ");
161 offsetbits.prettyPrint(writer);
162 writer.outputline("");
164 /* derive offset in bytes */
165 VarDescriptor offset = VarDescriptor.makeNew("offset");
166 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
168 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
169 VarDescriptor shift = VarDescriptor.makeNew("shift");
170 writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() +
171 " - (" + offset.getSafeSymbol() + " << 3);");
172 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
174 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
175 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
177 "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " +
178 " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");
179 } else { /* a structure address or a ptr */
180 String ptr = fd.getPtr() ? "*(int *)" : "";
181 /* type var = [*(int *)] (base + offset) */
183 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
185 writer.outputline("int " + dest.getSafeSymbol() +
186 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
192 if (dotypecheck) { /* typemap checks! */
194 // high is 'low' + sizeof(fd.getDataStructure) <<< can be cached!!
196 // #ATTN#: we need to get the size of the fieldtype (if its a label the type of the label, not the
197 // underlying field's type
199 Expr sizeofexpr = fieldtype.getSizeExpr();
200 VarDescriptor sizeof = VarDescriptor.makeNew("sizeof");
201 sizeofexpr.generate(writer, sizeof);
203 String low = dest.getSafeSymbol();
204 String high = VarDescriptor.makeNew("high").getSafeSymbol();
205 writer.outputline("int " + high + " = " + low + " + " + sizeof.getSafeSymbol() + ";");
206 writer.outputline("assertvalidmemory(" + low + ", " + high + ");");
208 // we need to null value check and conditionalize the rest of the rule... we'll use a hack
209 // here where we store the number of indents in this class... and then provide a static
210 // method to unwind...
211 //writer.outputline("// assertvalidmemory ");
212 //DotExpr.memoryindents++;
213 //writer.outputline("if (" + dest.getSafeSymbol() + " != NULL)");
214 //writer.startblock();
219 private int bitmask(int bits) {
222 for (int i = 0; i < bits; i++) {
230 public void prettyPrint(PrettyPrinter pp) {
231 left.prettyPrint(pp);
232 pp.output("." + field);
235 index.prettyPrint(pp);
240 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
241 TypeDescriptor lefttype = left.typecheck(sa);
242 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
244 if ((lefttype == null) || (index != null && indextype == null)) {
248 if (indextype != null) {
249 if (indextype != ReservedTypeDescriptor.INT) {
250 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
255 if (lefttype instanceof StructureTypeDescriptor) {
256 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
257 FieldDescriptor fd = struct.getField(field);
258 LabelDescriptor ld = struct.getLabel(field);
260 if (fd != null) { /* field */
263 if (indextype == null && fd instanceof ArrayDescriptor) {
264 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
266 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
267 sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
271 this.td = fd.getType();
272 } else if (ld != null) { /* label */
276 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
280 this.td = ld.getType();
282 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
286 /* we promote bit, byte and short to integer types */
287 if (this.td == ReservedTypeDescriptor.BIT ||
288 this.td == ReservedTypeDescriptor.BYTE ||
289 this.td == ReservedTypeDescriptor.SHORT) {
290 this.td = ReservedTypeDescriptor.INT;
295 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");