Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/core/CoreMethodNodeManager.java @ 13706:232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 20 Jan 2014 13:44:54 +0100 |
parents | 497fada09efb |
children | 64fa70319890 |
rev | line source |
---|---|
13514 | 1 /* |
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This | |
3 * code is released under a tri EPL/GPL/LGPL license. You can use it, | |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.nodes.core; | |
11 | |
12 import java.util.*; | |
13 | |
14 import com.oracle.truffle.api.*; | |
15 import com.oracle.truffle.api.dsl.*; | |
16 import com.oracle.truffle.api.frame.*; | |
17 import com.oracle.truffle.api.nodes.*; | |
18 import com.oracle.truffle.ruby.nodes.*; | |
19 import com.oracle.truffle.ruby.nodes.control.*; | |
20 import com.oracle.truffle.ruby.nodes.debug.*; | |
21 import com.oracle.truffle.ruby.nodes.methods.arguments.*; | |
22 import com.oracle.truffle.ruby.nodes.objects.*; | |
23 import com.oracle.truffle.ruby.runtime.*; | |
24 import com.oracle.truffle.ruby.runtime.core.*; | |
25 import com.oracle.truffle.ruby.runtime.methods.*; | |
26 | |
27 public abstract class CoreMethodNodeManager { | |
28 | |
29 /** | |
30 * Register all the nodes that represent core methods as methods with their respective classes, | |
31 * given the Object class object, which should already be initialized with all the core classes. | |
32 */ | |
33 public static void addMethods(RubyClass rubyObjectClass) { | |
34 for (MethodDetails methodDetails : getMethods()) { | |
13645
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
35 addMethod(rubyObjectClass, methodDetails); |
13514 | 36 } |
37 } | |
38 | |
39 /** | |
40 * Collect up all the core method nodes. Abstracted to allow the SVM to implement at compile | |
41 * type. | |
42 */ | |
43 public static List<MethodDetails> getMethods() { | |
44 final List<MethodDetails> methods = new ArrayList<>(); | |
45 getMethods(methods, ArrayNodesFactory.getFactories()); | |
46 getMethods(methods, BasicObjectNodesFactory.getFactories()); | |
47 getMethods(methods, BignumNodesFactory.getFactories()); | |
48 getMethods(methods, ClassNodesFactory.getFactories()); | |
49 getMethods(methods, ContinuationNodesFactory.getFactories()); | |
50 getMethods(methods, ComparableNodesFactory.getFactories()); | |
51 getMethods(methods, DebugNodesFactory.getFactories()); | |
52 getMethods(methods, DirNodesFactory.getFactories()); | |
53 getMethods(methods, ExceptionNodesFactory.getFactories()); | |
54 getMethods(methods, FalseClassNodesFactory.getFactories()); | |
55 getMethods(methods, FiberNodesFactory.getFactories()); | |
56 getMethods(methods, FileNodesFactory.getFactories()); | |
57 getMethods(methods, FixnumNodesFactory.getFactories()); | |
58 getMethods(methods, FloatNodesFactory.getFactories()); | |
59 getMethods(methods, HashNodesFactory.getFactories()); | |
60 getMethods(methods, KernelNodesFactory.getFactories()); | |
61 getMethods(methods, MainNodesFactory.getFactories()); | |
62 getMethods(methods, MatchDataNodesFactory.getFactories()); | |
63 getMethods(methods, MathNodesFactory.getFactories()); | |
64 getMethods(methods, ModuleNodesFactory.getFactories()); | |
65 getMethods(methods, NilClassNodesFactory.getFactories()); | |
66 getMethods(methods, ObjectNodesFactory.getFactories()); | |
67 getMethods(methods, ObjectSpaceNodesFactory.getFactories()); | |
68 getMethods(methods, ProcessNodesFactory.getFactories()); | |
69 getMethods(methods, ProcNodesFactory.getFactories()); | |
70 getMethods(methods, RangeNodesFactory.getFactories()); | |
71 getMethods(methods, RegexpNodesFactory.getFactories()); | |
72 getMethods(methods, SignalNodesFactory.getFactories()); | |
73 getMethods(methods, StringNodesFactory.getFactories()); | |
74 getMethods(methods, StructNodesFactory.getFactories()); | |
75 getMethods(methods, SymbolNodesFactory.getFactories()); | |
76 getMethods(methods, ThreadNodesFactory.getFactories()); | |
77 getMethods(methods, TimeNodesFactory.getFactories()); | |
78 getMethods(methods, TrueClassNodesFactory.getFactories()); | |
79 return methods; | |
80 } | |
81 | |
82 /** | |
83 * Collect up the core methods created by a factory. | |
84 */ | |
85 private static void getMethods(List<MethodDetails> methods, List<? extends NodeFactory<? extends CoreMethodNode>> nodeFactories) { | |
86 for (NodeFactory<? extends RubyNode> nodeFactory : nodeFactories) { | |
87 final GeneratedBy generatedBy = nodeFactory.getClass().getAnnotation(GeneratedBy.class); | |
88 final Class<?> nodeClass = generatedBy.value(); | |
89 final CoreClass classAnnotation = nodeClass.getEnclosingClass().getAnnotation(CoreClass.class); | |
90 final CoreMethod methodAnnotation = nodeClass.getAnnotation(CoreMethod.class); | |
91 methods.add(new MethodDetails(classAnnotation, methodAnnotation, nodeFactory)); | |
92 } | |
93 } | |
94 | |
95 /** | |
96 * Take a core method node factory, the annotations for the class and method, and add it as a | |
97 * method on the correct class. | |
98 */ | |
99 private static void addMethod(RubyClass rubyObjectClass, MethodDetails methodDetails) { | |
100 assert rubyObjectClass != null; | |
101 assert methodDetails != null; | |
102 | |
103 final RubyContext context = rubyObjectClass.getContext(); | |
104 | |
105 RubyModule module; | |
106 | |
107 if (methodDetails.getClassAnnotation().name().equals("main")) { | |
108 module = context.getCoreLibrary().getMainObject().getSingletonClass(); | |
109 } else { | |
110 module = (RubyModule) rubyObjectClass.lookupConstant(methodDetails.getClassAnnotation().name()); | |
111 } | |
112 | |
113 assert module != null : methodDetails.getClassAnnotation().name(); | |
114 | |
115 final List<String> names = Arrays.asList(methodDetails.getMethodAnnotation().names()); | |
116 assert names.size() >= 1; | |
117 | |
118 final String canonicalName = names.get(0); | |
119 final List<String> aliases = names.subList(1, names.size()); | |
120 | |
121 final UniqueMethodIdentifier uniqueIdentifier = new UniqueMethodIdentifier(); | |
122 final Visibility visibility = Visibility.PUBLIC; | |
123 | |
124 final RubyRootNode pristineRootNode = makeGenericMethod(context, methodDetails); | |
125 final CallTarget callTarget = Truffle.getRuntime().createCallTarget(NodeUtil.cloneNode(pristineRootNode)); | |
126 | |
127 final String intrinsicName = methodDetails.getClassAnnotation().name() + "#" + canonicalName; | |
128 | |
129 final InlinableMethodImplementation methodImplementation = new InlinableMethodImplementation(callTarget, null, new FrameDescriptor(), pristineRootNode, true, | |
130 methodDetails.getMethodAnnotation().appendCallNode()); | |
131 final RubyMethod method = new RubyMethod(pristineRootNode.getSourceSection(), module, uniqueIdentifier, intrinsicName, canonicalName, visibility, false, methodImplementation); | |
132 | |
133 module.addMethod(method); | |
134 | |
135 if (methodDetails.getMethodAnnotation().isModuleMethod()) { | |
136 module.getSingletonClass().addMethod(method); | |
137 } | |
138 | |
139 for (String alias : aliases) { | |
140 final RubyMethod withAlias = method.withNewName(alias); | |
141 | |
142 module.addMethod(withAlias); | |
143 | |
144 if (methodDetails.getMethodAnnotation().isModuleMethod()) { | |
145 module.getSingletonClass().addMethod(withAlias); | |
146 } | |
147 } | |
148 } | |
149 | |
150 private static RubyRootNode makeGenericMethod(RubyContext context, MethodDetails methodDetails) { | |
151 final SourceSection sourceSection = new CoreSourceSection(methodDetails.getClassAnnotation().name() + "#" + methodDetails.getMethodAnnotation().names()[0]); | |
152 | |
153 final Arity arity = new Arity(methodDetails.getMethodAnnotation().minArgs(), methodDetails.getMethodAnnotation().maxArgs()); | |
154 | |
155 final List<RubyNode> argumentsNodes = new ArrayList<>(); | |
156 | |
157 if (methodDetails.getMethodAnnotation().needsSelf()) { | |
158 argumentsNodes.add(new SelfNode(context, sourceSection)); | |
159 } | |
160 | |
161 if (methodDetails.getMethodAnnotation().isSplatted()) { | |
162 argumentsNodes.add(new ReadAllArgumentsNode(context, sourceSection)); | |
163 } else { | |
164 assert arity.getMaximum() != Arity.NO_MAXIMUM; | |
165 | |
166 for (int n = 0; n < arity.getMaximum(); n++) { | |
167 argumentsNodes.add(new ReadPreArgumentNode(context, sourceSection, n, true)); | |
168 } | |
169 } | |
170 | |
171 if (methodDetails.getMethodAnnotation().needsBlock()) { | |
172 argumentsNodes.add(new ReadBlockArgumentNode(context, sourceSection, true)); | |
173 } | |
174 | |
175 final RubyNode methodNode = methodDetails.getNodeFactory().createNode(context, sourceSection, argumentsNodes.toArray(new RubyNode[argumentsNodes.size()])); | |
176 final CheckArityNode checkArity = new CheckArityNode(context, sourceSection, arity); | |
177 final SequenceNode block = new SequenceNode(context, sourceSection, checkArity, methodNode); | |
178 | |
13706
232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
Christian Humer <christian.humer@gmail.com>
parents:
13645
diff
changeset
|
179 return new RubyRootNode(sourceSection, null, methodDetails.getClassAnnotation().name() + "#" + methodDetails.getMethodAnnotation().names()[0] + "(core)", block); |
13514 | 180 } |
181 | |
182 public static class MethodDetails { | |
183 | |
184 private final CoreClass classAnnotation; | |
185 private final CoreMethod methodAnnotation; | |
186 private final NodeFactory<? extends RubyNode> nodeFactory; | |
187 | |
188 public MethodDetails(CoreClass classAnnotation, CoreMethod methodAnnotation, NodeFactory<? extends RubyNode> nodeFactory) { | |
189 assert classAnnotation != null; | |
190 assert methodAnnotation != null; | |
191 assert nodeFactory != null; | |
192 this.classAnnotation = classAnnotation; | |
193 this.methodAnnotation = methodAnnotation; | |
194 this.nodeFactory = nodeFactory; | |
195 } | |
196 | |
197 public CoreClass getClassAnnotation() { | |
198 return classAnnotation; | |
199 } | |
200 | |
201 public CoreMethod getMethodAnnotation() { | |
202 return methodAnnotation; | |
203 } | |
204 | |
205 public NodeFactory<? extends RubyNode> getNodeFactory() { | |
206 return nodeFactory; | |
207 } | |
208 | |
209 } | |
210 | |
211 } |