public CanCauseError <byte[]> GetDataUnit(IExpression <int>[] parameters, Func <string, int?> getSymbolValue, ScopeStructure <int> scope) { byte[] code = this.baseData.Clone() as byte[]; localLabels.Clear(); externLabels.Clear(); for (int index = 0; index < parameters.Length; ++index) { TemplateParameter paramTemp = this[index]; if (paramTemp.lenght > 0) { if (scope.GetRegisteredASMCLabels().Exists(o => o == parameters[index].ToString()) && !scope.IsLocalLabelExisted(parameters[index].ToString())) { paramTemp.InsertValues(new int [1], code); AddExternLabel(paramTemp.position / 8, parameters[index].ToString()); } else { CanCauseError <int[]> values = CodeTemplate.GetValues(parameters[index], paramTemp, getSymbolValue, this.pointerMaker); if (values.CausedError) { return(values.ConvertError <byte[]>()); } paramTemp.InsertValues(values.Result, code); if (scope.IsLocalLabelExisted(parameters[index].ToString())) { localLabels.Add(paramTemp.position / 8, parameters[index].ToString()); } } } } return((CanCauseError <byte[]>)code); }
private void AddPointerListCodes(int offset, int[] pointerList, SortedDictionary <int, Code> lines, ILog log) { CanCauseError <ICodeTemplate> template = this.codeStorage.FindTemplate("POIN", Priority.pointer); if (template.CausedError) { log.AddError(template.ErrorMessage); } else { ICodeTemplate result = template.Result; int index1 = 0; for (int index2 = 0; index2 < this.pointerList.Length; ++index2) { List <string> stringList = new List <string> (); stringList.Add(result.Name); int offset1 = offset + 4 * index1; for (int index3 = 0; index3 < this.pointerList [index2].Length; ++index3) { stringList.Add(pointerList [index1].ToHexString("$")); ++index1; } lines [offset1] = new Code(stringList.ToArray(), result, this.pointerList [index2].Length * 4, offset1); } } }
public CanCauseError <ICodeTemplate> FindTemplate(byte[] code, int index, IEnumerable <Priority> allowedPriorities) { int num = (int)code[index] + (int)code[index + 1] * 256; foreach (Priority allowedPriority in allowedPriorities) { List <ICodeTemplate> codeTemplateList; if (this.disassemblyCodes.TryGetValue(new KeyValuePair <Priority, int>(allowedPriority, num), out codeTemplateList) || num != 0 && this.disassemblyCodes.TryGetValue(new KeyValuePair <Priority, int>(allowedPriority, 0), out codeTemplateList)) { List <ICodeTemplate> collection = new List <ICodeTemplate>(); foreach (ICodeTemplate codeTemplate in codeTemplateList) { if (codeTemplate.CanBeDisassembled && codeTemplate.Matches(code, index)) { collection.Add(codeTemplate); } } if (collection.Count > 0) { return(CanCauseError <ICodeTemplate> .NoError(collection.Max <ICodeTemplate>(this.templateComparer))); } } } return(CanCauseError <ICodeTemplate> .Error("No code found.")); }
public CanCauseError <byte[]> GetData(IExpression <int>[] code, Func <string, int?> getSymbolValue, ScopeStructure <int> scope) { if (!canBeRepeated) { return(GetDataUnit(code, getSymbolValue, scope)); } if (code.Length == 0) { return(CanCauseError <byte[]> .Error("Encountered {0} code with no parameters", Name)); } List <byte> byteList = new List <byte>(code.Length * this.LengthInBytes); int num = code.Length / AmountOfParams; for (int index = 0; index < num; ++index) { CanCauseError <byte[]> dataUnit = GetDataUnit(new IExpression <int>[1] { code[index] }, getSymbolValue, scope); if (dataUnit.CausedError) { return(dataUnit.ConvertError <byte[]>()); } byteList.AddRange(dataUnit.Result); } return((CanCauseError <byte[]>)byteList.ToArray()); }
private string ExpressionToString(IExpression <int> exp, ScopeStructure <int> scope) { switch (exp.Type) { case EAExpressionType.Code: Code <int> code = exp as Code <int>; return(code.CodeName.Name + ((IEnumerable <IExpression <int> >)code.Parameters).Select((x => this.ExpressionToString(x, scope))).ToElementWiseString <string> (" ", " ", "")); case EAExpressionType.XOR: case EAExpressionType.AND: case EAExpressionType.OR: case EAExpressionType.LeftShift: case EAExpressionType.RightShift: case EAExpressionType.ArithmeticRightShift: case EAExpressionType.Division: case EAExpressionType.Multiply: case EAExpressionType.Modulus: case EAExpressionType.Minus: case EAExpressionType.Sum: case EAExpressionType.Value: case EAExpressionType.Symbol: CanCauseError <int> canCauseError = Folding.Fold(exp, (y => this.GetSymbolValue(scope, y))); if (canCauseError.CausedError) { return(exp.ToString()); } return(canCauseError.Result.ToHexString("0x")); case EAExpressionType.List: return(exp.GetChildren().ToElementWiseString <IExpression <int> > (", ", "[", "]")); default: throw new ArgumentException("malformed tree"); } }
private void HandleBuiltInAssert(Code <int> code, ScopeStructure <int> scope) { if (code.ParameterCount.IsInRange(1, 1)) { if (code [0] is ExpressionList <int> ) { this.AddNotAtomTypeParameter <int> (code [0]); } else { CanCauseError <int> error = Folding.Fold(code [0], (x => this.GetSymbolValue(scope, x))); if (error.CausedError) { this.AddError((IExpression <int>)code, error); } else if (error.Result < 0) { this.AddError(code, "Assertion failed."); } } } else { this.AddNotCorrectParameters <int> (code, 1); } }
private void HandleBuiltInProtect(Code <int> code, ScopeStructure <int> scope) { if (code.ParameterCount.IsInRange(1, 1)) { CanCauseError <int> canCauseError = Folding.Fold(code [0], (x => this.GetSymbolValue(scope, x))); if (!canCauseError.CausedError) { this.protectedRegions.Add(new Tuple <int, int> (canCauseError.Result, 4)); } } else if (code.ParameterCount.IsInRange(2, 2)) { CanCauseError <int> firstParam = Folding.Fold(code [0], (x => this.GetSymbolValue(scope, x))); CanCauseError <int> secondParam = Folding.Fold(code [1], (x => this.GetSymbolValue(scope, x))); if (!firstParam.CausedError && !secondParam.CausedError) { if (firstParam.Result < secondParam.Result) { this.protectedRegions.Add(new Tuple <int, int> (firstParam.Result, secondParam.Result - firstParam.Result)); } else { this.log.AddWarning("Protected region not valid (end offset not after start offset). No region protected."); } } } else { this.AddNotCorrectParameters <int> (code, 2); } }
private void HandleBuiltInOffsetAlign(Code <int> code, ScopeStructure <int> scope) { if (code.ParameterCount.IsInRange(1, 1)) { if (code [0] is ExpressionList <int> ) { this.AddNotAtomTypeParameter <int> (code [0]); } else { CanCauseError <int> error = Folding.Fold(code [0], (x => this.GetSymbolValue(scope, x))); if (error.CausedError) { this.AddError <int, int> ((IExpression <int>)code, error); } else { this.currentOffset = this.currentOffset.ToMod(error.Result); } } } else { this.AddNotCorrectParameters <int> (code, 1); } }
private void HandleCodeSecondPass( IExpression <T> expression, AssemblyContext <T> assemblyContext) { Code <T> code = (Code <T>)expression; if (code.IsEmpty) { return; } if (HandleBuiltInCode(code, assemblyContext, true)) { return; } int codeOffset; ICodeTemplate codeTemplate; if (!assemblyContext.TryGetCodeData(code, out codeOffset, out codeTemplate)) { return; } assemblyContext.CurrentOffset = codeOffset; if (codeOffset % codeTemplate.OffsetMod != 0) { assemblyContext.AddError(code, "Code {0}'s offset {1} is not divisible by {2}", codeTemplate.Name, codeOffset.ToHexString("$"), codeTemplate.OffsetMod); } if (assemblyContext.Output.BaseStream.Position != assemblyContext.CurrentOffset) { if (!assemblyContext.Output.BaseStream.CanSeek) { assemblyContext.AddError(code, "Stream cannot be seeked."); } else { assemblyContext.Output.BaseStream.Seek(assemblyContext.CurrentOffset, SeekOrigin.Begin); } } CanCauseError <byte[]> rawData = codeTemplate.GetData(code.Parameters, x => GetSymbolVal(x, assemblyContext), intType, pointerMaker); if (rawData.CausedError) { assemblyContext.AddError(code, rawData); } else { assemblyContext.Log.AddMessage(code.ToString() + rawData.Result.ToElementWiseString(" ", "[", "]")); assemblyContext.Output.Write(rawData.Result); } assemblyContext.CurrentOffset = (int)assemblyContext.Output.BaseStream.Position; }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { CanCauseError canCauseError; if (parameters.Length > 1) { int length = parameters[0].IndexOf('('); int num = parameters[0].LastIndexOf(')'); string[] strArray; string name; if (length != -1 && num != -1 && length < num) { strArray = parameters[0].Substring(length + 1, num - length - 1).Split(macroSeparators, uniters); name = parameters[0].Substring(0, length); } else { strArray = new string[0]; name = parameters[0]; } for (int index = 0; index < strArray.Length; ++index) { strArray[index] = strArray[index].Trim(); } if (name.Equals(parameters[1])) { canCauseError = CanCauseError.Error("Defining something as itself. "); } else if (!host.DefCol.IsValidName(name)) { canCauseError = CanCauseError.Error(name + " is not valid name to define."); } else if (host.IsValidToDefine(name)) { canCauseError = CanCauseError.Error(name + " cannot be redefined."); } else { if (host.DefCol.ContainsName(name, strArray)) { host.Log.AddWarning(host.Input.GetPositionString() + ", Warning: Redefining " + name); host.DefCol.Remove(name, strArray); } host.DefCol.Add(name, parameters[1].Trim('"'), strArray); canCauseError = CanCauseError.NoError; } } else if (parameters.Length == 1) { host.DefCol.Add(parameters[0], ""); canCauseError = CanCauseError.NoError; } else { canCauseError = CanCauseError.NoError; } return(canCauseError); }
private void HandleDirective(string line) { string[] parameters1 = Crazycolorz5.Parser.SplitToParameters(line); string key; int length; if (parameters1[0].Equals("#")) { key = parameters1[1]; length = parameters1.Length - 2; } else { key = parameters1[0].TrimStart('#'); length = parameters1.Length - 1; } string[] parameters2 = new string[length]; Array.Copy(parameters1, parameters1.Length - length, (Array)parameters2, 0, length); IDirective parameterized; if (this.directives.TryGetValue(key, out parameterized)) { string error1; if (parameterized.Matches("Directive " + key, length, out error1)) { if (parameterized.RequireIncluding) { if (!this.include.And()) { return; } CanCauseError error2 = parameterized.Apply(parameters2, (IDirectivePreprocessor)this); if (!(bool)error2) { return; } this.messageLog.AddError(this.inputStream.GetErrorString(error2)); } else { CanCauseError error2 = parameterized.Apply(parameters2, (IDirectivePreprocessor)this); if (!(bool)error2) { return; } this.messageLog.AddError(this.inputStream.GetErrorString(error2)); } } else { this.messageLog.AddError(this.inputStream.GetErrorString(error1)); } } else { this.messageLog.AddError(this.inputStream.GetErrorString(": No directive with the name #" + key + " exists")); } }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { if (host.Include.Count <= 0) { return(CanCauseError.Error("#endif used without #ifdef or #ifndef.")); } host.Include.Pop(); return(CanCauseError.NoError); }
private CanCauseError <ICodeTemplate> GetTemplateFrom(string codeName, Language.Types.Type[] parameterTypes, List <ICodeTemplate> templates) { IEnumerable <ICodeTemplate> codeTemplates = templates.Where <ICodeTemplate>((Func <ICodeTemplate, bool>)(template => template.Matches(parameterTypes))); if (codeTemplates.Any <ICodeTemplate>()) { return(CanCauseError <ICodeTemplate> .NoError(codeTemplates.Min <ICodeTemplate>(this.templateComparer))); } return(CanCauseError <ICodeTemplate> .Error(parameterTypes.Length != 0? "Incorrect parameters in code {0} {1}" : "Incorrect parameters in code {0}", (object)codeName, (object)((IEnumerable <Language.Types.Type>)parameterTypes).ToElementWiseString <Nintenlord.Event_Assembler.Core.Code.Language.Types.Type>(" ", "", ""))); }
public CanCauseError AddNewSymbol(string symbol, IExpression <T> value) { if (definedSymbols.ContainsKey(symbol)) { return(CanCauseError.Error("Symbol \"{0}\" already exists (ignoring second definition).", symbol)); } definedSymbols[symbol] = value; return(CanCauseError.NoError); }
private bool HandleBuiltInCodeLayout(Code <int> code, ScopeStructure <int> scope) { switch (code.CodeName.Name) { case messagePrinterCode: case errorPrinterCode: case warningPrinterCode: return(true); case currentOffsetCode: case offsetAligner: HandleBuiltInOffsetAlign(code, scope); return(true); case offsetChanger: HandleBuiltInOffsetChange(code, scope); return(true); case offsetPusher: HandleBuiltInOffsetPush(code, scope); return(true); case offsetPopper: HandleBuiltInOffsetPop(code, scope); return(true); case assertion: return(true); case protectCode: HandleBuiltInProtect(code, scope); return(true); case ThumbAssembly: case ARMAssembly: return(true); case ExternSymbol: CanCauseError err = scope.AddNewSymbol(code.Parameters[0].ToString(), new ValueExpression <int>(-1, new FilePosition())); if (err.CausedError) { AddWarning((IExpression <int>)code, err.ErrorMessage); } return(true); case GlobalSymbol: case sectionMaker: case sectionEnd: return(true); default: return(false); } }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { if (host.Include.Count <= 0) { return(CanCauseError.Error("#else before any #ifdef or #ifndef.")); } bool flag = host.Include.Pop(); host.Include.Push(!flag); return(CanCauseError.NoError); }
private void HandleBuiltInOffsetChange(Code <int> code, ScopeStructure <int> scope) { if (code.ParameterCount.IsInRange(1, 1) && !(code [0] is ExpressionList <int>)) { CanCauseError <int> canCauseError = Folding.Fold(code [0], (x => this.GetSymbolValue(scope, x))); if (!canCauseError.CausedError) { this.currentOffset = canCauseError.Result; } } }
public CanCauseError <bool> SecondPass(string[] code, Context context) { if (context.MoveToNextScope()) { return(false); } else { return(CanCauseError <bool> .Error("INTERNAL: No scope to move to.")); } }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { /* * for (int i=1; i<parameters.Length; i++) * { * CanCauseError<string> res = host.DefCol.ApplyDefines(parameters[i]); * if (!res.CausedError) * parameters[i] = res.Result; * }*/ string file = IO.IOHelpers.FindFile(host.Input.CurrentFile, getFileName(parameters[0])); if (file.Length <= 0) { return(CanCauseError.Error("Tool " + parameters[0] + " not found.")); } //from http://stackoverflow.com/a/206347/1644720 // Start the child process. System.Diagnostics.Process p = new System.Diagnostics.Process(); // Redirect the output stream of the child process. p.StartInfo.WorkingDirectory = Path.GetDirectoryName(host.Input.CurrentFile); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = file; string[] passedParams = parameters.GetRange(1); for (int i = 0; i < passedParams.Length; i++) { if (passedParams[i].ContainsWhiteSpace()) { passedParams[i] = "\"" + passedParams[i] + "\""; } } p.StartInfo.Arguments = passedParams.ToElementWiseString(" ", "", " --to-stdout"); p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. MemoryStream outputBytes = new MemoryStream(); p.StandardOutput.BaseStream.CopyTo(outputBytes); p.WaitForExit(); byte[] output = outputBytes.GetBuffer().GetRange(0, (int)outputBytes.Length); if (output.Length >= 7 && Encoding.ASCII.GetString(output.GetRange(0, 7)) == "ERROR: ") { return(CanCauseError.Error(Encoding.ASCII.GetString(output.GetRange(7)))); } host.Input.AddBytes(output); return(CanCauseError.NoError); }
public CanCauseError FirstPass(string[] code, Context context) { if (context.ScopesOnStack == 0) { return(CanCauseError.Error("No scope to end")); } else { context.EndScope(); return(CanCauseError.NoError); } }
public CanCauseError AddNewSymbol(string symbol, IExpression <T> value) { //Detect if adds cycles if (definedSymbols.ContainsKey(symbol)) { return(CanCauseError.Error("Symbol already exists.")); } else { definedSymbols[symbol] = value; return(CanCauseError.NoError); } }
public CanCauseError <int> Parse(string text) { int result; if (text.TryGetValue(out result)) { return(result); } else { return(CanCauseError <int> .Error("Improperly formatted integer.")); } }
public CanCauseError <string> Replace(string textToEdit) { StringBuilder textToEdit1 = new StringBuilder(textToEdit); CanCauseError canCauseError = this.Replace(textToEdit1); if (canCauseError.CausedError) { return(CanCauseError <string> .Error(canCauseError.ToString())); } return(CanCauseError <string> .NoError(textToEdit1.ToString())); }
public CanCauseError FirstPass(string[] code, Context context) { int newOffset; if (code[1].GetMathStringValue(out newOffset)) { context.Offset = newOffset; return(CanCauseError.NoError); } else { return(CanCauseError.Error(code[1] + " is not a valid offset.")); } }
public CanCauseError <string> Replace(string textToEdit) { StringBuilder bldr = new StringBuilder(textToEdit); var result = this.Replace(bldr); if (result.CausedError) { return(CanCauseError <string> .Error(result.ToString())); } else { return(CanCauseError <string> .NoError(bldr.ToString())); } }
private CanCauseError HandleParameter(string parameter) { switch (paramCount) { case 0: operation = parameter; break; case 1: language = parameter; break; case 2: DisassemblyMode disassemblyMode; if (!parameter.TryGetEnum(out disassemblyMode)) { return(CanCauseError.Error("{0} is not a valid disassembly mode.", parameter)); } this.disassemblyMode = disassemblyMode; break; case 3: int offset; if (!parameter.TryGetValue(out offset)) { return(CanCauseError.Error("{0} is not a valid offset.", parameter)); } this.offset = offset; break; case 4: Priority priority; if (!parameter.TryGetEnum(out priority)) { return(CanCauseError.Error("{0} is not a valid priority.", parameter)); } this.priority = priority; break; case 5: int size; if (!parameter.TryGetValue(out size) || size < 0) { return(CanCauseError.Error("{0} is not a valid size.", parameter)); } this.size = size; break; default: return(CanCauseError.Error("Too many parameters.")); } paramCount++; return(CanCauseError.NoError); }
public CanCauseError FirstPass(string[] code, Context context) { int value; if (code[1].TryGetValue(out value)) { context.Offset = context.Offset.ToMod(value); return(CanCauseError.NoError); } else { return(CanCauseError.Error(code[1] + " is not a valid number.")); } }
public CanCauseError <IExpression <T> > GetSymbolValue(string symbol) { IExpression <T> result; if (definedSymbols.TryGetValue(symbol, out result)) { return(CanCauseError <IExpression <T> > .NoError(result)); } if (ParentScope != null) { return(ParentScope.GetSymbolValue(symbol)); } return(CanCauseError <IExpression <T> > .Error("Symbol {0} not defined", symbol)); }
public CanCauseError Replace(StringBuilder textToEdit) { if (currentIter == maxIter) { return(CanCauseError.Error("Maximum amount of replacement iterations exceeeded while applying macro.")); } ++currentIter; foreach (KeyValuePair <int, Tuple <int, IMacro, string[]> > macro in FindMacros(textToEdit)) { string[] strArray = macro.Value.Item3; string[] parameters = new string[strArray.Length]; for (int index = 0; index < strArray.Length; ++index) { if (macro.Value.Item2.ShouldPreprocessParameter(index)) { CanCauseError <string> canCauseError = this.Replace(strArray[index]); if (canCauseError.CausedError) { return((CanCauseError)canCauseError); } parameters[index] = canCauseError.Result; } else { parameters[index] = strArray[index]; } } CanCauseError <string> canCauseError1 = this.Replace(macro.Value.Item2.Replace(parameters)); if (canCauseError1.CausedError) { return((CanCauseError)canCauseError1); } string @string = textToEdit.Substring(macro.Key, macro.Value.Item1).ToString(); textToEdit.Replace(@string, canCauseError1.Result, macro.Key, @string.Length); } --currentIter; return(CanCauseError.NoError); }
public CanCauseError <byte[]> GetData(IExpression <int>[] code, Func <string, int?> getSymbolValue) { List <byte> byteList = new List <byte>(this.GetLengthBytes(code)); for (int index = 0; index < code.Length; ++index) { CanCauseError <int> aResult = Folding.TryFold(code[index], getSymbolValue); if (aResult.CausedError) { return(aResult.ConvertError <byte[]>()); } byteList.AddRange((IEnumerable <byte>)BitConverter.GetBytes((short)aResult.Result)); } //int num = code.Length / this.AmountOfParams; return((CanCauseError <byte[]>)byteList.ToArray()); //throw new NotImplementedException(); }