Skip to content

Remove unused regsiters, opodes #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 6 additions & 114 deletions Sources/_StringProcessing/Engine/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.


}
}

Expand Down Expand Up @@ -385,32 +292,17 @@ 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
}
}
var elementRegister: ElementRegister? {
switch opcode {
case .match:
return payload.element
case .assertion:
return payload.pairedElementBool.0
default: return nil
}
}
Expand Down
111 changes: 1 addition & 110 deletions Sources/_StringProcessing/Engine/MEBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extension MEProgram {

var elements = TypedSetVector<Input.Element, _ElementRegister>()
var sequences = TypedSetVector<[Input.Element], _SequenceRegister>()
var strings = TypedSetVector<String, _StringRegister>()

var consumeFunctions: [ConsumeFunction] = []
var assertionFunctions: [AssertionFunction] = []
Expand All @@ -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)

Expand Down Expand Up @@ -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
) {
Expand All @@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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
) {
Expand All @@ -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
) {
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
Loading