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 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);
        }
Exemplo n.º 3
0
        public static X86Assembly GenerateAssembly(AsmToken[][] asmTokens)
        {
            var assembler = new X86Assembler(asmTokens);

            return(assembler.GenerateAssembly());
        }
Exemplo n.º 4
0
 public static X86Assembly GenerateAssembly(AsmToken[][] asmTokens)
 {
     var assembler = new X86Assembler(asmTokens);
     return assembler.GenerateAssembly();
 }