Example #1
0
        protected override void Compile(CodeContext c)
        {
            var v1 = c.Int32("v1");
            var v2 = c.Int32("v2");

            c.SetArgument(0, v1);
            c.SetArgument(1, v2);

            var l1 = c.Label();
            var l2 = c.Label();
            var l3 = c.Label();
            var l4 = c.Label();

            c.Jmp(l1);
            c.Bind(l2);
            c.Jmp(l4);
            c.Bind(l1);

            c.Cmp(v1, v2);
            c.Jg(l3);

            c.Mov(v1, 1);
            c.Jmp(l2);

            c.Bind(l3);
            c.Mov(v1, 2);
            c.Jmp(l2);

            c.Bind(l4);

            c.Ret(v1);
        }
Example #2
0
        protected override void Compile(CodeContext c)
        {
            var v1 = c.Int32("v1");
            var v2 = c.Int32("v2");

            c.SetArgument(0, v1);
            c.SetArgument(1, v2);

            var counter = c.Int32("counter");

            var l1   = c.Label();
            var loop = c.Label();
            var exit = c.Label();

            c.Cmp(v1, v2);
            c.Jg(l1);

            c.Mov(counter, 0);

            c.Bind(loop);
            c.Mov(v1, counter);

            c.Inc(counter);
            c.Cmp(counter, 1);
            c.Jle(loop);
            c.Jmp(exit);

            c.Bind(l1);
            c.Mov(v1, 2);

            c.Bind(exit);
            c.Ret(v1);
        }
Example #3
0
        protected override void Compile(CodeContext c)
        {
            var v0 = c.Int32("v0");
            var v1 = c.Int32("v1");
            var v2 = c.Int32("v2");

            c.SetArgument(0, v0);
            c.SetArgument(1, v1);
            c.SetArgument(2, v2);

            var fp = Memory.Fn(new Func <int, int, int, int>(CalledFunction));

            // Just do something.
            c.Shl(v0, 1);
            c.Shl(v1, 1);
            c.Shl(v2, 1);

            var fn = c.IntPtr("fn");

            c.Mov(fn, fp);

            var call = c.Call(fn, fp);

            call.SetArgument(0, v2);
            call.SetArgument(1, v1);
            call.SetArgument(2, v0);
            call.SetReturn(0, v0);

            c.Ret(v0);
        }
Example #4
0
        protected override void Compile(CodeContext c)
        {
            var stack = c.Stack(256, 1).SetSize(1);
            var i     = c.IntPtr("i");
            var a     = c.Int32("a");
            var b     = c.Int32("b");

            var l1 = c.Label();
            var l2 = c.Label();

            // Fill stack by sequence [0, 1, 2, 3 ... 255].
            c.Xor(i, i);

            c.Bind(l1);
            c.Mov(stack.Clone().SetIndex(i), i.As8());
            c.Inc(i);
            c.Cmp(i, 255);
            c.Jle(l1);

            // Sum sequence in stack.
            c.Xor(i, i);
            c.Xor(a, a);

            c.Bind(l2);
            c.Movzx(b, stack.Clone().SetIndex(i));
            c.Add(a, b);
            c.Inc(i);
            c.Cmp(i, 255);
            c.Jle(l2);

            c.Ret(a);
        }
Example #5
0
        protected override void Compile(CodeContext c)
        {
            var op = c.Int32("op");
            var a  = c.Int32("a");
            var b  = c.Int32("b");

            var zero = c.Label();
            var add  = c.Label();
            var sub  = c.Label();
            var mul  = c.Label();
            var div  = c.Label();

            c.SetArgument(0, op);
            c.SetArgument(1, a);
            c.SetArgument(2, b);

            c.Cmp(op, 0);
            c.Jz(add);

            c.Cmp(op, 1);
            c.Jz(sub);

            c.Cmp(op, 2);
            c.Jz(mul);

            c.Cmp(op, 3);
            c.Jz(div);

            c.Bind(zero);
            c.Xor(a, a);
            c.Ret(a);

            c.Bind(add);
            c.Add(a, b);
            c.Ret(a);

            c.Bind(sub);
            c.Sub(a, b);
            c.Ret(a);

            c.Bind(mul);
            c.Imul(a, b);
            c.Ret(a);

            c.Bind(div);
            c.Cmp(b, 0);
            c.Jz(zero);

            var z = c.Int32("z");

            c.Xor(z, z);
            c.Idiv(z, a, b);
            c.Ret(a);
        }
