exec.c 5.2 KB
Newer Older
P
Parallels 已提交
1 2
#include "cpu/exec.h"
#include "all-instr.h"
Z
Zihao Yu 已提交
3
#include <setjmp.h>
P
Parallels 已提交
4 5

static make_EHelper(load) {
6 7 8 9 10
  static OpcodeEntry table [8] = {
    EXW(lds, 1), EXW(lds, 2), EXW(lds, 4), EXW(ld, 8), EXW(ld, 1), EXW(ld, 2), EXW(ld, 4), EMPTY
  };
  decinfo.width = table[decinfo.isa.instr.funct3].width;
  idex(pc, &table[decinfo.isa.instr.funct3]);
P
Parallels 已提交
11 12 13
}

static make_EHelper(store) {
14 15 16 17 18
  static OpcodeEntry table [8] = {
    EXW(st, 1), EXW(st, 2), EXW(st, 4), EXW(st, 8), EMPTY, EMPTY, EMPTY, EMPTY
  };
  decinfo.width = table[decinfo.isa.instr.funct3].width;
  idex(pc, &table[decinfo.isa.instr.funct3]);
P
Parallels 已提交
19 20 21
}

static make_EHelper(op_imm) {
22 23 24 25
  static OpcodeEntry table [8] = {
    EX(add), EX(sll), EX(slt), EX(sltu), EX(xor), EX(srl), EX(or), EX(and)
  };
  idex(pc, &table[decinfo.isa.instr.funct3]);
P
Parallels 已提交
26 27
}

28
static make_EHelper(op_imm32) {
29 30 31 32
  static OpcodeEntry table [8] = {
    EX(addw), EX(sllw), EMPTY, EMPTY, EMPTY, EX(srlw), EMPTY, EMPTY
  };
  idex(pc, &table[decinfo.isa.instr.funct3]);
P
Parallels 已提交
33 34 35
}

static make_EHelper(op) {
36 37 38 39 40 41 42 43 44 45
  static OpcodeEntry table [3][8] = {
    {EX(add), EX(sll), EX(slt), EX(sltu), EX(xor), EX(srl), EX(or), EX(and)},
    {EX(mul), EX(mulh), EX(mulhsu), EX(mulhu), EX(div), EX(divu), EX(rem), EX(remu)},
    {EX(sub), EMPTY, EMPTY, EMPTY, EMPTY, EX(sra), EMPTY, EMPTY},
  };

  uint32_t idx = decinfo.isa.instr.funct7;
  if (idx == 32) idx = 2;
  assert(idx <= 2);
  idex(pc, &table[idx][decinfo.isa.instr.funct3]);
P
Parallels 已提交
46 47
}

48
static make_EHelper(op32) {
49 50 51 52 53 54 55 56 57 58
  static OpcodeEntry table [3][8] = {
    {EX(addw), EX(sllw), EMPTY, EMPTY, EMPTY, EX(srlw), EMPTY, EMPTY},
    {EX(mulw), EMPTY, EMPTY, EMPTY, EX(divw), EX(divuw), EX(remw), EX(remuw)},
    {EX(subw), EMPTY, EMPTY, EMPTY, EMPTY, EX(sraw), EMPTY, EMPTY},
  };

  uint32_t idx = decinfo.isa.instr.funct7;
  if (idx == 32) idx = 2;
  assert(idx <= 2);
  idex(pc, &table[idx][decinfo.isa.instr.funct3]);
P
Parallels 已提交
59 60 61
}

static make_EHelper(system) {
62
  static OpcodeEntry table [8] = {
Z
Zihao Yu 已提交
63
    EX(priv), IDEX(csr, csrrw), IDEX(csr, csrrs), EMPTY, EMPTY, EMPTY, IDEX(csri, csrrs), IDEX(csri, csrrc)
64 65
  };
  idex(pc, &table[decinfo.isa.instr.funct3]);
P
Parallels 已提交
66 67
}

Z
Zihao Yu 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static make_EHelper(atomic) {
  static OpcodeEntry table_lo [4] = {
    EMPTY, EX(amoswap), EMPTY, EMPTY
  };
  static OpcodeEntry table_hi [8] = {
    EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY
  };

  decinfo.width = 1 << decinfo.isa.instr.funct3;

  uint32_t funct5 = decinfo.isa.instr.funct7 >> 2;
  uint32_t idx_lo = funct5 & 0x3;
  uint32_t idx_hi = funct5 >> 2;
  if (idx_lo != 0) idex(pc, &table_lo[idx_lo]);
  else idex(pc, &table_hi[idx_hi]);
}

P
Parallels 已提交
85
static OpcodeEntry opcode_table [32] = {
Z
Zihao Yu 已提交
86
  /* b00 */ IDEX(ld, load), EMPTY, EMPTY, EX(fence), IDEX(I, op_imm), IDEX(U, auipc), IDEX(I, op_imm32), EMPTY,
Z
Zihao Yu 已提交
87
  /* b01 */ IDEX(st, store), EMPTY, EMPTY, IDEX(R, atomic), IDEX(R, op), IDEX(U, lui), IDEX(R, op32), EMPTY,
P
Parallels 已提交
88 89 90 91
  /* b10 */ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY,
  /* b11 */ IDEX(B, branch), IDEX(I, jalr), EX(nemu_trap), IDEX(J, jal), EX(system), EMPTY, EMPTY, EMPTY,
};

