From 76419d86bb3ffda26a92fb63c54ee7b18ec06811 Mon Sep 17 00:00:00 2001 From: nitrix Date: Thu, 23 Mar 2023 02:57:17 +0100 Subject: [PATCH] GenASM: Remove a few warnings --- .../hsrm/compiler/Klang/visitors/GenASM.java | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 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 badd685..6673292 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -13,16 +13,16 @@ import de.hsrm.compiler.Klang.types.Type; import java.util.*; public class GenASM implements Visitor { - private class FloatWriter { - private StringBuilder sb = new StringBuilder(); + private static class FloatWriter { + private final StringBuilder sb = new StringBuilder(); private int id = -1; public String getFloat(double d) { - Long longBits = Double.doubleToRawLongBits(d); - String binary = Long.toBinaryString(longBits); + long longBits = Double.doubleToRawLongBits(d); + StringBuilder binary = new StringBuilder(Long.toBinaryString(longBits)); int padCount = 64 - binary.length(); while (padCount > 0) { - binary = "0" + binary; + binary.insert(0, "0"); padCount--; } String upper = binary.substring(0, 32); @@ -59,14 +59,16 @@ public class GenASM implements Visitor { } } - private ASM asm; - private FloatWriter fw = new FloatWriter(); - private String mainName; + private final ASM asm; + private final FloatWriter fw = new FloatWriter(); + private final String mainName; + Map env = new HashMap<>(); Map structs; String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" }; String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" }; - private int lCount = 0; // Invariant: lCount is used + + private int lCount = 0; private int currentFunctionStartLabel = 0; private Parameter[] currentFunctionParams; @@ -320,7 +322,7 @@ public class GenASM implements Visitor { // Werte LHS aus // Wenn LHS != 0 bedeutet das true - // also können wir direkt sagen dass das Ergebnis true ist + // also können wir direkt sagen, dass das Ergebnis true ist e.lhs.welcome(this); asm.cmp("q", 0, "%rax"); asm.jne(lblTrue); @@ -338,11 +340,11 @@ public class GenASM implements Visitor { asm.mov("q", 1, "%rax"); asm.jmp(lblEnd); - // Die Expressoin wertet zu false aus + // Die Expression wertet zu false aus asm.label(lblFalse); asm.mov("q", 0, "%rax"); - // Das hier ist das ende + // Das hier ist das Ende asm.label(lblEnd); return null; } @@ -355,7 +357,7 @@ public class GenASM implements Visitor { // Werte LHS aus // Wenn LHS == 0, bedeutet das false - // also können wir direkt sagen dass das Ergebnis false ist + // also können wir direkt sagen, dass das Ergebnis false ist e.lhs.welcome(this); asm.cmp("q", 0, "%rax"); asm.je(lblFalse); @@ -373,11 +375,11 @@ public class GenASM implements Visitor { asm.mov("q", 1, "%rax"); asm.jmp(lblEnd); - // Die Expressoin wertet zu false aus + // Die Expression wertet zu false aus asm.label(lblFalse); asm.mov("q", 0, "%rax"); - // Das hier ist das ende + // Das hier ist das Ende asm.label(lblEnd); return null; } @@ -387,9 +389,9 @@ public class GenASM implements Visitor { int lblFalse = ++lCount; int lblEnd = ++lCount; - // Werte LHS aus - // Wenn LHS != 0 bedeutet das true, also jumpe zum false Teil - // Wenn nicht, falle durch zum true Teil + // Werte LHS aus. + // Wenn LHS != 0 bedeutet das true, also springe zum false Teil. + // Wenn nicht, falle durch zum true Teil. e.lhs.welcome(this); asm.cmp("q", 0, "%rax"); asm.jne(lblFalse); @@ -872,7 +874,7 @@ public class GenASM implements Visitor { e.expression.welcome(this); - // Move it from xmm0 rax if its a flaot + // Move it from xmm0 rax if it's a float if (e.expression.type.equals(Type.getFloatType())) { asm.mov("q", "%xmm0", "%rax"); } @@ -890,7 +892,7 @@ public class GenASM implements Visitor { asm.mov("q", Helper.getFieldOffset(structDef, e.path[i]), "%rax", "%rax"); } - // pop the expression that is ontop of the stack into the field of the struct that has to be updated + // pop the expression that is on top of the stack into the field of the struct that has to be updated asm.pop("q", Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)"); asm.mov("q", 0 , "%rax"); // clear rax since an assignment has no result @@ -907,12 +909,12 @@ public class GenASM implements Visitor { asm.mov("sd", "%xmm2", "%xmm0"); asm.mov("sd", "%xmm2", "%xmm0"); return true; - } else if (lhsIsFloat && !rhsIsFloat) { + } else if (lhsIsFloat) { lhs.welcome(this); rhs.welcome(this); asm.cvtsi2sd("%rax", "%xmm1"); return true; - } else if (!lhsIsFloat && rhsIsFloat) { + } else if (rhsIsFloat) { lhs.welcome(this); asm.cvtsi2sd("%rax", "%xmm2"); rhs.welcome(this);