Skip to content

File instr_op_imm.h

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

Go to the documentation of this file

#pragma once

#include <cstdint>
#include <vector>

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

namespace simtix {

class InstrOpImm : public Instr {
 public:
  using Op = void (InstrOpImm::*)();

  // Implementation of Instr's virtual methods
  void Decode() override;

  void Issue() override;

  void OperandCollect() override;

  void Execute() override;

  void Commit() override;

  void Reset() override;

  void Assign(const Instr *other) override;

  bool CanIssue() const override;

  bool CanExecute() const override { return rs1_data_ready_; }

  bool CanCommit() const override { return executed_; }

  bool CanRetire() const override { return committed_; }

  // Helper functions
  static inline int64_t DecodeImmItype(uint32_t iword) {
    return (int32_t(iword) >> 20);
  }

 protected:
  InstrOpImm(Warp *warp, uint32_t iword, uint64_t wpc);

  void Reinitialize(Warp *warp, uint32_t iword, uint64_t wpc) override;

  int64_t imm_ = 0;

  Op op_ = nullptr;

  bool rs1_data_ready_ = false;
  bool executed_ = false;
  bool committed_ = false;

  // Register data
  std::vector<int64_t> rs1_data_;
  std::vector<int64_t> rd_data_;

 private:
  // Operations defined in this Opcode
  void addi_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = (rs1_data_[tid] + imm_);
    }
  }

  void slti_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = (rs1_data_[tid] < imm_) ? 1 : 0;
    }
  }

  void sltiu_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = (uint64_t(rs1_data_[tid]) < uint64_t(imm_)) ? 1 : 0;
    }
  }

  void xori_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = rs1_data_[tid] ^ imm_;
    }
  }

  void ori_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = rs1_data_[tid] | imm_;
    }
  }

  void andi_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = rs1_data_[tid] & imm_;
    }
  }

  void slli_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = rs1_data_[tid] << (imm_ & 0x3f);
    }
  }

  void srli_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = uint64_t(rs1_data_[tid]) >> (imm_ & 0x3f);
    }
  }

  void srai_() {
    for (Thread *t : active_threads_) {
      uint32_t tid = t->tid();
      rd_data_[tid] = rs1_data_[tid] >> (imm_ & 0x3f);
    }
  }

  void nop_() {}

  // Make InstrPool friend so that they can call our protected constructor
  template <class InstrType>
  friend class InstrPool;
};

}  // namespace simtix