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

View File

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

View File

@ -192,8 +192,6 @@ public class ModuleGenerator {
String importName = (String)annotationValues.get( "name" ); String importName = (String)annotationValues.get( "name" );
writer.prepareImport( name, impoarModule, importName ); writer.prepareImport( name, impoarModule, importName );
writeMethodSignature( name, null, null ); writeMethodSignature( name, null, null );
} else {
writer.prepareFunction( name );
} }
} catch( Exception ioex ) { } catch( Exception ioex ) {
throw WasmException.create( ioex, sourceFile, className, -1 ); 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; 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 * Write an export directive
* @param name * @param name