// SPDX-License-Identifier: GPL-3.0-or-later import Foundation struct AssemblyDiagnostic: Hashable, Sendable { let path: String let line: Int let message: String } struct AssemblyBuildFailure: LocalizedError { let message: String let diagnostics: [AssemblyDiagnostic] init(_ message: String, diagnostics: [AssemblyDiagnostic] = []) { self.message = message self.diagnostics = diagnostics } var errorDescription: String? { message } } struct AssemblyInstructionLocation: Hashable, Sendable { let path: String let line: Int let address: UInt16 let bytes: [UInt8] } struct AssemblyBuildArtifact: Sendable { let id: UUID let snapshot: AssemblyProjectSnapshot let binaryData: Data let entry: UInt16 let ranges: [ClosedRange] let listing: String let symbols: String let buildLog: String let instructions: [AssemblyInstructionLocation] } /// Export accepts the full DECB address space. The machine's loader separately /// decides whether a given block and entry are safe in its current RAM mapping. private struct AssemblyImage { let entry: UInt16 let ranges: [ClosedRange] let bytes: [Int: UInt8] init(_ data: Data) throws { var offset = 0, ranges: [ClosedRange] = [], memory: [Int: UInt8] = [:] var entry: UInt16?, ambiguous = false while offset + 5 <= data.count { let type = data[offset], length = Int(data[offset + 1]) * 256 + Int(data[offset + 2]) let start = Int(data[offset + 3]) * 256 + Int(data[offset + 4]) offset += 5 if type == 255 { guard length == 0, offset == data.count else { throw AssemblyBuildFailure("Malformed DECB execution record.") } entry = UInt16(start) break } guard type == 0, length <= data.count - offset else { throw AssemblyBuildFailure("Malformed DECB load block.") } if length > 0 { let end = start + length - 1 ranges.append(UInt16(start)...UInt16(min(end, 65535))) if end > 65535 { ranges.append(0...UInt16(end - 65536)); ambiguous = true } for index in 0.. Data { let properties = try url.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey]) guard properties.isRegularFile == true, properties.isSymbolicLink != true, let size = properties.fileSize, size <= limit else { throw AssemblyBuildFailure("The file is missing, linked, or exceeds its size limit: " + url.lastPathComponent) } let handle = try FileHandle(forReadingFrom: url) defer { try? handle.close() } let bytes = try handle.read(upToCount: limit + 1) ?? Data() guard bytes.count <= limit else { throw AssemblyBuildFailure("File grew beyond its size limit.") } return bytes } func withWorkspace(_ body: @escaping @Sendable (URL, DevelopmentProcessControl) throws -> T) async throws -> T { let control = DevelopmentProcessControl() return try await withTaskCancellationHandler { try await Task.detached(priority: .userInitiated) { try control.checkCancellation() let root = FileManager.default.temporaryDirectory.appendingPathComponent("VCC-Development-" + UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: false, attributes: [.posixPermissions: 0o700]) defer { try? FileManager.default.removeItem(at: root) } let result = try body(root, control) try control.checkCancellation() return result }.value } onCancel: { control.cancel() } } func run(_ name: String, _ arguments: [String], in folder: URL, control: DevelopmentProcessControl, cpu: AssemblyCPU = .mc6809) throws -> (status: Int32, text: String) { let bin = toolchain.appendingPathComponent("bin") let executable = bin.appendingPathComponent(name) guard FileManager.default.isExecutableFile(atPath: executable.path) else { throw AssemblyBuildFailure("The bundled development tool is missing: " + name) } let environment = ["PATH": bin.path + ":/usr/bin:/bin", "LC_ALL": "C", "TMPDIR": folder.path, "LWASM": "lwasm", "LWLINK": "lwlink", "ASFLAGS": "--" + cpu.rawValue, "VCC_TOOLCHAIN": toolchain.path] return try control.run(executable: executable, arguments: arguments, directory: folder, environment: environment, timeout: 120) } /// BASIC already produced a fresh tokenized disk. Stage only that image so a /// failed tool invocation cannot alter a mounted disk or the chosen destination. func bootableBASICDisk(_ disk: Data, name: String) async throws -> Data { guard let name = BASICExportName.normalized(name) else { throw AssemblyBuildFailure("Invalid Disk BASIC program name.") } return try await withWorkspace { root, control in let url = root.appendingPathComponent("export.dsk") try disk.write(to: url) try installBootLoader(.basic(name), in: root, control: control) return try Self.readFile(url, limit: 35 * 18 * 256) } } func installBootLoader(_ program: DevelopmentDiskProgram, in root: URL, control: DevelopmentProcessControl) throws { // Both callers validate the on-disk name before it becomes a C literal. let command = program.bootCommand.replacingOccurrences(of: "\"", with: "\\\"") let source = "#define COMMAND \"\(command)\"\n" + CMOCMakefileSettings.dosBootLoaderSource try Data(source.utf8).write(to: root.appendingPathComponent("loader.c")) let compiled = try run("cmoc", ["--dos", "-O0", "--limit=2800", "-o", "loader.bin", "loader.c"], in: root, control: control) guard compiled.status == 0 else { throw AssemblyBuildFailure("Could not build the DOS bootloader.\n" + compiled.text) } let installed = try run("install-coco-boot-loader", ["export.dsk", "loader.bin"], in: root, control: control) guard installed.status == 0 else { throw AssemblyBuildFailure("Could not install the DOS bootloader.\n" + installed.text) } } } struct AssemblyBuildService: Sendable { let tools: DevelopmentBuildTools init(toolchain: URL = Bundle.main.bundleURL.appendingPathComponent("Contents/Resources/Toolchain")) { tools = DevelopmentBuildTools(toolchain: toolchain) } func build(_ snapshot: AssemblyProjectSnapshot) async throws -> AssemblyBuildArtifact { try await tools.withWorkspace { root, control in try snapshot.validate() let workspace = root.appendingPathComponent("src") let output = root.appendingPathComponent("out") try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) for (path, data) in snapshot.files { try control.checkCancellation() let url = workspace.appendingPathComponent(path) try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) try data.write(to: url, options: .withoutOverwriting) } var log = "", listings: [String] = [], symbols = "" var objects: [String] = [], modules: [(object: String, records: String)] = [] func invoke(_ tool: String, _ args: [String]) throws -> String { let result = try tools.run(tool, args, in: workspace, control: control, cpu: snapshot.cpu) let command = ([tool] + args).joined(separator: " ") if log.utf8.count < 512 * 1024 { log += command + "\n" + result.text + "\n" } guard result.status == 0 else { throw AssemblyBuildFailure(command + "\n" + result.text, diagnostics: Self.diagnostics(result.text, files: snapshot.files)) } return result.text } var binaryURL = output.appendingPathComponent("program.bin") let settings = snapshot.settings if let makefile = settings.makefile, let binary = settings.binary { _ = try invoke("make", ["-B", "-f", "./" + makefile, "MAKE=make", "--", settings.target ?? "all"]) binaryURL = workspace.appendingPathComponent(binary) guard binaryURL.resolvingSymlinksInPath().path == workspace.resolvingSymlinksInPath().appendingPathComponent(binary).path else { throw AssemblyBuildFailure("The Makefile must produce a regular binary within the build folder.") } log += "Custom Make declares the CPU target in its recipe. Its instruction set and source locations are unverified. Use the machine debugger.\n" } else { let linked = settings.linkScript != nil _ = try invoke("lwasm", ["--version"]) for (index, path) in ([snapshot.mainPath] + (settings.modules ?? [])).enumerated() { let stem = "../out/module" + String(index) let object = stem + ".o" let flags = ["--project-inputs", "--" + snapshot.cpu.rawValue, linked ? "--obj" : "--decb", "--error-format=gcc", "-I."] let fileArg = "./" + path _ = try invoke("lwasm", flags + ["--tabs=0", "--list=" + stem + ".lst", "--project-map=" + stem + ".lines", "--symbol-dump=" + stem + ".sym", "-o", linked ? object : "../out/program.bin", fileArg]) let records = try DevelopmentBuildTools.readFile(workspace.appendingPathComponent(stem + ".lines"), limit: 4 * 1024 * 1024) let list = try DevelopmentBuildTools.readFile(workspace.appendingPathComponent(stem + ".lst"), limit: 8 * 1024 * 1024) let listing = String(decoding: list, as: UTF8.self) guard listings.reduce(listing.utf8.count, { $0 + $1.utf8.count }) <= 16 * 1024 * 1024 else { throw AssemblyBuildFailure("Combined listings exceed 16 MB.") } modules.append((object, String(decoding: records, as: UTF8.self))) listings.append(listing) if linked { objects.append(object) } else { symbols = String(decoding: try DevelopmentBuildTools.readFile(workspace.appendingPathComponent(stem + ".sym"), limit: 4 * 1024 * 1024), as: UTF8.self) } } if let script = settings.linkScript { _ = try invoke("lwlink", ["--version"]) _ = try invoke("lwlink", ["--decb", "--script=./" + script, "--map=../out/link.map", "-o", "../out/program.bin"] + objects) symbols = String(decoding: try DevelopmentBuildTools.readFile(output.appendingPathComponent("link.map"), limit: 4 * 1024 * 1024), as: UTF8.self) } } let binary = try DevelopmentBuildTools.readFile(binaryURL, limit: BinaryFile.limit) let image = try AssemblyImage(binary) var instructions: [AssemblyInstructionLocation] = [] for (object, records) in modules { try control.checkCancellation() var sections: [String: CMOCSourceMap.VerifiedSection]? if settings.linkScript != nil { do { let data = try DevelopmentBuildTools.readFile(workspace.appendingPathComponent(object), limit: 8 * 1024 * 1024) sections = try CMOCSourceMap.verifiedSections(object: data, objectName: object, linkMap: Data(symbols.utf8), binary: binary).sections } catch { log += "Source locations unavailable for " + object + ". " + error.localizedDescription + "\n" continue } } instructions += Self.locations(records, files: snapshot.files, image: image, sections: sections) } // Repeated include expansions or two rows claiming a PC are ambiguous. let addresses = Dictionary(grouping: instructions, by: \.address) let lines = Dictionary(grouping: instructions) { $0.path + "\n" + String($0.line) } let verified = instructions.filter { addresses[$0.address]?.count == 1 && lines[$0.path + "\n" + String($0.line)]?.count == 1 } return AssemblyBuildArtifact(id: UUID(), snapshot: snapshot, binaryData: binary, entry: image.entry, ranges: image.ranges, listing: listings.joined(separator: "\n"), symbols: symbols, buildLog: log, instructions: verified) } } func diskImage(for artifact: AssemblyBuildArtifact, name: String, dosBoot: Bool = false) async throws -> Data { let dosName = name.uppercased() guard dosName.range(of: #"\A[A-Z0-9][A-Z0-9_-]{0,7}\z"#, options: .regularExpression) != nil else { throw AssemblyBuildFailure("Use a Disk BASIC name of one to eight letters or digits, with optional hyphens or underscores.") } return try await tools.withWorkspace { root, control in let image = root.appendingPathComponent("export.dsk") try Data(repeating: 0xff, count: 35 * 18 * 256).write(to: image) if dosBoot { try tools.installBootLoader(.binary(dosName), in: root, control: control) } let file = dosName + ".BIN" try artifact.binaryData.write(to: root.appendingPathComponent(file)) let result = try tools.run("writecocofile", ["export.dsk", file], in: root, control: control) guard result.status == 0 else { throw AssemblyBuildFailure(result.text) } return try DevelopmentBuildTools.readFile(image, limit: 35 * 18 * 256) } } private static func diagnostics(_ text: String, files: [String: Data]) -> [AssemblyDiagnostic] { let pattern = try! NSRegularExpression(pattern: #"^(.+?):([0-9]+): (?:error|warning): (.+)$"#) return text.components(separatedBy: .newlines).compactMap { row in guard let fields = groups(pattern, row), let number = Int(fields[2]), let path = sourcePath(fields[1], in: Set(files.keys)) else { return nil } return AssemblyDiagnostic(path: path, line: number, message: fields[3]) } } private static func groups(_ regex: NSRegularExpression, _ row: String) -> [String]? { let ns = row as NSString guard let match = regex.firstMatch(in: row, range: NSRange(location: 0, length: ns.length)) else { return nil } return (0..) -> String? { var token = token while token.hasPrefix("./") { token.removeFirst(2) } guard !token.isEmpty else { return nil } let candidates = inputs.filter { path in path == token || path.hasSuffix("/" + token) } return candidates.count == 1 ? candidates.first : nil } /// Physical source names, lines and section names come directly from LWASM. /// The display listing abbreviates names and can include macro expansions. private static func locations(_ records: String, files: [String: Data], image: AssemblyImage, sections: [String: CMOCSourceMap.VerifiedSection]?) -> [AssemblyInstructionLocation] { var result: [AssemblyInstructionLocation] = [] for row in records.split(whereSeparator: \.isNewline) { let fields = row.split(separator: "\t", omittingEmptySubsequences: false) guard fields.count == 5, let line = Int(fields[1]), line > 0, let offset = Int(fields[3], radix: 16), (0..<65536).contains(offset) else { continue } let path = String(fields[0]), digits = Array(fields[4]) guard files[path] != nil, (2...16).contains(digits.count), digits.count.isMultiple(of: 2) else { continue } let original = stride(from: 0, to: digits.count, by: 2).compactMap { UInt8(String(digits[$0...($0 + 1)]), radix: 16) } guard original.count * 2 == digits.count else { continue } let address: Int, bytes: [UInt8] if let sections { guard let section = sections[String(fields[2])], let final = section.final, offset + original.count <= section.original.count, section.original[offset..<(offset + original.count)].elementsEqual(original) else { continue } address = section.base + offset bytes = Array(final[offset..<(offset + original.count)]) } else { guard fields[2].isEmpty else { continue } address = offset bytes = original } guard address + bytes.count <= 65536, bytes.enumerated().allSatisfy({ image.bytes[address + $0.offset] == $0.element }) else { continue } result.append(.init(path: path, line: line, address: UInt16(address), bytes: bytes)) } return result } }