From 899725b6bdc5cf16d1121c72a1e8d8dc84cfe6aa Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 4 May 2018 19:05:11 +0300 Subject: [PATCH] C backend: fix bug in type inference, fix code generation for small int and long values --- .../backend/c/generate/CodeGeneratorUtil.java | 32 ++++++++++++------- .../org/teavm/model/util/TypeInferer.java | 8 ++--- 2 files changed, 22 insertions(+), 18 deletions(-) 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 8054c70a2..95f1f945f 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 @@ -30,22 +30,30 @@ public final class CodeGeneratorUtil { } else if (value instanceof Integer) { int i = (Integer) value; long v = i; - if (i < 0) { - writer.print("-"); - v = -i; + if (i == Integer.MIN_VALUE) { + writer.print("(int32_t) INT32_C(0x80000000)"); + } else { + if (i < 0) { + writer.print("-"); + v = -v; + } + writer.print("INT32_C("); + writeLongConstant(writer, v); + writer.print(")"); } - writer.print("INT32_C("); - writeLongConstant(writer, v); - writer.print(")"); } else if (value instanceof Long) { long v = (Long) value; - if (v < 0) { - writer.print("-"); - v = -v; + if (v == Long.MIN_VALUE) { + writer.print("(int64_t) INT64_C(0x8000000000000000)"); + } else { + if (v < 0) { + writer.print("-"); + v = -v; + } + writer.print("INT64_C("); + writeLongConstant(writer, v); + writer.print(")"); } - writer.print("INT64_C("); - writeLongConstant(writer, v); - writer.print(")"); } else if (value instanceof Float) { float f = (Float) value; if (Float.isInfinite(f)) { diff --git a/core/src/main/java/org/teavm/model/util/TypeInferer.java b/core/src/main/java/org/teavm/model/util/TypeInferer.java index afe8ba713..60b7bb2a6 100644 --- a/core/src/main/java/org/teavm/model/util/TypeInferer.java +++ b/core/src/main/java/org/teavm/model/util/TypeInferer.java @@ -90,7 +90,8 @@ public class TypeInferer { } public VariableType typeOf(int variableIndex) { - return types[variableIndex]; + VariableType result = types[variableIndex]; + return result != null ? result : VariableType.OBJECT; } VariableType convert(ValueType type) { @@ -183,11 +184,6 @@ public class TypeInferer { types[receiver.getIndex()] = VariableType.OBJECT; } - @Override - public void nullConstant(VariableReader receiver) { - types[receiver.getIndex()] = VariableType.OBJECT; - } - @Override public void nullCheck(VariableReader receiver, VariableReader value) { builder.addEdge(value.getIndex(), receiver.getIndex());