wasm gc: fix bugs related to invalid nested temporary variable generation

This commit is contained in:
Alexey Andreev 2024-09-12 18:23:05 +02:00
parent eccfaff889
commit 8e2cf27e94
6 changed files with 37 additions and 27 deletions

View File

@ -19,6 +19,7 @@ import static org.teavm.model.lowlevel.ExceptionHandlingUtil.isManagedMethodCall
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import org.teavm.ast.ArrayType; import org.teavm.ast.ArrayType;
import org.teavm.ast.CastExpr; import org.teavm.ast.CastExpr;
import org.teavm.ast.Expr; import org.teavm.ast.Expr;
@ -782,14 +783,14 @@ public class WasmGenerationVisitor extends BaseWasmGenerationVisitor {
} }
@Override @Override
protected void allocateArray(ValueType itemType, WasmExpression length, TextLocation location, WasmLocal local, protected void allocateArray(ValueType itemType, Supplier<WasmExpression> length, TextLocation location,
List<WasmExpression> target) { WasmLocal local, List<WasmExpression> target) {
int classPointer = classGenerator.getClassPointer(ValueType.arrayOf(itemType)); int classPointer = classGenerator.getClassPointer(ValueType.arrayOf(itemType));
var allocFunction = context.functions().forStaticMethod(new MethodReference(Allocator.class, "allocateArray", var allocFunction = context.functions().forStaticMethod(new MethodReference(Allocator.class, "allocateArray",
RuntimeClass.class, int.class, Address.class)); RuntimeClass.class, int.class, Address.class));
var call = new WasmCall(allocFunction); var call = new WasmCall(allocFunction);
call.getArguments().add(new WasmInt32Constant(classPointer)); call.getArguments().add(new WasmInt32Constant(classPointer));
call.getArguments().add(length); call.getArguments().add(length.get());
call.setLocation(location); call.setLocation(location);
if (local != null) { if (local != null) {
target.add(new WasmSetLocal(local, call)); target.add(new WasmSetLocal(local, call));
@ -800,9 +801,10 @@ public class WasmGenerationVisitor extends BaseWasmGenerationVisitor {
@Override @Override
protected WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType, protected WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType,
List<WasmExpression> dimensions, TextLocation location) { Supplier<List<WasmExpression>> dimensions, TextLocation location) {
int dimensionList = -1; int dimensionList = -1;
for (var dimension : dimensions) { var dimensionsValue = dimensions.get();
for (var dimension : dimensionsValue) {
int dimensionAddress = binaryWriter.append(DataPrimitives.INT.createValue()); int dimensionAddress = binaryWriter.append(DataPrimitives.INT.createValue());
if (dimensionList < 0) { if (dimensionList < 0) {
dimensionList = dimensionAddress; dimensionList = dimensionAddress;
@ -817,7 +819,7 @@ public class WasmGenerationVisitor extends BaseWasmGenerationVisitor {
var call = new WasmCall(allocFunction); var call = new WasmCall(allocFunction);
call.getArguments().add(new WasmInt32Constant(classPointer)); call.getArguments().add(new WasmInt32Constant(classPointer));
call.getArguments().add(new WasmInt32Constant(dimensionList)); call.getArguments().add(new WasmInt32Constant(dimensionList));
call.getArguments().add(new WasmInt32Constant(dimensions.size())); call.getArguments().add(new WasmInt32Constant(dimensionsValue.size()));
call.setLocation(location); call.setLocation(location);
return call; return call;
} }

View File

@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier;
import org.teavm.ast.ArrayFromDataExpr; import org.teavm.ast.ArrayFromDataExpr;
import org.teavm.ast.ArrayType; import org.teavm.ast.ArrayType;
import org.teavm.ast.AssignmentStatement; import org.teavm.ast.AssignmentStatement;
@ -1025,9 +1026,10 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
var callSiteId = generateCallSiteId(expr.getLocation()); var callSiteId = generateCallSiteId(expr.getLocation());
callSiteId.generateRegister(block.getBody(), expr.getLocation()); callSiteId.generateRegister(block.getBody(), expr.getLocation());
accept(expr.getLength()); allocateArray(expr.getType(), () -> {
var length = result; accept(expr.getLength());
allocateArray(expr.getType(), length, expr.getLocation(), null, block.getBody()); return result;
}, expr.getLocation(), null, block.getBody());
if (block.getBody().size() == 1) { if (block.getBody().size() == 1) {
result = block.getBody().get(0); result = block.getBody().get(0);
@ -1036,11 +1038,11 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
} }
} }
protected abstract void allocateArray(ValueType itemType, WasmExpression length, TextLocation location, protected abstract void allocateArray(ValueType itemType, Supplier<WasmExpression> length, TextLocation location,
WasmLocal local, List<WasmExpression> target); WasmLocal local, List<WasmExpression> target);
protected abstract WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType, protected abstract WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType,
List<WasmExpression> dimensions, TextLocation location); Supplier<List<WasmExpression>> dimensions, TextLocation location);
@Override @Override
public void visit(ArrayFromDataExpr expr) { public void visit(ArrayFromDataExpr expr) {
@ -1081,7 +1083,7 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
callSiteId.generateRegister(block.getBody(), expr.getLocation()); callSiteId.generateRegister(block.getBody(), expr.getLocation());
var array = tempVars.acquire(wasmArrayType); var array = tempVars.acquire(wasmArrayType);
allocateArray(expr.getType(), new WasmInt32Constant(expr.getData().size()), expr.getLocation(), array, allocateArray(expr.getType(), () -> new WasmInt32Constant(expr.getData().size()), expr.getLocation(), array,
block.getBody()); block.getBody());
for (int i = 0; i < expr.getData().size(); ++i) { for (int i = 0; i < expr.getData().size(); ++i) {
@ -1104,15 +1106,19 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
var callSiteId = generateCallSiteId(expr.getLocation()); var callSiteId = generateCallSiteId(expr.getLocation());
callSiteId.generateRegister(block.getBody(), expr.getLocation()); callSiteId.generateRegister(block.getBody(), expr.getLocation());
var wasmDimensions = new ArrayList<WasmExpression>();
var arrayType = expr.getType(); var arrayType = expr.getType();
for (var dimension : expr.getDimensions()) { for (var dimension : expr.getDimensions()) {
accept(dimension);
wasmDimensions.add(result);
arrayType = ValueType.arrayOf(arrayType); arrayType = ValueType.arrayOf(arrayType);
} }
block.setType(mapType(arrayType)); block.setType(mapType(arrayType));
var call = allocateMultiArray(block.getBody(), expr.getType(), wasmDimensions, expr.getLocation()); var call = allocateMultiArray(block.getBody(), expr.getType(), () -> {
var wasmDimensions = new ArrayList<WasmExpression>();
for (var dimension : expr.getDimensions()) {
accept(dimension);
wasmDimensions.add(result);
}
return wasmDimensions;
}, expr.getLocation());
block.getBody().add(call); block.getBody().add(call);
if (block.getBody().size() == 1) { if (block.getBody().size() == 1) {

View File

@ -1303,7 +1303,7 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit
var local = tempVars.acquire(enumArrayStruct.getReference()); var local = tempVars.acquire(enumArrayStruct.getReference());
var block = new WasmBlock(false); var block = new WasmBlock(false);
block.setType(enumArrayStruct.getReference()); block.setType(enumArrayStruct.getReference());
util.allocateArray(ValueType.parse(Enum.class), fields, null, null, block.getBody()); util.allocateArrayWithElements(ValueType.parse(Enum.class), () -> fields, null, null, block.getBody());
function.getBody().add(new WasmReturn(block)); function.getBody().add(new WasmReturn(block));
tempVars.release(local); tempVars.release(local);

View File

@ -54,7 +54,7 @@ class WasmGCNewArrayFunctionGenerator {
var targetVar = new WasmLocal(classInfoProvider.getClassInfo(ValueType.arrayOf(itemType)).getType(), var targetVar = new WasmLocal(classInfoProvider.getClassInfo(ValueType.arrayOf(itemType)).getType(),
"result"); "result");
function.add(targetVar); function.add(targetVar);
genUtil.allocateArray(itemType, new WasmGetLocal(sizeLocal), null, targetVar, function.getBody()); genUtil.allocateArray(itemType, () -> new WasmGetLocal(sizeLocal), null, targetVar, function.getBody());
function.getBody().add(new WasmReturn(new WasmGetLocal(targetVar))); function.getBody().add(new WasmReturn(new WasmGetLocal(targetVar)));
return function; return function;
} }

View File

@ -17,6 +17,7 @@ package org.teavm.backend.wasm.generate.gc.methods;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
import org.teavm.backend.wasm.generate.TemporaryVariablePool; import org.teavm.backend.wasm.generate.TemporaryVariablePool;
import org.teavm.backend.wasm.generate.gc.classes.WasmGCClassInfoProvider; import org.teavm.backend.wasm.generate.gc.classes.WasmGCClassInfoProvider;
import org.teavm.backend.wasm.model.WasmArray; import org.teavm.backend.wasm.model.WasmArray;
@ -43,16 +44,16 @@ public class WasmGCGenerationUtil {
this.tempVars = tempVars; this.tempVars = tempVars;
} }
public void allocateArray(ValueType itemType, WasmExpression length, TextLocation location, WasmLocal local, public void allocateArray(ValueType itemType, Supplier<WasmExpression> length, TextLocation location,
List<WasmExpression> target) { WasmLocal local, List<WasmExpression> target) {
allocateArray(itemType, location, local, target, arrayType -> new WasmArrayNewDefault(arrayType, length)); allocateArray(itemType, location, local, target, arrayType -> new WasmArrayNewDefault(arrayType, length.get()));
} }
public void allocateArray(ValueType itemType, List<? extends WasmExpression> data, TextLocation location, public void allocateArrayWithElements(ValueType itemType, Supplier<List<? extends WasmExpression>> data,
WasmLocal local, List<WasmExpression> target) { TextLocation location, WasmLocal local, List<WasmExpression> target) {
allocateArray(itemType, location, local, target, arrayType -> { allocateArray(itemType, location, local, target, arrayType -> {
var expr = new WasmArrayNewFixed(arrayType); var expr = new WasmArrayNewFixed(arrayType);
expr.getElements().addAll(data); expr.getElements().addAll(data.get());
return expr; return expr;
}); });
} }

View File

@ -16,6 +16,7 @@
package org.teavm.backend.wasm.generate.gc.methods; package org.teavm.backend.wasm.generate.gc.methods;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import org.teavm.ast.ArrayType; import org.teavm.ast.ArrayType;
import org.teavm.ast.BinaryExpr; import org.teavm.ast.BinaryExpr;
import org.teavm.ast.ConditionalExpr; import org.teavm.ast.ConditionalExpr;
@ -332,14 +333,14 @@ public class WasmGCGenerationVisitor extends BaseWasmGenerationVisitor {
} }
@Override @Override
protected void allocateArray(ValueType itemType, WasmExpression length, TextLocation location, WasmLocal local, protected void allocateArray(ValueType itemType, Supplier<WasmExpression> length, TextLocation location,
List<WasmExpression> target) { WasmLocal local, List<WasmExpression> target) {
generationUtil.allocateArray(itemType, length, location, local, target); generationUtil.allocateArray(itemType, length, location, local, target);
} }
@Override @Override
protected WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType, protected WasmExpression allocateMultiArray(List<WasmExpression> target, ValueType itemType,
List<WasmExpression> dimensions, TextLocation location) { Supplier<List<WasmExpression>> dimensions, TextLocation location) {
return null; return null;
} }