public static void Generate(ProgramSource ps, X86Assembly assembly, X86Instruction x86Instruction)
 {
     var translator = new CSharpTranslator(assembly);
     var addressTranslator = new CSharpTranslator(assembly, true);
     switch (x86Instruction.Name)
     {
         case "add":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " += " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "and":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " &= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "call":
             if (x86Instruction.OperandCount != 1)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             string function = x86Instruction.Dst.GetCode(translator);
             if (!X86Assembler.IsFunction(function))
                 throw new UnknownFunctionException(x86Instruction);
             ps.AddCodeLine("vm." + x86Instruction.Dst.GetCode(translator) + "()");
             break;
         case "cmp":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             GenerateCmp(ps, x86Instruction.Dst.GetCode(translator), x86Instruction.Src.GetCode(translator));
             break;
         case "jmp":
         case "je":
         case "jne":
         case "jg":
         case "jge":
         case "jl":
         case "jle":
         case "jz":
         case "jnz":
         case "loop":
             JumpInstruction(ps, assembly, x86Instruction);
             break;
         case "lea":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
             {
                 ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                                x86Instruction.Src.GetCode(translator));
                 break;
             }
             if (x86Instruction.Src.Type == OperandTypes.Memory)
             {
                 ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                                x86Instruction.Src.GetCode(addressTranslator));
                 break;
             }
             throw new WrongParameterException(x86Instruction);
         case "mov":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label &&
                 !assembly.ConstantExists(((LabelArgument) x86Instruction.Src).Label))
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "mul":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " *= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "or":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " |= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "push":
             if (x86Instruction.OperandCount != 1)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine("vm.Push(" + x86Instruction.Dst.GetCode(translator) + ")");
             break;
         case "pop":
             if (x86Instruction.OperandCount != 1)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = vm.Pop()");
             break;
         case "ret":
             if (x86Instruction.OperandCount != 0)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine("return;");
             break;
         case "shl":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " <<= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "shr":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " >>= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "sub":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " -= " +
                            x86Instruction.Src.GetCode(translator));
             break;
         case "xor":
             if (x86Instruction.OperandCount != 2)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                 x86Instruction.Dst.Type != OperandTypes.Register)
                 throw new WrongParameterException(x86Instruction);
             if (x86Instruction.Src.Type == OperandTypes.Label)
                 throw new WrongParameterException(x86Instruction);
             ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " ^= " +
                            x86Instruction.Src.GetCode(translator));
             break;
     }
 }
Example #2
0
        public static void Generate(ProgramSource ps, X86Assembly assembly, X86Instruction x86Instruction)
        {
            var translator        = new CSharpTranslator(assembly);
            var addressTranslator = new CSharpTranslator(assembly, true);

            switch (x86Instruction.Name)
            {
            case "add":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " += " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "and":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " &= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "call":
                if (x86Instruction.OperandCount != 1)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                string function = x86Instruction.Dst.GetCode(translator);
                if (!X86Assembler.IsFunction(function))
                {
                    throw new UnknownFunctionException(x86Instruction);
                }
                ps.AddCodeLine("vm." + x86Instruction.Dst.GetCode(translator) + "()");
                break;

            case "cmp":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                GenerateCmp(ps, x86Instruction.Dst.GetCode(translator), x86Instruction.Src.GetCode(translator));
                break;

            case "jmp":
            case "je":
            case "jne":
            case "jg":
            case "jge":
            case "jl":
            case "jle":
            case "jz":
            case "jnz":
            case "loop":
                JumpInstruction(ps, assembly, x86Instruction);
                break;

            case "lea":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                                   x86Instruction.Src.GetCode(translator));
                    break;
                }
                if (x86Instruction.Src.Type == OperandTypes.Memory)
                {
                    ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                                   x86Instruction.Src.GetCode(addressTranslator));
                    break;
                }
                throw new WrongParameterException(x86Instruction);

            case "mov":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label &&
                    !assembly.ConstantExists(((LabelArgument)x86Instruction.Src).Label))
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "mul":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " *= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "or":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " |= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "push":
                if (x86Instruction.OperandCount != 1)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine("vm.Push(" + x86Instruction.Dst.GetCode(translator) + ")");
                break;

            case "pop":
                if (x86Instruction.OperandCount != 1)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " = vm.Pop()");
                break;

            case "ret":
                if (x86Instruction.OperandCount != 0)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine("return;");
                break;

            case "shl":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " <<= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "shr":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " >>= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "sub":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " -= " +
                               x86Instruction.Src.GetCode(translator));
                break;

            case "xor":
                if (x86Instruction.OperandCount != 2)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Dst.Type != OperandTypes.Memory &&
                    x86Instruction.Dst.Type != OperandTypes.Register)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                if (x86Instruction.Src.Type == OperandTypes.Label)
                {
                    throw new WrongParameterException(x86Instruction);
                }
                ps.AddCodeLine(x86Instruction.Dst.GetCode(translator) + " ^= " +
                               x86Instruction.Src.GetCode(translator));
                break;
            }
        }