From b7323776d1bc5c2cdefee4d22c5cffd2d2ce4337 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Mon, 22 Apr 2019 15:56:11 +0200 Subject: [PATCH] Use index for fields of structs in text format until there are better naming rules. --- .../binary/BinaryModuleWriter.java | 5 ++--- .../jwebassembly/binary/StructTypeEntry.java | 18 ------------------ .../jwebassembly/module/ModuleGenerator.java | 4 ++-- .../jwebassembly/module/ModuleWriter.java | 7 +++++-- .../module/WasmStructInstruction.java | 3 ++- .../jwebassembly/text/TextModuleWriter.java | 11 ++++++----- .../jwebassembly/wasm/NamedStorageType.java | 15 ++++++++++----- 7 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index d31416c..7dfad49 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -1018,7 +1018,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException { + protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException { int opCode; switch(op) { case NEW: @@ -1043,8 +1043,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod codeStream.writeValueType( type ); } if( fieldName != null ) { - StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() ); - codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) ); + codeStream.writeVaruint32( idx ); } } } diff --git a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java index 7106515..f55123f 100644 --- a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java +++ b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java @@ -18,9 +18,6 @@ package de.inetsoftware.jwebassembly.binary; import java.io.IOException; import java.util.List; -import javax.annotation.Nonnull; - -import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -66,21 +63,6 @@ class StructTypeEntry extends TypeEntry { } } - /** - * Get the index of the field in this structure - * - * @param fieldName - * the name of the field - * @return the index - */ - int getFieldIdx( @Nonnull NamedStorageType fieldName ) { - int idx = fields.indexOf( fieldName ); - if( idx >= 0 ) { - return idx; - } - throw new WasmException( fieldName + " not found", -1 ); - } - /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 55b053b..eac08bb 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -427,13 +427,13 @@ public class ModuleGenerator { writer.writeConst( 0, ValueType.i32 ); break; case anyref: - writer.writeStructOperator( StructOperator.NULL, null, null ); + writer.writeStructOperator( StructOperator.NULL, null, null, -1 ); break; default: throw new WasmException( "Not supported storage type: " + type, instruction.getLineNumber() ); } } else { - writer.writeStructOperator( StructOperator.NULL, null, null ); + writer.writeStructOperator( StructOperator.NULL, null, null, -1 ); } } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index ac59a0d..4061fa1 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -247,9 +247,12 @@ public abstract class ModuleWriter implements Closeable { * the operation * @param type * the type of the struct - * @param fieldName TODO + * @param fieldName + * the fieldName if the operation is per field + * @param idx + * the index of the field if the operation is per field * @throws IOException * if any I/O error occur */ - protected abstract void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException; + protected abstract void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index bbe146d..9425527 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -93,7 +93,8 @@ class WasmStructInstruction extends WasmInstruction { * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - writer.writeStructOperator( op, type, fieldName ); + int idx = type != null && fieldName != null ? type.getFields().indexOf( fieldName ) : -1; + 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 e4cd66a..fae4368 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -103,13 +103,14 @@ public class TextModuleWriter extends ModuleWriter { int oldInset = inset; inset = 1; newline( output ); - output.append( "(type $" ).append( normalizeName( typeName ) ).append( " (struct" ); + typeName = normalizeName( typeName ); + output.append( "(type $" ).append( typeName ).append( " (struct" ); inset++; for( NamedStorageType field : fields ) { newline( output ); output.append( "(field" ); - if( debugNames && field.getUniqueName() != null ) { - output.append( " $" ).append( normalizeName( field.getUniqueName() ) ); + if( debugNames && field.getName() != null ) { + output.append( " $" ).append( typeName ).append( '.' ).append( field.getName() ); } output.append( " (mut " ); AnyType type = field.getType(); @@ -545,7 +546,7 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException { + protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException { String operation; switch( op ) { case NEW: @@ -571,7 +572,7 @@ public class TextModuleWriter extends ModuleWriter { methodOutput.append( ' ' ).append( normalizeName( type.toString() ) ); } if( fieldName != null ) { - methodOutput.append( " $" ).append( normalizeName( fieldName.getUniqueName() ) ); + methodOutput.append( ' ' ).append( idx ).append( " ;; $" ).append( normalizeName( fieldName.getName() ) ); } } } diff --git a/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java b/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java index 4153dbe..9fe9938 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java +++ b/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java @@ -27,6 +27,8 @@ public class NamedStorageType { private final AnyType type; + private final String className; + private final String name; /** @@ -38,7 +40,7 @@ public class NamedStorageType { * the FieldInfo */ public NamedStorageType( String className, FieldInfo field ) { - this( field.getType(), className + '.' + field.getName() ); + this( field.getType(), className, field.getName() ); } /** @@ -48,7 +50,7 @@ public class NamedStorageType { * the reference */ public NamedStorageType( ConstantRef ref ) { - this( ref.getType(), ref.getClassName() + '.' + ref.getName() ); + this( ref.getType(), ref.getClassName(), ref.getName() ); } /** @@ -56,11 +58,14 @@ public class NamedStorageType { * * @param type * the type + * @param className + * the class name * @param name * the name */ - private NamedStorageType( String type, String name ) { + private NamedStorageType( String type, String className, String name ) { this.type = new ValueTypeParser( type ).next(); + this.className = className; this.name = name; } @@ -79,7 +84,7 @@ public class NamedStorageType { * * @return the name */ - public String getUniqueName() { + public String getName() { return name; } @@ -106,6 +111,6 @@ public class NamedStorageType { return false; } NamedStorageType other = (NamedStorageType)obj; - return name.equals( other.name ); + return name.equals( other.name ) && className.equals( other.className ); } }