Skip to content

Commit fae0be3

Browse files
committed
simplify things before merging ILDecoder into FEC
1 parent 4113e9d commit fae0be3

File tree

3 files changed

+41
-55
lines changed

3 files changed

+41
-55
lines changed

src/FastExpressionCompiler.ILDecoder/ILReader.cs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,92 +77,77 @@ private ILInstruction Next(ref int position)
7777
case OperandType.ShortInlineBrTarget:
7878
{
7979
// 8-bit integer branch target
80-
var shortDelta = ReadSByte(ref position);
81-
return new ShortInlineBrTargetInstruction(offset, opCode, shortDelta);
80+
return new ShortInlineBrTargetInstruction(offset, opCode, ReadSByte(ref position));
8281
}
8382
case OperandType.InlineBrTarget:
8483
{
8584
// 32-bit integer branch target
86-
var delta = ReadInt32(ref position);
87-
return new InlineBrTargetInstruction(offset, opCode, delta);
85+
return new InlineBrTargetInstruction(offset, opCode, ReadInt32(ref position));
8886
}
8987
case OperandType.ShortInlineI:
9088
{
9189
// 8-bit integer: 001F ldc.i4.s, FE12 unaligned.
92-
var int8 = ReadByte(ref position);
93-
return new ShortInlineIInstruction(offset, opCode, int8);
90+
return new ShortInlineIInstruction(offset, opCode, ReadByte(ref position));
9491
}
9592
case OperandType.InlineI:
9693
{
9794
// 32-bit integer
98-
var int32 = ReadInt32(ref position);
99-
return new InlineIInstruction(offset, opCode, int32);
95+
return new InlineIInstruction(offset, opCode, ReadInt32(ref position));
10096
}
10197
case OperandType.InlineI8:
10298
{
10399
// 64-bit integer
104-
var int64 = ReadInt64(ref position);
105-
return new InlineI8Instruction(offset, opCode, int64);
100+
return new InlineI8Instruction(offset, opCode, ReadInt64(ref position));
106101
}
107102
case OperandType.ShortInlineR:
108103
{
109104
// 32-bit IEEE floating point number
110-
var float32 = ReadSingle(ref position);
111-
return new ShortInlineRInstruction(offset, opCode, float32);
105+
return new ShortInlineRInstruction(offset, opCode, ReadSingle(ref position));
112106
}
113107
case OperandType.InlineR:
114108
{
115109
// 64-bit IEEE floating point number
116-
var float64 = ReadDouble(ref position);
117-
return new InlineRInstruction(offset, opCode, float64);
110+
return new InlineRInstruction(offset, opCode, ReadDouble(ref position));
118111
}
119112
case OperandType.ShortInlineVar:
120113
{
121114
// 8-bit integer containing the ordinal of a local variable or an argument
122-
var index8 = ReadByte(ref position);
123-
return new ShortInlineVarInstruction(offset, opCode, index8);
115+
return new ShortInlineVarInstruction(offset, opCode, ReadByte(ref position));
124116
}
125117
case OperandType.InlineVar:
126118
{
127119
// 16-bit integer containing the ordinal of a local variable or an argument
128-
var index16 = ReadUInt16(ref position);
129-
return new InlineVarInstruction(offset, opCode, index16);
120+
return new InlineVarInstruction(offset, opCode, ReadUInt16(ref position));
130121
}
131122
case OperandType.InlineString:
132123
{
133124
// 32-bit metadata string token
134-
var token = ReadInt32(ref position);
135-
return new InlineStringInstruction(offset, opCode, token, _resolver);
125+
return new InlineStringInstruction(offset, opCode, ReadInt32(ref position), _resolver);
136126
}
137127
case OperandType.InlineSig:
138128
{
139129
// 32-bit metadata signature token
140-
var token = ReadInt32(ref position);
141-
return new InlineSigInstruction(offset, opCode, token, _resolver);
130+
return new InlineSigInstruction(offset, opCode, ReadInt32(ref position), _resolver);
142131
}
143132
case OperandType.InlineMethod:
144133
{
145134
// 32-bit metadata token
146-
var token = ReadInt32(ref position);
147-
return new InlineMethodInstruction(offset, opCode, token, _resolver);
135+
return new InlineMethodInstruction(offset, opCode, ReadInt32(ref position), _resolver);
148136
}
149137
case OperandType.InlineField:
150138
{
151139
// 32-bit metadata token
152-
var token = ReadInt32(ref position);
153-
return new InlineFieldInstruction(_resolver, offset, opCode, token);
140+
return new InlineFieldInstruction(_resolver, offset, opCode, ReadInt32(ref position));
154141
}
155142
case OperandType.InlineType:
156143
{
157144
// 32-bit metadata token
158-
var token = ReadInt32(ref position);
159-
return new InlineTypeInstruction(offset, opCode, token, _resolver);
145+
return new InlineTypeInstruction(offset, opCode, ReadInt32(ref position), _resolver);
160146
}
161147
case OperandType.InlineTok:
162148
{
163149
// FieldRef, MethodRef, or TypeRef token
164-
var token = ReadInt32(ref position);
165-
return new InlineTokInstruction(offset, opCode, token, _resolver);
150+
return new InlineTokInstruction(offset, opCode, ReadInt32(ref position), _resolver);
166151
}
167152
case OperandType.InlineSwitch:
168153
{
@@ -240,4 +225,4 @@ private double ReadDouble(ref int position)
240225
position += 8;
241226
return value;
242227
}
243-
}
228+
}

