add "sub" operation

This commit is contained in:
Volker Berlin 2017-04-11 21:12:27 +02:00
parent b62f78087b
commit ac57b04730
5 changed files with 95 additions and 21 deletions

View File

@ -26,6 +26,7 @@ import java.util.Map;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.NumericOperator;
import de.inetsoftware.jwebassembly.module.ValueType;
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
@ -286,21 +287,46 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc}
*/
@Override
protected void writeAdd( @Nullable ValueType valueType ) throws IOException {
switch( valueType ) {
case i32:
codeStream.write( I32_ADD );
protected void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException {
int op = 0;
switch( numOp ) {
case add:
switch( valueType ) {
case i32:
op = I32_ADD;
break;
case i64:
op = I64_ADD;
break;
case f32:
op = F32_ADD;
break;
case f64:
op = F64_ADD;
break;
}
break;
case i64:
codeStream.write( I64_ADD );
break;
case f32:
codeStream.write( F32_ADD );
break;
case f64:
codeStream.write( F64_ADD );
case sub:
switch( valueType ) {
case i32:
op = I32_SUB;
break;
case i64:
op = I64_SUB;
break;
case f32:
op = F32_SUB;
break;
case f64:
op = F64_SUB;
break;
}
break;
}
if( op == 0 ) {
throw new Error();
}
codeStream.write( op );
}
/**

View File

@ -51,14 +51,24 @@ interface InstructionOpcodes {
static final int F64_CONST = 0x44;
// === numerical operations ======
static final int I32_ADD = 0x6A;
static final int I32_SUB = 0x6B;
static final int I64_ADD = 0x7C;
static final int I64_SUB = 0x7D;
static final int F32_ADD = 0x92;
static final int F32_SUB = 0x93;
static final int F64_ADD = 0xA0;
static final int F64_SUB = 0xA1;
// === data type conversions =====
static final int I32_WRAP_I64 = 0xA7;

View File

@ -315,16 +315,28 @@ public abstract class ModuleWriter implements Closeable {
writeLoadStore( false, ValueType.i64, op - 63 );
break;
case 96: // iadd
writeAdd( ValueType.i32);
writeNumericOperator( NumericOperator.add, ValueType.i32);
break;
case 97: // ladd
writeAdd( ValueType.i64 );
writeNumericOperator( NumericOperator.add, ValueType.i64 );
break;
case 98: // fadd
writeAdd( ValueType.f32 );
writeNumericOperator( NumericOperator.add, ValueType.f32 );
break;
case 99: // dadd
writeAdd( ValueType.f64 );
writeNumericOperator( NumericOperator.add, ValueType.f64 );
break;
case 100: // isub
writeNumericOperator( NumericOperator.sub, ValueType.i32 );
break;
case 101: // lsub
writeNumericOperator( NumericOperator.sub, ValueType.i64 );
break;
case 102: // fsub
writeNumericOperator( NumericOperator.sub, ValueType.f32 );
break;
case 103: // dsub
writeNumericOperator( NumericOperator.sub, ValueType.f64 );
break;
case 136: // l2i
writeCast( ValueTypeConvertion.l2i );
@ -463,14 +475,14 @@ public abstract class ModuleWriter implements Closeable {
/**
* Write a add operator
*
* @param numOp TODO
* @param valueType
* the type of the parameters
*
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeAdd( @Nullable ValueType valueType ) throws IOException;
protected abstract void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException;
/**
* Cast a value from one type to another

View File

@ -21,6 +21,7 @@ import java.util.List;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.NumericOperator;
import de.inetsoftware.jwebassembly.module.ValueType;
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
@ -163,9 +164,9 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@Override
protected void writeAdd( @Nullable ValueType valueType ) throws IOException {
protected void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException {
newline( methodOutput );
methodOutput.append( valueType ).append( ".add" );
methodOutput.append( valueType ).append( '.' ).append( numOp );
}
/**

View File

@ -16,7 +16,6 @@
package de.inetsoftware.jwebassembly.math;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.junit.ClassRule;
@ -60,6 +59,10 @@ public class MathOperations {
addParam( list, script, "addLong" );
addParam( list, script, "addFloat", 1F, 3.5F );
addParam( list, script, "addDouble", 1.0, 3.5 );
addParam( list, script, "subInt", 1, 3 );
addParam( list, script, "subLong" );
addParam( list, script, "subFloat", 1F, 3.5F );
addParam( list, script, "subDouble", 1.0, 3.5 );
}
return list;
}
@ -111,5 +114,27 @@ public class MathOperations {
static double addDouble( double a, double b ) {
return a + b;
}
@Export
static int subInt( int a, int b ) {
return a - b;
}
@Export
static int subLong() {
long a = 1L;
long b = 3L;
return (int)(a - b);
}
@Export
static float subFloat( float a, float b ) {
return a - b;
}
@Export
static double subDouble( double a, double b ) {
return a - b;
}
}
}