@henols/c64-re-tools 2.6.0-rc.4 → 2.6.0-rc.6

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.
@@ -56,8 +56,12 @@ export { OPERAND_SELECTS, type OperandSelect } from "./expression.ts";
56
56
  export const SUPPRESSION_ENTITIES = ["symbol", "region", "reference"] as const;
57
57
  export type SuppressionEntity = (typeof SUPPRESSION_ENTITIES)[number];
58
58
 
59
- /** A module file of the generated source: a plain name that ends in .asm, no directory. */
60
- export const MODULE_FILE = /^[A-Za-z0-9_][A-Za-z0-9_-]{0,59}\.asm$/;
59
+ /**
60
+ * A module file of the generated source: a name that ends in .asm, in the
61
+ * source folder or in one folder of it, for example music/driver.asm. A folder
62
+ * name is lower case, so two modules cannot differ only in its letter case.
63
+ */
64
+ export const MODULE_FILE = /^(?:[a-z0-9_][a-z0-9_-]{0,29}\/)?[A-Za-z0-9_][A-Za-z0-9_-]{0,59}\.asm$/;
61
65
  /** A switch of the generated source: upper case, so it reads as a build option and not as a label. */
62
66
  export const SWITCH_NAME = /^[A-Z][A-Z0-9_]{1,31}$/;
63
67
  /** A release label: the switch-name form. The build of a release defines RELEASE_<label>. */
@@ -100,12 +104,22 @@ export const MEMORY_ROW = 64;
100
104
  /** Files that c64-source writes itself: no module can have these names. */
101
105
  export const GENERATED_FILES = ["main.asm", "loader.asm", "switches.asm", "config.asm", "constants.asm", "macros.asm", "start_screen.asm"] as const;
102
106
 
103
- /** The file of a range of leftovers in the generated source: leftover_xxxx.asm. */
104
- export const LEFTOVER_FILE = /^leftover_[0-9a-f]{4}\.asm$/;
107
+ /** The folder of the leftovers in the generated source: leftovers/leftover_xxxx.asm. */
108
+ export const LEFTOVER_FOLDER = "leftovers";
109
+ /** The folder of the parts of segments that no module holds: segments/segment_xxxx.asm. */
110
+ export const SEGMENT_FOLDER = "segments";
111
+ /** The folders that c64-source writes itself: no module file is in them. */
112
+ export const GENERATED_FOLDERS = [LEFTOVER_FOLDER, SEGMENT_FOLDER] as const;
105
113
 
106
- /** True for a generated file name in any letter case: the file systems of macOS and Windows ignore case. */
114
+ /**
115
+ * True for a generated file name in any letter case (the file systems of macOS
116
+ * and Windows ignore case), also in a folder: a folder with a generated
117
+ * main.asm is the source of another space, not a folder of modules.
118
+ */
107
119
  export function isGeneratedFile(file: string): boolean {
108
- return (GENERATED_FILES as readonly string[]).includes(file.toLowerCase()) || LEFTOVER_FILE.test(file.toLowerCase());
120
+ const lower = file.toLowerCase();
121
+ const name = lower.slice(lower.lastIndexOf("/") + 1);
122
+ return (GENERATED_FILES as readonly string[]).includes(name) || GENERATED_FOLDERS.some((folder) => lower.startsWith(`${folder}/`));
109
123
  }
110
124
 
