コード例 #1
0
        public OperandToken(OperandTokenType type, Boolean isAddressOf,
                            Boolean isDereference, UInt64 op, UInt32 line, String text)
        {
            this.type = type;

            this.isAddressOf   = isAddressOf;
            this.isDereference = isDereference;
            this.op            = op;

            this.line = line;
            this.text = text;
        }
コード例 #2
0
        public OperandToken NextOperand()
        {
            Boolean isAddressOf = false;

            ToNextInterestingCharacter();

            //
            // Make sure you're not at the end of the file
            //
            if (next < 0)
            {
                throw FormatError("NextOperand: Unexpected EOF");
            }

            if ((Char)next == '&')
            {
                isAddressOf = true;
                next        = reader.Read();
                if (next < 0)
                {
                    throw FormatError("NextOperand: Unexpected EOF");
                }
            }

            OperandTokenType tokenType = OperandTokenType.Label;
            String           opString  = null;

            if (next == '-' || (next >= '0' && next <= '9'))
            {
                tokenType = OperandTokenType.Literal;

                stringBuilder.Length = 0;

                do
                {
                    stringBuilder.Append((Char)next);
                    next = reader.Read();
                } while (((Char)next >= '0' && (Char)next <= '9') ||
                         ((Char)next >= 'A' && (Char)next <= 'F') ||
                         ((Char)next >= 'a' && (Char)next <= 'f') ||
                         (Char)next == 'x' || (Char)next == 'b');

                if (stringBuilder.Length <= 1 && stringBuilder[0] == '-')
                {
                    throw FormatError("'-' is not a valid operand");
                }
                opString = stringBuilder.ToString();
            }

            if (tokenType != OperandTokenType.Literal)
            {
                //
                // Check if it's a Label or a Command
                //
                if (((Char)next >= 'a' && (Char)next <= 'z') || ((Char)next >= 'A' && (Char)next <= 'Z'))
                {
                    stringBuilder.Length = 0;

                    do
                    {
                        stringBuilder.Append((Char)next);
                        next = reader.Read();
                    }while (((Char)next >= 'a' && (Char)next <= 'z') ||
                            ((Char)next >= 'A' && (Char)next <= 'Z') ||
                            ((Char)next >= '0' && (Char)next <= '9'));


                    if (stringBuilder.Length == 2)
                    {
                        if (stringBuilder[1] == 'p')
                        {
                            if (stringBuilder[0] == 'f')
                            {
                                tokenType = OperandTokenType.FramePointer;
                            }
                            else if (stringBuilder[1] == 'd')
                            {
                                tokenType = OperandTokenType.DataPointer;
                            }
                        }
                    }
                    opString = stringBuilder.ToString();
                }
                else
                {
                    throw FormatError("Expected Operand but could not recognize character '{0}' (0x{1:X}) ({1})", (Char)next, next);
                }
            }

            //
            // Check for dereference
            //
            Boolean isDereference     = false;
            UInt64  dereferenceOffset = 0;

            if (next == '(')
            {
                isDereference = true;
                next          = reader.Read();
                if (next < 0)
                {
                    throw new FormatException("Expected ')' or '0-9' but got EOF");
                }

                if (next == ')')
                {
                    next = reader.Read();
                    return(new OperandToken(tokenType, isAddressOf, true, 0, currentLineNumber, opString));
                }

                if ((next >= '0' && next <= '9'))
                {
                    stringBuilder.Length = 0;

                    do
                    {
                        stringBuilder.Append((Char)next);
                        next = reader.Read();
                        if (next < 0)
                        {
                            throw FormatError("Expected ')' but got EOF");
                        }
                    } while (next != ')');

                    next = reader.Read();

                    // TODO: Need to parse dereference offset
                    dereferenceOffset = UInt64.Parse(stringBuilder.ToString());
                }
                else
                {
                    throw FormatError("After '(' expected ')' or '0-9' but got '{0}'", (Char)next);
                }
            }


            if (tokenType == OperandTokenType.Literal)
            {
                UInt64 literal = UInt64.Parse(opString);
                return(isDereference ? new OperandToken(tokenType, isAddressOf, isDereference, literal + dereferenceOffset, currentLineNumber, opString) :
                       new OperandToken(tokenType, isAddressOf, false, literal, currentLineNumber, opString));
            }
            return(new OperandToken(tokenType, isAddressOf, isDereference, dereferenceOffset, currentLineNumber, opString));


            throw FormatError("NextGlobalToken: Unexpected character '{0}'", (Char)next);
        }