-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathRiscvOpcodesParser.cs
More file actions
329 lines (280 loc) · 12.3 KB
/
RiscvOpcodesParser.cs
File metadata and controls
329 lines (280 loc) · 12.3 KB
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
//
// Copyright (c) 2010-2025 Antmicro
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Antmicro.Renode.Exceptions;
using Antmicro.Renode.Utilities;
namespace Antmicro.Renode.Peripherals.CPU
{
public static class RiscvOpcodesExtensions
{
public static void EnableRiscvOpcodesCounting(this BaseRiscV cpu, ReadFilePath file)
{
foreach(var x in RiscVOpcodesParser.Parse(file))
{
cpu.InstallOpcodeCounterPattern(x.Item1, x.Item2);
}
cpu.EnableOpcodesCounting = true;
}
public static void EnableRiscvOpcodesCounting(this BaseRiscV cpu)
{
foreach(var set in cpu.ArchitectureSets)
{
cpu.EnableRiscvOpcodesCountingByExtension(set);
}
}
public static void EnableRiscvOpcodesCountingFromEmbeddedResource(this BaseRiscV cpu, string name)
{
var assembly = Assembly.GetExecutingAssembly();
if(!assembly.TryFromResourceToTemporaryFile($"{ResourceNamePrefix}.{name}", out var file))
{
var availableResources = string.Join("\n", cpu.GetRiscvOpcodesEmbeddedResourceNames());
throw new RecoverableException($"Couldn't load the {name} resource - please choose from:\n{availableResources}");
}
cpu.EnableRiscvOpcodesCounting(file);
}
public static IEnumerable<string> GetRiscvOpcodesEmbeddedResourceNames(this BaseRiscV _)
{
var assembly = Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceNames().Where(x => x.StartsWith(ResourceNamePrefix)).Select(x => x.Substring(ResourceNamePrefix.Length + 1));
}
public static void EnableRiscvOpcodesCountingByExtension(this BaseRiscV cpu, BaseRiscV.InstructionSet instructionSet)
{
Dictionary<BaseRiscV.InstructionSet, IEnumerable<string>> map = null;
if(cpu.Architecture == "riscv")
{
map = opcodesFilesMap32;
}
else if(cpu.Architecture == "riscv64")
{
map = opcodesFilesMap64;
}
else
{
throw new RecoverableException($"Unsupported CPU type: {cpu.GetType()}");
}
if(!map.TryGetValue(instructionSet, out var resourceNames))
{
throw new RecoverableException($"Opcodes counting for the {instructionSet} extension not supported");
}
foreach(var resourceName in resourceNames)
{
var assembly = Assembly.GetExecutingAssembly();
if(!assembly.TryFromResourceToTemporaryFile(resourceName, out var file))
{
throw new RecoverableException($"Couldn't load the {resourceName} resource - this might indicate a broken compilation");
}
cpu.EnableRiscvOpcodesCounting(file);
}
}
private const string ResourceNamePrefix = "Antmicro.Renode.Cores.RiscV.Opcodes";
private static readonly Dictionary<BaseRiscV.InstructionSet, IEnumerable<string>> opcodesFilesMap32 = new Dictionary<BaseRiscV.InstructionSet, IEnumerable<string>>
{
{ BaseRiscV.InstructionSet.I, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.System", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32i" } },
{ BaseRiscV.InstructionSet.M, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32m" } },
{ BaseRiscV.InstructionSet.A, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32a" } },
{ BaseRiscV.InstructionSet.F, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32f" } },
{ BaseRiscV.InstructionSet.B, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32b" } },
{ BaseRiscV.InstructionSet.D, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32d", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32d-zfh" } },
{ BaseRiscV.InstructionSet.C, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rvc", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32c" } },
{ BaseRiscV.InstructionSet.V, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rvv", "Antmicro.Renode.Cores.RiscV.Opcodes.Rvv-pseudo" } },
{ BaseRiscV.InstructionSet.S, Array.Empty<string>() },
{ BaseRiscV.InstructionSet.U, Array.Empty<string>() },
};
private static readonly Dictionary<BaseRiscV.InstructionSet, IEnumerable<string>> opcodesFilesMap64 = new Dictionary<BaseRiscV.InstructionSet, IEnumerable<string>>
{
{ BaseRiscV.InstructionSet.I, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.System", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32i", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64i" } },
{ BaseRiscV.InstructionSet.M, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32m", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64m" } },
{ BaseRiscV.InstructionSet.A, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32a", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64a" } },
{ BaseRiscV.InstructionSet.F, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32f", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64f" } },
{ BaseRiscV.InstructionSet.B, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32b", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64b" } },
{ BaseRiscV.InstructionSet.D, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rv32d", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64d" } },
{ BaseRiscV.InstructionSet.C, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rvc", "Antmicro.Renode.Cores.RiscV.Opcodes.Rv64c" } },
{ BaseRiscV.InstructionSet.V, new [] { "Antmicro.Renode.Cores.RiscV.Opcodes.Rvv", "Antmicro.Renode.Cores.RiscV.Opcodes.Rvv-pseudo" } },
{ BaseRiscV.InstructionSet.S, Array.Empty<string>() },
{ BaseRiscV.InstructionSet.U, Array.Empty<string>() },
};
}
public static class RiscVOpcodesParser
{
public static IEnumerable<Tuple<string, string>> Parse(string file)
{
var result = new List<Tuple<string, string>>();
try
{
foreach(var line in File.ReadLines(file).Select(x => RemoveComments(x)))
{
if(line.Length == 0)
{
continue;
}
result.Add(ParseLine(line));
}
}
catch(IOException e)
{
throw new RecoverableException($"There was na error when parsing RISC-V opcodes from {file}:\n{e.Message}");
}
return result;
}
private static string RemoveComments(string line)
{
var pos = line.IndexOf("#");
if(pos != -1)
{
line = line.Remove(pos);
}
return line.Trim();
}
private static Tuple<string, string> ParseLine(string lineContent, int opcodeLength = 32)
{
var pattern = new StringBuilder(new String('_', opcodeLength));
var elems = lineContent.Split(LineSplitPatterns, StringSplitOptions.RemoveEmptyEntries);
if(elems.Length < 2)
{
throw new RecoverableException($"Couldn't split line: {lineContent}");
}
var instructionName = elems[0];
foreach(var elem in elems.Skip(1))
{
var parts = elem.Split(PartSplitPatterns);
if(parts.Length != 2)
{
// let's ignore all non-explicit ranges
continue;
}
if(!BitsRange.TryParse(parts[0], out var range))
{
throw new RecoverableException($"Couldn't parse range: {parts[0]}");
}
if(!BitsValue.TryParse(parts[1], out var value))
{
throw new RecoverableException($"Couldn't parse value: {parts[1]}");
}
if(!range.TryApply(pattern, value))
{
throw new RecoverableException($"Couldn't apply value {value} in range {range} to pattern {pattern}");
}
}
return Tuple.Create(instructionName, pattern.ToString());
}
private static readonly char[] PartSplitPatterns = new [] { '=' };
private static readonly char[] LineSplitPatterns = new [] { ' ', '\t' };
private static readonly string[] BitRangeSplitPatterns = new [] { ".." };
private struct BitsValue
{
public static bool TryParse(string s, out BitsValue bv)
{
if(s == "ignore")
{
bv = new BitsValue(-1, ignored: true);
return true;
}
if(SmartParser.Instance.TryParse(s, typeof(int), out var result))
{
bv = new BitsValue((int)result);
return true;
}
bv = new BitsValue(-1);
return false;
}
public bool TryGetBinaryPattern(int length, out string result)
{
if(Ignored)
{
result = new String('x', length);
return true;
}
result = Convert.ToString(Value, 2);
if(result.Length > length)
{
return false;
}
result = result.PadLeft(length, '0');
return true;
}
private BitsValue(int value, bool ignored = false)
{
Value = value;
Ignored = ignored;
}
public int Value { get; }
public bool Ignored { get; }
public override string ToString()
{
return $"[BitsValue: {Value}, ignored: {Ignored}]";
}
}
private struct BitsRange
{
// There are two expected formats:
// * hi..lo (hi, lo being integers)
// * bit (bit being an integer; treated as bit..bit)
public static bool TryParse(string s, out BitsRange br)
{
var x = s.Split(BitRangeSplitPatterns, StringSplitOptions.None);
if(x.Length == 1)
{
if(int.TryParse(x[0], out var bit))
{
br = new BitsRange(bit, bit);
return true;
}
}
else if(x.Length == 2)
{
if(int.TryParse(x[0], out var upperBit)
&& int.TryParse(x[1], out var lowerBit))
{
br = new BitsRange(lowerBit, upperBit);
return true;
}
}
br = new BitsRange(-1, -1);
return false;
}
public bool TryApply(StringBuilder s, BitsValue v)
{
if(!v.TryGetBinaryPattern(this.Width, out var pattern))
{
return false;
}
if(Higher >= s.Length)
{
return false;
}
var pIdx = pattern.Length - 1;
var sIdx = s.Length - Lower - 1;
for(var i = Lower; i <= Higher; i++, pIdx--, sIdx--)
{
if(s[sIdx] == '1' || s[sIdx] == '0')
{
return false;
}
s[sIdx] = pattern[pIdx];
}
return true;
}
public override string ToString()
{
return $"[BitsRange: {Lower} - {Higher}]";
}
private BitsRange(int lower, int higher)
{
Lower = lower;
Higher = higher;
}
public int Lower { get; }
public int Higher { get; }
public int Width => Higher - Lower + 1;
}
}
}