more binary operator support

This commit is contained in:
404Simon 2024-05-14 12:25:15 +02:00
parent 9a7d296ecc
commit 69548745c2
4 changed files with 62 additions and 12 deletions

View File

@ -1,6 +1,5 @@
package de.maishai.typedast;
import de.maishai.VariableGenerator;
import lombok.*;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;

View File

@ -7,6 +7,7 @@ import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.Type;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -69,14 +70,63 @@ public class TypedBinary implements TypedExpression {
@Override
public void codeGen(MethodContext ctx) {
left.codeGen(ctx);
right.codeGen(ctx);
if (op == Operator.ADD) {
ctx.getMv().visitInsn(Opcodes.IADD);
} else if (op == Operator.SUB) {
ctx.getMv().visitInsn(Opcodes.ISUB);
} else if (op == Operator.MUL) {
ctx.getMv().visitInsn(Opcodes.IMUL);
if (op == Operator.AND) {
Label end = new Label();
Label returnTrue = new Label();
Label returnFalse = new Label();
left.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnFalse);
right.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnFalse);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnTrue);
ctx.getMv().visitLabel(returnFalse);
ctx.getMv().visitInsn(Opcodes.ICONST_0);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);
ctx.getMv().visitLabel(returnTrue);
ctx.getMv().visitInsn(Opcodes.ICONST_1);
ctx.getMv().visitLabel(end);
} else if (op == Operator.OR) {
Label end = new Label();
Label returnTrue = new Label();
Label returnFalse = new Label();
left.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnTrue);
right.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnTrue);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnFalse);
ctx.getMv().visitLabel(returnFalse);
ctx.getMv().visitInsn(Opcodes.ICONST_0);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);
ctx.getMv().visitLabel(returnTrue);
ctx.getMv().visitInsn(Opcodes.ICONST_1);
ctx.getMv().visitLabel(end);
} else if (op == Operator.EQ) {
Label end = new Label();
Label returnTrue = new Label();
Label returnFalse = new Label();
left.codeGen(ctx);
right.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IF_ICMPNE, returnFalse);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnTrue);
ctx.getMv().visitLabel(returnFalse);
ctx.getMv().visitInsn(Opcodes.ICONST_0);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);
ctx.getMv().visitLabel(returnTrue);
ctx.getMv().visitInsn(Opcodes.ICONST_1);
ctx.getMv().visitLabel(end);
} else {
left.codeGen(ctx);
right.codeGen(ctx);
if (op == Operator.ADD) {
ctx.getMv().visitInsn(Opcodes.IADD);
} else if (op == Operator.SUB) {
ctx.getMv().visitInsn(Opcodes.ISUB);
} else if (op == Operator.MUL) {
ctx.getMv().visitInsn(Opcodes.IMUL);
}
}
//TODO: implement other operators
ctx.popStack();

View File

@ -51,6 +51,7 @@ public class TypedReturn implements TypedStatement {
if (ret == null) {
ctx.getMv().visitInsn(Opcodes.RETURN);
} else {
System.out.println("return: " + ret);
ret.codeGen(ctx);
if (ret.getType().getKind() != Type.Kind.REFERENCE) {
ctx.getMv().visitInsn(Opcodes.IRETURN);

View File

@ -3,9 +3,9 @@ public class ClassCanBeTyped {
public int x;
public boolean y;
public ClassCanBeTyped(int x, boolean y) {
this.c.x = 2 * x;
this.y = !y;
public boolean test(boolean b, boolean c) {
return b == c;
}
}