coscup 2016 - llvm 由淺入淺
TRANSCRIPT
楊宗凡 — 浪打 ‣ 成功⼤大學電機系四年級 ‣ sonic.tw.tp (at) gmail.com
戴宏穎 — Hydai ‣ 清華⼤大學資⼯工碩⼀一年級 ‣ z54981220 (at) gmail.com
Github Repos:https://github.com/sonicyang/ws-frontend
https://github.com/sonicyang/llvm-z80
#include <stdio.h> int main(int argc, char* argv[]){ puts(“Hello World!”); return 0; }
In C
Hello World!print “Hello World!”
In Python
Hello World!
魔法 我想學魔法!
The Magic
原始碼 組合語⾔言 機械碼
AssemblerCompiler
print “!@#$” mov d, msg$ mov c, 9
call 5
110100….
>./hw機械碼
110100….
Hello World!
bdos equ 0005H start: mvi c,9 lxi d,msg$ call bdos ret msg$: db 'Hello, world!$' end start
Intel 8080
AssemblerMSG: .ASCIIZ "Hello, world!" LDX #0 LDA MSG,X @LP: JSR $FFD2 INX LDA MSG,X BNE @LP RTS
MOS 6502
8080 Assemble
6502 Assemble
110110.. 010111..
CP/M Apple Dos
Front End to IR
程式碼 LLVM IRParser LLVM API
• Module • IRBuilder • Function • BasicBlock • Instruction
LLVM IR
• RISC-style (Reduced Instruction Set Computing)
• ⼈人類可讀的
• 具備 SSA form (Static Single Assignment)
• 無限多的虛擬暫存器 • 任意位元⼤大⼩小
• 不改變⾏行為下 Transform IR ,來做最佳化。
Optimizer
• LLVM 使⽤用 opt 做最佳化
• 每⼀一種類的分析跟轉換的 pass 都是 opt 的參數
• opt 會照順序幫你⼀一個⼀一個⾛走過這些 pass
• 每個 pass 會⾃自⼰己決定要不要做事
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined DAG
Legalized DAG
Target Legalizing
DAG Combiner
Instruction Selection
TableGen
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined DAG
Legalized DAG
Target Legalizing
DAG Combiner
Instruction Selection
TableGen
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined DAG
Legalized DAG
Target Legalizing
DAG Combiner
Instruction Selection
TableGen
1. Architecture for Next Generation GCC ftp://gcc.gnu.org/pub/gcc/summit/2003/Architecture%20for%20a%20Next-Generation%20GCC.pdf
2. Life of an Instruction in LLVM http://eli.thegreenplace.net/2012/11/24/life-of-an-instruction-in-llvm
3. A deeper look into the LLVM code generator http://eli.thegreenplace.net/2013/02/25/a-deeper-look-into-the-llvm-code-generator-part-1
4. LLVM TableGen Documentation http://llvm.org/docs/TableGen/index.html
5. LLVM TableGen Introduction http://llvm.org/docs/TableGen/LangIntro.html
6. Independent Code Generator http://llvm.org/docs/CodeGenerator.html
7. The Relationship between selectiondag and selectiondagisel http://stackoverflow.com/questions/26814062/the-relationship-between-selectiondag-and-selectiondagisel
8. How TableGen’s DAGISel Backend Works https://github.com/draperlaboratory/fracture/wiki/How-TableGen's-DAGISel-Backend-Works
9. ZASM - Z80 Assembler http://k1.spdns.de/Develop/Projects/zasm/Documentation/
10. LLVM Z80 Backend https://github.com/mostlyuseful/llvm-z80/network
11. Whitespace LLVM https://github.com/Subv/Whitespace-LLVM