# HG changeset patch # User Andreas Woess # Date 1416323925 -3600 # Node ID f439fdb137a3c968c6aa37e993ad3e476a89e30a # Parent 194041c3fdabe9465cbc4275b3a4b6424816ca07 Truffle: initial commit of object API diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/BaseLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/BaseLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface BaseLocation { + /** + * Get object value as object at this location in store. + * + * @param shape the current shape of the object, which must contain this location + */ + Object get(DynamicObject store, Shape shape); + + /** + * Get object value as object at this location in store. For internal use only and subject to + * change, use {@link #get(DynamicObject, Shape)} instead. + * + * @param condition the result of a shape check or {@code false} + * @see #get(DynamicObject, Shape) + */ + Object get(DynamicObject store, boolean condition); + + /** + * Set object value at this location in store. + * + * @throws IncompatibleLocationException for storage type invalidations + * @throws FinalLocationException for effectively final fields + */ + void set(DynamicObject store, Object value) throws IncompatibleLocationException, FinalLocationException; + + /** + * Set object value at this location in store. + * + * @param shape the current shape of the storage object + * @throws IncompatibleLocationException for storage type invalidations + * @throws FinalLocationException for effectively final fields + */ + void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException; + + /** + * Set object value at this location in store and update shape. + * + * @param oldShape the shape before the transition + * @param newShape new shape after the transition + * @throws IncompatibleLocationException if value is of non-assignable type + */ + void set(DynamicObject store, Object value, Shape oldShape, Shape newShape) throws IncompatibleLocationException; +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/BooleanLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/BooleanLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface BooleanLocation extends TypedLocation { + /** + * @see #get(DynamicObject, Shape) + */ + boolean getBoolean(DynamicObject store, Shape shape); + + /** + * @see #get(DynamicObject, boolean) + */ + boolean getBoolean(DynamicObject store, boolean condition); + + /** + * @see #set(DynamicObject, Object) + */ + void setBoolean(DynamicObject store, boolean value) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape) + */ + void setBoolean(DynamicObject store, boolean value, Shape shape) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape, Shape) + */ + void setBoolean(DynamicObject store, boolean value, Shape oldShape, Shape newShape); + + Class getType(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DebugCounter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DebugCounter.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +import java.io.*; +import java.util.*; +import java.util.concurrent.atomic.*; + +public final class DebugCounter { + private static final ArrayList allCounters = new ArrayList<>(); + + private final String name; + private final AtomicLong value; + + private DebugCounter(String name) { + this.name = name; + this.value = new AtomicLong(); + allCounters.add(this); + } + + public static DebugCounter create(String name) { + return new DebugCounter(name); + } + + public long get() { + return value.get(); + } + + public void inc() { + value.incrementAndGet(); + } + + @Override + public String toString() { + return name + ": " + value; + } + + public static void dumpCounters() { + dumpCounters(System.out); + } + + public static void dumpCounters(PrintStream out) { + for (DebugCounter counter : allCounters) { + out.println(counter); + } + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DoubleLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DoubleLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface DoubleLocation extends TypedLocation { + /** + * @see #get(DynamicObject, Shape) + */ + double getDouble(DynamicObject store, Shape shape); + + /** + * @see #get(DynamicObject, boolean) + */ + double getDouble(DynamicObject store, boolean condition); + + /** + * @see #set(DynamicObject, Object) + */ + void setDouble(DynamicObject store, double value) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape) + */ + void setDouble(DynamicObject store, double value, Shape shape) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape, Shape) + */ + void setDouble(DynamicObject store, double value, Shape oldShape, Shape newShape); + + Class getType(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import com.oracle.truffle.api.*; + +public interface DynamicObject extends TypedObject { + /** + * Get the object's current shape. + */ + Shape getShape(); + + /** + * Get property value. + * + * @param key property identifier + * @param defaultValue return value if property is not found + * @return property value or defaultValue if object has no such property + */ + Object get(Object key, Object defaultValue); + + /** + * Set value of existing property. + * + * @param key property identifier + * @param value value to be set + * @return {@code true} if successful or {@code false} if property not found + */ + boolean set(Object key, Object value); + + /** + * Define new property or redefine existing property. + * + * @param key property identifier + * @param value value to be set + * @param flags flags to be set + */ + void define(Object key, Object value, int flags); + + /** + * Define new property with a static location or change existing property. + * + * @param key property identifier + * @param value value to be set + * @param flags flags to be set + * @param locationFactory factory function that creates a location for a given shape and value + */ + void define(Object key, Object value, int flags, LocationFactory locationFactory); + + /** + * Change property flags. + * + * @param key property identifier + * @param newFlags flags to be set + * @return {@code true} if successful or {@code false} if property not found + */ + boolean changeFlags(Object key, int newFlags); + + /** + * Change property flags. + * + * @param key property identifier + * @param flagsUpdateFunction function updating old flags to new flags + * @return {@code true} if successful or {@code false} if property not found + */ + boolean changeFlags(Object key, FlagsFunction flagsUpdateFunction); + + /** + * Delete property. + * + * @param key property identifier + * @return {@code true} if successful or {@code false} if property not found + */ + boolean delete(Object key); + + /** + * Returns the number of properties in this object. + */ + int size(); + + /** + * Returns {@code true} if this object contains no properties. + */ + boolean isEmpty(); + + /** + * Set object shape and grow storage if necessary. + * + * @param oldShape the object's current shape (must equal {@link #getShape()}) + * @param newShape the new shape to be set + */ + void setShapeAndGrow(Shape oldShape, Shape newShape); + + /** + * Set object shape and resize storage if necessary. + * + * @param oldShape the object's current shape (must equal {@link #getShape()}) + * @param newShape the new shape to be set + */ + void setShapeAndResize(Shape oldShape, Shape newShape); + + /** + * Ensure object shape is up-to-date. + * + * @return {@code true} if shape has changed + */ + boolean updateShape(); + + public interface FlagsFunction { + int apply(int t); + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObjectFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObjectFactory.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +public interface DynamicObjectFactory { + DynamicObject newInstance(Object... initialValues); + + Shape getShape(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/FinalLocationException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/FinalLocationException.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import com.oracle.truffle.api.nodes.*; + +public final class FinalLocationException extends SlowPathException { + private static final long serialVersionUID = -30188494510914293L; +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/HiddenKey.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/HiddenKey.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +public final class HiddenKey { + private final String name; + + public HiddenKey(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return name; + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/IncompatibleLocationException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/IncompatibleLocationException.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import com.oracle.truffle.api.nodes.*; + +public final class IncompatibleLocationException extends SlowPathException { + private static final long serialVersionUID = -7734865392357341789L; +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/IntLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/IntLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface IntLocation extends TypedLocation { + /** + * @see #get(DynamicObject, Shape) + */ + int getInt(DynamicObject store, Shape shape); + + /** + * @see #get(DynamicObject, boolean) + */ + int getInt(DynamicObject store, boolean condition); + + /** + * @see #set(DynamicObject, Object) + */ + void setInt(DynamicObject store, int value) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape) + */ + void setInt(DynamicObject store, int value, Shape shape) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape, Shape) + */ + void setInt(DynamicObject store, int value, Shape oldShape, Shape newShape); + + Class getType(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Layout.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Layout.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import java.util.*; + +import com.oracle.truffle.api.nodes.NodeUtil.FieldOffsetProvider; +import com.oracle.truffle.api.object.Shape.*; + +public abstract class Layout { + public static final EnumSet NONE = EnumSet.noneOf(ImplicitCast.class); + public static final EnumSet INT_TO_DOUBLE = EnumSet.of(ImplicitCast.IntToDouble); + public static final EnumSet INT_TO_LONG = EnumSet.of(ImplicitCast.IntToLong); + + public static final String OPTION_PREFIX = "truffle.object."; + + private static final LayoutFactory LAYOUT_FACTORY = loadLayoutFactory(); + + public enum ImplicitCast { + IntToDouble, + IntToLong, + } + + public static Layout createLayout() { + return createLayout(NONE); + } + + public static Layout createLayout(EnumSet allowedImplicitCasts) { + return new LayoutBuilder().setAllowedImplicitCasts(allowedImplicitCasts).build(); + } + + public static Layout createLayout(EnumSet allowedImplicitCasts, FieldOffsetProvider fieldOffsetProvider) { + return new LayoutBuilder().setAllowedImplicitCasts(allowedImplicitCasts).setFieldOffsetProvider(fieldOffsetProvider).build(); + } + + public abstract DynamicObject newInstance(Shape shape); + + public abstract Class getType(); + + public abstract Shape createShape(ObjectType operations); + + public abstract Shape createShape(ObjectType operations, Object sharedData); + + public abstract Shape createShape(ObjectType operations, Object sharedData, int id); + + /** + * Create an allocator for static property creation. Reserves all array extension slots. + */ + public abstract Allocator createAllocator(); + + protected static LayoutFactory getFactory() { + return LAYOUT_FACTORY; + } + + private static LayoutFactory loadLayoutFactory() { + LayoutFactory bestLayoutFactory = null; + + String layoutFactoryImplClassName = System.getProperty(OPTION_PREFIX + "LayoutFactory"); + if (layoutFactoryImplClassName != null) { + Class clazz; + try { + clazz = Class.forName(layoutFactoryImplClassName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + try { + bestLayoutFactory = (LayoutFactory) clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new AssertionError(e); + } + } else { + ServiceLoader serviceLoader = ServiceLoader.load(LayoutFactory.class, Layout.class.getClassLoader()); + for (LayoutFactory currentLayoutFactory : serviceLoader) { + if (bestLayoutFactory == null) { + bestLayoutFactory = currentLayoutFactory; + } else if (currentLayoutFactory.getPriority() >= bestLayoutFactory.getPriority()) { + assert currentLayoutFactory.getPriority() != bestLayoutFactory.getPriority(); + bestLayoutFactory = currentLayoutFactory; + } + } + } + + if (bestLayoutFactory == null) { + throw new AssertionError("LayoutFactory not found"); + } + return bestLayoutFactory; + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutBuilder.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +import java.util.*; + +import com.oracle.truffle.api.nodes.NodeUtil.*; +import com.oracle.truffle.api.object.Layout.*; + +public class LayoutBuilder { + private EnumSet allowedImplicitCasts; + private FieldOffsetProvider fieldOffsetProvider; + + public LayoutBuilder() { + this.allowedImplicitCasts = Layout.NONE; + this.fieldOffsetProvider = null; + } + + public Layout build() { + return Layout.getFactory().createLayout(this); + } + + public LayoutBuilder setAllowedImplicitCasts(EnumSet allowedImplicitCasts) { + this.allowedImplicitCasts = allowedImplicitCasts; + return this; + } + + public LayoutBuilder setFieldOffsetProvider(FieldOffsetProvider fieldOffsetProvider) { + this.fieldOffsetProvider = fieldOffsetProvider; + return this; + } + + public EnumSet getAllowedImplicitCasts() { + return allowedImplicitCasts; + } + + public FieldOffsetProvider getFieldOffsetProvider() { + return fieldOffsetProvider; + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface LayoutFactory { + Layout createLayout(LayoutBuilder layoutBuilder); + + Property createProperty(Object id, Location location, int flags); + + int getPriority(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import com.oracle.truffle.api.*; + +/** + * Property location. + * + * @see Shape + * @see Property + * @see DynamicObject + */ +public abstract class Location implements BaseLocation { + protected static IncompatibleLocationException incompatibleLocation() throws IncompatibleLocationException { + CompilerDirectives.transferToInterpreterAndInvalidate(); + throw new IncompatibleLocationException(); + } + + protected static FinalLocationException finalLocation() throws FinalLocationException { + CompilerDirectives.transferToInterpreterAndInvalidate(); + throw new FinalLocationException(); + } + + public final Object get(DynamicObject store, Shape shape) { + return get(store, checkShape(store, shape)); + } + + public Object get(DynamicObject store, boolean condition) { + return getInternal(store); + } + + public void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException { + setInternal(store, value); + } + + public final void set(DynamicObject store, Object value, Shape oldShape, Shape newShape) throws IncompatibleLocationException { + if (canStore(value)) { + store.setShapeAndGrow(oldShape, newShape); + try { + setInternal(store, value); + } catch (IncompatibleLocationException ex) { + throw new IllegalStateException(); + } + } else { + throw incompatibleLocation(); + } + } + + public final void set(DynamicObject store, Object value) throws IncompatibleLocationException, FinalLocationException { + set(store, value, null); + } + + protected abstract Object getInternal(DynamicObject store); + + /** + * Like {@link #set(DynamicObject, Object, Shape)}, but does not invalidate final locations. For + * internal use only and subject to change, use {@link DynamicObjectFactory} to create objects + * with predefined properties. + * + * @throws IncompatibleLocationException if value is of non-assignable type + */ + protected abstract void setInternal(DynamicObject store, Object value) throws IncompatibleLocationException; + + /** + * Returns {@code true} if the location can be set to the value. + * + * @param store the receiver object + * @param value the value in question + */ + public boolean canSet(DynamicObject store, Object value) { + return canStore(value); + } + + /** + * Returns {@code true} if the location is compatible with the value. + * + * The value may still be rejected if {@link #canSet(DynamicObject, Object)} returns false. + * + * @param value the value in question + */ + public boolean canStore(Object value) { + return true; + } + + /** + * Returns {@code true} if this is a final location, i.e. readonly once set. + */ + public boolean isFinal() { + return false; + } + + /** + * Returns {@code true} if this is an immutable constant location. + */ + public boolean isConstant() { + return false; + } + + /* + * Abstract to force overriding. + */ + @Override + public abstract int hashCode(); + + /* + * Abstract to force overriding. + */ + @Override + public abstract boolean equals(Object obj); + + protected static boolean checkShape(DynamicObject store, Shape shape) { + return store.getShape() == shape; + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LocationFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LocationFactory.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface LocationFactory { + Location createLocation(Shape shape, Object value); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LocationModifier.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LocationModifier.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +public enum LocationModifier { + Final, + NonNull, +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LongLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LongLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface LongLocation extends TypedLocation { + /** + * @see #get(DynamicObject, Shape) + */ + long getLong(DynamicObject store, Shape shape); + + /** + * @see #get(DynamicObject, boolean) + */ + long getLong(DynamicObject store, boolean condition); + + /** + * @see #set(DynamicObject, Object) + */ + void setLong(DynamicObject store, long value) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape) + */ + void setLong(DynamicObject store, long value, Shape shape) throws FinalLocationException; + + /** + * @see #set(DynamicObject, Object, Shape, Shape) + */ + void setLong(DynamicObject store, long value, Shape oldShape, Shape newShape); + + Class getType(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObjectLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObjectLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface ObjectLocation extends TypedLocation { + Class getType(); + + /** + * If {@code true}, this location does not accept {@code null} values. + */ + boolean isNonNull(); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObjectType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObjectType.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; + +public class ObjectType { + /** + * Delegate method for {@link DynamicObject#equals(Object)}. + */ + public boolean equals(DynamicObject object, Object other) { + return object == other; + } + + /** + * Delegate method for {@link DynamicObject#hashCode()}. + */ + public int hashCode(DynamicObject object) { + return System.identityHashCode(object); + } + + /** + * Delegate method for {@link DynamicObject#toString()}. + */ + @TruffleBoundary + public String toString(DynamicObject object) { + return "DynamicObject<" + this.toString() + ">@" + Integer.toHexString(hashCode(object)); + } + + /** + * Creates a data object to be associated with a newly created shape. + * + * @param shape the shape for which to create the data object + */ + public Object createShapeData(Shape shape) { + return null; + } + + /** + * Called when a new property is added to a shape. + * + * @param property the added property + * @param shapeBefore shape before the property was added + * @param shapeAfter shape after the property was added + */ + public void onPropertyAdded(Property property, Shape shapeBefore, Shape shapeAfter) { + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Property.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Property.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2012, 2014, 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.object; + +/** + * Property objects represent the mapping between low-level stores and high-level data. The simplest + * Property could be nothing more than a map of one index to one property's value, but abstracting + * the interface allows for getter/setter methods, type-checked properties, and other such + * specialized and language-specific behavior. ECMAScript[8.6.1] + */ +public abstract class Property { + protected Property() { + } + + public static Property create(Object key, Location location, int flags) { + return Layout.getFactory().createProperty(key, location, flags); + } + + /** + * Get property identifier. + */ + public abstract Object getKey(); + + /** + * Get property flags. + */ + public abstract int getFlags(); + + /** + * Change the property's location. + * + * @return a Property with the new location (or {@code this} if the location is unchanged). + */ + public abstract Property relocate(Location newLocation); + + /** + * Gets the value of this property of the object. + * + * @param store the store that this property resides in + * @param shape the current shape of the object, which must contain this location + * @see DynamicObject#get(Object, Object) + */ + public abstract Object get(DynamicObject store, Shape shape); + + /** + * Gets the value of this property of the object. + * + * @param store the store that this property resides in + * @param condition the result of a shape check or {@code false} + * @see DynamicObject#get(Object, Object) + * @see #get(DynamicObject, Shape) + */ + public abstract Object get(DynamicObject store, boolean condition); + + /** + * Assigns value to this property of the object. + * + * Throws an exception if the value cannot be assigned to the property's current location. + * + * @param store the store that this property resides in + * @param value the value to assign + * @param shape the current shape of the object or {@code null} + * @throws IncompatibleLocationException if the value is incompatible with the property location + * @throws FinalLocationException if the location is final and values differ + * @see DynamicObject#set(Object, Object) + */ + public abstract void set(DynamicObject store, Object value, Shape shape) throws IncompatibleLocationException, FinalLocationException; + + /** + * Assigns value to this property of the object. + * + * Automatically relocates the property if the value cannot be assigned to its current location. + * + * @param shape the current shape of the object or {@code null} + */ + public abstract void setGeneric(DynamicObject store, Object value, Shape shape); + + /** + * Like {@link #set(DynamicObject, Object, Shape)}, but throws an {@link IllegalStateException} + * instead. + */ + public abstract void setSafe(DynamicObject store, Object value, Shape shape); + + /** + * Like {@link #setSafe}, but ignores the finalness of the property. For internal use only. + * + * @param store the store that this property resides in + * @param value the value to assign + */ + public abstract void setInternal(DynamicObject store, Object value); + + /** + * Assigns value to this property of the object, changing the object's shape. + * + * Combines {@link DynamicObject#setShapeAndGrow(Shape, Shape)} and + * {@link #set(DynamicObject, Object, Shape)} to an atomic operation. + * + * @param store the store that this property resides in + * @param value the value to assign + * @param oldShape the shape before the transition + * @param newShape the shape after the transition + * @throws IncompatibleLocationException if the value is incompatible with the property location + */ + public abstract void set(DynamicObject store, Object value, Shape oldShape, Shape newShape) throws IncompatibleLocationException; + + /** + * Assigns value to this property of the object, changing the object's shape. + * + * Combines {@link DynamicObject#setShapeAndGrow(Shape, Shape)} and + * {@link #setGeneric(DynamicObject, Object, Shape)} to an atomic operation. + * + * @param store the store that this property resides in + * @param value the value to assign + * @param oldShape the shape before the transition + * @param newShape the shape after the transition + */ + public abstract void setGeneric(DynamicObject store, Object value, Shape oldShape, Shape newShape); + + /** + * Assigns value to this property of the object, changing the object's shape. + * + * Combines {@link DynamicObject#setShapeAndGrow(Shape, Shape)} and + * {@link #setSafe(DynamicObject, Object, Shape)} to an atomic operation. + * + * @param store the store that this property resides in + * @param value the value to assign + * @param oldShape the shape before the transition + * @param newShape the shape after the transition + */ + public abstract void setSafe(DynamicObject store, Object value, Shape oldShape, Shape newShape); + + /** + * Returns {@code true} if this property and some other property have the same key and flags. + */ + public abstract boolean isSame(Property other); + + /** + * Get the property location. + */ + public abstract Location getLocation(); + + /** + * Is this property hidden from iteration. + * + * @see HiddenKey + */ + public abstract boolean isHidden(); + + public abstract boolean isShadow(); + + /** + * Create a copy of the property with the given flags. + */ + public abstract Property copyWithFlags(int newFlags); + + public abstract Property copyWithRelocatable(boolean newRelocatable); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Shape.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Shape.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,304 @@ +/* + * Copyright (c) 2012, 2014, 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.object; + +import java.util.*; + +import com.oracle.truffle.api.*; + +/** + * Shape objects create a mapping of Property objects to Locations. Shapes are immutable; adding or + * deleting a property yields a new Shape which links to the old one. This allows inline caching to + * simply check the identity of an object's Shape to determine if the cache is valid. There is one + * exception to this immutability, the transition map, but that is used simply to assure that an + * identical series of property additions and deletions will yield the same Shape object. + * + * @see DynamicObject + * @see Property + * @see Location + */ +public abstract class Shape { + /** + * Get a property entry by key. + * + * @param key the identifier to look up + * @return a Property object, or null if not found + */ + public abstract Property getProperty(Object key); + + /** + * Add a new property in the map, yielding a new or cached Shape object. + * + * @param property the property to add + * @return the new Shape + */ + public abstract Shape addProperty(Property property); + + /** + * An {@link Iterable} over the shape's properties in insertion order. + */ + public abstract Iterable getProperties(); + + /** + * Get a list of properties that this Shape stores. + * + * @return list of properties + */ + public abstract List getPropertyList(Pred filter); + + /** + * Get a list of all properties that this Shape stores. + * + * @return list of properties + */ + public abstract List getPropertyList(); + + /** + * Returns all (also hidden) property objects in this shape. + * + * @param ascending desired order ({@code true} for insertion order, {@code false} for reverse + * insertion order) + */ + public abstract List getPropertyListInternal(boolean ascending); + + /** + * Get a filtered list of property keys in insertion order. + */ + public abstract List getKeyList(Pred filter); + + /** + * Get a list of all property keys in insertion order. + */ + public abstract List getKeyList(); + + /** + * Get all property keys in insertion order. + */ + public abstract Iterable getKeys(); + + /** + * Get an assumption that the shape is valid. + */ + public abstract Assumption getValidAssumption(); + + /** + * Check whether this shape is valid. + */ + public abstract boolean isValid(); + + /** + * Get an assumption that the shape is a leaf. + */ + public abstract Assumption getLeafAssumption(); + + /** + * Check whether this shape is a leaf in the transition graph, i.e. transitionless. + */ + public abstract boolean isLeaf(); + + /** + * @return the parent shape or {@code null} if none. + */ + public abstract Shape getParent(); + + /** + * Check whether the shape has a property with the given key. + */ + public abstract boolean hasProperty(Object key); + + /** + * Remove the given property from the shape. + */ + public abstract Shape removeProperty(Property property); + + /** + * Replace a property in the shape. + */ + public abstract Shape replaceProperty(Property oldProperty, Property newProperty); + + /** + * Get the last added property. + */ + public abstract Property getLastProperty(); + + public abstract int getId(); + + /** + * Append the property, relocating it to the next allocated location. + */ + public abstract Shape append(Property oldProperty); + + /** + * Obtain an {@link Allocator} instance for the purpose of allocating locations. + */ + public abstract Allocator allocator(); + + /** + * For copying over properties after exchanging the prototype of an object. + */ + public abstract Shape copyOverPropertiesInternal(Shape destination); + + /** + * Get number of properties in this shape. + */ + public abstract int getPropertyCount(); + + /** + * Get the shape's operations. + */ + public abstract ObjectType getObjectType(); + + /** + * Get the root shape. + */ + public abstract Shape getRoot(); + + /** + * Check whether this shape is identical to the given shape. + */ + public abstract boolean check(DynamicObject subject); + + /** + * Get the shape's layout. + */ + public abstract Layout getLayout(); + + /** + * Get the shape's custom data. + */ + public abstract Object getData(); + + /** + * Get the shape's shared data. + */ + public abstract Object getSharedData(); + + /** + * Query whether the shape has a transition with the given key. + */ + public abstract boolean hasTransitionWithKey(Object key); + + /** + * Clone off a separate shape with new shared data. + */ + public abstract Shape createSeparateShape(Object sharedData); + + /** + * Change the shape's type, yielding a new shape. + */ + public abstract Shape changeType(ObjectType newOps); + + /** + * Reserve the primitive extension array field. + */ + public abstract Shape reservePrimitiveExtensionArray(); + + /** + * Create a new {@link DynamicObject} instance with this shape. + */ + public abstract DynamicObject newInstance(); + + /** + * Create a {@link DynamicObjectFactory} for creating instances of this shape. + */ + public abstract DynamicObjectFactory createFactory(); + + /** + * Get mutex object shared by related shapes, i.e. shapes with a common root. + */ + public abstract Object getMutex(); + + public abstract int getObjectArraySize(); + + public abstract int getObjectFieldSize(); + + public abstract int getPrimitiveArraySize(); + + public abstract int getPrimitiveFieldSize(); + + public abstract int getObjectArrayCapacity(); + + public abstract int getPrimitiveArrayCapacity(); + + public abstract boolean hasPrimitiveArray(); + + /** + * Are these two shapes related, i.e. do they have the same root? + * + * @param other Shape to compare to + * @return true if one shape is an upcast of the other, or the Shapes are equal + */ + public abstract boolean isRelated(Shape other); + + public abstract Shape tryMerge(Shape other); + + public R accept(ShapeVisitor visitor) { + return visitor.visitShape(this); + } + + public abstract static class Allocator { + protected abstract Location locationForValue(Object value, boolean useFinal, boolean nonNull); + + public final Location locationForValue(Object value) { + return locationForValue(value, false, value != null); + } + + public final Location locationForValue(Object value, EnumSet modifiers) { + assert value != null || !modifiers.contains(LocationModifier.NonNull); + return locationForValue(value, modifiers.contains(LocationModifier.Final), modifiers.contains(LocationModifier.NonNull)); + } + + protected abstract Location locationForType(Class type, boolean useFinal, boolean nonNull); + + public final Location locationForType(Class type) { + return locationForType(type, false, false); + } + + public final Location locationForType(Class type, EnumSet modifiers) { + return locationForType(type, modifiers.contains(LocationModifier.Final), modifiers.contains(LocationModifier.NonNull)); + } + + public abstract Location constantLocation(Object value); + + public abstract Location declaredLocation(Object value); + + public abstract Allocator addLocation(Location location); + } + + /** + * Represents a predicate (boolean-valued function) of one argument. For Java 7 compatibility. + * + * @param the type of the input to the predicate + */ + public interface Pred { + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, otherwise {@code false} + */ + boolean test(T t); + } +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ShapeVisitor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ShapeVisitor.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2014, 2014, 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.object; + +public interface ShapeVisitor { + R visitShape(Shape shape); +} diff -r 194041c3fdab -r f439fdb137a3 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/TypedLocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/TypedLocation.java Tue Nov 18 16:18:45 2014 +0100 @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2013, 2014, 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.object; + +public interface TypedLocation extends BaseLocation { + /** + * The type of this location. + */ + Class getType(); +} diff -r 194041c3fdab -r f439fdb137a3 mx/suite.py --- a/mx/suite.py Fri Nov 14 13:49:12 2014 +0100 +++ b/mx/suite.py Tue Nov 18 16:18:45 2014 +0100 @@ -1132,6 +1132,15 @@ "workingSets" : "Truffle,Codegen", }, + "com.oracle.truffle.api.object" : { + "subDir" : "graal", + "sourceDirs" : ["src"], + "dependencies" : ["com.oracle.truffle.api"], + "checkstyle" : "com.oracle.truffle.api", + "javaCompliance" : "1.7", + "workingSets" : "API,Truffle", + }, + "com.oracle.truffle.sl" : { "subDir" : "graal", "sourceDirs" : ["src"],