public bool ProcessInstruction(ExtendedInstruction test) { ProcessInstructionInternal(new ExtendedInstruction(test), "test", "test", "dat1", "dat2"); ProcessInstructionInternal(new ExtendedInstruction(test), "test", "dat3", "dat1", "dat2"); ProcessInstructionInternal(new ExtendedInstruction(test), "dat3", "test", "dat1", "dat2"); ProcessInstructionInternal(new ExtendedInstruction(test), "dat3", "dat3", "dat1", "dat2"); ProcessInstructionInternal(new ExtendedInstruction(test), "dat1", "dat2", "dat1", "dat2"); ProcessInstructionInternal(new ExtendedInstruction(test), "dat2", "dat1", "dat1", "dat2"); return false; }
public ExtendedInstruction(ExtendedInstruction src) : base(src) { Address = src.Address; Label = src.Label; Comment = src.Comment; ExpressionA = src.ExpressionA; ExpressionB = src.ExpressionB; OriginalInstruction = src.OriginalInstruction; }
public bool ProcessInstruction(ExtendedInstruction test) { string name = "Perform" + test.Operation + test.Modifier + test.ModeA + test.ModeB; /* DynamicMethod dm = new DynamicMethod(name, typeof(void), new Type[] {}, typeof(FastEngine)); ILGenerator generator = dm.GetILGenerator(); DynamicILInfo info = dm.GetDynamicILInfo(); info.GetTokenFor("") */ //new CSharpCodeProvider().CompileAssemblyFromSource(); return false; }
public bool ProcessInstruction(ExtendedInstruction instruction) { if (last.Operation != instruction.Operation) { if (sw != null) { sw.WriteLine(" }"); sw.WriteLine("}"); sw.Close(); } sw = new StreamWriter(@"N:\nMars\nMars.Fngine\Fngine\Fngine01Instructions" + instruction.Operation + ".cs"); sw.WriteLine("using nMars.RedCode;"); sw.WriteLine("using nMars.Engine;"); sw.WriteLine("namespace nMars.Fngine.Fngine"); sw.WriteLine("{"); sw.WriteLine(" public partial class Fngine"); sw.WriteLine(" {"); } last = instruction; swm.WriteLine(" case " + instruction.Code + ": return " + instruction.FunctionName + ";"); sw.WriteLine(" public void "); sw.WriteLine(instruction.FunctionName); sw.WriteLine("(int ip, EngineWarrior warrior)"); sw.WriteLine(" {"); string sdecl; string saddra; string saddrb; string sexec; GetBody(instruction, out sdecl, out saddra, out saddrb, out sexec); sw.Write(sdecl); sw.Write(saddra); sw.Write(saddrb); sw.Write(sexec); sw.WriteLine(" }"); return false; }
public static void GetBody(ExtendedInstruction instruction, out string sdecl, out string saddra, out string saddrb, out string sexec) { StringBuilder sbexec = new StringBuilder(); ExecuteInstruction(sbexec, instruction.Operation, instruction.Modifier); sexec = sbexec.ToString(); StringBuilder sbaddrb = new StringBuilder(); GetEffectiveAddress(sbaddrb, sexec, instruction.ModeB, "reg_AdrB", "reg_AB_Value", "reg_IR_ValueB", "reg_IR_ValueCpyB"); saddrb = sbaddrb.ToString(); StringBuilder sbaddra = new StringBuilder(); GetEffectiveAddress(sbaddra, sexec + saddrb, instruction.ModeA, "reg_AdrA", "reg_AA_Value", "reg_IR_ValueA", "reg_IR_ValueCpyA"); saddra = sbaddra.ToString(); StringBuilder sbdecl = new StringBuilder(); string sall = saddra + saddrb + sexec; string sinit = saddra + saddrb; GetDecl(instruction, saddra, sall, sbdecl, sinit); sdecl = sbdecl.ToString(); }
public static void Generate(ProcessInstruction processor) { foreach (Operation operation in Enum.GetValues(typeof(Operation))) { foreach (Modifier modifier in Enum.GetValues(typeof(Modifier))) { if (modifier == Modifier.NULL) continue; foreach (Mode modeA in Enum.GetValues(typeof(Mode))) { if (modeA == Mode.NULL) continue; foreach (Mode modeB in Enum.GetValues(typeof(Mode))) { if (modeB == Mode.NULL) continue; ExtendedInstruction instruction = new ExtendedInstruction(operation, modifier, modeA, 0, modeB, 0); if (processor.Invoke(instruction)) return; } } } } }
public override void ExpandStatements(ExtendedWarrior warrior, WarriorParser parser, ref int currentAddress, int coreSize, bool evaluate) { //set all labels foreach (LabelName label in Labels) { parser.variables[label.GetFullName(parser, currentAddress)] = new Address(currentAddress); } ExtendedInstruction instruction; if (!evaluate) { instruction = new ExtendedInstruction(Operation, Modifier, A.Mode, Int32.MinValue, B.Mode, Int32.MinValue); instruction.Address = currentAddress; warrior.Add(instruction); } else { instruction = (ExtendedInstruction)warrior.Instructions[currentAddress]; instruction.ValueA = Instruction.Mod(A.Expression.Evaluate(parser, currentAddress, coreSize), coreSize); instruction.ValueB = Instruction.Mod(B.Expression.Evaluate(parser, currentAddress, coreSize), coreSize); instruction.ExpressionA = A.Expression.ToString(); instruction.ExpressionB = B.Expression.ToString(); if (instruction.ModeA == Mode.NULL) { instruction.ModeA=A.Expression.GetMode(parser, currentAddress); } if (instruction.ModeA == Mode.NULL) { instruction.ModeA = Mode.Direct; } if (instruction.ModeB == Mode.NULL) { instruction.ModeB=B.Expression.GetMode(parser, currentAddress); } if (instruction.ModeB == Mode.NULL) { instruction.ModeB = Mode.Direct; } if (instruction.Modifier == Modifier.NULL) { instruction.Modifier = Instruction.DefaultModifier(instruction.Operation, instruction.ModeA, instruction.ModeB); } if (Comments != null) { instruction.Comment = Comments[Comments.Count - 1]; } else { instruction.Comment = ""; } instruction.OriginalInstruction = OriginalInstruction; if (Labels.Count > 0) { instruction.Label = Labels[Labels.Count - 1].GetFullName(parser, currentAddress); } else { instruction.Label = ""; } } currentAddress++; parser.variables["CURLINE"] = new Value(currentAddress); }
private static void GetDecl(ExtendedInstruction instruction, string saddra, string sall, StringBuilder sbdecl, string sinit) { if (sall.Contains("reg_IR_ValueA")) if (sinit.Contains("reg_IR_ValueA =")) sbdecl.AppendLine(@"int reg_IR_ValueA;"); else sbdecl.AppendLine(@"int reg_IR_ValueA = ValuesA[ip];"); if (sall.Contains("reg_IR_ValueB")) if (sinit.Contains("reg_IR_ValueB =") && !saddra.Contains("= reg_IR_ValueB")) sbdecl.AppendLine(@"int reg_IR_ValueB;"); else sbdecl.AppendLine(@"int reg_IR_ValueB = ValuesB[ip];"); if (sall.Contains("reg_IR_ValueCpyA")) if (sinit.Contains("reg_IR_ValueCpyA =")) sbdecl.AppendLine(@"int reg_IR_ValueCpyA;"); else sbdecl.AppendLine(@"int reg_IR_ValueCpyA = ValuesA[ip];"); if (sall.Contains("reg_IR_ValueCpyB")) if (sinit.Contains("reg_IR_ValueCpyB =")) sbdecl.AppendLine(@"int reg_IR_ValueCpyB;"); else sbdecl.AppendLine(@"int reg_IR_ValueCpyB = ValuesB[ip];"); if (sall.Contains("reg_AdrA")) sbdecl.AppendLine(@"int reg_AdrA;"); if (sall.Contains("reg_AdrB")) sbdecl.AppendLine(@"int reg_AdrB;"); if (sall.Contains("reg_AA_Value")) if (sinit.Contains("reg_AA_Value =")) sbdecl.AppendLine(@"int reg_AA_Value;"); else sbdecl.AppendLine(@"int reg_AA_Value = ValuesA[ip];"); if (sall.Contains("reg_AB_Value")) if (sinit.Contains("reg_AB_Value =")) sbdecl.AppendLine(@"int reg_AB_Value;"); else sbdecl.AppendLine(@"int reg_AB_Value = ValuesA[ip];"); if (sall.Contains("reg_direct")) sbdecl.AppendLine(@"int reg_direct;"); if (sall.Contains("reg_jump")) sbdecl.AppendLine(@"bool reg_jump;"); if (sall.Contains("reg_die")) if (instruction.Operation==Operation.DAT) sbdecl.AppendLine(@"bool reg_die;"); else sbdecl.AppendLine(@"bool reg_die = false;"); if (sall.Contains("reg_psAddress")) sbdecl.AppendLine(@"int reg_psAddress;"); }
public void ProcessInstructionInternal(ExtendedInstruction test, string tA, string tB, string dA, string dB) { string name = "test-" + test.Operation + "." + test.Modifier + "-" + test.ModeA + tA + "-" + test.ModeB + tB + "-" + dA + dB + ".red"; ExtendedWarrior w = new ExtendedWarrior(rules); w.FileName = @"N:\nMarsRelease\regression\gen\" + name; w.Name = name; w.Author = "Pavel Savara"; w.Date = DateTime.Today.ToShortDateString(); ExtendedInstruction dat1 = new ExtendedInstruction(); dat1.Modifier = Modifier.A; dat1.Label = "dat1"; dat1.ValueA = rand.Next(0, Rules.DefaultRules.CoreSize - 1); dat1.ValueB = rand.Next(0, Rules.DefaultRules.CoreSize - 1); w.Add(dat1); ExtendedInstruction dat2 = new ExtendedInstruction(); dat2.Modifier = Modifier.B; dat2.Label = "dat2"; dat2.ValueA = rand.Next(0, Rules.DefaultRules.CoreSize - 1); dat2.ValueB = rand.Next(0, Rules.DefaultRules.CoreSize - 1); w.Add(dat2); ExtendedInstruction dat3 = new ExtendedInstruction(); dat3.Modifier = Modifier.AB; dat3.Label = "dat3"; dat3.ExpressionA = dA; dat3.ExpressionB = dB; w.Add(dat3); //tested instruction test.Label = "test"; test.ExpressionA = tA; test.ExpressionB = tB; w.Add(test); w.StartOffset = test.Address; ExtendedInstruction step = new ExtendedInstruction(); //step.Modifier = Modifier.BA; step.Label = "step"; w.Add(step); ExtendedInstruction skip = new ExtendedInstruction(); //skip.Modifier = Modifier.X; skip.Label = "skip"; w.Add(skip); ExtendedInstruction jump = new ExtendedInstruction(); //jump.Modifier = Modifier.I; jump.Label = "jump"; w.Add(jump); switch (test.ModeA) { case Mode.Direct: case Mode.Immediate: test.ExpressionA = "dat1"; break; default: break; } switch (test.ModeB) { case Mode.Direct: case Mode.Immediate: test.ExpressionB = "dat2"; break; default: break; } switch (test.Operation) { case Operation.JMZ: case Operation.JMP: case Operation.JMN: case Operation.DJN: case Operation.SLT: case Operation.SNE: switch (test.ModeA) { case Mode.Direct: test.ExpressionA = "skip"; break; case Mode.Immediate: //jump to same address File.Delete(w.FileName); return; case Mode.IndirectA: case Mode.PreDecIndirectA: case Mode.PostIncIndirectA: test.ExpressionA = "dat1"; dat1.ExpressionA = "skip"; break; case Mode.IndirectB: case Mode.PreDecIndirectB: case Mode.PostIncIndirectB: test.ExpressionA = "dat2"; dat2.ExpressionB = "skip"; break; default: throw new NotImplementedException(); } /*switch (test.ModeB) { case Mode.Direct: case Mode.Immediate: test.ExpressionB = "skip"; break; case Mode.IndirectA: case Mode.PreDecIndirectA: case Mode.PostIncIndirectA: test.ExpressionB = "dat1"; dat1.ExpressionA = "skip-test"; break; case Mode.IndirectB: case Mode.PreDecIndirectB: case Mode.PostIncIndirectB: test.ExpressionB = "dat2"; dat2.ExpressionB = "skip-test"; break; default: throw new NotImplementedException(); }*/ break; default: break; } DumpWarrior(w, ParserOptions.Full); Project project = new Project(rules); project.WarriorFiles.Add(w.FileName); parser.Parse(project, null); engine.BeginMatch(project); engine.NextStep(); if (engine.NextInstructionAddress == skip.Address) { skip.Operation = Operation.JMP; skip.ExpressionA = "cck0"; /* step.Label = "k1ll"; jump.Label = "k3ll"; */ } else if (engine.NextInstructionAddress == step.Address) { step.Operation = Operation.JMP; step.ExpressionA = "cck0"; /* skip.Label = "k2ll"; jump.Label = "k3ll"; */ } else if (engine.NextInstructionAddress == jump.Address) { jump.Operation = Operation.JMP; jump.ExpressionA = "cck0"; /* skip.Label = "k1ll"; step.Label = "k2ll"; */ } else if (engine.NextInstructionAddress == -1) { //died File.Delete(w.FileName); return; } else { while(true) { engine.EndMatch(null); engine.BeginMatch(project); engine.NextStep(); } } List<ExtendedInstruction> check=new List<ExtendedInstruction>(); for (int address = 0; address < rules.CoreSize; address++) { IRunningInstruction runningInstruction = engine[address]; if (!runningInstruction.Equals(empty) || address<=w.StartOffset) { ExtendedInstruction cck = new ExtendedInstruction(); cck.Operation = Operation.CMP; cck.Modifier = Modifier.I; cck.ExpressionA = "cpy" + check.Count; cck.ExpressionB = runningInstruction.Address + "+" + "dat1"; cck.Label = "cck" + check.Count; w.Add(cck); ExtendedInstruction killer=new ExtendedInstruction(); killer.Label = "klr" + check.Count; killer.Modifier = Modifier.I; w.Add(killer); ExtendedInstruction copy = new ExtendedInstruction(runningInstruction); copy.Label = "cpy" + check.Count; check.Add(copy); } } ExtendedInstruction loop = new ExtendedInstruction(); loop.Label = "loop"; loop.Operation = Operation.JMP; w.Add(loop); engine.EndMatch(null); foreach (ExtendedInstruction ccks in check) { w.Add(ccks); } DumpWarrior(w, ParserOptions.Full); parser.Parse(project, null); DumpWarrior(project.Warriors[0], ParserOptions.Pure); }
public void Add(ExtendedInstruction instruction) { instruction.Address = Instructions.Count; Instructions.Add(instruction); }