From 841e09cbd5a880fd1a0a3287913f2de056e082c4 Mon Sep 17 00:00:00 2001 From: lax1dude Date: Sun, 3 Nov 2024 15:26:00 -0800 Subject: [PATCH] Fix static init for LaxMalloc class --- .../org/teavm/backend/wasm/WasmGCTarget.java | 12 +++++- .../backend/wasm/gc/WasmGCDependencies.java | 1 + .../gc/LaxMallocInitializerContributor.java | 38 +++++++++++++++++++ .../gc/WasmGCDeclarationsGenerator.java | 4 ++ .../java/org/teavm/runtime/LaxMalloc.java | 3 ++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/teavm/backend/wasm/generate/gc/LaxMallocInitializerContributor.java diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmGCTarget.java b/core/src/main/java/org/teavm/backend/wasm/WasmGCTarget.java index 224a410df..e8e54d178 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmGCTarget.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmGCTarget.java @@ -33,6 +33,7 @@ import org.teavm.backend.wasm.gc.TeaVMWasmGCHost; import org.teavm.backend.wasm.gc.WasmGCClassConsumer; import org.teavm.backend.wasm.gc.WasmGCClassConsumerContext; import org.teavm.backend.wasm.gc.WasmGCDependencies; +import org.teavm.backend.wasm.generate.gc.LaxMallocInitializerContributor; import org.teavm.backend.wasm.generate.gc.WasmGCDeclarationsGenerator; import org.teavm.backend.wasm.generate.gc.WasmGCNameProvider; import org.teavm.backend.wasm.generate.gc.classes.WasmGCCustomTypeMapperFactory; @@ -77,6 +78,7 @@ import org.teavm.model.transformation.BoundCheckInsertion; import org.teavm.model.transformation.NullCheckFilter; import org.teavm.model.transformation.NullCheckInsertion; import org.teavm.model.util.VariableCategoryProvider; +import org.teavm.runtime.LaxMalloc; import org.teavm.vm.BuildTarget; import org.teavm.vm.TeaVMTarget; import org.teavm.vm.TeaVMTargetController; @@ -301,6 +303,14 @@ public class WasmGCTarget implements TeaVMTarget, TeaVMWasmGCHost { refQueueSupplyFunction.setExportName("teavm.reportGarbageCollectedValue"); } + if(enableDirectMallocSupport) { + var laxMallocClinitRef = new MethodReference(LaxMalloc.class, "", void.class); + if (controller.getDependencyInfo().getMethod(laxMallocClinitRef) != null) { + var laxMallocClinit = declarationsGenerator.functions().forStaticMethod(laxMallocClinitRef); + declarationsGenerator.addEarlyInitializerContributor(new LaxMallocInitializerContributor(laxMallocClinit)); + } + } + moduleGenerator.generate(); customGenerators.contributeToModule(module); generateExceptionExports(declarationsGenerator); @@ -311,7 +321,7 @@ public class WasmGCTarget implements TeaVMTarget, TeaVMWasmGCHost { heapSegment.setOffset(WasmRuntime.align(lastSegment.getOffset() + lastSegment.getLength(), WasmHeap.PAGE_SIZE)); } - heapSegment.setLength(directMallocMinHeapSize); + heapSegment.setLength(WasmHeap.PAGE_SIZE); module.getSegments().add(heapSegment); intrinsics.setupLaxMallocHeap(heapSegment.getOffset(), heapSegment.getOffset() + directMallocMinHeapSize, heapSegment.getOffset() + directMallocMaxHeapSize); diff --git a/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java b/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java index e491f256a..2e9957803 100644 --- a/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java +++ b/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java @@ -131,6 +131,7 @@ public class WasmGCDependencies { } public void contributeDirectMalloc() { + analyzer.linkMethod(new MethodReference(LaxMalloc.class, "", void.class)).use(); analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxMalloc", int.class, Address.class)).use(); analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxCalloc", int.class, Address.class)).use(); analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxFree", Address.class, void.class)).use(); diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/LaxMallocInitializerContributor.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/LaxMallocInitializerContributor.java new file mode 100644 index 000000000..5efac2b1a --- /dev/null +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/LaxMallocInitializerContributor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 lax1dude. + * + * 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.wasm.generate.gc; + +import org.teavm.backend.wasm.model.WasmFunction; +import org.teavm.backend.wasm.model.expression.WasmCall; + +public class LaxMallocInitializerContributor implements WasmGCInitializerContributor { + + private final WasmFunction clinit; + + public LaxMallocInitializerContributor(WasmFunction clinit) { + this.clinit = clinit; + } + + @Override + public void contributeToInitializerDefinitions(WasmFunction function) { + } + + @Override + public void contributeToInitializer(WasmFunction function) { + function.getBody().add(new WasmCall(clinit)); + } + +} diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java index 28433d47a..ca82f55f1 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java @@ -178,4 +178,8 @@ public class WasmGCDeclarationsGenerator { public void addToInitializer(Consumer contributor) { methodGenerator.getGenerationContext().addToInitializer(contributor); } + + public void addEarlyInitializerContributor(WasmGCInitializerContributor contributor) { + initializerContributors.add(contributor); + } } diff --git a/core/src/main/java/org/teavm/runtime/LaxMalloc.java b/core/src/main/java/org/teavm/runtime/LaxMalloc.java index ea9480391..c3867c258 100644 --- a/core/src/main/java/org/teavm/runtime/LaxMalloc.java +++ b/core/src/main/java/org/teavm/runtime/LaxMalloc.java @@ -80,6 +80,9 @@ public final class LaxMalloc { // initialize heap limit addrHeap(ADDR_HEAP_INNER_LIMIT).putAddress(addrHeap(ADDR_HEAP_DATA_START)); addrHeap(ADDR_HEAP_OUTER_LIMIT).putAddress(getHeapMinAddr()); + + //TODO: Need to handle error setting the heap to its initial size + growHeapOuter(getHeapMinAddr().toInt() >> 16); } /**