Пример #1
0
            Temp.Temp TranslateExpr(CALL expr)
            {
                Call c = new Call();

                c.Name = new Label((expr.Func as NAME).Label);
                TempList args = null, ptr = null;

                for (ExpList exp = expr.Args; exp != null; exp = exp.Tail)
                {
                    Temp.Temp arg = TranslateExpr(exp.Head);
                    if (args == null)
                    {
                        ptr = args = new TempList(arg, null);
                    }
                    else
                    {
                        ptr = ptr.Tail = new TempList(arg, null);
                    }
                }
                c.Param = args;
                MipsFrame t = null;

                foreach (MipsFrame f in MipsFrame.AllFrames)
                {
                    if (c.Name.Lab == f.Name)
                    {
                        t = f;
                        break;
                    }
                }
                if (t == null)
                {
                    int count = 0;
                    for (ptr = c.Param; ptr != null; ptr = ptr.Tail)
                    {
                        InstrList.Add(new Move(Frame.A(count), ptr.Head));
                        ++count;
                    }
                }
                else
                {
                    int count = 0;
                    ptr = c.Param;
                    for (AccessList al = t.Formals; al != null; al = al.Tail, ptr = ptr.Tail)
                    {
                        if (al.Head is InReg)
                        {
                            InstrList.Add(new Move(t.A(count), ptr.Head));
                            ++count;
                        }
                        else
                        {
                            InstrList.Add(new Store(t.SP(), (al.Head as InFrame).Offset, ptr.Head));
                        }
                    }
                }
                InstrList.Add(c);
                return(Frame.RV());
            }