Example #6
0
        protected override void Compile(CodeContext c)
        {
            var src0 = c.Int32("src0");
            var src1 = c.Int32("src1");
            var dst0 = c.IntPtr("dst0");

            c.SetArgument(0, src0);
            c.SetArgument(1, src1);
            c.SetArgument(2, dst0);

            c.Cmp(src0, src1);
            c.Setz(Memory.Byte(dst0));
        }
Example #7
0
        protected override void Compile(CodeContext c)
        {
            var v0 = c.Int32("v0");
            var v1 = c.Int32("v1");

            var c0 = c.Int32Const(ConstantScope.Local, 200);
            var c1 = c.Int32Const(ConstantScope.Local, 33);

            c.Mov(v0, c0);
            c.Mov(v1, c1);
            c.Add(v0, v1);

            c.Ret(v0);
        }
Example #8
0
        protected override void Compile(CodeContext c)
        {
            const int count = 8;

            var a0 = c.IntPtr("a0");
            var a1 = c.IntPtr("a1");

            c.SetArgument(0, a0);
            c.SetArgument(1, a1);

            // Create some variables.
            var t = c.Int32("t");
            var x = new GpVariable[count];

            int i;

            for (i = 0; i < count; i++)
            {
                x[i] = c.Int32("x" + i);
            }

            // Setup variables (use mov with reg/imm to se if register allocator works).
            for (i = 0; i < count; i++)
            {
                c.Mov(x[i], i + 1);
            }

            // Make sum (addition).
            c.Xor(t, t);
            for (i = 0; i < count; i++)
            {
                c.Add(t, x[i]);
            }

            // Store result to a given pointer in first argument.
            c.Mov(Memory.DWord(a0), t);

            // Clear t.
            c.Xor(t, t);

            // Make sum (subtraction).
            for (i = 0; i < count; i++)
            {
                c.Sub(t, x[i]);
            }

            // Store result to a given pointer in second argument.
            c.Mov(Memory.DWord(a1), t);
        }
Example #9
0
        protected override void Compile(CodeContext c)
        {
            var fn = c.IntPtr("fn");
            var rv = c.Int32("rv");

            var fp = Memory.Fn(new Func <IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, int>(CalledFunction));

            c.Mov(fn, fp);

            var call = c.Call(fn, fp);

            call.SetArgument(0, 1);
            call.SetArgument(1, 2);
            call.SetArgument(2, 3);
            call.SetArgument(3, 4);
            call.SetArgument(4, 5);
            call.SetArgument(5, 6);
            call.SetArgument(6, 7);
            call.SetArgument(7, 8);
            call.SetArgument(8, 9);
            call.SetArgument(9, 10);
            call.SetReturn(0, rv);

            c.Ret(rv);
        }
Example #10
0
        protected override void Compile(CodeContext c)
        {
            var dst = c.IntPtr("dst");
            var src = c.IntPtr("src");
            var cnt = c.UIntPtr("cnt");

            var loop = c.Label();             // Create base labels we use
            var exit = c.Label();             // in our function.

            c.SetArgument(0, dst);
            c.SetArgument(1, src);
            c.SetArgument(2, cnt);

            c.Allocate(dst);             // Allocate all registers now,
            c.Allocate(src);             // because we want to keep them
            c.Allocate(cnt);             // in physical registers only.

            c.Test(cnt, cnt);            // Exit if length is zero.
            c.Jz(exit);

            c.Bind(loop);             // Bind the loop label here.

            var tmp = c.Int32("tmp"); // Copy a single dword (4 bytes).

            c.Mov(tmp, Memory.DWord(src));
            c.Mov(Memory.DWord(dst), tmp);

            c.Add(src, 4);             // Increment dst/src pointers.
            c.Add(dst, 4);

            c.Dec(cnt);             // Loop until cnt isn't zero.
            c.Jnz(loop);

            c.Bind(exit);             // Bind the exit label here.
        }
