public UnexpectedTokenException(AsmToken token, AsmTokens expected)
     : base(
         "Unexpected token " + token.Token + " " + token.Value + " at line " + token.AsmLine + ". Expected: " +
         expected)
 {
     Token = token;
 }
示例#2
0
        public static AsmToken ModifyToken(AsmToken source, string newToken)
        {
            AsmToken ret = source;

            ret.token = newToken;
            return(ret);
        }
 private static void DisplayToken(StringBuilder output, AsmToken asmToken)
 {
     if (asmToken.LeadingWhiteSpace != null)
     {
         DisplayToken(output, asmToken.LeadingWhiteSpace);
     }
     output.Append("<span class=\"" + GetClassFromTokenType(asmToken.Type) + "\">");
     output.Append(asmToken.Text);
     output.Append("</span>");
 }
示例#4
0
        public static void ReplaceAddressWithSymbolInProgramLine(ProgramLine instructionLine, int addressParameterIndexToReplace, bool addressIsRelative, string symbolName)
        {
            if (addressParameterIndexToReplace == 0 || addressParameterIndexToReplace == 1)
            {
                if (instructionLine.OpCodeParameters != null && instructionLine.OpCodeParameters.Count > 0)
                {
                    InstructionLineParam paramToReplace = instructionLine.OpCodeParameters[addressParameterIndexToReplace];

                    // Find token to replace
                    AsmToken tokenToReplace = paramToReplace.Tokens.FirstOrDefault <AsmToken>(t => t.Type == AsmTokenType.NUMBER);
                    if (tokenToReplace != null)
                    {
                        // Generate new text for the line, replacing only the address token
                        StringBuilder lineText = new StringBuilder();
                        foreach (AsmToken token in instructionLine.AllTokens)
                        {
                            if ((!addressIsRelative && token != tokenToReplace) || !paramToReplace.Tokens.Contains(token))
                            {
                                lineText.Append(token.TextWithLeadingWhiteSpace);
                            }
                            else if (token == tokenToReplace)
                            {
                                if (tokenToReplace.LeadingWhiteSpace != null)
                                {
                                    lineText.Append(tokenToReplace.LeadingWhiteSpace.Text);
                                }
                                else if (addressIsRelative && (paramToReplace.Tokens[0] != tokenToReplace))
                                {
                                    if (paramToReplace.Tokens[0].LeadingWhiteSpace != null)
                                    {
                                        lineText.Append(paramToReplace.Tokens[0].LeadingWhiteSpace.Text);
                                    }
                                }
                                lineText.Append(symbolName);
                            }
                        }

                        // Parse new line from text
                        ProgramLine newTextLine = Assembler.ParseProgramLine(lineText.ToString());

                        // Copy textual representation to the original program Line
                        instructionLine.CopyTextualRepresentationFrom(newTextLine);
                    }
                }
            }
        }
 public UnexpectedTokenException(AsmToken token)
     : base("Unexpected token " + token.Token + " at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public NotARegisterException(AsmToken token)
     : base(token.Value + " is not a register at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public ConstantRedeclaredException(AsmToken token)
     : base("Constant " + token.Value + " redeclared at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public LabelRedeclaredException(AsmToken token)
     : base("Label " + token.Value + " redeclared at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public UnexpectedTokenException(AsmToken token)
     : base("Unexpected token " + token.Token + " at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public ParameterMissingException(AsmToken token)
     : base("Missing or too much parameter for instruction " + token.Value + " at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public ConstantRedeclaredException(AsmToken token)
     : base("Constant " + token.Value + " redeclared at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public static X86Instruction ReadInstruction(AsmToken[] tokenLine)
 {
     int tokenCount = tokenLine.Length;
     if (tokenLine[0].Token != AsmTokens.Name)
     {
         throw new UnexpectedTokenException(tokenLine[0], AsmTokens.Name);
     }
     string instructionName = tokenLine[0].Value;
     int line = tokenLine[0].AsmLine;
     if (tokenCount == 1)
     {
         return new X86Instruction(line, instructionName);
     }
     X86Operand dst;
     X86Operand src = null;
     switch (tokenLine[1].Token)
     {
         case AsmTokens.Name:
             if (X86Assembler.IsRegister(tokenLine[1].Value))
             {
                 dst = new RegisterArgument(tokenLine[1].Value);
             }
             else
             {
                 dst = new LabelArgument(tokenLine[1].Value);
             }
             break;
         case AsmTokens.Number:
             dst = new ImmediateArgument(tokenLine[1].IntValue);
             break;
         case AsmTokens.LeftBracket:
             IntegerArithmeticExpression expr = IntegerArithmeticProcessor.Process(tokenLine, 1);
             dst = new MemoryArgument(expr);
             break;
         default:
             throw new UnexpectedTokenException(tokenLine[1]);
     }
     bool foundSrc = false;
     for (int i = 2; i < tokenCount; i++)
     {
         if (tokenLine[i].Token == AsmTokens.Comma && i < tokenCount - 1)
         {
             foundSrc = true;
             continue;
         }
         switch (tokenLine[i].Token)
         {
             case AsmTokens.Name:
                 if (X86Assembler.IsRegister(tokenLine[i].Value))
                 {
                     src = new RegisterArgument(tokenLine[i].Value);
                 }
                 else
                 {
                     src = new LabelArgument(tokenLine[i].Value);
                 }
                 goto Finish;
             case AsmTokens.Number:
                 src = new ImmediateArgument(tokenLine[i].IntValue);
                 goto Finish;
             case AsmTokens.LeftBracket:
                 IntegerArithmeticExpression expr = IntegerArithmeticProcessor.Process(tokenLine, i);
                 src = new MemoryArgument(expr);
                 goto Finish;
             default:
                 throw new UnexpectedTokenException(tokenLine[i]);
         }
     }
     Finish:
     if (!foundSrc)
     {
         return new X86Instruction(line, instructionName, dst);
     }
     return new X86Instruction(line, instructionName, dst, src);
 }
 public ParameterMissingException(AsmToken token)
     : base("Missing or too much parameter for instruction " + token.Value + " at line " + token.AsmLine + ".")
 {
     Token = token;
 }
        public static IntegerArithmeticExpression Process(AsmToken[] tokenLine, int offset)
        {
            int  tokenCount = tokenLine.Length;
            bool closed     = false;

            if (tokenCount < offset + 2)
            {
                throw new Exception("Wrong formatted instruction at line" + tokenLine[offset].AsmLine);
            }
            int i          = 1;
            var expression = new IntegerArithmeticExpression();

            while ((offset + i) < tokenCount)
            {
                AsmToken token = tokenLine[offset + i];
                if ((i & 1) == 1) //Oddth token. It should be either a register or a number.
                {
                    switch (token.Token)
                    {
                    case AsmTokens.Name:

                        string regName = token.Value;
                        if (!X86Assembler.IsRegister(regName))
                        {
                            throw new NotARegisterException(token);
                        }
                        expression.AddTerm(regName);
                        break;

                    case AsmTokens.Number:
                        expression.AddTerm(token.IntValue);
                        break;

                    default:
                        throw new UnexpectedTokenException(token);
                    }
                }
                else //Eventh token. It is either an operator or the end of the expression.
                {
                    switch (token.Token)
                    {
                    case AsmTokens.Plus:
                        expression.AddOperator(IntegerArithmeticOperatorType.Addition);
                        break;

                    case AsmTokens.Mult:
                        expression.AddOperator(IntegerArithmeticOperatorType.Multiplication);
                        break;

                    case AsmTokens.Sub:
                        expression.AddOperator(IntegerArithmeticOperatorType.Substraction);
                        break;

                    case AsmTokens.RightBracket:
                        closed = true;
                        break;

                    default:
                        throw new UnexpectedTokenException(token);
                    }
                }

                i++;
            }
            if (!closed)
            {
                throw new Exception("Wrong formatted instruction at line " + tokenLine[0].AsmLine);
            }

            return(expression);
        }
 public X86Assembler(AsmToken[][] asmTokens)
 {
     _asmTokens = asmTokens;
 }
 public static X86Assembly GenerateAssembly(AsmToken[][] asmTokens)
 {
     var assembler = new X86Assembler(asmTokens);
     return assembler.GenerateAssembly();
 }
 public NotARegisterException(AsmToken token)
     : base(token.Value + " is not a register at line " + token.AsmLine + ".")
 {
     Token = token;
 }
 public LabelRedeclaredException(AsmToken token)
     : base("Label " + token.Value + " redeclared at line " + token.AsmLine + ".")
 {
     Token = token;
 }
        public static IntegerArithmeticExpression Process(AsmToken[] tokenLine, int offset)
        {
            int tokenCount = tokenLine.Length;
            bool closed = false;
            if (tokenCount < offset + 2)
            {
                throw new Exception("Wrong formatted instruction at line" + tokenLine[offset].AsmLine);
            }
            int i = 1;
            var expression = new IntegerArithmeticExpression();
            while ((offset + i) < tokenCount)
            {
                AsmToken token = tokenLine[offset + i];
                if ((i & 1) == 1) //Oddth token. It should be either a register or a number.
                {
                    switch (token.Token)
                    {
                        case AsmTokens.Name:

                            string regName = token.Value;
                            if (!X86Assembler.IsRegister(regName))
                            {
                                throw new NotARegisterException(token);
                            }
                            expression.AddTerm(regName);
                            break;
                        case AsmTokens.Number:
                            expression.AddTerm(token.IntValue);
                            break;
                        default:
                            throw new UnexpectedTokenException(token);
                    }
                }
                else //Eventh token. It is either an operator or the end of the expression.
                {
                    switch (token.Token)
                    {
                        case AsmTokens.Plus:
                            expression.AddOperator(IntegerArithmeticOperatorType.Addition);
                            break;
                        case AsmTokens.Mult:
                            expression.AddOperator(IntegerArithmeticOperatorType.Multiplication);
                            break;
                        case AsmTokens.Sub:
                            expression.AddOperator(IntegerArithmeticOperatorType.Substraction);
                            break;
                        case AsmTokens.RightBracket:
                            closed = true;
                            break;
                        default:
                            throw new UnexpectedTokenException(token);
                    }
                }

                i++;
            }
            if (!closed)
                throw new Exception("Wrong formatted instruction at line " + tokenLine[0].AsmLine);

            return expression;
        }
            public bool TryGetNextToken(out AsmToken token)
            {
                token = new AsmToken();
                while (true)
                {
                    var startPosition = _position;

                    if (_c == 0)
                    {
                        return(false);
                    }

                    if (_c == '.')
                    {
                        token = ParseDirective(startPosition);
                        return(true);
                    }

                    // Like everywhere else in this file, we are inlining the matching characters instead
                    // of using helper functions, as Mono might not be enough good at inlining by itself
                    if (_c >= 'a' && _c <= 'z' || _c >= 'A' && _c <= 'Z' || _c == '_' || _c == '@')
                    {
                        token = ParseInstructionOrIdentifierOrRegister(startPosition);
                        return(true);
                    }

                    if (_c >= '0' && _c <= '9' || _c == '-')
                    {
                        token = ParseNumber(startPosition);
                        return(true);
                    }

                    if (_c == '"')
                    {
                        token = ParseString(startPosition);
                        return(true);
                    }

                    if (_c == _commentStartChar)
                    {
                        token = ParseComment(startPosition);
                        return(true);
                    }

                    if (_c == '\r')
                    {
                        if (PreviewChar() == '\n')
                        {
                            NextChar(); // skip \r
                        }
                        token = ParseNewLine(startPosition);
                        return(true);
                    }

                    if (_c == '\n')
                    {
                        token = ParseNewLine(startPosition);
                        return(true);
                    }

                    token = ParseMisc(startPosition);
                    return(true);
                }
            }
 public UnexpectedTokenException(AsmToken token, AsmTokens expected)
     : base("Unexpected token " + token.Token + " " + token.Value + " at line " + token.AsmLine + ". Expected: " +
         expected)
 {
     Token = token;
 }