1 package serp.bytecode; 2 3 import serp.bytecode.visitor.*; 4 5 /*** 6 * Loads a value from an array onto the stack. 7 * 8 * @author Abe White 9 */ 10 public class ArrayLoadInstruction extends ArrayInstruction { 11 private static final Class[][] _mappings = new Class[][] { 12 { boolean.class, int.class }, 13 { void.class, int.class }, 14 }; 15 16 ArrayLoadInstruction(Code owner) { 17 super(owner); 18 } 19 20 ArrayLoadInstruction(Code owner, int opcode) { 21 super(owner, opcode); 22 } 23 24 public int getLogicalStackChange() { 25 switch (getOpcode()) { 26 case Constants.NOP: 27 return 0; 28 default: 29 return -1; 30 } 31 } 32 33 public int getStackChange() { 34 switch (getOpcode()) { 35 case Constants.DALOAD: 36 case Constants.LALOAD: 37 case Constants.NOP: 38 return 0; 39 default: 40 return -1; 41 } 42 } 43 44 public String getTypeName() { 45 switch (getOpcode()) { 46 case Constants.IALOAD: 47 return int.class.getName(); 48 case Constants.LALOAD: 49 return long.class.getName(); 50 case Constants.FALOAD: 51 return float.class.getName(); 52 case Constants.DALOAD: 53 return double.class.getName(); 54 case Constants.AALOAD: 55 return Object.class.getName(); 56 case Constants.BALOAD: 57 return byte.class.getName(); 58 case Constants.CALOAD: 59 return char.class.getName(); 60 case Constants.SALOAD: 61 return short.class.getName(); 62 default: 63 return null; 64 } 65 } 66 67 public TypedInstruction setType(String type) { 68 type = mapType(type, _mappings, true); 69 if (type == null) 70 return (TypedInstruction) setOpcode(Constants.NOP); 71 72 switch (type.charAt(0)) { 73 case 'i': 74 return (TypedInstruction) setOpcode(Constants.IALOAD); 75 case 'l': 76 return (TypedInstruction) setOpcode(Constants.LALOAD); 77 case 'f': 78 return (TypedInstruction) setOpcode(Constants.FALOAD); 79 case 'd': 80 return (TypedInstruction) setOpcode(Constants.DALOAD); 81 case 'b': 82 return (TypedInstruction) setOpcode(Constants.BALOAD); 83 case 'c': 84 return (TypedInstruction) setOpcode(Constants.CALOAD); 85 case 's': 86 return (TypedInstruction) setOpcode(Constants.SALOAD); 87 default: 88 return (TypedInstruction) setOpcode(Constants.AALOAD); 89 } 90 } 91 92 public void acceptVisit(BCVisitor visit) { 93 visit.enterArrayLoadInstruction(this); 94 visit.exitArrayLoadInstruction(this); 95 } 96 }