Use the GC type for strings and classes table.

This commit is contained in:
Volker Berlin 2021-01-03 17:14:55 +01:00
parent 672aca29a7
commit 5c7f7f380c
2 changed files with 29 additions and 4 deletions

View File

@ -96,6 +96,10 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private FunctionName startFunction;
private StructType stringType;
private StructType classType;
/**
* Create new instance.
*
@ -195,14 +199,14 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
// string constants table
if( count >= 2 ) {
stream.writeValueType( ValueType.externref ); // the type of elements
stream.writeValueType( stringType != null ? stringType : ValueType.externref ); // the type of elements
stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available
stream.writeVaruint32( stringCount ); // initial length
}
// table with classes
if( count >= 3 ) {
stream.writeValueType( ValueType.externref ); // the type of elements
stream.writeValueType( classType != null ? classType : ValueType.externref ); // the type of elements
stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available
stream.writeVaruint32( typeCount ); // initial length
}
@ -506,6 +510,15 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
return ValueType.externref.getCode();
}
switch( type.getName() ) {
case "java/lang/String":
stringType = type;
break;
case "java/lang/Class":
classType = type;
break;
}
int typeId = functionTypes.size();
List<NamedStorageType> fields = type.getFields();
functionTypes.add( type.getKind() == StructTypeKind.array_native ? new ArrayTypeEntry( fields ) : new StructTypeEntry( fields ) );

View File

@ -86,6 +86,10 @@ public class TextModuleWriter extends ModuleWriter {
private boolean callIndirect;
private boolean useTypeString;
private boolean useTypeClass;
/**
* Create a new instance.
*
@ -154,7 +158,7 @@ public class TextModuleWriter extends ModuleWriter {
textOutput.append( "(table $functions 0 funcref)" );
}
newline( textOutput );
String tableTypeName = options.useGC() && types.contains( "java/lang/String" ) ? "$java/lang/String" : "externref";
String tableTypeName = options.useGC() && useTypeString ? "(ref null $java/lang/String)" : "externref";
textOutput.append( "(table $strings " ).append( Integer.toString( stringCount ) ).append( ' ' ).append( tableTypeName ).append( ')' );
}
@ -162,7 +166,7 @@ public class TextModuleWriter extends ModuleWriter {
int typeCount = options.types.size();
if( typeCount > 0 ) {
newline( textOutput );
String tableTypeName = options.useGC() && types.contains( "java/lang/Class" ) ? "$java/lang/Class" : "externref";
String tableTypeName = options.useGC() && useTypeClass ? "(ref null $java/lang/Class)" : "externref";
textOutput.append( "(table $classes " ).append( Integer.toString( typeCount ) ).append( ' ' ).append( tableTypeName ).append( ')' );
}
@ -209,6 +213,14 @@ public class TextModuleWriter extends ModuleWriter {
inset = 1;
newline( output );
String typeName = normalizeName( type.getName() );
switch( typeName ) {
case "java/lang/String":
useTypeString = true;
break;
case "java/lang/Class":
useTypeClass = true;
break;
}
String kind = type.getKind() == StructTypeKind.array_native ? "array" : "struct";
output.append( "(type $" ).append( typeName ).append( " (" ).append( kind );
inset++;