Write the functions also in text format in the registered order.

This commit is contained in:
Volker Berlin 2019-06-09 22:39:53 +02:00
parent 90126b16da
commit d3bd4902fd
2 changed files with 58 additions and 7 deletions

View File

@ -0,0 +1,30 @@
/*
* Copyright 2017 - 2019 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.text;
/**
* A function in the wasm.
*
* @author Volker Berlin
*/
class Function {
int id;
int typeId;
final StringBuilder output = new StringBuilder();
}

View File

@ -63,9 +63,11 @@ public class TextModuleWriter extends ModuleWriter {
private ArrayList<String> types = new ArrayList<>(); private ArrayList<String> types = new ArrayList<>();
private StringBuilder methodOutput = new StringBuilder(); private StringBuilder methodOutput;
private Map<String, Integer> functions = new LinkedHashMap<>(); private StringBuilder imports = new StringBuilder();
private Map<String, Function> functions = new LinkedHashMap<>();
private int inset; private int inset;
@ -109,7 +111,11 @@ public class TextModuleWriter extends ModuleWriter {
output.append( "(type $t" ).append( Integer.toString( i ) ).append( " (func" ).append( types.get( i ) ).append( "))" ); output.append( "(type $t" ).append( Integer.toString( i ) ).append( " (func" ).append( types.get( i ) ).append( "))" );
} }
output.append( methodOutput ); output.append( imports );
for( Function func : functions.values() ) {
output.append( func.output );
}
if( callIndirect ) { if( callIndirect ) {
int count = functions.size(); int count = functions.size();
@ -151,7 +157,7 @@ public class TextModuleWriter extends ModuleWriter {
protected int writeStructType( StructType type ) throws IOException { protected int writeStructType( StructType type ) throws IOException {
type.setVTable( dataStream.size() ); type.setVTable( dataStream.size() );
for( FunctionName funcName : type.getMethods() ) { for( FunctionName funcName : type.getMethods() ) {
int functIdx = functions.get( funcName.signatureName ); int functIdx = functions.get( funcName.signatureName ).id;
// little-endian byte order // little-endian byte order
dataStream.write( functIdx >>> 0 ); dataStream.write( functIdx >>> 0 );
dataStream.write( functIdx >>> 8 ); dataStream.write( functIdx >>> 8 );
@ -211,6 +217,7 @@ public class TextModuleWriter extends ModuleWriter {
@Override @Override
protected void prepareImport( FunctionName name, String importModule, String importName ) throws IOException { protected void prepareImport( FunctionName name, String importModule, String importName ) throws IOException {
if( importName != null ) { if( importName != null ) {
methodOutput = imports;
newline( methodOutput ); newline( methodOutput );
methodOutput.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( normalizeName( name ) ); methodOutput.append( "(import \"" ).append( importModule ).append( "\" \"" ).append( importName ).append( "\" (func $" ).append( normalizeName( name ) );
isImport = true; isImport = true;
@ -318,19 +325,32 @@ public class TextModuleWriter extends ModuleWriter {
idx = types.size(); idx = types.size();
types.add( typeStr ); types.add( typeStr );
} }
functions.put( name.signatureName, idx ); getFunction( name ).typeId = idx;
if( isImport ) { if( isImport ) {
isImport = false; isImport = false;
methodOutput.append( "))" ); methodOutput.append( "))" );
methodOutput = null;
} }
} }
private Function getFunction( FunctionName name ) {
Function func = functions.get( name.signatureName );
if( func == null ) {
func = new Function();
func.id = functions.size();
functions.put( name.signatureName, func );
}
return func;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException { protected void writeMethodStart( FunctionName name, String sourceFile ) throws IOException {
methodOutput = getFunction( name ).output;
newline( methodOutput ); newline( methodOutput );
methodOutput.append( "(func $" ); methodOutput.append( "(func $" );
methodOutput.append( normalizeName( name ) ); methodOutput.append( normalizeName( name ) );
@ -353,6 +373,7 @@ public class TextModuleWriter extends ModuleWriter {
inset--; inset--;
newline( methodOutput ); newline( methodOutput );
methodOutput.append( ')' ); methodOutput.append( ')' );
methodOutput = null;
} }
/** /**
@ -593,9 +614,9 @@ public class TextModuleWriter extends ModuleWriter {
methodOutput.append( "i32.load offset=" ).append( virtualFunctionIdx * 4 ); // use default alignment methodOutput.append( "i32.load offset=" ).append( virtualFunctionIdx * 4 ); // use default alignment
newline( methodOutput ); newline( methodOutput );
if(spiderMonkey) if(spiderMonkey)
methodOutput.append( "call_indirect $t" ).append( functions.get( name.signatureName ) ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779 methodOutput.append( "call_indirect $t" ).append( functions.get( name.signatureName ).typeId ); // https://bugzilla.mozilla.org/show_bug.cgi?id=1556779
else else
methodOutput.append( "call_indirect (type $t" ).append( functions.get( name.signatureName ) ).append( ')' ); methodOutput.append( "call_indirect (type $t" ).append( functions.get( name.signatureName ).typeId ).append( ')' );
} }
/** /**