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

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#ifCmp() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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;
}