Commit 48c13d0c authored by 徐世桐's avatar 徐世桐
Browse files

sx119: finished extension optimise test script, to be moved to labts test

parent 8910c406
......@@ -10,6 +10,7 @@ stages:
- build
- frontend-test
- backend-test
- optimise-test
build-job:
stage: build
......@@ -57,6 +58,17 @@ test-execute-valid:
- echo "Making arm executable for each assembly file and trying to execute them..."
- ./scripts/assembleExecuteTest
- ./scripts/executeOutputTest
artifacts:
paths:
- log/assembly
- log/output
test-optimise:
stage: optimise-test
script:
- echo "compile oprimise sample tests and conpair with given .log result file"
- ./scripts/constantPropagate
- ./scripts/extensionValidTest
artifacts:
paths:
- log/assembly
......
......@@ -39,4 +39,5 @@ then
fi
# compile the given wacc file here
java -cp ./bin:./lib/antlr-4.9.1-complete.jar Compiler $1 $PARSE_ONLY $PRINT_AST $OPTIMISE "--assembly" $EXECUTE
\ No newline at end of file
# default as using -o1 optimise and generate assembly
java -cp ./bin:./lib/antlr-4.9.1-complete.jar Compiler $1 $PARSE_ONLY $PRINT_AST $OPTIMISE "--optimise" "1" "--assembly" $EXECUTE
\ No newline at end of file
......@@ -7,4 +7,6 @@
./scripts/semanticCheckerValid
./scripts/semanticCheckerSemanticErr
./scripts/assembleTest.sh
./scripts/executeTest.sh
\ No newline at end of file
./scripts/executeTest.sh
./scripts/constantPropagation.sh
./scripts/extensionValidTest.sh
\ No newline at end of file
#!/bin/bash
VALID_EXAMPLE_DIR="./src/test/examples/valid/"
VALID_EXAMPLES=(
"/constantPropagation/evaluation/"
"/constantPropagation/propagation"
"/constantPropagation/evaluation"
)
VALID_EXAMPLE_LOG="./log/const_eval.log"
> $VALID_EXAMPLE_LOG
VALID_EXAMPLES_SRC_DIR="./src/test/custom/valid"
ASSEMBLY_OUTPUT_DIR="./log/assembly"
EXECUTE_OUTPUT_DIR="./log/output"
mkdir log
mkdir $ASSEMBLY_OUTPUT_DIR
mkdir $EXECUTE_OUTPUT_DIR
TEST_DIR="$VALID_EXAMPLE_DIR$VALID_EXAMPLES"
# counters to represent the total number of test files to be processed
TOTAL_COUNT=$(find "$VALID_EXAMPLE_DIR${VALID_EXAMPLES[@]}" -name "*.wacc" | wc -l)
TOTAL_COUNT=$(find "${VALID_EXAMPLES[@]/#/${VALID_EXAMPLES_SRC_DIR}}" -name "*.wacc" | wc -l)
COUNTER=0
for folder in "$VALID_EXAMPLE_DIR${VALID_EXAMPLES[@]}"; do
for file in $(find "$folder" -name "*.wacc"); do
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%.*}")
./compile -o1 -t $file 2> $VALID_EXAMPLE_LOG
ret=$?
echo "exit status: " $ret
if [ $ret -ne 0 ]; then
echo $file
echo "status code incorrect: " $ret
exit 1
fi
if diff "$TEST_DIR${FILE_NAME}.log" $VALID_EXAMPLE_LOG > temp; then
echo $FILE_NAME
echo "optimise differ from expected result: "
exit 1
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"
if diff "${EXECUTABLE_FILE_NAME}.log.txt" "${VALID_EXAMPLES_SRC_DIR}${folder}/${FILE_NAME}.log" -I scope; then
(( COUNTER += 1 ))
fi
(( COUNTER += 1 ))
echo "$COUNTER / $(($TOTAL_COUNT)) finished"
echo ""
echo "$COUNTER / $(($TOTAL_COUNT)) files have been executed"
done
echo "========================================================================================"
......
......@@ -5,8 +5,8 @@ VALID_EXAMPLES=(
"/bitwiseOperation"
"/do-while"
"/for"
"/import" # OK
"/jump" # OK
"/import"
"/jump"
"/struct"
"/switch"
)
......@@ -45,11 +45,11 @@ for folder in ${VALID_EXAMPLES[@]}; do
echo "execution exit status" $ret2
## altomatically generate output .log file
mv "${EXECUTABLE_OUTPUT_FILE}.output.txt" "${VALID_EXAMPLES_SRC_DIR}${folder}/${FILE_NAME}.log"
# mv "${EXECUTABLE_OUTPUT_FILE}.output.txt" "${VALID_EXAMPLES_SRC_DIR}${folder}/${FILE_NAME}.log"
if [ "$ret1" -eq 0 ] && [ "$ret2" -eq 0 ]; then
## test if output is same as .log file
# diff "${EXECUTABLE_OUTPUT_FILE}.output.txt" "${VALID_EXAMPLES_SRC_DIR}${folder}/${FILE_NAME}.log"
diff "${EXECUTABLE_OUTPUT_FILE}.output.txt" "${VALID_EXAMPLES_SRC_DIR}${folder}/${FILE_NAME}.log"
(( COUNTER += 1 ))
fi
......
......@@ -65,7 +65,7 @@ public class Compiler {
int optimise_cmd_index = cmd_ops.indexOf("--optimise");
/* if not found optimise flag, no optimise */
if (optimise_cmd_index == 0) {
if (optimise_cmd_index != 0) {
String optimise_level = cmd_ops.get(optimise_cmd_index + 1);
switch (optimise_level) {
case "0":
......
......@@ -684,13 +684,7 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
/* 1 leave space for variables in stack */
int stackSize = node.getStackSize();
int temp = stackSize;
while (temp > 0) {
int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
instructions.add(new Sub(SP, SP,
new Operand2(realStackSize)));
temp = temp - realStackSize;
}
decStack(stackSize);
/* accumulate function stack size, in case this scope is a function scope and contain return */
funcStackSize += stackSize;
......@@ -709,13 +703,7 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
loopStackSize -= stackSize;
/* 3 restore stack */
temp = stackSize;
while (temp > 0) {
int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
instructions.add(new Add(SP, SP,
new Operand2(realStackSize)));
temp = temp - realStackSize;
}
incStack(stackSize);
return null;
}
......@@ -872,15 +860,9 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
public Void visitForNode(ForNode node) {
/* 1 translate the initiator of the for-loop */
currSymbolTable = node.getIncrement().getScope();
/* TODO: better code quality here */
int stackSize = currSymbolTable.getSize();
int temp = stackSize;
while (temp > 0) {
int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
instructions.add(new Sub(SP, SP,
new Operand2(realStackSize)));
temp = temp - realStackSize;
}
decStack(stackSize);
visit(node.getInit());
/* 2 create labels and translate for for-loop body */
......@@ -889,11 +871,6 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
Label nextLabel = branchLabelGenerator.getLabel();
Label incrementLabel = branchLabelGenerator.getLabel();
System.out.println("bodyLabel: " + bodyLabel.assemble());
System.out.println("condLabel: " + condLabel.assemble());
System.out.println("nextLabel: " + nextLabel.assemble());
System.out.println("incrementLabel: " + incrementLabel.assemble());
/* restore the last jump-to label after visiting the for-loop body */
Label lastBreakJumpToLabel = currBreakJumpToLabel;
Label lastContinueJumpToLabel = currContinueJumpToLabel;
......@@ -923,16 +900,7 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
/* 3 add label for condition checking */
instructions.add(condLabel);
// currSymbolTable = node.getIncrement().getScope();
// /* TODO: better code quality here */
// int stackSize = currSymbolTable.getSize();
// int temp = stackSize;
// while (temp > 0) {
// int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
// instructions.add(new Sub(SP, SP,
// new Operand2(realStackSize)));
// temp = temp - realStackSize;
// }
currSymbolTable = node.getIncrement().getScope();
visit(node.getCond());
......@@ -947,14 +915,7 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
/* loop varient's stack clearing should be after next label,
so that BREAK can add back stack used by varients */
temp = stackSize;
while (temp > 0) {
int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
instructions.add(new Add(SP, SP,
new Operand2(realStackSize)));
temp = temp - realStackSize;
}
incStack(stackSize);
return null;
}
......@@ -964,25 +925,12 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
List<Instruction> addStack = new ArrayList<>();
/* this snippet is to deal with for-loop stack difference */
if (!node.getJumpType().equals(JumpType.BREAK) || !node.getJumpContext().equals(JumpContext.SWITCH)) {
int temp = loopStackSize;
while (temp > 0) {
int realStackSize = temp / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : temp;
addStack.add(new Add(SP, SP,
new Operand2(realStackSize)));
temp = temp - realStackSize;
}
incStack(loopStackSize);
}
instructions.addAll(addStack);
if (node.getJumpType().equals(JumpType.BREAK)) {
// if (addStack != null) instructions.addAll(addStack);
instructions.add(new B(NULL, currBreakJumpToLabel.getName()));
} else if (node.getJumpType().equals(JumpType.CONTINUE)) {
/* this snippet is to deal with the for-loop increment */
// StatNode increment = node.getForIncrement();
// if (increment != null) visit(increment);
// if (addStack != null) instructions.addAll(addStack);
instructions.add(new B(NULL, currContinueJumpToLabel.getName()));
}
......@@ -1028,4 +976,22 @@ public class ARMInstructionGenerator implements NodeVisitor<Void> {
return null;
}
private void incStack(int stackSize) {
while (stackSize > 0) {
int realStackSize = stackSize / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : stackSize;
instructions.add(new Add(SP, SP,
new Operand2(realStackSize)));
stackSize = stackSize - realStackSize;
}
}
private void decStack(int stackSize) {
while (stackSize > 0) {
int realStackSize = stackSize / MAX_STACK_STEP >= 1 ? MAX_STACK_STEP : stackSize;
instructions.add(new Sub(SP, SP,
new Operand2(realStackSize)));
stackSize = stackSize - realStackSize;
}
}
}
......@@ -328,12 +328,13 @@ public class ASTPrinter implements NodeVisitor<Void> {
/* body */
leadingSpace += INDENT_SIZE;
visit(node.getBody());
leadingSpace -= INDENT_SIZE;
appendLeadingSpace();
System.out.print("while ");
visit(node.getCond());
System.out.println(" :");
leadingSpace -= INDENT_SIZE;
} else {
System.out.print("while ");
visit(node.getCond());
......@@ -384,10 +385,16 @@ public class ASTPrinter implements NodeVisitor<Void> {
/* for 3 expressions : */
appendLeadingSpace();
System.out.print("for ");
leadingSpace += INDENT_SIZE;
visit(node.getInit());
appendLeadingSpace();
visit(node.getCond());
visit(node.getIncrement());
System.out.println(" :");
leadingSpace -= INDENT_SIZE;
appendLeadingSpace();
System.out.println(":");
/* body */
leadingSpace += INDENT_SIZE;
......@@ -406,15 +413,18 @@ public class ASTPrinter implements NodeVisitor<Void> {
public Void visitSwitchNode(SwitchNode node) {
/* switch statement */
appendLeadingSpace();
System.out.println("switch ");
System.out.println(node.getExpr());
System.out.print("switch ");
visit(node.getExpr());
System.out.println();
/* switch body */
leadingSpace += INDENT_SIZE;
for (CaseStat c : node.getCases()) {
System.out.println("case ");
appendLeadingSpace();
System.out.print("case ");
visit(c.getExpr());
System.out.println();
/* case body */
leadingSpace += INDENT_SIZE;
......@@ -422,9 +432,12 @@ public class ASTPrinter implements NodeVisitor<Void> {
leadingSpace -= INDENT_SIZE;
}
appendLeadingSpace();
System.out.println("default");
leadingSpace += INDENT_SIZE;
visit(node.getDefault());
leadingSpace -= INDENT_SIZE;
leadingSpace -= 2 * INDENT_SIZE;
return null;
}
......
......@@ -566,7 +566,7 @@ public class SemanticChecker extends WACCParserBaseVisitor<Node> {
if (!isContinueAllowed.peek()) {
branchStatementPositionError(ctx, JumpType.CONTINUE);
}
StatNode continueNode = new JumpNode(JumpType.CONTINUE, currForLoopIncrementContinue.peek(), jumpContext.peek());
StatNode continueNode = new JumpNode(JumpType.CONTINUE, null, jumpContext.peek());
continueNode.setScope(currSymbolTable);
isJumpRepeated.pop();
isJumpRepeated.push(true);
......
{scope
{scope
INTEGER a0 = 0
INTEGER b0 = 0
CHAR c0 = <char:32>
INTEGER d0 = 32
INTEGER i = 0
INTEGER j = 0
for {scope
i (17) = 0
j (21) = 1
}
true {scope
i (17) = i (17) PLUS 1
}
:
{scope
for {scope
i (17) = 0
j (21) = 1
}
true {scope
i (17) = i (17) PLUS 1
}
:
{scope
b0 (8) = 1
while true :
{scope
{scope
c0 (9) = <char:97>
d0 (13) = 32
}
......
{scope size = 20 current scope = utils.frontend.symbolTable.SymbolTable@67b64c45 parent = null
{scope
INTEGER a = 0
INTEGER b = 0
INTEGER c = 0
INTEGER d = 0
switch
frontend.node.expr.IntegerNode@6bf256fa
case
0 b (8) = 1
case
1 b (8) = 0
case
4 d (16) = 0
c (12) = 1
switch 3
case 0
b (8) = 1
case 1
b (8) = 0
case 4
d (16) = 0
default
c (12) = 1
INTEGER e = 1
e (20) = b (8) PLUS 1
e (20) = c (12) PLUS 1
......
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