WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.6% of users enabled 2FA.

zebu_c.py 68.7 KB
Newer Older
John Zhang's avatar
John Zhang committed
1 2 3 4 5 6 7 8
"""
Mu API RPython binding with C backend.
This file is auto-generated and then added a few minor modifications.
NOTE: THIS FILE IS *NOT* RPYTHON.
"""

from rpython.rtyper.lltypesystem import rffi
from rpython.rtyper.lltypesystem import lltype
9
from rpython.tool.udir import udir
10 11
import os

John Zhang's avatar
John Zhang committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

class CCall(object):
    __slots__ = ('fnc_name', 'args', 'rtn_var', 'context', 'check_err')

    def __init__(self, fnc_name, args, rtn_var, context=None, check_err=True):
        self.fnc_name = fnc_name
        self.args = args
        self.rtn_var = rtn_var
        self.context = context
        self.check_err = check_err

    def __str__(self):
        s = '{rtn_stm}{ctx}{fnc}({arg_lst})'.format(rtn_stm='%s = ' % self.rtn_var if self.rtn_var else '',
                                                    fnc=self.fnc_name,
                                                    arg_lst=', '.join(map(str, self.args)),
                                                    ctx='%s->' % self.context if self.context else '')
        if self.check_err:
            s = "CHECK(%s)" % s
        else:
            s = s + ";"
        return s

    __repr__ = __str__

class CStr(object):
    __slots__ = ('string', )

    def __init__(self, string):
        self.string = string

    def __str__(self):
        return '"%s"' % self.string

    def __len__(self):
        return len(self.string) - self.string.count('\\n')

    __repr__ = __str__

NULL = 'NULL'

class CArrayConst(object):
    def __init__(self, c_elm_t, lst):
        self.c_elm_t = c_elm_t
        self.lst = lst

    def __str__(self):
        if self.c_elm_t == 'MuCString':
John Zhang's avatar
John Zhang committed
59 60
            return '({type}){value}'.format(type='char*[%d]' % len(self.lst),
                                            value='{%s}' % ', '.join(map(lambda s: str(CStr(s)), self.lst)))
John Zhang's avatar
John Zhang committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        else:
            return '({type}){value}'.format(type='%s [%d]' % (self.c_elm_t, len(self.lst)),
                                            value='{%s}' % ', '.join(map(str, self.lst)))

    __repr__ = __str__

class CFloatConst(object):
    def __init__(self, flt, ctype_str):
        self.flt = flt
        self.c_type = ctype_str

    def __str__(self):
        c_flt_t = self.c_type
        c_int_t = 'uint64_t' if c_flt_t == 'double' else 'uint32_t'
        repr_str = ftohstr(self.flt, self.c_type)
        return '*(%(c_flt_t)s*)(%(c_int_t)s [1]){%(repr_str)s}' % locals()

class CIntConst(object):
    def __init__(self, i, ctype_str):
        self.i = i
        self.c_type = ctype_str

    def __str__(self):
        return itohstr(self.i, self.c_type)

class CLibNameConst(object):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return "LIB_FILE_NAME(%s)" % self.name

class CVar(object):
94 95 96 97
    __slots__ = ('type', 'idx')
    _var_ctr = {}
    _array_dic = {}
    _named_vars = set()
John Zhang's avatar
John Zhang committed
98

99
    def __init__(self, c_type, name):
John Zhang's avatar
John Zhang committed
100
        self.type = c_type
101 102 103
        if c_type.endswith('*'):
            self.idx = name
            CVar._named_vars.add(self)
John Zhang's avatar
John Zhang committed
104
        else:
105 106 107 108 109 110 111 112 113
            if c_type in CVar._var_ctr:
                n = CVar._var_ctr[c_type]
                n += 1
            else:
                n = 1
            CVar._var_ctr[c_type] = n
            self.idx = n - 1
            if c_type not in CVar._array_dic:
                CVar._array_dic[c_type] = 'arr' + c_type
John Zhang's avatar
John Zhang committed
114 115

    def __str__(self):
116 117 118
        if isinstance(self.idx, str):
            return self.idx
        return '%s[%d]' % (CVar._array_dic[self.type], self.idx)
John Zhang's avatar
John Zhang committed
119 120
    __repr__ = __str__

121

John Zhang's avatar
John Zhang committed
122 123 124 125 126 127 128 129 130 131 132 133
class CFuncPtr(CVar):
    def __init__(self, arg_ts, ret_t, var_name):
        self.arg_ts = arg_ts
        self.ret_t = ret_t
        self.name = var_name

    def decl(self):
        return '%s (*%s) (%s);' % (self.ret_t, self.name, ', '.join(self.arg_ts))

class APILogger:
    def __init__(self):
        self.ccalls = []
134 135 136 137
        self.c_dir = udir.join('mu_c_src')

    def logcall(self, fnc_name, args, rtn_var, context=None, check_err=True):
        self.ccalls.append(CCall(fnc_name, args, rtn_var, context, check_err))
John Zhang's avatar
John Zhang committed
138 139 140 141

    def clear(self):
        APILogger.__init__(self)

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 195 196 197 198 199 200 201 202 203 204 205
    def gen_c_files(self):
        MAX_FUNC_SIZE = 5000    # 5000 lines of calls
        done = 0
        ctr = 0
        while done < len(self.ccalls):
            calls = self.ccalls[done:done + MAX_FUNC_SIZE]

            c_file = self.c_dir.join('file_%d.c' % ctr)
            with c_file.open('w') as fp:
                fp.write('#include "header.h"\n')
                fp.write('void fnc_%d () {\n' % ctr)
                for ccall in calls:
                    fp.write('    ' + '%(ccall)s\n' % locals())
                fp.write('}\n')

            done += len(calls)
            ctr += 1

        return ctr

    def gen_header(self, num_c_funcs):
        header_file = self.c_dir.join('header.h')
        with header_file.open('w') as fp:
            fp.write('#ifndef PYPY_MU_C_SRC_HEADER\n'
                     '#define PYPY_MU_C_SRC_HEADER\n')
            fp.write('\n'
                     '#include <stdio.h>\n'
                     '#include <stdlib.h>\n'
                     '#include <stdbool.h>\n'
                     '#include <dlfcn.h>\n'
                     '#include "muapi.h"\n'
                     '#include "mu-fastimpl.h"\n'
                     '#ifdef __APPLE__\n'
                     '    #define LIB_EXT ".dylib"\n'
                     '#elif __linux__\n'
                     '    #define LIB_EXT ".so"\n'
                     '#elif _WIN32\n'
                     '    #define LIB_EXT ".dll"\n'
                     '#endif\n'
                     '#define LIB_FILE_NAME(name) "lib" name LIB_EXT\n')

            fp.write('\n'
                     '#define CHECK(line) line; \n')

            # declare named variables
            for var in CVar._named_vars:
                fp.write('%s %s;\n' % (var.type, var.idx))
            # declare arrays
            for ty, arr in CVar._array_dic.items():
                fp.write('%(ty)s *%(arr)s;\n' % locals())

            # declare build function slices
            for i in range(num_c_funcs):
                fp.write('void fnc_%d();\n' % i)

            fp.write('#endif\n')

    def genc(self, target_base_name=''):
        if not self.c_dir.exists():
            self.c_dir.mkdir()

        num_c_files = self.gen_c_files()
        self.gen_header(num_c_files)
        self.gen_main(num_c_files)
206
        return self.gen_makefile(num_c_files, target_base_name)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    def gen_main(self, num_c_files):
        with self.c_dir.join('main.c').open('w') as fp:
            fp.write('#include "header.h"\n')
            fp.write('int main(int argc, char** argv) {\n')
            idt = ' ' * 4
            for ty, count in CVar._var_ctr.items():
                arr = CVar._array_dic[ty]
                fp.write(idt + '%(arr)s = (%(ty)s*)malloc(sizeof(%(ty)s) * %(count)d);\n' % locals())

            for i in range(num_c_files):
                fp.write(idt + 'fnc_%d();\n' % i)

            fp.write(idt + 'return 0;\n')
            fp.write('}\n')

    def gen_makefile(self, num_c_files, tbn):
224 225 226
        from rpython.translator.platform import platform
        from rpython.translator.platform.posix import GnuMakefile
        mf = GnuMakefile(self.c_dir.strpath)
227
        target = tbn + '_build'
228 229 230
        defs = [
            ('OBJECTS', ' '.join(['file_%d.o' % i for i in range(num_c_files)]) + ' main.o'),
            ('INCLUDEDIRS', '-I$(MU_ZEBU)/src/vm/api'),
231
            ('LIBDIRS', '-L$(MU_ZEBU)/target/%s' % os.environ.get('ZEBU_BUILD', 'debug')),
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            ('LIBS', '-lmu'),
            ('TARGET', target),
            ('CC', platform.cc),
        ]
        for d in defs:
            mf.definition(*d)

        rules = [
            ('all', '$(TARGET)', []),
            ('%.o', '%.c', '$(CC) $(CFLAGS) -o $@ -c $< $(INCLUDEDIRS)'),
            ('$(TARGET)', '$(OBJECTS)', '$(CC) -o $@ $(OBJECTS) $(LIBDIRS) $(LIBS)')
        ]
        for r in rules:
            mf.rule(*r)

        mf.write()
        return mf, self.c_dir.join(target)
249

John Zhang's avatar
John Zhang committed
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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 570 571 572 573 574 575 576 577 578 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 641 642 643 644 645 646 647 648 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 707 708 709 710 711 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 839 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 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 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 1176 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 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
_apilog = APILogger()
def get_global_apilogger():
    return _apilog

