-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiscVInterpreter.java
598 lines (560 loc) · 20.8 KB
/
RiscVInterpreter.java
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import org.apache.tools.ant.util.StringUtils;
public class RiscVInterpreter {
Boolean worked = false;
String printResult = "";
HashMap<Integer,Integer> memory;
HashMap<String, Integer> regs;
HashMap<String, String> equivs;
ArrayList<String> lines;
HashMap<String, Integer> labels;
ArrayList<String> instructions;
Integer address;
StringBuilder printer;
Boolean active;
Boolean badbadbad = false;
Boolean debug = false;
// Instruction level parameters
String regTo;
String reg1;
String reg2;
Integer imm;
String label;
/* Constructor */
public RiscVInterpreter(String program) {
// We can't precompute a program that requires input
if (program.contains("jal $input")) {
return;
}
if (debug) {
print(program);
}
// Initialize internals
memory = new HashMap<Integer, Integer>();
regs = new HashMap<String, Integer>();
equivs = new HashMap<String, String>();
labels = new HashMap<String, Integer>();
instructions = new ArrayList<String>();
printer = new StringBuilder();
active = true;
initRegs();
// Process program instructions
String[] strLines = program.split("\n");
for (int i = 0; i < strLines.length; i++) {
strLines[i] = strLines[i].trim();
}
lines = new ArrayList<String>(Arrays.asList(strLines));
// Clean program instructions
removeComments();
makeEquivs();
makeData();
makeInstructionList();
// Execute
runProgram();
if (!badbadbad) {
worked = true;
}
}
/* Initialize program registers */
private void initRegs() {
for (int i = 0; i <= 7; i++) {
regs.put("a" + Integer.toString(i), 0);
}
for (int i = 0; i <= 6; i++) {
regs.put("t" + Integer.toString(i), 0);
}
for (int i = 1; i <= 11; i++) {
regs.put("s" + Integer.toString(i), 0);
}
regs.put("fp", 0);
regs.put("sp", 0x7ffffff0);
regs.put("gp", 0x10000000);
regs.put("ra", 0);
regs.put("zero", 0);
regs.put("x0", 0);
}
/* Scrape and replace .equiv directives */
private void makeEquivs() {
ArrayList<Integer> indices = new ArrayList<Integer>();
// Create dictionary of .equiv equivalencies
for (int i = 0; i < lines.size(); ++i) {
String line = lines.get(i);
if (line.startsWith(".equiv")) {
String[] fields = line.split(" ");
fields[1] = fields[1].substring(0, fields[1].length() - 1);
equivs.put(fields[1], fields[2]);
indices.add(i);
}
}
Collections.reverse(indices);
for (Integer ind : indices) {
lines.remove((int) ind); // remove in backwards order
}
// replace equivalent string values + remove comments
for (int i = 0; i < lines.size(); ++i) {
String line = lines.get(i);
if (line.startsWith(".string")) {
continue;
}
for (String key : equivs.keySet()) {
line = StringUtils.replace(line, key, equivs.get(key));
}
lines.set(i, line);
}
}
/* Print memory hashmap in sequential order */
private void printMemory() {
ArrayList<Integer> hi = new ArrayList<Integer>(memory.keySet());
Collections.sort(hi);
for (Integer key : hi) {
System.out.print(Integer.toHexString(key) + " ");
System.out.println(Integer.toHexString(memory.get(key)));
}
}
/* Initialize data portion of memory under .data directives */
private void data(Integer i) {
String line = lines.get(i);
// Record labels
if (line.contains(":")) {
Integer ind = line.indexOf(":");
String label = line.substring(0, ind);
labels.put(label, address);
}
// Place .word directives into memory
else if (line.contains(".word")) {
String[] word = line.split(" ");
if (word[1].matches("-?(0|[1-9]\\d*)")) {
Integer val = Integer.parseInt(word[1]);
memory.put(address, val);
} else if (labels.containsKey(word[1])) {
Integer val = labels.get(word[1]);
memory.put(address, val);
}
address += 4;
}
// Place .string directives into memory
else if (line.contains(".string")) {
Integer startInd = line.indexOf("\"");
Integer endInd = line.lastIndexOf("\"");
String literal = line.substring(startInd + 1, endInd);
byte[] bytes = literal.getBytes(StandardCharsets.US_ASCII);
byte[] padBytes = new byte[4 - (bytes.length % 4)];
byte[] total = new byte[bytes.length + padBytes.length];
System.arraycopy(bytes, 0, total, 0, bytes.length);
System.arraycopy(padBytes, 0, total, bytes.length, padBytes.length);
ByteBuffer inter = ByteBuffer.wrap(total).order(ByteOrder.LITTLE_ENDIAN);
int lengthLeft = total.length;
while (lengthLeft > 0) {
memory.put(address, inter.getInt());
lengthLeft -= 4;
address += 4;
}
}
}
/* Find and record labels within instruction space */
private void setMainGlobals() {
address = 0x0;
Integer startInd = lines.indexOf(".globl main");
Integer endInd = lines.lastIndexOf(".data");
for (int i = startInd; i < endInd; i++) {
String line = lines.get(i);
if (line.contains(":")){
String label = line.substring(0, line.indexOf(":"));
labels.put(label, address);
} else if(line.startsWith(".") || line.startsWith("#") || line.length() < 4) {
// do nothing
} else {
address += 4;
}
}
}
/* Populate labels properly */
private void makeData() {
Integer startInd = lines.indexOf(".data");
Integer endInd = lines.indexOf(".text");
address = 0x10000000;
for (int i = startInd; i < endInd; i++) {
data(i);
}
Integer startLast = lines.lastIndexOf(".data");
for (int i = startLast; i < lines.size(); i++) {
data(i);
}
setMainGlobals();
address = 0x10000000;
// Run two passes to catch forward references
for (int i = startInd; i < endInd; i++) {
data(i);
}
for (int i = startLast; i < lines.size(); i++) {
data(i);
}
}
/* Strip comments on surrounding code */
private void removeComments() {
ArrayList<Integer> removes = new ArrayList<Integer>();
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
Integer commentInd = line.indexOf('#');
if (commentInd >= 0) {
line = line.substring(0, commentInd).trim();
}
if (line.length() == 0) {
removes.add(i);
}
lines.set(i, line);
}
Collections.reverse(removes);
for (Integer rem : removes) {
lines.remove((int) rem);
}
}
/* Make list of instructions */
private void makeInstructionList() {
Integer startInd = lines.indexOf(".globl main");
Integer endInd = lines.lastIndexOf(".data");
for(String line : lines.subList(startInd, endInd)) {
if (line.startsWith(".")) {
continue;
} else if (line.contains(":")) {
continue;
} else {
instructions.add(line);
}
}
}
/* Convert signed to unsigned integer (JAVA) */
public long getUnsignedInt(int x) {
return x & 0x00000000ffffffffL;
}
/* Perform system call */
public void ecall() {
switch(regs.get("a0")) {
case 1: // int
printer.append(regs.get("a1"));
break;
case 11: // char
printer.append("\n");
break;
case 4: // str
// a0 has address of null terminated string
Integer value = memory.get(regs.get("a1"));
Integer counter = 0;
while (value != 0) {
counter++;
if ((value & 0xff) == 0) {
break;
}
char character = (char) (value & 0xff);
printer.append(character);
if (counter % 4 == 0) {
value = memory.get(regs.get("a1") + counter);
} else {
value = value >> 8;
}
}
break;
case 9:
regs.put("a0", 0x10008000);
break;
case 10: // exit goodly
active = false;
break;
case 17: // exit BADLY
active = false;
String errorCode = regs.get("a1").toString();
printer.append("Exited with error code " + errorCode + "\n");
break;
default:
break;
}
return;
}
/* Run program */
private void runProgram() {
Integer pc = 0; // program counter
Integer cycles = 0;
// If program doesn't halt, then force terminate
while (active && cycles < 300000) {
String instruction = processInstruction(instructions.get(pc / 4));
if (debug) {
print(instructions.get(pc / 4));
}
switch(instruction) {
case "lui":
regs.put(regTo, imm << 12);
break;
case "add":
regs.put(regTo, regs.get(reg1) + regs.get(reg2));
break;
case "jal": // check if works
regs.put("ra", pc + 4); // save next instruction
pc = labels.get(label) - 4; // cuz we advance later anyway
break;
case "mv":
regs.put(regTo, regs.get(reg1));
break;
case "addi":
regs.put(regTo, regs.get(reg1) + imm);
break;
case "sw":
String[] splitInstr = instructions.get(pc / 4).split(" ");
if (splitInstr[2].contains("(")) {
memory.put(regs.get(reg1) + imm, regs.get(regTo));
} else {
memory.put(labels.get(label), regs.get(regTo));
}
break;
case "li":
regs.put(regTo, imm);
break;
case "lw":
splitInstr = instructions.get(pc / 4).split(" ");
if (splitInstr[2].contains("(")) {
regs.put(regTo, memory.get(regs.get(reg1) + imm));
} else {
regs.put(regTo, memory.get(labels.get(label)));
}
break;
case "ecall":
ecall();
break;
case "beq":
if (regs.get(regTo).equals(regs.get(reg1))) {
pc = labels.get(label) - 4;
}
break;
case "bne":
if (!regs.get(regTo).equals(regs.get(reg1))) {
pc = labels.get(label) - 4;
}
break;
case "blt":
if (regs.get(regTo) < regs.get(reg1)) {
pc = labels.get(label) - 4;
}
break;
case "bge":
if (regs.get(regTo) >= regs.get(reg1)) {
pc = labels.get(label) - 4;
}
break;
case "bltu":
if (getUnsignedInt(regs.get(regTo)) < getUnsignedInt(regs.get(reg1))) {
pc = labels.get(label) - 4;
}
break;
case "bgeu":
if (getUnsignedInt(regs.get(regTo)) >= getUnsignedInt(regs.get(reg1))) {
pc = labels.get(label) - 4;
}
break;
case "beqz":
if (regs.get(regTo).equals(0)) {
pc = labels.get(label) - 4;
}
break;
case "bnez":
if (!regs.get(regTo).equals(0)) {
pc = labels.get(label) - 4;
}
break;
case "bgtz":
if (regs.get(regTo) > 0) {
pc = labels.get(label) - 4;
}
break;
case "bgez":
if (regs.get(regTo) >= 0) {
pc = labels.get(label) - 4;
}
break;
case "blez":
if (regs.get(regTo) <= 0) {
pc = labels.get(label) - 4;
}
break;
case "bltz":
if (regs.get(regTo) < 0) {
pc = labels.get(label) - 4;
}
break;
case "jr":
pc = regs.get(regTo) - 4;
break;
case "la":
regs.put(regTo, labels.get(label));
break;
case "j":
pc = labels.get(label) - 4;
break;
case "xor":
regs.put(regTo, regs.get(reg1) ^ regs.get(reg2));
break;
case "div":
regs.put(regTo, regs.get(reg1) / regs.get(reg2));
break;
case "slli":
regs.put(regTo, regs.get(reg1) << imm);
break;
case "srli":
regs.put(regTo, regs.get(reg1) >> imm);
break;
case "jalr":
regs.put("ra", pc + 4);
pc = regs.get(regTo) - 4;
break;
case "mul":
regs.put(regTo, regs.get(reg1) * regs.get(reg2));
break;
case "or":
regs.put(regTo, regs.get(reg1) | regs.get(reg2));
break;
case "and":
regs.put(regTo, regs.get(reg1) & regs.get(reg2));
break;
case "sub":
regs.put(regTo, regs.get(reg1) - regs.get(reg2));
break;
case "lb":// CHECK
// add immediate, then clear last two bits
Integer actualMemoryAddress = regs.get(reg1) + imm;
Integer memoryAddress = (actualMemoryAddress >> 2) << 2;
Integer locatedWord = memory.get(memoryAddress);
byte[] bytes = ByteBuffer.allocate(4)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(locatedWord)
.array();
Integer value = (int) bytes[actualMemoryAddress & 0x3];
regs.put(regTo, value);
break;
case "sb":
byte store = (byte) ((int) regs.get(regTo));
actualMemoryAddress = regs.get(reg1) + imm;
memoryAddress = (actualMemoryAddress >> 2) << 2;
if (memory.containsKey(memoryAddress)) {
locatedWord = memory.get(memoryAddress);
} else {
locatedWord = 0;
}
bytes = ByteBuffer.allocate(4)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(locatedWord)
.array();
bytes[actualMemoryAddress & 0x3] = store;
value = ByteBuffer.wrap(bytes)
.order(ByteOrder.LITTLE_ENDIAN)
.getInt();
memory.put(memoryAddress, value);
break;
case "rem":
Integer r1 = regs.get(reg1);
Integer r2 = regs.get(reg2);
Integer val = 0;
if (r2.equals(0)) {
val = 0;
} else if (r1.equals(Integer.MIN_VALUE) && r2.equals(-1)) {
val = 0;
} else {
val = r1 % r2;
}
regs.put(regTo, val);
break;
default:
System.out.println("couldn't");
System.out.println(instructions.get(pc / 4));
badbadbad = true;
active = false;
break;
}
if (debug) {
print(regs);
}
pc += 4;
cycles += 1;
}
}
/* Print function */
private void print(Object... stuff) {
for (int i = 0; i < stuff.length - 1; i++) {
System.out.print(stuff[i]);
System.out.print(" ");
}
System.out.println(stuff[stuff.length - 1]);
}
/* Grab registers and label from instruction */
private String processInstruction(String instr) {
String[] splitInstr = instr.split(" ");
String instruction = splitInstr[0];
if (instruction.equals("ecall")) {
return instruction;
}
if (splitInstr[1].contains(",")) {
splitInstr[1] = StringUtils.replace(splitInstr[1], ",", "");
}
if (splitInstr.length > 2) {
if (splitInstr[2].contains(",")) {
splitInstr[2] = StringUtils.replace(splitInstr[2], ",", "");
}
if (splitInstr[2].matches("-?(0|[1-9]\\d*)")) {
imm = Integer.parseInt(splitInstr[2]);
}
if (regs.containsKey(splitInstr[2])) {
reg1 = splitInstr[2];
} else {
label = splitInstr[2];
}
if (splitInstr[2].contains("(")) {
int firstParen = splitInstr[2].indexOf("(");
int lastParen = splitInstr[2].indexOf(")");
reg1 = splitInstr[2].substring(firstParen + 1, lastParen);
String tempImm = splitInstr[2].substring(0, firstParen);
if (tempImm.matches("-?(0|[1-9]\\d*)")) {
imm = Integer.parseInt(tempImm);
} else {
Integer ind = Math.max(tempImm.indexOf("+"), tempImm.indexOf("-"));
Integer num1 = Integer.parseInt(tempImm.substring(0, ind));
Integer num2 = Integer.parseInt(tempImm.substring(ind + 1));
if (tempImm.contains("-")) {
imm = num1 - num2;
} else if (tempImm.contains("+")) {
imm = num1 + num2;
} else {
System.out.println("ERRORORORORR");
}
}
}
}
if (splitInstr.length > 3) {
if (splitInstr[3].contains(",")) {
splitInstr[3] = StringUtils.replace(splitInstr[3], ",", "");
}
if (splitInstr[3].matches("-?(0|[1-9]\\d*)")) {
imm = Integer.parseInt(splitInstr[3]);
}
if (regs.containsKey(splitInstr[3])) {
reg2 = splitInstr[3];
} else {
label = splitInstr[3];
}
}
if (instruction.equals("jal")) {
if (regs.containsKey(splitInstr[1])) {
label = splitInstr[2];
} else {
label = splitInstr[1];
}
}
if (instruction.equals("j")) {
label = splitInstr[1];
}
regTo = splitInstr[1];
return instruction;
}
}