BooleanType.java revision 1068:34ef988d5959
1/* 2 * Copyright (c) 2010, 2013, 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 26/* 27 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 28 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 29 * 30 * This code is free software; you can redistribute it and/or modify it 31 * under the terms of the GNU General Public License version 2 only, as 32 * published by the Free Software Foundation. Oracle designates this 33 * particular file as subject to the "Classpath" exception as provided 34 * by Oracle in the LICENSE file that accompanied this code. 35 * 36 * This code is distributed in the hope that it will be useful, but WITHOUT 37 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 39 * version 2 for more details (a copy is included in the LICENSE file that 40 * accompanied this code). 41 * 42 * You should have received a copy of the GNU General Public License version 43 * 2 along with this work; if not, write to the Free Software Foundation, 44 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 45 * 46 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 47 * or visit www.oracle.com if you need additional information or have any 48 * questions. 49 */ 50 51package jdk.nashorn.internal.codegen.types; 52 53import static jdk.internal.org.objectweb.asm.Opcodes.I2D; 54import static jdk.internal.org.objectweb.asm.Opcodes.I2L; 55import static jdk.internal.org.objectweb.asm.Opcodes.IADD; 56import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_0; 57import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1; 58import static jdk.internal.org.objectweb.asm.Opcodes.ILOAD; 59import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN; 60import static jdk.internal.org.objectweb.asm.Opcodes.ISTORE; 61import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; 62import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT; 63import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT; 64 65import jdk.internal.org.objectweb.asm.MethodVisitor; 66import jdk.nashorn.internal.codegen.CompilerConstants; 67 68/** 69 * The boolean type class 70 */ 71public final class BooleanType extends Type { 72 private static final long serialVersionUID = 1L; 73 74 private static final CompilerConstants.Call VALUE_OF = staticCallNoLookup(Boolean.class, "valueOf", Boolean.class, boolean.class); 75 private static final CompilerConstants.Call TO_STRING = staticCallNoLookup(Boolean.class, "toString", String.class, boolean.class); 76 77 /** 78 * Constructor 79 */ 80 protected BooleanType() { 81 super("boolean", boolean.class, 1, 1); 82 } 83 84 @Override 85 public Type nextWider() { 86 return INT; 87 } 88 89 @Override 90 public Class<?> getBoxedType() { 91 return Boolean.class; 92 } 93 94 @Override 95 public char getBytecodeStackType() { 96 return 'I'; 97 } 98 99 @Override 100 public Type loadUndefined(final MethodVisitor method) { 101 method.visitLdcInsn(UNDEFINED_INT); 102 return BOOLEAN; 103 } 104 105 @Override 106 public Type loadForcedInitializer(final MethodVisitor method) { 107 method.visitInsn(ICONST_0); 108 return BOOLEAN; 109 } 110 111 @Override 112 public void _return(final MethodVisitor method) { 113 method.visitInsn(IRETURN); 114 } 115 116 @Override 117 public Type load(final MethodVisitor method, final int slot) { 118 assert slot != -1; 119 method.visitVarInsn(ILOAD, slot); 120 return BOOLEAN; 121 } 122 123 @Override 124 public void store(final MethodVisitor method, final int slot) { 125 assert slot != -1; 126 method.visitVarInsn(ISTORE, slot); 127 } 128 129 @Override 130 public Type ldc(final MethodVisitor method, final Object c) { 131 assert c instanceof Boolean; 132 method.visitInsn((Boolean) c ? ICONST_1 : ICONST_0); 133 return BOOLEAN; 134 } 135 136 @Override 137 public Type convert(final MethodVisitor method, final Type to) { 138 if (isEquivalentTo(to)) { 139 return to; 140 } 141 142 if (to.isNumber()) { 143 method.visitInsn(I2D); 144 } else if (to.isLong()) { 145 method.visitInsn(I2L); 146 } else if (to.isInteger()) { 147 //nop 148 } else if (to.isString()) { 149 invokestatic(method, TO_STRING); 150 } else if (to.isObject()) { 151 invokestatic(method, VALUE_OF); 152 } else { 153 throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to); 154 } 155 156 return to; 157 } 158 159 @Override 160 public Type add(final MethodVisitor method, final int programPoint) { 161 // Adding booleans in JavaScript is perfectly valid, they add as if false=0 and true=1 162 if(programPoint == INVALID_PROGRAM_POINT) { 163 method.visitInsn(IADD); 164 } else { 165 method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint); 166 } 167 return INT; 168 } 169} 170