From ae0f1e3410565a077cd9f2edd852709cc057ec33 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Fri, 20 Mar 2020 20:35:13 +0100 Subject: [PATCH] add FunctionType to differ between real and abstract methods --- .../binary/BinaryModuleWriter.java | 21 +++++++++---- .../jwebassembly/module/ModuleGenerator.java | 15 ++++++---- .../jwebassembly/module/ModuleWriter.java | 8 +++-- .../jwebassembly/text/TextModuleWriter.java | 3 +- .../jwebassembly/wasm/FunctionType.java | 30 +++++++++++++++++++ 5 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/wasm/FunctionType.java diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index c470bdc..496006a 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -18,6 +18,7 @@ package de.inetsoftware.jwebassembly.binary; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -38,6 +39,7 @@ import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter; import de.inetsoftware.jwebassembly.sourcemap.SourceMapping; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; +import de.inetsoftware.jwebassembly.wasm.FunctionType; import de.inetsoftware.jwebassembly.wasm.MemoryOperator; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NumericOperator; @@ -77,6 +79,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod private Map imports = new LinkedHashMap<>(); + private Map abstracts = new HashMap<>(); + private Function function; private FunctionTypeEntry functionType; @@ -513,8 +517,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeMethodParamStart( FunctionName name ) throws IOException { - function = getFunction( name ); + protected void writeMethodParamStart( FunctionName name, FunctionType funcType ) throws IOException { + if( funcType == FunctionType.Abstract ) { + abstracts.put( name.signatureName, function = new Function() ); + } else { + function = getFunction( name ); + } functionType = new FunctionTypeEntry(); locals.clear(); } @@ -1186,9 +1194,12 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod if( func == null ) { func = imports.get( signatureName ); if( func == null ) { - func = new Function(); - func.id = functions.size() + imports.size(); - functions.put( signatureName, func ); + func = abstracts.get( signatureName ); + if( func == null ) { + func = new Function(); + func.id = functions.size() + imports.size(); + functions.put( signatureName, func ); + } } } return func; diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 80c8afa..acf475f 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -43,6 +43,7 @@ import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.wasm.AnyType; +import de.inetsoftware.jwebassembly.wasm.FunctionType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -294,20 +295,20 @@ public class ModuleGenerator { importName = name.methodName; } writer.prepareImport( name, importModule, importName ); - writeMethodSignature( name, null ); + writeMethodSignature( name, FunctionType.Import, null ); javaScript.addImport( importModule, importName, importAnannotation ); } // init/write the function types for( Iterator iterator = functions.getWriteLater(); iterator.hasNext(); ) { FunctionName name = iterator.next(); - writeMethodSignature( name, null ); + writeMethodSignature( name, FunctionType.Code, null ); } // register types of abstract and interface methods for( Iterator iterator = functions.getAbstractedFunctions(); iterator.hasNext(); ) { FunctionName name = iterator.next(); - //writeMethodSignature( name, null ); + writeMethodSignature( name, FunctionType.Abstract, null ); } JWebAssembly.LOGGER.fine( "scan finsih" ); @@ -516,7 +517,7 @@ public class ModuleGenerator { private void writeMethodImpl( FunctionName name, WasmCodeBuilder codeBuilder ) throws WasmException, IOException { writer.writeMethodStart( name, sourceFile ); functions.markAsWritten( name ); - writeMethodSignature( name, codeBuilder ); + writeMethodSignature( name, FunctionType.Code, codeBuilder ); List instructions = codeBuilder.getInstructions(); optimizer.optimze( instructions ); @@ -600,6 +601,8 @@ public class ModuleGenerator { * * @param name * the Java signature, typical method.getType(); + * @param funcType + * the type of function * @param codeBuilder * the calculated variables * @throws IOException @@ -607,8 +610,8 @@ public class ModuleGenerator { * @throws WasmException * if some Java code can't converted */ - private void writeMethodSignature( FunctionName name, WasmCodeBuilder codeBuilder ) throws IOException, WasmException { - writer.writeMethodParamStart( name ); + private void writeMethodSignature( @Nonnull FunctionName name, @Nonnull FunctionType funcType, @Nullable WasmCodeBuilder codeBuilder ) throws IOException, WasmException { + writer.writeMethodParamStart( name, funcType ); int paramCount = 0; if( functions.needThisParameter( name ) ) { StructType instanceType = types.valueOf( name.className ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 18edcc6..b2d8a16 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -26,6 +26,7 @@ import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; +import de.inetsoftware.jwebassembly.wasm.FunctionType; import de.inetsoftware.jwebassembly.wasm.MemoryOperator; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NumericOperator; @@ -117,11 +118,13 @@ public abstract class ModuleWriter implements Closeable { * * @param name * the function name + * @param funcType + * the type of function * * @throws IOException * if any I/O error occur */ - protected abstract void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException; + protected abstract void writeMethodParamStart( @Nonnull FunctionName name, @Nonnull FunctionType funcType ) throws IOException; /** * Write a method parameter. @@ -149,13 +152,12 @@ public abstract class ModuleWriter implements Closeable { protected abstract void writeMethodParamFinish( @Nonnull FunctionName name ) throws IOException; /** - * Write the method header. + * Start the writing of method/function code. * * @param name * the function name * @param sourceFile * the name of the source file - * * @throws IOException * if any I/O error occur */ diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index a170174..fee36a2 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -37,6 +37,7 @@ import de.inetsoftware.jwebassembly.module.WasmOptions; import de.inetsoftware.jwebassembly.module.WasmTarget; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; +import de.inetsoftware.jwebassembly.wasm.FunctionType; import de.inetsoftware.jwebassembly.wasm.MemoryOperator; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NumericOperator; @@ -328,7 +329,7 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeMethodParamStart( @Nonnull FunctionName name ) throws IOException { + protected void writeMethodParamStart( @Nonnull FunctionName name, FunctionType funcType ) throws IOException { typeOutput.setLength( 0 ); methodParamNames.clear(); } diff --git a/src/de/inetsoftware/jwebassembly/wasm/FunctionType.java b/src/de/inetsoftware/jwebassembly/wasm/FunctionType.java new file mode 100644 index 0000000..cafe8e2 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/wasm/FunctionType.java @@ -0,0 +1,30 @@ +/* + * 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.wasm; + +/** + * Type of function name. + * + * @author Volker Berlin + */ +public enum FunctionType { + /** imported function */ + Import, + /** has real code */ + Code, + /** abstract or interface, only used for indirrect call */ + Abstract; +}