forked from milindkulkarni/RiscSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstructions.py
More file actions
1107 lines (833 loc) · 29.6 KB
/
instructions.py
File metadata and controls
1107 lines (833 loc) · 29.6 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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from util import parseint
import re
import timingmodel
import config
#base class for instructions
class Instruction :
def __init__(self, opcode) :
self.opcode = opcode #name of opcode
#execute the instruction, including updating memory/registers as necessary
#assumption: exec does not check dependences. This will be checked at other phases in the code
#could simply schedule code for execution, rather than directly executing it
#TODO: extend these to handle different timing models -- move the basic exec code into a "simpleExec" function instead
def exec(self) :
raise NotImplementedError('exec not implemented for ' + self.opcode)
def __repr__(self) :
return str(self)
#base class for u-type instructions
class UInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
# print(instr)
match = re.match(r'(\S+) (\S+), (\S+)', instr)
return cls(match[2], match[3], match[1])
def __init__(self, dst, imm, opcode) :
super().__init__(opcode)
self.dst = dst
self._imm = 0
self.imm = imm
@property
def dsttype(self) :
raise NotImplementedError("Define type in derived class")
@property
def imm(self) :
return self._imm
@imm.setter
def imm(self, value) :
raise NotImplementedError("Define immediate setter in derived class")
def exec(self) :
config.machine.timingModel.exec(self)
destReg = config.machine.registerFile[self.dst]
assert destReg.type == self.dsttype, "Destination register is not " + str(self.dsttype)
d = self.funcExec(self.imm)
destReg.write(d)
config.machine.pc += 4
def funcExec(self, imm) :
raise NotImplementedError("funcExec not implemented for u-type instruction " + self.opcode)
def __str__(self) :
return str(self.opcode + " " + self.dst + " " + str(self.imm))
#base class for u-type instruction with integer immediate
class IUInstruction(UInstruction) :
@property
def dsttype(self) :
return int
@UInstruction.imm.setter # pylint: disable=no-member
def imm(self, value) :
new_imm = parseint(value)
assert (new_imm < (2 ** 19 - 1) and new_imm > (-1 * 2 ** 19)), "Immediate must fit in 20 bits"
self._imm = new_imm
#base class for u-type instruction with float immediate
class FUInstruction(UInstruction) :
@property
def dsttype(self) :
return float
@UInstruction.imm.setter # pylint: disable=no-member
def imm(self, value) :
new_imm = float(value)
self._imm = new_imm
#base class for magic integer immediate instruction
class MIUInstruction(UInstruction) :
@property
def dsttype(self) :
return int
@UInstruction.imm.setter # pylint: disable=no-member
def imm(self, value) :
new_imm = parseint(value)
self._imm = new_imm
#base class for 2-operand r-type instructions
class ORInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+) (\S+), (\S+)', instr)
return cls(match[3], match[2], match[1])
def __init__(self, src1, dst, opcode) :
super().__init__(opcode)
self.src1 = src1
self.dst = dst
@property
def srctype(self) :
raise NotImplementedError("Define type in the derived class!")
@property
def dsttype(self) :
raise NotImplementedError("Define type in the derived class!")
def exec(self) :
config.machine.timingModel.exec(self)
src1reg = config.machine.registerFile[self.src1]
assert src1reg.type == self.srctype, "Src 1 register is not " + str(self.srctype)
s1 = src1reg.read()
d = self.funcExec(s1)
destReg = config.machine.registerFile[self.dst]
assert destReg.type == self.dsttype, "Destination register is not " + str(self.dsttype)
destReg.write(d)
config.machine.pc += 4
def funcExec(self, s1) :
raise NotImplementedError("funcExec not implemented for 2-operand r-type instruction " + self.opcode)
def __str__(self) :
return str(self.opcode + " " + self.dst + " " + self.src1)
#base class for integer 2-operand instructions
class IORInstruction(ORInstruction) :
@property
def srctype(self) :
return int
@property
def dsttype(self) :
return int
#base class for fp 2-operand instructions
class FORInstruction(ORInstruction) :
@property
def srctype(self) :
return float
@property
def dsttype(self) :
return float
#base class for 3-operand r-type instructions
class RInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+) (\S+), (\S+), (\S+)', instr)
return cls(match[3], match[4], match[2], match[1])
def __init__(self, src1, src2, dst, opcode) :
super().__init__(opcode)
self.src1 = src1
self.src2 = src2
self.dst = dst
@property
def srctype(self) :
raise NotImplementedError("Define type in the derived class!")
@property
def dsttype(self) :
raise NotImplementedError("Define type in the derived class!")
def exec(self) :
config.machine.timingModel.exec(self)
src1reg = config.machine.registerFile[self.src1]
assert src1reg.type == self.srctype, "Src 1 register is not " + str(self.srctype)
s1 = src1reg.read()
src2reg = config.machine.registerFile[self.src2]
assert src2reg.type == self.srctype, "Src 2 register is not " + str(self.srctype)
s2 = src2reg.read()
d = self.funcExec(s1, s2)
destReg = config.machine.registerFile[self.dst]
assert destReg.type == self.dsttype, "Destination register is not " + str(self.dsttype)
destReg.write(d)
config.machine.pc += 4
def funcExec(self, s1, s2) :
raise NotImplementedError("funcExec not implemented for r-type instruction " + self.opcode)
def __str__(self) :
return str(self.opcode + " " + self.dst + " " + self.src1 + " " + self.src2)
#base class for int r-type instructions
class IRInstruction(RInstruction) :
@property
def srctype(self) :
return int
@property
def dsttype(self) :
return int
#base class for fp r-type instructions
class FRInstruction(RInstruction) :
@property
def srctype(self) :
return float
@property
def dsttype(self) :
return float
#base class for fp comparison instructions
class FCmpInstruction(RInstruction) :
@property
def srctype(self) :
return float
@property
def dsttype(self) :
return int
#base class for i-type instructions
class IInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+) (\S+), (\S+), (\S+)', instr)
return cls(match[3], match[4], match[2], match[1])
def __init__(self, src1, imm, dst, opcode) :
super().__init__(opcode)
self.src1 = src1
self.imm = imm
self.dst = dst
def exec(self) :
config.machine.timingModel.exec(inst = self)
src1reg = config.machine.registerFile[self.src1]
assert src1reg.type == int, "Src 1 register is not an integer"
s1 = src1reg.read()
#cast imm to the type of the destination register
destReg = config.machine.registerFile[self.dst]
assert destReg.type == int, "Destination register is not an integer"
imm = destReg.type(self.imm)
assert (imm < (2 ** 11 - 1) and imm > (-1 * 2 ** 11)), "Immediate value is too large; must fit in 12 bits"
d = self.funcExec(s1, imm)
destReg.write(d)
config.machine.pc += 4
def funcExec(self, s1, imm) :
raise NotImplementedError("funcExec not implemented for i-type instruction " + self.opcode)
def __str__(self) :
# print("here")
return str(self.opcode + " " + self.dst + " " + self.src1 + " " + self.imm)
#base class for memory instruction
class MemInstruction(Instruction) :
#LOAD: LW reg1, imm(reg2) : reg1 = *(reg2 + imm)
#STORE: SW reg1, imm(reg2) : *(reg2 + imm) = reg1
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+) (\S+), (\S+)\((\S+)\)', instr)
return cls(match[2], match[4], match[3], match[1])
def __init__(self, reg1, reg2, imm, opcode) :
super().__init__(opcode)
self.reg1 = reg1
self.reg2 = reg2
self.imm = imm
def _calculateAddress(self) :
offset = int(self.imm)
assert (offset < 2 ** 12), "Offset too large"
base = config.machine.registerFile[self.reg2].read()
return base + offset
def __str__(self) :
return str(self.opcode + " " + self.reg1 + " " + self.imm + "(" + self.reg2 + ")")
#base class for int/fp loads
class LDInstruction(MemInstruction) :
def exec(self) :
#calculate address
addr = self._calculateAddress()
#perform load
val = self.funcExec(addr, config.machine.memory)
#store result into register
assert(type(val) == self.dsttype), "Value in memory not of type " + str(self.dsttype)
destReg = config.machine.registerFile[self.reg1]
assert (destReg.type == self.dsttype), "Destination register not of type " + str(self.dsttype)
destReg.write(val)
config.machine.timingModel.cacheExec(self, addr)
config.machine.pc += 4
def funcExec(self, addr, memory) :
return memory[addr]
@property
def dsttype(self) :
raise NotImplementedError("Specialize type in derived class")
#base class for int/fp stores
class STInstruction(MemInstruction) :
def exec(self) :
#calculate address
addr = self._calculateAddress()
#get value from register
srcReg = config.machine.registerFile[self.reg1]
assert (srcReg.type == self.srctype), "Source register not of type " + str(self.srctype)
val = srcReg.read()
#perform store
self.funcExec(addr, val, config.machine.memory)
config.machine.timingModel.cacheExec(self, addr)
config.machine.pc += 4
def funcExec(self, addr, val, memory) :
# print("updating memory location: " + hex(addr))
memory[addr] = val
@property
def srctype(self) :
raise NotImplementedError("Specialize type in derived class")
#base class for IO magic instructions
class IOInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+) (\S+)', instr)
return cls(match[2], match[1])
def __init__(self, reg, opcode) :
super().__init__(opcode)
self.reg = reg
def __str__(self) :
return str(self.opcode + " " + self.reg)
#base class for reading stdin
class InputInstruction(IOInstruction) :
def exec(self) :
dstreg = config.machine.registerFile[self.reg]
assert dstreg.type == self.dsttype, "Reading into register of type " + str(dstreg.type) + " when expecting " + str(self.dsttype)
val = self.funcExec()
dstreg.write(val)
config.machine.pc += 4
def funcExec(self) :
return self.dsttype(input())
@property
def dsttype(self) :
raise NotImplementedError("Implement in derived class")
#base class for writing to stdout
class OutputInstruction(IOInstruction) :
def exec(self) :
srcreg = config.machine.registerFile[self.reg]
assert srcreg.type == self.srctype, "Writing register of type " + str(srcreg.type) + " when expecting " + str(self.srctype)
val = srcreg.read()
self.funcExec(val)
config.machine.pc += 4
def funcExec(self, val) :
print (val)
@property
def srctype(self) :
raise NotImplementedError("Implement in derived class")
class ImmControlInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
#OP label
match = re.match(r'(\S+) (\S+)', instr)
return cls(match[1], match[2])
def __init__(self, opcode, label) :
self.opcode = opcode
self.label = label
def exec(self) :
raise NotImplementedError("Implement exec in base class")
def __str__(self) :
return str(self.opcode + " " + self.label)
#base class for branch instructinos
class BranchInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
#OP src1, src2, label
match = re.match(r'(\S+) (\S+), (\S+), (\S+)', instr)
return cls(match[1], match[2], match[3], match[4])
def __init__(self, opcode, src1, src2, label) :
self.opcode = opcode
self.src1 = src1
self.src2 = src2
self.label = label
def exec(self) :
srcreg1 = config.machine.registerFile[self.src1]
assert srcreg1.type == int, "Can only compare integer registers"
srcreg2 = config.machine.registerFile[self.src2]
assert srcreg2.type == int, "Can only compare integer registers"
taken = self.funcExec(srcreg1.read(), srcreg2.read())
if (taken == True) :
config.machine.pc = config.machine.prog.labels[self.label]
else :
config.machine.pc += 4
def funcExec(self, val1, val2) :
raise NotImplementedError("Implement funcExec in derived class")
def __str__(self) :
return str(self.opcode + " " + self.src1 + " " + self.src2 + " " + self.label);
###### Instructions ######
#map opcodes (in text) to class associated with instruction
opCodeMap = {}
#decorator for concrete instructions to set up opcode map
class concreteInstruction :
def __init__(self, opcode) :
self.opcode = opcode
def __call__(self, cls) :
opCodeMap[self.opcode] = cls
return cls
@concreteInstruction('JAL')
class JalInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
#JAL reg, label
match = re.match(r'(\S+) (\S+), (\S+)', instr)
return cls(match[1], match[2], match[3])
def __init__(self, opcode, reg, label) :
self.opcode = opcode
self.label = label
self.reg = reg
def exec(self) :
config.machine.timingModel.exec(inst = self)
dstreg = config.machine.registerFile[self.reg]
dstreg.write(config.machine.pc + 4)
config.machine.pc = config.machine.prog.labels[self.label]
def __str__(self) :
return str(self.opcode + " " + self.reg + ", " + self.label)
@concreteInstruction('JALR')
class JalrInstruction(IInstruction) :
def exec(self) :
config.machine.timingModel.exec(inst = self)
src1reg = config.machine.registerFile[self.src1]
assert src1reg.type == int, "Src 1 register is not an integer"
s1 = src1reg.read()
#cast imm to the type of the destination register
destReg = config.machine.registerFile[self.dst]
assert destReg.type == int, "Destination register is not an integer"
imm = destReg.type(self.imm)
assert (imm < (2 ** 11 - 1) and imm > (-1 * 2 ** 11)), "Immediate value is too large; must fit in 12 bits"
destReg.write(config.machine.pc + 4)
config.machine.pc = s1 + imm
@concreteInstruction('RET')
class RetInstruction(Instruction) :
@classmethod
def parse(cls, instr) :
match = re.match(r'(\S+)', instr)
return cls(match[1])
def __init__(self, opcode) :
self.opcode = opcode
self._jalr = JalrInstruction('x1', 0, 'x0', 'JALR')
def exec(self) :
return self._jalr.exec()
def __str__(self) :
return self.opcode
@concreteInstruction('JR')
class JrInstruction(ImmControlInstruction) :
def __init__(self, opcode, label) :
super().__init__(opcode, label)
self._jal = JalInstruction('JAL', 'x1', self.label)
def exec(self) :
self._jal.exec()
@concreteInstruction('J')
class JInstruction(ImmControlInstruction) :
def __init__(self, opcode, label) :
super().__init__(opcode, label)
# print(JalInstruction)
self._jal = JalInstruction('JAL', 'x0', self.label)
def exec(self) :
self._jal.exec()
@concreteInstruction('ADD')
class AddInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 + s2
@concreteInstruction('SUB')
class SubInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 - s2
@concreteInstruction('MUL')
class MulInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 * s2
@concreteInstruction('DIV')
class DivInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 // s2
@concreteInstruction('REM')
class RemInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 % s2
@concreteInstruction('SLT')
class SltInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return 1 if s1 < s2 else 0
@concreteInstruction('AND')
class AndInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 & s2
@concreteInstruction('OR')
class OIRInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 | s2
@concreteInstruction('XOR')
class XoIRInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 ^ s2
@concreteInstruction('SLL')
class SllInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 << (s2 % 32)
@concreteInstruction('SRL')
class SrlInstruction(IRInstruction) :
def funcExec(self, s1, s2) :
return s1 >> (s2 % 32)
@concreteInstruction('ADDI')
class AddiInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 + imm
@concreteInstruction('ANDI')
class AndiInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 & imm
@concreteInstruction('ORI')
class OriInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 | imm
@concreteInstruction('XORI')
class XoriInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 ^ imm
@concreteInstruction('SLTI')
class SltiInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return 1 if s1 < imm else 0
@concreteInstruction('SLLI')
class SlliInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 << (imm % 5)
@concreteInstruction('SRLI')
class SrliInstruction(IInstruction) :
def funcExec(self, s1, imm) :
return s1 >> (imm % 5)
@concreteInstruction('LUI')
class LuiInstruction(IUInstruction) :
def funcExec(self, imm) :
return imm << 12
@concreteInstruction('MV')
class MvInstruction(IORInstruction) :
def funcExec(self, s1) :
return s1
@concreteInstruction('NOT')
class NotInstruction(IORInstruction) :
def funcExec(self, s1) :
return ~s1
@concreteInstruction('NEG')
class NegInstruction(IORInstruction) :
def funcExec(self, s1) :
return -1 * s1
@concreteInstruction('FADD.S')
class FaddInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 + s2
@concreteInstruction('FSUB.S')
class FsubInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 - s2
@concreteInstruction('FMUL.S')
class FmulInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 * s2
@concreteInstruction('FDIV.S')
class FdivInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 / s2
@concreteInstruction('FMIN.S')
class FminInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 if s1 < s2 else s2
@concreteInstruction('FMAX.S')
class FmaxInstruction(FRInstruction) :
def funcExec(self, s1, s2) :
return s1 if s1 > s2 else s2
@concreteInstruction('FSQRT.S')
class FsqrtInstruction(FORInstruction) :
def funcExec(self, s1) :
return s1 ** 0.5
@concreteInstruction('FMV.S')
class FmvInstruction(FORInstruction) :
def funcExec(self, s1) :
return s1
@concreteInstruction('FABS.S')
class FabsInstruction(FORInstruction) :
def funcExec(self, s1) :
return abs(s1)
@concreteInstruction('FNEG.S')
class FnegInstruction(FORInstruction) :
def funcExec(self, s1) :
return -1 * s1
@concreteInstruction('FLT.S')
class FltInstruction(FCmpInstruction) :
def funcExec(self, s1, s2) :
return 1 if s1 < s2 else 0
@concreteInstruction('FLE.S')
class FleInstruction(FCmpInstruction) :
def funcExec(self, s1, s2) :
return 1 if s1 <= s2 else 0
@concreteInstruction('FEQ.S')
class FeqInstruction(FCmpInstruction) :
def funcExec(self, s1, s2) :
return 1 if s1 == s2 else 0
@concreteInstruction('LW')
class LwInstruction(LDInstruction) :
@property
def dsttype(self) :
return int
@concreteInstruction('SW')
class SwInstruction(STInstruction) :
@property
def srctype(self) :
return int
@concreteInstruction('FLW')
class FlwInstruction(LDInstruction) :
@property
def dsttype(self) :
return float
@concreteInstruction('FSW')
class FswInstruction(STInstruction) :
@property
def srctype(self) :
return float
@concreteInstruction('NOP')
class NopInstruction(Instruction) :
@classmethod
def parseInstruction(cls, inst) :
match = re.match(r'(\S+)', inst)
cls(match[1])
def exec(self) :
config.machine.pc += 4
def __str__(self) :
return self.opcode
@concreteInstruction('BGE')
class BgeInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 >= val2 else False
@concreteInstruction('BLE')
class BleInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 <= val2 else False
@concreteInstruction('BGT')
class BgtInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 > val2 else False
@concreteInstruction('BLT')
class BltInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 < val2 else False
@concreteInstruction('BEQ')
class BeqInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 == val2 else False
@concreteInstruction('BNE')
class BneInstruction(BranchInstruction) :
def funcExec(self, val1, val2) :
return True if val1 != val2 else False
#### Custom instructions -- not actually part of RISC-V instruction set ####
#Load address to register
@concreteInstruction('LA')
class LaInstruction(MIUInstruction) :
def funcExec(self, imm) :
return imm
#Load immediate to register -- same as la
@concreteInstruction('LI')
class LiInstruction(MIUInstruction) :
def funcExec(self, imm) :
return imm
#store FP immediate to register
@concreteInstruction('FIMM.S')
class FimmInstruction(FUInstruction) :
def funcExec(self, imm) :
return imm
#move floating point to integer
@concreteInstruction('FMOVI.S')
class FmoviInstruction(FORInstruction) :
def funcExec(self, src1) :
return int(src1)
@property
def srctype(self) :
return float
@property
def dsttype(self) :
return int
#move integer to floating point
@concreteInstruction('IMOVF.S')
class ImovfInstruction(FORInstruction) :
def funcExec(self, src1) :
return float(src1)
@property
def dsttype(self) :
return float
@property
def srctype(self) :
return int
#read integer from stdin
@concreteInstruction('GETI')
class GetiInstruction(InputInstruction) :
@property
def dsttype(self) :
return int
#read float from stdin
@concreteInstruction('GETF')
class GetfInstruction(InputInstruction) :
@property
def dsttype(self) :
return float
@concreteInstruction('PUTI')
class PutiInstruction(OutputInstruction) :
@property
def srctype(self) :
return int
@concreteInstruction('PUTF')
class PutfInstruction(OutputInstruction) :
@property
def srctype(self) :
return float
@concreteInstruction('PUTS')
class PutsInstruction(IOInstruction) :
def __init__(self, reg, opcode) :
self.reg = reg
self.opcode = opcode
def exec(self) :
addr = config.machine.registerFile[self.reg].read()
assert (addr >= config.machine.memory.strings[0] and addr < config.machine.memory.strings[1]), "Writing string from a bad address"
print(config.machine.memory[addr], end = '')
config.machine.pc += 4
@concreteInstruction('HALT')
class HaltInstruction(Instruction) :
@classmethod
def parse(cls, inst) :
match = re.match(r'(\S+)', inst)
return cls(match[1])
def exec(self) :
#HALT by moving pc to -1
config.machine.pc = -1
def __str__(self) :
return self.opcode
@concreteInstruction('MALLOC')
class MallocInstruction(Instruction) :
@classmethod
def parse(cls, inst) :
match = re.match(r'(\S+) (\S+), (\S+)', inst)
return cls(match[2], match[3], match[1])
def __init__(self, dstReg, sizeReg, opcode) :
self.dstReg = dstReg
self.sizeReg = sizeReg
self.opcode = opcode
def exec(self) :
config.machine.timingModel.exec(self)
sizeReg = config.machine.registerFile[self.sizeReg]
assert sizeReg.type == int, "Size register is not an integer"
size = sizeReg.read()
#call the memory allocator to allocate sizeReg amount of space
addr = config.machine.memoryManager.malloc(size)
# print("Allocated at address " + str(addr));
destReg = config.machine.registerFile[self.dstReg]
assert destReg.type == int, "Address not being stored in integer reg"
config.machine.registerFile[self.dstReg].write(addr)
config.machine.pc += 4
def __str__(self) :
return str(self.opcode + " " + self.dstReg + " " + self.sizeReg)
@concreteInstruction('FREE')
class FreeInstruction(Instruction) :
@classmethod
def parse(cls, inst) :
match = re.match(r'(\S+) (\S+)', inst)
return cls(match[2], match[1])
def __init__(self, addrReg, opcode) :
self.addrReg = addrReg
self.opcode = opcode
def exec(self) :
config.machine.timingModel.exec(self)
addrReg = config.machine.registerFile[self.addrReg]
assert addrReg.type == int, "address needs to be an integer reg"
addr = addrReg.read()
# print("Freeing address " + str(addr));
#call the memory allocator to release the address
config.machine.memoryManager.free(addr)
config.machine.pc += 4
def __str__(self) :
return str(self.opcode + " " + self.addrReg)
#### unimplemented instructions ####
@concreteInstruction('AUIPC')
class AuipcInstruction(IUInstruction) :
pass
@concreteInstruction('SLTIU')
class SltiuInstruction(IInstruction) :
pass
@concreteInstruction('SRAI')
class SraiInstruction(IInstruction) :
pass
@concreteInstruction('SLTU')
class SltuInstruction(IRInstruction) :
pass