Slide 1

Slide 1 text

Kolin/Native͸ͳͥಈ͘ͷ͔ @youta1119 2019.08.24 KotlinFest2019

Slide 2

Slide 2 text

About me • Ԯ໺ཅଠ @youta1119 • αʔόʔαΠυΤϯδχΞ@CyberAgent • ීஈ͸Javaͱ͔Goͱ͔ॻ͍ͯΔ

Slide 3

Slide 3 text

Goal Kotlin/Native͕ͳͥಈ͘ͷ͔ͳΜͱͳ͘ ཧղͰ͖ΔΑ͏ʹͳΔ

Slide 4

Slide 4 text

Agenda • ୈ1෦ Kotlin/Nativeͱ͸ • ୈ2෦ ͳͥKotlin͸NativeͰಈ͘ͷ͔ʁ • ୈ3෦ Kotlin͸ͲͷΑ͏ʹͯ͠ωΠςΟϒόΠφϦʹͳΔͷ͔? • Q&A

Slide 5

Slide 5 text

࠷৽൛1.3.50ʹରԠ͍ͯ͠·͢

Slide 6

Slide 6 text

ୈ1෦ Kotlin/Nativeͱ͸ʁ

Slide 7

Slide 7 text

Kotlin/Nativeͱ͸ʁ • KotlinϓϩάϥϜΛωΠςΟϒόΠφϦʹίϯύΠϧ͢Δٕज़ LLVM

Slide 8

Slide 8 text

αϙʔτ͍ͯ͠ΔϓϥοτϑΥʔϜ • iOS (arm32, arm64, simulator x86_64) • MacOS (x86_64) • Android (arm32, arm64) • Windows (mingw x86_64, x86) • Linux (x86_64, arm32, MIPS, MIPS little endian, Raspberry Pi) • WebAssembly (wasm32)

Slide 9

Slide 9 text

ओͳػೳ • ֤ϓϥοτϑΥʔϜ޲͚ͷόΠφϦੜ੒ • Cͱͷ૬ޓӡ༻(Cinterop) • Objective-CɾSwiftͱͷ૬ޓӡ༻(ObjcInterop)

Slide 10

Slide 10 text

ୈ2෦ ͳͥKotlin͸NativeͰಈ͘ͷ͔ʁ

Slide 11

Slide 11 text

Runtimeͱ͸ʁ

Slide 12

Slide 12 text

Runtimeͱ͸ʁ KotlinΛNativeͰಈ͔ͨ͢Ίʹඞཁͳ΋ͷ • ඪ४ϥΠϒϥϦ • ϝϞϦ؅ཧʢGCʣ • ϓϩάϥϜϥϯνϟʔ(ޙͰղઆ)

Slide 13

Slide 13 text

ͳͥRuntime͕ඞཁͳͷʁ

Slide 14

Slide 14 text

