gem5 v24.0.0.0
Loading...
Searching...
No Matches
pcstate.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 ARM Limited
3 * Copyright (c) 2023 The University of Edinburgh
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2010 Gabe Black
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#ifndef __ARCH_GENERIC_TYPES_HH__
43#define __ARCH_GENERIC_TYPES_HH__
44
45#include <iostream>
46#include <memory>
47#include <type_traits>
48
49#include "base/compiler.hh"
50#include "base/trace.hh"
51#include "base/types.hh"
52#include "sim/serialize.hh"
53
54namespace gem5
55{
56
57// The guaranteed interface.
59{
60 protected:
61 Addr _pc = 0;
63
64 PCStateBase(const PCStateBase &other) : _pc(other._pc), _upc(other._upc) {}
65 PCStateBase &operator=(const PCStateBase &other) = default;
67
68 public:
69 virtual ~PCStateBase() = default;
70
71 template<class Target>
72 Target &
74 {
75 return static_cast<Target &>(*this);
76 }
77
78 template<class Target>
79 const Target &
80 as() const
81 {
82 return static_cast<const Target &>(*this);
83 }
84
85 virtual PCStateBase *clone() const = 0;
86 virtual void
87 update(const PCStateBase &other)
88 {
89 _pc = other._pc;
90 _upc = other._upc;
91 }
92 void update(const PCStateBase *ptr) { update(*ptr); }
93
94 virtual void output(std::ostream &os) const = 0;
95
96 virtual bool
97 equals(const PCStateBase &other) const
98 {
99 return _pc == other._pc && _upc == other._upc;
100 }
101
107 Addr
108 instAddr() const
109 {
110 return _pc;
111 }
112
118 MicroPC
119 microPC() const
120 {
121 return _upc;
122 }
123
124 virtual void
126 {
127 _upc = 0;
128 }
129
130 virtual void
132 {
133 _pc = val;
134 _upc = 0;
135 }
136
137 virtual void advance() = 0;
138 virtual bool branching() const = 0;
139
140 void
141 serialize(CheckpointOut &cp) const override
142 {
145 }
146
147 void
153};
154
155static inline std::ostream &
156operator<<(std::ostream & os, const PCStateBase &pc)
157{
158 pc.output(os);
159 return os;
160}
161
162static inline bool
164{
165 return a.equals(b);
166}
167
168static inline bool
170{
171 return !a.equals(b);
172}
173
174namespace
175{
176
177inline void
178set(PCStateBase *&dest, const PCStateBase *src)
179{
180 if (GEM5_LIKELY(dest)) {
181 if (GEM5_LIKELY(src)) {
182 // Both src and dest already have storage, so just copy contents.
183 dest->update(src);
184 } else {
185 // src is empty, so clear out dest.
186 dest = nullptr;
187 }
188 } else {
189 if (GEM5_LIKELY(src)) {
190 // dest doesn't have storage, so create some as a copy of src.
191 dest = src->clone();
192 } else {
193 // dest is already nullptr, so nothing to do.
194 }
195 }
196}
197
198inline void
199set(std::unique_ptr<PCStateBase> &dest, const PCStateBase *src)
200{
201 PCStateBase *dest_ptr = dest.get();
202 set(dest_ptr, src);
203 if (dest.get() != dest_ptr)
204 dest.reset(dest_ptr);
205}
206
207inline void
208set(PCStateBase *&dest, const std::unique_ptr<PCStateBase> &src)
209{
210 const PCStateBase *src_ptr = src.get();
211 set(dest, src_ptr);
212}
213
214inline void
215set(std::unique_ptr<PCStateBase> &dest,
216 const std::unique_ptr<PCStateBase> &src)
217{
218 PCStateBase *dest_ptr = dest.get();
219 const PCStateBase *src_ptr = src.get();
220 set(dest_ptr, src_ptr);
221 if (dest.get() != dest_ptr)
222 dest.reset(dest_ptr);
223}
224
225inline void
226set(PCStateBase *&dest, const PCStateBase &src)
227{
228 if (GEM5_LIKELY(dest)) {
229 // Update dest with the contents of src.
230 dest->update(src);
231 } else {
232 // Clone src over to dest.
233 dest = src.clone();
234 }
235}
236
237inline void
238set(std::unique_ptr<PCStateBase> &dest, const PCStateBase &src)
239{
240 PCStateBase *dest_ptr = dest.get();
241 set(dest_ptr, src);
242 if (dest.get() != dest_ptr)
243 dest.reset(dest_ptr);
244}
245
246inline void
247set(PCStateBase &dest, const PCStateBase &src)
248{
249 dest.update(src);
250}
251
252} // anonymous namespace
253
254namespace GenericISA
255{
256
258{
259 protected:
261
263
265 _npc(other._npc), _nupc(other._nupc)
266 {}
267 PCStateWithNext &operator=(const PCStateWithNext &other) = default;
269
270 public:
271 Addr pc() const { return _pc; }
272 void pc(Addr val) { _pc = val; }
273
274 Addr npc() const { return _npc; }
275 void npc(Addr val) { _npc = val; }
276
277 MicroPC upc() const { return _upc; }
278 void upc(MicroPC val) { _upc = val; }
279
280 MicroPC nupc() const { return _nupc; }
281 void nupc(MicroPC val) { _nupc = val; }
282
283 // Reset the macroop's upc without advancing the regular pc.
284 void
285 uReset() override
286 {
288 _nupc = 1;
289 }
290
291 void
293 {
294 npc(val);
295 }
296
297 void
298 output(std::ostream &os) const override
299 {
300 ccprintf(os, "(%#x=>%#x)", this->pc(), this->npc());
301 }
302
303 void
304 update(const PCStateBase &other) override
305 {
306 PCStateBase::update(other);
307 auto &pcstate = other.as<PCStateWithNext>();
308 _npc = pcstate._npc;
309 _nupc = pcstate._nupc;
310 }
311
312 bool
313 equals(const PCStateBase &other) const override
314 {
315 auto &ps = other.as<PCStateWithNext>();
316 return PCStateBase::equals(other) &&
317 _npc == ps._npc && _nupc == ps._nupc;
318 }
319
320 void
321 set(Addr val) override
322 {
324 _npc = 0;
325 _nupc = 1;
326 }
327
328 void
329 serialize(CheckpointOut &cp) const override
330 {
334 }
335
336 void
343};
344
345
346/*
347 * Different flavors of PC state. Only ISA specific code should rely on
348 * any particular type of PC state being available. All other code should
349 * use the interface above.
350 */
351
352// The most basic type of PC.
353template <int InstWidth>
355{
356 protected:
358
359 public:
360 SimplePCState(const SimplePCState &other) : Base(other) {}
361 SimplePCState &operator=(const SimplePCState &other) = default;
363 explicit SimplePCState(Addr val) { set(val); }
364
366 clone() const override
367 {
368 return new SimplePCState<InstWidth>(*this);
369 }
370
377 void
378 set(Addr val) override
379 {
380 Base::set(val);
381 this->npc(val + InstWidth);
382 };
383
384 bool
385 branching() const override
386 {
387 return this->npc() != this->pc() + InstWidth;
388 }
389
390 // Advance the PC.
391 void
392 advance() override
393 {
394 this->_pc = this->_npc;
395 this->_npc += InstWidth;
396 }
397};
398
399// A PC and microcode PC.
400template <int InstWidth>
401class UPCState : public SimplePCState<InstWidth>
402{
403 protected:
405
406 public:
407 void
408 output(std::ostream &os) const override
409 {
411 ccprintf(os, ".(%d=>%d)", this->upc(), this->nupc());
412 }
413
415 clone() const override
416 {
417 return new UPCState<InstWidth>(*this);
418 }
419
420 void
421 set(Addr val) override
422 {
423 Base::set(val);
424 this->upc(0);
425 this->nupc(1);
426 }
427
428 UPCState(const UPCState &other) : Base(other) {}
429 UPCState &operator=(const UPCState &other) = default;
431 explicit UPCState(Addr val) { set(val); }
432
433 bool
434 branching() const override
435 {
436 return this->npc() != this->pc() + InstWidth ||
437 this->nupc() != this->upc() + 1;
438 }
439
440 // Advance the upc within the instruction.
441 void
443 {
444 this->upc(this->nupc());
445 this->nupc(this->nupc() + 1);
446 }
447
448 // End the macroop by resetting the upc and advancing the regular pc.
449 void
451 {
452 this->advance();
453 this->upc(0);
454 this->nupc(1);
455 }
456};
457
458// A PC with a delay slot.
459template <int InstWidth>
460class DelaySlotPCState : public SimplePCState<InstWidth>
461{
462 protected:
464
466
467 public:
468 void
469 output(std::ostream &os) const override
470 {
471 ccprintf(os, "(%#x=>%#x=>%#x)", this->pc(), this->npc(), nnpc());
472 }
473
475 clone() const override
476 {
477 return new DelaySlotPCState<InstWidth>(*this);
478 }
479
480 void
481 update(const PCStateBase &other) override
482 {
483 Base::update(other);
484 auto &pcstate = other.as<DelaySlotPCState<InstWidth>>();
485 _nnpc = pcstate._nnpc;
486 }
487
488 Addr nnpc() const { return _nnpc; }
489 void nnpc(Addr val) { _nnpc = val; }
490
491 void
492 set(Addr val) override
493 {
494 Base::set(val);
495 nnpc(val + 2 * InstWidth);
496 }
497
499 Base(other), _nnpc(other._nnpc)
500 {}
504
505 bool
506 branching() const override
507 {
508 return !(this->nnpc() == this->npc() + InstWidth &&
509 (this->npc() == this->pc() + InstWidth ||
510 this->npc() == this->pc() + 2 * InstWidth));
511 }
512
513 // Advance the PC.
514 void
515 advance() override
516 {
517 this->_pc = this->_npc;
518 this->_npc = this->_nnpc;
519 this->_nnpc += InstWidth;
520 }
521
522 bool
523 equals(const PCStateBase &other) const override
524 {
525 auto &ps = other.as<DelaySlotPCState<InstWidth>>();
526 return Base::equals(other) && ps._nnpc == this->_nnpc;
527 }
528
529 void
530 serialize(CheckpointOut &cp) const override
531 {
532 Base::serialize(cp);
534 }
535
536 void
538 {
541 }
542};
543
544// A PC with a delay slot and a microcode PC.
545template <int InstWidth>
546class DelaySlotUPCState : public DelaySlotPCState<InstWidth>
547{
548 protected:
550
551 public:
552 void
553 output(std::ostream &os) const override
554 {
556 ccprintf(os, ".(%d=>%d)", this->upc(), this->nupc());
557 }
558
560 clone() const override
561 {
562 return new DelaySlotUPCState<InstWidth>(*this);
563 }
564
565 void
566 set(Addr val) override
567 {
568 Base::set(val);
569 this->upc(0);
570 this->nupc(1);
571 }
572
573 DelaySlotUPCState(const DelaySlotUPCState &other) : Base(other) {}
577
578 bool
579 branching() const override
580 {
581 return Base::branching() || this->nupc() != this->upc() + 1;
582 }
583
584 // Advance the upc within the instruction.
585 void
587 {
588 this->_upc = this->_nupc;
589 this->_nupc++;
590 }
591
592 // End the macroop by resetting the upc and advancing the regular pc.
593 void
595 {
596 this->advance();
597 this->_upc = 0;
598 this->_nupc = 1;
599 }
600};
601
602}
603
604} // namespace gem5
605
606#endif
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
bool branching() const override
Definition pcstate.hh:506
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition pcstate.hh:537
void set(Addr val) override
Definition pcstate.hh:492
DelaySlotPCState & operator=(const DelaySlotPCState &other)=default
DelaySlotPCState(const DelaySlotPCState &other)
Definition pcstate.hh:498
void output(std::ostream &os) const override
Definition pcstate.hh:469
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pcstate.hh:530
bool equals(const PCStateBase &other) const override
Definition pcstate.hh:523
SimplePCState< InstWidth > Base
Definition pcstate.hh:463
PCStateBase * clone() const override
Definition pcstate.hh:475
void update(const PCStateBase &other) override
Definition pcstate.hh:481
DelaySlotUPCState & operator=(const DelaySlotUPCState &other)=default
void output(std::ostream &os) const override
Definition pcstate.hh:553
DelaySlotPCState< InstWidth > Base
Definition pcstate.hh:549
void set(Addr val) override
Definition pcstate.hh:566
bool branching() const override
Definition pcstate.hh:579
PCStateBase * clone() const override
Definition pcstate.hh:560
DelaySlotUPCState(const DelaySlotUPCState &other)
Definition pcstate.hh:573
void set(Addr val) override
Definition pcstate.hh:321
void output(std::ostream &os) const override
Definition pcstate.hh:298
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition pcstate.hh:337
PCStateWithNext & operator=(const PCStateWithNext &other)=default
bool equals(const PCStateBase &other) const override
Definition pcstate.hh:313
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pcstate.hh:329
PCStateWithNext(const PCStateWithNext &other)
Definition pcstate.hh:264
void update(const PCStateBase &other) override
Definition pcstate.hh:304
bool branching() const override
Definition pcstate.hh:385
SimplePCState & operator=(const SimplePCState &other)=default
SimplePCState(const SimplePCState &other)
Definition pcstate.hh:360
void set(Addr val) override
Force this PC to reflect a particular value, resetting all its other fields around it.
Definition pcstate.hh:378
PCStateBase * clone() const override
Definition pcstate.hh:366
UPCState & operator=(const UPCState &other)=default
void set(Addr val) override
Force this PC to reflect a particular value, resetting all its other fields around it.
Definition pcstate.hh:421
void output(std::ostream &os) const override
Definition pcstate.hh:408
PCStateBase * clone() const override
Definition pcstate.hh:415
SimplePCState< InstWidth > Base
Definition pcstate.hh:404
UPCState(const UPCState &other)
Definition pcstate.hh:428
bool branching() const override
Definition pcstate.hh:434
virtual bool equals(const PCStateBase &other) const
Definition pcstate.hh:97
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition pcstate.hh:148
Target & as()
Definition pcstate.hh:73
virtual void set(Addr val)
Definition pcstate.hh:131
virtual bool branching() const =0
MicroPC microPC() const
Returns the current micropc.
Definition pcstate.hh:119
virtual void advance()=0
Addr instAddr() const
Returns the memory address of the instruction this PC points to.
Definition pcstate.hh:108
void update(const PCStateBase *ptr)
Definition pcstate.hh:92
PCStateBase & operator=(const PCStateBase &other)=default
const Target & as() const
Definition pcstate.hh:80
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition pcstate.hh:141
virtual void output(std::ostream &os) const =0
virtual void update(const PCStateBase &other)
Definition pcstate.hh:87
virtual ~PCStateBase()=default
virtual PCStateBase * clone() const =0
virtual void uReset()
Definition pcstate.hh:125
PCStateBase(const PCStateBase &other)
Definition pcstate.hh:64
Basic support for object serialization.
Definition serialize.hh:170
Bitfield< 18, 16 > ps
Bitfield< 7 > b
Bitfield< 8 > a
Definition misc_types.hh:66
Bitfield< 12, 11 > set
Bitfield< 4 > pc
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
static bool operator==(const PCStateBase &a, const PCStateBase &b)
Definition pcstate.hh:163
std::ostream CheckpointOut
Definition serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition matrix.hh:564
static bool operator!=(const PCStateBase &a, const PCStateBase &b)
Definition pcstate.hh:169
uint16_t MicroPC
Definition types.hh:149
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568

Generated on Tue Jun 18 2024 16:23:59 for gem5 by doxygen 1.11.0