# -------------------------------------------------------------------------------------------------------
# Type definitions
MuValue = rffi.VOIDP
MuValuePtr = rffi.CArrayPtr(MuValue)
MuSeqValue = MuValue
MuSeqValuePtr = rffi.CArrayPtr(MuSeqValue)
MuGenRefValue = MuValue
MuGenRefValuePtr = rffi.CArrayPtr(MuGenRefValue)
MuIntValue = MuValue
MuIntValuePtr = rffi.CArrayPtr(MuIntValue)
MuFloatValue = MuValue
MuFloatValuePtr = rffi.CArrayPtr(MuFloatValue)
MuDoubleValue = MuValue
MuDoubleValuePtr = rffi.CArrayPtr(MuDoubleValue)
MuUPtrValue = MuValue
MuUPtrValuePtr = rffi.CArrayPtr(MuUPtrValue)
MuUFPValue = MuValue
MuUFPValuePtr = rffi.CArrayPtr(MuUFPValue)
MuStructValue = MuValue
MuStructValuePtr = rffi.CArrayPtr(MuStructValue)
MuArrayValue = MuSeqValue
MuArrayValuePtr = rffi.CArrayPtr(MuArrayValue)
MuVectorValue = MuSeqValue
MuVectorValuePtr = rffi.CArrayPtr(MuVectorValue)
MuRefValue = MuGenRefValue
MuRefValuePtr = rffi.CArrayPtr(MuRefValue)
MuIRefValue = MuGenRefValue
MuIRefValuePtr = rffi.CArrayPtr(MuIRefValue)
MuTagRef64Value = MuGenRefValue
MuTagRef64ValuePtr = rffi.CArrayPtr(MuTagRef64Value)
MuFuncRefValue = MuGenRefValue
MuFuncRefValuePtr = rffi.CArrayPtr(MuFuncRefValue)
MuThreadRefValue = MuGenRefValue
MuThreadRefValuePtr = rffi.CArrayPtr(MuThreadRefValue)
MuStackRefValue = MuGenRefValue
MuStackRefValuePtr = rffi.CArrayPtr(MuStackRefValue)
MuFCRefValue = MuGenRefValue
MuFCRefValuePtr = rffi.CArrayPtr(MuFCRefValue)
MuIBRefValue = MuGenRefValue
MuIBRefValuePtr = rffi.CArrayPtr(MuIBRefValue)
MuCString = rffi.CCHARP
MuCStringPtr = rffi.CArrayPtr(MuCString)
MuID = rffi.UINT
MuIDPtr = rffi.CArrayPtr(MuID)
MuName = MuCString
MuNamePtr = rffi.CArrayPtr(MuName)
MuCPtr = rffi.VOIDP
MuCPtrPtr = rffi.CArrayPtr(MuCPtr)
MuCFP = rffi.VOIDP
MuCFPPtr = rffi.CArrayPtr(MuCFP)
MuBool = rffi.INT
MuBoolPtr = rffi.CArrayPtr(MuBool)
MuArraySize = rffi.UINTPTR_T
MuArraySizePtr = rffi.CArrayPtr(MuArraySize)
MuWPID = rffi.UINT
MuWPIDPtr = rffi.CArrayPtr(MuWPID)
MuFlag = rffi.UINT
MuFlagPtr = rffi.CArrayPtr(MuFlag)
MuTrapHandlerResult = MuFlag
MuTrapHandlerResultPtr = rffi.CArrayPtr(MuTrapHandlerResult)
MuBinOpStatus = MuFlag
MuBinOpStatusPtr = rffi.CArrayPtr(MuBinOpStatus)
MuTypeNode = MuID
MuTypeNodePtr = rffi.CArrayPtr(MuTypeNode)
MuFuncSigNode = MuID
MuFuncSigNodePtr = rffi.CArrayPtr(MuFuncSigNode)
MuVarNode = MuID
MuVarNodePtr = rffi.CArrayPtr(MuVarNode)
MuGlobalVarNode = MuID
MuGlobalVarNodePtr = rffi.CArrayPtr(MuGlobalVarNode)
MuLocalVarNode = MuID
MuLocalVarNodePtr = rffi.CArrayPtr(MuLocalVarNode)
MuConstNode = MuID
MuConstNodePtr = rffi.CArrayPtr(MuConstNode)
MuFuncNode = MuID
MuFuncNodePtr = rffi.CArrayPtr(MuFuncNode)
MuFuncVerNode = MuID
MuFuncVerNodePtr = rffi.CArrayPtr(MuFuncVerNode)
MuBBNode = MuID
MuBBNodePtr = rffi.CArrayPtr(MuBBNode)
MuInstNode = MuID
MuInstNodePtr = rffi.CArrayPtr(MuInstNode)
MuDestClause = MuID
MuDestClausePtr = rffi.CArrayPtr(MuDestClause)
MuExcClause = MuID
MuExcClausePtr = rffi.CArrayPtr(MuExcClause)
MuKeepaliveClause = MuID
MuKeepaliveClausePtr = rffi.CArrayPtr(MuKeepaliveClause)
MuCurStackClause = MuID
MuCurStackClausePtr = rffi.CArrayPtr(MuCurStackClause)
MuNewStackClause = MuID
MuNewStackClausePtr = rffi.CArrayPtr(MuNewStackClause)
_MuVM = lltype.ForwardReference()
_MuVMPtr = lltype.Ptr(_MuVM)
_MuCtx = lltype.ForwardReference()
_MuCtxPtr = lltype.Ptr(_MuCtx)
_MuIRBuilder = lltype.ForwardReference()
_MuIRBuilderPtr = lltype.Ptr(_MuIRBuilder)
MuValuesFreer = rffi.CCallback([MuValuePtr, MuCPtr], lltype.Void)
MuValuesFreerPtr = rffi.CArrayPtr(MuValuesFreer)
MuTrapHandler = rffi.CCallback([
    _MuCtxPtr, MuThreadRefValue, MuStackRefValue, MuWPID,   # input
    MuTrapHandlerResultPtr, MuStackRefValuePtr, rffi.CArray(MuValuePtr), MuArraySizePtr,
    MuValuesFreerPtr, MuCPtrPtr, MuRefValuePtr,             # output
    MuCPtr  #input
], lltype.Void)
MuTrapHandlerPtr = rffi.CArrayPtr(MuTrapHandler)

# -------------------------------------------------------------------------------------------------------
# Flags
class MuTrapHandlerResult:
    THREAD_EXIT = "MU_THREAD_EXIT"
    REBIND_PASS_VALUES = "MU_REBIND_PASS_VALUES"
    REBIND_THROW_EXC = "MU_REBIND_THROW_EXC"
class MuBinOpStatus:
    N = "MU_BOS_N"
    Z = "MU_BOS_Z"
    C = "MU_BOS_C"
    V = "MU_BOS_V"
class MuBinOptr:
    ADD = "MU_BINOP_ADD"
    SUB = "MU_BINOP_SUB"
    MUL = "MU_BINOP_MUL"
    SDIV = "MU_BINOP_SDIV"
    SREM = "MU_BINOP_SREM"
    UDIV = "MU_BINOP_UDIV"
    UREM = "MU_BINOP_UREM"
    SHL = "MU_BINOP_SHL"
    LSHR = "MU_BINOP_LSHR"
    ASHR = "MU_BINOP_ASHR"
    AND = "MU_BINOP_AND"
    OR = "MU_BINOP_OR"
    XOR = "MU_BINOP_XOR"
    FADD = "MU_BINOP_FADD"
    FSUB = "MU_BINOP_FSUB"
    FMUL = "MU_BINOP_FMUL"
    FDIV = "MU_BINOP_FDIV"
    FREM = "MU_BINOP_FREM"
class MuCmpOptr:
    EQ = "MU_CMP_EQ"
    NE = "MU_CMP_NE"
    SGE = "MU_CMP_SGE"
    SGT = "MU_CMP_SGT"
    SLE = "MU_CMP_SLE"
    SLT = "MU_CMP_SLT"
    UGE = "MU_CMP_UGE"
    UGT = "MU_CMP_UGT"
    ULE = "MU_CMP_ULE"
    ULT = "MU_CMP_ULT"
    FFALSE = "MU_CMP_FFALSE"
    FTRUE = "MU_CMP_FTRUE"
    FUNO = "MU_CMP_FUNO"
    FUEQ = "MU_CMP_FUEQ"
    FUNE = "MU_CMP_FUNE"
    FUGT = "MU_CMP_FUGT"
    FUGE = "MU_CMP_FUGE"
    FULT = "MU_CMP_FULT"
    FULE = "MU_CMP_FULE"
    FORD = "MU_CMP_FORD"
    FOEQ = "MU_CMP_FOEQ"
    FONE = "MU_CMP_FONE"
    FOGT = "MU_CMP_FOGT"
    FOGE = "MU_CMP_FOGE"
    FOLT = "MU_CMP_FOLT"
    FOLE = "MU_CMP_FOLE"
class MuConvOptr:
    TRUNC = "MU_CONV_TRUNC"
    ZEXT = "MU_CONV_ZEXT"
    SEXT = "MU_CONV_SEXT"
    FPTRUNC = "MU_CONV_FPTRUNC"
    FPEXT = "MU_CONV_FPEXT"
    FPTOUI = "MU_CONV_FPTOUI"
    FPTOSI = "MU_CONV_FPTOSI"
    UITOFP = "MU_CONV_UITOFP"
    SITOFP = "MU_CONV_SITOFP"
    BITCAST = "MU_CONV_BITCAST"
    REFCAST = "MU_CONV_REFCAST"
    PTRCAST = "MU_CONV_PTRCAST"
class MuMemOrd:
    NOT_ATOMIC = "MU_ORD_NOT_ATOMIC"
    RELAXED = "MU_ORD_RELAXED"
    CONSUME = "MU_ORD_CONSUME"
    ACQUIRE = "MU_ORD_ACQUIRE"
    RELEASE = "MU_ORD_RELEASE"
    ACQ_REL = "MU_ORD_ACQ_REL"
    SEQ_CST = "MU_ORD_SEQ_CST"
class MuAtomicRMWOptr:
    XCHG = "MU_ARMW_XCHG"
    ADD = "MU_ARMW_ADD"
    SUB = "MU_ARMW_SUB"
    AND = "MU_ARMW_AND"
    NAND = "MU_ARMW_NAND"
    OR = "MU_ARMW_OR"
    XOR = "MU_ARMW_XOR"
    MAX = "MU_ARMW_MAX"
    MIN = "MU_ARMW_MIN"
    UMAX = "MU_ARMW_UMAX"
    UMIN = "MU_ARMW_UMIN"
class MuCallConv:
    DEFAULT = "MU_CC_DEFAULT"
class MuCommInst:
    NEW_STACK = "MU_CI_UVM_NEW_STACK"
    KILL_STACK = "MU_CI_UVM_KILL_STACK"
    THREAD_EXIT = "MU_CI_UVM_THREAD_EXIT"
    CURRENT_STACK = "MU_CI_UVM_CURRENT_STACK"
    SET_THREADLOCAL = "MU_CI_UVM_SET_THREADLOCAL"
    GET_THREADLOCAL = "MU_CI_UVM_GET_THREADLOCAL"
    TR64_IS_FP = "MU_CI_UVM_TR64_IS_FP"
    TR64_IS_INT = "MU_CI_UVM_TR64_IS_INT"
    TR64_IS_REF = "MU_CI_UVM_TR64_IS_REF"
    TR64_FROM_FP = "MU_CI_UVM_TR64_FROM_FP"
    TR64_FROM_INT = "MU_CI_UVM_TR64_FROM_INT"
    TR64_FROM_REF = "MU_CI_UVM_TR64_FROM_REF"
    TR64_TO_FP = "MU_CI_UVM_TR64_TO_FP"
    TR64_TO_INT = "MU_CI_UVM_TR64_TO_INT"
    TR64_TO_REF = "MU_CI_UVM_TR64_TO_REF"
    TR64_TO_TAG = "MU_CI_UVM_TR64_TO_TAG"
    FUTEX_WAIT = "MU_CI_UVM_FUTEX_WAIT"
    FUTEX_WAIT_TIMEOUT = "MU_CI_UVM_FUTEX_WAIT_TIMEOUT"
    FUTEX_WAKE = "MU_CI_UVM_FUTEX_WAKE"
    FUTEX_CMP_REQUEUE = "MU_CI_UVM_FUTEX_CMP_REQUEUE"
    KILL_DEPENDENCY = "MU_CI_UVM_KILL_DEPENDENCY"
    NATIVE_PIN = "MU_CI_UVM_NATIVE_PIN"
    NATIVE_UNPIN = "MU_CI_UVM_NATIVE_UNPIN"
    NATIVE_GET_ADDR = "MU_CI_UVM_NATIVE_GET_ADDR"
    NATIVE_EXPOSE = "MU_CI_UVM_NATIVE_EXPOSE"
    NATIVE_UNEXPOSE = "MU_CI_UVM_NATIVE_UNEXPOSE"
    NATIVE_GET_COOKIE = "MU_CI_UVM_NATIVE_GET_COOKIE"
    META_ID_OF = "MU_CI_UVM_META_ID_OF"
    META_NAME_OF = "MU_CI_UVM_META_NAME_OF"
    META_LOAD_BUNDLE = "MU_CI_UVM_META_LOAD_BUNDLE"
    META_LOAD_HAIL = "MU_CI_UVM_META_LOAD_HAIL"
    META_NEW_CURSOR = "MU_CI_UVM_META_NEW_CURSOR"
    META_NEXT_FRAME = "MU_CI_UVM_META_NEXT_FRAME"
    META_COPY_CURSOR = "MU_CI_UVM_META_COPY_CURSOR"
    META_CLOSE_CURSOR = "MU_CI_UVM_META_CLOSE_CURSOR"
    META_CUR_FUNC = "MU_CI_UVM_META_CUR_FUNC"
    META_CUR_FUNC_VER = "MU_CI_UVM_META_CUR_FUNC_VER"
    META_CUR_INST = "MU_CI_UVM_META_CUR_INST"
    META_DUMP_KEEPALIVES = "MU_CI_UVM_META_DUMP_KEEPALIVES"
    META_POP_FRAMES_TO = "MU_CI_UVM_META_POP_FRAMES_TO"
    META_PUSH_FRAME = "MU_CI_UVM_META_PUSH_FRAME"
    META_ENABLE_WATCHPOINT = "MU_CI_UVM_META_ENABLE_WATCHPOINT"
    META_DISABLE_WATCHPOINT = "MU_CI_UVM_META_DISABLE_WATCHPOINT"
    META_SET_TRAP_HANDLER = "MU_CI_UVM_META_SET_TRAP_HANDLER"
    IRBUILDER_NEW_IR_BUILDER = "MU_CI_UVM_IRBUILDER_NEW_IR_BUILDER"
    IRBUILDER_LOAD = "MU_CI_UVM_IRBUILDER_LOAD"
    IRBUILDER_ABORT = "MU_CI_UVM_IRBUILDER_ABORT"
    IRBUILDER_GEN_SYM = "MU_CI_UVM_IRBUILDER_GEN_SYM"
    IRBUILDER_NEW_TYPE_INT = "MU_CI_UVM_IRBUILDER_NEW_TYPE_INT"
    IRBUILDER_NEW_TYPE_FLOAT = "MU_CI_UVM_IRBUILDER_NEW_TYPE_FLOAT"
    IRBUILDER_NEW_TYPE_DOUBLE = "MU_CI_UVM_IRBUILDER_NEW_TYPE_DOUBLE"
    IRBUILDER_NEW_TYPE_UPTR = "MU_CI_UVM_IRBUILDER_NEW_TYPE_UPTR"
    IRBUILDER_NEW_TYPE_UFUNCPTR = "MU_CI_UVM_IRBUILDER_NEW_TYPE_UFUNCPTR"
    IRBUILDER_NEW_TYPE_STRUCT = "MU_CI_UVM_IRBUILDER_NEW_TYPE_STRUCT"
    IRBUILDER_NEW_TYPE_HYBRID = "MU_CI_UVM_IRBUILDER_NEW_TYPE_HYBRID"
    IRBUILDER_NEW_TYPE_ARRAY = "MU_CI_UVM_IRBUILDER_NEW_TYPE_ARRAY"
    IRBUILDER_NEW_TYPE_VECTOR = "MU_CI_UVM_IRBUILDER_NEW_TYPE_VECTOR"
    IRBUILDER_NEW_TYPE_VOID = "MU_CI_UVM_IRBUILDER_NEW_TYPE_VOID"
    IRBUILDER_NEW_TYPE_REF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_REF"
    IRBUILDER_NEW_TYPE_IREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_IREF"
    IRBUILDER_NEW_TYPE_WEAKREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_WEAKREF"
    IRBUILDER_NEW_TYPE_FUNCREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_FUNCREF"
    IRBUILDER_NEW_TYPE_TAGREF64 = "MU_CI_UVM_IRBUILDER_NEW_TYPE_TAGREF64"
    IRBUILDER_NEW_TYPE_THREADREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_THREADREF"
    IRBUILDER_NEW_TYPE_STACKREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_STACKREF"
    IRBUILDER_NEW_TYPE_FRAMECURSORREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_FRAMECURSORREF"
    IRBUILDER_NEW_TYPE_IRBUILDERREF = "MU_CI_UVM_IRBUILDER_NEW_TYPE_IRBUILDERREF"
    IRBUILDER_NEW_FUNCSIG = "MU_CI_UVM_IRBUILDER_NEW_FUNCSIG"
    IRBUILDER_NEW_CONST_INT = "MU_CI_UVM_IRBUILDER_NEW_CONST_INT"
    IRBUILDER_NEW_CONST_INT_EX = "MU_CI_UVM_IRBUILDER_NEW_CONST_INT_EX"
    IRBUILDER_NEW_CONST_FLOAT = "MU_CI_UVM_IRBUILDER_NEW_CONST_FLOAT"
    IRBUILDER_NEW_CONST_DOUBLE = "MU_CI_UVM_IRBUILDER_NEW_CONST_DOUBLE"
    IRBUILDER_NEW_CONST_NULL = "MU_CI_UVM_IRBUILDER_NEW_CONST_NULL"
    IRBUILDER_NEW_CONST_SEQ = "MU_CI_UVM_IRBUILDER_NEW_CONST_SEQ"
    IRBUILDER_NEW_CONST_EXTERN = "MU_CI_UVM_IRBUILDER_NEW_CONST_EXTERN"
    IRBUILDER_NEW_GLOBAL_CELL = "MU_CI_UVM_IRBUILDER_NEW_GLOBAL_CELL"
    IRBUILDER_NEW_FUNC = "MU_CI_UVM_IRBUILDER_NEW_FUNC"
    IRBUILDER_NEW_EXP_FUNC = "MU_CI_UVM_IRBUILDER_NEW_EXP_FUNC"
    IRBUILDER_NEW_FUNC_VER = "MU_CI_UVM_IRBUILDER_NEW_FUNC_VER"
    IRBUILDER_NEW_BB = "MU_CI_UVM_IRBUILDER_NEW_BB"
    IRBUILDER_NEW_DEST_CLAUSE = "MU_CI_UVM_IRBUILDER_NEW_DEST_CLAUSE"
    IRBUILDER_NEW_EXC_CLAUSE = "MU_CI_UVM_IRBUILDER_NEW_EXC_CLAUSE"
    IRBUILDER_NEW_KEEPALIVE_CLAUSE = "MU_CI_UVM_IRBUILDER_NEW_KEEPALIVE_CLAUSE"
    IRBUILDER_NEW_CSC_RET_WITH = "MU_CI_UVM_IRBUILDER_NEW_CSC_RET_WITH"
    IRBUILDER_NEW_CSC_KILL_OLD = "MU_CI_UVM_IRBUILDER_NEW_CSC_KILL_OLD"
    IRBUILDER_NEW_NSC_PASS_VALUES = "MU_CI_UVM_IRBUILDER_NEW_NSC_PASS_VALUES"
    IRBUILDER_NEW_NSC_THROW_EXC = "MU_CI_UVM_IRBUILDER_NEW_NSC_THROW_EXC"
    IRBUILDER_NEW_BINOP = "MU_CI_UVM_IRBUILDER_NEW_BINOP"
    IRBUILDER_NEW_BINOP_WITH_STATUS = "MU_CI_UVM_IRBUILDER_NEW_BINOP_WITH_STATUS"
    IRBUILDER_NEW_CMP = "MU_CI_UVM_IRBUILDER_NEW_CMP"
    IRBUILDER_NEW_CONV = "MU_CI_UVM_IRBUILDER_NEW_CONV"
    IRBUILDER_NEW_SELECT = "MU_CI_UVM_IRBUILDER_NEW_SELECT"
    IRBUILDER_NEW_BRANCH = "MU_CI_UVM_IRBUILDER_NEW_BRANCH"
    IRBUILDER_NEW_BRANCH2 = "MU_CI_UVM_IRBUILDER_NEW_BRANCH2"
    IRBUILDER_NEW_SWITCH = "MU_CI_UVM_IRBUILDER_NEW_SWITCH"
    IRBUILDER_NEW_CALL = "MU_CI_UVM_IRBUILDER_NEW_CALL"
    IRBUILDER_NEW_TAILCALL = "MU_CI_UVM_IRBUILDER_NEW_TAILCALL"
    IRBUILDER_NEW_RET = "MU_CI_UVM_IRBUILDER_NEW_RET"
    IRBUILDER_NEW_THROW = "MU_CI_UVM_IRBUILDER_NEW_THROW"
    IRBUILDER_NEW_EXTRACTVALUE = "MU_CI_UVM_IRBUILDER_NEW_EXTRACTVALUE"
    IRBUILDER_NEW_INSERTVALUE = "MU_CI_UVM_IRBUILDER_NEW_INSERTVALUE"
    IRBUILDER_NEW_EXTRACTELEMENT = "MU_CI_UVM_IRBUILDER_NEW_EXTRACTELEMENT"
    IRBUILDER_NEW_INSERTELEMENT = "MU_CI_UVM_IRBUILDER_NEW_INSERTELEMENT"
    IRBUILDER_NEW_SHUFFLEVECTOR = "MU_CI_UVM_IRBUILDER_NEW_SHUFFLEVECTOR"
    IRBUILDER_NEW_NEW = "MU_CI_UVM_IRBUILDER_NEW_NEW"
    IRBUILDER_NEW_NEWHYBRID = "MU_CI_UVM_IRBUILDER_NEW_NEWHYBRID"
    IRBUILDER_NEW_ALLOCA = "MU_CI_UVM_IRBUILDER_NEW_ALLOCA"
    IRBUILDER_NEW_ALLOCAHYBRID = "MU_CI_UVM_IRBUILDER_NEW_ALLOCAHYBRID"
    IRBUILDER_NEW_GETIREF = "MU_CI_UVM_IRBUILDER_NEW_GETIREF"
    IRBUILDER_NEW_GETFIELDIREF = "MU_CI_UVM_IRBUILDER_NEW_GETFIELDIREF"
    IRBUILDER_NEW_GETELEMIREF = "MU_CI_UVM_IRBUILDER_NEW_GETELEMIREF"
    IRBUILDER_NEW_SHIFTIREF = "MU_CI_UVM_IRBUILDER_NEW_SHIFTIREF"
    IRBUILDER_NEW_GETVARPARTIREF = "MU_CI_UVM_IRBUILDER_NEW_GETVARPARTIREF"
    IRBUILDER_NEW_LOAD = "MU_CI_UVM_IRBUILDER_NEW_LOAD"
    IRBUILDER_NEW_STORE = "MU_CI_UVM_IRBUILDER_NEW_STORE"
    IRBUILDER_NEW_CMPXCHG = "MU_CI_UVM_IRBUILDER_NEW_CMPXCHG"
    IRBUILDER_NEW_ATOMICRMW = "MU_CI_UVM_IRBUILDER_NEW_ATOMICRMW"
    IRBUILDER_NEW_FENCE = "MU_CI_UVM_IRBUILDER_NEW_FENCE"
    IRBUILDER_NEW_TRAP = "MU_CI_UVM_IRBUILDER_NEW_TRAP"
    IRBUILDER_NEW_WATCHPOINT = "MU_CI_UVM_IRBUILDER_NEW_WATCHPOINT"
    IRBUILDER_NEW_WPBRANCH = "MU_CI_UVM_IRBUILDER_NEW_WPBRANCH"
    IRBUILDER_NEW_CCALL = "MU_CI_UVM_IRBUILDER_NEW_CCALL"
    IRBUILDER_NEW_NEWTHREAD = "MU_CI_UVM_IRBUILDER_NEW_NEWTHREAD"
    IRBUILDER_NEW_SWAPSTACK = "MU_CI_UVM_IRBUILDER_NEW_SWAPSTACK"
    IRBUILDER_NEW_COMMINST = "MU_CI_UVM_IRBUILDER_NEW_COMMINST"

