test_rpython.py 51.5 KB
Newer Older
John Zhang's avatar
John Zhang committed
1
from rpython.rtyper.lltypesystem import rffi, lltype
2
from rpython.rlib import rmu_fast as rmu
3
from rpython.translator.platform import platform
4
from util import fncptr_from_rpy_func, fncptr_from_py_script, may_spawn_proc
5
import ctypes, py, stat
6
import pytest
John Zhang's avatar
John Zhang committed
7

8 9
# -------------------
# helper functions
10
def rand_list_of(n):
11
    # 32 extend to 64-bit integers (to avoid overflow in summation
12 13 14
    from random import randrange, getstate
    init_state = getstate()
    return [rffi.r_longlong(randrange(-(1 << 31), (1 << 31) - 1)) for _ in range(n)], init_state
15 16


17 18
# --------------------------
# tests
19
@may_spawn_proc
20 21 22 23 24 25
def test_add():
    def add(a, b):
        return a + b

    fn, _ = fncptr_from_rpy_func(add, [rffi.LONGLONG, rffi.LONGLONG], rffi.LONGLONG)

26
    assert fn(1, 2) == 3
27 28


29
@may_spawn_proc
John Zhang's avatar
John Zhang committed
30 31 32 33 34 35 36
def test_vec3prod():
    def prod(v1, v2):
        a = v1[0] * v2[0]
        b = v1[1] * v2[1]
        c = v1[2] * v2[2]
        return a + b + c

37
    fnc, (db, bdlgen) = fncptr_from_rpy_func(prod, [rffi.CArrayPtr(rffi.LONGLONG), rffi.CArrayPtr(rffi.LONGLONG)], rffi.LONGLONG)
John Zhang's avatar
John Zhang committed
38 39 40 41 42 43 44 45 46
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
    with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), 3) as vec1:
        vec1[0] = 1
        vec1[1] = 2
        vec1[2] = 3
        with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), 3) as vec2:
            vec2[0] = 4
            vec2[1] = 5
            vec2[2] = 6
47

48
            assert fnc(vec1, vec2) == 32
John Zhang's avatar
John Zhang committed
49 50


51
@may_spawn_proc
John Zhang's avatar
John Zhang committed
52 53 54 55 56 57 58 59 60
def test_find_min():
    def find_min(xs, sz):
        m = xs[0]
        for i in range(1, sz):
            x = xs[i]
            if x < m:
                m = x
        return m

61
    fnc, (db, bdlgen) = fncptr_from_rpy_func(find_min, [rffi.CArrayPtr(rffi.LONGLONG), rffi.INTPTR_T], rffi.LONGLONG)
62
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
John Zhang's avatar
John Zhang committed
63 64
    with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), 5) as arr:
        lst = [23, 100, 0, 78, -5]
65 66
        for i, k in enumerate(lst):
            arr[i] = k
67

68
        fnc(arr, 5) == -5
John Zhang's avatar
John Zhang committed
69 70


71
@may_spawn_proc
72 73 74 75
def test_arraysum():
    from rpython.rlib.jit import JitDriver
    d = JitDriver(greens=[], reds='auto')
    def arraysum(arr, sz):
76
        sum = rffi.r_longlong(0)
77 78 79 80 81
        for i in range(sz):
            d.jit_merge_point()
            sum += arr[i]
        return sum

82
    fnc, (db, bdlgen) = fncptr_from_rpy_func(arraysum, [rffi.CArrayPtr(rffi.LONGLONG), rffi.SIZE_T], rffi.LONGLONG)
83
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
84

85
    n = 100
86
    lst, _ = rand_list_of(n)
87
    with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), n) as arr:
88 89
        for i, k in enumerate(lst):
            arr[i] = k
90

91
        assert fnc(arr, rffi.cast(rffi.SIZE_T, n)) == arraysum(arr, rffi.cast(rffi.SIZE_T, n))
92 93


94
@may_spawn_proc
John Zhang's avatar
John Zhang committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
def test_quicksort():
    # algorithm taken from Wikipedia
    def swap(arr, i, j):
        t = arr[i]
        arr[i] = arr[j]
        arr[j] = t

    def partition(arr, idx_low, idx_high):
        pivot = arr[idx_high]
        i = idx_low
        for j in range(idx_low, idx_high):
            if arr[j] < pivot:
                swap(arr, i, j)
                i += 1
        swap(arr, i, idx_high)
        return i

    def quicksort(arr, start, end):
        if start < end:
            p = partition(arr, start, end)
            quicksort(arr, start, p - 1)
            quicksort(arr, p + 1, end)
John Zhang's avatar
John Zhang committed
117

118
    fnc, (db, bdlgen) = fncptr_from_rpy_func(quicksort, [rffi.CArrayPtr(rffi.LONGLONG), lltype.Signed, lltype.Signed], lltype.Void)
119
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
120
    # fnc = quicksort
121

122
    n = 100
123
    from random import setstate