Example #11
0
        protected override void Compile(CodeContext c)
        {
            var fn = c.IntPtr("fn");
            var rv = c.Int32("rv");

            var fp = Memory.Fn(new Func <int, int, int, int, int, int, int, int, int, int, int>(CalledFunction));

            c.Mov(fn, fp);

            var call = c.Call(fn, fp);

            call.SetArgument(0, 0x03);
            call.SetArgument(1, 0x12);
            call.SetArgument(2, 0xA0);
            call.SetArgument(3, 0x0B);
            call.SetArgument(4, 0x2F);
            call.SetArgument(5, 0x02);
            call.SetArgument(6, 0x0C);
            call.SetArgument(7, 0x12);
            call.SetArgument(8, 0x18);
            call.SetArgument(9, 0x1E);
            call.SetReturn(0, rv);

            c.Ret(rv);
        }
Example #12
0
        protected override void Compile(CodeContext c)
        {
            var fn = c.IntPtr("fn");
            var a  = c.Int32("a");

            var fp = Memory.Fn(new Func <int, int, int, int, int, int, int, int, int, int, int>(CalledFunction));

            c.Mov(fn, fp);
            c.Mov(a, 3);

            var call = c.Call(fn, fp);

            call.SetArgument(0, a);
            call.SetArgument(1, a);
            call.SetArgument(2, a);
            call.SetArgument(3, a);
            call.SetArgument(4, a);
            call.SetArgument(5, a);
            call.SetArgument(6, a);
            call.SetArgument(7, a);
            call.SetArgument(8, a);
            call.SetArgument(9, a);
            call.SetReturn(0, a);

            c.Ret(a);
        }
Example #13
0
        protected override void Compile(CodeContext c)
        {
            var dst       = c.IntPtr("dst");
            var var       = c.Int32("var");
            var vShlParam = c.Int32("vShlParam");
            var vRorParam = c.Int32("vRorParam");

            c.SetArgument(0, dst);
            c.SetArgument(1, var);
            c.SetArgument(2, vShlParam);
            c.SetArgument(3, vRorParam);

            c.Shl(var, vShlParam);
            c.Ror(var, vRorParam);

            c.Mov(Memory.DWord(dst), var);
        }
Example #14
0
        protected override void Compile(CodeContext c)
        {
            var fn = c.IntPtr("fn");
            var va = c.Int32("va");
            var vb = c.Int32("vb");
            var vc = c.Int32("vc");
            var vd = c.Int32("vd");
            var ve = c.Int32("ve");
            var vf = c.Int32("vf");
            var vg = c.Int32("vg");
            var vh = c.Int32("vh");
            var vi = c.Int32("vi");
            var vj = c.Int32("vj");

            var fp = Memory.Fn(new Func <int, int, int, int, int, int, int, int, int, int, int>(CalledFunction));

            c.Mov(fn, fp);
            c.Mov(va, 0x03);
            c.Mov(vb, 0x12);
            c.Mov(vc, 0xA0);
            c.Mov(vd, 0x0B);
            c.Mov(ve, 0x2F);
            c.Mov(vf, 0x02);
            c.Mov(vg, 0x0C);
            c.Mov(vh, 0x12);
            c.Mov(vi, 0x18);
            c.Mov(vj, 0x1E);

            var call = c.Call(fn, fp);

            call.SetArgument(0, va);
            call.SetArgument(1, vb);
            call.SetArgument(2, vc);
            call.SetArgument(3, vd);
            call.SetArgument(4, ve);
            call.SetArgument(5, vf);
            call.SetArgument(6, vg);
            call.SetArgument(7, vh);
            call.SetArgument(8, vi);
            call.SetArgument(9, vj);
            call.SetReturn(0, va);

            c.Ret(va);
        }
