Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/ObjectAccessNode.java @ 21770:c76742cc2c6f
Polishing inter-operability APIs: Exposing only Message, TruffleObject and ForeignAccess-related classes.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 08 Jun 2015 04:50:13 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
21769:b6309a7d5a44 | 21770:c76742cc2c6f |
---|---|
1 /* | |
2 * Copyright (c) 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.interop; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.frame.*; | |
29 import com.oracle.truffle.api.nodes.*; | |
30 | |
31 abstract class ObjectAccessNode extends Node { | |
32 | |
33 public abstract Object executeWith(VirtualFrame frame, TruffleObject receiver, Object[] arguments); | |
34 | |
35 } | |
36 | |
37 class UnresolvedObjectAccessNode extends ObjectAccessNode { | |
38 | |
39 private static final int CACHE_SIZE = 8; | |
40 private int cacheLength = 1; | |
41 | |
42 @Override | |
43 public Object executeWith(VirtualFrame frame, TruffleObject receiver, Object[] arguments) { | |
44 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
45 ForeignObjectAccessHeadNode nthParent = (ForeignObjectAccessHeadNode) NodeUtil.getNthParent(this, cacheLength); | |
46 ObjectAccessNode first = nthParent.getFirst(); | |
47 if (cacheLength < CACHE_SIZE) { | |
48 cacheLength++; | |
49 CachedObjectAccessNode createCachedAccess = createCachedAccess(receiver, nthParent.getAccessTree(), first); | |
50 return first.replace(createCachedAccess).executeWith(frame, receiver, arguments); | |
51 } else { | |
52 return first.replace(createGenericAccess(nthParent.getAccessTree())).executeWith(frame, receiver, arguments); | |
53 } | |
54 } | |
55 | |
56 private static CachedObjectAccessNode createCachedAccess(TruffleObject receiver, Message accessTree, ObjectAccessNode next) { | |
57 ForeignAccess fa = receiver.getForeignAccess(); | |
58 final CallTarget ct = fa.access(accessTree); | |
59 if (ct == null) { | |
60 throw new IllegalArgumentException("Message " + accessTree + " not recognized by " + fa); | |
61 } | |
62 return new CachedObjectAccessNode(Truffle.getRuntime().createDirectCallNode(ct), next, fa); | |
63 } | |
64 | |
65 private static GenericObjectAccessNode createGenericAccess(Message access) { | |
66 return new GenericObjectAccessNode(access); | |
67 } | |
68 } | |
69 | |
70 class GenericObjectAccessNode extends ObjectAccessNode { | |
71 | |
72 private final Message access; | |
73 @Child private IndirectCallNode indirectCallNode; | |
74 | |
75 public GenericObjectAccessNode(Message access) { | |
76 this.access = access; | |
77 indirectCallNode = Truffle.getRuntime().createIndirectCallNode(); | |
78 } | |
79 | |
80 public GenericObjectAccessNode(GenericObjectAccessNode prev) { | |
81 this(prev.access); | |
82 } | |
83 | |
84 @Override | |
85 public Object executeWith(VirtualFrame frame, TruffleObject truffleObject, Object[] arguments) { | |
86 final ForeignAccess fa = truffleObject.getForeignAccess(); | |
87 final CallTarget ct = fa.access(access); | |
88 if (ct == null) { | |
89 throw new IllegalStateException("Message " + access + " not recognized by " + fa); | |
90 } | |
91 return indirectCallNode.call(frame, ct, ForeignAccessArguments.create(truffleObject, arguments)); | |
92 } | |
93 } | |
94 | |
95 class CachedObjectAccessNode extends ObjectAccessNode { | |
96 @Child private DirectCallNode callTarget; | |
97 @Child private ObjectAccessNode next; | |
98 | |
99 private final ForeignAccess languageCheck; | |
100 | |
101 protected CachedObjectAccessNode(DirectCallNode callTarget, ObjectAccessNode next, ForeignAccess languageCheck) { | |
102 this.callTarget = callTarget; | |
103 this.next = next; | |
104 this.languageCheck = languageCheck; | |
105 this.callTarget.forceInlining(); | |
106 } | |
107 | |
108 protected CachedObjectAccessNode(CachedObjectAccessNode prev) { | |
109 this(prev.callTarget, prev.next, prev.languageCheck); | |
110 } | |
111 | |
112 @Override | |
113 public Object executeWith(VirtualFrame frame, TruffleObject receiver, Object[] arguments) { | |
114 return doAccess(frame, receiver, arguments); | |
115 } | |
116 | |
117 private Object doAccess(VirtualFrame frame, TruffleObject receiver, Object[] arguments) { | |
118 if (languageCheck.canHandle(receiver)) { | |
119 return callTarget.call(frame, ForeignAccessArguments.create(receiver, arguments)); | |
120 } else { | |
121 return doNext(frame, receiver, arguments); | |
122 } | |
123 } | |
124 | |
125 private Object doNext(VirtualFrame frame, TruffleObject receiver, Object[] arguments) { | |
126 return next.executeWith(frame, receiver, arguments); | |
127 } | |
128 } |