comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLDispatchNode.java @ 20956:2170de9acab0

SL: use DSL for call dispatches.
author Christian Humer <christian.humer@gmail.com>
date Tue, 14 Apr 2015 15:16:14 +0200
parents
children 99e3f4c5c853
comparison
equal deleted inserted replaced
20955:f7bc60c3a8f6 20956:2170de9acab0
1 /*
2 * Copyright (c) 2013, 2015, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.sl.nodes.call;
24
25 import com.oracle.truffle.api.*;
26 import com.oracle.truffle.api.dsl.*;
27 import com.oracle.truffle.api.frame.*;
28 import com.oracle.truffle.api.nodes.*;
29 import com.oracle.truffle.sl.runtime.*;
30
31 public abstract class SLDispatchNode extends Node {
32
33 protected static final int INLINE_CACHE_SIZE = 2;
34
35 public abstract Object executeDispatch(VirtualFrame frame, SLFunction function, Object[] arguments);
36
37 /**
38 * Inline cached specialization of the dispatch.
39 *
40 * <p>
41 * Since SL is a quite simple language, the benefit of the inline cache is quite small: after
42 * checking that the actual function to be executed is the same as the cachedFuntion, we can
43 * safely execute the cached call target. You can reasonably argue that caching the call target
44 * is overkill, since we could just retrieve it via {@code function.getCallTarget()}. However,
45 * in a more complex language the lookup of the call target is usually much more complicated
46 * than in SL. In addition, caching the call target allows method inlining.
47 * </p>
48 *
49 * <p>
50 * {@code limit = "INLINE_CACHE_SIZE"} Specifies the limit number of inline cache specialization
51 * instantiations.
52 * </p>
53 * <p>
54 * {@code guards = "function == cachedFunction"} The inline cache check. Note that
55 * cachedFunction is a final field so that the compiler can optimize the check.
56 * </p>
57 * <p>
58 * {@code assumptions = "cachedFunction.getCallTargetStable()"} Support for function
59 * redefinition: When a function is redefined, the call target maintained by the SLFunction
60 * object is change. To avoid a check for that, we use an Assumption that is invalidated by the
61 * SLFunction when the change is performed. Since checking an assumption is a no-op in compiled
62 * code, the assumption check performed by the DSL does not add any overhead during optimized
63 * execution.
64 * </p>
65 *
66 * @see Cached
67 * @see Specialization
68 *
69 * @param function the dynamically provided function
70 * @param cachedFunction the cached function of the specialization instance
71 * @param callNode the {@link DirectCallNode} specifically created for the {@link CallTarget} in
72 * cachedFunction.
73 */
74 @Specialization(limit = "INLINE_CACHE_SIZE", guards = "function == cachedFunction", assumptions = "cachedFunction.getCallTargetStable()")
75 protected static Object doDirect(VirtualFrame frame, SLFunction function, Object[] arguments, //
76 @Cached("function") SLFunction cachedFunction, //
77 @Cached("create(cachedFunction.getCallTarget())") DirectCallNode callNode) {
78 /* Inline cache hit, we are safe to execute the cached call target. */
79 return callNode.call(frame, arguments);
80 }
81
82 /**
83 * Slow-path code for a call, used when the polymorphic inline cache exceeded its maximum size
84 * specified in <code>INLINE_CACHE_SIZE</code>. Such calls are not optimized any further, e.g.,
85 * no method inlining is performed.
86 */
87 @Specialization(contains = "doDirect")
88 protected static Object doIndirect(VirtualFrame frame, SLFunction function, Object[] arguments, //
89 @Cached("create()") IndirectCallNode callNode) {
90 /*
91 * SL has a quite simple call lookup: just ask the function for the current call target, and
92 * call it.
93 */
94 return callNode.call(frame, function.getCallTarget(), arguments);
95 }
96
97 }