1from collections import namedtuple, defaultdict
2import copy
3import os
4import sys
5from itertools import permutations, takewhile
6from contextlib import contextmanager
7
8import numpy as np
9
10from llvmlite import ir as llvmir
11import llvmlite.llvmpy.core as lc
12from llvmlite.llvmpy.core import Type, Constant, LLVMException
13import llvmlite.binding as ll
14
15from numba.core import types, utils, typing, datamodel, debuginfo, funcdesc, config, cgutils, imputils
16from numba import _dynfunc, _helperlib
17from numba.core.compiler_lock import global_compiler_lock
18from numba.core.pythonapi import PythonAPI
19from numba.np import arrayobj
20from numba.core.imputils import (user_function, user_generator,
21                       builtin_registry, impl_ret_borrowed,
22                       RegistryLoader)
23from numba.cpython import builtins
24
25
26GENERIC_POINTER = Type.pointer(Type.int(8))
27PYOBJECT = GENERIC_POINTER
28void_ptr = GENERIC_POINTER
29
30
31class OverloadSelector(object):
32    """
33    An object matching an actual signature against a registry of formal
34    signatures and choosing the best candidate, if any.
35
36    In the current implementation:
37    - a "signature" is a tuple of type classes or type instances
38    - the "best candidate" is the most specific match
39    """
40
41    def __init__(self):
42        # A list of (formal args tuple, value)
43        self.versions = []
44        self._cache = {}
45
46    def find(self, sig):
47        out = self._cache.get(sig)
48        if out is None:
49            out = self._find(sig)
50            self._cache[sig] = out
51        return out
52
53    def _find(self, sig):
54        candidates = self._select_compatible(sig)
55        if candidates:
56            return candidates[self._best_signature(candidates)]
57        else:
58            raise NotImplementedError(self, sig)
59
60    def _select_compatible(self, sig):
61        """
62        Select all compatible signatures and their implementation.
63        """
64        out = {}
65        for ver_sig, impl in self.versions:
66            if self._match_arglist(ver_sig, sig):
67                out[ver_sig] = impl
68        return out
69
70    def _best_signature(self, candidates):
71        """
72        Returns the best signature out of the candidates
73        """
74        ordered, genericity = self._sort_signatures(candidates)
75        # check for ambiguous signatures
76        if len(ordered) > 1:
77            firstscore = genericity[ordered[0]]
78            same = list(takewhile(lambda x: genericity[x] == firstscore,
79                                  ordered))
80            if len(same) > 1:
81                msg = ["{n} ambiguous signatures".format(n=len(same))]
82                for sig in same:
83                    msg += ["{0} => {1}".format(sig, candidates[sig])]
84                raise TypeError('\n'.join(msg))
85        return ordered[0]
86
87    def _sort_signatures(self, candidates):
88        """
89        Sort signatures in ascending level of genericity.
90
91        Returns a 2-tuple:
92
93            * ordered list of signatures
94            * dictionary containing genericity scores
95        """
96        # score by genericity
97        genericity = defaultdict(int)
98        for this, other in permutations(candidates.keys(), r=2):
99            matched = self._match_arglist(formal_args=this, actual_args=other)
100            if matched:
101                # genericity score +1 for every another compatible signature
102                genericity[this] += 1
103        # order candidates in ascending level of genericity
104        ordered = sorted(candidates.keys(), key=lambda x: genericity[x])
105        return ordered, genericity
106
107    def _match_arglist(self, formal_args, actual_args):
108        """
109        Returns True if the signature is "matching".
110        A formal signature is "matching" if the actual signature matches exactly
111        or if the formal signature is a compatible generic signature.
112        """
113        # normalize VarArg
114        if formal_args and isinstance(formal_args[-1], types.VarArg):
115            ndiff = len(actual_args) - len(formal_args) + 1
116            formal_args = formal_args[:-1] + (formal_args[-1].dtype,) * ndiff
117
118        if len(formal_args) != len(actual_args):
119            return False
120
121        for formal, actual in zip(formal_args, actual_args):
122            if not self._match(formal, actual):
123                return False
124
125        return True
126
127    def _match(self, formal, actual):
128        if formal == actual:
129            # formal argument matches actual arguments
130            return True
131        elif types.Any == formal:
132            # formal argument is any
133            return True
134        elif isinstance(formal, type) and issubclass(formal, types.Type):
135            if isinstance(actual, type) and issubclass(actual, formal):
136                # formal arg is a type class and actual arg is a subclass
137                return True
138            elif isinstance(actual, formal):
139                # formal arg is a type class of which actual arg is an instance
140                return True
141
142    def append(self, value, sig):
143        """
144        Add a formal signature and its associated value.
145        """
146        assert isinstance(sig, tuple), (value, sig)
147        self.versions.append((sig, value))
148        self._cache.clear()
149
150
151@utils.runonce
152def _load_global_helpers():
153    """
154    Execute once to install special symbols into the LLVM symbol table.
155    """
156    # This is Py_None's real C name
157    ll.add_symbol("_Py_NoneStruct", id(None))
158
159    # Add Numba C helper functions
160    for c_helpers in (_helperlib.c_helpers, _dynfunc.c_helpers):
161        for py_name, c_address in c_helpers.items():
162            c_name = "numba_" + py_name
163            ll.add_symbol(c_name, c_address)
164
165    # Add Numpy C helpers (npy_XXX)
166    for c_name, c_address in _helperlib.npymath_exports.items():
167        ll.add_symbol(c_name, c_address)
168
169    # Add all built-in exception classes
170    for obj in utils.builtins.__dict__.values():
171        if isinstance(obj, type) and issubclass(obj, BaseException):
172            ll.add_symbol("PyExc_%s" % (obj.__name__), id(obj))
173
174
175class BaseContext(object):
176    """
177
178    Notes on Structure
179    ------------------
180
181    Most objects are lowered as plain-old-data structure in the generated
182    llvm.  They are passed around by reference (a pointer to the structure).
183    Only POD structure can live across function boundaries by copying the
184    data.
185    """
186    # True if the target requires strict alignment
187    # Causes exception to be raised if the record members are not aligned.
188    strict_alignment = False
189
190    # Force powi implementation as math.pow call
191    implement_powi_as_math_call = False
192    implement_pow_as_math_call = False
193
194    # Emit Debug info
195    enable_debuginfo = False
196    DIBuilder = debuginfo.DIBuilder
197
198    # Bound checking
199    @property
200    def enable_boundscheck(self):
201        if config.BOUNDSCHECK is not None:
202            return config.BOUNDSCHECK
203        return self._boundscheck
204
205    @enable_boundscheck.setter
206    def enable_boundscheck(self, value):
207        self._boundscheck = value
208
209    # NRT
210    enable_nrt = False
211
212    # Auto parallelization
213    auto_parallel = False
214
215    # PYCC
216    aot_mode = False
217
218    # Error model for various operations (only FP exceptions currently)
219    error_model = None
220
221    # Whether dynamic globals (CPU runtime addresses) is allowed
222    allow_dynamic_globals = False
223
224    # Fast math flags
225    fastmath = False
226
227    # python execution environment
228    environment = None
229
230    # the function descriptor
231    fndesc = None
232
233    def __init__(self, typing_context):
234        _load_global_helpers()
235
236        self.address_size = utils.MACHINE_BITS
237        self.typing_context = typing_context
238
239        # A mapping of installed registries to their loaders
240        self._registries = {}
241        # Declarations loaded from registries and other sources
242        self._defns = defaultdict(OverloadSelector)
243        self._getattrs = defaultdict(OverloadSelector)
244        self._setattrs = defaultdict(OverloadSelector)
245        self._casts = OverloadSelector()
246        self._get_constants = OverloadSelector()
247        # Other declarations
248        self._generators = {}
249        self.special_ops = {}
250        self.cached_internal_func = {}
251        self._pid = None
252        self._codelib_stack = []
253
254        self._boundscheck = False
255
256        self.data_model_manager = datamodel.default_manager
257
258        # Initialize
259        self.init()
260
261    def init(self):
262        """
263        For subclasses to add initializer
264        """
265
266    def refresh(self):
267        """
268        Refresh context with new declarations from known registries.
269        Useful for third-party extensions.
270        """
271        # Populate built-in registry
272        from numba.cpython import (slicing, tupleobj, enumimpl, hashing, heapq,
273                                   iterators, numbers, rangeobj)
274        from numba.core import optional
275        from numba.misc import gdb_hook, literal
276        from numba.np import linalg, polynomial, arraymath
277
278        try:
279            from numba.np import npdatetime
280        except NotImplementedError:
281            pass
282        self.install_registry(builtin_registry)
283        self.load_additional_registries()
284        # Also refresh typing context, since @overload declarations can
285        # affect it.
286        self.typing_context.refresh()
287
288    def load_additional_registries(self):
289        """
290        Load target-specific registries.  Can be overridden by subclasses.
291        """
292
293    def mangler(self, name, types):
294        """
295        Perform name mangling.
296        """
297        return funcdesc.default_mangler(name, types)
298
299    def get_env_name(self, fndesc):
300        """Get the environment name given a FunctionDescriptor.
301
302        Use this instead of the ``fndesc.env_name`` so that the target-context
303        can provide necessary mangling of the symbol to meet ABI requirements.
304        """
305        return fndesc.env_name
306
307    def declare_env_global(self, module, envname):
308        """Declare the Environment pointer as a global of the module.
309
310        The pointer is initialized to NULL.  It must be filled by the runtime
311        with the actual address of the Env before the associated function
312        can be executed.
313
314        Parameters
315        ----------
316        module :
317            The LLVM Module
318        envname : str
319            The name of the global variable.
320        """
321        if envname not in module.globals:
322            gv = llvmir.GlobalVariable(module, cgutils.voidptr_t, name=envname)
323            gv.linkage = 'common'
324            gv.initializer = cgutils.get_null_value(gv.type.pointee)
325
326        return module.globals[envname]
327
328    def get_arg_packer(self, fe_args):
329        return datamodel.ArgPacker(self.data_model_manager, fe_args)
330
331    def get_data_packer(self, fe_types):
332        return datamodel.DataPacker(self.data_model_manager, fe_types)
333
334    @property
335    def target_data(self):
336        raise NotImplementedError
337
338    @utils.cached_property
339    def nonconst_module_attrs(self):
340        """
341        All module attrs are constant for targets using BaseContext.
342        """
343        return tuple()
344
345    @utils.cached_property
346    def nrt(self):
347        from numba.core.runtime.context import NRTContext
348        return NRTContext(self, self.enable_nrt)
349
350    def subtarget(self, **kws):
351        obj = copy.copy(self)  # shallow copy
352        for k, v in kws.items():
353            if not hasattr(obj, k):
354                raise NameError("unknown option {0!r}".format(k))
355            setattr(obj, k, v)
356        if obj.codegen() is not self.codegen():
357            # We can't share functions across different codegens
358            obj.cached_internal_func = {}
359        return obj
360
361    def install_registry(self, registry):
362        """
363        Install a *registry* (a imputils.Registry instance) of function
364        and attribute implementations.
365        """
366        try:
367            loader = self._registries[registry]
368        except KeyError:
369            loader = RegistryLoader(registry)
370            self._registries[registry] = loader
371        self.insert_func_defn(loader.new_registrations('functions'))
372        self._insert_getattr_defn(loader.new_registrations('getattrs'))
373        self._insert_setattr_defn(loader.new_registrations('setattrs'))
374        self._insert_cast_defn(loader.new_registrations('casts'))
375        self._insert_get_constant_defn(loader.new_registrations('constants'))
376
377    def insert_func_defn(self, defns):
378        for impl, func, sig in defns:
379            self._defns[func].append(impl, sig)
380
381    def _insert_getattr_defn(self, defns):
382        for impl, attr, sig in defns:
383            self._getattrs[attr].append(impl, sig)
384
385    def _insert_setattr_defn(self, defns):
386        for impl, attr, sig in defns:
387            self._setattrs[attr].append(impl, sig)
388
389    def _insert_cast_defn(self, defns):
390        for impl, sig in defns:
391            self._casts.append(impl, sig)
392
393    def _insert_get_constant_defn(self, defns):
394        for impl, sig in defns:
395            self._get_constants.append(impl, sig)
396
397    def insert_user_function(self, func, fndesc, libs=()):
398        impl = user_function(fndesc, libs)
399        self._defns[func].append(impl, impl.signature)
400
401    def add_user_function(self, func, fndesc, libs=()):
402        if func not in self._defns:
403            msg = "{func} is not a registered user function"
404            raise KeyError(msg.format(func=func))
405        impl = user_function(fndesc, libs)
406        self._defns[func].append(impl, impl.signature)
407
408    def insert_generator(self, genty, gendesc, libs=()):
409        assert isinstance(genty, types.Generator)
410        impl = user_generator(gendesc, libs)
411        self._generators[genty] = gendesc, impl
412
413    def remove_user_function(self, func):
414        """
415        Remove user function *func*.
416        KeyError is raised if the function isn't known to us.
417        """
418        del self._defns[func]
419
420    def get_external_function_type(self, fndesc):
421        argtypes = [self.get_argument_type(aty)
422                    for aty in fndesc.argtypes]
423        # don't wrap in pointer
424        restype = self.get_argument_type(fndesc.restype)
425        fnty = Type.function(restype, argtypes)
426        return fnty
427
428    def declare_function(self, module, fndesc):
429        fnty = self.call_conv.get_function_type(fndesc.restype, fndesc.argtypes)
430        fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
431        self.call_conv.decorate_function(fn, fndesc.args, fndesc.argtypes, noalias=fndesc.noalias)
432        if fndesc.inline:
433            fn.attributes.add('alwaysinline')
434        return fn
435
436    def declare_external_function(self, module, fndesc):
437        fnty = self.get_external_function_type(fndesc)
438        fn = module.get_or_insert_function(fnty, name=fndesc.mangled_name)
439        assert fn.is_declaration
440        for ak, av in zip(fndesc.args, fn.args):
441            av.name = "arg.%s" % ak
442        return fn
443
444    def insert_const_string(self, mod, string):
445        """
446        Insert constant *string* (a str object) into module *mod*.
447        """
448        stringtype = GENERIC_POINTER
449        name = ".const.%s" % string
450        text = cgutils.make_bytearray(string.encode("utf-8") + b"\x00")
451        gv = self.insert_unique_const(mod, name, text)
452        return Constant.bitcast(gv, stringtype)
453
454    def insert_const_bytes(self, mod, bytes, name=None):
455        """
456        Insert constant *byte* (a `bytes` object) into module *mod*.
457        """
458        stringtype = GENERIC_POINTER
459        name = ".bytes.%s" % (name or hash(bytes))
460        text = cgutils.make_bytearray(bytes)
461        gv = self.insert_unique_const(mod, name, text)
462        return Constant.bitcast(gv, stringtype)
463
464    def insert_unique_const(self, mod, name, val):
465        """
466        Insert a unique internal constant named *name*, with LLVM value
467        *val*, into module *mod*.
468        """
469        try:
470            gv = mod.get_global(name)
471        except KeyError:
472            return cgutils.global_constant(mod, name, val)
473        else:
474            return gv
475
476    def get_argument_type(self, ty):
477        return self.data_model_manager[ty].get_argument_type()
478
479    def get_return_type(self, ty):
480        return self.data_model_manager[ty].get_return_type()
481
482    def get_data_type(self, ty):
483        """
484        Get a LLVM data representation of the Numba type *ty* that is safe
485        for storage.  Record data are stored as byte array.
486
487        The return value is a llvmlite.ir.Type object, or None if the type
488        is an opaque pointer (???).
489        """
490        return self.data_model_manager[ty].get_data_type()
491
492    def get_value_type(self, ty):
493        return self.data_model_manager[ty].get_value_type()
494
495    def pack_value(self, builder, ty, value, ptr, align=None):
496        """
497        Pack value into the array storage at *ptr*.
498        If *align* is given, it is the guaranteed alignment for *ptr*
499        (by default, the standard ABI alignment).
500        """
501        dataval = self.data_model_manager[ty].as_data(builder, value)
502        builder.store(dataval, ptr, align=align)
503
504    def unpack_value(self, builder, ty, ptr, align=None):
505        """
506        Unpack value from the array storage at *ptr*.
507        If *align* is given, it is the guaranteed alignment for *ptr*
508        (by default, the standard ABI alignment).
509        """
510        dm = self.data_model_manager[ty]
511        return dm.load_from_data_pointer(builder, ptr, align)
512
513    def get_constant_generic(self, builder, ty, val):
514        """
515        Return a LLVM constant representing value *val* of Numba type *ty*.
516        """
517        try:
518            impl = self._get_constants.find((ty,))
519            return impl(self, builder, ty, val)
520        except NotImplementedError:
521            raise NotImplementedError("Cannot lower constant of type '%s'" % (ty,))
522
523    def get_constant(self, ty, val):
524        """
525        Same as get_constant_generic(), but without specifying *builder*.
526        Works only for simple types.
527        """
528        # HACK: pass builder=None to preserve get_constant() API
529        return self.get_constant_generic(None, ty, val)
530
531    def get_constant_undef(self, ty):
532        lty = self.get_value_type(ty)
533        return Constant.undef(lty)
534
535    def get_constant_null(self, ty):
536        lty = self.get_value_type(ty)
537        return Constant.null(lty)
538
539    def get_function(self, fn, sig, _firstcall=True):
540        """
541        Return the implementation of function *fn* for signature *sig*.
542        The return value is a callable with the signature (builder, args).
543        """
544        assert sig is not None
545        sig = sig.as_function()
546        if isinstance(fn, (types.Function, types.BoundFunction,
547                           types.Dispatcher)):
548            key = fn.get_impl_key(sig)
549            overloads = self._defns[key]
550        else:
551            key = fn
552            overloads = self._defns[key]
553
554        try:
555            return _wrap_impl(overloads.find(sig.args), self, sig)
556        except NotImplementedError:
557            pass
558        if isinstance(fn, types.Type):
559            # It's a type instance => try to find a definition for the type class
560            try:
561                return self.get_function(type(fn), sig)
562            except NotImplementedError:
563                # Raise exception for the type instance, for a better error message
564                pass
565
566        # Automatically refresh the context to load new registries if we are
567        # calling the first time.
568        if _firstcall:
569            self.refresh()
570            return self.get_function(fn, sig, _firstcall=False)
571
572        raise NotImplementedError("No definition for lowering %s%s" % (key, sig))
573
574    def get_generator_desc(self, genty):
575        """
576        """
577        return self._generators[genty][0]
578
579    def get_generator_impl(self, genty):
580        """
581        """
582        res = self._generators[genty][1]
583        self.add_linking_libs(getattr(res, 'libs', ()))
584        return res
585
586    def get_bound_function(self, builder, obj, ty):
587        assert self.get_value_type(ty) == obj.type
588        return obj
589
590    def get_getattr(self, typ, attr):
591        """
592        Get the getattr() implementation for the given type and attribute name.
593        The return value is a callable with the signature
594        (context, builder, typ, val, attr).
595        """
596        const_attr = (typ, attr) not in self.nonconst_module_attrs
597        is_module = isinstance(typ, types.Module)
598        if is_module and const_attr:
599            # Implement getattr for module-level globals that we treat as
600            # constants.
601            # XXX We shouldn't have to retype this
602            attrty = self.typing_context.resolve_module_constants(typ, attr)
603            if attrty is None or isinstance(attrty, types.Dummy):
604                # No implementation required for dummies (functions, modules...),
605                # which are dealt with later
606                return None
607            else:
608                pyval = getattr(typ.pymod, attr)
609                llval = self.get_constant(attrty, pyval)
610                def imp(context, builder, typ, val, attr):
611                    return impl_ret_borrowed(context, builder, attrty, llval)
612                return imp
613
614        # Lookup specific getattr implementation for this type and attribute
615        overloads = self._getattrs[attr]
616        try:
617            return overloads.find((typ,))
618        except NotImplementedError:
619            pass
620        # Lookup generic getattr implementation for this type
621        overloads = self._getattrs[None]
622        try:
623            return overloads.find((typ,))
624        except NotImplementedError:
625            pass
626
627        raise NotImplementedError("No definition for lowering %s.%s" % (typ, attr))
628
629    def get_setattr(self, attr, sig):
630        """
631        Get the setattr() implementation for the given attribute name
632        and signature.
633        The return value is a callable with the signature (builder, args).
634        """
635        assert len(sig.args) == 2
636        typ = sig.args[0]
637        valty = sig.args[1]
638
639        def wrap_setattr(impl):
640            def wrapped(builder, args):
641                return impl(self, builder, sig, args, attr)
642            return wrapped
643
644        # Lookup specific setattr implementation for this type and attribute
645        overloads = self._setattrs[attr]
646        try:
647            return wrap_setattr(overloads.find((typ, valty)))
648        except NotImplementedError:
649            pass
650        # Lookup generic setattr implementation for this type
651        overloads = self._setattrs[None]
652        try:
653            return wrap_setattr(overloads.find((typ, valty)))
654        except NotImplementedError:
655            pass
656
657        raise NotImplementedError("No definition for lowering %s.%s = %s"
658                                  % (typ, attr, valty))
659
660    def get_argument_value(self, builder, ty, val):
661        """
662        Argument representation to local value representation
663        """
664        return self.data_model_manager[ty].from_argument(builder, val)
665
666    def get_returned_value(self, builder, ty, val):
667        """
668        Return value representation to local value representation
669        """
670        return self.data_model_manager[ty].from_return(builder, val)
671
672    def get_return_value(self, builder, ty, val):
673        """
674        Local value representation to return type representation
675        """
676        return self.data_model_manager[ty].as_return(builder, val)
677
678    def get_value_as_argument(self, builder, ty, val):
679        """Prepare local value representation as argument type representation
680        """
681        return self.data_model_manager[ty].as_argument(builder, val)
682
683    def get_value_as_data(self, builder, ty, val):
684        return self.data_model_manager[ty].as_data(builder, val)
685
686    def get_data_as_value(self, builder, ty, val):
687        return self.data_model_manager[ty].from_data(builder, val)
688
689    def pair_first(self, builder, val, ty):
690        """
691        Extract the first element of a heterogeneous pair.
692        """
693        pair = self.make_helper(builder, ty, val)
694        return pair.first
695
696    def pair_second(self, builder, val, ty):
697        """
698        Extract the second element of a heterogeneous pair.
699        """
700        pair = self.make_helper(builder, ty, val)
701        return pair.second
702
703    def cast(self, builder, val, fromty, toty):
704        """
705        Cast a value of type *fromty* to type *toty*.
706        This implements implicit conversions as can happen due to the
707        granularity of the Numba type system, or lax Python semantics.
708        """
709        if fromty == toty or toty == types.Any:
710            return val
711        try:
712            impl = self._casts.find((fromty, toty))
713            return impl(self, builder, fromty, toty, val)
714        except NotImplementedError:
715            raise NotImplementedError(
716                "Cannot cast %s to %s: %s" % (fromty, toty, val))
717
718    def generic_compare(self, builder, key, argtypes, args):
719        """
720        Compare the given LLVM values of the given Numba types using
721        the comparison *key* (e.g. '==').  The values are first cast to
722        a common safe conversion type.
723        """
724        at, bt = argtypes
725        av, bv = args
726        ty = self.typing_context.unify_types(at, bt)
727        assert ty is not None
728        cav = self.cast(builder, av, at, ty)
729        cbv = self.cast(builder, bv, bt, ty)
730        fnty = self.typing_context.resolve_value_type(key)
731        # the sig is homogeneous in the unified casted type
732        cmpsig = fnty.get_call_type(self.typing_context, (ty, ty), {})
733        cmpfunc = self.get_function(fnty, cmpsig)
734        self.add_linking_libs(getattr(cmpfunc, 'libs', ()))
735        return cmpfunc(builder, (cav, cbv))
736
737    def make_optional_none(self, builder, valtype):
738        optval = self.make_helper(builder, types.Optional(valtype))
739        optval.valid = cgutils.false_bit
740        return optval._getvalue()
741
742    def make_optional_value(self, builder, valtype, value):
743        optval = self.make_helper(builder, types.Optional(valtype))
744        optval.valid = cgutils.true_bit
745        optval.data = value
746        return optval._getvalue()
747
748    def is_true(self, builder, typ, val):
749        """
750        Return the truth value of a value of the given Numba type.
751        """
752        fnty = self.typing_context.resolve_value_type(bool)
753        sig = fnty.get_call_type(self.typing_context, (typ,), {})
754        impl = self.get_function(fnty, sig)
755        return impl(builder, (val,))
756
757    def get_c_value(self, builder, typ, name, dllimport=False):
758        """
759        Get a global value through its C-accessible *name*, with the given
760        LLVM type.
761        If *dllimport* is true, the symbol will be marked as imported
762        from a DLL (necessary for AOT compilation under Windows).
763        """
764        module = builder.function.module
765        try:
766            gv = module.get_global_variable_named(name)
767        except LLVMException:
768            gv = module.add_global_variable(typ, name)
769            if dllimport and self.aot_mode and sys.platform == 'win32':
770                gv.storage_class = "dllimport"
771        return gv
772
773    def call_external_function(self, builder, callee, argtys, args):
774        args = [self.get_value_as_argument(builder, ty, arg)
775                for ty, arg in zip(argtys, args)]
776        retval = builder.call(callee, args)
777        return retval
778
779    def get_function_pointer_type(self, typ):
780        return self.data_model_manager[typ].get_data_type()
781
782    def call_function_pointer(self, builder, funcptr, args, cconv=None):
783        return builder.call(funcptr, args, cconv=cconv)
784
785    def print_string(self, builder, text):
786        mod = builder.module
787        cstring = GENERIC_POINTER
788        fnty = Type.function(Type.int(), [cstring])
789        puts = mod.get_or_insert_function(fnty, "puts")
790        return builder.call(puts, [text])
791
792    def debug_print(self, builder, text):
793        mod = builder.module
794        cstr = self.insert_const_string(mod, str(text))
795        self.print_string(builder, cstr)
796
797    def printf(self, builder, format_string, *args):
798        mod = builder.module
799        if isinstance(format_string, str):
800            cstr = self.insert_const_string(mod, format_string)
801        else:
802            cstr = format_string
803        fnty = Type.function(Type.int(), (GENERIC_POINTER,), var_arg=True)
804        fn = mod.get_or_insert_function(fnty, "printf")
805        return builder.call(fn, (cstr,) + tuple(args))
806
807    def get_struct_type(self, struct):
808        """
809        Get the LLVM struct type for the given Structure class *struct*.
810        """
811        fields = [self.get_value_type(v) for _, v in struct._fields]
812        return Type.struct(fields)
813
814    def get_dummy_value(self):
815        return Constant.null(self.get_dummy_type())
816
817    def get_dummy_type(self):
818        return GENERIC_POINTER
819
820    def _compile_subroutine_no_cache(self, builder, impl, sig, locals={},
821                                     flags=None):
822        """
823        Invoke the compiler to compile a function to be used inside a
824        nopython function, but without generating code to call that
825        function.
826
827        Note this context's flags are not inherited.
828        """
829        # Compile
830        from numba.core import compiler
831
832        with global_compiler_lock:
833            codegen = self.codegen()
834            library = codegen.create_library(impl.__name__)
835            if flags is None:
836                flags = compiler.Flags()
837            flags.set('no_compile')
838            flags.set('no_cpython_wrapper')
839            flags.set('no_cfunc_wrapper')
840            cres = compiler.compile_internal(self.typing_context, self,
841                                             library,
842                                             impl, sig.args,
843                                             sig.return_type, flags,
844                                             locals=locals)
845
846            # Allow inlining the function inside callers.
847            self.active_code_library.add_linking_library(cres.library)
848            return cres
849
850    def compile_subroutine(self, builder, impl, sig, locals={}, flags=None,
851                           caching=True):
852        """
853        Compile the function *impl* for the given *sig* (in nopython mode).
854        Return an instance of CompileResult.
855
856        If *caching* evaluates True, the function keeps the compiled function
857        for reuse in *.cached_internal_func*.
858        """
859        cache_key = (impl.__code__, sig, type(self.error_model))
860        if not caching:
861            cached = None
862        else:
863            if impl.__closure__:
864                # XXX This obviously won't work if a cell's value is
865                # unhashable.
866                cache_key += tuple(c.cell_contents for c in impl.__closure__)
867            cached = self.cached_internal_func.get(cache_key)
868        if cached is None:
869            cres = self._compile_subroutine_no_cache(builder, impl, sig,
870                                                     locals=locals,
871                                                     flags=flags)
872            self.cached_internal_func[cache_key] = cres
873
874        cres = self.cached_internal_func[cache_key]
875        # Allow inlining the function inside callers.
876        self.active_code_library.add_linking_library(cres.library)
877        return cres
878
879    def compile_internal(self, builder, impl, sig, args, locals={}):
880        """
881        Like compile_subroutine(), but also call the function with the given
882        *args*.
883        """
884        cres = self.compile_subroutine(builder, impl, sig, locals)
885        return self.call_internal(builder, cres.fndesc, sig, args)
886
887    def call_internal(self, builder, fndesc, sig, args):
888        """
889        Given the function descriptor of an internally compiled function,
890        emit a call to that function with the given arguments.
891        """
892        status, res = self.call_internal_no_propagate(builder, fndesc, sig, args)
893        with cgutils.if_unlikely(builder, status.is_error):
894            self.call_conv.return_status_propagate(builder, status)
895
896        res = imputils.fix_returning_optional(self, builder, sig, status, res)
897        return res
898
899    def call_internal_no_propagate(self, builder, fndesc, sig, args):
900        """Similar to `.call_internal()` but does not handle or propagate
901        the return status automatically.
902        """
903        # Add call to the generated function
904        llvm_mod = builder.module
905        fn = self.declare_function(llvm_mod, fndesc)
906        status, res = self.call_conv.call_function(builder, fn, sig.return_type,
907                                                   sig.args, args)
908        return status, res
909
910    def call_unresolved(self, builder, name, sig, args):
911        """
912        Insert a function call to an unresolved symbol with the given *name*.
913
914        Note: this is used for recursive call.
915
916        In the mutual recursion case::
917
918            @njit
919            def foo():
920                ...  # calls bar()
921
922            @njit
923            def bar():
924                ... # calls foo()
925
926            foo()
927
928        When foo() is called, the compilation of bar() is fully completed
929        (codegen'ed and loaded) before foo() is. Since MCJIT's eager compilation
930        doesn't allow loading modules with declare-only functions (which is
931        needed for foo() in bar()), the call_unresolved injects a global
932        variable that the "linker" can update even after the module is loaded by
933        MCJIT. The linker would allocate space for the global variable before
934        the bar() module is loaded. When later foo() module is defined, it will
935        update bar()'s reference to foo().
936
937        The legacy lazy JIT and the new ORC JIT would allow a declare-only
938        function be used in a module as long as it is defined by the time of its
939        first use.
940        """
941        # Insert an unresolved reference to the function being called.
942        codegen = self.codegen()
943        fnty = self.call_conv.get_function_type(sig.return_type, sig.args)
944        fn = codegen.insert_unresolved_ref(builder, fnty, name)
945        # Normal call sequence
946        status, res = self.call_conv.call_function(builder, fn, sig.return_type,
947                                                   sig.args, args)
948        with cgutils.if_unlikely(builder, status.is_error):
949            self.call_conv.return_status_propagate(builder, status)
950
951        res = imputils.fix_returning_optional(self, builder, sig, status, res)
952        return res
953
954    def get_executable(self, func, fndesc):
955        raise NotImplementedError
956
957    def get_python_api(self, builder):
958        return PythonAPI(self, builder)
959
960    def sentry_record_alignment(self, rectyp, attr):
961        """
962        Assumes offset starts from a properly aligned location
963        """
964        if self.strict_alignment:
965            offset = rectyp.offset(attr)
966            elemty = rectyp.typeof(attr)
967            align = self.get_abi_alignment(self.get_data_type(elemty))
968            if offset % align:
969                msg = "{rec}.{attr} of type {type} is not aligned".format(
970                    rec=rectyp, attr=attr, type=elemty)
971                raise TypeError(msg)
972
973    def get_helper_class(self, typ, kind='value'):
974        """
975        Get a helper class for the given *typ*.
976        """
977        # XXX handle all types: complex, array, etc.
978        # XXX should it be a method on the model instead? this would allow a default kind...
979        return cgutils.create_struct_proxy(typ, kind)
980
981    def _make_helper(self, builder, typ, value=None, ref=None, kind='value'):
982        cls = self.get_helper_class(typ, kind)
983        return cls(self, builder, value=value, ref=ref)
984
985    def make_helper(self, builder, typ, value=None, ref=None):
986        """
987        Get a helper object to access the *typ*'s members,
988        for the given value or reference.
989        """
990        return self._make_helper(builder, typ, value, ref, kind='value')
991
992    def make_data_helper(self, builder, typ, ref=None):
993        """
994        As make_helper(), but considers the value as stored in memory,
995        rather than a live value.
996        """
997        return self._make_helper(builder, typ, ref=ref, kind='data')
998
999    def make_array(self, typ):
1000        return arrayobj.make_array(typ)
1001
1002    def populate_array(self, arr, **kwargs):
1003        """
1004        Populate array structure.
1005        """
1006        return arrayobj.populate_array(arr, **kwargs)
1007
1008    def make_complex(self, builder, typ, value=None):
1009        """
1010        Get a helper object to access the given complex numbers' members.
1011        """
1012        assert isinstance(typ, types.Complex), typ
1013        return self.make_helper(builder, typ, value)
1014
1015    def make_tuple(self, builder, typ, values):
1016        """
1017        Create a tuple of the given *typ* containing the *values*.
1018        """
1019        tup = self.get_constant_undef(typ)
1020        for i, val in enumerate(values):
1021            tup = builder.insert_value(tup, val, i)
1022        return tup
1023
1024    def make_constant_array(self, builder, typ, ary):
1025        """
1026        Create an array structure reifying the given constant array.
1027        A low-level contiguous array constant is created in the LLVM IR.
1028        """
1029        datatype = self.get_data_type(typ.dtype)
1030        # don't freeze ary of non-contig or bigger than 1MB
1031        size_limit = 10**6
1032
1033        if (self.allow_dynamic_globals and
1034                (typ.layout not in 'FC' or ary.nbytes > size_limit)):
1035            # get pointer from the ary
1036            dataptr = ary.ctypes.data
1037            data = self.add_dynamic_addr(builder, dataptr, info=str(type(dataptr)))
1038            rt_addr = self.add_dynamic_addr(builder, id(ary), info=str(type(ary)))
1039        else:
1040            # Handle data: reify the flattened array in "C" or "F" order as a
1041            # global array of bytes.
1042            flat = ary.flatten(order=typ.layout)
1043            # Note: we use `bytearray(flat.data)` instead of `bytearray(flat)` to
1044            #       workaround issue #1850 which is due to numpy issue #3147
1045            consts = Constant.array(Type.int(8), bytearray(flat.data))
1046            data = cgutils.global_constant(builder, ".const.array.data", consts)
1047            # Ensure correct data alignment (issue #1933)
1048            data.align = self.get_abi_alignment(datatype)
1049            # No reference to parent ndarray
1050            rt_addr = None
1051
1052        # Handle shape
1053        llintp = self.get_value_type(types.intp)
1054        shapevals = [self.get_constant(types.intp, s) for s in ary.shape]
1055        cshape = Constant.array(llintp, shapevals)
1056
1057        # Handle strides
1058        stridevals = [self.get_constant(types.intp, s) for s in ary.strides]
1059        cstrides = Constant.array(llintp, stridevals)
1060
1061        # Create array structure
1062        cary = self.make_array(typ)(self, builder)
1063
1064        intp_itemsize = self.get_constant(types.intp, ary.dtype.itemsize)
1065        self.populate_array(cary,
1066                            data=builder.bitcast(data, cary.data.type),
1067                            shape=cshape,
1068                            strides=cstrides,
1069                            itemsize=intp_itemsize,
1070                            parent=rt_addr,
1071                            meminfo=None)
1072
1073        return cary._getvalue()
1074
1075    def add_dynamic_addr(self, builder, intaddr, info):
1076        """
1077        Returns dynamic address as a void pointer `i8*`.
1078
1079        Internally, a global variable is added to inform the lowerer about
1080        the usage of dynamic addresses.  Caching will be disabled.
1081        """
1082        assert self.allow_dynamic_globals, "dyn globals disabled in this target"
1083        assert isinstance(intaddr, utils.INT_TYPES), 'dyn addr not of int type'
1084        mod = builder.module
1085        llvoidptr = self.get_value_type(types.voidptr)
1086        addr = self.get_constant(types.uintp, intaddr).inttoptr(llvoidptr)
1087        # Use a unique name by embedding the address value
1088        symname = 'numba.dynamic.globals.{:x}'.format(intaddr)
1089        gv = mod.add_global_variable(llvoidptr, name=symname)
1090        # Use linkonce linkage to allow merging with other GV of the same name.
1091        # And, avoid optimization from assuming its value.
1092        gv.linkage = 'linkonce'
1093        gv.initializer = addr
1094        return builder.load(gv)
1095
1096    def get_abi_sizeof(self, ty):
1097        """
1098        Get the ABI size of LLVM type *ty*.
1099        """
1100        assert isinstance(ty, llvmir.Type), "Expected LLVM type"
1101        return ty.get_abi_size(self.target_data)
1102
1103    def get_abi_alignment(self, ty):
1104        """
1105        Get the ABI alignment of LLVM type *ty*.
1106        """
1107        assert isinstance(ty, llvmir.Type), "Expected LLVM type"
1108        return ty.get_abi_alignment(self.target_data)
1109
1110    def get_preferred_array_alignment(context, ty):
1111        """
1112        Get preferred array alignment for Numba type *ty*.
1113        """
1114        # AVX prefers 32-byte alignment
1115        return 32
1116
1117    def post_lowering(self, mod, library):
1118        """Run target specific post-lowering transformation here.
1119        """
1120
1121    def create_module(self, name):
1122        """Create a LLVM module
1123        """
1124        return lc.Module(name)
1125
1126    @property
1127    def active_code_library(self):
1128        """Get the active code library
1129        """
1130        return self._codelib_stack[-1]
1131
1132    @contextmanager
1133    def push_code_library(self, lib):
1134        """Push the active code library for the context
1135        """
1136        self._codelib_stack.append(lib)
1137        try:
1138            yield
1139        finally:
1140            self._codelib_stack.pop()
1141
1142    def add_linking_libs(self, libs):
1143        """Add iterable of linking libraries to the *active_code_library*.
1144        """
1145        colib = self.active_code_library
1146        for lib in libs:
1147            colib.add_linking_library(lib)
1148
1149
1150class _wrap_impl(object):
1151    """
1152    A wrapper object to call an implementation function with some predefined
1153    (context, signature) arguments.
1154    The wrapper also forwards attribute queries, which is important.
1155    """
1156
1157    def __init__(self, imp, context, sig):
1158        self._callable = _wrap_missing_loc(imp)
1159        self._imp = self._callable()
1160        self._context = context
1161        self._sig = sig
1162
1163    def __call__(self, builder, args, loc=None):
1164        res = self._imp(self._context, builder, self._sig, args, loc=loc)
1165        self._context.add_linking_libs(getattr(self, 'libs', ()))
1166        return res
1167
1168    def __getattr__(self, item):
1169        return getattr(self._imp, item)
1170
1171    def __repr__(self):
1172        return "<wrapped %s>" % repr(self._callable)
1173
1174def _has_loc(fn):
1175    """Does function *fn* take ``loc`` argument?
1176    """
1177    sig = utils.pysignature(fn)
1178    return 'loc' in sig.parameters
1179
1180
1181class _wrap_missing_loc(object):
1182
1183    def __init__(self, fn):
1184        self.func = fn # store this to help with debug
1185
1186    def __call__(self):
1187        """Wrap function for missing ``loc`` keyword argument.
1188        Otherwise, return the original *fn*.
1189        """
1190        fn = self.func
1191        if not _has_loc(fn):
1192            def wrapper(*args, **kwargs):
1193                kwargs.pop('loc')     # drop unused loc
1194                return fn(*args, **kwargs)
1195
1196            # Copy the following attributes from the wrapped.
1197            # Following similar implementation as functools.wraps but
1198            # ignore attributes if not available (i.e fix py2.7)
1199            attrs = '__name__', 'libs'
1200            for attr in attrs:
1201                try:
1202                    val = getattr(fn, attr)
1203                except AttributeError:
1204                    pass
1205                else:
1206                    setattr(wrapper, attr, val)
1207
1208            return wrapper
1209        else:
1210            return fn
1211
1212    def __repr__(self):
1213        return "<wrapped %s>" % self.func
1214