diff --git a/src/de/inetsoftware/jwebassembly/module/StringManager.java b/src/de/inetsoftware/jwebassembly/module/StringManager.java index 34f23e8..7a45182 100644 --- a/src/de/inetsoftware/jwebassembly/module/StringManager.java +++ b/src/de/inetsoftware/jwebassembly/module/StringManager.java @@ -75,10 +75,10 @@ public class StringManager extends LinkedHashMap { @Nonnull FunctionName getStringConstantFunction() { if( stringConstantFunction == null ) { - stringConstantFunction = new FunctionName( "de/inetsoftware/jwebassembly/module/StringManager.stringConstant(I)Ljava/lang/String;" ); + stringConstantFunction = new FunctionName( "de/inetsoftware/jwebassembly/module/nativecode/StringTable.stringConstant(I)Ljava/lang/String;" ); // register the function stringsMemoryOffset() as synthetic function WatCodeSyntheticFunctionName offsetFunction = - new WatCodeSyntheticFunctionName( "de/inetsoftware/jwebassembly/module/StringManager", "stringsMemoryOffset", "()I", "", null, ValueType.i32 ) { + new WatCodeSyntheticFunctionName( "de/inetsoftware/jwebassembly/module/nativecode/StringTable", "stringsMemoryOffset", "()I", "", null, ValueType.i32 ) { protected String getCode() { return "i32.const " + stringMemoryOffset; } @@ -176,101 +176,4 @@ public class StringManager extends LinkedHashMap { } while( value != 0 ); } - /** - * WASM code

- * Get a constant string from the table. - * - * @param strIdx - * the id/index of the string. - * @return the string - * @see #STRING_CONSTANT_FUNCTION - */ - static String stringConstant( int strIdx ) { - String str = getStringFromTable( strIdx ); - if( str != null ) { - return str; - } - - // read the compact string length - int offset = getIntFromMemory( strIdx * 4 + stringsMemoryOffset() ); - int length = 0; - int b; - int shift = 0; - do { - b = getUnsignedByteFromMemory( offset++ ); - length += (b & 0x7F) << shift; - shift += 7; - } while( b >= 0x80 ); - - // copy the bytes from the data section - byte[] bytes = new byte[length]; - for( int i = 0; i < length; i++ ) { - bytes[i] = getUnsignedByteFromMemory( i + offset ); - } - str = new String( bytes ); - // save the string for future use - setStringIntoTable( strIdx, str ); - return str; - } - - /** - * WASM code

- * Get a string from the string table. Should be inlined from the optimizer. - * - * @param strIdx - * the id/index of the string. - * @return the string or null if not already set. - */ - @WasmTextCode( "local.get 0 " + // - "table.get 1 " + // - "return" ) - private static native String getStringFromTable( int strIdx ); - - /** - * WASM code

- * Set a string in the string table. Should be inlined from the optimizer. - * - * @param strIdx - * the id/index of the string. - * @param str - * the string - */ - @WasmTextCode( "local.get 0 " + // - "local.get 1 " + // - "table.set 1 " + // - "return" ) - private static native void setStringIntoTable( int strIdx, String str ); - - /** - * WASM code

- * Placeholder for a synthetic function. Should be inlined from the optimizer. - * @return the memory offset of the serialized string data in the element section - */ - private static native int stringsMemoryOffset(); - - /** - * WASM code

- * Load an i32 from memory. The offset must be aligned. Should be inlined from the optimizer. - * - * @param pos - * the memory position - * @return the value from the memory - */ - @WasmTextCode( "local.get 0 " + // - "i32.load offset=0 align=4 " + // - "return" ) - private static native int getIntFromMemory( int pos ); - - /** - * WASM code

- * Load a byte from the memory. Should be inlined from the optimizer. - * - * @param pos - * the memory position - * @return the value from the memory - */ - @WasmTextCode( "local.get 0 " + // - "i32.load8_u offset=0 " + // - "return" ) - private static native byte getUnsignedByteFromMemory( int pos ); } diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index dd8c29b..e55e960 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -67,22 +67,22 @@ public class TypeManager { /** * Byte position in the type description that contains the offset to the interfaces. Length 4 bytes. */ - static final int TYPE_DESCRIPTION_INTERFACE_OFFSET = 0; + public static final int TYPE_DESCRIPTION_INTERFACE_OFFSET = 0; /** * Byte position in the type description that contains the offset to the instanceof list. Length 4 bytes. */ - static final int TYPE_DESCRIPTION_INSTANCEOF_OFFSET = 4; + public static final int TYPE_DESCRIPTION_INSTANCEOF_OFFSET = 4; /** * Byte position in the type description that contains the offset to class name idx in the string constant table. Length 4 bytes. */ - static final int TYPE_DESCRIPTION_TYPE_NAME = 8; + public static final int TYPE_DESCRIPTION_TYPE_NAME = 8; /** * Byte position in the type description that contains the type of the array (component type). Length 4 bytes. */ - public static final int TYPE_DESCRIPTION_ARRAY_TYPE = 12; + public static final int TYPE_DESCRIPTION_ARRAY_TYPE = 12; /** * The reserved position on start of the vtable: diff --git a/src/de/inetsoftware/jwebassembly/module/ReplacementForClass.java b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java similarity index 86% rename from src/de/inetsoftware/jwebassembly/module/ReplacementForClass.java rename to src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java index a1a8883..134a98b 100644 --- a/src/de/inetsoftware/jwebassembly/module/ReplacementForClass.java +++ b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java @@ -14,12 +14,24 @@ limitations under the License. */ -package de.inetsoftware.jwebassembly.module; +package de.inetsoftware.jwebassembly.module.nativecode; + +import static de.inetsoftware.jwebassembly.module.TypeManager.BOOLEAN; +import static de.inetsoftware.jwebassembly.module.TypeManager.BYTE; +import static de.inetsoftware.jwebassembly.module.TypeManager.CHAR; +import static de.inetsoftware.jwebassembly.module.TypeManager.DOUBLE; +import static de.inetsoftware.jwebassembly.module.TypeManager.FLOAT; +import static de.inetsoftware.jwebassembly.module.TypeManager.INT; +import static de.inetsoftware.jwebassembly.module.TypeManager.LONG; +import static de.inetsoftware.jwebassembly.module.TypeManager.SHORT; +import static de.inetsoftware.jwebassembly.module.TypeManager.TYPE_DESCRIPTION_ARRAY_TYPE; +import static de.inetsoftware.jwebassembly.module.TypeManager.TYPE_DESCRIPTION_INSTANCEOF_OFFSET; +import static de.inetsoftware.jwebassembly.module.TypeManager.TYPE_DESCRIPTION_TYPE_NAME; +import static de.inetsoftware.jwebassembly.module.TypeManager.VOID; import de.inetsoftware.jwebassembly.api.annotation.Replace; import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode; - -import static de.inetsoftware.jwebassembly.module.TypeManager.*; +import de.inetsoftware.jwebassembly.module.TypeManager; /** * Replacement for java.lang.Class @@ -47,7 +59,7 @@ class ReplacementForClass { * @return the name */ String getName() { - return StringManager.stringConstant( getIntFromMemory( vtable + TYPE_DESCRIPTION_TYPE_NAME ) ); + return StringTable.stringConstant( getIntFromMemory( vtable + TYPE_DESCRIPTION_TYPE_NAME ) ); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/nativecode/StringTable.java b/src/de/inetsoftware/jwebassembly/module/nativecode/StringTable.java new file mode 100644 index 0000000..c801550 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/nativecode/StringTable.java @@ -0,0 +1,125 @@ +/* + Copyright 2020 Volker Berlin (i-net software) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ +package de.inetsoftware.jwebassembly.module.nativecode; + +import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode; + +/** + * The WASm string table to create String constant on the fly and hold it. + * + * @author Volker Berlin + */ +class StringTable { + + /** + * WASM code

+ * Get a constant string from the table. + * + * @param strIdx + * the id/index of the string. + * @return the string + * @see #STRING_CONSTANT_FUNCTION + */ + static String stringConstant( int strIdx ) { + String str = getStringFromTable( strIdx ); + if( str != null ) { + return str; + } + + // read the compact string length + int offset = getIntFromMemory( strIdx * 4 + stringsMemoryOffset() ); + int length = 0; + int b; + int shift = 0; + do { + b = getUnsignedByteFromMemory( offset++ ); + length += (b & 0x7F) << shift; + shift += 7; + } while( b >= 0x80 ); + + // copy the bytes from the data section + byte[] bytes = new byte[length]; + for( int i = 0; i < length; i++ ) { + bytes[i] = getUnsignedByteFromMemory( i + offset ); + } + str = new String( bytes ); + // save the string for future use + setStringIntoTable( strIdx, str ); + return str; + } + + /** + * WASM code

+ * Get a string from the string table. Should be inlined from the optimizer. + * + * @param strIdx + * the id/index of the string. + * @return the string or null if not already set. + */ + @WasmTextCode( "local.get 0 " + // + "table.get 1 " + // + "return" ) + private static native String getStringFromTable( int strIdx ); + + /** + * WASM code

+ * Set a string in the string table. Should be inlined from the optimizer. + * + * @param strIdx + * the id/index of the string. + * @param str + * the string + */ + @WasmTextCode( "local.get 0 " + // + "local.get 1 " + // + "table.set 1 " + // + "return" ) + private static native void setStringIntoTable( int strIdx, String str ); + + /** + * WASM code

+ * Placeholder for a synthetic function. Should be inlined from the optimizer. + * @return the memory offset of the serialized string data in the element section + */ + private static native int stringsMemoryOffset(); + + /** + * WASM code

+ * Load an i32 from memory. The offset must be aligned. Should be inlined from the optimizer. + * + * @param pos + * the memory position + * @return the value from the memory + */ + @WasmTextCode( "local.get 0 " + // + "i32.load offset=0 align=4 " + // + "return" ) + private static native int getIntFromMemory( int pos ); + + /** + * WASM code

+ * Load a byte from the memory. Should be inlined from the optimizer. + * + * @param pos + * the memory position + * @return the value from the memory + */ + @WasmTextCode( "local.get 0 " + // + "i32.load8_u offset=0 " + // + "return" ) + private static native byte getUnsignedByteFromMemory( int pos ); +}