Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#goTo()

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#goTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionBooleanOr.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label exit = new Label();
	Label labelTrue = new Label();
	for (Expression predicate : expressions) {
		Type type = predicate.load(ctx);
		checkArgument(type == BOOLEAN_TYPE);
		g.ifZCmp(GeneratorAdapter.NE, labelTrue);
	}
	g.push(false);
	g.goTo(exit);

	g.mark(labelTrue);
	g.push(true);

	g.mark(exit);
	return BOOLEAN_TYPE;
}
 
Example 2
Source File: ExpressionIf.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	Label labelTrue = new Label();
	Label labelExit = new Label();

	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Type conditionType = condition.load(ctx);
	g.push(true);

	g.ifCmp(conditionType, GeneratorAdapter.EQ, labelTrue);

	right.load(ctx);

	g.goTo(labelExit);

	g.mark(labelTrue);
	Type leftType = left.load(ctx);

	g.mark(labelExit);
	return leftType;
}
 
Example 3
Source File: ExpressionBooleanAnd.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label exit = new Label();
	Label labelFalse = new Label();
	for (Expression predicate : expressions) {
		Type type = predicate.load(ctx);
		checkArgument(type == BOOLEAN_TYPE);
		g.ifZCmp(GeneratorAdapter.EQ, labelFalse);
	}
	g.push(true);
	g.goTo(exit);

	g.mark(labelFalse);
	g.push(false);

	g.mark(exit);
	return BOOLEAN_TYPE;
}
 
Example 4
Source File: ExpressionIsNull.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	Label labelNull = new Label();
	Label labelExit = new Label();

	expression.load(ctx);
	g.ifNull(labelNull);
	g.push(false);
	g.goTo(labelExit);

	g.mark(labelNull);
	g.push(true);

	g.mark(labelExit);

	return Type.BOOLEAN_TYPE;
}
 
Example 5
Source File: ExpressionIsNotNull.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	Label labelNotNull = new Label();
	Label labelExit = new Label();

	expression.load(ctx);
	g.ifNonNull(labelNotNull);
	g.push(false);
	g.goTo(labelExit);

	g.mark(labelNotNull);
	g.push(true);

	g.mark(labelExit);

	return Type.BOOLEAN_TYPE;
}
 
Example 6
Source File: AbstractExpressionIteratorForEach.java    From datakernel with Apache License 2.0 5 votes vote down vote up
public Type arrayForEach(Context ctx, GeneratorAdapter g, Label labelLoop, Label labelExit) {
	VarLocal len = ctx.newLocal(INT_TYPE);
	g.arrayLength();
	len.store(ctx);

	g.push(0);
	VarLocal varPosition = ctx.newLocal(INT_TYPE);
	varPosition.store(ctx);

	g.mark(labelLoop);

	varPosition.load(ctx);
	len.load(ctx);

	g.ifCmp(INT_TYPE, GeneratorAdapter.GE, labelExit);

	collection.load(ctx);
	varPosition.load(ctx);
	g.arrayLoad(getType(type));

	VarLocal it = ctx.newLocal(getType(type));
	it.store(ctx);

	forEach.apply(getValue(it)).load(ctx);

	varPosition.load(ctx);
	g.push(1);
	g.math(GeneratorAdapter.ADD, INT_TYPE);
	varPosition.store(ctx);

	g.goTo(labelLoop);
	g.mark(labelExit);

	return VOID_TYPE;
}
 
Example 7
Source File: AbstractExpressionMapForEach.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
public final Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelLoop = new Label();
	Label labelExit = new Label();

	ctx.invoke(getEntries(), "iterator");
	VarLocal iterator = ctx.newLocal(getType(Iterator.class));
	iterator.store(ctx);

	g.mark(labelLoop);

	ctx.invoke(iterator, "hasNext");
	g.push(false);
	g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit);

	Type entryType = getType(entryClazz);
	ctx.cast(ctx.invoke(iterator, "next"), entryType);

	VarLocal entry = ctx.newLocal(entryType);
	entry.store(ctx);

	Type forKeyType = forKey.apply(getKey(entry)).load(ctx);
	if (forKeyType.getSize() == 1)
		g.pop();
	if (forKeyType.getSize() == 2)
		g.pop2();

	Type forValueType = forValue.apply(getValue(entry)).load(ctx);
	if (forValueType.getSize() == 1)
		g.pop();
	if (forValueType.getSize() == 2)
		g.pop2();

	g.goTo(labelLoop);
	g.mark(labelExit);
	return Type.VOID_TYPE;
}
 
Example 8
Source File: ExpressionNeg.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
	public Type load(Context ctx) {
		GeneratorAdapter g = ctx.getGeneratorAdapter();
		Type argType = arg.load(ctx);
		int argSort = argType.getSort();

		if (argSort == Type.DOUBLE || argSort == Type.FLOAT || argSort == Type.LONG || argSort == Type.INT) {
			g.math(GeneratorAdapter.NEG, argType);
			return argType;
		}
		if (argSort == Type.BYTE || argSort == Type.SHORT || argSort == Type.CHAR) {
//			g.cast(argType, INT_TYPE);
			g.math(GeneratorAdapter.NEG, INT_TYPE);
			return argType;
		}

		if (argSort == Type.BOOLEAN) {
			Label labelTrue = new Label();
			Label labelExit = new Label();
			g.push(true);
			g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
			g.push(true);
			g.goTo(labelExit);

			g.mark(labelTrue);
			g.push(false);

			g.mark(labelExit);
			return INT_TYPE;
		}

		throw new RuntimeException(format("%s is not primitive. %s",
				ctx.toJavaType(argType),
				exceptionInGeneratedClass(ctx))
		);
	}
 
Example 9
Source File: ExpressionBooleanNot.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelFalse = g.newLabel();
	Label labelExit = g.newLabel();
	expression.load(ctx);
	g.ifZCmp(GeneratorAdapter.EQ, labelFalse);
	g.push(false);
	g.goTo(labelExit);
	g.visitLabel(labelFalse);
	g.push(true);
	g.visitLabel(labelExit);
	return Type.BOOLEAN_TYPE;
}
 
Example 10
Source File: ExpressionFor.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelLoop = new Label();
	Label labelExit = new Label();

	VarLocal to = ctx.newLocal(INT_TYPE);
	this.to.load(ctx);
	to.store(ctx);

	from.load(ctx);
	VarLocal it = ctx.newLocal(INT_TYPE);
	it.store(ctx);

	g.mark(labelLoop);

	it.load(ctx);
	to.load(ctx);

	g.ifCmp(INT_TYPE, GeneratorAdapter.GE, labelExit);

	Type forType = forVar.apply(it).load(ctx);
	if (forType.getSize() == 1)
		g.pop();
	if (forType.getSize() == 2)
		g.pop2();

	it.load(ctx);
	g.push(1);
	g.math(GeneratorAdapter.ADD, INT_TYPE);
	it.store(ctx);

	g.goTo(labelLoop);
	g.mark(labelExit);

	return VOID_TYPE;
}
 
Example 11
Source File: ConstructorArgsRedirection.java    From AnoleFix with MIT License 4 votes vote down vote up
@Override
protected void restore(GeneratorAdapter mv, List<Type> args) {
    // At this point, init$args has been called and the result Object is on the stack.
    // The value of that Object is Object[] with exactly n + 1 elements.
    // The first element is a string with the qualified name of the constructor to call.
    // The remaining elements are the constructtor arguments.

    // Create a new local that holds the result of init$args call.
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    int constructorArgs = mv.newLocal(Type.getType("[Ljava/lang/Object;"));
    mv.storeLocal(constructorArgs);

    // Reinstate local values
    mv.loadLocal(locals);
    int stackIndex = 0;
    for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) {
        Type arg = args.get(arrayIndex);
        // Do not restore "this"
        if (arrayIndex > 0) {
            // duplicates the array
            mv.dup();
            // index in the array of objects to restore the boxed parameter.
            mv.push(arrayIndex);
            // get it from the array
            mv.arrayLoad(Type.getType(Object.class));
            // unbox the argument
            ByteCodeUtils.unbox(mv, arg);
            // restore the argument
            mv.visitVarInsn(arg.getOpcode(Opcodes.ISTORE), stackIndex);
        }
        // stack index must progress according to the parameter type we just processed.
        stackIndex += arg.getSize();
    }
    // pops the array
    mv.pop();

    // Push a null for the marker parameter.
    mv.loadLocal(constructorArgs);
    mv.visitInsn(Opcodes.ACONST_NULL);

    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, thisClassName, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    mv.goTo(end.getLabel());
}
 
