C backend: bugfixes

This commit is contained in:
Alexey Andreev 2019-04-22 19:03:37 +03:00
parent 2a6ca2d0d8
commit cbc8d3f638
5 changed files with 77 additions and 9 deletions

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.util;
import java.util.TimeZone;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.java.lang.TComparable;
import org.teavm.classlib.java.lang.TSystem;
import org.teavm.jso.core.JSDate;
@ -184,8 +185,12 @@ public class TDate implements TComparable<TDate> {
@Override
public String toString() {
if (PlatformDetector.isLowLevel()) {
return "";
} else {
return JSDate.create(value).stringValue();
}
}
@Deprecated
public String toLocaleString() {

View File

@ -15,6 +15,7 @@
*/
package org.teavm.classlib.java.util.logging;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.java.lang.TInteger;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.lang.TThrowable;
@ -58,6 +59,12 @@ public class TLogger {
public void log(TLogRecord record) {
String message = format(record.getMessage(), record.getParameters());
if (PlatformDetector.isLowLevel()) {
System.out.print("[");
System.out.print(record.getLevel().getName());
System.out.print("] ");
System.out.println(message);
} else {
if (record.getLevel().intValue() >= TLevel.SEVERE.intValue()) {
error(message);
} else if (record.getLevel().intValue() >= TLevel.WARNING.intValue()) {
@ -66,6 +73,7 @@ public class TLogger {
infoImpl(message);
}
}
}
private String format(String message, Object[] params) {
if (params == null) {

View File

@ -63,6 +63,7 @@ import org.teavm.backend.c.intrinsic.PlatformIntrinsic;
import org.teavm.backend.c.intrinsic.PlatformObjectIntrinsic;
import org.teavm.backend.c.intrinsic.RuntimeClassIntrinsic;
import org.teavm.backend.c.intrinsic.ShadowStackIntrinsic;
import org.teavm.backend.c.intrinsic.StringsIntrinsic;
import org.teavm.backend.c.intrinsic.StructureIntrinsic;
import org.teavm.backend.lowlevel.dependency.ExceptionHandlingDependencyListener;
import org.teavm.backend.lowlevel.transform.CoroutineTransformation;
@ -195,6 +196,9 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
void.class)).use();
dependencyAnalyzer.linkMethod(new MethodReference(ExceptionHandling.class, "throwNullPointerException",
void.class)).use();
dependencyAnalyzer.linkMethod(new MethodReference(NullPointerException.class, "<init>", void.class))
.propagate(0, NullPointerException.class.getName())
.use();
dependencyAnalyzer.linkMethod(new MethodReference(ExceptionHandling.class, "catchException",
Throwable.class)).use();
@ -287,6 +291,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
intrinsics.add(new FiberIntrinsic());
intrinsics.add(new LongIntrinsic());
intrinsics.add(new IntegerIntrinsic());
intrinsics.add(new StringsIntrinsic());
List<Generator> generators = new ArrayList<>();
generators.add(new ArrayGenerator());

View File

@ -15,8 +15,17 @@
*/
package org.teavm.backend.c.generate;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.teavm.ast.ArrayType;
import org.teavm.ast.AssignmentStatement;
@ -97,6 +106,8 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
private static final MethodReference MONITOR_EXIT_SYNC = new MethodReference(Object.class, "monitorExitSync",
Object.class, void.class);
private static final Map<String, String> BUFFER_TYPES = new HashMap<>();
private GenerationContext context;
private NameProvider names;
private CodeWriter writer;
@ -107,6 +118,16 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
private boolean end;
private boolean async;
static {
BUFFER_TYPES.put(ByteBuffer.class.getName(), "int8_t");
BUFFER_TYPES.put(ShortBuffer.class.getName(), "int16_t");
BUFFER_TYPES.put(CharBuffer.class.getName(), "char16_t");
BUFFER_TYPES.put(IntBuffer.class.getName(), "int32_t");
BUFFER_TYPES.put(LongBuffer.class.getName(), "int64_t");
BUFFER_TYPES.put(FloatBuffer.class.getName(), "float");
BUFFER_TYPES.put(DoubleBuffer.class.getName(), "double");
}
public CodeGenerationVisitor(GenerationContext context, CodeWriter writer, Set<? super String> includes) {
this.context = context;
this.writer = writer;
@ -469,7 +490,7 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
}
for (int i = 0; i < expr.getArguments().size(); ++i) {
temporaries.add(allocTemporaryVariable(CVariableType.PTR));
temporaries.add(allocTemporaryVariable(parameterTypeForCall(method, i)));
}
boolean stringResult = method.getResultType().isObject(String.class);
@ -489,6 +510,13 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
writer.print("ARRAY_DATA(");
expr.getArguments().get(i).acceptVisitor(this);
writer.print(", ").printStrictType(((ValueType.Array) type).getItemType()).print(")");
} else if (isPrimitiveBuffer(type)) {
writer.print("ARRAY_DATA(FIELD(");
String typeName = ((ValueType.Object) type).getClassName();
expr.getArguments().get(i).acceptVisitor(this);
writer.print(", ").print(names.forClass(typeName)).print(", ")
.print(names.forMemberField(new FieldReference(typeName, "array"))).print(")");
writer.print(", ").print(BUFFER_TYPES.get(typeName)).print(")");
} else {
expr.getArguments().get(i).acceptVisitor(this);
}
@ -507,7 +535,7 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
writer.print(", ");
}
writer.print(temporaries.get(i));
freeTemporaryVariable(CVariableType.PTR);
freeTemporaryVariable(parameterTypeForCall(method, i));
}
writer.print(")");
} else if (method.parameterCount() > 0 || method.getResultType() == ValueType.VOID) {
@ -534,6 +562,14 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
writer.print(")");
}
private CVariableType parameterTypeForCall(MethodReader method, int index) {
if (method.hasModifier(ElementModifier.STATIC)) {
return typeToCType(method.parameterType(index));
} else {
return index == 0 ? CVariableType.PTR : typeToCType(method.parameterType(index - 1));
}
}
private static boolean isPrimitiveArray(ValueType type) {
if (!(type instanceof ValueType.Array)) {
return false;
@ -542,14 +578,27 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
return ((ValueType.Array) type).getItemType() instanceof ValueType.Primitive;
}
private static boolean isPrimitiveBuffer(ValueType type) {
if (!(type instanceof ValueType.Object)) {
return false;
}
return BUFFER_TYPES.containsKey(((ValueType.Object) type).getClassName());
}
private boolean isWrappedNativeCall(MethodReader method) {
if (!method.hasModifier(ElementModifier.NATIVE)) {
return false;
}
for (ValueType type : method.getParameterTypes()) {
if (type.isObject(String.class)) {
if (method.getAnnotations().get(Variable.class.getName()) != null) {
return true;
}
for (ValueType type : method.getParameterTypes()) {
if (type.isObject(String.class) || isPrimitiveArray(type) || isPrimitiveBuffer(type)) {
return true;
}
}
if (method.getResultType().isObject(String.class)) {
return true;
}
return false;
}

View File

@ -305,6 +305,7 @@ static int32_t teavm_timeZoneOffset() {
}
static char* teavm_stringToC(void*);
static JavaString* teavm_cToString(char*);
static inline void teavm_free(void*);
static inline int64_t teavm_reinterpretDoubleToLong(double v) {