add FunctionType to differ between real and abstract methods

This commit is contained in:
Volker Berlin 2020-03-20 20:35:13 +01:00
parent dbf658ec41
commit ae0f1e3410
5 changed files with 62 additions and 15 deletions

View File

@ -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<String, ImportFunction> imports = new LinkedHashMap<>();
private Map<String, Function> 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;

View File

@ -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<FunctionName> 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<FunctionName> 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<WasmInstruction> 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 );

View File

@ -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
*/

View File

@ -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();
}

View File

@ -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;
}