Use UNREACHABLE operation after endless loop.

This commit is contained in:
Volker 2018-07-27 17:51:36 +02:00
parent bab58acebe
commit 776cf133bc
5 changed files with 10 additions and 16 deletions

View File

@ -727,6 +727,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
codeStream.write( LOOP );
codeStream.write( 0x40 ); // void; the return type of the loop. currently we does not use it
break;
case UNREACHABLE:
codeStream.write( UNREACHABLE );
break;
default:
throw new Error( "Unknown block: " + op );
}

View File

@ -25,6 +25,8 @@ package de.inetsoftware.jwebassembly.binary;
*/
interface InstructionOpcodes {
static final int UNREACHABLE = 0x00;
static final int NOP = 0x01;
// === Control flow operators ====

View File

@ -910,22 +910,7 @@ public class ModuleGenerator {
// if a method ends with a loop without a break then code after the loop is no reachable
// Java does not need a return byte code in this case
// But WebAssembly need the dead code to validate
switch( returnType ) {
case i32:
instructions.add( new WasmConstInstruction( 0, byteCode.getCodePosition() ) );
break;
case i64:
instructions.add( new WasmConstInstruction( 0L, byteCode.getCodePosition() ) );
break;
case f32:
instructions.add( new WasmConstInstruction( 0F, byteCode.getCodePosition() ) );
break;
case f64:
instructions.add( new WasmConstInstruction( 0D, byteCode.getCodePosition() ) );
break;
default:
}
instructions.add( new WasmBlockInstruction( WasmBlockOperator.RETURN, null, byteCode.getCodePosition() ) );
instructions.add( new WasmBlockInstruction( WasmBlockOperator.UNREACHABLE, null, byteCode.getCodePosition() ) );
}
} catch( WasmException ex ) {
throw ex;

View File

@ -33,4 +33,5 @@ public enum WasmBlockOperator {
BR_IF,
BR_TABLE,
LOOP,
UNREACHABLE,
}

View File

@ -311,6 +311,9 @@ public class TextModuleWriter extends ModuleWriter {
name = "loop";
insetAfter++;
break;
case UNREACHABLE:
name = "unreachable";
break;
default:
throw new Error( "Unknown block: " + op );
}