KotlinFest2019

1652e9ff825e902738efa399389878df?s=47 youta ogino
August 24, 2019
6.8k

 KotlinFest2019

1652e9ff825e902738efa399389878df?s=128

youta ogino

August 24, 2019
Tweet

Transcript

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

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

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

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

    • Q&A
  5. ࠷৽൛1.3.50ʹରԠ͍ͯ͠·͢

  6. ୈ1෦ Kotlin/Nativeͱ͸ʁ

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

  8. αϙʔτ͍ͯ͠ΔϓϥοτϑΥʔϜ • 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)
  9. ओͳػೳ • ֤ϓϥοτϑΥʔϜ޲͚ͷόΠφϦੜ੒ • Cͱͷ૬ޓӡ༻(Cinterop) • Objective-CɾSwiftͱͷ૬ޓӡ༻(ObjcInterop)

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

  11. Runtimeͱ͸ʁ

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

  13. ͳͥRuntime͕ඞཁͳͷʁ

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

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

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

  17. ඪ४ϥΠϒϥϦ • kotlin.*͔Β࢝·Δύοέʔδͷ΍ͭ • Kotlin Native༻ʹඪ४ϥΠϒϥϦΛҰ͔Β࣮૷ • جຊతʹC++Ͱ࣮૷ • ࣮૷͸͜ͷล

    https://github.com/JetBrains/kotlin-native/tree/master/runtime/src/main
  18. ඪ४ϥΠϒϥϦͷ࣮૷ ̎ύλʔϯͷ࣮૷ํ๏͕͋Δ 1. KotlinͷΈͰ࣮૷ (e.g ranges, coroutines) 2. جຊతʹKotlinͰ͸ؔ਺ͷఆٛͷΈΛߦ͍ɺ࣮૷͸C++Ͱߦ͏ ʢe.g

    stringܕ, printؔ਺ʣ
  19. package kotlin.io /** Prints the given [message] to the standard

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

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

    output stream. */ @SymbolName("Kotlin_io_Console_print") external public fun print(message: String) Kotlinଆ ίϯύΠϥʹରͯؔ͠਺ͷ࣮ମ͸֎ ෦ʹ͋Δͱڭ͑Δҝͷ΋ͷ ؔ਺ͷ࣮ମͷγϯϘϧ໊ $ ؔ਺໊ Λද͢
  22. 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++ଆ
  23. Runtimeͱ͸ʁ KotlinΛNativeͰಈ͔ͨ͢Ίʹ࠷௿ݶඞཁͳ΋ͷ • ඪ४ϥΠϒϥϦ • ϝϞϦ؅ཧʢGCʣ • ϓϩάϥϜϥϯνϟʔ(ޙͰղઆ)

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

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

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

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

  28. ϓϥοτϑΥʔϜʹΑͬͯKotlinϓϩάϥϜͷىಈํ๏͕ҧ͏ • Mac, Linux, Windowsͩͱ… ࣮ߦͨ͠ͱ͖ • Androidͩͱ… Activity͕࡞ΒΕͨͱ͖ •

    Wasmͩͱ… ϒϥ΢β্ͰJSʹݺ͹Εͨͱ͖
  29. 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); }
  30. 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ؔ਺
  31. 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); }
  32. 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); } ($ͷॳظԽ
  33. 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ͷϓϩάϥϜΛىಈͤ͞Δؔ਺ ͷݺͼग़͠
  34. 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); } ϝϞϦղ์
  35. 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); }
  36. 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); }
  37. ϓϥοτϑΥʔϜϥΠϒϥϦͱ͸ʁ

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

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

  40. // 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
  41. // 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໊Λࢦఆ
  42. // 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 ελϒੜ੒ͷର৅ͷϔομϑΝΠϧ
  43. // 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 ελϒΛੜ੒͢Δͱ͖ʹ࢖͏ίϯύ ΠϥΦϓγϣϯΛࢦఆ
  44. // 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
  45. // in bash $ cinterop -def posix.def QPTJYLMJC͕ੜ੒͞Ε·͢

  46. //posix.kt @CCall("knifunptr_platform_posix2") external fun printf(@CCall.CString arg0: String?, vararg variadicArguments: Any?):

    Int fun putc(arg0: Int, arg1: CValuesRef<FILE>?): Int { memScoped { return kniBridge24(arg0, arg1?.getPointer(memScope).rawValue) } } //(தུ)
  47. ୈ3෦ Kotlin͸ͲͷΑ͏ʹͯ͠ωΠςΟϒόΠφ ϦʹͳΔͷ͔?

  48. Kotlin/Native Compiler

  49. 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
  50. 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 ϑΣʔζ໊
  51. 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 ϑΣʔζͷઆ໌
  52. 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
  53. Frontend Phase

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

  55. Psi2Ir Phase

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

  57. ---------------------------------------------- MODULE_FRAGMENT name:<program> FILE fqName:<root> 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") }
  58. LLVMʹ͍ͭͯ

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

  60. LLVM-IR ม׵ ίʔυੜ੒

  61. LLVM-IR ม׵ ίʔυੜ੒

  62. LLVM-IR ίʔυੜ੒ ม׵

  63. Backend Phase

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

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

  66. Linker Phase

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

  68. /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
  69. /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
  70. /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
  71. Linker Phase TemporaryσΟϨΫτϦʹ Object File͕ੜ੒͞ΕΔ

  72. /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
  73. /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
  74. /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
  75. Linker Phase όΠφϦͰ͖ͨ!