IR to other language (llvm backend)

这里的other language可以包括汇编码和二进制码。

这里是针对不同平台的,我们主要看x86就可以了。

对于这方面,主要有这么四个部分需要注意:

1.llvm language reference manual 汇编语言手册

2.The llvm target-independent code generator

3.TableGen 

4.Pass

我现在不知道这四部分怎么串在一起就写成了一个backend了。

接下来是一个步骤关于这个backend怎么写的。结合源代码来看。我这里就结合x86的源码来看。

Create a subclass of the TargetMachine class that describes characteristics of your target machine.
Copy existing examples of specific TargetMachine class and header files; for example, start with
SparcTargetMachine.cpp and SparcTargetMachine.h, but change the file names for your target.

Similarly, change code that references “Sparc” to reference your target.

1.创建一个TargetMachine的类,这个类用来描述目标机器的特征。(3)

• Describe the register set of the target. Use TableGen to generate code for register definition, register aliases, and
register classes from a target-specific RegisterInfo.td input file. You should also write additional code
for a subclass of the TargetRegisterInfo class that represents the class register file data used for register

allocation and also describes the interactions between registers.

2.描述目标的寄存器堆。用TableGen从特定目标的RegisterInfo.td输入文件去产生寄存器定义的代码,寄存器重命名,寄存器类。你应该写额外对于TarRegisterInfo类(这个类是寄存器文件数据用于寄存器分配也是描述寄存器交互)的代码。(3)

• Describe the instruction set of the target. Use TableGen to generate code for target-specific instructions from
target-specific versions of TargetInstrFormats.td and TargetInstrInfo.td. You should write
additional code for a subclass of the TargetInstrInfo class to represent machine instructions supported by

the target machine.

3.描述目标的指令集。用TableGen从特定目标版本的TargetInstrFormats.td和TargetInstrInfo.td为特定目标指令产生代码。你应该写额外的代码为了TargetInstrInfo类的子类头表述特定机器支持的机器指令。

• Describe the selection and conversion of the LLVM IR from a Directed Acyclic Graph (DAG) representation
of instructions to native target-specific instructions. Use TableGen to generate code that matches patterns and
selects instructions based on additional information in a target-specific version of TargetInstrInfo.td.
Write code for XXXISelDAGToDAG.cpp, where XXX identifies the specific target, to perform pattern matching
and DAG-to-DAG instruction selection. Also write code in XXXISelLowering.cpp to replace or remove

operations and data types that are not supported natively in a SelectionDAG.

4.描述LLVM IR从有向无环图表示指令方式到原生目标指令。用TableGen去产生指令match pattern再挑选指令基于额外的信息在TargetInstrInfo.td的特殊版本。为XXXISelDAGToDAG.cpp写代码去执行模式匹配和DAG到DAG指令选择。并为XXXISelLowering.cpp写代码去替代或者去除操作和不被SelectionDAG原生支持的数据类型。

• Write code for an assembly printer that converts LLVM IR to a GAS format for your target machine. You should
add assembly strings to the instructions defined in your target-specific version of TargetInstrInfo.td.
You should also write code for a subclass of AsmPrinter that performs the LLVM-to-assembly conversion

and a trivial subclass of TargetAsmInfo.

5.写把LLVM IR转化到你的目标机器GAS格式的汇编打印机的代码。你应该添加汇编串给定义在你的TargetInstrInfo.td特定版本。你也应该写代码为你的AsmPrinter子类(执行LLVM-to-assembly转换和一个TargetAsmInfo子类)(4?)

• Optionally, add support for subtargets (i.e., variants with different capabilities). You should also write code for a
subclass of the TargetSubtarget class, which allows you to use the -mcpu= and -mattr= command-line

options.

6.可选,为字母表添加支持。你也应该为TargetSubTarget类的子类写代码,这些子类准许你用-mcpi= 和-mattr=命令行的选项

• Optionally, add JIT support and create a machine code emitter (subclass of TargetJITInfo) that is used to

emit binary code directly into memory

7.可选,添加JIT支持并且创造机器码发布器(TargetJITInfo子类)that被用来发布二进制台吗直接去内存。

下面来详细分解一下这些步骤。

首先在lib/Target下建立一个目录来放置所有的文件。 比如X84_64

在这个新的目录里,创建一个CMakeLists.txt的文件。你可以从别的地方复制过来再修改。

这个文件至少应该包括LLVM_TARGET_DEFINITIONS变量。这个库可以叫做LLVMDummy。同样地,你也可以把这个库分为LLVMDummyCodeGen和LLVMDummyAsmPrinter,后者应该被执行在子类目录lib/Target/Dummy里。

为了使得你的目标可以实实在在地做一些事情,你需要去执行一个TargetMachine的子类。它的实现应该典型地在文件lib/Target/DummyTargetMachine.cpp里面,但是任何lib/Target目录里的文件都应该被build并且工作。为了用LLVM的target independent code generator,你应该做所有现在机器的后端做的事情:创建一个LLVMTargetMachine子类。

为了使得llvm实际建立并且链接到你的目标,你应该运行cmake在选项-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=X86_64。这个选项将build你的目标而不需要加到它里面所有的目标。

一旦你的目标稳定了,你能把它加到CMakeLists.txt文件中的LLVM_ALL_TARGETS变量里。

以上所说的都是一些与主体无关的东西,接下来说的是主题的东西。

目标机器的描述

目标寄存器的描述

目标寄存器堆和寄存器类的描述

目标指令集的描述

目标指令选择器的描述

汇编打印机

子目标支持

JIT支持


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值