/
RegexCompiler.cs
3460 lines (2985 loc) · 123 KB
/
RegexCompiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="RegexCompiler.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
// The RegexCompiler class is internal to the Regex package.
// It translates a block of RegexCode to MSIL, and creates a
// subclass of the RegexRunner type.
#if !SILVERLIGHT
namespace System.Text.RegularExpressions {
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Versioning;
/*
* RegexDynamicModule
*
* Because dynamic modules are expensive and not thread-safe, we create
* one dynamic module per-thread, and cache as much information about it
* as we can.
*
* While we're at it, we just create one RegexCompiler per thread
* as well, and have RegexCompiler inherit from RegexDynamicModule.
*/
internal abstract class RegexCompiler {
// fields that never change (making them saves about 6% overall running time)
internal static FieldInfo _textbegF;
internal static FieldInfo _textendF;
internal static FieldInfo _textstartF;
internal static FieldInfo _textposF;
internal static FieldInfo _textF;
internal static FieldInfo _trackposF;
internal static FieldInfo _trackF;
internal static FieldInfo _stackposF;
internal static FieldInfo _stackF;
internal static FieldInfo _trackcountF;
// note some methods
internal static MethodInfo _ensurestorageM;
internal static MethodInfo _captureM;
internal static MethodInfo _transferM;
internal static MethodInfo _uncaptureM;
internal static MethodInfo _ismatchedM;
internal static MethodInfo _matchlengthM;
internal static MethodInfo _matchindexM;
internal static MethodInfo _isboundaryM;
internal static MethodInfo _isECMABoundaryM;
internal static MethodInfo _chartolowerM;
internal static MethodInfo _getcharM;
internal static MethodInfo _crawlposM;
internal static MethodInfo _charInSetM;
internal static MethodInfo _getCurrentCulture;
internal static MethodInfo _getInvariantCulture;
internal static MethodInfo _checkTimeoutM;
#if DBG
internal static MethodInfo _dumpstateM;
#endif
internal ILGenerator _ilg;
// tokens representing local variables
internal LocalBuilder _textstartV;
internal LocalBuilder _textbegV;
internal LocalBuilder _textendV;
internal LocalBuilder _textposV;
internal LocalBuilder _textV;
internal LocalBuilder _trackposV;
internal LocalBuilder _trackV;
internal LocalBuilder _stackposV;
internal LocalBuilder _stackV;
internal LocalBuilder _tempV;
internal LocalBuilder _temp2V;
internal LocalBuilder _temp3V;
internal LocalBuilder _loopV;
internal RegexCode _code; // the RegexCode object (used for debugging only)
internal int[] _codes; // the RegexCodes being translated
internal String[] _strings; // the stringtable associated with the RegexCodes
internal RegexPrefix _fcPrefix; // the possible first chars computed by RegexFCD
internal RegexBoyerMoore _bmPrefix; // a prefix as a boyer-moore machine
internal int _anchors; // the set of anchors
internal Label[] _labels; // a label for every operation in _codes
internal BacktrackNote[] _notes; // a list of the backtracking states to be generated
internal int _notecount; // true count of _notes (allocation grows exponentially)
internal int _trackcount; // count of backtracking states (used to reduce allocations)
internal Label _backtrack; // label for backtracking
internal int _regexopcode; // the current opcode being processed
internal int _codepos; // the current code being translated
internal int _backpos; // the current backtrack-note being translated
internal RegexOptions _options; // options
// special code fragments
internal int[] _uniquenote; // _notes indices for code that should be emitted <= once
internal int[] _goto; // indices for forward-jumps-through-switch (for allocations)
// indices for unique code fragments
internal const int stackpop = 0; // pop one
internal const int stackpop2 = 1; // pop two
internal const int stackpop3 = 2; // pop three
internal const int capback = 3; // uncapture
internal const int capback2 = 4; // uncapture 2
internal const int branchmarkback2 = 5; // back2 part of branchmark
internal const int lazybranchmarkback2 = 6; // back2 part of lazybranchmark
internal const int branchcountback2 = 7; // back2 part of branchcount
internal const int lazybranchcountback2 = 8; // back2 part of lazybranchcount
internal const int forejumpback = 9; // back part of forejump
internal const int uniquecount = 10;
private const int LoopTimeoutCheckCount = 2000; // A conservative value to guarantee the correct timeout handling.
private static readonly bool UseLegacyTimeoutCheck = LocalAppContextSwitches.UseLegacyTimeoutCheck;
static RegexCompiler() {
// <SECREVIEW> Regex only generates string manipulation, so this is ok.
// </SECREVIEW>
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try {
// note some fields
_textbegF = RegexRunnerField("runtextbeg");
_textendF = RegexRunnerField("runtextend");
_textstartF = RegexRunnerField("runtextstart");
_textposF = RegexRunnerField("runtextpos");
_textF = RegexRunnerField("runtext");
_trackposF = RegexRunnerField("runtrackpos");
_trackF = RegexRunnerField("runtrack");
_stackposF = RegexRunnerField("runstackpos");
_stackF = RegexRunnerField("runstack");
_trackcountF = RegexRunnerField("runtrackcount");
// note some methods
_ensurestorageM = RegexRunnerMethod("EnsureStorage");
_captureM = RegexRunnerMethod("Capture");
_transferM = RegexRunnerMethod("TransferCapture");
_uncaptureM = RegexRunnerMethod("Uncapture");
_ismatchedM = RegexRunnerMethod("IsMatched");
_matchlengthM = RegexRunnerMethod("MatchLength");
_matchindexM = RegexRunnerMethod("MatchIndex");
_isboundaryM = RegexRunnerMethod("IsBoundary");
_charInSetM = RegexRunnerMethod("CharInClass");
_isECMABoundaryM= RegexRunnerMethod("IsECMABoundary");
_crawlposM = RegexRunnerMethod("Crawlpos");
_checkTimeoutM = RegexRunnerMethod("CheckTimeout");
_chartolowerM = typeof(Char).GetMethod("ToLower", new Type[] {typeof(Char), typeof(CultureInfo)});
_getcharM = typeof(String).GetMethod("get_Chars", new Type[] {typeof(int)});
_getCurrentCulture = typeof(CultureInfo).GetMethod("get_CurrentCulture");
_getInvariantCulture = typeof(CultureInfo).GetMethod("get_InvariantCulture");
#if DBG
_dumpstateM = RegexRunnerMethod("DumpState");
#endif
}
finally {
CodeAccessPermission.RevertAssert();
}
}
private static FieldInfo RegexRunnerField(String fieldname) {
return typeof(RegexRunner).GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
}
private static MethodInfo RegexRunnerMethod(String methname) {
return typeof(RegexRunner).GetMethod(methname, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
}
/*
* Entry point to dynamically compile a regular expression. The expression is compiled to
* an in-memory assembly.
*/
internal static RegexRunnerFactory Compile(RegexCode code, RegexOptions options) {
RegexLWCGCompiler c = new RegexLWCGCompiler();
RegexRunnerFactory factory;
// <SECREVIEW> Regex only generates string manipulation, so this is ok.
// </SECREVIEW>
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try {
factory = c.FactoryInstanceFromCode(code, options);
}
finally {
CodeAccessPermission.RevertAssert();
}
return factory;
}
/*
* Compile regular expressions into an assembly on disk.
*/
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[SuppressMessage("Microsoft.Security","CA2106:SecureAsserts", Justification="Microsoft: SECREVIEW : Regex only generates string manipulation, so this is OK")]
internal static void CompileToAssembly(RegexCompilationInfo[] regexes, AssemblyName an, CustomAttributeBuilder[] attribs, String resourceFile) {
RegexTypeCompiler c = new RegexTypeCompiler(an, attribs, resourceFile);
for (int i=0; i<regexes.Length; i++) {
if (regexes[i] == null) {
throw new ArgumentNullException("regexes", SR.GetString(SR.ArgumentNull_ArrayWithNullElements));
}
String pattern = regexes[i].Pattern;
RegexOptions options = regexes[i].Options;
String fullname;
if (regexes[i].Namespace.Length == 0)
fullname = regexes[i].Name;
else
fullname = regexes[i].Namespace + "." + regexes[i].Name;
TimeSpan mTimeout = regexes[i].MatchTimeout;
RegexTree tree = RegexParser.Parse(pattern, options);
RegexCode code = RegexWriter.Write(tree);
Type factory;
new ReflectionPermission(PermissionState.Unrestricted).Assert();
try {
factory = c.FactoryTypeFromCode(code, options, fullname);
c.GenerateRegexType(pattern, options, fullname, regexes[i].IsPublic, code, tree, factory, mTimeout);
}
finally {
CodeAccessPermission.RevertAssert();
}
}
c.Save();
}
/*
* Keeps track of an operation that needs to be referenced in the backtrack-jump
* switch table, and that needs backtracking code to be emitted (if flags != 0)
*/
internal sealed class BacktrackNote {
internal BacktrackNote(int flags, Label label, int codepos) {
_codepos = codepos;
_flags = flags;
_label = label;
}
internal int _codepos;
internal int _flags;
internal Label _label;
}
/*
* Adds a backtrack note to the list of them, and returns the index of the new
* note (which is also the index for the jump used by the switch table)
*/
internal int AddBacktrackNote(int flags, Label l, int codepos) {
if (_notes == null || _notecount >= _notes.Length) {
BacktrackNote[] newnotes = new BacktrackNote[_notes == null ? 16 : _notes.Length * 2];
if (_notes != null)
System.Array.Copy(_notes, 0, newnotes, 0, _notecount);
_notes = newnotes;
}
_notes[_notecount] = new BacktrackNote(flags, l, codepos);
return _notecount++;
}
/*
* Adds a backtrack note for the current operation; creates a new label for
* where the code will be, and returns the switch index.
*/
internal int AddTrack() {
return AddTrack(RegexCode.Back);
}
/*
* Adds a backtrack note for the current operation; creates a new label for
* where the code will be, and returns the switch index.
*/
internal int AddTrack(int flags) {
return AddBacktrackNote(flags, DefineLabel(), _codepos);
}
/*
* Adds a switchtable entry for the specified position (for the forward
* logic; does not cause backtracking logic to be generated)
*/
internal int AddGoto(int destpos) {
if (_goto[destpos] == -1)
_goto[destpos] = AddBacktrackNote(0, _labels[destpos], destpos);
return _goto[destpos];
}
/*
* Adds a note for backtracking code that only needs to be generated once;
* if it's already marked to be generated, returns the switch index
* for the unique piece of code.
*/
internal int AddUniqueTrack(int i) {
return AddUniqueTrack(i, RegexCode.Back);
}
/*
* Adds a note for backtracking code that only needs to be generated once;
* if it's already marked to be generated, returns the switch index
* for the unique piece of code.
*/
internal int AddUniqueTrack(int i, int flags) {
if (_uniquenote[i] == -1)
_uniquenote[i] = AddTrack(flags);
return _uniquenote[i];
}
/*
* A macro for _ilg.DefineLabel
*/
internal Label DefineLabel() {
return _ilg.DefineLabel();
}
/*
* A macro for _ilg.MarkLabel
*/
internal void MarkLabel(Label l) {
_ilg.MarkLabel(l);
}
/*
* Returns the ith operand of the current operation
*/
internal int Operand(int i) {
return _codes[_codepos + i + 1];
}
/*
* True if the current operation is marked for the leftward direction
*/
internal bool IsRtl() {
return(_regexopcode & RegexCode.Rtl) != 0;
}
/*
* True if the current operation is marked for case insensitive operation
*/
internal bool IsCi() {
return(_regexopcode & RegexCode.Ci) != 0;
}
#if DBG
/*
* True if we need to do the backtrack logic for the current operation
*/
internal bool IsBack() {
return(_regexopcode & RegexCode.Back) != 0;
}
/*
* True if we need to do the second-backtrack logic for the current operation
*/
internal bool IsBack2() {
return(_regexopcode & RegexCode.Back2) != 0;
}
#endif
/*
* Returns the raw regex opcode (masking out Back and Rtl)
*/
internal int Code() {
return _regexopcode & RegexCode.Mask;
}
internal void Ldstr(string str) {
_ilg.Emit(OpCodes.Ldstr, str);
}
/*
* A macro for the various forms of Ldc
*/
internal void Ldc(int i) {
if (i <= 127 && i >= -128)
_ilg.Emit(OpCodes.Ldc_I4_S, (byte)i);
else
_ilg.Emit(OpCodes.Ldc_I4, i);
}
internal void LdcI8(long i) {
if (i <= Int32.MaxValue && i >= Int32.MinValue) {
Ldc((Int32) i);
_ilg.Emit(OpCodes.Conv_I8);
} else {
_ilg.Emit(OpCodes.Ldc_I8, i);
}
}
/*
* A macro for _ilg.Emit(OpCodes.Dup)
*/
internal void Dup() {
_ilg.Emit(OpCodes.Dup);
}
/*
* A macro for _ilg.Emit(OpCodes.Ret)
*/
internal void Ret() {
_ilg.Emit(OpCodes.Ret);
}
/*
* A macro for _ilg.Emit(OpCodes.Rem)
*/
private void Rem()
{
_ilg.Emit(OpCodes.Rem);
}
/*
* A macro for _ilg.Emit(OpCodes.Ceq)
*/
private void Ceq()
{
_ilg.Emit(OpCodes.Ceq);
}
/*
* A macro for _ilg.Emit(OpCodes.Pop)
*/
internal void Pop() {
_ilg.Emit(OpCodes.Pop);
}
/*
* A macro for _ilg.Emit(OpCodes.Add)
*/
internal void Add() {
_ilg.Emit(OpCodes.Add);
}
/*
* A macro for _ilg.Emit(OpCodes.Add); a true flag can turn it into a Sub
*/
internal void Add(bool negate) {
if (negate)
_ilg.Emit(OpCodes.Sub);
else
_ilg.Emit(OpCodes.Add);
}
/*
* A macro for _ilg.Emit(OpCodes.Sub)
*/
internal void Sub() {
_ilg.Emit(OpCodes.Sub);
}
/*
* A macro for _ilg.Emit(OpCodes.Sub); a true flag can turn it into a Add
*/
internal void Sub(bool negate) {
if (negate)
_ilg.Emit(OpCodes.Add);
else
_ilg.Emit(OpCodes.Sub);
}
/*
* A macro for _ilg.Emit(OpCodes.Ldloc);
*/
internal void Ldloc(LocalBuilder lt) {
_ilg.Emit(OpCodes.Ldloc_S, lt);
}
/*
* A macro for _ilg.Emit(OpCodes.Stloc);
*/
internal void Stloc(LocalBuilder lt) {
_ilg.Emit(OpCodes.Stloc_S, lt);
}
/*
* A macro for _ilg.Emit(OpCodes.Ldarg_0);
*/
internal void Ldthis() {
_ilg.Emit(OpCodes.Ldarg_0);
}
/*
* A macro for Ldthis(); Ldfld();
*/
internal void Ldthisfld(FieldInfo ft) {
Ldthis();
_ilg.Emit(OpCodes.Ldfld, ft);
}
/*
* A macro for Ldthis(); Ldfld(); Stloc();
*/
internal void Mvfldloc(FieldInfo ft, LocalBuilder lt) {
Ldthisfld(ft);
Stloc(lt);
}
/*
* A macro for Ldthis(); Ldthisfld(); Stloc();
*/
internal void Mvlocfld(LocalBuilder lt, FieldInfo ft) {
Ldthis();
Ldloc(lt);
Stfld(ft);
}
/*
* A macro for _ilg.Emit(OpCodes.Stfld);
*/
internal void Stfld(FieldInfo ft) {
_ilg.Emit(OpCodes.Stfld, ft);
}
/*
* A macro for _ilg.Emit(OpCodes.Callvirt);
*/
internal void Callvirt(MethodInfo mt) {
_ilg.Emit(OpCodes.Callvirt, mt);
}
/*
* A macro for _ilg.Emit(OpCodes.Call);
*/
internal void Call(MethodInfo mt) {
_ilg.Emit(OpCodes.Call, mt);
}
/*
* A macro for _ilg.Emit(OpCodes.Newobj);
*/
internal void Newobj(ConstructorInfo ct) {
_ilg.Emit(OpCodes.Newobj, ct);
}
/*
* A macro for _ilg.Emit(OpCodes.Brfalse) (long form)
*/
internal void BrfalseFar(Label l) {
_ilg.Emit(OpCodes.Brfalse, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Brtrue) (long form)
*/
internal void BrtrueFar(Label l) {
_ilg.Emit(OpCodes.Brtrue, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Br) (long form)
*/
internal void BrFar(Label l) {
_ilg.Emit(OpCodes.Br, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Ble) (long form)
*/
internal void BleFar(Label l) {
_ilg.Emit(OpCodes.Ble, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Blt) (long form)
*/
internal void BltFar(Label l) {
_ilg.Emit(OpCodes.Blt, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bge) (long form)
*/
internal void BgeFar(Label l) {
_ilg.Emit(OpCodes.Bge, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bgt) (long form)
*/
internal void BgtFar(Label l) {
_ilg.Emit(OpCodes.Bgt, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bne) (long form)
*/
internal void BneFar(Label l) {
_ilg.Emit(OpCodes.Bne_Un, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Beq) (long form)
*/
internal void BeqFar(Label l) {
_ilg.Emit(OpCodes.Beq, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Brfalse_S) (short jump)
*/
internal void Brfalse(Label l) {
_ilg.Emit(OpCodes.Brfalse_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Br_S) (short jump)
*/
internal void Br(Label l) {
_ilg.Emit(OpCodes.Br_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Ble_S) (short jump)
*/
internal void Ble(Label l) {
_ilg.Emit(OpCodes.Ble_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Blt_S) (short jump)
*/
internal void Blt(Label l) {
_ilg.Emit(OpCodes.Blt_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bge_S) (short jump)
*/
internal void Bge(Label l) {
_ilg.Emit(OpCodes.Bge_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bgt_S) (short jump)
*/
internal void Bgt(Label l) {
_ilg.Emit(OpCodes.Bgt_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bleun_S) (short jump)
*/
internal void Bgtun(Label l) {
_ilg.Emit(OpCodes.Bgt_Un_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Bne_S) (short jump)
*/
internal void Bne(Label l) {
_ilg.Emit(OpCodes.Bne_Un_S, l);
}
/*
* A macro for _ilg.Emit(OpCodes.Beq_S) (short jump)
*/
internal void Beq(Label l) {
_ilg.Emit(OpCodes.Beq_S, l);
}
/*
* A macro for the Ldlen instruction
*/
internal void Ldlen() {
_ilg.Emit(OpCodes.Ldlen);
}
/*
* Loads the char to the right of the current position
*/
internal void Rightchar() {
Ldloc(_textV);
Ldloc(_textposV);
Callvirt(_getcharM);
}
/*
* Loads the char to the right of the current position and advances the current position
*/
internal void Rightcharnext() {
Ldloc(_textV);
Ldloc(_textposV);
Dup();
Ldc(1);
Add();
Stloc(_textposV);
Callvirt(_getcharM);
}
/*
* Loads the char to the left of the current position
*/
internal void Leftchar() {
Ldloc(_textV);
Ldloc(_textposV);
Ldc(1);
Sub();
Callvirt(_getcharM);
}
/*
* Loads the char to the left of the current position and advances (leftward)
*/
internal void Leftcharnext() {
Ldloc(_textV);
Ldloc(_textposV);
Ldc(1);
Sub();
Dup();
Stloc(_textposV);
Callvirt(_getcharM);
}
/*
* Creates a backtrack note and pushes the switch index it on the tracking stack
*/
internal void Track() {
ReadyPushTrack();
Ldc(AddTrack());
DoPush();
}
/*
* Pushes the current switch index on the tracking stack so the backtracking
* logic will be repeated again next time we backtrack here.
*
* <
*/
internal void Trackagain() {
ReadyPushTrack();
Ldc(_backpos);
DoPush();
}
/*
* Saves the value of a local variable on the tracking stack
*/
internal void PushTrack(LocalBuilder lt) {
ReadyPushTrack();
Ldloc(lt);
DoPush();
}
/*
* Creates a backtrack note for a piece of code that should only be generated once,
* and emits code that pushes the switch index on the backtracking stack.
*/
internal void TrackUnique(int i) {
ReadyPushTrack();
Ldc(AddUniqueTrack(i));
DoPush();
}
/*
* Creates a second-backtrack note for a piece of code that should only be
* generated once, and emits code that pushes the switch index on the
* backtracking stack.
*/
internal void TrackUnique2(int i) {
ReadyPushTrack();
Ldc(AddUniqueTrack(i, RegexCode.Back2));
DoPush();
}
/*
* Prologue to code that will push an element on the tracking stack
*/
internal void ReadyPushTrack() {
_ilg.Emit(OpCodes.Ldloc_S, _trackV);
_ilg.Emit(OpCodes.Ldloc_S, _trackposV);
_ilg.Emit(OpCodes.Ldc_I4_1);
_ilg.Emit(OpCodes.Sub);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Stloc_S, _trackposV);
}
/*
* Pops an element off the tracking stack (leave it on the operand stack)
*/
internal void PopTrack() {
_ilg.Emit(OpCodes.Ldloc_S, _trackV);
_ilg.Emit(OpCodes.Ldloc_S, _trackposV);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Ldc_I4_1);
_ilg.Emit(OpCodes.Add);
_ilg.Emit(OpCodes.Stloc_S, _trackposV);
_ilg.Emit(OpCodes.Ldelem_I4);
}
/*
* Retrieves the top entry on the tracking stack without popping
*/
internal void TopTrack() {
_ilg.Emit(OpCodes.Ldloc_S, _trackV);
_ilg.Emit(OpCodes.Ldloc_S, _trackposV);
_ilg.Emit(OpCodes.Ldelem_I4);
}
/*
* Saves the value of a local variable on the grouping stack
*/
internal void PushStack(LocalBuilder lt) {
ReadyPushStack();
_ilg.Emit(OpCodes.Ldloc_S, lt);
DoPush();
}
/*
* Prologue to code that will replace the ith element on the grouping stack
*/
internal void ReadyReplaceStack(int i) {
_ilg.Emit(OpCodes.Ldloc_S, _stackV);
_ilg.Emit(OpCodes.Ldloc_S, _stackposV);
if (i != 0) {
Ldc(i);
_ilg.Emit(OpCodes.Add);
}
}
/*
* Prologue to code that will push an element on the grouping stack
*/
internal void ReadyPushStack() {
_ilg.Emit(OpCodes.Ldloc_S, _stackV);
_ilg.Emit(OpCodes.Ldloc_S, _stackposV);
_ilg.Emit(OpCodes.Ldc_I4_1);
_ilg.Emit(OpCodes.Sub);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Stloc_S, _stackposV);
}
/*
* Retrieves the top entry on the stack without popping
*/
internal void TopStack() {
_ilg.Emit(OpCodes.Ldloc_S, _stackV);
_ilg.Emit(OpCodes.Ldloc_S, _stackposV);
_ilg.Emit(OpCodes.Ldelem_I4);
}
/*
* Pops an element off the grouping stack (leave it on the operand stack)
*/
internal void PopStack() {
_ilg.Emit(OpCodes.Ldloc_S, _stackV);
_ilg.Emit(OpCodes.Ldloc_S, _stackposV);
_ilg.Emit(OpCodes.Dup);
_ilg.Emit(OpCodes.Ldc_I4_1);
_ilg.Emit(OpCodes.Add);
_ilg.Emit(OpCodes.Stloc_S, _stackposV);
_ilg.Emit(OpCodes.Ldelem_I4);
}
/*
* Pops 1 element off the grouping stack and discards it
*/
internal void PopDiscardStack() {
PopDiscardStack(1);
}
/*
* Pops i elements off the grouping stack and discards them
*/
internal void PopDiscardStack(int i) {
_ilg.Emit(OpCodes.Ldloc_S, _stackposV);
Ldc(i);
_ilg.Emit(OpCodes.Add);
_ilg.Emit(OpCodes.Stloc_S, _stackposV);
}
/*
* Epilogue to code that will replace an element on a stack (use Ld* in between)
*/
internal void DoReplace() {
_ilg.Emit(OpCodes.Stelem_I4);
}
/*
* Epilogue to code that will push an element on a stack (use Ld* in between)
*/
internal void DoPush() {
_ilg.Emit(OpCodes.Stelem_I4);
}
/*
* Jump to the backtracking switch
*/
internal void Back() {
_ilg.Emit(OpCodes.Br, _backtrack);
}
/*
* Branch to the MSIL corresponding to the regex code at i
*
* A trick: since track and stack space is gobbled up unboundedly
* only as a result of branching backwards, this is where we check
* for sufficient space and trigger reallocations.
*
* If the "goto" is backwards, we generate code that checks
* available space against the amount of space that would be needed
* in the worst case by code that will only go forward; if there's
* not enough, we push the destination on the tracking stack, then
* we jump to the place where we invoke the allocator.
*
* Since forward gotos pose no threat, they just turn into a Br.
*/
internal void Goto(int i) {
if (i < _codepos) {
Label l1 = DefineLabel();
// When going backwards, ensure enough space.
Ldloc(_trackposV);
Ldc(_trackcount * 4);
Ble(l1);
Ldloc(_stackposV);
Ldc(_trackcount * 3);
BgtFar(_labels[i]);
MarkLabel(l1);
ReadyPushTrack();
Ldc(AddGoto(i));
DoPush();
BrFar(_backtrack);
}
else {
BrFar(_labels[i]);
}
}
/*
* Returns the position of the next operation in the regex code, taking
* into account the different numbers of arguments taken by operations
*/
internal int NextCodepos() {
return _codepos + RegexCode.OpcodeSize(_codes[_codepos]);
}
/*
* The label for the next (forward) operation
*/
internal Label AdvanceLabel() {
return _labels[NextCodepos()];
}
/*
* Goto the next (forward) operation
*/
internal void Advance() {
_ilg.Emit(OpCodes.Br, AdvanceLabel());
}
internal void CallToLower()
{
if ((_options & RegexOptions.CultureInvariant) != 0)
Call(_getInvariantCulture);
else
Call(_getCurrentCulture);
Call(_chartolowerM);
}
/*
* Generates the first section of the MSIL. This section contains all
* the forward logic, and corresponds directly to the regex codes.
*
* In the absence of backtracking, this is all we would need.
*/
internal void GenerateForwardSection() {
int codepos;
_labels = new Label[_codes.Length];
_goto = new int[_codes.Length];
// initialize
for (codepos = 0; codepos < _codes.Length; codepos += RegexCode.OpcodeSize(_codes[codepos])) {
_goto[codepos] = -1;