126 lines
3.5 KiB
Java
Executable File
126 lines
3.5 KiB
Java
Executable File
package midend.llvm;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
import backend.mips.Register;
|
|
import backend.mips.instr.MipsAnnotation;
|
|
import backend.mips.instr.MipsJump;
|
|
import backend.mips.instr.MipsLabel;
|
|
import backend.mips.instr.fake.MipsLi;
|
|
import backend.mips.instr.MipsSyscall;
|
|
import backend.mips.instr.type.MipsJumpType;
|
|
import midend.llvm.constant.IrConstantStr;
|
|
import midend.llvm.value.IrFuncValue;
|
|
import midend.llvm.value.IrGlobalValue;
|
|
import midend.llvm.instr.GetIntInstr;
|
|
import midend.llvm.instr.PutChInstr;
|
|
import midend.llvm.instr.PutIntInstr;
|
|
import midend.llvm.instr.PutStrInstr;
|
|
|
|
public class IrModule {
|
|
private ArrayList<String> decls;
|
|
private HashMap<String, IrConstantStr> strs;
|
|
private ArrayList<IrGlobalValue> globalVars;
|
|
private ArrayList<IrFuncValue> funcs;
|
|
|
|
public IrModule() {
|
|
decls = new ArrayList<>();
|
|
strs = new HashMap<>();
|
|
globalVars = new ArrayList<>();
|
|
funcs = new ArrayList<>();
|
|
decls.add(GetIntInstr.getIntDecl());
|
|
decls.add(PutChInstr.putChDecl());
|
|
decls.add(PutIntInstr.putIntDecl());
|
|
decls.add(PutStrInstr.putStrDecl());
|
|
}
|
|
|
|
public ArrayList<String> getDecls() {
|
|
return decls;
|
|
}
|
|
|
|
public HashMap<String, IrConstantStr> getStrs() {
|
|
return strs;
|
|
}
|
|
|
|
public ArrayList<IrGlobalValue> getGlobalVars() {
|
|
return globalVars;
|
|
}
|
|
|
|
public ArrayList<IrFuncValue> getFuncs() {
|
|
return funcs;
|
|
}
|
|
|
|
public void addFunc(IrFuncValue func) {
|
|
funcs.add(func);
|
|
}
|
|
|
|
public void addStr(IrConstantStr str) {
|
|
if (strs.containsKey(str.getValue())) {
|
|
return;
|
|
}
|
|
strs.put(str.getValue(), str);
|
|
}
|
|
|
|
public boolean containStr(String str) {
|
|
return strs.containsKey(str);
|
|
}
|
|
|
|
public IrConstantStr getStr(String str) {
|
|
if (containStr(str)) {
|
|
return strs.get(str);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void addGlobalVar(IrGlobalValue globalVar) {
|
|
globalVars.add(globalVar);
|
|
}
|
|
|
|
public IrFuncValue getMainFunc() {
|
|
for (IrFuncValue func : funcs) {
|
|
if (func.isMain()) {
|
|
return func;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (String decl : decls) {
|
|
sb.append(decl).append("\n");
|
|
}
|
|
for (IrConstantStr str : strs.values()) {
|
|
sb.append(str.toString()).append("\n");
|
|
}
|
|
for (IrGlobalValue globalVar : globalVars) {
|
|
sb.append(globalVar.toString()).append("\n");
|
|
}
|
|
for (IrFuncValue func : funcs) {
|
|
sb.append(func.toString()).append("\n");
|
|
}
|
|
// System.out.println(funcs.size());
|
|
return sb.toString();
|
|
}
|
|
|
|
public void toMips() {
|
|
for (IrGlobalValue globalVar : this.globalVars) {
|
|
globalVar.toMips();
|
|
}
|
|
for (IrConstantStr str : this.strs.values()) {
|
|
str.toMips(str.getMipsLabel());
|
|
}
|
|
new MipsAnnotation("jump to main");
|
|
new MipsJump(MipsJumpType.JAL, "main");
|
|
new MipsJump(MipsJumpType.J, "end");
|
|
|
|
for (IrFuncValue irFunction : this.funcs) {
|
|
irFunction.toMips();
|
|
}
|
|
new MipsLabel("end");
|
|
new MipsLi(Register.V0, 10);
|
|
new MipsSyscall();
|
|
}
|
|
}
|