implements "pop" instruction code

This commit is contained in:
Volker Berlin 2018-04-02 11:53:12 +02:00
parent 6da8916a75
commit 327d685d32
6 changed files with 50 additions and 38 deletions

View File

@ -15,8 +15,8 @@ Status of the project
* Public API of the Compiler
### Partially Finished
* Binary format file writer (121 of 201 byte code instructions)
* Text format file writer (121 of 201 byte code instructions)
* Binary format file writer (130 of 201 byte code instructions)
* Text format file writer (130 of 201 byte code instructions)
### Open Features
* Exception handling - required the next version of WebAssembly

View File

@ -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");
* 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 );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeReturn() throws IOException {
codeStream.write( RETURN );
}
/**
* {@inheritDoc}
*/
@ -617,6 +609,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
@Override
protected void writeBlockCode( BlockOperator op ) throws IOException {
switch( op ) {
case RETURN:
codeStream.write( RETURN );
break;
case IF:
codeStream.write( IF );
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:
codeStream.write( END );
break;
case DROP:
codeStream.write( DROP );
break;
default:
throw new Error( "Unknown block: " + op );
}

View File

@ -23,8 +23,10 @@ package de.inetsoftware.jwebassembly.module;
*
*/
public enum BlockOperator {
RETURN,
IF,
ELSE,
END,
GOTO,
DROP,
}

View File

@ -367,6 +367,7 @@ public abstract class ModuleWriter implements Closeable {
switch( op ) {
case 0: // nop
return;
//TODO case 1: // aconst_null
case 2: // iconst_m1
case 3: // iconst_0
case 4: // iconst_1
@ -476,6 +477,19 @@ public abstract class ModuleWriter implements Closeable {
case 74: // dstore_3
writeLoadStore( false, ValueType.f64, op - 71 );
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
writeNumericOperator( NumericOperator.add, ValueType.i32);
break;
@ -657,7 +671,7 @@ public abstract class ModuleWriter implements Closeable {
case 174: // freturn
case 175: // dreturn
case 177: // return void
writeReturn();
writeBlockCode( BlockOperator.RETURN );
break;
case 184: // invokestatic
idx = byteCode.readUnsignedShort();
@ -668,6 +682,8 @@ public abstract class ModuleWriter implements Closeable {
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, lineNumber );
}
}
} catch( WasmException ex ) {
throw ex;
} catch( Exception ex ) {
throw WasmException.create( ex, sourceFile, lineNumber );
}
@ -831,14 +847,6 @@ public abstract class ModuleWriter implements Closeable {
*/
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.
*

View File

@ -220,15 +220,6 @@ public class TextModuleWriter extends ModuleWriter {
methodOutput.append( op );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeReturn() throws IOException {
newline( methodOutput );
methodOutput.append( "return" );
}
/**
* Add a newline the insets.
*
@ -258,25 +249,33 @@ public class TextModuleWriter extends ModuleWriter {
*/
@Override
protected void writeBlockCode( BlockOperator op ) throws IOException {
String name;
int insetAfter = 0;
switch( op ) {
case RETURN:
name = "return";
break;
case IF:
newline( methodOutput );
methodOutput.append( "if" );
inset++;
name = "if";
insetAfter++;
break;
case ELSE:
inset--;
newline( methodOutput );
methodOutput.append( "else" );
inset++;
name = "else";
insetAfter++;
break;
case END:
inset--;
newline( methodOutput );
methodOutput.append( "end" );
name = "end";
break;
case DROP:
name = "drop";
break;
default:
throw new Error( "Unknown block: " + op );
}
newline( methodOutput );
methodOutput.append( name );
inset += insetAfter;
}
}

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -54,6 +54,8 @@ public class CallFunctions extends AbstractBaseTest {
@Export
static int intCall() {
intConst();
doubleConst();
return intConst();
}
@ -61,5 +63,8 @@ public class CallFunctions extends AbstractBaseTest {
return -42;
}
static double doubleConst() {
return 3.5;
}
}
}