Ejemplo n.º 1
0
        public ASTree GeneratePextCode(string[] arguments)
        {
            ASTree pextTree = new ASTree();

            //Preventing memory loss simply by storing B register content firstly.
            //To do it we have to move arguments which represents B on the first poistion.
            //We will use some simple sorting
            for (int i = 1; i < arguments.Length; i++)
            {
                for (int j = i; j < arguments.Length; j++)
                {
                    if (arguments[j] == "b")
                    {
                        dictLine cache = new dictLine();
                        //save
                        cache.argAddr = placeholders[i - 1];
                        cache.argName = arguments[i];
                        //mov
                        placeholders[i - 1] = placeholders[j - 1];
                        arguments[i]        = arguments[j];
                        //mov from cache
                        placeholders[j - 1] = cache.argAddr;
                        arguments[j]        = cache.argName;
                        break;
                    }
                }
            }
            pextTree.Add(new Swm(mountPoint));
            for (int i = 1; i < arguments.Length; i++)
            {
                if (arguments[i] == "a")
                {
                    pextTree.Add(new Mov("b", "a")); //may cause memory loss (but fixed earlier)
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
                else if (arguments[i] == "b")
                {
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
                else
                {
                    pextTree.Add(new Mov("b", arguments[i]));
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
            }
            pextTree.Add(new Ld(result.ToString()));
            return(pextTree);
        }
Ejemplo n.º 2
0
        public Assembly(string programName)
        {
            this.programName = programName;

            string[] lines = CodeIO.LoadFile(programName);
            //Clearing Code
            //Removing comments and trimming
            Utilities.Utilities.VerbouseOut("-=-=Clearing code from comments=-=-");
            lines = ClearCode(lines);
            Utilities.Utilities.VerbouseOut("-=-=Parsing lines=-=-");
            parsed = SplitCode(lines);
            //Get hat
            Utilities.Utilities.VerbouseOut("-=-=Parsing libraries imports=-=-");
            ArrayList imports = GetImports(parsed);

            //Catching imports
            Utilities.Utilities.VerbouseOut("Importing");
            importManager = new Preprocessor(imports);
            //Calling preprocessor
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Inserting macroses...");
            for (int i = 0; i < importManager.CountMacroses(); i++)
            {
                parsed = InsertAllMacro(parsed);
            }
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Updated code: ");
            if (Program.verboseMode)
            {
                for (int j = 0; j < parsed.Length; j++)
                {
                    Console.Write(j + ":\t");
                    for (int k = 0; k < parsed[j].Length; k++)
                    {
                        Console.Write(parsed[j][k] + " ");
                    }
                    Console.WriteLine();
                }
            }
            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Finding definitions...");
            parsed = importManager.CatchDefines(parsed);
            var safer = new List <string[]>();

            for (int i = 0; i < parsed.Length; i++)
            {
                if (parsed[i] != null)
                {
                    safer.Add(parsed[i]);
                }
            }
            parsed = safer.ToArray();
            Utilities.Utilities.VerbouseOut("-=-=Parsing pExts=-=-");
            ArrayList pexts = GetPexts(parsed);

            importManager.ImportPexts(pexts);
            Utilities.Utilities.VerbouseOut("Removing preprocessor code from source...");
            parsed = ClearHatAfterImport(parsed);
            //Catching labels
            Utilities.Utilities.VerbouseOut("Parsing labels...");
            CodeLine[] code = LabelCatcher(parsed);
            //Converting code to object form

            Utilities.Utilities.VerbouseOut("PREPROCESSOR", "Cleared code: ");
            if (Program.verboseMode)
            {
                for (int j = 0; j < parsed.Length; j++)
                {
                    Console.Write(j + ":\t");
                    for (int k = 0; k < parsed[j].Length; k++)
                    {
                        Console.Write(parsed[j][k] + " ");
                    }
                    Console.WriteLine();
                }
            }

            Utilities.Utilities.VerbouseOut("Converting code to object form");
            for (int i = 0; i < code.Length; i++)
            {
                string opcode = code[i].code[0].ToLower();
                string label  = code[i].label;
                if (opcode == "add")
                {
                    program.Add(new Add(code[i].code[1], code[i].code[2]));
                }
                else if (opcode == "in")
                {
                    if (code[i].code.Length == 2)
                    {
                        program.Add(new In(code[i].code[1]));
                    }
                    else if (code[i].code.Length > 2)
                    {
                        program.Add(new In(code[i].code[1], code[i].code[2]));
                    }
                }
                else if (opcode == "jmp")
                {
                    program.Add(new Jmp(code[i].code[1]));
                }
                else if (opcode == "jnc")
                {
                    program.Add(new Jnc(code[i].code[1]));
                }
                else if (opcode == "ld")
                {
                    program.Add(new Ld(code[i].code[1]));
                }
                else if (opcode == "mov")
                {
                    if (code[i].code.Length == 3)
                    {
                        program.Add(new Mov(code[i].code[1], code[i].code[2]));
                    }
                    else if (code[i].code.Length > 3)
                    {
                        program.Add(new Mov(code[i].code[1], code[i].code[2], code[i].code[3]));
                    }
                }
                else if (opcode == "out")
                {
                    if (code[i].code.Length == 2)
                    {
                        program.Add(new Out(code[i].code[1]));
                    }
                    else if (code[i].code.Length > 2)
                    {
                        program.Add(new Out(code[i].code[1], code[i].code[2]));
                    }
                }
                else if (opcode == "st")
                {
                    program.Add(new St(code[i].code[1]));
                }
                else if (opcode == "swi")
                {
                    program.Add(new Swi(code[i].code[1]));
                }
                else if (opcode == "swm")
                {
                    program.Add(new Swm(code[i].code[1]));
                }
                else
                {
                    program.InsertSubTree(program.Count, importManager.LookUpPext(opcode, code[i].code));
                }
                if (label != null)
                {
                    program.AddLabel(label);
                }
            }
            Utilities.Utilities.VerbouseOut("Linking labels...");
            for (int i = 0; i <= program.Count; i++)
            {
                if (program[i].opcode is Jmp || program[i].opcode is Jnc)
                {
                    if (program[i].opcode.Arg1 == null)
                    {
                        ((PCChanger)program[i].opcode).Link = program[program[i].opcode.FastAdd.toInt() + 1];
                        Utilities.Utilities.VerbouseOut("LABEL_LINKER", "Linked: " + program[i].opcode.Name + " " + program[i].opcode.FastAdd.ToString() + " to " + ((PCChanger)program[i].opcode).Link.ToString());
                    }
                    else
                    {
                        ((PCChanger)program[i].opcode).Link = program.GetLabel(program[i].opcode.Arg1);
                        Utilities.Utilities.VerbouseOut("LABEL_LINKER", "Linked: " + program[i].opcode.Name + " " + program[i].opcode.Arg1.ToString() + " to " + ((PCChanger)program[i].opcode).Link.ToString());
                    }
                }
            }
            if (Program.verboseMode)
            {
                Console.WriteLine("---Final code---");
                int length = program.Count;
                for (int i = 1; i <= length; i++)
                {
                    Console.Write(i + ":\t");
                    IOpcode line = program.Get(i).opcode;
                    Console.Write(line.Name + "\t");
                    if (line.Arg1 != null)
                    {
                        Console.Write(line.Arg1 + "\t");
                    }
                    if (line is Mov)
                    {
                        if (line.Arg2 != null)
                        {
                            Console.Write(line.Arg2 + "\t");
                        }
                    }
                    if (line.FastAdd != null)
                    {
                        Console.Write(line.FastAdd.GetValue());
                    }
                    Console.WriteLine();
                }
            }
        }