Exemplo n.º 1
0
 public static string TranslateSpecialMethod(this MemberReferenceExpression mre, object data, out bool noSemicolon)
 {
     //callFunc = true;
     noSemicolon = false;
     if (mre.Target.Annotations.Count() > 0)
     {
         foreach (var ann in mre.Target.Annotations)
         {
             var pd = ann as ICSharpCode.Decompiler.ILAst.ILVariable;
             if (pd != null)
             {
                 SpecialMember sm = CUDALanguage.GetSpecialMethod(mre.MemberName, pd.Type.FullName);// .GetType().Name);
                 //callFunc = sm.CallFunction;
                 noSemicolon = sm.NoSemicolon;
                 return(sm.GetTranslation(mre, data));
             }
         }
     }
     else
     {
         SpecialMember sm = CUDALanguage.GetSpecialMethod(mre.MemberName, mre.Target.ToString());// .GetType().Name);
         noSemicolon = sm.NoSemicolon;
         return(sm.GetTranslation(mre, data));
     }
     throw new InvalidOperationException("SpecialMethod not found.");
 }
Exemplo n.º 2
0
        public static SpecialMember GetSpecialMethod(this MemberReferenceExpression mre)
        {
            SpecialMember sm = null;

            if (mre.Target.Annotations.Count() > 0)
            {
                foreach (var ann in mre.Target.Annotations)
                {
                    var pd = ann as ICSharpCode.Decompiler.ILAst.ILVariable;
                    if (pd != null)
                    {
                        sm = CUDALanguage.GetSpecialMethod(mre.MemberName, pd.Type.FullName);//.GetType().Name))
                        if (sm != null)
                        {
                            return(sm);
                        }
                    }
                }
            }
            else
            {
                return(CUDALanguage.GetSpecialMethod(mre.MemberName, mre.Target.ToString()));
            }
            return(sm);
        }
Exemplo n.º 3
0
 public static bool IsSpecialMethod(this MemberReferenceExpression mre)
 {
     if (mre.Target.Annotations.Count() > 0)
     {
         foreach (var ann in mre.Target.Annotations)
         {
             var pd = ann as ICSharpCode.Decompiler.ILAst.ILVariable;
             if (pd != null)
             {
                 if (CUDALanguage.IsSpecialMethod(mre.MemberName, pd.Type.FullName))//.GetType().Name))
                 {
                     return(true);
                 }
             }
             else
             {
                 var fd = ann as FieldDefinition;
                 if (fd != null)
                 {
                     if (CUDALanguage.IsSpecialMethod(mre.MemberName, fd.FieldType.GetType().Name))
                     {
                         return(true);
                     }
                 }
             }
         }
     }
     else
     {
         return(CUDALanguage.IsSpecialMethod(mre.MemberName, mre.Target.ToString()));
     }
     return(false);
 }
Exemplo n.º 4
0
        public static string TranslateSpecialProperty(this MemberReferenceExpression mre)
        {
            IEnumerable <object> annotations = mre.Target.Annotations;//mre.Annotations.Any() ? mre.Annotations :

            if (annotations.Count() > 0)
            {
                foreach (var ann in mre.Target.Annotations)
                {
                    var pd = ann as ICSharpCode.Decompiler.ILAst.ILVariable;
                    if (pd != null)
                    {
                        SpecialMember sm = CUDALanguage.GetSpecialProperty(mre.MemberName, pd.Type.FullName);
                        if (sm != null)
                        {
                            return(sm.GetTranslation(mre));
                        }
                        else
                        {
                            return(mre.MemberName);
                        }
                    }
                    else
                    {
                        var fd = ann as FieldDefinition;
                        if (fd != null)
                        {
                            SpecialMember sm = CUDALanguage.GetSpecialProperty(mre.MemberName, fd.FieldType.GetType().Name);
                            return(sm.GetTranslation(mre));
                        }
                        else if (mre.NodeType == NodeType.Expression)
                        {
                            if (mre.MemberName == "Length")
                            {
                                return((mre.Target.ToString().Length - 2).ToString());
                            }
                        }
                    }
                }
            }
            throw new InvalidOperationException("SpecialProperty not found.");
        }
