Search in sources :

Example 56 with JavaConstant

use of jdk.vm.ci.meta.JavaConstant in project graal by oracle.

the class NativeImageCodeCache method layoutConstants.

public void layoutConstants() {
    for (CompilationResult compilation : compilations.values()) {
        for (DataSection.Data data : compilation.getDataSection()) {
            if (data instanceof SubstrateDataBuilder.ObjectData) {
                JavaConstant constant = ((SubstrateDataBuilder.ObjectData) data).getConstant();
                constantReasons.put(constant, compilation.getName());
            }
        }
        dataSection.addAll(compilation.getDataSection());
    }
    dataSection.close();
}
Also used : DataSection(org.graalvm.compiler.code.DataSection) JavaConstant(jdk.vm.ci.meta.JavaConstant) CompilationResult(org.graalvm.compiler.code.CompilationResult)

Example 57 with JavaConstant

use of jdk.vm.ci.meta.JavaConstant in project graal by oracle.

the class NativeImageHeap method writeConstant.

private void writeConstant(RelocatableBuffer buffer, int index, JavaKind kind, Object value, ObjectInfo info) {
    if (value instanceof RelocatedPointer) {
        addNonDataRelocation(buffer, index, (RelocatedPointer) value);
        return;
    }
    final JavaConstant con;
    if (value instanceof WordBase) {
        con = JavaConstant.forIntegerKind(FrameAccess.getWordKind(), ((WordBase) value).rawValue());
    } else if (value == null && kind == FrameAccess.getWordKind()) {
        con = JavaConstant.forIntegerKind(FrameAccess.getWordKind(), 0);
    } else {
        assert kind == JavaKind.Object || value != null : "primitive value must not be null";
        con = SubstrateObjectConstant.forBoxedValue(kind, value);
    }
    write(buffer, index, con, info);
}
Also used : RelocatedPointer(org.graalvm.nativeimage.c.function.RelocatedPointer) WordBase(org.graalvm.word.WordBase) JavaConstant(jdk.vm.ci.meta.JavaConstant)

Example 58 with JavaConstant

use of jdk.vm.ci.meta.JavaConstant 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)

Example 59 with JavaConstant

use of jdk.vm.ci.meta.JavaConstant in project graal by oracle.

the class NativeImageHeap method addObjectToBootImageHeap.

/**
 * It has been determined that an object should be added to the model of the native image heap.
 * This is the mechanics of recursively adding the object and all its fields and array elements
 * to the model of the native image heap.
 */
private void addObjectToBootImageHeap(final Object original, final Object canonicalObj, final boolean canonicalizable, boolean immutableFromParent, final int identityHashCode, final Object reason) {
    final Optional<HostedType> optionalType = getMetaAccess().optionalLookupJavaType(canonicalObj.getClass());
    if (!optionalType.isPresent() || !optionalType.get().isInstantiated()) {
        throw UserError.abort("Image heap writing found an object whose class was not seen as instantiated during static analysis. " + "Did a static field or an object referenced from a static field changed during native image generation? " + "For example, a lazily initialized cache could have been initialized during image generation, " + "in which case you need to force eager initialization of the cache before static analysis or reset the cache using a field value recomputation.\n" + "  object: " + original + "  of class: " + original.getClass().getTypeName() + "\n" + "  reachable through:\n" + fillReasonStack(new StringBuilder(), reason));
    }
    final HostedType type = optionalType.get();
    if (type.isInstanceClass()) {
        final HostedInstanceClass clazz = (HostedInstanceClass) type;
        final JavaConstant con = SubstrateObjectConstant.forObject(canonicalObj);
        final Object hybridArray;
        final long size;
        if (HybridLayout.isHybrid(clazz)) {
            HybridLayout<?> hybridLayout = hybridLayouts.get(clazz);
            if (hybridLayout == null) {
                hybridLayout = new HybridLayout<>(clazz, layout);
                hybridLayouts.put(clazz, hybridLayout);
            }
            /*
                 * The hybrid array and bit set are written within the hybrid object. So they may
                 * not be written as separate objects. We use the blacklist to check that.
                 */
            HostedField bitsetField = hybridLayout.getBitsetField();
            if (bitsetField != null) {
                BitSet bitSet = (BitSet) SubstrateObjectConstant.asObject(bitsetField.readStorageValue(con));
                if (bitSet != null) {
                    blacklist.put(bitSet, Boolean.TRUE);
                }
            }
            hybridArray = SubstrateObjectConstant.asObject(hybridLayout.getArrayField().readStorageValue(con));
            blacklist.put(hybridArray, Boolean.TRUE);
            size = hybridLayout.getTotalSize(Array.getLength(hybridArray));
        } else {
            hybridArray = null;
            size = LayoutEncoding.getInstanceSize(clazz.getHub().getLayoutEncoding()).rawValue();
        }
        // All canonicalizable objects are immutable,
        // as are instances of known immutable classes.
        final ObjectInfo info = addToHeapPartition(original, canonicalObj, clazz, size, identityHashCode, canonicalizable, immutableFromParent, reason);
        recursiveAddObject(clazz.getHub(), canonicalizable, false, info);
        // Recursively add all the fields of the object.
        // Even if the parent is not canonicalizable, the fields may be canonicalizable.
        final boolean fieldsAreImmutable = canonicalObj instanceof String;
        for (HostedField field : clazz.getInstanceFields(true)) {
            if (field.getType().getStorageKind() == JavaKind.Object && !HybridLayout.isHybridField(field) && field.isAccessed()) {
                assert field.getLocation() >= 0;
                recursiveAddObject(SubstrateObjectConstant.asObject(field.readStorageValue(con)), canonicalizable, fieldsAreImmutable, info);
            }
        }
        if (hybridArray instanceof Object[]) {
            addArrayElements((Object[]) hybridArray, canonicalizable, info);
        }
    } else if (type.isArray()) {
        HostedArrayClass clazz = (HostedArrayClass) type;
        int length = Array.getLength(canonicalObj);
        JavaKind kind = type.getComponentType().getJavaKind();
        final long size = layout.getArraySize(kind, length);
        final ObjectInfo info = addToHeapPartition(original, canonicalObj, clazz, size, identityHashCode, canonicalizable, immutableFromParent, reason);
        recursiveAddObject(clazz.getHub(), canonicalizable, false, info);
        if (kind == JavaKind.Object) {
            addArrayElements((Object[]) canonicalObj, canonicalizable, info);
        }
    } else {
        throw shouldNotReachHere();
    }
}
Also used : HostedField(com.oracle.svm.hosted.meta.HostedField) BitSet(java.util.BitSet) JavaConstant(jdk.vm.ci.meta.JavaConstant) HostedInstanceClass(com.oracle.svm.hosted.meta.HostedInstanceClass) HostedType(com.oracle.svm.hosted.meta.HostedType) HostedArrayClass(com.oracle.svm.hosted.meta.HostedArrayClass) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 60 with JavaConstant