MU_NO_ID = "MU_NO_ID"

# -------------------------------------------------------------------------------------------------------
# OO wrappers
class MuVM:
    def __init__(self, config_str=""):
        self._mu = CVar('MuVM*', 'mu')
        _apilog.logcall('mu_fastimpl_new_with_opts', [CStr('init_mu ' + config_str)], self._mu)

    def new_context(self):
        # type: () -> MuCtx
        res_var = CVar('MuCtx*', 'ctx')
        _apilog.logcall('new_context', [self._mu], res_var, self._mu)
        return MuCtx(res_var)

    def id_of(self, name):
        # type: (str) -> MuID
        name_cstr = CStr(name) if name else NULL
        res_var = CVar('MuID', 'id')
        _apilog.logcall('id_of', [self._mu, name_cstr], res_var, self._mu)
        return res_var

    def name_of(self, id):
        # type: (MuID) -> str
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuName', 'var')
        _apilog.logcall('name_of', [self._mu, id], res_var, self._mu)
        return res_var

    def set_trap_handler(self, trap_handler, userdata):
        # type: (MuTrapHandler, MuCPtr) -> None
        _apilog.logcall('set_trap_handler', [self._mu, trap_handler, userdata], None, self._mu)

    def compile_to_sharedlib(self, lib_name, extra_srcs):
        # type: (str, [MuCString]) -> None
        lib_name_cstr = CLibNameConst(CStr(lib_name))
        extra_srcs_arr, extra_srcs_sz = lst2arr('MuCString', extra_srcs)
        _apilog.logcall('compile_to_sharedlib', [self._mu, lib_name_cstr, extra_srcs_arr, extra_srcs_sz], None, self._mu)

    def current_thread_as_mu_thread(self, threadlocal):
        # type: (MuCPtr) -> None
        _apilog.logcall('current_thread_as_mu_thread', [self._mu, threadlocal], None, self._mu)


