mips without optimize

This commit is contained in:
colden
2025-12-12 20:14:00 +08:00
parent 84827838e2
commit c94bebf37b
130 changed files with 5462 additions and 4182 deletions

View File

@@ -0,0 +1,93 @@
package backend.mips;
import java.util.HashMap;
import java.util.ArrayList;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.data.MipsAsciiz;
import backend.mips.instr.data.MipsSpace;
import backend.mips.instr.data.MipsWord;
import midend.llvm.value.IrFuncValue;
import midend.llvm.value.IrValue;
public class MipsBuilder {
private static MipsModule module = null;
private static int offset = 0;
private static HashMap<IrValue, Integer> valueOffsetMap = new HashMap<>();
private static HashMap<IrValue, Register> valueRegisterMap = new HashMap<>();
public static void setModule(MipsModule module) {
MipsBuilder.module = module;
}
public static int getOffset() {
return offset;
}
public static void setOffset(int offset) {
MipsBuilder.offset = offset;
}
public static void setValueOffsetMap(HashMap<IrValue, Integer> valueOffsetMap) {
MipsBuilder.valueOffsetMap = valueOffsetMap;
}
public static HashMap<IrValue, Integer> getValueOffsetMap() {
return valueOffsetMap;
}
public static void setValueRegisterMap(HashMap<IrValue, Register> valueRegisterMap) {
MipsBuilder.valueRegisterMap = valueRegisterMap;
}
public static HashMap<IrValue, Register> getValueRegisterMap() {
return valueRegisterMap;
}
public static MipsModule getModule() {
return module;
}
public static void addMipsInstr(MipsInstr instr) {
if (instr instanceof MipsAsciiz || instr instanceof MipsWord || instr instanceof MipsSpace) { //data段
module.addData(instr);
} else {
module.addText(instr);
}
}
public static void enterNewFunc(IrFuncValue func) {
offset = 0;
valueOffsetMap = func.getValueOffsetMap();
valueOffsetMap.clear(); //TODO是否需要清空
valueRegisterMap = func.getValueRegisterMap();
}
public static Register getRegister(IrValue value) {
return valueRegisterMap.get(value);
}
public static Integer getOffset(IrValue value) {
return valueOffsetMap.get(value);
}
public static void allocaRegister(IrValue value, Register reg) {
valueRegisterMap.put(value, reg);
}
public static void allocaOffset(IrValue value) {
if (!valueOffsetMap.containsKey(value)) {
offset -= 4;
int valueOffset = offset;
valueOffsetMap.put(value, valueOffset);
}
}
public static void allocaOffset(int extra) {
offset -= extra;
}
public static ArrayList<Register> getUsedRegisters() {
return new ArrayList<>(valueRegisterMap.values());
}
}

View File

@@ -0,0 +1,51 @@
package backend.mips;
import java.util.ArrayList;
import backend.mips.instr.MipsAnnotation;
import backend.mips.instr.MipsLabel;
import backend.mips.instr.MipsInstr;
public class MipsModule {
private ArrayList<MipsInstr> dataList;
private ArrayList<MipsInstr> textList;
public MipsModule() {
this.dataList = new ArrayList<MipsInstr>();
this.textList = new ArrayList<MipsInstr>();
}
public ArrayList<MipsInstr> getDataList() {
return dataList;
}
public ArrayList<MipsInstr> getTextList() {
return textList;
}
public void addData(MipsInstr instr) {
dataList.add(instr);
}
public void addText(MipsInstr instr) {
textList.add(instr);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(".data\n");
for (MipsInstr instr : dataList) {
sb.append("\t" + instr.toString());
}
sb.append("\n.text\n");
for (MipsInstr instr : textList) {
if (instr instanceof MipsLabel || instr instanceof MipsAnnotation) {
sb.append(instr.toString());
continue;
}
sb.append("\t" + instr.toString());
}
return sb.toString();
}
}

58
backend/mips/Register.java Executable file
View File

