From 548d701444794779dd7444c2f7cd5f51d50363ab Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 21 Mar 2020 22:57:42 +0100 Subject: [PATCH] write string constant in the text format for easer reading --- .../binary/BinaryModuleWriter.java | 6 ++--- .../jwebassembly/module/ModuleWriter.java | 4 ++- .../module/WasmCallInstruction.java | 25 ++++++++++++++++++- .../module/WasmCallVirtualInstruction.java | 2 +- .../jwebassembly/module/WasmCodeBuilder.java | 2 +- .../module/WasmStructInstruction.java | 2 +- .../jwebassembly/text/TextModuleWriter.java | 9 ++++--- 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 496006a..df314bb 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -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 ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index b2d8a16..46e8695 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -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. diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index 848123f..29e3916 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -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 ); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java index be85082..7415731 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java @@ -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 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 2feeb2a..175f06f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -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 ) { diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index b6d8706..09c1ab7 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -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 ); } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 3acdca1..67f82f0 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -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 ); } /**