John Zhang's avatar
John Zhang committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    init_state = (3, (
    2147483648L, 3430835514L, 2928424416L, 3147699060L, 2823572732L, 2905216632L, 1887281517L, 14272356L, 1356039141L,
    2741361235L, 1824725388L, 2228169284L, 2679861265L, 3150239284L, 657657570L, 1407124159L, 517316568L, 653526369L,
    139268705L, 3784719953L, 2212355490L, 3452491289L, 1232629882L, 1791207424L, 2898278956L, 1147783320L, 1824413680L,
    1993303973L, 2568444883L, 4228847642L, 4163974668L, 385627078L, 3663560714L, 320542554L, 1565882322L, 3416481154L,
    4219229298L, 315071254L, 778331393L, 3961037651L, 2951403614L, 3355970261L, 102946340L, 2509883952L, 215897963L,
    3361072826L, 689991350L, 3348092598L, 1763608447L, 2140226443L, 3813151178L, 2619956936L, 51244592L, 2130725065L,
    3867113849L, 1980820881L, 2600246771L, 3207535572L, 257556968L, 2223367443L, 3706150033L, 1711074250L, 4252385224L,
    3197142331L, 4139558716L, 748471849L, 2281163369L, 2596250092L, 2804492653L, 484240110L, 3726117536L, 2483815933L,
    2173995598L, 3765136999L, 3178931194L, 1237068319L, 3427263384L, 3958412830L, 2268556676L, 360704423L, 4113430429L,
    3758882140L, 3743971788L, 1685454939L, 488386L, 3511218911L, 3020688912L, 2168345327L, 3149651862L, 1472484695L,
    2011779229L, 1112533726L, 1873931730L, 2196153055L, 3806225492L, 1515074892L, 251489714L, 1958141723L, 2081062631L,
    3703490262L, 3211541213L, 1436109217L, 2664448365L, 2350764370L, 1285829042L, 3496997759L, 2306637687L, 1571644344L,
    1020052455L, 3114491401L, 2994766034L, 1518527036L, 994512437L, 1732585804L, 2089330296L, 2592371643L, 2377347339L,
    2617648350L, 1478066246L, 389918052L, 1126787130L, 2728695369L, 2921719205L, 3193658789L, 2101782606L, 4284039483L,
    2704867468L, 3843423543L, 119359906L, 1882384901L, 832276556L, 1862974878L, 1943541262L, 1823624942L, 2146680272L,
    333006125L, 929197835L, 639017219L, 1640196300L, 1424826762L, 2119569013L, 4259272802L, 2089277168L, 2030198981L,
    2950559216L, 621654826L, 3452546704L, 4085446289L, 3038316311L, 527272378L, 1679817853L, 450787204L, 3525043861L,
    3838351358L, 1558592021L, 3649888848L, 3328370698L, 3247166155L, 3855970537L, 1183088418L, 2778702834L, 2820277014L,
    1530905121L, 1434023607L, 3942716950L, 41643359L, 310637634L, 1537174663L, 4265200088L, 3126624846L, 2837665903L,
    446994733L, 85970060L, 643115053L, 1751804182L, 1480207958L, 2977093071L, 544778713L, 738954842L, 3370733859L,
    3242319053L, 2707786138L, 4041098196L, 1671493839L, 3420415077L, 2473516599L, 3949211965L, 3686186772L, 753757988L,
    220738063L, 772481263L, 974568026L, 3190407677L, 480257177L, 3620733162L, 2616878358L, 665763320L, 2808607644L,
    3851308236L, 3633157256L, 4240746864L, 1261222691L, 268963935L, 1449514350L, 4229662564L, 1342533852L, 1913674460L,
    1761163533L, 1974260074L, 739184472L, 3811507072L, 2880992381L, 3998389163L, 2673626426L, 2212222504L, 231447607L,
    2608719702L, 3509764733L, 2403318909L, 635983093L, 4233939991L, 2894463467L, 177171270L, 2962364044L, 1191007101L,
    882222586L, 1004217833L, 717897978L, 2125381922L, 626199402L, 3694698943L, 1373935523L, 762314613L, 2291077454L,
    2111081024L, 3758576304L, 2812129656L, 4067461097L, 3700761868L, 2281420733L, 197217625L, 460620692L, 506837624L,
    1532931238L, 3872395078L, 3629107738L, 2273221134L, 2086345980L, 1240615886L, 958420495L, 4059583254L, 3119201875L,
    3742950862L, 891360845L, 2974235885L, 87814219L, 4067521161L, 615939803L, 1881195074L, 2225917026L, 2775128741L,
    2996201447L, 1590546624L, 3960431955L, 1417477945L, 913935155L, 1610033170L, 3212701447L, 2545374014L, 2887105562L,
    2991635417L, 3194532260L, 1565555757L, 2142474733L, 621483430L, 2268177481L, 919992760L, 2022043644L, 2756890220L,
    881105937L, 2621060794L, 4262292201L, 480112895L, 2557060162L, 2367031748L, 2172434102L, 296539623L, 3043643256L,
    59166373L, 2947638193L, 1312917612L, 1798724013L, 75864164L, 339661149L, 289536004L, 422147716L, 1134944052L,
    1095534216L, 1231984277L, 239787072L, 923053211L, 1015393503L, 2558889580L, 4194512643L, 448088150L, 707905706L,
    2649061310L, 3081089715L, 3432955562L, 2217740069L, 1965789353L, 3320360228L, 3625802364L, 2420747908L, 3116949010L,
    442654625L, 2157578112L, 3603825090L, 3111995525L, 1124579902L, 101836896L, 3297125816L, 136981134L, 4253748197L,
    3809600572L, 1668193778L, 4146759785L, 3712590372L, 2998653463L, 3032597504L, 1046471011L, 2843821193L, 802959497L,
    3307715534L, 3226042258L, 1014478160L, 3105844949L, 3209150965L, 610876993L, 2563947590L, 2482526324L, 3913970138L,
    2812702315L, 4281779167L, 1026357391L, 2579486306L, 402208L, 3457975059L, 1714004950L, 2543595755L, 2421499458L,
    478932497L, 3117588180L, 1565800974L, 1757724858L, 1483685124L, 2262270397L, 3794544469L, 3986696110L, 2914756339L,
    1952061826L, 2672480198L, 3793151752L, 309930721L, 1861137379L, 94571340L, 1162935802L, 3681554226L, 4027302061L,
    21079572L, 446709644L, 1587253187L, 1845056582L, 3080553052L, 3575272255L, 2526224735L, 3569822959L, 2685900491L,
    918305237L, 1399881227L, 1554912161L, 703181091L, 738501299L, 269937670L, 1078548118L, 2313670525L, 3495159622L,
    2659487842L, 11394628L, 1222454456L, 3392065094L, 3426833642L, 1153231613L, 1234517654L, 3144547626L, 2148039080L,
    3790136587L, 684648337L, 3956093475L, 1384378197L, 2042781475L, 759764431L, 222267088L, 3187778457L, 3795259108L,
    2817237549L, 3494781277L, 3762880618L, 892345749L, 2153484401L, 721588894L, 779278769L, 3306398772L, 4221452913L,
    1981375723L, 379087895L, 1604791625L, 1426046977L, 4231163093L, 1344994557L, 1341041093L, 1072537134L, 1829925137L,
    3791772627L, 3176876700L, 2553745117L, 664821113L, 473469583L, 1076256869L, 2406012795L, 3141453822L, 4123012649L,
    3058620143L, 1785080140L, 1181483189L, 3587874749L, 1453504375L, 707249496L, 2022787257L, 2436320047L, 602521701L,
    483826957L, 821599664L, 3333871672L, 3024431570L, 3814441382L, 416508285L, 1217138244L, 3975201118L, 3077724941L,
    180118569L, 3754556886L, 4121534265L, 3495283397L, 700504668L, 3113972067L, 719371171L, 910731026L, 619936911L,
    2937105529L, 2039892965L, 3853404454L, 3783801801L, 783321997L, 1135195902L, 326690505L, 1774036419L, 3476057413L,
    1518029608L, 1248626026L, 427510490L, 3443223611L, 4087014505L, 2858955517L, 1918675812L, 3921514056L, 3929126528L,
    4048889933L, 1583842117L, 3742539544L, 602292017L, 3393759050L, 3929818519L, 3119818281L, 3472644693L, 1993924627L,
    4163228821L, 2943877721L, 3143487730L, 4087113198L, 1149082355L, 1713272081L, 1243627655L, 3511633996L, 3358757220L,
    3812981394L, 650044449L, 2143650644L, 3869591312L, 3719322297L, 386030648L, 2633538573L, 672966554L, 3498396042L,
    3907556L, 2308686209L, 2878779858L, 1475925955L, 2701537395L, 1448018484L, 2962578755L, 1383479284L, 3731453464L,
    3659512663L, 1521189121L, 843749206L, 2243090279L, 572717972L, 3400421356L, 3440777300L, 1393518699L, 1681924551L,
    466257295L, 568413244L, 3288530316L, 2951425105L, 2624424893L, 2410788864L, 2243174464L, 1385949609L, 2454100663L,
    1113953725L, 2127471443L, 1775715557L, 3874125135L, 1901707926L, 3152599339L, 2277843623L, 1941785089L, 3171888228L,
    802596998L, 3397391306L, 1743834429L, 395463904L, 2099329462L, 3761809163L, 262702111L, 1868879810L, 2887406426L,
    1160032302L, 4164116477L, 2287740849L, 3312176050L, 747117003L, 4048006270L, 3955419375L, 2724452926L, 3141695820L,
    791246424L, 524525849L, 1794277132L, 295485241L, 4125127474L, 825108028L, 1582794137L, 1259992755L, 2938829230L,
    912029932L, 1534496985L, 3075283272L, 4052041116L, 1125808104L, 2032938837L, 4008676545L, 1638361535L, 1649316497L,
    1302633381L, 4221627277L, 1206130263L, 3114681993L, 3409690900L, 3373263243L, 2922903613L, 349048087L, 4049532385L,
    3458779287L, 1737687814L, 287275672L, 645786941L, 1492233180L, 3925845678L, 3344829077L, 1669219217L, 665224162L,
    2679234088L, 1986576411L, 50610077L, 1080114376L, 1881648396L, 3818465156L, 1486861008L, 3824208930L, 1782008170L,
    4115911912L, 656413265L, 771498619L, 2709443211L, 1919820065L, 451888753L, 1449812173L, 2001941180L, 2997921765L,
    753032713L, 3011517640L, 2386888602L, 3181040472L, 1280522185L, 1036471598L, 1243809973L, 2985144032L, 2238294821L,
    557934351L, 347132246L, 1797956016L, 624L), None)
195 196 197
    setstate(init_state)
    lst, init_state = rand_list_of(n)

qinsoon's avatar
qinsoon committed
198 199 200
    print "original list:"
    print lst

201
    with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), n) as arr:
202 203
        for i, k in enumerate(lst):
            arr[i] = k
204

205
        fnc(arr, 0, n - 1)  # inplace sort
206

207
        lst_s = sorted(lst)
qinsoon's avatar
qinsoon committed
208

209 210
        for i in range(n):
            assert lst_s[i] == arr[i], "%d != %d" % (lst_s[i], arr[i])
211

212

