From a28d966b942b85081d779a8a879ec86bbdbc48d8 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 13 Feb 2022 22:38:14 +0100 Subject: [PATCH] Use variable slot instead variable index for duplicate of THIS of on virtual calls --- .../jwebassembly/module/DupThis.java | 19 ++++++++++++------- .../module/WasmCallIndirectInstruction.java | 19 ++++++++++++------- .../jwebassembly/module/WasmCodeBuilder.java | 12 ++++++------ 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/DupThis.java b/src/de/inetsoftware/jwebassembly/module/DupThis.java index 6a0bb8b..63df7b8 100644 --- a/src/de/inetsoftware/jwebassembly/module/DupThis.java +++ b/src/de/inetsoftware/jwebassembly/module/DupThis.java @@ -1,5 +1,5 @@ /* - Copyright 2019 - 2021 Volker Berlin (i-net software) + Copyright 2019 - 2022 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. @@ -30,22 +30,27 @@ class DupThis extends WasmInstruction { private WasmCallIndirectInstruction virtualCall; - private int tempVarIdx; + private int tempVarSlot; + + private LocaleVariableManager localVariables; /** * Create a instance. * * @param virtualCall * the related virtual function call. - * @param tempVarIdx - * the index of the temporary variable + * @param tempVarSlot + * the slot of the temporary variable + * @param localVariables + * the manager for local variables * @param javaCodePos * the code position */ - DupThis( WasmCallIndirectInstruction virtualCall, int tempVarIdx, int javaCodePos ) { + DupThis( WasmCallIndirectInstruction virtualCall, int tempVarSlot, LocaleVariableManager localVariables, int javaCodePos ) { super( javaCodePos, virtualCall.getLineNumber() ); this.virtualCall = virtualCall; - this.tempVarIdx = tempVarIdx; + this.tempVarSlot = tempVarSlot; + this.localVariables = localVariables; } /** @@ -62,7 +67,7 @@ class DupThis extends WasmInstruction { @Override void writeTo( ModuleWriter writer ) throws IOException { if( virtualCall.isVirtual() ) { - writer.writeLocal( VariableOperator.tee, tempVarIdx ); + writer.writeLocal( VariableOperator.tee, localVariables.get( tempVarSlot, getCodePosition() ) ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java index 2f80142..e7c57c0 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java @@ -1,5 +1,5 @@ /* - Copyright 2019 - 2020 Volker Berlin (i-net software) + Copyright 2019 - 2022 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. @@ -28,7 +28,9 @@ abstract class WasmCallIndirectInstruction extends WasmCallInstruction { private final StructType type; - private int tempVarIdx; + private int tempVarSlot; + + private LocaleVariableManager localVariables; /** * Create an instance of a function call instruction @@ -57,11 +59,14 @@ abstract class WasmCallIndirectInstruction extends WasmCallInstruction { } /** - * Set the variable index on which this can be found. - * @param tempVarIdx the index + * Set the Java variable slot on which THIS can be found. + * @param tempVarSlot the slot + * @param localVariables + * the manager for local variables to resolve the index */ - void setVariableIndexOfThis( int tempVarIdx ) { - this.tempVarIdx = tempVarIdx; + void setVariableSlotOfThis( int tempVarSlot, LocaleVariableManager localVariables ) { + this.tempVarSlot = tempVarSlot; + this.localVariables = localVariables; } /** @@ -70,7 +75,7 @@ abstract class WasmCallIndirectInstruction extends WasmCallInstruction { * @return the index of the variable */ int getVariableIndexOfThis() { - return tempVarIdx; + return localVariables.get( tempVarSlot, getCodePosition() ); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index dc8b853..d75e0c9 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -676,21 +676,21 @@ public abstract class WasmCodeBuilder { int javaCodePos = indirectCall.getCodePosition(); StackValue stackValue = StackInspector.findInstructionThatPushValue( instructions, count, javaCodePos ); WasmInstruction instr = stackValue.instr; - int varIndex = -1; + int varSlot = -1; // if it is a GET to a local variable then we can use it if( instr.getType() == Type.Local ) { WasmLocalInstruction local1 = (WasmLocalInstruction)instr; if( local1.getOperator() == VariableOperator.get ) { - varIndex = local1.getIndex(); + varSlot = local1.getSlot(); } } //alternate we need to create a new locale variable - if( varIndex < 0 ) { - varIndex = getTempVariable( indirectCall.getThisType(), instr.getCodePosition(), javaCodePos + 1 ); + if( varSlot < 0 ) { + varSlot = getTempVariable( indirectCall.getThisType(), instr.getCodePosition(), javaCodePos + 1 ); int idx = count == 1 ? instructions.size() : stackValue.idx + 1; - instructions.add( idx, new DupThis( indirectCall, varIndex, instr.getCodePosition() + 1 ) ); + instructions.add( idx, new DupThis( indirectCall, varSlot, localVariables, instr.getCodePosition() + 1 ) ); } - indirectCall.setVariableIndexOfThis( varIndex ); + indirectCall.setVariableSlotOfThis( varSlot, localVariables ); instructions.add( indirectCall ); options.registerGet_i32(); // for later access of the vtable }