Kotlin primitives source code


#1

When I click “source code” in documentation of Int I see something like “header file”. Methods are listed, but no code is there. I assume, that it’s so because it’s different for Java and JS implementations. Where can I find actual implementation of Int.rem(), etc.?


#2

For the Jvm target this is actually implemented as a jvm bytecode. To make that work the compiler would need to implement it as an intrinsic. I imagine that it is similarly mapped directly to the corresponding javascript operator. (the actual implementation lives in your processor core)


#3

There isn’t one. Int.rem is translated to the irem bytecode on the JVM and to the % operator on JS.


#4

pdvrieze, thanks for reply. I have read something like this in the past about (about intrinsic). But I would like to clarify: it’s not the same as Java’s intrinsic, right ?


#5

@yole, thanks for the info! It’s exactly what I was looking for. Where can I find such mapping for the other methods/classes? I am checking git repo, but have not found it yet.


#6

For JVM the mapping is here and in other classes in the same package. I’m not familiar with the JS implementation, sorry.


#7

@yole, thanks for the link for JVM. I will try to find JS implementation meanwhile.


#8

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).


#9

@pdvrieze thanks for a such detailed and informative post about Intrinsic and about what’s common between Java’s and Kotlin’s Intrinsics.