mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
add "sub" operation
This commit is contained in:
parent
b62f78087b
commit
ac57b04730
@ -26,6 +26,7 @@ import java.util.Map;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||||
|
import de.inetsoftware.jwebassembly.module.NumericOperator;
|
||||||
import de.inetsoftware.jwebassembly.module.ValueType;
|
import de.inetsoftware.jwebassembly.module.ValueType;
|
||||||
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
||||||
|
|
||||||
@ -286,21 +287,46 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeAdd( @Nullable ValueType valueType ) throws IOException {
|
protected void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException {
|
||||||
switch( valueType ) {
|
int op = 0;
|
||||||
case i32:
|
switch( numOp ) {
|
||||||
codeStream.write( I32_ADD );
|
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;
|
break;
|
||||||
case i64:
|
case sub:
|
||||||
codeStream.write( I64_ADD );
|
switch( valueType ) {
|
||||||
break;
|
case i32:
|
||||||
case f32:
|
op = I32_SUB;
|
||||||
codeStream.write( F32_ADD );
|
break;
|
||||||
break;
|
case i64:
|
||||||
case f64:
|
op = I64_SUB;
|
||||||
codeStream.write( F64_ADD );
|
break;
|
||||||
|
case f32:
|
||||||
|
op = F32_SUB;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_SUB;
|
||||||
|
break;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if( op == 0 ) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
codeStream.write( op );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,14 +51,24 @@ interface InstructionOpcodes {
|
|||||||
|
|
||||||
static final int F64_CONST = 0x44;
|
static final int F64_CONST = 0x44;
|
||||||
|
|
||||||
|
// === numerical operations ======
|
||||||
|
|
||||||
static final int I32_ADD = 0x6A;
|
static final int I32_ADD = 0x6A;
|
||||||
|
|
||||||
|
static final int I32_SUB = 0x6B;
|
||||||
|
|
||||||
static final int I64_ADD = 0x7C;
|
static final int I64_ADD = 0x7C;
|
||||||
|
|
||||||
|
static final int I64_SUB = 0x7D;
|
||||||
|
|
||||||
static final int F32_ADD = 0x92;
|
static final int F32_ADD = 0x92;
|
||||||
|
|
||||||
|
static final int F32_SUB = 0x93;
|
||||||
|
|
||||||
static final int F64_ADD = 0xA0;
|
static final int F64_ADD = 0xA0;
|
||||||
|
|
||||||
|
static final int F64_SUB = 0xA1;
|
||||||
|
|
||||||
// === data type conversions =====
|
// === data type conversions =====
|
||||||
|
|
||||||
static final int I32_WRAP_I64 = 0xA7;
|
static final int I32_WRAP_I64 = 0xA7;
|
||||||
|
@ -315,16 +315,28 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
writeLoadStore( false, ValueType.i64, op - 63 );
|
writeLoadStore( false, ValueType.i64, op - 63 );
|
||||||
break;
|
break;
|
||||||
case 96: // iadd
|
case 96: // iadd
|
||||||
writeAdd( ValueType.i32);
|
writeNumericOperator( NumericOperator.add, ValueType.i32);
|
||||||
break;
|
break;
|
||||||
case 97: // ladd
|
case 97: // ladd
|
||||||
writeAdd( ValueType.i64 );
|
writeNumericOperator( NumericOperator.add, ValueType.i64 );
|
||||||
break;
|
break;
|
||||||
case 98: // fadd
|
case 98: // fadd
|
||||||
writeAdd( ValueType.f32 );
|
writeNumericOperator( NumericOperator.add, ValueType.f32 );
|
||||||
break;
|
break;
|
||||||
case 99: // dadd
|
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;
|
break;
|
||||||
case 136: // l2i
|
case 136: // l2i
|
||||||
writeCast( ValueTypeConvertion.l2i );
|
writeCast( ValueTypeConvertion.l2i );
|
||||||
@ -463,14 +475,14 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a add operator
|
* Write a add operator
|
||||||
*
|
* @param numOp TODO
|
||||||
* @param valueType
|
* @param valueType
|
||||||
* the type of the parameters
|
* the type of the parameters
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* 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
|
* Cast a value from one type to another
|
||||||
|
@ -21,6 +21,7 @@ import java.util.List;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||||
|
import de.inetsoftware.jwebassembly.module.NumericOperator;
|
||||||
import de.inetsoftware.jwebassembly.module.ValueType;
|
import de.inetsoftware.jwebassembly.module.ValueType;
|
||||||
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
||||||
|
|
||||||
@ -163,9 +164,9 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeAdd( @Nullable ValueType valueType ) throws IOException {
|
protected void writeNumericOperator( NumericOperator numOp, @Nullable ValueType valueType ) throws IOException {
|
||||||
newline( methodOutput );
|
newline( methodOutput );
|
||||||
methodOutput.append( valueType ).append( ".add" );
|
methodOutput.append( valueType ).append( '.' ).append( numOp );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
package de.inetsoftware.jwebassembly.math;
|
package de.inetsoftware.jwebassembly.math;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
@ -60,6 +59,10 @@ public class MathOperations {
|
|||||||
addParam( list, script, "addLong" );
|
addParam( list, script, "addLong" );
|
||||||
addParam( list, script, "addFloat", 1F, 3.5F );
|
addParam( list, script, "addFloat", 1F, 3.5F );
|
||||||
addParam( list, script, "addDouble", 1.0, 3.5 );
|
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;
|
return list;
|
||||||
}
|
}
|
||||||
@ -111,5 +114,27 @@ public class MathOperations {
|
|||||||
static double addDouble( double a, double b ) {
|
static double addDouble( double a, double b ) {
|
||||||
return a + 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user