John Zhang's avatar
John Zhang committed
213
@may_spawn_proc
qinsoon's avatar
qinsoon committed
214
def test_partition_in_quicksort():
John Zhang's avatar
John Zhang committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    # algorithm taken from Wikipedia
    def swap(arr, i, j):
        t = arr[i]
        arr[i] = arr[j]
        arr[j] = t

    def partition(arr, idx_low, idx_high):
        pivot = arr[idx_high]
        i = idx_low
        for j in range(idx_low, idx_high):
            if arr[j] < pivot:
                swap(arr, i, j)
                i += 1
        swap(arr, i, idx_high)
        return i

    fnc, (db, bdlgen) = fncptr_from_rpy_func(partition, [rffi.CArrayPtr(rffi.LONGLONG), lltype.Signed, lltype.Signed],
                                             lltype.Signed)
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
    # fnc = partition

    n = 100
    from random import setstate
    init_state = (3, (
    2147483648L, 3430835514L, 2928424416L, 3147699060L, 2823572732L, 2905216632L, 1887281517L, 14272356L, 1356039141L,
    2741361235L, 1824725388L, 2228169284L, 2679861265L, 3150239284L, 657657570L, 1407124159L, 517316568L, 653526369L,
    139268705L, 3784719953L, 2212355490L, 3452491289L, 1232629882L, 1791207424L, 2898278956L, 1147783320L, 1824413680L,
    1993303973L, 2568444883L, 4228847642L, 4163974668L, 385627078L, 3663560714L, 320542554L, 1565882322L, 3416481154L,
    4219229298L, 315071254L, 778331393L, 3961037651L, 2951403614L, 3355970261L, 102946340L, 2509883952L, 215897963L,
    3361072826L, 689991350L, 3348092598L, 1763608447L, 2140226443L, 3813151178L, 2619956936L, 51244592L, 2130725065L,
    3867113849L, 1980820881L, 2600246771L, 3207535572L, 257556968L, 2223367443L, 3706150033L, 1711074250L, 4252385224L,
    3197142331L, 4139558716L, 748471849L, 2281163369L, 2596250092L, 2804492653L, 484240110L, 3726117536L, 2483815933L,
    2173995598L, 3765136999L, 3178931194L, 1237068319L, 3427263384L, 3958412830L, 2268556676L, 360704423L, 4113430429L,
    3758882140L, 3743971788L, 1685454939L, 488386L, 3511218911L, 3020688912L, 2168345327L, 3149651862L, 1472484695L,
    2011779229L, 1112533726L, 1873931730L, 2196153055L, 3806225492L, 1515074892L, 251489714L, 1958141723L, 2081062631L,
    3703490262L, 3211541213L, 1436109217L, 2664448365L, 2350764370L, 1285829042L, 3496997759L, 2306637687L, 1571644344L,
    1020052455L, 3114491401L, 2994766034L, 1518527036L, 994512437L, 1732585804L, 2089330296L, 2592371643L, 2377347339L,
    2617648350L, 1478066246L, 389918052L, 1126787130L, 2728695369L, 2921719205L, 3193658789L, 2101782606L, 4284039483L,
    2704867468L, 3843423543L, 119359906L, 1882384901L, 832276556L, 1862974878L, 1943541262L, 1823624942L, 2146680272L,
    333006125L, 929197835L, 639017219L, 1640196300L, 1424826762L, 2119569013L, 4259272802L, 2089277168L, 2030198981L,
    2950559216L, 621654826L, 3452546704L, 4085446289L, 3038316311L, 527272378L, 1679817853L, 450787204L, 3525043861L,
    3838351358L, 1558592021L, 3649888848L, 3328370698L, 3247166155L, 3855970537L, 1183088418L, 2778702834L, 2820277014L,
    1530905121L, 1434023607L, 3942716950L, 41643359L, 310637634L, 1537174663L, 4265200088L, 3126624846L, 2837665903L,
    446994733L, 85970060L, 643115053L, 1751804182L, 1480207958L, 2977093071L, 544778713L, 738954842L, 3370733859L,
    3242319053L, 2707786138L, 4041098196L, 1671493839L, 3420415077L, 2473516599L, 3949211965L, 3686186772L, 753757988L,
    220738063L, 772481263L, 974568026L, 3190407677L, 480257177L, 3620733162L, 2616878358L, 665763320L, 2808607644L,
    3851308236L, 3633157256L, 4240746864L, 1261222691L, 268963935L, 1449514350L, 4229662564L, 1342533852L, 1913674460L,
    1761163533L, 1974260074L, 739184472L, 3811507072L, 2880992381L, 3998389163L, 2673626426L, 2212222504L, 231447607L,
    2608719702L, 3509764733L, 2403318909L, 635983093L, 4233939991L, 2894463467L, 177171270L, 2962364044L, 1191007101L,
    882222586L, 1004217833L, 717897978L, 2125381922L, 626199402L, 3694698943L, 1373935523L, 762314613L, 2291077454L,
    2111081024L, 3758576304L, 2812129656L, 4067461097L, 3700761868L, 2281420733L, 197217625L, 460620692L, 506837624L,
    1532931238L, 3872395078L, 3629107738L, 2273221134L, 2086345980L, 1240615886L, 958420495L, 4059583254L, 3119201875L,
    3742950862L, 891360845L, 2974235885L, 87814219L, 4067521161L, 615939803L, 1881195074L, 2225917026L, 2775128741L,
    2996201447L, 1590546624L, 3960431955L, 1417477945L, 913935155L, 1610033170L, 3212701447L, 2545374014L, 2887105562L,
    2991635417L, 3194532260L, 1565555757L, 2142474733L, 621483430L, 2268177481L, 919992760L, 2022043644L, 2756890220L,
    881105937L, 2621060794L, 4262292201L, 480112895L, 2557060162L, 2367031748L, 2172434102L, 296539623L, 3043643256L,
    59166373L, 2947638193L, 1312917612L, 1798724013L, 75864164L, 339661149L, 289536004L, 422147716L, 1134944052L,
    1095534216L, 1231984277L, 239787072L, 923053211L, 1015393503L, 2558889580L, 4194512643L, 448088150L, 707905706L,
    2649061310L, 3081089715L, 3432955562L, 2217740069L, 1965789353L, 3320360228L, 3625802364L, 2420747908L, 3116949010L,
    442654625L, 2157578112L, 3603825090L, 3111995525L, 1124579902L, 101836896L, 3297125816L, 136981134L, 4253748197L,
    3809600572L, 1668193778L, 4146759785L, 3712590372L, 2998653463L, 3032597504L, 1046471011L, 2843821193L, 802959497L,
    3307715534L, 3226042258L, 1014478160L, 3105844949L, 3209150965L, 610876993L, 2563947590L, 2482526324L, 3913970138L,
    2812702315L, 4281779167L, 1026357391L, 2579486306L, 402208L, 3457975059L, 1714004950L, 2543595755L, 2421499458L,
    478932497L, 3117588180L, 1565800974L, 1757724858L, 1483685124L, 2262270397L, 3794544469L, 3986696110L, 2914756339L,
    1952061826L, 2672480198L, 3793151752L, 309930721L, 1861137379L, 94571340L, 1162935802L, 3681554226L, 4027302061L,
    21079572L, 446709644L, 1587253187L, 1845056582L, 3080553052L, 3575272255L, 2526224735L, 3569822959L, 2685900491L,
    918305237L, 1399881227L, 1554912161L, 703181091L, 738501299L, 269937670L, 1078548118L, 2313670525L, 3495159622L,
    2659487842L, 11394628L, 1222454456L, 3392065094L, 3426833642L, 1153231613L, 1234517654L, 3144547626L, 2148039080L,
    3790136587L, 684648337L, 3956093475L, 1384378197L, 2042781475L, 759764431L, 222267088L, 3187778457L, 3795259108L,
    2817237549L, 3494781277L, 3762880618L, 892345749L, 2153484401L, 721588894L, 779278769L, 3306398772L, 4221452913L,
    1981375723L, 379087895L, 1604791625L, 1426046977L, 4231163093L, 1344994557L, 1341041093L, 1072537134L, 1829925137L,
    3791772627L, 3176876700L, 2553745117L, 664821113L, 473469583L, 1076256869L, 2406012795L, 3141453822L, 4123012649L,
    3058620143L, 1785080140L, 1181483189L, 3587874749L, 1453504375L, 707249496L, 2022787257L, 2436320047L, 602521701L,
    483826957L, 821599664L, 3333871672L, 3024431570L, 3814441382L, 416508285L, 1217138244L, 3975201118L, 3077724941L,
    180118569L, 3754556886L, 4121534265L, 3495283397L, 700504668L, 3113972067L, 719371171L, 910731026L, 619936911L,
    2937105529L, 2039892965L, 3853404454L, 3783801801L, 783321997L, 1135195902L, 326690505L, 1774036419L, 3476057413L,
    1518029608L, 1248626026L, 427510490L, 3443223611L, 4087014505L, 2858955517L, 1918675812L, 3921514056L, 3929126528L,
    4048889933L, 1583842117L, 3742539544L, 602292017L, 3393759050L, 3929818519L, 3119818281L, 3472644693L, 1993924627L,
    4163228821L, 2943877721L, 3143487730L, 4087113198L, 1149082355L, 1713272081L, 1243627655L, 3511633996L, 3358757220L,
    3812981394L, 650044449L, 2143650644L, 3869591312L, 3719322297L, 386030648L, 2633538573L, 672966554L, 3498396042L,
    3907556L, 2308686209L, 2878779858L, 1475925955L, 2701537395L, 1448018484L, 2962578755L, 1383479284L, 3731453464L,
    3659512663L, 1521189121L, 843749206L, 2243090279L, 572717972L, 3400421356L, 3440777300L, 1393518699L, 1681924551L,
    466257295L, 568413244L, 3288530316L, 2951425105L, 2624424893L, 2410788864L, 2243174464L, 1385949609L, 2454100663L,
    1113953725L, 2127471443L, 1775715557L, 3874125135L, 1901707926L, 3152599339L, 2277843623L, 1941785089L, 3171888228L,
    802596998L, 3397391306L, 1743834429L, 395463904L, 2099329462L, 3761809163L, 262702111L, 1868879810L, 2887406426L,
    1160032302L, 4164116477L, 2287740849L, 3312176050L, 747117003L, 4048006270L, 3955419375L, 2724452926L, 3141695820L,
    791246424L, 524525849L, 1794277132L, 295485241L, 4125127474L, 825108028L, 1582794137L, 1259992755L, 2938829230L,
    912029932L, 1534496985L, 3075283272L, 4052041116L, 1125808104L, 2032938837L, 4008676545L, 1638361535L, 1649316497L,
    1302633381L, 4221627277L, 1206130263L, 3114681993L, 3409690900L, 3373263243L, 2922903613L, 349048087L, 4049532385L,
    3458779287L, 1737687814L, 287275672L, 645786941L, 1492233180L, 3925845678L, 3344829077L, 1669219217L, 665224162L,
    2679234088L, 1986576411L, 50610077L, 1080114376L, 1881648396L, 3818465156L, 1486861008L, 3824208930L, 1782008170L,
    4115911912L, 656413265L, 771498619L, 2709443211L, 1919820065L, 451888753L, 1449812173L, 2001941180L, 2997921765L,
    753032713L, 3011517640L, 2386888602L, 3181040472L, 1280522185L, 1036471598L, 1243809973L, 2985144032L, 2238294821L,
    557934351L, 347132246L, 1797956016L, 624L), None)
    setstate(init_state)
    lst, init_state = rand_list_of(n)

    with lltype.scoped_alloc(rffi.CArray(rffi.LONGLONG), n) as arr:
        for i, k in enumerate(lst):
            arr[i] = k

        idx = fnc(arr, 0, n - 1)

        first_partition = [
            -562164038,
            -2071388465,
            -663526532,
            77489857,
            -343649111,
            -1660130362,
            -1364581753,
            -2038184925,
            -1165174475,
            -1849978230,
            -1236284585,
            -347764193,
            -415184763,
            -864996653,
            -1431147879,
            -254259567,
            -948603419,
            -777817366,
            -762104870,
            118960100,
            -982992600,
            -291431596,
            -1300455919,
            98312853,
            -451757010,
            -127589060,
            -1770428162,
            -1836098229,
            -918293874,
            -337375506,
            -1787719536,
            -2086483893,
            -730620516,
            -365703180,
            -1528919012,
            -1666015908,
            75036665,
            -1068382947,
            -2097740676,
            -158140475,
            181349155,
            1134943658,
            926214681,
            1436898456,
            1896535137,
            654725403,
            964722898,
            829972680,
            777329866,
            726385788,
            1050914914,
            1280292061,
            727975360,
            1023937016,
            640384790,
            637969418,
            1884043455,
            1925731670,
            1057772537,
            1322685888,
            1351410892,
            945183403,
            2014860171,
            1918531212,
            955471993,
            1075682797,
            238111242,
            1508508491,
            828291293,
            1789417882,
            1102829861,
            1435471727,
            1980476539,
            1344494232,
            1771547746,
            784699465,
            478704353,
            1664007571,
            511675340,
            1174338681,
            835473661,
            1039011592,
            1901271880,
            1983373831,
            782060246,
            1847820592,
            1751300194,
            558677750,
            1338238899,
            1313544470,
            232877310,
            599055646,
            873066597,
            1433425901,
            1192634012,
            1322616334,
            2026877877,
            1070749459,
            1899988061,
            632945766,
        ]

        assert idx == 40
        for i in range(n):
            assert arr[i] == first_partition[i], "%d != %d" % (arr[i], first_partition[i])


