internal static void AddFunction(Label f) { if (!Symbols.ContainsKey(f.Addr)) { Symbols.Add(f.Addr, f); } }
public void AddSymbol(AdsSymbol symbol) { if (Symbols.ContainsKey(symbol.Name)) { throw new ArgumentException($"Symbol with the same name ('{symbol.Name}') already exists"); } //Define Symbol Offset var offset = GetCurrentDataOffset(); symbol.Offset = offset; var symbolBytes = symbol.GetBytes(); //Add symbol to list Symbols.Add(symbol.Name, symbol); //Add symbol to data memory.AddData(61451, symbolBytes); memory.AddData(61449, symbolBytes); //add symbol handlers memory.AddData(61443, offset.GetBytes()); memory.AddData(61446, offset.GetBytes()); //Update symbolUploadInfo memory.SetData(61455, new SymbolUploadInfo(Symbols.Count, GetCurrentSymbolSize()).GetBytes()); //Add Data memory.AddData(61445, new byte[symbol.Size]); }
/// <summary> /// access a symbol with string /// </summary> /// <param name="value"></param> /// <param name="symbolType"></param> /// <returns></returns> public ISymbol GetOrCreateSymbol(string value, SymbolType symbolType) { if (symbolType == SymbolType.Terminal) { if (value == Terminal.EndOfFile.Value) { return(Terminal.EndOfFile); } if (value == "") { return(Terminal.Epsilon); } if (!Symbols.ContainsKey(value)) { Symbols.Add(value, new Terminal(value)); } return(Symbols[value]); } // else if (!Symbols.ContainsKey(value)) { Symbols.Add(value, new Variable(value)); } return(Symbols[value]); }
internal static void FirstParse(BinaryReader br, DisassemblyTask task) { InitRelocationLabels(br, task); foreach (Section s in task.Sections.Values) { if (s.IsCode) { FirstParse(br, s); } } //complete the label list RemoveFalseFunctions(); List <(N64Ptr start, N64Ptr end)> textSections = GetTextSectionRanges(task); foreach (var addr in RelocationLabels.Values) { if (!Symbols.ContainsKey(addr)) { if (textSections.Exists(x => x.start >= addr && addr < x.end)) { Symbols[addr] = new Label(Label.Type.LBL, addr); } else { Symbols[addr] = new Label(Label.Type.VAR, addr); } } } }
internal static void DataDisassembly(StreamWriter sw, BinaryReader br, Section section, IEnumerable <Overlay.RelocationWord> rel) { WriteSectionName(sw, section); sw.WriteLine(); br.BaseStream.Position = section.Offset; pc = section.VRam; List <byte> byteChain = new List <byte>(); N64Ptr end = section.VRam + section.Size; while (pc < end) { if (Symbols.ContainsKey(pc)) { DumpByteChain(sw, ref byteChain); sw.Write($"{Symbols[pc]}: "); } if (rel.Any(x => x.Offset == br.BaseStream.Position)) { DumpByteChain(sw, ref byteChain); N64Ptr lbl = br.ReadBigInt32(); pc += 4; sw.WriteLine($".word {Symbols[lbl]}"); } else { byteChain.Add(br.ReadByte()); pc += 1; } } DumpByteChain(sw, ref byteChain); sw.WriteLine(); }
internal static void SimpleDisassembly(StreamWriter sw, BinaryReader br, Section section) { //disable symbol detection First_Parse = false; jaltaken = 0; pc = section.VRam; br.BaseStream.Position = section.Offset; Reset_Gpr_Regs(); int textsize = section.Size; for (int i = 0; i < textsize; i += 4) { if (Symbols.ContainsKey(pc)) { var label = Symbols[pc]; //if function start if (label.Kind == Label.Type.FUNC) { PrintFunctionStart(sw); Reset_Gpr_Regs(); } else { sw.WriteLine($"{label}:"); } } //read and write opcode int word = br.ReadBigInt32(); sw.WriteLine($"\t{FormatOp(GetOP(word))}"); if (jaltaken != 0) { jaltaken--; if (!(jaltaken != 0)) { Reset_Gpr_Regs_Soft(); } } pc += 4; if (Symbols.ContainsKey(pc) && Symbols[pc].Kind == Label.Type.FUNC) { sw.WriteLine(); sw.WriteLine(); } } }
public void WriteSymbol(string name, byte[] data) { if (Symbols.ContainsKey(name)) { memory.SetData(Symbols[name].Header.IndexGroup, Symbols[name].Header.IndexOffset, data); } else { throw new ArgumentException("Symbol not found!"); } }
public Symbol FindSymbol(string value)//唯一值渲染。根据唯一值寻找符号 { //return output; if (Symbols.ContainsKey(value)) { return(Symbols[value]); } else { return(DefaultSymbol); //默认符号输出 } }
public byte[] ReadSymbol(string name) { if (Symbols.ContainsKey(name)) { return(memory.GetData(Symbols[name].Header.IndexGroup, Symbols[name].Header.IndexOffset, Symbols[name].Header.Size)); } else { throw new ArgumentException("Symbol not found!"); } }
public bool Contains(string id) { if (Symbols.ContainsKey(id)) { return(true); } if (!(ParentContext is null)) { return(ParentContext.Contains(id)); } return(false); }
public Boolean AllowedForwardReferences(Ast ast) { if (Symbols.ContainsKey(ast.Token.TokenValue)) { return(AllowAllForwardReferences); } if (EnclosingScope == null) { return(false); } return(EnclosingScope.AllowedForwardReferences(ast)); }
public List <GrammarScriptItem> GetItems(string name, bool includeMatches = false) { if (!includeMatches) { List <GrammarScriptItem> list = new List <GrammarScriptItem>(); if (Terminals.ContainsKey(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.Terminal, name)); } else if (Symbols.ContainsKey(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.Symbol, name)); } else if (SymbolSets.ContainsKey(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.SymbolSet, name)); } return(list); } else { List <GrammarScriptItem> list = new List <GrammarScriptItem>(); foreach (string k in Terminals.Keys) { if (k.Contains(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.Terminal, k)); } } foreach (string k in Symbols.Keys) { if (k.Contains(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.Symbol, k)); } } foreach (string k in Terminals.Keys) { if (k.Contains(name)) { list.Add(new GrammarScriptItem(GrammarScriptItemType.Terminal, k)); } } return(list); } }
public Symbol this[string id] { get { if (Symbols.ContainsKey(id)) { return(Symbols[id]); } if (!(ParentContext is null)) { return(ParentContext[id]); } if (RuntimeVariables.Symbols.ContainsKey(id)) { return(RuntimeVariables.Symbols[id]); } throw new Exception($"Variavel {id} não está declarada nesse escopo"); } }
// Evaluate the expression. public double EvaluateSimple(string expr) { int best_pos = 0; int parens = 0; int expr_len = expr.Length; //if (expr_len == 0) return 0; // If we find + or - now, then it's a unary operator. bool is_unary = true; // So far we have nothing. Precedence best_prec = Precedence.None; // Find the operator with the lowest precedence. // Look for places where there are no open // parentheses. for (int pos = 0; pos < expr_len; pos++) { // Examine the next character. char ch = expr[pos]; // Assume we will not find an operator. In // that case, the next operator will not // be unary. bool next_unary = false; if (ch == '(') { // Increase the open parentheses count. parens += 1; // A + or - after "(" is unary. next_unary = true; } else if (ch == ')') { // Decrease the open parentheses count. parens -= 1; // An operator after ")" is not unary. next_unary = false; // If parens < 0, too many )'s. if (parens < 0) { throw new FormatException("Too many close parentheses in '" + expr + "'"); } } else if (parens == 0) { // See if this is an operator. if ((ch == '^') || (ch == '*') || (ch == '/') || (ch == '\\') || (ch == '%') || (ch == '+') || (ch == '-') || // Cesc (ch == '&') || (ch == '|') || (ch == '~') || (ch == '!') || (ch == '<') || (ch == '>') ) { // An operator after an operator is unary. next_unary = true; // See if this operator has higher precedence than the current one. switch (ch) { case '^': if (best_prec >= Precedence.Power) { best_prec = Precedence.Power; best_pos = pos; } break; case '*': case '/': if (best_prec >= Precedence.Times) { best_prec = Precedence.Times; best_pos = pos; } break; case '%': if (best_prec >= Precedence.Modulus) { best_prec = Precedence.Modulus; best_pos = pos; } break; case '<': if (best_prec >= Precedence.RotateL) { best_prec = Precedence.RotateL; best_pos = pos; } break; case '>': if (best_prec >= Precedence.RotateR) { best_prec = Precedence.RotateR; best_pos = pos; } break; case '&': if (best_prec >= Precedence.And) { best_prec = Precedence.And; best_pos = pos; } break; case '|': if (best_prec >= Precedence.Or) { best_prec = Precedence.Or; best_pos = pos; } break; case '~': // Cesc not case '+': case '-': // Ignore unary operators // for now. if ((!is_unary) && best_prec >= Precedence.Plus) { best_prec = Precedence.Plus; best_pos = pos; } break; } } } is_unary = next_unary; } // If the parentheses count is not zero, there's a ) missing. if (parens != 0) { throw new FormatException( "Missing close parenthesis in '" + expr + "'"); } // Hopefully we have the operator. if (best_prec < Precedence.None) { string lexpr = expr.Substring(0, best_pos); string rexpr = expr.Substring(best_pos + 1); switch (expr[best_pos]) { case '^': return(Math.Pow(EvaluateSimple(lexpr), EvaluateSimple(rexpr))); case '*': return (EvaluateSimple(lexpr) * EvaluateSimple(rexpr)); case '/': return (EvaluateSimple(lexpr) / EvaluateSimple(rexpr)); case '%': return (EvaluateSimple(lexpr) % EvaluateSimple(rexpr)); case '+': return (EvaluateSimple(lexpr) + EvaluateSimple(rexpr)); case '-': return (EvaluateSimple(lexpr) - EvaluateSimple(rexpr)); case '&': return ((double)((int)EvaluateSimple(lexpr) & (int)EvaluateSimple(rexpr))); case '|': return ((double)((int)EvaluateSimple(lexpr) | (int)EvaluateSimple(rexpr))); case '<': return ((double)((int)EvaluateSimple(lexpr) << (int)EvaluateSimple(rexpr))); case '>': return ((double)((int)EvaluateSimple(lexpr) >> (int)EvaluateSimple(rexpr))); } } // if we do not yet have an operator, there // are several possibilities: // // 1. expr is (expr2) for some expr2. // 2. expr is -expr2 or +expr2 for some expr2. // 3. expr is Fun(expr2) for a function Fun. // 4. expr is a primitive. // 5. It's a literal like "3.14159". // Look for (expr2). if (expr.StartsWith("(") && expr.EndsWith(")")) { // Remove the parentheses. return(EvaluateSimple(expr.Substring(1, expr_len - 2))); } // Look for -expr2. if (expr.StartsWith("-")) { return(-EvaluateSimple(expr.Substring(1))); } // Look for +expr2. if (expr.StartsWith("+")) { return(EvaluateSimple(expr.Substring(1))); } // Look for ~expr2. if (expr.StartsWith("~")) { return((double)(~(int)EvaluateSimple(expr.Substring(1)))); } // Look for Fun(expr2). if (expr_len > 5 && expr.EndsWith(")")) { // Find the first (. int paren_pos = expr.IndexOf("("); if (paren_pos > 0) { // See what the function is. string lexpr = expr.Substring(0, paren_pos); string rexpr = expr.Substring(paren_pos + 1, expr_len - paren_pos - 2); switch (lexpr.ToLower()) { case "int": return((int)(EvaluateSimple(rexpr))); } } } // See if it's a primitive. if (Symbols.ContainsKey(expr.ToLower())) { try { return(Symbols[expr.ToLower()].Value); } catch (Exception) { throw new FormatException( "Primative '" + expr + "' has value '" + Symbols[expr] + "' which is not a Double."); } } // It must be a literal like "2.71828". return(interpretValue(expr, 0)); /* * try * { * return interpretValue(expr,0); * } * catch (Exception) * { * if (expr.Contains ('[') || * expr.Contains (']')|| * expr.Contains ("@@")) { * Console.WriteLine ("Syntax error in {0}. Is for asmsx ? use the modifier --asmsx.", expr); * return 0; * //throw new FormatException ("Error evaluating '" + expr + "' as a constant." + * // "Error, is syntax is asmsx, use the modifier --asmsx."); * } else { * //throw new FormatException ("Error evaluating '" + expr + "' as a constant."); * Console.WriteLine ("Error evaluating '" + expr + "' as a constant."); * return 0; * } * }*/ }
// Interpret value double interpretValue(string expression, uint PC) { // Interpret value if (expression == "$") { return(PC); } else if (expression.StartsWith("0x")) { // Hex return(Convert.ToUInt64(expression.Substring(2), 16)); } // Cesc floats else if (expression.Contains(".") && expression.Count(c => !"0123456789.".Contains(c)) == 0) { // Try to convert the expression into a Double. return(double.Parse(expression, CultureInfo.InvariantCulture)); } else if (expression.StartsWith("$") || (expression.EndsWith("h") && expression.Remove(expression.Length - 1).ToLower().Count(c => !"0123456789abcdef".Contains(c)) == 0)) { return(Convert.ToUInt64(expression.Trim('$', 'h'), 16)); } else if (expression.StartsWith("0b")) // Binary { return(Convert.ToUInt64(expression.Substring(2), 2)); } else if (expression.StartsWith("%") || (expression.EndsWith("b") && expression.Remove(expression.Length - 1).ToLower().Count(c => !"01".Contains(c)) == 0)) { return(Convert.ToUInt64(expression.Trim('%', 'b'), 2)); } else if (expression.StartsWith("0o")) // Octal { return(Convert.ToUInt64(expression.Substring(2), 8)); } else if (expression == "true") { return(1); } else if (expression == "false") { return(0); } else if (expression.StartsWith("'") && expression.EndsWith("'")) { var character = expression.Substring(1, expression.Length - 2).Unescape(); if (character.Length != 1) { throw new InvalidOperationException("Invalid character."); } return(Settings.Encoding.GetBytes(character)[0]); } else { // Check for number bool number = true; for (int i = 0; i < expression.Length; i++) { if (!char.IsNumber(expression [i])) { number = false; break; } } if (number) { if (expression == "") { expression = "0"; return(Convert.ToUInt64(expression)); } else { return(Convert.ToUInt64(expression)); } } else { // Look up reference var symbol = expression.ToLower(); if (symbol.StartsWith(".")) { symbol = symbol.Substring(1) + "@" + LastGlobalLabel; } if (Symbols.ContainsKey(symbol)) { return(Symbols [symbol].Value); } throw new KeyNotFoundException("The specified symbol:" + symbol + " was not found."); } } }
public bool HasSymbol(string name) => Symbols.ContainsKey(name);
private static Gen <PsuedoToken> GenBareIdentifier() { return(GenRegex(@"[a-zA-Z_][a-zA-Z_0-9]*") .Where(s => !Symbols.ContainsKey(s)) // don't emit keywords .Select(s => new PsuedoToken(typeof(IBareIdentifierToken), s, s))); }
private static Gen <PsuedoToken> GenEscapedIdentifier() { return(GenRegex(@"\\[a-zA-Z_0-9]+") .Where(s => !Symbols.ContainsKey(s)) // don't emit keywords .Select(s => new PsuedoToken(typeof(IEscapedIdentifierToken), s, s.Substring(1)))); }
public bool Contains(string symbol) { return(Symbols.ContainsKey(symbol)); }
public ulong Evaluate(string expression, uint PC, int rootLineNumber) { expression = expression.Trim(); // Check for relative labels (special case, because they're bloody annoying to parse) if (expression.EndsWith("_")) { bool relative = true, firstPlus = false; int offset = 0; for (int i = 0; i < expression.Length - 1; i++) { if (expression[i] == '-') { offset--; } else if (expression[i] == '+') { if (firstPlus) { offset++; } else { firstPlus = true; } } else { relative = false; break; } } if (relative) { int i; for (i = 0; i < RelativeLabels.Count; i++) { if (RelativeLabels[i].RootLineNumber > rootLineNumber) { break; } } i += offset; if (i < 0 || i >= RelativeLabels.Count) { throw new KeyNotFoundException("Relative label not found."); } return(RelativeLabels[i].Address); } } while (expression.SafeContains('(') && expression.SafeContains(')')) { int index = -1; int length = -1; GetParenthesis(expression, out index, out length); var subexpression = expression.Substring(index + 1, length - 2); expression = expression.Remove(index) + Evaluate(subexpression, PC, rootLineNumber) + expression.Substring(index + length); } // Check for parenthesis if (HasOperators(expression)) { // Recurse var parts = SplitExpression(expression); if (parts[0] == "" && parts[1] == "-") // Negate { return((ulong)-(long)Evaluate(parts[2], PC, rootLineNumber)); } if (parts[0] == "" && parts[1] == "~") // NOT { return(~Evaluate(parts[2], PC, rootLineNumber)); } if (parts[0] == string.Empty && parts[1] == "%") { return(Convert.ToUInt64(expression.Trim('%', 'b'), 2)); } switch (parts[1]) // Evaluate { case "+": return(Evaluate(parts[0], PC, rootLineNumber) + Evaluate(parts[2], PC, rootLineNumber)); case "-": return(Evaluate(parts[0], PC, rootLineNumber) - Evaluate(parts[2], PC, rootLineNumber)); case "*": return(Evaluate(parts[0], PC, rootLineNumber) * Evaluate(parts[2], PC, rootLineNumber)); case "/": return(Evaluate(parts[0], PC, rootLineNumber) / Evaluate(parts[2], PC, rootLineNumber)); case "%": return(Evaluate(parts[0], PC, rootLineNumber) % Evaluate(parts[2], PC, rootLineNumber)); case "<<": return(Evaluate(parts[0], PC, rootLineNumber) << (int)Evaluate(parts[2], PC, rootLineNumber)); case ">>": return(Evaluate(parts[0], PC, rootLineNumber) >> (int)Evaluate(parts[2], PC, rootLineNumber)); case "<": return(Evaluate(parts[0], PC, rootLineNumber) < Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case "<=": return(Evaluate(parts[0], PC, rootLineNumber) <= Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case ">": return(Evaluate(parts[0], PC, rootLineNumber) > Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case ">=": return(Evaluate(parts[0], PC, rootLineNumber) >= Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case "==": return(Evaluate(parts[0], PC, rootLineNumber) == Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case "!=": return(Evaluate(parts[0], PC, rootLineNumber) != Evaluate(parts[2], PC, rootLineNumber) ? 1UL : 0UL); case "&": return(Evaluate(parts[0], PC, rootLineNumber) & Evaluate(parts[2], PC, rootLineNumber)); case "^": return(Evaluate(parts[0], PC, rootLineNumber) ^ Evaluate(parts[2], PC, rootLineNumber)); case "|": return(Evaluate(parts[0], PC, rootLineNumber) | Evaluate(parts[2], PC, rootLineNumber)); case "&&": return((Evaluate(parts[0], PC, rootLineNumber) == 1 && Evaluate(parts[2], PC, rootLineNumber) == 1) ? 1UL : 0UL); case "||": return((Evaluate(parts[0], PC, rootLineNumber) == 1 || Evaluate(parts[2], PC, rootLineNumber) == 1) ? 1UL : 0UL); } } else { // Interpret value if (expression == "$") { return(PC); } else if (expression.StartsWith("0x")) // Hex { return(Convert.ToUInt64(expression.Substring(2), 16)); } else if (expression.StartsWith("$") || (expression.EndsWith("h") && expression.Remove(expression.Length - 1).ToLower().Count(c => !"0123456789abcdef".Contains(c)) == 0)) { return(Convert.ToUInt64(expression.Trim('$', 'h'), 16)); } else if (expression.StartsWith("0b")) // Binary { return(Convert.ToUInt64(expression.Substring(2), 2)); } else if (expression.StartsWith("$") || (expression.EndsWith("h") && expression.Remove(expression.Length - 1).ToLower().Count(c => !"01".Contains(c)) == 0)) { return(Convert.ToUInt64(expression.Trim('%', 'b'), 2)); } else if (expression.StartsWith("0o")) // Octal { return(Convert.ToUInt64(expression.Substring(2), 8)); } else if (expression == "true") { return(1); } else if (expression == "false") { return(0); } else if (expression.StartsWith("'") && expression.EndsWith("'")) { var character = expression.Substring(1, expression.Length - 2).Unescape(); if (character.Length != 1) { throw new InvalidOperationException("Invalid character."); } return(Settings.Encoding.GetBytes(character)[0]); } else { // Check for number bool number = true; for (int i = 0; i < expression.Length; i++) { if (!char.IsNumber(expression[i])) { number = false; } } if (number) // Decimal { return(Convert.ToUInt64(expression)); } else { // Look up reference var symbol = expression.ToLower(); if (symbol.StartsWith(".")) { symbol = symbol.Substring(1) + "@" + LastGlobalLabel; } if (Symbols.ContainsKey(symbol)) { return(Symbols[symbol].Value); } throw new KeyNotFoundException("The specified symbol was not found."); } } } throw new InvalidOperationException("Invalid expression"); }
public bool HasSymbolInfo(string name) { return(Symbols.ContainsKey(name)); }