reduce the preparing and write more on the fly

This commit is contained in:
Volker Berlin 2018-12-11 22:38:53 +01:00
parent bf6bf44252
commit 01dcb85b36
4 changed files with 26 additions and 33 deletions

View File

@ -58,8 +58,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private WasmOutputStream codeStream = new WasmOutputStream();
private WasmOutputStream functionsStream = new WasmOutputStream();
private List<FunctionType> functionTypes = new ArrayList<>();
private Map<String, Function> functions = new LinkedHashMap<>();
@ -175,7 +173,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
}
WasmOutputStream stream = new WasmOutputStream();
stream.writeVaruint32( size );
functionsStream.writeTo( stream );
for( Function func : functions.values() ) {
func.functionsStream.writeTo( stream );
}
wasm.writeSection( SectionType.Code, stream );
}
@ -237,14 +237,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
functionType = new FunctionType();
}
/**
* {@inheritDoc}
*/
@Override
protected void prepareFunction( FunctionName name ) {
functions.put( name.signatureName, new Function() );
}
/**
* {@inheritDoc}
*/
@ -274,7 +266,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
*/
@Override
protected void writeMethodStart( FunctionName name ) throws IOException {
function = functions.get( name.signatureName );
function = getFunction( name );
functionType = new FunctionType();
codeStream.reset();
locals.clear();
@ -328,6 +320,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types.
localsStream.writeValueType( valueType );
}
WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream();
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );
localsStream.writeTo( functionsStream );
codeStream.writeTo( functionsStream );
@ -702,23 +695,31 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
*/
@Override
protected void writeFunctionCall( FunctionName name ) throws IOException {
Function func = getFunction( name );
codeStream.writeOpCode( CALL );
codeStream.writeVaruint32( func.id );
}
/**
* Get the function object for the name. If not exists then it will be created.
*
* @param name
* the function name
* @return the function object
*/
@Nonnull
private Function getFunction( FunctionName name ) {
String signatureName = name.signatureName;
int id;
Function func = functions.get( signatureName );
if( func != null ) {
id = func.id;
} else {
ImportFunction entry = imports.get( signatureName );
if( entry != null ) {
id = entry.id;
} else {
if( func == null ) {
func = imports.get( signatureName );
if( func == null ) {
func = new Function();
id = func.id = functions.size() + imports.size();
func.id = functions.size() + imports.size();
functions.put( signatureName, func );
}
}
codeStream.writeOpCode( CALL );
codeStream.writeVaruint32( id );
return func;
}
/**

View File

@ -31,6 +31,8 @@ class Function extends SectionEntry {
List<String> paramNames;
WasmOutputStream functionsStream;
/**
* {@inheritDoc}
*/

View File

@ -192,8 +192,6 @@ public class ModuleGenerator {
String importName = (String)annotationValues.get( "name" );
writer.prepareImport( name, impoarModule, importName );
writeMethodSignature( name, null, null );
} else {
writer.prepareFunction( name );
}
} catch( Exception ioex ) {
throw WasmException.create( ioex, sourceFile, className, -1 );

View File

@ -58,14 +58,6 @@ public abstract class ModuleWriter implements Closeable {
*/
protected abstract void prepareImport( FunctionName name, String importModule, String importName ) throws IOException;
/**
* Prepare a single function in the prepare phase.
*
* @param name
* the function name
*/
protected void prepareFunction( FunctionName name ) {}
/**
* Write an export directive
* @param name