426
@may_spawn_proc
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
def test_linkedlist_reversal():
    def reverse_linkedlist(head):
        h = head
        nxt = head.nxt
        while nxt:
            n = nxt.nxt
            nxt.nxt = h
            h = nxt
            nxt = n
        head.nxt = nxt
        return h

    Node = lltype.ForwardReference()
    NodePtr = lltype.Ptr(Node)
    Node.become(lltype.Struct("Node", ('val', rffi.CHAR), ('nxt', NodePtr)))

443
    fnc, (db, bdlgen) = fncptr_from_rpy_func(reverse_linkedlist, [NodePtr], NodePtr)
444 445
    bdlgen.mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
    # fnc = reverse_linkedlist
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

    # linked list: a -> b -> c -> d
    with lltype.scoped_alloc(Node) as a:
        a.val = 'a'
        with lltype.scoped_alloc(Node) as b:
            a.nxt = b
            b.val = 'b'
            with lltype.scoped_alloc(Node) as c:
                b.nxt = c
                c.val = 'c'
                with lltype.scoped_alloc(Node) as d:
                    c.nxt = d
                    d.val = 'd'
                    d.nxt = lltype.nullptr(Node)

461 462 463 464 465 466 467
                    h = fnc(a)
                    print '%s -> %s -> %s -> %s' % (h.val, h.nxt.val, h.nxt.nxt.val, h.nxt.nxt.nxt.val)
                    assert h.val == 'd'
                    assert h.nxt.val == 'c'
                    assert h.nxt.nxt.val == 'b'
                    assert h.nxt.nxt.nxt.val == 'a'
                    assert h.nxt.nxt.nxt.nxt == lltype.nullptr(Node)
468

469

