mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
fix structure problems with globals in text writer.
This commit is contained in:
parent
658b859b62
commit
6ade59d02d
@ -56,6 +56,8 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
|
|
||||||
private Map<String, Function> functions = new LinkedHashMap<>();
|
private Map<String, Function> functions = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
private List<ValueType> locals;
|
||||||
|
|
||||||
private Map<String, Global> globals = new LinkedHashMap<>();
|
private Map<String, Global> globals = new LinkedHashMap<>();
|
||||||
|
|
||||||
private Map<String, String> exports = new LinkedHashMap<>();
|
private Map<String, String> exports = new LinkedHashMap<>();
|
||||||
@ -301,20 +303,21 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodParamFinish() throws IOException {
|
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
|
||||||
int typeId = functionTypes.indexOf( functionType );
|
int typeId = functionTypes.indexOf( functionType );
|
||||||
if( typeId < 0 ) {
|
if( typeId < 0 ) {
|
||||||
typeId = functionTypes.size();
|
typeId = functionTypes.size();
|
||||||
functionTypes.add( functionType );
|
functionTypes.add( functionType );
|
||||||
}
|
}
|
||||||
function.typeId = typeId;
|
function.typeId = typeId;
|
||||||
|
this.locals = locals;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
|
protected void writeMethodFinish() throws IOException {
|
||||||
WasmOutputStream localsStream = new WasmOutputStream();
|
WasmOutputStream localsStream = new WasmOutputStream();
|
||||||
localsStream.writeVaruint32( locals.size() );
|
localsStream.writeVaruint32( locals.size() );
|
||||||
for( ValueType valueType : locals ) {
|
for( ValueType valueType : locals ) {
|
||||||
|
@ -41,8 +41,6 @@ public class ModuleGenerator {
|
|||||||
|
|
||||||
private final ModuleWriter writer;
|
private final ModuleWriter writer;
|
||||||
|
|
||||||
private int paramCount;
|
|
||||||
|
|
||||||
private ValueType returnType;
|
private ValueType returnType;
|
||||||
|
|
||||||
private LocaleVariableManager localVariables = new LocaleVariableManager();
|
private LocaleVariableManager localVariables = new LocaleVariableManager();
|
||||||
@ -157,7 +155,6 @@ public class ModuleGenerator {
|
|||||||
FunctionName name = new FunctionName( method );
|
FunctionName name = new FunctionName( method );
|
||||||
writeExport( name, method );
|
writeExport( name, method );
|
||||||
writer.writeMethodStart( name );
|
writer.writeMethodStart( name );
|
||||||
writeMethodSignature( method );
|
|
||||||
|
|
||||||
localVariables.reset();
|
localVariables.reset();
|
||||||
branchManager.reset();
|
branchManager.reset();
|
||||||
@ -165,11 +162,12 @@ public class ModuleGenerator {
|
|||||||
byteCode = code.getByteCode();
|
byteCode = code.getByteCode();
|
||||||
writeCode( byteCode, method.getConstantPool() );
|
writeCode( byteCode, method.getConstantPool() );
|
||||||
localVariables.calculate();
|
localVariables.calculate();
|
||||||
|
writeMethodSignature( method );
|
||||||
|
|
||||||
for( WasmInstruction instruction : instructions ) {
|
for( WasmInstruction instruction : instructions ) {
|
||||||
instruction.writeTo( writer );
|
instruction.writeTo( writer );
|
||||||
}
|
}
|
||||||
writer.writeMethodFinish( localVariables.getLocalTypes( paramCount ) );
|
writer.writeMethodFinish();
|
||||||
}
|
}
|
||||||
} catch( Exception ioex ) {
|
} catch( Exception ioex ) {
|
||||||
int lineNumber = byteCode == null ? -1 : byteCode.getLineNumber();
|
int lineNumber = byteCode == null ? -1 : byteCode.getLineNumber();
|
||||||
@ -215,19 +213,20 @@ public class ModuleGenerator {
|
|||||||
int paramCount = 0;
|
int paramCount = 0;
|
||||||
ValueType type = null;
|
ValueType type = null;
|
||||||
for( int i = 1; i < signature.length(); i++ ) {
|
for( int i = 1; i < signature.length(); i++ ) {
|
||||||
paramCount++;
|
|
||||||
if( signature.charAt( i ) == ')' ) {
|
if( signature.charAt( i ) == ')' ) {
|
||||||
this.paramCount = paramCount - 1;
|
|
||||||
kind = "result";
|
kind = "result";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if( kind == "param" ) {
|
||||||
|
paramCount++;
|
||||||
|
}
|
||||||
type = ValueType.getValueType( signature, i );
|
type = ValueType.getValueType( signature, i );
|
||||||
if( type != null ) {
|
if( type != null ) {
|
||||||
writer.writeMethodParam( kind, type );
|
writer.writeMethodParam( kind, type );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.returnType = type;
|
this.returnType = type;
|
||||||
writer.writeMethodParamFinish();
|
writer.writeMethodParamFinish( localVariables.getLocalTypes( paramCount ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -98,21 +98,22 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
/**
|
/**
|
||||||
* Finish the function parameter.
|
* Finish the function parameter.
|
||||||
*
|
*
|
||||||
|
* @param locals
|
||||||
|
* a list with types of local variables
|
||||||
|
*
|
||||||
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
protected abstract void writeMethodParamFinish() throws IOException;
|
protected abstract void writeMethodParamFinish( List<ValueType> locals ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Complete the method
|
* Complete the method
|
||||||
*
|
*
|
||||||
* @param locals
|
|
||||||
* a list with types of local variables
|
|
||||||
*
|
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* 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
|
* Write a constant number value
|
||||||
|
@ -98,11 +98,11 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodStart( FunctionName name ) throws IOException {
|
protected void writeMethodStart( FunctionName name ) throws IOException {
|
||||||
newline( output );
|
|
||||||
output.append( "(func $" );
|
|
||||||
output.append( name.fullName );
|
|
||||||
inset++;
|
|
||||||
methodOutput.setLength( 0 );
|
methodOutput.setLength( 0 );
|
||||||
|
newline( methodOutput );
|
||||||
|
methodOutput.append( "(func $" );
|
||||||
|
methodOutput.append( name.fullName );
|
||||||
|
inset++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,17 +110,21 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodParam( String kind, ValueType valueType ) throws IOException {
|
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}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodParamFinish() throws IOException {
|
protected void writeMethodParamFinish( List<ValueType> locals ) throws IOException {
|
||||||
if( isImport ) {
|
if( isImport ) {
|
||||||
isImport = false;
|
isImport = false;
|
||||||
output.append( "))" );
|
output.append( "))" );
|
||||||
|
} else {
|
||||||
|
for( ValueType valueType : locals ) {
|
||||||
|
methodOutput.append( " (local " ).append( valueType.toString() ).append( ')' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,10 +132,7 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeMethodFinish( List<ValueType> locals ) throws IOException {
|
protected void writeMethodFinish() throws IOException {
|
||||||
for( ValueType valueType : locals ) {
|
|
||||||
output.append( " (local " ).append( valueType.toString() ).append( ')' );
|
|
||||||
}
|
|
||||||
output.append( methodOutput );
|
output.append( methodOutput );
|
||||||
inset--;
|
inset--;
|
||||||
newline( output );
|
newline( output );
|
||||||
@ -174,11 +175,11 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
// declare global variable if not already declared.
|
// declare global variable if not already declared.
|
||||||
output.append( "\n " );
|
output.append( "\n " );
|
||||||
String type = ValueType.getValueType( ref.getType(), 0 ).toString();
|
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 );
|
globals.add( name.fullName );
|
||||||
}
|
}
|
||||||
newline( methodOutput );
|
newline( methodOutput );
|
||||||
methodOutput.append( load ? "get_local " : "set_local " ).append( name.fullName );
|
methodOutput.append( load ? "get_global $" : "set_global $" ).append( name.fullName );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user