| 1 | package com.rox.debug.cpu; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.HashMap; | |
| 5 | import java.util.List; | |
| 6 | import java.util.Map; | |
| 7 | import java.util.Set; | |
| 8 | import java.util.regex.Matcher; | |
| 9 | import java.util.regex.Pattern; | |
| 10 | ||
| 11 | /** | |
| 12 | * Finds what to color in {@link CpuStateText#panel} output. Spans may overlap: a background kind | |
| 13 | * ({@link Kind#CHANGED}, {@link Kind#CURRENT_LINE}) layers over a text-colour kind. On instruction lines | |
| 14 | * carrying a state, each value that differs from the next such line down is marked {@link Kind#CHANGED}. | |
| 15 | */ | |
| 16 | public final class CpuTextHighlighter { | |
| 17 | public enum Kind { | |
| 18 | PAUSED, REGISTER_NAME, REGISTER_VALUE, CHANGED, FLAG_SET, FLAG_CLEAR, ACTIVE, HEADER, | |
| 19 | CURRENT_LINE, ADDRESS, JUMP_FLASH, FLOW, LOAD_STORE, ARITHMETIC, FLAGS_AND_STACK, IMMEDIATE, OPERAND, UNKNOWN | |
| 20 | } | |
| 21 | ||
| 22 | /** {@code length} characters of the text from {@code start}. */ | |
| 23 | public record Span(int start, int length, Kind kind) { | |
| 24 | } | |
| 25 | ||
| 26 | private static final Set<String> FLOW = Set.of("JMP", "JSR", "RTS", "RTI", "BRK", | |
| 27 | "BCC", "BCS", "BEQ", "BMI", "BNE", "BPL", "BVC", "BVS"); | |
| 28 | private static final Set<String> LOAD_STORE = Set.of("LDA", "LDX", "LDY", "STA", "STX", "STY", | |
| 29 | "TAX", "TAY", "TXA", "TYA", "TSX", "TXS"); | |
| 30 | private static final Set<String> ARITHMETIC = Set.of("ADC", "SBC", "AND", "ORA", "EOR", "CMP", "CPX", "CPY", | |
| 31 | "BIT", "ASL", "LSR", "ROL", "ROR", "INC", "DEC", "INX", "INY", "DEX", "DEY"); | |
| 32 | private static final Set<String> FLAGS_AND_STACK = Set.of("CLC", "SEC", "CLI", "SEI", "CLV", "CLD", "SED", | |
| 33 | "PHA", "PLA", "PHP", "PLP", "NOP"); | |
| 34 | ||
| 35 | private static final Pattern REGISTER = Pattern.compile("(PC|A|X|Y|SP): (\\$[0-9A-F]+)"); | |
| 36 | private static final Pattern ACTIVE = Pattern.compile("\\b(asserted|yes)\\b"); | |
| 37 | private static final Pattern INSTRUCTION = Pattern.compile("([> ]) (\\$[0-9A-F]{4}) (\\S+(?: \\S+)?)(?: +(A:.*))?"); | |
| 38 | private static final Pattern LINE_REGISTER = Pattern.compile("(A|X|Y|SP):([0-9A-F]{2})"); | |
| 39 | private static final Pattern LINE_FLAGS = Pattern.compile("[Nn][Vv]-[Bb][Dd][Ii][Zz][Cc]"); | |
| 40 | private static final Pattern LINE_INTERRUPT = Pattern.compile("\\b(irq|nmi)\\b"); | |
| 41 | private static final String FLAG_NAMES = "NV-BDIZC"; | |
| 42 | private static final String FLAGS_PREFIX = "Flags: "; | |
| 43 | private static final int FLAG_COUNT = 8; //NV-BDIZC | |
| 44 | ||
| 45 | private CpuTextHighlighter(){ | |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * @param changed register/flag names to mark as {@link Kind#CHANGED} - see {@link RegisterChanges} | |
| 50 | */ | |
| 51 | public static List<Span> highlight(final String text, final Set<String> changed){ | |
| 52 |
1
1. highlight : replaced return value with Collections.emptyList for com/rox/debug/cpu/CpuTextHighlighter::highlight → KILLED |
return highlight(text, changed, false); |
| 53 | } | |
| 54 | ||
| 55 | /** @param flashCurrentAddress also mark the current instruction's address as {@link Kind#JUMP_FLASH} - see {@link JumpFlash} */ | |
| 56 | public static List<Span> highlight(final String text, final Set<String> changed, final boolean flashCurrentAddress){ | |
| 57 | final List<Span> spans = new ArrayList<>(); | |
| 58 | final List<LineState> states = new ArrayList<>(); | |
| 59 | int lineStart = 0; | |
| 60 | for (final String line : text.split("\n", -1)){ | |
| 61 |
1
1. highlight : removed call to com/rox/debug/cpu/CpuTextHighlighter::highlightLine → KILLED |
highlightLine(line, lineStart, changed, flashCurrentAddress, spans, states); |
| 62 |
2
1. highlight : Replaced integer addition with subtraction → KILLED 2. highlight : Replaced integer addition with subtraction → KILLED |
lineStart += line.length() + 1; |
| 63 | } | |
| 64 |
3
1. highlight : negated conditional → KILLED 2. highlight : changed conditional boundary → KILLED 3. highlight : Replaced integer addition with subtraction → KILLED |
for (int i = 0; i + 1 < states.size(); i++){ |
| 65 |
2
1. highlight : removed call to com/rox/debug/cpu/CpuTextHighlighter::markChangesFromBelow → KILLED 2. highlight : Replaced integer addition with subtraction → KILLED |
markChangesFromBelow(states.get(i), states.get(i + 1), spans); |
| 66 | } | |
| 67 |
1
1. highlight : replaced return value with Collections.emptyList for com/rox/debug/cpu/CpuTextHighlighter::highlight → KILLED |
return spans; |
| 68 | } | |
| 69 | ||
| 70 | /** One instruction line's state values, by name ({@code A}, {@code N}, {@code irq}...), with where each sits. */ | |
| 71 | private record LineState(Map<String, String> values, Map<String, Integer> offsets) { | |
| 72 | } | |
| 73 | ||
| 74 | private static void markChangesFromBelow(final LineState line, final LineState below, final List<Span> spans){ | |
| 75 |
1
1. markChangesFromBelow : removed call to java/util/Map::forEach → KILLED |
line.values().forEach((name, value) -> { |
| 76 |
1
1. lambda$markChangesFromBelow$0 : negated conditional → KILLED |
if (!value.equals(below.values().get(name))){ |
| 77 | spans.add(new Span(line.offsets().get(name), value.length(), Kind.CHANGED)); | |
| 78 | } | |
| 79 | }); | |
| 80 | } | |
| 81 | ||
| 82 | private static void highlightLine(final String line, final int offset, final Set<String> changed, | |
| 83 | final boolean flashCurrentAddress, final List<Span> spans, | |
| 84 | final List<LineState> states){ | |
| 85 |
1
1. highlightLine : negated conditional → KILLED |
if (line.startsWith("PAUSED")){ |
| 86 | spans.add(new Span(offset, "PAUSED".length(), Kind.PAUSED)); | |
| 87 |
1
1. highlightLine : negated conditional → KILLED |
} else if (line.startsWith("PC: ")){ |
| 88 | final Matcher register = REGISTER.matcher(line); | |
| 89 |
1
1. highlightLine : negated conditional → KILLED |
while (register.find()){ |
| 90 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + register.start(1), register.group(1).length(), Kind.REGISTER_NAME)); |
| 91 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + register.start(2), register.group(2).length(), Kind.REGISTER_VALUE)); |
| 92 |
1
1. highlightLine : negated conditional → KILLED |
if (changed.contains(register.group(1))){ |
| 93 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + register.start(2), register.group(2).length(), Kind.CHANGED)); |
| 94 | } | |
| 95 | } | |
| 96 |
1
1. highlightLine : negated conditional → KILLED |
} else if (line.startsWith(FLAGS_PREFIX)){ |
| 97 |
2
1. highlightLine : negated conditional → KILLED 2. highlightLine : changed conditional boundary → KILLED |
for (int i = 0; i < FLAG_COUNT; i++){ |
| 98 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
final int column = FLAGS_PREFIX.length() + i; |
| 99 | final char flag = line.charAt(column); | |
| 100 |
1
1. highlightLine : negated conditional → KILLED |
if (flag == '-'){ |
| 101 | continue; | |
| 102 | } | |
| 103 |
2
1. highlightLine : Replaced integer addition with subtraction → KILLED 2. highlightLine : negated conditional → KILLED |
spans.add(new Span(offset + column, 1, Character.isUpperCase(flag) ? Kind.FLAG_SET : Kind.FLAG_CLEAR)); |
| 104 |
1
1. highlightLine : negated conditional → KILLED |
if (changed.contains(String.valueOf(Character.toUpperCase(flag)))){ |
| 105 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + column, 1, Kind.CHANGED)); |
| 106 | } | |
| 107 | } | |
| 108 |
1
1. highlightLine : negated conditional → KILLED |
} else if (line.startsWith("IRQ line: ")){ |
| 109 | final Matcher active = ACTIVE.matcher(line); | |
| 110 |
1
1. highlightLine : negated conditional → KILLED |
while (active.find()){ |
| 111 |
1
1. highlightLine : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + active.start(), active.group().length(), Kind.ACTIVE)); |
| 112 | } | |
| 113 |
1
1. highlightLine : negated conditional → KILLED |
} else if (line.startsWith("---")){ |
| 114 | spans.add(new Span(offset, line.length(), Kind.HEADER)); | |
| 115 | } else { | |
| 116 |
1
1. highlightLine : removed call to com/rox/debug/cpu/CpuTextHighlighter::highlightInstruction → KILLED |
highlightInstruction(line, offset, flashCurrentAddress, spans, states); |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | private static void highlightInstruction(final String line, final int offset, final boolean flashCurrentAddress, | |
| 121 | final List<Span> spans, final List<LineState> states){ | |
| 122 | final Matcher instruction = INSTRUCTION.matcher(line); | |
| 123 |
1
1. highlightInstruction : negated conditional → KILLED |
if (!instruction.matches()){ |
| 124 | return; | |
| 125 | } | |
| 126 |
1
1. highlightInstruction : negated conditional → KILLED |
if (instruction.group(1).equals(">")){ |
| 127 | spans.add(new Span(offset, line.length(), Kind.CURRENT_LINE)); | |
| 128 | } | |
| 129 |
1
1. highlightInstruction : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + instruction.start(2), instruction.group(2).length(), Kind.ADDRESS)); |
| 130 |
2
1. highlightInstruction : negated conditional → KILLED 2. highlightInstruction : negated conditional → KILLED |
if (flashCurrentAddress && instruction.group(1).equals(">")){ |
| 131 |
1
1. highlightInstruction : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + instruction.start(2), instruction.group(2).length(), Kind.JUMP_FLASH)); |
| 132 | } | |
| 133 |
1
1. highlightInstruction : negated conditional → KILLED |
if (instruction.group(4) != null){ |
| 134 |
1
1. highlightInstruction : Replaced integer addition with subtraction → KILLED |
states.add(highlightState(instruction.group(4), offset + instruction.start(4), spans)); |
| 135 | } | |
| 136 | final String code = instruction.group(3); | |
| 137 |
1
1. highlightInstruction : Replaced integer addition with subtraction → KILLED |
final int codeStart = offset + instruction.start(3); |
| 138 |
1
1. highlightInstruction : negated conditional → KILLED |
if (code.startsWith("???")){ |
| 139 | spans.add(new Span(codeStart, code.length(), Kind.UNKNOWN)); | |
| 140 | return; | |
| 141 | } | |
| 142 | final String[] parts = code.split(" ", 2); | |
| 143 | final String mnemonic = parts[0]; | |
| 144 | final Kind kind = mnemonicKind(mnemonic); | |
| 145 |
1
1. highlightInstruction : negated conditional → KILLED |
if (kind != null){ |
| 146 | spans.add(new Span(codeStart, mnemonic.length(), kind)); | |
| 147 | } | |
| 148 |
1
1. highlightInstruction : negated conditional → KILLED |
if (parts.length == 2){ |
| 149 | final String operand = parts[1]; | |
| 150 |
2
1. highlightInstruction : Replaced integer addition with subtraction → KILLED 2. highlightInstruction : Replaced integer addition with subtraction → KILLED |
final int operandStart = codeStart + mnemonic.length() + 1; |
| 151 |
1
1. highlightInstruction : negated conditional → KILLED |
if (operand.startsWith("#")){ |
| 152 | spans.add(new Span(operandStart, operand.length(), Kind.IMMEDIATE)); | |
| 153 |
1
1. highlightInstruction : negated conditional → KILLED |
} else if (operand.contains("$")){ |
| 154 | spans.add(new Span(operandStart, operand.length(), Kind.OPERAND)); | |
| 155 | } | |
| 156 | } | |
| 157 | } | |
| 158 | ||
| 159 | /** Colours an instruction line's state, returning its values for the line-to-line comparison. */ | |
| 160 | private static LineState highlightState(final String state, final int offset, final List<Span> spans){ | |
| 161 | final Map<String, String> values = new HashMap<>(); | |
| 162 | final Map<String, Integer> offsets = new HashMap<>(); | |
| 163 | final Matcher register = LINE_REGISTER.matcher(state); | |
| 164 |
1
1. highlightState : negated conditional → KILLED |
while (register.find()){ |
| 165 |
1
1. highlightState : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + register.start(1), register.group(1).length(), Kind.REGISTER_NAME)); |
| 166 |
1
1. highlightState : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + register.start(2), register.group(2).length(), Kind.REGISTER_VALUE)); |
| 167 | values.put(register.group(1), register.group(2)); | |
| 168 |
1
1. highlightState : Replaced integer addition with subtraction → KILLED |
offsets.put(register.group(1), offset + register.start(2)); |
| 169 | } | |
| 170 | final Matcher flags = LINE_FLAGS.matcher(state); | |
| 171 |
1
1. highlightState : negated conditional → KILLED |
if (flags.find()){ |
| 172 |
2
1. highlightState : changed conditional boundary → KILLED 2. highlightState : negated conditional → KILLED |
for (int i = 0; i < FLAG_NAMES.length(); i++){ |
| 173 | final char flag = flags.group().charAt(i); | |
| 174 |
1
1. highlightState : negated conditional → KILLED |
if (flag == '-'){ |
| 175 | continue; | |
| 176 | } | |
| 177 |
2
1. highlightState : Replaced integer addition with subtraction → KILLED 2. highlightState : Replaced integer addition with subtraction → KILLED |
final int at = offset + flags.start() + i; |
| 178 |
1
1. highlightState : negated conditional → KILLED |
spans.add(new Span(at, 1, Character.isUpperCase(flag) ? Kind.FLAG_SET : Kind.FLAG_CLEAR)); |
| 179 | values.put(String.valueOf(FLAG_NAMES.charAt(i)), String.valueOf(flag)); | |
| 180 | offsets.put(String.valueOf(FLAG_NAMES.charAt(i)), at); | |
| 181 | } | |
| 182 | } | |
| 183 | final Matcher interrupt = LINE_INTERRUPT.matcher(state); | |
| 184 |
1
1. highlightState : negated conditional → KILLED |
while (interrupt.find()){ |
| 185 |
1
1. highlightState : Replaced integer addition with subtraction → KILLED |
spans.add(new Span(offset + interrupt.start(), interrupt.group().length(), Kind.ACTIVE)); |
| 186 | values.put(interrupt.group(), interrupt.group()); | |
| 187 |
1
1. highlightState : Replaced integer addition with subtraction → KILLED |
offsets.put(interrupt.group(), offset + interrupt.start()); |
| 188 | } | |
| 189 |
1
1. highlightState : replaced return value with null for com/rox/debug/cpu/CpuTextHighlighter::highlightState → KILLED |
return new LineState(values, offsets); |
| 190 | } | |
| 191 | ||
| 192 | private static Kind mnemonicKind(final String mnemonic){ | |
| 193 |
1
1. mnemonicKind : negated conditional → KILLED |
if (FLOW.contains(mnemonic)){ |
| 194 |
1
1. mnemonicKind : replaced return value with null for com/rox/debug/cpu/CpuTextHighlighter::mnemonicKind → KILLED |
return Kind.FLOW; |
| 195 | } | |
| 196 |
1
1. mnemonicKind : negated conditional → KILLED |
if (LOAD_STORE.contains(mnemonic)){ |
| 197 |
1
1. mnemonicKind : replaced return value with null for com/rox/debug/cpu/CpuTextHighlighter::mnemonicKind → KILLED |
return Kind.LOAD_STORE; |
| 198 | } | |
| 199 |
1
1. mnemonicKind : negated conditional → KILLED |
if (ARITHMETIC.contains(mnemonic)){ |
| 200 |
1
1. mnemonicKind : replaced return value with null for com/rox/debug/cpu/CpuTextHighlighter::mnemonicKind → KILLED |
return Kind.ARITHMETIC; |
| 201 | } | |
| 202 |
1
1. mnemonicKind : negated conditional → KILLED |
if (FLAGS_AND_STACK.contains(mnemonic)){ |
| 203 |
1
1. mnemonicKind : replaced return value with null for com/rox/debug/cpu/CpuTextHighlighter::mnemonicKind → KILLED |
return Kind.FLAGS_AND_STACK; |
| 204 | } | |
| 205 | return null; | |
| 206 | } | |
| 207 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 2.2 |
|
| 64 |
1.1 2.2 3.3 |
|
| 65 |
1.1 2.2 |
|
| 67 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 85 |
1.1 |
|
| 87 |
1.1 |
|
| 89 |
1.1 |
|
| 90 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 93 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 2.2 |
|
| 98 |
1.1 |
|
| 100 |
1.1 |
|
| 103 |
1.1 2.2 |
|
| 104 |
1.1 |
|
| 105 |
1.1 |
|
| 108 |
1.1 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 113 |
1.1 |
|
| 116 |
1.1 |
|
| 123 |
1.1 |
|
| 126 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 2.2 |
|
| 131 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 137 |
1.1 |
|
| 138 |
1.1 |
|
| 145 |
1.1 |
|
| 148 |
1.1 |
|
| 150 |
1.1 2.2 |
|
| 151 |
1.1 |
|
| 153 |
1.1 |
|
| 164 |
1.1 |
|
| 165 |
1.1 |
|
| 166 |
1.1 |
|
| 168 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 2.2 |
|
| 174 |
1.1 |
|
| 177 |
1.1 2.2 |
|
| 178 |
1.1 |
|
| 184 |
1.1 |
|
| 185 |
1.1 |
|
| 187 |
1.1 |
|
| 189 |
1.1 |
|
| 193 |
1.1 |
|
| 194 |
1.1 |
|
| 196 |
1.1 |
|
| 197 |
1.1 |
|
| 199 |
1.1 |
|
| 200 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |