Search in sources :

Example 1 with HostedClass

use of com.oracle.svm.hosted.meta.HostedClass in project graal by oracle.

the class NativeImageHeap method writeObject.

private void writeObject(ObjectInfo info, final RelocatableBuffer roBuffer, final RelocatableBuffer rwBuffer) {
    /*
         * Write a reference from the object to its hub. This lives at layout.getHubOffset() from
         * the object base.
         */
    final RelocatableBuffer buffer = bufferForPartition(info, roBuffer, rwBuffer);
    final int indexInSection = info.getIntIndexInSection(layout.getHubOffset());
    assert layout.isReferenceAligned(info.getOffsetInPartition());
    assert layout.isReferenceAligned(indexInSection);
    final HostedClass clazz = info.getClazz();
    final DynamicHub hub = clazz.getHub();
    final long objectHeaderBits = Heap.getHeap().getObjectHeader().setBootImageOnLong(0L);
    writeDynamicHub(buffer, indexInSection, hub, objectHeaderBits);
    if (clazz.isInstanceClass()) {
        JavaConstant con = SubstrateObjectConstant.forObject(info.getObject());
        HybridLayout<?> hybridLayout = hybridLayouts.get(clazz);
        HostedField hybridArrayField = null;
        HostedField hybridBitsetField = null;
        int maxBitIndex = -1;
        Object hybridArray = null;
        if (hybridLayout != null) {
            hybridArrayField = hybridLayout.getArrayField();
            hybridArray = SubstrateObjectConstant.asObject(hybridArrayField.readStorageValue(con));
            hybridBitsetField = hybridLayout.getBitsetField();
            if (hybridBitsetField != null) {
                BitSet bitSet = (BitSet) SubstrateObjectConstant.asObject(hybridBitsetField.readStorageValue(con));
                if (bitSet != null) {
                    /*
                         * Write the bits of the hybrid bit field. The bits are located between the
                         * array length and the instance fields.
                         */
                    int bitsPerByte = Byte.SIZE;
                    for (int bit = bitSet.nextSetBit(0); bit >= 0; bit = bitSet.nextSetBit(bit + 1)) {
                        final int index = info.getIntIndexInSection(hybridLayout.getBitFieldOffset()) + bit / bitsPerByte;
                        if (index > maxBitIndex) {
                            maxBitIndex = index;
                        }
                        int mask = 1 << (bit % bitsPerByte);
                        assert mask < (1 << bitsPerByte);
                        buffer.putByte(index, (byte) (buffer.getByte(index) | mask));
                    }
                }
            }
        }
        /*
             * Write the regular instance fields.
             */
        for (HostedField field : clazz.getInstanceFields(true)) {
            if (!field.equals(hybridArrayField) && !field.equals(hybridBitsetField) && field.isAccessed()) {
                assert field.getLocation() >= 0;
                assert info.getIntIndexInSection(field.getLocation()) > maxBitIndex;
                writeField(buffer, info, field, con, info);
            }
        }
        if (hub.getHashCodeOffset() != 0) {
            buffer.putInt(info.getIntIndexInSection(hub.getHashCodeOffset()), info.getIdentityHashCode());
        }
        if (hybridArray != null) {
            /*
                 * Write the hybrid array length and the array elements.
                 */
            int length = Array.getLength(hybridArray);
            buffer.putInt(info.getIntIndexInSection(layout.getArrayLengthOffset()), length);
            for (int i = 0; i < length; i++) {
                final int elementIndex = info.getIntIndexInSection(hybridLayout.getArrayElementOffset(i));
                final JavaKind elementKind = hybridLayout.getArrayElementKind();
                final Object array = Array.get(hybridArray, i);
                writeConstant(buffer, elementIndex, elementKind, array, info);
            }
        }
    } else if (clazz.isArray()) {
        JavaKind kind = clazz.getComponentType().getJavaKind();
        Object array = info.getObject();
        int length = Array.getLength(array);
        buffer.putInt(info.getIntIndexInSection(layout.getArrayLengthOffset()), length);
        buffer.putInt(info.getIntIndexInSection(layout.getArrayHashCodeOffset()), info.getIdentityHashCode());
        if (array instanceof Object[]) {
            Object[] oarray = (Object[]) array;
            assert oarray.length == length;
            for (int i = 0; i < length; i++) {
                final int elementIndex = info.getIntIndexInSection(layout.getArrayElementOffset(kind, i));
                final Object element = aUniverse.replaceObject(oarray[i]);
                writeConstant(buffer, elementIndex, kind, element, info);
            }
        } else {
            for (int i = 0; i < length; i++) {
                final int elementIndex = info.getIntIndexInSection(layout.getArrayElementOffset(kind, i));
                final Object element = Array.get(array, i);
                writeConstant(buffer, elementIndex, kind, element, info);
            }
        }
    } else {
        throw shouldNotReachHere();
    }
}
Also used : HostedField(com.oracle.svm.hosted.meta.HostedField) DynamicHub(com.oracle.svm.core.hub.DynamicHub) BitSet(java.util.BitSet) JavaConstant(jdk.vm.ci.meta.JavaConstant) HostedClass(com.oracle.svm.hosted.meta.HostedClass) JavaKind(jdk.vm.ci.meta.JavaKind)

Aggregations

DynamicHub (com.oracle.svm.core.hub.DynamicHub)1 HostedClass (com.oracle.svm.hosted.meta.HostedClass)1 HostedField (com.oracle.svm.hosted.meta.HostedField)1 BitSet (java.util.BitSet)1 JavaConstant (jdk.vm.ci.meta.JavaConstant)1 JavaKind (jdk.vm.ci.meta.JavaKind)1