Opcodes 说明

简介: 在 rocket-chip 中对 opcodes 进行定制化的过程主要是在 riscv-tools/riscv-opcodes 目录中:.├── encoding.

在 rocket-chip 中对 opcodes 进行定制化的过程主要是在 riscv-tools/riscv-opcodes 目录中:

.
├── encoding.h                 --- 基础编码工具
├── LICENSE
├── Makefile                   --- 编译脚本
├── opcodes                    --- opcodes 定义,下同
├── opcodes-custom
├── opcodes-pseudo
├── opcodes-rvc
├── opcodes-rvc-pseudo
├── parse-opcodes              ---  opcodes 自动化生成工具
└── README.md                  ---  说明文档

接下来解读下 Makefile 文件中的内容:

SHELL := /bin/sh

# 定义目标 encoding.h 目标文件位置
ISASIM_H := ../riscv-isa-sim/riscv/encoding.h
PK_H := ../riscv-pk/machine/encoding.h
FESVR_H := ../riscv-fesvr/fesvr/encoding.h
ENV_H := ../riscv-tests/env/encoding.h
OPENOCD_H := ../riscv-openocd/src/target/riscv/encoding.h

ALL_OPCODES := opcodes-pseudo opcodes opcodes-rvc opcodes-rvc-pseudo opcodes-custom

install: $(ISASIM_H) $(PK_H) $(FESVR_H) $(ENV_H) $(OPENOCD_H) inst.chisel instr-table.tex priv-instr-table.tex

# 安装所有指令码到 c 语言库,并拷贝 encoding.h 文件到目标目录
$(ISASIM_H) $(PK_H) $(FESVR_H) $(ENV_H) $(OPENOCD_H): $(ALL_OPCODES) parse-opcodes encoding.h
    cp encoding.h $@
    cat opcodes opcodes-rvc-pseudo opcodes-rvc opcodes-custom | ./parse-opcodes -c >> $@

# 安装所有的指令码到 Chisel 代码中,通过 parse-opcodes 脚本生成 Instructions 类
inst.chisel: $(ALL_OPCODES) parse-opcodes
    cat opcodes opcodes-custom opcodes-pseudo | ./parse-opcodes -chisel > $@

# 安装所有的指令码到 Go 代码中,通过 parse-opcodes 脚本生成 
inst.go: opcodes opcodes-pseudo parse-opcodes
    cat opcodes opcodes-pseudo | ./parse-opcodes -go > $@

instr-table.tex: $(ALL_OPCODES) parse-opcodes
    cat opcodes opcodes-pseudo | ./parse-opcodes -tex > $@

priv-instr-table.tex: $(ALL_OPCODES) parse-opcodes
    cat opcodes opcodes-pseudo | ./parse-opcodes -privtex > $@

.PHONY : install

执行 make install 之后,在 riscv-fesvr/fesvr/encoding.h 文件中,存在机器码的定义过程,比如:

#ifdef DECLARE_INSN
DECLARE_INSN(beq, MATCH_BEQ, MASK_BEQ)
DECLARE_INSN(bne, MATCH_BNE, MASK_BNE)
DECLARE_INSN(blt, MATCH_BLT, MASK_BLT)
DECLARE_INSN(bge, MATCH_BGE, MASK_BGE)
DECLARE_INSN(bltu, MATCH_BLTU, MASK_BLTU)
DECLARE_INSN(bgeu, MATCH_BGEU, MASK_BGEU)
DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
DECLARE_INSN(jal, MATCH_JAL, MASK_JAL)
DECLARE_INSN(lui, MATCH_LUI, MASK_LUI)
DECLARE_INSN(auipc, MATCH_AUIPC, MASK_AUIPC)
DECLARE_INSN(addi, MATCH_ADDI, MASK_ADDI)
DECLARE_INSN(slli, MATCH_SLLI, MASK_SLLI)
DECLARE_INSN(slti, MATCH_SLTI, MASK_SLTI)
...省略...
#endif

解析过程

简单分析下 parse-opcodes 文件中生成 encoding.h 文件部分的实现:

...此前省略...
def make_c(match,mask):
  print '/* Automatically generated by parse-opcodes.  */'
  print '#ifndef RISCV_ENCODING_H'
  print '#define RISCV_ENCODING_H'
// 递归指令名称列表,组装 MATCH_*, MASK_*, CSR_*, CAUSE_* 等宏定义
  for name in namelist:
    name2 = name.upper().replace('.','_')
    print '#define MATCH_%s %s' % (name2, hex(match[name]))
    print '#define MASK_%s  %s' % (name2, hex(mask[name]))
  for num, name in csrs+csrs32:
    print '#define CSR_%s %s' % (name.upper(), hex(num))
  for num, name in causes:
    print '#define CAUSE_%s %s' % (name.upper().replace(' ', '_'), hex(num))
  print '#endif'

