write string constant in the text format for easer reading

This commit is contained in:
Volker Berlin 2020-03-21 22:57:42 +01:00
parent 63384e359a
commit 548d701444
7 changed files with 39 additions and 11 deletions

View File

@ -992,7 +992,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( options.useGC() ) { if( options.useGC() ) {
op = REF_EQ; op = REF_EQ;
} else { } else {
writeFunctionCall( options.ref_eq ); writeFunctionCall( options.ref_eq, null );
return; return;
} }
break; break;
@ -1000,7 +1000,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( options.useGC() ) { if( options.useGC() ) {
codeStream.writeOpCode( REF_EQ ); codeStream.writeOpCode( REF_EQ );
} else { } else {
writeFunctionCall( options.ref_eq ); writeFunctionCall( options.ref_eq, null );
} }
op = I32_EQZ; op = I32_EQZ;
break; break;
@ -1161,7 +1161,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeFunctionCall( FunctionName name ) throws IOException { protected void writeFunctionCall( FunctionName name, String comments ) throws IOException {
Function func = getFunction( name ); Function func = getFunction( name );
codeStream.writeOpCode( CALL ); codeStream.writeOpCode( CALL );
codeStream.writeVaruint32( func.id ); codeStream.writeVaruint32( func.id );

View File

@ -267,10 +267,12 @@ public abstract class ModuleWriter implements Closeable {
* *
* @param name * @param name
* the function name * the function name
* @param comment
* optional comment for the text format
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void writeFunctionCall( FunctionName name ) throws IOException; protected abstract void writeFunctionCall( FunctionName name, String comment ) throws IOException;
/** /**
* Write a function call to an instance function. On the stack there must be the object. * Write a function call to an instance function. On the stack there must be the object.

View File

@ -42,6 +42,8 @@ class WasmCallInstruction extends WasmInstruction {
private final boolean needThisParameter; private final boolean needThisParameter;
private final String comment;
/** /**
* Create an instance of a function call instruction * Create an instance of a function call instruction
* *
@ -57,10 +59,31 @@ class WasmCallInstruction extends WasmInstruction {
* true, if this function need additional to the parameter of the signature an extra "this" parameter * true, if this function need additional to the parameter of the signature an extra "this" parameter
*/ */
WasmCallInstruction( FunctionName name, int javaCodePos, int lineNumber, @Nonnull TypeManager types, boolean needThisParameter ) { WasmCallInstruction( FunctionName name, int javaCodePos, int lineNumber, @Nonnull TypeManager types, boolean needThisParameter ) {
this( name, javaCodePos, lineNumber, types, needThisParameter, null );
}
/**
* Create an instance of a function call instruction
*
* @param name
* the function name that should be called
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
* @param types
* the type manager
* @param needThisParameter
* true, if this function need additional to the parameter of the signature an extra "this" parameter
* @param comment
* optional comment for the text format
*/
WasmCallInstruction( FunctionName name, int javaCodePos, int lineNumber, @Nonnull TypeManager types, boolean needThisParameter, String comment ) {
super( javaCodePos, lineNumber ); super( javaCodePos, lineNumber );
this.name = name; this.name = name;
this.types = types; this.types = types;
this.needThisParameter = needThisParameter; this.needThisParameter = needThisParameter;
this.comment = comment;
} }
/** /**
@ -93,7 +116,7 @@ class WasmCallInstruction extends WasmInstruction {
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeFunctionCall( name ); writer.writeFunctionCall( name, comment );
} }
/** /**

View File

@ -83,7 +83,7 @@ class WasmCallVirtualInstruction extends WasmCallIndirectInstruction {
writer.writeLocal( VariableOperator.get, getVariableIndexOfThis() ); writer.writeLocal( VariableOperator.get, getVariableIndexOfThis() );
writer.writeConst( virtualFunctionIdx * 4, ValueType.i32 ); writer.writeConst( virtualFunctionIdx * 4, ValueType.i32 );
writer.writeFunctionCall( options.getCallVirtual() ); writer.writeFunctionCall( options.getCallVirtual(), null );
StructType type = getThisType(); StructType type = getThisType();
writer.writeVirtualFunctionCall( getFunctionName(), type ); writer.writeVirtualFunctionCall( getFunctionName(), type );
} }

View File

@ -398,7 +398,7 @@ public abstract class WasmCodeBuilder {
Integer id = strings.get( value ); Integer id = strings.get( value );
FunctionName name = strings.getStringConstantFunction(); FunctionName name = strings.getStringConstantFunction();
instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) ); instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) );
addCallInstruction( name, javaCodePos, lineNumber ); instructions.add( new WasmCallInstruction( name, javaCodePos, lineNumber, types, false, "\"" + value + "\"" ) );
} else if( value instanceof Number ) { } else if( value instanceof Number ) {
instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) ); instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) );
} else if( value instanceof ConstantClass ) { } else if( value instanceof ConstantClass ) {

View File

@ -188,7 +188,7 @@ class WasmStructInstruction extends WasmInstruction {
if( fieldName != null ) { if( fieldName != null ) {
writer.writeConst( idx, ValueType.i32 ); writer.writeConst( idx, ValueType.i32 );
} }
writer.writeFunctionCall( functionName ); writer.writeFunctionCall( functionName, null );
} else { } else {
writer.writeStructOperator( op, type, fieldName, idx ); writer.writeStructOperator( op, type, fieldName, idx );
} }

View File

@ -590,7 +590,7 @@ public class TextModuleWriter extends ModuleWriter {
newline( methodOutput ); newline( methodOutput );
methodOutput.append( op ); methodOutput.append( op );
} else { } else {
writeFunctionCall( options.ref_eq ); writeFunctionCall( options.ref_eq, null );
} }
if( negate ) { if( negate ) {
writeNumericOperator( NumericOperator.eqz, ValueType.i32 ); writeNumericOperator( NumericOperator.eqz, ValueType.i32 );
@ -685,9 +685,12 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeFunctionCall( FunctionName name ) throws IOException { protected void writeFunctionCall( FunctionName name, String comments ) throws IOException {
newline( methodOutput ); newline( methodOutput );
methodOutput.append( "call $" ).append( normalizeName( name ) ); methodOutput.append( "call $" ).append( normalizeName( name ) );
if( comments != null ) {
methodOutput.append( ") ;; " ).append( comments );
}
} }
/** /**
@ -701,7 +704,7 @@ public class TextModuleWriter extends ModuleWriter {
if(spiderMonkey) if(spiderMonkey)
methodOutput.append( "call_indirect $t" ).append( getFunction( name ).typeId ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779 methodOutput.append( "call_indirect $t" ).append( getFunction( name ).typeId ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779
else else
methodOutput.append( "call_indirect (type $t" ).append( getFunction( name ).typeId ).append( ") ;; " + name.signatureName ); methodOutput.append( "call_indirect (type $t" ).append( getFunction( name ).typeId ).append( ") ;; " ).append( name.signatureName );
} }
/** /**