Fix static init for LaxMalloc class

This commit is contained in:
lax1dude 2024-11-03 15:26:00 -08:00
parent e204a0ecd0
commit 841e09cbd5
5 changed files with 57 additions and 1 deletions

View File

@ -33,6 +33,7 @@ import org.teavm.backend.wasm.gc.TeaVMWasmGCHost;
import org.teavm.backend.wasm.gc.WasmGCClassConsumer; import org.teavm.backend.wasm.gc.WasmGCClassConsumer;
import org.teavm.backend.wasm.gc.WasmGCClassConsumerContext; import org.teavm.backend.wasm.gc.WasmGCClassConsumerContext;
import org.teavm.backend.wasm.gc.WasmGCDependencies; 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.WasmGCDeclarationsGenerator;
import org.teavm.backend.wasm.generate.gc.WasmGCNameProvider; import org.teavm.backend.wasm.generate.gc.WasmGCNameProvider;
import org.teavm.backend.wasm.generate.gc.classes.WasmGCCustomTypeMapperFactory; 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.NullCheckFilter;
import org.teavm.model.transformation.NullCheckInsertion; import org.teavm.model.transformation.NullCheckInsertion;
import org.teavm.model.util.VariableCategoryProvider; import org.teavm.model.util.VariableCategoryProvider;
import org.teavm.runtime.LaxMalloc;
import org.teavm.vm.BuildTarget; import org.teavm.vm.BuildTarget;
import org.teavm.vm.TeaVMTarget; import org.teavm.vm.TeaVMTarget;
import org.teavm.vm.TeaVMTargetController; import org.teavm.vm.TeaVMTargetController;
@ -301,6 +303,14 @@ public class WasmGCTarget implements TeaVMTarget, TeaVMWasmGCHost {
refQueueSupplyFunction.setExportName("teavm.reportGarbageCollectedValue"); refQueueSupplyFunction.setExportName("teavm.reportGarbageCollectedValue");
} }
if(enableDirectMallocSupport) {
var laxMallocClinitRef = new MethodReference(LaxMalloc.class, "<clinit>", void.class);
if (controller.getDependencyInfo().getMethod(laxMallocClinitRef) != null) {
var laxMallocClinit = declarationsGenerator.functions().forStaticMethod(laxMallocClinitRef);
declarationsGenerator.addEarlyInitializerContributor(new LaxMallocInitializerContributor(laxMallocClinit));
}
}
moduleGenerator.generate(); moduleGenerator.generate();
customGenerators.contributeToModule(module); customGenerators.contributeToModule(module);
generateExceptionExports(declarationsGenerator); generateExceptionExports(declarationsGenerator);
@ -311,7 +321,7 @@ public class WasmGCTarget implements TeaVMTarget, TeaVMWasmGCHost {
heapSegment.setOffset(WasmRuntime.align(lastSegment.getOffset() heapSegment.setOffset(WasmRuntime.align(lastSegment.getOffset()
+ lastSegment.getLength(), WasmHeap.PAGE_SIZE)); + lastSegment.getLength(), WasmHeap.PAGE_SIZE));
} }
heapSegment.setLength(directMallocMinHeapSize); heapSegment.setLength(WasmHeap.PAGE_SIZE);
module.getSegments().add(heapSegment); module.getSegments().add(heapSegment);
intrinsics.setupLaxMallocHeap(heapSegment.getOffset(), heapSegment.getOffset() + directMallocMinHeapSize, intrinsics.setupLaxMallocHeap(heapSegment.getOffset(), heapSegment.getOffset() + directMallocMinHeapSize,
heapSegment.getOffset() + directMallocMaxHeapSize); heapSegment.getOffset() + directMallocMaxHeapSize);

View File

@ -131,6 +131,7 @@ public class WasmGCDependencies {
} }
public void contributeDirectMalloc() { public void contributeDirectMalloc() {
analyzer.linkMethod(new MethodReference(LaxMalloc.class, "<clinit>", void.class)).use();
analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxMalloc", int.class, Address.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, "laxCalloc", int.class, Address.class)).use();
analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxFree", Address.class, void.class)).use(); analyzer.linkMethod(new MethodReference(LaxMalloc.class, "laxFree", Address.class, void.class)).use();

View File

@ -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));
}
}

View File

@ -178,4 +178,8 @@ public class WasmGCDeclarationsGenerator {
public void addToInitializer(Consumer<WasmFunction> contributor) { public void addToInitializer(Consumer<WasmFunction> contributor) {
methodGenerator.getGenerationContext().addToInitializer(contributor); methodGenerator.getGenerationContext().addToInitializer(contributor);
} }
public void addEarlyInitializerContributor(WasmGCInitializerContributor contributor) {
initializerContributors.add(contributor);
}
} }

View File

@ -80,6 +80,9 @@ public final class LaxMalloc {
// initialize heap limit // initialize heap limit
addrHeap(ADDR_HEAP_INNER_LIMIT).putAddress(addrHeap(ADDR_HEAP_DATA_START)); addrHeap(ADDR_HEAP_INNER_LIMIT).putAddress(addrHeap(ADDR_HEAP_DATA_START));
addrHeap(ADDR_HEAP_OUTER_LIMIT).putAddress(getHeapMinAddr()); addrHeap(ADDR_HEAP_OUTER_LIMIT).putAddress(getHeapMinAddr());
//TODO: Need to handle error setting the heap to its initial size
growHeapOuter(getHeapMinAddr().toInt() >> 16);
} }
/** /**