111
125
  /**
@@ -287,8 +301,6 @@ function createSchema(db: DatabaseSync): void {
287
301
  id TEXT PRIMARY KEY,
288
302
  space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
289
303
  label TEXT NOT NULL CHECK (length(label) > 0),
290
- file TEXT NOT NULL CHECK (length(file) > 0),
291
- sha256 TEXT NOT NULL CHECK (length(sha256) = 64),
292
304
  base INTEGER NOT NULL CHECK (base IN (0, 1)),
293
305
  comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
294
306
  origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
@@ -356,6 +368,17 @@ function createSchema(db: DatabaseSync): void {
356
368
  valid_to_revision TEXT REFERENCES revisions (id)
357
369
  );
358
370
  CREATE INDEX memory_current ON memory (space, address) WHERE valid_to_revision IS NULL;
371
+ CREATE TABLE release_memory (
372
+ id TEXT PRIMARY KEY,
373
+ space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
374
+ label TEXT NOT NULL CHECK (length(label) > 0),
375
+ address INTEGER NOT NULL CHECK (address BETWEEN 0 AND 65535),
376
+ bytes TEXT NOT NULL CHECK (length(bytes) BETWEEN 2 AND ${MEMORY_ROW * 2} AND length(bytes) % 2 = 0 AND address + length(bytes) / 2 <= 65536),
377
+ origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
378
+ valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
379
+ valid_to_revision TEXT REFERENCES revisions (id)
380
+ );
381
+ CREATE INDEX release_memory_current ON release_memory (space, label, address) WHERE valid_to_revision IS NULL;
359
382
  CREATE TABLE entries (
360
383
  id TEXT PRIMARY KEY,
361
384
  space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
@@ -379,6 +402,7 @@ function createSchema(db: DatabaseSync): void {
379
402
  );
380
403
  CREATE INDEX coverage_current ON coverage (space, kind) WHERE valid_to_revision IS NULL;
381
404
  CREATE VIEW current_memory AS SELECT * FROM memory WHERE valid_to_revision IS NULL;
405
+ CREATE VIEW current_release_memory AS SELECT * FROM release_memory WHERE valid_to_revision IS NULL;
382
406
  CREATE VIEW current_entries AS SELECT * FROM entries WHERE valid_to_revision IS NULL;
383
407
  CREATE VIEW current_coverage AS SELECT * FROM coverage WHERE valid_to_revision IS NULL;
384
408
  CREATE VIEW current_symbols AS SELECT * FROM symbols WHERE valid_to_revision IS NULL;
@@ -399,7 +423,7 @@ function createSchema(db: DatabaseSync): void {
399
423
  }
400
424
 
401
425
  /** The version of the index this build makes. An index with another version is emptied and made again. */
402
- export const INDEX_VERSION = 13;
426
+ export const INDEX_VERSION = 14;
403
427
 
404
428
  function indexVersion(db: DatabaseSync): number | undefined {
405
429
  const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
@@ -166,12 +166,12 @@ export const TABLES: readonly TableSpec[] = [
166
166
  origin,
167
167
  ],
168
168
  },
169
- // The releases of the program: the file of each, and the one whose bytes are the program.
169
+ // The releases of the program, and the one whose bytes are the program. The bytes of the other releases are in release_memory.jsonl.
170
170
  {
171
171
  table: "releases",
172
172
  file: "releases.jsonl",
173
173
  sort: "label",
174
- fields: [space, field("label", "label", "text"), field("file", "file", "text"), field("sha256", "sha256", "text"), field("base", "base", "integer"), field("comment", "comment", "text", true), origin],
174
+ fields: [space, field("label", "label", "text"), field("base", "base", "integer"), field("comment", "comment", "text", true), origin],
175
175
  },
176
176
  {
177
177
  table: "macros",
@@ -209,6 +209,8 @@ export const TABLES: readonly TableSpec[] = [
209
209
  },
210
210
  // The program itself: its bytes, at most 64 to a row, so a changed byte changes one line.
211
211
  { table: "memory", file: "memory.jsonl", sort: "address", fields: [space, field("at", "address", "address"), field("bytes", "bytes", "hex"), origin] },
212
+ // The bytes of each release other than the base release, as the bytes of the program.
213
+ { table: "release_memory", file: "release_memory.jsonl", sort: "address", fields: [space, field("label", "label", "text"), field("at", "address", "address"), field("bytes", "bytes", "hex"), origin] },
212
214
  // Where the program starts, the machine state there, and the stages of its unpacker.
213
215
  {
214
216
  table: "entries",
@@ -78,7 +78,7 @@ import {
78
78
  type SwitchRow,
79
79
  type SymbolRow,
80
80
  } from "./read.ts";
81
- import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, OFF_KINDS, RELEASE_DEFINE, RELEASE_LABEL, LEFTOVERS_SWITCH, START_SCREEN_MODES, START_SCREEN_SWITCH, SWITCH_KINDS, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type StartScreenMode, type SwitchKind, type SymbolKind } from "./schema.ts";
81
+ import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MEMORY_ROW, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, GENERATED_FOLDERS, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, OFF_KINDS, RELEASE_DEFINE, RELEASE_LABEL, LEFTOVERS_SWITCH, START_SCREEN_MODES, START_SCREEN_SWITCH, SWITCH_KINDS, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type StartScreenMode, type SwitchKind, type SymbolKind } from "./schema.ts";
82
82
  import { newRevisionId, rowId, sqlName } from "./text.ts";
83
83
 
84
84
  /** What a change rests on: a coverage run, a test or another project file with its SHA-256, or a note. */
