001: /*
002: * Dupx2Command.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/itor
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019: package net.sf.jdec.jvminstructions.commands;
020:
021: import net.sf.jdec.core.GlobalVariableStore;
022: import net.sf.jdec.core.JvmOpCodes;
023: import net.sf.jdec.core.Operand;
024: import net.sf.jdec.core.OperandStack;
025: import net.sf.jdec.reflection.Behaviour;
026:
027: public class Dupx2Command extends AbstractInstructionCommand {
028:
029: public Dupx2Command(Behaviour context) {
030: super (context);
031:
032: }
033:
034: public int getSkipBytes() {
035: return 0;
036: }
037:
038: public void execute() {
039: OperandStack opStack = getStack();
040: int currentForIndex = getCurrentInstPosInCode();
041: Operand op1 = (Operand) opStack.pop();
042: Operand op2 = (Operand) opStack.pop();
043: Operand op3 = (Operand) opStack.pop();
044: boolean add = true;
045: int next = currentForIndex + 1;
046: int prev = currentForIndex - 1;
047: byte[] code = getCode();
048: boolean specialCase = false;
049: if (code[next] == JvmOpCodes.FASTORE) {
050: if (code[prev] == JvmOpCodes.FADD
051: || code[prev] == JvmOpCodes.FSUB) {
052: specialCase = true;
053: if (code[prev] == JvmOpCodes.FSUB)
054: add = false;
055: }
056: }
057: if (!specialCase) {
058: if (code[next] == JvmOpCodes.IASTORE) {
059: if (code[prev] == JvmOpCodes.IADD
060: || code[prev] == JvmOpCodes.ISUB) {
061: specialCase = true;
062: if (code[prev] == JvmOpCodes.ISUB)
063: add = false;
064: }
065: }
066: }
067: if (!specialCase) {
068: if (code[next] == JvmOpCodes.SASTORE) {
069: if (code[prev] == JvmOpCodes.I2S) {
070: prev = prev - 1;
071: if (code[prev] == JvmOpCodes.IADD
072: || code[prev] == JvmOpCodes.ISUB) {
073: specialCase = true;
074: if (code[prev] == JvmOpCodes.ISUB)
075: add = false;
076: }
077: }
078: }
079: }
080: if (!specialCase) {
081: if (code[next] == JvmOpCodes.BASTORE) {
082: if (code[prev] == JvmOpCodes.I2B) {
083: prev = prev - 1;
084: if (code[prev] == JvmOpCodes.IADD
085: || code[prev] == JvmOpCodes.ISUB) {
086: specialCase = true;
087: if (code[prev] == JvmOpCodes.ISUB)
088: add = false;
089: }
090: }
091: }
092: }
093: if (!specialCase) {
094: if (code[next] == JvmOpCodes.CASTORE) {
095: if (code[prev] == JvmOpCodes.I2C) {
096: prev = prev - 1;
097: if (code[prev] == JvmOpCodes.IADD
098: || code[prev] == JvmOpCodes.ISUB) {
099: specialCase = true;
100: if (code[prev] == JvmOpCodes.ISUB)
101: add = false;
102: }
103: }
104: }
105: }
106: if (!specialCase) {
107: opStack.push(op1);
108: opStack.push(op3);
109: opStack.push(op2);
110: opStack.push(op1);
111: } else {
112:
113: // stack.push(op3);
114: // stack.push(op4);
115: java.lang.String temp = "";
116: boolean skip = true;
117: /*
118: * if(code[prev]==JvmOpCodes.FADD || code[prev]==JvmOpCodes.IADD){
119: * temp="++"+op3.getOperandValue()+"[" +op2.getOperandValue()+"]"; }
120: * else if(code[prev]==JvmOpCodes.FSUB ||
121: * code[prev]==JvmOpCodes.ISUB){ temp="--"+op3.getOperandValue()+"["
122: * +op2.getOperandValue()+"]"; } else{
123: * temp=op3.getOperandValue()+"[" +op2.getOperandValue()+"]";
124: * skip=false; }
125: */
126: if (newfound()
127: && GlobalVariableStore.getArraytimesstack().size() > 0) {
128: java.lang.String v = "";
129: if (add) {
130: int br = op1.getOperandValue().indexOf("(");
131: if (br == -1) {
132: v = "++"
133: + op1.getOperandValue().substring(
134: 0,
135: op1.getOperandValue()
136: .lastIndexOf("]") + 1);
137: opStack.push(createOperand(v));
138: } else {
139: v = "++"
140: + op1.getOperandValue().substring(
141: br + 1,
142: op1.getOperandValue()
143: .lastIndexOf("]") + 1);
144: opStack.push(createOperand(v));
145: }
146: } else {
147: int br = op1.getOperandValue().indexOf("(");
148: if (br == -1) {
149: v = "--"
150: + op1.getOperandValue().substring(
151: 0,
152: op1.getOperandValue()
153: .lastIndexOf("]") + 1);
154: opStack.push(createOperand(v));
155: } else {
156: v = "--"
157: + op1.getOperandValue().substring(
158: br + 1,
159: op1.getOperandValue()
160: .lastIndexOf("]") + 1);
161: opStack.push(createOperand(v));
162: }
163: }
164: opStack.push(createOperand(v));
165: opStack.push(op3);
166: opStack.push(op2);
167: opStack.push(createOperand(v));
168: } else {
169: temp = op3.getOperandValue() + "["
170: + op2.getOperandValue() + "]";
171: Operand otemp = new Operand();
172: otemp.setOperandValue(temp);
173: opStack.push(otemp);
174: opStack.push(op3);
175: opStack.push(op2);
176: opStack.push(op1);
177: }
178:
179: /*
180: * if(!skip){ Operand otemp=new Operand();
181: * otemp.setOperandValue(temp); opStack.push(otemp);
182: * opStack.push(op3); opStack.push(op2); opStack.push(op1); } else{
183: * Operand otemp=new Operand(); otemp.setOperandValue(temp);
184: * opStack.push(otemp); }
185: */
186: }
187:
188: }
189:
190: }
|