C: fix support for resources

This commit is contained in:
Alexey Andreev 2021-03-24 18:52:54 +03:00
parent 7726d47795
commit b34e25414e
6 changed files with 61 additions and 17 deletions

View File

@ -29,7 +29,7 @@ TeaVM_Array* teavm_resourceMapKeys(TeaVM_ResourceMap *map) {
void** data = TEAVM_ARRAY_DATA(array, void*); void** data = TEAVM_ARRAY_DATA(array, void*);
for (int32_t i = 0; i < map->size; ++i) { for (int32_t i = 0; i < map->size; ++i) {
if (map->entries[i].key != NULL) { if (map->entries[i].key != NULL) {
data[index++] = map->entries[i].key; data[index++] = *map->entries[i].key;
} }
} }

View File

@ -19,6 +19,10 @@ int32_t teavm_hashCode(TeaVM_String* string) {
} }
int32_t teavm_equals(TeaVM_String* first, TeaVM_String* second) { int32_t teavm_equals(TeaVM_String* first, TeaVM_String* second) {
if (first == second) {
return 1;
}
if (first->characters->size != second->characters->size) { if (first->characters->size != second->characters->size) {
return 0; return 0;
} }

View File

@ -204,7 +204,7 @@ class MetadataCIntrinsic implements Generator {
int hashCode = key.hashCode(); int hashCode = key.hashCode();
int collisionRatio = 0; int collisionRatio = 0;
while (true) { while (true) {
int index = mod(hashCode++, table.length); int index = Integer.remainderUnsigned(hashCode++, table.length);
if (table[index] == null) { if (table[index] == null) {
table[index] = key; table[index] = key;
break; break;
@ -249,14 +249,6 @@ class MetadataCIntrinsic implements Generator {
context.writerBefore().outdent().print("}"); context.writerBefore().outdent().print("}");
} }
private static int mod(int a, int b) {
a %= b;
if (a < 0) {
a += b;
}
return a;
}
class MethodGenerator { class MethodGenerator {
private MethodReference targetMethod; private MethodReference targetMethod;
private MetadataGenerator generator; private MetadataGenerator generator;

View File

@ -95,6 +95,7 @@ public class PlatformPlugin implements TeaVMPlugin, MetadataRegistration {
TeaVMCHost cHost = host.getExtension(TeaVMCHost.class); TeaVMCHost cHost = host.getExtension(TeaVMCHost.class);
if (cHost != null) { if (cHost != null) {
host.add(new ResourceCTransformer());
MetadataCIntrinsic metadataCIntrinsic = new MetadataCIntrinsic(); MetadataCIntrinsic metadataCIntrinsic = new MetadataCIntrinsic();
cHost.addGenerator(ctx -> { cHost.addGenerator(ctx -> {
metadataCIntrinsic.init(ctx.getClassSource(), ctx.getClassLoader(), metadataCIntrinsic.init(ctx.getClassSource(), ctx.getClassLoader(),

View File

@ -0,0 +1,34 @@
/*
* Copyright 2014 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.platform.plugin;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.MethodHolder;
import org.teavm.model.Program;
class ResourceCTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
for (MethodHolder method : cls.getMethods()) {
Program program = method.getProgram();
if (program != null) {
new ResourceProgramTransformer(context.getHierarchy(), program).removeCasts();
}
}
}
}

View File

@ -61,18 +61,31 @@ class ResourceProgramTransformer {
insn.delete(); insn.delete();
} }
} else if (insn instanceof CastInstruction) { } else if (insn instanceof CastInstruction) {
CastInstruction cast = (CastInstruction) insn; removeCastToResource((CastInstruction) insn);
if (hierarchy.isSuperType(RESOURCE, cast.getTargetType(), false)) { }
AssignInstruction assign = new AssignInstruction(); }
assign.setReceiver(cast.getReceiver()); }
assign.setAssignee(cast.getValue());
assign.setLocation(cast.getLocation()); void removeCasts() {
insn.replace(assign); for (BasicBlock block : program.getBasicBlocks()) {
for (Instruction insn : block) {
if (insn instanceof CastInstruction) {
removeCastToResource((CastInstruction) insn);
} }
} }
} }
} }
private void removeCastToResource(CastInstruction cast) {
if (hierarchy.isSuperType(RESOURCE, cast.getTargetType(), false)) {
AssignInstruction assign = new AssignInstruction();
assign.setReceiver(cast.getReceiver());
assign.setAssignee(cast.getValue());
assign.setLocation(cast.getLocation());
cast.replace(assign);
}
}
private List<Instruction> transformInvoke(InvokeInstruction insn) { private List<Instruction> transformInvoke(InvokeInstruction insn) {
if (insn.getType() != InvocationType.VIRTUAL) { if (insn.getType() != InvocationType.VIRTUAL) {
return null; return null;