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