Ejemplo n.º 1
0
        private static bool GenerateOMPParallelForCall(statement_node body, SyntaxTree.for_node for_node, var_definition_node loop_variable, statements_list omp_stmts, syntax_tree_visitor syntax_tree_visitor, expression_node fromInclusive, expression_node toInclusive)
        {
            SyntaxTree.statement syntax_body = for_node.statements;

            expression_node omp_call = null;
            base_function_call bfc = body as base_function_call;
            if (bfc != null && bfc.parameters.Count == 1 && bfc.parameters[0] is variable_reference &&
                ((variable_reference)bfc.parameters[0]).VariableDefinition == loop_variable && ((bfc.function.parameters[0].type as type_node).PrintableName.ToLower() == "integer"))
            {
                //если тело цикла - вызов функции с одни параметром - переменной цикла,
                //если при этом у вызываемой функции тип параметра - integer, а не какой-нибудь object, как это бывает с write и вообще может быть с перегрузкой
                //то генерировать класс не надо.
                //генерируем вызов и все
                omp_call = syntax_tree_visitor.CreateDelegateCall(bfc);
                if (omp_call == null)
                {
                    syntax_tree_visitor.AddWarning(new OMP_ConstructionNotSupportedNow(body.location));
                    syntax_tree_visitor.convertion_data_and_alghoritms.statement_list_stack_pop();
                    return false;
                }
                base_function_call omp_parallel_for_call = null;
                if (SystemLibrary.SystemLibInitializer.OMP_ParallelFor.sym_info is common_namespace_function_node)
                    omp_parallel_for_call = new common_namespace_function_call(SystemLibrary.SystemLibInitializer.OMP_ParallelFor.sym_info as common_namespace_function_node, body.location);
                else
                    omp_parallel_for_call = new compiled_static_method_call(SystemLibrary.SystemLibInitializer.OMP_ParallelFor.sym_info as compiled_function_node, body.location);
                omp_parallel_for_call.parameters.AddElement(fromInclusive);
                omp_parallel_for_call.parameters.AddElement(toInclusive);
                omp_parallel_for_call.parameters.AddElement(omp_call);
                omp_stmts.statements.AddElement(omp_parallel_for_call);
            }
            else
            {
                //ищем используемые переменные, получаем редукцию из директивы и составляем список переменных по типам
                VarFinderSyntaxVisitor VFvis = new VarFinderSyntaxVisitor(syntax_body, syntax_tree_visitor.context, true);
                SyntaxTree.compiler_directive dir = syntax_tree_visitor.DirectivesToNodesLinks[for_node];
                //if (DirInfosTable[dir].ErrorName == "WARNING_IN_CLAUSE_PARAMETERS_REPEATED_VARS")
                //    syntax_tree_visitor.AddWarning(new Errors.CommonWarning(StringResources.Get(DirInfosTable[dir].ErrorName), for_node.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                //else if (DirInfosTable[dir].ErrorName == "ERROR_IN_CLAUSE_PARAMETERS")
                //{
                //    syntax_tree_visitor.AddWarning(new Errors.CommonWarning(StringResources.Get(DirInfosTable[dir].ErrorName), for_node.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                //}
                //else
               
                if (DirInfosTable[dir].ErrorName !=null)//== "ERROR_IN_CLAUSE")
                {
                    syntax_tree_visitor.AddWarning(new Errors.CommonWarning(PascalABCCompiler.StringResources.Get(DirInfosTable[dir].ErrorName), for_node.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                }

                VarInfoContainer Vars = GetVarInfoContainer(VFvis, DirInfosTable[dir].Reductions, DirInfosTable[dir].Privates, syntax_tree_visitor, dir);

                //сохраняем контекст
                ContextInfo contextInfo = new ContextInfo(syntax_tree_visitor);

                string ClassName = syntax_tree_visitor.context.get_free_name("$for_class{0}");

                try
                {
                    //создаем и конвертируем класс
                    SyntaxTree.class_members member;
                    SyntaxTree.type_declarations Decls = CreateClass(ClassName, out member, Vars);
                    member.members.Add(CreateMethod("Method", syntax_body, for_node.loop_variable.name, member, Vars));
                    syntax_tree_visitor.visit(Decls);
                }
                finally
                {
                    //восстанавливаем контекст
                    contextInfo.RestoreContext(syntax_tree_visitor);
                }

                //создаем инициализацию, вызов и финализацию
                string ObjName = syntax_tree_visitor.context.get_free_name("$for_obj{0}");

                SyntaxTree.dot_node dn = new SyntaxTree.dot_node(new SyntaxTree.ident(ObjName), new SyntaxTree.ident("Method"));

                SyntaxTree.statement_list stl = CreateInitPart(ClassName, ObjName, Vars);
                stl.subnodes.Add(CreateNestedRegionBorder(true));
                stl.subnodes.Add(CreateOMPParallelForCall(dn, for_node.initial_value, for_node.finish_value));
                stl.subnodes.Add(CreateNestedRegionBorder(false));
                stl.subnodes.AddRange(CreateFinalPart(ObjName, Vars).subnodes);
                omp_stmts.statements.AddElement(syntax_tree_visitor.ret.visit(stl));
            }
            return true;
        }
Ejemplo n.º 2
0
        //инициализация критических секций с созданием класса
        private static void InitCriticals(syntax_tree_visitor visitor)
        {
            //генерируем класс
            SyntaxTree.type_declarations TypeDecls = new PascalABCCompiler.SyntaxTree.type_declarations();
            SyntaxTree.type_declaration TypeDecl = new PascalABCCompiler.SyntaxTree.type_declaration();
            TypeDecls.types_decl.Add(TypeDecl);
            LocksName = visitor.context.get_free_name("$locks_container{0}");
            TypeDecl.type_name = new PascalABCCompiler.SyntaxTree.ident(LocksName);
            SyntaxTree.class_definition ClassDef = new PascalABCCompiler.SyntaxTree.class_definition();
            TypeDecl.type_def = ClassDef;
            SyntaxTree.class_body ClassBody = new PascalABCCompiler.SyntaxTree.class_body();
            ClassDef.body = ClassBody;
            SyntaxTree.class_members ClassMember = new PascalABCCompiler.SyntaxTree.class_members();
            ClassBody.class_def_blocks.Add(ClassMember);
            ClassMember.access_mod = new PascalABCCompiler.SyntaxTree.access_modifer_node(PascalABCCompiler.SyntaxTree.access_modifer.public_modifer);

            List<string> ProcessedNames = new List<string>();

            foreach (KeyValuePair<SyntaxTree.compiler_directive, DirectiveInfo> pair in DirInfosTable)
            {

                if (pair.Value.Kind == DirectiveKind.Critical)
                {
                    string LockName = "$default";
                    if (pair.Value.Name.Length != 0)
                        LockName = pair.Value.Name;

                    if (ProcessedNames.Contains(LockName))
                        continue;
                    ProcessedNames.Add(LockName);

                    SyntaxTree.var_def_statement vds = new PascalABCCompiler.SyntaxTree.var_def_statement();
                    SyntaxTree.ident_list idl = new PascalABCCompiler.SyntaxTree.ident_list();
                    vds.vars = idl;
                    idl.Add(new SyntaxTree.ident(LockName));
                    SyntaxTree.named_type_reference ntr = new PascalABCCompiler.SyntaxTree.named_type_reference();
                    vds.vars_type = ntr;
                    ntr.Add(new SyntaxTree.ident("object"));
                    SyntaxTree.new_expr ne = new PascalABCCompiler.SyntaxTree.new_expr();
                    vds.inital_value = ne;
                    ne.type = ntr;
                    vds.var_attr = PascalABCCompiler.SyntaxTree.definition_attribute.Static;
                    ClassMember.members.Add(vds);
                }

            }
            //сохраняем контекст
            ContextInfo contextInfo = new ContextInfo(visitor);

            try
            {
                visitor.visit(TypeDecls);
                LocksInitialized = true;
            }
            finally
            {
                //восстанавливаем контекст
                contextInfo.RestoreContext(visitor);
            }
        }
Ejemplo n.º 3
0
 private static if_node CreateIfCondition(syntax_tree_visitor syntax_tree_visitor, statements_list ifthen, statements_list ifelse, location loc)
 {
     //сохраняем контекст
     ContextInfo contextInfo = new ContextInfo(syntax_tree_visitor);
     
     if_node res = null;
     try
     {
         SyntaxTree.if_node if_node = new PascalABCCompiler.SyntaxTree.if_node();
         SyntaxTree.un_expr une = new PascalABCCompiler.SyntaxTree.un_expr();
         une.operation_type = SyntaxTree.Operators.LogicalNOT;
         une.subnode = new SyntaxTree.ident(InParallelSection);
         if_node.condition = une;
         if_node.then_body = new SyntaxTree.empty_statement();
         if_node.then_body = new SyntaxTree.empty_statement();
         res = syntax_tree_visitor.convert_strong(if_node) as if_node;
         res.then_body = ifthen;
         res.else_body = ifelse;
         res.location = loc;
     }
     finally
     {
         //восстанавливаем контекст
         contextInfo.RestoreContext(syntax_tree_visitor);
     }
     return res;
 }
Ejemplo n.º 4
0
        private static void CreateInParallelVariable(syntax_tree_visitor syntax_tree_visitor, out string VarName)
        {
            //сохраняем контекст
            ContextInfo contextInfo = new ContextInfo(syntax_tree_visitor);

            VarName = "$InParallelSection";

            try
            {
                //создание и конвертирование переменной                
                SyntaxTree.var_def_statement vds = new PascalABCCompiler.SyntaxTree.var_def_statement();
                SyntaxTree.variable_definitions var_def = new PascalABCCompiler.SyntaxTree.variable_definitions(vds, null);
                SyntaxTree.ident_list idl = new PascalABCCompiler.SyntaxTree.ident_list();
                vds.vars = idl;
                idl.Add(new SyntaxTree.ident(VarName));
                SyntaxTree.named_type_reference ntr = new PascalABCCompiler.SyntaxTree.named_type_reference();
                vds.vars_type = ntr;
                ntr.names.Add(new PascalABCCompiler.SyntaxTree.ident("boolean"));
                vds.inital_value = new PascalABCCompiler.SyntaxTree.ident("false");
                syntax_tree_visitor.visit(var_def);
            }
            finally
            {
                //восстанавливаем контекст
                contextInfo.RestoreContext(syntax_tree_visitor);
            }
            InParallelSectionCreated = true;
        }
Ejemplo n.º 5
0
        private static bool GenerateOMPParallelSectionsCall(statements_list stmts, SyntaxTree.statement_list syntax_stmts, statements_list omp_stmts, syntax_tree_visitor syntax_tree_visitor)
        {

            SyntaxTree.expression_list delegates = new PascalABCCompiler.SyntaxTree.expression_list();
            SyntaxTree.statement_list stlInit = new PascalABCCompiler.SyntaxTree.statement_list();
            SyntaxTree.statement_list stlFinal = new PascalABCCompiler.SyntaxTree.statement_list();
            VarInfoContainer Vars = new VarInfoContainer();
            string ClassName = syntax_tree_visitor.context.get_free_name("$section_class{0}");
            List<SyntaxTree.statement> Sections = new List<PascalABCCompiler.SyntaxTree.statement>();
            foreach (SyntaxTree.statement syntax_statement in syntax_stmts.subnodes)
            {
                if (syntax_statement is SyntaxTree.empty_statement)
                    continue;       //А зачем? ;-)
                if (syntax_statement is SyntaxTree.var_statement)
                {
                    //выдать предупреждение. Это не нормально для параллельных секций
                    syntax_tree_visitor.visit(syntax_statement as SyntaxTree.var_statement);
                }
                else
                {
                    //ищем используемые переменные
                    VarFinderSyntaxVisitor VFvis = new VarFinderSyntaxVisitor(syntax_statement, syntax_tree_visitor.context, false);
                    SyntaxTree.compiler_directive dir = syntax_tree_visitor.DirectivesToNodesLinks[syntax_stmts];
                    
                    //if (DirInfosTable[dir].ErrorName == "WARNING_IN_CLAUSE_PARAMETERS_REPEATED_VARS")
                    //    syntax_tree_visitor.AddWarning(new Errors.CommonWarning(StringResources.Get(DirInfosTable[dir].ErrorName), syntax_stmts.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                    //else if (DirInfosTable[dir].ErrorName == "ERROR_IN_CLAUSE_PARAMETERS")
                    //{
                    //    syntax_tree_visitor.AddWarning(new Errors.CommonWarning(StringResources.Get(DirInfosTable[dir].ErrorName), syntax_stmts.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                    //}
                    //else
                    if (DirInfosTable[dir].ErrorName != null)
                    {
                        syntax_tree_visitor.AddWarning(new Errors.CommonWarning(PascalABCCompiler.StringResources.Get(DirInfosTable[dir].ErrorName), syntax_stmts.source_context.FileName, DirInfosTable[dir].SC.begin_position.line_num, DirInfosTable[dir].SC.begin_position.column_num));
                    }
                    Vars.UnionWith(GetVarInfoContainer(VFvis, null, DirInfosTable[dir].Privates, syntax_tree_visitor, dir));
                    Sections.Add(syntax_statement);
                }
            }
            //сохраняем контекст
            ContextInfo contextInfo = new ContextInfo(syntax_tree_visitor);

            try
            {
                //создание и конвертирование класса
                SyntaxTree.class_members member;
                SyntaxTree.type_declarations Decls = CreateClass(ClassName, out member, Vars);
                for (int i = 0; i < Sections.Count; ++i)
                    member.members.Add(CreateMethod("method" + i.ToString(), Sections[i], "", member, Vars));
                syntax_tree_visitor.visit(Decls);
            }
            finally
            {
                //восстанавливаем контекст
                contextInfo.RestoreContext(syntax_tree_visitor);
            }
            //создаем инициализацию и финализацию
            int NameNum = 0;
            string ObjName = GetFreeName("$section_obj", ref NameNum, syntax_tree_visitor.context);

            stlInit.subnodes.AddRange(CreateInitPart(ClassName, ObjName, Vars).subnodes);
            stlFinal.subnodes.AddRange(CreateFinalPart(ObjName, Vars).subnodes);

            SyntaxTree.procedure_call pc = new PascalABCCompiler.SyntaxTree.procedure_call();
            SyntaxTree.method_call mc = new PascalABCCompiler.SyntaxTree.method_call();
            mc.dereferencing_value = CreateTPLFunctionReference("Invoke");
            pc.func_name = mc;
            SyntaxTree.expression_list exl = new PascalABCCompiler.SyntaxTree.expression_list();
            //foreach (string str in ObjNames)
            for (int i=0; i<Sections.Count; ++i)
                exl.Add(new SyntaxTree.dot_node(new SyntaxTree.ident(ObjName), new SyntaxTree.ident("Method"+i.ToString())));
            mc.parameters = exl;

            stlInit.subnodes.Add(CreateNestedRegionBorder(true));
            stlInit.subnodes.Add(pc);
            stlInit.subnodes.AddRange(stlFinal.subnodes);
            stlInit.subnodes.Add(CreateNestedRegionBorder(false));

            statement_node st = syntax_tree_visitor.ret.visit(stlInit);
            omp_stmts.statements.AddElement(st);
            return true;
        }