Skip to content

Commit e6a4032

Browse files
authored
Remove unused regsiters, opodes (#506)
1 parent bb558ea commit e6a4032

File tree

6 files changed

+28
-345
lines changed

6 files changed

+28
-345
lines changed

Sources/_StringProcessing/Engine/Instruction.swift

+6-114
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,6 @@ extension Instruction {
2727

2828
// MARK: - General Purpose
2929

30-
/// Do nothing
31-
///
32-
/// nop(comment: String?)
33-
///
34-
/// Operand: Optional string register containing a comment or reason
35-
///
36-
case nop
37-
38-
/// Decrement the value stored in a register.
39-
/// Returns whether the value was set to zero
40-
///
41-
/// decrement(_ i: IntReg) -> Bool
42-
///
43-
/// Operands:
44-
/// - Int register to decrease
45-
/// - Condition register set if now zero
46-
///
47-
case decrement
48-
4930
/// Move an immediate value into a register
5031
///
5132
/// moveImmediate(_ i: Int, into: IntReg)
@@ -65,15 +46,6 @@ extension Instruction {
6546
/// Operand: instruction address to branch to
6647
case branch
6748

68-
/// Conditionally branch
69-
///
70-
/// condBranch(to: InstAddr, if: BoolReg)
71-
///
72-
/// Operands:
73-
/// - Address to branch to
74-
/// - Condition register to check
75-
case condBranch
76-
7749
/// Conditionally branch if zero, otherwise decrement
7850
///
7951
/// condBranch(
@@ -85,39 +57,7 @@ extension Instruction {
8557
///
8658
case condBranchZeroElseDecrement
8759

88-
// MARK: General Purpose: Function calls
89-
90-
/// Push an instruction address to the stack
91-
///
92-
/// Operand: the instruction address
93-
///
94-
/// UNIMPLEMENTED
95-
case push
96-
97-
/// Pop return address from call stack
98-
///
99-
/// UNIMPLEMENTED
100-
case pop
101-
102-
/// Composite push-next-branch instruction
103-
///
104-
/// Operand: the function's start address
105-
case call
106-
107-
/// Composite pop-branch instruction
108-
///
109-
/// Operand: the instruction address
110-
///
111-
/// NOTE: Currently, empty stack -> ACCEPT
112-
case ret
113-
114-
// MARK: General Purpose: Debugging instructions
115-
116-
/// Print a string to the output
117-
///
118-
/// Operand: String register
119-
case print
120-
60+
// TODO: Function calls
12161

12262
// MARK: - Matching
12363

@@ -144,28 +84,11 @@ extension Instruction {
14484
/// Operand: Sequence register to compare against.
14585
case matchSequence
14686

147-
/// Match against a slice of the input
148-
///
149-
/// matchSlice(
150-
/// lowerBound: PositionReg, upperBound: PositionReg)
151-
///
152-
/// Operands:
153-
/// - Lowerbound position in the input
154-
/// - Upperbound position in the input
155-
case matchSlice
156-
157-
/// Save the current position in the input in a register
158-
///
159-
/// movePosition(into: PositionReg)
160-
///
161-
/// Operand: The position register to move into
162-
case movePosition
87+
/// TODO: builtin assertions and anchors
88+
case builtinAssertion
16389

164-
/// Match against a provided element.
165-
///
166-
/// Operand: Packed condition register to write to and element register to
167-
/// compare against.
168-
case assertion
90+
/// TODO: builtin character classes
91+
case builtinCharacterClass
16992

17093
// MARK: Extension points
17194

@@ -235,14 +158,6 @@ extension Instruction {
235158
/// Precondition: The operand is in the save point list
236159
case clearThrough
237160

238-
/// View the most recently saved point
239-
///
240-
/// UNIMPLEMENTED
241-
case peek
242-
243-
/// Composite peek-branch-clear else FAIL
244-
case restore
245-
246161
/// Fused save-and-branch.
247162
///
248163
/// split(to: target, saving: backtrackPoint)
@@ -290,17 +205,9 @@ extension Instruction {
290205
/// Signal failure (currently same as `restore`)
291206
case fail
292207

293-
/// Halt, fail, and signal failure
294-
///
295-
/// Operand: optional string register specifying the reason
296-
///
297-
/// TODO: Could have an Error existential area instead
298-
case abort
299-
300208
// TODO: Fused assertions. It seems like we often want to
301209
// branch based on assertion fail or success.
302210

303-
304211
}
305212
}
306213

@@ -385,32 +292,17 @@ extension Instruction {
385292

386293
// TODO: replace with instruction formatters...
387294
extension Instruction {
388-
var stringRegister: StringRegister? {
389-
switch opcode {
390-
case .nop, .abort:
391-
return payload.optionalString
392-
case .print:
393-
return payload.string
394-
default: return nil
395-
}
396-
}
397295
var instructionAddress: InstructionAddress? {
398296
switch opcode {
399-
case .branch, .save, .saveAddress, .call:
297+
case .branch, .save, .saveAddress:
400298
return payload.addr
401-
402-
case .condBranch:
403-
return payload.pairedAddrBool.0
404-
405299
default: return nil
406300
}
407301
}
408302
var elementRegister: ElementRegister? {
409303
switch opcode {
410304
case .match:
411305
return payload.element
412-
case .assertion:
413-
return payload.pairedElementBool.0
414306
default: return nil
415307
}
416308
}

Sources/_StringProcessing/Engine/MEBuilder.swift

+1-110
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extension MEProgram {
1717

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

2221
var consumeFunctions: [ConsumeFunction] = []
2322
var assertionFunctions: [AssertionFunction] = []
@@ -29,9 +28,7 @@ extension MEProgram {
2928
var addressFixups: [(InstructionAddress, AddressFixup)] = []
3029

3130
// Registers
32-
var nextBoolRegister = BoolRegister(0)
3331
var nextIntRegister = IntRegister(0)
34-
var nextPositionRegister = PositionRegister(0)
3532
var nextCaptureRegister = CaptureRegister(0)
3633
var nextValueRegister = ValueRegister(0)
3734

@@ -79,20 +76,6 @@ extension MEProgram.Builder {
7976
.init(instructions.endIndex - 1)
8077
}
8178

82-
mutating func buildNop(_ r: StringRegister? = nil) {
83-
instructions.append(.init(.nop, .init(optionalString: r)))
84-
}
85-
mutating func buildNop(_ s: String) {
86-
buildNop(strings.store(s))
87-
}
88-
89-
mutating func buildDecrement(
90-
_ i: IntRegister, nowZero: BoolRegister
91-
) {
92-
instructions.append(.init(
93-
.decrement, .init(bool: nowZero, int: i)))
94-
}
95-
9679
mutating func buildMoveImmediate(
9780
_ value: UInt64, into: IntRegister
9881
) {
@@ -108,24 +91,10 @@ extension MEProgram.Builder {
10891
buildMoveImmediate(uint, into: into)
10992
}
11093

111-
mutating func buildMoveCurrentPosition(
112-
into: PositionRegister
113-
) {
114-
instructions.append(.init(
115-
.movePosition, .init(position: into)))
116-
}
117-
11894
mutating func buildBranch(to t: AddressToken) {
11995
instructions.append(.init(.branch))
12096
fixup(to: t)
12197
}
122-
mutating func buildCondBranch(
123-
_ condition: BoolRegister, to t: AddressToken
124-
) {
125-
instructions.append(
126-
.init(.condBranch, .init(bool: condition)))
127-
fixup(to: t)
128-
}
12998

13099
mutating func buildCondBranch(
131100
to t: AddressToken, ifZeroElseDecrement i: IntRegister
@@ -157,27 +126,9 @@ extension MEProgram.Builder {
157126
instructions.append(.init(.clearThrough))
158127
fixup(to: t)
159128
}
160-
mutating func buildRestore() {
161-
instructions.append(.init(.restore))
162-
}
163129
mutating func buildFail() {
164130
instructions.append(.init(.fail))
165131
}
166-
mutating func buildCall(_ t: AddressToken) {
167-
instructions.append(.init(.call))
168-
fixup(to: t)
169-
}
170-
mutating func buildRet() {
171-
instructions.append(.init(.ret))
172-
}
173-
174-
mutating func buildAbort(_ s: StringRegister? = nil) {
175-
instructions.append(.init(
176-
.abort, .init(optionalString: s)))
177-
}
178-
mutating func buildAbort(_ s: String) {
179-
buildAbort(strings.store(s))
180-
}
181132

182133
mutating func buildAdvance(_ n: Distance) {
183134
instructions.append(.init(.advance, .init(distance: n)))
@@ -196,14 +147,6 @@ extension MEProgram.Builder {
196147
.init(sequence: sequences.store(.init(s)))))
197148
}
198149

199-
mutating func buildMatchSlice(
200-
lower: PositionRegister, upper: PositionRegister
201-
) {
202-
instructions.append(.init(
203-
.matchSlice,
204-
.init(pos: lower, pos2: upper)))
205-
}
206-
207150
mutating func buildConsume(
208151
by p: @escaping MEProgram.ConsumeFunction
209152
) {
@@ -218,21 +161,10 @@ extension MEProgram.Builder {
218161
.assertBy, .init(assertion: makeAssertionFunction(p))))
219162
}
220163

221-
mutating func buildAssert(
222-
_ e: Character, into cond: BoolRegister
223-
) {
224-
instructions.append(.init(.assertion, .init(
225-
element: elements.store(e), bool: cond)))
226-
}
227-
228164
mutating func buildAccept() {
229165
instructions.append(.init(.accept))
230166
}
231167

232-
mutating func buildPrint(_ s: StringRegister) {
233-
instructions.append(.init(.print, .init(string: s)))
234-
}
235-
236168
mutating func buildBeginCapture(
237169
_ cap: CaptureRegister
238170
) {
@@ -315,13 +247,10 @@ extension MEProgram.Builder {
315247
let payload: Instruction.Payload
316248

317249
switch inst.opcode {
318-
case .condBranch:
319-
payload = .init(addr: addr, bool: inst.payload.bool)
320-
321250
case .condBranchZeroElseDecrement:
322251
payload = .init(addr: addr, int: inst.payload.int)
323252

324-
case .branch, .save, .saveAddress, .call, .clearThrough:
253+
case .branch, .save, .saveAddress, .clearThrough:
325254
payload = .init(addr: addr)
326255

327256
case .splitSaving:
@@ -342,10 +271,7 @@ extension MEProgram.Builder {
342271
var regInfo = MEProgram.RegisterInfo()
343272
regInfo.elements = elements.count
344273
regInfo.sequences = sequences.count
345-
regInfo.strings = strings.count
346-
regInfo.bools = nextBoolRegister.rawValue
347274
regInfo.ints = nextIntRegister.rawValue
348-
regInfo.positions = nextPositionRegister.rawValue
349275
regInfo.values = nextValueRegister.rawValue
350276
regInfo.consumeFunctions = consumeFunctions.count
351277
regInfo.assertionFunctions = assertionFunctions.count
@@ -357,7 +283,6 @@ extension MEProgram.Builder {
357283
instructions: InstructionList(instructions),
358284
staticElements: elements.stored,
359285
staticSequences: sequences.stored,
360-
staticStrings: strings.stored,
361286
staticConsumeFunctions: consumeFunctions,
362287
staticAssertionFunctions: assertionFunctions,
363288
staticTransformFunctions: transformFunctions,
@@ -468,18 +393,10 @@ extension MEProgram.Builder {
468393
return nextCaptureRegister
469394
}
470395

471-
mutating func makeBoolRegister() -> BoolRegister {
472-
defer { nextBoolRegister.rawValue += 1 }
473-
return nextBoolRegister
474-
}
475396
mutating func makeIntRegister() -> IntRegister {
476397
defer { nextIntRegister.rawValue += 1 }
477398
return nextIntRegister
478399
}
479-
mutating func makePositionRegister() -> PositionRegister {
480-
defer { nextPositionRegister.rawValue += 1 }
481-
return nextPositionRegister
482-
}
483400
mutating func makeValueRegister() -> ValueRegister {
484401
defer { nextValueRegister.rawValue += 1 }
485402
return nextValueRegister
@@ -494,32 +411,6 @@ extension MEProgram.Builder {
494411
return r
495412
}
496413

497-
// Allocate and initialize a register
498-
mutating func makePositionRegister(
499-
initializingWithCurrentPosition: ()
500-
) -> PositionRegister {
501-
let r = makePositionRegister()
502-
self.buildMoveCurrentPosition(into: r)
503-
return r
504-
}
505-
506-
// 'kill' or release allocated registers
507-
mutating func kill(_ r: IntRegister) {
508-
// TODO: Release/reuse registers, for now nop makes
509-
// reading the code easier
510-
buildNop("kill \(r)")
511-
}
512-
mutating func kill(_ r: BoolRegister) {
513-
// TODO: Release/reuse registers, for now nop makes
514-
// reading the code easier
515-
buildNop("kill \(r)")
516-
}
517-
mutating func kill(_ r: PositionRegister) {
518-
// TODO: Release/reuse registers, for now nop makes
519-
// reading the code easier
520-
buildNop("kill \(r)")
521-
}
522-
523414
// TODO: A register-mapping helper struct, which could release
524415
// registers without monotonicity required
525416

0 commit comments

Comments
 (0)