Example 12
Source File: TestThreadExecutionGenerator.java    From lin-check with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void generateRun(ClassVisitor cv, Type testType, int iThread, List<Actor> actors, List<Object> objArgs, boolean waitsEnabled) {
    int access = ACC_PUBLIC;
    Method m = new Method("call", RESULT_ARRAY_TYPE, NO_ARGS);
    GeneratorAdapter mv = new GeneratorAdapter(access, m,
        // Try-catch blocks sorting is required
        new TryCatchBlockSorter(cv.visitMethod(access, m.getName(), m.getDescriptor(), null, null),
            access, m.getName(), m.getDescriptor(), null, null)
    );
    mv.visitCode();
    // Create Result[] array and store it to a local variable
    int resLocal = createResultArray(mv, actors.size());
    // Call runner's onStart(iThread) method
    mv.loadThis();
    mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE);
    mv.push(iThread);
    mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_START_METHOD);
    // Number of current operation (starts with 0)
    int iLocal = mv.newLocal(Type.INT_TYPE);
    mv.push(0);
    mv.storeLocal(iLocal);
    // Invoke actors
    for (int i = 0; i < actors.size(); i++) {
        Actor actor = actors.get(i);
        // Add busy-wait before operation execution (for non-first operations only)
        if (waitsEnabled && i > 0) {
            mv.loadThis();
            mv.getField(TEST_THREAD_EXECUTION_TYPE, "waits", INT_ARRAY_TYPE);
            mv.push(i - 1);
            mv.arrayLoad(Type.INT_TYPE);
            mv.invokeStatic(UTILS_TYPE, UTILS_CONSUME_CPU);
        }
        // Start of try-catch block for exceptions which this actor should handle
        Label start, end = null, handler = null, handlerEnd = null;
        if (actor.handlesExceptions()) {
            start = mv.newLabel();
            end = mv.newLabel();
            handler = mv.newLabel();
            handlerEnd = mv.newLabel();
            for (Class<? extends Throwable> ec : actor.handledExceptions)
                mv.visitTryCatchBlock(start, end, handler, Type.getType(ec).getInternalName());
            mv.visitLabel(start);
        }
        // Load result array and index to store the current result
        mv.loadLocal(resLocal);
        mv.push(i);
        // Load test instance
        mv.loadThis();
        mv.getField(TEST_THREAD_EXECUTION_TYPE, "testInstance", OBJECT_TYPE);
        mv.checkCast(testType);
        // Load arguments for operation
        for (int j = 0; j < actor.arguments.length; j++) {
            pushArgumentOnStack(mv, objArgs, actor.arguments[j], actor.method.getParameterTypes()[j]);
        }
        // Invoke operation
        Method actorMethod = Method.getMethod(actor.method);
        mv.invokeVirtual(testType, actorMethod);
        // Create result
        mv.box(actorMethod.getReturnType()); // box if needed
        if (actor.method.getReturnType() == void.class) {
            mv.pop();
            mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VOID_RESULT);
        } else {
            mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VALUE_RESULT);
        }
        // Store result to array
        mv.arrayStore(RESULT_TYPE);
        // End of try-catch block
        if (actor.handlesExceptions()) {
            mv.visitLabel(end);
            mv.goTo(handlerEnd);
            mv.visitLabel(handler);
            storeExceptionResultFromThrowable(mv, resLocal, iLocal);
            mv.visitLabel(handlerEnd);
        }
        // Increment number of current operation
        mv.iinc(iLocal, 1);
    }
    // Call runner's onFinish(iThread) method
    mv.loadThis();
    mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE);
    mv.push(iThread);
    mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_FINISH_METHOD);
    // Return results
    mv.loadThis();
    mv.loadLocal(resLocal);
    mv.returnValue();
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}
 
