public void LookUp_With_Item_Inserted() { SymbolTable testTable = new SymbolTable(); testTable.insert("red", Globals.Symbol.idT, 1); testTable.insert("red", Globals.Symbol.idT, 1); testTable.insert("red", Globals.Symbol.idT, 2); testTable.insert("red", Globals.Symbol.idT, 3); testTable.insert("red", Globals.Symbol.idT, 4); testTable.insert("red", Globals.Symbol.idT, 5); testTable.insert("blue", Globals.Symbol.idT, 1); testTable.insert("blue", Globals.Symbol.idT, 1); testTable.insert("blue", Globals.Symbol.idT, 2); testTable.insert("blue", Globals.Symbol.idT, 3); testTable.insert("blue", Globals.Symbol.idT, 4); VariableEntry actual = new VariableEntry() { lexeme = "blue", tokenType = Globals.Symbol.idT, depth = 4, size = 4, variableType = TableEntry.VariableType.intType, Offset = 0 }; VariableEntry entry = testTable.lookup("blue") as VariableEntry; Assert.AreEqual(actual.Offset, entry.Offset); Assert.AreEqual(actual.tokenType, entry.tokenType); Assert.AreEqual(actual.depth, entry.depth); Assert.AreEqual(actual.variableType, entry.variableType); Assert.AreEqual(actual.size, entry.size); Assert.AreEqual(actual.lexeme, entry.lexeme); }
Exp TranslateDeclaration(VariableDeclaration dec) { ExpressionType init = TranslateExpression(dec.Init); Types.Type type = null; if (dec.Type == null) { if (init.Type.CoerceTo(Types.Type._nil)) { Error.Report(dec.Init.Pos, "Unknown type cannot be initialized with 'nil'"); return(null); } else { type = init.Type; } } else { type = TranslateType(dec.Type).Actual; if (!init.Type.CoerceTo(type)) { Error.Report(dec.Init.Pos, "Type mismatch in variable initialization"); return(null); } } VariableEntry var = new VariableEntry(Level.AllocLocal(dec.Escape), type); Env.ValueEnvironment[dec.Name] = var; return(Translate.TranslateAssignExp(Translate.TranslateSimpleVar(var.Access, Level), init.Exp)); }
public void Lookup_Blank_Table() { SymbolTable testTable = new SymbolTable(); VariableEntry entry = testTable.lookup("blue") as VariableEntry; Assert.AreEqual(entry, null); }
public void Lookup_Insert_Hash() { SymbolTable testTable = new SymbolTable(); testTable.insert("red", Globals.Symbol.idT, 1); testTable.insert("red", Globals.Symbol.idT, 1); testTable.insert("red", Globals.Symbol.idT, 2); testTable.insert("red", Globals.Symbol.idT, 3); testTable.insert("red", Globals.Symbol.idT, 4); testTable.insert("red", Globals.Symbol.idT, 5); testTable.insert("blue", Globals.Symbol.idT, 1); testTable.insert("blue", Globals.Symbol.idT, 1); testTable.insert("blue", Globals.Symbol.idT, 2); testTable.insert("blue", Globals.Symbol.idT, 3); testTable.insert("blue", Globals.Symbol.idT, 4); testTable.insert("blue", Globals.Symbol.idT, 5); VariableEntry comparedEntry = new VariableEntry() { lexeme = "blue", tokenType = Globals.Symbol.idT, depth = 5 }; TableEntry entry = testTable.lookup("blue"); Assert.AreEqual(comparedEntry.lexeme, entry.lexeme); Assert.AreEqual(comparedEntry.tokenType, entry.tokenType); Assert.AreEqual(comparedEntry.depth, entry.depth); }
/// <summary> /// Clears the variables. /// </summary> public void ClearVariables() { for (int i = variableEntriesList.Count - 1; 0 <= i; --i) { VariableEntry entry = variableEntriesList[i]; RemoveVariable(entry.variable.Name); } }
void AddGlobalVariablesButton() { GUILayout.BeginVertical("Box"); GUILayout.Label("Global Variables"); if (GlobalVariables != null) { for (int i = 0; i < GlobalVariables.Count; i++) { VariableEntry variables = GlobalVariables[i]; GUILayout.BeginVertical("Box"); variables.Name = EditorGUILayout.TextField("Name", variables.Name); variables.Value = EditorGUILayout.FloatField("Value", variables.Value == null ? 0 : variables.Value.Value); variables.Max = EditorGUILayout.FloatField("Max", variables.Max == null ? 0 : variables.Max.Value); variables.Min = EditorGUILayout.FloatField("Min", variables.Min == null ? 0 : variables.Min.Value); variables.Description = EditorGUILayout.TextField("Description", variables.Description); if (GUILayout.Button("Delete")) { GlobalVariables.RemoveAt(i); i--; GUILayout.EndVertical(); continue; } GUILayout.EndVertical(); if (variables.Name.Equals(GlobalVariables[i].Name) && variables.Value == GlobalVariables[i].Value && variables.Max == GlobalVariables[i].Max && variables.Min == GlobalVariables[i].Min && variables.Description.Equals(GlobalVariables[i])) { continue; } GlobalVariables[i] = variables; } } if (GUILayout.Button("Add")) { if (GlobalVariables == null) { GlobalVariables = new List <VariableEntry>(); } GlobalVariables.Add(new VariableEntry("", 0, 0, 0, "")); } GUILayout.EndVertical(); }
ExpressionType TranslateVariable(SimpleVariable var) { Entry ent = Env.ValueEnvironment[var.Name] as Entry; if (ent is VariableEntry) { VariableEntry vent = ent as VariableEntry; return(new ExpressionType(Translate.TranslateSimpleVar(vent.Access, Level), vent.Type)); } else { Error.Report(var.Pos, "Undefined variable"); return(new ExpressionType(null, Types.Type._unknown)); } }
/// <summary> /// Adds the variable. /// </summary> /// <param name="variable">The variable.</param> /// <returns><code>true</code> if variable is added successfully, <code>false</code> otherwise.</returns> public bool AddVariable(PacketPropertyVariable variable) { bool ret = false; try { ret = !variableEntriesByName.ContainsKey(variable.Name); if (ret) { // Add variable to collections VariableEntry entry = new VariableEntry(); entry.variable = variable; variableEntriesByName.Add(variable.Name, entry); int oldCount = variableEntriesList.Count; variableEntriesList.Add(entry); // Check for errors ret = (oldCount != variableEntriesList.Count); if (!ret) { string message = "Internal lists are in inconsistent state after adding of variable " + (variable ?? (object)"(null)"); Log.Error(message); throw new Exception(message); } else { // Fire event FireVariableOperationEvent(variable, VariableAdded); } } } catch (Exception e) { ret = false; Log.Error("Error adding a variable: variable=" + (variable ?? (object)"null"), e); } return(ret); }
void RemoveVariable(VariableEntry variable) { if (Scanner.DebugLevel > 1) { BlockList.ComputeOffsets(); Scanner.LogDebug(2, $"DELETE VARIABLE: {Method.Name} {variable}"); Scanner.DumpBlocks(2); } for (int i = 0; i < BlockList.Count; i++) { var block = BlockList [i]; for (int j = 0; j < block.Instructions.Count; j++) { var instruction = block.Instructions [j]; Scanner.LogDebug(2, $" {CecilHelper.Format (instruction)}"); switch (instruction.OpCode.Code) { case Code.Ldloc_0: if (variable.Index == 0 && variable.IsConstant) { BlockList.ReplaceInstructionAt(ref block, j, CecilHelper.CreateConstantLoad(variable.Value)); } break; case Code.Stloc_0: break; case Code.Ldloc_1: if (variable.Index < 1) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Ldloc_0)); } else if (variable.Index == 1) { BlockList.ReplaceInstructionAt(ref block, j, CecilHelper.CreateConstantLoad(variable.Value)); } break; case Code.Stloc_1: if (variable.Index < 1) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Stloc_0)); } break; case Code.Ldloc_2: if (variable.Index < 2) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Ldloc_1)); } else if (variable.Index == 2) { BlockList.ReplaceInstructionAt(ref block, j, CecilHelper.CreateConstantLoad(variable.Value)); } break; case Code.Stloc_2: if (variable.Index < 2) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Stloc_1)); } break; case Code.Ldloc_3: if (variable.Index < 3) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Ldloc_2)); } else if (variable.Index == 3) { BlockList.ReplaceInstructionAt(ref block, j, CecilHelper.CreateConstantLoad(variable.Value)); } break; case Code.Stloc_3: if (variable.Index < 3) { BlockList.ReplaceInstructionAt(ref block, j, Instruction.Create(OpCodes.Stloc_2)); } break; case Code.Ldloc: case Code.Ldloc_S: var argument = CecilHelper.GetVariable(Method.Body, instruction); if (argument == variable.Variable) { BlockList.ReplaceInstructionAt(ref block, j, CecilHelper.CreateConstantLoad(variable.Value)); } break; } } } if (Scanner.DebugLevel > 1) { BlockList.ComputeOffsets(); Scanner.LogDebug(2, $"DELETE VARIABLE DONE: {Method.Name} {variable}"); Scanner.DumpBlocks(2); } }
public bool RemoveUnusedVariables() { Scanner.LogDebug(1, $"REMOVE VARIABLES: {Method.Name}"); if (Method.Body.HasExceptionHandlers) { return(false); } var removed = false; var variables = new Dictionary <VariableDefinition, VariableEntry> (); for (int i = 0; i < Method.Body.Variables.Count; i++) { var variable = new VariableEntry(Method.Body.Variables [i], i); variables.Add(variable.Variable, variable); if (Scanner.DebugLevel > 1) { Scanner.LogDebug(2, $" VARIABLE: {variable}"); } } foreach (var block in BlockList.Blocks) { Scanner.LogDebug(2, $"REMOVE VARIABLES #1: {block}"); for (int i = 0; i < block.Instructions.Count; i++) { var instruction = block.Instructions [i]; Scanner.LogDebug(2, $" {CecilHelper.Format (instruction)}"); var variable = CecilHelper.GetVariable(Method.Body, instruction); if (variable == null) { continue; } var entry = variables [variable]; if (entry == null) { throw DebugHelpers.AssertFail(Method, block, $"Cannot resolve variable from instruction `{CecilHelper.Format (instruction)}`."); } entry.Used = true; if (entry.Modified) { continue; } switch (instruction.OpCode.Code) { case Code.Ldloc_0: case Code.Ldloc_1: case Code.Ldloc_2: case Code.Ldloc_3: case Code.Ldloc: case Code.Ldloc_S: continue; case Code.Ldloca: case Code.Ldloca_S: entry.SetModified(); continue; case Code.Stloc: case Code.Stloc_0: case Code.Stloc_1: case Code.Stloc_2: case Code.Stloc_3: case Code.Stloc_S: break; default: throw DebugHelpers.AssertFailUnexpected(Method, block, instruction); } if (i == 0 || entry.IsConstant) { entry.SetModified(); continue; } var load = block.Instructions [i - 1]; switch (block.Instructions [i - 1].OpCode.Code) { case Code.Ldc_I4_0: entry.SetConstant(block, load, ConstantValue.Zero); break; case Code.Ldc_I4_1: entry.SetConstant(block, load, ConstantValue.One); break; case Code.Ldnull: entry.SetConstant(block, load, ConstantValue.Null); break; default: entry.SetModified(); break; } } } Scanner.LogDebug(1, $"REMOVE VARIABLES #1"); for (int i = Method.Body.Variables.Count - 1; i >= 0; i--) { var variable = variables [Method.Body.Variables [i]]; Scanner.LogDebug(2, $" VARIABLE #{i}: {variable}"); if (!variable.Used) { Scanner.LogDebug(2, $" --> REMOVE"); Scanner.LogDebug(1, $"REMOVE VARIABLES - REMOVE: {Method.Name}"); RemoveVariable(variable); Method.Body.Variables.RemoveAt(i); removed = true; continue; } if (variable.IsConstant) { Scanner.LogDebug(2, $" --> CONSTANT ({variable.Value}): {variable.Instruction}"); Scanner.LogDebug(1, $"REMOVE VARIABLES - CONSTANT: {Method.Name}"); Scanner.DumpBlock(2, variable.Block); var position = variable.Block.IndexOf(variable.Instruction); var block = variable.Block; BlockList.RemoveInstructionAt(ref block, position + 1); BlockList.RemoveInstructionAt(ref block, position); RemoveVariable(variable); Method.Body.Variables.RemoveAt(i); removed = true; continue; } } if (removed) { BlockList.ComputeOffsets(); Scanner.LogDebug(1, $"REMOVE VARIABLES DONE: {removed}"); Scanner.DumpBlocks(1); Scanner.Context.Options.OptimizerReport?.RemovedDeadVariables(Method); } return(removed); }
/// <summary> /// Adds the variable. /// </summary> /// <param name="variable">The variable.</param> /// <returns><code>true</code> if variable is added successfully, <code>false</code> otherwise.</returns> public bool AddVariable(PacketPropertyVariable variable) { bool ret = false; try { ret = !variableEntriesByName.ContainsKey(variable.Name); if (ret) { // Add variable to collections VariableEntry entry = new VariableEntry(); entry.variable = variable; variableEntriesByName.Add(variable.Name, entry); int oldCount = variableEntriesList.Count; variableEntriesList.Add(entry); // Check for errors ret = (oldCount != variableEntriesList.Count); if (!ret) { string message = "Internal lists are in inconsistent state after adding of variable " + (variable ?? (object)"(null)"); Log.Error(message); throw new Exception(message); } else { // Fire event FireVariableOperationEvent(variable, VariableAdded); } } } catch (Exception e) { ret = false; Log.Error("Error adding a variable: variable=" + (variable ?? (object)"null"), e); } return ret; }
/// <summary> /// 添加全局变量 /// </summary> /// <param name="name">待添加的全局变量名称</param> /// <param name="value">待添加的全局变量值</param> public void AddGlobalVariable(VariableEntry variable) { variable.Validate(); //验证该全局变量是否可用 m_listGlobalVariables.Add(variable); }