Example #15
0
        protected override void Compile(CodeContext c)
        {
            var dstHi = c.IntPtr("dstHi");
            var dstLo = c.IntPtr("dstLo");
            var vLo   = c.Int32("vLo");
            var src   = c.Int32("src");

            c.SetArgument(0, dstHi);
            c.SetArgument(1, dstLo);
            c.SetArgument(2, vLo);
            c.SetArgument(3, src);

            var vHi = c.Int32("vHi");

            c.Imul(vHi, vLo, src);

            c.Mov(Memory.DWord(dstHi), vHi);
            c.Mov(Memory.DWord(dstLo), vLo);
        }
Example #16
0
        protected override void Compile(CodeContext c)
        {
            int i;

            var buf  = c.IntPtr("buf");
            var acc0 = c.Int32("acc0");
            var acc1 = c.Int32("acc1");

            c.SetArgument(0, buf);

            c.Mov(acc0, 0);
            c.Mov(acc1, 0);

            for (i = 0; i < 4; i++)
            {
                var ret = c.Int32("ret");
                var ptr = c.IntPtr("ptr");
                var idx = c.Int32("idx");

                c.Mov(ptr, buf);
                c.Mov(idx, i);

                var call = c.Call(Memory.Fn(new Func <IntPtr, int, int>(CalledFunction) /*, CallingConvention.HostDefaultFast*/));
                call.SetArgument(0, ptr);
                call.SetArgument(1, idx);
                call.SetReturn(0, ret);

                c.Add(acc0, ret);

                c.Mov(ptr, buf);
                c.Mov(idx, i);

                call = c.Call(Memory.Fn(new Func <IntPtr, int, int>(CalledFunction) /*, CallingConvention.HostDefaultFast*/));
                call.SetArgument(0, ptr);
                call.SetArgument(1, idx);
                call.SetReturn(0, ret);

                c.Sub(acc1, ret);
            }

            c.Add(acc0, acc1);
            c.Ret(acc0);
        }
Example #17
0
        protected override void Compile(CodeContext c)
        {
            var h     = LoadLibrary(IntPtr.Size > 4 ? "c:\\SampleDLL64.dll" : "c:\\SampleDLL.dll");
            var fnPtr = GetProcAddress(h, "addNumbers");

            var v0 = c.Int32("v0");
            var v1 = c.Int32("v1");
            var v2 = c.Int32("v2");

            c.SetArgument(0, v0);
            c.SetArgument(1, v1);

            var call = c.Call(Memory.Fn <Func <int, int, int> >(fnPtr));

            call.SetArgument(0, v0);
            call.SetArgument(1, v1);
            call.SetReturn(0, v2);

            c.Ret(v2);
        }
Example #18
0
        protected override void Compile(CodeContext c)
        {
            var dst = c.IntPtr("dst");
            var src = c.IntPtr("src");

            c.SetArgument(0, dst);
            c.SetArgument(1, src);

            for (var i = 0; i < 4; i++)
            {
                var x  = c.Int32("x");
                var y  = c.Int32("y");
                var hi = c.Int32("hi");

                c.Mov(x, Memory.DWord(src, 0));
                c.Mov(y, Memory.DWord(src, 4));

                c.Imul(hi, x, y);
                c.Add(Memory.DWord(dst, 0), hi);
                c.Add(Memory.DWord(dst, 4), x);
            }
        }
Example #19
0
        protected override void Compile(CodeContext c)
        {
            var v0 = c.Int32("v0");
            var v1 = c.Int32("v1");
            var v2 = c.Int32("v2");
            var v3 = c.Int32("v3");
            var v4 = c.Int32("v4");

            c.Xor(v0, v0);

            c.Mov(v1, 1);
            c.Mov(v2, 2);
            c.Mov(v3, 3);
            c.Mov(v4, 4);

            c.Add(v0, v1);
            c.Add(v0, v2);
            c.Add(v0, v3);
            c.Add(v0, v4);

            c.Ret(v0);
        }
