From 9642eeec0acd70c001d7e63fe41b0cee2feb794d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 17 Jun 2019 15:16:44 +0300 Subject: [PATCH] C: support passing strings as char16_t*, fix bugs under msvc --- .../java/lang/MathNativeGenerator.java | 6 ++- .../org/teavm/classlib/java/lang/TMath.java | 12 +++++- .../backend/c/generate/ClassGenerator.java | 2 +- .../c/generate/CodeGenerationVisitor.java | 14 ++++++- .../backend/c/generate/CodeGeneratorUtil.java | 8 +++- .../backend/c/intrinsic/StringsIntrinsic.java | 12 ++++++ .../resources/org/teavm/backend/c/runtime.c | 38 +++++++++++++++++++ .../resources/org/teavm/backend/c/runtime.h | 2 + .../main/java/org/teavm/interop/Strings.java | 4 ++ .../main/java/org/teavm/interop/c/Char16.java | 26 +++++++++++++ 10 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 interop/core/src/main/java/org/teavm/interop/c/Char16.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java index 89452848d..90a77e5bc 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java @@ -24,7 +24,11 @@ import org.teavm.model.MethodReference; public class MathNativeGenerator implements Generator { @Override public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { - function(context, writer, "Math." + methodRef.getName(), methodRef.parameterCount()); + String name = methodRef.getName(); + if (name.equals("randomImpl")) { + name = "random"; + } + function(context, writer, "Math." + name, methodRef.parameterCount()); } private void function(GeneratorContext context, SourceWriter writer, String name, int paramCount) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index 66f813263..13512312f 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.lang; import org.teavm.backend.javascript.spi.GeneratedBy; +import org.teavm.classlib.PlatformDetector; import org.teavm.interop.Import; import org.teavm.interop.NoSideEffects; import org.teavm.interop.Unmanaged; @@ -126,10 +127,17 @@ public final class TMath extends TObject { return (long) (a + signum(a) * 0.5); } + @Unmanaged + public static double random() { + return PlatformDetector.isC() ? randomC() : randomImpl(); + } + + @Import(name = "teavm_rand") + private static native double randomC(); + @GeneratedBy(MathNativeGenerator.class) @Import(module = "teavmMath", name = "random") - @Unmanaged - public static native double random(); + private static native double randomImpl(); public static int min(int a, int b) { return a < b ? a : b; diff --git a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java index 07cf48b69..bd72ddc13 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java @@ -244,7 +244,7 @@ public class ClassGenerator { } private void generateStringPool(ValueType type) { - if (!context.isIncremental()) { + if (!context.isIncremental() || context.getStringPool().getStrings().isEmpty()) { return; } diff --git a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java index 72943125d..9af0209af 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java @@ -70,7 +70,9 @@ import org.teavm.backend.c.intrinsic.IntrinsicContext; import org.teavm.backend.c.util.InteropUtil; import org.teavm.diagnostics.Diagnostics; import org.teavm.interop.Address; +import org.teavm.interop.c.Char16; import org.teavm.interop.c.Variable; +import org.teavm.model.AnnotationContainerReader; import org.teavm.model.CallLocation; import org.teavm.model.ClassReader; import org.teavm.model.ClassReaderSource; @@ -558,8 +560,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { temporaries.add(allocTemporaryVariable(parameterTypeForCall(method, i))); } boolean stringResult = method.getResultType().isObject(String.class); + boolean string16Result = method.getAnnotations().get(Char16.class.getName()) != null; writer.print("("); + AnnotationContainerReader[] parameterAnnotations = method.getParameterAnnotations(); + for (int i = 0; i < arguments.size(); ++i) { String tmp = temporaries.get(i); writer.print(tmp + " = "); @@ -567,7 +572,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { ? method.parameterType(i) : i == 0 ? ValueType.object(method.getOwnerName()) : method.parameterType(i - 1); if (type.isObject(String.class)) { - writer.print("teavm_stringToC("); + int annotIndex = method.hasModifier(ElementModifier.STATIC) ? i : i - 1; + boolean is16Char = annotIndex >= 0 + && parameterAnnotations[annotIndex].get(Char16.class.getName()) != null; + String functionName = is16Char ? "teavm_stringToC16" : "teavm_stringToC"; + writer.print(functionName).print("("); arguments.get(i).acceptVisitor(this); writer.print(")"); stringTemporaries.add(tmp); @@ -616,7 +625,8 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { if (resultTmp != null) { writer.print(", "); if (stringResult) { - writer.print("teavm_cToString("); + String functionName = string16Result ? "teavm_c16ToString" : "teavm_cToString"; + writer.print(functionName).print("("); } writer.print(resultTmp); if (stringResult) { diff --git a/core/src/main/java/org/teavm/backend/c/generate/CodeGeneratorUtil.java b/core/src/main/java/org/teavm/backend/c/generate/CodeGeneratorUtil.java index 197dcff56..b80619204 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/CodeGeneratorUtil.java +++ b/core/src/main/java/org/teavm/backend/c/generate/CodeGeneratorUtil.java @@ -70,8 +70,10 @@ public final class CodeGeneratorUtil { writer.print("INFINITY"); } else if (Float.isNaN(f)) { writer.print("NAN"); - } else { + } else if ((int) f == f) { writer.print(f + "f"); + } else { + writer.print(Float.toHexString(f) + "f"); } } else if (value instanceof Double) { double d = (Double) value; @@ -82,8 +84,10 @@ public final class CodeGeneratorUtil { writer.print("INFINITY"); } else if (Double.isNaN(d)) { writer.print("NAN"); - } else { + } else if ((int) d == d) { writer.print(value.toString()); + } else { + writer.print(Double.toHexString(d)); } } else if (value instanceof Boolean) { writer.print((Boolean) value ? "1" : "0"); diff --git a/core/src/main/java/org/teavm/backend/c/intrinsic/StringsIntrinsic.java b/core/src/main/java/org/teavm/backend/c/intrinsic/StringsIntrinsic.java index 1801722e7..5193b0e05 100644 --- a/core/src/main/java/org/teavm/backend/c/intrinsic/StringsIntrinsic.java +++ b/core/src/main/java/org/teavm/backend/c/intrinsic/StringsIntrinsic.java @@ -41,6 +41,18 @@ public class StringsIntrinsic implements Intrinsic { context.emit(invocation.getArguments().get(0)); context.writer().print(")"); break; + case "toC16": { + Expr arg = invocation.getArguments().get(0); + context.writer().print("teavm_stringToC16("); + context.emit(arg); + context.writer().print(")"); + break; + } + case "fromC16": + context.writer().print("teavm_c16ToString("); + context.emit(invocation.getArguments().get(0)); + context.writer().print(")"); + break; } } } diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.c b/core/src/main/resources/org/teavm/backend/c/runtime.c index 76932da8b..8b5b35379 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.c +++ b/core/src/main/resources/org/teavm/backend/c/runtime.c @@ -192,6 +192,14 @@ int64_t teavm_currentTimeMillis() { uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32); return (int64_t) ((current - teavm_unixTimeOffset) / 10000); } +int64_t teavm_currentTimeNano() { + SYSTEMTIME time; + FILETIME fileTime; + GetSystemTime(&time); + SystemTimeToFileTime(&time, &fileTime); + uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32); + return (int64_t) ((current - teavm_unixTimeOffset) / 10); +} #endif int32_t teavm_timeZoneOffset() { @@ -353,6 +361,21 @@ char* teavm_stringToC(void* obj) { return result; } +char16_t* teavm_stringToC16(void* obj) { + if (obj == NULL) { + return NULL; + } + + TeaVM_String* javaString = (TeaVM_String*) obj; + TeaVM_Array* charArray = javaString->characters; + char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t); + size_t sz = charArray->size; + char16_t* result = malloc((sz + 1) * sizeof(char16_t)); + memcpy(result, javaChars, sz * sizeof(char16_t)); + result[sz] = 0; + return result; +} + TeaVM_String* teavm_cToString(char* cstring) { if (cstring == NULL) { return NULL; @@ -375,6 +398,21 @@ TeaVM_String* teavm_cToString(char* cstring) { return teavm_createString(charArray); } +TeaVM_String* teavm_c16ToString(char16_t* cstring) { + if (cstring == NULL) { + return NULL; + } + + int32_t size = 0; + while (cstring[size] == 0) { + ++size; + } + TeaVM_Array* charArray = teavm_allocateCharArray(size); + char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t); + memcpy(javaChars, cstring, size * sizeof(char16_t)); + return teavm_createString(charArray); +} + char16_t* teavm_mbToChar16(char* cstring, int32_t* length) { size_t clen = strlen(cstring); int32_t size = teavm_c16Size(cstring, clen); diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.h b/core/src/main/resources/org/teavm/backend/c/runtime.h index b4098b6a0..c249a8f03 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.h +++ b/core/src/main/resources/org/teavm/backend/c/runtime.h @@ -207,6 +207,8 @@ extern int32_t teavm_timeZoneOffset(); extern char* teavm_stringToC(void*); extern TeaVM_String* teavm_cToString(char*); +extern char16_t* teavm_stringToC16(void*); +extern TeaVM_String* teavm_c16ToString(char16_t*); extern char16_t* teavm_mbToChar16(char*, int32_t*); extern char* teavm_char16ToMb(char16_t*, int32_t); static inline void teavm_free(void* s) { diff --git a/interop/core/src/main/java/org/teavm/interop/Strings.java b/interop/core/src/main/java/org/teavm/interop/Strings.java index 2831c2d1f..e4a6262a9 100644 --- a/interop/core/src/main/java/org/teavm/interop/Strings.java +++ b/interop/core/src/main/java/org/teavm/interop/Strings.java @@ -22,4 +22,8 @@ public final class Strings { public static native Address toC(String javaString); public static native String fromC(Address cString); + + public static native Address toC16(String javaString); + + public static native String fromC16(Address cString); } diff --git a/interop/core/src/main/java/org/teavm/interop/c/Char16.java b/interop/core/src/main/java/org/teavm/interop/c/Char16.java new file mode 100644 index 000000000..e0df53747 --- /dev/null +++ b/interop/core/src/main/java/org/teavm/interop/c/Char16.java @@ -0,0 +1,26 @@ +/* + * Copyright 2019 konsoletyper. + * + * 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 org.teavm.interop.c; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER, ElementType.METHOD}) +public @interface Char16 { +}