private static IEnumerable <InstructionData> GenerateInstructionDataWithSource(ReverseSymbolTable symTable,
                                                                                       TextSegmentAccessor textSegment,
                                                                                       SourceDebugData dbgData)
        {
            var instructions = new List <InstructionData>();

            using (var reader = File.OpenText(dbgData.SourceFilePath))
            {
                int currPgrmCtr = textSegment.StartingSegmentAddress;
                foreach (DisassembledInstruction inst in textSegment.RawInstructions)
                {
                    IParameterStringifier stringifier = InstructionTextMap.GetParameterStringifier(inst.InstructionType);
                    string formattedInstruction       = stringifier.GetFormattedInstruction(currPgrmCtr, inst, symTable);
                    string originalSourceLine         = string.Empty;
                    int    lineNum = -1;
                    if (dbgData.IsSourceTextAssociatedWithAddress(currPgrmCtr))
                    {
                        lineNum            = dbgData.GetLineNumberAssociatedWithAddress(currPgrmCtr);
                        originalSourceLine = reader.ReadLineAt(lineNum);
                        originalSourceLine = originalSourceLine.Trim();
                    }
                    var srcLineInfo     = new SourceLineInformation(dbgData.SourceFilePath, lineNum, currPgrmCtr, originalSourceLine);
                    var instructionElem = new InstructionData(inst.InstructionWord, currPgrmCtr, formattedInstruction, srcLineInfo);
                    instructions.Add(instructionElem);
                    currPgrmCtr += sizeof(int);
                }

                return(instructions);
            }
        }
 public InstructionData(int rawWord, int programCounterLoc, string instruction, SourceLineInformation srcData)
 {
     m_Instruction   = instruction;
     m_RawWord       = rawWord;
     m_ProgramCtrLoc = programCounterLoc;
     m_SrcLineInfo   = srcData;
 }
        private static IEnumerable <InstructionData> GenerateInstructionDataWithNoSource(ReverseSymbolTable symTable,
                                                                                         TextSegmentAccessor textSegment)
        {
            var instructions = new List <InstructionData>();
            int currPgrmCtr  = textSegment.StartingSegmentAddress;

            foreach (DisassembledInstruction inst in textSegment.RawInstructions)
            {
                IParameterStringifier stringifier = InstructionTextMap.GetParameterStringifier(inst.InstructionType);
                string formattedInstruction       = stringifier.GetFormattedInstruction(currPgrmCtr, inst, symTable);
                var    srcLineInfo     = new SourceLineInformation(string.Empty, -1, currPgrmCtr);
                var    instructionElem = new InstructionData(inst.InstructionWord, currPgrmCtr, formattedInstruction, srcLineInfo);
                instructions.Add(instructionElem);
                currPgrmCtr += sizeof(int);
            }

            return(instructions);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates code for a .text instruction in the file.
        /// </summary>
        /// <param name="fileName">The source file name.</param>
        /// <param name="asmLine">The line to parse.</param>
        /// <param name="objFile">The object file that will be written to.</param>
        /// <param name="currAlignment">The current specified alignment of the file. Unused for .text parsers.</param>
        public void GenerateCodeForSegment(string fileName, LineData asmLine, BasicObjectFile objFile, int currAlignment)
        {
            // scan to the first instruction.
            // this could share the same line as a label, so split on ':' and ','
            string[] tokenizedStr     = asmLine.Text.Split(new char[] { ',', ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            bool     foundInstruction = false;

            string instructionToken = string.Empty;

            for (int i = 0; i < tokenizedStr.Length && !foundInstruction; ++i)
            {
                string token = tokenizedStr[i].Trim();
                // we found our instruction. build a string from this token
                // to the end of the array.
                if (m_ParserFac.IsInstruction(token))
                {
                    foundInstruction = true;
                    instructionToken = token;
                }
            }

            if (foundInstruction)
            {
                // first, validate that the instruction is not the last token in the string.
                // try to parse the instruction parameters
                // get the substring starting at the index of the next character after the instruction
                string instSubstring = asmLine.Text.Substring(asmLine.Text.IndexOf(instructionToken) + instructionToken.Length);

                //split the substring at the comma to get the instruction parameters.
                string[] argTokens = instSubstring.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                // trim whitespace from the beginning and end of each token.
                argTokens = argTokens.Apply((str) => str.Trim()).ToArray();

                // find the parser for the instruction.
                IInstructionGenerator parser = m_ParserFac.GetProcessorForInstruction(instructionToken);

                // beq instructions should (hopefully) not generate multiple instructions..
                IEnumerable <int> generatedInstructions = parser.GenerateCodeForInstruction(m_CurrTextAddress, argTokens);

                var srcInfo = new SourceLineInformation(fileName, asmLine.LineNum, m_CurrTextAddress, asmLine.Text);
                objFile.AddSourceInformation(srcInfo);

                foreach (int generatedInstruction in generatedInstructions)
                {
                    objFile.AddInstruction(generatedInstruction);
                    m_CurrTextAddress += CommonConstants.BASE_INSTRUCTION_SIZE_BYTES;
                }
            }
            else
            {
                // if not an instruction (may be a symbol)
                // preprocesor instruction shouldn't bring us here.
                // make sure the user is not typing garbage.
                string symStr = tokenizedStr[0];
                if (!m_SymTbl.ContainsSymbol(symStr))
                {
                    throw new AssemblyException(asmLine.LineNum, "Unknown instruction \"" + asmLine.Text + "\" found.");
                }
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Adds .text source information to the object file.
 /// </summary>
 /// <param name="srcInfo">The source line information structure to add.</param>
 public virtual void AddSourceInformation(SourceLineInformation srcInfo)
 {
     m_DbgData.AddSourceLineInformation(srcInfo);
 }