KT-737 compareTo() intrinsic

This commit is contained in:
Alex Tkachman
2011-12-03 20:51:56 +02:00
parent 90367ab138
commit 1135c8ce7a
5 changed files with 68 additions and 1 deletions
@@ -320,7 +320,8 @@ public class JetTypeMapper {
ClassDescriptor containingClass = (ClassDescriptor) functionParent;
boolean isInterface = CodegenUtil.isInterface(containingClass);
OwnerKind kind1 = isInterface && superCall ? OwnerKind.TRAIT_IMPL : OwnerKind.IMPLEMENTATION;
owner = mapType(containingClass.getDefaultType(), kind1).getInternalName();
Type type = mapType(containingClass.getDefaultType(), kind1);
owner = type.getInternalName();
invokeOpcode = isInterface
? (superCall ? Opcodes.INVOKESTATIC : Opcodes.INVOKEINTERFACE)
: (superCall ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL);
@@ -0,0 +1,45 @@
package org.jetbrains.jet.codegen.intrinsics;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
import java.util.List;
/**
* @author alex.tkachman
*/
public class CompareTo implements IntrinsicMethod {
@Override
public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, @Nullable PsiElement element, @Nullable List<JetExpression> arguments, StackValue receiver) {
assert arguments != null;
receiver.put(receiver.type, v);
codegen.gen(arguments.get(0), receiver.type);
if(receiver.type == Type.BYTE_TYPE || receiver.type == Type.SHORT_TYPE || receiver.type == Type.CHAR_TYPE)
v.sub(Type.INT_TYPE);
else if(receiver.type == Type.INT_TYPE) {
v.invokestatic("jet/runtime/Intrinsics", "compare", "(II)I");
}
else if(receiver.type == Type.BOOLEAN_TYPE) {
v.invokestatic("jet/runtime/Intrinsics", "compare", "(ZZ)I");
}
else if(receiver.type == Type.LONG_TYPE) {
v.invokestatic("jet/runtime/Intrinsics", "compare", "(JJ)I");
}
else if(receiver.type == Type.FLOAT_TYPE) {
v.invokestatic("java/lang/Float", "compare", "(FF)I");
}
else if(receiver.type == Type.DOUBLE_TYPE) {
v.invokestatic("java/lang/Double", "compare", "(DD)I");
}
else {
throw new UnsupportedOperationException();
}
return StackValue.onStack(Type.INT_TYPE);
}
}
@@ -97,6 +97,9 @@ public class IntrinsicMethods {
declareIntrinsicFunction("FloatIterator", "next", 0, ITERATOR_NEXT);
declareIntrinsicFunction("DoubleIterator", "next", 0, ITERATOR_NEXT);
for (String type : PRIMITIVE_NUMBER_TYPES) {
declareIntrinsicFunction(type, "compareTo", 1, new CompareTo());
}
// declareIntrinsicFunction("Any", "equals", 1, new Equals());
//
declareIntrinsicStringMethods();
@@ -315,6 +315,12 @@ public class PrimitiveTypesTest extends CodegenTestCase {
assertTrue(generateToText().contains("IFNULL"));
}
public void testKt737() throws Exception {
loadText("fun box() = if(3.compareTo(2) != 1) \"fail\" else if(5.byt.compareTo(10.lng) >= 0) \"fail\" else \"OK\"");
System.out.println(generateToText());
assertEquals("OK", blackBox());
}
public void testKt665() throws Exception {
loadText("fun f(x: Long, zzz: Long = 1): Long\n" +
"{\n" +
+12
View File
@@ -17,6 +17,18 @@ public class Intrinsics {
throw new JetNullPointerException();
}
public static int compare(long thisVal, long anotherVal) {
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
public static int compare(int thisVal, int anotherVal) {
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
public static int compare(boolean thisVal, boolean anotherVal) {
return (thisVal == anotherVal ? 0 : (anotherVal ? 1 : -1));
}
private static Throwable sanitizeStackTrace(Throwable throwable) {
StackTraceElement[] stackTrace = throwable.getStackTrace();
ArrayList<StackTraceElement> list = new ArrayList<StackTraceElement>();