470
@may_spawn_proc
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
def test_threadtran_fib():
    def build_test_bundle(bldr, rmu):
        """
        Builds the following test bundle.
            .typedef @i64 = int<64>
            .const @0_i64 <@i64> = 0
            .const @1_i64 <@i64> = 1
            .const @2_i64 <@i64> = 2
            .funcsig @sig_i64_i64 = (@i64) -> (@i64)
            .funcdef @fib VERSION @fib_v1 <@sig_i64_i64> {
                @fib_v1.blk0(<@i64> @fib_v1.blk0.k):
                    SWITCH <@i64> @fib_v1.blk0.k @fib_v1.blk2 (@fib_v1.blk0.k) {
                        @0_i64 @fib_v1.blk1 (@0_i64)
                        @1_i64 @fib_v1.blk1 (@1_i64)
                    }
                @fib_v1.blk1(<@i64> @fib_v1.blk1.rtn):
                    RET @fib_v1.blk1.rtn
                @fib_v1.blk2(<@i64> @fib_v1.blk1.k):
                    @fib_v1.blk2.k_1 = SUB <@i64> @fib_v1.blk2.k @1_i64
                    @fib_v1.blk2.res1 = CALL <@sig_i64_i64> @fib (@fib_v1.blk2.k_1)
                    @fib_v1.blk2.k_2 = SUB <@i64> @fib_v1.blk2.k @2_i64
                    @fib_v1.blk2.res2 = CALL <@sig_i64_i64> @fib (@fib_v1.blk2.k_2)
                    @fib_v1.blk2.res = ADD <@i64> @fib_v1.blk2.res1 @fib_v1.blk2.res2
                    RET @fib_v1.blk2.res2
            }
        :type bldr: rpython.rlib.rmu.MuIRBuilder
        :type rmu: rpython.rlib.rmu_fast
        :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID)
        """
        i64 = bldr.gen_sym("@i64")
        bldr.new_type_int(i64, 64)

        c_0_i64 = bldr.gen_sym("@0_i64")
        bldr.new_const_int(c_0_i64, i64, 0)
        c_1_i64 = bldr.gen_sym("@1_i64")
        bldr.new_const_int(c_1_i64, i64, 1)
        c_2_i64 = bldr.gen_sym("@2_i64")
        bldr.new_const_int(c_2_i64, i64, 2)

        sig_i64_i64 = bldr.gen_sym("@sig_i64_i64")
        bldr.new_funcsig(sig_i64_i64, [i64], [i64])

        fib = bldr.gen_sym("@fib")
        bldr.new_func(fib, sig_i64_i64)

        # function body
        v1 = bldr.gen_sym("@fib_v1")
        blk0 = bldr.gen_sym("@fib_v1.blk0")
        blk1 = bldr.gen_sym("@fib_v1.blk1")
        blk2 = bldr.gen_sym("@fib_v1.blk2")

        # blk0
        blk0_k = bldr.gen_sym("@fib_v1.blk0.k")
        dest_defl = bldr.gen_sym()
        dest_0 = bldr.gen_sym()
        dest_1 = bldr.gen_sym()
        bldr.new_dest_clause(dest_defl, blk2, [blk0_k])
        bldr.new_dest_clause(dest_0, blk1, [c_0_i64])
        bldr.new_dest_clause(dest_1, blk1, [c_1_i64])
        op_switch = bldr.gen_sym()
        bldr.new_switch(op_switch, i64, blk0_k, dest_defl, [c_0_i64, c_1_i64], [dest_0, dest_1])
        bldr.new_bb(blk0, [blk0_k], [i64], rmu.MU_NO_ID, [op_switch])

        # blk1
        blk1_rtn = bldr.gen_sym("@fig_v1.blk1.rtn")
        blk1_op_ret = bldr.gen_sym()
        bldr.new_ret(blk1_op_ret, [blk1_rtn])
        bldr.new_bb(blk1, [blk1_rtn], [i64], rmu.MU_NO_ID, [blk1_op_ret])

        # blk2
        blk2_k = bldr.gen_sym("@fig_v1.blk2.k")
        blk2_k_1 = bldr.gen_sym("@fig_v1.blk2.k_1")
        blk2_k_2 = bldr.gen_sym("@fig_v1.blk2.k_2")
        blk2_res = bldr.gen_sym("@fig_v1.blk2.res")
        blk2_res1 = bldr.gen_sym("@fig_v1.blk2.res1")
        blk2_res2 = bldr.gen_sym("@fig_v1.blk2.res2")
        op_sub_1 = bldr.gen_sym()
        bldr.new_binop(op_sub_1, blk2_k_1, rmu.MuBinOptr.SUB, i64, blk2_k, c_1_i64)
        op_call_1 = bldr.gen_sym()
        bldr.new_call(op_call_1, [blk2_res1], sig_i64_i64, fib, [blk2_k_1])
        op_sub_2 = bldr.gen_sym()
        bldr.new_binop(op_sub_2, blk2_k_2, rmu.MuBinOptr.SUB, i64, blk2_k, c_2_i64)
        op_call_2 = bldr.gen_sym()
        bldr.new_call(op_call_2, [blk2_res2], sig_i64_i64, fib, [blk2_k_2])
        op_add = bldr.gen_sym()
        bldr.new_binop(op_add, blk2_res, rmu.MuBinOptr.ADD, i64, blk2_res1, blk2_res2)
        blk2_op_ret = bldr.gen_sym()
        bldr.new_ret(blk2_op_ret, [blk2_res])
        bldr.new_bb(blk2, [blk2_k], [i64], rmu.MU_NO_ID,
                    [op_sub_1, op_call_1, op_sub_2, op_call_2, op_add, blk2_op_ret])
        bldr.new_func_ver(v1, fib, [blk0, blk1, blk2])

        return {
            "@i64": i64,
            "test_fnc_sig": sig_i64_i64,
            "test_fnc": fib,
            "result_type": i64
        }

570
    (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, None, 'fib', [ctypes.c_longlong])
571 572

    mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
573

574
    assert fnp(20) == 6765
575 576


577
@may_spawn_proc
578
def test_new():
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    def build_test_bundle(bldr, rmu):
        """
        Builds the following test bundle.
            .typedef @i64 = int<64>
            .typedef @refi64 = ref<@i64>
            .const @1_i64 <@i64> = 1
            .const @NULL_refi64 <@refi64> = NULL
            .funcsig @sig__i64 = () -> (@i64)
            .funcdef @test_fnc VERSION @test_fnc.v1 <@sig__i64> {
                %blk0():
                    %r = NEW <@i64>
                    %ir = GETIREF <@refi64> %r
                    STORE <@i64> %ir @1_i64
                    %res = LOAD <@i64> %ir
                    RET %res
            }
        :type bldr: rpython.rlib.rmu.MuIRBuilder
        :type rmu: rpython.rlib.rmu_fast
        :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID)
        """
        i1 = bldr.gen_sym("@i1")
        bldr.new_type_int(i1, 1)
        i64 = bldr.gen_sym("@i64")
        bldr.new_type_int(i64, 64)
        refi64 = bldr.gen_sym("@refi64")
        bldr.new_type_ref(refi64, i64)

        c_1_i64 = bldr.gen_sym("@1_64")
        bldr.new_const_int(c_1_i64, i64, 1)

        sig__i64 = bldr.gen_sym("@sig__i64")
        bldr.new_funcsig(sig__i64, [], [i64])

        test_fnc = bldr.gen_sym("@test_fnc")
        bldr.new_func(test_fnc, sig__i64)

        test_fnc_v1 = bldr.gen_sym("@test_fnc.v1")
        blk0 = bldr.gen_sym("@test_fnc.v1.blk0")
        r = bldr.gen_sym("@test_fnc.v1.blk0.r")
        ir = bldr.gen_sym("@test_fnc.v1.blk0.ir")
        res = bldr.gen_sym("@test_fnc.v1.blk0.res")
        op_new = bldr.gen_sym()
        bldr.new_new(op_new, r, i64)
        op_getiref = bldr.gen_sym()
        bldr.new_getiref(op_getiref, ir, refi64, r)
        op_store = bldr.gen_sym()
        bldr.new_store(op_store, False, rmu.MuMemOrd.NOT_ATOMIC, i64, ir, c_1_i64)
        op_load = bldr.gen_sym()
        bldr.new_load(op_load, res, False, rmu.MuMemOrd.NOT_ATOMIC, i64, ir)
        op_ret = bldr.gen_sym()
        bldr.new_ret(op_ret, [res])
        bldr.new_bb(blk0, [], [], rmu.MU_NO_ID, [op_new, op_getiref, op_store, op_load, op_ret])

        bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0])

        return {
            "test_fnc": test_fnc,
            "test_fnc_sig": sig__i64,
            "result_type": i64,
            "@i64": i64
        }

641
    (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, None, 'test_fnc')
642 643

    mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
644
    assert fnp() == 1
645 646


