comparison graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Layout.java @ 18407:f439fdb137a3

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