Skip to content

Commit

Permalink
#68 Support for Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkosertic committed Nov 28, 2018
1 parent eddc000 commit 7f4439a
Show file tree
Hide file tree
Showing 15 changed files with 869 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 Mirko Sertic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.mirkosertic.bytecoder.classlib.java.lang;

import de.mirkosertic.bytecoder.api.SubstitutesInClass;

@SubstitutesInClass(completeReplace = true)
public class TThread implements Runnable {

private static final TThread MAIN = new TThread(null);

public static TThread currentThread() {
return MAIN;
}

private final Runnable runnable;

public TThread(final Runnable aRunable) {
runnable = aRunable;
}

@Override
public void run() {
runnable.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,38 @@
@SubstitutesInClass(completeReplace = true)
public class TArrays {

public static void sort(Object[] aData, int aStart, int aEnd) {
public static void sort(final Object[] aData, final int aStart, final int aEnd) {
}

public static <T> List<T> asList(T... aValues) {
ArrayList<T> theResult = new ArrayList<>();
for (T theValue : aValues) {
public static <T> List<T> asList(final T... aValues) {
final ArrayList<T> theResult = new ArrayList<>();
for (final T theValue : aValues) {
theResult.add(theValue);
}
return theResult;
}

public static <T> T[] copyOf(T[] original, int newLength) {
public static <T> T[] copyOf(final T[] original, final int newLength) {
return copyOf(original, newLength, null);
}

public static <T> T[] copyOf(T[] original, int newLength, Class aType) {
public static <T> T[] copyOf(final T[] original, final int newLength, final Class aType) {
int theMax = original.length;
if (newLength < theMax) {
theMax = newLength;
}
T[] theResult = (T[]) new Object[newLength];
final T[] theResult = (T[]) new Object[newLength];
for (int i=0;i<theMax;i++) {
theResult[i] = original[i];
}
return theResult;
}

public static <T> T[] copyOfRange(final T[] original, final int from, final int to, final Class aType) {
final T[] theResult = (T[]) new Object[to - from];
for (int i=from;i<to;i++) {
theResult[i-from] = original[i];
}
return theResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ public class CompileOptions {
private final boolean debugOutput;
private final Optimizer optimizer;
private final boolean enableExceptions;
private final String filenamePrefix;

public CompileOptions(final Logger aLogger, final boolean aDebugOutput, final Optimizer aOptimizer, final boolean aEnableExceptions) {
public CompileOptions(final Logger aLogger, final boolean aDebugOutput, final Optimizer aOptimizer, final boolean aEnableExceptions,
final String aFilenamePrefix) {
logger = aLogger;
debugOutput = aDebugOutput;
optimizer = aOptimizer;
this.enableExceptions = aEnableExceptions;
enableExceptions = aEnableExceptions;
filenamePrefix = aFilenamePrefix;
}

public Logger getLogger() {
Expand All @@ -47,4 +50,8 @@ public Optimizer getOptimizer() {
public boolean isEnableExceptions() {
return enableExceptions;
}

public String getFilenamePrefix() {
return filenamePrefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,6 @@ public JSCompileResult generateCodeFor(final CompileOptions aOptions, final Byte

theWriter.flush();

return new JSCompileResult(new JSCompileResult.JSContent("bytecoder.js", theStrWriter.toString()));
return new JSCompileResult(new JSCompileResult.JSContent(aOptions.getFilenamePrefix() + ".js", theStrWriter.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void updateFromBuffer() {
platform = aPlatform;
cachedKernels = new HashMap<>();
backend = new OpenCLCompileBackend();
compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true);
compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true, "opencl");

final cl_context_properties contextProperties = new cl_context_properties();
contextProperties.addProperty(CL_CONTEXT_PLATFORM, aPlatform.selectedPlatform.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ public int getVTableIndexOf(final BytecodeObjectTypeRef aObjectType) {
public static class WASMTextualCompileResult extends WASMCompileContent {

private final String data;
private final String filenamePrefix;

public WASMTextualCompileResult(final WASMMemoryLayouter memoryLayouter, final BytecodeLinkerContext linkerContext,
final List<String> generatedFunctions, final String data) {
final List<String> generatedFunctions, final String data, final String afilenamePrefix) {
super(memoryLayouter, linkerContext, generatedFunctions);
this.data = data;
this.filenamePrefix = afilenamePrefix;
}

@Override
public String getFileName() {
return "bytecoder.wat";
return filenamePrefix + ".wat";
}

@Override
Expand All @@ -82,16 +84,18 @@ public void writeTo(final OutputStream stream) {
public static class WASMBinaryCompileResult extends WASMCompileContent {

private final byte[] data;
private final String filenamePrefix;

public WASMBinaryCompileResult(final WASMMemoryLayouter memoryLayouter, final BytecodeLinkerContext linkerContext,
final List<String> generatedFunctions, final byte[] data) {
final List<String> generatedFunctions, final byte[] data, final String filenamePrefix) {
super(memoryLayouter, linkerContext, generatedFunctions);
this.data = data;
this.filenamePrefix = filenamePrefix;
}

@Override
public String getFileName() {
return "bytecoder.wasm";
return filenamePrefix + ".wasm";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ public Function resolveCallsiteBootstrapFor(final BytecodeClass owningClass, fin
}

return new WASMCompileResult(
new WASMCompileResult.WASMTextualCompileResult(theMemoryLayout, aLinkerContext, new ArrayList<>(), theStringWriter.toString()),
new WASMCompileResult.WASMBinaryCompileResult(theMemoryLayout, aLinkerContext, new ArrayList<>(), bos.toByteArray()));
new WASMCompileResult.WASMTextualCompileResult(theMemoryLayout, aLinkerContext, new ArrayList<>(), theStringWriter.toString(), aOptions.getFilenamePrefix()),
new WASMCompileResult.WASMBinaryCompileResult(theMemoryLayout, aLinkerContext, new ArrayList<>(), bos.toByteArray(), aOptions.getFilenamePrefix()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

public class BytecodeInstructionINVOKESTATIC extends BytecodeInstructionGenericInvoke {

public BytecodeInstructionINVOKESTATIC(BytecodeOpcodeAddress aOpcodeIndex, int aIndex, BytecodeConstantPool aConstantPool) {
public BytecodeInstructionINVOKESTATIC(final BytecodeOpcodeAddress aOpcodeIndex, final int aIndex, final BytecodeConstantPool aConstantPool) {
super(aOpcodeIndex, aIndex, aConstantPool);
}

@Override
public void performLinking(BytecodeClass aOwningClass, BytecodeLinkerContext aLinkerContext) {
BytecodeMethodRefConstant theMethodRefConstant = getMethodReference();
BytecodeClassinfoConstant theClassConstant = theMethodRefConstant.getClassIndex().getClassConstant();
BytecodeNameAndTypeConstant theMethodRef = theMethodRefConstant.getNameAndTypeIndex().getNameAndType();
public void performLinking(final BytecodeClass aOwningClass, final BytecodeLinkerContext aLinkerContext) {
final BytecodeMethodRefConstant theMethodRefConstant = getMethodReference();
final BytecodeClassinfoConstant theClassConstant = theMethodRefConstant.getClassIndex().getClassConstant();
final BytecodeNameAndTypeConstant theMethodRef = theMethodRefConstant.getNameAndTypeIndex().getNameAndType();

BytecodeMethodSignature theSig = theMethodRef.getDescriptorIndex().methodSignature();
BytecodeUtf8Constant theName = theMethodRef.getNameIndex().getName();
final BytecodeMethodSignature theSig = theMethodRef.getDescriptorIndex().methodSignature();
final BytecodeUtf8Constant theName = theMethodRef.getNameIndex().getName();

if (!aLinkerContext.resolveClass(BytecodeObjectTypeRef.fromUtf8Constant(theClassConstant.getConstant()))
.resolveStaticMethod(theName.stringValue(), theSig)) {
throw new IllegalStateException("Cannot find static method " + theName.stringValue() + " in " + theClassConstant.getConstant().stringValue());
throw new IllegalStateException("Cannot find static method " + theName.stringValue() + " in " + theClassConstant.getConstant().stringValue() + " wizh signature " +theSig.toString());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private void testJSBackendFrameworkMethod(final FrameworkMethod aFrameworkMethod
final StringWriter theStrWriter = new StringWriter();
final PrintWriter theCodeWriter = new PrintWriter(theStrWriter);

final CompileOptions theOptions = new CompileOptions(LOGGER, true, KnownOptimizer.ALL, true);
final CompileOptions theOptions = new CompileOptions(LOGGER, true, KnownOptimizer.ALL, true, "bytecoder");
final JSCompileResult result = (JSCompileResult) theCompileTarget.compileToJS(theOptions, testClass.getJavaClass(), aFrameworkMethod.getName(), theSignature);
final JSCompileResult.JSContent content = result.getContent()[0];

Expand Down Expand Up @@ -349,7 +349,7 @@ private void testWASMASTBackendFrameworkMethod(final FrameworkMethod aFrameworkM
final BytecodeMethodSignature theSignature = theCompileTarget.toMethodSignature(aFrameworkMethod.getMethod());
final BytecodeObjectTypeRef theTypeRef = new BytecodeObjectTypeRef(testClass.getName());

final CompileOptions theOptions = new CompileOptions(LOGGER, true, KnownOptimizer.ALL, false);
final CompileOptions theOptions = new CompileOptions(LOGGER, true, KnownOptimizer.ALL, false, "bytecoder");
final WASMCompileResult theResult = (WASMCompileResult) theCompileTarget.compileToJS(theOptions, testClass.getJavaClass(), aFrameworkMethod.getName(), theSignature);
final WASMCompileResult.WASMCompileContent textualContent = theResult.getContent()[0];
final WASMCompileResult.WASMCompileContent binaryContent = theResult.getContent()[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Kernel createKernel() {
public void testSimpleKernel() throws IOException {

final OpenCLCompileBackend backend = new OpenCLCompileBackend();
final CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true);
final CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true, "opencl");

final Kernel theKernel = createKernel();
final Class theKernelClass = theKernel.getClass();
Expand All @@ -105,7 +105,7 @@ public void testSimpleKernel() throws IOException {
public void testKernelWithComplexType() throws IOException {

final OpenCLCompileBackend backend = new OpenCLCompileBackend();
final CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true);
final CompileOptions compileOptions = new CompileOptions(new Slf4JLogger(), false, KnownOptimizer.ALL, true, "opencl");

final Float2[] theIn = new Float2[10];
final Float2[] theOut = new Float2[10];
Expand Down
90 changes: 90 additions & 0 deletions integrationtest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<name>Bytecoder Integrationtest</name>
<description>Bytecoder Integration Tests</description>

<properties>
<kotlin.version>1.3.10</kotlin.version>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
Expand All @@ -32,10 +36,72 @@
<version>2.2.1.1</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>de.mirkosertic.bytecoder</groupId>
<artifactId>bytecoder-mavenplugin</artifactId>
Expand All @@ -56,6 +122,18 @@
<enableExceptionHandling>true</enableExceptionHandling>
</configuration>
</execution>
<execution>
<id>javascript-kotlin</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<mainClass>de.mirkosertic.bytecoder.integrationtest.JBox2DSimulationKotlin</mainClass>
<backend>js</backend>
<enableExceptionHandling>true</enableExceptionHandling>
<filenamePrefix>bytecoder-kotlin</filenamePrefix>
</configuration>
</execution>
<execution>
<id>wasm</id>
<goals>
Expand All @@ -67,6 +145,18 @@
<enableExceptionHandling>false</enableExceptionHandling>
</configuration>
</execution>
<execution>
<id>wasm-kotlin</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<mainClass>de.mirkosertic.bytecoder.integrationtest.JBox2DSimulationKotlin</mainClass>
<backend>wasm</backend>
<enableExceptionHandling>false</enableExceptionHandling>
<filenamePrefix>bytecoder-kotlin</filenamePrefix>
</configuration>
</execution>
</executions>
</plugin>

Expand Down
Loading

0 comments on commit 7f4439a

Please sign in to comment.