001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.codegen.intrinsics;
018    
019    import com.google.common.collect.ImmutableList;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor;
023    import org.jetbrains.jet.lang.resolve.CompileTimeConstantUtils;
024    import org.jetbrains.jet.lang.resolve.DescriptorUtils;
025    import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
026    import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
027    import org.jetbrains.jet.lang.resolve.name.Name;
028    import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
029    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
030    import org.jetbrains.jet.lang.types.lang.PrimitiveType;
031    
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    import static org.jetbrains.jet.lang.types.lang.KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME;
036    import static org.jetbrains.org.objectweb.asm.Opcodes.*;
037    
038    public class IntrinsicMethods {
039        private static final IntrinsicMethod UNARY_MINUS = new UnaryMinus();
040        private static final IntrinsicMethod UNARY_PLUS = new UnaryPlus();
041        private static final IntrinsicMethod NUMBER_CAST = new NumberCast();
042        private static final IntrinsicMethod INV = new Inv();
043        private static final IntrinsicMethod RANGE_TO = new RangeTo();
044        private static final IntrinsicMethod INC = new Increment(1);
045        private static final IntrinsicMethod DEC = new Increment(-1);
046        private static final IntrinsicMethod HASH_CODE = new HashCode();
047    
048        private static final IntrinsicMethod ARRAY_SIZE = new ArraySize();
049        private static final IntrinsicMethod ARRAY_INDICES = new ArrayIndices();
050        private static final Equals EQUALS = new Equals();
051        private static final IdentityEquals IDENTITY_EQUALS = new IdentityEquals();
052        private static final IteratorNext ITERATOR_NEXT = new IteratorNext();
053        private static final ArraySet ARRAY_SET = new ArraySet();
054        private static final ArrayGet ARRAY_GET = new ArrayGet();
055        private static final StringPlus STRING_PLUS = new StringPlus();
056        private static final ToString TO_STRING = new ToString();
057        private static final Clone CLONE = new Clone();
058    
059        private static final FqNameUnsafe KOTLIN_ANY_FQ_NAME = DescriptorUtils.getFqName(KotlinBuiltIns.getInstance().getAny());
060        private static final FqNameUnsafe KOTLIN_STRING_FQ_NAME = DescriptorUtils.getFqName(KotlinBuiltIns.getInstance().getString());
061    
062        private final Map<String, IntrinsicMethod> namedMethods = new HashMap<String, IntrinsicMethod>();
063        private static final IntrinsicMethod ARRAY_ITERATOR = new ArrayIterator();
064        private final IntrinsicsMap intrinsicsMap = new IntrinsicsMap();
065    
066        public IntrinsicMethods() {
067            namedMethods.put("kotlin.javaClass.function", new JavaClassFunction());
068            namedMethods.put("kotlin.javaClass.property", new JavaClassProperty());
069            namedMethods.put("kotlin.arrays.array", new JavaClassArray());
070            namedMethods.put("kotlin.collections.copyToArray", new CopyToArray());
071            namedMethods.put("kotlin.jvm.internal.unsafe.monitorEnter", MonitorInstruction.MONITOR_ENTER);
072            namedMethods.put("kotlin.jvm.internal.unsafe.monitorExit", MonitorInstruction.MONITOR_EXIT);
073    
074            ImmutableList<Name> primitiveCastMethods = OperatorConventions.NUMBER_CONVERSIONS.asList();
075            for (Name method : primitiveCastMethods) {
076                String methodName = method.asString();
077                declareIntrinsicFunction("Number", methodName, 0, NUMBER_CAST);
078                for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) {
079                    declareIntrinsicFunction(type.getTypeName().asString(), methodName, 0, NUMBER_CAST);
080                }
081            }
082    
083            for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) {
084                String typeName = type.getTypeName().asString();
085                declareIntrinsicFunction(typeName, "plus", 0, UNARY_PLUS);
086                declareIntrinsicFunction(typeName, "minus", 0, UNARY_MINUS);
087                declareIntrinsicFunction(typeName, "inv", 0, INV);
088                declareIntrinsicFunction(typeName, "rangeTo", 1, RANGE_TO);
089                declareIntrinsicFunction(typeName, "inc", 0, INC);
090                declareIntrinsicFunction(typeName, "dec", 0, DEC);
091            }
092    
093            for (PrimitiveType type : PrimitiveType.values()) {
094                String typeName = type.getTypeName().asString();
095                declareIntrinsicFunction(typeName, "equals", 1, EQUALS);
096                declareIntrinsicFunction(typeName, "hashCode", 0, HASH_CODE);
097                declareIntrinsicFunction(typeName, "toString", 0, TO_STRING);
098            }
099    
100            declareBinaryOp("plus", IADD);
101            declareBinaryOp("minus", ISUB);
102            declareBinaryOp("times", IMUL);
103            declareBinaryOp("div", IDIV);
104            declareBinaryOp("mod", IREM);
105            declareBinaryOp("shl", ISHL);
106            declareBinaryOp("shr", ISHR);
107            declareBinaryOp("ushr", IUSHR);
108            declareBinaryOp("and", IAND);
109            declareBinaryOp("or", IOR);
110            declareBinaryOp("xor", IXOR);
111    
112            declareIntrinsicFunction("Boolean", "not", 0, new Not());
113    
114            declareIntrinsicFunction("String", "plus", 1, new Concat());
115            declareIntrinsicFunction("CharSequence", "get", 1, new StringGetChar());
116            declareIntrinsicFunction("String", "get", 1, new StringGetChar());
117    
118            declareIntrinsicFunction("Cloneable", "clone", 0, CLONE);
119    
120            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "toString", 0, TO_STRING);
121            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "equals", 1, EQUALS);
122            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "identityEquals", 1, IDENTITY_EQUALS);
123            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_STRING_FQ_NAME, "plus", 1, STRING_PLUS);
124            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, null, "arrayOfNulls", 1, new NewArray());
125    
126            for (PrimitiveType type : PrimitiveType.values()) {
127                String typeName = type.getTypeName().asString();
128                declareIntrinsicFunction(typeName, "compareTo", 1, new CompareTo());
129                declareIntrinsicFunction(typeName + "Iterator", "next", 0, ITERATOR_NEXT);
130            }
131    
132            declareIntrinsicProperty("CharSequence", "length", new StringLength());
133            declareIntrinsicProperty("String", "length", new StringLength());
134    
135            declareArrayMethods();
136        }
137    
138        private void declareArrayMethods() {
139            for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
140                declareArrayMethodsForPrimitive(jvmPrimitiveType);
141            }
142    
143            declareIntrinsicProperty("Array", "size", ARRAY_SIZE);
144            declareIntrinsicProperty("Array", "indices", ARRAY_INDICES);
145            declareIntrinsicFunction("Array", "set", 2, ARRAY_SET);
146            declareIntrinsicFunction("Array", "get", 1, ARRAY_GET);
147            declareIntrinsicFunction("Array", "clone", 0, CLONE);
148            declareIterator("Array");
149        }
150    
151        private void declareArrayMethodsForPrimitive(@NotNull JvmPrimitiveType jvmPrimitiveType) {
152            String arrayTypeName = jvmPrimitiveType.getPrimitiveType().getArrayTypeName().asString();
153            declareIntrinsicProperty(arrayTypeName, "size", ARRAY_SIZE);
154            declareIntrinsicProperty(arrayTypeName, "indices", ARRAY_INDICES);
155            declareIntrinsicFunction(arrayTypeName, "set", 2, ARRAY_SET);
156            declareIntrinsicFunction(arrayTypeName, "get", 1, ARRAY_GET);
157            declareIntrinsicFunction(arrayTypeName, "clone", 0, CLONE);
158            declareIterator(arrayTypeName);
159        }
160    
161        private void declareIterator(@NotNull String arrayClassName) {
162            declareIntrinsicFunction(arrayClassName, "iterator", 0, ARRAY_ITERATOR);
163        }
164    
165        private void declareBinaryOp(@NotNull String methodName, int opcode) {
166            BinaryOp op = new BinaryOp(opcode);
167            for (PrimitiveType type : PrimitiveType.values()) {
168                declareIntrinsicFunction(type.getTypeName().asString(), methodName, 1, op);
169            }
170        }
171    
172        private void declareIntrinsicProperty(@NotNull String className, @NotNull String methodName, @NotNull IntrinsicMethod implementation) {
173            declareIntrinsicFunction(className, methodName, -1, implementation);
174        }
175    
176        private void declareIntrinsicFunction(
177                @NotNull String className,
178                @NotNull String methodName,
179                int arity,
180                @NotNull IntrinsicMethod implementation
181        ) {
182            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier(className)),
183                                            null, methodName, arity, implementation);
184        }
185    
186        @Nullable
187        public IntrinsicMethod getIntrinsic(@NotNull CallableMemberDescriptor descriptor) {
188            IntrinsicMethod intrinsicMethod = intrinsicsMap.getIntrinsic(descriptor);
189            if (intrinsicMethod != null) {
190                return intrinsicMethod;
191            }
192    
193            String value = CompileTimeConstantUtils.getIntrinsicAnnotationArgument(descriptor);
194            if (value != null) {
195                return namedMethods.get(value);
196            }
197    
198            return null;
199        }
200    }