647
@may_spawn_proc
648
def test_new_cmpeq():
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    def build_test_bundle(bldr, rmu):
        """
        Builds the following test bundle.
            .typedef @i64 = int<64>
            .typedef @refi64 = ref<@i64>
            .const @NULL_refi64 <@refi64> = NULL
            .funcsig @sig__i64 = () -> (@i64)
            .funcdef @test_fnc VERSION @test_fnc.v1 <@sig__i64> {
                @test_fnc.v1.blk0():
                    @test_fnc.v1.blk0.r = NEW <@i64>
                    @test_fnc.v1.blk0.cmpres = EQ <@refi64> @test_fnc.v1.blk0.r @NULL_refi64
                    @@test_fnc.v1.blk0.res = ZEXT <@i1 @i64> @test_fnc.v1.blk0.cmpres
                    RET @test_fnc.v1.blk0.res
            }
        :type bldr: rpython.rlib.rmu.MuIRBuilder
        :type rmu: rpython.rlib.rmu_fast
        :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID)
        """
        i1 = bldr.gen_sym("@i1")
        bldr.new_type_int(i1, 1)
        i64 = bldr.gen_sym("@i64")
        bldr.new_type_int(i64, 64)
        refi64 = bldr.gen_sym("@refi64")
        bldr.new_type_ref(refi64, i64)

        NULL_refi64 = bldr.gen_sym("@NULL_refi64")
        bldr.new_const_null(NULL_refi64, refi64)

        sig__i64 = bldr.gen_sym("@sig__i64")
        bldr.new_funcsig(sig__i64, [], [i64])

        test_fnc = bldr.gen_sym("@test_fnc")
        bldr.new_func(test_fnc, sig__i64)

        test_fnc_v1 = bldr.gen_sym("@test_fnc.v1")
        blk0 = bldr.gen_sym("@test_fnc.v1.blk0")
        r = bldr.gen_sym("@test_fnc.v1.blk0.r")
        cmpres = bldr.gen_sym("@test_fnc.v1.blk0.cmpres")
        res = bldr.gen_sym("@test_fnc.v1.blk0.res")
        op_new = bldr.gen_sym()
        bldr.new_new(op_new, r, i64)
        op_eq = bldr.gen_sym()
        bldr.new_cmp(op_eq, cmpres, rmu.MuCmpOptr.EQ, refi64, r, NULL_refi64)
        op_zext = bldr.gen_sym()
        bldr.new_conv(op_zext, res, rmu.MuConvOptr.ZEXT, i1, i64, cmpres)
        op_ret = bldr.gen_sym()
        bldr.new_ret(op_ret, [res])
        bldr.new_bb(blk0, [], [], rmu.MU_NO_ID, [op_new, op_eq, op_zext, op_ret])

        bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0])

        return {
            "test_fnc": test_fnc,
            "test_fnc_sig": sig__i64,
            "result_type": i64,
            "@i64": i64
        }

707
    (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, None, 'test_fnc')
708 709

    mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
710 711
    assert fnp() == 0

John Zhang's avatar
John Zhang committed
712

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 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 827 828 829 830 831 832 833 834 835 836 837 838
@may_spawn_proc
def test_throw():
    def build_test_bundle(bldr, rmu):
        """
        Builds the following test bundle.
            .typedef @i64 = int<64>
            .typedef @void = void
            .typedef @refi64 = ref<@i64>
            .typedef @refvoid = ref<@void>
            .const @c_10 <@i64> = 10
            .const @c_20 <@i64> = 20
            .const @c_42 <@i64> = 42
            .funcsig @sig_i64_i64 = (@i64) -> (@i64)
            .funcdef @throw_fnc VERSION @throw_fnc.v1 <@sig_i64_i64> {
                %blk0(<@i64> %num):
                    %cmpres = SLT <@i64> %num @c_42
                    BRANCH2 %cmpres %blk1() %blk2()
                %blk1():
                    %excobj = NEW <@i64>
                    %iref_obj = GETIREF <@i64> %excobj
                    STORE <@i64> %iref_obj @c_20
                    THROW %excobj
                %blk2():
                    RET (@c_10)
            }
            .funcdef @test_fnc VERSION @test_fnc.v1 <@sig_i64_i64> {
                %blk0(<@i64> %num):
                    %res = CALL <@sig_i64_i64> @throw_fnc (%num) EXC(%blk1(%res) %blk2())
                %blk1(<@i64> %rtn):
                    RET %rtn
                %blk2()[%excobj]:
                    %ri64 = REFCAST <@refvoid @refi64> %excobj
                    %iri64 = GETIREF <@i64> %excobj
                    %obj = LOAD <@i64> %iri64
                    BRANCH %blk1(%obj)
            }
        :type bldr: rpython.rlib.rmu.MuIRBuilder
        :type rmu: rpython.rlib.rmu
        :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID)
        """
        void = bldr.gen_sym("@void"); bldr.new_type_void(void)
        i64 = bldr.gen_sym("@i64"); bldr.new_type_int(i64, 64)
        refi64 = bldr.gen_sym("@refi64"); bldr.new_type_ref(refi64, i64)
        refvoid = bldr.gen_sym("@refvoid"); bldr.new_type_ref(refvoid, void)

        c_10 = bldr.gen_sym("@c_10"); bldr.new_const_int(c_10, i64, 10)
        c_20 = bldr.gen_sym("@c_20"); bldr.new_const_int(c_20, i64, 20)
        c_42 = bldr.gen_sym("@c_42"); bldr.new_const_int(c_42, i64, 42)

        sig_i64_i64 = bldr.gen_sym("@sig_i64_i64"); bldr.new_funcsig(sig_i64_i64, [i64], [i64])

        test_fnc = bldr.gen_sym("@test_fnc"); bldr.new_func(test_fnc, sig_i64_i64)
        throw_fnc = bldr.gen_sym("@throw_fnc"); bldr.new_func(throw_fnc, sig_i64_i64)

        throw_fnc_v1 = bldr.gen_sym("@throw_fnc.v1")
        blk0 = bldr.gen_sym("@throw_fnc.v1.blk0")
        blk1 = bldr.gen_sym("@throw_fnc.v1.blk1")
        blk2 = bldr.gen_sym("@throw_fnc.v1.blk2")

        # blk0
        num = bldr.gen_sym("@throw_fnc.v1.blk0.num")
        cmpres = bldr.gen_sym("@throw_fnc.v1.blk0.cmpres")
        op_slt = bldr.gen_sym(); bldr.new_cmp(op_slt, cmpres, rmu.MuCmpOptr.SLT, i64, num, c_42)
        dst_t = bldr.gen_sym(); bldr.new_dest_clause(dst_t, blk1, [])
        dst_f = bldr.gen_sym(); bldr.new_dest_clause(dst_f, blk2, [])
        op_br2 = bldr.gen_sym(); bldr.new_branch2(op_br2, cmpres, dst_t, dst_f)
        bldr.new_bb(blk0, [num], [i64], rmu.MU_NO_ID, [op_slt, op_br2])

        # blk1
        excobj = bldr.gen_sym("@throw_fnc.v1.blk1.excobj")
        iref_obj = bldr.gen_sym("@throw_fnc.v1.blk1.iref_obj")
        op_new = bldr.gen_sym(); bldr.new_new(op_new, excobj, i64)
        op_getiref = bldr.gen_sym(); bldr.new_getiref(op_getiref, iref_obj, i64, excobj)
        op_store = bldr.gen_sym(); bldr.new_store(op_store, False, rmu.MuMemOrd.NOT_ATOMIC, i64, iref_obj, c_20)
        op_throw = bldr.gen_sym(); bldr.new_throw(op_throw, excobj)
        bldr.new_bb(blk1, [], [], rmu.MU_NO_ID, [op_new, op_getiref, op_store, op_throw])

        # blk2
        op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [c_10])
        bldr.new_bb(blk2, [], [], rmu.MU_NO_ID, [op_ret])

        bldr.new_func_ver(throw_fnc_v1, throw_fnc, [blk0, blk1, blk2])

        test_fnc_v1 = bldr.gen_sym("@test_fnc.v1")
        blk0 = bldr.gen_sym("@test_fnc.v1.blk0")
        blk1 = bldr.gen_sym("@test_fnc.v1.blk1")
        blk2 = bldr.gen_sym("@test_fnc.v1.blk2")

        # blk0
        num = bldr.gen_sym("@test_fnc.v1.blk0.num")
        res = bldr.gen_sym("@test_fnc.v1.blk0.res")
        dst_nor = bldr.gen_sym(); bldr.new_dest_clause(dst_nor, blk1, [res])
        dst_exc = bldr.gen_sym(); bldr.new_dest_clause(dst_exc, blk2, [])
        exc = bldr.gen_sym(); bldr.new_exc_clause(exc, dst_nor, dst_exc)
        op_call = bldr.gen_sym(); bldr.new_call(op_call, [res], sig_i64_i64, throw_fnc, [num], exc)
        bldr.new_bb(blk0, [num], [i64], rmu.MU_NO_ID, [op_call])

        # blk1
        rtn = bldr.gen_sym("@test_fnc.v1.blk1.rtn")
        op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [rtn])
        bldr.new_bb(blk1, [rtn], [i64], rmu.MU_NO_ID, [op_ret])

        # blk2
        excobj = bldr.gen_sym("@test_fnc.v1.blk2.excobj")
        ri64 = bldr.gen_sym("@test_fnc.v1.blk2.ri64")
        iri64 = bldr.gen_sym("@test_fnc.v1.blk2.iri64")
        obj = bldr.gen_sym("@test_fnc.v1.blk2.obj")
        op_refcast = bldr.gen_sym(); bldr.new_conv(op_refcast, ri64, rmu.MuConvOptr.REFCAST, refvoid, refi64, excobj)
        op_getiref = bldr.gen_sym(); bldr.new_getiref(op_getiref, iri64, i64, ri64)
        op_load = bldr.gen_sym(); bldr.new_load(op_load, obj, False, rmu.MuMemOrd.NOT_ATOMIC, i64, iri64)
        dst = bldr.gen_sym(); bldr.new_dest_clause(dst, blk1, [obj])
        op_br = bldr.gen_sym(); bldr.new_branch(op_br, dst)
        bldr.new_bb(blk2, [], [], excobj, [op_refcast, op_getiref, op_load, op_br])

        bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0, blk1, blk2])

        return {
            "test_fnc": test_fnc,
            "test_fnc_sig": sig_i64_i64,
            "result_type": i64,
            "@i64": i64,
            "@refi64": refi64
        }

    from rpython.rlib import rmu_fast as rmu
    (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, None, 'test_fnc', [ctypes.c_int64],
839
                                                      ctypes.c_int64, mode=ctypes.RTLD_GLOBAL)
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
    assert fnp(0) == 20
    assert fnp(100) == 10


