Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / ANEWARRAY.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.jvm.bytecode;
19
20 import gov.nasa.jpf.vm.ClassInfo;
21 import gov.nasa.jpf.vm.ClassLoaderInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.Heap;
24 import gov.nasa.jpf.vm.Instruction;
25 import gov.nasa.jpf.vm.LoadOnJPFRequired;
26 import gov.nasa.jpf.vm.StackFrame;
27 import gov.nasa.jpf.vm.ThreadInfo;
28 import gov.nasa.jpf.vm.Types;
29
30
31 /**
32  * Create new array of reference
33  * ..., count => ..., arrayref
34  */
35 public class ANEWARRAY extends NewArrayInstruction {
36
37   public ANEWARRAY (String typeDescriptor){
38     type = Types.getTypeSignature(typeDescriptor, true);
39   }
40
41   @Override
42   public Instruction execute (ThreadInfo ti) {
43     // resolve the component class first
44     String compType = Types.getTypeName(type);
45     if(Types.isReferenceSignature(type)) {
46       try {
47         ti.resolveReferencedClass(compType);
48       } catch(LoadOnJPFRequired lre) {
49         return ti.getPC();
50       }
51     }
52
53     // there is no clinit for array classes, but we still have  to create a class object
54     // since its a builtin class, we also don't have to bother with NoClassDefFoundErrors
55     String clsName = "[" + type;
56     ClassInfo ci = ClassLoaderInfo.getCurrentResolvedClassInfo(clsName);
57
58     if (!ci.isRegistered()) {
59       ci.registerClass(ti);
60       ci.setInitialized();
61     }
62
63     StackFrame frame = ti.getModifiableTopFrame();
64
65     arrayLength = frame.pop();
66     if (arrayLength < 0){
67       return ti.createAndThrowException("java.lang.NegativeArraySizeException");
68     }
69
70     Heap heap = ti.getHeap();
71     if (heap.isOutOfMemory()) { // simulate OutOfMemoryError
72       return ti.createAndThrowException("java.lang.OutOfMemoryError",
73                                         "trying to allocate new " +
74                                           Types.getTypeName(type) +
75                                         "[" + arrayLength + "]");
76     }
77
78     ElementInfo eiArray = heap.newArray(type, arrayLength, ti);
79     int aRef = eiArray.getObjectRef();
80     
81     // pushes the object reference on the top stack frame
82     frame.push(aRef, true);
83     
84     return getNext(ti);
85   }
86
87   @Override
88   public int getLength () {
89     return 3; // opcode, index1, index2
90   }
91   
92   @Override
93   public int getByteCode () {
94     return 0xBD;
95   }
96   
97   @Override
98   public void accept(JVMInstructionVisitor insVisitor) {
99           insVisitor.visit(this);
100   }
101 }