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. }
public void TestBadTypeReplacement() { var cc = new CodeContext(); cc.Add("d", Expression.Variable(typeof(int), "f")); cc.Add("f", Expression.Constant(20.0)); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(Expression.Variable(typeof(int), "d"), cc); }
public void TestReplacementInfiniate() { CodeContext cc = new CodeContext(); cc.Add("expr2", Expression.Parameter(typeof(int), "expr1")); cc.Add("expr1", Expression.Parameter(typeof(int), "expr2")); Assert.AreEqual("expr1", (new ValSimple("expr1", typeof(int))).PerformAllSubstitutions(cc).RawValue, "no translation expected"); }
public void TestSimpleNestedReplacement() { var cc = new CodeContext(); cc.Add("d", Expression.Variable(typeof(int), "f")); cc.Add("f", Expression.Constant(20)); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(Expression.Variable(typeof(int), "d"), cc); var asconst = expr as ConstantExpression; Assert.IsNotNull(asconst, "constant replacement"); Assert.AreEqual(20, asconst.Value, "value of translation"); }
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); }
protected override void Compile(CodeContext c) { var l1 = c.Label(); var l2 = c.Label(); var l3 = c.Label(); var l4 = c.Label(); var l5 = c.Label(); var l6 = c.Label(); var l7 = c.Label(); var v0 = c.UInt32("v0"); var v1 = c.UInt32("v1"); c.Bind(l2); c.Bind(l3); c.Jmp(l1); c.Bind(l5); c.Mov(v0, 0); c.Bind(l6); c.Jmp(l3); c.Mov(v1, 1); c.Jmp(l1); c.Bind(l4); c.Jmp(l2); c.Bind(l7); c.Add(v0, v1); c.Align(AligningMode.Code, 16); c.Bind(l1); c.Ret(); }
public void TestSubqueryPatternReplacement() { var qexpr = new QuerySourceReferenceExpression(new dummyQuerySource()); CodeContext cc = new CodeContext(); cc.Add("d", Expression.Variable(typeof(int), "f")); cc.Add(qexpr.ReferencedQuerySource, Expression.Parameter(typeof(int), "d")); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(qexpr, cc); var asp = expr as ParameterExpression; Assert.IsNotNull(asp, "expected the returned expression to be of type ParameterExpression"); Assert.AreEqual(typeof(int), asp.Type, "bad type coming back"); Assert.AreEqual("f", asp.Name, "variable name"); }
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); }
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); }
public async Task <IActionResult> Create([Bind("ID,Title,Description,Snippet,Language")] Code code) { if (ModelState.IsValid) { _context.Add(code); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(code)); }
public void TestDeclarableParameterReplacement() { var e1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var e2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int)); var cc = new CodeContext(); cc.Add(e1.ParameterName, e2); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(e1, cc); Assert.AreEqual(e2, expr, "value of translation"); }
protected override void Compile(CodeContext c) { var rPtr = c.UIntPtr("rPtr"); var rSum = c.UInt32("rSum"); c.SetArgument(0, rPtr); var rVar = new GpVariable[Cnt]; int i; for (i = 0; i < Cnt; i++) { rVar[i] = c.UInt32("x" + i); } // Init pseudo-regs with values from our array. for (i = 0; i < Cnt; i++) { c.Mov(rVar[i], Memory.DWord(rPtr, i * 4)); } for (i = 2; i < Cnt; i++) { // Add and truncate to 8 bit; no purpose, just mess with jit. c.Add(rVar[i], rVar[i - 1]); c.Movzx(rVar[i], rVar[i].As8()); c.Movzx(rVar[i - 2], rVar[i - 1].As8()); c.Movzx(rVar[i - 1], rVar[i - 2].As8()); } // Sum up all computed values. c.Mov(rSum, 0); for (i = 0; i < Cnt; i++) { c.Add(rSum, rVar[i]); } // Return the sum. c.Ret(rSum); }
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); }
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); } }
public void TestLambdaWithPredefinedNameAsParam() { /// This is basically a scope test! :-) Expression <Func <testLambdaSimple, float> > lambdaExpr = t => t.pt / ((float)100.0); CodeContext cc = new CodeContext(); cc.Add("t", Expression.Parameter(typeof(testLambdaSimple), "f**k")); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(lambdaExpr, cc); Assert.AreEqual(lambdaExpr.ToString(), expr.ToString(), "lambda changed"); Assert.AreEqual("f**k", (cc.GetReplacement("t") as ParameterExpression).Name, "code context was altered"); }
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); }
protected override void Compile(CodeContext c) { const int cnt = 8; var var = new GpVariable[cnt]; int i; for (i = 0; i < cnt; i++) { var[i] = c.IntPtr("var" + i); c.SetArgument(i, var[i]); } for (i = 0; i < cnt; i++) { c.Add(var[i], i + 1); } for (i = 0; i < cnt; i++) { c.Add(Memory.Byte(var[i]), i + 1); } }
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); }
public void TestArrayReplacement() { var myvar = Expression.Variable(typeof(int[]), "d"); var myref = Expression.ArrayIndex(myvar, Expression.Constant(1)); var cc = new CodeContext(); cc.Add("d", Expression.Variable(typeof(int[]), "dude")); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(myref, cc); var be = expr as BinaryExpression; Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "index bad"); var pe = be.Left as ParameterExpression; Assert.IsNotNull(pe, "bad array value"); Assert.AreEqual("dude", pe.Name, "array parameter name"); }
public void TestArrayIndexReplacement() { var myarray = Expression.Variable(typeof(int[]), "darray"); var myindex = Expression.Variable(typeof(int), "d"); var myref = Expression.ArrayIndex(myarray, myindex); var cc = new CodeContext(); cc.Add("d", Expression.Constant(10)); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(myref, cc); var be = expr as BinaryExpression; Assert.AreEqual(ExpressionType.ArrayIndex, be.NodeType, "index bad"); var ce = be.Right as ConstantExpression; Assert.IsNotNull(ce, "reference to constant failed"); Assert.AreEqual(10, ce.Value, "value of array index"); }
protected override void Compile(CodeContext c) { c.Push(c.Ebp); c.Mov(c.Ebp, c.Esp); c.Sub(c.Esp, 192); c.Push(c.Ebx); c.Push(c.Esi); c.Push(c.Edi); c.Lea(c.Edi, Memory.DWord(c.Ebp, -192)); c.Mov(c.Ecx, 48); c.Mov(c.Eax, -858993460); c.RepStosd(); c.Mov(c.Eax, Memory.DWord(c.Ebp, 8)); c.Add(c.Eax, Memory.DWord(c.Ebp, 12)); c.Pop(c.Edi); c.Pop(c.Esi); c.Pop(c.Ebx); c.Mov(c.Esp, c.Ebp); c.Pop(c.Ebp); c.Ret(8); }
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]); } }
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); }
public void TestLambdaWithPredefinedNameAsParam() { /// This is basically a scope test! :-) Expression<Func<testLambdaSimple, float>> lambdaExpr = t => t.pt / ((float)100.0); CodeContext cc = new CodeContext(); cc.Add("t", Expression.Parameter(typeof(testLambdaSimple), "f**k")); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(lambdaExpr, cc); Assert.AreEqual(lambdaExpr.ToString(), expr.ToString(), "lambda changed"); Assert.AreEqual("f**k", (cc.GetReplacement("t") as ParameterExpression).Name, "code context was altered"); }
public void TestSubqueryNestedPatternReplacement() { var qexpr = new QuerySourceReferenceExpression(new dummyQuerySource()); CodeContext cc = new CodeContext(); cc.Add("d", Expression.Variable(typeof(int), "f")); cc.Add("f", Expression.Variable(typeof(int), "fork")); cc.Add(qexpr.ReferencedQuerySource, Expression.Parameter(typeof(int), "d")); var expr = ParameterReplacementExpressionVisitor.ReplaceParameters(qexpr, cc); var asp = expr as ParameterExpression; Assert.IsNotNull(asp, "expected the returned expression to be of type ParameterExpression"); Assert.AreEqual(typeof(int), asp.Type, "bad type coming back"); Assert.AreEqual("fork", asp.Name, "variable name"); }
protected override void Compile(CodeContext c) { var dst = c.IntPtr("dst"); var src = c.IntPtr("src"); var i = c.IntPtr("i"); var j = c.IntPtr("j"); var t = c.IntPtr("t"); var cZero = c.Xmm("cZero"); var cMul255A = c.Xmm("cMul255A"); var cMul255M = c.Xmm("cMul255M"); var x0 = c.Xmm("x0"); var x1 = c.Xmm("x1"); var y0 = c.Xmm("y0"); var a0 = c.Xmm("a0"); var a1 = c.Xmm("a1"); var smallLoop = c.Label(); var smallEnd = c.Label(); var largeLoop = c.Label(); var largeEnd = c.Label(); var data = c.Label(); c.SetArgument(0, dst); c.SetArgument(1, src); c.SetArgument(2, i); c.Allocate(dst); c.Allocate(src); c.Allocate(i); // How many pixels have to be processed to make the loop aligned. c.Lea(t, Memory.Ptr(data)); c.Xor(j, j); c.Xorps(cZero, cZero); c.Sub(j, dst); c.Movaps(cMul255A, Memory.Ptr(t, 0)); c.And(j, 15); c.Movaps(cMul255M, Memory.Ptr(t, 16)); c.Shr(j, 2); c.Jz(smallEnd); // j = min(i, j). c.Cmp(j, i); c.Cmovg(j, i); // i -= j. c.Sub(i, j); // Small loop. c.Bind(smallLoop); c.Pcmpeqb(a0, a0); c.Movd(y0, Memory.Ptr(src)); c.Pxor(a0, y0); c.Movd(x0, Memory.Ptr(dst)); c.Psrlw(a0, 8); c.Punpcklbw(x0, cZero); c.Pshuflw(a0, a0, AsmJit.Common.Utils.Shuffle(1, 1, 1, 1)); c.Punpcklbw(y0, cZero); c.Pmullw(x0, a0); c.Paddsw(x0, cMul255A); c.Pmulhuw(x0, cMul255M); c.Paddw(x0, y0); c.Packuswb(x0, x0); c.Movd(Memory.Ptr(dst), x0); c.Add(dst, 4); c.Add(src, 4); c.Dec(j); c.Jnz(smallLoop); // Second section, prepare for an aligned loop. c.Bind(smallEnd); c.Test(i, i); c.Mov(j, i); c.Jz(c.Exit); c.And(j, 3); c.Shr(i, 2); c.Jz(largeEnd); // Aligned loop. c.Bind(largeLoop); c.Movups(y0, Memory.Ptr(src)); c.Pcmpeqb(a0, a0); c.Movaps(x0, Memory.Ptr(dst)); c.Xorps(a0, y0); c.Movaps(x1, x0); c.Psrlw(a0, 8); c.Punpcklbw(x0, cZero); c.Movaps(a1, a0); c.Punpcklwd(a0, a0); c.Punpckhbw(x1, cZero); c.Punpckhwd(a1, a1); c.Pshufd(a0, a0, AsmJit.Common.Utils.Shuffle(3, 3, 1, 1)); c.Pshufd(a1, a1, AsmJit.Common.Utils.Shuffle(3, 3, 1, 1)); c.Pmullw(x0, a0); c.Pmullw(x1, a1); c.Paddsw(x0, cMul255A); c.Paddsw(x1, cMul255A); c.Pmulhuw(x0, cMul255M); c.Pmulhuw(x1, cMul255M); c.Add(src, 16); c.Packuswb(x0, x1); c.Paddw(x0, y0); c.Movaps(Memory.Ptr(dst), x0); c.Add(dst, 16); c.Dec(i); c.Jnz(largeLoop); c.Bind(largeEnd); c.Test(j, j); c.Jnz(smallLoop); // Data c.Data(data, 16, Data.Of(0x0080008000800080, 0x0080008000800080), Data.Of(0x0101010101010101, 0x0080008000800080)); }