Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                string inputFileName;
                string templateFileName;

                if ((args.Length == 1) || (args.Length == 2))
                {
                    if (args.Length == 1)
                    {
                        templateFileName = "CUDAC.stg";
                        inputFileName = args[0];
                    }
                    else
                    {
                        templateFileName = args[0];
                        inputFileName = args[1];
                    }

                    if (!Path.IsPathRooted(templateFileName))
                    {
                        //templateFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateFileName);
                        templateFileName = Path.Combine(Environment.CurrentDirectory, templateFileName);
                    }
                    if (!Path.IsPathRooted(inputFileName))
                    {
                        inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
                    }

                    templates = new StringTemplateGroup(new StreamReader(templateFileName),
                                        typeof(AngleBracketTemplateLexer));
                    templates2 = new StringTemplateGroup(new StreamReader("CUDAPattern.stg"),
                                        typeof(AngleBracketTemplateLexer));
                    templates3 = new StringTemplateGroup(new StreamReader("CUDACkernel.stg"),
                                       typeof(AngleBracketTemplateLexer));

                    //ArrayList one=new ArrayList();
                    //one.Add("i");
                    ////one.Add("+");
                    ////one.Add("1");

                    //ArrayList two = new ArrayList();
                    //two.Add("i");

                    Analysis analysis = new Analysis();
                    //if (analysis.BenergyTest(one, two))
                    //{
                    //    Console.Out.WriteLine("Iterative Dependecy");
                    //}
                    //else
                    //{
                    //    Console.Out.WriteLine("No Dependecy");
                    //}
                    //return;

                    ICharStream input = new ANTLRFileStream(inputFileName);
                    C2CUDATranslatorLexer lexer = new C2CUDATranslatorLexer(input);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    C2CUDATranslatorParser parser = new C2CUDATranslatorParser(tokens);
                    parser.TemplateLib = templates;
                    RuleReturnScope r = parser.program();
                    Console.Out.WriteLine(r.Template.ToString().Replace(@"\/", "/").ToString());
                    // Part 2
                    // Console.Error.WriteLine("Getting Pattern");
                    ICharStream input3 = new ANTLRFileStream(inputFileName);
                    C2CUDATranslatorLexer lexer3 = new C2CUDATranslatorLexer(input3);
                    CommonTokenStream tokens3 = new CommonTokenStream(lexer3);
                    C2CUDATranslatorParser parser3 = new C2CUDATranslatorParser(tokens3);
                    parser3.TemplateLib = templates3;
                    RuleReturnScope r3 = parser3.program();

                    ICharStream input2 = new ANTLRStringStream(r3.Template.ToString().Replace("((", "( (").Replace(@"\/", "/").ToString().ToString().Replace("))", ") )").ToString().Replace("){", ") {").ToString().Replace("**", "*").ToString());
                    PatternGeneratorLexer lexer2 = new PatternGeneratorLexer(input2);
                    CommonTokenStream tokens2 = new CommonTokenStream(lexer2);
                    PatternGeneratorParser parser2 = new PatternGeneratorParser(tokens2);
                    parser2.TemplateLib = templates2;
                    RuleReturnScope r2 = parser2.pattern();
                    Console.Error.WriteLine("// Pattern : ");
                    string pattern= r2.Template.ToString();
                    Console.Out.WriteLine("//" + r2.Template.ToString().Replace("\r\n","\r\n//"));

                    //Console.Out.WriteLine("Checking in Database");

                    //string connecstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
                    //SqlConnection con = new SqlConnection(connecstr);
                    //con.Close();
                    //con.Open();
                    //String sql = "Select * from PatternDatabase";
                    //DataTable dt = new DataTable();
                    //SqlDataAdapter adp = new SqlDataAdapter(sql,con);
                    //adp.Fill(dt);
                    //bool flag=false ;
                    //for(int i=0;i<dt.Rows.Count -1 ;i++)
                    //{
                    //    if(pattern==dt.Rows[i][1].ToString())
                    //    {
                    //        Console.Out.WriteLine("Found in Database");
                    //        flag =true ;
                    //    }
                    //    else
                    //    {
                    //    }
                    //}
                    //if (flag == true)
                    //{
                    //}
                    //else
                    //{
                    //    sql = "Insert into PatternDatabase values('"+pattern.ToString()+"','"+pattern.ToString()+"')";
                    //    SqlCommand cmd = new SqlCommand(sql, con);
                    //    cmd.ExecuteNonQuery();
                    //    con.Close();
                    //    Console.Out.WriteLine("Not Found in Database But added in database");
                    //}
                    //con.Close();
                    string lines = "group CUDA;\r\n pattern(type,name,args,";

                     string[] p = pattern.ToString().Replace("{", " ").ToString().Replace("}", " ").ToString().Replace("\n", ",").Replace(@"\/", "/").ToString().Substring(34, pattern.Length - 34).ToString().Split(',');
                    for (int i = 0;i< p.Length  -1; i++)
                    {
                        if (p[i].ToString().Trim() != "")
                        {
                            lines = lines + p[i].ToString().Trim() + ",\r\n";
                        }
                    }
                    lines = lines + ") ::= << " + "\r\n" + "<type> <name>(<args; separator=\"\\n\">) {" + "\r\n";

                    for (int i = 0;i< p.Length  -1; i++)
                    {
                        if (p[i].ToString().Trim() != "")
                        {
                            lines = lines + " <" + p[i].ToString().Trim() + ">" + "\r\n";
                        }
                    }
                    lines = lines + ">>";
                    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.stg");
                    file.WriteLine(lines);

                    file.Close();

                    Console.ReadLine();

                }
                else
                    Console.Error.WriteLine("Usage: C2CUDATranslator [<output-template-file>] <input-file>");
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine("exception: " + e);
                Console.Error.WriteLine(e.StackTrace);
                Console.ReadLine();
            }
        }
       string GetIndexofe1(C2CUDATranslatorParser.kassignStat_return e1)
                {

                    string index = "";
                    if (e1==null)
                	{
                	return index;
                	}
                    int i=0;
                    while (e1.ST.ToString()[i] != '=')
                    {
                        index = index + e1.ST.ToString()[i];
                        i++;
                    }
                    return index;
                }
          string Getsizeofe2(C2CUDATranslatorParser.kexpr_return  e2)
                   {
                       string index = "";
                       string temp = "";
                       Boolean flag = false;
                        if (e2==null)
                    	{
                    	return index;
                    	}
                       int i = 0;
                      
                       
                       for (i = 0; i < e2.ST.ToString().Length; i++)
                       {
                           if (e2.ST.ToString()[i] >= '0' && e2.ST.ToString()[i] <= '9')
                           {
                               index = index + e2.ST.ToString()[i];
                               
                           }
                           else
                           {
                               if (e2.ST.ToString()[i] == '>' || e2.ST.ToString()[i] == '<' || e2.ST.ToString()[i] == '=' || e2.ST.ToString()[i] == '!')
                               {
                                   flag=true ;
                               }
                               if ((e2.ST.ToString()[i] >= 'A' && e2.ST.ToString()[i] <= 'Z') || (e2.ST.ToString()[i] >= 'a' && e2.ST.ToString()[i] <= 'z') || (e2.ST.ToString()[i] >= '_'))
                               {
                                   if (flag)
                                   {
                                       temp = temp + e2.ST.ToString()[i];
                                   }
                               }
                               else
                               {
                                   
                               }
                           }

                       }
                      temp= temp.Replace("gpu_","").ToString();
                  for(i=0;i<((program_scope)program_stack.Peek()).Constants.Count;i++)
                  {
                      String s = ((program_scope)program_stack.Peek()).Constants[i].ToString();
                      if (String.Compare(temp, s) == 0)
                      {
                          index = temp;
                      }
                  }
                      
                       return index;
                   }
