1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-08-03 01:29:04 +00:00

Added more options to CompileLog for controlling verbosity.

This commit is contained in:
jespergravgaard 2017-12-30 17:31:22 +01:00
parent 174346d0f1
commit 58c206cef4
3 changed files with 98 additions and 36 deletions

View File

@ -1,15 +1,41 @@
package dk.camelot64.kickc; package dk.camelot64.kickc;
/** Log of actions & results during compile*/ /**
* Log of actions & results during compile
*/
public class CompileLog { public class CompileLog {
private StringBuilder log; private StringBuilder log;
/** Should register uplift analysis be verbose. */ /**
private boolean verboseUplift; * Should register uplift analysis be verbose.
*/
private boolean verboseUplift = false;
/** Should live range analysis be verbose. */ /**
private boolean verboseLiveRanges; * Should live range analysis be verbose.
*/
private boolean verboseLiveRanges = false;
/**
* Should fragment synthesis be verbose.
*/
private boolean verboseFragmentLog = false;
/**
* Should ASM optimization be verbose.
*/
private boolean verboseAsmOptimize = false;
/**
* Should SSA optimization be verbose.
*/
private boolean verboseSSAOptimize = false;
/**
* Should the log be output to System.out while being built
*/
private boolean sysOut = false;
public CompileLog() { public CompileLog() {
this.log = new StringBuilder(); this.log = new StringBuilder();
@ -18,7 +44,9 @@ public class CompileLog {
public void append(String msg) { public void append(String msg) {
log.append(msg); log.append(msg);
log.append("\n"); log.append("\n");
//System.out.append(msg+"\n"); if(sysOut) {
System.out.append(msg + "\n");
}
} }
public StringBuilder getLog() { public StringBuilder getLog() {
@ -41,6 +69,38 @@ public class CompileLog {
this.verboseLiveRanges = verboseLiveRanges; this.verboseLiveRanges = verboseLiveRanges;
} }
public boolean isVerboseFragmentLog() {
return verboseFragmentLog;
}
public void setVerboseFragmentLog(boolean verboseFragmentLog) {
this.verboseFragmentLog = verboseFragmentLog;
}
public boolean isVerboseAsmOptimize() {
return verboseAsmOptimize;
}
public void setVerboseAsmOptimize(boolean verboseAsmOptimize) {
this.verboseAsmOptimize = verboseAsmOptimize;
}
public boolean isVerboseSSAOptimize() {
return verboseSSAOptimize;
}
public void setVerboseSSAOptimize(boolean verboseSSAOptimize) {
this.verboseSSAOptimize = verboseSSAOptimize;
}
public boolean isSysOut() {
return sysOut;
}
public void setSysOut(boolean sysOut) {
this.sysOut = sysOut;
}
@Override @Override
public String toString() { public String toString() {
return log.toString(); return log.toString();

View File

@ -43,8 +43,8 @@ public class Compiler {
pass4RegisterAllocation(); pass4RegisterAllocation();
pass5GenerateAndOptimizeAsm(); pass5GenerateAndOptimizeAsm();
return program; return program;
} catch (Exception e) { } catch(Exception e) {
System.out.println("EXCEPTION DURING COMPILE "+e.getMessage()); System.out.println("EXCEPTION DURING COMPILE " + e.getMessage());
System.out.println(getLog().toString()); System.out.println(getLog().toString());
throw e; throw e;
} }
@ -54,12 +54,12 @@ public class Compiler {
try { try {
File file = loadFile(fileName, program); File file = loadFile(fileName, program);
List<String> imported = program.getImported(); List<String> imported = program.getImported();
if (imported.contains(file.getAbsolutePath())) { if(imported.contains(file.getAbsolutePath())) {
return; return;
} }
final CharStream fileStream = CharStreams.fromPath(file.toPath()); final CharStream fileStream = CharStreams.fromPath(file.toPath());
imported.add(file.getAbsolutePath()); imported.add(file.getAbsolutePath());
program.getLog().append("PARSING "+file.getPath()); program.getLog().append("PARSING " + file.getPath());
program.getLog().append(fileStream.toString()); program.getLog().append(fileStream.toString());
KickCLexer lexer = new KickCLexer(fileStream); KickCLexer lexer = new KickCLexer(fileStream);
KickCParser parser = new KickCParser(new CommonTokenStream(lexer)); KickCParser parser = new KickCParser(new CommonTokenStream(lexer));
@ -77,23 +77,23 @@ public class Compiler {
} }
}); });
pass0GenerateStatementSequence.generate(parser.file()); pass0GenerateStatementSequence.generate(parser.file());
} catch (IOException e) { } catch(IOException e) {
throw new CompileError("Error loading file " + fileName, e); throw new CompileError("Error loading file " + fileName, e);
} }
} }
private static File loadFile(String fileName, Program program) { private static File loadFile(String fileName, Program program) {
if (!fileName.endsWith(".kc")) { if(!fileName.endsWith(".kc")) {
fileName += ".kc"; fileName += ".kc";
} }
List<String> importPaths = program.getImportPaths(); List<String> importPaths = program.getImportPaths();
for (String importPath : importPaths) { for(String importPath : importPaths) {
if (!importPath.endsWith("/")) { if(!importPath.endsWith("/")) {
importPath += "/"; importPath += "/";
} }
String filePath = importPath + fileName; String filePath = importPath + fileName;
File file = new File(filePath); File file = new File(filePath);
if (file.exists()) { if(file.exists()) {
return file; return file;
} }
} }
@ -163,7 +163,7 @@ public class Compiler {
assertions.add(new Pass2AssertNoProcs(program)); assertions.add(new Pass2AssertNoProcs(program));
assertions.add(new Pass2AssertNoLabels(program)); assertions.add(new Pass2AssertNoLabels(program));
assertions.add(new Pass2AssertSingleAssignment(program)); assertions.add(new Pass2AssertSingleAssignment(program));
for (Pass2SsaAssertion assertion : assertions) { for(Pass2SsaAssertion assertion : assertions) {
assertion.check(); assertion.check();
} }
} }
@ -192,18 +192,20 @@ public class Compiler {
private void pass2OptimizeSSA(List<Pass2SsaOptimization> optimizations) { private void pass2OptimizeSSA(List<Pass2SsaOptimization> optimizations) {
getLog().append("OPTIMIZING CONTROL FLOW GRAPH"); getLog().append("OPTIMIZING CONTROL FLOW GRAPH");
boolean ssaOptimized = true; boolean ssaOptimized = true;
while (ssaOptimized) { while(ssaOptimized) {
pass2AssertSSA(); pass2AssertSSA();
ssaOptimized = false; ssaOptimized = false;
for (Pass2SsaOptimization optimization : optimizations) { for(Pass2SsaOptimization optimization : optimizations) {
boolean stepOptimized = true; boolean stepOptimized = true;
while (stepOptimized) { while(stepOptimized) {
stepOptimized = optimization.step(); stepOptimized = optimization.step();
if (stepOptimized) { if(stepOptimized) {
getLog().append("Succesful SSA optimization " + optimization.getClass().getSimpleName() + ""); getLog().append("Succesful SSA optimization " + optimization.getClass().getSimpleName() + "");
ssaOptimized = true; ssaOptimized = true;
//getLog().append("CONTROL FLOW GRAPH"); if(getLog().isVerboseSSAOptimize()) {
//getLog().append(program.getGraph().toString(program)); getLog().append("CONTROL FLOW GRAPH");
getLog().append(program.getGraph().toString(program));
}
} }
} }
} }
@ -294,7 +296,7 @@ public class Compiler {
boolean change; boolean change;
do { do {
change = new Pass4RegisterUpliftPotentialRegisterAnalysis(program).findPotentialRegisters(); change = new Pass4RegisterUpliftPotentialRegisterAnalysis(program).findPotentialRegisters();
} while (change); } while(change);
getLog().append(program.getRegisterPotentials().toString()); getLog().append(program.getRegisterPotentials().toString());
// Find register uplift scopes // Find register uplift scopes
@ -338,15 +340,17 @@ public class Compiler {
pass5Optimizations.add(new Pass5UnreachableCodeElimination(program)); pass5Optimizations.add(new Pass5UnreachableCodeElimination(program));
pass5Optimizations.add(new Pass5RelabelLongLabels(program)); pass5Optimizations.add(new Pass5RelabelLongLabels(program));
boolean asmOptimized = true; boolean asmOptimized = true;
while (asmOptimized) { while(asmOptimized) {
asmOptimized = false; asmOptimized = false;
for (Pass5AsmOptimization optimization : pass5Optimizations) { for(Pass5AsmOptimization optimization : pass5Optimizations) {
boolean stepOptimized = optimization.optimize(); boolean stepOptimized = optimization.optimize();
if (stepOptimized) { if(stepOptimized) {
getLog().append("Succesful ASM optimization " + optimization.getClass().getSimpleName()); getLog().append("Succesful ASM optimization " + optimization.getClass().getSimpleName());
asmOptimized = true; asmOptimized = true;
//getLog().append("ASSEMBLER"); if(getLog().isVerboseAsmOptimize()) {
//getLog().append(program.getAsm().toString()); getLog().append("ASSEMBLER");
getLog().append(program.getAsm().toString());
}
} }
} }
} }
@ -355,7 +359,7 @@ public class Compiler {
getLog().append(program.getScope().getSymbolTableContents(program)); getLog().append(program.getScope().getSymbolTableContents(program));
getLog().append("\nFINAL ASSEMBLER"); getLog().append("\nFINAL ASSEMBLER");
getLog().append("Score: "+Pass4RegisterUpliftCombinations.getAsmScore(program)+"\n"); getLog().append("Score: " + Pass4RegisterUpliftCombinations.getAsmScore(program) + "\n");
getLog().append(program.getAsm().toString()); getLog().append(program.getAsm().toString());
} }

View File

@ -21,8 +21,6 @@ import java.util.regex.Pattern;
*/ */
public class AsmFragmentManager { public class AsmFragmentManager {
static boolean verboseFragmentLog = false;
/** /**
* Cache for fragment files. Maps signature to the parsed file. * Cache for fragment files. Maps signature to the parsed file.
*/ */
@ -44,7 +42,7 @@ public class AsmFragmentManager {
private static KickCParser.AsmFileContext getFragmentFile(AsmFragmentSignature signature, CompileLog log) { private static KickCParser.AsmFileContext getFragmentFile(AsmFragmentSignature signature, CompileLog log) {
KickCParser.AsmFileContext fragmentCtx = fragmentFileCache.get(signature.getSignature()); KickCParser.AsmFileContext fragmentCtx = fragmentFileCache.get(signature.getSignature());
if(fragmentCtx == UNKNOWN) { if(fragmentCtx == UNKNOWN) {
if(verboseFragmentLog) { if(log.isVerboseFragmentLog()) {
log.append("Unknown fragment " + signature.getSignature()); log.append("Unknown fragment " + signature.getSignature());
} }
throw new UnknownFragmentException(signature.toString()); throw new UnknownFragmentException(signature.toString());
@ -53,7 +51,7 @@ public class AsmFragmentManager {
FragmentSynthesizer synthesizer = new FragmentSynthesizer(signature, log); FragmentSynthesizer synthesizer = new FragmentSynthesizer(signature, log);
List<CharStream> candidates = synthesizer.loadOrSynthesizeFragment(signature.getSignature()); List<CharStream> candidates = synthesizer.loadOrSynthesizeFragment(signature.getSignature());
if(candidates.size() == 0) { if(candidates.size() == 0) {
if(verboseFragmentLog) { if(log.isVerboseFragmentLog()) {
log.append("Unknown fragment " + signature.toString()); log.append("Unknown fragment " + signature.toString());
} }
fragmentFileCache.put(signature.getSignature(), UNKNOWN); fragmentFileCache.put(signature.getSignature(), UNKNOWN);
@ -78,7 +76,7 @@ public class AsmFragmentManager {
fragmentCtx = candidateCtx; fragmentCtx = candidateCtx;
} }
} }
if(verboseFragmentLog) { if(log.isVerboseFragmentLog()) {
log.append("Found fragment " + signature + " score: " + minScore + " from " + candidates.size() + " candidates"); log.append("Found fragment " + signature + " score: " + minScore + " from " + candidates.size() + " candidates");
} }
fragmentFileCache.put(signature.getSignature(), fragmentCtx); fragmentFileCache.put(signature.getSignature(), fragmentCtx);
@ -114,7 +112,7 @@ public class AsmFragmentManager {
CharStream fragmentCharStream = loadFragment(signature); CharStream fragmentCharStream = loadFragment(signature);
if(fragmentCharStream != null) { if(fragmentCharStream != null) {
candidates.add(fragmentCharStream); candidates.add(fragmentCharStream);
if(verboseFragmentLog) { if(log.isVerboseFragmentLog()) {
log.append("Finding fragment "+this.signature.getSignature()+" - Successfully loaded fragment " + signature); log.append("Finding fragment "+this.signature.getSignature()+" - Successfully loaded fragment " + signature);
} }
} }
@ -123,7 +121,7 @@ public class AsmFragmentManager {
for(FragmentSynthesis synth : synths) { for(FragmentSynthesis synth : synths) {
List<CharStream> synthesized = synth.synthesize(signature, this); List<CharStream> synthesized = synth.synthesize(signature, this);
if(synthesized != null) { if(synthesized != null) {
if(verboseFragmentLog && synthesized.size() > 0) { if(log.isVerboseFragmentLog() && synthesized.size() > 0) {
log.append("Finding fragment "+this.signature.getSignature()+" - Successfully synthesized " + synthesized.size() + " fragments " + signature + " (from " + synth.getSubSignature() + ")"); log.append("Finding fragment "+this.signature.getSignature()+" - Successfully synthesized " + synthesized.size() + " fragments " + signature + " (from " + synth.getSubSignature() + ")");
} }
candidates.addAll(synthesized); candidates.addAll(synthesized);