context: /*
    Copyright (C) 2002-2010 Jeroen Frijters
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.
  
    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:
  
    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
  
    Jeroen Frijters
    jeroen@frijters.net
    
  */
  using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Diagnostics;
  #if STATIC_COMPILER
  using IKVM.Reflection.Emit;
  using Type = IKVM.Reflection.Type;
  #else
  using System.Reflection.Emit;
  #endif
  using IKVM.Internal;
  using InstructionFlags = IKVM.Internal.ClassFile.Method.InstructionFlags;
  using ExceptionTableEntry = IKVM.Internal.ClassFile.Method.ExceptionTableEntry;
  
  sealed class LocalVar
  {
  	internal bool isArg;
  	internal int local;
  	internal TypeWrapper type;
  	internal CodeEmitterLocal builder;
  	// used to emit debugging info, only available if ClassLoaderWrapper.EmitDebugInfo is true
  	internal string name;
  	internal int start_pc;
  	internal int end_pc;
  }
  
  struct LocalVarInfo
  {
  	private readonly LocalVar[/*instructionIndex*/] localVars;
  	private readonly LocalVar[/*instructionIndex*/][/*localIndex*/] invokespecialLocalVars;
  	private readonly LocalVar[/*index*/] allLocalVars;
  
  	internal LocalVarInfo(CodeInfo ma, ClassFile classFile, ClassFile.Method method, UntangledExceptionTable exceptions, MethodWrapper mw, ClassLoaderWrapper classLoader)
  	{
  		Dictionary<int, string>[] localStoreReaders = FindLocalVariables(ma, mw, classFile, method);
  
  		// now that we've done the code flow analysis, we can do a liveness analysis on the local variables
  		ClassFile.Method.Instruction[] instructions = method.Instructions;
  		Dictionary<long, LocalVar> localByStoreSite = new Dictionary<long, LocalVar>();
  		List<LocalVar> locals = new List<LocalVar>();
  		for (int i = 0; i < localStoreReaders.Length; i++)
  		{
  			if (localStoreReaders[i] != null)
  			{
  				VisitLocalLoads(ma, method, locals, localByStoreSite, localStoreReaders[i], i, classLoader.EmitDebugInfo);
  			}
  		}
  		Dictionary<LocalVar, LocalVar> forwarders = new Dictionary<LocalVar, LocalVar>();
  		if (classLoader.EmitDebugInfo)
  		{
  			InstructionFlags[] flags = MethodAnalyzer.ComputePartialReachability(ma, method.Instructions, exceptions, 0, false);
  			// if we're emitting debug info, we need to keep dead stores as well...
  			for (int i = 0; i < instructions.Length; i++)
  			{
  				if ((flags[i] & InstructionFlags.Reachable) != 0
  					&& IsStoreLocal(instructions[i].NormalizedOpCode))
  				{
  					if (!localByStoreSite.ContainsKey(MakeKey(i, instructions[i].NormalizedArg1)))
  					{
  						LocalVar v = new LocalVar();
  						v.local = instructions[i].NormalizedArg1;
  						v.type = ma.GetStackTypeWrapper(i, 0);
  						FindLvtEntry(v, method, i);
  						locals.Add(v);
  						localByStoreSite.Add(MakeKey(i, v.local), v);
  					}
  				}
  			}
  			// to make the debugging experience better, we have to trust the
  			// LocalVariableTable (unless it's clearly bogus) and merge locals
  			// together that are the same according to the LVT
  			for (int i = 0; i < locals.Count - 1; i++)
  			{
  				for (int j = i + 1; j < locals.Count; j++)
  				{
  					LocalVar v1 = (LocalVar)locals[i];
  					LocalVar v2 = (LocalVar)locals[j];
  					if (v1.name != null && v1.name == v2.name && v1.start_pc == v2.start_pc && v1.end_pc == v2.end_pc)
  					{
  						// we can only merge if the resulting type is valid (this protects against incorrect
  						// LVT data, but is also needed for constructors, where the uninitialized this is a different
  						// type from the initialized this)
  						TypeWrapper tw = InstructionState.FindCommonBaseType(v1.type, v2.type);
  						if (tw != VerifierTypeWrapper.Invalid)
  						{
  							v1.isArg |= v2.isArg;
  							v1.type = tw;
  							forwarders.Add(v2, v1);
  							locals.RemoveAt(j);
  							j--;
  						}
  					}
  				}
  			}
  		}
  		else
  		{
  			for (int i = 0; i < locals.Count - 1; i++)
  			{
  				for (int j = i + 1; j < locals.Count; j++)
  				{
  					LocalVar v1 = (LocalVar)locals[i];
  					LocalVar v2 = (LocalVar)locals[j];
  					// if the two locals are the same, we merge them, this is a small
  					// optimization, it should *not* be required for correctness.
  					if (v1.local == v2.local && v1.type == v2.type)
  					{
  						v1.isArg |= v2.isArg;
  						forwarders.Add(v2, v1);
  						locals.RemoveAt(j);
  						j--;
  					}
  				}
  			}
  		}
  		invokespecialLocalVars = new LocalVar[instructions.Length][];
  		localVars = new LocalVar[instructions.Length];
  		for (int i = 0; i < localVars.Length; i++)
  		{
  			LocalVar v = null;
  			if (localStoreReaders[i] != null)
  			{
  				Debug.Assert(IsLoadLocal(instructions[i].NormalizedOpCode));
  				// lame way to look up the local variable for a load
  				// (by indirecting through a corresponding store)
  				foreach (int store in localStoreReaders[i].Keys)
  				{
  					v = localByStoreSite[MakeKey(store, instructions[i].NormalizedArg1)];
  					break;
  				}
  			}
  			else
  			{
  				if (instructions[i].NormalizedOpCode == NormalizedByteCode.__invokespecial || instructions[i].NormalizedOpCode == NormalizedByteCode.__dynamic_invokespecial)
  				{
  					invokespecialLocalVars[i] = new LocalVar[method.MaxLocals];
  					for (int j = 0; j < invokespecialLocalVars[i].Length; j++)
  					{
  						localByStoreSite.TryGetValue(MakeKey(i, j), out invokespecialLocalVars[i][j]);
  					}
  				}
  				else
  				{
  					localByStoreSite.TryGetValue(MakeKey(i, instructions[i].NormalizedArg1), out v);
  				}
  			}
  			if (v != null)
  			{
  				LocalVar fwd;
  				if (forwarders.TryGetValue(v, out fwd))
  				{
  					v = fwd;
  				}
  				localVars[i] = v;
  			}
  		}
  		this.allLocalVars = locals.ToArray();
  	}
  
  	private static void FindLvtEntry(LocalVar lv, ClassFile.Method method, int instructionIndex)
  	{
  		ClassFile.Method.LocalVariableTableEntry[] lvt = method.LocalVariableTableAttribute;
  		if (lvt != null)
  		{
  			int pc = method.Instructions[instructionIndex].PC;
  			int nextPC = method.Instructions[instructionIndex + 1].PC;
  			bool isStore = IsStoreLocal(method.Instructions[instructionIndex].NormalizedOpCode);
  			foreach (ClassFile.Method.LocalVariableTableEntry e in lvt)
  			{
  				// TODO validate the contents of the LVT entry
  				if (e.index == lv.local &&
  					(e.start_pc <= pc || (e.start_pc == nextPC && isStore)) &&
  					e.start_pc + e.length > pc)
  				{
  					lv.name = e.name;
  					lv.start_pc = e.start_pc;
  					lv.end_pc = e.start_pc + e.length;
  					break;
  				}
  			}
  		}
  	}
  
  	// NOTE for dead stores, this returns null
  	internal LocalVar GetLocalVar(int instructionIndex)
  	{
  		return localVars[instructionIndex];
  	}
  
  	internal LocalVar[] GetLocalVarsForInvokeSpecial(int instructionIndex)
  	{
  		return invokespecialLocalVars[instructionIndex];
  	}
  
  
gt: 	internal LocalVar[] GetAllLocalVars()