@may_spawn_proc
def test_exception_stack_unwind():
    from rpython.rlib import rmu_fast as rmu
    def build_test_bundle(bldr, rmu):
        """
        Builds the following test bundle.
            .typedef @i64 = int<64>
            .typedef @void = void
            .typedef @refi64 = ref<@i64>
            .typedef @refvoid = ref<@void>
            .const @c_10 <@i64> = 10
            .const @c_20 <@i64> = 20
            .const @c_42 <@i64> = 42
            .funcsig @sig_i64_i64 = (@i64) -> (@i64)
            .funcdef @throw_fnc VERSION @throw_fnc.v1 <@sig_i64_i64> {
                %blk0(<@i64> %num):
                    %cmpres = SLT <@i64> %num @c_42
                    BRANCH2 %cmpres %blk1() %blk2()
                %blk1():
                    %excobj = NEW <@i64>
                    %iref_obj = GETIREF <@i64> %excobj
                    STORE <@i64> %iref_obj @c_20
                    THROW %excobj
                %blk2():
                    RET (@c_10)
            }
            .funcdef @intermediate_fnc VERSION @intermediate_fnc.v1 <@sig_i64_i64> {
                %blk0(<@i64> %num):
                    %res = CALL <@sig_i64_i64> @throw_fnc (%num)
                    RET (%res)
            }
            .funcdef @test_fnc VERSION @test_fnc.v1 <@sig_i64_i64> {
                %blk0(<@i64> %num):
                    %res = CALL <@sig_i64_i64> @intermediate_fnc (%num) EXC(%blk1(%res) %blk2())
                %blk1(<@i64> %rtn):
                    RET %rtn
                %blk2()[%excobj]:
                    %ri64 = REFCAST <@refvoid @refi64> %excobj
                    %iri64 = GETIREF <@i64> %excobj
                    %obj = LOAD <@i64> %iri64
                    BRANCH %blk1(%obj)
            }
        :type bldr: rpython.rlib.rmu.MuIRBuilder
        :type rmu: rpython.rlib.rmu
        :return: (rmu.MuVM(), rmu.MuCtx, rmu.MuIRBuilder, MuID, MuID)
        """
        void = bldr.gen_sym("@void"); bldr.new_type_void(void)
        i64 = bldr.gen_sym("@i64"); bldr.new_type_int(i64, 64)
        refi64 = bldr.gen_sym("@refi64"); bldr.new_type_ref(refi64, i64)
        refvoid = bldr.gen_sym("@refvoid"); bldr.new_type_ref(refvoid, void)

        c_10 = bldr.gen_sym("@c_10"); bldr.new_const_int(c_10, i64, 10)
        c_20 = bldr.gen_sym("@c_20"); bldr.new_const_int(c_20, i64, 20)
        c_42 = bldr.gen_sym("@c_42"); bldr.new_const_int(c_42, i64, 42)

        sig_i64_i64 = bldr.gen_sym("@sig_i64_i64"); bldr.new_funcsig(sig_i64_i64, [i64], [i64])

        test_fnc = bldr.gen_sym("@test_fnc"); bldr.new_func(test_fnc, sig_i64_i64)
        throw_fnc = bldr.gen_sym("@throw_fnc"); bldr.new_func(throw_fnc, sig_i64_i64)
        intermediate_fnc = bldr.gen_sym("@intermediate_fnc"); bldr.new_func(intermediate_fnc, sig_i64_i64)

        throw_fnc_v1 = bldr.gen_sym("@throw_fnc.v1")
        blk0 = bldr.gen_sym("@throw_fnc.v1.blk0")
        blk1 = bldr.gen_sym("@throw_fnc.v1.blk1")
        blk2 = bldr.gen_sym("@throw_fnc.v1.blk2")

        # blk0
        num = bldr.gen_sym("@throw_fnc.v1.blk0.num")
        cmpres = bldr.gen_sym("@throw_fnc.v1.blk0.cmpres")
        op_slt = bldr.gen_sym(); bldr.new_cmp(op_slt, cmpres, rmu.MuCmpOptr.SLT, i64, num, c_42)
        dst_t = bldr.gen_sym(); bldr.new_dest_clause(dst_t, blk1, [])
        dst_f = bldr.gen_sym(); bldr.new_dest_clause(dst_f, blk2, [])
        op_br2 = bldr.gen_sym(); bldr.new_branch2(op_br2, cmpres, dst_t, dst_f)
        bldr.new_bb(blk0, [num], [i64], rmu.MU_NO_ID, [op_slt, op_br2])

        # blk1
        excobj = bldr.gen_sym("@throw_fnc.v1.blk1.excobj")
        iref_obj = bldr.gen_sym("@throw_fnc.v1.blk1.iref_obj")
        op_new = bldr.gen_sym(); bldr.new_new(op_new, excobj, i64)
        op_getiref = bldr.gen_sym(); bldr.new_getiref(op_getiref, iref_obj, i64, excobj)
        op_store = bldr.gen_sym(); bldr.new_store(op_store, False, rmu.MuMemOrd.NOT_ATOMIC, i64, iref_obj, c_20)
        op_throw = bldr.gen_sym(); bldr.new_throw(op_throw, excobj)
        bldr.new_bb(blk1, [], [], rmu.MU_NO_ID, [op_new, op_getiref, op_store, op_throw])

        # blk2
        op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [c_10])
        bldr.new_bb(blk2, [], [], rmu.MU_NO_ID, [op_ret])

        bldr.new_func_ver(throw_fnc_v1, throw_fnc, [blk0, blk1, blk2])

        intermediate_fnc_v1 = bldr.gen_sym("@intermediate_fnc.v1")
        blk0 = bldr.gen_sym("@intermediate_fnc.v1.blk0")
        num = bldr.gen_sym("@intermediate_fnc.v1.blk0.num")
        res = bldr.gen_sym("@intermediate_fnc.v1.blk0.res")
        op_call = bldr.gen_sym(); bldr.new_call(op_call, [res], sig_i64_i64, throw_fnc, [num])
        op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [res])
        bldr.new_bb(blk0, [num], [i64], rmu.MU_NO_ID, [op_call, op_ret])

        bldr.new_func_ver(intermediate_fnc_v1, intermediate_fnc, [blk0])

        test_fnc_v1 = bldr.gen_sym("@test_fnc.v1")
        blk0 = bldr.gen_sym("@test_fnc.v1.blk0")
        blk1 = bldr.gen_sym("@test_fnc.v1.blk1")
        blk2 = bldr.gen_sym("@test_fnc.v1.blk2")

        # blk0
        num = bldr.gen_sym("@test_fnc.v1.blk0.num")
        res = bldr.gen_sym("@test_fnc.v1.blk0.res")
        dst_nor = bldr.gen_sym(); bldr.new_dest_clause(dst_nor, blk1, [res])
        dst_exc = bldr.gen_sym(); bldr.new_dest_clause(dst_exc, blk2, [])
        exc = bldr.gen_sym(); bldr.new_exc_clause(exc, dst_nor, dst_exc)
        op_call = bldr.gen_sym(); bldr.new_call(op_call, [res], sig_i64_i64, intermediate_fnc, [num], exc)
        bldr.new_bb(blk0, [num], [i64], rmu.MU_NO_ID, [op_call])

        # blk1
        rtn = bldr.gen_sym("@test_fnc.v1.blk1.rtn")
        op_ret = bldr.gen_sym(); bldr.new_ret(op_ret, [rtn])
        bldr.new_bb(blk1, [rtn], [i64], rmu.MU_NO_ID, [op_ret])

        # blk2
        excobj = bldr.gen_sym("@test_fnc.v1.blk2.excobj")
        ri64 = bldr.gen_sym("@test_fnc.v1.blk2.ri64")
        iri64 = bldr.gen_sym("@test_fnc.v1.blk2.iri64")
        obj = bldr.gen_sym("@test_fnc.v1.blk2.obj")
        op_refcast = bldr.gen_sym(); bldr.new_conv(op_refcast, ri64, rmu.MuConvOptr.REFCAST, refvoid, refi64, excobj)
        op_getiref = bldr.gen_sym(); bldr.new_getiref(op_getiref, iri64, i64, ri64)
        op_load = bldr.gen_sym(); bldr.new_load(op_load, obj, False, rmu.MuMemOrd.NOT_ATOMIC, i64, iri64)
        dst = bldr.gen_sym(); bldr.new_dest_clause(dst, blk1, [obj])
        op_br = bldr.gen_sym(); bldr.new_branch(op_br, dst)
        bldr.new_bb(blk2, [], [], excobj, [op_refcast, op_getiref, op_load, op_br])

        bldr.new_func_ver(test_fnc_v1, test_fnc, [blk0, blk1, blk2])

        return {
            "test_fnc": test_fnc,
            "test_fnc_sig": sig_i64_i64,
            "result_type": i64,
            "@i64": i64,
            "@refi64": refi64
        }
    (fnp, _), (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, None, 'test_fnc', [ctypes.c_int64],
986
                                                      ctypes.c_int64, mode=ctypes.RTLD_GLOBAL)
