Skip to content

File instr_ptr.h

File List > projects > simtix > src > simtix > sm > instr_ptr.h

Go to the documentation of this file

#pragma once

#include <simtix/riscv.h>
#include <simtix/trace.h>

#include <string_view>

#include "sm/instr.h"
#include "sm/instr_pool.h"
#include "sm/thread.h"
#include "sm/warp.h"

namespace simtix {

class InstrPtr {
 public:
  InstrPtr();

  explicit InstrPtr(Warp *warp, uint64_t wpc, std::optional<uint32_t> ssw);

  // No copy constructor.
  explicit InstrPtr(const InstrPtr &) = delete;

  InstrPtr(InstrPtr &&other);

  ~InstrPtr();

  // Group: Public Functions

  void Instantiate(uint32_t iword);

  void Instantiate(riscv::Exceptions exception);

  // No copy assignment operator.
  InstrPtr &operator=(const InstrPtr &) = delete;

  InstrPtr &operator=(InstrPtr &&other) noexcept;

  InstrPtr DuplicateForWarp(Warp *warp) const noexcept;

  Instr *operator->() const noexcept { return instr_; }

  void AddKonataLabel(std::string_view label) {
    if (unique_id_ < UINT64_MAX) {
      KONATA(L, unique_id_, 1, label);
    }
  }

  void set_flushed() { flushed_ = true; }

  // Public accessors.
  uint32_t wid() const noexcept { return warp_ ? warp_->wid() : UINT32_MAX; }
  std::optional<uint32_t> ssw() const noexcept { return ssw_; }
  uint64_t wpc() const noexcept { return wpc_; }
  Instr *instr() const noexcept { return instr_; }
  uint64_t unique_id() const noexcept { return unique_id_; }

  // Function: set_first_stage_name
  static void set_first_stage_name(std::string_view first_stage_name) {
    first_stage_name_ = first_stage_name;
  }

 private:
  // Group: Private Functions

  void RecycleInstrIfNotNull();

  void Reset() {
    warp_ = nullptr;
    ssw_ = std::nullopt;
    wpc_ = 0;
    pool_ = nullptr;
    instr_ = nullptr;
    flushed_ = false;
    unique_id_ = UINT64_MAX;
  }

  Warp *warp_;

  std::optional<uint32_t> ssw_;

  uint64_t wpc_;

  InstrPoolInterface *pool_;

  Instr *instr_;

  bool flushed_;

  uint64_t unique_id_;

  // Group: Properties

  inline static uint64_t instr_count_ = 0;

  inline static std::string_view first_stage_name_ = "F";
};

}  // namespace simtix