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 package jdk.nashorn.internal.codegen.types;
  27 
  28 import static jdk.internal.org.objectweb.asm.Opcodes.I2D;
  29 import static jdk.internal.org.objectweb.asm.Opcodes.I2L;
  30 import static jdk.internal.org.objectweb.asm.Opcodes.IADD;
  31 import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_0;
  32 import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1;
  33 import static jdk.internal.org.objectweb.asm.Opcodes.ILOAD;
  34 import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN;
  35 import static jdk.internal.org.objectweb.asm.Opcodes.ISTORE;
  36 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
  37 import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT;
  38 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
  39 
  40 import jdk.internal.org.objectweb.asm.MethodVisitor;
  41 import jdk.nashorn.internal.codegen.CompilerConstants;
  42 
  43 /**
  44  * The boolean type class
  45  */
  46 public final class BooleanType extends Type {
  47     private static final long serialVersionUID = 1L;
  48 
  49     private static final CompilerConstants.Call VALUE_OF = staticCallNoLookup(Boolean.class, "valueOf", Boolean.class, boolean.class);
  50     private static final CompilerConstants.Call TO_STRING = staticCallNoLookup(Boolean.class, "toString", String.class, boolean.class);
  51 
  52     /**
  53      * Constructor
  54      */
  55     protected BooleanType() {
  56         super("boolean", boolean.class, 1, 1);
  57     }
  58 
  59     @Override
  60     public Type nextWider() {
  61         return INT;
  62     }
  63 
  64     @Override
  65     public Class<?> getBoxedType() {
  66         return Boolean.class;
  67     }
  68 
  69     @Override
  70     public char getBytecodeStackType() {
  71         return 'I';
  72     }
  73 
  74     @Override
  75     public Type loadUndefined(final MethodVisitor method) {
  76         method.visitLdcInsn(UNDEFINED_INT);
  77         return BOOLEAN;
  78     }
  79 
  80     @Override
  81     public Type loadForcedInitializer(final MethodVisitor method) {
  82         method.visitInsn(ICONST_0);
  83         return BOOLEAN;
  84     }
  85 
  86     @Override
  87     public void _return(final MethodVisitor method) {
  88         method.visitInsn(IRETURN);
  89     }
  90 
  91     @Override
  92     public Type load(final MethodVisitor method, final int slot) {
  93         assert slot != -1;
  94         method.visitVarInsn(ILOAD, slot);
  95         return BOOLEAN;
  96     }
  97 
  98     @Override
  99     public void store(final MethodVisitor method, final int slot) {
 100         assert slot != -1;
 101         method.visitVarInsn(ISTORE, slot);
 102     }
 103 
 104     @Override
 105     public Type ldc(final MethodVisitor method, final Object c) {
 106         assert c instanceof Boolean;
 107         method.visitInsn((Boolean) c ? ICONST_1 : ICONST_0);
 108         return BOOLEAN;
 109     }
 110 
 111     @Override
 112     public Type convert(final MethodVisitor method, final Type to) {
 113         if (isEquivalentTo(to)) {
 114             return to;
 115         }
 116 
 117         if (to.isNumber()) {
 118             method.visitInsn(I2D);
 119         } else if (to.isLong()) {
 120             method.visitInsn(I2L);
 121         } else if (to.isInteger()) {
 122             //nop
 123         } else if (to.isString()) {
 124             invokestatic(method, TO_STRING);
 125         } else if (to.isObject()) {
 126             invokestatic(method, VALUE_OF);
 127         } else {
 128             throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to);
 129         }
 130 
 131         return to;
 132     }
 133 
 134     @Override
 135     public Type add(final MethodVisitor method, final int programPoint) {
 136         // Adding booleans in JavaScript is perfectly valid, they add as if false=0 and true=1
 137         if(programPoint == INVALID_PROGRAM_POINT) {
 138             method.visitInsn(IADD);
 139         } else {
 140             method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint);
 141         }
 142         return INT;
 143     }
 144 }