mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
Add UNREACHABLE also if the branchkmanger adds some blocks instructions
This commit is contained in:
parent
cd2f07733d
commit
fb3ed8795e
@ -26,9 +26,9 @@ import de.inetsoftware.classparser.ConstantClass;
|
|||||||
import de.inetsoftware.classparser.ConstantPool;
|
import de.inetsoftware.classparser.ConstantPool;
|
||||||
import de.inetsoftware.classparser.ConstantRef;
|
import de.inetsoftware.classparser.ConstantRef;
|
||||||
import de.inetsoftware.jwebassembly.WasmException;
|
import de.inetsoftware.jwebassembly.WasmException;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
|
||||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||||
@ -81,11 +81,9 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
|||||||
* if some Java code can't converted
|
* if some Java code can't converted
|
||||||
*/
|
*/
|
||||||
private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, boolean hasReturn ) throws WasmException {
|
private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, boolean hasReturn ) throws WasmException {
|
||||||
boolean endWithReturn = false;
|
|
||||||
try {
|
try {
|
||||||
while( byteCode.available() > 0 ) {
|
while( byteCode.available() > 0 ) {
|
||||||
int codePos = byteCode.getCodePosition();
|
int codePos = byteCode.getCodePosition();
|
||||||
endWithReturn = false;
|
|
||||||
int op = byteCode.readUnsignedByte();
|
int op = byteCode.readUnsignedByte();
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
case 0: // nop
|
case 0: // nop
|
||||||
@ -542,7 +540,6 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
addBlockInstruction( WasmBlockOperator.RETURN, type, codePos );
|
addBlockInstruction( WasmBlockOperator.RETURN, type, codePos );
|
||||||
endWithReturn = true;
|
|
||||||
break;
|
break;
|
||||||
case 178: // getstatic
|
case 178: // getstatic
|
||||||
ConstantRef ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
|
ConstantRef ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
|
||||||
@ -630,8 +627,8 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
|||||||
}
|
}
|
||||||
branchManager.calculate();
|
branchManager.calculate();
|
||||||
branchManager.handle( byteCode ); // add branch operations
|
branchManager.handle( byteCode ); // add branch operations
|
||||||
if( !endWithReturn && hasReturn ) {
|
if( hasReturn && !isEndsWithReturn() ) {
|
||||||
// if a method ends with a loop without a break then code after the loop is no reachable
|
// if a method ends with a loop or block without a break then code after the loop is no reachable
|
||||||
// Java does not need a return byte code in this case
|
// Java does not need a return byte code in this case
|
||||||
// But WebAssembly need the dead code to validate
|
// But WebAssembly need the dead code to validate
|
||||||
addBlockInstruction( WasmBlockOperator.UNREACHABLE, null, byteCode.getCodePosition() );
|
addBlockInstruction( WasmBlockOperator.UNREACHABLE, null, byteCode.getCodePosition() );
|
||||||
|
@ -23,9 +23,10 @@ import javax.annotation.Nonnull;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import de.inetsoftware.classparser.Member;
|
import de.inetsoftware.classparser.Member;
|
||||||
|
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
|
||||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||||
@ -52,6 +53,19 @@ public abstract class WasmCodeBuilder {
|
|||||||
return instructions;
|
return instructions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the last instruction is a return instruction
|
||||||
|
*
|
||||||
|
* @return true, if a return
|
||||||
|
*/
|
||||||
|
boolean isEndsWithReturn() {
|
||||||
|
WasmInstruction instr = instructions.get( instructions.size() - 1 );
|
||||||
|
if( instr.getType() == Type.Block ) {
|
||||||
|
return ((WasmBlockInstruction)instr).getOperation() == WasmBlockOperator.RETURN;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the code builder;
|
* Initialize the code builder;
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user