From 537992db54da4e5529ba7ecc02940efbafface9c Mon Sep 17 00:00:00 2001 From: Michael Ilseman Date: Tue, 21 Jun 2022 09:35:03 -0600 Subject: [PATCH] Remove unused regsiters, opodes --- .../Engine/Instruction.swift | 120 +----------------- .../_StringProcessing/Engine/MEBuilder.swift | 111 +--------------- .../_StringProcessing/Engine/MEBuiltins.swift | 13 ++ .../_StringProcessing/Engine/MEProgram.swift | 5 - .../_StringProcessing/Engine/Processor.swift | 92 ++------------ .../_StringProcessing/Engine/Registers.swift | 32 ----- 6 files changed, 28 insertions(+), 345 deletions(-) create mode 100644 Sources/_StringProcessing/Engine/MEBuiltins.swift diff --git a/Sources/_StringProcessing/Engine/Instruction.swift b/Sources/_StringProcessing/Engine/Instruction.swift index 9144c031f..9d2ae5a69 100644 --- a/Sources/_StringProcessing/Engine/Instruction.swift +++ b/Sources/_StringProcessing/Engine/Instruction.swift @@ -27,25 +27,6 @@ extension Instruction { // MARK: - General Purpose - /// Do nothing - /// - /// nop(comment: String?) - /// - /// Operand: Optional string register containing a comment or reason - /// - case nop - - /// Decrement the value stored in a register. - /// Returns whether the value was set to zero - /// - /// decrement(_ i: IntReg) -> Bool - /// - /// Operands: - /// - Int register to decrease - /// - Condition register set if now zero - /// - case decrement - /// Move an immediate value into a register /// /// moveImmediate(_ i: Int, into: IntReg) @@ -65,15 +46,6 @@ extension Instruction { /// Operand: instruction address to branch to case branch - /// Conditionally branch - /// - /// condBranch(to: InstAddr, if: BoolReg) - /// - /// Operands: - /// - Address to branch to - /// - Condition register to check - case condBranch - /// Conditionally branch if zero, otherwise decrement /// /// condBranch( @@ -85,39 +57,7 @@ extension Instruction { /// case condBranchZeroElseDecrement - // MARK: General Purpose: Function calls - - /// Push an instruction address to the stack - /// - /// Operand: the instruction address - /// - /// UNIMPLEMENTED - case push - - /// Pop return address from call stack - /// - /// UNIMPLEMENTED - case pop - - /// Composite push-next-branch instruction - /// - /// Operand: the function's start address - case call - - /// Composite pop-branch instruction - /// - /// Operand: the instruction address - /// - /// NOTE: Currently, empty stack -> ACCEPT - case ret - - // MARK: General Purpose: Debugging instructions - - /// Print a string to the output - /// - /// Operand: String register - case print - + // TODO: Function calls // MARK: - Matching @@ -144,28 +84,11 @@ extension Instruction { /// Operand: Sequence register to compare against. case matchSequence - /// Match against a slice of the input - /// - /// matchSlice( - /// lowerBound: PositionReg, upperBound: PositionReg) - /// - /// Operands: - /// - Lowerbound position in the input - /// - Upperbound position in the input - case matchSlice - - /// Save the current position in the input in a register - /// - /// movePosition(into: PositionReg) - /// - /// Operand: The position register to move into - case movePosition + /// TODO: builtin assertions and anchors + case builtinAssertion - /// Match against a provided element. - /// - /// Operand: Packed condition register to write to and element register to - /// compare against. - case assertion + /// TODO: builtin character classes + case builtinCharacterClass // MARK: Extension points @@ -235,14 +158,6 @@ extension Instruction { /// Precondition: The operand is in the save point list case clearThrough - /// View the most recently saved point - /// - /// UNIMPLEMENTED - case peek - - /// Composite peek-branch-clear else FAIL - case restore - /// Fused save-and-branch. /// /// split(to: target, saving: backtrackPoint) @@ -290,17 +205,9 @@ extension Instruction { /// Signal failure (currently same as `restore`) case fail - /// Halt, fail, and signal failure - /// - /// Operand: optional string register specifying the reason - /// - /// TODO: Could have an Error existential area instead - case abort - // TODO: Fused assertions. It seems like we often want to // branch based on assertion fail or success. - } } @@ -385,23 +292,10 @@ extension Instruction { // TODO: replace with instruction formatters... extension Instruction { - var stringRegister: StringRegister? { - switch opcode { - case .nop, .abort: - return payload.optionalString - case .print: - return payload.string - default: return nil - } - } var instructionAddress: InstructionAddress? { switch opcode { - case .branch, .save, .saveAddress, .call: + case .branch, .save, .saveAddress: return payload.addr - - case .condBranch: - return payload.pairedAddrBool.0 - default: return nil } } @@ -409,8 +303,6 @@ extension Instruction { switch opcode { case .match: return payload.element - case .assertion: - return payload.pairedElementBool.0 default: return nil } } diff --git a/Sources/_StringProcessing/Engine/MEBuilder.swift b/Sources/_StringProcessing/Engine/MEBuilder.swift index f998a4952..f278b7328 100644 --- a/Sources/_StringProcessing/Engine/MEBuilder.swift +++ b/Sources/_StringProcessing/Engine/MEBuilder.swift @@ -17,7 +17,6 @@ extension MEProgram { var elements = TypedSetVector() var sequences = TypedSetVector<[Input.Element], _SequenceRegister>() - var strings = TypedSetVector() var consumeFunctions: [ConsumeFunction] = [] var assertionFunctions: [AssertionFunction] = [] @@ -29,9 +28,7 @@ extension MEProgram { var addressFixups: [(InstructionAddress, AddressFixup)] = [] // Registers - var nextBoolRegister = BoolRegister(0) var nextIntRegister = IntRegister(0) - var nextPositionRegister = PositionRegister(0) var nextCaptureRegister = CaptureRegister(0) var nextValueRegister = ValueRegister(0) @@ -79,20 +76,6 @@ extension MEProgram.Builder { .init(instructions.endIndex - 1) } - mutating func buildNop(_ r: StringRegister? = nil) { - instructions.append(.init(.nop, .init(optionalString: r))) - } - mutating func buildNop(_ s: String) { - buildNop(strings.store(s)) - } - - mutating func buildDecrement( - _ i: IntRegister, nowZero: BoolRegister - ) { - instructions.append(.init( - .decrement, .init(bool: nowZero, int: i))) - } - mutating func buildMoveImmediate( _ value: UInt64, into: IntRegister ) { @@ -108,24 +91,10 @@ extension MEProgram.Builder { buildMoveImmediate(uint, into: into) } - mutating func buildMoveCurrentPosition( - into: PositionRegister - ) { - instructions.append(.init( - .movePosition, .init(position: into))) - } - mutating func buildBranch(to t: AddressToken) { instructions.append(.init(.branch)) fixup(to: t) } - mutating func buildCondBranch( - _ condition: BoolRegister, to t: AddressToken - ) { - instructions.append( - .init(.condBranch, .init(bool: condition))) - fixup(to: t) - } mutating func buildCondBranch( to t: AddressToken, ifZeroElseDecrement i: IntRegister @@ -157,27 +126,9 @@ extension MEProgram.Builder { instructions.append(.init(.clearThrough)) fixup(to: t) } - mutating func buildRestore() { - instructions.append(.init(.restore)) - } mutating func buildFail() { instructions.append(.init(.fail)) } - mutating func buildCall(_ t: AddressToken) { - instructions.append(.init(.call)) - fixup(to: t) - } - mutating func buildRet() { - instructions.append(.init(.ret)) - } - - mutating func buildAbort(_ s: StringRegister? = nil) { - instructions.append(.init( - .abort, .init(optionalString: s))) - } - mutating func buildAbort(_ s: String) { - buildAbort(strings.store(s)) - } mutating func buildAdvance(_ n: Distance) { instructions.append(.init(.advance, .init(distance: n))) @@ -196,14 +147,6 @@ extension MEProgram.Builder { .init(sequence: sequences.store(.init(s))))) } - mutating func buildMatchSlice( - lower: PositionRegister, upper: PositionRegister - ) { - instructions.append(.init( - .matchSlice, - .init(pos: lower, pos2: upper))) - } - mutating func buildConsume( by p: @escaping MEProgram.ConsumeFunction ) { @@ -218,21 +161,10 @@ extension MEProgram.Builder { .assertBy, .init(assertion: makeAssertionFunction(p)))) } - mutating func buildAssert( - _ e: Character, into cond: BoolRegister - ) { - instructions.append(.init(.assertion, .init( - element: elements.store(e), bool: cond))) - } - mutating func buildAccept() { instructions.append(.init(.accept)) } - mutating func buildPrint(_ s: StringRegister) { - instructions.append(.init(.print, .init(string: s))) - } - mutating func buildBeginCapture( _ cap: CaptureRegister ) { @@ -315,13 +247,10 @@ extension MEProgram.Builder { let payload: Instruction.Payload switch inst.opcode { - case .condBranch: - payload = .init(addr: addr, bool: inst.payload.bool) - case .condBranchZeroElseDecrement: payload = .init(addr: addr, int: inst.payload.int) - case .branch, .save, .saveAddress, .call, .clearThrough: + case .branch, .save, .saveAddress, .clearThrough: payload = .init(addr: addr) case .splitSaving: @@ -342,10 +271,7 @@ extension MEProgram.Builder { var regInfo = MEProgram.RegisterInfo() regInfo.elements = elements.count regInfo.sequences = sequences.count - regInfo.strings = strings.count - regInfo.bools = nextBoolRegister.rawValue regInfo.ints = nextIntRegister.rawValue - regInfo.positions = nextPositionRegister.rawValue regInfo.values = nextValueRegister.rawValue regInfo.consumeFunctions = consumeFunctions.count regInfo.assertionFunctions = assertionFunctions.count @@ -357,7 +283,6 @@ extension MEProgram.Builder { instructions: InstructionList(instructions), staticElements: elements.stored, staticSequences: sequences.stored, - staticStrings: strings.stored, staticConsumeFunctions: consumeFunctions, staticAssertionFunctions: assertionFunctions, staticTransformFunctions: transformFunctions, @@ -468,18 +393,10 @@ extension MEProgram.Builder { return nextCaptureRegister } - mutating func makeBoolRegister() -> BoolRegister { - defer { nextBoolRegister.rawValue += 1 } - return nextBoolRegister - } mutating func makeIntRegister() -> IntRegister { defer { nextIntRegister.rawValue += 1 } return nextIntRegister } - mutating func makePositionRegister() -> PositionRegister { - defer { nextPositionRegister.rawValue += 1 } - return nextPositionRegister - } mutating func makeValueRegister() -> ValueRegister { defer { nextValueRegister.rawValue += 1 } return nextValueRegister @@ -494,32 +411,6 @@ extension MEProgram.Builder { return r } - // Allocate and initialize a register - mutating func makePositionRegister( - initializingWithCurrentPosition: () - ) -> PositionRegister { - let r = makePositionRegister() - self.buildMoveCurrentPosition(into: r) - return r - } - - // 'kill' or release allocated registers - mutating func kill(_ r: IntRegister) { - // TODO: Release/reuse registers, for now nop makes - // reading the code easier - buildNop("kill \(r)") - } - mutating func kill(_ r: BoolRegister) { - // TODO: Release/reuse registers, for now nop makes - // reading the code easier - buildNop("kill \(r)") - } - mutating func kill(_ r: PositionRegister) { - // TODO: Release/reuse registers, for now nop makes - // reading the code easier - buildNop("kill \(r)") - } - // TODO: A register-mapping helper struct, which could release // registers without monotonicity required diff --git a/Sources/_StringProcessing/Engine/MEBuiltins.swift b/Sources/_StringProcessing/Engine/MEBuiltins.swift new file mode 100644 index 000000000..f791da37e --- /dev/null +++ b/Sources/_StringProcessing/Engine/MEBuiltins.swift @@ -0,0 +1,13 @@ + + +extension Processor { + + + mutating func builtinAssertion() { + fatalError("TODO: assertions and anchors") + } + + mutating func builtinCharacterClass() { + fatalError("TODO: character classes") + } +} diff --git a/Sources/_StringProcessing/Engine/MEProgram.swift b/Sources/_StringProcessing/Engine/MEProgram.swift index dd166e554..2a0ec2719 100644 --- a/Sources/_StringProcessing/Engine/MEProgram.swift +++ b/Sources/_StringProcessing/Engine/MEProgram.swift @@ -26,7 +26,6 @@ struct MEProgram { var staticElements: [Input.Element] var staticSequences: [[Input.Element]] - var staticStrings: [String] var staticConsumeFunctions: [ConsumeFunction] var staticAssertionFunctions: [AssertionFunction] var staticTransformFunctions: [TransformFunction] @@ -46,7 +45,6 @@ extension MEProgram: CustomStringConvertible { var description: String { var result = """ Elements: \(staticElements) - Strings: \(staticStrings) """ if !staticConsumeFunctions.isEmpty { @@ -58,9 +56,6 @@ extension MEProgram: CustomStringConvertible { for idx in instructions.indices { let inst = instructions[idx] result += "[\(idx.rawValue)] \(inst)" - if let sp = inst.stringRegister { - result += " // \(staticStrings[sp.rawValue])" - } if let ia = inst.instructionAddress { result += " // \(instructions[ia])" } diff --git a/Sources/_StringProcessing/Engine/Processor.swift b/Sources/_StringProcessing/Engine/Processor.swift index 1717e485d..8471ef861 100644 --- a/Sources/_StringProcessing/Engine/Processor.swift +++ b/Sources/_StringProcessing/Engine/Processor.swift @@ -265,20 +265,6 @@ extension Processor { switch opcode { case .invalid: fatalError("Invalid program") - case .nop: - if checkComments, - let s = payload.optionalString - { - doPrint(registers[s]) - } - controller.step() - - case .decrement: - let (bool, int) = payload.pairedBoolInt - let newValue = registers[int] - 1 - registers[bool] = newValue == 0 - registers[int] = newValue - controller.step() case .moveImmediate: let (imm, reg) = payload.pairedImmediateInt @@ -288,22 +274,9 @@ extension Processor { registers[reg] = int controller.step() - case .movePosition: - let reg = payload.position - registers[reg] = currentPosition - controller.step() - case .branch: controller.pc = payload.addr - case .condBranch: - let (addr, cond) = payload.pairedAddrBool - if registers[cond] { - controller.pc = addr - } else { - controller.step() - } - case .condBranchZeroElseDecrement: let (addr, int) = payload.pairedAddrInt if registers[int] == 0 { @@ -341,38 +314,6 @@ extension Processor { case .clearThrough: clearThrough(payload.addr) - - case .peek: - fatalError() - - case .restore: - signalFailure() - - case .push: - fatalError() - - case .pop: - fatalError() - - case .call: - controller.step() - callStack.append(controller.pc) - controller.pc = payload.addr - - case .ret: - // TODO: Should empty stack mean success? - guard let r = callStack.popLast() else { - tryAccept() - return - } - controller.pc = r - - case .abort: - // TODO: throw or otherwise propagate - if let s = payload.optionalString { - doPrint(registers[s]) - } - state = .fail case .accept: tryAccept() @@ -398,14 +339,6 @@ extension Processor { controller.step() } - case .matchSlice: - let (lower, upper) = payload.pairedPosPos - let range = registers[lower].. String { - strings[i.rawValue] - } subscript(_ i: SequenceRegister) -> [Input.Element] { sequences[i.rawValue] } @@ -70,14 +58,6 @@ extension Processor.Registers { get { ints[i.rawValue] } set { ints[i.rawValue] = newValue } } - subscript(_ i: BoolRegister) -> Bool { - get { bools[i.rawValue] } - set { bools[i.rawValue] = newValue } - } - subscript(_ i: PositionRegister) -> Input.Index { - get { positions[i.rawValue] } - set { positions[i.rawValue] = newValue } - } subscript(_ i: ValueRegister) -> Any { get { values[i.rawValue] } set { @@ -126,23 +106,14 @@ extension Processor.Registers { self.matcherFunctions = program.staticMatcherFunctions assert(matcherFunctions.count == info.matcherFunctions) - self.strings = program.staticStrings - assert(strings.count == info.strings) - - self.bools = Array(repeating: false, count: info.bools) - self.ints = Array(repeating: 0, count: info.ints) - self.positions = Array(repeating: sentinel, count: info.positions) - self.values = Array( repeating: SentinelValue(), count: info.values) } mutating func reset(sentinel: Input.Index) { - self.bools._setAll(to: false) self.ints._setAll(to: 0) - self.positions._setAll(to: sentinel) self.values._setAll(to: SentinelValue()) } } @@ -191,10 +162,7 @@ extension Processor.Registers: CustomStringConvertible { return """ \(formatRegisters("elements", elements))\ - \(formatRegisters("bools", bools))\ - \(formatRegisters("strings", strings))\ \(formatRegisters("ints", ints))\ - \(formatRegisters("positions", positions))\ """ }