Mercurial > hg > truffle
view truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java @ 22490:fa86f9f3848d
change FrameDescriptor#copy to also copy info
author | Andreas Woess <andreas.woess@oracle.com> |
---|---|
date | Thu, 10 Dec 2015 18:51:41 +0100 |
parents | dc2bfc816011 |
children | a73f1d7a5a3e |
line wrap: on
line source
/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.api.frame; import com.oracle.truffle.api.Assumption; import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.Truffle; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Set; /** * Descriptor of the slots of frame objects. Multiple frame instances are associated with one such * descriptor. */ public final class FrameDescriptor implements Cloneable { private final Object defaultValue; private final ArrayList<FrameSlot> slots; private final HashMap<Object, FrameSlot> identifierToSlotMap; private Assumption version; private HashMap<Object, Assumption> identifierToNotInFrameAssumptionMap; /** * Constructs empty descriptor. The {@link #getDefaultValue()} is <code>null</code>. */ public FrameDescriptor() { this(null); } /** * Constructs new descriptor with specified {@link #getDefaultValue()}. * * @param defaultValue to be returned from {@link #getDefaultValue()} */ public FrameDescriptor(Object defaultValue) { CompilerAsserts.neverPartOfCompilation(); this.defaultValue = defaultValue; slots = new ArrayList<>(); identifierToSlotMap = new HashMap<>(); version = createVersion(); } /** * Use {@link #FrameDescriptor()}. * * @return new instance of the descriptor * @deprecated */ @Deprecated public static FrameDescriptor create() { return new FrameDescriptor(); } /** * Use {@link #FrameDescriptor(java.lang.Object) }. * * @return new instance of the descriptor * @deprecated */ @Deprecated public static FrameDescriptor create(Object defaultValue) { return new FrameDescriptor(defaultValue); } /** * Adds frame slot. Delegates to * {@link #addFrameSlot(java.lang.Object, java.lang.Object, com.oracle.truffle.api.frame.FrameSlotKind) * addFrameSlot}(identifier, <code>null</code>, {@link FrameSlotKind#Illegal}). This is slow * operation that switches to interpreter mode. * * @param identifier key for the slot * @return the newly created slot */ public FrameSlot addFrameSlot(Object identifier) { return addFrameSlot(identifier, null, FrameSlotKind.Illegal); } /** * Adds frame slot. Delegates to * {@link #addFrameSlot(java.lang.Object, java.lang.Object, com.oracle.truffle.api.frame.FrameSlotKind) * addFrameSlot}(identifier, <code>null</code>, <code>kind</code>). This is slow operation that * switches to interpreter mode. * * @param identifier key for the slot * @param kind the kind of the new slot * @return the newly created slot */ public FrameSlot addFrameSlot(Object identifier, FrameSlotKind kind) { return addFrameSlot(identifier, null, kind); } /** * Adds new frame slot to {@link #getSlots()} list. This is slow operation that switches to * interpreter mode. * * @param identifier key for the slot - it needs proper {@link #equals(java.lang.Object)} and * {@link Object#hashCode()} implementations * @param info additional {@link FrameSlot#getInfo() information for the slot} * @param kind the kind of the new slot * @return the newly created slot */ public FrameSlot addFrameSlot(Object identifier, Object info, FrameSlotKind kind) { CompilerAsserts.neverPartOfCompilation("interpreter-only. includes hashmap operations."); assert !identifierToSlotMap.containsKey(identifier); FrameSlot slot = new FrameSlot(this, identifier, info, slots.size(), kind); slots.add(slot); identifierToSlotMap.put(identifier, slot); updateVersion(); invalidateNotInFrameAssumption(identifier); return slot; } /** * Finds an existing slot. This is slow operation. * * @param identifier the key of the slot to search for * @return the slot or <code>null</code> */ public FrameSlot findFrameSlot(Object identifier) { CompilerAsserts.neverPartOfCompilation("interpreter-only. includes hashmap operations."); return identifierToSlotMap.get(identifier); } /** * Finds an existing slot or creates new one. This is slow operation. * * @param identifier the key of the slot to search for * @return the slot */ public FrameSlot findOrAddFrameSlot(Object identifier) { FrameSlot result = findFrameSlot(identifier); if (result != null) { return result; } return addFrameSlot(identifier); } /** * Finds an existing slot or creates new one. This is slow operation. * * @param identifier the key of the slot to search for * @param kind the kind for the newly created slot * @return the found or newly created slot */ public FrameSlot findOrAddFrameSlot(Object identifier, FrameSlotKind kind) { FrameSlot result = findFrameSlot(identifier); if (result != null) { return result; } return addFrameSlot(identifier, kind); } /** * Finds an existing slot or creates new one. This is slow operation. * * @param identifier the key of the slot to search for * @param info info for the newly created slot * @param kind the kind for the newly created slot * @return the found or newly created slot */ public FrameSlot findOrAddFrameSlot(Object identifier, Object info, FrameSlotKind kind) { FrameSlot result = findFrameSlot(identifier); if (result != null) { return result; } return addFrameSlot(identifier, info, kind); } /** * Removes a slot. If the identifier is found, its slot is removed from this descriptor. This is * slow operation. * * @param identifier identifies the slot to remove */ public void removeFrameSlot(Object identifier) { CompilerAsserts.neverPartOfCompilation("interpreter-only. includes hashmap operations."); assert identifierToSlotMap.containsKey(identifier); slots.remove(identifierToSlotMap.get(identifier)); identifierToSlotMap.remove(identifier); updateVersion(); getNotInFrameAssumption(identifier); } /** * Returns number of slots in the descriptor. * * @return the same value as {@link #getSlots()}.{@link List#size()} would return */ public int getSize() { return slots.size(); } /** * Current set of slots in the descriptor. * * @return unmodifiable list of {@link FrameSlot} */ public List<? extends FrameSlot> getSlots() { return Collections.unmodifiableList(slots); } /** * Retrieve the list of all the identifiers associated with this frame descriptor. * * @return the list of all the identifiers in this frame descriptor */ public Set<Object> getIdentifiers() { return Collections.unmodifiableSet(identifierToSlotMap.keySet()); } /** * Deeper copy of the descriptor. Copies all slots in the descriptor, but only their * {@linkplain FrameSlot#getIdentifier() identifier} and {@linkplain FrameSlot#getInfo() info} * but not their {@linkplain FrameSlot#getKind() kind}! * * @return new instance of a descriptor with copies of values from this one */ public FrameDescriptor copy() { FrameDescriptor clonedFrameDescriptor = new FrameDescriptor(this.defaultValue); for (int i = 0; i < slots.size(); i++) { FrameSlot slot = slots.get(i); clonedFrameDescriptor.addFrameSlot(slot.getIdentifier(), slot.getInfo(), FrameSlotKind.Illegal); } return clonedFrameDescriptor; } /** * Shallow copy of the descriptor. Re-uses the existing slots in new descriptor. As a result, if * you {@link FrameSlot#setKind(com.oracle.truffle.api.frame.FrameSlotKind) change kind} of one * of the slots it is changed in the original as well as in the shallow copy. * * @return new instance of a descriptor with copies of values from this one */ public FrameDescriptor shallowCopy() { FrameDescriptor clonedFrameDescriptor = new FrameDescriptor(this.defaultValue); clonedFrameDescriptor.slots.addAll(slots); clonedFrameDescriptor.identifierToSlotMap.putAll(identifierToSlotMap); return clonedFrameDescriptor; } void updateVersion() { version.invalidate(); version = createVersion(); } public Assumption getVersion() { return version; } private static Assumption createVersion() { return Truffle.getRuntime().createAssumption("frame version"); } /** * Default value for the created slots. * * @return value provided to {@link #FrameDescriptor(java.lang.Object)} */ public Object getDefaultValue() { return defaultValue; } public Assumption getNotInFrameAssumption(Object identifier) { if (identifierToSlotMap.containsKey(identifier)) { throw new IllegalArgumentException("Cannot get not-in-frame assumption for existing frame slot!"); } if (identifierToNotInFrameAssumptionMap == null) { identifierToNotInFrameAssumptionMap = new HashMap<>(); } else { Assumption assumption = identifierToNotInFrameAssumptionMap.get(identifier); if (assumption != null) { return assumption; } } Assumption assumption = Truffle.getRuntime().createAssumption("not in frame: " + identifier); identifierToNotInFrameAssumptionMap.put(identifier, assumption); return assumption; } private void invalidateNotInFrameAssumption(Object identifier) { if (identifierToNotInFrameAssumptionMap != null) { Assumption assumption = identifierToNotInFrameAssumptionMap.get(identifier); if (assumption != null) { assumption.invalidate(); identifierToNotInFrameAssumptionMap.remove(identifier); } } } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("FrameDescriptor@").append(Integer.toHexString(hashCode())); sb.append("{"); boolean comma = false; for (FrameSlot slot : slots) { if (comma) { sb.append(", "); } else { comma = true; } sb.append(slot.getIndex()).append(":").append(slot.getIdentifier()); } sb.append("}"); return sb.toString(); } }