Пример #2
0
 void PrintExp(CALL e, int d)
 {
     Indent(d); SayLn("CALL(");
     PrintExp(e.Func, d + 1);
     for (ExpList a = e.Args; a != null; a = a.Tail)
     {
         SayLn(","); PrintExp(a.Head, d + 2);
     }
     Say(")");
 }
        public override void InitPrototype(IGlobal global)
        {
            ((JsFunction)Prototype).Scope = global.ObjectClass.Scope;
            Prototype.DefineOwnProperty("constructor", this, PropertyAttributes.DontEnum);

            Prototype.DefineOwnProperty(CALL.ToString(), new JsCallFunction(this), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty(APPLY.ToString(), new JsApplyFunction(this), PropertyAttributes.DontEnum);

            Prototype.DefineOwnProperty("toString", New <JsDictionaryObject>(ToString2), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty("toLocaleString", New <JsDictionaryObject>(ToString2), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty("length", new PropertyDescriptor <JsObject>(global, Prototype, "length", GetLengthImpl, SetLengthImpl));
        }
Пример #4
0
        public override void btnRun_Click(object sender, EventArgs e)
        {
            CParameterInitialize ParameterInitialize = _DataRecords.ParameterInitialize;
            SaveFileDialog       SFD = new SaveFileDialog();

            SFD.ShowDialog();
            if (SFD.FileName == null || SFD.FileName == "")
            {
                return;
            }
            ParameterInitialize.strSavePath = SFD.FileName;
            ParameterInitialize.pWorkspace  = CHelpFunc.OpenWorkspace(ParameterInitialize.strSavePath);
            _pALL = new CALL(_DataRecords);
            _RelativeInterpolationCpl = _DataRecords.ParameterResult.FromCpl;
        }
Пример #5
0
        public override void InitPrototype(IGlobal global)
        {
            var Prototype = PrototypeProperty;

            // ((JsFunction)Prototype).Scope = global.ObjectClass.Scope;

            Prototype.DefineOwnProperty("constructor", this, PropertyAttributes.DontEnum);

            Prototype.DefineOwnProperty(CALL.ToString(), new JsCallFunction(this), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty(APPLY.ToString(), new JsApplyFunction(this), PropertyAttributes.DontEnum);

            Prototype.DefineOwnProperty("toString", New <JsDictionaryObject>(ToString2), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty("toLocaleString", New <JsDictionaryObject>(ToString2), PropertyAttributes.DontEnum);
            Prototype.DefineOwnProperty(new PropertyDescriptor <JsObject>(global, Prototype, "length", GetLengthImpl, SetLengthImpl));

            if (global.HasOption(Options.Ecmascript5))
            {
                Prototype.DefineOwnProperty("bind", global.FunctionClass.New <JsObject>(Bind, 1), PropertyAttributes.DontEnum);
            }
        }
Пример #6
0
        public static void AssociateOpcodes()
        {
            instructions[0] = new NOP();
            instructions[1] = new PRNT();

            instructions[16] = new PUSH();
            instructions[17] = new POP();
            instructions[18] = new SAVE();
            instructions[19] = new CPY();
            instructions[20] = new RNDM();
            instructions[21] = new EMPTY();

            instructions[128] = new ADD();
            instructions[129] = new SUB();
            instructions[130] = new MUL();
            instructions[131] = new DIV();

            instructions[132] = new SUB2();
            instructions[133] = new DIV2();

            instructions[134] = new NEG();
            instructions[135] = new ABS();

            instructions[144] = new INC();
            instructions[145] = new DEC();

            instructions[64] = new JMP();
            instructions[65] = new JGZ();
            instructions[66] = new JLZ();
            instructions[67] = new JEZ();
            instructions[68] = new JNZ();
            instructions[69] = new CALL();
            instructions[70] = new RET();
            instructions[71] = new LDLOC();
            instructions[72] = new STLOC();
            instructions[73] = new LDARG();
            instructions[74] = new STARG();

            instructions[0b10100100] = new CMP();
Пример #7
0
        public void Test_CALL()
        {
            ushort sp      = 0x3333;
            ushort pc      = 0x1122;
            byte   lsbData = 0x24;
            byte   msbData = 0x42;

            var expectedState = new CpuState();

            expectedState.StackPointer   = (ushort)(sp - 2);
            expectedState.ProgramCounter = (ushort)((msbData << 8) | lsbData);

            var actualState = new CpuState();

            actualState.StackPointer   = sp;
            actualState.ProgramCounter = pc;

            var memoryMock = new Mock <IRandomAccessMemory>();

            memoryMock.Setup(m => m.ReadByte(pc)).Returns(lsbData);
            memoryMock.Setup(m => m.ReadByte((ushort)(pc + 1))).Returns(msbData);

            var instruction = new CALL();

            instruction.Initialize();

            //act
            while (!instruction.IsFetchNecessary())
            {
                instruction.ExecuteCycle(actualState, memoryMock.Object);
            }

            TestHelper.AssertCpuState(expectedState, actualState);
            memoryMock.Verify(m => m.WriteByte((ushort)(sp - 1), (byte)((pc + 2) >> 8)), Times.Once);
            memoryMock.Verify(m => m.WriteByte((ushort)(sp - 2), (byte)((pc + 2) & 0x00FF)), Times.Once);
        }
Пример #8
0
 protected override void Visit_CALL(CALL node)
 {
     /* MOVE CODE HERE */
 }
Пример #9
0
 void PrintExp(CALL e, int d)
 {
     Indent(d); SayLn("CALL(");
     PrintExp(e.Func, d + 1);
     for (ExpList a = e.Args; a != null; a = a.Tail)
     {
         SayLn(","); PrintExp(a.Head, d + 2);
     }
     Say(")");
 }
Пример #10
0
 protected virtual void Visit_CALL(CALL node)
 {
 }
Пример #11
0
        public static Instruction[] PeToInstructions(string FilePath)
        {
            UnmanagedBuffer    buffer    = new UnmanagedBuffer(File.ReadAllBytes(FilePath));
            List <Instruction> instructs = new List <Instruction>();
            Disasm             disasm    = new Disasm();

            disasm.EIP = new IntPtr(buffer.Ptr.ToInt64() + 0x400);

            List <int> Addresses = new List <int>();

            while (true)
            {
                int result = Asm.Net.src.BeaEngine.BeaEngine.Disasm(disasm);
                Addresses.Add(disasm.EIP.ToInt32());

                if (result == (int)BeaConstants.SpecialInfo.UNKNOWN_OPCODE)
                {
                    break;
                }

                //Console.WriteLine("0x" + disasm.EIP.ToString("X") + " " + disasm.CompleteInstr);

                //convert the data to instructions so we are able to execute it in Asm.Net
                //We also need to change the pointers for push, call, inc, dec etc... so Asm.Net is able to understand it

                switch (disasm.Instruction.Opcode)
                {
                case (int)OpcodeList.CALL:
                {
                    string tmp      = disasm.Argument1.ArgMnemonic.Substring(0, disasm.Argument1.ArgMnemonic.Length - 1).Replace("FFFFFFFF", "");
                    int    Jmpvalue = Convert.ToInt32(tmp, 16);

                    CALL call = new CALL(Jmpvalue);
                    call.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    instructs.Add(call);
                    break;
                }

                case (int)OpcodeList.INC_EAX:
                {
                    INC_EAX IncEax = new INC_EAX();
                    IncEax.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    instructs.Add(IncEax);
                    break;
                }

                case (int)OpcodeList.INC_EDX:
                {
                    break;
                }

                case (int)OpcodeList.JE:
                {
                    break;
                }

                case (int)OpcodeList.JMP:
                {
                    string tmp      = disasm.Argument1.ArgMnemonic.Substring(0, disasm.Argument1.ArgMnemonic.Length - 1);
                    int    Jmpvalue = Convert.ToInt32(tmp.Replace("FFFFFFFF", ""), 16);

                    JMP jmp = new JMP(Jmpvalue);
                    jmp.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    //instructs.Add(jmp);
                    break;
                }

                case (int)OpcodeList.JNZ:
                {
                    string tmp      = disasm.Argument1.ArgMnemonic.Substring(0, disasm.Argument1.ArgMnemonic.Length - 1);
                    int    Jmpvalue = Convert.ToInt32(tmp.Replace("FFFFFFFF", ""), 16);

                    JNZ jnz = new JNZ(Jmpvalue);
                    jnz.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    //instructs.Add(jnz);
                    break;
                }

                case (int)OpcodeList.MOV_EAX:
                {
                    string  tmp      = disasm.Argument2.ArgMnemonic.Substring(0, disasm.Argument2.ArgMnemonic.Length - 1);
                    int     MovValue = Convert.ToInt32(tmp, 16);
                    MOV_EAX MovEAX   = new MOV_EAX(new VirtualAddress(0, MovValue));
                    MovEAX.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    instructs.Add(MovEAX);
                    break;
                }

                case (int)OpcodeList.NOP:
                {
                    NOP nop = new NOP();
                    nop.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    instructs.Add(nop);
                    break;
                }

                case (int)OpcodeList.PUSH_EAX:
                {
                    break;
                }

                case (int)OpcodeList.RET:
                {
                    RET ret = new RET();
                    ret.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                    instructs.Add(ret);
                    break;
                }

                case (int)OpcodeList.ADD:
                {
                    //need to reverse check the opcodes...
                    Instruction ADD = null;
                    switch (disasm.Argument1.ArgMnemonic)
                    {
                    case "eax":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "al":
                        {
                            ADD = new ADD_BYTE_PTR_EAX_AL();
                            break;
                        }
                        }
                        break;
                    }
                    }

                    if (ADD != null)
                    {
                        ADD.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                        instructs.Add(ADD);
                    }
                    break;
                }

                case (int)OpcodeList.XOR_REGISTER:
                {
                    Instruction xor = null;
                    switch (disasm.Argument1.ArgMnemonic)
                    {
                    case "eax":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_EAX_EAX(); break; }

                        case "ecx": { xor = new XOR_EAX_ECX(); break; }

                        case "edx": { xor = new XOR_EAX_EDX(); break; }

                        case "ebx": { xor = new XOR_EAX_EBX(); break; }

                        case "esp": { xor = new XOR_EAX_ESP(); break; }

                        case "ebp": { xor = new XOR_EAX_EBP(); break; }

                        case "esi": { xor = new XOR_EAX_ESI(); break; }

                        case "edi": { xor = new XOR_EAX_EDI(); break; }
                        }
                        break;
                    }

                    case "ecx":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_ECX_EAX(); break; }

                        case "ecx": { xor = new XOR_ECX_ECX(); break; }

                        case "edx": { xor = new XOR_ECX_EDX(); break; }

                        case "ebx": { xor = new XOR_ECX_EBX(); break; }

                        case "esp": { xor = new XOR_ECX_ESP(); break; }

                        case "ebp": { xor = new XOR_ECX_EBP(); break; }

                        case "esi": { xor = new XOR_ECX_ESI(); break; }

                        case "edi": { xor = new XOR_ECX_EDI(); break; }
                        }
                        break;
                    }

                    case "edx":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_EDX_EAX(); break; }

                        case "ecx": { xor = new XOR_EDX_ECX(); break; }

                        case "edx": { xor = new XOR_EDX_EDX(); break; }

                        case "ebx": { xor = new XOR_EDX_EBX(); break; }

                        case "esp": { xor = new XOR_EDX_ESP(); break; }

                        case "ebp": { xor = new XOR_EDX_EBP(); break; }

                        case "esi": { xor = new XOR_EDX_ESI(); break; }

                        case "edi": { xor = new XOR_EDX_EDI(); break; }
                        }
                        break;
                    }

                    case "ebx":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_EBX_EAX(); break; }

                        case "ecx": { xor = new XOR_EBX_ECX(); break; }

                        case "edx": { xor = new XOR_EBX_EDX(); break; }

                        case "ebx": { xor = new XOR_EBX_EBX(); break; }

                        case "esp": { xor = new XOR_EBX_ESP(); break; }

                        case "ebp": { xor = new XOR_EBX_EBP(); break; }

                        case "esi": { xor = new XOR_EBX_ESI(); break; }

                        case "edi": { xor = new XOR_EBX_EDI(); break; }
                        }
                        break;
                    }

                    case "esp":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_ESP_EAX(); break; }

                        case "ecx": { xor = new XOR_ESP_ECX(); break; }

                        case "edx": { xor = new XOR_ESP_EDX(); break; }

                        case "ebx": { xor = new XOR_ESP_EBX(); break; }

                        case "esp": { xor = new XOR_ESP_ESP(); break; }

                        case "ebp": { xor = new XOR_ESP_EBP(); break; }

                        case "esi": { xor = new XOR_ESP_ESI(); break; }

                        case "edi": { xor = new XOR_ESP_EDI(); break; }
                        }
                        break;
                    }

                    case "ebp":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_EBP_EAX(); break; }

                        case "ecx": { xor = new XOR_EBP_ECX(); break; }

                        case "edx": { xor = new XOR_EBP_EDX(); break; }

                        case "ebx": { xor = new XOR_EBP_EBX(); break; }

                        case "esp": { xor = new XOR_EBP_ESP(); break; }

                        case "ebp": { xor = new XOR_EBP_EBP(); break; }

                        case "esi": { xor = new XOR_EBP_ESI(); break; }

                        case "edi": { xor = new XOR_EBP_EDI(); break; }
                        }
                        break;
                    }

                    case "esi":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_ESI_EAX(); break; }

                        case "ecx": { xor = new XOR_ESI_ECX(); break; }

                        case "edx": { xor = new XOR_ESI_EDX(); break; }

                        case "ebx": { xor = new XOR_ESI_EBX(); break; }

                        case "esp": { xor = new XOR_ESI_ESP(); break; }

                        case "ebp": { xor = new XOR_ESI_EBP(); break; }

                        case "esi": { xor = new XOR_ESI_ESI(); break; }

                        case "edi": { xor = new XOR_ESI_EDI(); break; }
                        }
                        break;
                    }

                    case "edi":
                    {
                        switch (disasm.Argument2.ArgMnemonic)
                        {
                        case "eax": { xor = new XOR_EDI_EAX(); break; }

                        case "ecx": { xor = new XOR_EDI_ECX(); break; }

                        case "edx": { xor = new XOR_EDI_EDX(); break; }

                        case "ebx": { xor = new XOR_EDI_EBX(); break; }

                        case "esp": { xor = new XOR_EDI_ESP(); break; }

                        case "ebp": { xor = new XOR_EDI_EBP(); break; }

                        case "esi": { xor = new XOR_EDI_ESI(); break; }

                        case "edi": { xor = new XOR_EDI_EDI(); break; }
                        }
                        break;
                    }
                    }

                    if (xor != null)     //this check is just for temp, not all the XOR instructions are added
                    {
                        xor.NativeVirtualAddress.Address = disasm.EIP.ToInt32();
                        instructs.Add(xor);
                    }
                    break;
                }
                }

                disasm.EIP = new IntPtr(disasm.EIP.ToInt64() + result);
            }

            //set all the pointers correct
            int offset = Options.MemoryBaseAddress;

            foreach (Instruction instruction in instructs)
            {
                switch (instruction.ToByteArray()[0])
                {
                case (int)OpcodeList.CALL:
                {
                    break;
                }

                case (int)OpcodeList.INC_EAX:
                {
                    break;
                }

                case (int)OpcodeList.INC_EDX:
                {
                    break;
                }

                case (int)OpcodeList.JE:
                {
                    break;
                }

                case (int)OpcodeList.JMP:
                {
                    //lets set our new jmp pointer, We also should load the modules which are required to run this program
                    //We need to load the Import Table and get all the .dll's from it and getting all the instructions from it
                    bool NewSet = false;
                    foreach (Instruction instruct in instructs)
                    {
                        if (Addresses.Contains(((IJump)instruction).JumpAddress))
                        {
                            //Set the ASM.net pointer
                            int index = Addresses.IndexOf(((IJump)instruction).JumpAddress) - 1;
                        }

                        //if (((IJump)instruction).JumpAddress == instruct.NativeVirtualAddress.Address)
                        //{
                        //}
                    }

                    //if (!NewSet)
                    //    throw new Exception("Unable to find the JMP Pointer, Invalid memory address ?");
                    break;
                }

                case (int)OpcodeList.MOV_EAX:
                {
                    break;
                }

                case (int)OpcodeList.NOP:
                {
                    break;
                }

                case (int)OpcodeList.PUSH_EAX:
                {
                    break;
                }

                case (int)OpcodeList.RET:
                {
                    break;
                }
                }

                instruction.VirtualAddress.Address = offset;
                offset += instruction.VirtualAddress.Size;
            }

            return(instructs.ToArray());
        }
