From bab58acebed7d716f2b1dbd950b4239b393d7b61 Mon Sep 17 00:00:00 2001 From: Volker Date: Sun, 15 Jul 2018 18:06:25 +0200 Subject: [PATCH] add branch operation after the creating the in memory model and not on parsing. --- .../jwebassembly/module/BranchManger.java | 35 +++++++++++++------ .../jwebassembly/module/ModuleGenerator.java | 4 +-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index a013058..83eb5cd 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -16,7 +16,6 @@ */ package de.inetsoftware.jwebassembly.module; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -441,7 +440,17 @@ class BranchManger { * the byte code stream */ void handle( CodeInputStream byteCode ) { - root.handle( byteCode.getCodePosition(), instructions ); + int codePosition = -1; + for( int idx = 0; idx < instructions.size(); idx++ ) { + int nextCodePosition = instructions.get( idx ).getCodePosition(); + if( nextCodePosition <= codePosition ) { + continue; + } else { + codePosition = nextCodePosition; + } + idx = root.handle( codePosition, instructions, idx ); + } + root.handle( byteCode.getCodePosition(), instructions, instructions.size() ); } /** @@ -549,24 +558,28 @@ class BranchManger { /** * Handle branches on the current codePosition * - * @param codePositions + * @param codePosition * current code position * @param instructions * the target for instructions + * @param idx + * index in the current instruction + * @return the new index in the instructions */ - void handle( int codePositions, List instructions ) { - if( codePositions < startPos || codePositions > endPos ) { - return; + int handle( int codePosition, List instructions, int idx ) { + if( codePosition < startPos || codePosition > endPos ) { + return idx; } - if( codePositions == startPos && startOp != null ) { - instructions.add( new WasmBlockInstruction( startOp, data, codePositions ) ); + if( codePosition == startPos && startOp != null ) { + instructions.add( idx++, new WasmBlockInstruction( startOp, data, codePosition ) ); } for( BranchNode branch : this ) { - branch.handle( codePositions, instructions ); + idx = branch.handle( codePosition, instructions, idx ); } - if( codePositions == endPos && endOp != null ) { - instructions.add( new WasmBlockInstruction( endOp, null, codePositions ) ); + if( codePosition == endPos && endOp != null ) { + instructions.add( idx++, new WasmBlockInstruction( endOp, null, codePosition ) ); } + return idx; } } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 9c4ef3a..9f161e9 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -530,7 +530,6 @@ public class ModuleGenerator { while( byteCode.available() > 0 ) { WasmInstruction instr = null; int codePos = byteCode.getCodePosition(); - branchManager.handle( byteCode ); endWithReturn = false; int op = byteCode.readUnsignedByte(); switch( op ) { @@ -879,6 +878,7 @@ public class ModuleGenerator { //TODO case 165: // if_acmpeq //TODO case 166: // if_acmpne case 167: // goto + instr = new WasmNopInstruction( codePos ); // marker of the line number for the branch manager byteCode.skip(2); // handle in the branch manager break; case 170: // tableswitch @@ -905,7 +905,7 @@ public class ModuleGenerator { instructions.add( instr ); } } - branchManager.handle( byteCode ); // write the last end operators + branchManager.handle( byteCode ); // add branch operations if( !endWithReturn && returnType != null ) { // 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