Skip to content

Commit

Permalink
Fixes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 17, 2016
1 parent abf68aa commit 6ccc59b
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 96 deletions.
36 changes: 15 additions & 21 deletions community/codegen/src/main/java/org/neo4j/codegen/CodeBlock.java
Expand Up @@ -87,14 +87,9 @@ protected void endBlock()
}
}

protected void emit( Consumer<MethodEmitter> emitFunction )
{
emitFunction.accept( emitter );
}

public void expression( Expression expression )
{
emit( ( e ) -> e.expression( expression ) );
emitter.expression( expression );
}

LocalVariable local( String name )
Expand All @@ -105,13 +100,13 @@ LocalVariable local( String name )
public LocalVariable declare( TypeReference type, String name )
{
LocalVariable local = localVariables.createNew( type, name );
emit( e -> e.declare( local ) );
emitter.declare( local );
return local;
}

public void assign( LocalVariable local, Expression value )
{
emit( e -> e.assignVariableInScope( local, value ) );
emitter.assignVariableInScope( local, value );
}

public void assign( Class<?> type, String name, Expression value )
Expand All @@ -122,12 +117,12 @@ public void assign( Class<?> type, String name, Expression value )
public void assign( TypeReference type, String name, Expression value )
{
LocalVariable variable = localVariables.createNew( type, name );
emit( e -> e.assign( variable, value ) );
emitter.assign( variable, value );
}

public void put( Expression target, FieldReference field, Expression value )
{
emit( e -> e.put( target, field, value ) );
emitter.put( target, field, value );
}

public Expression self()
Expand Down Expand Up @@ -161,58 +156,57 @@ public CodeBlock forEach( Parameter local, Expression iterable )

public CodeBlock whileLoop( Expression test )
{
emit( e -> e.beginWhile( test ) );
emitter.beginWhile( test );
return new CodeBlock( this );
}

public CodeBlock ifStatement( Expression test )
{
emit( e -> e.beginIf( test ) );
emitter.beginIf( test );
return new CodeBlock( this );
}

public CodeBlock ifNotStatement( Expression test )
{
emit( e -> e.beginIfNot( test ) );
emitter.beginIfNot( test );
return new CodeBlock( this );
}

public CodeBlock ifNullStatement( Expression test )
{
emit( e -> e.beginIfNull( test ) );
emitter.beginIfNull( test );
return new CodeBlock( this );
}

public CodeBlock ifNonNullStatement( Expression test )
{
emit( e -> e.beginIfNonNull( test ) );
emitter.beginIfNonNull( test );
return new CodeBlock( this );
}

public void tryCatch( Consumer<CodeBlock> body, Consumer<CodeBlock> onError, Parameter exception )
{
emit( e -> e.tryCatchBlock( body, onError, localVariables.createNew( exception.type(), exception.name() ),
this ) );
emitter.tryCatchBlock( body, onError, localVariables.createNew( exception.type(), exception.name() ),
this );
}

public void returns()
{
emit( MethodEmitter::returns );
emitter.returns();
}

public void returns( Expression value )
{
emit( e -> e.returns( value ) );
emitter.returns( value );
}

public void throwException( Expression exception )
{
emit( e -> e.throwException( exception ) );
emitter.throwException( exception );
}

public TypeReference owner()
{
return clazz.handle();
}

}
Expand Up @@ -106,7 +106,7 @@ public Builder throwsException( TypeReference type )
return this;
}

