From d1b4e6af5a9845c31ae891c4cfd373fcf1cbac5c Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 22 May 2022 19:10:34 +0200 Subject: [PATCH] add params to findBlockStart() --- .../jwebassembly/module/WasmCodeBuilder.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 5f5ca100..168c0063 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -157,8 +157,8 @@ boolean isEndsWithReturn() { } /** - * We need one value from the stack inside of a block. We need to find the WasmInstruction on which the block can - * start. If this a function call or numeric expression this can be complex to find the right point. + * We need a value (or several) from the stack inside a block. We need to find the WasmInstruction where the block + * can can begin. If it is a function call or a numeric expression, it can be complicated to find the right point. * * @param count * the count of values on the stack back. 1 means the last value. 2 means the penultimate value. @@ -169,8 +169,8 @@ int findBlockStartCodePosition( int count ) { } /** - * We need one value from the stack inside of a block. We need to find the WasmInstruction on which the block can - * start. If this a function call or numeric expression this can be complex to find the right point. + * We need a value (or several) from the stack inside a block. We need to find the WasmInstruction where the block + * can can begin. If it is a function call or a numeric expression, it can be complicated to find the right point. * * @param count * the count of values on the stack back. 1 means the last value. 2 means the penultimate value. @@ -179,9 +179,26 @@ int findBlockStartCodePosition( int count ) { * @return the code position that push the last instruction */ private int findBlockStart( int count, boolean codePosition ) { + return findBlockStart( count, codePosition, instructions, instructions.size() ); + } + + /** + * We need a value (or several) from the stack inside a block. We need to find the WasmInstruction where the block + * can can begin. If it is a function call or a numeric expression, it can be complicated to find the right point. + * + * @param count + * the count of values on the stack back. 1 means the last value. 2 means the penultimate value. + * @param codePosition + * true, get the code position; false, get the index in the instructions + * @param instructions + * the instruction list for searching + * @param idx + * the start index for the search. Between 0 and instructions.size(). + * @return the code position that push the last instruction + */ + static int findBlockStart( int count, boolean codePosition, List instructions, int idx ) { int valueCount = 0; - List instructions = this.instructions; - for( int i = instructions.size() - 1; i >= 0; i-- ) { + for( int i = idx - 1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); AnyType valueType = instr.getPushValueType(); if( valueType != null ) { @@ -192,7 +209,7 @@ private int findBlockStart( int count, boolean codePosition ) { return codePosition ? instr.getCodePosition() : i; } } - throw new WasmException( "Start position not found", -1 ); // should never occur + throw new WasmException( "Block start position not found", -1 ); // should never occur } /**