Пример #12
0
 Temp.Temp TranslateExpr(CALL expr)
 {
     Call c = new Call();
     c.Name = new Label((expr.Func as NAME).Label);
     TempList args = null, ptr = null;
     for (ExpList exp = expr.Args; exp != null; exp = exp.Tail)
     {
         Temp.Temp arg = TranslateExpr(exp.Head);
         if (args == null)
             ptr = args = new TempList(arg, null);
         else
             ptr = ptr.Tail = new TempList(arg, null);
     }
     c.Param = args;
     MipsFrame t = null;
     foreach (MipsFrame f in MipsFrame.AllFrames)
     {
         if (c.Name.Lab == f.Name)
         {
             t = f;
             break;
         }
     }
     if (t == null)
     {
         int count = 0;
         for (ptr = c.Param; ptr != null; ptr = ptr.Tail)
         {
             InstrList.Add(new Move(Frame.A(count), ptr.Head));
             ++count;
         }
     }
     else
     {
         int count = 0;
         ptr = c.Param;
         for (AccessList al = t.Formals; al != null; al = al.Tail, ptr = ptr.Tail)
         {
             if (al.Head is InReg)
             {
                 InstrList.Add(new Move(t.A(count), ptr.Head));
                 ++count;
             }
             else
                 InstrList.Add(new Store(t.SP(), (al.Head as InFrame).Offset, ptr.Head));
         }
     }
     InstrList.Add(c);
     return Frame.RV();
 }