Use variable slot instead variable index for duplicate of THIS of on virtual calls

This commit is contained in:
Volker Berlin 2022-02-13 22:38:14 +01:00
parent 1f045f312d
commit a28d966b94
3 changed files with 30 additions and 20 deletions

View File

@ -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"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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 WasmCallIndirectInstruction virtualCall;
private int tempVarIdx; private int tempVarSlot;
private LocaleVariableManager localVariables;
/** /**
* Create a instance. * Create a instance.
* *
* @param virtualCall * @param virtualCall
* the related virtual function call. * the related virtual function call.
* @param tempVarIdx * @param tempVarSlot
* the index of the temporary variable * the slot of the temporary variable
* @param localVariables
* the manager for local variables
* @param javaCodePos * @param javaCodePos
* the code position * the code position
*/ */
DupThis( WasmCallIndirectInstruction virtualCall, int tempVarIdx, int javaCodePos ) { DupThis( WasmCallIndirectInstruction virtualCall, int tempVarSlot, LocaleVariableManager localVariables, int javaCodePos ) {
super( javaCodePos, virtualCall.getLineNumber() ); super( javaCodePos, virtualCall.getLineNumber() );
this.virtualCall = virtualCall; this.virtualCall = virtualCall;
this.tempVarIdx = tempVarIdx; this.tempVarSlot = tempVarSlot;
this.localVariables = localVariables;
} }
/** /**
@ -62,7 +67,7 @@ class DupThis extends WasmInstruction {
@Override @Override
void writeTo( ModuleWriter writer ) throws IOException { void writeTo( ModuleWriter writer ) throws IOException {
if( virtualCall.isVirtual() ) { if( virtualCall.isVirtual() ) {
writer.writeLocal( VariableOperator.tee, tempVarIdx ); writer.writeLocal( VariableOperator.tee, localVariables.get( tempVarSlot, getCodePosition() ) );
} }
} }

View File

@ -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"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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 final StructType type;
private int tempVarIdx; private int tempVarSlot;
private LocaleVariableManager localVariables;
/** /**
* Create an instance of a function call instruction * 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. * Set the Java variable slot on which THIS can be found.
* @param tempVarIdx the index * @param tempVarSlot the slot
* @param localVariables
* the manager for local variables to resolve the index
*/ */
void setVariableIndexOfThis( int tempVarIdx ) { void setVariableSlotOfThis( int tempVarSlot, LocaleVariableManager localVariables ) {
this.tempVarIdx = tempVarIdx; this.tempVarSlot = tempVarSlot;
this.localVariables = localVariables;
} }
/** /**
@ -70,7 +75,7 @@ abstract class WasmCallIndirectInstruction extends WasmCallInstruction {
* @return the index of the variable * @return the index of the variable
*/ */
int getVariableIndexOfThis() { int getVariableIndexOfThis() {
return tempVarIdx; return localVariables.get( tempVarSlot, getCodePosition() );
} }
/** /**

View File

@ -676,21 +676,21 @@ public abstract class WasmCodeBuilder {
int javaCodePos = indirectCall.getCodePosition(); int javaCodePos = indirectCall.getCodePosition();
StackValue stackValue = StackInspector.findInstructionThatPushValue( instructions, count, javaCodePos ); StackValue stackValue = StackInspector.findInstructionThatPushValue( instructions, count, javaCodePos );
WasmInstruction instr = stackValue.instr; 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 it is a GET to a local variable then we can use it
if( instr.getType() == Type.Local ) { if( instr.getType() == Type.Local ) {
WasmLocalInstruction local1 = (WasmLocalInstruction)instr; WasmLocalInstruction local1 = (WasmLocalInstruction)instr;
if( local1.getOperator() == VariableOperator.get ) { if( local1.getOperator() == VariableOperator.get ) {
varIndex = local1.getIndex(); varSlot = local1.getSlot();
} }
} }
//alternate we need to create a new locale variable //alternate we need to create a new locale variable
if( varIndex < 0 ) { if( varSlot < 0 ) {
varIndex = getTempVariable( indirectCall.getThisType(), instr.getCodePosition(), javaCodePos + 1 ); varSlot = getTempVariable( indirectCall.getThisType(), instr.getCodePosition(), javaCodePos + 1 );
int idx = count == 1 ? instructions.size() : stackValue.idx + 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 ); instructions.add( indirectCall );
options.registerGet_i32(); // for later access of the vtable options.registerGet_i32(); // for later access of the vtable
} }