32: Require data type for all asm functions

This commit is contained in:
Marvin Kaiser
2020-03-17 16:02:03 +01:00
parent 982fc6417d
commit 49b024b95f
15 changed files with 84 additions and 177 deletions

View File

@@ -12,10 +12,6 @@ public class ASM {
this.mnemonics = new ArrayList<Mnemonic>();
}
public void push(String operand) {
mnemonics.add(new Push(operand));
}
public void push(String dataType, int immediate) {
mnemonics.add(new Push(dataType, immediate));
}
@@ -24,22 +20,10 @@ public class ASM {
mnemonics.add(new Push(dataType, operand));
}
public void pop(String operand) {
mnemonics.add(new Pop(operand));
}
public void pop(String dataType, String operand) {
mnemonics.add(new Pop(dataType, operand));
}
public void mov(int immediate, String dst) {
mnemonics.add(new Mov(immediate, dst));
}
public void mov(String src, String dst) {
mnemonics.add(new Mov(src, dst));
}
public void mov(String dataType, String src, String dst) {
mnemonics.add(new Mov(dataType, src, dst));
}
@@ -60,16 +44,12 @@ public class ASM {
mnemonics.add(new Mov(dataType, immediate, dst));
}
public void ucomi(String src, String dst) {
mnemonics.add(new Ucomi(src, dst));
}
public void ucomi(String dataType, String src, String dst) {
mnemonics.add(new Ucomi(dataType, src, dst));
}
public void cmp(String src, String dst) {
mnemonics.add(new Cmp(src, dst));
public void cmp(String dataType, String src, String dst) {
mnemonics.add(new Cmp(dataType, src, dst));
}
public void cmp(String dataType, int immediate, String dst) {
@@ -156,10 +136,6 @@ public class ASM {
mnemonics.add(new Label(labelPrefix, label));
}
public void add(String src, String dst) {
mnemonics.add(new Add(src, dst));
}
public void add(String dataType, String src, String dst) {
mnemonics.add(new Add(dataType, src, dst));
}
@@ -171,27 +147,15 @@ public class ASM {
public void add(String dataType, int immediate, String dst) {
mnemonics.add(new Add(dataType, immediate, dst));
}
public void sub(String src, String dst) {
mnemonics.add(new Sub(src, dst));
}
public void sub(String dataType, String src, String dst) {
mnemonics.add(new Sub(dataType, src, dst));
}
public void mul(String src, String dst) {
mnemonics.add(new Mul(src, dst));
}
public void mul(String dataType, String src, String dst) {
mnemonics.add(new Mul(dataType, src, dst));
}
public void div(String src, String dst) {
mnemonics.add(new Div(src, dst));
}
public void div(String dataType, String src, String dst) {
mnemonics.add(new Div(dataType, src, dst));
}
@@ -200,12 +164,8 @@ public class ASM {
mnemonics.add(new Idiv(dataType, operand));
}
public void idiv(String operand) {
mnemonics.add(new Idiv(operand));
}
public void imul(String src, String dst) {
mnemonics.add(new Imul(src, dst));
public void imul(String dataType, String src, String dst) {
mnemonics.add(new Imul(dataType, src, dst));
}
public void cqto() {
@@ -219,10 +179,6 @@ public class ASM {
public void xor(String dataType, String src, String dst) {
mnemonics.add(new Xor(dataType, src, dst));
}
public void xor(String src, String dst) {
mnemonics.add(new Xor(src, dst));
}
public void neg(String operand) {
mnemonics.add(new Neg(operand));

View File

@@ -1,13 +1,8 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Add extends TwoOperandMnemonic {
public String dataType = "q";
public Add(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Add(String dataType, String src, String dst) {
this.dataType = dataType;
this.src = src;

View File

@@ -1,9 +1,10 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Cmp extends TwoOperandMnemonic {
public Cmp(String src, String dst) {
this.dataType = "";
public String dataType;
public Cmp(String dataType, String src, String dst) {
this.dataType = dataType;
this.src = src;
this.dst = dst;
}

View File

@@ -1,12 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Div extends TwoOperandMnemonic {
public String dataType = "q";
public Div(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Div(String dataType, String src, String dst) {
this.dataType = dataType;

View File

@@ -1,11 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Idiv extends OneOperandMnemonic{
public String dataType = "";
public Idiv(String operand) {
this.operand = operand;
}
public String dataType;
public Idiv(String dataType, String operand) {
this.dataType = dataType;

View File

@@ -1,9 +1,10 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Imul extends TwoOperandMnemonic {
public String dataType = "q";
public String dataType;
public Imul(String src, String dst) {
public Imul(String dataType, String src, String dst) {
this.dataType = dataType;
this.src = src;
this.dst = dst;
}

View File

@@ -1,7 +1,6 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public abstract class Mnemonic {
public String dataType = "";
public abstract String toAsm();
public int indentation = 2;
}

View File

@@ -3,16 +3,6 @@ package de.hsrm.compiler.Klang.asm.mnemonics;
public class Mov extends TwoOperandMnemonic{
public String dataType = "q";
public Mov(int immediate, String dst) {
this.src = "$" + immediate;
this.dst = dst;
}
public Mov(String src, String dst) {
this.src = src;
this.dst = dst;
}
public Mov(String dataType, String src, String dst) {
this.dataType = dataType;
this.src = src;

View File

@@ -1,12 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Mul extends TwoOperandMnemonic {
public String dataType = "q";
public Mul(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Mul(String dataType, String src, String dst) {
this.dataType = dataType;

View File

@@ -1,10 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Pop extends OneOperandMnemonic {
public String dataType = "q";
public Pop(String operand) {
this.operand = operand;
}
public String dataType;
public Pop(String dataType, String operand) {
this.dataType = dataType;

View File

@@ -1,10 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Push extends OneOperandMnemonic {
public Push(String operand){
this.dataType = "q";
this.operand = operand;
}
public String dataType;
public Push(String dataType, int immediate){
this.dataType = dataType;

View File

@@ -1,12 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Sub extends TwoOperandMnemonic {
public String dataType = "q";
public Sub(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Sub(String dataType, String src, String dst) {
this.dataType = dataType;

View File

@@ -1,12 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Ucomi extends TwoOperandMnemonic {
public String dataType = "q";
public Ucomi(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Ucomi(String dataType, String src, String dst) {
this.dataType = dataType;

View File

@@ -1,12 +1,7 @@
package de.hsrm.compiler.Klang.asm.mnemonics;
public class Xor extends TwoOperandMnemonic {
public String dataType = "q";
public Xor(String src, String dst) {
this.src = src;
this.dst = dst;
}
public String dataType;
public Xor(String dataType, String src, String dst) {
this.dataType = dataType;

View File

@@ -83,7 +83,7 @@ public class GenASM implements Visitor<Void> {
asm.mov("sd", "%xmm0", "%xmm2");
rhs.welcome(this);
asm.mov("sd", "%xmm2", "%xmm0");
asm.mov("%xmm2", "%xmm0");
asm.mov("sd", "%xmm2", "%xmm0");
return true;
} else if (lhsIsFloat && !rhsIsFloat) {
lhs.welcome(this);
@@ -99,10 +99,10 @@ public class GenASM implements Visitor<Void> {
return true;
} else {
lhs.welcome(this);
asm.push("%rax");
asm.push("q", "%rax");
rhs.welcome(this);
asm.mov("%rax", "%rbx");
asm.pop("%rax");
asm.mov("q", "%rax", "%rbx");
asm.pop("q", "%rax");
return false;
}
}
@@ -123,7 +123,7 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(IntegerExpression e) {
asm.mov(e.value, "%rax");
asm.mov("q", e.value, "%rax");
return null;
}
@@ -136,7 +136,7 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(BooleanExpression e) {
asm.mov(e.value ? 1 : 0, "%rax");
asm.mov("q", e.value ? 1 : 0, "%rax");
return null;
}
@@ -159,15 +159,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm1", "xmm0");
} else {
asm.cmp("%rbx", "%rax");
asm.cmp("q", "%rbx", "%rax");
}
asm.je(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -181,15 +181,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm0", "%xmm1");
} else {
asm.cmp("%rax", "%rbx");
asm.cmp("q", "%rax", "%rbx");
}
asm.jne(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -203,15 +203,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm1", "%xmm0");
} else {
asm.cmp("%rbx", "%rax");
asm.cmp("q", "%rbx", "%rax");
}
asm.jg(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -225,15 +225,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm1", "xmm0");
} else {
asm.cmp("%rbx", "%rax");
asm.cmp("q", "%rbx", "%rax");
}
asm.jge(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -247,15 +247,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm1", "%xmm0");
} else {
asm.cmp("%rbx", "%rax");
asm.cmp("q", "%rbx", "%rax");
}
asm.jl(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -269,15 +269,15 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.ucomi("sd", "%xmm1", "%xmm0");
} else {
asm.cmp("%rbx", "%rax");
asm.cmp("q", "%rbx", "%rax");
}
asm.jle(lblTrue);
// false
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
asm.jmp(lblEnd);
asm.label(lblTrue);
// true
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.label(lblEnd);
return null;
}
@@ -288,7 +288,7 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.add("sd", "%xmm1", "%xmm0");
} else {
asm.add("%rbx", "%rax");
asm.add("q", "%rbx", "%rax");
}
return null;
}
@@ -299,7 +299,7 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.sub("sd", "%xmm1", "%xmm0");
} else {
asm.sub("%rbx", "%rax");
asm.sub("q", "%rbx", "%rax");
}
return null;
}
@@ -310,7 +310,7 @@ public class GenASM implements Visitor<Void> {
if (isFloatOperation) {
asm.mul("sd", "%xmm1", "%xmm0");
} else {
asm.imul("%rbx", "%rax");
asm.imul("q", "%rbx", "%rax");
}
return null;
}
@@ -322,7 +322,7 @@ public class GenASM implements Visitor<Void> {
asm.div("sd", "%xmm1", "%xmm0");
} else {
asm.cqto();
asm.idiv("%rbx");
asm.idiv("q", "%rbx");
}
return null;
}
@@ -330,13 +330,13 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(ModuloExpression e) {
e.lhs.welcome(this);
asm.push("%rax");
asm.push("q", "%rax");
e.rhs.welcome(this);
asm.mov("%rax", "%rbx");
asm.pop("%rax");
asm.mov("q", "%rax", "%rbx");
asm.pop("q", "%rax");
asm.cqto();
asm.idiv("%rbx");
asm.mov("%rdx", "%rax");
asm.idiv("q", "%rbx");
asm.mov("q", "%rdx", "%rax");
return null;
}
@@ -376,12 +376,12 @@ public class GenASM implements Visitor<Void> {
// Die Expression wertet zu true aus
// Springe am false Teil vorbei
asm.label(lblTrue);
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.jmp(lblEnd);
// Die Expressoin wertet zu false aus
asm.label(lblFalse);
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
// Das hier ist das ende
asm.label(lblEnd);
@@ -411,12 +411,12 @@ public class GenASM implements Visitor<Void> {
// Die Expression wertet zu true aus
// Springe am false Teil vorbei
asm.label(lblTrue);
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.jmp(lblEnd);
// Die Expressoin wertet zu false aus
asm.label(lblFalse);
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
// Das hier ist das ende
asm.label(lblEnd);
@@ -437,13 +437,13 @@ public class GenASM implements Visitor<Void> {
// Hier ist das Ergebnis true
// Springe am false Teil vorbei
asm.mov(1, "%rax");
asm.mov("q", 1, "%rax");
asm.jmp(lblEnd);
// Hier ist das Ergebnis false
// Falle zum Ende durch
asm.label(lblFalse);
asm.mov(0, "%rax");
asm.mov("q", 0, "%rax");
// Hier ist das Ende
asm.label(lblEnd);
@@ -555,8 +555,8 @@ public class GenASM implements Visitor<Void> {
@Override
public Void visit(ReturnStatement e) {
e.expression.welcome(this);
asm.mov("%rbp", "%rsp");
asm.pop("%rbp");
asm.mov("q", "%rbp", "%rsp");
asm.pop("q", "%rbp");
asm.ret();
return null;
}
@@ -575,8 +575,8 @@ public class GenASM implements Visitor<Void> {
this.currentFunctionStartLabel = lblStart;
this.currentFunctionParams = e.parameters;
asm.functionHead(e.name);
asm.push("%rbp");
asm.mov("%rsp", "%rbp");
asm.push("q", "%rbp");
asm.mov("q", "%rsp", "%rbp");
asm.label(lblStart);
// hole die anzahl der lokalen variablen
@@ -623,13 +623,13 @@ public class GenASM implements Visitor<Void> {
fi = 0;
for (var param: registerParameters) {
if (param.type.equals(Type.getFloatType())) {
asm.mov(this.floatRegisters[fi], "%rax");
asm.push("%rax");
asm.mov("q", this.floatRegisters[fi], "%rax");
asm.push("q", "%rax");
offset -= 8;
this.env.put(param.name, offset); // negative, liegt unter aktuellem BP
fi++;
} else {
asm.push(this.registers[ri]);
asm.push("q", this.registers[ri]);
offset -= 8;
this.env.put(param.name, offset); // negative, liegt unter aktuellem BP
ri++;
@@ -656,15 +656,15 @@ public class GenASM implements Visitor<Void> {
e.arguments[i].welcome(this);
if (e.arguments[i].type.equals(Type.getFloatType())) {
asm.mov("%xmm0", "%rax0");
asm.mov("q", "%xmm0", "%rax0");
}
asm.push("%rax");
asm.push("q", "%rax");
}
// push args into local var locations, last arg is ontop of the stack
for (int i = e.arguments.length - 1; i >= 0; i--) {
asm.pop(this.env.get(this.currentFunctionParams[i].name) + "(%rbp)");
asm.pop("q", this.env.get(this.currentFunctionParams[i].name) + "(%rbp)");
}
asm.jmp(this.currentFunctionStartLabel);
@@ -713,10 +713,10 @@ public class GenASM implements Visitor<Void> {
for (var arg : e.arguments) {
arg.welcome(this);
if (arg.type.equals(Type.getFloatType())) {
asm.mov("%xmm0", "%rax");
asm.push("%rax");
asm.mov("q", "%xmm0", "%rax");
asm.push("q", "%rax");
} else {
asm.push("%rax");
asm.push("q", "%rax");
}
}
@@ -760,11 +760,11 @@ public class GenASM implements Visitor<Void> {
asm.newline();
}
asm.functionHead(mainName);
asm.push("%rbp");
asm.mov("%rsp", "%rbp");
asm.push("q", "%rbp");
asm.mov("q", "%rsp", "%rbp");
e.expression.welcome(this);
asm.mov("%rbp", "%rsp");
asm.pop("%rbp");
asm.mov("q", "%rbp", "%rsp");
asm.pop("q", "%rbp");
asm.ret();
asm.text(fw.getFloatSection());
@@ -810,7 +810,7 @@ public class GenASM implements Visitor<Void> {
// push rax to xmm0 if the result type is a float
if (e.type.equals(Type.getFloatType())) {
asm.mov("%rax", "%xmm0");
asm.mov("q", "%rax", "%xmm0");
}
return null;
@@ -824,10 +824,10 @@ public class GenASM implements Visitor<Void> {
// move float values from xmm0 to rax first
if (arg.type.equals(Type.getFloatType())) {
asm.mov("%xmm0", "%rax");
asm.mov("q", "%xmm0", "%rax");
}
asm.push("%rax");
asm.push("q", "%rax");
}
// allocate heap memory by calling malloc
@@ -837,7 +837,7 @@ public class GenASM implements Visitor<Void> {
// push args into struct memory, last arg is ontop of the stack
for (int i = e.args.length - 1; i >= 0; i--) {
asm.pop(Helper.getFieldOffset(structDef, i) + "(%rax)");
asm.pop("q", Helper.getFieldOffset(structDef, i) + "(%rax)");
}
return null;
@@ -866,11 +866,11 @@ public class GenASM implements Visitor<Void> {
// Move it from xmm0 rax if its a flaot
if (e.expression.type.equals(Type.getFloatType())) {
asm.mov("%xmm0", "%rax");
asm.mov("q", "%xmm0", "%rax");
}
// Push the expression onto the stack
asm.push("%rax");
asm.push("q", "%rax");
// move struct address into rax
asm.mov("q", offset, "%rbp", "%rax");
@@ -883,7 +883,7 @@ public class GenASM implements Visitor<Void> {
}
// pop the expression that is ontop of the stack into the field of the struct that has to be updated
asm.pop(Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)");
asm.pop("q", Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)");
asm.mov("q", 0 , "%rax"); // clear rax since an assignment has no result
return null;