001: /*
002: *
003: * This program is free software; you can redistribute it and/or
004: * modify it under the terms of the GNU General Public License
005: * as published by the Free Software Foundation; either version 2
006: * of the License, or (at your option) any later version.
007:
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012:
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, write to the Free Software
015: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
016: *
017: */
018:
019: package net.sf.jdec.lookup;
020:
021: import net.sf.jdec.blocks.IFBlock;
022: import net.sf.jdec.blocks.Loop;
023: import net.sf.jdec.blocks.Switch;
024: import net.sf.jdec.core.JvmOpCodes;
025:
026: /*
027: * LoadInstrFinder.java Copyright (c) 2006,07 Swaroop Belur
028: *
029: * This program is free software; you can redistribute it and/or
030: * modify it under the terms of the GNU General Public License
031: * as published by the Free Software Foundation; either version 2
032: * of the License, or (at your option) any later version.
033:
034: * This program is distributed in the hope that it will be useful,
035: * but WITHOUT ANY WARRANTY; without even the implied warranty of
036: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
037: * GNU General Public License for more details.
038:
039: * You should have received a copy of the GNU General Public License
040: * along with this program; if not, write to the Free Software
041: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
042: *
043: */
044:
045: /**
046: * Implementation specifics:
047: * a> Implements all find methods concerning load instructions here.
048: * b> throws UnsupportedOperationException for all other find methods.
049: *
050: * @author swaroop belur(belurs)
051: * @version 1.2.1
052: */
053: public class LoadInstrFinder extends BasicFinder {
054:
055: public static final LoadInstrFinder loadFinder = new LoadInstrFinder();
056:
057: private LoadInstrFinder() {
058: }
059:
060: public boolean isCurrentInstStore(int pos) {
061: throw new UnsupportedOperationException(
062: "Invalid call[isCurrentInstStore] on LoadInstrFinder Object");
063: }
064:
065: public boolean isGotoPrecededByDUPSTORE(int pos) {
066: throw new UnsupportedOperationException(
067: "Invalid call[isGotoPrecededByDUPSTORE] on LoadInstrFinder Object");
068: }
069:
070: public boolean isHandlerEndPresentAtGuardEnd(int i) {
071: throw new UnsupportedOperationException(
072: "Invalid call[isHandlerEndPresentAtGuardEnd] on LoadInstrFinder Object");
073: }
074:
075: public boolean isEndOfGuard(int pos) {
076: throw new UnsupportedOperationException(
077: "Invalid call[isEndOfGuard] on LoadInstrFinder Object");
078: }
079:
080: public boolean isIfConditionForSomeOtherLoop(int pos,
081: StringBuffer lend) {
082: throw new UnsupportedOperationException(
083: "Invalid call[isIfConditionForSomeOtherLoop] on LoadInstrFinder Object");
084: }
085:
086: public boolean isIfFirstIfInLoopCondition(int pos) {
087: throw new UnsupportedOperationException(
088: "Invalid call[isIfFirstIfInLoopCondition] on LoadInstrFinder Object");
089: }
090:
091: public boolean isIfForLoadClass(IFBlock ifst) {
092: throw new UnsupportedOperationException(
093: "Invalid call[isIfForLoadClass] on LoadInstrFinder object");
094: }
095:
096: public boolean isIFForThisElseATernaryIF(int pos, StringBuffer sb) {
097: throw new UnsupportedOperationException(
098: "Invalid call[isIFForThisElseATernaryIF] on LoadInstrFinder object");
099: }
100:
101: public Loop isIfInADoWhile(int pos, IFBlock ifst) {
102: throw new UnsupportedOperationException(
103: "Invalid call[isIfInADoWhile] on LoadInstrFinder object");
104: }
105:
106: public Switch.Case isIFInCase(int pos, IFBlock ifs) {
107: throw new UnsupportedOperationException(
108: "Invalid call[isIfInCase] on LoadInstrFinder object");
109: }
110:
111: public boolean isIfPartOfTernaryIfCond(int pos) {
112: throw new UnsupportedOperationException(
113: "Invalid call[isIfPartOfTernaryIfCond] on LoadInstrFinder object");
114: }
115:
116: public boolean isIFpresentInTernaryList(IFBlock ifst) {
117: throw new UnsupportedOperationException(
118: "Invalid call[isIFpresentInTernaryList] on LoadInstrFinder object");
119: }
120:
121: public boolean isIFShortcutORComp(int pos) {
122: throw new UnsupportedOperationException(
123: "Invalid call[isIFShortcutORComp] on LoadInstrFinder object");
124: }
125:
126: public boolean isIndexEndOfLoop(int pos) {
127: throw new UnsupportedOperationException(
128: "Invalid call[isIndexEndOfLoop] on LoadInstrFinder object");
129: }
130:
131: /**
132: * Pass the instuction position to search for.
133: */
134: public boolean isInstrPosAAload(int pos) {
135:
136: boolean start = isThisInstrStart(pos);
137: if (!start)
138: return false;
139: int inst = getMethod().getCode()[pos];
140: return inst == JvmOpCodes.AALOAD;
141: }
142:
143: /**
144: * @param pos - position to be initially tested for in the code
145: * @param bf - containing the index of the aload instruction [ ex: 0 in case of aload_0]
146: * @return - -1 if not found or pos of instr in code.
147: * @
148: */
149: public int isInstAload(int pos, StringBuffer bf) {
150: if (isThisInstrStart(pos)) {
151: switch (getMethod().getCode()[pos]) {
152: case JvmOpCodes.ALOAD_0:
153: bf.append(0);
154: return pos;
155: case JvmOpCodes.ALOAD_1:
156: bf.append(1);
157: return pos;
158: case JvmOpCodes.ALOAD_2:
159: bf.append(2);
160: return pos;
161: case JvmOpCodes.ALOAD_3:
162: bf.append(3);
163: return pos;
164: case JvmOpCodes.ALOAD:
165: bf.append(getMethod().getCode()[(pos + 1)]);
166: return pos;
167: }
168: }
169:
170: int temp = pos - 1;
171: if (isThisInstrStart(temp)) {
172: if (getMethod().getCode()[temp] == JvmOpCodes.ALOAD) {
173: bf.append(getMethod().getCode()[pos]);
174: return temp;
175: }
176: }
177: return -1;
178: }
179:
180: public int isInstIloadInst(int pos, StringBuffer buffer) {
181: if (isThisInstrStart(pos)) {
182: switch (getMethod().getCode()[pos]) {
183: case JvmOpCodes.ILOAD_0:
184: buffer.append(0);
185: return pos;
186:
187: case JvmOpCodes.ILOAD_1:
188:
189: buffer.append(1);
190: return pos;
191:
192: case JvmOpCodes.ILOAD_2:
193:
194: buffer.append(2);
195: return pos;
196:
197: case JvmOpCodes.ILOAD_3:
198:
199: buffer.append(3);
200: return pos;
201:
202: case JvmOpCodes.ILOAD:
203: buffer.append(getMethod().getCode()[pos + 1]);
204: return pos;
205:
206: }
207: }
208: int temp = pos - 1;
209: if (temp >= 0 && isThisInstrStart(temp)
210: && getMethod().getCode()[temp] == JvmOpCodes.ILOAD) {
211: buffer.append(getMethod().getCode()[pos]);
212: return temp;
213:
214: }
215:
216: return -1;
217:
218: }
219:
220: public int isInstLloadInst(int s, StringBuffer sb2) {
221: if (isThisInstrStart(s)) {
222: switch (getMethod().getCode()[(s)]) {
223: case JvmOpCodes.LLOAD_0:
224: sb2.append(0);
225: return s;
226:
227: case JvmOpCodes.LLOAD_1:
228:
229: sb2.append(1);
230: return s;
231:
232: case JvmOpCodes.LLOAD_2:
233:
234: sb2.append(2);
235: return s;
236:
237: case JvmOpCodes.LLOAD_3:
238:
239: sb2.append(3);
240: return s;
241:
242: /*case JvmOpCodes.ILOAD:
243: sb2.append(code[s+1]);
244: return s;*/
245:
246: }
247: }
248: int temp = s - 1;
249: if (isThisInstrStart(temp)) {
250: if (getMethod().getCode()[temp] == JvmOpCodes.LLOAD) {
251: sb2.append(getMethod().getCode()[s]);
252: return temp;
253:
254: }
255: }
256:
257: return -1;
258:
259: }
260:
261: public boolean isInstLoopStart(int pos) {
262: throw new UnsupportedOperationException(
263: "Invalid call[isInstLoopStart] on LoadInstrFinder object");
264: }
265:
266: public boolean isInstPrimitiveArrayStore(int inst) {
267: throw new UnsupportedOperationException(
268: "Invalid call[isInstPrimitiveArrayStore] on LoadInstrFinder object");
269: }
270:
271: public boolean isInstReturnInst(int pos, StringBuffer sb) {
272: throw new UnsupportedOperationException(
273: "Invalid call[isInstReturnInst] on LoadInstrFinder object");
274: }
275:
276: public boolean isInstructionIF(int instruction) {
277: throw new UnsupportedOperationException(
278: "Invalid call[isInstructionIF] on LoadInstrFinder object");
279: }
280:
281: public boolean isInstStore0(int pos) {
282: throw new UnsupportedOperationException(
283: "Invalid call[isInstStore0] on LoadInstrFinder object");
284: }
285:
286: public boolean isNextInstAStore(int pos) {
287: throw new UnsupportedOperationException(
288: "Invalid call[isNextInstAStore] on LoadInstrFinder object");
289: }
290:
291: public boolean isNextInstructionLoad(int pos) {
292: boolean flag = false;
293: if (!isThisInstrStart(pos))
294: return flag;
295:
296: switch (getMethod().getCode()[pos]) {
297:
298: case JvmOpCodes.BIPUSH:
299: flag = true;
300: break;
301:
302: case JvmOpCodes.SIPUSH:
303: flag = true;
304: break;
305:
306: case JvmOpCodes.AALOAD:
307: flag = true;
308: break;
309:
310: case JvmOpCodes.BALOAD:
311: flag = true;
312: break;
313:
314: case JvmOpCodes.DALOAD:
315: flag = true;
316: break;
317:
318: case JvmOpCodes.FALOAD:
319: flag = true;
320: break;
321:
322: case JvmOpCodes.LALOAD:
323: flag = true;
324: break;
325:
326: case JvmOpCodes.IALOAD:
327: flag = true;
328: break;
329:
330: case JvmOpCodes.SALOAD:
331: flag = true;
332: break;
333:
334: case JvmOpCodes.CALOAD:
335: flag = true;
336: break;
337:
338: case JvmOpCodes.ALOAD:
339: flag = true;
340: break;
341: case JvmOpCodes.ALOAD_0:
342: flag = true;
343: break;
344: case JvmOpCodes.ALOAD_1:
345: flag = true;
346: break;
347: case JvmOpCodes.ALOAD_2:
348: flag = true;
349: break;
350: case JvmOpCodes.ALOAD_3:
351: flag = true;
352: break;
353: case JvmOpCodes.ILOAD:
354: flag = true;
355: break;
356: case JvmOpCodes.ILOAD_0:
357: flag = true;
358: break;
359: case JvmOpCodes.ILOAD_1:
360: flag = true;
361: break;
362: case JvmOpCodes.ILOAD_2:
363: flag = true;
364: break;
365: case JvmOpCodes.ILOAD_3:
366: flag = true;
367: break;
368:
369: case JvmOpCodes.LLOAD:
370: flag = true;
371: break;
372: case JvmOpCodes.LLOAD_0:
373: flag = true;
374: break;
375: case JvmOpCodes.LLOAD_1:
376: flag = true;
377: break;
378: case JvmOpCodes.LLOAD_2:
379: flag = true;
380: break;
381: case JvmOpCodes.LLOAD_3:
382: flag = true;
383: break;
384:
385: case JvmOpCodes.FLOAD:
386: flag = true;
387: break;
388: case JvmOpCodes.FLOAD_0:
389: flag = true;
390: break;
391: case JvmOpCodes.FLOAD_1:
392: flag = true;
393: break;
394: case JvmOpCodes.FLOAD_2:
395: flag = true;
396: break;
397: case JvmOpCodes.FLOAD_3:
398: flag = true;
399: break;
400:
401: case JvmOpCodes.DLOAD:
402: flag = true;
403: break;
404: case JvmOpCodes.DLOAD_0:
405: flag = true;
406: break;
407: case JvmOpCodes.DLOAD_1:
408: flag = true;
409: break;
410: case JvmOpCodes.DLOAD_2:
411: flag = true;
412: break;
413: case JvmOpCodes.DLOAD_3:
414: flag = true;
415: break;
416: case JvmOpCodes.ICONST_0:
417: case JvmOpCodes.ICONST_1:
418: case JvmOpCodes.ICONST_2:
419: case JvmOpCodes.ICONST_3:
420: case JvmOpCodes.ICONST_M1:
421: case JvmOpCodes.ICONST_4:
422: case JvmOpCodes.ICONST_5:
423: case JvmOpCodes.LCONST_0:
424: case JvmOpCodes.LCONST_1:
425: case JvmOpCodes.DCONST_0:
426: case JvmOpCodes.DCONST_1:
427: case JvmOpCodes.FCONST_0:
428: case JvmOpCodes.FCONST_1:
429: case JvmOpCodes.FCONST_2:
430: flag = true;
431: break;
432:
433: }
434:
435: return flag;
436: }
437:
438: public boolean isNextInstructionPrimitiveStoreInst(int pos,
439: StringBuffer index) {
440: throw new UnsupportedOperationException(
441: "Invalid call[isNextInstructionPrimitiveStoreInst] on LoadInstrFinder object");
442: }
443:
444: public boolean isNextInstructionReturn(int nextInstruction) {
445: throw new UnsupportedOperationException(
446: "Invalid call[isNextInstructionReturn] on LoadInstrFinder object");
447: }
448:
449: public boolean isNextInstructionStore(int nextInstruction) {
450: throw new UnsupportedOperationException(
451: "Invalid call[isNextInstructionStore] on LoadInstrFinder object");
452: }
453:
454: public boolean isPrevInstALOADInst(int pos, StringBuffer s) {
455: if (isThisInstrStart((pos - 1))) {
456:
457: switch (getMethod().getCode()[(pos - 1)]) {
458:
459: case JvmOpCodes.ALOAD_0:
460: s.append(0);
461: return true;
462:
463: case JvmOpCodes.ALOAD_1:
464: s.append(1);
465: return true;
466:
467: case JvmOpCodes.ALOAD_2:
468: s.append(2);
469: return true;
470:
471: case JvmOpCodes.ALOAD_3:
472: s.append(3);
473: return true;
474:
475: }
476:
477: }
478: if (isThisInstrStart((pos - 2))) {
479:
480: switch (getMethod().getCode()[(pos - 2)]) {
481: case JvmOpCodes.ALOAD:
482: s.append(getMethod().getCode()[(pos - 1)]);
483: return true;
484:
485: }
486: }
487: return false;
488: }
489:
490: public boolean isPrevInstIloadInst(int s, StringBuffer sb2) {
491:
492: byte[] code = getMethod().getCode();
493: boolean b = false;
494: if (isThisInstrStart((s - 1))) {
495: switch (code[(s - 1)]) {
496: case JvmOpCodes.ILOAD_0:
497: b = true;
498: sb2.append(0);
499: break;
500: case JvmOpCodes.ILOAD_1:
501: b = true;
502: sb2.append(1);
503: break;
504: case JvmOpCodes.ILOAD_2:
505: b = true;
506: sb2.append(2);
507: break;
508:
509: case JvmOpCodes.ILOAD_3:
510: b = true;
511: sb2.append(3);
512: break;
513:
514: }
515: }
516:
517: if (!b && (s - 2) >= 0 && isThisInstrStart((s - 2))) {
518:
519: switch (code[s - 2]) {
520:
521: case JvmOpCodes.ILOAD:
522: b = true;
523: sb2.append(code[s - 1]);
524: break;
525: default:
526: b = false;
527: break;
528:
529: }
530: }
531:
532: return b;
533: }
534:
535: public boolean isPrevInstPrimitiveLoad(int pos, StringBuffer sb) {
536: boolean flag = false;
537: byte[] c = getMethod().getCode();
538: if (isThisInstrStart((pos - 1))) {
539: switch (c[(pos - 1)]) {
540:
541: case JvmOpCodes.ILOAD_0:
542: flag = true;
543: break;
544: case JvmOpCodes.ILOAD_1:
545: flag = true;
546: break;
547: case JvmOpCodes.ILOAD_2:
548: flag = true;
549: break;
550: case JvmOpCodes.ILOAD_3:
551: flag = true;
552: break;
553:
554: case JvmOpCodes.LLOAD_0:
555: flag = true;
556: break;
557: case JvmOpCodes.LLOAD_1:
558: flag = true;
559: break;
560: case JvmOpCodes.LLOAD_2:
561: flag = true;
562: break;
563: case JvmOpCodes.LLOAD_3:
564: flag = true;
565: break;
566:
567: case JvmOpCodes.FLOAD_0:
568: flag = true;
569: break;
570: case JvmOpCodes.FLOAD_1:
571: flag = true;
572: break;
573: case JvmOpCodes.FLOAD_2:
574: flag = true;
575: break;
576: case JvmOpCodes.FLOAD_3:
577: flag = true;
578: break;
579:
580: case JvmOpCodes.DLOAD_0:
581: flag = true;
582: break;
583: case JvmOpCodes.DLOAD_1:
584: flag = true;
585: break;
586: case JvmOpCodes.DLOAD_2:
587: flag = true;
588: break;
589: case JvmOpCodes.DLOAD_3:
590: flag = true;
591: break;
592:
593: default:
594: flag = false;
595: break;
596:
597: }
598: }
599:
600: if (flag)
601: sb.append((pos - 1));
602: if (!flag) {
603: if (isThisInstrStart((pos - 2))) {
604: switch (c[(pos - 2)]) {
605: case JvmOpCodes.ILOAD:
606: flag = true;
607: break;
608: case JvmOpCodes.LLOAD:
609: flag = true;
610: break;
611:
612: case JvmOpCodes.FLOAD:
613: flag = true;
614: break;
615:
616: case JvmOpCodes.DLOAD:
617: flag = true;
618: break;
619: default:
620: flag = false;
621: break;
622: }
623: if (flag)
624: sb.append((pos - 2));
625: }
626:
627: }
628:
629: return flag;
630: }
631:
632: public boolean isPrevInstructionAload(int pos, StringBuffer sb) {
633: boolean present = false;
634: int pos1 = pos - 1;
635: int pos2 = pos - 2;
636: int jvmInst_1 = -1;
637: int jvmInst_2 = -1;
638: if (pos1 >= 0)
639: jvmInst_1 = getMethod().getCode()[pos1];
640: if (pos2 >= 0)
641: jvmInst_2 = getMethod().getCode()[pos2];
642: if (pos1 != -1) {
643: switch (jvmInst_1) {
644:
645: case JvmOpCodes.ALOAD_0:
646: case JvmOpCodes.ALOAD_1:
647: case JvmOpCodes.ALOAD_2:
648: case JvmOpCodes.ALOAD_3:
649: present = true;
650: break;
651: default:
652: present = false;
653: break;
654:
655: }
656: }
657: if (present)
658: sb.append(pos1);
659: if (present == false) {
660:
661: if (jvmInst_2 == JvmOpCodes.ALOAD) {
662: present = true;
663: sb.append(pos2);
664: }
665:
666: }
667:
668: return present;
669: }
670:
671: public boolean isStoreInst(int index) {
672: throw new UnsupportedOperationException(
673: "Invalid call[isStoreInst] on LoadInstrFinder object");
674: }
675:
676: public boolean isStoreInst(int index, StringBuffer varindex,
677: StringBuffer t) {
678: throw new UnsupportedOperationException(
679: "Invalid call[isStoreInst] on LoadInstrFinder object");
680: }
681:
682: public boolean isThisDUPSTOREAtEndOFTernaryIF(int pos, String type) {
683: throw new UnsupportedOperationException(
684: "Invalid call[isThisDUPSTOREAtEndOFTernaryIF] on LoadInstrFinder object");
685: }
686:
687: public boolean isThisIfALoopCondition(IFBlock IF) {
688: throw new UnsupportedOperationException(
689: "Invalid call[isThisIfALoopCondition] on LoadInstrFinder object");
690: }
691:
692: public boolean isThisInstASTOREInst(int pos, StringBuffer sb) {
693: throw new UnsupportedOperationException(
694: "Invalid call[isThisInstASTOREInst] on LoadInstrFinder object");
695: }
696:
697: public boolean isThisInstructionIStoreInst(int s, StringBuffer sb) {
698: throw new UnsupportedOperationException(
699: "Invalid call[isThisInstructionIStoreInst] on LoadInstrFinder object");
700: }
701:
702: public boolean isThisLoopEndAlso(int i, int ifstart) {
703: throw new UnsupportedOperationException(
704: "Invalid call[isThisLoopEndAlso] on LoadInstrFinder object");
705: }
706:
707: public boolean isThisLoopStart(IFBlock IF) {
708: throw new UnsupportedOperationException(
709: "Invalid call[isThisLoopStart] on LoadInstrFinder object");
710: }
711:
712: public boolean isThisTryStart(int i) {
713: throw new UnsupportedOperationException(
714: "Invalid call[isThisTryStart] on LoadInstrFinder object");
715: }
716:
717: public boolean lastIFinShortCutChain(IFBlock ifst, int i) {
718: throw new UnsupportedOperationException(
719: "Invalid call[lastIFinShortCutChain] on LoadInstrFinder object");
720: }
721:
722: public boolean isInstReturnInst(byte[] code, int pos,
723: StringBuffer sb) {
724: throw new UnsupportedOperationException();
725: }
726:
727: public boolean isNextInstructionIf(int nextInstruction) {
728: throw new UnsupportedOperationException();
729: }
730:
731: public boolean isStoreInst(int index, byte[] info) {
732: throw new UnsupportedOperationException();
733: }
734:
735: public boolean isThisInstructionIStoreInst(byte[] code, int s,
736: StringBuffer sb) {
737: throw new UnsupportedOperationException();
738: }
739: }
|