annotate graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Layout.java @ 21557:31fc2fce38f3

Merge.
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 13:32:18 +0200
parents b1530a6cce8c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18407
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
1 /*
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
4 *
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
10 *
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
15 * accompanied this code).
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
16 *
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
20 *
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
23 * questions.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
24 */
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
25 package com.oracle.truffle.api.object;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
26
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
27 import java.util.*;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
28
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
29 import com.oracle.truffle.api.nodes.NodeUtil.FieldOffsetProvider;
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 18407
diff changeset
30 import com.oracle.truffle.api.object.Shape.Allocator;
18407
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
31
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
32 public abstract class Layout {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
33 public static final EnumSet<ImplicitCast> NONE = EnumSet.noneOf(ImplicitCast.class);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
34 public static final EnumSet<ImplicitCast> INT_TO_DOUBLE = EnumSet.of(ImplicitCast.IntToDouble);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
35 public static final EnumSet<ImplicitCast> INT_TO_LONG = EnumSet.of(ImplicitCast.IntToLong);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
36
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
37 public static final String OPTION_PREFIX = "truffle.object.";
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
38
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
39 private static final LayoutFactory LAYOUT_FACTORY = loadLayoutFactory();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
40
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
41 public enum ImplicitCast {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
42 IntToDouble,
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
43 IntToLong,
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
44 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
45
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
46 public static Layout createLayout() {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
47 return createLayout(NONE);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
48 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
49
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
50 public static Layout createLayout(EnumSet<ImplicitCast> allowedImplicitCasts) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
51 return new LayoutBuilder().setAllowedImplicitCasts(allowedImplicitCasts).build();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
52 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
53
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
54 public static Layout createLayout(EnumSet<ImplicitCast> allowedImplicitCasts, FieldOffsetProvider fieldOffsetProvider) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
55 return new LayoutBuilder().setAllowedImplicitCasts(allowedImplicitCasts).setFieldOffsetProvider(fieldOffsetProvider).build();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
56 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
57
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
58 public abstract DynamicObject newInstance(Shape shape);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
59
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
60 public abstract Class<? extends DynamicObject> getType();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
61
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
62 public abstract Shape createShape(ObjectType operations);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
63
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
64 public abstract Shape createShape(ObjectType operations, Object sharedData);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
65
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
66 public abstract Shape createShape(ObjectType operations, Object sharedData, int id);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
67
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
68 /**
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
69 * Create an allocator for static property creation. Reserves all array extension slots.
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
70 */
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
71 public abstract Allocator createAllocator();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
72
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
73 protected static LayoutFactory getFactory() {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
74 return LAYOUT_FACTORY;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
75 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
76
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
77 private static LayoutFactory loadLayoutFactory() {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
78 LayoutFactory bestLayoutFactory = null;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
79
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
80 String layoutFactoryImplClassName = System.getProperty(OPTION_PREFIX + "LayoutFactory");
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
81 if (layoutFactoryImplClassName != null) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
82 Class<?> clazz;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
83 try {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
84 clazz = Class.forName(layoutFactoryImplClassName);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
85 } catch (ClassNotFoundException e) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
86 throw new RuntimeException(e);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
87 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
88 try {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
89 bestLayoutFactory = (LayoutFactory) clazz.newInstance();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
90 } catch (InstantiationException | IllegalAccessException e) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
91 throw new AssertionError(e);
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
92 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
93 } else {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
94 ServiceLoader<LayoutFactory> serviceLoader = ServiceLoader.load(LayoutFactory.class, Layout.class.getClassLoader());
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
95 for (LayoutFactory currentLayoutFactory : serviceLoader) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
96 if (bestLayoutFactory == null) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
97 bestLayoutFactory = currentLayoutFactory;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
98 } else if (currentLayoutFactory.getPriority() >= bestLayoutFactory.getPriority()) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
99 assert currentLayoutFactory.getPriority() != bestLayoutFactory.getPriority();
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
100 bestLayoutFactory = currentLayoutFactory;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
101 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
102 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
103 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
104
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
105 if (bestLayoutFactory == null) {
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
106 throw new AssertionError("LayoutFactory not found");
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
107 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
108 return bestLayoutFactory;
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
109 }
f439fdb137a3 Truffle: initial commit of object API
Andreas Woess <andreas.woess@jku.at>
parents:
diff changeset
110 }