Exemplo n.º 5
0
        public static bool IsSpecialProperty(this MemberReferenceExpression mre)
        {
            IEnumerable <object> annotations = mre.Annotations.Any() ? mre.Annotations : mre.Target.Annotations;

            if (annotations.Count() > 0)
            {
                foreach (var ann in mre.Target.Annotations)
                {
                    var pd = ann as ICSharpCode.Decompiler.ILAst.ILVariable;
                    if (pd != null)
                    {
                        if (CUDALanguage.IsSpecialProperty(mre.MemberName, pd.Type.FullName))
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        var fd = ann as FieldDefinition;
                        if (fd != null)
                        {
                            if (CUDALanguage.IsSpecialProperty(mre.MemberName, fd.FieldType.GetType().Name))
                            {
                                return(true);
                            }
                        }
                        else if (mre.NodeType == NodeType.Expression)
                        {
                            if (mre.MemberName == "Length")
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        private static CudafyModule DoCudafy(CudafyModule cm, params Type[] types)
        {
            MemoryStream output   = new MemoryStream();
            var          outputSw = new StreamWriter(output);

            MemoryStream structs    = new MemoryStream();
            var          structsSw  = new StreamWriter(structs);
            var          structsPto = new PlainTextOutput(structsSw);

            MemoryStream declarations    = new MemoryStream();
            var          declarationsSw  = new StreamWriter(declarations);
            var          declarationsPto = new PlainTextOutput(declarationsSw);

            MemoryStream code    = new MemoryStream();
            var          codeSw  = new StreamWriter(code);
            var          codePto = new PlainTextOutput(codeSw);

            bool isDummy = false;
            eCudafyDummyBehaviour behaviour = eCudafyDummyBehaviour.Default;

            Dictionary <string, ModuleDefinition> modules = new Dictionary <string, ModuleDefinition>();

            var compOpts = new DecompilationOptions {
                FullDecompilation = true
            };

            CUDALanguage.Reset();
            bool firstPass = true;

            if (cm == null)
            {
                cm = new CudafyModule();// #######!!!
            }
            else
            {
                firstPass = false;
            }

            // Test structs
            //foreach (var strct in types.Where(t => !t.IsClass))
            //    if (strct.GetCustomAttributes(typeof(CudafyAttribute), false).Length == 0)
            //        throw new CudafyLanguageException(CudafyLanguageException.csCUDAFY_ATTRIBUTE_IS_MISSING_ON_X, strct.Name);

            IEnumerable <Type> typeList = GetWithNestedTypes(types);

            foreach (var type in typeList)
            {
                if (!modules.ContainsKey(type.Assembly.Location))
                {
                    modules.Add(type.Assembly.Location, ModuleDefinition.ReadModule(type.Assembly.Location));
                }
            }

            // Additional loop to compile in order
            foreach (var requestedType in typeList)
            {
                foreach (var kvp in modules)
                {
                    foreach (var td in kvp.Value.Types)
                    {
                        List <TypeDefinition> tdList = new List <TypeDefinition>();
                        tdList.Add(td);
                        tdList.AddRange(td.NestedTypes);

                        Type type = null;
                        foreach (var t in tdList)
                        {
                            //type = typeList.Where(tt => tt.FullName.Replace("+", "") == t.FullName.Replace("/", "")).FirstOrDefault();
                            // Only select type if this matches the requested type (to ensure order is maintained).
                            type = requestedType.FullName.Replace("+", "") == t.FullName.Replace("/", "") ? requestedType : null;

                            if (type == null)
                            {
                                continue;
                            }
                            Debug.WriteLine(t.FullName);
                            // Types
                            var attr = t.GetCudafyType(out isDummy, out behaviour);
                            if (attr != null)
                            {
                                _cl.DecompileType(t, structsPto, compOpts);
                                if (firstPass)
                                {
                                    cm.Types.Add(type.FullName.Replace("+", ""), new KernelTypeInfo(type, isDummy, behaviour));// #######!!!
                                }
                            }
                            else if (t.Name == td.Name)
                            {
                                // Fields
                                foreach (var fi in td.Fields)
                                {
                                    attr = fi.GetCudafyType(out isDummy, out behaviour);
                                    if (attr != null)
                                    {
                                        VerifyMemberName(fi.Name);
                                        System.Reflection.FieldInfo fieldInfo = type.GetField(fi.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                                        if (fieldInfo == null)
                                        {
                                            throw new CudafyLanguageException(CudafyLanguageException.csX_ARE_NOT_SUPPORTED, "Non-static fields");
                                        }
                                        int[] dims = _cl.GetFieldInfoDimensions(fieldInfo);
                                        _cl.DecompileCUDAConstantField(fi, dims, codePto, compOpts);
                                        var kci = new KernelConstantInfo(fi.Name, fieldInfo, isDummy);
                                        if (firstPass)
                                        {
                                            cm.Constants.Add(fi.Name, kci);// #######!!!
                                        }
                                        CUDALanguage.AddConstant(kci);
                                    }
                                }
#warning TODO Only Global Methods can be called from host
#warning TODO For OpenCL may need to do Methods once all Constants have been handled
                                // Methods
                                foreach (var med in td.Methods)
                                {
                                    attr = med.GetCudafyType(out isDummy, out behaviour);
                                    if (attr != null)
                                    {
                                        if (!med.IsStatic)
                                        {
                                            throw new CudafyLanguageException(CudafyLanguageException.csX_ARE_NOT_SUPPORTED, "Non-static methods");
                                        }
                                        _cl.DecompileMethodDeclaration(med, declarationsPto, new DecompilationOptions {
                                            FullDecompilation = false
                                        });
                                        _cl.DecompileMethod(med, codePto, compOpts);
                                        MethodInfo mi = type.GetMethod(med.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                                        if (mi == null)
                                        {
                                            continue;
                                        }
                                        VerifyMemberName(med.Name);
                                        eKernelMethodType kmt = eKernelMethodType.Device;
                                        kmt = GetKernelMethodType(attr, mi);
                                        if (firstPass)
                                        {
                                            cm.Functions.Add(med.Name, new KernelMethodInfo(type, mi, kmt, isDummy, behaviour, cm));// #######!!!
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            codeSw.Flush();

            if (CudafyTranslator.Language == eLanguage.OpenCL)
            {
                outputSw.WriteLine("#if defined(cl_khr_fp64)");
                outputSw.WriteLine("#pragma OPENCL EXTENSION cl_khr_fp64: enable");
                outputSw.WriteLine("#elif defined(cl_amd_fp64)");
                outputSw.WriteLine("#pragma OPENCL EXTENSION cl_amd_fp64: enable");
                outputSw.WriteLine("#endif");
            }

            foreach (var oh in CUDALanguage.OptionalHeaders)
            {
                if (oh.Used && !oh.AsResource)
                {
                    outputSw.WriteLine(oh.IncludeLine);
                }
                else if (oh.Used)
                {
                    outputSw.WriteLine(GetResourceString(oh.IncludeLine));
                }
            }
            foreach (var oh in CUDALanguage.OptionalFunctions)
            {
                if (oh.Used)
                {
                    outputSw.WriteLine(oh.Code);
                }
            }

            declarationsSw.WriteLine();
            declarationsSw.Flush();

            structsSw.WriteLine();
            structsSw.Flush();

            foreach (var def in cm.GetDummyDefines())
            {
                outputSw.WriteLine(def);
            }
            foreach (var inc in cm.GetDummyStructIncludes())
            {
                outputSw.WriteLine(inc);
            }
            foreach (var inc in cm.GetDummyIncludes())
            {
                outputSw.WriteLine(inc);
            }
            outputSw.Flush();

            output.Write(structs.GetBuffer(), 0, (int)structs.Length);
            output.Write(declarations.GetBuffer(), 0, (int)declarations.Length);
            output.Write(code.GetBuffer(), 0, (int)code.Length);
            outputSw.Flush();
#if DEBUG
            using (FileStream fs = new FileStream("output.cu", FileMode.Create))
            {
                fs.Write(output.GetBuffer(), 0, (int)output.Length);
            }
#endif
            String s = Encoding.UTF8.GetString(output.GetBuffer(), 0, (int)output.Length);
            //cm.SourceCode = s;// #######!!!
            var scf = new SourceCodeFile(s, Language, _architecture);
            cm.AddSourceCodeFile(scf);
            return(cm);
        }