当前位置:网站首页>MC Instruction Decoder
MC Instruction Decoder
2022-06-30 16:41:00 【Hui's Sutra Pavilion】
Instruction Decoder
MC Instruction Decoder Belong to MC layer LLVM Disassembly module in , The main function is to read and write the input binary file as Bytes form , The output corresponds to Target Machine instructions MCInst, Is an indispensable module in disassembly :

MCDisassembler class
MCDisassembler Class is MC Instruction Decoder Improved interface classes in and out , Belong to all target disassembler Of superclass,
This class is located in :llvm\include\llvm\MC\MCDisassembler\MCDisassembler.h file
This class has few interfaces , One of the most important interfaces :getInstruction:
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &CStream)This interface converts executable binaries to Bytes As input , Input through disassembly MCInst Machine instructions .
This interface is a virtual function , Every target Machine instructions are different , Writing a new backend requires MCDisassembler Base class , Derive from target Corresponding disassembler, Realization getInstruction Interface .
AMDGPU The derivation relationship is as follows :

AMDGPUDisassembler be located llvm\lib\Target\AMDGPU\Disassembler\AMDGPUDisassembler.h
Disassembler Usage method
disassembler The usage method is usually divided into the following steps :

RegisterMCDisassembler()
When a new backend is added , Need to call RegisterMCDisassembler towards Target Registered in disassemble, AMDGPU Registered at llvm\lib\Target\AMDGPU\Disassemble\AMDGPUDisassembler.cpp:
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUDisassembler() {
TargetRegistry::RegisterMCDisassembler(getTheGCNTarget(),
createAMDGPUDisassembler);
TargetRegistry::RegisterMCSymbolizer(getTheGCNTarget(),
createAMDGPUSymbolizer);
}RegisterMCDisassembler The main registration corresponds to establish disassamble Callback function createAMDGPUDisassembler.
static void RegisterMCDisassembler(Target &T,
Target::MCDisassemblerCtorTy Fn) {
T.MCDisassemblerCtorFn = Fn;
}establish disassembler The callback function is registered to MCDisassemblerCtorFn.
createMCDisassembler
createMCDisassembler Create the corresponding MCDisassembler:
MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
MCContext &Ctx) const {
if (!MCDisassemblerCtorFn)
return nullptr;
return MCDisassemblerCtorFn(*this, STI, Ctx);
}Call registered createAMDGPUDisassemble Callback function , establish AMDGPUDisassembler.
getInstruction
The parsed binary file Text Code segment , Turn into ArrayRef<uint8_t>, Disassembly , for example :
MCInst Inst;
bool Disassembled =
DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
SectionAddr + Index, CommentStream);AMDGPU getInstruction()
AMDGPU The disassembly implementation is located at llvm\lib\Target\AMDGPU\Disassembler\AMDGPUDisassembler.cpp file :
The implementation process is relatively clear , Disassembly is mainly divided into two parts :
- First, disassemble the binary , Look for all kinds of DecoderTable surface , Find the instruction to which the binary belongs . The characteristic of this step is to check DecoderTable surface ,AMDGPU According to different architecture instructions and instruction types, it is divided into multiple DecoderTable surface , Step by step matching is required .
- After matching , Further processing for some special instructions .
DecoderTable
AMDGPU Back end to support disassembly , Internally, the command is divided into multiple DecoderTable,getInstruction Each... Will be matched and queried in turn DecoderTable Table to match , Exist at present DecoderTable There are mainly :
- DecoderTableGFX10_B64
- DecoderTableDPP864
- DecoderTableDPP64
- DecoderTableSDWA64
- DecoderTableSDWA964
- DecoderTableSDWA1064
- DecoderTableGFX80_UNPACKED64
- DecoderTableGFX832
- DecoderTableAMDGPU32
- DecoderTableGFX932
- DecoderTableGFX90A32
- DecoderTableGFX10_B32
- DecoderTableGFX1032
- DecoderTableGFX90A64
- DecoderTableGFX864
- DecoderTableAMDGPU64
- DecoderTableGFX964
- DecoderTableGFX1064
above DecoderTable Yes TableGen Automatically generate to... During the compilation phase build/lib/Target/AMDGPU/AMDGPUGenDisassemblerTables.inc In file , Generate commands manually :
llvm-tblgen -gen-disassembler./AMDGPU.td -I ../../../includeWith DecoderTableGFX932 For example, GFX 32 position DecoderTable surface :
The beginning of each line MCD::OPC_XXXX Is the corresponding status , And determine the significance of subsequent numerical values
For example, the beginning MCD::OPC_ExtractField follow-up 25,7 Jump to the start and end of the match for the next step ,start=25, end=31.
decodeInstruction
decodeInstruction Based on the DecoderTable surface , Disassemble and decode , be located build/lib/Target/AMDGPU/AMDGPUGenDisassemblerTables.inc In file ,

According to the matching table MCD::OPC_XXXX Match operation , until MCD::OPC_Decode The status description is in this DecoderTable Table matching succeeded , otherwise MCD::OPC_Fail Matching failure , Try the next DecoderTable Table to match

Here is an example :
/* 62 */ MCD::OPC_Decode, 236, 127, 46, // Opcode: V_ADDC_CO_U32_e32_gfx9236,127 Solve the corresponding... For the corresponding opcode, The highest number 8 individual bit Bit flag bit , When the setting represents the opcode Greater than 128 It needs to be split into multiple , Until the highest position is not 1 until , The calculation method is as follows :
(236&~128)+ 127<<746 Corresponding DecodeIdx,decodeToMCInst Decide to follow up operand Register parsing operation :

Eventually the machine instructions will be disassembled MCInst.
边栏推荐
- '&lt;', hexadecimal value 0x3C, is an invalid 问题解决
- flinkcdc如果监控的数据库为mongo就必须是集群版吗
- Hundreds of lines of code to implement a JSON parser
- 招标公告:深圳市财政局数据库异地灾备项目
- How the edge computing platform helps the development of the Internet of things
- The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
- dart:字符串replace相关的方法解决替换字符
- 居家办公浅谈远程协助快速提效心得 | 社区征文
- After 15 years of working on 21 types of hardware, where is Google?
- 牛客网:有多少个不同的二叉搜索树
猜你喜欢

RT thread heap size setting

实时渲染和预渲染有什么区别

Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

MC Instruction Decoder

7 月 2 日邀你来TD Hero 线上发布会

备战数学建模34-BP神经网络预测2

Distributed machine learning: model average Ma and elastic average easgd (pyspark)

Carry two load balancing notes and find them in the future

The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?
随机推荐
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
Three development trends of enterprise application viewed from the third technological revolution
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
“低代码”在企业数字化转型中扮演着什么角色?
RT-Thread 堆区大小设置
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
Build cloud native observability capability suitable for organizations
ArcMap operation series: 80 plane to latitude and longitude 84
register_chrdev和cdev_init cdev_add用法区别
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
Hundreds of lines of code to implement a JSON parser
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
构建适合组织的云原生可观测性能力
RTP 发送PS流零拷贝方案
2020蓝桥杯国赛B组-搬砖-(贪心排序+01背包)
Cesium-1.72 learning (deploy offline resources)
电子烟强制性国家标准GB 41700-2022发布 2022年10月1日起实施
[BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
快照和备份