mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
implements "pop" instruction code
This commit is contained in:
parent
6da8916a75
commit
327d685d32
@ -15,8 +15,8 @@ Status of the project
|
|||||||
* Public API of the Compiler
|
* Public API of the Compiler
|
||||||
|
|
||||||
### Partially Finished
|
### Partially Finished
|
||||||
* Binary format file writer (121 of 201 byte code instructions)
|
* Binary format file writer (130 of 201 byte code instructions)
|
||||||
* Text format file writer (121 of 201 byte code instructions)
|
* Text format file writer (130 of 201 byte code instructions)
|
||||||
|
|
||||||
### Open Features
|
### Open Features
|
||||||
* Exception handling - required the next version of WebAssembly
|
* Exception handling - required the next version of WebAssembly
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 Volker Berlin (i-net software)
|
* Copyright 2017 - 2018 Volker Berlin (i-net software)
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -590,14 +590,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
codeStream.write( op );
|
codeStream.write( op );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void writeReturn() throws IOException {
|
|
||||||
codeStream.write( RETURN );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@ -617,6 +609,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
@Override
|
@Override
|
||||||
protected void writeBlockCode( BlockOperator op ) throws IOException {
|
protected void writeBlockCode( BlockOperator op ) throws IOException {
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
|
case RETURN:
|
||||||
|
codeStream.write( RETURN );
|
||||||
|
break;
|
||||||
case IF:
|
case IF:
|
||||||
codeStream.write( IF );
|
codeStream.write( IF );
|
||||||
codeStream.write( 0x40 ); // void; the return type of the block. currently we does not use it
|
codeStream.write( 0x40 ); // void; the return type of the block. currently we does not use it
|
||||||
@ -627,6 +622,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
case END:
|
case END:
|
||||||
codeStream.write( END );
|
codeStream.write( END );
|
||||||
break;
|
break;
|
||||||
|
case DROP:
|
||||||
|
codeStream.write( DROP );
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error( "Unknown block: " + op );
|
throw new Error( "Unknown block: " + op );
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,10 @@ package de.inetsoftware.jwebassembly.module;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public enum BlockOperator {
|
public enum BlockOperator {
|
||||||
|
RETURN,
|
||||||
IF,
|
IF,
|
||||||
ELSE,
|
ELSE,
|
||||||
END,
|
END,
|
||||||
GOTO,
|
GOTO,
|
||||||
|
DROP,
|
||||||
}
|
}
|
||||||
|
@ -367,6 +367,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
switch( op ) {
|
switch( op ) {
|
||||||
case 0: // nop
|
case 0: // nop
|
||||||
return;
|
return;
|
||||||
|
//TODO case 1: // aconst_null
|
||||||
case 2: // iconst_m1
|
case 2: // iconst_m1
|
||||||
case 3: // iconst_0
|
case 3: // iconst_0
|
||||||
case 4: // iconst_1
|
case 4: // iconst_1
|
||||||
@ -476,6 +477,19 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
case 74: // dstore_3
|
case 74: // dstore_3
|
||||||
writeLoadStore( false, ValueType.f64, op - 71 );
|
writeLoadStore( false, ValueType.f64, op - 71 );
|
||||||
break;
|
break;
|
||||||
|
case 87: // pop
|
||||||
|
case 88: // pop2
|
||||||
|
writeBlockCode( BlockOperator.DROP );
|
||||||
|
break;
|
||||||
|
case 89: // dup: duplicate the value on top of the stack
|
||||||
|
case 90: // dup_x1
|
||||||
|
case 91: // dup_x2
|
||||||
|
case 92: // dup2
|
||||||
|
case 93: // dup2_x1
|
||||||
|
case 94: // dup2_x2
|
||||||
|
case 95: // swap
|
||||||
|
// can be do with functions with more as one return value in future WASM standard
|
||||||
|
throw new WasmException( "Stack duplicate is not supported in current WASM. try to save immediate values in a local variable: " + op, sourceFile, lineNumber );
|
||||||
case 96: // iadd
|
case 96: // iadd
|
||||||
writeNumericOperator( NumericOperator.add, ValueType.i32);
|
writeNumericOperator( NumericOperator.add, ValueType.i32);
|
||||||
break;
|
break;
|
||||||
@ -657,7 +671,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
case 174: // freturn
|
case 174: // freturn
|
||||||
case 175: // dreturn
|
case 175: // dreturn
|
||||||
case 177: // return void
|
case 177: // return void
|
||||||
writeReturn();
|
writeBlockCode( BlockOperator.RETURN );
|
||||||
break;
|
break;
|
||||||
case 184: // invokestatic
|
case 184: // invokestatic
|
||||||
idx = byteCode.readUnsignedShort();
|
idx = byteCode.readUnsignedShort();
|
||||||
@ -668,6 +682,8 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, lineNumber );
|
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, lineNumber );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch( WasmException ex ) {
|
||||||
|
throw ex;
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throw WasmException.create( ex, sourceFile, lineNumber );
|
throw WasmException.create( ex, sourceFile, lineNumber );
|
||||||
}
|
}
|
||||||
@ -831,14 +847,6 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
*/
|
*/
|
||||||
protected abstract void writeCast( ValueTypeConvertion cast ) throws IOException;
|
protected abstract void writeCast( ValueTypeConvertion cast ) throws IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Write a return
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* if any I/O error occur
|
|
||||||
*/
|
|
||||||
protected abstract void writeReturn() throws IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a call to a function.
|
* Write a call to a function.
|
||||||
*
|
*
|
||||||
|
@ -220,15 +220,6 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
methodOutput.append( op );
|
methodOutput.append( op );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void writeReturn() throws IOException {
|
|
||||||
newline( methodOutput );
|
|
||||||
methodOutput.append( "return" );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a newline the insets.
|
* Add a newline the insets.
|
||||||
*
|
*
|
||||||
@ -258,25 +249,33 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void writeBlockCode( BlockOperator op ) throws IOException {
|
protected void writeBlockCode( BlockOperator op ) throws IOException {
|
||||||
|
String name;
|
||||||
|
int insetAfter = 0;
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
|
case RETURN:
|
||||||
|
name = "return";
|
||||||
|
break;
|
||||||
case IF:
|
case IF:
|
||||||
newline( methodOutput );
|
name = "if";
|
||||||
methodOutput.append( "if" );
|
insetAfter++;
|
||||||
inset++;
|
|
||||||
break;
|
break;
|
||||||
case ELSE:
|
case ELSE:
|
||||||
inset--;
|
inset--;
|
||||||
newline( methodOutput );
|
name = "else";
|
||||||
methodOutput.append( "else" );
|
insetAfter++;
|
||||||
inset++;
|
|
||||||
break;
|
break;
|
||||||
case END:
|
case END:
|
||||||
inset--;
|
inset--;
|
||||||
newline( methodOutput );
|
name = "end";
|
||||||
methodOutput.append( "end" );
|
break;
|
||||||
|
case DROP:
|
||||||
|
name = "drop";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error( "Unknown block: " + op );
|
throw new Error( "Unknown block: " + op );
|
||||||
}
|
}
|
||||||
|
newline( methodOutput );
|
||||||
|
methodOutput.append( name );
|
||||||
|
inset += insetAfter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 Volker Berlin (i-net software)
|
* Copyright 2017 - 2018 Volker Berlin (i-net software)
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -54,6 +54,8 @@ public class CallFunctions extends AbstractBaseTest {
|
|||||||
|
|
||||||
@Export
|
@Export
|
||||||
static int intCall() {
|
static int intCall() {
|
||||||
|
intConst();
|
||||||
|
doubleConst();
|
||||||
return intConst();
|
return intConst();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,5 +63,8 @@ public class CallFunctions extends AbstractBaseTest {
|
|||||||
return -42;
|
return -42;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static double doubleConst() {
|
||||||
|
return 3.5;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user