Пример #1
0
		// Dump to stdout
		public void Dump(int indent)
		{
			Utils.WriteIndentedLine(indent, "Scope: {0}", Node);

			if (Symbols.Sort().Count>0)
			{
				Utils.WriteIndentedLine(indent + 1, "Local Symbols:");
				Symbols.Dump(indent + 2);
			}

			foreach (var i in InnerScopes)
			{
				var merged = new SymbolFrequency();
				merged.CopyFrom(Symbols);
				merged.MergeSymbols(i.AllSymbols);

				if (merged.Sort().Count > 0)
				{
					Utils.WriteIndentedLine(indent + 1, "Symbols after merge with {0}:", i.Node);
					merged.Dump(indent + 2);
				}
			}

			foreach (var i in InnerScopes)
			{
				i.Dump(indent + 1);
			}
		}
Пример #2
0
        // Deep copy all symbols from another symbol frequency map
        public void CopyFrom(SymbolFrequency other)
        {
            this.Clear();

            foreach (var i in other)
            {
                AddSymbol(new Symbol(i.Value));
            }
        }
Пример #3
0
		// Prepare symbol ranks of all local symbols
		//  - For each inner scope, merge this symbol frequency map
		//    of this scope with that of the inner scope
		//  - Update the rank of each symbol
		//  - Repeat for each inner scope
		public void Prepare()
		{
			// Recurse through all scopes
			foreach (var i in InnerScopes)
			{
				i.Prepare();
			}

			// Apply default accessibility to own local symbols
			foreach (var i in Symbols)
			{
				if (i.Value.Accessibility == Accessibility.Default)
				{
					i.Value.Accessibility = DefaultAccessibility;
				}
			}

			// Calculate base rank based on frequency of our own
			// local symbols
			Symbols.UpdateRanks(Symbols.Sort());

			// Merge each inner scope and update ranks
			foreach (var i in InnerScopes)
			{
				var merged = new SymbolFrequency();
				merged.CopyFrom(Symbols);
				merged.MergeSymbols(i.AllSymbols);

				Symbols.UpdateRanks(merged.Sort());
			}

			// Member symbol
			Members.UpdateRanks(Members.Sort());
			foreach (var i in InnerScopes)
			{
				var merged = new SymbolFrequency();
				merged.CopyFrom(Members);
				merged.MergeSymbols(i.AllMembers);

				Members.UpdateRanks(merged.Sort());
			}
		}
Пример #4
0
		// Obfuscate all local symbols
		//  - enumerate all local symbols and tell the symbol allocator
		//    that it can be obfuscated.
		//	- where there are `holes` in the rank mapping, tell the symbol
		//    allocator to reserve those symbols.  These holes are to be
		//    filled by higher frequency symbols on the inner scopes.
		public void ObfuscateSymbols(RenderContext ctx, SymbolFrequency SymbolFrequency, SymbolAllocator Allocator, string prefix)
		{
			// Walk through local symbols
			int expectedRank = 0;
			foreach (var symbol in SymbolFrequency.Sort())
			{
				// Ignore public symbols
				if (symbol.Accessibility != Accessibility.Private)
					continue;

				// Ignore non-local symbols
				if (symbol.Scope != Symbol.ScopeType.local)
					continue;

				// Reserve space for inner, higher frequency symbols
				if (symbol.Rank > expectedRank)
				{
					if (ctx.Compiler.Formatted && ctx.Compiler.SymbolInfo)
					{
						for (int r = expectedRank; r < symbol.Rank; r++)
						{
							ctx.StartLine();
							ctx.AppendFormat("// #{0} reserved", r);
						}
					}
					Allocator.ReserveObfuscatedSymbols(symbol.Rank - expectedRank);
				}

				string newSymbol = Allocator.OnfuscateSymbol(symbol.Name);

				// Show info
				if (ctx.Compiler.Formatted && ctx.Compiler.SymbolInfo)
				{
					ctx.StartLine();
					ctx.AppendFormat("// #{0} {3}{1} -> {3}{2}", symbol.Rank, symbol.Name, newSymbol, prefix);
				}

				expectedRank = symbol.Rank + 1;
			}
		}
Пример #5
0
        // Merge symbols from an inner frequency map.
        // Symbols are merged by:
        //   - inner symbols from the inner map are mapped to inner symbols on the outer map
        //   - local symbols from the inner map are mapped to inner symbols on the outer map
        //   - outer symbols from the inner map which are defined in the outer map
        //			are mapped to local symbols on the outer map
        //	 - outer symbols from the inner map which are not defined in the outer map
        //			are mapped to outer symbols on the outer map
        public void MergeSymbols(SymbolFrequency inner)
        {
            foreach (var i in inner)
            {
                // Don't merge inner public symbols
                if (i.Value.Accessibility == Accessibility.Public)
                {
                    continue;
                }

                Symbol s;
                switch (i.Value.Scope)
                {
                case Symbol.ScopeType.inner:
                case Symbol.ScopeType.local:
                    if (!FindSymbol(i.Value.Name, Symbol.ScopeType.inner, out s))
                    {
                        s = AddSymbol(new Symbol(i.Value.Name, Symbol.ScopeType.inner));
                    }
                    break;

                case Symbol.ScopeType.outer:
                default:
                    if (!FindSymbol(i.Value.Name, Symbol.ScopeType.local, out s))
                    {
                        if (!FindSymbol(i.Value.Name, Symbol.ScopeType.outer, out s))
                        {
                            s = AddSymbol(new Symbol(i.Value.Name, Symbol.ScopeType.outer));
                        }
                    }
                    break;
                }

                s.Frequency += i.Value.Frequency;
            }
        }