245 lines
7.1 KiB
Java
Raw Normal View History

/*
* 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.module;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nonnull;
2017-04-09 18:46:27 +02:00
import javax.annotation.Nullable;
import de.inetsoftware.classparser.Member;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
import de.inetsoftware.jwebassembly.wasm.StorageType;
2018-12-05 22:14:26 +01:00
import de.inetsoftware.jwebassembly.wasm.StructOperator;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
/**
* Module Writer base class.
*
* @author Volker Berlin
*/
public abstract class ModuleWriter implements Closeable {
2018-05-30 18:57:36 +02:00
/**
* Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the
* first method.
*/
public void prepareFinish() {
}
/**
* Write a type/struct.
*
* @param typeName
* the name
* @param fields
* the fields
* @return type ID
* @throws IOException
* if any I/O error occur
*/
protected abstract int writeStruct( String typeName, List<NamedStorageType> fields ) throws IOException;
2018-05-30 18:57:36 +02:00
/**
2018-05-31 21:35:01 +02:00
* Prepare a imported single function in the prepare phase.
2018-05-30 18:57:36 +02:00
*
* @param name
* the function name
* @param importModule
* the import module name if it is a import function
* @param importName
* the import name if it is a import function
* @throws IOException
* if any I/O error occur
*/
2018-05-31 21:35:01 +02:00
protected abstract void prepareImport( FunctionName name, String importModule, String importName ) throws IOException;
/**
* Write an export directive
2018-05-21 14:29:32 +02:00
* @param name
* the function name
* @param exportName
* the export name, if null then the same like the method name
*
* @throws IOException
* if any I/O error occur
*/
2018-05-21 14:29:32 +02:00
protected abstract void writeExport( FunctionName name, String exportName ) throws IOException;
/**
* Write the method header.
* @param name
2018-05-21 14:29:32 +02:00
* the function name
*
* @throws IOException
* if any I/O error occur
*/
2018-05-21 14:29:32 +02:00
protected abstract void writeMethodStart( FunctionName name ) throws IOException;
/**
* Write a method parameter.
*
* @param kind
* "param", "result" or "local"
* @param valueType
* the data type of the parameter
* @param name
* optional name of the parameter
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodParam( String kind, StorageType valueType, @Nullable String name ) throws IOException;
/**
* Finish the function parameter.
2018-05-12 10:18:33 +02:00
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodParamFinish() throws IOException;
/**
* Complete the method
*
2017-04-09 18:18:53 +02:00
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeMethodFinish( ) throws IOException;
2017-04-09 12:44:01 +02:00
/**
* Write a constant number value
2017-04-09 12:44:01 +02:00
*
* @param value
* the value
* @param valueType
* the data type of the number
2017-04-09 12:44:01 +02:00
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeConst( Number value, ValueType valueType ) throws IOException;
2017-04-09 12:44:01 +02:00
/**
* Write a variable load.
*
* @param idx
* the index of the parameter variable
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeLoad( int idx ) throws IOException;
/**
* Write a variable store.
*
* @param idx
* the index of the parameter variable
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeStore( int idx ) throws IOException;
/**
* Write a set_global variable
* @param load
* true: if load or GET
* @param name
* the variable name
* @param ref
* the definition of the variable
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeGlobalAccess( boolean load, FunctionName name, Member ref ) throws IOException;
/**
* Write a add operator
*
* @param numOp
* the numeric operation
2017-04-09 18:46:27 +02:00
* @param valueType
* the type of the parameters
*
* @throws IOException
* if any I/O error occur
*/
2017-04-11 21:12:27 +02:00
protected abstract void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException;
/**
* Cast a value from one type to another
*
* @param cast
* the operator
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeCast( ValueTypeConvertion cast ) throws IOException;
/**
* Write a call to a function.
*
* @param name
2018-11-24 16:14:52 +01:00
* the function name
* @throws IOException
* if any I/O error occur
*/
2018-11-24 16:14:52 +01:00
protected abstract void writeFunctionCall( FunctionName name ) throws IOException;
2018-03-25 12:57:04 +02:00
/**
* Write a block/branch code
*
* @param op
* the operation
* @param data
* extra data depending of the operator
2018-03-25 12:57:04 +02:00
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object data ) throws IOException;
2018-12-02 19:54:59 +01:00
/**
* Write an array operation.
*
* @param op
* the operation
* @param type
2018-12-02 19:54:59 +01:00
* the resulting type
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeArrayOperator( @Nonnull ArrayOperator op, StorageType type ) throws IOException;
2018-12-05 22:14:26 +01:00
/**
* Write a struct operation
*
* @param op
* the operation
* @param type
* the type of the struct
2019-01-13 11:36:07 +01:00
* @param fieldName TODO
2018-12-05 22:14:26 +01:00
* @throws IOException
* if any I/O error occur
*/
2019-01-13 11:36:07 +01:00
protected abstract void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException;
}