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