Kotlin on JVM JVM ϝϞϦ؅ཧ όΠτίʔυ࣮ߦ JRE ඪ४ϥΠϒϥϦ(JavaAPIʣ

Slide 15

Slide 15 text

Kotlin on Native JVM ϝϞϦ؅ཧ όΠτίʔυ࣮ߦ JRE ඪ४ϥΠϒϥϦ(JavaAPIʣ NativeʹJREͳΜͯͳ͍

Slide 16

Slide 16 text

Runtimeͱ͸ʁ KotlinΛNativeͰಈ͔ͨ͢Ίʹ࠷௿ݶඞཁͳ΋ͷ • ඪ४ϥΠϒϥϦ • ϝϞϦ؅ཧʢGCʣ • ϓϩάϥϜϥϯνϟʔ(ޙͰղઆ)

Slide 17

Slide 17 text

ඪ४ϥΠϒϥϦ • kotlin.*͔Β࢝·Δύοέʔδͷ΍ͭ • Kotlin Native༻ʹඪ४ϥΠϒϥϦΛҰ͔Β࣮૷ • جຊతʹC++Ͱ࣮૷ • ࣮૷͸͜ͷล https://github.com/JetBrains/kotlin-native/tree/master/runtime/src/main

Slide 18

Slide 18 text

ඪ४ϥΠϒϥϦͷ࣮૷ ̎ύλʔϯͷ࣮૷ํ๏͕͋Δ 1. KotlinͷΈͰ࣮૷ (e.g ranges, coroutines) 2. جຊతʹKotlinͰ͸ؔ਺ͷఆٛͷΈΛߦ͍ɺ࣮૷͸C++Ͱߦ͏ ʢe.g stringܕ, printؔ਺ʣ

Slide 19

Slide 19 text

package kotlin.io /** Prints the given [message] to the standard output stream. */ @SymbolName("Kotlin_io_Console_print") external public fun print(message: String) Kotlinଆ

Slide 20

Slide 20 text

package kotlin.io /** Prints the given [message] to the standard output stream. */ @SymbolName("Kotlin_io_Console_print") external public fun print(message: String) Kotlinଆ

Slide 21

Slide 21 text

package kotlin.io /** Prints the given [message] to the standard output stream. */ @SymbolName("Kotlin_io_Console_print") external public fun print(message: String) Kotlinଆ ίϯύΠϥʹରͯؔ͠਺ͷ࣮ମ͸֎ ෦ʹ͋Δͱڭ͑Δҝͷ΋ͷ ؔ਺ͷ࣮ମͷγϯϘϧ໊ $ؔ਺໊ Λද͢

Slide 22

Slide 22 text

void Kotlin_io_Console_print(KString message) { if (message->type_info() != theStringTypeInfo) { ThrowClassCastException(message->obj(), theStringTypeInfo); } // TODO: system stdout must be aware about UTF-8. const KChar* utf16 = CharArrayAddressOfElementAt(message, 0); KStdString utf8; // Replace incorrect sequences with a default codepoint (see utf8::with_replacement::default_replacement) utf8::with_replacement::utf16to8(utf16, utf16 + message->count_, back_inserter(utf8)); konan::consoleWriteUtf8(utf8.c_str(), utf8.size()); } package kotlin.io /** Prints the given [message] to the standard output stream. */ @SymbolName("Kotlin_io_Console_print") external public fun print(message: String) Kotlinଆ C++ଆ

Slide 23

Slide 23 text

Runtimeͱ͸ʁ KotlinΛNativeͰಈ͔ͨ͢Ίʹ࠷௿ݶඞཁͳ΋ͷ • ඪ४ϥΠϒϥϦ • ϝϞϦ؅ཧʢGCʣ • ϓϩάϥϜϥϯνϟʔ(ޙͰղઆ)

Slide 24

Slide 24 text

ϝϞϦ؅ཧ • جຊతʹGCΛ࢖͏ • CͷϥΠϒϥϦΛ࢖͏࣌͸ϓϩάϥϚͷ੹೚Ͱ… • GCΛࣗલ࣮૷͍ͯ͠Δʢݴޠ͸C++ʣ • Bacon's algorithmͱ͍͏GCΞϧΰϦζϜΛ࢖ͬͯΔΆ͍

Slide 25

Slide 25 text

Runtimeͱ͸ʁ KotlinΛNativeͰಈ͔ͨ͢Ίʹ࠷௿ݶඞཁͳ΋ͷ • ඪ४ϥΠϒϥϦ • ϝϞϦ؅ཧʢGCʣ • ϓϩάϥϜϥϯνϟʔ(ޙͰղઆ)

Slide 26

Slide 26 text

ϓϩάϥϜϥϯνϟʔ KotlinϓϩάϥϜΛىಈͤ͞Δ΋ͷ • GCͷॳظԽ • KotlinϓϩάϥϜͷىಈ

Slide 27

Slide 27 text

ͳͥϓϩάϥϜϥϯνϟʔ͕ඞཁͳͷ͔ʁ

Slide 28

Slide 28 text

ϓϥοτϑΥʔϜʹΑͬͯKotlinϓϩάϥϜͷىಈํ๏͕ҧ͏ • Mac, Linux, Windowsͩͱ… ࣮ߦͨ͠ͱ͖ • Androidͩͱ… Activity͕࡞ΒΕͨͱ͖ • Wasmͩͱ… ϒϥ΢β্ͰJSʹݺ͹Εͨͱ͖

Slide 29

Slide 29 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); }

Slide 30

Slide 30 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); } .BJOؔ਺

Slide 31

Slide 31 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); }

Slide 32

