$30 off During Our Annual Pro Sale. View Details »

KotlinFest2019

youta ogino
August 24, 2019
10k

 KotlinFest2019

youta ogino

August 24, 2019
Tweet

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. ୈ1෦
    Kotlin/Nativeͱ͸ʁ

    View Slide

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

    View Slide

  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)

    View Slide

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

    View Slide

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

    View Slide

  11. Runtimeͱ͸ʁ

    View Slide

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

    View Slide

  13. ͳͥRuntime͕ඞཁͳͷʁ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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ଆ

    View Slide

  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ଆ

    View Slide

  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ଆ
    ίϯύΠϥʹରͯؔ͠਺ͷ࣮ମ͸֎
    ෦ʹ͋Δͱڭ͑Δҝͷ΋ͷ
    ؔ਺ͷ࣮ମͷγϯϘϧ໊ $ؔ਺໊

    Λද͢

    View Slide

  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++ଆ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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);
    }

    View Slide

  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ؔ਺

    View Slide

  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);
    }

    View Slide

  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);
    }
    ($ͷॳظԽ

    View Slide

  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ͷϓϩάϥϜΛىಈͤ͞Δؔ਺
    ͷݺͼग़͠

    View Slide

  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);
    }
    ϝϞϦղ์

    View Slide

  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);
    }

    View Slide

  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);
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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໊Λࢦఆ

    View Slide

  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
    ελϒੜ੒ͷର৅ͷϔομϑΝΠϧ

    View Slide

  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
    ελϒΛੜ੒͢Δͱ͖ʹ࢖͏ίϯύ
    ΠϥΦϓγϣϯΛࢦఆ

    View Slide

  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

    View Slide

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

    View Slide

  46. //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)
    }
    }
    //(தུ)

    View Slide

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

    View Slide

  48. Kotlin/Native
    Compiler

    View Slide

  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

    View Slide

  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
    ϑΣʔζ໊

    View Slide

  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
    ϑΣʔζͷઆ໌

    View Slide

  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

    View Slide

  53. Frontend Phase

    View Slide

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

    View Slide

  55. Psi2Ir Phase

    View Slide

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

    View Slide

  57. ----------------------------------------------
    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")
    }

    View Slide

  58. LLVMʹ͍ͭͯ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  63. Backend Phase

    View Slide

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

    View Slide

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

    View Slide

  66. Linker Phase

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  75. Linker Phase
    όΠφϦͰ͖ͨ!

    View Slide