001/*
002 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot;
024
025import static com.oracle.graal.hotspot.HotSpotHostBackend.*;
026import jdk.internal.jvmci.code.*;
027import jdk.internal.jvmci.code.CompilationResult.*;
028import jdk.internal.jvmci.hotspot.*;
029
030/**
031 * {@link HotSpotCompiledCode} destined for installation as a RuntimeStub.
032 */
033public final class HotSpotCompiledRuntimeStub extends HotSpotCompiledCode {
034
035    public HotSpotCompiledRuntimeStub(CompilationResult compResult) {
036        super(compResult);
037        assert checkStubInvariants(compResult);
038    }
039
040    /**
041     * Checks the conditions a compilation must satisfy to be installed as a RuntimeStub.
042     */
043    private boolean checkStubInvariants(CompilationResult compResult) {
044        assert compResult.getExceptionHandlers().isEmpty() : this;
045
046        // Stubs cannot be recompiled so they cannot be compiled with
047        // assumptions and there is no point in recording evol_method dependencies
048        assert compResult.getAssumptions() == null : "stubs should not use assumptions: " + this;
049        assert compResult.getMethods() == null : "stubs should not record evol_method dependencies: " + this;
050
051        for (DataPatch data : compResult.getDataPatches()) {
052            if (data.reference instanceof ConstantReference) {
053                ConstantReference ref = (ConstantReference) data.reference;
054                if (ref.getConstant() instanceof HotSpotMetaspaceConstant) {
055                    HotSpotMetaspaceConstant c = (HotSpotMetaspaceConstant) ref.getConstant();
056                    if (c.asResolvedJavaType() != null && c.asResolvedJavaType().getName().equals("[I")) {
057                        // special handling for NewArrayStub
058                        // embedding the type '[I' is safe, since it is never unloaded
059                        continue;
060                    }
061                }
062            }
063
064            assert !(data.reference instanceof ConstantReference) : this + " cannot have embedded object or metadata constant: " + data.reference;
065        }
066        for (Infopoint infopoint : compResult.getInfopoints()) {
067            assert infopoint instanceof Call : this + " cannot have non-call infopoint: " + infopoint;
068            Call call = (Call) infopoint;
069            assert call.target instanceof HotSpotForeignCallLinkage : this + " cannot have non runtime call: " + call.target;
070            HotSpotForeignCallLinkage linkage = (HotSpotForeignCallLinkage) call.target;
071            assert !linkage.isCompiledStub() || linkage.getDescriptor().equals(UNCOMMON_TRAP_HANDLER) : this + " cannot call compiled stub " + linkage;
072        }
073        return true;
074    }
075
076    @Override
077    public String toString() {
078        return name;
079    }
080}