Fix Struct.NEW_DEFAULT for GC mode.

This commit is contained in:
Volker Berlin 2021-01-16 17:38:11 +01:00
parent 87af94232c
commit cde24d98ed
4 changed files with 26 additions and 28 deletions

View File

@ -1417,7 +1417,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
case NEW_DEFAULT:
codeStream.writeOpCode( RTT_CANON );
codeStream.writeValueType( type );
opCode = STRUCT_NEW;
opCode = STRUCT_NEW_DEFAULT;
break;
case GET:
opCode = STRUCT_GET;
@ -1433,7 +1433,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
opCode = RTT_CANON;
break;
case NEW_WITH_RTT:
opCode = STRUCT_NEW;
opCode = STRUCT_NEW_DEFAULT;
break;
default:
throw new Error( "Unknown operator: " + op );

View File

@ -491,6 +491,8 @@ interface InstructionOpcodes {
static final int STRUCT_NEW = 0xFB01;
static final int STRUCT_NEW_DEFAULT = 0xFB02;
static final int STRUCT_GET = 0xFB03;
static final int STRUCT_SET = 0xFB06;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2020 Volker Berlin (i-net software)
* Copyright 2017 - 2021 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,8 +44,6 @@ import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.FunctionType;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
import de.inetsoftware.jwebassembly.wasm.StructOperator;
import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.ValueTypeParser;
import de.inetsoftware.jwebassembly.watparser.WatParser;
@ -603,23 +601,6 @@ public class ModuleGenerator {
default:
}
break;
case Struct:
if( !writer.options.useGC() ) {
break;
}
WasmStructInstruction instr = (WasmStructInstruction)instruction;
if( instr.getOperator() == StructOperator.NEW_DEFAULT ) {
StructType structType = instr.getStructType();
List<NamedStorageType> list = structType.getFields();
for( NamedStorageType storageType : list ) {
if( TypeManager.FIELD_VTABLE == storageType.getName() ) {
writer.writeConst( structType.getVTable(), ValueType.i32 );
} else {
writer.writeDefaultValue( storageType.getType() );
}
}
}
break;
default:
}

View File

@ -820,12 +820,27 @@ public abstract class WasmCodeBuilder {
protected void addStructInstruction( StructOperator op, @Nonnull String typeName, @Nullable NamedStorageType fieldName, int javaCodePos, int lineNumber ) {
WasmStructInstruction structInst = new WasmStructInstruction( op, typeName, fieldName, javaCodePos, lineNumber, types );
instructions.add( structInst );
if( !options.useGC() || op == StructOperator.CAST || op == StructOperator.INSTANCEOF ) {
SyntheticFunctionName name = structInst.createNonGcFunction();
if( name != null ) {
functions.markAsNeeded( name );
functions.markAsImport( name, name.getAnnotation() );
}
switch( op ) {
case CAST:
case INSTANCEOF:
structInst.createNonGcFunction();
break;
case NEW_DEFAULT:
if( options.useGC() ) {
addDupInstruction( javaCodePos, lineNumber );
addConstInstruction( structInst.getStructType().getVTable(), javaCodePos, lineNumber );
instructions.add( new WasmStructInstruction( StructOperator.SET, typeName, new NamedStorageType( ValueType.i32, "", TypeManager.FIELD_VTABLE ), javaCodePos, lineNumber, types ) );
break;
}
//$FALL-THROUGH$
default:
if( !options.useGC() ) {
SyntheticFunctionName name = structInst.createNonGcFunction();
if( name != null ) {
functions.markAsNeeded( name );
functions.markAsImport( name, name.getAnnotation() );
}
}
}
}