Example #20
0
        protected override void Compile(CodeContext c)
        {
            var var = new GpVariable[32];

            var a = c.IntPtr("a");

            c.SetArgument(0, a);

            int i;

            for (i = 0; i < var.Length; i++)
            {
                var[i] = c.Int32("var" + i);
            }

            for (i = 0; i < var.Length; i++)
            {
                c.Xor(var[i], var[i]);
            }

            var v0 = c.Int32("v0");
            var l  = c.Label();

            c.Mov(v0, 32);
            c.Bind(l);

            for (i = 0; i < var.Length; i++)
            {
                c.Add(var[i], i);
            }

            c.Dec(v0);
            c.Jnz(l);

            for (i = 0; i < var.Length; i++)
            {
                c.Mov(Memory.DWord(a, i * 4), var[i]);
            }
        }
Example #21
0
        protected override void Compile(CodeContext c)
        {
            var a = c.IntPtr("a");
            var b = c.IntPtr("b");

            c.SetArgument(0, a);
            c.SetArgument(1, b);

            var dummy = c.Int32("dummy");

            c.Xor(dummy, dummy);
            c.Idiv(dummy, a, b);
            c.Ret(a);
        }
Example #22
0
        protected override void Compile(CodeContext c)
        {
            var v0  = c.Int32("v0");
            var v1  = c.Int32("v1");
            var cnt = c.Int32("cnt");

            c.Xor(v0, v0);
            c.Xor(v1, v1);
            c.Spill(v0);
            c.Spill(v1);

            var l = c.Label();

            c.Mov(cnt, 32);
            c.Bind(l);

            c.Inc(v1);
            c.Add(v0, v1);

            c.Dec(cnt);
            c.Jnz(l);

            c.Ret(v0);
        }
Example #23
0
        protected override void Compile(CodeContext c)
        {
            var l1 = c.Label();

            var iIdx = c.Int32("iIdx");
            var iEnd = c.Int32("iEnd");

            var aIdx = c.Int32("aIdx");
            var aEnd = c.Int32("aEnd");

            c.SetArgument(0, aIdx);
            c.SetArgument(1, aEnd);

            c.Mov(iIdx, aIdx);
            c.Mov(iEnd, aEnd);
            c.Spill(iEnd);

            c.Bind(l1);
            c.Inc(iIdx);
            c.Cmp(iIdx, iEnd.ToMemory());
            c.Jne(l1);

            c.Ret(iIdx);
        }
Example #24
0
//		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
//		delegate int CalledFunctionDel(int x, int y);

        protected override void Compile(CodeContext c)
        {
            var x  = c.Int32("x");
            var y  = c.Int32("y");
            var op = c.Int32("op");

            c.SetArgument(0, x);
            c.SetArgument(1, y);
            c.SetArgument(2, op);

            var opAdd = c.Label();
            var opMul = c.Label();

            c.Cmp(op, 0);
            c.Jz(opAdd);
            c.Cmp(op, 1);
            c.Jz(opMul);

            var result = c.Int32("result_0");

            c.Mov(result, 0);
            c.Ret(result);

            c.Bind(opAdd);
            result = c.Int32("result_1");

//			CalledFunctionDel f = CalledFunctionAdd;
//			var call = c.Call(Memory.Fn(f));
            var call = c.Call(Memory.Fn(new Func <int, int, int>(CalledFunctionAdd)));

            call.SetArgument(0, x);
            call.SetArgument(1, y);
            call.SetReturn(0, result);
            c.Ret(result);

            c.Bind(opMul);
            result = c.Int32("result_2");

//			f = CalledFunctionMul;
//			call = c.Call(Memory.Fn(f));
            call = c.Call(Memory.Fn(new Func <int, int, int>(CalledFunctionMul)));
            call.SetArgument(0, x);
            call.SetArgument(1, y);
            call.SetReturn(0, result);

            c.Ret(result);
        }
Example #25
0
        protected override void Compile(CodeContext c)
        {
            var var = c.Int32("var");
            var fn  = c.IntPtr("fn");

            c.SetArgument(0, var);

            var fp = Memory.Fn(new Func <int, int>(CalledFunction) /*, CallingConvention.HostDefaultFast*/);

            c.Mov(fn, fp);

            var call = c.Call(fn, fp);

            call.SetArgument(0, var);
            call.SetReturn(0, var);

            call = c.Call(fn, fp);
            call.SetArgument(0, var);
            call.SetReturn(0, var);

            c.Ret(var);
        }