// 定义 INSN 指令
  print '#ifdef DECLARE_INSN'
  for name in namelist:
    name2 = name.replace('.','_')
    print 'DECLARE_INSN(%s, MATCH_%s, MASK_%s)' % (name2, name2.upper(), name2.upper())
  print '#endif'

// 声明 CSR 
  print '#ifdef DECLARE_CSR'
  for num, name in csrs+csrs32:
    print 'DECLARE_CSR(%s, CSR_%s)' % (name, name.upper())
  print '#endif'

// 声明 CAUSE 
  print '#ifdef DECLARE_CAUSE'
  for num, name in causes:
    print 'DECLARE_CAUSE("%s", CAUSE_%s)' % (name, name.upper().replace(' ', '_'))
  print '#endif'
...此后省略...

Opcodes 定义文件

以 opcodes-rvc-pseudo 文件为例,其中包含了一些特定的指令:

# these aren't really pseudo-ops, but they overlay other encodings,
# so they are here to prevent parse-opcodes from barfing

@c.nop      1..0=1 15..13=0 12=0      11..7=0      6..2=0
@c.addi16sp 1..0=1 15..13=3 12=ignore 11..7=2      6..2=ignore
@c.jr       1..0=2 15..13=4 12=0      11..7=ignore 6..2=0
@c.jalr     1..0=2 15..13=4 12=1      11..7=ignore 6..2=0
@c.ebreak   1..0=2 15..13=4 12=1      11..7=0      6..2=0

# RV64C
@c.ld      1..0=0 15..13=3 12=ignore 11..2=ignore # c.flw for RV32
@c.sd      1..0=0 15..13=7 12=ignore 11..2=ignore # c.fsw for RV32
@c.addiw   1..0=1 15..13=1 12=ignore 11..2=ignore # c.jal for RV32
@c.ldsp    1..0=2 15..13=3 12=ignore 11..2=ignore # c.flwsp for RV32
@c.sdsp    1..0=2 15..13=7 12=ignore 11..2=ignore # c.fswsp for RV32
目录
相关文章
|
人工智能 自然语言处理 安全
从 ChatGPT 到 AI 大模型私有化部署,为什么企业需要私有化专属大模型?
目前,大模型已经能够切实的影响到我们每个人的工作、学习、生活,赋能千行万业,但是开放的大模型却无法很好的适应企业或单位的内部需要,为此,此处研究并提出为什么企业需要私有化大模型,并探讨私有化大模型的优势和挑战,同时本文也举出了一些实践落地的例子,希望能给读者带来一些思考和启发。
|
监控 Linux 测试技术
【实战技巧】使用inotify实现实时文件监控
`inotify`是Linux内核提供的文件系统监控机制,用于实时捕获文件和目录的创建、删除、移动和修改等事件。通过`inotify_init`初始化,`inotify_add_watch`添加监视点,如`. IN_ACCESS`, `. IN_MODIFY`等,及`inotify_rm_watch`移除监视。示例代码展示了监听指定路径下文件修改事件,当事件发生时打印信息。使用`inotify`能高效地构建实时应用,如文件同步和日志监控,简化系统编程。
2380 130
|
Python
md文件格式转成word文档
md文件格式转成word文档
1326 0
|
JavaScript 调度
Vue事件总线(EventBus)使用指南:详细解析与实战应用
Vue事件总线(EventBus)使用指南:详细解析与实战应用
1181 1
|
开发框架 人工智能 安全
ModelScope魔搭25年5月发布月报
不知不觉间,日历已经翻过了立夏,而开源模型的世界中似乎并没有春夏秋冬。在刚刚过去的四月份,见证了开源社区又一次蓬勃发展的浪潮。以Qwen3家族为代表,一系列新模型的开源为整个生态注入了新的活力。通过全面覆盖多种规格的 dense 与 MoE 模型架构,Qwen3 首次在开源模型中引入“快思考与慢思考” 双模式的支持,获得了广大开发者的热烈欢迎,成为新一代开源大模型的标杆之作。
714 10
|
机器学习/深度学习 自然语言处理 并行计算
探索深度学习中的Transformer模型:原理、优势与应用
探索深度学习中的Transformer模型:原理、优势与应用
2640 0
|
人工智能 测试技术 API
成功注册Google的SerpAPI实现AI Agent的tool
成功注册Google的SerpAPI实现AI Agent的tool
5112 6
|
机器学习/深度学习 人工智能 网络协议
|
机器学习/深度学习 运维 监控
|
运维 Cloud Native Java
热联集团:从 APISIX 迁移到云原生网关
我们将核心业务系统从 IDC 全栈迁移到阿里云后,并采用了云原生 API 网关,通过其独有的软硬一体的加速方案,相比普通 HTTPS 请求 TLS 握手时延降低一倍,极限 QPS 提升 80% 以上,运维效率也提升了 50%,此外,我们把 Nacos 迁移到 MSE Nacos,稳定性、性能和运维成本等方面都具备了明显的优势。