/// <returns>Hashtable<Namespace,ArrayList<TerminalName>></returns> private static ReadOnlyDictionary CreateNamespaceToVariablesAllocatedTherein(IEnumerable liveLocalNonParameterVariableNames) { var result=new Hashtable(); foreach(TerminalName name in liveLocalNonParameterVariableNames) { result.LookupOrCreateArrayList(name.Namespace).Add(name); } return result.AsReadOnlyDictionary(); }
private static ReadOnlyDictionary EstablishRegisterAllocationCounts(ReadOnlyDictionary depthToNamespaces, int freeRegisterMask, IEnumerable leaves, int maxDepth, ReadOnlyDictionary nsToLiveVars) { var initialBudget=0; while(freeRegisterMask>0) { ++initialBudget; freeRegisterMask&=(freeRegisterMask-1); } var budget=new Hashtable(); //Hashtable<Namespace,int> foreach(Namespace ns in leaves) { budget[ns]=initialBudget; } var result=new Hashtable(); //Hashtable<Namespace,int> object boxedMaxValue=int.MaxValue; for(var depth=maxDepth; depth>=0; --depth) { var itemsToProcess=(ArrayList)depthToNamespaces[depth]; foreach(Namespace ns in itemsToProcess) { var capacity=(int)budget[ns]; var liveVars=(ArrayList)nsToLiveVars[ns]; var requested=liveVars!=null ? liveVars.Count : 0; var granted=Math.Min(capacity, requested); var remaining=capacity-granted; result.Add(ns, granted); result[ns]=granted; //now forward the budget to my parent var inner=ns.Inner; if(inner!=null) { var existingBudget=(int)(budget[inner] ?? boxedMaxValue); var newBudget=Math.Min(existingBudget, remaining); budget[inner]=newBudget; } } } return result.AsReadOnlyDictionary(); }
/// <summary> /// Find the namespace leaves /// </summary> /// <returns>Hashtable<Namespace,null>></returns> private static ReadOnlyDictionary FindLeaves(IEnumerable liveLocalNonParameterVariableNames) { var leaves=new Hashtable(); //Hashtable<Namespace,null> foreach(TerminalName name in liveLocalNonParameterVariableNames) { leaves[name.Namespace]=null; } foreach(TerminalName name in liveLocalNonParameterVariableNames) { for(var ns=name.Namespace.Inner; ns!=null; ns=ns.Inner) { leaves.Remove(ns); } } return leaves.AsReadOnlyDictionary(); }
/// <summary> /// Basically: liveVariables.Select(lv=>lv.Namespace).GroupBy(ns=>ns.Depth) /// </summary> /// <returns>Hashtable<int,ArrayList<Namespace>></returns> private static ReadOnlyDictionary MakeDepthToNamespaces(IEnumerable liveLocalNonParameterVariableNames, out int maxDepth) { var result=new Hashtable(); var beenHere=new Hashtable(); maxDepth=-1; foreach(TerminalName name in liveLocalNonParameterVariableNames) { var ns=name.Namespace; maxDepth=Math.Max(maxDepth, ns.Depth); while(ns!=null) { if(beenHere.Contains(ns)) { break; } beenHere.Add(ns, null); result.LookupOrCreateArrayList(ns.Depth).Add(ns); ns=ns.Inner; } } return result.AsReadOnlyDictionary(); }