예제 #1
0
 def _visit_Return(self, stmt):
     results = self._visit(stmt.value)
     if not isinstance(results, (list, PythonTuple, PythonList)):
         results = [results]
     expr = Return(results)
     expr.set_fst(stmt)
     return expr
예제 #2
0
파일: lambdify.py 프로젝트: noushi/pyccel
def lambdify(expr, args):
    if isinstance(args, Lambda):
        new_expr = args.expr
        new_expr = Return(new_expr)
        new_expr.set_fst(expr)
        f_arguments = args.variables
        func = FunctionDef('lambda', f_arguments, [], [new_expr])
        return func

    code = compile(args.body[0], '', 'single')
    g = {}
    eval(code, g)
    f_name = str(args.name)
    code = g[f_name]
    new_args = args.arguments
    new_expr = code(*new_args)
    f_arguments = list(new_expr.free_symbols)
    stmts = cse(new_expr)
    if isinstance(stmts[-1], (Assign, GC)):
        var = stmts[-1].lhs
    else:
        var = create_variable(expr)
        stmts[-1] = Assign(var, stmts[-1])
    stmts += [Return([var])]
    set_fst(stmts, args.fst)
    func = FunctionDef(f_name, new_args, [], stmts, decorators=args.decorators)
    return func
예제 #3
0
    def _create_wrapper_check(self, check_var, parse_args, types_dict, used_names, func_name):
        check_func_body = []
        flags = (len(types_dict) - 1) * 4
        for arg in types_dict:
            var_name = ""
            body = []
            types = []
            arg_type_check_list = list(types_dict[arg])
            arg_type_check_list.sort(key= lambda x : x[0].precision)
            for elem in arg_type_check_list:
                var_name = elem[0].name
                value = elem[2] << flags
                body.append((elem[1], [AugAssign(check_var, '+' ,value)]))
                types.append(elem[0])
            flags -= 4
            error = ' or '.join(['{} bit {}'.format(v.precision * 8 , str_dtype(v.dtype)) if not isinstance(v.dtype, NativeBool)
                            else  str_dtype(v.dtype) for v in types])
            body.append((LiteralTrue(), [PyErr_SetString('PyExc_TypeError', '"{} must be {}"'.format(var_name, error)), Return([LiteralInteger(0)])]))
            check_func_body += [If(*body)]

        check_func_body = [Assign(check_var, LiteralInteger(0))] + check_func_body
        check_func_body.append(Return([check_var]))
        # Creating check function definition
        check_func_name = self.get_new_name(used_names.union(self._global_names), 'type_check')
        self._global_names.add(check_func_name)
        check_func_def = FunctionDef(name = check_func_name,
            arguments = parse_args,
            results = [check_var],
            body = check_func_body,
            local_vars = [])
        return check_func_def
예제 #4
0
    def _body_optional_variable(self,
                                tmp_variable,
                                variable,
                                collect_var,
                                check_type=False):
        """
        Responsible for collecting value and managing error and create the body
        of optional arguments in format
                if (pyobject == Py_None){
                    collect Null
                }else if(Type Check == False){
                    Print TypeError Wrong Type
                    return Null
                }else{
                    assign pyobject value to tmp variable
                    collect the adress of the tmp variable
                }
        Parameters:
        ----------
        tmp_variable : Variable
            The temporary variable  to hold result
        Variable : Variable
            The optional variable
        collect_var : variable
            the pyobject type variable  holder of value
        check_type : Boolean
            True if the type is needed

        Returns
        -------
        body : list
            A list of statements
        """
        body = [(PyccelEq(VariableAddress(collect_var),
                          VariableAddress(Py_None)),
                 [Assign(VariableAddress(variable), Nil())])]
        if check_type:  # Type check
            check = PyccelNot(
                PyccelOr(NumpyType_Check(variable, collect_var),
                         PythonType_Check(variable, collect_var)))
            error = PyErr_SetString(
                'PyExc_TypeError',
                '"{} must be {}"'.format(variable, variable.dtype))
            body += [(check, [error, Return([Nil()])])]
        body += [(LiteralTrue(), [
            self._create_collecting_value_body(variable, collect_var,
                                               tmp_variable),
            Assign(VariableAddress(variable), VariableAddress(tmp_variable))
        ])]
        body = [If(*body)]

        return body
예제 #5
0
파일: codegen.py 프로젝트: pyccel/lampy
    def _visit_LampyLambda(self, stmt):
        func = stmt.func

        # ...
        args = [self._visit(i) for i in func.variables]
        # ...

        # ...
        body = self._visit(func.expr)
        body = MainBlock(body, accelerator=self.accelerator)
        body = [body]
        # ...

        #        # ... DEBUG
        #        body += [Import('omp_get_max_threads', 'pyccel.stdlib.internal.openmp')]
        #
        #        msg = lambda x: (String('> maximum available threads = '), x)
        #        x = Call('omp_get_max_threads', ())
        #        body += [Print(msg(x))]
        #        # ...

        # ...
        results = self._visit(self.main)
        if not isinstance(results, (list, tuple, Tuple)):
            results = [results]
        # ...

        # ... scalar results
        s_results = [r for r in results if r.rank == 0]
        # ...

        # ... vector/matrix results as inout arguments
        m_results = [r for r in results if not r in s_results]
        # ...

        # ... return a function def where
        #     we append m_results to the arguments as inout
        #     and we return all results.
        #     first, we initialize arguments_inout to False for all args
        inout = [False for i in args]
        inout += [True for i in m_results]

        args = args + m_results
        # ...

        # ...
        if len(s_results) == 1:
            body += [Return(s_results[0])]

        elif len(results) > 1:
            body += [Return(s_results)]
        # ...

        # ...
#        decorators = {'types':         build_types_decorator(args),
#                      'external_call': []}

        decorators = {'types': build_types_decorator(args), 'external': []}

        tag = random_string(6)
        name = 'lambda_{}'.format(tag)
        # ...

        return LambdaFunctionDef(name,
                                 args,
                                 s_results,
                                 body,
                                 arguments_inout=inout,
                                 decorators=decorators,
                                 generators=self.generators,
                                 m_results=m_results)
예제 #6
0
    def _print_FunctionDef(self, expr):
        # Save all used names
        used_names = set([a.name for a in expr.arguments] +
                         [r.name for r in expr.results] + [expr.name.name])

        # Find a name for the wrapper function
        wrapper_name = self._get_wrapper_name(used_names, expr)
        used_names.add(wrapper_name)
        # Collect local variables
        wrapper_vars = {a.name: a for a in expr.arguments}
        wrapper_vars.update({r.name: r for r in expr.results})
        python_func_args = self.get_new_PyObject("args", used_names)
        python_func_kwargs = self.get_new_PyObject("kwargs", used_names)
        python_func_selfarg = self.get_new_PyObject("self", used_names)

        # Collect arguments and results
        wrapper_args = [
            python_func_selfarg, python_func_args, python_func_kwargs
        ]
        wrapper_results = [self.get_new_PyObject("result", used_names)]

        if expr.is_private:
            wrapper_func = FunctionDef(
                name=wrapper_name,
                arguments=wrapper_args,
                results=wrapper_results,
                body=[
                    PyErr_SetString(
                        'PyExc_NotImplementedError',
                        '"Private functions are not accessible from python"'),
                    AliasAssign(wrapper_results[0], Nil()),
                    Return(wrapper_results)
                ])
            return CCodePrinter._print_FunctionDef(self, wrapper_func)
        if any(isinstance(arg, FunctionAddress) for arg in expr.arguments):
            wrapper_func = FunctionDef(
                name=wrapper_name,
                arguments=wrapper_args,
                results=wrapper_results,
                body=[
                    PyErr_SetString('PyExc_NotImplementedError',
                                    '"Cannot pass a function as an argument"'),
                    AliasAssign(wrapper_results[0], Nil()),
                    Return(wrapper_results)
                ])
            return CCodePrinter._print_FunctionDef(self, wrapper_func)

        # Collect argument names for PyArgParse
        arg_names = [a.name for a in expr.arguments]
        keyword_list_name = self.get_new_name(used_names, 'kwlist')
        keyword_list = PyArgKeywords(keyword_list_name, arg_names)

        wrapper_body = [keyword_list]
        wrapper_body_translations = []

        parse_args = []
        collect_vars = {}
        for arg in expr.arguments:
            collect_var, cast_func = self.get_PyArgParseType(used_names, arg)
            collect_vars[arg] = collect_var

            body, tmp_variable = self._body_management(used_names, arg,
                                                       collect_var, cast_func,
                                                       True)
            if tmp_variable:
                wrapper_vars[tmp_variable.name] = tmp_variable

            # If the variable cannot be collected from PyArgParse directly
            wrapper_vars[collect_var.name] = collect_var

            # Save cast to argument variable
            wrapper_body_translations.extend(body)

            parse_args.append(collect_var)

            # Write default values
            if isinstance(arg, ValuedVariable):
                wrapper_body.append(
                    self.get_default_assign(parse_args[-1], arg))

        # Parse arguments
        parse_node = PyArg_ParseTupleNode(python_func_args, python_func_kwargs,
                                          expr.arguments, parse_args,
                                          keyword_list)
        wrapper_body.append(If((PyccelNot(parse_node), [Return([Nil()])])))
        wrapper_body.extend(wrapper_body_translations)

        # Call function
        static_function, static_args, additional_body = self._get_static_function(
            used_names, expr, collect_vars)
        wrapper_body.extend(additional_body)
        for var in static_args:
            wrapper_vars[var.name] = var

        if len(expr.results) == 0:
            func_call = FunctionCall(static_function, static_args)
        else:
            results = expr.results if len(
                expr.results) > 1 else expr.results[0]
            func_call = Assign(results,
                               FunctionCall(static_function, static_args))

        wrapper_body.append(func_call)

        # Loop over results to carry out necessary casts and collect Py_BuildValue type string
        res_args = []
        for a in expr.results:
            collect_var, cast_func = self.get_PyBuildValue(used_names, a)
            if cast_func is not None:
                wrapper_vars[collect_var.name] = collect_var
                wrapper_body.append(AliasAssign(collect_var, cast_func))

            res_args.append(
                VariableAddress(collect_var) if collect_var.
                is_pointer else collect_var)

        # Call PyBuildNode
        wrapper_body.append(
            AliasAssign(wrapper_results[0], PyBuildValueNode(res_args)))

        # Call free function for python type
        wrapper_body += [
            FunctionCall(Py_DECREF, [i]) for i in self._to_free_PyObject_list
        ]
        self._to_free_PyObject_list.clear()
        #Return
        wrapper_body.append(Return(wrapper_results))
        # Create FunctionDef and write using classic method
        wrapper_func = FunctionDef(name=wrapper_name,
                                   arguments=wrapper_args,
                                   results=wrapper_results,
                                   body=wrapper_body,
                                   local_vars=wrapper_vars.values())
        return CCodePrinter._print_FunctionDef(self, wrapper_func)
예제 #7
0
    def _print_Interface(self, expr):

        # Collecting all functions
        funcs = expr.functions
        # Save all used names
        used_names = set(n.name for n in funcs)

        # Find a name for the wrapper function
        wrapper_name = self._get_wrapper_name(used_names, expr)
        self._global_names.add(wrapper_name)

        # Collect local variables
        python_func_args = self.get_new_PyObject("args", used_names)
        python_func_kwargs = self.get_new_PyObject("kwargs", used_names)
        python_func_selfarg = self.get_new_PyObject("self", used_names)

        # Collect wrapper arguments and results
        wrapper_args = [
            python_func_selfarg, python_func_args, python_func_kwargs
        ]
        wrapper_results = [self.get_new_PyObject("result", used_names)]

        # Collect parser arguments
        wrapper_vars = {}

        # Collect argument names for PyArgParse
        arg_names = [a.name for a in funcs[0].arguments]
        keyword_list_name = self.get_new_name(used_names, 'kwlist')
        keyword_list = PyArgKeywords(keyword_list_name, arg_names)
        wrapper_body = [keyword_list]

        wrapper_body_translations = []
        body_tmp = []

        # To store the mini function responsible of collecting value and calling interfaces functions and return the builded value
        funcs_def = []
        default_value = {
        }  # dict to collect all initialisation needed in the wrapper
        check_var = Variable(dtype=NativeInteger(),
                             name=self.get_new_name(used_names, "check"))
        wrapper_vars[check_var.name] = check_var
        types_dict = OrderedDict(
            (a, set()) for a in funcs[0].arguments
        )  #dict to collect each variable possible type and the corresponding flags
        # collect parse arg
        parse_args = [
            Variable(dtype=PyccelPyArrayObject(),
                     is_pointer=True,
                     rank=a.rank,
                     order=a.order,
                     name=self.get_new_name(used_names, a.name +
                                            "_tmp")) if a.rank > 0 else
            Variable(dtype=PyccelPyObject(),
                     name=self.get_new_name(used_names, a.name + "_tmp"),
                     is_pointer=True) for a in funcs[0].arguments
        ]
        # Managing the body of wrapper
        for func in funcs:
            mini_wrapper_func_body = []
            res_args = []
            mini_wrapper_func_vars = {a.name: a for a in func.arguments}
            flags = 0
            collect_vars = {}

            # Loop for all args in every functions and create the corresponding condition and body
            for p_arg, f_arg in zip(parse_args, func.arguments):
                collect_vars[f_arg] = p_arg
                body, tmp_variable = self._body_management(
                    used_names, f_arg, p_arg, None)
                if tmp_variable:
                    mini_wrapper_func_vars[tmp_variable.name] = tmp_variable

                # get check type function
                check = self._get_check_type_statement(f_arg, p_arg)
                # If the variable cannot be collected from PyArgParse directly
                wrapper_vars[p_arg.name] = p_arg

                # Save the body
                wrapper_body_translations.extend(body)

                # Write default values
                if isinstance(f_arg, ValuedVariable):
                    wrapper_body.append(
                        self.get_default_assign(parse_args[-1], f_arg))

                flag_value = flags_registry[(f_arg.dtype, f_arg.precision)]
                flags = (flags << 4) + flag_value  # shift by 4 to the left
                types_dict[f_arg].add(
                    (f_arg, check,
                     flag_value))  # collect variable type for each arguments
                mini_wrapper_func_body += body

            # create the corresponding function call
            static_function, static_args, additional_body = self._get_static_function(
                used_names, func, collect_vars)
            mini_wrapper_func_body.extend(additional_body)

            for var in static_args:
                mini_wrapper_func_vars[var.name] = var

            if len(func.results) == 0:
                func_call = FunctionCall(static_function, static_args)
            else:
                results = func.results if len(
                    func.results) > 1 else func.results[0]
                func_call = Assign(results,
                                   FunctionCall(static_function, static_args))

            mini_wrapper_func_body.append(func_call)

            # Loop for all res in every functions and create the corresponding body and cast
            for r in func.results:
                collect_var, cast_func = self.get_PyBuildValue(used_names, r)
                mini_wrapper_func_vars[collect_var.name] = collect_var
                if cast_func is not None:
                    mini_wrapper_func_vars[r.name] = r
                    mini_wrapper_func_body.append(
                        AliasAssign(collect_var, cast_func))
                res_args.append(
                    VariableAddress(collect_var) if collect_var.
                    is_pointer else collect_var)

            # Building PybuildValue and freeing the allocated variable after.
            mini_wrapper_func_body.append(
                AliasAssign(wrapper_results[0], PyBuildValueNode(res_args)))
            mini_wrapper_func_body += [
                FunctionCall(Py_DECREF, [i])
                for i in self._to_free_PyObject_list
            ]
            mini_wrapper_func_body.append(Return(wrapper_results))
            self._to_free_PyObject_list.clear()
            # Building Mini wrapper function
            mini_wrapper_func_name = self.get_new_name(
                used_names.union(self._global_names),
                func.name.name + '_mini_wrapper')
            self._global_names.add(mini_wrapper_func_name)

            mini_wrapper_func_def = FunctionDef(
                name=mini_wrapper_func_name,
                arguments=parse_args,
                results=wrapper_results,
                body=mini_wrapper_func_body,
                local_vars=mini_wrapper_func_vars.values())
            funcs_def.append(mini_wrapper_func_def)

            # append check condition to the functioncall
            body_tmp.append((PyccelEq(check_var, LiteralInteger(flags)), [
                AliasAssign(wrapper_results[0],
                            FunctionCall(mini_wrapper_func_def, parse_args))
            ]))

        # Errors / Types management
        # Creating check_type function
        check_func_def = self._create_wrapper_check(check_var, parse_args,
                                                    types_dict, used_names,
                                                    funcs[0].name.name)
        funcs_def.append(check_func_def)

        # Create the wrapper body with collected informations
        body_tmp = [((PyccelNot(check_var), [Return([Nil()])]))] + body_tmp
        body_tmp.append((LiteralTrue(), [
            PyErr_SetString('PyExc_TypeError',
                            '"Arguments combinations don\'t exist"'),
            Return([Nil()])
        ]))
        wrapper_body_translations = [If(*body_tmp)]

        # Parsing Arguments
        parse_node = PyArg_ParseTupleNode(python_func_args, python_func_kwargs,
                                          funcs[0].arguments, parse_args,
                                          keyword_list, True)
        wrapper_body += list(default_value.values())
        wrapper_body.append(If((PyccelNot(parse_node), [Return([Nil()])])))

        #finishing the wrapper body
        wrapper_body.append(
            Assign(check_var, FunctionCall(check_func_def, parse_args)))
        wrapper_body.extend(wrapper_body_translations)
        wrapper_body.append(Return(wrapper_results))  # Return

        # Create FunctionDef
        funcs_def.append(
            FunctionDef(name=wrapper_name,
                        arguments=wrapper_args,
                        results=wrapper_results,
                        body=wrapper_body,
                        local_vars=wrapper_vars.values()))

        sep = self._print(SeparatorComment(40))

        return sep + '\n'.join(
            CCodePrinter._print_FunctionDef(self, f) for f in funcs_def)
