BooleanType.java revision 953:221a84ef44c0
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
73    private static final CompilerConstants.Call VALUE_OF = staticCallNoLookup(Boolean.class, "valueOf", Boolean.class, boolean.class);
74    private static final CompilerConstants.Call TO_STRING = staticCallNoLookup(Boolean.class, "toString", String.class, boolean.class);
75
76    /**
77     * Constructor
78     */
79    protected BooleanType() {
80        super("boolean", boolean.class, 1, 1);
81    }
82
83    @Override
84    public Type nextWider() {
85        return INT;
86    }
87
88    @Override
89    public Class<?> getBoxedType() {
90        return Boolean.class;
91    }
92
93    @Override
94    public char getBytecodeStackType() {
95        return 'I';
96    }
97
98    @Override
99    public Type loadUndefined(final MethodVisitor method) {
100        method.visitLdcInsn(UNDEFINED_INT);
101        return BOOLEAN;
102    }
103
104    @Override
105    public Type loadForcedInitializer(final MethodVisitor method) {
106        method.visitInsn(ICONST_0);
107        return BOOLEAN;
108    }
109
110    @Override
111    public void _return(final MethodVisitor method) {
112        method.visitInsn(IRETURN);
113    }
114
115    @Override
116    public Type load(final MethodVisitor method, final int slot) {
117        assert slot != -1;
118        method.visitVarInsn(ILOAD, slot);
119        return BOOLEAN;
120    }
121
122    @Override
123    public void store(final MethodVisitor method, final int slot) {
124        assert slot != -1;
125        method.visitVarInsn(ISTORE, slot);
126    }
127
128    @Override
129    public Type ldc(final MethodVisitor method, final Object c) {
130        assert c instanceof Boolean;
131        method.visitInsn((Boolean) c ? ICONST_1 : ICONST_0);
132        return BOOLEAN;
133    }
134
135    @Override
136    public Type convert(final MethodVisitor method, final Type to) {
137        if (isEquivalentTo(to)) {
138            return to;
139        }
140
141        if (to.isNumber()) {
142            method.visitInsn(I2D);
143        } else if (to.isLong()) {
144            method.visitInsn(I2L);
145        } else if (to.isInteger()) {
146            //nop
147        } else if (to.isString()) {
148            invokestatic(method, TO_STRING);
149        } else if (to.isObject()) {
150            invokestatic(method, VALUE_OF);
151        } else {
152            throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to);
153        }
154
155        return to;
156    }
157
158    @Override
159    public Type add(final MethodVisitor method, final int programPoint) {
160        // Adding booleans in JavaScript is perfectly valid, they add as if false=0 and true=1
161        if(programPoint == INVALID_PROGRAM_POINT) {
162            method.visitInsn(IADD);
163        } else {
164            method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint);
165        }
166        return INT;
167    }
168}
169