ll2mu.py 81.3 KB
Newer Older
1
from rpython.annotator.model import SomeString, SomeBool
2
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
3
from rpython.translator.mu import mutype, layout
4
from rpython.rtyper.normalizecalls import TotalOrderSymbolic
5
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator
6
from rpython.rtyper.llannotation import lltype_to_annotation as l2a
7
from rpython.rlib.objectmodel import CDefinedIntSymbolic
8
from rpython.rlib import rarithmetic
9
from rpython.flowspace.model import Variable, Constant, SpaceOperation, Link
10
from rpython.translator.c.node import needs_gcheader
11
from rpython.translator.platform import CompilationError
12
from random import randint
John Zhang's avatar
John Zhang committed
13
import weakref
14

15 16 17 18 19 20 21 22
try:
    from rpython.rlib.rmu import holstein as rmu
except Exception:
    try:
        from rpython.rlib.rmu import zebu as rmu
    except Exception:
        raise

23 24 25 26 27 28
from rpython.tool.ansi_print import AnsiLogger
from rpython.tool.ansi_mandelbrot import Driver

log = AnsiLogger("ll2mu")
mdb = Driver()

29 30

class LL2MuMapper:
31
    GC_IDHASH_FIELD = ('gc_idhash', mutype.MU_INT64)
32

33
    def __init__(self, rtyper=None):
John Zhang's avatar
John Zhang committed
34 35 36
        """
        :type mlha: rpython.rtyper.annlowlevel.MixLevelHelperAnnotator
        """
37
        self._type_cache = {}
38
        self._pending_ptr_types = []
39
        self._name_cache = {}
40 41 42 43
        self._val_cache = {}
        self._ptr_cache = {}
        self._topstt_map = {}
        self._pending_ptr_values = []
44 45 46 47
        if rtyper:
            self.mlha = MixLevelHelperAnnotator(rtyper)
        else:
            self.mlha = None
48 49

        if self.mlha:
50
            self._translate_llhelpers()
51

52
    def _translate_llhelpers(self):
53 54 55 56 57 58 59 60 61 62 63
        # code taken from rpython/memory/gctransform/boehm.py
        HDR = lltype.Struct("header", ("hash", lltype.Signed))
        HDRPTR = lltype.Ptr(HDR)

        def ll_identityhash(addr):
            obj = llmemory.cast_adr_to_ptr(addr, HDRPTR)
            h = obj.hash
            if h == 0:
                obj.hash = h = llmemory.cast_adr_to_int(addr)
            return h

64
        idhash_c = self.mlha.constfunc(ll_identityhash, [l2a(llmemory.Address)], l2a(lltype.Signed))
65
        try:
66 67
            g = filter(lambda g: getattr(g, 'func', None) is rffi.str2charp,
                       self.mlha.rtyper.annotator.translator.graphs)[0]
68 69
            fnp = lltype.functionptr(lltype.FuncType(
                [a.concretetype for a in g.startblock.inputargs], g.returnblock.inputargs[0].concretetype),
70
                g.name, graph=g)
71 72 73 74 75
            str2charp_c = Constant(fnp, lltype.typeOf(fnp))
        except IndexError:
            str2charp_c = self.mlha.constfunc(rffi.str2charp, [SomeString(), SomeBool()], l2a(rffi.CCHARP))

        try:
76 77
            g = filter(lambda g: getattr(g, 'func', None) is rffi.free_charp,
                       self.mlha.rtyper.annotator.translator.graphs)[0]
78 79 80 81 82 83
            fnp = lltype.functionptr(lltype.FuncType(
                [a.concretetype for a in g.startblock.inputargs], g.returnblock.inputargs[0].concretetype),
                g.name, graph=g)
            freecharp_c = Constant(fnp, lltype.typeOf(fnp))
        except IndexError:
            freecharp_c = self.mlha.constfunc(rffi.free_charp, [l2a(rffi.CCHARP), SomeBool()], l2a(lltype.Void))
84

85 86
        self.mlha.finish()
        self.mlha.backend_optimize()
87 88 89 90 91 92 93 94 95 96
        idhash_c.value = self.map_value(idhash_c.value)
        idhash_c.concretetype = mutype.mutypeOf(idhash_c.value)
        str2charp_c.value = self.map_value(str2charp_c.value)
        freecharp_c.value = self.map_value(freecharp_c.value)
        str2charp_c.concretetype = mutype.mutypeOf(str2charp_c.value)
        freecharp_c.concretetype = mutype.mutypeOf(freecharp_c.value)

        self.ll_identityhash_c = idhash_c
        self.ll_str2charp_c = str2charp_c
        self.ll_freecharp_c = freecharp_c
97

98 99
        self.llhelper_graphs = [c.value.graph for c in (self.ll_identityhash_c, self.ll_str2charp_c,
                                                        self.ll_freecharp_c)]
100 101 102 103 104 105 106 107 108 109

    def _new_typename(self, name):
        if name not in self._name_cache:
            n = 2
            self._name_cache[name] = n
            return name
        n = self._name_cache[name]
        self._name_cache[name] = n + 1
        return "%(name)s_%(n)d" % locals()

110
    def map_type(self, LLT):
111 112 113 114 115
        assert isinstance(LLT, lltype.LowLevelType)
        try:
            return self._type_cache[LLT]
        except KeyError:
            if LLT is llmemory.Address:
116
                MuT = self.map_type_addr(LLT)
117
            elif isinstance(LLT, lltype.Primitive):
118
                MuT = self.map_type_prim(LLT)
119
            elif isinstance(LLT, lltype.FixedSizeArray):
120
                MuT = self.map_type_arrfix(LLT)
121 122 123
            elif isinstance(LLT, lltype.Struct):
                MuT = self.map_type_stt(LLT)
            elif isinstance(LLT, lltype.Array):
124
                MuT = self.map_type_arr(LLT)
125
            elif isinstance(LLT, lltype.Ptr):
126
                MuT = self.map_type_ptr(LLT)
127 128
            elif isinstance(LLT, lltype.InteriorPtr):
                MuT = self.map_type_iptr(LLT)
129 130
            elif isinstance(LLT, lltype.OpaqueType):
                MuT = self.map_type_opq(LLT)
131
            elif LLT is llmemory.WeakRef:
132
                MuT = self.map_wref(LLT)
John Zhang's avatar
John Zhang committed
133 134
            elif isinstance(LLT, lltype.Typedef):
                return self.map_type(LLT.OF)
135 136 137 138 139
            else:
                raise NotImplementedError("Don't know how to specialise %s using MuTS." % LLT)
            self._type_cache[LLT] = MuT
            return MuT

140
    def map_type_prim(self, LLT):
141
        type_map = {
142 143 144 145 146 147 148 149 150 151 152 153 154 155
            lltype.Signed: mutype.MU_INT64,
            lltype.Unsigned: mutype.MU_INT64,
            lltype.SignedLongLong: mutype.MU_INT64,
            lltype.UnsignedLongLong: mutype.MU_INT64,
            lltype.SignedLongLongLong: mutype.MU_INT128,

            lltype.Float: mutype.MU_DOUBLE,
            lltype.SingleFloat: mutype.MU_FLOAT,
            lltype.LongFloat: mutype.MU_DOUBLE,

            lltype.Char: mutype.MU_INT8,
            lltype.Bool: mutype.MU_INT8,
            lltype.Void: mutype.MU_VOID,
            lltype.UniChar: mutype.MU_INT32,  # wchar_t is 32-bits on OS X and Linux 64-bit machines
156 157 158 159
        }
        try:
            return type_map[LLT]
        except KeyError:
