Skip to content

Commit

Permalink
Fixed proper boxing and addition
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 17, 2016
1 parent 0070d7b commit 6245904
Show file tree
Hide file tree
Showing 19 changed files with 548 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class BaseExpressionVisitor implements ExpressionVisitor
@Override
public void invoke( Expression target, MethodReference method, Expression[] arguments )
{

target.accept( this );
}

@Override
Expand Down Expand Up @@ -97,7 +97,19 @@ public void or( Expression lhs, Expression rhs )
}

@Override
public void add( Expression lhs, Expression rhs )
public void addInts( Expression lhs, Expression rhs )
{

}

@Override
public void addLongs( Expression lhs, Expression rhs )
{

}

@Override
public void addDoubles( Expression lhs, Expression rhs )
{

}
Expand Down Expand Up @@ -125,4 +137,10 @@ public void newArray( TypeReference type, Expression... constants )
{

}

@Override
public void longToDouble( Expression expression )
{

}
}
40 changes: 38 additions & 2 deletions community/codegen/src/main/java/org/neo4j/codegen/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,38 @@ public void accept( ExpressionVisitor visitor )
};
}

public static Expression add( final Expression lhs, final Expression rhs )
public static Expression addInts( final Expression lhs, final Expression rhs )
{
return new Expression()
{
@Override
public void accept( ExpressionVisitor visitor )
{
visitor.add( lhs, rhs );
visitor.addInts( lhs, rhs );
}
};
}

public static Expression addLongs( final Expression lhs, final Expression rhs )
{
return new Expression()
{
@Override
public void accept( ExpressionVisitor visitor )
{
visitor.addLongs( lhs, rhs );
}
};
}

public static Expression addDoubles( final Expression lhs, final Expression rhs )
{
return new Expression()
{
@Override
public void accept( ExpressionVisitor visitor )
{
visitor.addDoubles( lhs, rhs );
}
};
}
Expand Down Expand Up @@ -248,6 +272,18 @@ public void accept( ExpressionVisitor visitor )
};
}

public static Expression toDouble( final Expression expression )
{
return new Expression()
{
@Override
public void accept( ExpressionVisitor visitor )
{
visitor.longToDouble( expression );
}
};
}

@Override
Expression materialize( CodeBlock method )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,25 @@ public void or( Expression lhs, Expression rhs )
rhs.accept( this );
result.append( ")" );
}
@Override
public void addInts( Expression lhs, Expression rhs )
{
add(lhs, rhs);
}

@Override
public void addLongs( Expression lhs, Expression rhs )
{
add(lhs, rhs);
}

@Override
public void add( Expression lhs, Expression rhs )
public void addDoubles( Expression lhs, Expression rhs )
{
add(lhs, rhs);
}

private void add( Expression lhs, Expression rhs )
{
result.append( "add(" );
lhs.accept( this );
Expand Down Expand Up @@ -207,4 +223,11 @@ public void newArray( TypeReference type, Expression... constants )
}
result.append( "}" );
}