987 988 989 990 991
    mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
    assert fnp(0) == 20
    assert fnp(100) == 10


992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
@may_spawn_proc
def test_make_boot_image_simple():
    c_printf = rffi.llexternal('printf', [rffi.CCHARP], rffi.INT, _nowrapper=True)
    c_putchar = rffi.llexternal('putchar', [rffi.CHAR], rffi.INT, _nowrapper=True)
    def pypy_mu_entry(argc, argv):
        for i in range(argc):
            c_printf(argv[i])
            c_putchar('\n')
        return 0

    from rpython.translator.interactive import Translation
    t = Translation(pypy_mu_entry, [rffi.INT, rffi.CCHARPP],
                    backend='mu', muimpl='fast', mucodegen='api')
    t.driver.standalone = True  # force standalone
John Zhang's avatar
John Zhang committed
1006
    t.driver.exe_name = '/tmp/test_make_boot_image_%(backend)s'
1007 1008
    t.driver.disable(['entrypoint_mu'])
    t.compile_mu()
John Zhang's avatar
John Zhang committed
1009
    exe = py.path.local('/tmp/test_make_boot_image_mu.mu')
1010

1011
    # zebu
1012 1013
    import os
    from rpython.translator.mu import dir_mu
1014
    exe.chmod(stat.S_IRWXU)
1015 1016
    res = platform.execute(str(exe), ['abc', '123'],
                           env={'DYLD_LIBRARY_PATH': os.path.join(dir_mu, 'rpyc')})
1017 1018 1019 1020 1021 1022
    # holstein
    # res = platform.execute('/Users/johnz/Documents/Work/mu-impl-ref2/tools/runmu.sh',
    #                      ['--vmLog=ERROR', str(exe), 'abc', '123'])
    assert res.returncode == 0, res.err
    assert res.out == '%s\nabc\n123\n' % exe

qinsoon's avatar
qinsoon committed
1023
@pytest.mark.xfail(reason = "doesnt work for now")
1024 1025 1026 1027 1028 1029 1030 1031
@may_spawn_proc
def test_rpytarget_print_argv():
    from rpython.translator.interactive import Translation

    def main(argv):
        print argv
        return 0

John Zhang's avatar
John Zhang committed
1032
    t = Translation(main, None, backend='mu', muimpl='fast', mucodegen='api')
1033 1034 1035 1036 1037
    t.driver.exe_name = '/tmp/test_printargv_%(backend)s'
    t.compile_mu()
    exe = py.path.local('/tmp/test_printargv_mu.mu')

    # zebu
1038 1039
    import os
    from rpython.translator.mu import dir_mu
1040
    exe.chmod(stat.S_IRWXU)
1041 1042
    res = platform.execute(str(exe), ['abc', '123'],
                           env={'DYLD_LIBRARY_PATH': os.path.join(dir_mu, 'rpyc')})
1043 1044 1045 1046 1047 1048
    # holstein
    # res = platform.execute('/Users/johnz/Documents/Work/mu-impl-ref2/tools/runmu.sh',
    #                      ['--vmLog=ERROR', str(exe), 'abc', '123'])
    assert res.returncode == 0, res.err
    assert res.out == '[%s, abc, 123]\n' % exe

qinsoon's avatar
qinsoon committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
@pytest.mark.xfail(reason = "probably need to align frame size to 16 bytes")
@may_spawn_proc
def test_rpython_helloworld():
    from rpython.translator.interactive import Translation

    def main(argv):
	print "hello world"
	return 0

    t = Translation(main, None, backend='mu', muimpl='fast', mucodegen='api')
    t.driver.exe_name = '/tmp/test_helloworld'
    t.compile_mu()
    exe = py.path.local('/tmp/test_helloworld.mu')

    # zebu
    import os
    from rpython.translator.mu import dir_mu
    exe.chmod(stat.S_IRWXU)
    res = platform.execute(str(exe), [], env={'DYLD_LIBRARY_PATH': os.path.join(dir_mu, 'rpyc')})

    assert res.returncode == 0, res.err
    assert res.out == 'hello world\n'

1072
@pytest.mark.skipif("True")
John Zhang's avatar
John Zhang committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
@may_spawn_proc
def test_rpytarget_sha1sum():
    john1 = \
'''
In the beginning was the Word, and the Word was with God, and the Word was God.
He was in the beginning with God.
All things were made through him, and without him was not any thing made that was made.
In him was life, and the life was the light of men.
The light shines in the darkness, and the darkness has not overcome it.
'''
    from rpython.translator.goal.targetsha1sum import entry_point
    from rpython.translator.interactive import Translation
    t = Translation(entry_point, None,
                    backend='mu', muimpl='fast', mucodegen='api')
    t.driver.standalone = True  # force standalone
    t.driver.exe_name = '/tmp/test_sha1sum_%(backend)s'
    t.compile_mu()
    exe = py.path.local('/tmp/test_sha1sum_mu.mu')
    test_file = py.path.local('/tmp/john1.txt')
    with test_file.open('w') as fp:
        fp.write(john1)
1094

John Zhang's avatar
John Zhang committed
1095
    # zebu
1096 1097
    import os
    from rpython.translator.mu import dir_mu
John Zhang's avatar
John Zhang committed
1098
    exe.chmod(stat.S_IRWXU)
1099 1100
    res = platform.execute(str(exe), [str(test_file)],
                           env={'DYLD_LIBRARY_PATH': os.path.join(dir_mu, 'rpyc')})
John Zhang's avatar
John Zhang committed
1101 1102 1103 1104 1105 1106
    # holstein
    # res = platform.execute('/Users/johnz/Documents/Work/mu-impl-ref2/tools/runmu.sh',
    #                      ['--vmLog=ERROR', str(exe), str(test_file)])
    assert res.returncode == 0, res.err
    assert res.out == '53b45a7e3fb6ccb2d9e43c45cb57b6b56c784def /tmp/john1.txt\n'

John Zhang's avatar
John Zhang committed
1107 1108 1109 1110 1111 1112 1113
if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('testfnc', help="Test function name")
    opts = parser.parse_args()

    globals()[opts.testfnc]()