【轉】爲LLVM移植一個新的後端所需的幾個基本步驟

To write a compiler backend for LLVM that converts the LLVM IR to code for a specified target (machine or other language), follow these steps:
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.
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.
Describe the instruction set of the target. Use TableGen to generate code for target-specific instructions from target-specific versions ofTargetInstrFormats.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.
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.
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.
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.
Optionally, add JIT support and create a machine code emitter (subclass of TargetJITInfo) that is used to emit binary code directly into memory.

In the .cpp and .h. files, initially stub up these methods and then implement them later. Initially, you may not know which private members that the class will need and which components will need to be subclassed.


爲LLVM編寫一個編譯器後端以將LLVM IR (中間語言, 或稱中間表示)轉換到某個特定的目標機或語言,應遵循以下幾步:
  • 建立一個描述目標機特性的TargetMachine 的子類. 你可以複製一些已經存在的某個目標機的類和頭文件,比如說, SparcTargetMachine.cpp和SparcTargetMachine.h, 把Sparc這個名字改成你所要移植的目標機的名字, 同樣的, 也要修改代碼中的與Sparc相關的地方.
  • 描 述目標機的寄存器. 在RegisterInfo.td中描述目標機特定的寄存器定義、寄存器別名和寄存器的種類,把它做爲TableGen的輸入生成包含這些信息的代碼。你 可能還需要爲TargetRegisterInfo這個類寫一些子類,在這當中描述一些用於寄存器分配時的信息以及描述各個寄存器之間的是否互相影響等。
  • 描 述目標機的指令集。在TargetInstrFormats.td和TargetInstrInfo.td兩個文件中描述目標機指令集的各個版本,之後用 TableGen生成描述目標機的代碼。同樣的,在之前生成的代碼中爲TargetInstrInfo這個類寫一些附加的子類,以描述目標機的指令。
  • 描 述如何把用LLVM IR指令描述的DAG(有向無環圖)選擇並轉換成本地目標機的指令。使用包含目標機附加信息的TargetInstrInfo.td作爲TableGen 的輸入,生成包含指令匹配和選擇的代碼。爲XXXISelDAGToDAG.cpp(XXX代表目標機的名字)編寫模式匹配和DAG到DAG指令選擇的代 碼。在XXXISelLowering.cpp中, 還需要替換或者刪掉在SelectionDAG裏不支持的操作或者數據類型。
  • 編 寫彙編代碼生成器的代碼以將LLVM IR轉換爲針對你目標機的GAS格式。你應該在描述你目標機的TargetInstrInfo.td中爲每條指令加入對應的字符串名稱。你還應該爲 AsmPrinter這個類寫一些子類以描述從LLVM到彙編的轉換,TargetAsmInfo也有一些很瑣碎的子類需要完成。
  • (可選的)加入所支持的子目標機(如:同一目標機的不同特性的變體)。這需要爲TargetSubtarget這個類添加一些子類,以使你能夠使用像 –mcpu= 和 –mattr=這樣的命令行選項。
  • (可選的)添加JIT(Just In Time,運行時編譯執行)支持並且完成一個指令發射器,它能夠把編譯出的二進制碼直接發射到內存中去。

在.cpp和.h文件中,首先定義這些方法但不要急於實現他。在最開始的時候,你通常不知道這些類需要哪些私有的成員,也不知到組成它需要哪些子類。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章