Slide 32 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); } ($ͷॳظԽ

Slide 33

Slide 33 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); } ,PUMJOͷϓϩάϥϜΛىಈͤ͞Δؔ਺ ͷݺͼग़͠

Slide 34

Slide 34 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); } ϝϞϦղ์

Slide 35

Slide 35 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); }

Slide 36

Slide 36 text

extern "C" RUNTIME_USED int Init_and_run_start(int argc, const char** argv, int memoryDeInit) { #ifdef KONAN_NO_CTORS_SECTION extern void _Konan_constructors(void); _Konan_constructors(); #endif Kotlin_initRuntimeIfNeeded(); // GCͷॳظԽ KInt exitStatus = Konan_run_start(argc, argv); // KotlinϓϩάϥϜͷىಈ if (memoryDeInit) Kotlin_deinitRuntimeIfNeeded(); // ϝϞϦղ์ return exitStatus; } // Mainؔ਺ extern "C" RUNTIME_USED int Konan_main(int argc, const char** argv) { return Init_and_run_start(argc, argv, 1); }

Slide 37

Slide 37 text

ϓϥοτϑΥʔϜϥΠϒϥϦͱ͸ʁ

Slide 38

Slide 38 text

ϓϥοτϑΥʔϜϥΠϒϥϦ • platform.* ͱ͍͏ύοέʔδͷ΍ͭ • ֤ϓϥοτϑΥʔϜݻ༗ͷAPIʹΞΫηε͢ΔҝͷϥΠϒϥϦ (e.g posix) • CInteropͱ͍͏ػೳΛ࢖ͬͯࣗಈੜ੒͍ͯ͠Δ

Slide 39

Slide 39 text

CInteropͱ͸? • CͱKotlinΛ૬ޓӡ༻Մೳʹ͢Δπʔϧ • ϒϦοδ༻ͷελϒΛੜ੒͢Δ

Slide 40

Slide 40 text

// macos posix.def depends = package = platform.posix headers = alloca.h ar.h assert.h complex.h dirent.h dlfcn.h err.h errno.h fcntl.h \ fenv.h float.h fnmatch.h fts.h ftw.h getopt.h grp.h inttypes.h libgen.h limits.h \ locale.h math.h memory.h netdb.h paths.h poll.h \ pthread.h pwd.h regex.h resolv.h sched.h search.h semaphore.h setjmp.h sgtty.h signal.h \ stdatomic.h stdint.h stdio.h stdlib.h string.h strings.h syslog.h termios.h \ time.h ucontext.h unistd.h utime.h utmp.h wchar.h wctype.h xlocale.h \ net/ethernet.h net/if.h net/if_arp.h net/route.h \ netinet/icmp6.h netinet/if_ether.h netinet/in.h netinet/in_systm.h \ netinet/ip.h netinet/ip6.h netinet/ip_icmp.h netinet/tcp.h netinet/udp.h \ sys/acl.h sys/ioctl.h sys/ipc.h sys/mman.h sys/poll.h sys/ptrace.h \ sys/queue.h sys/select.h sys/shm.h sys/socket.h sys/stat.h \ sys/syslimits.h sys/time.h sys/times.h sys/utsname.h sys/wait.h compilerOpts = -D_XOPEN_SOURCE -DSHARED_LIBBIND -D_DARWIN_NO_64_BIT_INODE # -D_ANSI_SOURCE, sigh, breaks user_addr_t excludedFunctions = KERNEL_AUDIT_TOKEN KERNEL_SECURITY_TOKEN add_profil \ addrsel_policy_init \ in6addr_linklocal_allv2routers \ pfctlinput regwnexec_l res_send_setqhook res_send_setrhook \ unwhiteout zopen profil openat acl_valid_link_np linkerOpts = -ldl -lresolv

Slide 41

Slide 41 text

// macos posix.def depends = package = platform.posix headers = alloca.h ar.h assert.h complex.h dirent.h dlfcn.h err.h errno.h fcntl.h \ fenv.h float.h fnmatch.h fts.h ftw.h getopt.h grp.h inttypes.h libgen.h limits.h \ locale.h math.h memory.h netdb.h paths.h poll.h \ pthread.h pwd.h regex.h resolv.h sched.h search.h semaphore.h setjmp.h sgtty.h signal.h \ stdatomic.h stdint.h stdio.h stdlib.h string.h strings.h syslog.h termios.h \ time.h ucontext.h unistd.h utime.h utmp.h wchar.h wctype.h xlocale.h \ net/ethernet.h net/if.h net/if_arp.h net/route.h \ netinet/icmp6.h netinet/if_ether.h netinet/in.h netinet/in_systm.h \ netinet/ip.h netinet/ip6.h netinet/ip_icmp.h netinet/tcp.h netinet/udp.h \ sys/acl.h sys/ioctl.h sys/ipc.h sys/mman.h sys/poll.h sys/ptrace.h \ sys/queue.h sys/select.h sys/shm.h sys/socket.h sys/stat.h \ sys/syslimits.h sys/time.h sys/times.h sys/utsname.h sys/wait.h compilerOpts = -D_XOPEN_SOURCE -DSHARED_LIBBIND -D_DARWIN_NO_64_BIT_INODE # -D_ANSI_SOURCE, sigh, breaks user_addr_t excludedFunctions = KERNEL_AUDIT_TOKEN KERNEL_SECURITY_TOKEN add_profil \ addrsel_policy_init \ in6addr_linklocal_allv2routers \ pfctlinput regwnexec_l res_send_setqhook res_send_setrhook \ unwhiteout zopen profil openat acl_valid_link_np linkerOpts = -ldl -lresolv ੜ੒͞ΕΔ,PUMJO༻ελϒͷ 1BDLBHF໊Λࢦఆ

Slide 42

Slide 42 text

// macos posix.def depends = package = platform.posix headers = alloca.h ar.h assert.h complex.h dirent.h dlfcn.h err.h errno.h fcntl.h \ fenv.h float.h fnmatch.h fts.h ftw.h getopt.h grp.h inttypes.h libgen.h limits.h \ locale.h math.h memory.h netdb.h paths.h poll.h \ pthread.h pwd.h regex.h resolv.h sched.h search.h semaphore.h setjmp.h sgtty.h signal.h \ stdatomic.h stdint.h stdio.h stdlib.h string.h strings.h syslog.h termios.h \ time.h ucontext.h unistd.h utime.h utmp.h wchar.h wctype.h xlocale.h \ net/ethernet.h net/if.h net/if_arp.h net/route.h \ netinet/icmp6.h netinet/if_ether.h netinet/in.h netinet/in_systm.h \ netinet/ip.h netinet/ip6.h netinet/ip_icmp.h netinet/tcp.h netinet/udp.h \ sys/acl.h sys/ioctl.h sys/ipc.h sys/mman.h sys/poll.h sys/ptrace.h \ sys/queue.h sys/select.h sys/shm.h sys/socket.h sys/stat.h \ sys/syslimits.h sys/time.h sys/times.h sys/utsname.h sys/wait.h compilerOpts = -D_XOPEN_SOURCE -DSHARED_LIBBIND -D_DARWIN_NO_64_BIT_INODE # -D_ANSI_SOURCE, sigh, breaks user_addr_t excludedFunctions = KERNEL_AUDIT_TOKEN KERNEL_SECURITY_TOKEN add_profil \ addrsel_policy_init \ in6addr_linklocal_allv2routers \ pfctlinput regwnexec_l res_send_setqhook res_send_setrhook \ unwhiteout zopen profil openat acl_valid_link_np linkerOpts = -ldl -lresolv ελϒੜ੒ͷର৅ͷϔομϑΝΠϧ

Slide 43

Slide 43 text

// macos posix.def depends = package = platform.posix headers = alloca.h ar.h assert.h complex.h dirent.h dlfcn.h err.h errno.h fcntl.h \ fenv.h float.h fnmatch.h fts.h ftw.h getopt.h grp.h inttypes.h libgen.h limits.h \ locale.h math.h memory.h netdb.h paths.h poll.h \ pthread.h pwd.h regex.h resolv.h sched.h search.h semaphore.h setjmp.h sgtty.h signal.h \ stdatomic.h stdint.h stdio.h stdlib.h string.h strings.h syslog.h termios.h \ time.h ucontext.h unistd.h utime.h utmp.h wchar.h wctype.h xlocale.h \ net/ethernet.h net/if.h net/if_arp.h net/route.h \ netinet/icmp6.h netinet/if_ether.h netinet/in.h netinet/in_systm.h \ netinet/ip.h netinet/ip6.h netinet/ip_icmp.h netinet/tcp.h netinet/udp.h \ sys/acl.h sys/ioctl.h sys/ipc.h sys/mman.h sys/poll.h sys/ptrace.h \ sys/queue.h sys/select.h sys/shm.h sys/socket.h sys/stat.h \ sys/syslimits.h sys/time.h sys/times.h sys/utsname.h sys/wait.h compilerOpts = -D_XOPEN_SOURCE -DSHARED_LIBBIND -D_DARWIN_NO_64_BIT_INODE # -D_ANSI_SOURCE, sigh, breaks user_addr_t excludedFunctions = KERNEL_AUDIT_TOKEN KERNEL_SECURITY_TOKEN add_profil \ addrsel_policy_init \ in6addr_linklocal_allv2routers \ pfctlinput regwnexec_l res_send_setqhook res_send_setrhook \ unwhiteout zopen profil openat acl_valid_link_np linkerOpts = -ldl -lresolv ελϒΛੜ੒͢Δͱ͖ʹ࢖͏ίϯύ ΠϥΦϓγϣϯΛࢦఆ

Slide 44

Slide 44 text

// macos posix.def depends = package = platform.posix headers = alloca.h ar.h assert.h complex.h dirent.h dlfcn.h err.h errno.h fcntl.h \ fenv.h float.h fnmatch.h fts.h ftw.h getopt.h grp.h inttypes.h libgen.h limits.h \ locale.h math.h memory.h netdb.h paths.h poll.h \ pthread.h pwd.h regex.h resolv.h sched.h search.h semaphore.h setjmp.h sgtty.h signal.h \ stdatomic.h stdint.h stdio.h stdlib.h string.h strings.h syslog.h termios.h \ time.h ucontext.h unistd.h utime.h utmp.h wchar.h wctype.h xlocale.h \ net/ethernet.h net/if.h net/if_arp.h net/route.h \ netinet/icmp6.h netinet/if_ether.h netinet/in.h netinet/in_systm.h \ netinet/ip.h netinet/ip6.h netinet/ip_icmp.h netinet/tcp.h netinet/udp.h \ sys/acl.h sys/ioctl.h sys/ipc.h sys/mman.h sys/poll.h sys/ptrace.h \ sys/queue.h sys/select.h sys/shm.h sys/socket.h sys/stat.h \ sys/syslimits.h sys/time.h sys/times.h sys/utsname.h sys/wait.h compilerOpts = -D_XOPEN_SOURCE -DSHARED_LIBBIND -D_DARWIN_NO_64_BIT_INODE # -D_ANSI_SOURCE, sigh, breaks user_addr_t excludedFunctions = KERNEL_AUDIT_TOKEN KERNEL_SECURITY_TOKEN add_profil \ addrsel_policy_init \ in6addr_linklocal_allv2routers \ pfctlinput regwnexec_l res_send_setqhook res_send_setrhook \ unwhiteout zopen profil openat acl_valid_link_np linkerOpts = -ldl -lresolv

Slide 45

Slide 45 text

// in bash $ cinterop -def posix.def QPTJYLMJC͕ੜ੒͞Ε·͢

Slide 46

Slide 46 text

//posix.kt @CCall("knifunptr_platform_posix2") external fun printf(@CCall.CString arg0: String?, vararg variadicArguments: Any?): Int fun putc(arg0: Int, arg1: CValuesRef?): Int { memScoped { return kniBridge24(arg0, arg1?.getPointer(memScope).rawValue) } } //(தུ)

Slide 47

Slide 47 text

ୈ3෦ Kotlin͸ͲͷΑ͏ʹͯ͠ωΠςΟϒόΠφ ϦʹͳΔͷ͔?

Slide 48

Slide 48 text

Kotlin/Native Compiler

Slide 49

Slide 49 text

Frontend: Frontend builds AST CreateSymbolTable: Create SymbolTable ObjCExport: Objective-C header generation BuildCExports: Build C exports Psi2Ir: Psi to IR conversion DestroySymbolTable: Destroy SymbolTable IrGeneratorPlugins: Plugged-in ir generators CopyDefaultValuesToActual: Copy default values from expect to actual declarations Serializer: Serialize descriptor tree and inline IR bodies Backend: All backend IrLowering: IR Lowering (தུ) LowerLibIR: Lower library's IR Bitcode: LLVM Bitcode generation (தུ) VerifyBitcode: Verify bitcode PrintBitcode: Print bitcode ProduceOutput: Produce output Link: Link stage ObjectFiles: Bitcode to object file Linker: Linker $ kotlinc-native -Xlist-phases

Slide 50

Slide 50 text

Frontend: Frontend builds AST CreateSymbolTable: Create SymbolTable ObjCExport: Objective-C header generation BuildCExports: Build C exports Psi2Ir: Psi to IR conversion DestroySymbolTable: Destroy SymbolTable IrGeneratorPlugins: Plugged-in ir generators CopyDefaultValuesToActual: Copy default values from expect to actual declarations Serializer: Serialize descriptor tree and inline IR bodies Backend: All backend IrLowering: IR Lowering (தུ) LowerLibIR: Lower library's IR Bitcode: LLVM Bitcode generation (தུ) VerifyBitcode: Verify bitcode PrintBitcode: Print bitcode ProduceOutput: Produce output Link: Link stage ObjectFiles: Bitcode to object file Linker: Linker $ kotlinc-native -Xlist-phases ϑΣʔζ໊

Slide 51

Slide 51 text

Frontend: Frontend builds AST CreateSymbolTable: Create SymbolTable ObjCExport: Objective-C header generation BuildCExports: Build C exports Psi2Ir: Psi to IR conversion DestroySymbolTable: Destroy SymbolTable IrGeneratorPlugins: Plugged-in ir generators CopyDefaultValuesToActual: Copy default values from expect to actual declarations Serializer: Serialize descriptor tree and inline IR bodies Backend: All backend IrLowering: IR Lowering (தུ) LowerLibIR: Lower library's IR Bitcode: LLVM Bitcode generation (தུ) VerifyBitcode: Verify bitcode PrintBitcode: Print bitcode ProduceOutput: Produce output Link: Link stage ObjectFiles: Bitcode to object file Linker: Linker $ kotlinc-native -Xlist-phases ϑΣʔζͷઆ໌

Slide 52

Slide 52 text

Frontend: Frontend builds AST CreateSymbolTable: Create SymbolTable ObjCExport: Objective-C header generation BuildCExports: Build C exports Psi2Ir: Psi to IR conversion DestroySymbolTable: Destroy SymbolTable IrGeneratorPlugins: Plugged-in ir generators CopyDefaultValuesToActual: Copy default values from expect to actual declarations Serializer: Serialize descriptor tree and inline IR bodies Backend: All backend IrLowering: IR Lowering (தུ) LowerLibIR: Lower library's IR Bitcode: LLVM Bitcode generation (தུ) VerifyBitcode: Verify bitcode PrintBitcode: Print bitcode ProduceOutput: Produce output Link: Link stage ObjectFiles: Bitcode to object file Linker: Linker $ kotlinc-native -Xlist-phases

Slide 53

Slide 53 text

Frontend Phase

Slide 54

Slide 54 text

Frontend Phase • ໾ׂ: KotlinͷϓϩάϥϜͷιʔείʔυΛASTʹม׵͢Δ

Slide 55

Slide 55 text

Psi2Ir Phase

Slide 56

Slide 56 text

Psi2IR Phase • ໾ׂ: AST(PSI)ΛKotlin-IRͱ͍͏தؒݴޠʹม׵͢Δ

Slide 57

Slide 57 text

---------------------------------------------- MODULE_FRAGMENT name: FILE fqName: fileName:/Users/youta1119/Sandbox/main.kt FUN name:main visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY VAR name:a type:kotlin.Int [val] CONST Int type=kotlin.Int value=10 CALL 'public final fun println (message: kotlin.String): kotlin.Unit [external] declared in kotlin.io' type=kotlin.Unit origin=null message: CONST String type=kotlin.String value="hello world" fun main() { val a: Int = 10 println("hello world") }

Slide 58

Slide 58 text

LLVMʹ͍ͭͯ

Slide 59

Slide 59 text

LLVMͱ͸ • ίϯύΠϥͷͨΊͷϑϨʔϜϫʔΫ

Slide 60

Slide 60 text

LLVM-IR ม׵ ίʔυੜ੒

Slide 61

Slide 61 text

LLVM-IR ม׵ ίʔυੜ੒

Slide 62

Slide 62 text

LLVM-IR ίʔυੜ੒ ม׵

Slide 63

Slide 63 text

Backend Phase

Slide 64

Slide 64 text

Backend Phase • ໾ׂ: Kotlin-IRΛLLVM-Bitcodeʹม׵͢Δ ˒ LLVM-Bitcodeͱ͸LLVM-IRΛόΠφϦܗࣜͰදݱͨ͠΋ͷ

Slide 65

Slide 65 text

Backend Phase TemporaryσΟϨΫτϦʹ LLVM Bitcode͕ੜ੒͞ΕΔ

Slide 66

Slide 66 text

Linker Phase

Slide 67

Slide 67 text

Linker Phase ໾ׂ: • LLVM-Bitcode͔ΒΦϒδΣΫτϑΝΠϧͷੜ੒Λੜ੒͢Δ • ੜ੒ͨ͠ΦϒδΣΫτϑΝΠϧͱґଘϥΠϒϥϦΛϦϯΫͯ͞ ωΠςΟϒόΠφϦΛ࡞Δ

Slide 68

Slide 68 text

/Applications/Xcode.app/Contents/Developer/Toolchains/ \ XcodeDefault.xctoolchain/usr/bin/clang++ \ -cc1 -emit-obj -disable-llvm-passes -x ir \ -triple x86_64-apple-macosx10.11.0 -O1 -fembed-bitcode=off \ -mllvm -debug-pass=Structure ./tmp/program.kt.bc -o./tmp/result.o

Slide 69

Slide 69 text

/Applications/Xcode.app/Contents/Developer/Toolchains/ XcodeDefault.xctoolchain/usr/bin/clang++ \ -cc1 -emit-obj -disable-llvm-passes -x ir \ -triple x86_64-apple-macosx10.11.0 -O1 -fembed-bitcode=off \ -mllvm -debug-pass=Structure ./tmp/program.kt.bc -o ./tmp/result.o

Slide 70

Slide 70 text

/Applications/Xcode.app/Contents/Developer/Toolchains/ XcodeDefault.xctoolchain/usr/bin/clang++ \ -cc1 -emit-obj -disable-llvm-passes -x ir \ -triple x86_64-apple-macosx10.11.0 -O1 -fembed-bitcode=off \ -mllvm -debug-pass=Structure ./tmp/program.kt.bc -o ./tmp/result.o

Slide 71

Slide 71 text

Linker Phase TemporaryσΟϨΫτϦʹ Object File͕ੜ੒͞ΕΔ

Slide 72

Slide 72 text

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \ -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 \ -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/ SDKs/MacOSX10.14.sdk \ -o /Users/youta1119/Sandbox/program.kexe \ /Users/youta1119/Sandbox/./tmp/result.o -S \ -lSystem -lc++ -lobjc -framework Foundation /Applications/Xcode.app/Contents/Developer/ Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a / Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/ 10.0.1/lib/darwin/libclang_rt.profile_osx.a -alias _Konan_main _main -rpath @executable_path/../Frameworks

Slide 73

Slide 73 text

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \ -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 \ -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/ SDKs/MacOSX10.14.sdk \ -o /Users/youta1119/Sandbox/program.kexe \ /Users/youta1119/Sandbox/./tmp/result.o -S \ -lSystem -lc++ -lobjc -framework Foundation /Applications/Xcode.app/Contents/Developer/ Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a / Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/ 10.0.1/lib/darwin/libclang_rt.profile_osx.a -alias _Konan_main _main -rpath @executable_path/../Frameworks

Slide 74

Slide 74 text

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \ -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 \ -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/ SDKs/MacOSX10.14.sdk \ -o /Users/youta1119/Sandbox/program.kexe \ /Users/youta1119/Sandbox/./tmp/result.o -S \ -lSystem -lc++ -lobjc -framework Foundation /Applications/Xcode.app/Contents/Developer/ Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a / Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/ 10.0.1/lib/darwin/libclang_rt.profile_osx.a -alias _Konan_main _main -rpath @executable_path/../Frameworks

Slide 75

Slide 75 text

Linker Phase όΠφϦͰ͖ͨ!