SlideShare a Scribd company logo
1 of 62
Download to read offline
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
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺

More Related Content

What's hot

Zynq mp勉強会資料
Zynq mp勉強会資料Zynq mp勉強会資料
Zynq mp勉強会資料一路 川染
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
 
Vivado hlsのシミュレーションとhlsストリーム
Vivado hlsのシミュレーションとhlsストリームVivado hlsのシミュレーションとhlsストリーム
Vivado hlsのシミュレーションとhlsストリームmarsee101
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDPlcplcp1
 
Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardJian-Hong Pan
 
[COSCUP 2020] How to use llvm frontend library-libtooling
[COSCUP 2020] How to use llvm frontend library-libtooling[COSCUP 2020] How to use llvm frontend library-libtooling
[COSCUP 2020] How to use llvm frontend library-libtoolingDouglas Chen
 
Performance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedBrendan Gregg
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu WorksZhen Wei
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)Brendan Gregg
 
Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Kent Chen
 
EBPF and Linux Networking
EBPF and Linux NetworkingEBPF and Linux Networking
EBPF and Linux NetworkingPLUMgrid
 
LLVM Register Allocation
LLVM Register AllocationLLVM Register Allocation
LLVM Register AllocationWang Hsiangkai
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMMin-Yih Hsu
 
Introduction to eBPF
Introduction to eBPFIntroduction to eBPF
Introduction to eBPFRogerColl2
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDBDavid Khosid
 
ACRiウェビナー:小野様ご講演資料
ACRiウェビナー:小野様ご講演資料ACRiウェビナー:小野様ご講演資料
ACRiウェビナー:小野様ご講演資料直久 住川
 

What's hot (20)

Zynq mp勉強会資料
Zynq mp勉強会資料Zynq mp勉強会資料
Zynq mp勉強会資料
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
Vivado hlsのシミュレーションとhlsストリーム
Vivado hlsのシミュレーションとhlsストリームVivado hlsのシミュレーションとhlsストリーム
Vivado hlsのシミュレーションとhlsストリーム
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
 
Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development Board
 
[COSCUP 2020] How to use llvm frontend library-libtooling
[COSCUP 2020] How to use llvm frontend library-libtooling[COSCUP 2020] How to use llvm frontend library-libtooling
[COSCUP 2020] How to use llvm frontend library-libtooling
 
Performance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting Started
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!Working Remotely (via SSH) Rocks!
Working Remotely (via SSH) Rocks!
 
EBPF and Linux Networking
EBPF and Linux NetworkingEBPF and Linux Networking
EBPF and Linux Networking
 
eBPF/XDP
eBPF/XDP eBPF/XDP
eBPF/XDP
 
LLVM Register Allocation
LLVM Register AllocationLLVM Register Allocation
LLVM Register Allocation
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVM
 
Linux : PSCI
Linux : PSCILinux : PSCI
Linux : PSCI
 
Introduction to eBPF
Introduction to eBPFIntroduction to eBPF
Introduction to eBPF
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
ACRiウェビナー:小野様ご講演資料
ACRiウェビナー:小野様ご講演資料ACRiウェビナー:小野様ご講演資料
ACRiウェビナー:小野様ご講演資料
 
eBPF Basics
eBPF BasicseBPF Basics
eBPF Basics
 

Similar to COSCUP2016 - LLVM框架、由淺入淺

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 National Cheng Kung University
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽COSCUP 2014 : open source compiler 戰國時代的軍備競賽
COSCUP 2014 : open source compiler 戰國時代的軍備競賽Kito Cheng
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913lusecheng
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdfczzz1
 
網路技術心得分享
網路技術心得分享網路技術心得分享
網路技術心得分享Mux Baxer
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)Kris Mok
 
⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 
Simple tech-talk
Simple tech-talkSimple tech-talk
Simple tech-talkliltos
 
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--20120509tidesq
 
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 AnalysisXiaoming Chen
 
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)
自动化漏洞利用关键技术研究(Automatic Vulnerability Exploitation Technologies)Jun LI
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台Shengyou Fan
 
Build desktop app_by_xulrunner
Build desktop app_by_xulrunnerBuild desktop app_by_xulrunner
Build desktop app_by_xulrunnerRack Lin
 

Similar to COSCUP2016 - LLVM框架、由淺入淺 (20)

LLVM introduction
LLVM introductionLLVM introduction
LLVM introduction
 
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 的应用
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
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
 

More from hydai

Introduction to Ewasm - crosslink taipei 2019
Introduction to Ewasm - crosslink taipei 2019Introduction to Ewasm - crosslink taipei 2019
Introduction to Ewasm - crosslink taipei 2019hydai
 
Introduction to ewasm
Introduction to ewasmIntroduction to ewasm
Introduction to ewasmhydai
 
Lity - 讓你更安全的 Smart Contract Language
Lity - 讓你更安全的 Smart Contract LanguageLity - 讓你更安全的 Smart Contract Language
Lity - 讓你更安全的 Smart Contract Languagehydai
 
連哈秋都懂的Git教學
連哈秋都懂的Git教學連哈秋都懂的Git教學
連哈秋都懂的Git教學hydai
 
Vim 由淺入淺
Vim 由淺入淺Vim 由淺入淺
Vim 由淺入淺hydai
 
Slack&typora
Slack&typoraSlack&typora
Slack&typorahydai
 

More from hydai (6)

Introduction to Ewasm - crosslink taipei 2019
Introduction to Ewasm - crosslink taipei 2019Introduction to Ewasm - crosslink taipei 2019
Introduction to Ewasm - crosslink taipei 2019
 
Introduction to ewasm
Introduction to ewasmIntroduction to ewasm
Introduction to ewasm
 
Lity - 讓你更安全的 Smart Contract Language
Lity - 讓你更安全的 Smart Contract LanguageLity - 讓你更安全的 Smart Contract Language
Lity - 讓你更安全的 Smart Contract Language
 
連哈秋都懂的Git教學
連哈秋都懂的Git教學連哈秋都懂的Git教學
連哈秋都懂的Git教學
 
Vim 由淺入淺
Vim 由淺入淺Vim 由淺入淺
Vim 由淺入淺
 
Slack&typora
Slack&typoraSlack&typora
Slack&typora
 

COSCUP2016 - LLVM框架、由淺入淺