class MuCtx:
    def __init__(self, ctx_var):
        self._ctx = ctx_var
    def id_of(self, name):
        # type: (str) -> MuID
        name_cstr = CStr(name) if name else NULL
        res_var = CVar('MuID', 'id')
        _apilog.logcall('id_of', [self._ctx, name_cstr], res_var, self._ctx)
        return res_var

    def name_of(self, id):
        # type: (MuID) -> str
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuName', 'var')
        _apilog.logcall('name_of', [self._ctx, id], res_var, self._ctx)
        return res_var

    def close_context(self):
        # type: () -> None
        _apilog.logcall('close_context', [self._ctx], None, self._ctx)

    def load_bundle(self, buf):
        # type: (str) -> None
        buf_cstr = CStr(buf) if buf else NULL
        sz = len(buf_cstr)
        _apilog.logcall('load_bundle', [self._ctx, buf_cstr, sz], None, self._ctx)

    def load_hail(self, buf):
        # type: (str) -> None
        buf_cstr = CStr(buf) if buf else NULL
        sz = len(buf_cstr)
        _apilog.logcall('load_hail', [self._ctx, buf_cstr, sz], None, self._ctx)

    def handle_from_sint8(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'int8_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_sint8', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_uint8(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'uint8_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_uint8', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_sint16(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'int16_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_sint16', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_uint16(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'uint16_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_uint16', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_sint32(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'int32_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_sint32', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_uint32(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'uint32_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_uint32', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_sint64(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'int64_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_sint64', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_uint64(self, num, len):
        # type: (int, int) -> MuIntValue
        num_int = CIntConst(num, 'uint64_t')
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_uint64', [self._ctx, num_int, len_int], res_var, self._ctx)
        return res_var

    def handle_from_uint64s(self, nums, len):
        # type: ([rffi.ULONG], int) -> MuIntValue
        nums_arr, nums_sz = lst2arr('uint64_t', nums)
        len_int = CIntConst(len, 'int')
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('handle_from_uint64s', [self._ctx, nums_arr, nums_sz, len_int], res_var, self._ctx)
        return res_var

    def handle_from_float(self, num):
        # type: (float) -> MuFloatValue
        num_cflt = CFloatConst(num, 'float')
        res_var = CVar('MuFloatValue', 'hfltval')
        _apilog.logcall('handle_from_float', [self._ctx, num_cflt], res_var, self._ctx)
        return res_var

    def handle_from_double(self, num):
        # type: (float) -> MuDoubleValue
        num_cflt = CFloatConst(num, 'double')
        res_var = CVar('MuDoubleValue', 'hdblval')
        _apilog.logcall('handle_from_double', [self._ctx, num_cflt], res_var, self._ctx)
        return res_var

    def handle_from_ptr(self, mu_type, ptr):
        # type: (MuID, MuCPtr) -> MuUPtrValue
        res_var = CVar('MuUPtrValue', 'huptrval')
        _apilog.logcall('handle_from_ptr', [self._ctx, mu_type, ptr], res_var, self._ctx)
        return res_var

    def handle_from_fp(self, mu_type, fp):
        # type: (MuID, MuCFP) -> MuUFPValue
        res_var = CVar('MuUFPValue', 'hufpval')
        _apilog.logcall('handle_from_fp', [self._ctx, mu_type, fp], res_var, self._ctx)
        return res_var

    def handle_to_sint8(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('int8_t', 'var')
        _apilog.logcall('handle_to_sint8', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_uint8(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('uint8_t', 'var')
        _apilog.logcall('handle_to_uint8', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_sint16(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('int16_t', 'var')
        _apilog.logcall('handle_to_sint16', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_uint16(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('uint16_t', 'var')
        _apilog.logcall('handle_to_uint16', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_sint32(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('int32_t', 'var')
        _apilog.logcall('handle_to_sint32', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_uint32(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('uint32_t', 'var')
        _apilog.logcall('handle_to_uint32', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_sint64(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('int64_t', 'var')
        _apilog.logcall('handle_to_sint64', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_uint64(self, opnd):
        # type: (MuIntValue) -> int
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('uint64_t', 'var')
        _apilog.logcall('handle_to_uint64', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_float(self, opnd):
        # type: (MuFloatValue) -> float
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('float', 'var')
        _apilog.logcall('handle_to_float', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_double(self, opnd):
        # type: (MuDoubleValue) -> float
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('double', 'var')
        _apilog.logcall('handle_to_double', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_ptr(self, opnd):
        # type: (MuUPtrValue) -> MuCPtr
        res_var = CVar('MuCPtr', 'var')
        _apilog.logcall('handle_to_ptr', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_to_fp(self, opnd):
        # type: (MuUFPValue) -> MuCFP
        res_var = CVar('MuCFP', 'var')
        _apilog.logcall('handle_to_fp', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def handle_from_const(self, id):
        # type: (MuID) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('handle_from_const', [self._ctx, id], res_var, self._ctx)
        return res_var

    def handle_from_global(self, id):
        # type: (MuID) -> MuIRefValue
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('handle_from_global', [self._ctx, id], res_var, self._ctx)
        return res_var

    def handle_from_func(self, id):
        # type: (MuID) -> MuFuncRefValue
        res_var = CVar('MuFuncRefValue', 'hfncref')
        _apilog.logcall('handle_from_func', [self._ctx, id], res_var, self._ctx)
        return res_var

    def handle_from_expose(self, id):
        # type: (MuID) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('handle_from_expose', [self._ctx, id], res_var, self._ctx)
        return res_var

    def delete_value(self, opnd):
        # type: (MuValue) -> None
        _apilog.logcall('delete_value', [self._ctx, opnd], None, self._ctx)

    def ref_eq(self, lhs, rhs):
        # type: (MuGenRefValue, MuGenRefValue) -> bool
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuBool', 'var')
        _apilog.logcall('ref_eq', [self._ctx, lhs, rhs], res_var, self._ctx)
        return res_var

    def ref_ult(self, lhs, rhs):
        # type: (MuIRefValue, MuIRefValue) -> bool
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuBool', 'var')
        _apilog.logcall('ref_ult', [self._ctx, lhs, rhs], res_var, self._ctx)
        return res_var

    def extract_value(self, str, index):
        # type: (MuStructValue, int) -> MuValue
        index_int = CIntConst(index, 'int')
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('extract_value', [self._ctx, str, index_int], res_var, self._ctx)
        return res_var

    def insert_value(self, str, index, newval):
        # type: (MuStructValue, int, MuValue) -> MuStructValue
        index_int = CIntConst(index, 'int')
        res_var = CVar('MuStructValue', 'hstt')
        _apilog.logcall('insert_value', [self._ctx, str, index_int, newval], res_var, self._ctx)
        return res_var

    def extract_element(self, str, index):
        # type: (MuSeqValue, MuIntValue) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('extract_element', [self._ctx, str, index], res_var, self._ctx)
        return res_var

    def insert_element(self, str, index, newval):
        # type: (MuSeqValue, MuIntValue, MuValue) -> MuSeqValue
        res_var = CVar('MuSeqValue', 'hseq')
        _apilog.logcall('insert_element', [self._ctx, str, index, newval], res_var, self._ctx)
        return res_var

    def new_fixed(self, mu_type):
        # type: (MuID) -> MuRefValue
        res_var = CVar('MuRefValue', 'href')
        _apilog.logcall('new_fixed', [self._ctx, mu_type], res_var, self._ctx)
        return res_var

    def new_hybrid(self, mu_type, length):
        # type: (MuID, MuIntValue) -> MuRefValue
        res_var = CVar('MuRefValue', 'href')
        _apilog.logcall('new_hybrid', [self._ctx, mu_type, length], res_var, self._ctx)
        return res_var

    def refcast(self, opnd, new_type):
        # type: (MuGenRefValue, MuID) -> MuGenRefValue
        res_var = CVar('MuGenRefValue', 'var')
        _apilog.logcall('refcast', [self._ctx, opnd, new_type], res_var, self._ctx)
        return res_var

    def get_iref(self, opnd):
        # type: (MuRefValue) -> MuIRefValue
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('get_iref', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def get_field_iref(self, opnd, field):
        # type: (MuIRefValue, int) -> MuIRefValue
        field_int = CIntConst(field, 'int')
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('get_field_iref', [self._ctx, opnd, field_int], res_var, self._ctx)
        return res_var

    def get_elem_iref(self, opnd, index):
        # type: (MuIRefValue, MuIntValue) -> MuIRefValue
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('get_elem_iref', [self._ctx, opnd, index], res_var, self._ctx)
        return res_var

    def shift_iref(self, opnd, offset):
        # type: (MuIRefValue, MuIntValue) -> MuIRefValue
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('shift_iref', [self._ctx, opnd, offset], res_var, self._ctx)
        return res_var

    def get_var_part_iref(self, opnd):
        # type: (MuIRefValue) -> MuIRefValue
        res_var = CVar('MuIRefValue', 'hiref')
        _apilog.logcall('get_var_part_iref', [self._ctx, opnd], res_var, self._ctx)
        return res_var

    def load(self, ord, loc):
        # type: (MuFlag, MuIRefValue) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('load', [self._ctx, ord, loc], res_var, self._ctx)
        return res_var

    def store(self, ord, loc, newval):
        # type: (MuFlag, MuIRefValue, MuValue) -> None
        _apilog.logcall('store', [self._ctx, ord, loc, newval], None, self._ctx)

    def cmpxchg(self, ord_succ, ord_fail, weak, loc, expected, desired, is_succ):
        # type: (MuFlag, MuFlag, bool, MuIRefValue, MuValue, MuValue, MuBoolPtr) -> MuValue
        weak_bool = 'true' if weak else 'false'
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('cmpxchg', [self._ctx, ord_succ, ord_fail, weak_bool, loc, expected, desired, is_succ], res_var, self._ctx)
        return res_var

    def atomicrmw(self, ord, op, loc, opnd):
        # type: (MuFlag, MuFlag, MuIRefValue, MuValue) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('atomicrmw', [self._ctx, ord, op, loc, opnd], res_var, self._ctx)
        return res_var

    def fence(self, ord):
        # type: (MuFlag) -> None
        _apilog.logcall('fence', [self._ctx, ord], None, self._ctx)

    def new_stack(self, func):
        # type: (MuFuncRefValue) -> MuStackRefValue
        res_var = CVar('MuStackRefValue', 'hstkref')
        _apilog.logcall('new_stack', [self._ctx, func], res_var, self._ctx)
        return res_var

    def new_thread_nor(self, stack, threadlocal, vals):
        # type: (MuStackRefValue, MuRefValue, [MuValue]) -> MuThreadRefValue
        vals_arr, vals_sz = lst2arr('MuValue', vals)
        res_var = CVar('MuThreadRefValue', 'hthdref')
        _apilog.logcall('new_thread_nor', [self._ctx, stack, threadlocal, vals_arr, vals_sz], res_var, self._ctx)
        return res_var

    def new_thread_exc(self, stack, threadlocal, exc):
        # type: (MuStackRefValue, MuRefValue, MuRefValue) -> MuThreadRefValue
        res_var = CVar('MuThreadRefValue', 'hthdref')
        _apilog.logcall('new_thread_exc', [self._ctx, stack, threadlocal, exc], res_var, self._ctx)
        return res_var

    def kill_stack(self, stack):
        # type: (MuStackRefValue) -> None
        _apilog.logcall('kill_stack', [self._ctx, stack], None, self._ctx)

    def set_threadlocal(self, thread, threadlocal):
        # type: (MuThreadRefValue, MuRefValue) -> None
        _apilog.logcall('set_threadlocal', [self._ctx, thread, threadlocal], None, self._ctx)

    def get_threadlocal(self, thread):
        # type: (MuThreadRefValue) -> MuRefValue
        res_var = CVar('MuRefValue', 'href')
        _apilog.logcall('get_threadlocal', [self._ctx, thread], res_var, self._ctx)
        return res_var

    def new_cursor(self, stack):
        # type: (MuStackRefValue) -> MuFCRefValue
        res_var = CVar('MuFCRefValue', 'hfcr')
        _apilog.logcall('new_cursor', [self._ctx, stack], res_var, self._ctx)
        return res_var

    def next_frame(self, cursor):
        # type: (MuFCRefValue) -> None
        _apilog.logcall('next_frame', [self._ctx, cursor], None, self._ctx)

    def copy_cursor(self, cursor):
        # type: (MuFCRefValue) -> MuFCRefValue
        res_var = CVar('MuFCRefValue', 'hfcr')
        _apilog.logcall('copy_cursor', [self._ctx, cursor], res_var, self._ctx)
        return res_var

    def close_cursor(self, cursor):
        # type: (MuFCRefValue) -> None
        _apilog.logcall('close_cursor', [self._ctx, cursor], None, self._ctx)

    def cur_func(self, cursor):
        # type: (MuFCRefValue) -> MuID
        res_var = CVar('MuID', 'id')
        _apilog.logcall('cur_func', [self._ctx, cursor], res_var, self._ctx)
        return res_var

    def cur_func_ver(self, cursor):
        # type: (MuFCRefValue) -> MuID
        res_var = CVar('MuID', 'id')
        _apilog.logcall('cur_func_ver', [self._ctx, cursor], res_var, self._ctx)
        return res_var

    def cur_inst(self, cursor):
        # type: (MuFCRefValue) -> MuID
        res_var = CVar('MuID', 'id')
        _apilog.logcall('cur_inst', [self._ctx, cursor], res_var, self._ctx)
        return res_var

    def dump_keepalives(self, cursor, results):
        # type: (MuFCRefValue, MuValuePtr) -> None
        _apilog.logcall('dump_keepalives', [self._ctx, cursor, results], None, self._ctx)

    def pop_frames_to(self, cursor):
        # type: (MuFCRefValue) -> None
        _apilog.logcall('pop_frames_to', [self._ctx, cursor], None, self._ctx)

    def push_frame(self, stack, func):
        # type: (MuStackRefValue, MuFuncRefValue) -> None
        _apilog.logcall('push_frame', [self._ctx, stack, func], None, self._ctx)

    def tr64_is_fp(self, value):
        # type: (MuTagRef64Value) -> bool
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuBool', 'var')
        _apilog.logcall('tr64_is_fp', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_is_int(self, value):
        # type: (MuTagRef64Value) -> bool
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuBool', 'var')
        _apilog.logcall('tr64_is_int', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_is_ref(self, value):
        # type: (MuTagRef64Value) -> bool
        # NOTE: runtime dependent method, the return value should not be examined in Python.
        res_var = CVar('MuBool', 'var')
        _apilog.logcall('tr64_is_ref', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_to_fp(self, value):
        # type: (MuTagRef64Value) -> MuDoubleValue
        res_var = CVar('MuDoubleValue', 'hdblval')
        _apilog.logcall('tr64_to_fp', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_to_int(self, value):
        # type: (MuTagRef64Value) -> MuIntValue
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('tr64_to_int', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_to_ref(self, value):
        # type: (MuTagRef64Value) -> MuRefValue
        res_var = CVar('MuRefValue', 'href')
        _apilog.logcall('tr64_to_ref', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_to_tag(self, value):
        # type: (MuTagRef64Value) -> MuIntValue
        res_var = CVar('MuIntValue', 'hintval')
        _apilog.logcall('tr64_to_tag', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_from_fp(self, value):
        # type: (MuDoubleValue) -> MuTagRef64Value
        res_var = CVar('MuTagRef64Value', 'htag')
        _apilog.logcall('tr64_from_fp', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_from_int(self, value):
        # type: (MuIntValue) -> MuTagRef64Value
        res_var = CVar('MuTagRef64Value', 'htag')
        _apilog.logcall('tr64_from_int', [self._ctx, value], res_var, self._ctx)
        return res_var

    def tr64_from_ref(self, ref, tag):
        # type: (MuRefValue, MuIntValue) -> MuTagRef64Value
        res_var = CVar('MuTagRef64Value', 'htag')
        _apilog.logcall('tr64_from_ref', [self._ctx, ref, tag], res_var, self._ctx)
        return res_var

    def enable_watchpoint(self, wpid):
        # type: (MuWPID) -> None
        _apilog.logcall('enable_watchpoint', [self._ctx, wpid], None, self._ctx)

    def disable_watchpoint(self, wpid):
        # type: (MuWPID) -> None
        _apilog.logcall('disable_watchpoint', [self._ctx, wpid], None, self._ctx)

    def pin(self, loc):
        # type: (MuValue) -> MuUPtrValue
        res_var = CVar('MuUPtrValue', 'huptrval')
        _apilog.logcall('pin', [self._ctx, loc], res_var, self._ctx)
        return res_var

    def unpin(self, loc):
        # type: (MuValue) -> None
        _apilog.logcall('unpin', [self._ctx, loc], None, self._ctx)

    def get_addr(self, loc):
        # type: (MuValue) -> MuUPtrValue
        res_var = CVar('MuUPtrValue', 'huptrval')
        _apilog.logcall('get_addr', [self._ctx, loc], res_var, self._ctx)
        return res_var

    def expose(self, func, call_conv, cookie):
        # type: (MuFuncRefValue, MuFlag, MuIntValue) -> MuValue
        res_var = CVar('MuValue', 'hdl')
        _apilog.logcall('expose', [self._ctx, func, call_conv, cookie], res_var, self._ctx)
        return res_var

    def unexpose(self, call_conv, value):
        # type: (MuFlag, MuValue) -> None
        _apilog.logcall('unexpose', [self._ctx, call_conv, value], None, self._ctx)

    def new_ir_builder(self):
        # type: () -> MuIRBuilder
        res_var = CVar('MuIRBuilder*', 'bldr')
        _apilog.logcall('new_ir_builder', [self._ctx], res_var, self._ctx)
        return MuIRBuilder(res_var)

    def make_boot_image(self, whitelist, primordial_func, primordial_stack, primordial_threadlocal, sym_fields, sym_strings, reloc_fields, reloc_strings, output_file):
        # type: ([MuID], MuFuncRefValue, MuStackRefValue, MuRefValue, [MuIRefValue], [MuCString], [MuIRefValue], [MuCString], str) -> None
        whitelist_arr, whitelist_sz = lst2arr('MuID', whitelist)
        sym_fields_arr, sym_fields_sz = lst2arr('MuIRefValue', sym_fields)
        sym_strings_arr, sym_strings_sz = lst2arr('MuCString', sym_strings)
        reloc_fields_arr, reloc_fields_sz = lst2arr('MuIRefValue', reloc_fields)
        reloc_strings_arr, reloc_strings_sz = lst2arr('MuCString', reloc_strings)
        output_file_cstr = CStr(output_file) if output_file else NULL
        _apilog.logcall('make_boot_image', [self._ctx, whitelist_arr, whitelist_sz, primordial_func, primordial_stack, primordial_threadlocal, sym_fields_arr, sym_strings_arr, sym_strings_sz, reloc_fields_arr, reloc_strings_arr, reloc_strings_sz, output_file_cstr], None, self._ctx)


class MuIRBuilder:
    def __init__(self, bldr_var):
        self._bldr = bldr_var

    def load(self):
        # type: () -> None
        _apilog.logcall('load', [self._bldr], None, self._bldr)

    def abort(self):
        # type: () -> None
        _apilog.logcall('abort', [self._bldr], None, self._bldr)

    def gen_sym(self, name=None):
        # type: (str) -> MuID
        name_cstr = CStr(name) if name else NULL
        res_var = CVar('MuID', 'id')
        _apilog.logcall('gen_sym', [self._bldr, name_cstr], res_var, self._bldr)
        return res_var

    def new_type_int(self, id, len):
        # type: (MuID, int) -> None
        len_int = CIntConst(len, 'int')
        _apilog.logcall('new_type_int', [self._bldr, id, len_int], None, self._bldr)

    def new_type_float(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_float', [self._bldr, id], None, self._bldr)

    def new_type_double(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_double', [self._bldr, id], None, self._bldr)

    def new_type_uptr(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_type_uptr', [self._bldr, id, ty], None, self._bldr)

    def new_type_ufuncptr(self, id, sig):
        # type: (MuID, MuFuncSigNode) -> None
        _apilog.logcall('new_type_ufuncptr', [self._bldr, id, sig], None, self._bldr)

    def new_type_struct(self, id, fieldtys):
        # type: (MuID, [MuTypeNode]) -> None
        fieldtys_arr, fieldtys_sz = lst2arr('MuTypeNode', fieldtys)
        _apilog.logcall('new_type_struct', [self._bldr, id, fieldtys_arr, fieldtys_sz], None, self._bldr)

    def new_type_hybrid(self, id, fixedtys, varty):
        # type: (MuID, [MuTypeNode], MuTypeNode) -> None
        fixedtys_arr, fixedtys_sz = lst2arr('MuTypeNode', fixedtys)
        _apilog.logcall('new_type_hybrid', [self._bldr, id, fixedtys_arr, fixedtys_sz, varty], None, self._bldr)

    def new_type_array(self, id, elemty, len):
        # type: (MuID, MuTypeNode, int) -> None
        len_int = CIntConst(len, 'uint64_t')
        _apilog.logcall('new_type_array', [self._bldr, id, elemty, len_int], None, self._bldr)

    def new_type_vector(self, id, elemty, len):
        # type: (MuID, MuTypeNode, int) -> None
        len_int = CIntConst(len, 'uint64_t')
        _apilog.logcall('new_type_vector', [self._bldr, id, elemty, len_int], None, self._bldr)

    def new_type_void(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_void', [self._bldr, id], None, self._bldr)

    def new_type_ref(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_type_ref', [self._bldr, id, ty], None, self._bldr)

    def new_type_iref(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_type_iref', [self._bldr, id, ty], None, self._bldr)

    def new_type_weakref(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_type_weakref', [self._bldr, id, ty], None, self._bldr)

    def new_type_funcref(self, id, sig):
        # type: (MuID, MuFuncSigNode) -> None
        _apilog.logcall('new_type_funcref', [self._bldr, id, sig], None, self._bldr)

    def new_type_tagref64(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_tagref64', [self._bldr, id], None, self._bldr)

    def new_type_threadref(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_threadref', [self._bldr, id], None, self._bldr)

    def new_type_stackref(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_stackref', [self._bldr, id], None, self._bldr)

    def new_type_framecursorref(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_framecursorref', [self._bldr, id], None, self._bldr)

    def new_type_irbuilderref(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_type_irbuilderref', [self._bldr, id], None, self._bldr)

    def new_funcsig(self, id, paramtys, rettys):
        # type: (MuID, [MuTypeNode], [MuTypeNode]) -> None
        paramtys_arr, paramtys_sz = lst2arr('MuTypeNode', paramtys)
        rettys_arr, rettys_sz = lst2arr('MuTypeNode', rettys)
        _apilog.logcall('new_funcsig', [self._bldr, id, paramtys_arr, paramtys_sz, rettys_arr, rettys_sz], None, self._bldr)

    def new_const_int(self, id, ty, value):
        # type: (MuID, MuTypeNode, int) -> None
        value_int = CIntConst(value, 'uint64_t')
        _apilog.logcall('new_const_int', [self._bldr, id, ty, value_int], None, self._bldr)

    def new_const_int_ex(self, id, ty, values):
        # type: (MuID, MuTypeNode, [rffi.ULONG]) -> None
        values_arr, values_sz = lst2arr('uint64_t', values)
        _apilog.logcall('new_const_int_ex', [self._bldr, id, ty, values_arr, values_sz], None, self._bldr)

    def new_const_float(self, id, ty, value):
        # type: (MuID, MuTypeNode, float) -> None
        value_cflt = CFloatConst(value, 'float')
        _apilog.logcall('new_const_float', [self._bldr, id, ty, value_cflt], None, self._bldr)

    def new_const_double(self, id, ty, value):
        # type: (MuID, MuTypeNode, float) -> None
        value_cflt = CFloatConst(value, 'double')
        _apilog.logcall('new_const_double', [self._bldr, id, ty, value_cflt], None, self._bldr)

    def new_const_null(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_const_null', [self._bldr, id, ty], None, self._bldr)

    def new_const_seq(self, id, ty, elems):
        # type: (MuID, MuTypeNode, [MuGlobalVarNode]) -> None
        elems_arr, elems_sz = lst2arr('MuGlobalVarNode', elems)
        _apilog.logcall('new_const_seq', [self._bldr, id, ty, elems_arr, elems_sz], None, self._bldr)

    def new_const_extern(self, id, ty, symbol):
        # type: (MuID, MuTypeNode, str) -> None
        symbol_cstr = CStr(symbol) if symbol else NULL
        _apilog.logcall('new_const_extern', [self._bldr, id, ty, symbol_cstr], None, self._bldr)

    def new_global_cell(self, id, ty):
        # type: (MuID, MuTypeNode) -> None
        _apilog.logcall('new_global_cell', [self._bldr, id, ty], None, self._bldr)

    def new_func(self, id, sig):
        # type: (MuID, MuFuncSigNode) -> None
        _apilog.logcall('new_func', [self._bldr, id, sig], None, self._bldr)

    def new_exp_func(self, id, func, callconv, cookie):
        # type: (MuID, MuFuncNode, MuFlag, MuConstNode) -> None
        _apilog.logcall('new_exp_func', [self._bldr, id, func, callconv, cookie], None, self._bldr)

    def new_func_ver(self, id, func, bbs):
        # type: (MuID, MuFuncNode, [MuBBNode]) -> None
        bbs_arr, bbs_sz = lst2arr('MuBBNode', bbs)
        _apilog.logcall('new_func_ver', [self._bldr, id, func, bbs_arr, bbs_sz], None, self._bldr)

    def new_bb(self, id, nor_param_ids, nor_param_types, exc_param_id, insts):
        # type: (MuID, [MuID], [MuTypeNode], MuID, [MuInstNode]) -> None
        nor_param_ids_arr, nor_param_ids_sz = lst2arr('MuID', nor_param_ids)
        nor_param_types_arr, nor_param_types_sz = lst2arr('MuTypeNode', nor_param_types)
        insts_arr, insts_sz = lst2arr('MuInstNode', insts)
        _apilog.logcall('new_bb', [self._bldr, id, nor_param_ids_arr, nor_param_types_arr, nor_param_types_sz, exc_param_id, insts_arr, insts_sz], None, self._bldr)

    def new_dest_clause(self, id, dest, vars):
        # type: (MuID, MuBBNode, [MuVarNode]) -> None
        vars_arr, vars_sz = lst2arr('MuVarNode', vars)
        _apilog.logcall('new_dest_clause', [self._bldr, id, dest, vars_arr, vars_sz], None, self._bldr)

    def new_exc_clause(self, id, nor, exc):
        # type: (MuID, MuDestClause, MuDestClause) -> None
        _apilog.logcall('new_exc_clause', [self._bldr, id, nor, exc], None, self._bldr)

    def new_keepalive_clause(self, id, vars):
        # type: (MuID, [MuLocalVarNode]) -> None
        vars_arr, vars_sz = lst2arr('MuLocalVarNode', vars)
        _apilog.logcall('new_keepalive_clause', [self._bldr, id, vars_arr, vars_sz], None, self._bldr)

    def new_csc_ret_with(self, id, rettys):
        # type: (MuID, [MuTypeNode]) -> None
        rettys_arr, rettys_sz = lst2arr('MuTypeNode', rettys)
        _apilog.logcall('new_csc_ret_with', [self._bldr, id, rettys_arr, rettys_sz], None, self._bldr)

    def new_csc_kill_old(self, id):
        # type: (MuID) -> None
        _apilog.logcall('new_csc_kill_old', [self._bldr, id], None, self._bldr)

    def new_nsc_pass_values(self, id, tys, vars):
        # type: (MuID, [MuTypeNode], [MuVarNode]) -> None
        tys_arr, tys_sz = lst2arr('MuTypeNode', tys)
        vars_arr, vars_sz = lst2arr('MuVarNode', vars)
        _apilog.logcall('new_nsc_pass_values', [self._bldr, id, tys_arr, vars_arr, vars_sz], None, self._bldr)

    def new_nsc_throw_exc(self, id, exc):
        # type: (MuID, MuVarNode) -> None
        _apilog.logcall('new_nsc_throw_exc', [self._bldr, id, exc], None, self._bldr)

    def new_binop(self, id, result_id, optr, ty, opnd1, opnd2, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuFlag, MuTypeNode, MuVarNode, MuVarNode, MuExcClause) -> None
        _apilog.logcall('new_binop', [self._bldr, id, result_id, optr, ty, opnd1, opnd2, exc_clause], None, self._bldr)

    def new_binop_with_status(self, id, result_id, status_result_ids, optr, status_flags, ty, opnd1, opnd2, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, [MuID], MuFlag, MuBinOpStatus, MuTypeNode, MuVarNode, MuVarNode, MuExcClause) -> None
        status_result_ids_arr, status_result_ids_sz = lst2arr('MuID', status_result_ids)
        _apilog.logcall('new_binop_with_status', [self._bldr, id, result_id, status_result_ids_arr, status_result_ids_sz, optr, status_flags, ty, opnd1, opnd2, exc_clause], None, self._bldr)

    def new_cmp(self, id, result_id, optr, ty, opnd1, opnd2):
        # type: (MuID, MuID, MuFlag, MuTypeNode, MuVarNode, MuVarNode) -> None
        _apilog.logcall('new_cmp', [self._bldr, id, result_id, optr, ty, opnd1, opnd2], None, self._bldr)

    def new_conv(self, id, result_id, optr, from_ty, to_ty, opnd):
        # type: (MuID, MuID, MuFlag, MuTypeNode, MuTypeNode, MuVarNode) -> None
        _apilog.logcall('new_conv', [self._bldr, id, result_id, optr, from_ty, to_ty, opnd], None, self._bldr)

    def new_select(self, id, result_id, cond_ty, opnd_ty, cond, if_true, if_false):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode, MuVarNode) -> None
        _apilog.logcall('new_select', [self._bldr, id, result_id, cond_ty, opnd_ty, cond, if_true, if_false], None, self._bldr)

    def new_branch(self, id, dest):
        # type: (MuID, MuDestClause) -> None
        _apilog.logcall('new_branch', [self._bldr, id, dest], None, self._bldr)

    def new_branch2(self, id, cond, if_true, if_false):
        # type: (MuID, MuVarNode, MuDestClause, MuDestClause) -> None
        _apilog.logcall('new_branch2', [self._bldr, id, cond, if_true, if_false], None, self._bldr)

    def new_switch(self, id, opnd_ty, opnd, default_dest, cases, dests):
        # type: (MuID, MuTypeNode, MuVarNode, MuDestClause, [MuConstNode], [MuDestClause]) -> None
        cases_arr, cases_sz = lst2arr('MuConstNode', cases)
        dests_arr, dests_sz = lst2arr('MuDestClause', dests)
        _apilog.logcall('new_switch', [self._bldr, id, opnd_ty, opnd, default_dest, cases_arr, dests_arr, dests_sz], None, self._bldr)

    def new_call(self, id, result_ids, sig, callee, args, exc_clause=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, [MuID], MuFuncSigNode, MuVarNode, [MuVarNode], MuExcClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        args_arr, args_sz = lst2arr('MuVarNode', args)
        _apilog.logcall('new_call', [self._bldr, id, result_ids_arr, result_ids_sz, sig, callee, args_arr, args_sz, exc_clause, keepalive_clause], None, self._bldr)

    def new_tailcall(self, id, sig, callee, args):
        # type: (MuID, MuFuncSigNode, MuVarNode, [MuVarNode]) -> None
        args_arr, args_sz = lst2arr('MuVarNode', args)
        _apilog.logcall('new_tailcall', [self._bldr, id, sig, callee, args_arr, args_sz], None, self._bldr)

    def new_ret(self, id, rvs):
        # type: (MuID, [MuVarNode]) -> None
        rvs_arr, rvs_sz = lst2arr('MuVarNode', rvs)
        _apilog.logcall('new_ret', [self._bldr, id, rvs_arr, rvs_sz], None, self._bldr)

    def new_throw(self, id, exc):
        # type: (MuID, MuVarNode) -> None
        _apilog.logcall('new_throw', [self._bldr, id, exc], None, self._bldr)

    def new_extractvalue(self, id, result_id, strty, index, opnd):
        # type: (MuID, MuID, MuTypeNode, int, MuVarNode) -> None
        index_int = CIntConst(index, 'int')
        _apilog.logcall('new_extractvalue', [self._bldr, id, result_id, strty, index_int, opnd], None, self._bldr)

    def new_insertvalue(self, id, result_id, strty, index, opnd, newval):
        # type: (MuID, MuID, MuTypeNode, int, MuVarNode, MuVarNode) -> None
        index_int = CIntConst(index, 'int')
        _apilog.logcall('new_insertvalue', [self._bldr, id, result_id, strty, index_int, opnd, newval], None, self._bldr)

    def new_extractelement(self, id, result_id, seqty, indty, opnd, index):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode) -> None
        _apilog.logcall('new_extractelement', [self._bldr, id, result_id, seqty, indty, opnd, index], None, self._bldr)

    def new_insertelement(self, id, result_id, seqty, indty, opnd, index, newval):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode, MuVarNode) -> None
        _apilog.logcall('new_insertelement', [self._bldr, id, result_id, seqty, indty, opnd, index, newval], None, self._bldr)

    def new_shufflevector(self, id, result_id, vecty, maskty, vec1, vec2, mask):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode, MuVarNode) -> None
        _apilog.logcall('new_shufflevector', [self._bldr, id, result_id, vecty, maskty, vec1, vec2, mask], None, self._bldr)

    def new_new(self, id, result_id, allocty, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuTypeNode, MuExcClause) -> None
        _apilog.logcall('new_new', [self._bldr, id, result_id, allocty, exc_clause], None, self._bldr)

    def new_newhybrid(self, id, result_id, allocty, lenty, length, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuExcClause) -> None
        _apilog.logcall('new_newhybrid', [self._bldr, id, result_id, allocty, lenty, length, exc_clause], None, self._bldr)

    def new_alloca(self, id, result_id, allocty, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuTypeNode, MuExcClause) -> None
        _apilog.logcall('new_alloca', [self._bldr, id, result_id, allocty, exc_clause], None, self._bldr)

    def new_allocahybrid(self, id, result_id, allocty, lenty, length, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuTypeNode, MuTypeNode, MuVarNode, MuExcClause) -> None
        _apilog.logcall('new_allocahybrid', [self._bldr, id, result_id, allocty, lenty, length, exc_clause], None, self._bldr)

    def new_getiref(self, id, result_id, refty, opnd):
        # type: (MuID, MuID, MuTypeNode, MuVarNode) -> None
        _apilog.logcall('new_getiref', [self._bldr, id, result_id, refty, opnd], None, self._bldr)

    def new_getfieldiref(self, id, result_id, is_ptr, refty, index, opnd):
        # type: (MuID, MuID, bool, MuTypeNode, int, MuVarNode) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        index_int = CIntConst(index, 'int')
        _apilog.logcall('new_getfieldiref', [self._bldr, id, result_id, is_ptr_bool, refty, index_int, opnd], None, self._bldr)

    def new_getelemiref(self, id, result_id, is_ptr, refty, indty, opnd, index):
        # type: (MuID, MuID, bool, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_getelemiref', [self._bldr, id, result_id, is_ptr_bool, refty, indty, opnd, index], None, self._bldr)

    def new_shiftiref(self, id, result_id, is_ptr, refty, offty, opnd, offset):
        # type: (MuID, MuID, bool, MuTypeNode, MuTypeNode, MuVarNode, MuVarNode) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_shiftiref', [self._bldr, id, result_id, is_ptr_bool, refty, offty, opnd, offset], None, self._bldr)

    def new_getvarpartiref(self, id, result_id, is_ptr, refty, opnd):
        # type: (MuID, MuID, bool, MuTypeNode, MuVarNode) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_getvarpartiref', [self._bldr, id, result_id, is_ptr_bool, refty, opnd], None, self._bldr)

    def new_load(self, id, result_id, is_ptr, ord, refty, loc, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, bool, MuFlag, MuTypeNode, MuVarNode, MuExcClause) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_load', [self._bldr, id, result_id, is_ptr_bool, ord, refty, loc, exc_clause], None, self._bldr)

    def new_store(self, id, is_ptr, ord, refty, loc, newval, exc_clause=MU_NO_ID):
        # type: (MuID, bool, MuFlag, MuTypeNode, MuVarNode, MuVarNode, MuExcClause) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_store', [self._bldr, id, is_ptr_bool, ord, refty, loc, newval, exc_clause], None, self._bldr)

    def new_cmpxchg(self, id, value_result_id, succ_result_id, is_ptr, is_weak, ord_succ, ord_fail, refty, loc, expected, desired, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuID, bool, bool, MuFlag, MuFlag, MuTypeNode, MuVarNode, MuVarNode, MuVarNode, MuExcClause) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        is_weak_bool = 'true' if is_weak else 'false'
        _apilog.logcall('new_cmpxchg', [self._bldr, id, value_result_id, succ_result_id, is_ptr_bool, is_weak_bool, ord_succ, ord_fail, refty, loc, expected, desired, exc_clause], None, self._bldr)

    def new_atomicrmw(self, id, result_id, is_ptr, ord, optr, ref_ty, loc, opnd, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, bool, MuFlag, MuFlag, MuTypeNode, MuVarNode, MuVarNode, MuExcClause) -> None
        is_ptr_bool = 'true' if is_ptr else 'false'
        _apilog.logcall('new_atomicrmw', [self._bldr, id, result_id, is_ptr_bool, ord, optr, ref_ty, loc, opnd, exc_clause], None, self._bldr)

    def new_fence(self, id, ord):
        # type: (MuID, MuFlag) -> None
        _apilog.logcall('new_fence', [self._bldr, id, ord], None, self._bldr)

    def new_trap(self, id, result_ids, rettys, exc_clause=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, [MuID], [MuTypeNode], MuExcClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        rettys_arr, rettys_sz = lst2arr('MuTypeNode', rettys)
        _apilog.logcall('new_trap', [self._bldr, id, result_ids_arr, rettys_arr, rettys_sz, exc_clause, keepalive_clause], None, self._bldr)

    def new_watchpoint(self, id, wpid, result_ids, rettys, dis, ena, exc=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, MuWPID, [MuID], [MuTypeNode], MuDestClause, MuDestClause, MuDestClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        rettys_arr, rettys_sz = lst2arr('MuTypeNode', rettys)
        _apilog.logcall('new_watchpoint', [self._bldr, id, wpid, result_ids_arr, rettys_arr, rettys_sz, dis, ena, exc, keepalive_clause], None, self._bldr)

    def new_wpbranch(self, id, wpid, dis, ena):
        # type: (MuID, MuWPID, MuDestClause, MuDestClause) -> None
        _apilog.logcall('new_wpbranch', [self._bldr, id, wpid, dis, ena], None, self._bldr)

    def new_ccall(self, id, result_ids, callconv, callee_ty, sig, callee, args, exc_clause=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, [MuID], MuFlag, MuTypeNode, MuFuncSigNode, MuVarNode, [MuVarNode], MuExcClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        args_arr, args_sz = lst2arr('MuVarNode', args)
        _apilog.logcall('new_ccall', [self._bldr, id, result_ids_arr, result_ids_sz, callconv, callee_ty, sig, callee, args_arr, args_sz, exc_clause, keepalive_clause], None, self._bldr)

    def new_newthread(self, id, result_id, stack, threadlocal, new_stack_clause, exc_clause=MU_NO_ID):
        # type: (MuID, MuID, MuVarNode, MuVarNode, MuNewStackClause, MuExcClause) -> None
        _apilog.logcall('new_newthread', [self._bldr, id, result_id, stack, threadlocal, new_stack_clause, exc_clause], None, self._bldr)

    def new_swapstack(self, id, result_ids, swappee, cur_stack_clause, new_stack_clause, exc_clause=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, [MuID], MuVarNode, MuCurStackClause, MuNewStackClause, MuExcClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        _apilog.logcall('new_swapstack', [self._bldr, id, result_ids_arr, result_ids_sz, swappee, cur_stack_clause, new_stack_clause, exc_clause, keepalive_clause], None, self._bldr)

    def new_comminst(self, id, result_ids, opcode, flags, tys, sigs, args, exc_clause=MU_NO_ID, keepalive_clause=MU_NO_ID):
        # type: (MuID, [MuID], MuFlag, [MuFlag], [MuTypeNode], [MuFuncSigNode], [MuVarNode], MuExcClause, MuKeepaliveClause) -> None
        result_ids_arr, result_ids_sz = lst2arr('MuID', result_ids)
        flags_arr, flags_sz = lst2arr('MuFlag', flags)
        tys_arr, tys_sz = lst2arr('MuTypeNode', tys)
        sigs_arr, sigs_sz = lst2arr('MuFuncSigNode', sigs)
        args_arr, args_sz = lst2arr('MuVarNode', args)
        _apilog.logcall('new_comminst', [self._bldr, id, result_ids_arr, result_ids_sz, opcode, flags_arr, flags_sz, tys_arr, tys_sz, sigs_arr, sigs_sz, args_arr, args_sz, exc_clause, keepalive_clause], None, self._bldr)


# -------------------------------------------------------------------------------------------------------
# Helpers
def null(rmu_t):
    return NULL


def ftohstr(flt, c_type):
    import struct
    fmt = 'd' if c_type == 'double' else 'f'
    pkstr = struct.pack('!'+fmt, flt)
    hexstr = '0x' + ''.join(['%02x' % ord(b) for b in pkstr])
    return hexstr + 'ull'

def itohstr(i, c_type):
    import struct
    fmt_dic = {
        'int8_t': 'b',
        'uint8_t': 'B',
        'int16_t': 'h',
        'uint16_t': 'H',
        'int32_t': 'i',
        'uint32_t': 'I',
        'int': 'i',
        'int64_t': 'q',
        'uint64_t': 'Q'
    }
    fmt = fmt_dic[c_type]
    try:
        i_str = '0x' + ''.join(['%02x' % ord(b) for b in struct.pack('!' + fmt, i)])
    except Exception:
        fmt = fmt.upper() if fmt.islower() else fmt.lower()
        i_str = '0x' + ''.join(['%02x' % ord(b) for b in struct.pack('!' + fmt, i)])
    return i_str + 'ull'


def lst2arr(c_elm_t, lst):
    sz = len(lst)
    if len(lst) > 0:
        arr = CArrayConst(c_elm_t, lst)
    else:
        arr = NULL
    return arr, sz