2 * Copyright (c) 2010 Gabe Black
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef __ARCH_GENERIC_TYPES_HH__
32 #define __ARCH_GENERIC_TYPES_HH__
36 #include "base/trace.hh"
37 #include "base/types.hh"
38 #include "sim/serialize.hh"
43 // The guaranteed interface.
51 PCStateBase(Addr val) { set(val); }
55 * Returns the memory address the bytes of this instruction came from.
57 * @return Memory address of the current instruction's encoding.
66 * Returns the memory address the bytes of the next instruction came from.
68 * @return Memory address of the next instruction's encoding.
77 * Returns the current micropc.
79 * @return The current micropc.
88 * Force this PC to reflect a particular value, resetting all its other
89 * fields around it. This is useful for in place (re)initialization.
91 * @param val The value to set the PC to.
96 operator == (const PCStateBase &opc) const
98 return _pc == opc._pc && _npc == opc._npc;
102 operator != (const PCStateBase &opc) const
104 return !(*this == opc);
108 serialize(std::ostream &os)
110 SERIALIZE_SCALAR(_pc);
111 SERIALIZE_SCALAR(_npc);
115 unserialize(Checkpoint *cp, const std::string §ion)
117 UNSERIALIZE_SCALAR(_pc);
118 UNSERIALIZE_SCALAR(_npc);
124 * Different flavors of PC state. Only ISA specific code should rely on
125 * any particular type of PC state being available. All other code should
126 * use the interface above.
129 // The most basic type of PC.
130 template <class MachInst>
131 class SimplePCState : public PCStateBase
134 typedef PCStateBase Base;
138 Addr pc() const { return _pc; }
139 void pc(Addr val) { _pc = val; }
141 Addr npc() const { return _npc; }
142 void npc(Addr val) { _npc = val; }
148 npc(val + sizeof(MachInst));
152 SimplePCState(Addr val) { set(val); }
157 return this->npc() != this->pc() + sizeof(MachInst);
165 _npc += sizeof(MachInst);
169 template <class MachInst>
171 operator<<(std::ostream & os, const SimplePCState<MachInst> &pc)
173 ccprintf(os, "(%#x=>%#x)", pc.pc(), pc.npc());
177 // A PC and microcode PC.
178 template <class MachInst>
179 class UPCState : public SimplePCState<MachInst>
182 typedef SimplePCState<MachInst> Base;
189 MicroPC upc() const { return _upc; }
190 void upc(MicroPC val) { _upc = val; }
192 MicroPC nupc() const { return _nupc; }
193 void nupc(MicroPC val) { _nupc = val; }
210 UPCState(Addr val) { set(val); }
215 return this->npc() != this->pc() + sizeof(MachInst) ||
216 this->nupc() != this->upc() + 1;
219 // Advance the upc within the instruction.
227 // End the macroop by resetting the upc and advancing the regular pc.
237 operator == (const UPCState<MachInst> &opc) const
239 return Base::_pc == opc._pc &&
240 Base::_npc == opc._npc &&
241 _upc == opc._upc && _nupc == opc._nupc;
245 operator != (const UPCState<MachInst> &opc) const
247 return !(*this == opc);
251 serialize(std::ostream &os)
254 SERIALIZE_SCALAR(_upc);
255 SERIALIZE_SCALAR(_nupc);
259 unserialize(Checkpoint *cp, const std::string §ion)
261 Base::unserialize(cp, section);
262 UNSERIALIZE_SCALAR(_upc);
263 UNSERIALIZE_SCALAR(_nupc);
267 template <class MachInst>
269 operator<<(std::ostream & os, const UPCState<MachInst> &pc)
271 ccprintf(os, "(%#x=>%#x).(%d=>%d)",
272 pc.pc(), pc.npc(), pc.upc(), pc.nupc());
276 // A PC with a delay slot.
277 template <class MachInst>
278 class DelaySlotPCState : public SimplePCState<MachInst>
281 typedef SimplePCState<MachInst> Base;
287 Addr nnpc() const { return _nnpc; }
288 void nnpc(Addr val) { _nnpc = val; }
294 nnpc(val + 2 * sizeof(MachInst));
297 DelaySlotPCState() {}
298 DelaySlotPCState(Addr val) { set(val); }
303 return !(this->nnpc() == this->npc() + sizeof(MachInst) &&
304 (this->npc() == this->pc() + sizeof(MachInst) ||
305 this->npc() == this->pc() + 2 * sizeof(MachInst)));
312 Base::_pc = Base::_npc;
314 _nnpc += sizeof(MachInst);
318 operator == (const DelaySlotPCState<MachInst> &opc) const
320 return Base::_pc == opc._pc &&
321 Base::_npc == opc._npc &&
326 operator != (const DelaySlotPCState<MachInst> &opc) const
328 return !(*this == opc);
332 serialize(std::ostream &os)
335 SERIALIZE_SCALAR(_nnpc);
339 unserialize(Checkpoint *cp, const std::string §ion)
341 Base::unserialize(cp, section);
342 UNSERIALIZE_SCALAR(_nnpc);
346 template <class MachInst>
348 operator<<(std::ostream & os, const DelaySlotPCState<MachInst> &pc)
350 ccprintf(os, "(%#x=>%#x=>%#x)",
351 pc.pc(), pc.npc(), pc.nnpc());
355 // A PC with a delay slot and a microcode PC.
356 template <class MachInst>
357 class DelaySlotUPCState : public DelaySlotPCState<MachInst>
360 typedef DelaySlotPCState<MachInst> Base;
367 MicroPC upc() const { return _upc; }
368 void upc(MicroPC val) { _upc = val; }
370 MicroPC nupc() const { return _nupc; }
371 void nupc(MicroPC val) { _nupc = val; }
387 DelaySlotUPCState() {}
388 DelaySlotUPCState(Addr val) { set(val); }
393 return Base::branching() || this->nupc() != this->upc() + 1;
396 // Advance the upc within the instruction.
404 // End the macroop by resetting the upc and advancing the regular pc.
414 operator == (const DelaySlotUPCState<MachInst> &opc) const
416 return Base::_pc == opc._pc &&
417 Base::_npc == opc._npc &&
418 Base::_nnpc == opc._nnpc &&
419 _upc == opc._upc && _nupc == opc._nupc;
423 operator != (const DelaySlotUPCState<MachInst> &opc) const
425 return !(*this == opc);
429 serialize(std::ostream &os)
432 SERIALIZE_SCALAR(_upc);
433 SERIALIZE_SCALAR(_nupc);
437 unserialize(Checkpoint *cp, const std::string §ion)
439 Base::unserialize(cp, section);
440 UNSERIALIZE_SCALAR(_upc);
441 UNSERIALIZE_SCALAR(_nupc);
445 template <class MachInst>
447 operator<<(std::ostream & os, const DelaySlotUPCState<MachInst> &pc)
449 ccprintf(os, "(%#x=>%#x=>%#x).(%d=>%d)",
450 pc.pc(), pc.npc(), pc.nnpc(), pc.upc(), pc.nupc());