diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass5FixLongBranches.java b/src/main/java/dk/camelot64/kickc/passes/Pass5FixLongBranches.java index 212121e59..81306644b 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass5FixLongBranches.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass5FixLongBranches.java @@ -43,14 +43,13 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization { } /** - * Perform - * @return + * Detect if any branch distance is to long, and fix it by rewriting the branch if it is. + * @return true if any branch was rewritten */ private boolean step() { // Reindex ASM lines new Pass5ReindexAsmLines(getProgram()).optimize(); - // Create a temporary directory for the ASM file try { tmpDir = Files.createTempDirectory("kickc"); @@ -68,7 +67,6 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization { throw new CompileError("Error writing ASM temp file.", e); } - // Compile using KickAssembler - catch the output in a String File asmFile = getTmpFile(fileName, ".asm"); File asmPrgFile = getTmpFile(fileName, ".prg"); @@ -78,7 +76,7 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization { System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); String output = kickAssOut.toString(); - // Look for a long branch error + // Look for a long branch distance error if(asmRes != 0) { String outputLines[] = output.split("\\r?\\n"); for(int i = 0; i < outputLines.length; i++) { @@ -126,11 +124,11 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization { getLog().append("Fixing long branch ["+idx+"] "+asmLine.toString() + " to "+inverseType.getMnemnonic()); String branchDest = asmInstruction.getParameter(); asmInstruction.setType(inverseType); - asmInstruction.setParameter("!b+"); + asmInstruction.setParameter("!"+branchDest+"+"); AsmInstructionType jmpType = AsmInstructionSet.getInstructionType("jmp", AsmAddressingMode.ABS, false); AsmInstruction jmpInstruction = new AsmInstruction(jmpType, branchDest); asmSegment.addLineAfter(asmInstruction, jmpInstruction); - asmSegment.addLineAfter(jmpInstruction, new AsmLabel("!b")); + asmSegment.addLineAfter(jmpInstruction, new AsmLabel("!"+branchDest)); return true; } } diff --git a/src/test/java/dk/camelot64/kickc/test/ref/halfscii.asm b/src/test/java/dk/camelot64/kickc/test/ref/halfscii.asm index eb945103a..05c27c03d 100644 --- a/src/test/java/dk/camelot64/kickc/test/ref/halfscii.asm +++ b/src/test/java/dk/camelot64/kickc/test/ref/halfscii.asm @@ -126,15 +126,15 @@ main: { !: lda chargen+1 cmp #>CHARGEN+$800 - bcs !b+ + bcs !b1+ jmp b1 - !b: + !b1: bne !+ lda chargen cmp #CHARGEN+$800 - bcs !b+ + bcs !b1+ jmp b1 - !b: + !b1: bne !+ lda chargen cmp #