llvmir some opt

This commit is contained in:
邓智航
2025-12-10 17:58:17 +08:00
commit 84827838e2
103 changed files with 5838 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
package midend.symbol;
public class ArraySymbol extends Symbol {
private int dim; // -1 means unknown
public ArraySymbol(String name, SymbolType type, int line, int dim) {
super(name, type, line);
this.dim = dim;
}
public int getDim() {
return dim;
}
public void fullValue() {
for (int i = getValueList().size(); i < dim; i++) {
addValue(0);
}
}
}

View File

@@ -0,0 +1,50 @@
package midend.symbol;
import java.util.ArrayList;
public class FuncSymbol extends Symbol {
private int returnType; // 0 for void, 1 for int
private ArrayList<Integer> paramList; // 0 for var, 1 for array
private ArrayList<Symbol> paramSymbolList;
public FuncSymbol(String name, SymbolType type, int line, int returnType) {
super(name, type, line);
paramList = new ArrayList<>();
paramSymbolList = new ArrayList<>();
this.returnType = returnType;
}
public void addParam(int param) {
paramList.add(param);
}
public void addParamSymbol(Symbol paramSymbol) {
paramSymbolList.add(paramSymbol);
}
public int getParamNum() {
return paramList.size();
}
public int getReturnType() {
return returnType;
}
public int getParamType(int index) {
return paramList.get(index);
}
public void printParams() {
for (int i = 0; i < paramList.size(); i++) {
System.out.print(paramList.get(i) + " ");
}
}
public ArrayList<Integer> getParamList() {
return paramList;
}
public ArrayList<Symbol> getParamSymbolList() {
return paramSymbolList;
}
}

77
midend/symbol/Symbol.java Normal file
View File

@@ -0,0 +1,77 @@
package midend.symbol;
import java.util.ArrayList;
import midend.llvm.value.IrValue;
public class Symbol {
private String name;
private SymbolType type;
private int line;
private ArrayList<Integer> valueList;
private IrValue irValue;
public Symbol(String name, SymbolType type, int line) {
this.name = name;
this.type = type;
this.line = line;
valueList = new ArrayList<>();
irValue = null;
}
public void setIrValue(IrValue irValue) {
this.irValue = irValue;
}
public IrValue getIrValue() {
return irValue;
}
public void addValue(int value) {
valueList.add(value);
}
public void addValue(ArrayList<Integer> valueList) {
this.valueList.addAll(valueList);
}
public ArrayList<Integer> getValueList() {
return valueList;
}
public int getValue(int index) {
return valueList.get(index);
}
public String getName() {
return name;
}
public String getTypeStr() {
return type.toString();
}
public SymbolType getType() {
return type;
}
public int getLine() {
return line;
}
public String toString() {
return name + " " + type.toString() + "\n";
}
public boolean isThatType(int type) { // 0 for normal var, 1 for array, 2 for func
if (type == 1) {
return this.type == SymbolType.INT_ARRAY || this.type == SymbolType.CONST_INT_ARRAY
|| this.type == SymbolType.STATIC_INT_ARRAY;
} else if (type == 2) {
return this.type == SymbolType.INT_FUNC || this.type == SymbolType.VOID_FUNC;
} else {
return this.type == SymbolType.INT || this.type == SymbolType.CONST_INT
|| this.type == SymbolType.STATIC_INT;
}
}
}

View File

