Write locals to text format.

This commit is contained in:
Volker Berlin 2017-03-26 16:49:07 +02:00
parent 212e18ccfa
commit 6de8d441b3

View File

@ -31,6 +31,8 @@ public class TextModuleWriter extends ModuleWriter {
private Appendable output; private Appendable output;
private StringBuilder methodOutput = new StringBuilder();
private int inset; private int inset;
/** /**
@ -53,7 +55,7 @@ public class TextModuleWriter extends ModuleWriter {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
inset--; inset--;
newline(); newline( output );
output.append( ')' ); output.append( ')' );
} }
@ -62,10 +64,11 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeMethodStart( String name ) throws IOException { protected void writeMethodStart( String name ) throws IOException {
newline(); newline( output );
output.append( "(func $" ); output.append( "(func $" );
output.append( name ); output.append( name );
inset++; inset++;
methodOutput.setLength( 0 );
} }
/** /**
@ -81,8 +84,12 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeMethodFinish( List<ValueType> locals ) throws IOException { protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
for( ValueType valueType : locals ) {
output.append( " (local " ).append( valueType.toString() ).append( ')' );
}
output.append( methodOutput );
inset--; inset--;
newline(); newline( output );
output.append( ')' ); output.append( ')' );
} }
@ -91,8 +98,8 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeConstInt( int value ) throws IOException { protected void writeConstInt( int value ) throws IOException {
newline(); newline( methodOutput );
output.append( "i32.const " ).append( Integer.toString( value ) ); methodOutput.append( "i32.const " ).append( Integer.toString( value ) );
} }
/** /**
@ -100,8 +107,8 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeLoad( int idx ) throws IOException { protected void writeLoad( int idx ) throws IOException {
newline(); newline( methodOutput );
output.append( "get_local " ).append( Integer.toString( idx ) ); methodOutput.append( "get_local " ).append( Integer.toString( idx ) );
} }
/** /**
@ -109,8 +116,8 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeStore( int idx ) throws IOException { protected void writeStore( int idx ) throws IOException {
newline(); newline( methodOutput );
output.append( "set_local " ).append( Integer.toString( idx ) ); methodOutput.append( "set_local " ).append( Integer.toString( idx ) );
} }
/** /**
@ -118,8 +125,8 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeAddInt() throws IOException { protected void writeAddInt() throws IOException {
newline(); newline( methodOutput );
output.append( "i32.add" ); methodOutput.append( "i32.add" );
} }
/** /**
@ -127,8 +134,8 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeReturn() throws IOException { protected void writeReturn() throws IOException {
newline(); newline( methodOutput );
output.append( "return" ); methodOutput.append( "return" );
} }
/** /**
@ -137,7 +144,7 @@ public class TextModuleWriter extends ModuleWriter {
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
private void newline() throws IOException { private void newline( Appendable output ) throws IOException {
output.append( '\n' ); output.append( '\n' );
for( int i = 0; i < inset; i++ ) { for( int i = 0; i < inset; i++ ) {
output.append( ' ' ); output.append( ' ' );