Example 13
Source File: AbstractExpressionIteratorForEach.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public final Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelLoop = new Label();
	Label labelExit = new Label();

	Type collectionType = collection.load(ctx);
	if (collectionType.getSort() == ARRAY) {
		return arrayForEach(ctx, g, labelLoop, labelExit);
	}

	VarLocal varIter = ctx.newLocal(getType(Iterator.class));

	Class<?> t = ctx.toJavaType(collectionType);
	if (t.isInstance(Iterator.class) || t == Iterator.class) {
		// do nothing
	} else {
		ctx.invoke(collectionType, "iterator");
	}
	varIter.store(ctx);

	g.mark(labelLoop);

	ctx.invoke(varIter, "hasNext");
	g.push(false);
	g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit);

	ctx.cast(ctx.invoke(varIter, "next"), getType(type));
	VarLocal it = ctx.newLocal(getType(type));
	it.store(ctx);

	Type forEachType = forEach.apply(getValue(it)).load(ctx);
	if (forEachType.getSize() == 1)
		g.pop();
	if (forEachType.getSize() == 2)
		g.pop2();

	g.goTo(labelLoop);
	g.mark(labelExit);
	return VOID_TYPE;

}
 
Example 14
Source File: ExpressionToString.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	g.newInstance(getType(StringBuilder.class));
	g.dup();
	g.invokeConstructor(getType(StringBuilder.class), getMethod("void <init> ()"));

	boolean first = true;

	for (Object key : arguments.keySet()) {
		String str = first ? begin : separator;
		first = false;
		if (key instanceof String) {
			str += key;
		}
		if (!str.isEmpty()) {
			g.dup();
			g.push(str);
			g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
			g.pop();
		}

		g.dup();
		Expression expression = arguments.get(key);
		Type type = expression.load(ctx);
		if (isPrimitiveType(type)) {
			g.invokeStatic(wrap(type), new Method("toString", getType(String.class), new Type[]{type}));
		} else {
			Label nullLabel = new Label();
			Label afterToString = new Label();
			g.dup();
			g.ifNull(nullLabel);
			g.invokeVirtual(type, getMethod("String toString()"));
			g.goTo(afterToString);
			g.mark(nullLabel);
			g.pop();
			g.push("null");
			g.mark(afterToString);
		}
		g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
		g.pop();
	}

	if (!end.isEmpty()) {
		g.dup();
		g.push(end);
		g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
		g.pop();
	}

	g.invokeVirtual(getType(StringBuilder.class), getMethod("String toString()"));
	return getType(String.class);
}
 
Example 15
Source File: ExpressionCmp.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelTrue = new Label();
	Label labelExit = new Label();

	Type leftType = left.load(ctx);
	Type rightType = right.load(ctx);
	checkArgument(Objects.equals(leftType, rightType), "Types of compared values should match");

	if (isPrimitiveType(leftType)) {
		g.ifCmp(leftType, operation.opCode, labelTrue);
	} else {
		if (operation == EQ || operation == NE) {
			g.invokeVirtual(leftType, new Method("equals", BOOLEAN_TYPE, new Type[]{Type.getType(Object.class)}));
			g.push(operation == EQ);
			g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
		} else {
			g.invokeVirtual(leftType, new Method("compareTo", INT_TYPE, new Type[]{Type.getType(Object.class)}));
			if (operation == LT) {
				g.ifZCmp(GeneratorAdapter.LT, labelTrue);
			} else if (operation == GT) {
				g.ifZCmp(GeneratorAdapter.GT, labelTrue);
			} else if (operation == LE) {
				g.ifZCmp(GeneratorAdapter.LE, labelTrue);
			} else if (operation == GE) {
				g.ifZCmp(GeneratorAdapter.GE, labelTrue);
			}
		}
	}

	g.push(false);
	g.goTo(labelExit);

	g.mark(labelTrue);
	g.push(true);

	g.mark(labelExit);

	return BOOLEAN_TYPE;
}