use of jdk.vm.ci.meta.JavaConstant in project graal by oracle.

the class ConstantFoldLoadFieldPlugin method tryConstantFold.

private static boolean tryConstantFold(GraphBuilderContext b, ResolvedJavaField field, JavaConstant receiver) {
    ConstantNode result = ConstantFoldUtil.tryConstantFold(b.getConstantFieldProvider(), b.getConstantReflection(), b.getMetaAccess(), field, receiver, b.getOptions());
    if (result != null) {
        JavaConstant value = result.asJavaConstant();
        if (b.getMetaAccess() instanceof AnalysisMetaAccess && value.getJavaKind() == JavaKind.Object && value.isNonNull()) {
            SubstrateObjectConstant sValue = (SubstrateObjectConstant) value;
            SubstrateObjectConstant sReceiver = (SubstrateObjectConstant) receiver;
            Object root;
            if (receiver == null) {
                /* Found a root, map the constant value to the root field. */
                root = field;
            } else {
                /* Map the constant value to the root field of it's receiver. */
                root = sReceiver.getRoot();
                assert root != null : receiver.toValueString() + " : " + field + " : " + b.getGraph();
            }
            sValue.setRoot(root);
        }
        result = b.getGraph().unique(result);
        b.push(field.getJavaKind(), result);
        return true;
    }
    return false;
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) SubstrateObjectConstant(com.oracle.svm.core.meta.SubstrateObjectConstant) JavaConstant(jdk.vm.ci.meta.JavaConstant) AnalysisMetaAccess(com.oracle.graal.pointsto.meta.AnalysisMetaAccess)

Aggregations

JavaConstant (jdk.vm.ci.meta.JavaConstant)122 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)33 ValueNode (org.graalvm.compiler.nodes.ValueNode)24 Test (org.junit.Test)19 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)17 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)15 Stamp (org.graalvm.compiler.core.common.type.Stamp)11 LIRValueUtil.asJavaConstant (org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant)11 LIRValueUtil.isJavaConstant (org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant)11 JavaKind (jdk.vm.ci.meta.JavaKind)10 Constant (jdk.vm.ci.meta.Constant)9 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)8 AllocatableValue (jdk.vm.ci.meta.AllocatableValue)7 Condition (org.graalvm.compiler.core.common.calc.Condition)7 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)6 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)6 LogicNode (org.graalvm.compiler.nodes.LogicNode)6 FixedNode (org.graalvm.compiler.nodes.FixedNode)5 ParameterNode (org.graalvm.compiler.nodes.ParameterNode)5 ReturnNode (org.graalvm.compiler.nodes.ReturnNode)5