SlideShare ist ein Scribd-Unternehmen logo
1 von 62
Downloaden Sie, um offline zu lesen
LLVM框架、由淺⼊入淺
浪打、Hydai
Speakers
$ whoami
楊宗凡 — 浪打
‣ 成功⼤大學電機系四年級
‣ 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
Code to Executable
$ gcc helloworld.c
#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!
Compiler
原始碼
組合語⾔言
Lex Parse
Token
Stream
轉譯AST
bdos equ 0005H
start: mvi c,9
lxi d,msg$
call bdos
ret
msg$:db 'Hello, world!$'
end start
Intel 8080
Assembler
MSG: .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
Perl
Python
Java
C
Ruby
Javascript
他們都造⾃自⼰己的輪⼦子
對應不同的機器
IA-32 AMD64
IA-64 Arch32
AArch64
Sun Sparc
Compiler
原始碼
組合語⾔言
Lex Parse
Token
Stream
轉譯AST
Modern Compiler
$ export CFLAGS = “-O3”
Modern Wheel
原始碼
組合語⾔言
Lex
Yacc
AST
演算法
中介語⾔言
轉譯
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
Copy Propagation
Constant Propagation
Constant Folding
Dead Code Elimination(1)
Dead Code Elimination(2)
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
Python的故事
Python
組合語⾔言
Interpreter
C Code GCC
LLVM Framework
$ git clone http://llvm.org/git/llvm.git
三階段編譯流程
原始碼
Backend
組合語⾔言
OptimizerFrontend
IR
LLVM Framework -
Front End
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
IR
LLVM Framework -
Optimizer
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
IR
LLVM Framework -
Back End
原始碼 Backend 組合語⾔言Frontend
IRPass IR Pass … Pass IR Pass
模組化的開發
原始碼
Backend
組合語⾔言
OptimizerFrontend
LLVM IR
Front End
$ clang -S -emit-llvm main.c
以下使⽤用 Whitespace 語⾔言
Hello, world!
Hello, world(syntax hl)
Whitespace 簡介
• 只有三種 Tokens:
• Space 空⽩白(' ')
• Tabs 制表符(t)
• New lines 換⾏行(n)
• 其餘的字元全部都被當成註解
Front End to IR
程式碼 LLVM IR
Front End to IR
程式碼 LLVM IRParser LLVM API
• Module
• IRBuilder
• Function
• BasicBlock
• Instruction
LLVM IR
$ cat main.ll
LLVM IR
• RISC-style (Reduced Instruction Set Computing)
• ⼈人類可讀的
• 具備 SSA form (Static Single Assignment)
• 無限多的虛擬暫存器
• 任意位元⼤大⼩小
• 不改變⾏行為下 Transform IR ,來做最佳化。
C to IR
int	
  a	
  =	
  1;
int	
  c	
  =	
  a+b;
Optimizer
• LLVM 使⽤用 opt 做最佳化
• 每⼀一種類的分析跟轉換的 pass 都是 opt 的參數
• opt 會照順序幫你⼀一個⼀一個⾛走過這些 pass
• 每個 pass 會⾃自⼰己決定要不要做事
opt 剛才的 C Code
Backend
$ llc -march=z80 main.ll
Basic Ideas
LLVM IR 組合語⾔言
Basic Ideas
SelectionDAGLLVM IR 組合語⾔言
•Combining
•Legalizing
•Scheduling
•Register Allocation
What’s a DAG?
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
TableGen
組譯器
反組譯器
編譯器
除錯器
Target CPU
暫存器
指令格式
指令
代表A
代表B
代表C
代表D
TableGen
組譯器
反組譯器
編譯器
除錯器
Target CPU
Table
Gen
暫存器
指令格式
指令
Legalizing
LOAD LD
8bit
加法
除法
32bit
加法
⼀一堆
減法
Legal
Promote
Custom
Instruction Selecting
組合語⾔言
Legalized
DAG
Instruction
Selection
TableGe
n
Register Allocation
SSA
虛擬暫存器
CPU暫存器
分配
最佳化
Instruction Scheduling
原先指令順序 CPU
更適合的順續
順序
最佳化
llvm::SelectionDAGISel
LLVM IR 組合語⾔言
Original DAG
Combined
DAG
Legalized
DAG
Target
Legalizing
DAG
Combiner
Instruction
Selection
TableGe
n
Demo
A 40 years portal by LLVM, Linking 1976 and 2016
Demo
A 40 years portal by LLVM, Linking 1976 and 2016
WhiteSpace
LLVM IR 組合語⾔言
CP/M
LLVM
Zilog Z80
模組化的LLVM
原始碼
Backend
組合語⾔言
OptimizerFrontend
Reference
$ uname -a
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
Q & A
$ man man
COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺

Weitere ähnliche Inhalte

Was ist angesagt?

The Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEPThe Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEP
Gregor Schmidt
 

Was ist angesagt? (20)

Virtual Machine Constructions for Dummies
Virtual Machine Constructions for DummiesVirtual Machine Constructions for Dummies
Virtual Machine Constructions for Dummies
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
The Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEPThe Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEP
 
CMake best practices
CMake best practicesCMake best practices
CMake best practices
 
用十分鐘 向jserv學習作業系統設計
用十分鐘  向jserv學習作業系統設計用十分鐘  向jserv學習作業系統設計
用十分鐘 向jserv學習作業系統設計
 
ARM and SoC Traning Part II - System
ARM and SoC Traning Part II - SystemARM and SoC Traning Part II - System
ARM and SoC Traning Part II - System
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
GCC
GCCGCC
GCC
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for Developers
 
Introduction to eBPF
Introduction to eBPFIntroduction to eBPF
Introduction to eBPF
 
系統程式 -- 附錄
系統程式 -- 附錄系統程式 -- 附錄
系統程式 -- 附錄
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
 
ARM Trusted FirmwareのBL31を単体で使う!
ARM Trusted FirmwareのBL31を単体で使う!ARM Trusted FirmwareのBL31を単体で使う!
ARM Trusted FirmwareのBL31を単体で使う!
 
makefiles tutorial
makefiles tutorialmakefiles tutorial
makefiles tutorial
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shell
 
Golang
GolangGolang
Golang
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
レシピの作り方入門
レシピの作り方入門レシピの作り方入門
レシピの作り方入門
 

Ähnlich wie COSCUP 2016 - LLVM 由淺入淺

網路技術心得分享
網路技術心得分享網路技術心得分享
網路技術心得分享
Mux Baxer
 
Simple tech-talk
Simple tech-talkSimple tech-talk
Simple tech-talk
liltos
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log Analysis
Xiaoming Chen
 
Build desktop app_by_xulrunner
Build desktop app_by_xulrunnerBuild desktop app_by_xulrunner
Build desktop app_by_xulrunner
Rack Lin
 

Ähnlich wie COSCUP 2016 - LLVM 由淺入淺 (20)

Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
 
網路技術心得分享
網路技術心得分享網路技術心得分享
網路技術心得分享
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
Simple tech-talk
Simple tech-talkSimple tech-talk
Simple tech-talk
 
Tcfsh bootcamp day2
 Tcfsh bootcamp day2 Tcfsh bootcamp day2
Tcfsh bootcamp day2
 
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
 
Rootkit 101
Rootkit 101Rootkit 101
Rootkit 101
 
Baidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log AnalysisBaidu LSP and DISQL for Log Analysis
Baidu LSP and DISQL for Log Analysis
 
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
 
Build desktop app_by_xulrunner
Build desktop app_by_xulrunnerBuild desktop app_by_xulrunner
Build desktop app_by_xulrunner
 
Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出Shell,信号量以及java进程的退出
Shell,信号量以及java进程的退出
 
Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫
 

Mehr von 宗凡 楊 (7)

uRock @ Jserv Course Final
uRock @ Jserv Course Final uRock @ Jserv Course Final
uRock @ Jserv Course Final
 
uRock @ SITCON 2015
uRock @ SITCON 2015uRock @ SITCON 2015
uRock @ SITCON 2015
 
C4Labs PCB Miller Project
C4Labs PCB Miller ProjectC4Labs PCB Miller Project
C4Labs PCB Miller Project
 
Lambda's CNC @FabLab 10/27/14
Lambda's CNC @FabLab 10/27/14Lambda's CNC @FabLab 10/27/14
Lambda's CNC @FabLab 10/27/14
 
Cnc fablab
Cnc   fablabCnc   fablab
Cnc fablab
 
Cnc fablab
Cnc   fablabCnc   fablab
Cnc fablab
 
Lambda's CNC @FabLab 10/27/14
Lambda's CNC @FabLab 10/27/14Lambda's CNC @FabLab 10/27/14
Lambda's CNC @FabLab 10/27/14
 

COSCUP 2016 - LLVM 由淺入淺