Kotlin primitives source code

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

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)

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

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 ?

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

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

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

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

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

@pdvrieze,
I’ve been studying Kotlin Coroutines, I’ve reached a point of the code, where it appears this Intrinsics:

@SinceKotlin("1.3")
@InlineOnly
@Suppress("UNUSED_PARAMETER", "RedundantSuspendModifier")
public suspend inline fun <T> suspendCoroutineUninterceptedOrReturn(crossinline block: (Continuation<T>) -> Any?): T =
    throw NotImplementedError("Implementation of suspendCoroutineUninterceptedOrReturn is intrinsic")

I would like to take a look at source code, in order to study and understand better…
As far as I understood from the discussion here, this part is implemented on JVM side, right?
Where can I find this source code?

I’ve tried to search on OpenJDK project:

$ find . -name "*.java" | xargs grep "suspendCoroutineUninterceptedOrReturn"

and

find . -name "*.*" | xargs grep "suspendCoroutineUninterceptedOrReturn"

But no success. Probably, I am looking in the wrong place, right?
Can you help me with that?

Thanks.

That isn’t in OpenJDK but in Kotlin Compiler.
Indeed suspendCoroutineUninterceptedOrReturn is very simple, nearly nothing.

If you decompile the simple souce to Java,

suspend fun main(): Unit {
  println(1)
  suspendCoroutineUninterceptedOrReturn { aCont ->
    println(2)
    aCont.resume(42)
    println(3)
  }
  println(4)
}

suspendCoroutineUninterceptedOrReturn means just
Continuation aCont = (Continuation)$continuation;

The Magic is the compiler which rewrite suspend functions to normal functions with Continuations.

1 Like