160
            if isinstance(LLT, lltype.Number) and issubclass(LLT._type, rarithmetic.base_int):  # integer type
161
                b = LLT._type.BITS
162 163 164 165
                if hasattr(mutype, "MU_INT%d" % b):
                    return getattr(mutype, "MU_INT%d" % b)
                else:
                    return mutype.MuIntType("MU_INT%d" % b,
166
                                            rarithmetic.build_int('r_uint%d' % b, False, b))  # unsigned
167 168
            raise NotImplementedError("Don't know how to specialise %s using MuTS." % LLT)

169
    def map_type_arrfix(self, LLT):
170
        if LLT.length == 1:
171
            return self.map_type(LLT.OF)  # consider length 1 fixed sized array transparent
172

173
        return mutype.MuArray(self.map_type(LLT.OF), LLT.length)
174 175 176

    def map_type_stt(self, LLT):
        if LLT._is_varsize():
177
            return self.map_type_varstt(LLT)
178 179 180

        flds = []
        if needs_gcheader(LLT):
181
            flds.append(LL2MuMapper.GC_IDHASH_FIELD)
182 183

        for n in LLT._names:
184
            MuT = self.map_type(LLT._flds[n])
185 186 187
            if MuT is not mutype.MU_VOID:
                flds.append((n, MuT))

John Zhang's avatar
John Zhang committed
188 189 190 191 192 193 194
        if len(flds) == 0:  # empty struct
            # Mu does not support empty struct
            # From the spec:
            #   In Mu, if it is desired to allocate an empty unit in the heap,
            #   the appropriate type is `void`
            return mutype.MU_VOID

195 196 197
        name = self._new_typename(LLT._name)
        return mutype.MuStruct(name, *flds)

198 199
    def map_type_varstt(self, LLT):
        VarT = self.map_type(LLT._flds[LLT._arrayfld].OF)
200 201 202 203 204 205 206

        _names = LLT._names_without_voids()[:-1]
        _flds = LLT._flds.copy()
        if 'length' not in _names:
            _names.append('length')
            _flds['length'] = lltype.Signed

207
        flds = [(n, self.map_type(_flds[n])) for n in _names] + \
208 209
               [(LLT._arrayfld, VarT)]
        if needs_gcheader(LLT):
210
            flds.insert(0, LL2MuMapper.GC_IDHASH_FIELD)
211

212
        name = self._new_typename(LLT._name)
213 214
        return mutype.MuHybrid(name, *flds)

215
    def map_type_arr(self, LLT):
John Zhang's avatar
John Zhang committed
216 217 218
        if LLT._hints.get('mu_render_as', None):
            return LLT._hints['mu_render_as']

John Zhang's avatar
John Zhang committed
219
        name = "arr" + ("%s" % LLT.OF.__name__ if hasattr(LLT.OF, '__name__') else str(LLT.OF))
220 221 222 223

        if LLT.OF is lltype.Void:
            return mutype.MuStruct(name, ('length', mutype.MU_INT64))

224 225 226
        if LLT._hints.get('render_as_void', False):
            return mutype.MU_VOID

227 228 229 230 231 232 233
        MuT_OF = self.map_type(LLT.OF)
        flds = [('items', MuT_OF)]

        no_length = LLT._hints.get('nolength', False)
        no_hashfield = LLT._hints.get('mu_nohashfield', False)

        if not no_length:
234 235
            flds.insert(0, ('length', mutype.MU_INT64))

236
        if not no_hashfield and needs_gcheader(LLT):
237
            flds.insert(0, LL2MuMapper.GC_IDHASH_FIELD)
238 239 240

        return mutype.MuHybrid(name, *flds)

241
    def map_type_ptr(self, LLT):
242
        if isinstance(LLT.TO, lltype.FuncType):
243
            return self.map_type_funcptr(LLT)
244

245 246 247
        elif isinstance(LLT.TO, lltype.OpaqueType):
            return self.map_type_opqptr(LLT)

248 249 250 251 252 253
        if LLT.TO._gckind == 'gc':
            cls = mutype.MuRef
        else:
            cls = mutype.MuUPtr

        MuObjT = mutype.MuForwardReference()
254
        self._pending_ptr_types.append((LLT.TO, MuObjT))
255 256
        return cls(MuObjT)

257 258 259 260 261
    def map_type_iptr(self, LLT):
        MuObjT = mutype.MuForwardReference()
        self._pending_ptr_types.append((LLT.TO, MuObjT))
        return mutype.MuIRef(MuObjT)

262 263 264 265
    def resolve_ptr_types(self):
        while len(self._pending_ptr_types) > 0:
            LLObjT, MuObjT = self._pending_ptr_types.pop()
            MuObjT.become(self.map_type(LLObjT))
266

267
    def map_type_addr(self, LLT):
268
        return mutype.MuUPtr(mutype.MU_INT8)  # NOTE: all Address types are mapped to uptr<int<8>>
269

270 271 272
    def map_type_opqptr(self, LLT):
        T = LLT.TO

273 274 275 276
        if T is lltype.RuntimeTypeInfo:
            return mutype.MU_VOIDP  # rtti is never used;
        elif T is llmemory.GCREF.TO:
            return mutype.MU_VOIDR  # translate GCREF as ref<void>
John Zhang's avatar
John Zhang committed
277 278
        elif 'mu_render_ptr_as' in T.hints:
            return T.hints['mu_render_ptr_as']
279 280 281 282 283

        if LLT.TO._gckind == 'gc':
            cls = mutype.MuRef
        else:
            cls = mutype.MuUPtr
284 285
        try:
            MuT = self.map_type(T)
286
        except (NotImplementedError, CompilationError):
287 288 289 290 291 292 293 294 295 296 297 298
            MuT = mutype.MU_INT64  # default to ref/uptr<int<64>>

        return cls(MuT)

    def map_type_opq(self, LLT):
        # sometimes we still need to map opaque types, such as fd_set (_rsocket_rffi.fd_set)
        # translate to array with the same size
        if 'getsize' in LLT.hints:
            size = LLT.hints['getsize']()
            MuT = mutype.MuArray(mutype.MU_INT8, size)
            return MuT
        raise NotImplementedError("don't know how to translate OpaqueType %s" % LLT)
299

300
    def map_type_funcptr(self, LLT):
301
        LLFnc = LLT.TO
302
        ARG_TS = tuple(self.map_type(ARG) for ARG in LLFnc.ARGS if ARG != lltype.Void)
303
        RTN_TS = (self.map_type(LLFnc.RESULT),) if LLFnc.RESULT != lltype.Void else tuple()
304 305
        sig = mutype.MuFuncSig(ARG_TS, RTN_TS)
        return mutype.MuFuncRef(sig)
306 307

    def map_wref(self, LLT):
John Zhang's avatar
John Zhang committed
308 309 310 311 312
        """
        Translate weak references as struct<weakref<void>>
        This is because weakref can only be a memory location (not SSA variable).
        """
        return mutype.MuStruct('WeakRef', ('wref', mutype.MU_WEAKREF_VOID))
313

314 315
    # -----------------------------------------------------------------------------
    def map_value(self, llv, **kwargs):
316
        cache, v = (self._ptr_cache, llv._obj) \
317
            if isinstance(llv, lltype._abstract_ptr) \
