๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/๐Ÿ˜ถ ๊ถ๊ธˆ์ฆ

[iOS] Swift์˜ ์ปดํŒŒ์ผ๋Ÿฌ _ LLVM

by MINT09 2024. 3. 12.

โค๏ธ Swift Compiler _ LLVM

LLVM์€ 2000๋…„ ์ผ๋ฆฌ๋…ธ์ด ๋Œ€ํ•™์—์„œ Chris Lattner ์™ธ ๋ช‡ ๋ช…์œผ๋กœ ๊ตฌ์„ฑ๋œ ์—ฐ๊ตฌ ํŒ€์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ์ •์  ๋ฐ ๋™์  ์ปดํŒŒ์ผ์„ ๋ชจ๋‘ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜„๋Œ€์ ์ธ ์ปดํŒŒ์ผ ์ „๋žต์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ๊ตฌ ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘๋˜์—ˆ๋‹ค. intermediate / binary ๊ธฐ๊ณ„ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑ, ์ตœ์ ํ™”, ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ปดํ“จํ„ฐ๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” intermediate, binary ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

๐Ÿ“’ ์ปดํ“จํ„ฐ์˜ ํ•˜๋“œ์›จ์–ด๋Š” Binary Code๋งŒ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋“ค์„ ํ•˜๋“œ์›จ์–ด๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊ฟ”์ฃผ๋Š” ์ž‘์—…์„ ๊ฑฐ์ณ์•ผ ํ•˜๋Š”๋ฐ ์ด๋•Œ Intermediate Representation, ์ฆ‰ ์ค‘๊ฐ„ํ‘œํ˜„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฑฐ์น˜๋Š”, ์ปดํŒŒ์ผ๋Ÿฌ๋‚˜ ์ฝ”๋“œ ์ตœ์ ํ™” ๋„๊ตฌ์— ์˜ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ˜น์€ ์ฝ”๋“œ์ด๋‹ค. ์ตœ์ ํ™”, ๋ณ€ํ™˜ ๋“ฑ ์ถ”๊ฐ€์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์„ค๊ณ„๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ •ํ™•๋„(์ •๋ณด์˜ ์†Œ์‹ค ์œ ๋ฌด)์™€ ๋…๋ฆฝ์„ฑ์„ ์ค‘์š”์‹œํ•œ๋‹ค. ์ถ”์ƒ์ ์ธ ํ”„๋กœ๊ทธ๋žจ ํ‘œํ˜„์ด๋‹ค.

์ค‘๊ฐ„ ํ‘œํ˜„

 

์ค‘๊ฐ„ ํ‘œํ˜„ - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „. ์ค‘๊ฐ„ ํ‘œํ˜„(intermediate representation, IR)์€ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ปดํŒŒ์ผ๋Ÿฌ๋‚˜ ๊ฐ€์ƒ ๋จธ์‹ ์— ์˜ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ค‘๊ฐ„ ํ‘œํ˜„์€

ko.wikipedia.org

๐ŸŸ  ์ด๋ฏธ ์กด์žฌํ•˜๋˜ ์ปดํŒŒ์ผ๋Ÿฌ, GCC

๊ธฐ์กด์— GCC๋ผ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด GCC๋Š” ๋‹จ์ผ ๋ชจ๋†€๋ฆฌ์‹(๋‚ด๋ถ€ ์š”์†Œ๊ฐ„ ์˜์กด์„ฑ์ด ๊ฐ•ํ•œ, ๊ตฌ์กฐ์ ์ธ ๊ฒฐํ•ฉ์ด ๊ฐ•๋ ฅํ•œ) ์ปดํŒŒ์ผ๋Ÿฌ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด ํ™•์žฅ์ด ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๋‹ค.

https://gcc.gnu.org/

 

GCC, the GNU Compiler Collection - GNU Project

GCC, the GNU Compiler Collection The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages (libstdc++,...). GCC was originally written as the compiler for the GNU operating sy

gcc.gnu.org

๐ŸŸ  LLVM์˜ ์žฅ์ 

ํ™•์žฅ (๋ชจ๋“ˆ์„ฑ)

LLVM์€ ๊ฐ ์ปดํฌ๋„ŒํŠธ๋“ค์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ์ค‘์š”์‹œํ•ด์„œ ๋ชจ๋“ˆํ™”๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด GCC์˜ ํ™•์žฅ์ด ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ตœ์ ํ™”

  • Intermediate Representation์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ์— ์ตœ์ ํ™”๊ฐ€ ์šฉ์ดํ•ด์ง„๋‹ค. LLVM ๊ธฐ๋ฐ˜์˜ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ƒˆ๋กœ์šด ์–ธ์–ด์˜ ์ง€์›์ด๋‚˜ ์ตœ์ ํ™” ๊ธฐ๋Šฅ ์ถ”๊ฐ€๊ฐ€ ๋” ๊ฐ„๋‹จํ•˜๊ณ  ์œ ์—ฐํ•˜๋‹ค.
  • LLVM ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ฝ”๋“œ ์ตœ์ ํ™” ๋ฐ ์ปดํŒŒ์ผ ์†๋„ ๋ฉด์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ GCC๋ณด๋‹ค ๋น ๋ฅด๋‹ค.