src/FastExpressionCompiler.ILDecoder/ILReaderFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,5 @@ public static string TypeToCode(this Type type,
292292

293293
return printType?.Invoke(arrayType ?? type, s.ToString()) ?? s.ToString();
294294
}
295-
}
295+
}
296+

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9083,6 +9083,28 @@ internal static StringBuilder AppendName(this StringBuilder sb, object parOrTarg
90839083
internal static StringBuilder AppendLabelName(this StringBuilder sb, LabelTarget target, ref SmallList4<NamedWithIndex> named) =>
90849084
sb.AppendName(target, target.Name, target.Type.ToCode(stripNamespace: true), ref named);
90859085

9086+
/// <summary>Returns the standard name (alias) for the well-known primitive type, e.g. Int16 -> short</summary>
9087+
public static string GetPrimitiveTypeNameAliasOrNull(this Type type) =>
9088+
Type.GetTypeCode(type) switch
9089+
{
9090+
TypeCode.Byte => "byte",
9091+
TypeCode.SByte => "sbyte",
9092+
TypeCode.Int16 => "short",
9093+
TypeCode.Int32 => "int",
9094+
TypeCode.Int64 => "long",
9095+
TypeCode.UInt16 => "ushort",
9096+
TypeCode.UInt32 => "uint",
9097+
TypeCode.UInt64 => "ulong",
9098+
TypeCode.Single => "float",
9099+
TypeCode.Double => "double",
9100+
TypeCode.Boolean => "bool",
9101+
TypeCode.Char => "char",
9102+
TypeCode.String => "string",
9103+
_ => type == typeof(void) ? "void" :
9104+
type == typeof(object) ? "object" :
9105+
null
9106+
};
9107+
90869108
// todo: @simplify add `addTypeof = false` or use `AppendTypeOf` generally
90879109
/// <summary>Converts the <paramref name="type"/> into the proper C# representation.</summary>
90889110
public static string ToCode(this Type type,
@@ -9108,29 +9130,7 @@ public static string ToCode(this Type type,
91089130
type = type.GetElementType();
91099131
}
91109132

9111-
// the default handling of the built-in types
9112-
string buildInTypeString = null;
9113-
if (type == typeof(void))
9114-
buildInTypeString = "void";
9115-
else if (type == typeof(object))
9116-
buildInTypeString = "object";
9117-
else if (type == typeof(bool))
9118-
buildInTypeString = "bool";
9119-
else if (type == typeof(int))
9120-
buildInTypeString = "int";
9121-
else if (type == typeof(short))
9122-
buildInTypeString = "short";
9123-
else if (type == typeof(byte))
9124-
buildInTypeString = "byte";
9125-
else if (type == typeof(double))
9126-
buildInTypeString = "double";
9127-
else if (type == typeof(float))
9128-
buildInTypeString = "float";
9129-
else if (type == typeof(char))
9130-
buildInTypeString = "char";
9131-
else if (type == typeof(string))
9132-
buildInTypeString = "string";
9133-
9133+
var buildInTypeString = type.GetPrimitiveTypeNameAliasOrNull();
91349134
if (buildInTypeString != null)
91359135
{
91369136
if (arrayType != null)

0 commit comments

Comments
 (0)