5 public class DotExpr extends Expr {
12 static int memoryindents = 0;
14 public static void generate_memory_endblocks(CodeWriter cr) {
15 while (memoryindents > 0) {
23 public DotExpr(Expr left, String field, Expr index) {
29 public Set getRequiredDescriptors() {
30 Set v = left.getRequiredDescriptors();
33 v.addAll(index.getRequiredDescriptors());
39 public Expr getExpr() {
43 public void generate(CodeWriter writer, VarDescriptor dest) {
44 VarDescriptor leftd = VarDescriptor.makeNew("left");
46 writer.output("// " + leftd.getSafeSymbol() + " <-- ");
47 left.prettyPrint(writer);
48 writer.outputline("");
50 left.generate(writer, leftd);
52 writer.output("// " + leftd.getSafeSymbol() + " = ");
53 left.prettyPrint(writer);
54 writer.outputline("");
56 StructureTypeDescriptor struct = (StructureTypeDescriptor) left.getType();
57 FieldDescriptor fd = struct.getField(field);
58 LabelDescriptor ld = struct.getLabel(field);
59 TypeDescriptor fieldtype;
60 Expr intindex = index;
63 if (ld != null) { /* label */
65 fieldtype = ld.getType(); // d.s ==> Superblock, while, d.b ==> Block
68 assert intindex == null;
69 intindex = ld.getIndex();
71 fieldtype = fd.getType();
74 // #ATTN#: getOffsetExpr needs to be called with the fielddescriptor obect that is in teh vector list
75 // this means that if the field is an arraydescriptor you have to call getOffsetExpr with the array
76 // descriptor not the underlying field descriptor
78 /* we calculate the offset in bits */
79 offsetbits = struct.getOffsetExpr(fd);
81 if (fd instanceof ArrayDescriptor) {
82 fd = ((ArrayDescriptor) fd).getField();
85 if (intindex != null) {
86 if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) {
87 /* short circuit for constant 0 */
89 Expr basesize = fd.getBaseSizeExpr();
90 offsetbits = new OpExpr(Opcode.ADD, offsetbits, new OpExpr(Opcode.MULT, basesize, intindex));
94 final SymbolTable st = writer.getSymbolTable();
95 TypeDescriptor td = offsetbits.typecheck(new SemanticAnalyzer() {
96 public IRErrorReporter getErrorReporter() { throw new IRException("badness"); }
97 public SymbolTable getSymbolTable() { return st; }
101 throw new IRException();
102 } else if (td != ReservedTypeDescriptor.INT) {
103 throw new IRException();
106 // #TBD#: ptr's to bits and byte's and stuff are a little iffy...
107 // right now, a bit* is the same as a int* = short* = byte* (that is there
108 // is no post-derefernce mask)
110 // #ATTN#: do we handle int* correctly? what is the correct behavior? we automatically
111 // dereference pointers, but for structures that means that if we have a nested structure
112 // we return an integer address to that nested structure. if we have a pointer to a
113 // structure else where we want that base address ... yeah so we *(int *) it... ok we are
116 boolean dotypecheck = false;
118 if (offsetbits instanceof IntegerLiteralExpr) {
119 int offsetinbits = ((IntegerLiteralExpr) offsetbits).getValue();
120 int offset = offsetinbits >> 3; /* offset in bytes */
122 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
123 int shift = offsetinbits - (offset << 3);
124 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
126 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
127 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
129 "(" + leftd.getSafeSymbol() + " + " + offset + ")) " +
130 " >> " + shift + ") & 0x" + Integer.toHexString(mask) + ";");
131 } else { /* a structure address or a ptr! */
132 String ptr = fd.getPtr() ? "*(int *)" : "";
133 /* type var = [*(int *)] (base + offset) */
135 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
137 writer.outputline("int " + dest.getSafeSymbol() +
138 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset + ");");
142 } else { /* offset in bits is an expression that must be generated */
143 VarDescriptor ob = VarDescriptor.makeNew("offsetinbits");
144 writer.output("// " + ob.getSafeSymbol() + " <-- ");
145 offsetbits.prettyPrint(writer);
146 writer.outputline("");
147 offsetbits.generate(writer, ob);
148 writer.output("// " + ob.getSafeSymbol() + " = ");
149 offsetbits.prettyPrint(writer);
150 writer.outputline("");
152 /* derive offset in bytes */
153 VarDescriptor offset = VarDescriptor.makeNew("offset");
154 writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;");
156 if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) {
157 VarDescriptor shift = VarDescriptor.makeNew("shift");
158 writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() +
159 " - (" + offset.getSafeSymbol() + " << 3);");
160 int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue());
162 /* type var = ((*(int *) (base + offset)) >> shift) & mask */
163 writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol() +
165 "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ")) " +
166 " >> " + shift.getSafeSymbol() + ") & 0x" + Integer.toHexString(mask) + ";");
167 } else { /* a structure address or a ptr */
168 String ptr = fd.getPtr() ? "*(int *)" : "";
169 /* type var = [*(int *)] (base + offset) */
171 // #ATTN: was 'getType.getGeneratedType()' instead of 'int' but all pointers are represented
173 writer.outputline("int " + dest.getSafeSymbol() +
174 " = " + ptr + "(" + leftd.getSafeSymbol() + " + " + offset.getSafeSymbol() + ");");
180 if (dotypecheck) { /* typemap checks! */
182 // high is 'low' + sizeof(fd.getDataStructure) <<< can be cached!!
184 // #ATTN#: we need to get the size of the fieldtype (if its a label the type of the label, not the
185 // underlying field's type
187 Expr sizeofexpr = fieldtype.getSizeExpr();
188 VarDescriptor sizeof = VarDescriptor.makeNew("sizeof");
189 sizeofexpr.generate(writer, sizeof);
191 String low = dest.getSafeSymbol();
192 String high = VarDescriptor.makeNew("high").getSafeSymbol();
193 writer.outputline("int " + high + " = " + low + " + " + sizeof.getSafeSymbol() + ";");
194 writer.outputline("assertvalidmemory(" + low + ", " + high + ");");
196 // we need to null value check and conditionalize the rest of the rule... we'll use a hack
197 // here where we store the number of indents in this class... and then provide a static
198 // method to unwind...
199 //writer.outputline("// assertvalidmemory ");
200 //DotExpr.memoryindents++;
201 //writer.outputline("if (" + dest.getSafeSymbol() + " != NULL)");
202 //writer.startblock();
207 private int bitmask(int bits) {
210 for (int i = 0; i < bits; i++) {
218 public void prettyPrint(PrettyPrinter pp) {
219 left.prettyPrint(pp);
220 pp.output("." + field);
223 index.prettyPrint(pp);
228 public TypeDescriptor typecheck(SemanticAnalyzer sa) {
229 TypeDescriptor lefttype = left.typecheck(sa);
230 TypeDescriptor indextype = index == null ? null : index.typecheck(sa);
232 if ((lefttype == null) || (index != null && indextype == null)) {
236 if (indextype != null) {
237 if (indextype != ReservedTypeDescriptor.INT) {
238 sa.getErrorReporter().report(null, "Index must be of type 'int' not '" + indextype.getSymbol() + "'");
243 if (lefttype instanceof StructureTypeDescriptor) {
244 StructureTypeDescriptor struct = (StructureTypeDescriptor) lefttype;
245 FieldDescriptor fd = struct.getField(field);
246 LabelDescriptor ld = struct.getLabel(field);
248 if (fd != null) { /* field */
251 if (indextype == null && fd instanceof ArrayDescriptor) {
252 sa.getErrorReporter().report(null, "Must specify an index what accessing array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
254 } else if (indextype != null && !(fd instanceof ArrayDescriptor)) {
255 sa.getErrorReporter().report(null, "Cannot specify an index when accessing non-array field '" + struct.getSymbol() + "." + fd.getSymbol() + "'");
259 this.td = fd.getType();
260 } else if (ld != null) { /* label */
264 sa.getErrorReporter().report(null, "A label cannot be accessed as an array");
268 this.td = ld.getType();
270 sa.getErrorReporter().report(null, "No such field or label '" + field + "' in structure '" + struct.getSymbol() + "'");
274 /* we promote bit, byte and short to integer types */
275 if (this.td == ReservedTypeDescriptor.BIT ||
276 this.td == ReservedTypeDescriptor.BYTE ||
277 this.td == ReservedTypeDescriptor.SHORT) {
278 this.td = ReservedTypeDescriptor.INT;
283 sa.getErrorReporter().report(null, "Left hand side of . expression must be a structure type, not '" + lefttype.getSymbol() + "'");