155 lines
4.0 KiB
Java
Raw Normal View History

2017-03-20 17:48:05 +01:00
/*
* Copyright 2017 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;
import java.io.IOException;
2017-03-26 13:40:50 +02:00
import java.util.List;
2017-03-20 17:48:05 +01:00
2017-03-24 18:08:03 +01:00
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.ValueType;
2017-03-20 17:48:05 +01:00
/**
* Module Writer for text format with S-expressions.
*
* @author Volker Berlin
*
*/
2017-03-24 18:08:03 +01:00
public class TextModuleWriter extends ModuleWriter {
2017-03-20 17:48:05 +01:00
private Appendable output;
2017-03-26 16:49:07 +02:00
private StringBuilder methodOutput = new StringBuilder();
2017-03-20 17:48:05 +01:00
private int inset;
/**
* Create a new instance.
*
* @param output
* target for the result
* @throws IOException
* if any I/O error occur
*/
2017-03-24 18:08:03 +01:00
public TextModuleWriter( Appendable output ) throws IOException {
2017-03-20 17:48:05 +01:00
this.output = output;
output.append( "(module" );
inset++;
}
/**
* {@inheritDoc}
2017-03-20 17:48:05 +01:00
*/
@Override
public void close() throws IOException {
inset--;
2017-03-26 16:49:07 +02:00
newline( output );
2017-03-20 17:48:05 +01:00
output.append( ')' );
}
/**
* {@inheritDoc}
2017-03-20 17:48:05 +01:00
*/
@Override
protected void writeMethodStart( String name ) throws IOException {
2017-03-26 16:49:07 +02:00
newline( output );
output.append( "(func $" );
output.append( name );
inset++;
2017-03-26 16:49:07 +02:00
methodOutput.setLength( 0 );
2017-03-20 17:48:05 +01:00
}
/**
* {@inheritDoc}
2017-03-20 17:48:05 +01:00
*/
@Override
protected void writeMethodParam( String kind, ValueType valueType ) throws IOException {
output.append( " (" ).append( kind ).append( ' ' ).append( valueType.toString() ).append( ')' );
}
/**
* {@inheritDoc}
*/
@Override
2017-03-26 13:40:50 +02:00
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
2017-03-26 16:49:07 +02:00
for( ValueType valueType : locals ) {
output.append( " (local " ).append( valueType.toString() ).append( ')' );
}
output.append( methodOutput );
2017-03-20 17:48:05 +01:00
inset--;
2017-03-26 16:49:07 +02:00
newline( output );
2017-03-20 17:48:05 +01:00
output.append( ')' );
}
/**
* {@inheritDoc}
2017-03-20 17:48:05 +01:00
*/
@Override
protected void writeConstInt( int value ) throws IOException {
2017-03-26 16:49:07 +02:00
newline( methodOutput );
methodOutput.append( "i32.const " ).append( Integer.toString( value ) );
}
2017-03-26 13:40:50 +02:00
/**
* {@inheritDoc}
*/
@Override
2017-03-26 13:40:50 +02:00
protected void writeLoad( int idx ) throws IOException {
2017-03-26 16:49:07 +02:00
newline( methodOutput );
methodOutput.append( "get_local " ).append( Integer.toString( idx ) );
}
/**
* {@inheritDoc}
*/
@Override
2017-03-26 13:40:50 +02:00
protected void writeStore( int idx ) throws IOException {
2017-03-26 16:49:07 +02:00
newline( methodOutput );
methodOutput.append( "set_local " ).append( Integer.toString( idx ) );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeAddInt() throws IOException {
2017-03-26 16:49:07 +02:00
newline( methodOutput );
methodOutput.append( "i32.add" );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeReturn() throws IOException {
2017-03-26 16:49:07 +02:00
newline( methodOutput );
methodOutput.append( "return" );
2017-03-20 17:48:05 +01:00
}
/**
* Add a newline the insets.
*
* @throws IOException
* if any I/O error occur
*/
2017-03-26 16:49:07 +02:00
private void newline( Appendable output ) throws IOException {
2017-03-20 17:48:05 +01:00
output.append( '\n' );
for( int i = 0; i < inset; i++ ) {
output.append( ' ' );
output.append( ' ' );
}
}
}