Skip to content

Commit 2198432

Browse files
committed
More doc
1 parent 9ebf63c commit 2198432

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ buildscript {
1818
}
1919

2020
group 'com.github.koresframework'
21-
version '4.1.10.bytecode.3'
21+
version '4.1.10.bytecode.4'
2222

2323
apply from: project(":Kores").file("gradle/common.gradle")
2424

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Kores-BytecodeWriter - Translates Kores Structure to JVM Bytecode <https://github.com/JonathanxD/CodeAPI-BytecodeWriter>
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/) <jonathan.scripter@programmer.net>
7+
* Copyright (c) contributors
8+
*
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
package com.github.jonathanxd.kores.bytecode.doc
29+
30+
import java.lang.invoke.LambdaMetafactory
31+
import java.lang.invoke.CallSite
32+
import java.lang.invoke.StringConcatFactory
33+
34+
/**
35+
* Born by the [Da Vinci Machine Project](https://openjdk.java.net/projects/mlvm/) with the
36+
* [JSR 292](https://jcp.org/en/jsr/detail?id=292) specification, `invokedynamic` is a special kind of invocation
37+
* that uses a **bootstrap method** to link to **static methods**, improving JVM performance and support to
38+
* languages other than Java, mainly dynamic ones. And Java 11 brought improvements with the
39+
* [JEP 309](https://openjdk.java.net/jeps/309), that introduced **dynamic constants** which are resolved using a **bootstrap method**.
40+
*
41+
* `invokedynamic` is a kind of invocation that uses a **bootstrap method** to resolve the target method to link to,
42+
* once linked, all subsequent invocations are **directly** dispatched to the target method without calling the **boostrap
43+
* method** again. This allows dynamic method resolution without sacrificing JIT optimizations as well as other optimizations,
44+
* like method inlining.
45+
*
46+
* It is very likely to **lazy evaluation**, with the biggest advantage as being candidate to all optimizations that
47+
* would not be possible with runtime method resolution, like Groovy and other dynamic and static JVM languages used to do.
48+
*
49+
* Even though `invokedynamic` mainly purpose was to have a more language-agnostic JVM, as JVM has been a very mature
50+
* inviting ecosystem to write your own language or runtime of other languages like Python (with [Jython](https://github.com/jython/jython)),
51+
* Java Language itself is taking advantage of this opcode for various features. For example, **Lambda** and **Method references**
52+
* used to be implemented using **Anonymous Abstract Classes** in its early stage, before the integration of `invokedynamic`
53+
* into the JVM, after the implementation of the specification, Lambda and Method references started using the instruction
54+
* to generate the implementation at runtime and to link to the target method (through [LambdaMetafactory]),
55+
* performance-wise, the average is very close to anonymous classes performance.
56+
* Also, Java started to use the instruction to generate [string concatenation][IndyConcatLogic] through [StringConcatFactory],
57+
* which is more
58+
* easy to detect and optimize and to generate record `toString`, `equals` and `hashCode` methods implementation,
59+
* using the [ObjectMethods](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/runtime/ObjectMethods.html)
60+
* ([ref][java.lang.runtime.ObjectMethods]).
61+
*
62+
* ## How does Kores treats `invokedynamic`
63+
*
64+
* Kores uses `invokedynamic` to generate lambdas, method references and string concatenations (Kores currently does not support
65+
* record types), custom InvokeDynamic instructions are supported and are generated following the JVM Specification.
66+
*
67+
* InvokeDynamic is a very powerful tool for both programming languages and bytecode generation libraries, as it allows functions to be
68+
* resolved at runtime without sacrificing de average performance (first invocation is slower because of the initial resolution).
69+
*
70+
* ## How does `invokedynamic` works?
71+
*
72+
* When the JVM finds an `invokedynamic` instruction, it resolves the **bootstrap method** (which is statically resolved)
73+
* and invokes the resolved bootstrap method with additional information as well as with the boostrap arguments constants,
74+
* then the **bootstrap method** resolves the dynamic method and returns a [CallSite] that is linked to a method to invoke.
75+
* This [CallSite] holds the resolved method with its static information (localization, name, parameter and return types)
76+
* and other additional information that specifies how to invoke the method, like how to take the arguments and the instance
77+
* to used to invoke the method (if the method is not static nor a constructor). The [CallSite] object is linked to the instruction for future invocations.
78+
* After all resolution steps, the method itself is invoked with the arguments. From this moment, all invocations will simply skip the **bootstrap
79+
* resolution step** and invoke the method directly, and, from now, JIT sees this `invokedynamic` opcode just as a regular method
80+
* invocation, which corresponds to the resolved method invocation.
81+
*
82+
* **bootstrap method** does not have access to arguments that are passed to the resolved method, this is because **bootstrap methods**
83+
* behavior must never change based on arguments, the bootstrap is called once for the `invokedynamic` instruction, having
84+
* access to arguments will give the method access only to arguments provided at the resolution time, but not for subsequent invocations.
85+
*
86+
* @since 4.0.5.bytecode.5
87+
*/
88+
@Doc
89+
object IndyGenerationLogic

0 commit comments

Comments
 (0)