318 319 320 321 322 323 324
            else (self._val_cache, llv)
        LLT = lltype.typeOf(llv)
        key = (LLT, v)
        try:
            return cache[key]
        except KeyError:
            if isinstance(LLT, lltype.Primitive):
325
                muv = self.map_value_prim(llv)
326 327

            elif isinstance(llv, lltype._fixedsizearray):
328
                muv = self.map_value_arrfix(llv)
329 330

            elif isinstance(llv, lltype._struct):
331
                muv = self.map_value_stt(llv, **kwargs)
332 333

            elif isinstance(llv, lltype._array):
334
                muv = self.map_value_arr(llv)
335 336

            elif isinstance(llv, lltype._ptr):
337
                muv = self.map_value_ptr(llv)
338

339 340 341
            elif isinstance(llv, lltype._interior_ptr):
                muv = self.map_value_iptr(llv)

342
            elif isinstance(llv, lltype._opaque):
343
                muv = self.map_value_opq(llv)
344 345

            elif isinstance(llv, llmemory._wref):
346
                muv = self.map_value_wref(llv)
347 348 349 350 351 352 353 354 355 356

            else:
                raise NotImplementedError(
                    "Don't know how to specialise value %r of type %r." % (llv, lltype.typeOf(llv)))

            if key not in cache:  # may have already been added to cache (in stt to prevent recursion).
                cache[key] = muv
            return muv
        except TypeError, e:
            if isinstance(llv, llmemory.AddressOffset):
357
                return self.map_value_adrofs(llv)
358
            if isinstance(LLT, lltype.Primitive):
359
                return self.map_value_prim(llv)
360 361
            raise e

362 363
    def map_value_prim(self, llv):
        MuT = self.map_type(lltype.typeOf(llv))
364 365 366
        if isinstance(llv, TotalOrderSymbolic):
            llv = llv.compute_fn()
        elif isinstance(llv, CDefinedIntSymbolic):
367 368
            from rpython.rlib import objectmodel, jit
            if llv is objectmodel._translated_to_c:
369
                llv = 1  # faking it to make it work
370
            elif llv is objectmodel.malloc_zero_filled:
371
                llv = 1  # Mu NEW zeros memory
372
            elif llv is jit._we_are_jitted:
373
                llv = 0  # this is AoT, not jitted
John Zhang's avatar
John Zhang committed
374 375
            elif 'FinalizerQueue TAG' in llv.expr:  # rgc.py:515
                llv = 0
376 377 378
            else:
                raise NotImplementedError("Unanticipated %s" % llv)

379 380 381 382 383 384 385 386
        elif isinstance(llv, (str, unicode)):
            assert len(llv) == 1  # char
            llv = ord(llv)
        elif isinstance(llv, rffi.CConstant):
            from pypy.module._minimal_curses.fficurses import ERR, OK
            if llv in (ERR, OK):
                llv = -1 if llv is ERR else 0
            else:
387
                raise NotImplementedError("Unanticipted %s" % llv)
388 389 390
        elif isinstance(llv, llmemory.fakeaddress):
            assert not llv  # must be NULL
            return MuT._null()
391

392
        return MuT._val_type(llv)
393

394 395
    def map_value_arrfix(self, llv):
        MuT = self.map_type(lltype.typeOf(llv))
396 397
        arr = mutype._muarray(MuT)
        for i in range(llv.getlength()):
398
            arr[i] = self.map_value(llv.getitem(i))
399 400
        return arr

401
    def map_value_stt(self, llv, building=False):
402 403 404
        LLT = lltype.typeOf(llv)
        topstt = llv._normalizedcontainer()
        if building:
405
            MuT = self.map_type(LLT)
406
            self.resolve_ptr_types()
407 408 409
            stt = mutype._mustruct(MuT)
            self._val_cache[(LLT, llv)] = stt

410
            gcidfld, gcidfld_T = self.GC_IDHASH_FIELD
411 412 413

            if len(llv._TYPE._names) != 0:  # origional value struct is non-empty
                for fld in filter(lambda n: n != gcidfld, MuT._names):
414
                    setattr(stt, fld, self.map_value(getattr(llv, fld), building=True))
415 416 417

            if hasattr(stt, gcidfld) and hasattr(topstt, '_hash_cache_'):
                _idhash = topstt._hash_cache_
418
                setattr(stt, gcidfld, gcidfld_T._val_type(_idhash))
419 420 421 422 423 424

            llprnt = llv._parentstructure()
            llprnt_t = lltype.typeOf(llprnt)
            if llprnt and isinstance(llprnt_t, lltype.Struct):
                key = (llprnt_t, llprnt)
                assert key in self._val_cache
425
                stt._setparentstructure(self._val_cache[key], llv._parent_index)
426 427
        else:
            if LLT._is_varsize():
428
                return self.map_value_varstt(llv)
429

430
            MuT = self.map_type(LLT)
431
            if MuT is mutype.MU_VOID:  # empty struct, struct<>
432 433
                return None

434 435
            if topstt not in self._topstt_map:
                # build from top
436
                topstt_mu = self.map_value(topstt, building=True)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                self._topstt_map[topstt] = topstt_mu
            else:
                topstt_mu = self._topstt_map[topstt]

            # work out the depth of parent structure
            depth = 0
            prnt = llv
            while not prnt is topstt:
                depth += 1
                prnt = prnt._parentstructure()

            # traverse down according to the depth
            stt = topstt_mu
            while depth > 0:
                depth -= 1
                stt = stt.super

        return stt

456
    def map_value_varstt(self, llv):
457
        LLT = lltype.typeOf(llv)
458
        MuT = self.map_type(LLT)
459
        arr = getattr(llv, LLT._arrayfld)
460
        hyb = mutype._muhybrid(MuT, MuT.length._val_type(arr.getlength()))
461

462
        gcidfld, gcidfld_T = self.GC_IDHASH_FIELD
463

464
        for fld in filter(lambda n: n != gcidfld and n != 'length', MuT._names[:-1]):
465
            setattr(hyb, fld, self.map_value(getattr(llv, fld)))
466 467 468

        if hasattr(hyb, gcidfld) and hasattr(llv, '_hash_cache_'):
            _idhash = llv._hash_cache_
469
            setattr(hyb, gcidfld, gcidfld_T._val_type(_idhash))
470 471 472

        _memarr = getattr(hyb, MuT._varfld)
        for i in range(arr.getlength()):
473
            _memarr[i] = self.map_value(arr.getitem(i))
474

475
        if hasattr(hyb, 'length'):
476
            hyb.length = self.map_value(arr.getlength())
477 478
        return hyb

479
    def map_value_arr(self, llv):
480
        LLT = lltype.typeOf(llv)
481
        MuT = self.map_type(LLT)
482

John Zhang's avatar
John Zhang committed
483
        if llv._TYPE.OF == lltype.Void:
484
            stt = mutype._mustruct(MuT)
485
            stt.length = self.map_value(llv.getlength())
486 487
            return stt

488
        hyb = mutype._muhybrid(MuT, self.map_value(llv.getlength()))
489

490 491 492
        if 'length' in MuT._names:
            hyb.length = mutype.mu_int64(llv.getlength())

493
        _memarr = getattr(hyb, MuT._varfld)
494
        for i in range(llv.getlength()):
495
            _memarr[i] = self.map_value(llv.getitem(i))
496 497 498

        return hyb

499
    def map_value_ptr(self, llv):
