001: package net.sf.jdec.jvminstructions.commands;
002:
003: import net.sf.jdec.core.GlobalVariableStore;
004: import net.sf.jdec.core.JvmOpCodes;
005: import net.sf.jdec.core.Operand;
006: import net.sf.jdec.core.OperandStack;
007: import net.sf.jdec.reflection.Behaviour;
008: import net.sf.jdec.util.Util;
009:
010: import java.util.List;
011: import java.util.Stack;
012:
013: /*
014: * AAstoreCommand.java Copyright (c) 2006,07 Swaroop Belur
015: *
016: * This program is free software; you can redistribute it and/or
017: * modify it under the terms of the GNU General Public License
018: * as published by the Free Software Foundation; either version 2
019: * of the License, or (at your option) any later version.
020:
021: * This program is distributed in the hope that it will be useful,
022: * but WITHOUT ANY WARRANTY; without even the implied warranty of
023: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
024: * GNU General Public License for more details.
025:
026: * You should have received a copy of the GNU General Public License
027: * along with this program; if not, write to the Free Software
028: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
029: *
030: */
031: public class AAstoreCommand extends AbstractInstructionCommand {
032:
033: public AAstoreCommand(Behaviour context) {
034: super (context);
035: }
036:
037: public int getSkipBytes() {
038: return 0;
039: }
040:
041: public void execute() {
042: Behaviour behavior = getContext();
043: int currentForIndex = getCurrentInstPosInCode();
044: if (skipAASTORE(currentForIndex))
045: return;
046: boolean doNotPop = GlobalVariableStore.isDoNotPop();
047: OperandStack opStack = getContext().getOpStack();
048: boolean embeddedNewArrayCopy = GlobalVariableStore
049: .isEmbeddedNewArrayCopy();
050: Stack arraytimesstack = GlobalVariableStore
051: .getArraytimesstack();
052: int arraytimespush = GlobalVariableStore.getArraytimespush();
053: boolean primitiveastore = GlobalVariableStore
054: .isPrimitiveastore();
055: byte cd[] = getContext().getCode();
056: boolean add = (cd[currentForIndex - 1] == JvmOpCodes.AASTORE);
057: if (!doNotPop) {
058: if (opStack.size() >= 3) { // See test program A.java
059: Operand value = createOperand("");
060: Operand arindex = createOperand("");
061: Operand arRef = createOperand("");
062: if (!embeddedNewArrayCopy) {
063: value = (Operand) opStack.pop();
064: arindex = (Operand) opStack.pop();
065: arRef = (Operand) opStack.pop();
066: } else {
067: arRef = (Operand) opStack.pop();
068:
069: }
070: /* if(specialIASTORE==true && opStack.size() > 2){
071: opStack.pop();
072: opStack.pop();
073: specialIASTORE=false;
074: //Add open bracket ?
075:
076: }*/
077: java.lang.String tempString = "";
078:
079: tempString = arRef.getOperandValue() + "["
080: + arindex.getOperandValue() + "]="
081: + value.getOperandValue() + ";\n";
082: if (arraytimesstack.size() == 0)
083: behavior.appendToBuffer(Util
084: .formatDecompiledStatement(tempString));
085: else {
086: arraytimespush = Integer.parseInt(arraytimesstack
087: .peek().toString());
088: if (arraytimespush > 0) {
089:
090: if (newfound()) {
091: // Operand z=createOperand(op.getOperandValue());
092: Operand y = value;
093: java.lang.String newv = "";
094:
095: newv = arRef.getOperandValue()
096: + y.getOperandValue();
097: arraytimespush = Integer
098: .parseInt(arraytimesstack.pop()
099: .toString());
100: arraytimespush--;
101: if (arraytimespush == 0) {
102: newv += "}";
103: } else {
104: arraytimesstack.push(""
105: + arraytimespush);
106:
107: newv += ",";
108: }
109: /*if(arindex.getOperandValue().trim().equals("0")==false)
110: {
111: opStack.pop();
112: } */
113: opStack.push(createOperand(newv));
114: } else {
115: arraytimespush = Integer
116: .parseInt(arraytimesstack.pop()
117: .toString());
118: arraytimespush--;
119: if (primitiveastore) {
120: if (arraytimespush == 0
121: && arraytimesstack.size() == 0) {
122: Util.forceNewLine = false;
123: behavior
124: .appendToBuffer(Util
125: .formatDecompiledStatement("};\n"));
126:
127: } else if (arraytimespush == 0
128: && arraytimesstack.size() != 0) {
129: Util.forceNewLine = false;
130: behavior
131: .appendToBuffer(Util
132: .formatDecompiledStatement("},\n"));
133:
134: } else {
135: arraytimesstack.push(""
136: + arraytimespush);
137: // codeStatements+=value.getOperandValue()+",";
138: }
139: primitiveastore = false;
140: } else {
141: if (arraytimespush == 0
142: && arraytimesstack.size() == 0
143: && !add) {
144: Util.forceStartSpace = true;
145: Util.newlinebeg = true;
146: behavior
147: .appendToBuffer(Util
148: .formatDecompiledStatement(value
149: .getOperandValue()));
150: Util.forceNewLine = false;
151: //Util.forceStartSpace=false;
152: Util.newlinebeg = false;
153: StringBuffer sb = new StringBuffer(
154: "");
155: if (arrayClosingBracketCount(
156: currentForIndex, sb)) {
157: int total = Integer.parseInt(sb
158: .toString());
159: if (total == 0) {
160: Util.forceBeginStartSpace = false;
161: behavior
162: .appendToBuffer("\n"
163: + Util
164: .formatDecompiledStatement("\n}"));
165: behavior
166: .appendToBuffer(";\n");
167: Util.forceBeginStartSpace = true;
168:
169: } else {
170: Util.forceBeginStartSpace = false;
171: behavior
172: .appendToBuffer("\n"
173: + Util
174: .formatDecompiledStatement("\n}"));
175: behavior
176: .appendToBuffer(";\n");
177: Util.forceBeginStartSpace = true;
178: }
179:
180: } else {
181: Util.forceBeginStartSpace = false;
182: behavior
183: .appendToBuffer("\n"
184: + Util
185: .formatDecompiledStatement("\n}"));
186: behavior.appendToBuffer(";\n");
187: Util.forceBeginStartSpace = true;
188: }
189: Util.forceNewLine = true;
190: Util.forceStartSpace = true;
191: Util.newlinebeg = true;
192:
193: } else if (arraytimespush == 0
194: && arraytimesstack.size() == 0
195: && add) {
196: Util.forceNewLine = false;
197: Util.forceStartSpace = false;
198: Util.newlinebeg = false;
199: StringBuffer sb = new StringBuffer(
200: "");
201: if (arrayClosingBracketCount(
202: currentForIndex, sb)) {
203: int total = Integer.parseInt(sb
204: .toString());
205: if (total == 0) {
206: behavior
207: .appendToBuffer(Util
208: .formatDecompiledStatement("\n}\n"));
209: behavior
210: .appendToBuffer(";\n");
211:
212: } else {
213: behavior
214: .appendToBuffer(Util
215: .formatDecompiledStatement("\n}\n"));
216: behavior
217: .appendToBuffer(";\n");
218: }
219:
220: } else {
221: behavior
222: .appendToBuffer(Util
223: .formatDecompiledStatement("\n}\n"));
224: behavior.appendToBuffer(";\n");
225: }
226: Util.forceNewLine = true;
227: Util.forceStartSpace = true;
228: Util.newlinebeg = true;
229:
230: } else if (arraytimespush == 0
231: && arraytimesstack.size() != 0
232: && !add) {
233: behavior
234: .appendToBuffer(Util
235: .formatDecompiledStatement(value
236: .getOperandValue()));
237: Util.forceNewLine = false;
238: behavior
239: .appendToBuffer(Util
240: .formatDecompiledStatement("},\n"));
241:
242: } else {
243: if (!add) {
244: //arraytimesstack.push(""+arraytimespush);
245: Util.forceNewLine = false;
246: Util.forceStartSpace = false;
247: behavior
248: .appendToBuffer(Util
249: .formatDecompiledStatement(value
250: .getOperandValue()
251: + ","));
252: }
253: if (arraytimespush > 0)
254: arraytimesstack.push(""
255: + arraytimespush);
256:
257: }
258: }
259: }
260: }
261: }
262: }
263: }
264: if (doNotPop) {
265: if (arraytimesstack.size() > 0) {
266:
267: if (opStack.size() > 0) {
268: java.lang.String tpval = opStack.peekTopOfStack()
269: .getOperandValue();
270: try {
271: if (tpval != null) {
272: Integer.parseInt(tpval.trim());
273: opStack.pop();
274: }
275: } catch (NumberFormatException ne) {
276:
277: }
278: }
279:
280: arraytimespush = Integer.parseInt(arraytimesstack.pop()
281: .toString());
282: arraytimespush--;
283: if (arraytimespush == 0) {
284:
285: } else {
286: arraytimesstack.push("" + arraytimespush);
287: }
288:
289: }
290:
291: }
292: if (doNotPop == true)
293: doNotPop = false;
294: if (embeddedNewArrayCopy)
295: embeddedNewArrayCopy = false;
296:
297: GlobalVariableStore.setPrimitiveastore(primitiveastore);
298: GlobalVariableStore.setArraytimespush(arraytimespush);
299: GlobalVariableStore.setArraytimesstack(arraytimesstack);
300: GlobalVariableStore.setDoNotPop(doNotPop);
301: GlobalVariableStore
302: .setEmbeddedNewArrayCopy(embeddedNewArrayCopy);
303:
304: }
305:
306: private boolean skipAASTORE(int pos) {
307: List skipaastores = GlobalVariableStore.getSkipaastores();
308: for (int z = 0; z < skipaastores.size(); z++) {
309:
310: if (((Integer) skipaastores.get(z)).intValue() == pos) {
311: return true;
312: }
313: }
314: return false;
315: }
316:
317: }
|