1 package jrre.instructionset.push;
2
3 import jrre.*;
4 import jrre.types.*;
5 import jrre.classloader.classfile.pool_entries.CPInfo;
6 import jrre.classloader.classfile.pool_entries.CPInteger;
7 import jrre.classloader.classfile.pool_entries.CPFloat;
8 import jrre.classloader.classfile.pool_entries.CPString;
9 import jrre.classloader.classfile.pool_entries.CPUTF8;
10
11 public class Ldc extends jrre.instructionset.Instruction {
12
13 private int operandOne;
14
15 public Ldc(int operandOne){
16
17 this.operandOne = operandOne;
18
19 name = "ldc "+operandOne;
20 description = "pushes a constant.";
21 length = 1;
22 }
23
24 /***
25 * Executes the <strong><code>ldc</code></strong> instruction.
26 *
27 */
28 public void execute(){
29
30
31 jrre.api.java.lang.Class thisClass = Stack.getClassContainingMethod();
32
33 CPInfo constant = thisClass.getSymbol(operandOne);
34
35 if(constant instanceof CPString){
36 if(jrre.JRRE.getVerbose())
37 System.out.println("Executing ldc String: "+operandOne);
38
39 String constantString = ((CPUTF8)thisClass.getSymbol(((CPString)constant).getUTF8Index())).getValue();
40
41 ArrayInstance charArray = new ArrayInstance(5, constantString.length());
42 ReferenceType charArrayRef = new ReferenceType(charArray);
43
44 for(int i=0;i < constantString.length();i++)
45 charArray.setElement(i, new CharType(constantString.charAt(i)));
46
47 jrre.api.java.lang.Class stringClass = jrre.MethodArea.getClass("java/lang/String");
48 if(stringClass == null)
49 return;
50
51 jrre.ObjectInstance stringObject = stringClass.instanceOf();
52
53 // hash
54 stringObject.setInstanceValue(2, new PrimitiveType(0));
55
56 // char array
57 stringObject.setInstanceValue(3, charArrayRef);
58
59 // length
60 stringObject.setInstanceValue(4, new PrimitiveType(constantString.length()));
61 // offset
62 stringObject.setInstanceValue(5, new PrimitiveType(0));
63
64 Stack.pushOperand(new ReferenceType(stringObject));
65
66 }
67 else if(constant instanceof CPInteger){
68
69 if(jrre.JRRE.getVerbose())
70 System.out.println("Executing ldc Integer: "+operandOne);
71
72 PrimitiveType newInteger = new PrimitiveType(((CPInteger)constant).getValue());
73 Stack.pushOperand(newInteger);
74 }
75 else if(constant instanceof CPFloat){
76
77 //System.out.println(constant +"In ldc float\n\n\n");
78
79 FloatType floatType = new FloatType(((CPFloat)constant).getValue());
80 Stack.pushOperand(floatType);
81 }
82 else
83 System.out.println("Need to implement "+constant+" in Ldc");
84
85 }
86
87 public String toString(){
88 StringBuffer toReturn = new StringBuffer();
89 toReturn.append("ldc ");
90 toReturn.append(operandOne);
91 return toReturn.toString();
92 }
93 }
This page was automatically generated by Maven