500
        LLT = lltype.typeOf(llv)
501
        MuT = self.map_type(LLT)
502 503

        if llv._obj0 is None:
504
            return MuT._null()
505 506

        if isinstance(LLT.TO, lltype.FuncType):
507
            return self.map_value_funcptr(llv)
508

509
        ref = MuT._null()  # set object later
510

511
        self._pending_ptr_values.append((llv._obj, ref))
512 513
        return ref

514 515 516 517 518
    def map_value_iptr(self, llv):
        parent = self.map_value(llv._parent._as_ptr())
        MuT = self.map_type(lltype.typeOf(llv))
        return mutype._muiref(MuT, parent, llv._offsets)

519 520 521 522
    def resolve_ptr_values(self):
        while len(self._pending_ptr_values) > 0:
            llv, ref = self._pending_ptr_values.pop()
            obj = self.map_value(llv)
523 524
            if isinstance(ref, mutype._muref):
                ref._obj = obj  # directly set _obj in _muref
John Zhang's avatar
John Zhang committed
525 526
            elif isinstance(ref, mutype._muweakref):
                ref._obj = weakref.ref(obj)
527
            else:
528
                ref._store(obj)  # otherwise (iref, uptr) call _store
529

530
    def map_value_funcptr(self, llv):
531
        LLT = lltype.typeOf(llv)
532
        MuT = self.map_type(LLT)
533 534 535 536 537 538 539 540 541 542
        fnc = llv._obj
        graph = getattr(fnc, 'graph', None)
        if graph:
            return mutype._mufuncref(MuT, graph=graph,
                                     _name=getattr(fnc, '_name', ''))
        else:
            # external functions
            Sig = MuT.Sig
            MuT = mutype.MuUFuncPtr(Sig)
            c_name = fnc._name
John Zhang's avatar
John Zhang committed
543
            return mutype._muufuncptr(MuT, _name=c_name, eci=fnc.compilation_info, _llfnctype=LLT.TO)
544

545
    def map_value_adrofs(self, llv):
546 547 548 549 550 551 552
        def rec(llv):
            if isinstance(llv, llmemory.CompositeOffset):
                ofs = 0
                for llv2 in llv.offsets:
                    ofs += rec(llv2)
                return ofs
            elif isinstance(llv, llmemory.ItemOffset):
553
                MuT = mutype.MuArray(self.map_type(llv.TYPE), llv.repeat)
554 555
                return layout.mu_offsetOf(MuT, llv.repeat)
            elif isinstance(llv, llmemory.FieldOffset):
556
                MuT = self.map_type(llv.TYPE)
557 558 559 560 561 562
                if isinstance(MuT, mutype.MuHybrid) and \
                                llv.fldname == MuT._varfld and len(MuT._names) > 1:
                    # get the offset of the 'length' field instead of variable part
                    return layout.mu_offsetOf(MuT, MuT._names[-2])
                return layout.mu_offsetOf(MuT, llv.fldname)
            elif isinstance(llv, llmemory.ArrayItemsOffset):
563 564
                MuT = self.map_type(llv.TYPE)
                _ofs = 8 if self.GC_IDHASH_FIELD[0] in MuT._names else 0  # __gc_idhash field
565 566 567
                if llv.TYPE._hints.get("nolength", False):
                    return _ofs
                return _ofs + 8  # sizeof(i64)
568 569 570
            elif isinstance(llv, llmemory.ArrayLengthOffset):
                MuT = self.map_type(llv.TYPE)
                return layout.mu_offsetOf(MuT, 'length')
571 572
            else:
                raise AssertionError("Value {:r} of type {:r} shouldn't appear.".format(llv, type(llv)))
573

574
        MuT = self.map_type(lltype.typeOf(llv))
575
        return MuT._val_type(rec(llv))
576

577
    def map_value_opq(self, llv):
John Zhang's avatar
John Zhang committed
578
        if llv._TYPE == lltype.RuntimeTypeInfo:
579 580
            # RuntimeTypeInfo is not used, translate to NULL
            return None
581 582 583

        if hasattr(llv, 'container'):
            container = llv._normalizedcontainer()
584
            muv = self.map_value(container)
585 586 587 588
            # log.ll2mu_val("%(llv)r really is %(muv)r" % locals())
            return muv

        muv = mutype.mu_int64(randint(0, 0xffffffff))  # randomise it.
589
        # log.ll2mu_val("WARNING: specialising '%r' to '%r' of type '%s'." % (llv, muv, muv._TYPE))
590 591
        return muv

592 593
    def map_value_wref(self, llv):
        MuT = self.map_type(lltype.typeOf(llv))
594
        stt = mutype._mustruct(MuT)
John Zhang's avatar
John Zhang committed
595 596 597 598 599
        llptr = llv._dereference()
        wref = mutype.MU_WEAKREF_VOID._null()
        if llptr:
            self._pending_ptr_values.append((llptr._obj, wref))
        setattr(stt, 'wref', wref)
600
        return stt
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

    # -----------------------------------------------------------------------------
    def mapped_const(self, llv, LLT=None):
        if LLT is None:
            try:
                LLT = lltype.typeOf(llv)
            except TypeError:
                LLT = lltype.Void
        MuT = self.map_type(LLT)
        muv = self.map_value(llv) if LLT != lltype.Void else llv
        c = Constant(muv, MuT)
        return c

    def map_op(self, llop):
        """
        May RTyped operations to Mu operations.
        NOTE: the name of the original operation is changed.

        :param llop: SpaceOperation
        :return: [SpaceOperation]
        """
John Zhang's avatar
John Zhang committed
622
        if hasattr(self, 'map_op_' + llop.opname):
623 624
            return getattr(self, 'map_op_' + llop.opname)(llop)

625
        elif llop.opname in _binop_map:  # a binop
John Zhang's avatar
John Zhang committed
626 627 628 629 630 631
            if any(cmpop in llop.opname for cmpop in 'lt le eq ne ge gt'.split(' ')):
                return self._map_cmpop(llop)
            else:
                return self._map_binop(llop)
        elif llop.opname in _prim_castop_map:  # a convop
            return self._map_convop(llop)
John Zhang's avatar
John Zhang committed
632

John Zhang's avatar
John Zhang committed
633
        else:
634 635 636 637 638 639 640 641 642 643
            raise NotImplementedError("Has not implemented specialisation for operation '%s'" % llop)

    def dest_clause(self, blk, args):
        """ Destination clause is a Link """
        return Link(args, blk)

    def exc_clause(self, dst_nor, dst_exc):
        """ Exception clause is a tuple """
        return dst_nor, dst_exc

644 645 646 647 648 649 650 651 652 653
    def _same_as_false(self, llop):
        llop.__init__('same_as', [self.mapped_const(False)], llop.result)
        return [llop]

    def _same_as_true(self, llop):
        llop.__init__('same_as', [self.mapped_const(True)], llop.result)
        return [llop]

    def _rename_to_same_as(self, llop):
        llop.opname = 'same_as'
John Zhang's avatar
John Zhang committed
654
        return [llop]
655

656 657
    # ----------------
    # call ops
658 659 660 661 662 663 664 665 666 667 668 669
    def _prepare_call_args(self, args, Sig):
        ops = []
        call_args = []
        for (a, ARG) in zip(args, Sig.ARGS):
            if a.concretetype == ARG:
                call_args.append(a)
            else:
                a_cast = varof(ARG)
                ops.extend(self.map_op_force_cast(SpaceOperation('force_cast', [a], a_cast)))
                call_args.append(a_cast)
        return call_args, ops