The LLVM Compiler Infrastructure Project

 

The LLVM Compiler Infrastructure Project

LLVM Overview The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines. The name "LLVM" itself is not an acronym; it is the full name of the proj

llvm.org

 

โค๏ธ LLDB _ Low Level Debugger

  • Xcode IDE์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” Command-Line Debug ํ™˜๊ฒฝ

์•ž์„œ LLVM์—์„œ ๋ชจ๋“ˆํ™” ๋˜์–ด ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ด์šฉํ•ด ์ง„ํ–‰๋œ ์ฃผ์š” ์„œ๋ธŒ ํ”„๋กœ์ ํŠธ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ ์ด LLDB๋ผ๋Š” Debugger์ด๋‹ค. ๊ธฐ๊ณ„์–ด์— ๊ฐ€๊นŒ์šด ์˜์—ญ๊นŒ์ง€ ๋””๋ฒ„๊น…์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

๐ŸŸ  Debuger

๋””๋ฒ„๊ฑฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๋Ÿฐํƒ€์ž„ ์‹œ ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋ถˆํ•„์š”ํ•œ ๋นŒ๋“œ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ๋””๋ฒ„๊ฑฐ์—๋Š” ์‹คํ–‰ ํ๋ฆ„ ์ œ์–ด์™€ ์ƒํƒœ ์ ‘๊ทผ์ด๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. ์ฃผ๋กœ ์ฝ”๋“œ์˜ ์—ฌ๋Ÿฌ ์œ„์น˜์— Breakpoint๋ฅผ ์„ค์ •ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์ œ์–ดํ•œ๋‹ค. breakpoint์— ํ”„๋กœ๊ทธ๋žจ์ด ๋„๋‹ฌํ•  ๋•Œ๋งˆ๋‹ค ๋””๋ฒ„๊ฑฐ๋Š” ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์ผ์‹œ์ ์œผ๋กœ ์ค‘์ง€ํ•˜๊ณ , ๋‹ค์–‘ํ•œ ๋ณ€์ˆ˜์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ฒ€์‚ฌ ๋˜๋Š” ์ˆ˜์ •ํ•˜๊ณ , ๋‹ค์Œ statement๋กœ ๋„˜์–ด๊ฐ€๊ฑฐ๋‚˜ ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜ ํ˜น์€ ์‹คํ–‰์„ ๊ณ„์†ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๐ŸŸ  Stepping

  • Over
    • ํ˜„์žฌ Break ๊ฑธ๋ ค ์žˆ๋Š” ์ง€์ ์—์„œ ๋ฐ”๋กœ ๋‹ค์Œ Statement๋กœ Step Over
    • (lldb) next
    • (lldb) n
  • In
    • ๋‹ค์Œ Statement๊ฐ€ Function Call์ธ ๊ฒฝ์šฐ Debugger๋ฅผ ํ•ด๋‹น ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์œ„์น˜ํ•œ ์‹œ์ž‘ ์ง€์ ์œผ๋กœ ์ด๋™
    • (lldb) step
    • (lldb) s
  • Out
    • ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ function์ด return ๋  ๋•Œ๊นŒ์ง€ ํ”„๋กœ๊ทธ๋žจ์„ ์ง„ํ–‰ํ•œ ํ›„ Break
    • (lldb) finish

๐ŸŸ  po

  • description ์ถœ๋ ฅ
  • NSObject์˜ debugDescription ์ถœ๋ ฅ
  • ๋ณต์žกํ•œ ๋‚ด์šฉ์˜ description ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ์ถœ๋ ฅ

About LLDB and Debugging

 

About LLDB and Debugging

About LLDB and Debugging Debugging refers to creating and using an analytical framework to isolate causal pathways and test hypotheses. The most important tool of debugging is the debugger, which helps you understand how your program behaves at runtime, wi

developer.apple.com

LLDB ์ •๋ณต - ์•ผ๊ณฐ๋‹ท๋„ท

 

LLDB ์ •๋ณต - ์•ผ๊ณฐ๋‹ท๋„ท

po ๋ฅผ ์•Œ๊ณ ๊ณ„์‹ ๊ฐ€์š”? ๋ชฐ๋ผ๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. LLDB Debugging ๋ฐฐ์šฐ๋Ÿฌ ๊ฐ‘์‹œ๋‹ค! ?

yagom.net