@@ -0,0 +1,136 @@
package midend.symbol;
import java.util.ArrayList;
import error.Errors;
public class SymbolManager {
public static ArrayList<SymbolTable> symbolTableList;
public static ArrayList<Integer> sequence;
public static SymbolTable currentTable;
public static int currentSequence;
public static void init() {
symbolTableList = new ArrayList<>();
sequence = new ArrayList<>();
currentTable = null;
currentSequence = -1;
}
public static void addSymbolTable() {
SymbolTable symbolTable = new SymbolTable(symbolTableList.size() + 1);
symbolTableList.add(symbolTable);
sequence.add(symbolTable.getTableId());
currentSequence++;
if (currentTable != null) {
symbolTable.setParentTable(currentTable);
}
currentTable = symbolTable;
}
public static ArrayList<SymbolTable> getSymbolTableList() {
return symbolTableList;
}
public static void releaseSymbolTable(int tableId) {
symbolTableList.get(tableId - 1).release();
currentTable = null;
for (int i = symbolTableList.size() - 1; i >= 0; i--) {
if (!symbolTableList.get(i).isReleased()) {
currentTable = symbolTableList.get(i);
currentSequence++;
sequence.add(currentTable.getTableId());
break;
}
}
}
public static void releaseSymbolTable() {
releaseSymbolTable(currentTable.getTableId());
}
public static int getSymbolTableSize() {
return symbolTableList.size();
}
public static void addSymbol(Symbol symbol, Errors errors) {
currentTable.addSymbol(symbol, errors);
}
public static String getSymbolTableInfo() {
String info = "";
for (SymbolTable symbolTable : symbolTableList) {
info += symbolTable.toString();
}
return info;
}
public static Symbol getSymbol(String name) {
SymbolTable stable = currentTable;
Symbol symbol = null;
while (stable != null) {
symbol = stable.getSymbol(name);
if (symbol != null) {
break;
}
stable = stable.getParentTable();
}
return symbol;
}
public static Symbol getSymbol(String name, boolean defined) {
SymbolTable stable = currentTable;
Symbol symbol = null;
while (stable != null) {
symbol = stable.getSymbol(name);
if (symbol != null && symbol.getIrValue() != null) {
break;
}
stable = stable.getParentTable();
}
return symbol;
}
public static void nextTable() {
currentSequence++;
if (currentSequence >= sequence.size()) {
return;
}
currentTable = symbolTableList.get(sequence.get(currentSequence) - 1);
}
public static void lastTable() {
currentSequence--;
if (currentSequence < 0) {
currentSequence = 0;
currentTable = symbolTableList.get(0);
return;
}
currentTable = symbolTableList.get(sequence.get(currentSequence) - 1);
}
public static void reset() {
currentSequence = -1;
currentTable = null;
}
public static boolean IsGlobal() {
return currentTable.getTableId() == 1;
}
public static ArrayList<Integer> getSequence() {
return sequence;
}
public static int getCurrentSequence() {
return currentSequence;
}
public static int getCurrentTableId() {
return sequence.get(currentSequence);
}
public static SymbolTable getCurrentTable() {
return currentTable;
}
}

View File

@@ -0,0 +1,68 @@
package midend.symbol;
import java.util.ArrayList;
import java.util.HashMap;
import error.Error;
import error.ErrorType;
import error.Errors;
public class SymbolTable {
private ArrayList<Symbol> symbolList;
private HashMap<String, Symbol> symbolMap;
private int tableId;
private boolean isReleased;
private SymbolTable parentTable;
public SymbolTable(int tableId) {
this.tableId = tableId;
isReleased = false;
symbolList = new ArrayList<>();
symbolMap = new HashMap<>();
parentTable = null;
}
public int getTableId() {
return tableId;
}
public boolean isReleased() {
return isReleased;
}
public void release() {
isReleased = true;
}
public void addSymbol(Symbol symbol, Errors errors) {
if (symbolMap.containsKey(symbol.getName())) {
errors.addError(new Error(symbol.getLine(), ErrorType.b));
return;
}
symbolList.add(symbol);
symbolMap.put(symbol.getName(), symbol);
}
public void setParentTable(SymbolTable parentTable) {
this.parentTable = parentTable;
}
public SymbolTable getParentTable() {
return parentTable;
}
public Symbol getSymbol(String name) {
if (symbolMap.containsKey(name)) {
return symbolMap.get(name);
}
return null;
}
public String toString() {
String info = "";
for (Symbol symbol : symbolList) {
info += tableId + " " + symbol.toString();
}
return info;
}
}

View File

@@ -0,0 +1,22 @@
package midend.symbol;
public enum SymbolType {
CONST_INT("ConstInt"),
CONST_INT_ARRAY("ConstIntArray"),
STATIC_INT("StaticInt"),
INT("Int"),
INT_ARRAY("IntArray"),
STATIC_INT_ARRAY("StaticIntArray"),
VOID_FUNC("VoidFunc"),
INT_FUNC("IntFunc");
private final String type;
SymbolType(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
}