【问题标题】:How to add new instruction and simulate it(spike)?如何添加新指令并模拟它(spike)?
【发布时间】:2017-03-24 13:22:23
【问题描述】:

我想在 RISCV ISA 中添加新指令,我按照以下步骤操作:

模拟新指令。向模拟器添加指令需要两个步骤:

  1. 在文件riscv/insns/<new_instruction_name>.h 中描述指令的功能行为。检查该目录中的其他说明作为起点。
  2. 将操作码和操作码掩码添加到riscv/opcodes.h。或者,将其添加到 riscv-opcodes 包中,它会为您执行此操作:$ cs ../riscv-opcodes; vi opcodes // add a line for the new instruction; make install
  3. 重建求和器。

我编写了简单的汇编代码来测试:

.file   "hello.c"
.text
.align  2
.globl  main
.type   main, @function
main:
   li   a0, 2
   mac  a1, a2, a3
   add  a0, a0, a1
.size main, .-main
.ident "GCC: (GNU)5.2.

但它无法识别新指令 (mac a1,a2,a3) 并显示错误消息:

$ riscv64-unknown-elf-gcc hello.s
...
hello.s:8: Error:unrecognized opcode `mac a1,a2,a3'

我该怎么办?

【问题讨论】:

标签: gcc riscv


【解决方案1】:

您的指令 (https://github.com/riscv/riscv-isa-sim#simulating-a-new-instruction) 用于将指令添加到模拟器,而不是添加到由您的 gcc 命令调用的汇编程序 (binutils as/gas)。消息“Error: unrecognized opcode 'mac a1,a2,a3'”来自汇编工具,有产生此错误的代码:https://github.com/riscv/riscv-binutils-gdb/blob/master/gas/config/tc-riscv.c#L1187

/* 该例程将指令汇编成二进制格式。作为一个 副作用,它将全局变量 imm_reloc 设置为 如果操作数之一是地址表达式,则进行重定位。 */

static const char *
riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
      bfd_reloc_code_real_type *imm_reloc)
...
  const char *error = "unrecognized opcode";
  for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
  {
  ...
  }
out:
  ...
  return error;

您可以将新指令编码为原始字节以使用您当前的汇编程序;或者您需要更改riscv/riscv-tools 的汇编程序,通过更改riscv/riscv-opcodes/ 中的操作码列表并使用新的riscv-opcodes 重建您的riscv-binutils (https://github.com/riscv/riscv-binutils-gdb)。

【讨论】:

  • 感谢您的回复,我已经更改了risv/riscv-opcodes/ 中的操作码列表并进行安装。我还在文件riscv/insns/ 中描述了指令的功能行为。最后,我在riscv-binutils-gdb/opcodes/riscv-opc.c 中添加了指令。但它仍然给我错误(我无法重建 gnu-tool-chain)。我做错了哪一步?
  • jjlin,错误的步骤是您创建了错误的问题。在这个问题中,您仅链接了更改模拟器的说明。 gnu 工具链的重建错误是不同的错误(我们需要有关它的更多详细信息)并且很难免费调试此类错误(下一个您关于 stackoverflow 的问题可能会作为题外话关闭)。从未修改的 riscv 工具链开始,并在进行任何自定义更改之前学习如何重建它们(其他操作系统是否会帮助,查找最近的指令)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
相关资源
最近更新 更多