From 327d685d32048ed5cb37c666918d0bedb0a06347 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Mon, 2 Apr 2018 11:53:12 +0200 Subject: [PATCH] implements "pop" instruction code --- README.md | 4 +-- .../binary/BinaryModuleWriter.java | 16 ++++----- .../jwebassembly/module/BlockOperator.java | 2 ++ .../jwebassembly/module/ModuleWriter.java | 26 ++++++++++----- .../jwebassembly/text/TextModuleWriter.java | 33 +++++++++---------- .../jwebassembly/runtime/CallFunctions.java | 7 +++- 6 files changed, 50 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 8d54f6a..233222e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index dd9834d..c50fb0f 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -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 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/BlockOperator.java b/src/de/inetsoftware/jwebassembly/module/BlockOperator.java index d4b6fd2..15efdbe 100644 --- a/src/de/inetsoftware/jwebassembly/module/BlockOperator.java +++ b/src/de/inetsoftware/jwebassembly/module/BlockOperator.java @@ -23,8 +23,10 @@ package de.inetsoftware.jwebassembly.module; * */ public enum BlockOperator { + RETURN, IF, ELSE, END, GOTO, + DROP, } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index cf6e23f..d545f44 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -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. * diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 24687b1..2f3bfe3 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -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; } } diff --git a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java index 5f43ac1..eecc60d 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java @@ -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; + } } }