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("CharSequence", "get", 1, new StringGetChar()); 117 declareIntrinsicFunction("String", "get", 1, new StringGetChar()); 118 119 declareIntrinsicFunction("Cloneable", "clone", 0, CLONE); 120 121 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "toString", 0, TO_STRING); 122 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "equals", 1, EQUALS); 123 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_ANY_FQ_NAME, "identityEquals", 1, IDENTITY_EQUALS); 124 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, KOTLIN_STRING_FQ_NAME, "plus", 1, STRING_PLUS); 125 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, null, "arrayOfNulls", 1, new NewArray()); 126 127 for (PrimitiveType type : PrimitiveType.values()) { 128 String typeName = type.getTypeName().asString(); 129 declareIntrinsicFunction(typeName, "compareTo", 1, new CompareTo()); 130 declareIntrinsicFunction(typeName + "Iterator", "next", 0, ITERATOR_NEXT); 131 } 132 133 declareIntrinsicProperty("CharSequence", "length", new StringLength()); 134 declareIntrinsicProperty("String", "length", new StringLength()); 135 136 declareArrayMethods(); 137 } 138 139 private void declareArrayMethods() { 140 for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) { 141 declareArrayMethodsForPrimitive(jvmPrimitiveType); 142 } 143 144 declareIntrinsicFunction("Array", "size", 0, ARRAY_SIZE); 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 declareIntrinsicFunction(arrayTypeName, "size", 0, ARRAY_SIZE); 154 declareIntrinsicFunction(arrayTypeName, "set", 2, ARRAY_SET); 155 declareIntrinsicFunction(arrayTypeName, "get", 1, ARRAY_GET); 156 declareIntrinsicFunction(arrayTypeName, "clone", 0, CLONE); 157 declareIterator(arrayTypeName); 158 } 159 160 private void declareIterator(@NotNull String arrayClassName) { 161 declareIntrinsicFunction(arrayClassName, "iterator", 0, ARRAY_ITERATOR); 162 } 163 164 private void declareBinaryOp(@NotNull String methodName, int opcode) { 165 BinaryOp op = new BinaryOp(opcode); 166 for (PrimitiveType type : PrimitiveType.values()) { 167 declareIntrinsicFunction(type.getTypeName().asString(), methodName, 1, op); 168 } 169 } 170 171 private void declareIntrinsicProperty(@NotNull String className, @NotNull String methodName, @NotNull IntrinsicMethod implementation) { 172 declareIntrinsicFunction(className, methodName, -1, implementation); 173 } 174 175 private void declareIntrinsicFunction( 176 @NotNull String className, 177 @NotNull String methodName, 178 int arity, 179 @NotNull IntrinsicMethod implementation 180 ) { 181 intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier(className)), 182 null, methodName, arity, implementation); 183 } 184 185 @Nullable 186 public IntrinsicMethod getIntrinsic(@NotNull CallableMemberDescriptor descriptor) { 187 IntrinsicMethod intrinsicMethod = intrinsicsMap.getIntrinsic(descriptor); 188 if (intrinsicMethod != null) { 189 return intrinsicMethod; 190 } 191 192 String value = CompileTimeConstantUtils.getIntrinsicAnnotationArgument(descriptor); 193 if (value != null) { 194 return namedMethods.get(value); 195 } 196 197 return null; 198 } 199 }