92 93 94
// RVC

static make_EHelper(C_10_100) {
95
  static OpcodeEntry table [8] = {
Z
Zihao Yu 已提交
96
    EMPTY, EMPTY, IDEX(C_rs1_rs2_0, jalr), IDEX(C_0_rs2_rd, add), EMPTY, EMPTY, IDEX(C_JALR, jalr), IDEX(C_rs1_rs2_rd, add),
97
  };
Z
Zihao Yu 已提交
98 99 100 101 102
  uint32_t cond_c_simm12_not0 = (decinfo.isa.instr.c_simm12 != 0);
  uint32_t cond_c_rd_rs1_not0 = (decinfo.isa.instr.c_rd_rs1 != 0);
  uint32_t cond_c_rs2_not0 = (decinfo.isa.instr.c_rs2 != 0);
  uint32_t idx = (cond_c_simm12_not0 << 2) | (cond_c_rd_rs1_not0 << 1) | cond_c_rs2_not0;
  assert(idx < 8);
103
  idex(pc, &table[idx]);
104 105
}

Z
Zihao Yu 已提交
106
static make_EHelper(C_01_011) {
Z
Zihao Yu 已提交
107
  static OpcodeEntry table [2] = { IDEX(C_0_imm_rd, lui), IDEX(C_ADDI16SP, add)};
Z
Zihao Yu 已提交
108 109 110 111 112 113
  assert(decinfo.isa.instr.c_rd_rs1 != 0);
  int idx = (decinfo.isa.instr.c_rd_rs1 == 2);
  idex(pc, &table[idx]);
}

static make_EHelper(C_01_100) {
Z
Zihao Yu 已提交
114 115 116 117
  uint32_t func = decinfo.isa.instr.c_func6 & 0x3;
  if (func == 3) {
    decode_CR(pc);
    static OpcodeEntry table [8] = {
Z
Zihao Yu 已提交
118
      EX(sub), EX(xor), EX(or), EX(and), EX(subw), EX(addw), EMPTY, EMPTY,
Z
Zihao Yu 已提交
119 120 121 122 123 124 125 126 127
    };

    uint32_t idx2 = (decinfo.isa.instr.c_func6 >> 2) & 0x1;
    uint32_t idx1_0 = decinfo.isa.instr.c_func2;
    uint32_t idx = (idx2 << 2) | idx1_0;
    assert(idx < 8);
    idex(pc, &table[idx]);
  } else {
    decode_C_rs1__imm_rd_(pc);
Z
Zihao Yu 已提交
128
    static OpcodeEntry table [3] = { EX(srl), EX(sra), EX(and) };
Z
Zihao Yu 已提交
129
    idex(pc, &table[func]);
Z
Zihao Yu 已提交
130 131 132
  }
}

133
static OpcodeEntry rvc_table [3][8] = {
Z
Zihao Yu 已提交
134 135 136
  {IDEX(C_ADDI4SPN, add), EMPTY, IDEX(C_LW, lds), IDEX(C_LD, ld), EMPTY, EMPTY, IDEX(C_SW, st), IDEX(C_SD, st)},
  {IDEX(C_rs1_imm_rd, add), IDEX(C_rs1_imm_rd, addw), IDEX(C_0_imm_rd, add), EX(C_01_011), EX(C_01_100), IDEX(C_J, jal), IDEX(CB, beq), IDEX(CB, bne)},
  {IDEX(C_rs1_imm_rd, sll), EMPTY, IDEX(C_LWSP, lds), IDEX(C_LDSP, ld), EX(C_10_100), EMPTY, IDEX(C_SWSP, st), IDEX(C_SDSP, st)}
137 138
};

P
Parallels 已提交
139
void isa_exec(vaddr_t *pc) {
Z
Zihao Yu 已提交
140 141 142 143 144 145 146 147 148
  extern jmp_buf intr_buf;
  int setjmp_ret;
  if ((setjmp_ret = setjmp(intr_buf)) != 0) {
    int exception = setjmp_ret - 1;
    void raise_intr(word_t, vaddr_t);
    raise_intr(exception, cpu.pc);
    return;
  }

P
Parallels 已提交
149
  decinfo.isa.instr.val = instr_fetch(pc, 4);
150 151 152 153 154 155 156
  if (decinfo.isa.instr.opcode1_0 == 0x3) {
    idex(pc, &opcode_table[decinfo.isa.instr.opcode6_2]);
  } else {
    // RVC instructions are only 2-byte
    *pc -= 2;
    idex(pc, &rvc_table[decinfo.isa.instr.opcode1_0][decinfo.isa.instr.c_funct3]);
  }
P
Parallels 已提交
157
}