Skip to content

Commit a7c09b7

Browse files
committed
wip: refactor to say max extra trips
1 parent 2b69446 commit a7c09b7

File tree

7 files changed

+72
-72
lines changed

7 files changed

+72
-72
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,16 @@ fileprivate extension Compiler.ByteCodeGen {
459459
assert(high != 0)
460460
assert((0...(high ?? Int.max)).contains(low))
461461

462-
let extraTrips: Int?
462+
let maxExtraTrips: Int?
463463
if let h = high {
464-
extraTrips = h - low
464+
maxExtraTrips = h - low
465465
} else {
466-
extraTrips = nil
466+
maxExtraTrips = nil
467467
}
468468
let minTrips = low
469-
assert((extraTrips ?? 1) >= 0)
469+
assert((maxExtraTrips ?? 1) >= 0)
470470

471-
if tryEmitFastQuant(child, updatedKind, minTrips, extraTrips) {
471+
if tryEmitFastQuant(child, updatedKind, minTrips, maxExtraTrips) {
472472
return
473473
}
474474

@@ -486,19 +486,19 @@ fileprivate extension Compiler.ByteCodeGen {
486486
decrement %minTrips and fallthrough
487487
488488
loop-body:
489-
<if can't guarantee forward progress && extraTrips = nil>:
489+
<if can't guarantee forward progress && maxExtraTrips = nil>:
490490
mov currentPosition %pos
491491
evaluate the subexpression
492-
<if can't guarantee forward progress && extraTrips = nil>:
492+
<if can't guarantee forward progress && maxExtraTrips = nil>:
493493
if %pos is currentPosition:
494494
goto exit
495495
goto min-trip-count control block
496496
497497
exit-policy control block:
498-
if %extraTrips is zero:
498+
if %maxExtraTrips is zero:
499499
goto exit
500500
else:
501-
decrement %extraTrips and fallthrough
501+
decrement %maxExtraTrips and fallthrough
502502
503503
<if eager>:
504504
save exit and goto loop-body
@@ -525,12 +525,12 @@ fileprivate extension Compiler.ByteCodeGen {
525525
/* fallthrough */
526526
"""
527527

528-
// Specialization based on `extraTrips` for 0 or unbounded
528+
// Specialization based on `maxExtraTrips` for 0 or unbounded
529529
_ = """
530530
exit-policy control block:
531-
<if extraTrips == 0>:
531+
<if maxExtraTrips == 0>:
532532
goto exit
533-
<if extraTrips == .unbounded>:
533+
<if maxExtraTrips == .unbounded>:
534534
/* fallthrough */
535535
"""
536536

@@ -563,12 +563,12 @@ fileprivate extension Compiler.ByteCodeGen {
563563
minTripsReg = nil
564564
}
565565

566-
let extraTripsReg: IntRegister?
567-
if (extraTrips ?? 0) > 0 {
568-
extraTripsReg = builder.makeIntRegister(
569-
initialValue: extraTrips!)
566+
let maxExtraTripsReg: IntRegister?
567+
if (maxExtraTrips ?? 0) > 0 {
568+
maxExtraTripsReg = builder.makeIntRegister(
569+
initialValue: maxExtraTrips!)
570570
} else {
571-
extraTripsReg = nil
571+
maxExtraTripsReg = nil
572572
}
573573

574574
// Set up a dummy save point for possessive to update
@@ -600,7 +600,7 @@ fileprivate extension Compiler.ByteCodeGen {
600600
let startPosition: PositionRegister?
601601
let emitPositionChecking =
602602
(!optimizationsEnabled || !child.guaranteesForwardProgress) &&
603-
extraTrips == nil
603+
maxExtraTrips == nil
604604

605605
if emitPositionChecking {
606606
startPosition = builder.makePositionRegister()
@@ -610,7 +610,7 @@ fileprivate extension Compiler.ByteCodeGen {
610610
}
611611
try emitNode(child)
612612
if emitPositionChecking {
613-
// in all quantifier cases, no matter what minTrips or extraTrips is,
613+
// in all quantifier cases, no matter what minTrips or maxExtraTrips is,
614614
// if we have a successful non-advancing match, branch to exit because it
615615
// can match an arbitrary number of times
616616
builder.buildCondBranch(to: exit, ifSamePositionAs: startPosition!)
@@ -623,20 +623,20 @@ fileprivate extension Compiler.ByteCodeGen {
623623
}
624624

625625
// exit-policy:
626-
// condBranch(to: exit, ifZeroElseDecrement: %extraTrips)
626+
// condBranch(to: exit, ifZeroElseDecrement: %maxExtraTrips)
627627
// <eager: split(to: loop, saving: exit)>
628628
// <possesive:
629629
// clearSavePoint
630630
// split(to: loop, saving: exit)>
631631
// <reluctant: save(restoringAt: loop)
632632
builder.label(exitPolicy)
633-
switch extraTrips {
633+
switch maxExtraTrips {
634634
case nil: break
635635
case 0: builder.buildBranch(to: exit)
636636
default:
637-
assert(extraTripsReg != nil, "logic inconsistency")
637+
assert(maxExtraTripsReg != nil, "logic inconsistency")
638638
builder.buildCondBranch(
639-
to: exit, ifZeroElseDecrement: extraTripsReg!)
639+
to: exit, ifZeroElseDecrement: maxExtraTripsReg!)
640640
}
641641

642642
switch updatedKind {
@@ -666,12 +666,12 @@ fileprivate extension Compiler.ByteCodeGen {
666666
_ child: DSLTree.Node,
667667
_ kind: AST.Quantification.Kind,
668668
_ minTrips: Int,
669-
_ extraTrips: Int?
669+
_ maxExtraTrips: Int?
670670
) -> Bool {
671671
let isScalarSemantics = options.semanticLevel == .unicodeScalar
672672
guard optimizationsEnabled
673673
&& minTrips <= QuantifyPayload.maxStorableTrips
674-
&& extraTrips ?? 0 <= QuantifyPayload.maxStorableTrips
674+
&& maxExtraTrips ?? 0 <= QuantifyPayload.maxStorableTrips
675675
&& kind != .reluctant else {
676676
return false
677677
}
@@ -681,7 +681,7 @@ fileprivate extension Compiler.ByteCodeGen {
681681
guard let bitset = ccc.asAsciiBitset(options) else {
682682
return false
683683
}
684-
builder.buildQuantify(bitset: bitset, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
684+
builder.buildQuantify(bitset: bitset, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
685685

686686
case .atom(let atom):
687687
switch atom {
@@ -690,17 +690,17 @@ fileprivate extension Compiler.ByteCodeGen {
690690
guard let val = c._singleScalarAsciiValue else {
691691
return false
692692
}
693-
builder.buildQuantify(asciiChar: val, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
693+
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
694694

695695
case .any:
696696
builder.buildQuantifyAny(
697-
matchesNewlines: true, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
697+
matchesNewlines: true, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
698698
case .anyNonNewline:
699699
builder.buildQuantifyAny(
700-
matchesNewlines: false, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
700+
matchesNewlines: false, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
701701
case .dot:
702702
builder.buildQuantifyAny(
703-
matchesNewlines: options.dotMatchesNewline, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
703+
matchesNewlines: options.dotMatchesNewline, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
704704

705705
case .characterClass(let cc):
706706
// Custom character class that consumes a single grapheme
@@ -709,19 +709,19 @@ fileprivate extension Compiler.ByteCodeGen {
709709
model: model,
710710
kind,
711711
minTrips,
712-
extraTrips,
712+
maxExtraTrips,
713713
isScalarSemantics: isScalarSemantics)
714714
default:
715715
return false
716716
}
717717
case .convertedRegexLiteral(let node, _):
718-
return tryEmitFastQuant(node, kind, minTrips, extraTrips)
718+
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
719719
case .nonCapturingGroup(let groupKind, let node):
720720
// .nonCapture nonCapturingGroups are ignored during compilation
721721
guard groupKind.ast == .nonCapture else {
722722
return false
723723
}
724-
return tryEmitFastQuant(node, kind, minTrips, extraTrips)
724+
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
725725
default:
726726
return false
727727
}

Sources/_StringProcessing/Engine/InstPayload.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,26 +392,26 @@ struct QuantifyPayload: RawRepresentable {
392392
// b39-b38 - isScalarSemantics
393393
// b38-b35 - Payload type (one of 4 types, stored on 3 bits)
394394
// b35-b27 - minTrips (8 bit int)
395-
// b27-b18 - extraTrips (8 bit value, one bit for nil)
395+
// b27-b18 - maxExtraTrips (8 bit value, one bit for nil)
396396
// b18-b16 - Quantification type (one of three types)
397397
// b16-b0 - Payload value (depends on payload type)
398398
static var quantKindShift: UInt64 { 16 }
399-
static var extraTripsShift: UInt64 { 18 }
399+
static var maxExtraTripsShift: UInt64 { 18 }
400400
static var minTripsShift: UInt64 { 27 }
401401
static var typeShift: UInt64 { 35 }
402402
static var maxStorableTrips: UInt64 { (1 << 8) - 1 }
403403
static var isScalarSemanticsBit: UInt64 { 1 &<< 38 }
404404

405405
var quantKindMask: UInt64 { 3 }
406-
var extraTripsMask: UInt64 { 0x1FF }
406+
var maxExtraTripsMask: UInt64 { 0x1FF }
407407
var minTripsMask: UInt64 { 0xFF }
408408
var typeMask: UInt64 { 7 }
409409
var payloadMask: UInt64 { 0xFF_FF }
410410

411411
static func packInfoValues(
412412
_ kind: AST.Quantification.Kind,
413413
_ minTrips: Int,
414-
_ extraTrips: Int?,
414+
_ maxExtraTrips: Int?,
415415
_ type: PayloadType,
416416
isScalarSemantics: Bool
417417
) -> UInt64 {
@@ -425,10 +425,10 @@ struct QuantifyPayload: RawRepresentable {
425425
kindVal = 2
426426
}
427427
// TODO: refactor / reimplement
428-
let extraTripsVal: UInt64 = extraTrips == nil ? 1 : UInt64(extraTrips!) << 1
428+
let maxExtraTripsVal: UInt64 = maxExtraTrips == nil ? 1 : UInt64(maxExtraTrips!) << 1
429429
let scalarSemanticsBit = isScalarSemantics ? Self.isScalarSemanticsBit : 0
430430
return (kindVal << QuantifyPayload.quantKindShift) |
431-
(extraTripsVal << QuantifyPayload.extraTripsShift) |
431+
(maxExtraTripsVal << QuantifyPayload.maxExtraTripsShift) |
432432
(UInt64(minTrips) << QuantifyPayload.minTripsShift) |
433433
(type.rawValue << QuantifyPayload.typeShift) |
434434
scalarSemanticsBit
@@ -443,49 +443,49 @@ struct QuantifyPayload: RawRepresentable {
443443
bitset: AsciiBitsetRegister,
444444
_ kind: AST.Quantification.Kind,
445445
_ minTrips: Int,
446-
_ extraTrips: Int?,
446+
_ maxExtraTrips: Int?,
447447
isScalarSemantics: Bool
448448
) {
449449
assert(bitset.bits <= _payloadMask)
450450
self.rawValue = bitset.bits
451-
+ QuantifyPayload.packInfoValues(kind, minTrips, extraTrips, .bitset, isScalarSemantics: isScalarSemantics)
451+
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .bitset, isScalarSemantics: isScalarSemantics)
452452
}
453453

454454
init(
455455
asciiChar: UInt8,
456456
_ kind: AST.Quantification.Kind,
457457
_ minTrips: Int,
458-
_ extraTrips: Int?,
458+
_ maxExtraTrips: Int?,
459459
isScalarSemantics: Bool
460460
) {
461461
self.rawValue = UInt64(asciiChar)
462-
+ QuantifyPayload.packInfoValues(kind, minTrips, extraTrips, .asciiChar, isScalarSemantics: isScalarSemantics)
462+
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .asciiChar, isScalarSemantics: isScalarSemantics)
463463
}
464464

465465
init(
466466
matchesNewlines: Bool,
467467
_ kind: AST.Quantification.Kind,
468468
_ minTrips: Int,
469-
_ extraTrips: Int?,
469+
_ maxExtraTrips: Int?,
470470
isScalarSemantics: Bool
471471
) {
472472
self.rawValue = (matchesNewlines ? 1 : 0)
473-
+ QuantifyPayload.packInfoValues(kind, minTrips, extraTrips, .any, isScalarSemantics: isScalarSemantics)
473+
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .any, isScalarSemantics: isScalarSemantics)
474474
}
475475

476476
init(
477477
model: _CharacterClassModel,
478478
_ kind: AST.Quantification.Kind,
479479
_ minTrips: Int,
480-
_ extraTrips: Int?,
480+
_ maxExtraTrips: Int?,
481481
isScalarSemantics: Bool
482482
) {
483483
assert(model.cc.rawValue < 0xFF)
484484
let packedModel = model.cc.rawValue
485485
+ (model.isInverted ? 1 << 9 : 0)
486486
+ (model.isStrictASCII ? 1 << 10 : 0)
487487
self.rawValue = packedModel
488-
+ QuantifyPayload.packInfoValues(kind, minTrips, extraTrips, .builtin, isScalarSemantics: isScalarSemantics)
488+
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .builtin, isScalarSemantics: isScalarSemantics)
489489
}
490490

491491
var type: PayloadType {
@@ -506,8 +506,8 @@ struct QuantifyPayload: RawRepresentable {
506506
(self.rawValue >> QuantifyPayload.minTripsShift) & minTripsMask
507507
}
508508

509-
var extraTrips: UInt64? {
510-
let val = (self.rawValue >> QuantifyPayload.extraTripsShift) & extraTripsMask
509+
var maxExtraTrips: UInt64? {
510+
let val = (self.rawValue >> QuantifyPayload.maxExtraTripsShift) & maxExtraTripsMask
511511
if val == 1 {
512512
return nil
513513
} else {

Sources/_StringProcessing/Engine/MEBuilder.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,48 +225,48 @@ extension MEProgram.Builder {
225225
bitset: DSLTree.CustomCharacterClass.AsciiBitset,
226226
_ kind: AST.Quantification.Kind,
227227
_ minTrips: Int,
228-
_ extraTrips: Int?,
228+
_ maxExtraTrips: Int?,
229229
isScalarSemantics: Bool
230230
) {
231231
instructions.append(.init(
232232
.quantify,
233-
.init(quantify: .init(bitset: makeAsciiBitset(bitset), kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics))))
233+
.init(quantify: .init(bitset: makeAsciiBitset(bitset), kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics))))
234234
}
235235

236236
mutating func buildQuantify(
237237
asciiChar: UInt8,
238238
_ kind: AST.Quantification.Kind,
239239
_ minTrips: Int,
240-
_ extraTrips: Int?,
240+
_ maxExtraTrips: Int?,
241241
isScalarSemantics: Bool
242242
) {
243243
instructions.append(.init(
244244
.quantify,
245-
.init(quantify: .init(asciiChar: asciiChar, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics))))
245+
.init(quantify: .init(asciiChar: asciiChar, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics))))
246246
}
247247

248248
mutating func buildQuantifyAny(
249249
matchesNewlines: Bool,
250250
_ kind: AST.Quantification.Kind,
251251
_ minTrips: Int,
252-
_ extraTrips: Int?,
252+
_ maxExtraTrips: Int?,
253253
isScalarSemantics: Bool
254254
) {
255255
instructions.append(.init(
256256
.quantify,
257-
.init(quantify: .init(matchesNewlines: matchesNewlines, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics))))
257+
.init(quantify: .init(matchesNewlines: matchesNewlines, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics))))
258258
}
259259

260260
mutating func buildQuantify(
261261
model: _CharacterClassModel,
262262
_ kind: AST.Quantification.Kind,
263263
_ minTrips: Int,
264-
_ extraTrips: Int?,
264+
_ maxExtraTrips: Int?,
265265
isScalarSemantics: Bool
266266
) {
267267
instructions.append(.init(
268268
.quantify,
269-
.init(quantify: .init(model: model,kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics))))
269+
.init(quantify: .init(model: model,kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics))))
270270
}
271271

272272
mutating func buildAccept() {

0 commit comments

Comments
 (0)