long arithmetics initial

This commit is contained in:
Dmitry Jemerov
2011-04-07 14:13:00 +02:00
parent 66cfb99ce9
commit 561bcee4e2
4 changed files with 32 additions and 14 deletions
@@ -240,7 +240,9 @@ public class ExpressionCodegen extends JetVisitor {
for (JetElement statement : statements) { for (JetElement statement : statements) {
if (statement instanceof JetProperty) { if (statement instanceof JetProperty) {
myMap.enter(bindingContext.getPropertyDescriptor((JetProperty) statement)); final PropertyDescriptor propertyDescriptor = bindingContext.getPropertyDescriptor((JetProperty) statement);
final Type type = typeMapper.mapType(propertyDescriptor.getOutType());
myMap.enter(propertyDescriptor, type.getSize());
} }
} }
@@ -466,7 +468,7 @@ public class ExpressionCodegen extends JetVisitor {
DeclarationDescriptor cls = op.getContainingDeclaration(); DeclarationDescriptor cls = op.getContainingDeclaration();
if (cls instanceof ClassDescriptor) { if (cls instanceof ClassDescriptor) {
final String className = cls.getName(); final String className = cls.getName();
if (className.equals("Int")) { if (className.equals("Int") || className.equals("Long")) {
if (op.getName().equals("compareTo")) { if (op.getName().equals("compareTo")) {
generateCompareOp(expression, opToken); generateCompareOp(expression, opToken);
} }
@@ -510,11 +512,12 @@ public class ExpressionCodegen extends JetVisitor {
private void generateBinaryOp(JetBinaryExpression expression, FunctionDescriptor op, int opcode) { private void generateBinaryOp(JetBinaryExpression expression, FunctionDescriptor op, int opcode) {
JetType returnType = op.getUnsubstitutedReturnType(); JetType returnType = op.getUnsubstitutedReturnType();
if (returnType.equals(stdlib.getIntType())) { final Type asmType = typeMapper.mapType(returnType);
gen(expression.getLeft(), Type.INT_TYPE); if (asmType == Type.INT_TYPE || asmType == Type.LONG_TYPE) {
gen(expression.getRight(), Type.INT_TYPE); gen(expression.getLeft(), asmType);
v.visitInsn(Type.INT_TYPE.getOpcode(opcode)); gen(expression.getRight(), asmType);
myStack.push(StackValue.onStack(Type.INT_TYPE)); v.visitInsn(asmType.getOpcode(opcode));
myStack.push(StackValue.onStack(asmType));
} }
else { else {
throw new UnsupportedOperationException("Don't know how to generate binary op with return type " + returnType); throw new UnsupportedOperationException("Don't know how to generate binary op with return type " + returnType);
@@ -8,14 +8,19 @@ import org.jetbrains.jet.lang.types.DeclarationDescriptor;
*/ */
public class FrameMap { public class FrameMap {
private final TObjectIntHashMap<DeclarationDescriptor> myVarIndex = new TObjectIntHashMap<DeclarationDescriptor>(); private final TObjectIntHashMap<DeclarationDescriptor> myVarIndex = new TObjectIntHashMap<DeclarationDescriptor>();
private final TObjectIntHashMap<DeclarationDescriptor> myVarSizes = new TObjectIntHashMap<DeclarationDescriptor>();
private int myMaxIndex = 0; private int myMaxIndex = 0;
public void enter(DeclarationDescriptor descriptor) { public void enter(DeclarationDescriptor descriptor, int size) {
myVarIndex.put(descriptor, myMaxIndex++); myVarIndex.put(descriptor, myMaxIndex);
myMaxIndex += size;
myVarSizes.put(descriptor, size);
} }
public int leave(DeclarationDescriptor descriptor) { public int leave(DeclarationDescriptor descriptor) {
myMaxIndex--; int size = myVarSizes.get(descriptor);
myMaxIndex -= size;
myVarSizes.remove(descriptor);
return myVarIndex.remove(descriptor); return myVarIndex.remove(descriptor);
} }
@@ -52,8 +52,9 @@ public class FunctionCodegen {
List<ValueParameterDescriptor> parameDescrs = bindingContext.getFunctionDescriptor(f).getUnsubstitutedValueParameters(); List<ValueParameterDescriptor> parameDescrs = bindingContext.getFunctionDescriptor(f).getUnsubstitutedValueParameters();
for (ValueParameterDescriptor parameter : parameDescrs) { for (int i = 0; i < parameDescrs.size(); i++) {
frameMap.enter(parameter); ValueParameterDescriptor parameter = parameDescrs.get(i);
frameMap.enter(parameter, parameterTypes[i].getSize());
} }
ExpressionCodegen codegen = new ExpressionCodegen(mv, bindingContext, standardLibrary, frameMap, typeMapper, returnType); ExpressionCodegen codegen = new ExpressionCodegen(mv, bindingContext, standardLibrary, frameMap, typeMapper, returnType);
@@ -207,11 +207,20 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(10, main.invoke(null, "false")); assertEquals(10, main.invoke(null, "false"));
} }
public void testBoxedInt() throws Exception { public void _testBoxedInt() throws Exception {
loadText("fun foo(a: Int?): Int = if (a != null) a else 239"); loadText("fun foo(a: Int?): Int = if (a != null) a else 239");
final Method main = generateFunction(); final Method main = generateFunction();
assertEquals(610, main.invoke(null, 610)); assertEquals(610, main.invoke(null, 610));
assertEquals(239, main.invoke(null, new Object[] { null })); assertEquals(239, main.invoke(null, new Object[]{null}));
}
public void testLong() throws Exception {
loadText("fun foo(a: Long, b: Long): Long = a + b");
System.out.println(generateToText());
final Method main = generateFunction();
long arg = (long) Integer.MAX_VALUE;
long expected = 2 * (long) Integer.MAX_VALUE;
assertEquals(expected, main.invoke(null, arg, arg));
} }
private void binOpTest(final String text, final int arg1, final int arg2, final int expected) throws Exception { private void binOpTest(final String text, final int arg1, final int arg2, final int expected) throws Exception {