@@ -0,0 +1,58 @@
package backend.mips;
import java.util.ArrayList;
public enum Register {
ZERO("$zero"),
AT("$at"),
V0("$v0"),
V1("$v1"),
A0("$a0"),
A1("$a1"),
A2("$a2"),
A3("$a3"),
T0("$t0"),
T1("$t1"),
T2("$t2"),
T3("$t3"),
T4("$t4"),
T5("$t5"),
T6("$t6"),
T7("$t7"),
S0("$s0"),
S1("$s1"),
S2("$s2"),
S3("$s3"),
S4("$s4"),
S5("$s5"),
S6("$s6"),
S7("$s7"),
T8("$t8"),
T9("$t9"),
K0("$k0"),
K1("$k1"),
GP("$gp"),
SP("$sp"),
FP("$fp"),
RA("$ra");
private String regName;
Register(String regName) {
this.regName = regName;
}
public String toString() {
return regName;
}
public static ArrayList<Register> availableRegisters() {
ArrayList<Register> regs = new ArrayList<>();
for (Register reg : Register.values()) {
if (reg.toString().charAt(1) == 't' || reg.toString().charAt(1) == 's') {
regs.add(reg);
}
}
return regs;
}
}

39
backend/mips/instr/MipsAlu.java Executable file
View File

@@ -0,0 +1,39 @@
package backend.mips.instr;
import backend.mips.Register;
import backend.mips.instr.type.MipsAluType;
import backend.mips.instr.type.MipsType;
public class MipsAlu extends MipsInstr {
private MipsAluType aluType;
private Register rd;
private Register rs;
private Register rt;
private int immediate;
private boolean isImInstr;
public MipsAlu(MipsAluType aluType, Register rd, Register rs, Register rt) {
super(MipsType.ALU);
this.aluType = aluType;
this.rd = rd;
this.rs = rs;
this.rt = rt;
this.immediate = 0;
this.isImInstr = false;
}
public MipsAlu(MipsAluType aluType, Register rd, Register rs, int immediate) {
super(MipsType.ALU);
this.aluType = aluType;
this.rd = rd;
this.rs = rs;
this.rt = null;
this.immediate = immediate;
this.isImInstr = true;
}
public String toString() {
return isImInstr ? aluType.toString() + " " + rd + ", " + rs + ", " + immediate + "\n"
: aluType.toString() + " " + rd + ", " + rs + ", " + rt + "\n";
}
}

View File

@@ -0,0 +1,16 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsType;
public class MipsAnnotation extends MipsInstr {
private String annotation;
public MipsAnnotation(String anno) {
super(MipsType.ANNOTATION);
this.annotation = anno;
}
public String toString() {
return "# " + this.annotation + "\n";
}
}

View File

@@ -0,0 +1,36 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsBranchType;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsBranch extends MipsInstr{
private MipsBranchType branchType;
private Register rs;
private Register rt;
private String label;
public MipsBranch(MipsBranchType branchType, Register rs, Register rt, String label) {
super(MipsType.BRANCH);
this.branchType = branchType;
this.rs = rs;
this.rt = rt;
this.label = label;
}
public MipsBranch(MipsBranchType branchType, Register rs, String label) {
super(MipsType.BRANCH);
this.branchType = branchType;
this.rs = rs;
this.rt = null;
this.label = label;
}
public String toString() {
if (this.rt == null) {
return this.branchType.toString() + " " + this.rs + ", " + this.label + "\n";
} else {
return this.branchType.toString() + " " + this.rs + ", " + this.rt + ", " + this.label + "\n";
}
}
}

View File

@@ -0,0 +1,42 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsCompareType;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsComp extends MipsInstr {
private MipsCompareType compareType;
private Register rd;
private Register rs;
private Register rt;
private int immediate;
private boolean isImmediate;
public MipsComp(MipsCompareType compareType, Register rd, Register rs, Register rt) {
super(MipsType.COMPARE);
this.compareType = compareType;
this.rd = rd;
this.rs = rs;
this.rt = rt;
this.immediate = 0;
this.isImmediate = false;
}
public MipsComp(MipsCompareType compareType, Register rd, Register rs, int immediate) {
super(MipsType.COMPARE);
this.compareType = compareType;
this.rd = rd;
this.rs = rs;
this.rt = null;
this.immediate = immediate;
this.isImmediate = true;
}
public String toString() {
if (this.isImmediate) {
return this.compareType.toString() + " " + this.rd + ", " + this.rs + ", " + this.immediate + "\n";
} else {
return this.compareType.toString() + " " + this.rd + ", " + this.rs + ", " + this.rt + "\n";
}
}
}

