improve the type handling of arrays

This commit is contained in:
Volker Berlin 2019-08-21 20:52:12 +02:00
parent 572d4d6c1f
commit 083ebf263e
4 changed files with 50 additions and 12 deletions

View File

@ -65,6 +65,26 @@ public abstract class NonGC {
return 0; // for compiler return 0; // for compiler
} }
@Import( js = "(a) => a.length" )
static int array_len_i64( Object array ) {
return 0; // for compiler
}
@Import( js = "(a) => a.length" )
static int array_len_f32( Object array ) {
return 0; // for compiler
}
@Import( js = "(a) => a.length" )
static int array_len_f64( Object array ) {
return 0; // for compiler
}
@Import( js = "(a) => a.length" )
static int array_len_anyref( Object array ) {
return 0; // for compiler
}
@Import( js = "(a,i,v) => a[i]=v" ) @Import( js = "(a,i,v) => a[i]=v" )
static void array_set_i8( byte[] array, int idx, byte value ) { static void array_set_i8( byte[] array, int idx, byte value ) {
} }

View File

@ -26,9 +26,9 @@ import de.inetsoftware.classparser.ConstantClass;
import de.inetsoftware.classparser.ConstantPool; import de.inetsoftware.classparser.ConstantPool;
import de.inetsoftware.classparser.ConstantRef; import de.inetsoftware.classparser.ConstantRef;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.ArrayType;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.NumericOperator;
import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.StructOperator;
@ -539,7 +539,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
case 175: // dreturn case 175: // dreturn
case 176: // areturn case 176: // areturn
case 177: // return void case 177: // return void
ValueType type = null; AnyType type = null;
switch( op ) { switch( op ) {
case 172: // ireturn case 172: // ireturn
type = ValueType.i32; type = ValueType.i32;
@ -603,22 +603,28 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
case 188: // newarray case 188: // newarray
int typeValue = byteCode.readByte(); int typeValue = byteCode.readByte();
switch( typeValue ) { switch( typeValue ) {
case 4: // boolean case 4: // boolean
case 5: // char type = ValueType.i8;
type = ValueType.i32; break;
case 5: // char
type = ValueType.i16;
break; break;
case 6: //float case 6: //float
type = ValueType.f32; type = ValueType.f32;
break; break;
case 7: //double case 7: //double
type = ValueType.f64; type = ValueType.f64;
break; break;
case 8: //byte case 8: //byte
case 9: //short type = ValueType.i8;
case 10: //int break;
case 9: //short
type = ValueType.i16;
break;
case 10: //int
type = ValueType.i32; type = ValueType.i32;
break; break;
case 11: //long case 11: //long
type = ValueType.i64; type = ValueType.i64;
break; break;
default: default:
@ -628,11 +634,12 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
break; break;
case 189: // anewarray case 189: // anewarray
name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName(); name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
type = ValueType.anyref; //TODO we need to use the right type from name type = ValueType.anyref; //TODO we need to use the right type from name; getTypeManager().valueOf( name );
addArrayInstruction( ArrayOperator.NEW, type, codePos, lineNumber ); addArrayInstruction( ArrayOperator.NEW, type, codePos, lineNumber );
break; break;
case 190: // arraylength case 190: // arraylength
addArrayInstruction( ArrayOperator.LEN, ValueType.i32, codePos, lineNumber ); type = ((ArrayType)findValueTypeFromStack( 1 )).getArrayType();
addArrayInstruction( ArrayOperator.LEN, type, codePos, lineNumber );
break; break;
case 191: // athrow case 191: // athrow
addBlockInstruction( WasmBlockOperator.THROW, null, codePos, lineNumber ); addBlockInstruction( WasmBlockOperator.THROW, null, codePos, lineNumber );

View File

@ -346,6 +346,9 @@ public abstract class WasmCodeBuilder {
instructions.add( new WasmArrayInstruction( op, type, javaCodePos, lineNumber ) ); instructions.add( new WasmArrayInstruction( op, type, javaCodePos, lineNumber ) );
} else { } else {
try { try {
if( type.getCode() >= 0 ) {
type = ValueType.anyref;
}
String api = "array_" + op.toString().toLowerCase() + "_" + type; String api = "array_" + op.toString().toLowerCase() + "_" + type;
ClassFile classFile = ClassFile.get( NonGC.class.getName().replace( '.', '/' ), getClass().getClassLoader() ); ClassFile classFile = ClassFile.get( NonGC.class.getName().replace( '.', '/' ), getClass().getClassLoader() );
for( MethodInfo method : classFile.getMethods() ) { for( MethodInfo method : classFile.getMethods() ) {

View File

@ -33,6 +33,14 @@ public class ArrayType implements AnyType {
this.arrayType = arrayType; this.arrayType = arrayType;
} }
/**
* The element type of the array
* @return the type
*/
public AnyType getArrayType() {
return arrayType;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */