001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Mikhail Y. Fursov
019: */package org.apache.harmony.drlvm;
020:
021: import org.vmmagic.unboxed.*;
022: import org.vmmagic.pragma.*;
023:
024: /**
025: Core class for DRLVM's vmmagic based helpers.
026: Resolved and initilized during VM startup
027:
028: Note: All classes with vmmagic based helpers registred in VM are also resolved and initialized at VM startup
029: Note: If you need to initialize another DRLVM's specific utility class related to vmmagic infrastructure
030: refer to it from static section of this class: and it will also be automatically initialized
031: */
032: public class VMHelper {
033:
034: //public constants
035:
036: public static final int POINTER_TYPE_SIZE = getPointerTypeSize();
037:
038: public static final boolean COMPRESSED_REFS_MODE = isCompressedRefsMode();
039:
040: public static final boolean COMPRESSED_VTABLE_MODE = isCompressedVTableMode();
041:
042: public static final long COMPRESSED_VTABLE_BASE_OFFSET = getCompressedModeVTableBaseOffset();
043:
044: public static final long COMPRESSED_REFS_OBJ_BASE_OFFSET = getCompressedModeObjectBaseOffset();
045:
046: public static final int OBJ_VTABLE_OFFSET = getObjectVtableOffset();
047:
048: public static final int VTABLE_GCPRIVATE_OFFSET = 0;
049:
050: public static final int VTABLE_CLASS_OFFSET = getVtableClassOffset();
051:
052: public static final int OBJ_INFO_OFFSET = 4;
053:
054: public static final int CLASS_JLC_HANDLE_OFFSET = getClassJLCHanldeOffset();
055:
056: // preload @Inline vmmagic class
057: static final Class pragmaInline = org.vmmagic.pragma.Inline.class;
058: static final Class threadHelper = org.apache.harmony.drlvm.thread.ThreadHelper.class;
059: static final Class vmFastPathes = org.apache.harmony.drlvm.VMHelperFastPath.class;
060:
061: //Slow path versions of helpers
062:
063: //TODO: allocation handle is int only on 32bit OS or (64bit OS && compressed mode)
064: public static Address newResolvedUsingAllocHandleAndSize(
065: int objSize, int allocationHandle) {
066: fail();
067: return null;
068: }
069:
070: //TODO: allocation handle is int only on 32bit OS or (64bit OS && compressed mode)
071: public static Address newVectorUsingAllocHandle(int arrayLen,
072: int allocationHandle) {
073: fail();
074: return null;
075: }
076:
077: public static void monitorEnter(Object obj) {
078: fail();
079: }
080:
081: public static void monitorExit(Object obj) {
082: fail();
083: }
084:
085: public static void memset0(Address addr, int size) {
086: fail();
087: }
088:
089: public static void prefetch(Address addr, int distance, int stride) {
090: fail();
091: }
092:
093: public static void writeBarrier(Address objBase, Address objSlot,
094: Address source) {
095: fail();
096: }
097:
098: public static Address getInterfaceVTable(Object obj,
099: Address intfTypePtr) {
100: fail();
101: return null;
102: }
103:
104: public static void checkCast(Object obj, Address castTypePtr) {
105: fail();
106: }
107:
108: public static boolean instanceOf(Object obj, Address castTypePtr) {
109: fail();
110: return false;
111: }
112:
113: //utility magics supported by JIT
114:
115: public static boolean isVMMagicPackageSupported() {
116: return false;
117: }
118:
119: public static Address getTlsBaseAddress() {
120: fail();
121: return null;
122: }
123:
124: public static boolean isArray(Address classHandle) {
125: VMHelper.fail();
126: return false;
127: }
128:
129: public static boolean isInterface(Address classHandle) {
130: VMHelper.fail();
131: return false;
132: }
133:
134: public static boolean isFinal(Address classHandle) {
135: VMHelper.fail();
136: return false;
137: }
138:
139: public static Address getArrayClass(Address elemClassHandle) {
140: VMHelper.fail();
141: return null;
142: }
143:
144: public static int getAllocationHandle(Address classHandle) {
145: VMHelper.fail();
146: return 0;
147: }
148:
149: public static int getTypeSize(Address classHandle) {
150: VMHelper.fail();
151: return 0;
152: }
153:
154: public static int getArrayElemSize(Address arrayClassHandle) {
155: VMHelper.fail();
156: return 0;
157: }
158:
159: public static int getFastTypeCheckDepth(Address classHandle) {
160: VMHelper.fail();
161: return 0;
162: }
163:
164: protected static void fail() {
165: throw new RuntimeException("Not supported!");
166: }
167:
168: //helpers implemented with magics
169:
170: @Inline
171: public static Address getVTable(Object obj) {
172: Address objAddr = ObjectReference.fromObject(obj).toAddress();
173: if (COMPRESSED_VTABLE_MODE) {
174: int compressedAddr = objAddr.loadInt(Offset
175: .fromIntZeroExtend(OBJ_VTABLE_OFFSET));
176: return Address.fromLong(compressedAddr
177: + COMPRESSED_VTABLE_BASE_OFFSET);
178: }
179: return objAddr.loadAddress(Offset
180: .fromIntZeroExtend(OBJ_VTABLE_OFFSET));
181: }
182:
183: @Inline
184: public static Address getNativeClass(Object obj) {
185: Address nativeClass = getVTable(obj).loadAddress(
186: Offset.fromIntZeroExtend(VTABLE_CLASS_OFFSET));
187: return nativeClass;
188: }
189:
190: @Inline
191: public static Address getManagedClass(Object obj) {
192: //accessing to ManagedObject** m_class_handle field + 1 dereference in Class.h
193: Address moAddr = getNativeClass(obj).loadAddress(
194: Offset.fromIntZeroExtend(CLASS_JLC_HANDLE_OFFSET))
195: .loadAddress();
196: return moAddr;
197: }
198:
199: // private area
200:
201: private VMHelper() {
202: }
203:
204: /** @return vtable offset in managed object structure */
205: private static native int getObjectVtableOffset();
206:
207: /** @return pointer-type size. 4 or 8 */
208: private static native int getPointerTypeSize();
209:
210: /** @return true if VM is run in compressed reference mode */
211: private static native boolean isCompressedRefsMode();
212:
213: /** @return true if VM is run in compressed vtables mode */
214: private static native boolean isCompressedVTableMode();
215:
216: /** @return vtable base offset if is in compressed-refs mode or -1*/
217: private static native long getCompressedModeVTableBaseOffset();
218:
219: /** @return object base offset if is in compressed-refs mode or -1*/
220: private static native long getCompressedModeObjectBaseOffset();
221:
222: /** @return native Class* struct offset in vtable*/
223: private static native int getVtableClassOffset();
224:
225: /** @return managed object field offset in vtable*/
226: private static native int getClassJLCHanldeOffset();
227:
228: }
|