public Builder modifiers(int modifiers)
public Builder modifiers( int modifiers )
{
this.modifiers = modifiers;
return this;
Expand Down Expand Up @@ -177,7 +177,7 @@ public boolean isStatic()

public boolean isGeneric()
{
if ( returnType().isGeneric() || typeParameters.length != 0)
if ( returnType().isGeneric() || typeParameters.length != 0 )
{
return true;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ public MethodDeclaration erased()
for ( int i = 0; i < parameters.length; i++ )
{
Parameter parameter = parameters[i];
TypeReference erasedType = erase( parameter.type(), table);
TypeReference erasedType = erase( parameter.type(), table );
newParameters[i] = param( erasedType, parameter.name() );
}
TypeReference[] newExceptions = new TypeReference[exceptions.length];
Expand All @@ -235,29 +235,11 @@ public MethodDeclaration erased()
String newName = name();
boolean newIsConstrucor = isConstructor();

return new MethodDeclaration( owner, newParameters, newExceptions, modifiers, typeParameters )
{
@Override
public boolean isConstructor()
{
return newIsConstrucor;
}

@Override
public TypeReference returnType()
{
return newReturnType;
}

@Override
public String name()
{
return newName;
}
};
return methodDeclaration( owner, newReturnType, newParameters, newExceptions, newName, newIsConstrucor,
modifiers, typeParameters );
}

private TypeReference erase( TypeReference reference, Map<String,TypeReference> table )
private TypeReference erase( TypeReference reference, Map<String,TypeReference> table )
{
TypeReference erasedReference = table.get( reference.name() );

Expand All @@ -267,51 +249,14 @@ private TypeReference erase( TypeReference reference, Map<String,TypeReference>
static MethodDeclaration method( TypeReference owner, final TypeReference returnType, final String name,
Parameter[] parameters, TypeReference[] exceptions, int modifiers, TypeParameter[] typeParameters )
{
return new MethodDeclaration( owner, parameters, exceptions, modifiers, typeParameters )
{
@Override
public boolean isConstructor()
{
return false;
}

@Override
public TypeReference returnType()
{
return returnType;
}

@Override
public String name()
{
return name;
}
};
return methodDeclaration( owner, returnType, parameters, exceptions, name, false, modifiers, typeParameters );
}

static MethodDeclaration constructor( TypeReference owner, Parameter[] parameters, TypeReference[] exceptions,
int modifiers, TypeParameter[] typeParameters )
{
return new MethodDeclaration( owner, parameters, exceptions ,modifiers, typeParameters )
{
@Override
public boolean isConstructor()
{
return true;
}

@Override
public TypeReference returnType()
{
return TypeReference.VOID;
}

@Override
public String name()
{
return "<init>";
}
};
return methodDeclaration( owner, TypeReference.VOID, parameters, exceptions, "<init>", true, modifiers,
typeParameters );
}

public static class TypeParameter
Expand Down Expand Up @@ -342,4 +287,30 @@ public TypeReference superBound()
return bound.superBound();
}
}

private static MethodDeclaration methodDeclaration( TypeReference owner, final TypeReference returnType,
final Parameter[] parameters, final TypeReference[] exceptions, final String name,
final boolean isConstrucor, int modifiers, TypeParameter[] typeParameters )
{
return new MethodDeclaration( owner, parameters, exceptions, modifiers, typeParameters )
{
@Override
public boolean isConstructor()
{
return isConstrucor;
}

@Override
public TypeReference returnType()
{
return returnType;
}

@Override
public String name()
{
return name;
}
};
}
}
Expand Up @@ -19,9 +19,8 @@
*/
package org.neo4j.codegen.bytecode;

import org.objectweb.asm.Opcodes;

interface Block extends Opcodes
interface Block
{
void endBlock();
}
Expand Up @@ -21,7 +21,6 @@

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.Modifier;

Expand All @@ -35,8 +34,67 @@
import static org.neo4j.codegen.ByteCodeUtils.byteCodeName;
import static org.neo4j.codegen.ByteCodeUtils.desc;
import static org.neo4j.codegen.ByteCodeUtils.typeName;

class ByteCodeExpressionVisitor implements ExpressionVisitor, Opcodes
import static org.objectweb.asm.Opcodes.AASTORE;
import static org.objectweb.asm.Opcodes.ACONST_NULL;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.ANEWARRAY;
import static org.objectweb.asm.Opcodes.BASTORE;
import static org.objectweb.asm.Opcodes.BIPUSH;
import static org.objectweb.asm.Opcodes.CASTORE;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.DADD;
import static org.objectweb.asm.Opcodes.DASTORE;
import static org.objectweb.asm.Opcodes.DCMPL;
import static org.objectweb.asm.Opcodes.DLOAD;
import static org.objectweb.asm.Opcodes.DMUL;
import static org.objectweb.asm.Opcodes.DSUB;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.FASTORE;
import static org.objectweb.asm.Opcodes.FCMPL;
import static org.objectweb.asm.Opcodes.FLOAD;
import static org.objectweb.asm.Opcodes.GETFIELD;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.IADD;
import static org.objectweb.asm.Opcodes.IASTORE;
import static org.objectweb.asm.Opcodes.ICONST_0;
import static org.objectweb.asm.Opcodes.ICONST_1;
import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.IFLE;
import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.IFNONNULL;
import static org.objectweb.asm.Opcodes.IFNULL;
import static org.objectweb.asm.Opcodes.IF_ACMPNE;
import static org.objectweb.asm.Opcodes.IF_ICMPLE;
import static org.objectweb.asm.Opcodes.IF_ICMPNE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.ISUB;
import static org.objectweb.asm.Opcodes.L2D;
import static org.objectweb.asm.Opcodes.LADD;
import static org.objectweb.asm.Opcodes.LASTORE;
import static org.objectweb.asm.Opcodes.LCMP;
import static org.objectweb.asm.Opcodes.LLOAD;
import static org.objectweb.asm.Opcodes.LMUL;
import static org.objectweb.asm.Opcodes.LSUB;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.NEWARRAY;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.SASTORE;
import static org.objectweb.asm.Opcodes.SIPUSH;
import static org.objectweb.asm.Opcodes.T_BOOLEAN;
import static org.objectweb.asm.Opcodes.T_BYTE;
import static org.objectweb.asm.Opcodes.T_CHAR;
import static org.objectweb.asm.Opcodes.T_DOUBLE;
import static org.objectweb.asm.Opcodes.T_FLOAT;
import static org.objectweb.asm.Opcodes.T_INT;
import static org.objectweb.asm.Opcodes.T_LONG;
import static org.objectweb.asm.Opcodes.T_SHORT;

class ByteCodeExpressionVisitor implements ExpressionVisitor
{
private final MethodVisitor methodVisitor;

Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
Expand All @@ -41,8 +40,14 @@
import static org.neo4j.codegen.ByteCodeUtils.outerName;
import static org.neo4j.codegen.ByteCodeUtils.signature;
import static org.neo4j.codegen.ByteCodeUtils.typeName;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.PUTSTATIC;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V1_8;

class ClassByteCodeWriter implements ClassEmitter, Opcodes
class ClassByteCodeWriter implements ClassEmitter
{
private final ClassWriter classWriter;
private final TypeReference type;
Expand Down
Expand Up @@ -21,7 +21,8 @@


import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import static org.objectweb.asm.Opcodes.RETURN;

public class Method implements Block
{
Expand All @@ -39,7 +40,7 @@ public void endBlock()
{
if ( isVoid )
{
methodVisitor.visitInsn( Opcodes.RETURN );
methodVisitor.visitInsn( RETURN );
}
//we rely on asm to keep track of stack depth
methodVisitor.visitMaxs( 0, 0 );
Expand Down

0 comments on commit 6ccc59b

Please sign in to comment.