decrease main class

This commit is contained in:
ChUrl
2021-01-29 22:05:38 +01:00
parent 290d4f1ff6
commit 3a355d9224
3 changed files with 25 additions and 48 deletions

View File

@ -1,4 +1,3 @@
import codegen.CodeGenerator;
import codegen.analysis.dataflow.DataFlowGraph; import codegen.analysis.dataflow.DataFlowGraph;
import codegen.analysis.liveness.LivenessAnalysis; import codegen.analysis.liveness.LivenessAnalysis;
import codegen.flowgraph.FlowGraph; import codegen.flowgraph.FlowGraph;
@ -42,47 +41,12 @@ public final class StupsCompiler {
System.out.println("Beginning compilation."); System.out.println("Beginning compilation.");
final long begin = System.nanoTime(); final long begin = System.nanoTime();
// File opening + Lexing final FlowGraphGenerator gen = getFlowGraphGen(filename);
Lexer lexer; final FlowGraph graph = gen.generateGraph();
try {
// Relativer Pfad
final Path programPath = Paths.get(System.getProperty("user.dir") + "/" + filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException e) {
try {
// Absoluter Pfad
final Path programPath = Paths.get(filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException ee) {
System.out.println("Das Programm konnte nicht gelesen werden.");
return;
}
}
// Grammar parsing from file
final Grammar grammar;
try {
final Path grammarFile = Paths.get(System.getProperty("user.dir") + "/stups.grammar");
grammar = Grammar.fromFile(grammarFile);
} catch (IOException e) {
System.out.println("Die Grammatik konnte nicht geöffnet werden.");
return;
}
// Parser from Grammar
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
// Parsing + Typechecking of program
final AST tree = stupsParser.parse(lexer.getAllTokens(), lexer.getVocabulary());
tree.postprocess(grammar);
final Map<ASTNode, String> nodeTable = TypeChecker.validate(tree);
// Codegeneration + Output // Codegeneration + Output
final String outputName = filename.replaceFirst("\\.stups", ".j"); final String outputName = filename.replaceFirst("\\.stups", ".j");
final String sourceCode = CodeGenerator.generateCode(tree, nodeTable, filename); final String sourceCode = graph.toString();
try { try {
final Path outputFile = Paths.get(System.getProperty("user.dir") + "/" + outputName); final Path outputFile = Paths.get(System.getProperty("user.dir") + "/" + outputName);
Files.writeString(outputFile, sourceCode); Files.writeString(outputFile, sourceCode);
@ -107,6 +71,14 @@ public final class StupsCompiler {
private static void liveness(String filename) { private static void liveness(String filename) {
System.out.println("Liveness-Analyse für " + filename); System.out.println("Liveness-Analyse für " + filename);
final FlowGraphGenerator gen = getFlowGraphGen(filename);
final FlowGraph graph = gen.generateGraph();
final DataFlowGraph dataFlowGraph = DataFlowGraph.fromSourceGraph(graph);
LivenessAnalysis.doLivenessAnalysis(dataFlowGraph, gen.getVarMap());
}
private static FlowGraphGenerator getFlowGraphGen(String filename) {
// File opening + Lexing // File opening + Lexing
Lexer lexer; Lexer lexer;
try { try {
@ -123,7 +95,7 @@ public final class StupsCompiler {
lexer = new StupsLexer(CharStreams.fromPath(programPath)); lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException ee) { } catch (IOException ee) {
System.out.println("Das Programm konnte nicht gelesen werden."); System.out.println("Das Programm konnte nicht gelesen werden.");
return; throw new IllegalStateException("Das Programm konnte nicht gelesen werden.");
} }
} }
@ -134,7 +106,7 @@ public final class StupsCompiler {
grammar = Grammar.fromFile(grammarFile); grammar = Grammar.fromFile(grammarFile);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Die Grammatik konnte nicht geöffnet werden."); System.out.println("Die Grammatik konnte nicht geöffnet werden.");
return; throw new IllegalStateException("Die Grammatik konnte nicht geöffnet werden.");
} }
// Parser from Grammar // Parser from Grammar
@ -145,10 +117,6 @@ public final class StupsCompiler {
tree.postprocess(grammar); tree.postprocess(grammar);
final Map<ASTNode, String> nodeTable = TypeChecker.validate(tree); final Map<ASTNode, String> nodeTable = TypeChecker.validate(tree);
final FlowGraphGenerator gen = FlowGraphGenerator.fromAST(tree, nodeTable, filename); return FlowGraphGenerator.fromAST(tree, nodeTable, filename);
final FlowGraph graph = gen.generateGraph();
final DataFlowGraph dataFlowGraph = DataFlowGraph.fromSourceGraph(graph);
LivenessAnalysis.doLivenessAnalysis(dataFlowGraph, gen.getVarMap());
} }
} }

View File

@ -70,7 +70,8 @@ public final class LivenessAnalysis {
for (Map.Entry<String, Integer> var : varMap.entrySet()) { for (Map.Entry<String, Integer> var : varMap.entrySet()) {
interferenceGraph.add(new InterferenceNode(var.getValue().toString())); interferenceGraph.add(new InterferenceNode(var.getValue().toString()));
} }
System.out.println("Interference nodes: " + interferenceGraph);
Logger.log("Interference nodes: " + interferenceGraph);
// Determine neighbours // Determine neighbours
for (DataFlowNode node : nodes) { for (DataFlowNode node : nodes) {
@ -119,7 +120,7 @@ public final class LivenessAnalysis {
Logger.call(() -> printInterferenceGraphToPicture(interferenceGraph)); Logger.call(() -> printInterferenceGraphToPicture(interferenceGraph));
System.out.println("\nLiveness: " + colors + " register nötig."); System.out.println("\nRegisters: " + colors);
} }
// Printing // Printing

View File

@ -1,5 +1,7 @@
package util; package util;
import java.util.function.Supplier;
// Maximal professioneller Logger // Maximal professioneller Logger
public final class Logger { public final class Logger {
@ -24,4 +26,10 @@ public final class Logger {
log(message); log(message);
} }
} }
public static void call(Supplier<String> call) {
if (enabled) {
call.get();
}
}
} }