From 982fc6417dc855afadf3e430a1de5926e1ea6e04 Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Sat, 14 Mar 2020 14:14:19 +0100 Subject: [PATCH 1/2] 32: Create Meta ASM Structure --- .../java/de/hsrm/compiler/Klang/Klang.java | 7 +- .../java/de/hsrm/compiler/Klang/asm/ASM.java | 262 ++++++++++ .../compiler/Klang/asm/mnemonics/Add.java | 34 ++ .../compiler/Klang/asm/mnemonics/Call.java | 14 + .../compiler/Klang/asm/mnemonics/Cmp.java | 21 + .../compiler/Klang/asm/mnemonics/Cqto.java | 8 + .../Klang/asm/mnemonics/Cvtsi2sd.java | 15 + .../compiler/Klang/asm/mnemonics/Div.java | 22 + .../Klang/asm/mnemonics/FunctionHead.java | 26 + .../compiler/Klang/asm/mnemonics/Idiv.java | 20 + .../compiler/Klang/asm/mnemonics/Imul.java | 16 + .../hsrm/compiler/Klang/asm/mnemonics/Je.java | 12 + .../hsrm/compiler/Klang/asm/mnemonics/Jg.java | 11 + .../compiler/Klang/asm/mnemonics/Jge.java | 11 + .../hsrm/compiler/Klang/asm/mnemonics/Jl.java | 11 + .../compiler/Klang/asm/mnemonics/Jle.java | 11 + .../compiler/Klang/asm/mnemonics/Jmp.java | 11 + .../compiler/Klang/asm/mnemonics/Jne.java | 11 + .../compiler/Klang/asm/mnemonics/Jnz.java | 11 + .../compiler/Klang/asm/mnemonics/Jump.java | 21 + .../hsrm/compiler/Klang/asm/mnemonics/Jz.java | 11 + .../compiler/Klang/asm/mnemonics/Label.java | 22 + .../Klang/asm/mnemonics/Mnemonic.java | 7 + .../compiler/Klang/asm/mnemonics/Mov.java | 50 ++ .../compiler/Klang/asm/mnemonics/Mul.java | 22 + .../compiler/Klang/asm/mnemonics/Neg.java | 14 + .../compiler/Klang/asm/mnemonics/Newline.java | 9 + .../asm/mnemonics/NoOperandMnemonic.java | 5 + .../asm/mnemonics/OneOperandMnemonic.java | 5 + .../compiler/Klang/asm/mnemonics/Pop.java | 19 + .../compiler/Klang/asm/mnemonics/Push.java | 24 + .../compiler/Klang/asm/mnemonics/Ret.java | 8 + .../compiler/Klang/asm/mnemonics/Sub.java | 22 + .../compiler/Klang/asm/mnemonics/Text.java | 15 + .../asm/mnemonics/TwoOperandMnemonic.java | 6 + .../compiler/Klang/asm/mnemonics/Ucomi.java | 22 + .../compiler/Klang/asm/mnemonics/Xor.java | 22 + .../hsrm/compiler/Klang/visitors/GenASM.java | 447 ++++++++---------- 38 files changed, 1032 insertions(+), 253 deletions(-) create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/ASM.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Call.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cqto.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cvtsi2sd.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/FunctionHead.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Je.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jg.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jge.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jl.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jle.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jmp.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jne.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jnz.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jump.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jz.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Label.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Neg.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Newline.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/NoOperandMnemonic.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/OneOperandMnemonic.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ret.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Text.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/TwoOperandMnemonic.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index 6bdc56a..029fdd6 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -129,11 +129,8 @@ public class Klang { } // Generate assembler code - // System.out.println("\nPrinting the assembler code"); - StringWriter wAsm = new StringWriter(); - GenASM.ExWriter exAsm = new GenASM.ExWriter(wAsm); - GenASM genasm = new GenASM(exAsm, mainName, structs); + GenASM genasm = new GenASM(mainName, structs); root.welcome(genasm); - generateOutput(out, wAsm.toString()); + generateOutput(out, genasm.toAsm()); } } diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java b/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java new file mode 100644 index 0000000..3e8e77f --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java @@ -0,0 +1,262 @@ +package de.hsrm.compiler.Klang.asm; + +import java.util.ArrayList; +import java.util.List; + +import de.hsrm.compiler.Klang.asm.mnemonics.*; + +public class ASM { + private List mnemonics; + + public ASM() { + this.mnemonics = new ArrayList(); + } + + public void push(String operand) { + mnemonics.add(new Push(operand)); + } + + public void push(String dataType, int immediate) { + mnemonics.add(new Push(dataType, immediate)); + } + + public void push(String dataType, String operand) { + 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)); + } + + public void mov(String dataType, String label, String src, String dst) { + mnemonics.add(new Mov(dataType, label, src, dst)); + } + + public void mov(String dataType, int offset, String src, String dst) { + mnemonics.add(new Mov(dataType, offset, src, dst)); + } + + public void mov(String dataType, String src, int offset, String dst) { + mnemonics.add(new Mov(dataType, src, offset, dst)); + } + + public void mov(String dataType, int immediate, String dst) { + 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, int immediate, String dst) { + mnemonics.add(new Cmp(dataType, immediate, dst)); + } + + public void je(int label) { + mnemonics.add(new Je(label)); + } + + public void je(String labelPrefix, int label) { + mnemonics.add(new Je(labelPrefix, label)); + } + + public void jmp(int label) { + mnemonics.add(new Jmp(label)); + } + + public void jmp(String labelPrefix, int label) { + mnemonics.add(new Jmp(labelPrefix, label)); + } + + public void jne(int label) { + mnemonics.add(new Jne(label)); + } + + public void jne(String labelPrefix, int label) { + mnemonics.add(new Jne(labelPrefix, label)); + } + + public void jg(int label) { + mnemonics.add(new Jg(label)); + } + + public void jg(String labelPrefix, int label) { + mnemonics.add(new Jg(labelPrefix, label)); + } + + public void jge(int label) { + mnemonics.add(new Jge(label)); + } + + public void jge(String labelPrefix, int label) { + mnemonics.add(new Jge(labelPrefix, label)); + } + + public void jl(int label) { + mnemonics.add(new Jl(label)); + } + + public void jl(String labelPrefix, int label) { + mnemonics.add(new Jl(labelPrefix, label)); + } + + public void jle(int label) { + mnemonics.add(new Jle(label)); + } + + public void jle(String labelPrefix, int label) { + mnemonics.add(new Jle(labelPrefix, label)); + } + + public void jz(int label) { + mnemonics.add(new Jz(label)); + } + + public void jz(String labelPrefix, int label) { + mnemonics.add(new Jz(labelPrefix, label)); + } + + public void jnz(int label) { + mnemonics.add(new Jnz(label)); + } + + public void jnz(String labelPrefix, int label) { + mnemonics.add(new Jnz(labelPrefix, label)); + } + + public void label(int label) { + mnemonics.add(new Label(label)); + } + + public void label(String labelPrefix, int label) { + 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)); + } + + public void add(String dataType, String src, int dstOffset, String dst) { + mnemonics.add(new Add(dataType, src, dstOffset, dst)); + } + + 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)); + } + + public void idiv(String dataType, String operand) { + 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 cqto() { + mnemonics.add(new Cqto()); + } + + public void ret() { + mnemonics.add(new Ret()); + } + + 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)); + } + + public void functionHead(String functionName) { + mnemonics.add(new FunctionHead(functionName)); + } + + public void call(String operand) { + mnemonics.add(new Call(operand)); + } + + public void text(String text) { + mnemonics.add(new Text(text)); + } + + public void newline() { + mnemonics.add(new Newline()); + } + + public void cvtsi2sd(String src, String dst) { + mnemonics.add(new Cvtsi2sd(src, dst)); + } + + public String toAsm() { + StringBuilder sb = new StringBuilder(); + mnemonics.stream().forEach(x -> { + for (int i = 0; i < x.indentation; i++) { + sb.append("\t"); + } + sb.append(x.toAsm()); + sb.append("\n"); + }); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java new file mode 100644 index 0000000..da69816 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java @@ -0,0 +1,34 @@ +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 Add(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + public Add(String dataType, String src, int dstOffset, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dstOffset + "(" + dst + ")"; + } + + public Add(String dataType, int immediate, String dst) { + this.dataType = dataType; + this.src = "$" + immediate; + this.dst = dst; + } + + @Override + public String toAsm() { + return "add" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Call.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Call.java new file mode 100644 index 0000000..657287e --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Call.java @@ -0,0 +1,14 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Call extends OneOperandMnemonic { + + public Call(String operand) { + this.operand = operand; + } + + @Override + public String toAsm() { + return "call " + this.operand; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java new file mode 100644 index 0000000..4ab5d3b --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java @@ -0,0 +1,21 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Cmp extends TwoOperandMnemonic { + + public Cmp(String src, String dst) { + this.dataType = ""; + this.src = src; + this.dst = dst; + } + + public Cmp(String dataType, int immediate, String dst) { + this.dataType = dataType; + this.src = "$" + immediate; + this.dst = dst; + } + + @Override + public String toAsm() { + return "cmp" + this.dataType + " " + this.src + ", " + this.dst; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cqto.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cqto.java new file mode 100644 index 0000000..0724a34 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cqto.java @@ -0,0 +1,8 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Cqto extends NoOperandMnemonic { + @Override + public String toAsm() { + return "cqto"; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cvtsi2sd.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cvtsi2sd.java new file mode 100644 index 0000000..9c7df5b --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cvtsi2sd.java @@ -0,0 +1,15 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Cvtsi2sd extends TwoOperandMnemonic { + + public Cvtsi2sd(String src, String dst) { + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "cvtsi2sd " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java new file mode 100644 index 0000000..dfb72f0 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java @@ -0,0 +1,22 @@ +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 Div(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "div" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/FunctionHead.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/FunctionHead.java new file mode 100644 index 0000000..4591837 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/FunctionHead.java @@ -0,0 +1,26 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class FunctionHead extends NoOperandMnemonic { + + public String functionName; + + public FunctionHead(String functionName) { + this.functionName = functionName; + this.indentation = 0; + } + + @Override + public String toAsm() { + StringBuilder sb = new StringBuilder(); + sb.append(".globl "); + sb.append(this.functionName); + sb.append("\n"); + sb.append(".type "); + sb.append(this.functionName); + sb.append(", @function\n"); + sb.append(this.functionName); + sb.append(":"); + return sb.toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java new file mode 100644 index 0000000..5aa776e --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java @@ -0,0 +1,20 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Idiv extends OneOperandMnemonic{ + public String dataType = ""; + + public Idiv(String operand) { + this.operand = operand; + } + + public Idiv(String dataType, String operand) { + this.dataType = dataType; + this.operand = operand; + } + + @Override + public String toAsm() { + return "idiv" + this.dataType + " " + this.operand; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java new file mode 100644 index 0000000..0e75e4a --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java @@ -0,0 +1,16 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Imul extends TwoOperandMnemonic { + public String dataType = "q"; + + public Imul(String src, String dst) { + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "imul" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Je.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Je.java new file mode 100644 index 0000000..4ed0ca3 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Je.java @@ -0,0 +1,12 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Je extends Jump { + + public Je(int label) { + super("je", label); + } + + public Je(String labelPrefix, int label) { + super("je", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jg.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jg.java new file mode 100644 index 0000000..65b7af0 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jg.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jg extends Jump { + public Jg(int label) { + super("jg", label); + } + + public Jg(String labelPrefix, int label) { + super("jg", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jge.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jge.java new file mode 100644 index 0000000..1bcf479 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jge.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jge extends Jump { + public Jge(int label) { + super("jge", label); + } + + public Jge(String labelPrefix, int label) { + super("jge", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jl.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jl.java new file mode 100644 index 0000000..9c8754b --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jl.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jl extends Jump { + public Jl(int label) { + super("jl", label); + } + + public Jl(String labelPrefix, int label) { + super("jl", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jle.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jle.java new file mode 100644 index 0000000..7de1233 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jle.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jle extends Jump { + public Jle(int label) { + super("jle", label); + } + + public Jle(String labelPrefix, int label) { + super("jle", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jmp.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jmp.java new file mode 100644 index 0000000..d5dc3a7 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jmp.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jmp extends Jump { + public Jmp(String labelPrefix, int label) { + super("jmp", labelPrefix, label); + } + + public Jmp(int label) { + super("jmp", label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jne.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jne.java new file mode 100644 index 0000000..71c237a --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jne.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jne extends Jump { + public Jne(int label) { + super("jne", label); + } + + public Jne(String labelPrefix, int label) { + super("jne", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jnz.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jnz.java new file mode 100644 index 0000000..245368b --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jnz.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jnz extends Jump { + public Jnz(int label) { + super("jnz", label); + } + + public Jnz(String labelPrefix, int label) { + super("jnz", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jump.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jump.java new file mode 100644 index 0000000..bf9b866 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jump.java @@ -0,0 +1,21 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public abstract class Jump extends Mnemonic { + protected String opcode; + public String labelPrefix = "L"; + public int label; + public Jump(String opcode, String labelPrefix, int label) { + this.opcode = opcode; + this.labelPrefix = labelPrefix; + this.label = label; + } + + public Jump(String opcode, int label) { + this.opcode = opcode; + this.label = label; + } + @Override + public String toAsm() { + return this.opcode + " ." + this.labelPrefix + this.label; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jz.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jz.java new file mode 100644 index 0000000..087f69e --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Jz.java @@ -0,0 +1,11 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Jz extends Jump { + public Jz(int label) { + super("jz", label); + } + + public Jz(String labelPrefix, int label) { + super("jz", labelPrefix, label); + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Label.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Label.java new file mode 100644 index 0000000..85906f3 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Label.java @@ -0,0 +1,22 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Label extends Mnemonic { + public String labelPrefix = "L"; + public int label; + + public Label(int label) { + this.label = label; + this.indentation = 0; + } + + public Label(String labelPrefix, int label) { + this.labelPrefix = labelPrefix; + this.label = label; + this.indentation = 0; + } + + @Override + public String toAsm() { + return "." + this.labelPrefix + this.label + ":"; + } +} diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java new file mode 100644 index 0000000..ce25776 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java @@ -0,0 +1,7 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public abstract class Mnemonic { + public String dataType = ""; + public abstract String toAsm(); + public int indentation = 2; +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java new file mode 100644 index 0000000..4a18870 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java @@ -0,0 +1,50 @@ +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; + this.dst = dst; + } + + public Mov(String dataType, String label, String src, String dst) { + this.dataType = dataType; + this.src = label + "(" + src + ")"; + this.dst = dst; + } + + public Mov(String dataType, int offset, String src, String dst) { + this.dataType = dataType; + this.src = offset + "(" + src + ")"; + this.dst = dst; + } + + public Mov(String dataType, int immediate, String dst) { + this.dataType = dataType; + this.src = "$" + immediate; + this.dst = dst; + } + + public Mov(String dataType, String src, int offset, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = offset + "(" + dst + ")"; + } + + @Override + public String toAsm() { + return "mov" + this.dataType + " " + this.src + ", " + this.dst; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java new file mode 100644 index 0000000..5ff1038 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java @@ -0,0 +1,22 @@ +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 Mul(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "mul" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Neg.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Neg.java new file mode 100644 index 0000000..e4f77b2 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Neg.java @@ -0,0 +1,14 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Neg extends OneOperandMnemonic { + + public Neg(String operand) { + this.operand = operand; + } + + @Override + public String toAsm() { + return "neg " + this.operand; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Newline.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Newline.java new file mode 100644 index 0000000..0865e06 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Newline.java @@ -0,0 +1,9 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Newline extends NoOperandMnemonic { + + @Override + public String toAsm() { + return ""; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/NoOperandMnemonic.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/NoOperandMnemonic.java new file mode 100644 index 0000000..26cefd3 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/NoOperandMnemonic.java @@ -0,0 +1,5 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public abstract class NoOperandMnemonic extends Mnemonic { + +} diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/OneOperandMnemonic.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/OneOperandMnemonic.java new file mode 100644 index 0000000..4d8678c --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/OneOperandMnemonic.java @@ -0,0 +1,5 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public abstract class OneOperandMnemonic extends Mnemonic { + public String operand; +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java new file mode 100644 index 0000000..cf44583 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java @@ -0,0 +1,19 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Pop extends OneOperandMnemonic { + public String dataType = "q"; + public Pop(String operand) { + this.operand = operand; + } + + public Pop(String dataType, String operand) { + this.dataType = dataType; + this.operand = operand; + } + + @Override + public String toAsm() { + return "pop" + this.dataType + " " + this.operand; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java new file mode 100644 index 0000000..12608ec --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java @@ -0,0 +1,24 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Push extends OneOperandMnemonic { + public Push(String operand){ + this.dataType = "q"; + this.operand = operand; + } + + public Push(String dataType, int immediate){ + this.dataType = dataType; + this.operand = "$" + immediate; + } + + public Push(String dataType, String operand) { + this.dataType = dataType; + this.operand = operand; + } + + @Override + public String toAsm() { + return "push" + this.dataType + " " + this.operand; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ret.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ret.java new file mode 100644 index 0000000..af9ccef --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ret.java @@ -0,0 +1,8 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Ret extends NoOperandMnemonic { + @Override + public String toAsm() { + return "ret"; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java new file mode 100644 index 0000000..7b23345 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java @@ -0,0 +1,22 @@ +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 Sub(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "sub" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Text.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Text.java new file mode 100644 index 0000000..a32307e --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Text.java @@ -0,0 +1,15 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public class Text extends NoOperandMnemonic { + public String text; + + public Text(String text) { + this.text = text; + this.indentation = 0; + } + + @Override + public String toAsm() { + return this.text; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/TwoOperandMnemonic.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/TwoOperandMnemonic.java new file mode 100644 index 0000000..22269bf --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/TwoOperandMnemonic.java @@ -0,0 +1,6 @@ +package de.hsrm.compiler.Klang.asm.mnemonics; + +public abstract class TwoOperandMnemonic extends Mnemonic { + public String src; + public String dst; +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java new file mode 100644 index 0000000..d3afa17 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java @@ -0,0 +1,22 @@ +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 Ucomi(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "ucomi" + this.dataType + " " + this.src + ", " + this.dst; + } + +} diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java new file mode 100644 index 0000000..9989576 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java @@ -0,0 +1,22 @@ +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 Xor(String dataType, String src, String dst) { + this.dataType = dataType; + this.src = src; + this.dst = dst; + } + + @Override + public String toAsm() { + return "xor" + this.dataType + " " + this.src + ", " + this.dst; + } + +} \ No newline at end of file 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 89df0ed..8796f00 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -1,12 +1,12 @@ package de.hsrm.compiler.Klang.visitors; -import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import de.hsrm.compiler.Klang.asm.ASM; import de.hsrm.compiler.Klang.helper.Helper; import de.hsrm.compiler.Klang.nodes.*; import de.hsrm.compiler.Klang.nodes.expressions.*; @@ -17,47 +17,6 @@ import de.hsrm.compiler.Klang.nodes.statements.*; import de.hsrm.compiler.Klang.types.Type; public class GenASM implements Visitor { - - public static class ExWriter { - Writer w; - String indent = ""; - - void addIndent() { - indent = indent + " "; - } - - void subIndent() { - indent = indent.substring(2); - } - - void nl() { - write("\n" + indent); - } - - int lbl = 0; - - int next() { - return lbl++; - } - - public ExWriter(Writer w) { - this.w = w; - } - - void lnwrite(Object o) { - nl(); - write(o); - } - - void write(Object o) { - try { - w.write(o + ""); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - private class FloatWriter { private StringBuilder sb = new StringBuilder(); private int id = -1; @@ -104,7 +63,7 @@ public class GenASM implements Visitor { } } - public ExWriter ex; + private ASM asm; private FloatWriter fw = new FloatWriter(); private String mainName; Map env = new HashMap<>(); @@ -112,81 +71,81 @@ public class GenASM implements Visitor { Set vars; String[] registers = { "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" }; String[] floatRegisters = { "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7" }; - private int lCount = 0; // Invariante: lCount ist benutzt + private int lCount = 0; // Invariant: lCount is used private int currentFunctionStartLabel = 0; private Parameter[] currentFunctionParams; - private void intToFloat(String src, String dst) { - this.ex.write(" cvtsi2sd " + src + ", " + dst + "\n"); - } - private boolean prepareRegisters(Expression lhs, Expression rhs) { boolean lhsIsFloat = lhs.type.equals(Type.getFloatType()); boolean rhsIsFloat = rhs.type.equals(Type.getFloatType()); if (lhsIsFloat && rhsIsFloat) { lhs.welcome(this); - this.ex.write(" movsd %xmm0, %xmm2\n"); + asm.mov("sd", "%xmm0", "%xmm2"); rhs.welcome(this); - this.ex.write(" movsd %xmm0, %xmm1\n"); - this.ex.write(" movsd %xmm2, %xmm0\n"); + asm.mov("sd", "%xmm2", "%xmm0"); + asm.mov("%xmm2", "%xmm0"); return true; } else if (lhsIsFloat && !rhsIsFloat) { lhs.welcome(this); rhs.welcome(this); - this.intToFloat("%rax", "%xmm1"); + asm.cvtsi2sd("%rax", "%xmm1"); return true; } else if (!lhsIsFloat && rhsIsFloat) { lhs.welcome(this); - this.intToFloat("%rax", "%xmm2"); + asm.cvtsi2sd("%rax", "%xmm2"); rhs.welcome(this); - this.ex.write(" movsd %xmm0, %xmm1\n"); - this.ex.write(" movsd %xmm2, %xmm0\n"); + asm.mov("sd", "%xmm0", "%xmm1"); + asm.mov("sd", "%xmm2", "%xmm0"); return true; } else { lhs.welcome(this); - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); rhs.welcome(this); - this.ex.write(" movq %rax, %rbx\n"); - this.ex.write(" popq %rax\n"); + asm.mov("%rax", "%rbx"); + asm.pop("%rax"); return false; } } - public GenASM(ExWriter ex, String mainName, Map structs) { - this.ex = ex; + public GenASM(String mainName, Map structs) { this.mainName = mainName; this.structs = structs; + this.asm = new ASM(); } - public GenASM(ExWriter ex, Map structs) { - this(ex, "main", structs); + public GenASM(Map structs) { + this("main", structs); + } + + public String toAsm() { + return asm.toAsm(); } @Override public Void visit(IntegerExpression e) { - this.ex.write(" movq $" + e.value + ", %rax\n"); + asm.mov(e.value, "%rax"); return null; } @Override public Void visit(FloatExpression e) { String floatLabel = fw.getFloat(e.value); - this.ex.write(" movsd " + floatLabel + "(%rip), %xmm0\n"); + asm.mov("sd", floatLabel, "%rip", "%xmm0"); return null; } @Override public Void visit(BooleanExpression e) { - this.ex.write(" movq $" + (e.value ? 1 : 0) + ", %rax\n"); + asm.mov(e.value ? 1 : 0, "%rax"); return null; } @Override public Void visit(Variable e) { if (e.type.equals(Type.getFloatType())) { - this.ex.write(" movsd " + this.env.get(e.name) + "(%rbp), %xmm0\n"); + asm.mov("sd", this.env.get(e.name), "%rbp", "%xmm0"); } else { - this.ex.write(" movq " + this.env.get(e.name) + "(%rbp), %rax\n"); + asm.mov("q", this.env.get(e.name), "%rbp", "%rax"); } return null; } @@ -198,18 +157,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm1, %xmm0\n"); + asm.ucomi("sd", "%xmm1", "xmm0"); } else { - this.ex.write(" cmp %rbx, %rax\n"); + asm.cmp("%rbx", "%rax"); } - this.ex.write(" je .L" + lblTrue + "\n"); + asm.je(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -220,18 +179,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm0, %xmm1\n"); + asm.ucomi("sd", "%xmm0", "%xmm1"); } else { - this.ex.write(" cmp %rax, %rbx\n"); + asm.cmp("%rax", "%rbx"); } - this.ex.write(" jne .L" + lblTrue + "\n"); + asm.jne(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -242,18 +201,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm1, %xmm0\n"); + asm.ucomi("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" cmp %rbx, %rax\n"); + asm.cmp("%rbx", "%rax"); } - this.ex.write(" jg .L" + lblTrue + "\n"); + asm.jg(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -264,18 +223,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm1, %xmm0\n"); + asm.ucomi("sd", "%xmm1", "xmm0"); } else { - this.ex.write(" cmp %rbx, %rax\n"); + asm.cmp("%rbx", "%rax"); } - this.ex.write(" jge .L" + lblTrue + "\n"); + asm.jge(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -286,18 +245,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm1, %xmm0\n"); + asm.ucomi("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" cmp %rbx, %rax\n"); + asm.cmp("%rbx", "%rax"); } - this.ex.write(" jl .L" + lblTrue + "\n"); + asm.jl(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -308,18 +267,18 @@ public class GenASM implements Visitor { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" ucomisd %xmm1, %xmm0\n"); + asm.ucomi("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" cmp %rbx, %rax\n"); + asm.cmp("%rbx", "%rax"); } - this.ex.write(" jle .L" + lblTrue + "\n"); + asm.jle(lblTrue); // false - this.ex.write(" movq $0, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblTrue + ":\n"); + asm.mov(0, "%rax"); + asm.jmp(lblEnd); + asm.label(lblTrue); // true - this.ex.write(" movq $1, %rax\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.mov(1, "%rax"); + asm.label(lblEnd); return null; } @@ -327,9 +286,9 @@ public class GenASM implements Visitor { public Void visit(AdditionExpression e) { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" addsd %xmm1, %xmm0\n"); + asm.add("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" addq %rbx, %rax\n"); + asm.add("%rbx", "%rax"); } return null; } @@ -338,9 +297,9 @@ public class GenASM implements Visitor { public Void visit(SubstractionExpression e) { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" subsd %xmm1, %xmm0\n"); + asm.sub("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" subq %rbx, %rax\n"); + asm.sub("%rbx", "%rax"); } return null; } @@ -349,9 +308,9 @@ public class GenASM implements Visitor { public Void visit(MultiplicationExpression e) { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" mulsd %xmm1, %xmm0\n"); + asm.mul("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" imulq %rbx, %rax\n"); + asm.imul("%rbx", "%rax"); } return null; } @@ -360,10 +319,10 @@ public class GenASM implements Visitor { public Void visit(DivisionExpression e) { boolean isFloatOperation = this.prepareRegisters(e.lhs, e.rhs); if (isFloatOperation) { - this.ex.write(" divsd %xmm1, %xmm0\n"); + asm.div("sd", "%xmm1", "%xmm0"); } else { - this.ex.write(" cqto\n"); // sign extend rax into rdx since we're dealing with signed values - this.ex.write(" idiv %rbx\n"); // %rax/%rbx, quotient now in %rax + asm.cqto(); + asm.idiv("%rbx"); } return null; } @@ -371,13 +330,13 @@ public class GenASM implements Visitor { @Override public Void visit(ModuloExpression e) { e.lhs.welcome(this); - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); e.rhs.welcome(this); - this.ex.write(" movq %rax, %rbx\n"); - this.ex.write(" popq %rax\n"); - this.ex.write(" cqto\n"); // sign extend rax into rdx since we're dealing with signed values - this.ex.write(" idiv %rbx\n"); // %rax/%rbx, remainder now in %rdx - this.ex.write(" movq %rdx, %rax\n"); + asm.mov("%rax", "%rbx"); + asm.pop("%rax"); + asm.cqto(); + asm.idiv("%rbx"); + asm.mov("%rdx", "%rax"); return null; } @@ -386,10 +345,10 @@ public class GenASM implements Visitor { e.lhs.welcome(this); if (e.lhs.type.equals(Type.getFloatType())) { String floatLabel = fw.getNegateFloat(); - this.ex.write(" movsd " + floatLabel + "(%rip), %xmm1\n"); - this.ex.write(" xorpd %xmm1, %xmm0\n"); + asm.mov("sd", floatLabel, "%rip", "%xmm1"); + asm.xor("pd", "%xmm1", "%xmm0"); } else { - this.ex.write(" neg %rax\n"); + asm.neg("%rax"); } return null; } @@ -404,28 +363,28 @@ public class GenASM implements Visitor { // Wenn LHS != 0 bedeutet das true // also können wir direkt sagen dass das Ergebnis true ist e.lhs.welcome(this); - this.ex.write(" cmpq $0, %rax\n"); - this.ex.write(" jne .L" + lblTrue + "\n"); + asm.cmp("q", 0, "%rax"); + asm.jne(lblTrue); // LHS war false, also werte RHS aus // Wenn RHS == 0 bedeutet das false, // also ist das Gesamtergebnis false e.rhs.welcome(this); - this.ex.write(" cmpq $0, %rax\n"); - this.ex.write(" je .L" + lblFalse + "\n"); + asm.cmp("q", 0, "%rax"); + asm.je(lblFalse); // Die Expression wertet zu true aus // Springe am false Teil vorbei - this.ex.write(".L" + lblTrue + ":\n"); - this.ex.write(" movq $1, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); + asm.label(lblTrue); + asm.mov(1, "%rax"); + asm.jmp(lblEnd); // Die Expressoin wertet zu false aus - this.ex.write(".L" + lblFalse + ":\n"); - this.ex.write(" movq $0, %rax\n"); + asm.label(lblFalse); + asm.mov(0, "%rax"); // Das hier ist das ende - this.ex.write(".L" + lblEnd + ":\n"); + asm.label(lblEnd); return null; } @@ -439,28 +398,28 @@ public class GenASM implements Visitor { // Wenn LHS == 0, bedeutet das false // also können wir direkt sagen dass das Ergebnis false ist e.lhs.welcome(this); - this.ex.write(" cmpq $0, %rax\n"); - this.ex.write(" je .L" + lblFalse + "\n"); + asm.cmp("q", 0, "%rax"); + asm.je(lblFalse); // LHS war true, also werte RHS aus. // Wenn RHS == 0, bedeutet das false // also ist das Gesamtergebnis false e.rhs.welcome(this); - this.ex.write(" cmpq $0, %rax\n"); - this.ex.write(" je .L" + lblFalse + "\n"); + asm.cmp("q", 0, "%rax"); + asm.je(lblFalse); // Die Expression wertet zu true aus // Springe am false Teil vorbei - this.ex.write(".L" + lblTrue + ":\n"); - this.ex.write(" movq $1, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); + asm.label(lblTrue); + asm.mov(1, "%rax"); + asm.jmp(lblEnd); // Die Expressoin wertet zu false aus - this.ex.write(".L" + lblFalse + ":\n"); - this.ex.write(" movq $0, %rax\n"); + asm.label(lblFalse); + asm.mov(0, "%rax"); // Das hier ist das ende - this.ex.write(".L" + lblEnd + ":\n"); + asm.label(lblEnd); return null; } @@ -473,21 +432,21 @@ public class GenASM implements Visitor { // Wenn LHS != 0 bedeutet das true, also jumpe zum false Teil // Wenn nicht, falle durch zum true Teil e.lhs.welcome(this); - this.ex.write(" cmpq $0, %rax\n"); - this.ex.write(" jne .L" + lblFalse + "\n"); + asm.cmp("q", 0, "%rax"); + asm.jne(lblFalse); // Hier ist das Ergebnis true // Springe am false Teil vorbei - this.ex.write(" movq $1, %rax\n"); - this.ex.write(" jmp .L" + lblEnd + "\n"); + asm.mov(1, "%rax"); + asm.jmp(lblEnd); // Hier ist das Ergebnis false // Falle zum Ende durch - this.ex.write(".L" + lblFalse + ":\n"); - this.ex.write("movq $0, %rax\n"); + asm.label(lblFalse); + asm.mov(0, "%rax"); // Hier ist das Ende - this.ex.write(".L" + lblEnd + ":\n"); + asm.label(lblEnd); return null; } @@ -497,25 +456,25 @@ public class GenASM implements Visitor { int lblEnd = ++lCount; boolean hasElse = e.alt != null || e.elif != null; e.cond.welcome(this); - this.ex.write(" cmp $0, %rax\n"); + asm.cmp("", 0, "%rax"); // in case of cond evaluating to false, jump to else/elif // Jump to end if there is no else part, this saves a label declaration if (hasElse) { - this.ex.write(" jz .L" + lblElse + "\n"); + asm.jz(lblElse); } else { - this.ex.write(" jz .L" + lblEnd + "\n"); + asm.jz(lblEnd); } e.then.welcome(this); if (hasElse) { - this.ex.write(" jmp .L" + lblEnd + "\n"); - this.ex.write(".L" + lblElse + ":\n"); + asm.jmp(lblEnd); + asm.label(lblElse); if (e.alt != null) { e.alt.welcome(this); } else { e.elif.welcome(this); } } - this.ex.write(".L" + lblEnd + ":\n"); + asm.label(lblEnd); return null; } @@ -523,24 +482,24 @@ public class GenASM implements Visitor { public Void visit(WhileLoop e) { int lblCond = ++lCount; int lblEnd = ++lCount; - this.ex.write(".L" + lblCond + ":\n"); + asm.label(lblCond); e.cond.welcome(this); - this.ex.write(" cmp $0, %rax\n"); - this.ex.write(" jz .L" + lblEnd + "\n"); + asm.cmp("", 0, "%rax"); + asm.jz(lblEnd); e.block.welcome(this); - this.ex.write(" jmp .L" + lblCond + "\n"); - this.ex.write(".L" + lblEnd + ":\n"); + asm.jmp(lblCond); + asm.label(lblEnd); return null; } @Override public Void visit(DoWhileLoop e) { int lblStart = ++lCount; - this.ex.write(".L" + lblStart + ":\n"); + asm.label(lblStart); e.block.welcome(this); e.cond.welcome(this); - this.ex.write(" cmp $0, %rax\n"); - this.ex.write(" jnz .L" + lblStart + "\n"); + asm.cmp("", 0, "%rax"); + asm.jnz(lblStart); return null; } @@ -549,15 +508,14 @@ public class GenASM implements Visitor { int lblStart = ++lCount; int lblEnd = ++lCount; e.init.welcome(this); - this.ex.write(".L" + lblStart + ":\n"); + asm.label(lblStart); e.condition.welcome(this); - this.ex.write(" cmp $0, %rax\n"); - this.ex.write(" jz .L" + lblEnd + "\n"); + asm.cmp("", 0, "%rax"); + asm.jz(lblEnd); e.block.welcome(this); e.step.welcome(this); - this.ex.write(" jmp .L" + lblStart + "\n"); - this.ex.write(".L" + lblEnd + ":\n"); - + asm.jmp(lblStart); + asm.label(lblEnd); return null; } @@ -568,12 +526,12 @@ public class GenASM implements Visitor { if (e.expression != null) { e.expression.welcome(this); int offset = this.env.get(e.name); - + if (e.expression.type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, %rax\n"); + asm.mov("q", "%xmm0", "%rax"); } - this.ex.write(" movq %rax, " + offset + "(%rbp)\n"); + asm.mov("q", "%rax", offset, "%rbp"); } return null; } @@ -586,20 +544,20 @@ public class GenASM implements Visitor { // Determine where the result of this expression was placed into // and move it onto the stack from there if (e.expression.type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, " + offset + "(%rbp)\n"); + asm.mov("q", "%xmm0", offset, "%rbp"); } else { - this.ex.write(" movq %rax, " + offset + "(%rbp)\n"); + asm.mov("q", "%rax", offset, "%rbp"); } - + return null; } @Override public Void visit(ReturnStatement e) { e.expression.welcome(this); - this.ex.write(" movq %rbp, %rsp\n"); - this.ex.write(" popq %rbp\n"); - this.ex.write(" ret\n"); + asm.mov("%rbp", "%rsp"); + asm.pop("%rbp"); + asm.ret(); return null; } @@ -616,12 +574,10 @@ public class GenASM implements Visitor { int lblStart = ++lCount; this.currentFunctionStartLabel = lblStart; this.currentFunctionParams = e.parameters; - this.ex.write(".globl " + e.name + "\n"); - this.ex.write(".type " + e.name + ", @function\n"); - this.ex.write(e.name + ":\n"); - this.ex.write(" pushq %rbp\n"); - this.ex.write(" movq %rsp, %rbp\n"); - this.ex.write(".L" + lblStart + ":\n"); + asm.functionHead(e.name); + asm.push("%rbp"); + asm.mov("%rsp", "%rbp"); + asm.label(lblStart); // hole die anzahl der lokalen variablen this.vars = new TreeSet(); @@ -661,19 +617,19 @@ public class GenASM implements Visitor { } } } - + offset = 0; ri = 0; fi = 0; for (var param: registerParameters) { if (param.type.equals(Type.getFloatType())) { - this.ex.write(" movq "+ this.floatRegisters[fi] + ", %rax\n"); - this.ex.write(" pushq %rax\n"); + asm.mov(this.floatRegisters[fi], "%rax"); + asm.push("%rax"); offset -= 8; this.env.put(param.name, offset); // negative, liegt unter aktuellem BP fi++; } else { - this.ex.write(" pushq " + this.registers[ri] + "\n"); + asm.push(this.registers[ri]); offset -= 8; this.env.put(param.name, offset); // negative, liegt unter aktuellem BP ri++; @@ -683,7 +639,7 @@ public class GenASM implements Visitor { // Reserviere Platz auf dem Stack für jede lokale variable for (String lok_var : vars) { offset -= 8; - this.ex.write(" pushq $0\n"); + asm.push("q", 0); this.env.put(lok_var, offset); } @@ -700,18 +656,18 @@ public class GenASM implements Visitor { e.arguments[i].welcome(this); if (e.arguments[i].type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, %rax\n"); + asm.mov("%xmm0", "%rax0"); } - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); } // push args into local var locations, last arg is ontop of the stack for (int i = e.arguments.length - 1; i >= 0; i--) { - this.ex.write(" popq " + this.env.get(this.currentFunctionParams[i].name) + "(%rbp)\n"); + asm.pop(this.env.get(this.currentFunctionParams[i].name) + "(%rbp)"); } - this.ex.write(" jmp .L" + this.currentFunctionStartLabel + "\n"); + asm.jmp(this.currentFunctionStartLabel); return null; } @@ -724,12 +680,12 @@ public class GenASM implements Visitor { // Mapping arguments index -> all purpose registers index int[] rIdxs = new int[this.registers.length]; int ri = -1; - + // Mapping arguments index -> stack ArrayList stackIdxs = new ArrayList(); // Go through arguments - // sort them into the memory regions they go when being passed to functions + // sort them into the memory regions they go when being passed to functions for (int i = 0; i < e.arguments.length; i++) { var arg = e.arguments[i]; if (arg.type.equals(Type.getFloatType())) { @@ -757,10 +713,10 @@ public class GenASM implements Visitor { for (var arg : e.arguments) { arg.welcome(this); if (arg.type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, %rax\n"); - this.ex.write(" pushq %rax\n"); + asm.mov("%xmm0", "%rax"); + asm.push("%rax"); } else { - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); } } @@ -768,14 +724,14 @@ public class GenASM implements Visitor { for (int i = 0; i <= fi; i++) { int indexInArguments = xmmIdxs[i]; int rspOffset = (((e.arguments.length - indexInArguments) - 1) * 8); - this.ex.write(" movsd " + rspOffset + "(%rsp), " + this.floatRegisters[i] + "\n"); + asm.mov("sd", rspOffset, "%rsp", this.floatRegisters[i]); } // Move primitives from stack to all purpose registers for (int i = 0; i <= ri; i++) { int indexInArguments = rIdxs[i]; int rspOffset = (((e.arguments.length - indexInArguments) - 1) * 8); - this.ex.write(" movq " + rspOffset + "(%rsp), " + this.registers[i] + "\n"); + asm.mov("q", rspOffset, "%rsp", this.registers[i]); } // Move everything else from a higher stack position to our stack frame start @@ -784,39 +740,34 @@ public class GenASM implements Visitor { stackStartOffset -= 8; int indexInArguments = stackIdxs.get(i); int rspOffset = (((e.arguments.length - indexInArguments) - 1) * 8); - this.ex.write(" movq " + rspOffset + "(%rsp), %rax\n"); - this.ex.write(" movq %rax, " + stackStartOffset + "(%rsp)\n"); + asm.mov("q", rspOffset, "%rsp", "%rax"); + asm.mov("q", "%rax", stackStartOffset, "%rsp"); } // Rescue RSP - this.ex.write(" addq $" + stackStartOffset + ", %rsp\n"); + asm.add("q", stackStartOffset, "%rsp"); } - this.ex.write(" call " + e.name + "\n"); + asm.call(e.name); return null; } @Override public Void visit(Program e) { - this.ex.write(".text\n"); + asm.text(".text"); for (var func : e.funcs) { func.welcome(this); - this.ex.write("\n"); + asm.newline(); } - this.ex.write(".globl " + mainName + "\n"); - this.ex.write(".type " + mainName + ", @function\n"); - this.ex.write(mainName + ":\n"); - this.ex.write(" pushq %rbp\n"); - this.ex.write(" movq %rsp, %rbp\n"); + asm.functionHead(mainName); + asm.push("%rbp"); + asm.mov("%rsp", "%rbp"); e.expression.welcome(this); + asm.mov("%rbp", "%rsp"); + asm.pop("%rbp"); + asm.ret(); - this.ex.write(" movq %rbp, %rsp\n"); - this.ex.write(" popq %rbp\n"); - this.ex.write(" ret\n"); - - // PRINT FLOATS HERE - this.ex.write("\n"); - this.ex.write(fw.getFloatSection()); + asm.text(fw.getFloatSection()); return null; } @@ -845,21 +796,21 @@ public class GenASM implements Visitor { int offset = this.env.get(e.varName); // move struct address into rax - this.ex.write(" movq " + offset + "(%rbp), %rax\n"); + asm.mov("q", offset, "%rbp", "%rax"); // "follow" the first path element by moving the referenced value into rax - this.ex.write(" movq " + Helper.getFieldOffset(structDef, e.path[0]) + "(%rax), %rax\n"); + asm.mov("q", Helper.getFieldOffset(structDef, e.path[0]), "%rax", "%rax"); for (int i = 1; i < e.path.length; i++) { // "follow" the current path element structDef = this.structs.get(structDef.fields[Helper.getFieldIndex(structDef, e.path[i - 1])].type.getName()); - this.ex.write(" movq " + Helper.getFieldOffset(structDef, e.path[i]) + "(%rax), %rax\n"); + asm.mov("q", Helper.getFieldOffset(structDef, e.path[i]), "%rax", "%rax"); } // desired value now in rax // push rax to xmm0 if the result type is a float if (e.type.equals(Type.getFloatType())) { - this.ex.write(" movq %rax, %xmm0\n"); + asm.mov("%rax", "%xmm0"); } return null; @@ -873,20 +824,20 @@ public class GenASM implements Visitor { // move float values from xmm0 to rax first if (arg.type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, %rax\n"); + asm.mov("%xmm0", "%rax"); } - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); } // allocate heap memory by calling malloc var structDef = this.structs.get(e.structName); - this.ex.write(" movl $" + Helper.getFieldSizeBytes(structDef) + ", %edi\n"); - this.ex.write(" call malloc@PLT\n"); // struct address now in rax + asm.mov("l", Helper.getFieldSizeBytes(structDef), "%edi"); + asm.call("malloc@PLT"); // push args into struct memory, last arg is ontop of the stack for (int i = e.args.length - 1; i >= 0; i--) { - this.ex.write(" popq " + Helper.getFieldOffset(structDef, i) + "(%rax)\n"); + asm.pop(Helper.getFieldOffset(structDef, i) + "(%rax)"); } return null; @@ -894,14 +845,14 @@ public class GenASM implements Visitor { @Override public Void visit(NullExpression e) { - this.ex.write(" movq $0, %rax\n"); + asm.mov("q", 0 , "%rax"); return null; } @Override public Void visit(DestructorCall e) { - this.ex.write(" movq " +this.env.get(e.name) + "(%rbp), %rdi\n"); - this.ex.write(" call free@PLT\n"); + asm.mov("q", this.env.get(e.name), "%rbp", "%rdi"); + asm.call("free@PLT"); return null; } @@ -915,25 +866,25 @@ public class GenASM implements Visitor { // Move it from xmm0 rax if its a flaot if (e.expression.type.equals(Type.getFloatType())) { - this.ex.write(" movq %xmm0, %rax\n"); + asm.mov("%xmm0", "%rax"); } // Push the expression onto the stack - this.ex.write(" pushq %rax\n"); + asm.push("%rax"); // move struct address into rax - this.ex.write(" movq " + offset + "(%rbp), %rax\n"); + asm.mov("q", offset, "%rbp", "%rax"); - // If there are at least two elements in the path, + // If there are at least two elements in the path, // move the address of the next referenced struct into rax for (int i = 1; i < e.path.length - 1; i++) { structDef = this.structs.get(structDef.fields[Helper.getFieldIndex(structDef, e.path[i - 1])].type.getName()); - this.ex.write(" movq " + Helper.getFieldOffset(structDef, e.path[i]) + "(%rax), %rax\n"); + 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 - this.ex.write(" popq " + Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)\n"); - this.ex.write(" movq $0, %rax\n"); // clear rax sind an assignment has no result + asm.pop(Helper.getFieldOffset(structDef, fieldNameToUpdate) + "(%rax)"); + asm.mov("q", 0 , "%rax"); // clear rax since an assignment has no result return null; } From 49b024b95f2e208b7641c5aac8edb048a787266a Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 17 Mar 2020 16:02:03 +0100 Subject: [PATCH 2/2] 32: Require data type for all asm functions --- .../java/de/hsrm/compiler/Klang/asm/ASM.java | 52 +------- .../compiler/Klang/asm/mnemonics/Add.java | 9 +- .../compiler/Klang/asm/mnemonics/Cmp.java | 7 +- .../compiler/Klang/asm/mnemonics/Div.java | 7 +- .../compiler/Klang/asm/mnemonics/Idiv.java | 6 +- .../compiler/Klang/asm/mnemonics/Imul.java | 5 +- .../Klang/asm/mnemonics/Mnemonic.java | 1 - .../compiler/Klang/asm/mnemonics/Mov.java | 10 -- .../compiler/Klang/asm/mnemonics/Mul.java | 7 +- .../compiler/Klang/asm/mnemonics/Pop.java | 5 +- .../compiler/Klang/asm/mnemonics/Push.java | 5 +- .../compiler/Klang/asm/mnemonics/Sub.java | 7 +- .../compiler/Klang/asm/mnemonics/Ucomi.java | 7 +- .../compiler/Klang/asm/mnemonics/Xor.java | 7 +- .../hsrm/compiler/Klang/visitors/GenASM.java | 126 +++++++++--------- 15 files changed, 84 insertions(+), 177 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java b/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java index 3e8e77f..c0d6909 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/ASM.java @@ -12,10 +12,6 @@ public class ASM { this.mnemonics = new ArrayList(); } - 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)); diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java index da69816..af6735c 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Add.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java index 4ab5d3b..9868421 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Cmp.java @@ -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; } diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java index dfb72f0..051d317 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Div.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java index 5aa776e..a642d34 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Idiv.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java index 0e75e4a..4cab0b3 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Imul.java @@ -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; } diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java index ce25776..8892c4d 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mnemonic.java @@ -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; } \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java index 4a18870..1aca57c 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mov.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java index 5ff1038..d3bac9e 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Mul.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java index cf44583..a32a5e7 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Pop.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java index 12608ec..c6b0b30 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Push.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java index 7b23345..7b14f65 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Sub.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java index d3afa17..ac15c40 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Ucomi.java @@ -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; diff --git a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java index 9989576..397c1eb 100644 --- a/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java +++ b/src/main/java/de/hsrm/compiler/Klang/asm/mnemonics/Xor.java @@ -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; 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 8796f00..aad1f62 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -83,7 +83,7 @@ public class GenASM implements Visitor { 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 { 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 { @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 { @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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { @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 { // 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 { // 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 { // 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 { @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 { 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 { 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 { 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 { 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 { 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 { // 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 { // 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 { // 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 { // 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 { } // 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;