LongType.java revision 1617:f35bd1bd0184
1/* 2 * Copyright (c) 2010, 2016, 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. 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 26package jdk.nashorn.internal.codegen.types; 27 28import static jdk.internal.org.objectweb.asm.Opcodes.L2D; 29import static jdk.internal.org.objectweb.asm.Opcodes.L2I; 30import static jdk.internal.org.objectweb.asm.Opcodes.LCONST_0; 31import static jdk.internal.org.objectweb.asm.Opcodes.LCONST_1; 32import static jdk.internal.org.objectweb.asm.Opcodes.LLOAD; 33import static jdk.internal.org.objectweb.asm.Opcodes.LRETURN; 34import static jdk.internal.org.objectweb.asm.Opcodes.LSTORE; 35import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; 36import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_LONG; 37 38import jdk.internal.org.objectweb.asm.MethodVisitor; 39import jdk.nashorn.internal.codegen.CompilerConstants; 40import jdk.nashorn.internal.runtime.JSType; 41 42/** 43 * Type class: LONG 44 */ 45class LongType extends Type { 46 private static final long serialVersionUID = 1L; 47 48 private static final CompilerConstants.Call VALUE_OF = staticCallNoLookup(Long.class, "valueOf", Long.class, long.class); 49 50 protected LongType(final String name) { 51 super(name, long.class, 3, 2); 52 } 53 54 protected LongType() { 55 this("long"); 56 } 57 58 @Override 59 public Type nextWider() { 60 return NUMBER; 61 } 62 63 @Override 64 public Class<?> getBoxedType() { 65 return Long.class; 66 } 67 68 @Override 69 public char getBytecodeStackType() { 70 return 'J'; 71 } 72 73 @Override 74 public Type load(final MethodVisitor method, final int slot) { 75 assert slot != -1; 76 method.visitVarInsn(LLOAD, slot); 77 return LONG; 78 } 79 80 @Override 81 public void store(final MethodVisitor method, final int slot) { 82 assert slot != -1; 83 method.visitVarInsn(LSTORE, slot); 84 } 85 86 @Override 87 public Type ldc(final MethodVisitor method, final Object c) { 88 assert c instanceof Long; 89 90 final long value = (Long) c; 91 92 if (value == 0L) { 93 method.visitInsn(LCONST_0); 94 } else if (value == 1L) { 95 method.visitInsn(LCONST_1); 96 } else { 97 method.visitLdcInsn(c); 98 } 99 100 return Type.LONG; 101 } 102 103 @Override 104 public Type convert(final MethodVisitor method, final Type to) { 105 if (isEquivalentTo(to)) { 106 return to; 107 } 108 109 if (to.isNumber()) { 110 method.visitInsn(L2D); 111 } else if (to.isInteger()) { 112 invokestatic(method, JSType.TO_INT32_L); 113 } else if (to.isBoolean()) { 114 method.visitInsn(L2I); 115 } else if (to.isObject()) { 116 invokestatic(method, VALUE_OF); 117 } else { 118 assert false : "Illegal conversion " + this + " -> " + to; 119 } 120 121 return to; 122 } 123 124 @Override 125 public Type add(final MethodVisitor method, final int programPoint) { 126 throw new UnsupportedOperationException("add"); 127 } 128 129 @Override 130 public void _return(final MethodVisitor method) { 131 method.visitInsn(LRETURN); 132 } 133 134 @Override 135 public Type loadUndefined(final MethodVisitor method) { 136 method.visitLdcInsn(UNDEFINED_LONG); 137 return LONG; 138 } 139 140 @Override 141 public Type loadForcedInitializer(final MethodVisitor method) { 142 method.visitInsn(LCONST_0); 143 return LONG; 144 } 145} 146