Kotlin primitives source code

Intrinsic basically means that the implementation is internal to the compiler. Normally either because the implementation cannot be provided by the library (for example this case where the implementation is a single bytecode), or because having the compiler do it allows for (significant) optimization of the output. For the JVM there are two compilation steps: (Java|Kotlin) → Class files (Java bytecode) and bytecode → machine executable / interpretation. In Kotlin’s case functions like Int.rem are provided to have a consistent library interface even though these functions are language primitives in disguise. This can have some advantages in language design (the library namespace is much less restrictive than keywords) as well as some elegance.

For Java these functions are generally defined as native functions in the standard library source even though they will not actually involve JNI in their invocation. JVM intrinsics are used either for the implementation of the unimplemented methods in the standard library (for example the guts of classloading does not happen at the Java level, but calls into the JVM) or to replace some functions in the standard library with faster implementations than are possible in Java itself (for example taking advantage of machine specifics), An example would be System.arraycopy that can take advantage of reduced range checking (and SSE style instructions) compared to a straightforward loop.

In general intrinsic thus means that it is something that is built in to the “translation” system rather than provided in other ways (library) but is not a specified element of the language syntax (the language itself is always built in to the compiler).

5 Likes