View File

@@ -0,0 +1,21 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsType;
import backend.mips.MipsBuilder;
public class MipsInstr {
private MipsType mt;
public MipsInstr(MipsType mt) {
this.mt = mt;
MipsBuilder.addMipsInstr(this);
}
public String toString() {
return "";
}
public MipsType getType() {
return mt;
}
}

View File

@@ -0,0 +1,33 @@
package backend.mips.instr;
import backend.mips.Register;
import backend.mips.instr.type.MipsJumpType;
import backend.mips.instr.type.MipsType;
public class MipsJump extends MipsInstr {
private MipsJumpType jumpType;
private String label;
private Register rd;
public MipsJump(MipsJumpType jumpType, String label) {
super(MipsType.JUMP);
this.jumpType = jumpType;
this.label = label;
this.rd = null;
}
public MipsJump(MipsJumpType jumpType, Register rd) {
super(MipsType.JUMP);
this.jumpType = jumpType;
this.label = null;
this.rd = rd;
}
public String toString() {
if (this.rd == null) {
return this.jumpType.toString() + " " + this.label + "\n";
} else {
return this.jumpType.toString() + " " + this.rd + "\n";
}
}
}

View File

@@ -0,0 +1,16 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsType;
public class MipsLabel extends MipsInstr {
private String label;
public MipsLabel(String label) {
super(MipsType.LABEL);
this.label = label;
}
public String toString() {
return this.label + ":\n";
}
}

View File

@@ -0,0 +1,39 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsLsType;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsLs extends MipsInstr {
private MipsLsType lsType;
private Register rd;
private Register rt;
private int offset;
private String label;
public MipsLs(MipsLsType lsType, Register rd, Register rt, int offset) {
super(MipsType.LS);
this.lsType = lsType;
this.rd = rd;
this.rt = rt;
this.offset = offset;
this.label = null;
}
public MipsLs(MipsLsType lsType, Register rd, int offset, String label) {
super(MipsType.LS);
this.lsType = lsType;
this.rd = rd;
this.rt = null;
this.offset = offset;
this.label = label;
}
public String toString() {
if (this.label == null) {
return this.lsType.toString() + " " + this.rd + ", " + this.offset + "(" + this.rt + ")\n";
} else {
return this.lsType.toString() + " " + this.rd + ", " + this.label + " + " + this.offset + "\n";
}
}
}

View File

@@ -0,0 +1,33 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsMdType;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsMd extends MipsInstr {
private MipsMdType mdType;
private Register rd;
private Register rt;
public MipsMd(MipsMdType mdType, Register rd, Register rt) {
super(MipsType.MD);
this.mdType = mdType;
this.rd = rd;
this.rt = rt;
}
public MipsMd(MipsMdType mdType, Register rd) {
super(MipsType.MD);
this.mdType = mdType;
this.rd = rd;
this.rt = null;
}
public String toString() {
if (this.rt == null) {
return this.mdType.toString() + " " + this.rd + "\n";
} else {
return this.mdType.toString() + " " + this.rd + ", " + this.rt + "\n";
}
}
}

View File

@@ -0,0 +1,13 @@
package backend.mips.instr;
import backend.mips.instr.type.MipsType;
public class MipsSyscall extends MipsInstr {
public MipsSyscall() {
super(MipsType.SYSCALL);
}
public String toString() {
return "syscall\n";
}
}

View File

@@ -0,0 +1,19 @@
package backend.mips.instr.data;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
public class MipsAsciiz extends MipsInstr {
private String name;
private String str;
public MipsAsciiz(String name, String str) {
super(MipsType.DATA);
this.name = name;
this.str = str;
}
public String toString() {
return name + ": .asciiz \"" + str.replaceAll("\n", "\\n") + "\"\n";
}
}

