comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLUninitializedDispatchNode.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLUninitializedCallNode.java@533b21375e58
children 64c77f0577bb
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
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.
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.frame.*;
27 import com.oracle.truffle.api.nodes.*;
28 import com.oracle.truffle.sl.*;
29 import com.oracle.truffle.sl.runtime.*;
30
31 /**
32 * The last entry of a polymorphic inline cache.
33 */
34 final class SLUninitializedDispatchNode extends SLAbstractDispatchNode {
35
36 /**
37 * When we reach this method, all the previous cache entries did not match the function. If the
38 * cache is still small enough, we extend it by adding another {@link SLDirectDispatchNode}. If
39 * the cache reached its maximum size, we replace the whole dispatch chain with a
40 * {@link SLGenericDispatchNode}.
41 */
42 @Override
43 protected Object executeDispatch(VirtualFrame frame, SLFunction function, SLArguments arguments) {
44 /* The following code modifies the AST, so compiled code must be invalidated. */
45 CompilerDirectives.transferToInterpreterAndInvalidate();
46
47 /*
48 * Count the number of SLDirectDispatchNodes we already have in the cache. We walk the chain
49 * of parent nodes until we hit the SLCallNode. We know that a SLCallNode is always present.
50 */
51 Node cur = this;
52 int depth = 0;
53 while (cur.getParent() instanceof SLAbstractDispatchNode) {
54 cur = cur.getParent();
55 depth++;
56 }
57 SLCallNode callNode = (SLCallNode) cur.getParent();
58
59 SLAbstractDispatchNode specialized;
60 if (function.getCallTarget() == null) {
61 /* Corner case: the function is not defined, so report an error to the user. */
62 throw new SLException("Call of undefined function: " + function.getName());
63
64 } else if (depth < INLINE_CACHE_SIZE) {
65 /* Extend the inline cache. Allocate the new cache entry, and the new end of the cache. */
66 SLAbstractDispatchNode next = new SLUninitializedDispatchNode();
67 SLAbstractDispatchNode direct = new SLDirectDispatchNode(next, function);
68 /* Replace ourselfs with the new cache entry. */
69 specialized = replace(direct);
70
71 } else {
72 /* Cache size exceeded, fall back to a single generic dispatch node. */
73 SLAbstractDispatchNode generic = new SLGenericDispatchNode();
74 /* Replace the whole chain, not just ourselfs, with the new generic node. */
75 specialized = callNode.dispatchNode.replace(generic);
76 }
77
78 /*
79 * Execute the newly created node perform the actual dispatch. That saves us from
80 * duplicating the actual call logic here.
81 */
82 return specialized.executeDispatch(frame, function, arguments);
83 }
84 }