mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
C backend: bug fixes and interop improvements
This commit is contained in:
parent
cfd6b64899
commit
2a6ca2d0d8
|
@ -346,7 +346,9 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
|
||||||
|
|
||||||
for (String className : classNames) {
|
for (String className : classNames) {
|
||||||
ClassHolder cls = classes.get(className);
|
ClassHolder cls = classes.get(className);
|
||||||
classGenerator.generateClass(cls);
|
if (cls != null) {
|
||||||
|
classGenerator.generateClass(cls);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
classGenerator.generateRemainingData(classNames, shadowStackTransformer);
|
classGenerator.generateRemainingData(classNames, shadowStackTransformer);
|
||||||
|
@ -369,8 +371,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
parent = RuntimeObject.class.getName();
|
parent = RuntimeObject.class.getName();
|
||||||
}
|
}
|
||||||
if (!parent.equals(Structure.class.getName())
|
if (!parent.equals(Structure.class.getName()) && stateMap.getOrDefault(parent, (byte) 0) == 0) {
|
||||||
&& stateMap.getOrDefault(cls.getParent(), (byte) 0) == 0) {
|
|
||||||
stack.push(parent);
|
stack.push(parent);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -491,6 +491,9 @@ public class ClassGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateVirtualTableStructure(ClassReader cls) {
|
private void generateVirtualTableStructure(ClassReader cls) {
|
||||||
|
if (cls == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String name = context.getNames().forClassClass(cls.getName());
|
String name = context.getNames().forClassClass(cls.getName());
|
||||||
|
|
||||||
vtableStructuresWriter.print("typedef struct ").print(name).println(" {").indent();
|
vtableStructuresWriter.print("typedef struct ").print(name).println(" {").indent();
|
||||||
|
|
|
@ -60,9 +60,11 @@ import org.teavm.backend.c.intrinsic.IntrinsicContext;
|
||||||
import org.teavm.diagnostics.Diagnostics;
|
import org.teavm.diagnostics.Diagnostics;
|
||||||
import org.teavm.interop.Address;
|
import org.teavm.interop.Address;
|
||||||
import org.teavm.interop.c.Include;
|
import org.teavm.interop.c.Include;
|
||||||
|
import org.teavm.interop.c.Variable;
|
||||||
import org.teavm.model.AnnotationContainerReader;
|
import org.teavm.model.AnnotationContainerReader;
|
||||||
import org.teavm.model.AnnotationReader;
|
import org.teavm.model.AnnotationReader;
|
||||||
import org.teavm.model.AnnotationValue;
|
import org.teavm.model.AnnotationValue;
|
||||||
|
import org.teavm.model.CallLocation;
|
||||||
import org.teavm.model.ClassReader;
|
import org.teavm.model.ClassReader;
|
||||||
import org.teavm.model.ElementModifier;
|
import org.teavm.model.ElementModifier;
|
||||||
import org.teavm.model.FieldReference;
|
import org.teavm.model.FieldReference;
|
||||||
|
@ -469,6 +471,7 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
for (int i = 0; i < expr.getArguments().size(); ++i) {
|
for (int i = 0; i < expr.getArguments().size(); ++i) {
|
||||||
temporaries.add(allocTemporaryVariable(CVariableType.PTR));
|
temporaries.add(allocTemporaryVariable(CVariableType.PTR));
|
||||||
}
|
}
|
||||||
|
boolean stringResult = method.getResultType().isObject(String.class);
|
||||||
|
|
||||||
writer.print("(");
|
writer.print("(");
|
||||||
for (int i = 0; i < expr.getArguments().size(); ++i) {
|
for (int i = 0; i < expr.getArguments().size(); ++i) {
|
||||||
|
@ -482,6 +485,10 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
expr.getArguments().get(i).acceptVisitor(this);
|
expr.getArguments().get(i).acceptVisitor(this);
|
||||||
writer.print(")");
|
writer.print(")");
|
||||||
stringTemporaries.add(tmp);
|
stringTemporaries.add(tmp);
|
||||||
|
} else if (isPrimitiveArray(type)) {
|
||||||
|
writer.print("ARRAY_DATA(");
|
||||||
|
expr.getArguments().get(i).acceptVisitor(this);
|
||||||
|
writer.print(", ").printStrictType(((ValueType.Array) type).getItemType()).print(")");
|
||||||
} else {
|
} else {
|
||||||
expr.getArguments().get(i).acceptVisitor(this);
|
expr.getArguments().get(i).acceptVisitor(this);
|
||||||
}
|
}
|
||||||
|
@ -492,28 +499,49 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
if (resultTmp != null) {
|
if (resultTmp != null) {
|
||||||
writer.print(resultTmp + " = ");
|
writer.print(resultTmp + " = ");
|
||||||
}
|
}
|
||||||
writer.print(names.forMethod(method.getReference())).print("(");
|
writer.print(names.forMethod(method.getReference()));
|
||||||
for (int i = 0; i < temporaries.size(); ++i) {
|
if (method.getAnnotations().get(Variable.class.getName()) == null) {
|
||||||
if (i > 0) {
|
writer.print("(");
|
||||||
writer.print(", ");
|
for (int i = 0; i < temporaries.size(); ++i) {
|
||||||
|
if (i > 0) {
|
||||||
|
writer.print(", ");
|
||||||
|
}
|
||||||
|
writer.print(temporaries.get(i));
|
||||||
|
freeTemporaryVariable(CVariableType.PTR);
|
||||||
}
|
}
|
||||||
writer.print(temporaries.get(i));
|
writer.print(")");
|
||||||
freeTemporaryVariable(CVariableType.PTR);
|
} else if (method.parameterCount() > 0 || method.getResultType() == ValueType.VOID) {
|
||||||
|
context.getDiagnostics().error(new CallLocation(method.getReference()),
|
||||||
|
"'@Variable' annotation is not applicable to method {{m0}}", method.getReference());
|
||||||
}
|
}
|
||||||
writer.print(")");
|
|
||||||
|
|
||||||
for (String tmp : stringTemporaries) {
|
for (String tmp : stringTemporaries) {
|
||||||
writer.print(", teavm_free(" + tmp + ")");
|
writer.print(", teavm_free(" + tmp + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resultTmp != null) {
|
if (resultTmp != null) {
|
||||||
writer.print(", " + resultTmp);
|
writer.print(", ");
|
||||||
|
if (stringResult) {
|
||||||
|
writer.print("teavm_cToString(");
|
||||||
|
}
|
||||||
|
writer.print(resultTmp);
|
||||||
|
if (stringResult) {
|
||||||
|
writer.print(")");
|
||||||
|
}
|
||||||
freeTemporaryVariable(typeToCType(method.getResultType()));
|
freeTemporaryVariable(typeToCType(method.getResultType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.print(")");
|
writer.print(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isPrimitiveArray(ValueType type) {
|
||||||
|
if (!(type instanceof ValueType.Array)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((ValueType.Array) type).getItemType() instanceof ValueType.Primitive;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isWrappedNativeCall(MethodReader method) {
|
private boolean isWrappedNativeCall(MethodReader method) {
|
||||||
if (!method.hasModifier(ElementModifier.NATIVE)) {
|
if (!method.hasModifier(ElementModifier.NATIVE)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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.backend.c.intrinsic;
|
||||||
|
|
||||||
|
import org.teavm.ast.InvocationExpr;
|
||||||
|
import org.teavm.interop.Strings;
|
||||||
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
|
public class StringsIntrinsic implements Intrinsic {
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(MethodReference method) {
|
||||||
|
return method.getClassName().equals(Strings.class.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(IntrinsicContext context, InvocationExpr invocation) {
|
||||||
|
switch (invocation.getMethod().getName()) {
|
||||||
|
case "toC":
|
||||||
|
context.writer().print("teavm_stringToC(");
|
||||||
|
context.emit(invocation.getArguments().get(0));
|
||||||
|
context.writer().print(")");
|
||||||
|
break;
|
||||||
|
case "fromC":
|
||||||
|
context.writer().print("teavm_cToString(");
|
||||||
|
context.emit(invocation.getArguments().get(0));
|
||||||
|
context.writer().print(")");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
interop/core/src/main/java/org/teavm/interop/Strings.java
Normal file
25
interop/core/src/main/java/org/teavm/interop/Strings.java
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public final class Strings {
|
||||||
|
private Strings() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static native Address toC(String javaString);
|
||||||
|
|
||||||
|
public static native String fromC(Address cString);
|
||||||
|
}
|
26
interop/core/src/main/java/org/teavm/interop/c/Variable.java
Normal file
26
interop/core/src/main/java/org/teavm/interop/c/Variable.java
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* 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.METHOD)
|
||||||
|
public @interface Variable {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user