Beispiel #4
0
        static void Main(string[] args)
        {
            try
            {
                string inputFileName;
                string templateFileName;

                if ((args.Length == 1) || (args.Length == 2))
                {
                    if (args.Length == 1)
                    {
                        templateFileName = "CUDAC.stg";
                        inputFileName    = args[0];
                    }
                    else
                    {
                        templateFileName = args[0];
                        inputFileName    = args[1];
                    }


                    if (!Path.IsPathRooted(templateFileName))
                    {
                        //templateFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateFileName);
                        templateFileName = Path.Combine(Environment.CurrentDirectory, templateFileName);
                    }
                    if (!Path.IsPathRooted(inputFileName))
                    {
                        inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
                    }

                    templates = new StringTemplateGroup(new StreamReader(templateFileName),
                                                        typeof(AngleBracketTemplateLexer));
                    templates2 = new StringTemplateGroup(new StreamReader("CUDAPattern.stg"),
                                                         typeof(AngleBracketTemplateLexer));
                    templates3 = new StringTemplateGroup(new StreamReader("CUDACkernel.stg"),
                                                         typeof(AngleBracketTemplateLexer));

                    //ArrayList one=new ArrayList();
                    //one.Add("i");
                    ////one.Add("+");
                    ////one.Add("1");

                    //ArrayList two = new ArrayList();
                    //two.Add("i");

                    Analysis analysis = new Analysis();
                    //if (analysis.BenergyTest(one, two))
                    //{
                    //    Console.Out.WriteLine("Iterative Dependecy");
                    //}
                    //else
                    //{
                    //    Console.Out.WriteLine("No Dependecy");
                    //}
                    //return;

                    ICharStream            input  = new ANTLRFileStream(inputFileName);
                    C2CUDATranslatorLexer  lexer  = new C2CUDATranslatorLexer(input);
                    CommonTokenStream      tokens = new CommonTokenStream(lexer);
                    C2CUDATranslatorParser parser = new C2CUDATranslatorParser(tokens);
                    parser.TemplateLib = templates;
                    RuleReturnScope r = parser.program();
                    Console.Out.WriteLine(r.Template.ToString().Replace(@"\/", "/").ToString());
                    // Part 2
                    // Console.Error.WriteLine("Getting Pattern");
                    ICharStream            input3  = new ANTLRFileStream(inputFileName);
                    C2CUDATranslatorLexer  lexer3  = new C2CUDATranslatorLexer(input3);
                    CommonTokenStream      tokens3 = new CommonTokenStream(lexer3);
                    C2CUDATranslatorParser parser3 = new C2CUDATranslatorParser(tokens3);
                    parser3.TemplateLib = templates3;
                    RuleReturnScope r3 = parser3.program();


                    ICharStream            input2  = new ANTLRStringStream(r3.Template.ToString().Replace("((", "( (").Replace(@"\/", "/").ToString().ToString().Replace("))", ") )").ToString().Replace("){", ") {").ToString().Replace("**", "*").ToString());
                    PatternGeneratorLexer  lexer2  = new PatternGeneratorLexer(input2);
                    CommonTokenStream      tokens2 = new CommonTokenStream(lexer2);
                    PatternGeneratorParser parser2 = new PatternGeneratorParser(tokens2);
                    parser2.TemplateLib = templates2;
                    RuleReturnScope r2 = parser2.pattern();
                    Console.Error.WriteLine("// Pattern : ");
                    string pattern = r2.Template.ToString();
                    Console.Out.WriteLine("//" + r2.Template.ToString().Replace("\r\n", "\r\n//"));


                    //Console.Out.WriteLine("Checking in Database");

                    //string connecstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";
                    //SqlConnection con = new SqlConnection(connecstr);
                    //con.Close();
                    //con.Open();
                    //String sql = "Select * from PatternDatabase";
                    //DataTable dt = new DataTable();
                    //SqlDataAdapter adp = new SqlDataAdapter(sql,con);
                    //adp.Fill(dt);
                    //bool flag=false ;
                    //for(int i=0;i<dt.Rows.Count -1 ;i++)
                    //{
                    //    if(pattern==dt.Rows[i][1].ToString())
                    //    {
                    //        Console.Out.WriteLine("Found in Database");
                    //        flag =true ;
                    //    }
                    //    else
                    //    {
                    //    }
                    //}
                    //if (flag == true)
                    //{
                    //}
                    //else
                    //{
                    //    sql = "Insert into PatternDatabase values('"+pattern.ToString()+"','"+pattern.ToString()+"')";
                    //    SqlCommand cmd = new SqlCommand(sql, con);
                    //    cmd.ExecuteNonQuery();
                    //    con.Close();
                    //    Console.Out.WriteLine("Not Found in Database But added in database");
                    //}
                    //con.Close();
                    string lines = "group CUDA;\r\n pattern(type,name,args,";

                    string[] p = pattern.ToString().Replace("{", " ").ToString().Replace("}", " ").ToString().Replace("\n", ",").Replace(@"\/", "/").ToString().Substring(34, pattern.Length - 34).ToString().Split(',');
                    for (int i = 0; i < p.Length - 1; i++)
                    {
                        if (p[i].ToString().Trim() != "")
                        {
                            lines = lines + p[i].ToString().Trim() + ",\r\n";
                        }
                    }
                    lines = lines + ") ::= << " + "\r\n" + "<type> <name>(<args; separator=\"\\n\">) {" + "\r\n";

                    for (int i = 0; i < p.Length - 1; i++)
                    {
                        if (p[i].ToString().Trim() != "")
                        {
                            lines = lines + " <" + p[i].ToString().Trim() + ">" + "\r\n";
                        }
                    }
                    lines = lines + ">>";
                    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.stg");
                    file.WriteLine(lines);

                    file.Close();



                    Console.ReadLine();
                }
                else
                {
                    Console.Error.WriteLine("Usage: C2CUDATranslator [<output-template-file>] <input-file>");
                }
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine("exception: " + e);
                Console.Error.WriteLine(e.StackTrace);
                Console.ReadLine();
            }
        }