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