@@ -281,8 +281,15 @@ export class Change {
281
281
 
282
282
  insertRelease(row: Omit<ReleaseRow, "revision">): void {
283
283
  this.#db
284
- .prepare("INSERT INTO releases (id, space, label, file, sha256, base, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
285
- .run(this.#rowId(), row.space, row.label, row.file, row.sha256, row.base ? 1 : 0, row.comment ?? null, row.origin, this.id());
284
+ .prepare("INSERT INTO releases (id, space, label, base, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?)")
285
+ .run(this.#rowId(), row.space, row.label, row.base ? 1 : 0, row.comment ?? null, row.origin, this.id());
286
+ }
287
+
288
+ /** Bytes of a release other than the base release, as a memory row of the program holds them. */
289
+ insertReleaseMemory(row: { space: Space; label: string; address: number; bytes: string; origin: Origin }): void {
290
+ this.#db
291
+ .prepare("INSERT INTO release_memory (id, space, label, address, bytes, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?)")
292
+ .run(this.#rowId(), row.space, row.label, row.address, row.bytes, row.origin, this.id());
286
293
  }
287
294
 
288
295
  insertStartScreen(row: Omit<StartScreenRow, "revision">): void {
@@ -751,9 +758,11 @@ type ModuleRequest = InSpace & { start: number; file: string; title?: string };
751
758
  function checkModule(request: ModuleRequest): void {
752
759
  checkSpace(request);
753
760
  checkAddress(request.start, "start");
754
- if (!MODULE_FILE.test(request.file)) throw new Refusal("invalid-input", "A module file is a name of letters, digits, underscores or hyphens (at most 60) that ends in .asm, without a directory.");
761
+ if (!MODULE_FILE.test(request.file)) {
762
+ throw new Refusal("invalid-input", "A module file is a name of letters, digits, underscores or hyphens (at most 60) that ends in .asm. It can have one folder of lower-case letters, digits, underscores or hyphens (at most 30), for example music/driver.asm.");
763
+ }
755
764
  if (isGeneratedFile(request.file)) {
756
- throw new Refusal("invalid-input", `A module cannot have the file ${request.file}, because the generated source writes ${GENERATED_FILES.join(", ")} itself, in any letter case. Give the module another file.`);
765
+ throw new Refusal("invalid-input", `A module cannot have the file ${request.file}, because the generated source writes ${GENERATED_FILES.join(", ")} in any folder, and the folders ${GENERATED_FOLDERS.map((folder) => `${folder}/`).join(", ")} itself, in any letter case. Give the module another file.`);
757
766
  }
758
767
  if (request.title !== undefined && (request.title.trim().length === 0 || request.title.length > MAX_COMMENT)) throw new Refusal("invalid-input", `A module title has 1 to ${MAX_COMMENT} characters.`);
759
768
  }
@@ -1020,16 +1029,23 @@ function unswitchStep(db: DatabaseSync, change: Change, _origin: Origin, request
1020
1029
  return { current: null };
1021
1030
  }
1022
1031
 
1023
- /** A release: `file` is a project path and `sha256` the hash of its bytes, both from the caller. */
1024
- type ReleaseRequest = InSpace & { label: string; file: string; sha256: string; base?: boolean; comment?: string };
1032
+ /**
1033
+ * A release. `rows` are the bytes of a release other than the base release,
1034
+ * as memoryRows gives them: the address of each row and its bytes as hex. The
1035
+ * base release has no bytes of its own: its bytes are the program.
1036
+ */
1037
+ type ReleaseRequest = InSpace & { label: string; base?: boolean; comment?: string; rows?: ReadonlyMap<number, string> };
1025
1038
 
1026
1039
  function checkRelease(request: ReleaseRequest): void {
1027
1040
  checkSpace(request);
1028
1041
  if (typeof request.label !== "string" || !RELEASE_LABEL.test(request.label)) {
1029
1042
  throw new Refusal("invalid-input", "A release label is an upper-case letter, then 1 to 31 upper-case letters, digits or underscores, for example ORIGINAL or FIRESTAR.");
1030
1043
  }
1031
- if (typeof request.file !== "string" || request.file.trim().length === 0 || request.file.length > MAX_EVIDENCE_REF) throw new Refusal("invalid-input", `The file of a release has 1 to ${MAX_EVIDENCE_REF} characters.`);
1032
- if (!/^[0-9a-f]{64}$/.test(request.sha256)) throw new Refusal("invalid-input", "The sha256 of a release is 64 lowercase hex digits.");
1044
+ for (const [address, bytes] of request.rows ?? []) {
1045
+ if (!Number.isInteger(address) || address < 0 || address > 0xffff || !/^(?:[0-9a-f]{2})+$/.test(bytes) || bytes.length > MEMORY_ROW * 2 || address + bytes.length / 2 > 0x10000) {
1046
+ throw new Refusal("invalid-input", `The bytes of a release at ${formatC64Address(address & 0xffff)} are not a memory row.`);
1047
+ }
1048
+ }
1033
1049
  checkCommentText(request.comment, "A release comment");
1034
1050
  }
1035
1051
 
@@ -1038,7 +1054,12 @@ function releaseStep(db: DatabaseSync, change: Change, origin: Origin, request:
1038
1054
  const existing = releaseLabeled(db, request.label, space);
1039
1055
  const base = request.base ?? existing?.base ?? false;
1040
1056
  const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
1041
- if (existing !== undefined && existing.file === request.file && existing.sha256 === request.sha256 && existing.base === base && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
1057
+ const held = db.prepare("SELECT id, address, bytes FROM current_release_memory WHERE space = ? AND label = ?").all(space, request.label) as Array<{ id: string; address: number; bytes: string }>;
1058
+ // A release without new bytes keeps its bytes. The base release has none: its bytes are the program.
1059
+ const wanted = base ? new Map<number, string>() : (request.rows ?? new Map(held.map((row) => [row.address, row.bytes])));
1060
+ if (!base && wanted.size === 0) throw new Refusal("invalid-input", `The release ${request.label} has no bytes. Give the file of the release.`);
1061
+ const sameBytes = held.length === wanted.size && held.every((row) => wanted.get(row.address) === row.bytes);
1062
+ if (existing !== undefined && existing.base === base && existing.comment === comment && sameBytes && !confirms(existing.origin, origin)) return { current: existing };
1042
1063
  const other = baseLabel(db, space);
1043
1064
  if (base && other !== undefined && other !== request.label) throw new Refusal("conflict", `The release ${other} is the base release. A space has one base release.`);
1044
1065
  // Every switch takes its base value from the base release: it cannot change under them.
@@ -1048,8 +1069,17 @@ function releaseStep(db: DatabaseSync, change: Change, origin: Origin, request:
1048
1069
  }
1049
1070
  const symbol = symbolNamed(db, `${RELEASE_DEFINE}${request.label}`, space);
1050
1071
  if (symbol !== undefined) throw new Refusal("conflict", `The build of the release defines ${RELEASE_DEFINE}${request.label}, and that is the name of the symbol at ${formatC64Address(symbol.address)}. Give the release another label.`);
1051
- if (existing !== undefined) change.close("releases", "space = ? AND label = ?", space, request.label);
1052
- change.insertRelease({ space, label: request.label, file: request.file, sha256: request.sha256, base, ...(comment === undefined ? {} : { comment }), origin: origin as ReleaseRow["origin"] });
1072
+ if (existing === undefined || existing.base !== base || existing.comment !== comment || confirms(existing.origin, origin)) {
1073
+ if (existing !== undefined) change.close("releases", "space = ? AND label = ?", space, request.label);
1074
+ change.insertRelease({ space, label: request.label, base, ...(comment === undefined ? {} : { comment }), origin: origin as ReleaseRow["origin"] });
1075
+ }
1076
+ // Only the rows that changed: a changed byte changes one short line of the text.
1077
+ const kept = new Set<number>();
1078
+ for (const row of held) {
1079
+ if (wanted.get(row.address) === row.bytes) kept.add(row.address);
1080
+ else change.close("release_memory", "id = ?", row.id);
1081
+ }
1082
+ for (const [address, bytes] of [...wanted].sort(([a], [b]) => a - b)) if (!kept.has(address)) change.insertReleaseMemory({ space, label: request.label, address, bytes, origin });
1053
1083
  const current = releaseLabeled(db, request.label, space)!;
1054
1084
  return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
1055
1085
  }
@@ -1064,6 +1094,7 @@ function unreleaseStep(db: DatabaseSync, change: Change, _origin: Origin, reques
1064
1094
  throw new Refusal("conflict", `The release ${request.label} ${why} the switches ${switches.map((row) => row.name).join(", ")}. Change or remove them first (switch, unswitch).`);
1065
1095
  }
1066
1096
  change.close("releases", "space = ? AND label = ?", space, request.label);
1097
+ change.close("release_memory", "space = ? AND label = ?", space, request.label);
1067
1098
  return { current: null };
1068
1099
  }
1069
1100
 
@@ -1415,7 +1446,7 @@ export function removeSwitch(db: DatabaseSync, context: WriteContext, request: I
1415
1446
  return result(write(db, context, "remove-switch", (change) => unswitchStep(db, change, context.origin, request)));
1416
1447
  }
1417
1448
 
1418
- /** Writes a release of the program: its project file and the SHA-256 of it. The caller checks the file. */
1449
+ /** Writes a release of the program and its bytes. The caller reads the bytes from the file of the release, and checks the file of the base release. */
1419
1450
  export function setRelease(db: DatabaseSync, context: WriteContext, request: ReleaseRequest): WriteResult<ReleaseRow, ReleaseRow> {
1420
1451
  checkRelease(request);
1421
1452
  return result(write(db, context, "set-release", (change) => releaseStep(db, change, context.origin, request)));
@@ -1606,6 +1637,7 @@ export function applyBatch(db: DatabaseSync, context: WriteContext, operations:
1606
1637
  /** The tables of the program and the columns that make a row. */
1607
1638
  const PROGRAM_TABLES = [
1608
1639
  ["memory", "space, address, bytes, origin"],
1640
+ ["release_memory", "space, label, address, bytes, origin"],
1609
1641
  ["entries", "space, address, state, stages, origin"],
1610
1642
  ["coverage", "space, kind, start_address, end_address, origin"],
1611
1643
  ] as const;
@@ -1635,7 +1667,7 @@ const KEYS = {
1635
1667
  releases: {
1636
1668
  columns: RELEASE_COLUMNS,
1637
1669
  map: toRelease,
1638
- key: (row: ReleaseRow) => `${row.space}|${row.label}|${row.file}|${row.sha256}|${row.base}|${row.comment ?? ""}|${row.origin}`,
1670
+ key: (row: ReleaseRow) => `${row.space}|${row.label}|${row.base}|${row.comment ?? ""}|${row.origin}`,
1639
1671
  },
1640
1672
  start_screens: {
1641
1673
  columns: START_SCREEN_COLUMNS,
@@ -1736,6 +1768,7 @@ export function revert(db: DatabaseSync, context: WriteContext, request: { revis
1736
1768
  if (nowKeys.has(key(row))) continue;
1737
1769
  const values = row as never;
1738
1770
  if (table === "memory") change.insertMemory(values);
1771
+ else if (table === "release_memory") change.insertReleaseMemory(values);
1739
1772
  else if (table === "entries") change.insertEntry(values);
1740
1773
  else change.insertCoverage({ ...(values as { space: Space; kind: string; origin: Origin }), start: row.start_address as number, end: row.end_address as number });
1741
1774
  }
package/src/project.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  import { Refusal } from "./errors.ts";
8
8
  import { accessSync, constants, existsSync, lstatSync, mkdirSync, readdirSync, realpathSync, statSync, writeFileSync } from "node:fs";
9
9
  import { homedir } from "node:os";
10
- import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
10
+ import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
11
11
 
12
12
  /** The folder that marks a project and holds its knowledge. */
13
13
  export const PROJECT_DIRECTORY = ".c64-re-tools";
@@ -15,6 +15,18 @@ export const PROJECT_DIRECTORY = ".c64-re-tools";
15
15
  /** The folder for machine-local scratch: saved results, batches and step files. */
16
16
  export const LOCAL_DIRECTORY = "local";
17
17
 
18
+ /** The folder in LOCAL_DIRECTORY for the reports of a generated source: its source map and the code that no run executed. */
19
+ export const GENERATED_DIRECTORY = "generated";
20
+
21
+ /**
22
+ * The folder of the reports of the source that was generated into `out`, relative
23
+ * to the project folder: .c64-re-tools/local/generated/<out>. The source folder
24
+ * then holds only source files.
25
+ */
26
+ export function generatedDirectory(out: string): string {
27
+ return posix.join(PROJECT_DIRECTORY, LOCAL_DIRECTORY, GENERATED_DIRECTORY, posix.normalize(out.split("\\").join("/")).replace(/\/+$/, ""));
28
+ }
29
+
18
30
  /** The .gitignore of LOCAL_DIRECTORY: git ignores everything in it, the .gitignore too. */
19
31
  const LOCAL_GITIGNORE = "# Machine-local scratch of c64-re-tools (saved results, batches, step files). Never committed.\n*\n";
20
32