670 671
    def map_op_direct_call(self, llop):
        fr = llop.args[0].value
672 673 674 675
        args, ops = self._prepare_call_args(llop.args[1:], mutype.mutypeOf(fr).Sig)
        mtd = self.gen_mu_ccall if isinstance(fr, mutype._muufuncptr) else self.gen_mu_call
        ops.append(mtd(llop.args[0], args, llop.result))
        return ops
676 677 678 679

    def map_op_indirect_call(self, llop):
        last = llop.args[-1]
        if isinstance(last, Constant) and isinstance(last.value, list):
John Zhang's avatar
John Zhang committed
680
            args = llop.args[1:-1]
681
        else:
John Zhang's avatar
John Zhang committed
682
            args = llop.args[1:]
683 684 685
        call_args, ops = self._prepare_call_args(args, llop.args[0].concretetype.Sig)
        ops.append(self.gen_mu_call(llop.args[0], call_args, llop.result))
        return ops
686 687 688 689 690

    # ----------------
    # primitive ops
    def map_op_bool_not(self, llop):
        ops = []
John Zhang's avatar
John Zhang committed
691
        if llop.args[0].concretetype is mutype.MU_INT1:
692
            ops.append(self.gen_mu_convop('ZEXT', mutype.MU_INT8, llop.args[0]))
693
            v = ops[-1].result
694 695
        else:
            v = llop.args[0]
696
        ops.append(self.gen_mu_binop('XOR', v, self.mapped_const(True), llop.result))
697 698 699
        return ops

    def map_op_int_is_true(self, llop):
700 701 702 703 704
        # x != 0
        MuT = llop.args[0].concretetype
        llop.__init__('int_ne', [llop.args[0], Constant(MuT._val_type(0), MuT)], llop.result)
        return self.map_op(llop)

John Zhang's avatar
John Zhang committed
705 706 707 708
    map_op_uint_is_true = map_op_int_is_true
    map_op_llong_is_true = map_op_int_is_true
    map_op_ullong_is_true = map_op_int_is_true
    map_op_lllong_is_true = map_op_int_is_true
709 710 711

    def map_op_int_neg(self, llop):
        MuT = llop.args[0].concretetype
John Zhang's avatar
John Zhang committed
712
        llop.__init__('int_sub', [
713 714 715
            Constant(MuT._val_type(0), MuT),
            llop.args[0],
        ],
716
                      llop.result)
John Zhang's avatar
John Zhang committed
717 718 719 720
        return self.map_op(llop)

    map_op_llong_neg = map_op_int_neg
    map_op_lllong_neg = map_op_int_neg
721 722 723 724

    def map_op_int_abs(self, llop):
        ops = []
        x = llop.args[0]
John Zhang's avatar
John Zhang committed
725
        MuT = x.concretetype
John Zhang's avatar
John Zhang committed
726
        # -x = 0 - x
727
        neg_x = varof(x.concretetype, 'neg_x')
728 729
        op_neg = SpaceOperation('int_neg', [x], neg_x)
        ops.extend(self.map_op(op_neg))
John Zhang's avatar
John Zhang committed
730
        # x > 0 ?
731
        cmp_res = varof(mutype.MU_INT1, 'cmp_res')
732
        ops.append(self.gen_mu_cmpop('SGT', x, Constant(MuT._val_type(0), MuT), cmp_res))
John Zhang's avatar
John Zhang committed
733
        # True -> x, False -> -x
734
        ops.append(self.gen_mu_select(cmp_res, x, neg_x, llop.result))
735
        return ops
John Zhang's avatar
John Zhang committed
736 737 738 739 740 741 742 743 744 745

    map_op_llong_abs = map_op_int_abs
    map_op_lllong_abs = map_op_int_abs

    def map_op_int_invert(self, llop):
        # 2's complement
        # x' = (-x) - 1
        ops = []
        x = llop.args[0]

746
        neg_x = varof(x.concretetype, 'neg_x')
John Zhang's avatar
John Zhang committed
747 748
        op_neg = SpaceOperation('int_neg', [x], neg_x)
        ops.extend(self.map_op(op_neg))
John Zhang's avatar
John Zhang committed
749 750
        op_sub = SpaceOperation('int_sub', [neg_x, Constant(x.concretetype._val_type(1), x.concretetype)], llop.result)
        ops.extend(self.map_op(op_sub))
751
        return ops
John Zhang's avatar
John Zhang committed
752 753 754 755 756 757 758 759

    map_op_uint_invert = map_op_int_invert
    map_op_llong_invert = map_op_int_invert
    map_op_ullong_invert = map_op_int_invert
    map_op_lllong_invert = map_op_int_invert

    def map_op_int_between(self, llop):
        muops = []
760 761
        ge_res = varof(mutype.MU_INT8, 'ge_res')
        lt_res = varof(mutype.MU_INT8, 'lt_res')
John Zhang's avatar
John Zhang committed
762 763 764 765
        op_ge = SpaceOperation('int_ge', [llop.args[1], llop.args[0]], ge_res)
        muops.extend(self.map_op(op_ge))
        op_lt = SpaceOperation('int_lt', [llop.args[1], llop.args[2]], lt_res)
        muops.extend(self.map_op(op_lt))
John Zhang's avatar
John Zhang committed
766
        llop.__init__('int_and', [ge_res, lt_res], llop.result)
John Zhang's avatar
John Zhang committed
767 768 769 770 771 772 773 774
        muops.extend(self.map_op(llop))
        return muops

    def map_op_int_force_ge_zero(self, llop):
        muops = []
        a = llop.args[0]
        MuT = a.concretetype
        zero = Constant(MuT._val_type(0), MuT)
775
        lt_zero = varof(mutype.MU_INT1, 'lt_zero')
776 777
        muops.append(self.gen_mu_cmpop('SLT', a, zero, lt_zero))
        muops.append(self.gen_mu_select(lt_zero, zero, a, llop.result))
778
        return muops
John Zhang's avatar
John Zhang committed
779 780

    def map_op_int_add_ovf(self, llop):
781
        flag_v = varof(mutype.MU_INT1, 'ovf_V')
782
        flag = 'V'
783
        return [self.gen_mu_binop('ADD', llop.args[0], llop.args[1], llop.result, flag, [flag_v])]
John Zhang's avatar
John Zhang committed
784 785 786 787

    map_op_int_add_nonneg_ovf = map_op_int_add_ovf

    def map_op_int_sub_ovf(self, llop):
788
        flag_v = varof(mutype.MU_INT1, 'ovf_V')
789
        flag = 'V'
790
        return [self.gen_mu_binop('SUB', llop.args[0], llop.args[1], llop.result, flag, [flag_v])]
John Zhang's avatar
John Zhang committed
791 792

    def map_op_int_mul_ovf(self, llop):
793
        flag_v = varof(mutype.MU_INT1, 'ovf_V')
794
        flag = 'V'
795
        return [self.gen_mu_binop('MUL', llop.args[0], llop.args[1], llop.result, flag, [flag_v])]
