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

View File

@ -267,10 +267,12 @@ public abstract class ModuleWriter implements Closeable {
*
* @param name
* the function name
* @param comment
* optional comment for the text format
* @throws IOException
* 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.

View File

@ -42,6 +42,8 @@ class WasmCallInstruction extends WasmInstruction {
private final boolean needThisParameter;
private final String comment;
/**
* 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
*/
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 );
this.name = name;
this.types = types;
this.needThisParameter = needThisParameter;
this.comment = comment;
}
/**
@ -93,7 +116,7 @@ class WasmCallInstruction extends WasmInstruction {
* {@inheritDoc}
*/
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.writeConst( virtualFunctionIdx * 4, ValueType.i32 );
writer.writeFunctionCall( options.getCallVirtual() );
writer.writeFunctionCall( options.getCallVirtual(), null );
StructType type = getThisType();
writer.writeVirtualFunctionCall( getFunctionName(), type );
}

View File

@ -398,7 +398,7 @@ public abstract class WasmCodeBuilder {
Integer id = strings.get( value );
FunctionName name = strings.getStringConstantFunction();
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 ) {
instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) );
} else if( value instanceof ConstantClass ) {

View File

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

View File

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