View File

@@ -0,0 +1,19 @@
package backend.mips.instr.data;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
public class MipsSpace extends MipsInstr {
private int size;
private String name;
public MipsSpace(int size, String name) {
super(MipsType.DATA);
this.size = size;
this.name = name;
}
public String toString() {
return name + ": .space " + size + "\n";
}
}

View File

@@ -0,0 +1,39 @@
package backend.mips.instr.data;
import java.util.ArrayList;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
public class MipsWord extends MipsInstr {
private String name;
private ArrayList<Integer> valueList;
public MipsWord(String name, ArrayList<Integer> valueList) {
super(MipsType.DATA);
this.name = name;
this.valueList = valueList;
}
public MipsWord(String name, int value) {
super(MipsType.DATA);
this.name = name;
this.valueList = new ArrayList<Integer>();
this.valueList.add(value);
}
public int getValueNum() {
return valueList.size();
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name).append(": .word ");
for (int value : valueList) {
sb.append(value).append(", ");
}
sb.delete(sb.length() - 2, sb.length());
sb.append("\n");
return sb.toString();
}
}

View File

@@ -0,0 +1,28 @@
package backend.mips.instr.fake;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsLa extends MipsInstr {
private Register rd;
private String label;
public MipsLa(Register rd, String label) {
super(MipsType.FAKE);
this.rd = rd;
this.label = label;
}
public Register getRd() {
return rd;
}
public String getLabel() {
return label;
}
public String toString() {
return "la " + rd + ", " + label + "\n";
}
}

View File

@@ -0,0 +1,29 @@
package backend.mips.instr.fake;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsLi extends MipsInstr {
private Register rd;
private int value;
public MipsLi(Register rd, int value) {
super(MipsType.FAKE);
this.rd = rd;
this.value = value;
}
public Register getRd() {
return rd;
}
public int getValue() {
return value;
}
public String toString() {
return "li " + rd + ", " + value + "\n";
}
}

View File

@@ -0,0 +1,28 @@
package backend.mips.instr.fake;
import backend.mips.instr.MipsInstr;
import backend.mips.instr.type.MipsType;
import backend.mips.Register;
public class MipsMove extends MipsInstr {
private Register rd;
private Register rt;
public MipsMove(Register rd, Register rt) {
super(MipsType.FAKE);
this.rd = rd;
this.rt = rt;
}
public Register getDest() {
return rd;
}
public Register getSrc() {
return rt;
}
public String toString() {
return "move " + rd + ", " + rt + "\n";
}
}

View File

@@ -0,0 +1,16 @@
package backend.mips.instr.type;
public enum MipsAluType {
// calc_R
ADD, SUB, ADDU, SUBU, AND, OR, NOR, XOR, SLT, SLTU,
// shiftv
SLLV, SRAV, SRLV,
// calc_I
ADDI, ADDIU, ANDI, ORI, XORI, SLTI, SLTIU,
// shift
SLL, SRA, SRL;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,9 @@
package backend.mips.instr.type;
public enum MipsBranchType {
BEQ, BNE, BLEZ, BGTZ, BLTZ, BGEZ;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,9 @@
package backend.mips.instr.type;
public enum MipsCompareType {
SLT, SLE, SGT, SGE, SEQ, SNE, SLTI;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,9 @@
package backend.mips.instr.type;
public enum MipsJumpType {
J, JAL, JR;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,9 @@
package backend.mips.instr.type;
public enum MipsLsType {
LW, SW, LH, SH, LHU, LB, SB, LBU;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,9 @@
package backend.mips.instr.type;
public enum MipsMdType {
MFHI, MFLO, MULT, DIV, MTHI, MTLO;
public String toString() {
return this.name().toLowerCase();
}
}

View File

@@ -0,0 +1,15 @@
package backend.mips.instr.type;
public enum MipsType {
ALU,
COMPARE,
BRANCH,
JUMP,
SYSCALL,
DATA,
LABEL,
ANNOTATION,
LS,
MD,
FAKE
}