forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerationConfig.ttinclude
173 lines (151 loc) · 5.73 KB
/
GenerationConfig.ttinclude
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
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text" #>
<#+
/* This file includes static data used as compilation configuration for the rest of the code generation.
It is shared here to ensure that all generated code compiles with the same constants and configurations. */
// The set of supported numeric types to compile
public static Type[] supportedTypes = new[]
{
typeof(Byte), typeof(SByte), typeof(UInt16), typeof(Int16),
typeof(UInt32), typeof(Int32), typeof(UInt64), typeof(Int64),
typeof(Single), typeof(Double)
};
// The set of unsigned types, a subset of the above. Used for excluding from certain methods, i.e. Abs and Negate
public static Type[] unsignedTypes = new[]
{
typeof(Byte), typeof(UInt16), typeof(UInt32), typeof(UInt64)
};
public static Type[] integralTypes = new[]
{
typeof(Byte), typeof(SByte), typeof(UInt16), typeof(Int16),
typeof(UInt32), typeof(Int32), typeof(UInt64), typeof(Int64)
};
public static Type[] nonClsCompliantTypes = new[]
{
typeof(SByte), typeof(UInt16),
typeof(UInt32), typeof(UInt64)
};
// The total register size, in bytes. 16 for SSE2, 32 for AVX, 64 for AVX512
public static int totalSize = 16;
/* General template helper procedures */
// Returns the constructed register field name for the given type and index.
public string GetRegisterFieldName(Type t, int index)
{
return "register." + t.Name.ToLowerInvariant() + "_" + index;
}
// Returns the number of fields for a given type, based on the current configuration's register size
public int GetNumFields(Type t, int totalSize)
{
return totalSize / Marshal.SizeOf(t);
}
public static HashSet<Type> WidenableTypes { get; private set; } = new HashSet<Type>()
{
typeof(byte),
typeof(ushort),
typeof(uint),
typeof(sbyte),
typeof(short),
typeof(int),
typeof(float),
};
private static Dictionary<Type, Type> s_widenTargets = new Dictionary<Type, Type>()
{
{ typeof(byte), typeof(ushort) },
{ typeof(ushort), typeof(uint) },
{ typeof(uint), typeof(ulong) },
{ typeof(sbyte), typeof(short) },
{ typeof(short), typeof(int) },
{ typeof(int), typeof(long) },
{ typeof(float), typeof(double) },
};
public Type GetWidenTarget(Type t)
{
Type target;
if (!s_widenTargets.TryGetValue(t, out target))
{
throw new InvalidOperationException("No widen target for " + t.Name);
}
return target;
}
public static HashSet<Type> NarrowableTypes { get; private set; } = new HashSet<Type>()
{
typeof(ushort),
typeof(uint),
typeof(ulong),
typeof(short),
typeof(int),
typeof(long),
typeof(double),
};
private static Dictionary<Type, Type> s_narrowTargets = new Dictionary<Type, Type>()
{
{ typeof(ulong), typeof(uint) },
{ typeof(uint), typeof(ushort) },
{ typeof(ushort), typeof(byte) },
{ typeof(long), typeof(int) },
{ typeof(int), typeof(short) },
{ typeof(short), typeof(sbyte) },
{ typeof(double), typeof(float) },
};
public Type GetNarrowTarget(Type t)
{
Type target;
if (!s_narrowTargets.TryGetValue(t, out target))
{
throw new InvalidOperationException("No narrow target for " + t.Name);
}
return target;
}
public static List<KeyValuePair<Type, Type>> SameSizeConversionPairs = new List<KeyValuePair<Type, Type>>()
{
new KeyValuePair<Type, Type>(typeof(int), typeof(float)),
new KeyValuePair<Type, Type>(typeof(uint), typeof(float)),
new KeyValuePair<Type, Type>(typeof(long), typeof(double)),
new KeyValuePair<Type, Type>(typeof(ulong), typeof(double)),
new KeyValuePair<Type, Type>(typeof(float), typeof(int)),
new KeyValuePair<Type, Type>(typeof(float), typeof(uint)),
new KeyValuePair<Type, Type>(typeof(double), typeof(long)),
new KeyValuePair<Type, Type>(typeof(double), typeof(ulong)),
};
public void GenerateCopyrightHeader()
{
#>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
<#+
}
public string GenerateIfStatementHeader(Type type)
{
string keyword = (type == supportedTypes[0]) ? "if" : "else if";
return string.Format("{0} (typeof(T) == typeof({1}))", keyword, type.Name);
}
public string GenerateIfStatementHeader(Type type, IEnumerable<Type> allTypes)
{
string keyword = (type == allTypes.ToArray()[0]) ? "if" : "else if";
return string.Format("{0} (typeof(T) == typeof({1}))", keyword, type.Name);
}
public string MakeTypeComparisonCondition(Type type)
{
return string.Format("(typeof(T) == typeof({0}))", type.Name);
}
public string GenerateIfConditionAllTypes(IEnumerable<Type> allTypes)
{
StringBuilder sbuilder = new StringBuilder();
bool firstTime = true;
foreach (var type in allTypes)
{
if (firstTime)
{
sbuilder.Append("if (").Append(MakeTypeComparisonCondition(type));
firstTime = false;
}
else
{
sbuilder.AppendLine().Append(" || ").Append(MakeTypeComparisonCondition(type));
}
}
sbuilder.Append(")");
return sbuilder.ToString();
}
#>