John Zhang's avatar
John Zhang committed
796

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    def map_op_float_is_true(self, llop):
        # x != 0
        MuT = llop.args[0].concretetype
        llop.__init__('float_ne', [llop.args[0], Constant(MuT._val_type(0.0), MuT)], llop.result)
        return self.map_op(llop)

    def map_op_float_neg(self, llop):
        MuT = llop.args[0].concretetype
        llop.__init__('float_sub', [
            Constant(MuT._val_type(0.0), MuT),
            llop.args[0],
        ],
                      llop.result)
        return self.map_op(llop)

    def map_op_float_abs(self, llop):
        ops = []
        x = llop.args[0]
        MuT = x.concretetype
        # -x = 0 - x
        neg_x = varof(x.concretetype, 'neg_x')
        op_neg = SpaceOperation('float_neg', [x], neg_x)
        ops.extend(self.map_op(op_neg))
        # x > 0 ?
        cmp_res = varof(mutype.MU_INT1, 'cmp_res')
        ops.append(self.gen_mu_cmpop('FOGT', x, Constant(MuT._val_type(0.0), MuT), cmp_res))
        # True -> x, False -> -x
        ops.append(self.gen_mu_select(cmp_res, x, neg_x, llop.result))
        return ops

John Zhang's avatar
John Zhang committed
827
    def _map_binop(self, llop):
828 829 830 831 832 833 834 835 836
        optr = _binop_map[llop.opname]
        if optr in ('SHL', 'LSHR', 'ASHR') and llop.args[1].concretetype != llop.args[0].concretetype:
            # type mismatch, force_cast the second one
            res = varof(llop.args[0].concretetype)
            ops = self.map_op(SpaceOperation('force_cast', [llop.args[1]], res))
            ops.append(self.gen_mu_binop(optr, llop.args[0], res, llop.result))
        else:
            ops = [self.gen_mu_binop(optr, llop.args[0], llop.args[1], llop.result)]
        return ops
John Zhang's avatar
John Zhang committed
837 838 839

    def _map_cmpop(self, llop):
        muops = []
840
        cmpres = varof(mutype.MU_INT1, 'cmpres')
841 842
        muops.append(self.gen_mu_cmpop(_binop_map[llop.opname], llop.args[0], llop.args[1], cmpres))
        muops.append(self.gen_mu_convop('ZEXT', mutype.MU_INT8, cmpres, llop.result))
John Zhang's avatar
John Zhang committed
843 844
        return muops

John Zhang's avatar
John Zhang committed
845
    def _map_convop(self, llop):
846
        return [self.gen_mu_convop(_prim_castop_map[llop.opname],
847
                                   llop.result.concretetype, llop.args[0], llop.result)]
John Zhang's avatar
John Zhang committed
848

849 850 851 852
    map_op_cast_int_to_uint = _rename_to_same_as
    map_op_cast_uint_to_int = _rename_to_same_as
    map_op_cast_int_to_unichar = _rename_to_same_as

John Zhang's avatar
John Zhang committed
853 854 855 856 857 858
    # ----------------
    # memory and pointer ops
    def map_op_malloc(self, llop):
        flavor = llop.args[-1].value['flavor']
        if flavor == 'gc':
            assert isinstance(llop.result.concretetype, mutype.MuRef)
859
            return [self.gen_mu_new(llop.args[0].value, llop.result)]
John Zhang's avatar
John Zhang committed
860 861
        else:
            assert isinstance(llop.result.concretetype, mutype.MuUPtr)
862 863 864
            sz = layout.mu_sizeOf(llop.args[0].value)
            llop.__init__('raw_malloc', [Constant(mutype.mu_int64(sz), mutype.MU_INT64)], llop.result)
            return self.map_op(llop)
John Zhang's avatar
John Zhang committed
865 866 867 868

    def map_op_malloc_varsize(self, llop):
        ops = []
        MuT_c, hints_c, n_c = llop.args
869
        MuT = MuT_c.value
John Zhang's avatar
John Zhang committed
870
        flavor = hints_c.value['flavor']
871 872 873 874 875 876 877

        if isinstance(MuT, mutype.MuStruct):
            if isinstance(n_c, Constant):
                assert n_c.value == 0
            llop.__init__('malloc', [MuT_c, hints_c], llop.result)
            return self.map_op(llop)

John Zhang's avatar
John Zhang committed
878 879
        if flavor == 'gc':
            assert isinstance(llop.result.concretetype, mutype.MuRef)
880
            ops.append(self.gen_mu_newhybrid(MuT, n_c, llop.result))
John Zhang's avatar
John Zhang committed
881 882
        else:
            assert isinstance(llop.result.concretetype, mutype.MuUPtr)
883 884 885 886 887 888 889 890
            if MuT == mutype.MU_VOID:
                # there are a few places in RPython code (especially libffi, _rawffi)
                # that does malloc(rffi.VOIDP.TO, size, flavor='raw').
                # In these cases MuT == MU_VOIDP
                # Thus directly allocate n_c bytes and return the pointer
                ops.extend(self.map_op(SpaceOperation('raw_malloc', [n_c], llop.result)))
                return ops

891 892 893 894 895
            fix = layout.mu_hybsizeOf(MuT, 0)
            itm = layout.mu_hybsizeOf(MuT, 1) - fix

            # sz = fix + itm * n
            v = varof(mutype.MU_INT64)
896 897
            ops.extend(self.map_op(SpaceOperation(
                'int_mul', [Constant(mutype.mu_int64(itm), mutype.MU_INT64), n_c], v)))
898
            sz = varof(mutype.MU_INT64, 'sz')
899 900 901 902
            ops.extend(self.map_op(SpaceOperation(
                'int_add', [Constant(mutype.mu_int64(fix), mutype.MU_INT64), v], sz)))
            ops.extend(self.map_op(SpaceOperation(
                'raw_malloc', [sz], llop.result)))
903

John Zhang's avatar
John Zhang committed
904
        if 'length' in MuT._names:
905 906 907
            ops.extend(self.map_op(SpaceOperation(
                'setfield', [llop.result, Constant('length', mutype.MU_VOID), n_c],
                varof(mutype.MU_VOID, 'dummy'))))
John Zhang's avatar
John Zhang committed
908 909 910 911 912 913 914

        return ops

    def _getfieldiref(self, var, fldname_c):
        ops = []
        MuT = var.concretetype
        fldname = fldname_c.value
915
        cls = mutype.MuUPtr if isinstance(MuT, mutype.MuUPtr) else mutype.MuIRef
John Zhang's avatar
John Zhang committed
916
        if isinstance(MuT, mutype.MuRef):
917
            iref = varof(cls(MuT.TO), 'ir%s' % var.name)
918
            ops.append(self.gen_mu_getiref(var, iref))
John Zhang's avatar
John Zhang committed
919 920
        else:
            iref = var
John Zhang's avatar
John Zhang committed
921 922

        assert isinstance(MuT.TO, (mutype.MuStruct, mutype.MuHybrid))
923
        idx = MuT.TO._index_of(fldname)  # NOTE: may throw AttributeError
924
        iref_fld = varof(cls(getattr(MuT.TO, fldname)), 'irf%s_%s' % (var.name, fldname))
925
        ops.append(self.gen_mu_getfieldiref(iref, fldname, iref_fld))
John Zhang's avatar
John Zhang committed
926 927 928 929 930 931 932
        return iref_fld, ops

    def map_op_getfield(self, llop):
        var, fldname_c = llop.args
        try:
            iref_fld, ops = self._getfieldiref(var, fldname_c)
        except AttributeError:
933
            # log.error("Field '%s' not found in type '%s'; ignoring %s." % (fldname_c.value, var.concretetype.TO, llop))
934
            return []
John Zhang's avatar
John Zhang committed
935

