fix structure problems with globals in text writer.

This commit is contained in:
Volker 2018-08-14 22:09:01 +02:00
parent 658b859b62
commit 6ade59d02d
4 changed files with 30 additions and 26 deletions

View File

@ -56,6 +56,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
private Map<String, Function> functions = new LinkedHashMap<>();
private List<ValueType> locals;
private Map<String, Global> globals = new LinkedHashMap<>();
private Map<String, String> exports = new LinkedHashMap<>();
@ -301,20 +303,21 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc}
*/
@Override
protected void writeMethodParamFinish() throws IOException {
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
int typeId = functionTypes.indexOf( functionType );
if( typeId < 0 ) {
typeId = functionTypes.size();
functionTypes.add( functionType );
}
function.typeId = typeId;
this.locals = locals;
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
protected void writeMethodFinish() throws IOException {
WasmOutputStream localsStream = new WasmOutputStream();
localsStream.writeVaruint32( locals.size() );
for( ValueType valueType : locals ) {

View File

@ -41,8 +41,6 @@ public class ModuleGenerator {
private final ModuleWriter writer;
private int paramCount;
private ValueType returnType;
private LocaleVariableManager localVariables = new LocaleVariableManager();
@ -157,7 +155,6 @@ public class ModuleGenerator {
FunctionName name = new FunctionName( method );
writeExport( name, method );
writer.writeMethodStart( name );
writeMethodSignature( method );
localVariables.reset();
branchManager.reset();
@ -165,11 +162,12 @@ public class ModuleGenerator {
byteCode = code.getByteCode();
writeCode( byteCode, method.getConstantPool() );
localVariables.calculate();
writeMethodSignature( method );
for( WasmInstruction instruction : instructions ) {
instruction.writeTo( writer );
}
writer.writeMethodFinish( localVariables.getLocalTypes( paramCount ) );
writer.writeMethodFinish();
}
} catch( Exception ioex ) {
int lineNumber = byteCode == null ? -1 : byteCode.getLineNumber();
@ -215,19 +213,20 @@ public class ModuleGenerator {
int paramCount = 0;
ValueType type = null;
for( int i = 1; i < signature.length(); i++ ) {
paramCount++;
if( signature.charAt( i ) == ')' ) {
this.paramCount = paramCount - 1;
kind = "result";
continue;
}
if( kind == "param" ) {
paramCount++;
}
type = ValueType.getValueType( signature, i );
if( type != null ) {
writer.writeMethodParam( kind, type );
}
}
this.returnType = type;
writer.writeMethodParamFinish();
writer.writeMethodParamFinish( localVariables.getLocalTypes( paramCount ) );
}
/**

View File

@ -98,21 +98,22 @@ public abstract class ModuleWriter implements Closeable {
/**
* Finish the function parameter.
*
* @param locals
* a list with types of local variables
*
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodParamFinish() throws IOException;
protected abstract void writeMethodParamFinish( List<ValueType> locals ) throws IOException;
/**
* Complete the method
*
* @param locals
* a list with types of local variables
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodFinish( List<ValueType> locals ) throws IOException;
protected abstract void writeMethodFinish( ) throws IOException;
/**
* Write a constant number value

View File

@ -98,11 +98,11 @@ public class TextModuleWriter extends ModuleWriter {
*/
@Override
protected void writeMethodStart( FunctionName name ) throws IOException {
newline( output );
output.append( "(func $" );
output.append( name.fullName );
inset++;
methodOutput.setLength( 0 );
newline( methodOutput );
methodOutput.append( "(func $" );
methodOutput.append( name.fullName );
inset++;
}
/**
@ -110,17 +110,21 @@ public class TextModuleWriter extends ModuleWriter {
*/
@Override
protected void writeMethodParam( String kind, ValueType valueType ) throws IOException {
output.append( " (" ).append( kind ).append( ' ' ).append( valueType.toString() ).append( ')' );
methodOutput.append( " (" ).append( kind ).append( ' ' ).append( valueType.toString() ).append( ')' );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeMethodParamFinish() throws IOException {
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
if( isImport ) {
isImport = false;
output.append( "))" );
} else {
for( ValueType valueType : locals ) {
methodOutput.append( " (local " ).append( valueType.toString() ).append( ')' );
}
}
}
@ -128,10 +132,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@Override
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
for( ValueType valueType : locals ) {
output.append( " (local " ).append( valueType.toString() ).append( ')' );
}
protected void writeMethodFinish() throws IOException {
output.append( methodOutput );
inset--;
newline( output );
@ -174,11 +175,11 @@ public class TextModuleWriter extends ModuleWriter {
// declare global variable if not already declared.
output.append( "\n " );
String type = ValueType.getValueType( ref.getType(), 0 ).toString();
output.append( "(global $" ).append( name.fullName ).append( type ).append( ' ' ).append( type ).append( ".const 0)" );
output.append( "(global $" ).append( name.fullName ).append( " (mut " ).append( type ).append( ") " ).append( type ).append( ".const 0)" );
globals.add( name.fullName );
}
newline( methodOutput );
methodOutput.append( load ? "get_local " : "set_local " ).append( name.fullName );
methodOutput.append( load ? "get_global $" : "set_global $" ).append( name.fullName );
}
/**