@henols/c64-re-tools 2.8.0-rc.1 → 2.8.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +7 -4
- package/skills/c64-assembler/scripts/assembler.ts +73 -44
- package/skills/c64-disk/scripts/disk.ts +3 -3
- package/skills/c64-disk/scripts/lib/containers.ts +2 -2
- package/skills/c64-docs/SKILL.md +1 -1
- package/skills/c64-docs/scripts/docs.ts +12 -20
- package/skills/c64-docs/scripts/lib/links.ts +1 -1
- package/skills/c64-docs/scripts/lib/markdown.ts +1 -1
- package/skills/c64-docs/scripts/lib/reference.ts +59 -13
- package/skills/c64-knowledge/SKILL.md +17 -8
- package/skills/c64-knowledge/scripts/knowledge.ts +200 -218
- package/skills/c64-knowledge/scripts/lib/check.ts +2 -3
- package/skills/c64-knowledge/scripts/lib/listing.ts +62 -31
- package/skills/c64-packer/scripts/packer.ts +3 -3
- package/skills/c64-provenance/SKILL.md +28 -18
- package/skills/c64-provenance/scripts/lib/compare.ts +31 -21
- package/skills/c64-provenance/scripts/lib/leftovers.ts +17 -35
- package/skills/c64-provenance/scripts/lib/library.ts +10 -10
- package/skills/c64-provenance/scripts/lib/runtime.ts +3 -14
- package/skills/c64-provenance/scripts/provenance.ts +144 -113
- package/skills/c64-source/SKILL.md +6 -1
- package/skills/c64-source/references/loader.md +2 -1
- package/skills/c64-source/scripts/lib/export.ts +54 -301
- package/skills/c64-source/scripts/lib/loader.ts +31 -51
- package/skills/c64-source/scripts/lib/source.ts +53 -45
- package/skills/c64-source/scripts/lib/stage.ts +9 -25
- package/skills/c64-source/scripts/source.ts +134 -294
- package/skills/c64-static-analysis/SKILL.md +13 -7
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +1 -3
- package/skills/c64-static-analysis/scripts/lib/findings.ts +16 -3
- package/skills/c64-static-analysis/scripts/lib/idioms.ts +98 -19
- package/skills/c64-static-analysis/scripts/lib/triage.ts +112 -0
- package/skills/c64-static-analysis/scripts/static-analysis.ts +249 -147
- package/skills/c64-testing/SKILL.md +13 -7
- package/skills/c64-testing/scripts/lib/diff-files.ts +6 -13
- package/skills/c64-testing/scripts/lib/recording.ts +47 -66
- package/skills/c64-testing/scripts/lib/replay.ts +53 -9
- package/skills/c64-testing/scripts/lib/runs.ts +11 -29
- package/skills/c64-testing/scripts/lib/scenario.ts +30 -31
- package/skills/c64-testing/scripts/lib/start-screen.ts +42 -5
- package/skills/c64-testing/scripts/testing.ts +32 -7
- package/skills/c64-unpacker/SKILL.md +9 -7
- package/skills/c64-unpacker/scripts/lib/evidence.ts +23 -38
- package/skills/c64-unpacker/scripts/lib/loads.ts +24 -39
- package/skills/c64-unpacker/scripts/lib/machine.ts +38 -18
- package/skills/c64-unpacker/scripts/lib/stages.ts +124 -40
- package/skills/c64-unpacker/scripts/unpacker.ts +81 -68
- package/skills/emulator/SKILL.md +1 -0
- package/src/c64.ts +133 -10
- package/src/cli/ap-sdk.ts +1 -1
- package/src/cli/cleanup.ts +4 -2
- package/src/compare.ts +36 -16
- package/src/errors.ts +10 -1
- package/src/host/main.ts +2 -1
- package/src/host/server.ts +7 -11
- package/src/host/tools/c1541.ts +9 -3
- package/src/host/vice/adapter.ts +20 -43
- package/src/host/vice/binary-monitor.ts +22 -21
- package/src/host/vice/process.ts +31 -40
- package/src/host/vice/session.ts +187 -247
- package/src/host/vice/text-monitor.ts +3 -3
- package/src/host-client/autostart.ts +1 -1
- package/src/host-client/connect.ts +2 -1
- package/src/host-client/coverage.ts +0 -3
- package/src/host-client/entry-state.ts +4 -11
- package/src/host-client/image.ts +6 -11
- package/src/host-client/steps.ts +13 -113
- package/src/host-client/tools.ts +4 -4
- package/src/host-client/transfer.ts +10 -0
- package/src/host-client/vice-session.ts +10 -6
- package/src/knowledge/code-reach.ts +364 -0
- package/src/knowledge/conflicts.ts +34 -72
- package/src/knowledge/database.ts +40 -28
- package/src/knowledge/evidence.ts +2 -2
- package/src/knowledge/graph.ts +3 -3
- package/src/knowledge/history.ts +50 -90
- package/src/knowledge/import.ts +28 -68
- package/src/knowledge/join.ts +1 -1
- package/src/knowledge/names.ts +12 -7
- package/src/knowledge/program.ts +59 -72
- package/src/knowledge/read.ts +104 -100
- package/src/knowledge/schema.ts +9 -3
- package/src/knowledge/separate.ts +175 -80
- package/src/knowledge/start-screen.ts +7 -5
- package/src/knowledge/text.ts +56 -33
- package/src/knowledge/write.ts +274 -515
- package/src/loader.ts +7 -25
- package/src/mcp/inputs.ts +3 -3
- package/src/mcp/server.ts +19 -38
- package/src/mcp/session-source.ts +2 -1
- package/src/mcp/tools/debug.ts +97 -24
- package/src/mcp/tools/execution.ts +1 -1
- package/src/mcp/tools/input.ts +3 -3
- package/src/mcp/tools/machine.ts +3 -3
- package/src/mcp/tools/media.ts +4 -4
- package/src/mcp/tools/memory.ts +13 -17
- package/src/mcp/tools/video.ts +11 -25
- package/src/native/acme.ts +3 -3
- package/src/native/c64run.ts +2 -1
- package/src/native/decompiler/analyze.ts +11 -8
- package/src/native/decompiler/walk.ts +3 -4
- package/src/native/decompiler/wasm.ts +2 -2
- package/src/native/discover.ts +1 -1
- package/src/native/dxa.ts +9 -11
- package/src/native/exomizer.ts +1 -1
- package/src/native/processes.ts +4 -1
- package/src/native/run.ts +3 -3
- package/src/native/types.ts +9 -8
- package/src/opcodes.ts +3 -0
- package/src/platform.ts +9 -10
- package/src/program-code.ts +151 -0
- package/src/project.ts +8 -5
- package/src/protocol/framing.ts +51 -18
- package/src/protocol/messages.ts +1 -11
- package/src/protocol/tools.ts +0 -3
- package/src/protocol/vice.ts +588 -773
- package/src/script.ts +25 -23
- package/src/time.ts +1 -1
- package/src/trace.ts +3 -2
|
@@ -3,80 +3,34 @@
|
|
|
3
3
|
// the README of the new project.
|
|
4
4
|
//
|
|
5
5
|
// A range becomes free only when no run executed or read it and nothing that
|
|
6
|
-
// stays reaches it. The
|
|
7
|
-
//
|
|
8
|
-
// it
|
|
9
|
-
//
|
|
10
|
-
// tells what stays. Without coverage nothing becomes free by itself. While the
|
|
11
|
-
// program has a flow whose targets the knowledge does not give (an indirect
|
|
12
|
-
// jump through a vector that a run wrote, or a jump whose address a run
|
|
13
|
-
// wrote), code that nothing reaches is unknown: it stays.
|
|
6
|
+
// stays reaches it. The code reach (src/knowledge/code-reach.ts) tells which
|
|
7
|
+
// code is unused and which code stays. Data becomes free when no run touched
|
|
8
|
+
// it and nothing that stays names it. Without coverage nothing becomes free by
|
|
9
|
+
// itself.
|
|
14
10
|
|
|
15
|
-
import { type AddressRange,
|
|
11
|
+
import { type AddressRange, c64AddressOf, formatC64Address, formatC64Range, hexWord, inRanges, mergeRanges, POINTER_TABLE_TYPES, rangesOverlap } from "#src/c64.ts";
|
|
16
12
|
import { symbolsPath } from "#src/build-name.ts";
|
|
17
13
|
import { Refusal } from "#src/errors.ts";
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
14
|
+
import { codeReach, type CodeReachInput, isTable, RAM_ACCESS } from "#src/knowledge/code-reach.ts";
|
|
15
|
+
import type { UnresolvedFlow } from "#src/knowledge/separate.ts";
|
|
16
|
+
import { INSTRUCTION_REGION_TYPES, type RegionType } from "#src/knowledge/schema.ts";
|
|
21
17
|
|
|
22
18
|
/**
|
|
23
19
|
* The data region types that can become free by themselves. The VIC-II reads
|
|
24
20
|
* charset, sprite, bitmap and screen data without the CPU, so the coverage
|
|
25
21
|
* does not show that a program uses them: only --drop makes them free.
|
|
26
22
|
*/
|
|
27
|
-
|
|
23
|
+
const DROPPABLE_DATA: readonly RegionType[] = ["bytes", "words", ...POINTER_TABLE_TYPES, "petscii", "workspace", "unused"];
|
|
28
24
|
|
|
29
25
|
/** The data that the VIC-II reads: a region of another type that overlaps it stays too, for example unused bytes in a bitmap. */
|
|
30
26
|
const VIC_DATA: readonly RegionType[] = ["charset", "sprite", "bitmap", "screen"];
|
|
31
27
|
|
|
32
|
-
/** Regions of code: their bytes are the blocks of the decoded instructions. */
|
|
33
|
-
const CODE_TYPES: readonly RegionType[] = ["code", "jumps", "dual-use"];
|
|
34
|
-
|
|
35
28
|
/** The hardware vectors $fffa-$ffff: they stay when the image holds them. */
|
|
36
29
|
const VECTORS: AddressRange = { start: 0xfffa, end: 0xffff };
|
|
37
30
|
|
|
38
|
-
|
|
39
|
-
* The coverage flags that can be a use of the RAM byte: a read or a run of the
|
|
40
|
-
* ROM uses the ROM, not the RAM under it, so a JSR into the KERNAL keeps no byte
|
|
41
|
-
* of the image. The I/O flags stay, as VICE gives each access at $d000-$dfff as I/O.
|
|
42
|
-
*/
|
|
43
|
-
const RAM_ACCESS = 0xff & ~(COVERAGE.romRead | COVERAGE.romExecute);
|
|
44
|
-
|
|
45
|
-
/** The I/O area: two immediates stored there are chip values, such as a timer latch, not an address. */
|
|
46
|
-
const IO_AREA: AddressRange = { start: 0xd000, end: 0xdfff };
|
|
47
|
-
|
|
48
|
-
/** The registers that each instruction changes, other than by a load of an immediate or a transfer. */
|
|
49
|
-
const WRITES_A = new Set(["lda", "adc", "sbc", "and", "ora", "eor", "pla", "lax", "anc", "asr", "arr", "ane", "lxa", "las", "slo", "rla", "sre", "rra", "isc"]);
|
|
50
|
-
const WRITES_X = new Set(["ldx", "inx", "dex", "tsx", "lax", "sbx", "lxa", "las"]);
|
|
51
|
-
const WRITES_Y = new Set(["ldy", "iny", "dey"]);
|
|
52
|
-
/** The registers that a JSR gets an address in, low byte first, as the KERNAL and BASIC routines get a pointer. */
|
|
53
|
-
const CALL_PAIRS = [
|
|
54
|
-
["x", "y"],
|
|
55
|
-
["a", "y"],
|
|
56
|
-
["a", "x"],
|
|
57
|
-
] as const;
|
|
58
|
-
|
|
59
|
-
export interface DropInput {
|
|
60
|
-
segments: readonly AddressRange[];
|
|
61
|
-
/** 64 KiB with the baked image at its addresses. */
|
|
62
|
-
memory: Uint8Array;
|
|
63
|
-
entry?: number;
|
|
64
|
-
/** The decoded instructions of the baked image: start and length. */
|
|
65
|
-
instructions: ReadonlyMap<number, number>;
|
|
66
|
-
coverage?: Uint8Array;
|
|
67
|
-
regions: ReadonlyArray<{ start: number; end: number; type: RegionType }>;
|
|
68
|
-
/** A reference of the user or the LLM resolves the flow of its instruction. */
|
|
69
|
-
references: ReadonlyArray<{ from: number; to: number; kind: string; origin?: string }>;
|
|
70
|
-
/** The addresses that the operand expression of an instruction names, by the address of each instruction with an operand expression; none for a constant. */
|
|
71
|
-
operandTargets: ReadonlyMap<number, readonly number[]>;
|
|
72
|
-
/** The ranges where the CPU sees ROM and not the RAM of the image: code does not go there, and no address points there. */
|
|
73
|
-
rom?: readonly AddressRange[];
|
|
31
|
+
export interface DropInput extends CodeReachInput {
|
|
74
32
|
/** The BASIC line that starts the program: RUN reads it, which no coverage from the entry shows. */
|
|
75
33
|
basic?: AddressRange;
|
|
76
|
-
/** The patch ranges whose bytes the export changed. */
|
|
77
|
-
changed: readonly AddressRange[];
|
|
78
|
-
keep: readonly AddressRange[];
|
|
79
|
-
drop: readonly AddressRange[];
|
|
80
34
|
routines: ReadonlyArray<{ address: number; name: string }>;
|
|
81
35
|
}
|
|
82
36
|
|
|
@@ -97,272 +51,71 @@ export interface DropPlan {
|
|
|
97
51
|
unresolved: UnresolvedFlow[];
|
|
98
52
|
}
|
|
99
53
|
|
|
100
|
-
const inside = (address: number, ranges: readonly AddressRange[]) => ranges.some((range) => address >= range.start && address <= range.end);
|
|
101
|
-
const overlaps = (a: AddressRange, b: AddressRange) => a.start <= b.end && b.start <= a.end;
|
|
102
54
|
const within = (a: AddressRange, b: AddressRange) => a.start >= b.start && a.end <= b.end;
|
|
103
|
-
const shown = (range: AddressRange) => `${formatC64Address(range.start)}-${formatC64Address(range.end)}`;
|
|
104
|
-
|
|
105
|
-
/** The blocks of the instructions: a block ends after an instruction that does not run on, and before a gap. An instruction inside the one before stays in its block. */
|
|
106
|
-
export function blocksOf(instructions: ReadonlyMap<number, number>, memory: Uint8Array): AddressRange[] {
|
|
107
|
-
const blocks: AddressRange[] = [];
|
|
108
|
-
let open = false;
|
|
109
|
-
for (const start of [...instructions.keys()].sort((a, b) => a - b)) {
|
|
110
|
-
const end = Math.min(0xffff, start + instructions.get(start)! - 1);
|
|
111
|
-
const last = blocks.at(-1);
|
|
112
|
-
if (last !== undefined && (start <= last.end || (open && start === last.end + 1))) last.end = Math.max(last.end, end);
|
|
113
|
-
else blocks.push({ start, end });
|
|
114
|
-
open = !NO_FALL_THROUGH.has(memory[start]!);
|
|
115
|
-
}
|
|
116
|
-
return blocks;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Joins ranges in address order that touch or overlap. */
|
|
120
|
-
function merged(ranges: readonly AddressRange[]): AddressRange[] {
|
|
121
|
-
const result: AddressRange[] = [];
|
|
122
|
-
for (const range of [...ranges].sort((a, b) => a.start - b.start)) {
|
|
123
|
-
const last = result.at(-1);
|
|
124
|
-
if (last !== undefined && range.start <= last.end + 1) last.end = Math.max(last.end, range.end);
|
|
125
|
-
else result.push({ ...range });
|
|
126
|
-
}
|
|
127
|
-
return result;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** A place where code goes or reads: the instruction (or table) at `from`, and what it names. */
|
|
131
|
-
interface Flow {
|
|
132
|
-
from: number;
|
|
133
|
-
to: number;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
interface DataReference {
|
|
137
|
-
from: number;
|
|
138
|
-
start: number;
|
|
139
|
-
end: number;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* The code flows and the data references of the instructions, the knowledge
|
|
144
|
-
* references and the operand expressions. `indexedEnd` gives the last address
|
|
145
|
-
* that an indexed operand can read.
|
|
146
|
-
*/
|
|
147
|
-
function flowsOf(input: DropInput, inImage: (address: number) => boolean, indexedEnd: (operand: number) => number) {
|
|
148
|
-
const flows: Flow[] = [];
|
|
149
|
-
const reads: DataReference[] = [];
|
|
150
|
-
const { memory } = input;
|
|
151
|
-
const word = (at: number) => memory[at]! | (memory[(at + 1) & 0xffff]! << 8);
|
|
152
|
-
const rom = (address: number) => inside(address, input.rom ?? []);
|
|
153
|
-
// The address of a vector, a table or an RTS target in two immediates of one block: the low byte and the high byte
|
|
154
|
-
// go into a pointer (two stores to N and N+1), into the registers of a JSR, or onto the stack (PHA high, PHA low).
|
|
155
|
-
type Immediate = { at: number; value: number } | undefined;
|
|
156
|
-
const registers: { a: Immediate; x: Immediate; y: Immediate } = { a: undefined, x: undefined, y: undefined };
|
|
157
|
-
const stores = new Map<number, NonNullable<Immediate>>();
|
|
158
|
-
const pushes: Immediate[] = [];
|
|
159
|
-
const pair = (low: Immediate, high: Immediate) => {
|
|
160
|
-
if (low === undefined || high === undefined) return;
|
|
161
|
-
const target = low.value | (high.value << 8);
|
|
162
|
-
if (!inImage(target) || rom(target)) return;
|
|
163
|
-
flows.push({ from: low.at, to: target }, { from: low.at, to: (target + 1) & 0xffff });
|
|
164
|
-
reads.push({ from: low.at, start: target, end: target });
|
|
165
|
-
};
|
|
166
|
-
const endBlock = () => {
|
|
167
|
-
registers.a = registers.x = registers.y = undefined;
|
|
168
|
-
stores.clear();
|
|
169
|
-
pushes.length = 0;
|
|
170
|
-
};
|
|
171
|
-
let previousEnd = -2;
|
|
172
|
-
for (const at of [...input.instructions.keys()].sort((a, b) => a - b)) {
|
|
173
|
-
const opcode = memory[at]!;
|
|
174
|
-
const { mnemonic, mode } = OPCODES[opcode]!;
|
|
175
|
-
if (at > previousEnd + 1) endBlock();
|
|
176
|
-
previousEnd = at + input.instructions.get(at)! - 1;
|
|
177
|
-
const operand = input.instructions.get(at) === 3 ? word(at + 1) : memory[at + 1]!;
|
|
178
|
-
// The registers: a load of an immediate that no operand expression explains, a transfer, a store, a push, a call.
|
|
179
|
-
const loaded = mode === "imm" && !input.operandTargets.has(at) ? { at, value: operand } : undefined;
|
|
180
|
-
if (WRITES_A.has(mnemonic) || (mode === "imp" && ["asl", "lsr", "rol", "ror"].includes(mnemonic))) registers.a = mnemonic === "lda" ? loaded : undefined;
|
|
181
|
-
if (WRITES_X.has(mnemonic)) registers.x = mnemonic === "ldx" ? loaded : undefined;
|
|
182
|
-
if (WRITES_Y.has(mnemonic)) registers.y = mnemonic === "ldy" ? loaded : undefined;
|
|
183
|
-
if (mnemonic === "tax") registers.x = registers.a;
|
|
184
|
-
if (mnemonic === "tay") registers.y = registers.a;
|
|
185
|
-
if (mnemonic === "txa") registers.a = registers.x;
|
|
186
|
-
if (mnemonic === "tya") registers.a = registers.y;
|
|
187
|
-
const stored = { sta: registers.a, stx: registers.x, sty: registers.y }[mnemonic];
|
|
188
|
-
if ((mode === "zp" || mode === "abs") && ["sta", "stx", "sty"].includes(mnemonic) && !inside(operand, [IO_AREA])) {
|
|
189
|
-
stores.delete(operand);
|
|
190
|
-
if (stored !== undefined) {
|
|
191
|
-
stores.set(operand, stored);
|
|
192
|
-
pair(stored, stores.get((operand + 1) & 0xffff));
|
|
193
|
-
pair(stores.get((operand - 1) & 0xffff), stored);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (mnemonic === "pha") {
|
|
197
|
-
pushes.push(registers.a);
|
|
198
|
-
if (pushes.length >= 2) pair(pushes.at(-1), pushes.at(-2));
|
|
199
|
-
}
|
|
200
|
-
if (mnemonic === "pla") pushes.pop();
|
|
201
|
-
if (mnemonic === "jsr") {
|
|
202
|
-
for (const [low, high] of CALL_PAIRS) pair(registers[low], registers[high]);
|
|
203
|
-
registers.a = registers.x = registers.y = undefined;
|
|
204
|
-
}
|
|
205
|
-
switch (mode) {
|
|
206
|
-
case "rel":
|
|
207
|
-
flows.push({ from: at, to: (at + 2 + (operand < 0x80 ? operand : operand - 0x100)) & 0xffff });
|
|
208
|
-
break;
|
|
209
|
-
case "ind":
|
|
210
|
-
reads.push({ from: at, start: operand, end: (operand + 1) & 0xffff });
|
|
211
|
-
if (inImage(operand) && inImage((operand + 1) & 0xffff)) flows.push({ from: at, to: word(operand) });
|
|
212
|
-
break;
|
|
213
|
-
case "abs":
|
|
214
|
-
case "zp":
|
|
215
|
-
if (opcode === 0x20 || opcode === 0x4c) {
|
|
216
|
-
if (!rom(operand)) flows.push({ from: at, to: operand });
|
|
217
|
-
} else reads.push({ from: at, start: operand, end: operand });
|
|
218
|
-
break;
|
|
219
|
-
case "abx":
|
|
220
|
-
case "aby":
|
|
221
|
-
case "zpx":
|
|
222
|
-
case "zpy":
|
|
223
|
-
reads.push({ from: at, start: operand, end: indexedEnd(operand) });
|
|
224
|
-
break;
|
|
225
|
-
case "izx":
|
|
226
|
-
case "izy":
|
|
227
|
-
reads.push({ from: at, start: operand, end: (operand + 1) & 0xff });
|
|
228
|
-
break;
|
|
229
|
-
default:
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
if (NO_FALL_THROUGH.has(opcode)) endBlock();
|
|
233
|
-
}
|
|
234
|
-
for (const reference of input.references) {
|
|
235
|
-
if (inside(reference.from, input.changed)) continue;
|
|
236
|
-
if (reference.kind === "call" || reference.kind === "jump" || reference.kind === "pointer") flows.push({ from: reference.from, to: reference.to });
|
|
237
|
-
reads.push({ from: reference.from, start: reference.to, end: reference.to });
|
|
238
|
-
}
|
|
239
|
-
for (const [at, targets] of input.operandTargets) {
|
|
240
|
-
for (const target of targets) {
|
|
241
|
-
flows.push({ from: at, to: target });
|
|
242
|
-
reads.push({ from: at, start: target, end: target });
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
return { flows, reads };
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/** The entries of a table of addresses or of jumps. */
|
|
249
|
-
function tableTargets(table: { start: number; end: number; type: RegionType }, memory: Uint8Array): number[] {
|
|
250
|
-
if (table.type === "jumps") return Array.from({ length: Math.floor((table.end - table.start + 1) / 3) }, (_, index) => table.start + 3 * index);
|
|
251
|
-
return pointerTableEntries(table as { start: number; end: number; type: PointerTableType }, (address) => memory[address]!).map((entry) => entry.target);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const isTable = (type: RegionType) => type === "jumps" || (POINTER_TABLE_TYPES as readonly string[]).includes(type);
|
|
255
55
|
|
|
256
56
|
/**
|
|
257
57
|
* The free ranges of the baked image, the code that a run executed and the
|
|
258
58
|
* entry does not reach, and the refusals of --drop and --keep.
|
|
259
59
|
*/
|
|
260
60
|
export function planDrops(input: DropInput): DropPlan {
|
|
261
|
-
const inImage = (address: number) =>
|
|
61
|
+
const inImage = (address: number) => inRanges(input.segments, address);
|
|
262
62
|
const touched = (address: number) => input.coverage !== undefined && (input.coverage[address]! & RAM_ACCESS) !== 0;
|
|
263
|
-
const
|
|
264
|
-
const
|
|
265
|
-
// The source writes a data region as data, also where the decompiler decoded instructions in it: those are no code.
|
|
266
|
-
const instructions = new Map([...input.instructions].filter(([start, length]) => !regions.some((region) => !CODE_TYPES.includes(region.type) && start <= region.end && region.start <= start + length - 1)));
|
|
267
|
-
const blocks = blocksOf(instructions, input.memory);
|
|
268
|
-
// An indexed operand reads in the region that holds it, and in the next region when it is the last byte (table-1,x).
|
|
269
|
-
// Without a region, it can read the 256 bytes from the operand.
|
|
270
|
-
const holding = (address: number) => regions.find((region) => address >= region.start && address <= region.end);
|
|
271
|
-
const indexedEnd = (operand: number) => {
|
|
272
|
-
const holder = holding(operand);
|
|
273
|
-
const next = holder === undefined || holder.end === operand ? holding((operand + 1) & 0xffff) : undefined;
|
|
274
|
-
return next?.end ?? holder?.end ?? Math.min(0xffff, operand + 0xff);
|
|
275
|
-
};
|
|
276
|
-
const { flows, reads } = flowsOf({ ...input, instructions }, inImage, indexedEnd);
|
|
63
|
+
const reach = codeReach(input);
|
|
64
|
+
const { regions, blocks, flows, reads, targetsOf } = reach;
|
|
277
65
|
|
|
278
66
|
for (const range of [...input.keep, ...input.drop]) {
|
|
279
67
|
for (let address = range.start; address <= range.end; address++) {
|
|
280
|
-
if (!inImage(address)) throw new Refusal("invalid-input", `${
|
|
68
|
+
if (!inImage(address)) throw new Refusal("invalid-input", `${formatC64Range(range)} is not in the program: ${formatC64Address(address)} is in no segment.`);
|
|
281
69
|
}
|
|
282
70
|
}
|
|
283
71
|
for (const range of input.drop) {
|
|
284
|
-
const kept = input.keep.find((other) =>
|
|
285
|
-
if (kept !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
286
|
-
const changed = input.changed.find((other) =>
|
|
287
|
-
if (changed !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
288
|
-
const cutBlock = blocks.find((block) =>
|
|
289
|
-
if (cutBlock !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
290
|
-
const cutData = regions.find((region) => !
|
|
291
|
-
if (cutData !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
72
|
+
const kept = input.keep.find((other) => rangesOverlap(range, other));
|
|
73
|
+
if (kept !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)} overlaps --keep ${formatC64Range(kept)}.`);
|
|
74
|
+
const changed = input.changed.find((other) => rangesOverlap(range, other));
|
|
75
|
+
if (changed !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)} overlaps the patch ${formatC64Range(changed)}, which the export changed.`);
|
|
76
|
+
const cutBlock = blocks.find((block) => rangesOverlap(block, range) && !within(block, range));
|
|
77
|
+
if (cutBlock !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)} cuts the code ${formatC64Range(cutBlock)}. Give a range that holds all of it, or none of it.`);
|
|
78
|
+
const cutData = regions.find((region) => !INSTRUCTION_REGION_TYPES.includes(region.type) && rangesOverlap(region, range) && !within(region, range));
|
|
79
|
+
if (cutData !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)} cuts the ${cutData.type} region ${formatC64Range(cutData)}. Give a range that holds all of it, or none of it.`);
|
|
292
80
|
}
|
|
293
|
-
|
|
294
|
-
// Tables that a run read or that the knowledge names: their entries can be the only way to code, so they are roots.
|
|
295
|
-
const tables = regions.filter((region) => isTable(region.type));
|
|
296
|
-
const tableUsers = (table: AddressRange) => reads.filter((read) => read.start <= table.end && table.start <= read.end).map((read) => read.from);
|
|
297
|
-
const tableRoots: Root[] = [];
|
|
298
|
-
for (const table of tables) {
|
|
299
|
-
const targets = tableTargets(table, input.memory);
|
|
300
|
-
let read = false;
|
|
301
|
-
for (let address = table.start; address <= table.end && !read; address++) read = touched(address);
|
|
302
|
-
if (read || input.references.some((reference) => reference.to >= table.start && reference.to <= table.end)) for (const target of targets) tableRoots.push({ address: target, from: "table entry" });
|
|
303
|
-
for (const from of tableUsers(table)) for (const target of targets) flows.push({ from, to: target });
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const asRegions = (list: readonly AddressRange[]) => list.map((block) => ({ ...block, type: "code" }));
|
|
307
|
-
const references = flows.map((flow) => ({ ...flow, kind: "jump" }));
|
|
308
|
-
const passA = separate({ segments: [...input.segments], memory: input.memory, ...(input.entry === undefined ? {} : { entry: input.entry }), codeRegions: asRegions(blocks), references, roots: tableRoots });
|
|
309
|
-
const rooted = passA.codeRegions.every((region) => region.reachable !== undefined);
|
|
310
|
-
const reachable = (index: number) => !rooted || passA.codeRegions[index]!.reachable === true;
|
|
311
81
|
for (const range of input.drop) {
|
|
312
|
-
const live = blocks.findIndex((block, index) => within(block, range) && reachable
|
|
313
|
-
if (live >= 0) throw new Refusal("invalid-input", `--drop ${
|
|
314
|
-
}
|
|
315
|
-
const ran = (block: AddressRange) => {
|
|
316
|
-
for (let address = block.start; address <= block.end; address++) if (executed(address)) return true;
|
|
317
|
-
return false;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
// Pass B: what stays. Blocks in a --drop range are gone.
|
|
321
|
-
const candidates = blocks.filter((block) => !input.drop.some((range) => within(block, range)));
|
|
322
|
-
const extra: Root[] = [...tableRoots];
|
|
323
|
-
for (const block of candidates) {
|
|
324
|
-
if (ran(block)) extra.push({ address: block.start, from: "executed code" });
|
|
325
|
-
else if ([...input.keep, ...input.changed].some((range) => overlaps(block, range))) extra.push({ address: block.start, from: "kept range" });
|
|
82
|
+
const live = blocks.findIndex((block, index) => within(block, range) && reach.reachable[index]);
|
|
83
|
+
if (live >= 0) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)} holds the code ${formatC64Range(blocks[live]!)}, which the entry or a vector reaches. Only code that nothing reaches can be dropped.`);
|
|
326
84
|
}
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
const
|
|
330
|
-
// A flow that nobody resolved can go to any of that code: it stays, as unknown.
|
|
331
|
-
const resolvedSites = new Set(input.references.filter((reference) => isSemantic(reference.origin)).map((reference) => reference.from));
|
|
332
|
-
const unresolved = unreachableCode.length === 0 ? [] : unresolvedFlows({ starts: [...instructions.keys()].sort((a, b) => a - b), memory: input.memory, inImage, coverage: input.coverage, resolvedSites });
|
|
333
|
-
const droppedCode = unresolved.length === 0 ? unreachableCode : [];
|
|
334
|
-
const unknown = unresolved.length === 0 ? [] : merged(unreachableCode);
|
|
335
|
-
const stays = candidates.filter((block) => !droppedCode.includes(block));
|
|
336
|
-
const inStaying = (address: number) => stays.some((block) => address >= block.start && address <= block.end);
|
|
85
|
+
const staying = new Uint8Array(0x10000);
|
|
86
|
+
for (const block of reach.stays) staying.fill(1, block.start, block.end + 1);
|
|
87
|
+
const inStaying = (address: number) => staying[address] === 1;
|
|
337
88
|
for (const range of input.drop) {
|
|
338
|
-
const into = flows.find((flow) => inStaying(flow.from) &&
|
|
339
|
-
if (into !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
340
|
-
const read = reads.find((item) => inStaying(item.from) &&
|
|
341
|
-
if (read !== undefined) throw new Refusal("invalid-input", `--drop ${
|
|
89
|
+
const into = flows.find((flow) => inStaying(flow.from) && inRanges([range], flow.to));
|
|
90
|
+
if (into !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)}: the code at ${formatC64Address(into.from)} stays and goes to ${formatC64Address(into.to)}.`);
|
|
91
|
+
const read = reads.find((item) => inStaying(item.from) && rangesOverlap(item, range));
|
|
92
|
+
if (read !== undefined) throw new Refusal("invalid-input", `--drop ${formatC64Range(range)}: the code at ${formatC64Address(read.from)} stays and reads ${formatC64Range({ start: Math.max(read.start, range.start), end: Math.min(read.end, range.end) })}.`);
|
|
342
93
|
}
|
|
343
94
|
|
|
344
95
|
// Data: a region of a droppable type that no run touched and that nothing that stays names.
|
|
345
|
-
const
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
96
|
+
const tables = [...targetsOf.keys()];
|
|
97
|
+
const gone = [...input.drop, ...reach.unused];
|
|
98
|
+
const units =
|
|
99
|
+
input.coverage !== undefined && reach.rooted
|
|
100
|
+
? regions.filter((region) => {
|
|
101
|
+
if (!DROPPABLE_DATA.includes(region.type) || gone.some((range) => rangesOverlap(range, region))) return false;
|
|
102
|
+
if (regions.some((other) => VIC_DATA.includes(other.type) && rangesOverlap(other, region))) return false;
|
|
103
|
+
if ([...input.keep, ...input.changed, VECTORS, ...(input.basic === undefined ? [] : [input.basic])].some((range) => rangesOverlap(range, region))) return false;
|
|
104
|
+
for (let address = region.start; address <= region.end; address++) if (touched(address)) return false;
|
|
105
|
+
return true;
|
|
106
|
+
})
|
|
107
|
+
: [];
|
|
355
108
|
const dropped = new Set(units.map((_, index) => index));
|
|
356
109
|
const unitAt = (address: number) => units.findIndex((unit) => address >= unit.start && address <= unit.end);
|
|
357
110
|
// A name from code that stays, or from a unit that stays, keeps the unit. Kept tables keep their entries.
|
|
358
111
|
for (let grown = true; grown; ) {
|
|
359
112
|
grown = false;
|
|
360
|
-
const keeps = (from: number) => inStaying(from) || (!
|
|
113
|
+
const keeps = (from: number) => inStaying(from) || (!inRanges(gone, from) && (unitAt(from) < 0 || !dropped.has(unitAt(from))));
|
|
361
114
|
for (const [index, unit] of units.entries()) {
|
|
362
115
|
if (!dropped.has(index)) continue;
|
|
363
116
|
const named = reads.some((read) => read.start <= unit.end && unit.start <= read.end && keeps(read.from));
|
|
364
|
-
const entry = units.some((table, other) => !dropped.has(other) && isTable(table.type) &&
|
|
365
|
-
const fromTable = tables.some((table) => unitAt(table.start) < 0 &&
|
|
117
|
+
const entry = units.some((table, other) => !dropped.has(other) && isTable(table.type) && targetsOf.get(table)!.some((target) => target >= unit.start && target <= unit.end));
|
|
118
|
+
const fromTable = tables.some((table) => unitAt(table.start) < 0 && targetsOf.get(table)!.some((target) => target >= unit.start && target <= unit.end));
|
|
366
119
|
if (named || entry || fromTable) {
|
|
367
120
|
dropped.delete(index);
|
|
368
121
|
grown = true;
|
|
@@ -373,7 +126,7 @@ export function planDrops(input: DropInput): DropPlan {
|
|
|
373
126
|
|
|
374
127
|
const pieces = [
|
|
375
128
|
...input.drop.map((range) => ({ ...range, code: blocks.some((block) => within(block, range)), data: !blocks.some((block) => within(block, range)) || regions.some((region) => within(region, range) && !isTable(region.type) && region.type !== "code") })),
|
|
376
|
-
...
|
|
129
|
+
...reach.unused.map((range) => ({ ...range, code: true, data: false })),
|
|
377
130
|
...droppedData.map((range) => ({ ...range, code: false, data: true })),
|
|
378
131
|
].sort((a, b) => a.start - b.start);
|
|
379
132
|
const free: FreeRange[] = [];
|
|
@@ -387,15 +140,15 @@ export function planDrops(input: DropInput): DropPlan {
|
|
|
387
140
|
}
|
|
388
141
|
|
|
389
142
|
const routines = [...input.routines].sort((a, b) => a.address - b.address);
|
|
390
|
-
const unreachableExecuted =
|
|
143
|
+
const unreachableExecuted = mergeRanges(blocks.filter((block, index) => !reach.reachable[index] && reach.executed[index] && !input.drop.some((range) => within(block, range)))).map((range) => {
|
|
391
144
|
const routine = routines.filter((item) => item.address <= range.start).at(-1)?.name;
|
|
392
145
|
return { ...range, ...(routine === undefined ? {} : { routine }) };
|
|
393
146
|
});
|
|
394
|
-
return { free, unreachableExecuted, rooted, unknown, unresolved };
|
|
147
|
+
return { free, unreachableExecuted, rooted: reach.rooted, unknown: reach.unknown, unresolved: reach.unresolved };
|
|
395
148
|
}
|
|
396
149
|
|
|
397
150
|
/** The label of a free range. */
|
|
398
|
-
export const freeName = (range: AddressRange) => `free_${range.start
|
|
151
|
+
export const freeName = (range: AddressRange) => `free_${hexWord(range.start)}`;
|
|
399
152
|
|
|
400
153
|
/** What the export does with one test file of the project. */
|
|
401
154
|
export type TestRewrite = { kind: "scenario" | "run"; text: string } | { reason: string };
|
|
@@ -441,9 +194,9 @@ export function rewriteTest(text: string, name: string, symbols: ReadonlyMap<str
|
|
|
441
194
|
if (!scenario && !run) return { reason: "it is not a scenario or a run file" };
|
|
442
195
|
if (hasSnapshot(test)) return { reason: "it restores a snapshot, which is a state of the old program" };
|
|
443
196
|
for (const location of locationsOf(test.steps)) {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
if (
|
|
197
|
+
const address = c64AddressOf(location);
|
|
198
|
+
if (address !== undefined) {
|
|
199
|
+
if (inRanges(free, address)) return { reason: `it names ${location}, which is in a free range` };
|
|
447
200
|
} else if (!symbols.has(location)) return { reason: `it names ${location}, which the exported program does not have` };
|
|
448
201
|
}
|
|
449
202
|
const side = (program: string) => ({ program: `${program}.prg`, symbols: symbolsPath(`${program}.prg`) });
|
|
@@ -511,7 +264,7 @@ export function readmeText(input: ReadmeInput): string {
|
|
|
511
264
|
if (input.free.length === 0) lines.push("None: the export dropped nothing.");
|
|
512
265
|
else {
|
|
513
266
|
lines.push("The export dropped code and data that nothing uses. Each range is `$00` in the source, with a label `free_xxxx`. Use the space for new code.", "", "| Range | Bytes | Was |", "|---|---|---|");
|
|
514
|
-
for (const range of input.free) lines.push(`| \`${
|
|
267
|
+
for (const range of input.free) lines.push(`| \`${formatC64Range(range)}\` | ${range.end - range.start + 1} | ${range.what} |`);
|
|
515
268
|
}
|
|
516
269
|
lines.push(
|
|
517
270
|
"",
|