KT-594 Array.get/set intrinsic

This commit is contained in:
Alex Tkachman
2011-11-23 11:56:47 +02:00
parent 8c842a6d5c
commit b8f942857b
6 changed files with 103 additions and 1 deletions
@@ -129,7 +129,7 @@ public class JetTypeMapper {
return type;
}
static Type correctElementType(Type type) {
public static Type correctElementType(Type type) {
String internalName = type.getInternalName();
assert internalName.charAt(0) == '[';
return Type.getType(internalName.substring(1));
@@ -0,0 +1,28 @@
package org.jetbrains.jet.codegen.intrinsics;
import com.intellij.psi.PsiElement;
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 ArrayGet implements IntrinsicMethod {
@Override
public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List<JetExpression> arguments, StackValue receiver) {
receiver.put(JetTypeMapper.TYPE_OBJECT, v);
Type type = JetTypeMapper.correctElementType(receiver.type);
codegen.gen(arguments.get(0), Type.INT_TYPE);
v.aload(type);
return StackValue.onStack(type);
}
}
@@ -0,0 +1,30 @@
package org.jetbrains.jet.codegen.intrinsics;
import com.intellij.psi.PsiElement;
import org.jetbrains.jet.codegen.CodegenUtil;
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 ArraySet implements IntrinsicMethod {
@Override
public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List<JetExpression> arguments, StackValue receiver) {
receiver.put(JetTypeMapper.TYPE_OBJECT, v);
Type type = JetTypeMapper.correctElementType(receiver.type);
codegen.gen(arguments.get(0), Type.INT_TYPE);
codegen.gen(arguments.get(1), type);
v.astore(type);
return StackValue.none();
}
}
@@ -36,6 +36,8 @@ public class IntrinsicMethods {
public static final IntrinsicMethod ARRAY_INDICES = new ArrayIndices();
public static final Equals EQUALS = new Equals();
public static final IteratorNext ITERATOR_NEXT = new IteratorNext();
public static final ArraySet ARRAY_SET = new ArraySet();
public static final ArrayGet ARRAY_GET = new ArrayGet();
private final Project myProject;
private final JetStandardLibrary myStdLib;
@@ -122,6 +124,26 @@ public class IntrinsicMethods {
declareIntrinsicProperty("CharArray", "indices", ARRAY_INDICES);
declareIntrinsicProperty("BooleanArray", "indices", ARRAY_INDICES);
declareIntrinsicFunction("Array", "set", 2, ARRAY_SET);
declareIntrinsicFunction("ByteArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("ShortArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("IntArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("LongArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("FloatArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("DoubleArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("CharArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("BooleanArray", "set", 2, ARRAY_SET);
declareIntrinsicFunction("Array", "get", 1, ARRAY_GET);
declareIntrinsicFunction("ByteArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("ShortArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("IntArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("LongArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("FloatArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("DoubleArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("CharArray", "get", 1, ARRAY_GET);
declareIntrinsicFunction("BooleanArray", "get", 1, ARRAY_GET);
declareIterator(myStdLib.getArray());
declareIterator(myStdLib.getByteArrayClass());
declareIterator(myStdLib.getShortArrayClass());
@@ -0,0 +1,16 @@
namespace array_test
fun box() : String {
var array : IntArray? = IntArray(10)
array?.set(0, 3)
if(array?.get(0) != 3) return "fail"
var a = Array<Array<String?>?>(5)
var b = Array<String?>(1)
b.set(0, "239")
a?.set(0, b)
if(a?.get(0)?.get(0) != "239") return "fail"
return "OK"
}
@@ -275,4 +275,10 @@ public class ArrayGenTest extends CodegenTestCase {
public void testKt503() {
blackBoxFile("regressions/kt503.jet");
}
public void testKt594() throws Exception {
loadFile("regressions/kt594.jet");
System.out.println(generateToText());
blackBox();
}
}