/*
   Copyright 2018 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.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/
package de.inetsoftware.jwebassembly.module;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.inetsoftware.jwebassembly.WasmException;

/**
 * This manager monitor the locale variables of a method to create a translation from the slot based index in Java to
 * the variable based index in WebAssembly. An 8-byte variable of type long and double consumes in Java 2 slots, but
 * only one index in WebAssmenbly.
 * 
 * @author Volker Berlin
 *
 */
class LocaleVariableManager {

    private LocaleVariable[]     variables;

    private int                  size;

    private ArrayList<ValueType> localTypes = new ArrayList<>();

    /**
     * Create a new instance.
     */
    LocaleVariableManager() {
        // initialize with a initial capacity that should be enough for the most methods
        variables = new LocaleVariable[8];
        for( int i = 0; i < variables.length; i++ ) {
            variables[i] = new LocaleVariable();
        }
    }

    /**
     * Reset the manager to an initial state
     */
    void reset() {
        for( int i = 0; i < size; i++ ) {
            LocaleVariable var = variables[i];
            var.valueType = null;
            var.idx = -1;
        }
        size = 0;
    }

    /**
     * Mark a variable slot as used with its type.
     * 
     * @param valueType
     *            the type of the local variable
     * @param slot
     *            the memory/slot index of the local variable
     */
    void use( ValueType valueType, int slot ) {
        ensureCapacity( slot );
        size = Math.max( size, slot + 1 );
        LocaleVariable var = variables[slot];
        if( var.valueType != null && var.valueType != valueType ) {
            throw new WasmException( "Redefine local variable type from " + var.valueType + " to "
                            + valueType, null, -1 );
        }
        var.valueType = valueType;
    }

    /**
     * Calculate the WebAssembly index position on the consumed data.
     */
    void calculate() {
        int idx = 0;
        for( int i = 0; i < size; i++ ) {
            LocaleVariable var = variables[i];
            if( var.valueType == null ) { // unused slot or extra slot for double and long values 
                continue;
            }
            var.idx = idx++;
        }
    }

    /**
     * Get the data types of the local variables. The value is only valid until the next call.
     * 
     * @param paramCount
     *            the count of method parameter which should be exclude
     * @return the reused list with fresh values
     */
    List<ValueType> getLocalTypes( int paramCount ) {
        localTypes.clear();
        for( int i = 0; i < size; i++ ) {
            LocaleVariable var = variables[i];
            if( var.idx >= paramCount ) {
                localTypes.add( var.valueType );
            }
        }
        return localTypes;
    }

    /**
     * Get the WebAssembly variable index of the given Java Slot.
     * 
     * @param slot
     *            the memory/slot index of the local variable in Java
     * @return the variable index in WebAssembly
     */
    int get( int slot ) {
        return variables[slot].idx;
    }

    /**
     * Ensure that there is enough capacity.
     * 
     * @param slot
     *            the needed slot
     */
    private void ensureCapacity( int slot ) {
        if( slot >= variables.length ) {
            int i = variables.length;
            variables = Arrays.copyOf( variables, slot + 1 );
            for( ; i < variables.length; i++ ) {
                variables[i] = new LocaleVariable();
            }
        }
    }

    /**
     * The state of a single local variable slot.
     */
    private static class LocaleVariable {
        private ValueType valueType;

        private int       idx = -1;
    }
}

@@ -1198,24 +1198,71 @@
      size;%0A%0A
+    private boolean              needTempI32;%0A%0A
     private 
@@ -1314,16 +1314,16 @@
 st%3C%3E();%0A
-
 %0A    /**
@@ -1896,24 +1896,53 @@
   size = 0;%0A
+        needTempI32 = false;%0A
     %7D%0A%0A    /
@@ -2598,32 +2598,176 @@
 lueType;%0A    %7D%0A%0A
+    /**%0A     * We need an extra temporary variable inn the WebAssembly code.%0A     */%0A    void useTempI32() %7B%0A        needTempI32 = true;%0A    %7D%0A%0A
     /**%0A     * C
@@ -2855,24 +2855,102 @@
 lculate() %7B%0A
+        if( needTempI32 ) %7B%0A            use( ValueType.i32, size );%0A        %7D%0A
         int 
@@ -3775,32 +3775,32 @@
     %7D%0A        %7D%0A
-
         return l
@@ -3809,32 +3809,182 @@
 alTypes;%0A    %7D%0A%0A
+    /**%0A     * Get the slot of the temporary variable.%0A     * %0A     * @return the slot%0A     */%0A    int getTempI32() %7B%0A        return size - 1;%0A    %7D%0A%0A
     /**%0A     * G

Add support for a temporary local variable to reused values on the stack.