forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodIndex.java
More file actions
208 lines (187 loc) · 7.23 KB
/
Copy pathMethodIndex.java
File metadata and controls
208 lines (187 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006-2007 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2006-2007 Charles Nutter <headius@headius.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.runtime;
import java.util.HashMap;
import java.util.Map;
import org.jruby.runtime.callsite.LtCallSite;
import org.jruby.runtime.callsite.LeCallSite;
import org.jruby.runtime.callsite.MinusCallSite;
import org.jruby.runtime.callsite.MulCallSite;
import org.jruby.runtime.callsite.NormalCachingCallSite;
import org.jruby.runtime.callsite.GtCallSite;
import org.jruby.runtime.callsite.PlusCallSite;
import org.jruby.runtime.callsite.GeCallSite;
import org.jruby.RubyInstanceConfig;
import org.jruby.runtime.callsite.CmpCallSite;
import org.jruby.runtime.callsite.EqCallSite;
import org.jruby.runtime.callsite.BitAndCallSite;
import org.jruby.runtime.callsite.BitOrCallSite;
import org.jruby.runtime.callsite.FunctionalCachingCallSite;
import org.jruby.runtime.callsite.RespondToCallSite;
import org.jruby.runtime.callsite.ShiftLeftCallSite;
import org.jruby.runtime.callsite.ShiftRightCallSite;
import org.jruby.runtime.callsite.SuperCallSite;
import org.jruby.runtime.callsite.VariableCachingCallSite;
import org.jruby.runtime.callsite.XorCallSite;
/**
*
* @author headius
*/
public class MethodIndex {
public static final int NO_METHOD = 0;
public static final int OP_EQUAL = 1;
public static final int EQL = 2;
public static final int HASH = 3;
public static final int OP_CMP = 4;
public static final int MAX_METHODS = 5;
public static final String[] METHOD_NAMES = {
"",
"==",
"eql?",
"hash",
"<=>"
};
public static CallSite getCallSite(String name) {
// fast and safe respond_to? call site logic
if (name.equals("respond_to?")) return new RespondToCallSite();
// only use fast ops if we're not tracing
if (RubyInstanceConfig.FASTOPS_COMPILE_ENABLED &&
!(RubyInstanceConfig.FULL_TRACE_ENABLED)) return getFastFixnumOpsCallSite(name);
return new NormalCachingCallSite(name);
}
private static final Map<String, String> FIXNUM_OPS = new HashMap<String, String>();
private static final String[][] fastFixnumOps = {
{"+", "op_plus"},
{"-", "op_minus"},
{"*", "op_mul"},
{"<", "op_lt"},
{"<=", "op_le"},
{">", "op_gt"},
{">=", "op_ge"},
{"<=>", "op_cmp"},
{"&", "op_and"},
{"|", "op_or"},
{"^", "op_xor"},
{">>", "op_rshift"},
{"<<", "op_lshift"}
};
static {
for (String[] fastOp : fastFixnumOps) FIXNUM_OPS.put(fastOp[0], fastOp[1]);
}
private static final Map<String, String> FLOAT_OPS = new HashMap<String, String>();
private static final String[][] fastFloatOps = {
{"+", "op_plus"},
{"-", "op_minus"},
{"*", "op_mul"},
{"<", "op_lt"},
{"<=", "op_le"},
{">", "op_gt"},
{">=", "op_ge"},
{"<=>", "op_cmp"}
};
static {
for (String[] fastOp : fastFloatOps) FLOAT_OPS.put(fastOp[0], fastOp[1]);
}
public static boolean hasFastFixnumOps(String name) {
return FIXNUM_OPS.containsKey(name);
}
public static String getFastFixnumOpsMethod(String name) {
return FIXNUM_OPS.get(name);
}
public static CallSite getFastFixnumOpsCallSite(String name) {
if (name.equals("+")) {
return new PlusCallSite();
} else if (name.equals("-")) {
return new MinusCallSite();
} else if (name.equals("*")) {
return new MulCallSite();
} else if (name.equals("<")) {
return new LtCallSite();
} else if (name.equals("<=")) {
return new LeCallSite();
} else if (name.equals(">")) {
return new GtCallSite();
} else if (name.equals(">=")) {
return new GeCallSite();
} else if (name.equals("==")) {
return new EqCallSite();
} else if (name.equals("<=>")) {
return new CmpCallSite();
} else if (name.equals("&")) {
return new BitAndCallSite();
} else if (name.equals("|")) {
return new BitOrCallSite();
} else if (name.equals("^")) {
return new XorCallSite();
} else if (name.equals(">>")) {
return new ShiftRightCallSite();
} else if (name.equals("<<")) {
return new ShiftLeftCallSite();
}
return new NormalCachingCallSite(name);
}
public static boolean hasFastFloatOps(String name) {
return FLOAT_OPS.containsKey(name);
}
public static String getFastFloatOpsMethod(String name) {
return FLOAT_OPS.get(name);
}
public static CallSite getFastFloatOpsCallSite(String name) {
if (name.equals("+")) {
return new PlusCallSite();
} else if (name.equals("-")) {
return new MinusCallSite();
} else if (name.equals("*")) {
return new MulCallSite();
} else if (name.equals("<")) {
return new LtCallSite();
} else if (name.equals("<=")) {
return new LeCallSite();
} else if (name.equals(">")) {
return new GtCallSite();
} else if (name.equals(">=")) {
return new GeCallSite();
} else if (name.equals("==")) {
return new EqCallSite();
} else if (name.equals("<=>")) {
return new CmpCallSite();
}
return new NormalCachingCallSite(name);
}
public static CallSite getFunctionalCallSite(String name) {
return new FunctionalCachingCallSite(name);
}
public static CallSite getVariableCallSite(String name) {
return new VariableCachingCallSite(name);
}
public static CallSite getSuperCallSite() {
return new SuperCallSite();
}
}