1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
12
13 class JArrayExpression
14 extends JExpression implements JLhs {
15
16
17 private JExpression theArray;
18
19
20 private JExpression indexExpr;
21
22
33
34 public JArrayExpression(int line, JExpression theArray,
35 JExpression indexExpr) {
36 super(line);
37 this.theArray = theArray;
38 this.indexExpr = indexExpr;
39 }
40
41
49
50 public JExpression analyze(Context context) {
51 theArray = (JExpression) theArray.analyze(context);
52 indexExpr = (JExpression) indexExpr.analyze(context);
53 if (!(theArray.type().isArray())) {
54 JAST.compilationUnit.reportSemanticError(line(),
55 "attempt to index a non-array object");
56 this.type = Type.ANY;
57 } else {
58 this.type = theArray.type().componentType();
59 }
60 indexExpr.type().mustMatchExpected(line(), Type.INT);
61 return this;
62 }
63
64
71
72 public JExpression analyzeLhs(Context context) {
73 analyze(context);
74 return this;
75 }
76
77
86
87 public void codegen(CLEmitter output) {
88 theArray.codegen(output);
89 indexExpr.codegen(output);
90 if (type == Type.INT) {
91 output.addNoArgInstruction(IALOAD);
92 } else if (type == Type.BOOLEAN) {
93 output.addNoArgInstruction(BALOAD);
94 } else if (type == Type.CHAR) {
95 output.addNoArgInstruction(CALOAD);
96 } else if (!type.isPrimitive()) {
97 output.addNoArgInstruction(AALOAD);
98 }
99 }
100
101
110
111 public void codegenLoadLhsLvalue(CLEmitter output) {
112 theArray.codegen(output);
115 indexExpr.codegen(output);
116 }
117
118
128
129 public void codegenLoadLhsRvalue(CLEmitter output) {
130 if (type == Type.STRING) {
133 output.addNoArgInstruction(DUP2_X1);
134 } else {
135 output.addNoArgInstruction(DUP2);
136 }
137 if (type == Type.INT) {
138 output.addNoArgInstruction(IALOAD);
139 } else if (type == Type.BOOLEAN) {
140 output.addNoArgInstruction(BALOAD);
141 } else if (type == Type.CHAR) {
142 output.addNoArgInstruction(CALOAD);
143 } else if (!type.isPrimitive()) {
144 output.addNoArgInstruction(AALOAD);
145 }
146 }
147
148
159
160 public void codegenDuplicateRvalue(CLEmitter output) {
161 output.addNoArgInstruction(DUP_X2);
163 }
164
165
173
174 public void codegenStore(CLEmitter output) {
175 if (type == Type.INT) {
176 output.addNoArgInstruction(IASTORE);
177 } else if (type == Type.BOOLEAN) {
178 output.addNoArgInstruction(BASTORE);
179 } else if (type == Type.CHAR) {
180 output.addNoArgInstruction(CASTORE);
181 } else if (!type.isPrimitive()) {
182 output.addNoArgInstruction(AASTORE);
183 }
184
185 }
186
187
190
191 public void writeToStdOut(PrettyPrinter p) {
192 p.println("<JArrayExpression>");
193 p.indentRight();
194 if (theArray != null) {
195 p.println("<TheArray>");
196 p.indentRight();
197 theArray.writeToStdOut(p);
198 p.indentLeft();
199 p.println("</TheArray>");
200 }
201 if (indexExpr != null) {
202 p.println("<IndexExpression>");
203 p.indentRight();
204 indexExpr.writeToStdOut(p);
205 p.indentLeft();
206 p.println("</IndexExpression>");
207 }
208 p.indentLeft();
209 p.println("</JArrayExpression>");
210 }
211 }
212