예제 #8
0
    def _body_array(self, variable, collect_var, check_type=False):
        """
        Responsible for collecting value and managing error and create the body
        of arguments with rank greater than 0 in format
                if (rank check == False){
                    print TypeError Wrong rank
                    return Null
                }else if(Type Check == False){
                    Print TypeError Wrong type
                    return Null
                }else if (order check == False){ #check for order for rank > 1
                    Print NotImplementedError Wrong Order
                    return Null
                }
                collect the value from PyArrayObject

        Parameters:
        ----------
        Variable : Variable
            The optional variable
        collect_var : variable
            the pyobject type variable  holder of value
        check_type : Boolean
            True if the type is needed

        Returns
        -------
        body : list
            A list of statements
        """
        body = []
        #TODO create and extern rank and order check function
        #check optional :
        if variable.is_optional:
            check = PyccelNot(VariableAddress(collect_var))
            body += [(check, [Assign(VariableAddress(variable), Nil())])]

        #rank check :
        check = PyccelNe(FunctionCall(numpy_get_ndims, [collect_var]),
                         LiteralInteger(collect_var.rank))
        error = PyErr_SetString(
            'PyExc_TypeError',
            '"{} must have rank {}"'.format(collect_var,
                                            str(collect_var.rank)))
        body += [(check, [error, Return([Nil()])])]
        if check_type:  #Type check
            numpy_dtype = self.find_in_numpy_dtype_registry(variable)
            arg_dtype = self.find_in_dtype_registry(
                self._print(variable.dtype), variable.precision)
            check = PyccelNe(FunctionCall(numpy_get_type, [collect_var]),
                             numpy_dtype)
            info_dump = PythonPrint(
                [FunctionCall(numpy_get_type, [collect_var]), numpy_dtype])
            error = PyErr_SetString(
                'PyExc_TypeError',
                '"{} must be {}"'.format(variable, arg_dtype))
            body += [(check, [info_dump, error, Return([Nil()])])]

        if collect_var.rank > 1 and self._target_language == 'fortran':  #Order check
            if collect_var.order == 'F':
                check = FunctionCall(numpy_check_flag,
                                     [collect_var, numpy_flag_f_contig])
            else:
                check = FunctionCall(numpy_check_flag,
                                     [collect_var, numpy_flag_c_contig])
                error = PyErr_SetString(
                    'PyExc_NotImplementedError',
                    '"Argument does not have the expected ordering ({})"'.
                    format(collect_var.order))
                body += [(PyccelNot(check), [error, Return([Nil()])])]
        body += [(LiteralTrue(), [
            Assign(VariableAddress(variable),
                   self.get_collect_function_call(variable, collect_var))
        ])]
        body = [If(*body)]

        return body
예제 #9
0
파일: interface.py 프로젝트: pyccel/lampy
    def __new__(cls, func, import_lambda):

        # ...
        m_results = func.m_results

        name = 'interface_{}'.format(func.name)
        args = [i for i in func.arguments if not i in m_results]
        s_results = func.results

        results = list(s_results) + list(m_results)
        # ...

        # ...
        imports = [import_lambda]
        stmts = []
        # ...

        # ... out argument
        if len(results) == 1:
            outs = [Symbol('out')]

        else:
            outs = [Symbol('out_{}'.format(i)) for i in range(0, len(results))]
        # ...

        # ...
        generators = func.generators
        d_shapes = {}
        for i in m_results:
            d_shapes[i] = compute_shape(i, generators)
        # ...

        # ... TODO build statements
        if_cond = Is(Symbol('out'), Nil())

        if_body = []

        # TODO add imports from numpy
        if_body += [Import('zeros', 'numpy')]
        if_body += [Import('float64', 'numpy')]

        for i, var in enumerate(results):
            if var in m_results:
                shaping = d_shapes[var]

                if_body += shaping.stmts
                if_body += [Assign(outs[i], Zeros(shaping.var, var.dtype))]

        # update statements
        stmts = [If((if_cond, if_body))]
        # ...

        # ... add call to the python or pyccelized function
        stmts += [FunctionCall(func, args + outs)]
        # ...

        # ... add return out
        if len(outs) == 1:
            stmts += [Return(outs[0])]

        else:
            stmts += [Return(outs)]
        # ...

        # ...
        body = imports + stmts
        # ...

        # update arguments with optional
        args += [Assign(Symbol('out'), Nil())]

        return FunctionDef(name, args, results, body)