author | mhaupt |
Tue, 09 Feb 2016 14:14:06 +0100 | |
changeset 35793 | b89ddacf3104 |
parent 35725 | c7a2c18529b1 |
child 36026 | ad5ff5d5459b |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
35793 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.codegen.types; |
|
27 |
||
28 |
import static jdk.internal.org.objectweb.asm.Opcodes.BIPUSH; |
|
29 |
import static jdk.internal.org.objectweb.asm.Opcodes.I2D; |
|
30 |
import static jdk.internal.org.objectweb.asm.Opcodes.I2L; |
|
24751 | 31 |
import static jdk.internal.org.objectweb.asm.Opcodes.IADD; |
16147 | 32 |
import static jdk.internal.org.objectweb.asm.Opcodes.IAND; |
33 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_0; |
|
34 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1; |
|
35 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_2; |
|
36 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_3; |
|
37 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_4; |
|
38 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_5; |
|
39 |
import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_M1; |
|
40 |
import static jdk.internal.org.objectweb.asm.Opcodes.ILOAD; |
|
24751 | 41 |
import static jdk.internal.org.objectweb.asm.Opcodes.IMUL; |
42 |
import static jdk.internal.org.objectweb.asm.Opcodes.INEG; |
|
16147 | 43 |
import static jdk.internal.org.objectweb.asm.Opcodes.IOR; |
44 |
import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN; |
|
45 |
import static jdk.internal.org.objectweb.asm.Opcodes.ISHL; |
|
46 |
import static jdk.internal.org.objectweb.asm.Opcodes.ISHR; |
|
47 |
import static jdk.internal.org.objectweb.asm.Opcodes.ISTORE; |
|
24751 | 48 |
import static jdk.internal.org.objectweb.asm.Opcodes.ISUB; |
16147 | 49 |
import static jdk.internal.org.objectweb.asm.Opcodes.IUSHR; |
50 |
import static jdk.internal.org.objectweb.asm.Opcodes.IXOR; |
|
51 |
import static jdk.internal.org.objectweb.asm.Opcodes.SIPUSH; |
|
52 |
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; |
|
24719 | 53 |
import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT; |
24751 | 54 |
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT; |
55 |
||
16147 | 56 |
import jdk.internal.org.objectweb.asm.MethodVisitor; |
57 |
import jdk.nashorn.internal.codegen.CompilerConstants; |
|
27358
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
58 |
import jdk.nashorn.internal.runtime.JSType; |
16147 | 59 |
|
60 |
/** |
|
61 |
* Type class: INT |
|
62 |
*/ |
|
63 |
class IntType extends BitwiseType { |
|
27204 | 64 |
private static final long serialVersionUID = 1L; |
16147 | 65 |
|
66 |
private static final CompilerConstants.Call TO_STRING = staticCallNoLookup(Integer.class, "toString", String.class, int.class); |
|
67 |
private static final CompilerConstants.Call VALUE_OF = staticCallNoLookup(Integer.class, "valueOf", Integer.class, int.class); |
|
68 |
||
69 |
protected IntType() { |
|
70 |
super("int", int.class, 2, 1); |
|
71 |
} |
|
72 |
||
73 |
@Override |
|
74 |
public Type nextWider() { |
|
34732 | 75 |
return NUMBER; |
16147 | 76 |
} |
77 |
||
78 |
@Override |
|
79 |
public Class<?> getBoxedType() { |
|
80 |
return Integer.class; |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
24719 | 84 |
public char getBytecodeStackType() { |
85 |
return 'I'; |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
16147 | 89 |
public Type ldc(final MethodVisitor method, final Object c) { |
90 |
assert c instanceof Integer; |
|
91 |
||
31549
b627094c5649
8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents:
27358
diff
changeset
|
92 |
final int value = ((Integer) c); |
16147 | 93 |
|
94 |
switch (value) { |
|
24719 | 95 |
case -1: |
96 |
method.visitInsn(ICONST_M1); |
|
97 |
break; |
|
98 |
case 0: |
|
99 |
method.visitInsn(ICONST_0); |
|
100 |
break; |
|
101 |
case 1: |
|
102 |
method.visitInsn(ICONST_1); |
|
103 |
break; |
|
104 |
case 2: |
|
105 |
method.visitInsn(ICONST_2); |
|
106 |
break; |
|
107 |
case 3: |
|
108 |
method.visitInsn(ICONST_3); |
|
109 |
break; |
|
110 |
case 4: |
|
111 |
method.visitInsn(ICONST_4); |
|
112 |
break; |
|
113 |
case 5: |
|
114 |
method.visitInsn(ICONST_5); |
|
115 |
break; |
|
116 |
default: |
|
117 |
if (value == (byte) value) { |
|
118 |
method.visitIntInsn(BIPUSH, value); |
|
119 |
} else if (value == (short) value) { |
|
120 |
method.visitIntInsn(SIPUSH, value); |
|
121 |
} else { |
|
122 |
method.visitLdcInsn(c); |
|
123 |
} |
|
124 |
break; |
|
16147 | 125 |
} |
126 |
||
127 |
return Type.INT; |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public Type convert(final MethodVisitor method, final Type to) { |
|
132 |
if (to.isEquivalentTo(this)) { |
|
133 |
return to; |
|
134 |
} |
|
135 |
||
136 |
if (to.isNumber()) { |
|
137 |
method.visitInsn(I2D); |
|
138 |
} else if (to.isLong()) { |
|
139 |
method.visitInsn(I2L); |
|
140 |
} else if (to.isBoolean()) { |
|
141 |
//nop |
|
142 |
} else if (to.isString()) { |
|
24719 | 143 |
invokestatic(method, TO_STRING); |
16147 | 144 |
} else if (to.isObject()) { |
24719 | 145 |
invokestatic(method, VALUE_OF); |
16147 | 146 |
} else { |
24719 | 147 |
throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to); |
16147 | 148 |
} |
149 |
||
150 |
return to; |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
24719 | 154 |
public Type add(final MethodVisitor method, final int programPoint) { |
24751 | 155 |
if(programPoint == INVALID_PROGRAM_POINT) { |
156 |
method.visitInsn(IADD); |
|
157 |
} else { |
|
35793 | 158 |
method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint); |
24751 | 159 |
} |
16147 | 160 |
return INT; |
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public Type shr(final MethodVisitor method) { |
|
165 |
method.visitInsn(IUSHR); |
|
166 |
return INT; |
|
167 |
} |
|
168 |
||
169 |
@Override |
|
170 |
public Type sar(final MethodVisitor method) { |
|
171 |
method.visitInsn(ISHR); |
|
172 |
return INT; |
|
173 |
} |
|
174 |
||
175 |
@Override |
|
176 |
public Type shl(final MethodVisitor method) { |
|
177 |
method.visitInsn(ISHL); |
|
178 |
return INT; |
|
179 |
} |
|
180 |
||
181 |
@Override |
|
182 |
public Type and(final MethodVisitor method) { |
|
183 |
method.visitInsn(IAND); |
|
184 |
return INT; |
|
185 |
} |
|
186 |
||
187 |
@Override |
|
188 |
public Type or(final MethodVisitor method) { |
|
189 |
method.visitInsn(IOR); |
|
190 |
return INT; |
|
191 |
} |
|
192 |
||
193 |
@Override |
|
194 |
public Type xor(final MethodVisitor method) { |
|
195 |
method.visitInsn(IXOR); |
|
196 |
return INT; |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public Type load(final MethodVisitor method, final int slot) { |
|
201 |
assert slot != -1; |
|
202 |
method.visitVarInsn(ILOAD, slot); |
|
203 |
return INT; |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public void store(final MethodVisitor method, final int slot) { |
|
208 |
assert slot != -1; |
|
209 |
method.visitVarInsn(ISTORE, slot); |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
24719 | 213 |
public Type sub(final MethodVisitor method, final int programPoint) { |
24751 | 214 |
if(programPoint == INVALID_PROGRAM_POINT) { |
215 |
method.visitInsn(ISUB); |
|
216 |
} else { |
|
35793 | 217 |
method.visitInvokeDynamicInsn("isub", "(II)I", MATHBOOTSTRAP, programPoint); |
24751 | 218 |
} |
16147 | 219 |
return INT; |
220 |
} |
|
221 |
||
222 |
@Override |
|
24719 | 223 |
public Type mul(final MethodVisitor method, final int programPoint) { |
24751 | 224 |
if(programPoint == INVALID_PROGRAM_POINT) { |
225 |
method.visitInsn(IMUL); |
|
226 |
} else { |
|
35793 | 227 |
method.visitInvokeDynamicInsn("imul", "(II)I", MATHBOOTSTRAP, programPoint); |
24751 | 228 |
} |
16147 | 229 |
return INT; |
230 |
} |
|
231 |
||
232 |
@Override |
|
24719 | 233 |
public Type div(final MethodVisitor method, final int programPoint) { |
27358
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
234 |
if (programPoint == INVALID_PROGRAM_POINT) { |
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
235 |
JSType.DIV_ZERO.invoke(method); |
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
236 |
} else { |
35793 | 237 |
method.visitInvokeDynamicInsn("idiv", "(II)I", MATHBOOTSTRAP, programPoint); |
27358
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
238 |
} |
16147 | 239 |
return INT; |
240 |
} |
|
241 |
||
242 |
@Override |
|
24751 | 243 |
public Type rem(final MethodVisitor method, final int programPoint) { |
27358
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
244 |
if (programPoint == INVALID_PROGRAM_POINT) { |
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
245 |
JSType.REM_ZERO.invoke(method); |
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
246 |
} else { |
35793 | 247 |
method.visitInvokeDynamicInsn("irem", "(II)I", MATHBOOTSTRAP, programPoint); |
27358
8898e06e68c1
8061957: Some arithmetic operations have unnecessary widening
attila
parents:
27204
diff
changeset
|
248 |
} |
16147 | 249 |
return INT; |
250 |
} |
|
251 |
||
252 |
@Override |
|
24719 | 253 |
public Type neg(final MethodVisitor method, final int programPoint) { |
24751 | 254 |
if(programPoint == INVALID_PROGRAM_POINT) { |
255 |
method.visitInsn(INEG); |
|
256 |
} else { |
|
35793 | 257 |
method.visitInvokeDynamicInsn("ineg", "(I)I", MATHBOOTSTRAP, programPoint); |
24751 | 258 |
} |
16147 | 259 |
return INT; |
260 |
} |
|
261 |
||
262 |
@Override |
|
263 |
public void _return(final MethodVisitor method) { |
|
264 |
method.visitInsn(IRETURN); |
|
265 |
} |
|
266 |
||
267 |
@Override |
|
268 |
public Type loadUndefined(final MethodVisitor method) { |
|
24719 | 269 |
method.visitLdcInsn(UNDEFINED_INT); |
270 |
return INT; |
|
271 |
} |
|
272 |
||
273 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
274 |
public Type loadForcedInitializer(final MethodVisitor method) { |
24719 | 275 |
method.visitInsn(ICONST_0); |
16147 | 276 |
return INT; |
277 |
} |
|
278 |
||
279 |
@Override |
|
280 |
public Type cmp(final MethodVisitor method, final boolean isCmpG) { |
|
24719 | 281 |
throw new UnsupportedOperationException("cmp" + (isCmpG ? 'g' : 'l')); |
16147 | 282 |
} |
283 |
||
284 |
@Override |
|
285 |
public Type cmp(final MethodVisitor method) { |
|
24719 | 286 |
throw new UnsupportedOperationException("cmp"); |
16147 | 287 |
} |
24719 | 288 |
|
16147 | 289 |
} |