Generalize the usage of SyntheticFuctionName

This commit is contained in:
Volker Berlin 2019-04-20 18:44:23 +02:00
parent 04b3b67927
commit 09b817117a
4 changed files with 63 additions and 15 deletions

View File

@ -352,11 +352,11 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
break;
case 114: // frem
//helper function like: (a - (int)(a / b) * (float)b)
addCallInstruction( new SyntheticFunctionName( "frem", "local.get 0 local.get 0 local.get 1 f32.div i32.trunc_sat_f32_s f32.convert_i32_s local.get 1 f32.mul f32.sub return", ValueType.f32, ValueType.f32, null, ValueType.f32 ), codePos, lineNumber );
addCallInstruction( new WatCodeSyntheticFunctionName( "frem", "local.get 0 local.get 0 local.get 1 f32.div i32.trunc_sat_f32_s f32.convert_i32_s local.get 1 f32.mul f32.sub return", ValueType.f32, ValueType.f32, null, ValueType.f32 ), codePos, lineNumber );
break;
case 115: // drem
//helper function like: (a - (long)(a / b) * (double)b)
addCallInstruction( new SyntheticFunctionName( "drem", "local.get 0 local.get 0 local.get 1 f64.div i64.trunc_sat_f64_s f64.convert_i64_s local.get 1 f64.mul f64.sub return", ValueType.f64, ValueType.f64, null, ValueType.f64 ), codePos, lineNumber );
addCallInstruction( new WatCodeSyntheticFunctionName( "drem", "local.get 0 local.get 0 local.get 1 f64.div i64.trunc_sat_f64_s f64.convert_i64_s local.get 1 f64.mul f64.sub return", ValueType.f64, ValueType.f64, null, ValueType.f64 ), codePos, lineNumber );
break;
case 116: // ineg
addConstInstruction( -1, ValueType.i32, codePos, lineNumber );

View File

@ -164,8 +164,7 @@ public class ModuleGenerator {
InputStream stream = libraries.getResourceAsStream( next.className + ".class" );
if( stream == null ) {
if( next instanceof SyntheticFunctionName ) {
watParser.parse( ((SyntheticFunctionName)next).getCode(), -1 );
writeMethodImpl( next, true, watParser );
writeMethodImpl( next, true, ((SyntheticFunctionName)next).getCodeBuilder( watParser ) );
} else {
throw new WasmException( "Missing function: " + next.signatureName, -1 );
}

View File

@ -20,32 +20,28 @@ import java.util.Arrays;
import java.util.Iterator;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.watparser.WatParser;
/**
* Synthetic/dynamic method.
*
* @author Volker Berlin
*/
public class SyntheticFunctionName extends FunctionName {
abstract class SyntheticFunctionName extends FunctionName {
private final AnyType[] signature;
private final String code;
/**
* Create a new instance.
*
* @param name
* the function name
* @param code
* the WAT code (WASM in text form)
* @param signature
* the method signature, first the parameters, then null and the the return types
*/
public SyntheticFunctionName( String name, String code, AnyType... signature ) {
public SyntheticFunctionName( String name, AnyType... signature ) {
super( "", name, "()V" ); //TODO better signature name
this.signature = signature;
this.code = code;
}
/**
@ -57,11 +53,10 @@ public class SyntheticFunctionName extends FunctionName {
}
/**
* Get the WAT code (WASM in text form)
* Get the WasmCodeBuilder.
*
* @param watParser a helping WatParser
* @return the code
*/
public String getCode() {
return code;
}
abstract WasmCodeBuilder getCodeBuilder( WatParser watParser );
}

View File

@ -0,0 +1,54 @@
/*
Copyright 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 de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.watparser.WatParser;
/**
* Synthetic/dynamic method based on WAT code (WASM in text form).
*
* @author Volker Berlin
*/
class WatCodeSyntheticFunctionName extends SyntheticFunctionName {
private final String code;
/**
* Create a new instance.
*
* @param name
* the function name
* @param code
* the WAT code (WASM in text form)
* @param signature
* the method signature, first the parameters, then null and the the return types
*/
public WatCodeSyntheticFunctionName( String name, String code, AnyType... signature ) {
super( name, signature );
this.code = code;
}
/**
* {@inheritDoc}
*/
@Override
public WasmCodeBuilder getCodeBuilder( WatParser watParser ) {
watParser.parse( code, -1 );
return watParser;
}
}