From cbc8020e816f1733a056c905fe52175c0fe5b882 Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Thu, 5 Mar 2020 09:06:22 +0100 Subject: [PATCH] 25: Rewrite prepareRegisters, correct operator order for comparisons --- .../hsrm/compiler/Klang/visitors/GenASM.java | 60 +++++++++++-------- src/test/math/math.c | 1 + 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java index f1b5efc..dbed3fb 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -119,24 +119,33 @@ public class GenASM implements Visitor { private boolean prepareRegisters(Expression lhs, Expression rhs) { boolean lhsIsFloat = lhs.type.equals(Type.getFloatType()); boolean rhsIsFloat = rhs.type.equals(Type.getFloatType()); - if (lhsIsFloat || rhsIsFloat) { + if (lhsIsFloat && rhsIsFloat) { lhs.welcome(this); - if (!lhsIsFloat) { - this.intToFloat("%rax", "%xmm1"); - } else { - this.ex.write(" movsd %xmm0, %xmm1\n"); - } + this.ex.write(" movsd %xmm0, %xmm2\n"); rhs.welcome(this); - if (!rhsIsFloat) { - this.intToFloat("%rax", "%xmm1"); - } + this.ex.write(" movsd %xmm0, %xmm1\n"); + this.ex.write(" movsd %xmm2, %xmm0\n"); return true; + } else if (lhsIsFloat && !rhsIsFloat) { + lhs.welcome(this); + rhs.welcome(this); + this.intToFloat("%rax", "%xmm1"); + return true; + } else if (!lhsIsFloat && rhsIsFloat) { + lhs.welcome(this); + this.intToFloat("%rax", "%xmm2"); + rhs.welcome(this); + this.ex.write(" movsd %xmm0, %xmm1\n"); + this.ex.write(" movsd %xmm2, %xmm0\n"); + return true; + } else { + lhs.welcome(this); + this.ex.write(" pushq %rax\n"); + rhs.welcome(this); + this.ex.write(" movq %rax, %rbx\n"); + this.ex.write(" popq %rax\n"); + return false; } - lhs.welcome(this); - this.ex.write(" pushq %rax\n"); - rhs.welcome(this); - this.ex.write(" popq %rbx\n"); - return false; } public GenASM(ExWriter ex, String mainName) { this.ex = ex; @@ -184,9 +193,9 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + this.ex.write(" ucomisd %xmm1, %xmm0\n"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + this.ex.write(" cmp %rbx, %rax\n"); } this.ex.write(" je .L" + lblTrue + "\n"); // false @@ -228,9 +237,9 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + this.ex.write(" ucomisd %xmm1, %xmm0\n"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + this.ex.write(" cmp %rbx, %rax\n"); } this.ex.write(" jg .L" + lblTrue + "\n"); // false @@ -250,9 +259,9 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + this.ex.write(" ucomisd %xmm1, %xmm0\n"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + this.ex.write(" cmp %rbx, %rax\n"); } this.ex.write(" jge .L" + lblTrue + "\n"); // false @@ -272,9 +281,9 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + this.ex.write(" ucomisd %xmm1, %xmm0\n"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + this.ex.write(" cmp %rbx, %rax\n"); } this.ex.write(" jl .L" + lblTrue + "\n"); // false @@ -294,9 +303,9 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + this.ex.write(" ucomisd %xmm1, %xmm0\n"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + this.ex.write(" cmp %rbx, %rax\n"); } this.ex.write(" jle .L" + lblTrue + "\n"); // false @@ -326,8 +335,7 @@ public class GenASM implements Visitor { if (isFloatOperation) { this.ex.write(" subsd %xmm1, %xmm0\n"); } else { - this.ex.write(" subq %rax, %rbx\n"); - this.ex.write(" movq %rbx, %rax\n"); + this.ex.write(" subq %rbx, %rax\n"); } return null; } diff --git a/src/test/math/math.c b/src/test/math/math.c index 7b144c4..6a2d63a 100644 --- a/src/test/math/math.c +++ b/src/test/math/math.c @@ -183,6 +183,7 @@ int runMathTests() math_testOneArg("neg", cNeg, neg, 0); math_testOneArg("neg", cNeg, neg, 1); math_testOneArg("neg", cNeg, neg, -1); + math_testOneArg("neg", cNeg, neg, -10); printf("\nIdentity Tests\n"); math_testOneArg("id", cId, id, 0);