1 ## Code Reflection Examples
  2 
  3 List of examples to learn and play with the Code Reflection API.
  4 
  5 - GitHub repo: [https://github.com/openjdk/babylon](https://github.com/openjdk/babylon)
  6 
  7 ### Learning Code Reflection?
  8 
  9 Here's an ordered list of code example to start learning code reflection and some of its features.
 10 Each example is self-contained, and it can be used within the IDE to explore and understand how code reflection
 11 transforms and executes code.
 12 
 13 1. [`HelloCodeReflection`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/HelloCodeReflection.java): Just start using code reflection and lowering code models.
 14 2. [`MathOptimizer`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java): First code transformations to optimize a math function.
 15 3. [`InlineExample`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/InliningExample.java): Simple example to illustrate the inlining.
 16 4. [`MathOptimizerWithInlining`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizerWithInlining.java): Follow up of the [`MathOptimizer`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/MathOptimizer.java) to inline optimize calls into the code model.
 17 5. [`DialectWithInvoke`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DialectWithInvoke.java): Example of creating a dialect that replaces `Invoke` `Op` with a specific signature with a new `Op`. The dialect is handled as an intrinsic replacement.
 18 6. [`DialectFMAOp`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DialectFMAOp.java): Example of how to extend the code reflection `Op` to create a new dialect. It analysis the code for substitution of Add(Mult) to create a new `FMA` Op.
 19 7. [`DynamicFunctionBuild`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/DynamicFunctionBuild.java): Example of how to create a new function dynamically to compute the inverse of a square root. The code model is built dynamically for a new method and it is evaluated in the `Interpreter`.
 20 8. [`CodeReflectionProcessor`](https://github.com/openjdk/babylon/blob/code-reflection/cr-examples/samples/src/main/java/oracle/code/samples/CodeReflectionProcessor.java): A simple code model-based annotation processor
 21 
 22 ### Resources
 23 
 24 1. [Article] [Code Models](https://openjdk.org/projects/babylon/articles/code-models)
 25 2. [Article] [Emulating C# LINQ in Java using Code Reflection](https://openjdk.org/projects/babylon/articles/linq)
 26 3. [Video] [Project Babylon - Code Reflection @JVMLS 2024](https://www.youtube.com/watch?v=6c0DB2kwF_Q)
 27 4. [Video] [Java and GPUs using Code Reflection @JVMLS 2023](https://www.youtube.com/watch?v=lbKBu3lTftc)
 28 
 29 ### How to build with project?
 30 
 31 #### 1. Build Babylon JDK
 32 
 33 We need to use the JDK build that enables the code reflection API (Babylon).
 34 
 35 ```bash
 36 git clone https://github.com/openjdk/babylon
 37 cd babylon
 38 bash configure --with-boot-jdk=${JAVA_HOME}
 39 ```
 40 
 41 Then, we use the built JDK as `JAVA_HOME`
 42 
 43 ```bash
 44 export JAVA_HOME=/$HOME/repos/babylon/build/macosx-aarch64-server-release/jdk/
 45 export PATH=$JAVA_HOME/bin:$PATH
 46 ```
 47 
 48 #### 2. Build examples
 49 
 50 ```bash
 51 mvn clean package
 52 ```
 53 
 54 #### 3. Run the examples
 55 
 56 
 57 ##### Run HelloCodeReflection
 58 
 59 ```bash
 60 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.HelloCodeReflection
 61 ```
 62 
 63 ##### Run MathOptimizer
 64 
 65 ```bash
 66 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizer
 67 ```
 68 
 69 ##### Run InlineExample
 70 
 71 ```bash
 72 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.InlineExample
 73 ```
 74 
 75 ##### Run MathOptimizerWithInlining
 76 
 77 ```bash
 78 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.MathOptimizerWithInlining
 79 ```
 80 
 81 ##### Run DialectWithInvoke
 82 
 83 ```bash
 84 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DialectWithInvoke
 85 ```
 86 
 87 ##### Run DialectFMAOp
 88 
 89 ```bash
 90 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DialectFMAOp
 91 ```
 92 
 93 ##### Run DynamicFunctionBuild
 94 
 95 ```bash
 96 java --add-modules jdk.incubator.code -cp target/crsamples-1.0-SNAPSHOT.jar oracle.code.samples.DynamicFunctionBuild
 97 ```
 98 
 99 ##### Compile with CodeReflectionProcessor
100 
101 ```bash
102 javac --add-modules jdk.incubator.code --processor-path target/crsamples-1.0-SNAPSHOT.jar -processor oracle.code.samples.CodeReflectionProcessor <.java files to compile>
103 ```