ukitaka
December 15, 2018
380

# SwiftのDemanglerを書く @ わいわいswiftc番外編

## ukitaka

December 15, 2018

## Transcript

1. Θ͍Θ͍swiftc൪֎ฤ @ukitaka
SwiftͷDemanglerΛॻ͘

2. ࠓ೔ͷ໨ඪ

3. \$S13ExampleNumber6isEven6numberSbSi_tF
isEven(number: Int) -> Bool
Ψν੎

4. Mangle / Demangleͱ͸
Mangleͱ͸
\$S13ExampleNumber6isEven6numberSbSi_tF
ExampleNumber.swift ͷ isEven(number: Int) -> Bool

5. Mangle / Demangleͱ͸
Demangleͱ͸
\$S13ExampleNumber6isEven6numberSbSi_tF
ExampleNumber.isEven(number: Swift.Int) -> Swift.Bool

6. Mangle / Demangleͱ͸
Mangle͞ΕͨγϯϘϧ໊Λ֬ೝ͢Δ
\$ swiftc -emit-sil Examples/ExampleNumber.swift

7. Mangle / Demangleͱ͸
Mangle͞ΕͨγϯϘϧ໊Λ֬ೝ͢Δ
\$ swiftc -emit-sil Examples/ExampleNumber.swift

8. Mangle / Demangleͱ͸
SwiftͷDemangleίϚϯυ
\$ swift demangle '\$S13ExampleNumber6isEven6numberSbSi_tF'
\$S13ExampleNumber6isEven6numberSbSi_tF
—> ExampleNumber.isEven(number: Swift.Int) -> Swift.Bool

9. Mangle / Demangleͱ͸
SwiftͷDemangleίϚϯυ
\$ swift demangle '\$S13ExampleNumber6isEven6numberSbSi_tF'
\$S13ExampleNumber6isEven6numberSbSi_tF
—> ExampleNumber.isEven(number: Swift.Int) -> Swift.Bool
ࠓ೔͸͜ΕΛ࡞Γ·͢

10. Demangler
Demangler
▸ Demanglerͷ࡞੒Λ௨ͯ͠ҎԼΛֶ΅͏
▸ SwiftͷManglingͷ࢓૊ΈΛֶͿ
▸ ؆୯ͳParserΛ࡞੒͢ΔͷͰɺParserͷςΫχοΫΛମݧͰ͖Δ

11. Swift Demangler workshop
՝୊ʹ͍ͭͯ
▸ ՝୊1ʙ5·Ͱ͋Γɺ՝୊1Λશһ͕ୡ੒͢Δ͜ͱΛ໨ඪʹ͠·͢
▸ ՝୊1ʹ͸αϙʔτ͕͍ͭͯΔͷͰɺͦΕΛΈͳ͕Β΍ͬͯΈ·͠ΐ͏
▸ Ͱ͖Δਓ͸ͲΜͲΜਐΊͯ΋Βͬͯେৎ෉Ͱ͢
▸ ಛʹ5ͷsubstitution͸ॏཁͳͱ͜ΖͳͷͰ

12. ՝୊1

13. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
ExampleNumber.isEven(number: Swift.Int) -> Swift.Bool

14. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
SwiftͷMangle nameʹڞ௨Ͱͭ͘Preﬁx

15. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
ؔ਺ΛMangleͨ͠΋ͷͰ͋Δͱ͍͏໨ҹ F

16. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
“ExampleNumber” ͱ͍͏Ϟδϡʔϧ໊ (ϑΝΠϧ໊)
13͸͔͜͜Β13จࣈ෼͕ࣝผࢠͰ͋Δ͜ͱΛද͢

17. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
“isEven” ͱ͍͏ؔ਺ͷ໊લ
6จࣈ෼͕ࣝผࢠͰ͋Δ͜ͱΛࣔ͢

18. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
“number” ͱ͍͏ϥϕϧ
6จࣈ෼͕ࣝผࢠͰ͋Δ͜ͱΛࣔ͢

19. Swift Demangler workshop
՝୊1
\$S13ExampleNumber6isEven6numberSbSi_tF
ؔ਺ͷγάωνϟ (≒ ܕ)
Sb͸ฦΓ஋ͷܕ Bool
Si_t͸Ҿ਺ͷܕ (Int)

20. Swift Demangler workshop
՝୊1
global ::= entity
entity ::= context entity-spec
context ::= module
module ::= identifier
\$S13ExampleNumber6isEven6numberSbSi_tF
global

21. Swift Demangler workshop
՝୊1
global ::= entity
entity ::= context entity-spec
context ::= module
module ::= identifier
\$S13ExampleNumber6isEven6numberSbSi_tF
global
entity

22. Swift Demangler workshop
՝୊1
global ::= entity
entity ::= context entity-spec
context ::= module
module ::= identifier
\$S13ExampleNumber6isEven6numberSbSi_tF
context
entity-spec
module

23. Swift Demangler workshop
՝୊1
entity-spec ::= decl-name label-list function-signature 'F'
function-signature ::= params-type params-type throws?
label-list ::= empty-list
label-list ::= ('_' | identifier)*
throws ::= 'K'
params-type ::= type
decl-name ::= identifier
identifier ::= NATURAL IDENTIFIER-STRING
\$S13ExampleNumber6isEven6numberSbSi_tF
decl-name label-list function-signature

24. Swift Demangler workshop
՝୊1 (࠶ܝ)
\$S13ExampleNumber6isEven6numberSbSi_tF
ExampleNumber.isEven(number: Swift.Int) -> Swift.Bool