Example #1
0
 private Expression TransformExpression(Expression e, bool insideThreadLoop)
 {
     var xe = e.Transform(
         (Eval eval) =>
         {
             var m = eval.InvokedMethod();
             var decl = m == null ? null : m.DeclaringType;
             while (decl != null && decl.DeclaringType != null) decl = decl.DeclaringType;
             return decl == typeof(Hints) ? null : eval.DefaultTransform();
         },
         (Fld fld) =>
         {
             var isXyz = fld.Field.Name == "X" || fld.Field.Name == "Y" || fld.Field.Name == "Z";
             if (isXyz)
             {
                 var parent = fld.This as Prop;
                 var p_api = (parent == null ? null : parent.Property).Api();
                 if (p_api != null && p_api.DeclaringType == typeof(IGridApi))
                 {
                     if (p_api.Name == "BlockIdx")
                     {
                         return new Fld(fld.Field, new Ref(_blockIdx));
                     }
                     else if (p_api.Name == "ThreadIdx")
                     {
                         return new Ref(_tids[fld.Field.Name.ToLower()]);
                     }
                     else
                     {
                         return fld.DefaultTransform();
                     }
                 }
                 else
                 {
                     return fld.DefaultTransform();
                 }
             }
             else
             {
                 return (Node)fld.DefaultTransform();
             }
         },
         (Prop prop) =>
         {
             var p_api = prop.Property.Api();
             if (p_api != null && p_api.DeclaringType == typeof(IGridApi))
             {
                 // todo. support raw queries for *Idx properties
                 return prop.DefaultTransform();
             }
             else
             {
                 return prop.DefaultTransform();
             }
         },
         (Assign ass) =>
         {
             var @ref = ass.Lhs as Ref;
             var local = @ref == null ? null : @ref.Sym as Local;
             if (local != null)
             {
                 if (Alloc(local) == allocPrivate &&
                     _needsReplication[local])
                 {
                     insideThreadLoop.AssertTrue();
                     var r_local = _replicatedLocals[local];
                     var replica = new Eval(new Apply(
                         new Lambda(r_local.Type.ArraySetter()),
                         new Ref(r_local), 
                         new Ref(_tids["z"]), 
                         new Ref(_tids["y"]), 
                         new Ref(_tids["x"]),
                         ass.Rhs.CurrentTransform()));
                     return (Node)replica;
                 }
                 else
                 {
                     return ass.DefaultTransform();
                 }
             }
             else
             {
                 return ass.DefaultTransform();
             }
         },
         (Ref @ref) =>
         {
             var sym = (Sym)@ref.Sym;
             if (sym == _params.Single())
             {
                 return new Ref(_this);
             }
             else
             {
                 var local = sym as Local;
                 if (local != null)
                 {
                     if (Alloc(local) == allocPrivate &&
                         _needsReplication[local])
                     {
                         insideThreadLoop.AssertTrue();
                         var r_local = _replicatedLocals[local];
                         var replica = new Eval(new Apply(
                             new Lambda(r_local.Type.ArrayGetter()),
                             new Ref(r_local),
                             new Ref(_tids["z"]),
                             new Ref(_tids["y"]),
                             new Ref(_tids["x"])));
                         return (Node)replica;
                     }
                     else
                     {
                         return @ref.DefaultTransform();
                     }
                 }
                 else
                 {
                     return @ref.DefaultTransform();
                 }
             }
         }).AssertCast<Expression>();
     return xe;
 }