936
        ops.append(self.gen_mu_load(iref_fld, llop.result))
John Zhang's avatar
John Zhang committed
937 938 939 940 941 942 943
        return ops

    def map_op_setfield(self, llop):
        var, fldname_c, val_c = llop.args
        try:
            iref_fld, ops = self._getfieldiref(var, fldname_c)
        except AttributeError:
944
            # log.error("Field '%s' not found in type '%s'; ignoring %s." % (fldname_c.value, var.concretetype.TO, llop))
945 946
            return []

947
        ops.append(self.gen_mu_store(iref_fld, val_c, llop.result))
John Zhang's avatar
John Zhang committed
948 949
        return ops

John Zhang's avatar
John Zhang committed
950 951
    def map_op_getsubstruct(self, llop):
        var, fldname_c = llop.args
952 953 954 955
        if isinstance(var.concretetype.TO, mutype.MuHybrid) and fldname_c.value == var.concretetype.TO._varfld:
            RES_T = llop.result.concretetype.TO
            assert isinstance(RES_T, mutype.MuHybrid)
            if hasattr(RES_T, 'length'):
956 957
                iref_fld, ops = self._getfieldiref(var, Constant('length'))  # get the length field instead
                ops.extend(self.map_op(SpaceOperation('cast_pointer',  # cast to correct type
958 959 960
                                                      [Constant(llop.result.concretetype), iref_fld], llop.result)))
            else:
                iref_fld, ops = self._getinterioriref(var, [fldname_c])
961
        else:
962 963 964 965 966 967 968
            try:
                iref_fld, ops = self._getfieldiref(var, fldname_c)
            except AttributeError:
                # log.error("Field '%s' not found in type '%s'; ignoring %s." % (fldname_c.value, var.concretetype.TO, llop))
                return []

        ops[-1].result = llop.result
John Zhang's avatar
John Zhang committed
969 970 971 972 973
        return ops

    def _getarrayitemiref(self, var, idx_vc):
        ops = []
        MuT = var.concretetype
974
        cls = mutype.MuUPtr if isinstance(MuT, mutype.MuUPtr) else mutype.MuIRef
John Zhang's avatar
John Zhang committed
975
        if isinstance(MuT, mutype.MuRef):
976
            iref = varof(cls(MuT.TO), 'ir%s' % var.name)
977
            ops.append(self.gen_mu_getiref(var, iref))
John Zhang's avatar
John Zhang committed
978 979 980 981
        else:
            iref = var

        if isinstance(MuT.TO, mutype.MuHybrid):
982
            iref_itm0 = varof(cls(MuT.TO._vartype.OF), 'ira%s' % var.name)
983
            ops.append(self.gen_mu_getvarpartiref(iref, iref_itm0))
John Zhang's avatar
John Zhang committed
984 985
        else:
            assert isinstance(MuT.TO, mutype.MuArray)
986
            iref_itm0 = varof(cls(MuT.TO.OF), 'ira%s' % var.name)
987
            ops.extend(self.map_op(SpaceOperation('cast_pointer', [iref], iref_itm0)))
John Zhang's avatar
John Zhang committed
988

989
        iref_itm = varof(cls(iref_itm0.concretetype.TO), 'ir%s_itm' % var.name)
990
        ops.append(self.gen_mu_shiftiref(iref_itm0, idx_vc, iref_itm))
John Zhang's avatar
John Zhang committed
991 992 993 994
        return iref_itm, ops

    def map_op_getarrayitem(self, llop):
        var, idx_vc = llop.args
995 996 997 998 999 1000 1001
        ops = []
        if var.concretetype.TO == mutype.MU_VOID:  # cast void* to char*
            buf = varof(type(var.concretetype)(self.map_type(rffi.CCHARP).TO))
            ops = self.map_op(SpaceOperation('cast_pointer', [var], buf))
            var = buf
        iref_itm, _ops = self._getarrayitemiref(var, idx_vc)
        ops.extend(_ops)
1002
        ops.append(self.gen_mu_load(iref_itm, llop.result))
John Zhang's avatar
John Zhang committed
1003 1004 1005
        return ops

    def map_op_setarrayitem(self, llop):
1006 1007 1008
        if len(llop.args) < 3:
            MuT = llop.args[0].concretetype.TO
            assert isinstance(MuT, mutype.MuStruct) and 'Void' in llop.args[0].concretetype.TO._name
1009
            return []  # setting Void type values to array of Voids; translate as no-op
John Zhang's avatar
John Zhang committed
1010
        var, idx_vc, val_vc = llop.args
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
        ops = []
        if var.concretetype.TO == mutype.MU_VOID:  # cast void* to char*
            buf = varof(type(var.concretetype)(self.map_type(rffi.CCHARP).TO))
            ops = self.map_op(SpaceOperation('cast_pointer', [var], buf))
            var = buf
        iref_itm, _ops = self._getarrayitemiref(var, idx_vc)
        ops.extend(_ops)
        if iref_itm.concretetype.TO != val_vc.concretetype:
            if isinstance(iref_itm.concretetype.TO, mutype.MuReferenceType) and \
                    isinstance(val_vc.concretetype, mutype.MuReferenceType) and \
                    isinstance(val_vc.concretetype, type(iref_itm.concretetype.TO)):
                casted = varof(type(iref_itm.concretetype.TO)(iref_itm.concretetype.TO.TO))
                ops.extend(self.map_op(SpaceOperation('cast_pointer', [val_vc], casted)))
                val_vc = casted
1025
        ops.append(self.gen_mu_store(iref_itm, val_vc, llop.result))
John Zhang's avatar
John Zhang committed
1026 1027 1028 1029
        return ops

    def map_op_getarraysubstruct(self, llop):
        _iref_itm, ops = self._getarrayitemiref(*llop.args)
1030
        ops[-1].result = llop.result
John Zhang's avatar
John Zhang committed
1031 1032 1033 1034
        return ops

    def map_op_getarraysize(self, llop):
        iref_fld, ops = self._getfieldiref(llop.args[0], Constant('length', mutype.MU_VOID))
1035
        ops.append(self.gen_mu_load(iref_fld, llop.result))
John Zhang's avatar
John Zhang committed
1036 1037
        return ops

1038 1039 1040 1041 1042 1043
    def _getinterioriref(self, var, offsets):
        ops = []
        MuT = var.concretetype
        cls = mutype.MuUPtr if isinstance(MuT, mutype.MuUPtr) else mutype.MuIRef
        if isinstance(MuT, mutype.MuRef):
            iref = varof(cls(MuT.TO), 'ir%s' % var.name)
1044
            ops.append(self.gen_mu_getiref(var, iref))
1045 1046 1047 1048
        else:
            iref = var

        for o in offsets:
1049
            T = iref.concretetype.TO
1050 1051 1052 1053 1054
            if o.concretetype == mutype.MU_VOID:
                assert isinstance(o, Constant)
                assert isinstance(o.value, str)
                if isinstance(T, mutype.MuHybrid) and o.value == T._varfld:
                    iref_var = varof(cls(T._vartype.OF), 'ira%s' % var.name)
1055
                    ops.append(self.gen_mu_getvarpartiref(iref, iref_var))
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
                    iref = iref_var
                else:
                    iref, subops = self._getfieldiref(iref, o)
                    ops.extend(subops)
            else:
                assert isinstance(o.concretetype, mutype.MuIntType)
                if len(ops) == 0 or ops[-1].opname != 'mu_getvarpartiref':
                    # This case happens when the outer container is array,
                    # and rtyper assumes it can respond to indexing.
                    # For translated hybrid type however, we need to get the variable part reference first.
