This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
forked from gluck/ikvm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CodeEmitter.cs
2322 lines (2183 loc) · 58 KB
/
CodeEmitter.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 (C) 2002-2010 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
//#define LABELCHECK
using System;
using System.Collections.Generic;
#if STATIC_COMPILER
using IKVM.Reflection;
using IKVM.Reflection.Emit;
using Type = IKVM.Reflection.Type;
#else
using System.Reflection;
using System.Reflection.Emit;
#endif
using System.Runtime.InteropServices;
using System.Diagnostics.SymbolStore;
using System.Diagnostics;
namespace IKVM.Internal
{
sealed class CodeEmitterLabel
{
internal readonly Label Label;
internal int Temp;
internal CodeEmitterLabel(Label label)
{
this.Label = label;
}
}
sealed class CodeEmitterLocal
{
private Type type;
private string name;
private LocalBuilder local;
internal CodeEmitterLocal(Type type)
{
this.type = type;
}
internal Type LocalType
{
get { return type; }
}
internal void SetLocalSymInfo(string name)
{
this.name = name;
}
internal int __LocalIndex
{
get { return local == null ? 0xFFFF : local.LocalIndex; }
}
internal void Emit(ILGenerator ilgen, OpCode opcode)
{
if (local == null)
{
// it's a temporary local that is only allocated on-demand
local = ilgen.DeclareLocal(type);
}
ilgen.Emit(opcode, local);
}
internal void Declare(ILGenerator ilgen)
{
local = ilgen.DeclareLocal(type);
if (name != null)
{
local.SetLocalSymInfo(name);
}
}
}
sealed class CodeEmitter
{
private static readonly MethodInfo objectToString = Types.Object.GetMethod("ToString", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
private static readonly MethodInfo verboseCastFailure = JVM.SafeGetEnvironmentVariable("IKVM_VERBOSE_CAST") == null ? null : ByteCodeHelperMethods.VerboseCastFailure;
private static readonly bool experimentalOptimizations = JVM.SafeGetEnvironmentVariable("IKVM_EXPERIMENTAL_OPTIMIZATIONS") != null;
private static MethodInfo memoryBarrier;
private ILGenerator ilgen_real;
#if !STATIC_COMPILER
private bool inFinally;
private Stack<bool> exceptionStack = new Stack<bool>();
#endif
private IKVM.Attributes.LineNumberTableAttribute.LineNumberWriter linenums;
private CodeEmitterLocal[] tempLocals = new CodeEmitterLocal[32];
private ISymbolDocumentWriter symbols;
private List<OpCodeWrapper> code = new List<OpCodeWrapper>(10);
#if LABELCHECK
private Dictionary<CodeEmitterLabel, System.Diagnostics.StackFrame> labels = new Dictionary<CodeEmitterLabel, System.Diagnostics.StackFrame>();
#endif
static CodeEmitter()
{
if (experimentalOptimizations)
{
Console.Error.WriteLine("IKVM.NET experimental optimizations enabled.");
}
}
enum CodeType : short
{
Unreachable,
OpCode,
BeginScope,
EndScope,
DeclareLocal,
ReleaseTempLocal,
SequencePoint,
LineNumber,
Label,
BeginExceptionBlock,
BeginCatchBlock,
BeginFaultBlock,
BeginFinallyBlock,
EndExceptionBlock,
MemoryBarrier,
}
enum CodeTypeFlags : short
{
None = 0,
EndFaultOrFinally = 1,
}
struct OpCodeWrapper
{
internal readonly CodeType pseudo;
private readonly CodeTypeFlags flags;
internal readonly OpCode opcode;
private readonly object data;
internal OpCodeWrapper(CodeType pseudo, object data)
{
this.pseudo = pseudo;
this.flags = CodeTypeFlags.None;
this.opcode = OpCodes.Nop;
this.data = data;
}
internal OpCodeWrapper(CodeType pseudo, CodeTypeFlags flags)
{
this.pseudo = pseudo;
this.flags = flags;
this.opcode = OpCodes.Nop;
this.data = null;
}
internal OpCodeWrapper(OpCode opcode, object data)
{
this.pseudo = CodeType.OpCode;
this.flags = CodeTypeFlags.None;
this.opcode = opcode;
this.data = data;
}
internal bool Match(OpCodeWrapper other)
{
return other.pseudo == pseudo
&& other.opcode == opcode
&& (other.data == data || (data != null && data.Equals(other.data)));
}
internal bool HasLabel
{
get { return data is CodeEmitterLabel; }
}
internal CodeEmitterLabel Label
{
get { return (CodeEmitterLabel)data; }
}
internal bool MatchLabel(OpCodeWrapper other)
{
return data == other.data;
}
internal CodeEmitterLabel[] Labels
{
get { return (CodeEmitterLabel[])data; }
}
internal CodeEmitterLocal Local
{
get { return (CodeEmitterLocal)data; }
}
internal bool MatchLocal(OpCodeWrapper other)
{
return data == other.data;
}
internal bool HasValueByte
{
get { return data is byte; }
}
internal byte ValueByte
{
get { return (byte)data; }
}
internal short ValueInt16
{
get { return (short)data; }
}
internal int ValueInt32
{
get { return (int)data; }
}
internal long ValueInt64
{
get { return (long)data; }
}
internal Type Type
{
get { return (Type)data; }
}
internal FieldInfo FieldInfo
{
get { return (FieldInfo)data; }
}
internal int Size
{
get
{
switch (pseudo)
{
case CodeType.Unreachable:
case CodeType.BeginScope:
case CodeType.EndScope:
case CodeType.DeclareLocal:
case CodeType.ReleaseTempLocal:
case CodeType.LineNumber:
case CodeType.Label:
case CodeType.BeginExceptionBlock:
return 0;
case CodeType.SequencePoint:
return 1;
case CodeType.BeginCatchBlock:
case CodeType.BeginFaultBlock:
case CodeType.BeginFinallyBlock:
case CodeType.EndExceptionBlock:
#if STATIC_COMPILER
return 0;
#else
if ((flags & CodeTypeFlags.EndFaultOrFinally) != 0)
{
return 1 + 2;
}
return 5 + 2;
#endif
case CodeType.MemoryBarrier:
return 5;
case CodeType.OpCode:
if (data == null)
{
return opcode.Size;
}
else if (data is int)
{
return opcode.Size + 4;;
}
else if (data is long)
{
return opcode.Size + 8;
}
else if (data is MethodInfo)
{
return opcode.Size + 4;
}
else if (data is ConstructorInfo)
{
return opcode.Size + 4;
}
else if (data is FieldInfo)
{
return opcode.Size + 4;
}
else if (data is sbyte)
{
return opcode.Size + 1;
}
else if (data is byte)
{
return opcode.Size + 1;
}
else if (data is short)
{
return opcode.Size + 2;
}
else if (data is float)
{
return opcode.Size + 4;
}
else if (data is double)
{
return opcode.Size + 8;
}
else if (data is string)
{
return opcode.Size + 4;
}
else if (data is Type)
{
return opcode.Size + 4;
}
else if (data is CodeEmitterLocal)
{
int index = ((CodeEmitterLocal)data).__LocalIndex;
if(index < 4 && opcode.Value != OpCodes.Ldloca.Value && opcode.Value != OpCodes.Ldloca_S.Value)
{
return 1;
}
else if(index < 256)
{
return 2;
}
else
{
return 4;
}
}
else if (data is CodeEmitterLabel)
{
switch(opcode.OperandType)
{
case OperandType.InlineBrTarget:
return opcode.Size + 4;
case OperandType.ShortInlineBrTarget:
return opcode.Size + 1;
default:
throw new InvalidOperationException();
}
}
else if (data is CodeEmitterLabel[])
{
return 5 + ((CodeEmitterLabel[])data).Length * 4;
}
else if (data is CalliWrapper)
{
return 5;
}
else
{
throw new InvalidOperationException();
}
default:
throw new InvalidOperationException();
}
}
}
internal void RealEmit(int ilOffset, CodeEmitter codeEmitter, ref int lineNumber)
{
if (pseudo == CodeType.OpCode)
{
if (lineNumber != -1)
{
if (codeEmitter.linenums == null)
{
codeEmitter.linenums = new IKVM.Attributes.LineNumberTableAttribute.LineNumberWriter(32);
}
codeEmitter.linenums.AddMapping(ilOffset, lineNumber);
lineNumber = -1;
}
codeEmitter.RealEmitOpCode(opcode, data);
}
else if (pseudo == CodeType.LineNumber)
{
lineNumber = (int)data;
}
else
{
codeEmitter.RealEmitPseudoOpCode(ilOffset, pseudo, data);
}
}
public override string ToString()
{
return pseudo.ToString() + " " + data;
}
}
sealed class CalliWrapper
{
internal readonly CallingConvention unmanagedCallConv;
internal readonly Type returnType;
internal readonly Type[] parameterTypes;
internal CalliWrapper(CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
{
this.unmanagedCallConv = unmanagedCallConv;
this.returnType = returnType;
this.parameterTypes = parameterTypes == null ? null : (Type[])parameterTypes.Clone();
}
}
internal static CodeEmitter Create(MethodBuilder mb)
{
return new CodeEmitter(mb.GetILGenerator());
}
internal static CodeEmitter Create(ConstructorBuilder cb)
{
return new CodeEmitter(cb.GetILGenerator());
}
#if !STATIC_COMPILER
internal static CodeEmitter Create(DynamicMethod dm)
{
return new CodeEmitter(dm.GetILGenerator());
}
#endif
private CodeEmitter(ILGenerator ilgen)
{
#if STATIC_COMPILER
ilgen.__DisableExceptionBlockAssistance();
#endif
this.ilgen_real = ilgen;
}
private void EmitPseudoOpCode(CodeType type, object data)
{
code.Add(new OpCodeWrapper(type, data));
}
private void EmitOpCode(OpCode opcode, object arg)
{
code.Add(new OpCodeWrapper(opcode, arg));
}
private void RealEmitPseudoOpCode(int ilOffset, CodeType type, object data)
{
switch (type)
{
case CodeType.Unreachable:
break;
case CodeType.BeginScope:
ilgen_real.BeginScope();
break;
case CodeType.EndScope:
ilgen_real.EndScope();
break;
case CodeType.DeclareLocal:
((CodeEmitterLocal)data).Declare(ilgen_real);
break;
case CodeType.ReleaseTempLocal:
break;
case CodeType.SequencePoint:
ilgen_real.MarkSequencePoint(symbols, (int)data, 0, (int)data + 1, 0);
// we emit a nop to make sure we always have an instruction associated with the sequence point
ilgen_real.Emit(OpCodes.Nop);
break;
case CodeType.Label:
ilgen_real.MarkLabel(((CodeEmitterLabel)data).Label);
break;
case CodeType.BeginExceptionBlock:
ilgen_real.BeginExceptionBlock();
break;
case CodeType.BeginCatchBlock:
ilgen_real.BeginCatchBlock((Type)data);
break;
case CodeType.BeginFaultBlock:
ilgen_real.BeginFaultBlock();
break;
case CodeType.BeginFinallyBlock:
ilgen_real.BeginFinallyBlock();
break;
case CodeType.EndExceptionBlock:
ilgen_real.EndExceptionBlock();
#if !STATIC_COMPILER
// HACK to keep the verifier happy we need this bogus jump
// (because of the bogus Leave that Ref.Emit ends the try block with)
ilgen_real.Emit(OpCodes.Br_S, (sbyte)-2);
#endif
break;
case CodeType.MemoryBarrier:
if (memoryBarrier == null)
{
memoryBarrier = JVM.Import(typeof(System.Threading.Thread)).GetMethod("MemoryBarrier", Type.EmptyTypes);
}
ilgen_real.Emit(OpCodes.Call, memoryBarrier);
break;
default:
throw new InvalidOperationException();
}
}
private void RealEmitOpCode(OpCode opcode, object arg)
{
if (arg == null)
{
ilgen_real.Emit(opcode);
}
else if (arg is int)
{
ilgen_real.Emit(opcode, (int)arg);
}
else if (arg is long)
{
ilgen_real.Emit(opcode, (long)arg);
}
else if (arg is MethodInfo)
{
ilgen_real.Emit(opcode, (MethodInfo)arg);
}
else if (arg is ConstructorInfo)
{
ilgen_real.Emit(opcode, (ConstructorInfo)arg);
}
else if (arg is FieldInfo)
{
ilgen_real.Emit(opcode, (FieldInfo)arg);
}
else if (arg is sbyte)
{
ilgen_real.Emit(opcode, (sbyte)arg);
}
else if (arg is byte)
{
ilgen_real.Emit(opcode, (byte)arg);
}
else if (arg is short)
{
ilgen_real.Emit(opcode, (short)arg);
}
else if (arg is float)
{
ilgen_real.Emit(opcode, (float)arg);
}
else if (arg is double)
{
ilgen_real.Emit(opcode, (double)arg);
}
else if (arg is string)
{
ilgen_real.Emit(opcode, (string)arg);
}
else if (arg is Type)
{
ilgen_real.Emit(opcode, (Type)arg);
}
else if (arg is CodeEmitterLocal)
{
CodeEmitterLocal local = (CodeEmitterLocal)arg;
local.Emit(ilgen_real, opcode);
}
else if (arg is CodeEmitterLabel)
{
CodeEmitterLabel label = (CodeEmitterLabel)arg;
ilgen_real.Emit(opcode, label.Label);
}
else if (arg is CodeEmitterLabel[])
{
CodeEmitterLabel[] labels = (CodeEmitterLabel[])arg;
Label[] real = new Label[labels.Length];
for (int i = 0; i < labels.Length; i++)
{
real[i] = labels[i].Label;
}
ilgen_real.Emit(opcode, real);
}
else if (arg is CalliWrapper)
{
CalliWrapper args = (CalliWrapper)arg;
ilgen_real.EmitCalli(opcode, args.unmanagedCallConv, args.returnType, args.parameterTypes);
}
else
{
throw new InvalidOperationException();
}
}
private void RemoveJumpNext()
{
for (int i = 1; i < code.Count; i++)
{
if (code[i].pseudo == CodeType.Label)
{
if (code[i - 1].opcode == OpCodes.Br
&& code[i - 1].MatchLabel(code[i]))
{
code.RemoveAt(i - 1);
i--;
}
else if (i >= 2
&& code[i - 1].pseudo == CodeType.LineNumber
&& code[i - 2].opcode == OpCodes.Br
&& code[i - 2].MatchLabel(code[i]))
{
code.RemoveAt(i - 2);
i--;
}
}
}
}
private void AnnihilateStoreReleaseTempLocals()
{
for (int i = 1; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Stloc
&& code[i + 1].pseudo == CodeType.ReleaseTempLocal
&& code[i].Local == code[i + 1].Local)
{
code.RemoveRange(i, 1);
code[i] = new OpCodeWrapper(OpCodes.Pop, null);
}
}
}
private void AnnihilatePops()
{
for (int i = 1; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Pop
&& IsSideEffectFreePush(i - 1))
{
code.RemoveRange(i - 1, 2);
i -= 2;
}
}
}
private bool IsSideEffectFreePush(int index)
{
if (code[index].opcode == OpCodes.Ldstr)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldnull)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldsfld)
{
// Here we are considering BeforeFieldInit to mean that we really don't care about
// when the type is initialized (which is what we mean in the rest of the IKVM code as well)
// but it is good to point it out here because strictly speaking we're violating the
// BeforeFieldInit contract here by considering dummy loads not to be field accesses.
FieldInfo field = code[index].FieldInfo;
if (field != null && (field.DeclaringType.Attributes & TypeAttributes.BeforeFieldInit) != 0)
{
return true;
}
else
{
return false;
}
}
else if (code[index].opcode == OpCodes.Ldc_I4)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldc_I8)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldc_R4)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldc_R8)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldloc)
{
return true;
}
else if (code[index].opcode == OpCodes.Ldarg)
{
return true;
}
else
{
return false;
}
}
private void OptimizeBranchSizes()
{
int offset = 0;
for (int i = 0; i < code.Count; i++)
{
if (code[i].pseudo == CodeType.Label)
{
code[i].Label.Temp = offset;
}
offset += code[i].Size;
}
offset = 0;
for (int i = 0; i < code.Count; i++)
{
int prevOffset = offset;
offset += code[i].Size;
if (code[i].HasLabel && code[i].opcode.OperandType == OperandType.InlineBrTarget)
{
CodeEmitterLabel label = code[i].Label;
int diff = label.Temp - (prevOffset + code[i].opcode.Size + 1);
if (-128 <= diff && diff <= 127)
{
OpCode opcode = code[i].opcode;
if (opcode == OpCodes.Brtrue)
{
opcode = OpCodes.Brtrue_S;
}
else if (opcode == OpCodes.Brfalse)
{
opcode = OpCodes.Brfalse_S;
}
else if (opcode == OpCodes.Br)
{
opcode = OpCodes.Br_S;
}
else if (opcode == OpCodes.Beq)
{
opcode = OpCodes.Beq_S;
}
else if (opcode == OpCodes.Bne_Un)
{
opcode = OpCodes.Bne_Un_S;
}
else if (opcode == OpCodes.Ble)
{
opcode = OpCodes.Ble_S;
}
else if (opcode == OpCodes.Ble_Un)
{
opcode = OpCodes.Ble_Un_S;
}
else if (opcode == OpCodes.Blt)
{
opcode = OpCodes.Blt_S;
}
else if (opcode == OpCodes.Blt_Un)
{
opcode = OpCodes.Blt_Un_S;
}
else if (opcode == OpCodes.Bge)
{
opcode = OpCodes.Bge_S;
}
else if (opcode == OpCodes.Bge_Un)
{
opcode = OpCodes.Bge_Un_S;
}
else if (opcode == OpCodes.Bgt)
{
opcode = OpCodes.Bgt_S;
}
else if (opcode == OpCodes.Bgt_Un)
{
opcode = OpCodes.Bgt_Un_S;
}
else if (opcode == OpCodes.Leave)
{
opcode = OpCodes.Leave_S;
}
code[i] = new OpCodeWrapper(opcode, label);
}
}
}
}
private void OptimizePatterns()
{
UpdateLabelRefCounts();
for (int i = 1; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Isinst
&& code[i + 1].opcode == OpCodes.Ldnull
&& code[i + 2].opcode == OpCodes.Cgt_Un
&& (code[i + 3].opcode == OpCodes.Brfalse || code[i + 3].opcode == OpCodes.Brtrue))
{
code.RemoveRange(i + 1, 2);
}
else if (code[i].opcode == OpCodes.Ldelem_I1
&& code[i + 1].opcode == OpCodes.Ldc_I4 && code[i + 1].ValueInt32 == 255
&& code[i + 2].opcode == OpCodes.And)
{
code[i] = new OpCodeWrapper(OpCodes.Ldelem_U1, null);
code.RemoveRange(i + 1, 2);
}
else if (code[i].opcode == OpCodes.Ldelem_I1
&& code[i + 1].opcode == OpCodes.Conv_I8
&& code[i + 2].opcode == OpCodes.Ldc_I8 && code[i + 2].ValueInt64 == 255
&& code[i + 3].opcode == OpCodes.And)
{
code[i] = new OpCodeWrapper(OpCodes.Ldelem_U1, null);
code.RemoveRange(i + 2, 2);
}
else if (code[i].opcode == OpCodes.Ldc_I4
&& code[i + 1].opcode == OpCodes.Ldc_I4
&& code[i + 2].opcode == OpCodes.And)
{
code[i] = new OpCodeWrapper(OpCodes.Ldc_I4, code[i].ValueInt32 & code[i + 1].ValueInt32);
code.RemoveRange(i + 1, 2);
}
else if (MatchCompare(i, OpCodes.Cgt, OpCodes.Clt_Un, Types.Double) // dcmpl
|| MatchCompare(i, OpCodes.Cgt, OpCodes.Clt_Un, Types.Single)) // fcmpl
{
PatchCompare(i, OpCodes.Ble_Un, OpCodes.Blt_Un, OpCodes.Bge, OpCodes.Bgt);
}
else if (MatchCompare(i, OpCodes.Cgt_Un, OpCodes.Clt, Types.Double) // dcmpg
|| MatchCompare(i, OpCodes.Cgt_Un, OpCodes.Clt, Types.Single)) // fcmpg
{
PatchCompare(i, OpCodes.Ble, OpCodes.Blt, OpCodes.Bge_Un, OpCodes.Bgt_Un);
}
else if (MatchCompare(i, OpCodes.Cgt, OpCodes.Clt, Types.Int64)) // lcmp
{
PatchCompare(i, OpCodes.Ble, OpCodes.Blt, OpCodes.Bge, OpCodes.Bgt);
}
else if (i < code.Count - 10
&& code[i].opcode == OpCodes.Ldc_I4
&& code[i + 1].opcode == OpCodes.Dup
&& code[i + 2].opcode == OpCodes.Ldc_I4_M1
&& code[i + 3].opcode == OpCodes.Bne_Un_S
&& code[i + 4].opcode == OpCodes.Pop
&& code[i + 5].opcode == OpCodes.Neg
&& code[i + 6].opcode == OpCodes.Br_S
&& code[i + 7].pseudo == CodeType.Label && code[i + 7].MatchLabel(code[i + 3]) && code[i + 7].Label.Temp == 1
&& code[i + 8].opcode == OpCodes.Div
&& code[i + 9].pseudo == CodeType.Label && code[i + 9].Label == code[i + 6].Label && code[i + 9].Label.Temp == 1)
{
int divisor = code[i].ValueInt32;
if (divisor == -1)
{
code[i] = code[i + 5];
code.RemoveRange(i + 1, 9);
}
else
{
code[i + 1] = code[i + 8];
code.RemoveRange(i + 2, 8);
}
}
else if (i < code.Count - 11
&& code[i].opcode == OpCodes.Ldc_I8
&& code[i + 1].opcode == OpCodes.Dup
&& code[i + 2].opcode == OpCodes.Ldc_I4_M1
&& code[i + 3].opcode == OpCodes.Conv_I8
&& code[i + 4].opcode == OpCodes.Bne_Un_S
&& code[i + 5].opcode == OpCodes.Pop
&& code[i + 6].opcode == OpCodes.Neg
&& code[i + 7].opcode == OpCodes.Br_S
&& code[i + 8].pseudo == CodeType.Label && code[i + 8].MatchLabel(code[i + 4]) && code[i + 8].Label.Temp == 1
&& code[i + 9].opcode == OpCodes.Div
&& code[i + 10].pseudo == CodeType.Label && code[i + 10].MatchLabel(code[i + 7]) && code[i + 10].Label.Temp == 1)
{
long divisor = code[i].ValueInt64;
if (divisor == -1)
{
code[i] = code[i + 6];
code.RemoveRange(i + 1, 10);
}
else
{
code[i + 1] = code[i + 9];
code.RemoveRange(i + 2, 9);
}
}
else if (code[i].opcode == OpCodes.Box
&& code[i + 1].opcode == OpCodes.Unbox && code[i + 1].Type == code[i].Type)
{
CodeEmitterLocal local = new CodeEmitterLocal(code[i].Type);
code[i] = new OpCodeWrapper(OpCodes.Stloc, local);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldloca, local);
}
else if (i < code.Count - 13
&& code[i + 0].opcode == OpCodes.Box
&& code[i + 1].opcode == OpCodes.Dup
&& code[i + 2].opcode == OpCodes.Brtrue_S
&& code[i + 3].opcode == OpCodes.Pop
&& code[i + 4].opcode == OpCodes.Ldloca && code[i + 4].Local.LocalType == code[i + 0].Type
&& code[i + 5].opcode == OpCodes.Initobj && code[i + 5].Type == code[i + 0].Type
&& code[i + 6].opcode == OpCodes.Ldloc && code[i + 6].Local == code[i + 4].Local
&& code[i + 7].pseudo == CodeType.ReleaseTempLocal && code[i + 7].Local == code[i + 6].Local
&& code[i + 8].opcode == OpCodes.Br_S
&& code[i + 9].pseudo == CodeType.Label && code[i + 9].MatchLabel(code[i + 2]) && code[i + 9].Label.Temp == 1
&& code[i + 10].opcode == OpCodes.Unbox && code[i + 10].Type == code[i + 0].Type
&& code[i + 11].opcode == OpCodes.Ldobj && code[i + 11].Type == code[i + 0].Type
&& code[i + 12].pseudo == CodeType.Label && code[i + 12].MatchLabel(code[i + 8]) && code[i + 12].Label.Temp == 1)
{
code.RemoveRange(i, 13);
}
// NOTE intentionally not an else, because we want to optimize the code generated by the earlier compare optimization
if (i < code.Count - 6
&& code[i].opcode.FlowControl == FlowControl.Cond_Branch
&& code[i + 1].opcode == OpCodes.Ldc_I4 && code[i + 1].ValueInt32 == 1
&& code[i + 2].opcode == OpCodes.Br
&& code[i + 3].pseudo == CodeType.Label && code[i + 3].MatchLabel(code[i]) && code[i + 3].Label.Temp == 1
&& code[i + 4].opcode == OpCodes.Ldc_I4 && code[i + 4].ValueInt32 == 0
&& code[i + 5].pseudo == CodeType.Label && code[i + 5].MatchLabel(code[i + 2]) && code[i + 5].Label.Temp == 1)
{
if (code[i].opcode == OpCodes.Bne_Un)
{
code[i] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 1, 5);
}
else if (code[i].opcode == OpCodes.Beq)
{
code[i + 0] = new OpCodeWrapper(OpCodes.Ceq, null);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldc_I4, 0);
code[i + 2] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 3, 3);
}
else if (code[i].opcode == OpCodes.Ble || code[i].opcode == OpCodes.Ble_Un)
{
code[i] = new OpCodeWrapper(OpCodes.Cgt, null);
code.RemoveRange(i + 1, 5);
}
else if (code[i].opcode == OpCodes.Blt)
{
code[i] = new OpCodeWrapper(OpCodes.Clt, null);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldc_I4, 0);
code[i + 2] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 3, 3);
}
else if (code[i].opcode == OpCodes.Blt_Un)
{
code[i] = new OpCodeWrapper(OpCodes.Clt_Un, null);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldc_I4, 0);
code[i + 2] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 3, 3);
}
else if (code[i].opcode == OpCodes.Bge || code[i].opcode == OpCodes.Bge_Un)
{
code[i] = new OpCodeWrapper(OpCodes.Clt, null);
code.RemoveRange(i + 1, 5);
}
else if (code[i].opcode == OpCodes.Bgt)
{
code[i] = new OpCodeWrapper(OpCodes.Cgt, null);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldc_I4, 0);
code[i + 2] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 3, 3);
}
else if (code[i].opcode == OpCodes.Bgt_Un)
{
code[i] = new OpCodeWrapper(OpCodes.Cgt_Un, null);
code[i + 1] = new OpCodeWrapper(OpCodes.Ldc_I4, 0);
code[i + 2] = new OpCodeWrapper(OpCodes.Ceq, null);
code.RemoveRange(i + 3, 3);
}
}
}
}
private bool MatchCompare(int index, OpCode cmp1, OpCode cmp2, Type type)
{
return code[index].opcode == OpCodes.Stloc && code[index].Local.LocalType == type
&& code[index + 1].opcode == OpCodes.Stloc && code[index + 1].Local.LocalType == type
&& code[index + 2].opcode == OpCodes.Ldloc && code[index + 2].MatchLocal(code[index + 1])
&& code[index + 3].opcode == OpCodes.Ldloc && code[index + 3].MatchLocal(code[index])
&& code[index + 4].opcode == cmp1
&& code[index + 5].opcode == OpCodes.Ldloc && code[index + 5].MatchLocal(code[index + 1])
&& code[index + 6].opcode == OpCodes.Ldloc && code[index + 6].MatchLocal(code[index])
&& code[index + 7].opcode == cmp2
&& code[index + 8].opcode == OpCodes.Sub
&& code[index + 9].pseudo == CodeType.ReleaseTempLocal && code[index + 9].Local == code[index].Local
&& code[index + 10].pseudo == CodeType.ReleaseTempLocal && code[index + 10].Local == code[index + 1].Local
&& ((code[index + 11].opcode.FlowControl == FlowControl.Cond_Branch && code[index + 11].HasLabel) ||
(code[index + 11].opcode == OpCodes.Ldc_I4_0
&& (code[index + 12].opcode.FlowControl == FlowControl.Cond_Branch && code[index + 12].HasLabel)));
}