-
Notifications
You must be signed in to change notification settings - Fork 82
/
BaseInstruction.cs
295 lines (248 loc) · 7.37 KB
/
BaseInstruction.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
// Copyright (c) MOSA Project. Licensed under the New BSD License.
using System;
using System.Text;
namespace Mosa.Compiler.Framework
{
/// <summary>
///
/// </summary>
public abstract class BaseInstruction
{
#region Properties
/// <summary>
/// Gets the default operand count of the instruction
/// </summary>
/// <value>The operand count.</value>
public byte DefaultOperandCount { get; protected set; }
/// <summary>
/// Gets the default result operand count of the instruction
/// </summary>
/// <value>The operand result count.</value>
public byte DefaultResultCount { get; protected set; }
/// <summary>
/// Determines flow behavior of this instruction.
/// </summary>
/// <remarks>
/// Knowledge of control flow is required for correct basic block
/// building. Any instruction that alters the control flow must override
/// this property and correctly identify its control flow modifications.
/// </remarks>
public virtual FlowControl FlowControl { get { return FlowControl.Next; } }
/// <summary>
/// Gets a value indicating whether to [ignore during code generation].
/// </summary>
/// <value>
/// <c>true</c> if [ignore during code generation]; otherwise, <c>false</c>.
/// </value>
public virtual bool IgnoreDuringCodeGeneration { get { return false; } }
/// <summary>
/// Gets a value indicating whether to [ignore instruction's basic block].
/// </summary>
/// <value>
/// <c>true</c> if [ignore instruction basic block]; otherwise, <c>false</c>.
/// </value>
public virtual bool IgnoreInstructionBasicBlockTargets { get { return false; } }
#endregion Properties
#region Construction
/// <summary>
/// Initializes a new instance of the <see cref="BaseInstruction"/> class.
/// </summary>
/// <param name="resultCount">The result count.</param>
/// <param name="operandCount">The operand count.</param>
public BaseInstruction(byte resultCount, byte operandCount)
{
DefaultResultCount = resultCount;
DefaultOperandCount = operandCount;
}
#endregion Construction
#region Methods
/// <summary>
/// Validates the specified instruction.
/// </summary>
/// <param name="ctx">The context.</param>
/// <param name="compiler">The compiler.</param>
public virtual void Resolve(Context ctx, BaseMethodCompiler compiler)
{
/* Default implementation is to do nothing */
}
/// <summary>
/// Returns a string representation of the context.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
string inst = GetType().ToString();
int index = inst.LastIndexOf(".");
if (index > 0)
inst = inst.Substring(index + 1);
index = inst.IndexOf("Instruction");
if (index > 0)
inst = inst.Substring(0, index);
return inst;
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <param name="node">The context.</param>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(InstructionNode node)
{
var sb = new StringBuilder();
sb.AppendFormat("L_{0:X4}", node.Label);
if (node.Marked)
sb.Append('*');
else
sb.Append(' ');
sb.Append(ToString());
var size = GetSizeString(node.Size);
if (size != string.Empty)
sb.Append("/" + size);
if (node.ConditionCode != ConditionCode.Undefined)
{
sb.Append(" [");
sb.Append(GetConditionString(node.ConditionCode));
sb.Append("]");
}
if (node.MosaType != null)
{
sb.Append(" [[");
sb.Append(node.MosaType.FullName);
sb.Append("]]");
}
if (node.MosaField != null)
{
sb.Append(" [[");
sb.Append(node.MosaField.FullName);
sb.Append("]]");
}
string mod = GetModifier(node);
if (mod != null)
{
sb.Append(" [");
sb.Append(mod);
sb.Append("]");
}
for (int i = 0; i < node.ResultCount; i++)
{
var op = node.GetResult(i);
sb.Append(" ");
sb.Append(op == null ? "[NULL]" : op.ToString(true));
sb.Append(",");
}
if (node.ResultCount > 0)
{
sb.Length = sb.Length - 1;
}
if (node.ResultCount > 0 && node.OperandCount > 0)
{
sb.Append(" <=");
}
for (int i = 0; i < node.OperandCount; i++)
{
var op = node.GetOperand(i);
sb.Append(" ");
sb.Append(op == null ? "[NULL]" : op.ToString(true));
sb.Append(",");
}
if (node.OperandCount > 0)
{
sb.Length = sb.Length - 1;
}
if (node.BranchTargets != null)
{
sb.Append(' ');
for (int i = 0; (i < 2) && (i < node.BranchTargetsCount); i++)
{
if (i != 0)
{
sb.Append(", ");
}
sb.Append(node.BranchTargets[i].ToString());
}
if (node.BranchTargetsCount > 2)
{
sb.Append(", [more]");
}
}
if (node.InvokeMethod != null)
{
sb.Append(" {");
sb.Append(node.InvokeMethod.FullName);
sb.Append("}");
}
if (node.MosaField != null)
{
sb.Append(" {");
sb.Append(node.MosaField.FullName);
sb.Append("}");
}
return sb.ToString();
}
/// <summary>
/// Allows visitor based dispatch for this instruction object.
/// </summary>
/// <param name="visitor">The visitor.</param>
/// <param name="context">The context.</param>
public virtual void Visit(IVisitor visitor, Context context)
{
}
/// <summary>
/// Gets the instruction modifier.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
protected virtual string GetModifier(InstructionNode node)
{
return null;
}
/// <summary>
/// Gets the condition string.
/// </summary>
/// <param name="conditioncode">The condition code.</param>
/// <returns></returns>
protected string GetConditionString(ConditionCode conditioncode)
{
switch (conditioncode)
{
case ConditionCode.Equal: return @"equal";
case ConditionCode.GreaterOrEqual: return @"greater or equal";
case ConditionCode.GreaterThan: return @"greater";
case ConditionCode.LessOrEqual: return @"less or equal";
case ConditionCode.LessThan: return @"less";
case ConditionCode.NotEqual: return @"not equal";
case ConditionCode.UnsignedGreaterOrEqual: return @"greater or equal (U)";
case ConditionCode.UnsignedGreaterThan: return @"greater (U)";
case ConditionCode.UnsignedLessOrEqual: return @"less or equal (U)";
case ConditionCode.UnsignedLessThan: return @"less (U)";
case ConditionCode.NotSigned: return @"not signed";
case ConditionCode.Signed: return @"signed";
case ConditionCode.Zero: return @"zero";
case ConditionCode.NotZero: return @"not zero";
case ConditionCode.Parity: return @"parity";
case ConditionCode.NoParity: return @"no parity";
case ConditionCode.Carry: return @"carry";
case ConditionCode.NoCarry: return @"no carry";
case ConditionCode.Always: return @"always";
default: throw new NotSupportedException();
}
}
public static string GetSizeString(InstructionSize size)
{
switch (size)
{
case InstructionSize.Size32: return "32";
case InstructionSize.Size8: return "8";
case InstructionSize.Size16: return "16";
case InstructionSize.Size64: return "64";
case InstructionSize.Size128: return "128";
case InstructionSize.Native: return string.Empty;// "Native";
default: return string.Empty;
}
}
#endregion Methods
}
}