@Override
public void longToDouble( Expression expression )
{
result.append( "(double)" );
expression.accept( this );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public interface ExpressionVisitor

void or( Expression lhs, Expression rhs );

void add( Expression lhs, Expression rhs );
void addInts( Expression lhs, Expression rhs );

void addLongs( Expression lhs, Expression rhs );

void addDoubles( Expression lhs, Expression rhs );

void gt( Expression lhs, Expression rhs );

Expand All @@ -54,4 +58,6 @@ public interface ExpressionVisitor
void cast( TypeReference type, Expression expression );

void newArray( TypeReference type, Expression... constants );

void longToDouble( Expression expression );
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,20 @@ public void constant( Object value )
else if ( value instanceof Integer )
{
pushInteger( (Integer) value );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false );
}
else if ( value instanceof Byte )
{
pushInteger( (Byte) value );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false );
}
else if ( value instanceof Short )
{
pushInteger( (Short) value );
//methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false );
// .visitMethodInsn( INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false );
}
else if ( value instanceof Long )
{
Expand All @@ -153,19 +153,21 @@ else if ( value instanceof Long )
else if ( value instanceof Double )
{
methodVisitor.visitLdcInsn( value );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false );
// methodVisitor
// .visitMethodInsn( INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false );
}
else if ( value instanceof Float )
{
methodVisitor.visitLdcInsn( value );
// methodVisitor.visitMethodInsn( INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false );
// methodVisitor.visitMethodInsn( INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;",
// false );
}
else if ( value instanceof Boolean )
{
boolean b = (boolean) value;
methodVisitor.visitInsn( b ? ICONST_1 : ICONST_0 );
// methodVisitor.visitMethodInsn( INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false );
// methodVisitor.visitMethodInsn( INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;
// ", false );
}
else
{
Expand Down Expand Up @@ -224,8 +226,8 @@ public void ternary( Expression test, Expression onTrue, Expression onFalse )
@Override
public void eq( Expression lhs, Expression rhs )
{
TypeReference lhsType = findLoadType( lhs );
TypeReference rhsType = findLoadType( rhs );
TypeReference lhsType = findType( lhs );
TypeReference rhsType = findType( rhs );

if ( !lhsType.equals( rhsType ) )
{
Expand Down Expand Up @@ -291,48 +293,35 @@ public void or( Expression lhs, Expression rhs )
}

@Override
public void add( Expression lhs, Expression rhs )
public void addInts( Expression lhs, Expression rhs )
{
TypeReference lhsType = findLoadType( lhs );
TypeReference rhsType = findLoadType( rhs );

if ( !lhsType.equals( rhsType ) )
{
throw new IllegalStateException( "Cannot compare values of different types" );
}
lhs.accept( this );
rhs.accept( this );
methodVisitor.visitInsn( IADD );
}

@Override
public void addLongs( Expression lhs, Expression rhs )
{
lhs.accept( this );
rhs.accept( this );
switch ( lhsType.simpleName() )
{
case "int":
case "byte":
case "short":
case "char":
case "boolean":
methodVisitor.visitInsn( IADD );
break;
case "long":
methodVisitor.visitInsn( LADD );
break;
case "float":
methodVisitor.visitInsn( FADD );
break;
case "double":
methodVisitor.visitInsn( DADD );
break;
default:
throw new IllegalStateException( "Addition is only supported for primitive number types" );
}
methodVisitor.visitInsn( LADD );
}

@Override
public void addDoubles( Expression lhs, Expression rhs )
{
lhs.accept( this );
rhs.accept( this );
methodVisitor.visitInsn( DADD );
}

@Override
public void gt( Expression lhs, Expression rhs )
{

TypeReference lhsType = findLoadType( lhs );
TypeReference rhsType = findLoadType( rhs );
TypeReference lhsType = findType( lhs );
TypeReference rhsType = findType( rhs );

if ( !lhsType.equals( rhsType ) )
{
Expand Down Expand Up @@ -365,8 +354,8 @@ public void gt( Expression lhs, Expression rhs )
@Override
public void sub( Expression lhs, Expression rhs )
{
TypeReference lhsType = findLoadType( lhs );
TypeReference rhsType = findLoadType( rhs );
TypeReference lhsType = findType( lhs );
TypeReference rhsType = findType( rhs );

if ( !lhsType.equals( rhsType ) )
{
Expand Down Expand Up @@ -419,6 +408,13 @@ public void newArray( TypeReference type, Expression... exprs )
}
}

@Override
public void longToDouble( Expression expression )
{
expression.accept( this );
methodVisitor.visitInsn( L2D );
}

private void compareIntOrReferenceType( Expression lhs, Expression rhs, int opcode )
{
lhs.accept( this );
Expand Down Expand Up @@ -450,7 +446,7 @@ private void compareLongOrFloatType( Expression lhs, Expression rhs, int opcode,
methodVisitor.visitLabel( l1 );
}

private TypeReference findLoadType( Expression expression )
private TypeReference findType( Expression expression )
{
TypeReference[] typeReference = new TypeReference[]{null};
expression.accept( new BaseExpressionVisitor()
Expand All @@ -460,6 +456,49 @@ public void load( LocalVariable variable )
{
typeReference[0] = variable.type();
}

@Override
public void constant( Object value )
{
//We are representing using unboxed expression whenever possible
if ( value == null )
{
throw new IllegalStateException( "The type of null is none of your business" );
}
else if ( value instanceof Integer )
{
typeReference[0] = TypeReference.typeReference( int.class );
}
else if ( value instanceof Byte )
{
typeReference[0] = TypeReference.typeReference( byte.class );
}
else if ( value instanceof Short )
{
typeReference[0] = TypeReference.typeReference( short.class );
}
else if ( value instanceof Long )
{
typeReference[0] = TypeReference.typeReference( long.class );
}
else if ( value instanceof Double )
{
typeReference[0] = TypeReference.typeReference( double.class );
}
else if ( value instanceof Float )
{
typeReference[0] = TypeReference.typeReference( float.class );
}
else if ( value instanceof Boolean )
{
typeReference[0] = TypeReference.typeReference( boolean.class );
}
else
{
typeReference[0] = TypeReference.typeReference( value.getClass() );
}

}
} );

if ( typeReference[0] == null )
Expand Down Expand Up @@ -556,4 +595,24 @@ private void arrayStore( TypeReference reference )

}
}

private int findAdder( TypeReference type )
{
switch ( type.simpleName() )
{
case "int":
case "byte":
case "short":
case "char":
return IADD;
case "long":
return LADD;
case "float":
return FADD;
case "double":
return DADD;
default:
throw new IllegalStateException( "Addition is only supported for primitive number types" );
}
}
}

0 comments on commit 6245904

Please sign in to comment.