1066
                    assert isinstance(T, mutype.MuHybrid)
1067
                    iref_var = varof(cls(T._vartype.OF), 'ira%s' % var.name)
1068
                    ops.append(self.gen_mu_getvarpartiref(iref, iref_var))
1069 1070
                    iref = iref_var
                iref_itm = varof(cls(iref.concretetype.TO), 'ir%s_itm' % var.name)
1071
                ops.append(self.gen_mu_shiftiref(iref, o, iref_itm))
1072 1073 1074 1075 1076 1077 1078 1079 1080
                iref = iref_itm

        return iref, ops

    def map_op_getinteriorfield(self, llop):
        var = llop.args[0]
        offsets = llop.args[1:]
        try:
            iref, ops = self._getinterioriref(var, offsets)
1081
        except AttributeError as e:
1082
            # log.error("%s; ignoring %s." % (e.message, llop))
1083
            return []
1084

1085
        ops.append(self.gen_mu_load(iref, llop.result))
1086 1087 1088 1089 1090 1091 1092 1093
        return ops

    def map_op_setinteriorfield(self, llop):
        var = llop.args[0]
        offsets = llop.args[1:-1]
        val_vc = llop.args[-1]
        try:
            iref, ops = self._getinterioriref(var, offsets)
1094
        except AttributeError as e:
1095
            # log.error("%s; ignoring %s." % (e.message, llop))
1096
            return []
1097

1098
        ops.append(self.gen_mu_store(iref, val_vc, llop.result))
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
        return ops

    def map_op_getinteriorarraysize(self, llop):
        iref, ops = self._getinterioriref(llop.args[0], llop.args[1:-1])
        o = llop.args[-1]
        assert o.concretetype == mutype.MU_VOID and isinstance(o.value, str)
        Hyb = iref.concretetype.TO
        assert isinstance(Hyb, mutype.MuHybrid) and o.value == Hyb._varfld

        ops.extend(self.map_op(SpaceOperation('getarraysize', [iref], llop.result)))
        return ops

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    def map_op_ptr_nonzero(self, llop):
        Ptr = llop.args[0].concretetype
        NULL_c = Constant(Ptr._null(), Ptr)
        llop.__init__('ptr_ne', [llop.args[0], NULL_c], llop.result)
        return self.map_op(llop)

    def map_op_ptr_iszero(self, llop):
        Ptr = llop.args[0].concretetype
        NULL_c = Constant(Ptr._null(), Ptr)
        llop.__init__('ptr_eq', [llop.args[0], NULL_c], llop.result)
        return self.map_op(llop)

1123 1124
    map_op_shrink_array = _same_as_false

1125 1126
    # TODO: reconsider direct_ptradd and direct_arrayitems, based on the semantic in lltype
    def map_op_direct_ptradd(self, llop):
1127 1128 1129 1130 1131 1132 1133 1134
        var, idx_vc = llop.args
        ops = []
        if var.concretetype.TO == mutype.MU_VOID:  # cast void* to char*
            buf = varof(type(var.concretetype)(self.map_type(rffi.CCHARP).TO))
            ops = self.map_op(SpaceOperation('cast_pointer', [var], buf))
            var = buf
        iref_itm, _ops = self._getarrayitemiref(var, idx_vc)
        ops.extend(_ops)
1135 1136 1137 1138 1139 1140
        ops[-1].result = llop.result
        return ops

    def map_op_direct_arrayitems(self, llop):
        ARRAY = llop.args[0].concretetype.TO
        if not (isinstance(ARRAY, mutype.MuArray) or mutype.mu_barebonearray(ARRAY)):
1141 1142 1143
            _, muops = self._getinterioriref(llop.args[0], [Constant('items', mutype.MU_VOID)])
            muops[-1].result = llop.result
            return muops
1144
        # otherwise cast to the correct type
1145
        llop.__init__('cast_pointer', [llop.args[0]], llop.result)
1146 1147 1148 1149
        return self.map_op_cast_pointer(llop)

    # ----------------
    # address operations
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    def map_op_adr_add(self, llop):
        return [self.gen_mu_shiftiref(llop.args[0], llop.args[1], llop.result)]

    def map_op_adr_sub(self, llop):
        ofs = llop.args[1]
        neg_ofs = varof(ofs.concretetype)
        ops = self.map_op(SpaceOperation('int_neg', [ofs], neg_ofs))
        ops.extend(self.map_op_adr_add(SpaceOperation('adr_add', [llop.args[0], neg_ofs], llop.result)))
        return ops

    def map_op_adr_delta(self, llop):
        ops = []
        adr1, adr2 = llop.args
        if isinstance(adr1, mutype.MuUPtr):
            adr1_i = varof(mutype.MU_INT64)
            ops.append(self.gen_mu_convop('PTRCAST', mutype.MU_INT64, adr1, adr1_i))
        else:
            adr1_i = adr1
        if isinstance(adr2, mutype.MuUPtr):
            adr2_i = varof(mutype.MU_INT64)
            ops.append(self.gen_mu_convop('PTRCAST', mutype.MU_INT64, adr2, adr2_i))
        else:
            adr2_i = adr2

        if llop.result.concretetype == mutype.MU_INT64:
            ops.extend(self.map_op(SpaceOperation('int_sub', [adr1_i, adr2_i], llop.result)))
1176
        elif isinstance(llop.result.concretetype, mutype.MuUPtr):  # not sure if this is ever the case though
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
            delta = varof(mutype.MU_INT64)
            ops.extend(self.map_op(SpaceOperation('int_sub', [adr1_i, adr2_i], delta)))
            ops.append(self.gen_mu_convop('PTRCAST', llop.result.concretetype, delta, llop.result))

        return ops

    def _adr_cmp(self, llop):
        # first cast to int<64>, then does signed comparison
        ops = []
        adr1, adr2 = llop.args
        if isinstance(adr1, mutype.MuUPtr):
            adr1_i = varof(mutype.MU_INT64)
            ops.append(self.gen_mu_convop('PTRCAST', mutype.MU_INT64, adr1, adr1_i))
        else:
            adr1_i = adr1
        if isinstance(adr2, mutype.MuUPtr):
            adr2_i = varof(mutype.MU_INT64)
            ops.append(self.gen_mu_convop('PTRCAST', mutype.MU_INT64, adr2, adr2_i))
        else:
            adr2_i = adr2

        cmpop = llop.opname[-2:]
        ops.extend(self.map_op(SpaceOperation('int_' + cmpop, [adr1_i, adr2_i], llop.result)))
        return ops

    map_op_adr_lt = _adr_cmp
    map_op_adr_le = _adr_cmp
    map_op_adr_gt = _adr_cmp
    map_op_adr_ge = _adr_cmp

1207 1208 1209
    def map_op_keepalive(self, llop):
        ref = llop.args[0]
        if isinstance(ref.concretetype, mutype.MuRef):
John Zhang's avatar
John Zhang committed
1210 1211
            assert llop.result.concretetype == mutype.MU_VOID
            return [self.gen_mu_comminst('NATIVE_UNPIN', [ref], llop.result, types=[ref.concretetype])]
1212 1213 1214
        else:
            return []

1215
    def _map_rawmemop(self, llop):
1216
        muops = []
1217 1218 1219 1220 1221 1222