当前位置:网站首页>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.
边栏推荐
- 抖快B为啥做不好综艺
- How cloudxr promotes the future development of XR
- Solution for IIS failing to load font files (*.woff, *.svg)
- Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
- Arcmap操作系列:80平面转经纬度84
- MySQL transaction / lock / log summary
- RT-Thread 堆區大小設置
- 19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
- Cesium-1.72 learning (deploy offline resources)
- 2022蓝桥杯国赛B组-2022-(01背包求方案数)
猜你喜欢

搬运两个负载均衡的笔记,日后省的找

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
![[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection](/img/c2/d6760826b81589781574aebff61f9a.png)
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection

【活动报名】探秘元宇宙,就差你了!7月2号我在深圳现场等你!

What is XR extended reality and what are the XR cloud streaming platforms
MySQL开放远程连接权限的两种方法

Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan

Cesium-1.72 learning (add points, lines, cubes, etc.)

Cloud XR, how to help industrial upgrading

牛客网:有多少个不同的二叉搜索树
随机推荐
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
大学生研究生毕业找工作,该选择哪个方向?
2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
POJ Project Summer
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Bc1.2 PD protocol
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
15年做糊21款硬件,谷歌到底栽在哪儿?
Installing jupyter notebook under Anaconda
7 月 2 日邀你来TD Hero 线上发布会
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
In depth analysis of the core code of the gadgetinspector
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
电子烟强制性国家标准GB 41700-2022发布 2022年10月1日起实施
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
[CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析
register_chrdev和cdev_init cdev_add用法区别
RT-Thread 堆區大小設置
实时渲染和预渲染有什么区别