implements struct.get/set

This commit is contained in:
Volker Berlin 2019-01-13 11:36:07 +01:00
parent 98f6265abd
commit caa59cab83
8 changed files with 60 additions and 21 deletions

View File

@ -838,7 +838,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException { protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException {
int opCode; int opCode;
switch(op) { switch(op) {
case NEW: case NEW:
@ -862,5 +862,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( type != null ) { if( type != null ) {
codeStream.writeValueType( type ); codeStream.writeValueType( type );
} }
if( fieldName != null ) {
StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() );
codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) );
}
} }
} }

View File

@ -18,6 +18,9 @@ package de.inetsoftware.jwebassembly.binary;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -60,6 +63,23 @@ 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 String fieldName ) {
for( int i = 0; i < fields.size(); i++ ) {
NamedStorageType field = fields.get( i );
if( field.name.equals( fieldName ) ) {
return i;
}
}
throw new WasmException( fieldName + " not found", -1 );
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -90,7 +90,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
case 0: // nop case 0: // nop
break; break;
case 1: // aconst_null case 1: // aconst_null
addStructInstruction( StructOperator.NULL, null, codePos ); addStructInstruction( StructOperator.NULL, null, null, codePos );
break; break;
case 2: // iconst_m1 case 2: // iconst_m1
case 3: // iconst_0 case 3: // iconst_0
@ -532,8 +532,14 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() ); ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
addGlobalInstruction( false, ref, codePos ); addGlobalInstruction( false, ref, codePos );
break; break;
//TODO case 180: // getfield case 180: // getfield
//TODO case 181: // putfield ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
addStructInstruction( StructOperator.GET, ref.getClassName(), ref.getName(), codePos );
break;
case 181: // putfield
ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
addStructInstruction( StructOperator.SET, ref.getClassName(), ref.getName(), codePos );
break;
//TODO case 182: // invokevirtual //TODO case 182: // invokevirtual
case 183: // invokespecial, invoke a constructor case 183: // invokespecial, invoke a constructor
case 184: // invokestatic case 184: // invokestatic
@ -545,7 +551,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
//TODO case 186: // invokedynamic //TODO case 186: // invokedynamic
case 187: // new case 187: // new
String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName(); String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
addStructInstruction( StructOperator.NEW_DEFAULT, name, codePos ); addStructInstruction( StructOperator.NEW_DEFAULT, name, null, codePos );
break; break;
case 188: // newarray case 188: // newarray
int typeValue = byteCode.readByte(); int typeValue = byteCode.readByte();

View File

@ -173,7 +173,7 @@ public class ModuleGenerator {
throw new WasmException( "Missing class: " + className, -1 ); throw new WasmException( "Missing class: " + className, -1 );
} }
ClassFile classFile = new ClassFile( stream ); ClassFile classFile = new ClassFile( stream );
ArrayList<NamedStorageType> list = new ArrayList<>(); List<NamedStorageType> list = new ArrayList<>();
FieldInfo[] fields = classFile.getFields(); FieldInfo[] fields = classFile.getFields();
for( FieldInfo field : fields ) { for( FieldInfo field : fields ) {
if( field.isStatic() ) { if( field.isStatic() ) {

View File

@ -236,8 +236,9 @@ public abstract class ModuleWriter implements Closeable {
* the operation * the operation
* @param type * @param type
* the type of the struct * the type of the struct
* @param fieldName TODO
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void writeStructOperator( StructOperator op, StorageType type ) throws IOException; protected abstract void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException;
} }

View File

@ -233,10 +233,12 @@ public abstract class WasmCodeBuilder {
* the operation * the operation
* @param typeName * @param typeName
* the type name * the type name
* @param fieldName
* the name of field if needed for the operation
* @param javaCodePos * @param javaCodePos
* the code position/offset in the Java method * the code position/offset in the Java method
*/ */
protected void addStructInstruction( StructOperator op, String typeName, int javaCodePos ) { protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
instructions.add( new WasmStructInstruction( op, typeName, javaCodePos ) ); instructions.add( new WasmStructInstruction( op, typeName, fieldName, javaCodePos ) );
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2018 Volker Berlin (i-net software) Copyright 2018 - 2019 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -41,6 +41,8 @@ class WasmStructInstruction extends WasmInstruction {
private StorageType type; private StorageType type;
private final String fieldName;
/** /**
* Create an instance of numeric operation. * Create an instance of numeric operation.
* *
@ -48,13 +50,16 @@ class WasmStructInstruction extends WasmInstruction {
* the struct operation * the struct operation
* @param typeName * @param typeName
* the type name of the parameters * the type name of the parameters
* @param fieldName
* the name of field if needed for the operation
* @param javaCodePos * @param javaCodePos
* the code position/offset in the Java method * the code position/offset in the Java method
*/ */
WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, int javaCodePos ) { WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
super( javaCodePos ); super( javaCodePos );
this.op = op; this.op = op;
this.typeName = typeName; this.typeName = typeName;
this.fieldName = fieldName;
} }
/** /**
@ -88,7 +93,7 @@ class WasmStructInstruction extends WasmInstruction {
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeStructOperator( op, type ); writer.writeStructOperator( op, type, fieldName );
} }
/** /**

View File

@ -257,8 +257,10 @@ public class TextModuleWriter extends ModuleWriter {
case ref_eq: case ref_eq:
methodOutput.append( "ref.eq" ); methodOutput.append( "ref.eq" );
return; return;
default:
} }
break; break;
default:
} }
methodOutput.append( valueType ).append( '.' ).append( op ); methodOutput.append( valueType ).append( '.' ).append( op );
} }
@ -320,8 +322,10 @@ public class TextModuleWriter extends ModuleWriter {
} }
/** /**
* Add a newline the insets. * Add a newline with the insets.
* *
* @param output
* the target
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
@ -445,7 +449,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException { protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException {
String operation; String operation;
switch( op ) { switch( op ) {
case NEW: case NEW:
@ -470,13 +474,10 @@ public class TextModuleWriter extends ModuleWriter {
newline( methodOutput ); newline( methodOutput );
methodOutput.append( operation ); methodOutput.append( operation );
if( type != null ) { if( type != null ) {
int typeCode = type.getCode(); methodOutput.append( ' ' ).append( type );
methodOutput.append( ' ' ); }
if( typeCode < 0 ) { if( fieldName != null ) {
methodOutput.append( type ); methodOutput.append( " $" ).append( fieldName );
} else {
methodOutput.append( typeCode );
}
} }
} }
} }