Skip to content

Commit

Permalink
test correctness pass (except 2 testcase)
Browse files Browse the repository at this point in the history
  • Loading branch information
abcdabcd987 committed May 5, 2016
1 parent 8b5c5fd commit 6abec16
Show file tree
Hide file tree
Showing 27 changed files with 2,677 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ Temporary Items


!lib/*.jar
lib/statspim/usr/
1 change: 1 addition & 0 deletions lib/statspim
Submodule statspim added at 720f6d
5 changes: 5 additions & 0 deletions src/com/abcdabcd987/compiler2016/FrontEnd/IRBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@ public void visit(FunctionCall node) {
node.parameters.forEach(x -> call.appendArg(x.intValue));
curBB.append(call);
node.intValue = reg;


if (node.ifTrue != null) {
curBB.end(new Branch(curBB, node.intValue, node.ifTrue, node.ifFalse));
}
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/com/abcdabcd987/compiler2016/MIPS/MIPSPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public void visit(IRRoot node) {
out.println(" sub $sp, $sp, 4");
out.println(" sw $ra, 0($sp)");
out.println(" jal " + blockLabel(node.functions.get("__init").getStartBB()));
out.println(" move $a0, $v0");
out.println(" li $v0, 1");
out.println(" syscall");
// // print exitcode
// out.println(" move $a0, $v0");
// out.println(" li $v0, 1");
// out.println(" syscall");
out.println(" lw $ra, 0($sp)");
out.println(" jr $ra");
out.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ private void replaceImmediateNumber(Function func, BasicBlock BB, IRInstruction
IntComparison icmp = (IntComparison) inst;
if (icmp.getLhs() instanceof IntImmediate) {
VirtualRegister lhs = new VirtualRegister("lhs");
icmp.setLhs(lhs);
inst.prepend(new Move(BB, lhs, icmp.getLhs()));
icmp.setLhs(lhs);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/com/abcdabcd987/compiler2016/Utility/TeeOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Created by abcdabcd987 on 2016-04-18.
*/
public class TeeOutputStream extends OutputStream {
private OutputStream[] streams;
private List<OutputStream> streams = new ArrayList<>();

public TeeOutputStream(OutputStream... streams) {
super();
this.streams = streams;
Collections.addAll(this.streams, streams);
}

public void add(OutputStream outputStream) {
streams.add(outputStream);
}

@Override
Expand Down
97 changes: 82 additions & 15 deletions test/com/abcdabcd987/compiler2016/BackEnd/FinalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.abcdabcd987.compiler2016.CompilerOptions;
import com.abcdabcd987.compiler2016.Mill;
import com.abcdabcd987.compiler2016.Utility.TeeOutputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
* Created by abcdabcd987 on 2016-04-13.
Expand All @@ -19,31 +19,98 @@
public class FinalTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
final String dataRoot = "testcase/final/";
final String srcExt = ".mx";
final String inExt = ".in";
final String ansExt = ".out";
SortedSet<String> files = new TreeSet<>(Arrays.stream(new File(dataRoot).listFiles())
.filter(File::isFile).map(x -> dataRoot + x.getName()).collect(Collectors.toSet()));
Collection<Object[]> params = new ArrayList<>();
for (File f : new File("testcase/final/").listFiles()) {
if (f.isFile() && f.getName().endsWith(".mx")) {
params.add(new Object[] { "testcase/final/" + f.getName() });
}
for (String file : files) {
if (!file.endsWith(srcExt)) continue;
String name = file.substring(0, file.length()-srcExt.length());
String in = files.contains(name + inExt) ? name + inExt : null;
String ans = files.contains(name + ansExt) ? name + ansExt : null;
params.add(new Object[] { file, in, ans });
}
return params;
}

private String filename;
private String srcFile;
private String inFile;
private String ansFile;

public FinalTest(String filename) {
this.filename = filename;
public FinalTest(String srcFile, String inFile, String ansFile) {
this.inFile = inFile;
this.ansFile = ansFile;
this.srcFile = srcFile;
}

@Test
public void testPass() throws Exception {
System.out.println("#" + filename);
System.out.println("# " + srcFile);
System.out.flush();
if (ansFile == null) throw new RuntimeException("no ans file");

final String outputPath = "./out/out.s";
final boolean runSPIM = true;
final boolean printToStdout = false;
CompilerOptions.ifPrintAST = false;
CompilerOptions.ifPrintRawIR = false;
CompilerOptions.ifPrintSSAIR = false;
CompilerOptions.enableSSA = false;
CompilerOptions.registerAllocator = "no";

InputStream is = new FileInputStream(filename);
new Mill(is, System.out).run();
TeeOutputStream tee = new TeeOutputStream();
OutputStream fileOut = new FileOutputStream(outputPath);
PrintStream out = new PrintStream(tee);
if (printToStdout) tee.add(System.out);
tee.add(fileOut);

InputStream is = new FileInputStream(srcFile);
new Mill(is, out).run();

out.flush();
fileOut.close();

if (runSPIM) {
Process spim = new ProcessBuilder("./lib/statspim/usr/bin/spim", "-lstack", "33554432", "-ldata", "33554432", "-stat", "-file", outputPath)
.redirectInput(ProcessBuilder.Redirect.PIPE)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start();
if (inFile != null) {
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintStream spimOut = new PrintStream(spim.getOutputStream());
String line;
while ((line = br.readLine()) != null)
spimOut.println(line);
spimOut.close();
}
spim.waitFor();

BufferedReader brAns = new BufferedReader(new FileReader(ansFile));
BufferedReader brOut = new BufferedReader(new InputStreamReader(spim.getInputStream()));
boolean correct = true;
System.out.println("PROGRAM OUTPUT:");
while (true) {
String lineOut = brOut.readLine();
String lineAns = brAns.readLine();
if ((lineOut == null) != (lineAns == null)) correct = false;
if (lineOut == null) break;
System.out.println(lineOut);
if (correct && !lineAns.trim().equals(lineOut.trim())) {
correct = false;
System.out.println("========== ANS OUTPUT: " + lineAns);
}
}


BufferedReader brErr = new BufferedReader(new InputStreamReader(spim.getErrorStream()));
System.out.println("STDERR:");
String line;
while ((line = brErr.readLine()) != null) System.out.println(line);
assertTrue(correct);
}
}
}
1 change: 1 addition & 0 deletions testcase/final/basicopt1-5100309127-hetianxing.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
587050
1 change: 1 addition & 0 deletions testcase/final/builtin-5140519064-youyurong.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
3 changes: 3 additions & 0 deletions testcase/final/builtin-5140519064-youyurong.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I have done 3 little thingstoo young!
8four
45
Loading

0 comments on commit 6abec16

Please sign in to comment.