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