1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-08-02 09:29:35 +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;
/** Log of actions & results during compile*/
/**
* Log of actions & results during compile
*/
public class CompileLog {
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() {
this.log = new StringBuilder();
@ -18,7 +44,9 @@ public class CompileLog {
public void append(String msg) {
log.append(msg);
log.append("\n");
//System.out.append(msg+"\n");
if(sysOut) {
System.out.append(msg + "\n");
}
}
public StringBuilder getLog() {
@ -41,6 +69,38 @@ public class CompileLog {
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
public String toString() {
return log.toString();

View File

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

View File

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