101 lines
2.6 KiB
Java
101 lines
2.6 KiB
Java
package midend.llvm;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
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;
|
|
}
|
|
|
|
//TODO: toString()方法编写
|
|
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();
|
|
}
|
|
}
|