forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.cpp
11026 lines (9342 loc) · 252 KB
/
Core.cpp
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
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cfenv>
#include <cmath>
#include <map>
#include <boost/format.hpp>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <assert.h>
#include <signal.h>
#include "Core.hpp"
#include "instforms.hpp"
using namespace WdRiscv;
template <typename TYPE>
static
bool
parseNumber(const std::string& numberStr, TYPE& number)
{
bool good = not numberStr.empty();
if (good)
{
char* end = nullptr;
if (sizeof(TYPE) == 4)
number = strtoul(numberStr.c_str(), &end, 0);
else if (sizeof(TYPE) == 8)
number = strtoull(numberStr.c_str(), &end, 0);
else
{
std::cerr << "parseNumber: Only 32/64-bit RISCV cores supported\n";
return false;
}
if (end and *end)
good = false; // Part of the string are non parseable.
}
return good;
}
template <typename URV>
Core<URV>::Core(unsigned hartId, Memory& memory, unsigned intRegCount)
: hartId_(hartId), memory_(memory), intRegs_(intRegCount), fpRegs_(32)
{
regionHasLocalMem_.resize(16);
// Tie the retired instruction and cycle counter CSRs to variable
// held in the core.
if constexpr (sizeof(URV) == 4)
{
URV* low = reinterpret_cast<URV*> (&retiredInsts_);
URV* high = low + 1;
auto& mirLow = csRegs_.regs_.at(size_t(CsrNumber::MINSTRET));
mirLow.tie(low);
auto& mirHigh = csRegs_.regs_.at(size_t(CsrNumber::MINSTRETH));
mirHigh.tie(high);
low = reinterpret_cast<URV*> (&cycleCount_);
high = low + 1;
auto& mcycleLow = csRegs_.regs_.at(size_t(CsrNumber::MCYCLE));
mcycleLow.tie(low);
auto& mcycleHigh = csRegs_.regs_.at(size_t(CsrNumber::MCYCLEH));
mcycleHigh.tie(high);
}
else
{
csRegs_.regs_.at(size_t(CsrNumber::MINSTRET)).tie(&retiredInsts_);
csRegs_.regs_.at(size_t(CsrNumber::MCYCLE)).tie(&cycleCount_);
}
}
template <typename URV>
Core<URV>::~Core()
{
}
template <typename URV>
void
Core<URV>::getImplementedCsrs(std::vector<CsrNumber>& vec) const
{
vec.clear();
for (unsigned i = 0; i <= unsigned(CsrNumber::MAX_CSR_); ++i)
{
CsrNumber csrn = CsrNumber(i);
if (csRegs_.getImplementedCsr(csrn))
vec.push_back(csrn);
}
}
template <typename URV>
void
Core<URV>::reset(bool resetMemoryMappedRegs)
{
intRegs_.reset();
csRegs_.reset();
// Suppress resetting memory mapped register on initial resets sent
// by the test bench. Otherwise, initial resets obliterate memory
// mapped register data loaded from the ELF file.
if (resetMemoryMappedRegs)
memory_.resetMemoryMappedRegisters();
clearTraceData();
clearPendingNmi();
storeQueue_.clear();
loadQueue_.clear();
pc_ = resetPc_;
currPc_ = resetPc_;
// Enable extension if corresponding bits are set in the MISA CSR.
// D requires F and is enabled only if F is enabled.
rvm_ = false;
rvc_ = false;
URV value = 0;
if (peekCsr(CsrNumber::MISA, value))
{
if (value & 1) // Atomic ('a') option.
rva_ = true;
if (value & (URV(1) << ('c' - 'a'))) // Compress option.
rvc_ = true;
if (value & (URV(1) << ('f' - 'a'))) // Single precision FP
{
rvf_ = true;
bool isDebug = false;
// Make sure FCSR/FRM/FFLAGS are enabled if F extension is on.
if (not csRegs_.getImplementedCsr(CsrNumber::FCSR))
csRegs_.configCsr("fcsr", true, 0, 0xff, 0xff, isDebug);
if (not csRegs_.getImplementedCsr(CsrNumber::FRM))
csRegs_.configCsr("frm", true, 0, 0x7, 0x7, isDebug);
if (not csRegs_.getImplementedCsr(CsrNumber::FFLAGS))
csRegs_.configCsr("fflags", true, 0, 0x1f, 0x1f, isDebug);
}
if (value & (URV(1) << ('d' - 'a'))) // Double precision FP.
{
if (rvf_)
rvd_ = true;
else
std::cerr << "Bit 3 (d) is set in the MISA register but f "
<< "extension (bit 5) is not enabled -- ignored\n";
}
if (not (value & (URV(1) << ('i' - 'a'))))
std::cerr << "Bit 8 (i extension) is cleared in the MISA register "
<< " but extension is mandatory -- assuming bit 8 set\n";
if (value & (URV(1) << ('m' - 'a'))) // Multiply/divide option.
rvm_ = true;
if (value & (URV(1) << ('u' - 'a'))) // User-mode option.
rvu_ = true;
if (value & (URV(1) << ('s' - 'a'))) // Supervisor-mode option.
rvs_ = true;
for (auto ec : { 'b', 'e', 'g', 'h', 'j', 'k', 'l', 'n', 'o', 'p',
'q', 'r', 't', 'v', 'w', 'x', 'y', 'z' } )
{
unsigned bit = ec - 'a';
if (value & (URV(1) << bit))
std::cerr << "Bit " << bit << " (" << ec << ") set in the MISA "
<< "register but extension is not supported "
<< "-- ignored\n";
}
}
prevCountersCsrOn_ = true;
countersCsrOn_ = true;
if (peekCsr(CsrNumber::MGPMC, value))
{
countersCsrOn_ = (value & 1) == 1;
prevCountersCsrOn_ = countersCsrOn_;
}
debugMode_ = false;
debugStepMode_ = false;
dcsrStepIe_ = false;
dcsrStep_ = false;
if (csRegs_.peek(CsrNumber::DCSR, value))
{
dcsrStep_ = (value >> 2) & 1;
dcsrStepIe_ = (value >> 11) & 1;
}
}
template <typename URV>
bool
Core<URV>::loadHexFile(const std::string& file)
{
return memory_.loadHexFile(file);
}
template <typename URV>
bool
Core<URV>::loadElfFile(const std::string& file, size_t& entryPoint,
size_t& exitPoint)
{
return memory_.loadElfFile(file, entryPoint, exitPoint);
}
template <typename URV>
bool
Core<URV>::peekMemory(size_t address, uint8_t& val) const
{
return memory_.readByte(address, val);
}
template <typename URV>
bool
Core<URV>::peekMemory(size_t address, uint16_t& val) const
{
if (memory_.readHalfWord(address, val))
return true;
// We may have failed because location is in instruction space.
return memory_.readInstHalfWord(address, val);
}
template <typename URV>
bool
Core<URV>::peekMemory(size_t address, uint32_t& val) const
{
if (memory_.readWord(address, val))
return true;
// We may have failed because location is in instruction space.
return memory_.readInstWord(address, val);
}
template <typename URV>
bool
Core<URV>::peekMemory(size_t address, uint64_t& val) const
{
uint32_t high = 0, low = 0;
if (memory_.readWord(address, low) and memory_.readWord(address + 4, high))
{
val = (uint64_t(high) << 32) | low;
return true;
}
// We may have failed because location is in instruction space.
if (memory_.readInstWord(address, low) and
memory_.readInstWord(address + 4, high))
{
val = (uint64_t(high) << 32) | low;
return true;
}
return true;
}
template <typename URV>
bool
Core<URV>::pokeMemory(size_t address, uint8_t val)
{
return memory_.pokeByte(address, val);
}
template <typename URV>
bool
Core<URV>::pokeMemory(size_t address, uint16_t val)
{
return memory_.poke(address, val);
}
template <typename URV>
bool
Core<URV>::pokeMemory(size_t address, uint32_t val)
{
// We allow poke to bypass masking for memory mapped registers
// otherwise, there is no way for external driver to clear bits that
// are read-only to this core.
return memory_.poke(address, val);
}
template <typename URV>
bool
Core<URV>::pokeMemory(size_t address, uint64_t val)
{
return memory_.poke(address, val);
}
template <typename URV>
void
Core<URV>::setPendingNmi(NmiCause cause)
{
nmiPending_ = true;
if (nmiCause_ == NmiCause::STORE_EXCEPTION or
nmiCause_ == NmiCause::LOAD_EXCEPTION)
; // Load/store exception is sticky -- do not over-write it.
else
nmiCause_ = cause;
URV val = 0; // DCSR value
if (peekCsr(CsrNumber::DCSR, val))
{
val |= 1 << 3; // nmip bit
pokeCsr(CsrNumber::DCSR, val);
recordCsrWrite(CsrNumber::DCSR);
}
}
template <typename URV>
void
Core<URV>::clearPendingNmi()
{
nmiPending_ = false;
nmiCause_ = NmiCause::UNKNOWN;
URV val = 0; // DCSR value
if (peekCsr(CsrNumber::DCSR, val))
{
val &= ~(URV(1) << 3); // nmip bit
pokeCsr(CsrNumber::DCSR, val);
recordCsrWrite(CsrNumber::DCSR);
}
}
template <typename URV>
void
Core<URV>::setToHostAddress(size_t address)
{
toHost_ = address;
toHostValid_ = true;
}
template <typename URV>
void
Core<URV>::clearToHostAddress()
{
toHost_ = 0;
toHostValid_ = false;
}
template <typename URV>
void
Core<URV>::putInStoreQueue(unsigned size, size_t addr, uint64_t data,
uint64_t prevData)
{
if (maxStoreQueueSize_ == 0 or memory_.isLastWriteToDccm())
return;
if (storeQueue_.size() >= maxStoreQueueSize_)
{
for (size_t i = 1; i < maxStoreQueueSize_; ++i)
storeQueue_[i-1] = storeQueue_[i];
storeQueue_[maxStoreQueueSize_-1] = StoreInfo(size, addr, data,
prevData);
}
else
storeQueue_.push_back(StoreInfo(size, addr, data, prevData));
}
template <typename URV>
void
Core<URV>::putInLoadQueue(unsigned size, size_t addr, unsigned regIx,
uint64_t data)
{
if (not loadQueueEnabled_)
return;
if (memory_.isAddrInDccm(addr))
{
// Blocking load. Invalidate target register in load queue so
// that it will not be reverted.
invalidateInLoadQueue(regIx);
return;
}
if (loadQueue_.size() >= maxLoadQueueSize_)
{
for (size_t i = 1; i < maxLoadQueueSize_; ++i)
loadQueue_[i-1] = loadQueue_[i];
loadQueue_[maxLoadQueueSize_-1] = LoadInfo(size, addr, regIx, data);
}
else
loadQueue_.push_back(LoadInfo(size, addr, regIx, data));
}
template <typename URV>
void
Core<URV>::invalidateInLoadQueue(unsigned regIx)
{
// Replace entry containing target register with x0 so that load exception
// matching entry will not revert target register.
for (unsigned i = 0; i < loadQueue_.size(); ++i)
if (loadQueue_[i].regIx_ == regIx)
loadQueue_[i].regIx_ = RegX0;
}
template <typename URV>
void
Core<URV>::removeFromLoadQueue(unsigned regIx)
{
if (regIx == 0)
return;
// Last (most recent) matching entry is removed. Subsequent entries
// are invalidated.
bool last = true;
size_t removeIx = loadQueue_.size();
for (size_t i = loadQueue_.size(); i > 0; --i)
{
auto& entry = loadQueue_.at(i-1);
if (entry.regIx_ == regIx)
{
if (last)
{
removeIx = i-1;
last = false;
}
else
entry.regIx_ = 0;
}
}
if (removeIx < loadQueue_.size())
loadQueue_.erase(loadQueue_.begin() + removeIx);
}
template <typename URV>
inline
void
Core<URV>::execBeq(uint32_t rs1, uint32_t rs2, int32_t offset)
{
if (intRegs_.read(rs1) != intRegs_.read(rs2))
return;
pc_ = currPc_ + SRV(offset);
pc_ = (pc_ >> 1) << 1; // Clear least sig bit.
lastBranchTaken_ = true;
}
template <typename URV>
inline
void
Core<URV>::execBne(uint32_t rs1, uint32_t rs2, int32_t offset)
{
if (intRegs_.read(rs1) == intRegs_.read(rs2))
return;
pc_ = currPc_ + SRV(offset);
pc_ = (pc_ >> 1) << 1; // Clear least sig bit.
lastBranchTaken_ = true;
}
template <typename URV>
inline
void
Core<URV>::execAddi(uint32_t rd, uint32_t rs1, int32_t imm)
{
SRV v = intRegs_.read(rs1) + SRV(imm);
intRegs_.write(rd, v);
}
template <typename URV>
inline
void
Core<URV>::execAdd(uint32_t rd, uint32_t rs1, int32_t rs2)
{
URV v = intRegs_.read(rs1) + intRegs_.read(rs2);
intRegs_.write(rd, v);
}
template <typename URV>
inline
void
Core<URV>::execAndi(uint32_t rd, uint32_t rs1, int32_t imm)
{
URV v = intRegs_.read(rs1) & SRV(imm);
intRegs_.write(rd, v);
}
template <typename URV>
bool
Core<URV>::isIdempotentRegion(size_t addr) const
{
unsigned region = addr >> (sizeof(URV)*8 - 4);
URV mracVal = 0;
if (csRegs_.read(CsrNumber::MRAC, PrivilegeMode::Machine, debugMode_,
mracVal))
{
unsigned bit = (mracVal >> (region*2 + 1)) & 1;
return bit == 0 or regionHasLocalMem_.at(region);
}
return true;
}
template <typename URV>
bool
Core<URV>::applyStoreException(URV addr, unsigned& matches)
{
bool prevLocked = csRegs_.mdseacLocked();
if (not prevLocked)
{
pokeCsr(CsrNumber::MDSEAC, addr); // MDSEAC is read only: Poke it.
csRegs_.lockMdseac(true);
setPendingNmi(NmiCause::STORE_EXCEPTION);
}
recordCsrWrite(CsrNumber::MDSEAC); // Always record change (per Ajay Nath)
if (not storeErrorRollback_)
{
matches = 1;
return true;
}
matches = 0;
for (const auto& entry : storeQueue_)
if (entry.size_ > 0 and addr >= entry.addr_ and
addr < entry.addr_ + entry.size_)
matches++;
if (matches != 1)
{
std::cerr << "Error: Store exception at 0x" << std::hex << addr;
if (matches == 0)
std::cerr << " does not match any address in the store queue\n";
else
std::cerr << " matches " << std::dec << matches << " entries"
<< " in the store queue\n";
return false;
}
// Undo matching item and remove it from queue (or replace with
// portion crossing double-word boundary). Restore previous
// bytes up to a double-word boundary.
bool hit = false; // True when address is found.
size_t undoBegin = addr, undoEnd = 0;
size_t removeIx = storeQueue_.size();
for (size_t ix = 0; ix < storeQueue_.size(); ++ix)
{
auto& entry = storeQueue_.at(ix);
size_t entryEnd = entry.addr_ + entry.size_;
if (hit)
{
// Re-play portions of subsequent (to one with exception)
// transactions covering undone bytes.
uint64_t data = entry.newData_;
for (size_t ba = entry.addr_; ba < entryEnd; ++ba, data >>= 8)
if (ba >= undoBegin and ba < undoEnd)
pokeMemory(ba, uint8_t(data));
}
else if (addr >= entry.addr_ and addr < entryEnd)
{
uint64_t prevData = entry.prevData_, newData = entry.newData_;
hit = true;
removeIx = ix;
size_t offset = addr - entry.addr_;
prevData >>= offset*8; newData >>= offset*8;
for (size_t i = offset; i < entry.size_; ++i)
{
pokeMemory(addr++, uint8_t(prevData));
prevData >>= 8; newData >>= 8;
undoEnd = addr;
if ((addr & 7) != 0)
continue; // Keep undoing store till double word boundary
// Reached double word boundary: trim & keep rest of store record
if (i + 1 < entry.size_)
{
entry = StoreInfo(entry.size_-i-1, addr, newData, prevData);
removeIx = storeQueue_.size(); // Squash entry removal.
break;
}
}
}
}
if (removeIx < storeQueue_.size())
{
for (size_t i = removeIx + 1; i < storeQueue_.size(); ++i)
storeQueue_.at(i-1) = storeQueue_.at(i);
storeQueue_.resize(storeQueue_.size() - 1);
}
return true;
}
template <typename URV>
bool
Core<URV>::applyLoadException(URV addr, unsigned& matches)
{
bool prevLocked = csRegs_.mdseacLocked();
if (not prevLocked)
{
pokeCsr(CsrNumber::MDSEAC, addr); // MDSEAC is read only: Poke it.
csRegs_.lockMdseac(true);
setPendingNmi(NmiCause::LOAD_EXCEPTION);
}
recordCsrWrite(CsrNumber::MDSEAC); // Always record change (per Ajay Nath)
if (not loadErrorRollback_)
{
matches = 1;
return true;
}
// Count matching records. Determine if there is an entry with the
// same register as the first match but younger.
bool hasYounger = false;
unsigned targetReg = 0; // Register of 1st match.
matches = 0;
unsigned zMatches = 0; // Matching records where target register is zero.
for (const LoadInfo& li : loadQueue_)
{
if (matches and targetReg == li.regIx_)
hasYounger = true;
if (li.size_ > 0 and addr >= li.addr_ and addr < li.addr_ + li.size_)
{
if (li.regIx_ != 0)
{
targetReg = li.regIx_;
matches++;
}
else
zMatches++;
}
}
if (matches == 0 and zMatches)
{
matches = 1;
return true;
}
matches += zMatches;
if (matches != 1)
{
std::cerr << "Error: Load exception at 0x" << std::hex << addr;
if (matches == 0)
std::cerr << " does not match any entry in the load queue\n";
else
std::cerr << " matches " << std::dec << matches << " entries"
<< " in the load queue\n";
return false;
}
// Revert register of matching item unless there are younger entries
// with same resister. Revert with value of older entry with same
// target register (if multiple such entry, use oldest). Invalidate
// all older entries with same target. Remove item from queue.
// Update prev-data of 1st younger item with same target register.
size_t removeIx = loadQueue_.size();
for (size_t ix = 0; ix < loadQueue_.size(); ++ix)
{
auto& entry = loadQueue_.at(ix);
size_t entryEnd = entry.addr_ + entry.size_;
bool match = entry.regIx_ and addr >= entry.addr_ and addr < entryEnd;
if (not match)
continue;
removeIx = ix;
URV prev = entry.prevData_;
// Revert to oldest entry with same target reg. Invalidate older
// entries with same target reg.
for (size_t ix2 = removeIx; ix2 > 0; --ix2)
{
auto& entry2 = loadQueue_.at(ix2-1);
if (entry2.regIx_ == entry.regIx_)
{
prev = entry2.prevData_;
entry2.regIx_ = RegX0;
}
}
if (not hasYounger)
pokeIntReg(entry.regIx_, prev);
// Update prev-data of 1st younger item with same target reg.
for (size_t ix2 = removeIx + 1; ix2 < loadQueue_.size(); ++ix2)
{
auto& entry2 = loadQueue_.at(ix2);
if (entry2.regIx_ == entry.regIx_)
{
entry2.prevData_ = prev;
break;
}
}
break;
}
if (removeIx < loadQueue_.size())
{
for (size_t i = removeIx + 1; i < loadQueue_.size(); ++i)
loadQueue_.at(i-1) = loadQueue_.at(i);
loadQueue_.resize(loadQueue_.size() - 1);
}
return true;
}
template <typename URV>
bool
Core<URV>::applyLoadFinished(URV addr, unsigned& matches)
{
if (not loadErrorRollback_)
{
matches = 1;
return true;
}
// Count matching records.
matches = 0;
unsigned zMatches = 0; // Matching records where target register is zero.
size_t matchIx = 0; // Index of oldest matchine entry.
size_t zMatchIx = 0; // Index of oldest matchine entry with zero register.
size_t size = loadQueue_.size();
for (size_t i = 0; i < size; ++i)
{
const LoadInfo& li = loadQueue_.at(i);
if (li.addr_ == addr)
{
if (li.regIx_ != 0)
{
if (not matches)
matchIx = i;
matches++;
}
else
{
if (not zMatches)
zMatchIx = i;
zMatches++;
}
}
}
if (matches > 1)
{
std::cerr << "Error: Load finished at 0x" << std::hex << addr;
std::cerr << " matches " << std::dec << matches << " entries"
<< " in the load queue\n";
}
if (matches == 0 and zMatches == 0)
{
std::cerr << "Warning: Load finished at 0x" << std::hex << addr;
std::cerr << " does not match any entry in the load queue\n";
return true;
}
if (matches)
{
// Process entries in reverse order (start with youngest)
LoadInfo& entry = loadQueue_.at(matchIx);
// Mark all earlier entries with same target register as invalid.
// Identify earliest previous value of target register.
unsigned targetReg = entry.regIx_;
size_t prevIx = matchIx;
URV prev = entry.prevData_; // Previous value of target reg.
for (size_t j = 0; j < matchIx; ++j)
{
LoadInfo& li = loadQueue_.at(j);
if (li.regIx_ != targetReg)
continue;
li.regIx_ = 0;
if (j < prevIx)
{
prevIx = j;
prev = li.prevData_;
}
}
// Update prev-data of 1st subsequent entry with same target.
for (size_t j = matchIx + 1; j < size; ++j)
if (loadQueue_.at(j).regIx_ == targetReg)
{
loadQueue_.at(j).prevData_ = prev;
break;
}
}
// Remove from matching entry or invalid matching entry.
if (matches or zMatches)
{
size_t ixToRemove = matches? matchIx : zMatchIx;
size_t newSize = 0;
for (size_t i = 0; i < size; ++i)
{
auto& li = loadQueue_.at(i);
bool remove = i == ixToRemove; // or (li.addr_ == addr and li.regIx_ == 0);
if (remove)
continue;
if (newSize != i)
loadQueue_.at(newSize) = li;
newSize++;
}
loadQueue_.resize(newSize);
}
return matches == 1 or (matches == 0 and zMatches);
}
static
void
printUnsignedHisto(const char* tag, const std::vector<uint64_t>& histo,
FILE* file)
{
if (histo.size() < 7)
return;
if (histo.at(0))
fprintf(file, " %s 0 %ld\n", tag, histo.at(0));
if (histo.at(1))
fprintf(file, " %s 1 %ld\n", tag, histo.at(1));
if (histo.at(2))
fprintf(file, " %s 2 %ld\n", tag, histo.at(2));
if (histo.at(3))
fprintf(file, " %s (2, 16] %ld\n", tag, histo.at(3));
if (histo.at(4))
fprintf(file, " %s (16, 1k] %ld\n", tag, histo.at(4));
if (histo.at(5))
fprintf(file, " %s (1k, 64k] %ld\n", tag, histo.at(5));
if (histo.at(6))
fprintf(file, " %s > 64k %ld\n", tag, histo.at(6));
}
static
void
printSignedHisto(const char* tag, const std::vector<uint64_t>& histo,
FILE* file)
{
if (histo.size() < 13)
return;
if (histo.at(0))
fprintf(file, " %s <= 64k %ld\n", tag, histo.at(0));
if (histo.at(1))
fprintf(file, " %s (-64k, -1k] %ld\n", tag, histo.at(1));
if (histo.at(2))
fprintf(file, " %s (-1k, -16] %ld\n", tag, histo.at(2));
if (histo.at(3))
fprintf(file, " %s (-16, -3] %ld\n", tag, histo.at(3));
if (histo.at(4))
fprintf(file, " %s -2 %ld\n", tag, histo.at(4));
if (histo.at(5))
fprintf(file, " %s -1 %ld\n", tag, histo.at(5));
if (histo.at(6))
fprintf(file, " %s 0 %ld\n", tag, histo.at(6));
if (histo.at(7))
fprintf(file, " %s 1 %ld\n", tag, histo.at(7));
if (histo.at(8))
fprintf(file, " %s 2 %ld\n", tag, histo.at(8));
if (histo.at(9))
fprintf(file, " %s (2, 16] %ld\n", tag, histo.at(9));
if (histo.at(10))
fprintf(file, " %s (16, 1k] %ld\n", tag, histo.at(10));
if (histo.at(11))
fprintf(file, " %s (1k, 64k] %ld\n", tag, histo.at(11));
if (histo.at(12))
fprintf(file, " %s > 64k %ld\n", tag, histo.at(12));
}
template <typename URV>
inline
void
Core<URV>::reportInstructionFrequency(FILE* file) const
{
struct CompareFreq
{
CompareFreq(const std::vector<InstProfile>& profileVec)
: profileVec(profileVec)
{ }
bool operator()(size_t a, size_t b) const
{ return profileVec.at(a).freq_ < profileVec.at(b).freq_; }
const std::vector<InstProfile>& profileVec;
};
std::vector<size_t> indices(instProfileVec_.size());
for (size_t i = 0; i < indices.size(); ++i)
indices.at(i) = i;
std::sort(indices.begin(), indices.end(), CompareFreq(instProfileVec_));
for (size_t i = 0; i < indices.size(); ++i)
{
size_t ix = indices.at(i);
InstId id = InstId(ix);
const InstInfo& info = instTable_.getInstInfo(id);
const InstProfile& prof = instProfileVec_.at(ix);
uint64_t freq = prof.freq_;
if (not freq)
continue;
fprintf(file, "%s %ld\n", info.name().c_str(), freq);
unsigned regCount = intRegCount();
uint64_t count = 0;
for (auto n : prof.rd_) count += n;
if (count)
{
fprintf(file, " +rd");
for (unsigned i = 0; i < regCount; ++i)
if (prof.rd_.at(i))
fprintf(file, " %d:%ld", i, prof.rd_.at(i));
fprintf(file, "\n");
}
uint64_t count1 = 0;
for (auto n : prof.rs1_) count1 += n;
if (count1)
{
fprintf(file, " +rs1");
for (unsigned i = 0; i < regCount; ++i)
if (prof.rs1_.at(i))
fprintf(file, " %d:%ld", i, prof.rs1_.at(i));
fprintf(file, "\n");
const auto& histo = prof.rs1Histo_;
if (info.isUnsigned())
printUnsignedHisto("+hist1", histo, file);
else
printSignedHisto("+hist1", histo, file);
}