Commit c3f365ba authored by xx1219's avatar xx1219
Browse files

Merge branch 'master' into wacc-extension-include

parents c1b05042 2d278458
#!/bin/bash
VALID_EXAMPLES=(
"/constantPropagation/propagation"
"/constantPropagation/evaluation"
"/propagation"
# "/evaluation"
)
VALID_EXAMPLES_SRC_DIR="./src/test/custom/valid"
ASSEMBLY_OUTPUT_DIR="./log/assembly"
EXECUTE_OUTPUT_DIR="./log/output"
VALID_EXAMPLES_SRC_DIR="./src/test/custom/valid/constantPropagation"
ASSEMBLY_OUTPUT_DIR="./log/assembly/constantPropagation"
mkdir log
mkdir $ASSEMBLY_OUTPUT_DIR
mkdir $EXECUTE_OUTPUT_DIR
# counters to represent the total number of test files to be processed
TOTAL_COUNT=$(find "${VALID_EXAMPLES[@]/#/${VALID_EXAMPLES_SRC_DIR}}" -name "*.wacc" | wc -l)
......@@ -19,14 +17,11 @@ COUNTER=0
for folder in ${VALID_EXAMPLES[@]}; do
ASSEMBLY_OUTPUT_VALID_FOLDER="${ASSEMBLY_OUTPUT_DIR}${folder}"
EXECUTE_OUTPUT_VALID_FOLDER="${EXECUTE_OUTPUT_DIR}${folder}"
mkdir $EXECUTE_OUTPUT_VALID_FOLDER
mkdir $ASSEMBLY_OUTPUT_VALID_FOLDER
for file in $(find "${VALID_EXAMPLES_SRC_DIR}${folder}" -name "*.wacc")
do
FILE_NAME=$(basename "${file%.*}")
EXECUTABLE_FILE_NAME="${ASSEMBLY_OUTPUT_VALID_FOLDER}/${FILE_NAME}"
EXECUTABLE_OUTPUT_FILE="${EXECUTE_OUTPUT_VALID_FOLDER}/${FILE_NAME}"
echo $file
./compile -t -o1 $file > "${EXECUTABLE_FILE_NAME}.log.txt"
......
......@@ -55,15 +55,4 @@ public class ArrayNode extends ExprNode {
public <T> T accept(NodeVisitor<T> visitor) {
return visitor.visitArrayNode(this);
}
/* array can be immediate, but will not be visited in binop */
@Override
public boolean isImmediate() {
return true;
}
@Override
public int getCastedVal() {
return length;
}
}
......@@ -20,6 +20,11 @@ public class ConstantPropagation implements NodeVisitor<Node> {
* used for the current visit of ident node */
private Map<Symbol, ExprNode> identValMap;
/* map of SYMBOL with the list of SYMBOL that depend on it */
private Map<Symbol, List<Symbol>> dependentMap;
/* list of SYMBOL of variables that right hand side contains */
private List<Symbol> dependentList;
/* list of ident map, which will be merged in the end of the CURRENT while, if, switch, for loop
* loopStartMapList is maplist that can reach while condition check
* breakMapList is all mapList copy of each time a BREAK is called */
......@@ -29,6 +34,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
identValMap = new HashMap<>();
this.loopStartMapList = new ArrayList<>();
this.breakMapList = new ArrayList<>();
this.dependentMap = new HashMap<>();
}
@Override
......@@ -90,6 +96,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
@Override
public Node visitIdentNode(IdentNode node) {
if (identValMap.containsKey(node.getSymbol())) {
dependentList.add(node.getSymbol());
return identValMap.get(node.getSymbol());
}
return node;
......@@ -145,6 +152,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
@Override
public Node visitAssignNode(AssignNode node) {
dependentList = new ArrayList<>();
ExprNode exprNode = visit(node.getRhs()).asExprNode();
AssignNode resultNode = new AssignNode(node.getLhs(), exprNode);
resultNode.setScope(node.getScope());
......@@ -152,14 +160,21 @@ public class ConstantPropagation implements NodeVisitor<Node> {
/* constant propagation:
* add new entry into map */
if ((node.getLhs() instanceof IdentNode) && exprNode.isImmediate()) {
identValMap.remove(((IdentNode) node.getLhs()).getSymbol());
identValMap.put(((IdentNode) node.getLhs()).getSymbol(), exprNode);
Symbol lhsSymbol = ((IdentNode) node.getLhs()).getSymbol();
/* remove prev map of symbol
and all other symbol that depend on prev value of this updated SYMBOL */
removeSymbol(lhsSymbol);
/* add in updated value */
identValMap.put(lhsSymbol, exprNode);
addDependent(lhsSymbol, dependentList);
}
return resultNode;
}
@Override
public Node visitDeclareNode(DeclareNode node) {
dependentList = new ArrayList<>();
ExprNode exprNode = visit(node.getRhs()).asExprNode();
DeclareNode resultNode = new DeclareNode(node.getIdentifier(), exprNode);
resultNode.setScope(node.getScope());
......@@ -167,11 +182,35 @@ public class ConstantPropagation implements NodeVisitor<Node> {
/* constant propagation:
* add new entry in map */
if (exprNode.isImmediate()) {
identValMap.put(node.getScope().lookup(node.getIdentifier()), exprNode);
Symbol lhsSymbol = node.getScope().lookup(node.getIdentifier());
identValMap.put(lhsSymbol, exprNode);
addDependent(lhsSymbol, dependentList);
}
return resultNode;
}
/* remove the symbol from current identValMap
* and remove all SYMBOL that depend on this SYMBOL */
private void removeSymbol(Symbol symbol) {
identValMap.remove(symbol);
if (dependentMap.containsKey(symbol)) {
for (Symbol childSymbol : dependentMap.get(symbol)) {
removeSymbol(childSymbol);
}
}
dependentMap.remove(symbol);
}
/* add lhsSymbol as symbol that depend on each rhsSymbols */
private void addDependent(Symbol lhsSymbol, List<Symbol> rhsSymbols) {
for (Symbol rhsSymbol : rhsSymbols) {
if (dependentMap.containsKey(rhsSymbol)) {
dependentMap.get(rhsSymbol).add(lhsSymbol);
}
}
}
@Override
public Node visitExitNode(ExitNode node) {
ExitNode resultNode = new ExitNode(visit(node.getValue()).asExprNode());
......@@ -232,15 +271,12 @@ public class ConstantPropagation implements NodeVisitor<Node> {
@Override
public Node visitReadNode(ReadNode node) {
ExprNode expr = visit(node.getInputExpr()).asExprNode();
ReadNode resultNode = new ReadNode(expr);
resultNode.setScope(node.getScope());
ExprNode expr = node.getInputExpr();
/* delete expr in identMap, since cannot determine expr's value */
if (expr instanceof IdentNode) {
identValMap.remove(((IdentNode) expr).getSymbol());
}
return resultNode;
return node;
}
@Override
......@@ -269,10 +305,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
/* constant propagation algorithm:
* visit body twice, get intersection with the first iterate */
/* 1 visit cond */
ExprNode cond = visit(node.getCond()).asExprNode();
/* 1.2 record the mapList, which might contain maps from parent WHILE
/* 1 record the mapList, which might contain maps from parent WHILE
* current identMap is one map of parent block of while block */
List<Map<Symbol, ExprNode>>
oldLoopStartMapList = loopStartMapList,
......@@ -294,6 +327,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
identValMap = mergedStartMap;
/* 3 visit again using map after getting the intersection */
ExprNode cond = visit(node.getCond()).asExprNode();
StatNode body = visit(node.getBody()).asStatNode();
identValMap = mergedEndMap;
......@@ -399,13 +433,9 @@ public class ConstantPropagation implements NodeVisitor<Node> {
@Override
public Node visitForNode(ForNode node) {
/* 1 visit init, view as part of block before loop */
/* init statement count as part of map of block before loop body */
StatNode initStat = visit(node.getInit()).asStatNode();
/* 2 visit cond, same as while, no special treat */
ExprNode cond = visit(node.getCond()).asExprNode();
/* 3 visit body and increment */
/* 1 visit body and increment */
/* (same as while) record the mapList, which might contain maps from parent WHILE
* current identMap is one map of parent block of while block */
List<Map<Symbol, ExprNode>>
......@@ -420,7 +450,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
* loop start map is merged, result is map parent of loop body and block after loop
* break is then merged, result is map before block after loop
* merged start/end map are used to record map, prevent overwrite in second visit */
visit(node.getBody()).asStatNode();
visit(node.getBody());
visit(node.getIncrement());
mergeIdMap(loopStartMapList);
Map<Symbol, ExprNode> mergedStartMap = new HashMap<>(identValMap);
......@@ -429,6 +459,7 @@ public class ConstantPropagation implements NodeVisitor<Node> {
identValMap = mergedStartMap;
/* 3 visit again using map after getting the intersection */
ExprNode cond = visit(node.getCond()).asExprNode();
StatNode body = visit(node.getBody()).asStatNode();
StatNode inc = visit(node.getIncrement()).asStatNode();
identValMap = mergedEndMap;
......
......@@ -21,8 +21,8 @@ WARNING: arithmetic on -14 and 2147483600 will cause overflow
b (5) = true
b (5) = true
b (5) = false
b (5) = true
b (5) = true
b (5) = false
b (5) = false
}
Assembly file created!
Assembly has been written to the file!
......@@ -6,7 +6,7 @@ begin
int i = 0; # i is update, no propagate
int j = 0; # j is init as different value, propagate to new init value
for (i = 0, j = 1; i < 10; i = i + 1) do
for (i = 0, j = 1; i < 1; i = i + 1) do
b0 = 1;
while true do
......
......@@ -102,8 +102,8 @@ public class Utils {
Binop.PLUS, ((x, y) -> arithmeticWithCheck(x, y, Math::addExact)),
Binop.MINUS, ((x, y) -> arithmeticWithCheck(x, y, Math::subtractExact)),
Binop.MUL, ((x, y) -> arithmeticWithCheck(x, y, Math::multiplyExact)),
Binop.DIV, ((x, y) -> new IntegerNode(x / y)),
Binop.MOD, ((x, y) -> new IntegerNode(x % y))
Binop.DIV, ((x, y) -> y == 0 ? null : new IntegerNode(x / y)),
Binop.MOD, ((x, y) -> y == 0 ? null : new IntegerNode(x % y))
);
public static final Map<Binop, BiFunction<Integer, Integer, Boolean>> cmpMap = Map.of(
......@@ -114,13 +114,13 @@ public class Utils {
Binop.EQUAL, ((x, y) -> x.compareTo(y) == 0),
Binop.INEQUAL, ((x, y) -> x.compareTo(y) != 0),
Binop.AND, ((x, y) -> (x & y) == 1),
Binop.OR, ((x, y) -> (x | y) == 0)
Binop.OR, ((x, y) -> (x | y) == 1)
);
public static final Map<Unop, Function<ExprNode, ExprNode>> unopApplyMap = Map.of(
Unop.MINUS, (x -> arithmeticWithCheck(0, x.getCastedVal(), Math::subtractExact)),
Unop.NOT, (x -> new BoolNode(x.getCastedVal() != 1)),
Unop.LEN, (x -> new IntegerNode(x.getCastedVal())),
Unop.LEN, (x -> x),
Unop.ORD, (x -> new IntegerNode(x.getCastedVal())),
Unop.CHR, (x -> new CharNode((char) x.getCastedVal()))
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment