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        public static final String INTRINSICS_CLASS_NAME = "kotlin/jvm/internal/Intrinsics";
040    
041        private static final IntrinsicMethod UNARY_MINUS = new UnaryMinus();
042        private static final IntrinsicMethod UNARY_PLUS = new UnaryPlus();
043        private static final IntrinsicMethod NUMBER_CAST = new NumberCast();
044        private static final IntrinsicMethod INV = new Inv();
045        private static final IntrinsicMethod RANGE_TO = new RangeTo();
046        private static final IntrinsicMethod INC = new Increment(1);
047        private static final IntrinsicMethod DEC = new Increment(-1);
048        private static final IntrinsicMethod HASH_CODE = new HashCode();
049    
050        private static final IntrinsicMethod ARRAY_SIZE = new ArraySize();
051        private static final Equals EQUALS = new Equals();
052        private static final IdentityEquals IDENTITY_EQUALS = new IdentityEquals();
053        private static final IteratorNext ITERATOR_NEXT = new IteratorNext();
054        private static final ArraySet ARRAY_SET = new ArraySet();
055        private static final ArrayGet ARRAY_GET = new ArrayGet();
056        private static final StringPlus STRING_PLUS = new StringPlus();
057        private static final ToString TO_STRING = new ToString();
058        private static final Clone CLONE = new Clone();
059    
060        private static final FqNameUnsafe KOTLIN_ANY_FQ_NAME = DescriptorUtils.getFqName(KotlinBuiltIns.getInstance().getAny());
061        private static final FqNameUnsafe KOTLIN_STRING_FQ_NAME = DescriptorUtils.getFqName(KotlinBuiltIns.getInstance().getString());
062    
063        private final Map<String, IntrinsicMethod> namedMethods = new HashMap<String, IntrinsicMethod>();
064        private static final IntrinsicMethod ARRAY_ITERATOR = new ArrayIterator();
065        private final IntrinsicsMap intrinsicsMap = new IntrinsicsMap();
066    
067        public IntrinsicMethods() {
068            namedMethods.put("kotlin.javaClass.function", new JavaClassFunction());
069            namedMethods.put("kotlin.javaClass.property", new JavaClassProperty());
070            namedMethods.put("kotlin.arrays.array", new JavaClassArray());
071            namedMethods.put("kotlin.collections.copyToArray", new CopyToArray());
072            namedMethods.put("kotlin.jvm.internal.unsafe.monitorEnter", MonitorInstruction.MONITOR_ENTER);
073            namedMethods.put("kotlin.jvm.internal.unsafe.monitorExit", MonitorInstruction.MONITOR_EXIT);
074    
075            ImmutableList<Name> primitiveCastMethods = OperatorConventions.NUMBER_CONVERSIONS.asList();
076            for (Name method : primitiveCastMethods) {
077                String methodName = method.asString();
078                declareIntrinsicFunction("Number", methodName, 0, NUMBER_CAST);
079                for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) {
080                    declareIntrinsicFunction(type.getTypeName().asString(), methodName, 0, NUMBER_CAST);
081                }
082            }
083    
084            for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) {
085                String typeName = type.getTypeName().asString();
086                declareIntrinsicFunction(typeName, "plus", 0, UNARY_PLUS);
087                declareIntrinsicFunction(typeName, "minus", 0, UNARY_MINUS);
088                declareIntrinsicFunction(typeName, "inv", 0, INV);
089                declareIntrinsicFunction(typeName, "rangeTo", 1, RANGE_TO);
090                declareIntrinsicFunction(typeName, "inc", 0, INC);
091                declareIntrinsicFunction(typeName, "dec", 0, DEC);
092            }
093    
094            for (PrimitiveType type : PrimitiveType.values()) {
095                String typeName = type.getTypeName().asString();
096                declareIntrinsicFunction(typeName, "equals", 1, EQUALS);
097                declareIntrinsicFunction(typeName, "hashCode", 0, HASH_CODE);
098                declareIntrinsicFunction(typeName, "toString", 0, TO_STRING);
099            }
100    
101            declareBinaryOp("plus", IADD);
102            declareBinaryOp("minus", ISUB);
103            declareBinaryOp("times", IMUL);
104            declareBinaryOp("div", IDIV);
105            declareBinaryOp("mod", IREM);
106            declareBinaryOp("shl", ISHL);
107            declareBinaryOp("shr", ISHR);
108            declareBinaryOp("ushr", IUSHR);
109            declareBinaryOp("and", IAND);
110            declareBinaryOp("or", IOR);
111            declareBinaryOp("xor", IXOR);
112    
113            declareIntrinsicFunction("Boolean", "not", 0, new Not());
114    
115            declareIntrinsicFunction("String", "plus", 1, new Concat());
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            declareArrayMethods();
133        }
134    
135        private void declareArrayMethods() {
136            for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
137                declareArrayMethodsForPrimitive(jvmPrimitiveType);
138            }
139    
140            declareIntrinsicFunction("Array", "size", 0, ARRAY_SIZE);
141            declareIntrinsicFunction("Array", "set", 2, ARRAY_SET);
142            declareIntrinsicFunction("Array", "get", 1, ARRAY_GET);
143            declareIntrinsicFunction("Array", "clone", 0, CLONE);
144            declareIterator("Array");
145        }
146    
147        private void declareArrayMethodsForPrimitive(@NotNull JvmPrimitiveType jvmPrimitiveType) {
148            String arrayTypeName = jvmPrimitiveType.getPrimitiveType().getArrayTypeName().asString();
149            declareIntrinsicFunction(arrayTypeName, "size", 0, ARRAY_SIZE);
150            declareIntrinsicFunction(arrayTypeName, "set", 2, ARRAY_SET);
151            declareIntrinsicFunction(arrayTypeName, "get", 1, ARRAY_GET);
152            declareIntrinsicFunction(arrayTypeName, "clone", 0, CLONE);
153            declareIterator(arrayTypeName);
154        }
155    
156        private void declareIterator(@NotNull String arrayClassName) {
157            declareIntrinsicFunction(arrayClassName, "iterator", 0, ARRAY_ITERATOR);
158        }
159    
160        private void declareBinaryOp(@NotNull String methodName, int opcode) {
161            BinaryOp op = new BinaryOp(opcode);
162            for (PrimitiveType type : PrimitiveType.values()) {
163                declareIntrinsicFunction(type.getTypeName().asString(), methodName, 1, op);
164            }
165        }
166    
167        private void declareIntrinsicFunction(
168                @NotNull String className,
169                @NotNull String methodName,
170                int arity,
171                @NotNull IntrinsicMethod implementation
172        ) {
173            intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier(className)),
174                                            null, methodName, arity, implementation);
175        }
176    
177        @Nullable
178        public IntrinsicMethod getIntrinsic(@NotNull CallableMemberDescriptor descriptor) {
179            IntrinsicMethod intrinsicMethod = intrinsicsMap.getIntrinsic(descriptor);
180            if (intrinsicMethod != null) {
181                return intrinsicMethod;
182            }
183    
184            String value = CompileTimeConstantUtils.getIntrinsicAnnotationArgument(descriptor);
185            if (value != null) {
186                return namedMethods.get(value);
187            }
188    
189            return null;
190        }
191    }