Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MethodBodyStreamEncoder.cs
Go to the documentation of this file.
2
3public readonly struct MethodBodyStreamEncoder
4{
5 public readonly struct MethodBody
6 {
7 public int Offset { get; }
8
9 public Blob Instructions { get; }
10
12
13 internal MethodBody(int bodyOffset, Blob instructions, ExceptionRegionEncoder exceptionRegions)
14 {
15 Offset = bodyOffset;
16 Instructions = instructions;
17 ExceptionRegions = exceptionRegions;
18 }
19 }
20
21 public BlobBuilder Builder { get; }
22
24 {
25 if (builder == null)
26 {
28 }
29 if (builder.Count % 4 != 0)
30 {
31 throw new ArgumentException(System.SR.BuilderMustAligned, "builder");
32 }
33 Builder = builder;
34 }
35
36 public MethodBody AddMethodBody(int codeSize, int maxStack, int exceptionRegionCount, bool hasSmallExceptionRegions, StandaloneSignatureHandle localVariablesSignature, MethodBodyAttributes attributes)
37 {
38 return AddMethodBody(codeSize, maxStack, exceptionRegionCount, hasSmallExceptionRegions, localVariablesSignature, attributes, hasDynamicStackAllocation: false);
39 }
40
41 public MethodBody AddMethodBody(int codeSize, int maxStack = 8, int exceptionRegionCount = 0, bool hasSmallExceptionRegions = true, StandaloneSignatureHandle localVariablesSignature = default(StandaloneSignatureHandle), MethodBodyAttributes attributes = MethodBodyAttributes.InitLocals, bool hasDynamicStackAllocation = false)
42 {
43 if (codeSize < 0)
44 {
45 Throw.ArgumentOutOfRange("codeSize");
46 }
47 if ((uint)maxStack > 65535u)
48 {
49 Throw.ArgumentOutOfRange("maxStack");
50 }
52 {
53 Throw.ArgumentOutOfRange("exceptionRegionCount");
54 }
55 int bodyOffset = SerializeHeader(codeSize, (ushort)maxStack, exceptionRegionCount, attributes, localVariablesSignature, hasDynamicStackAllocation);
56 Blob instructions = Builder.ReserveBytes(codeSize);
57 ExceptionRegionEncoder exceptionRegions = ((exceptionRegionCount > 0) ? ExceptionRegionEncoder.SerializeTableHeader(Builder, exceptionRegionCount, hasSmallExceptionRegions) : default(ExceptionRegionEncoder));
58 return new MethodBody(bodyOffset, instructions, exceptionRegions);
59 }
60
61 public int AddMethodBody(InstructionEncoder instructionEncoder, int maxStack, StandaloneSignatureHandle localVariablesSignature, MethodBodyAttributes attributes)
62 {
63 return AddMethodBody(instructionEncoder, maxStack, localVariablesSignature, attributes, hasDynamicStackAllocation: false);
64 }
65
66 public int AddMethodBody(InstructionEncoder instructionEncoder, int maxStack = 8, StandaloneSignatureHandle localVariablesSignature = default(StandaloneSignatureHandle), MethodBodyAttributes attributes = MethodBodyAttributes.InitLocals, bool hasDynamicStackAllocation = false)
67 {
68 if ((uint)maxStack > 65535u)
69 {
70 Throw.ArgumentOutOfRange("maxStack");
71 }
72 BlobBuilder codeBuilder = instructionEncoder.CodeBuilder;
73 ControlFlowBuilder controlFlowBuilder = instructionEncoder.ControlFlowBuilder;
74 if (codeBuilder == null)
75 {
76 Throw.ArgumentNull("instructionEncoder");
77 }
78 int exceptionRegionCount = controlFlowBuilder?.ExceptionHandlerCount ?? 0;
80 {
82 }
83 int result = SerializeHeader(codeBuilder.Count, (ushort)maxStack, exceptionRegionCount, attributes, localVariablesSignature, hasDynamicStackAllocation);
84 if (controlFlowBuilder != null && controlFlowBuilder.BranchCount > 0)
85 {
86 controlFlowBuilder.CopyCodeAndFixupBranches(codeBuilder, Builder);
87 }
88 else
89 {
90 codeBuilder.WriteContentTo(Builder);
91 }
92 controlFlowBuilder?.SerializeExceptionTable(Builder);
93 return result;
94 }
95
96 private int SerializeHeader(int codeSize, ushort maxStack, int exceptionRegionCount, MethodBodyAttributes attributes, StandaloneSignatureHandle localVariablesSignature, bool hasDynamicStackAllocation)
97 {
98 bool flag = (attributes & MethodBodyAttributes.InitLocals) != 0;
99 int count;
100 if (codeSize < 64 && maxStack <= 8 && localVariablesSignature.IsNil && (!hasDynamicStackAllocation || !flag) && exceptionRegionCount == 0)
101 {
103 Builder.WriteByte((byte)((uint)(codeSize << 2) | 2u));
104 }
105 else
106 {
107 Builder.Align(4);
109 ushort num = 12291;
110 if (exceptionRegionCount > 0)
111 {
112 num = (ushort)(num | 8u);
113 }
114 if (flag)
115 {
116 num = (ushort)(num | 0x10u);
117 }
118 Builder.WriteUInt16((ushort)((uint)attributes | (uint)num));
119 Builder.WriteUInt16(maxStack);
120 Builder.WriteInt32(codeSize);
121 Builder.WriteInt32((!localVariablesSignature.IsNil) ? MetadataTokens.GetToken(localVariablesSignature) : 0);
122 }
123 return count;
124 }
125}
void WriteContentTo(Stream destination)
void CopyCodeAndFixupBranches(BlobBuilder srcBuilder, BlobBuilder dstBuilder)
static int GetToken(this MetadataReader reader, EntityHandle handle)
static void ArgumentNull(string parameterName)
Definition Throw.cs:110
static void ArgumentOutOfRange(string parameterName)
Definition Throw.cs:145
static void BuilderArgumentNull()
Definition Throw.cs:138
static string TooManyExceptionRegions
Definition SR.cs:148
static string BuilderMustAligned
Definition SR.cs:180
Definition SR.cs:7
static bool IsExceptionRegionCountInBounds(int exceptionRegionCount)
static ExceptionRegionEncoder SerializeTableHeader(BlobBuilder builder, int exceptionRegionCount, bool hasSmallRegions)
MethodBody(int bodyOffset, Blob instructions, ExceptionRegionEncoder exceptionRegions)
MethodBody AddMethodBody(int codeSize, int maxStack=8, int exceptionRegionCount=0, bool hasSmallExceptionRegions=true, StandaloneSignatureHandle localVariablesSignature=default(StandaloneSignatureHandle), MethodBodyAttributes attributes=MethodBodyAttributes.InitLocals, bool hasDynamicStackAllocation=false)
int AddMethodBody(InstructionEncoder instructionEncoder, int maxStack, StandaloneSignatureHandle localVariablesSignature, MethodBodyAttributes attributes)
int SerializeHeader(int codeSize, ushort maxStack, int exceptionRegionCount, MethodBodyAttributes attributes, StandaloneSignatureHandle localVariablesSignature, bool hasDynamicStackAllocation)
int AddMethodBody(InstructionEncoder instructionEncoder, int maxStack=8, StandaloneSignatureHandle localVariablesSignature=default(StandaloneSignatureHandle), MethodBodyAttributes attributes=MethodBodyAttributes.InitLocals, bool hasDynamicStackAllocation=false)
MethodBody AddMethodBody(int codeSize, int maxStack, int exceptionRegionCount, bool hasSmallExceptionRegions, StandaloneSignatureHandle localVariablesSignature, MethodBodyAttributes attributes)