1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  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
  59
  60
  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
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 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
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 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
 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
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
// STARK, a system for computer augmented design.
// Copyright (C) 2021 Matthew Rothlisberger

// STARK is licensed under the terms of the GNU Affero General Public
// License. See the top level LICENSE file for the license text.

// Find full copyright information in the top level COPYRIGHT file.

// <>

// src/sail/core.rs

// Sail types and many, many important utility functions for building
// list structures and evaluating Sail code. Contains the necessary
// definitions for constructing a useful Sail environment.

// <>

//! TODO: Gradual typing; more extensible type system; subtypes

use std::convert::TryFrom;
use std::mem;
use std::ptr;

use super::memmgt::{self, Region};

/// Core type assertion
macro_rules! coretypck {
    ( $var:ident ; $typ:ident ) => {
        assert_eq!(core_type($var).unwrap(), CoreType::$typ);
    };
}

/// Core type predicate
macro_rules! coretypp {
    ( $var:ident ; $typ:ident ) => {
        match core_type($var) {
            Some(t) => t == CoreType::$typ,
            None => false,
        }
    };
}

/// Trait for base types that are always the same size
pub trait SizedBase {}

/// Implements SizedBase trait for basic Rust types
macro_rules! sized_base {
    ( $( $typ:ty, )+ ) => {
        $(
            impl SizedBase for $typ {}
        )+
    };
}

sized_base! {
    u8, i8, u16, i16,
    u32, i32, u64, i64,
    u128, i128,
    f32, f64,
    *mut SlHead,
}

/// Head includes pointer to next list element
pub const HEAD_LEN: u8 = 8;
pub const PTR_LEN: u8 = 8;
pub const SYMBOL_LEN: u8 = 4;
pub const NUM_8_LEN: u8 = 1;
pub const NUM_16_LEN: u8 = 2;
pub const NUM_32_LEN: u8 = 4;
pub const NUM_64_LEN: u8 = 8;
pub const NUM_128_LEN: u8 = 16;

/// Header for all Sail objects in memory
///
/// **Handle ONLY using methods that operate on pointers to SlHead**
/// Should only store information that every referenced Sail object needs
/// TODO: Think about references, memory management, and associative table support
#[repr(C)]
pub struct SlHead {
    pub cfg: u8,
    pub rc: u8,
}

// TODO: potential change: add a "shared" bit to indicate that an object
// may be read and / or written by other threads

/// ALL Sail objects that may be independently referenced, begin with bytes of this format
///
/// size: 4 bits - base type: 3 bit - list elt: 1 bit - type pred: 1 bit - rc: 8 bits
/// The first eight bits determine the subsequent memory layout
const _MIN_HEAD: u16 = 0b1110001011111111;

/// Pointer to the next element of a linked list;
/// tagged with the SlHead (upper 2 unused bytes)
struct _SlListPtr {
    ptr: *mut SlHead,
}

/// Generates TryFrom implementations for important enums
macro_rules! enum_and_tryfrom {
    ($(#[$meta:meta])* $vis:vis enum $name:ident {
        $($(#[$vmeta:meta])* $vname:ident $(= $val:expr)?,)*
    }) => {
        $(#[$meta])*
        $vis enum $name {
            $($(#[$vmeta])* $vname $(= $val)?,)*
        }

        impl std::convert::TryFrom<u8> for $name {
            type Error = ();

            #[inline(always)]
            fn try_from(v: u8) -> Result<Self, Self::Error> {
                match v {
                    $(x if x == $name::$vname as u8 => Ok($name::$vname),)*
                    _ => Err(()),
                }
            }
        }
    }
}

enum_and_tryfrom! {
    /// Contains all valid values for the high six bits of an SlHead
    #[derive(Debug, PartialEq, Eq)]
    #[repr(u8)]
    pub enum Cfg {
        B0BoolF = 0b00000000,
        B0BoolT = 0b00000100,
        B0Other = 0b00011100,
        B1U8 = 0b00100000,
        B1I8 = 0b00100100,
        B1Other = 0b00111100,
        B2U16 = 0b01000000,
        B2I16 = 0b01000100,
        B2Err = 0b01001000,
        B2Other = 0b01011100,
        B4U32 = 0b01100000,
        B4I32 = 0b01100100,
        B4F32 = 0b01101000,
        B4Sym = 0b01101100,
        B4Other = 0b01111100,
        B8U64 = 0b10000000,
        B8I64 = 0b10000100,
        B8F64 = 0b10001000,
        B8Ptr = 0b10001100,
        B8Other = 0b10011100,
        B16U128 = 0b10100000,
        B16I128 = 0b10100100,
        B16Other = 0b10111100,
        VecStd = 0b11000000,
        VecStr = 0b11000100,
        VecArr = 0b11001000,
        VecAny = 0b11001100,
        VecHash = 0b11010000,
        VecOther = 0b11011100,
        ProcLambda = 0b11100000,
        ProcNative = 0b11100100,
        // ProcLbdaCk = 0b11101000,
        // ProcNatvCk = 0b11101100,
        Other = 0b11111100,
    }
}

enum_and_tryfrom! {
    /// All type sizes that may be specified in the head
    #[derive(Debug, PartialEq, Eq)]
    #[repr(u8)]
    pub enum BaseSize {
        // don't mess around with the enum variants
        B0 = 0,
        B1 = 1,
        B2 = 2,
        B4 = 3,
        B8 = 4,
        B16 = 5,
        Vec = 6,
        Other = 7,
    }
}

enum_and_tryfrom! {
    /// The four "modes" a symbol can have
    #[derive(Debug, PartialEq, Eq)]
    #[repr(u8)]
    pub enum SymbolMode {
        Basic = 0,
        Module = 1,
        Keyword = 2,
        Type = 3,
    }
}

/// Core types that must be known in order to assemble the runtime
///
/// They can be represented in the object head, without an additional type specifier.
/// Null pointers "point to" Nil objects; the concept is like interning.
/// The next 15 types have statically known size, and correspond to Rust types.
/// The last 6 types have variable size, and must be inspected to get a size.
#[derive(Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum CoreType {
    Nil,
    Bool,
    U8,
    I8,
    U16,
    I16,
    U32,
    I32,
    U64,
    I64,
    U128,
    I128,
    F32,
    F64,
    Symbol,
    Ref,
    ErrCode,
    VecStd,
    VecStr,
    VecArr,
    VecAny,
    VecHash,
    ProcLambda,
    ProcNative,
}

impl TryFrom<Cfg> for CoreType {
    type Error = ();

    #[inline(always)]
    fn try_from(v: Cfg) -> Result<Self, Self::Error> {
        match v {
            Cfg::B0BoolF | Cfg::B0BoolT => Ok(Self::Bool),
            Cfg::B1U8 => Ok(Self::U8),
            Cfg::B1I8 => Ok(Self::I8),
            Cfg::B2U16 => Ok(Self::U16),
            Cfg::B2I16 => Ok(Self::I16),
            Cfg::B2Err => Ok(Self::ErrCode),
            Cfg::B4U32 => Ok(Self::U32),
            Cfg::B4I32 => Ok(Self::I32),
            Cfg::B4F32 => Ok(Self::F32),
            Cfg::B4Sym => Ok(Self::Symbol),
            Cfg::B8U64 => Ok(Self::U64),
            Cfg::B8I64 => Ok(Self::I64),
            Cfg::B8F64 => Ok(Self::F64),
            Cfg::B8Ptr => Ok(Self::Ref),
            Cfg::B16U128 => Ok(Self::U128),
            Cfg::B16I128 => Ok(Self::I128),
            Cfg::VecStd => Ok(Self::VecStd),
            Cfg::VecStr => Ok(Self::VecStr),
            Cfg::VecArr => Ok(Self::VecArr),
            Cfg::VecAny => Ok(Self::VecAny),
            Cfg::VecHash => Ok(Self::VecHash),
            Cfg::ProcLambda => Ok(Self::ProcLambda),
            Cfg::ProcNative => Ok(Self::ProcNative),
            _ => Err(()),
        }
    }
}

/// Signature for Sail functions implemented in Rust
///
/// Arguments:
/// - Memory region in which to place return value
/// - Symbol table
/// - Current environment
/// - Slice containing all Sail arguments
pub type NativeFn = fn(*mut Region, *mut SlHead, *mut SlHead, &[*mut SlHead]) -> *mut SlHead;

// TODO / NOTE: There is currently one spare, unused bit in the Sail head: the second lowest bit

/// Creates a nil Sail object
#[inline(always)]
pub fn nil() -> *mut SlHead {
    ptr::null_mut()
}

/// Checks whether a pointer, ostensibly to a Sail object, is null
#[inline(always)]
pub fn nil_p(loc: *mut SlHead) -> bool {
    loc.is_null()
}

// pub fn atom_p(loc: *mut SlHead) -> bool {
//     match core_type(loc) {
//         Some(t) if t != CoreType::Ref => true,
//         _ => false,
//     }
// }

/// Set a symbol to one of the four symbol modes
pub const fn modeize_sym(sym: u32, mode: SymbolMode) -> u32 {
    (sym & 0x3FFFFFFF) + ((mode as u32) << 30)
}

/// Returns a symbol set to the default, basic mode
pub const fn demodes_sym(sym: u32) -> u32 {
    sym & 0x3FFFFFFF
}

/// Get the mode of a symbol
pub const fn mode_of_sym(sym: u32) -> SymbolMode {
    unsafe { mem::transmute::<u8, SymbolMode>((sym >> 30) as u8) }
}

/// Checks whether a Sail object is a reference to another non-nil object
#[inline(always)]
pub fn nnil_ref_p(loc: *mut SlHead) -> bool {
    match core_type(loc) {
        Some(t) if t == CoreType::Ref => {
            if !ref_empty_p(loc) {
                true
            } else {
                false
            }
        }
        _ => false,
    }
}

/// Checks whether a Sail object is a basic symbol
#[inline(always)]
pub fn basic_sym_p(loc: *mut SlHead) -> bool {
    match core_type(loc) {
        Some(t) if t == CoreType::Symbol => {
            if mode_of_sym(sym_get_id(loc)) == SymbolMode::Basic {
                true
            } else {
                false
            }
        }
        _ => false,
    }
}

/// Checks whether a Sail object is an executable procedure
#[inline(always)]
pub fn proc_p(loc: *mut SlHead) -> bool {
    match core_type(loc) {
        Some(t) if t == CoreType::ProcLambda || t == CoreType::ProcNative => true,
        _ => false,
    }
}

// no longer relevant
// pub fn list_elt_p(loc: *mut SlHead) -> bool {
//     (get_cfg_all(loc) & 0b00000010) != 0
// }

/// Checks whether a valid Sail object has a type specifier with a predicate
#[inline(always)]
pub fn pred_type_p(loc: *mut SlHead) -> bool {
    (get_cfg_all(loc) & 0b00000001) != 0
}

/// Checks whether a valid Sail object has a type specifier for itself alone
#[inline(always)]
pub fn self_type_p(loc: *mut SlHead) -> bool {
    let head = get_cfg_all(loc);
    if head >> 5 == 7 || (head & 0b00011100) >> 2 == 7 {
        true
    } else {
        false
    }
}

/// Returns the truthiness of a valid Sail object
#[inline(always)]
pub fn truthy(loc: *mut SlHead) -> bool {
    if nil_p(loc)
        || (coretypp!(loc ; Bool) && !bool_get(loc))
        || (coretypp!(loc ; Ref) && ref_empty_p(loc))
    {
        false
    } else {
        true
    }
}

/// Gets the full configuration byte from a Sail object
#[inline(always)]
fn get_cfg_all(loc: *mut SlHead) -> u8 {
    unsafe { ptr::read_unaligned(loc as *const u8) }
}

/// Gets the size / type configuration from a Sail object
#[inline(always)]
pub fn get_cfg_spec(loc: *mut SlHead) -> Cfg {
    match Cfg::try_from(get_cfg_all(loc) & 0b11111100) {
        Ok(out) => out,
        Err(_) => panic!("invalid cfg specifier"),
    }
}

/// Gets the base size of a Sail object
#[inline(always)]
pub fn get_base_size(loc: *mut SlHead) -> BaseSize {
    match BaseSize::try_from(get_cfg_all(loc) >> 5) {
        Ok(out) => out,
        Err(_) => unreachable!(),
    }
}

/// Gets base type specifier from a Sail object (its meaning differs with size)
#[inline(always)]
fn get_base_spec(loc: *mut SlHead) -> u8 {
    (get_cfg_all(loc) & 0b00011100) >> 2
}

/// From a valid Sail object, returns a pointer to the start of the value proper
///
/// (After the header and type specifiers, if they exist)
#[inline(always)]
pub fn value_ptr(loc: *mut SlHead) -> *mut u8 {
    let offset = if self_type_p(loc) && pred_type_p(loc) {
        HEAD_LEN + SYMBOL_LEN + SYMBOL_LEN
    } else if self_type_p(loc) || pred_type_p(loc) {
        HEAD_LEN + SYMBOL_LEN
    } else {
        HEAD_LEN
    } as usize;

    unsafe { (loc as *mut u8).add(offset) }
}

/// Returns None if the object is not of a core type, or its type if it is
#[inline(always)]
pub fn core_type(loc: *mut SlHead) -> Option<CoreType> {
    if nil_p(loc) {
        Some(CoreType::Nil)
    } else {
        match CoreType::try_from(get_cfg_spec(loc)) {
            Ok(out) => Some(out),
            Err(_) => None,
        }
    }
}

/// Returns the size of an object, which must be of a core type
#[inline(always)]
pub fn core_size(loc: *mut SlHead) -> usize {
    use CoreType::*;
    match core_type(loc).expect("not a core type") {
        Nil | Bool => 0,
        U8 | I8 => 1,
        U16 | I16 | ErrCode => 2,
        U32 | I32 | F32 | Symbol => 4,
        U64 | I64 | F64 | Ref => 8,
        U128 | I128 => 16,
        VecStd => vec_size(8, 8, unsafe { read_field_unchecked::<u32>(loc, 0) }
            as usize),
        VecStr => vec_size(8, 1, unsafe { read_field_unchecked::<u32>(loc, 0) }
            as usize),
        VecHash => vec_size(8, 8, unsafe { read_field_unchecked::<u32>(loc, 0) }
            as usize),
        VecArr => vec_size(
            8,
            temp_get_size(unsafe { read_field_unchecked::<u32>(loc, 0) }),
            unsafe { read_field_unchecked::<u32>(loc, 4) } as usize,
        ),
        VecAny => vec_size(
            12,
            temp_get_size(unsafe { read_field_unchecked::<u32>(loc, 0) }),
            unsafe { read_field_unchecked::<u32>(loc, 4) } as usize,
        ),
        ProcLambda => proc_lambda_size(unsafe { read_field_unchecked::<u16>(loc, 0) }),
        ProcNative => proc_native_size(),
    }
}

// a VecArr has head of type, length
// a VecAny has head of type, capacity, length

// TODO: the lack of a real type system needs to be rectified

/// Identifies whether a given type ID refers to a base sized type
pub fn temp_base_sized_p(typ: u32) -> bool {
    match typ {
        t if t == super::T_U8.0
            || t == super::T_I8.0
            || t == super::T_U16.0
            || t == super::T_I16.0
            || t == super::T_U32.0
            || t == super::T_I32.0
            || t == super::T_U64.0
            || t == super::T_I64.0
            || t == super::T_U128.0
            || t == super::T_I128.0
            || t == super::T_F32.0
            || t == super::T_F64.0
            || t == super::T_SYMBOL.0
            || t == super::T_REF.0
            || t == super::T_ERR.0 =>
        {
            true
        }
        _t => false,
    }
}

/// Gives the size of a limited range of types (base sized) by type ID
pub fn temp_get_size(typ: u32) -> usize {
    match typ {
        t if t == super::T_U8.0 => 1,
        t if t == super::T_I8.0 => 1,
        t if t == super::T_U16.0 => 2,
        t if t == super::T_I16.0 => 2,
        t if t == super::T_U32.0 => 4,
        t if t == super::T_I32.0 => 4,
        t if t == super::T_U64.0 => 8,
        t if t == super::T_I64.0 => 8,
        t if t == super::T_U128.0 => 16,
        t if t == super::T_I128.0 => 16,
        t if t == super::T_F32.0 => 4,
        t if t == super::T_F64.0 => 8,
        t if t == super::T_SYMBOL.0 => 4,
        t if t == super::T_REF.0 => 8,
        t if t == super::T_ERR.0 => 2,
        _t => {
            panic!("type not allowed")
        }
    }
}

/// Initializes a Sail object from a base sized type and a pointer
pub fn temp_init_from(reg: *mut Region, typ: u32, ptr: *const u8) -> *mut SlHead {
    assert!(temp_base_sized_p(typ));
    unsafe {
        match typ {
            t if t == super::T_U8.0 => u8_init(reg, ptr::read_unaligned(ptr)),
            t if t == super::T_I8.0 => i8_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_U16.0 => u16_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_I16.0 => i16_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_U32.0 => u32_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_I32.0 => i32_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_U64.0 => u64_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_I64.0 => i64_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_U128.0 => u128_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_I128.0 => i128_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_F32.0 => f32_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_F64.0 => f64_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_SYMBOL.0 => sym_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_REF.0 => ref_init(reg, ptr::read_unaligned(ptr as *const _)),
            t if t == super::T_ERR.0 => {
                super::errcode_init(reg, ptr::read_unaligned(ptr as *const _))
            }
            _ => unreachable!(),
        }
    }
}

/// Gives the overall size of a Vec with certain parameters
#[inline(always)]
pub fn vec_size(head_size: usize, elt_size: usize, capacity: usize) -> usize {
    head_size + (elt_size * capacity)
}

/// Gives the overall size of a lambda procedure by argument count
#[inline(always)]
fn proc_lambda_size(argct: u16) -> usize {
    (NUM_16_LEN + PTR_LEN) as usize + (SYMBOL_LEN as usize * argct as usize)
}

/// Gives the overall size of a native procedure
#[inline(always)]
fn proc_native_size() -> usize {
    (NUM_16_LEN + PTR_LEN) as usize
}

/// Write to a field of a Sail object of a core type
#[inline(always)]
pub fn core_write_field<T: SizedBase>(loc: *mut SlHead, offset: usize, src: T) {
    unsafe {
        let dst = value_ptr(loc).add(offset) as *mut T;
        assert!(offset + mem::size_of::<T>() <= core_size(loc));
        ptr::write_unaligned(dst, src)
    }
}

/// Write to a field of a Sail object without any checks
#[inline(always)]
pub unsafe fn write_field_unchecked<T: SizedBase>(loc: *mut SlHead, offset: usize, src: T) {
    let dst = value_ptr(loc).add(offset) as *mut T;
    ptr::write_unaligned(dst, src)
}

/// Write to a field of a Sail object atomically without any checks
#[inline(always)]
pub unsafe fn write_field_atomic_unchecked<T: SizedBase + Copy>(
    loc: *mut SlHead,
    offset: usize,
    src: T,
) {
    let dst = value_ptr(loc).add(offset) as *mut T;
    std::intrinsics::atomic_store_rel(dst, src);
}

/// Write to a field of a Sail object only if the current value is the
/// same as `old`, without any checks
///
/// Returns true if the write was successful, or false if nothing was
/// written
#[inline(always)]
pub unsafe fn write_field_cmpxcg_unchecked<T: SizedBase + Copy>(
    loc: *mut SlHead,
    offset: usize,
    old: T,
    src: T,
) -> bool {
    let dst = value_ptr(loc).add(offset) as *mut T;
    std::intrinsics::atomic_cxchg_acqrel(dst, old, src).1
}

/// Read from a field of a Sail object of a core type
#[inline(always)]
pub fn core_read_field<T: SizedBase>(loc: *mut SlHead, offset: usize) -> T {
    unsafe {
        let src = value_ptr(loc).add(offset) as *mut T;
        assert!(offset + mem::size_of::<T>() <= core_size(loc));
        ptr::read_unaligned(src)
    }
}

/// Read from a field of a Sail object without any checks
#[inline(always)]
pub unsafe fn read_field_unchecked<T: SizedBase>(loc: *mut SlHead, offset: usize) -> T {
    let src = value_ptr(loc).add(offset) as *mut T;
    ptr::read_unaligned(src)
}

/// Read from a field of a Sail object atomically without any checks
#[inline(always)]
pub unsafe fn read_field_atomic_unchecked<T: SizedBase + Copy>(
    loc: *mut SlHead,
    offset: usize,
) -> T {
    let src = value_ptr(loc).add(offset) as *mut T;
    std::intrinsics::atomic_load_acq(src)
}

// irrelevant
// fn set_list_elt_bit(loc: *mut SlHead, elt: bool) {
//     let old = get_cfg_all(loc);
//     let new = if elt {
//         old | 0b00000010
//     } else {
//         old & 0b11111101
//     };
//     unsafe { ptr::write_unaligned(loc as *mut u8, new) }
// }

/// Set the pointer to a list element's next element
#[inline(always)]
pub fn set_next_list_elt(loc: *mut SlHead, next: *mut SlHead) {
    unsafe {
        let head = ptr::read_unaligned(loc as *mut u16);
        ptr::write_unaligned(loc as *mut u64, ((next as u64) << 16) + head as u64);
    }
}

/// Set the pointer to a list element's next element only if the
/// current pointer is equivalent to `old`
#[inline(always)]
pub fn set_next_list_elt_cmpxcg(loc: *mut SlHead, old: *mut SlHead, new: *mut SlHead) -> bool {
    unsafe {
        let head = ptr::read_unaligned(loc as *mut u16);
        std::intrinsics::atomic_cxchg_acqrel(
            loc as *mut u64,
            ((old as u64) << 16) + head as u64,
            ((new as u64) << 16) + head as u64,
        )
        .1
    }
}

/// Gets the pointer to the next element from a list element
///
/// TODO: in future, handle redirects?
#[inline(always)]
pub fn get_next_list_elt(loc: *mut SlHead) -> *mut SlHead {
    assert!(!nil_p(loc));
    unsafe { (ptr::read_unaligned(loc as *mut usize) >> 16) as *mut SlHead }
}

#[inline(always)]
pub fn ref_make(reg: *mut Region) -> *mut SlHead {
    unsafe {
        let ptr = memmgt::alloc(reg, PTR_LEN as usize, Cfg::B8Ptr as u8);
        write_field_unchecked(ptr, 0, nil());
        ptr
    }
}

// TODO: disallow null pointers to make sure that nil and the empty list are the same?
#[inline(always)]
pub fn ref_init(reg: *mut Region, val: *mut SlHead) -> *mut SlHead {
    unsafe {
        let ptr = memmgt::alloc(reg, PTR_LEN as usize, Cfg::B8Ptr as u8);
        write_field_unchecked(ptr, 0, val);
        ptr
    }
}

#[inline(always)]
pub fn sym_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, SYMBOL_LEN as usize, Cfg::B4Sym as u8) }
}

#[inline(always)]
pub fn sym_init(reg: *mut Region, val: u32) -> *mut SlHead {
    let ptr = sym_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
pub fn stdvec_make(reg: *mut Region, cap: u32) -> *mut SlHead {
    // cap, len, (pointer * cap)
    unsafe {
        let size = vec_size(NUM_32_LEN as usize * 2, PTR_LEN as usize, cap as usize);
        let ptr = memmgt::alloc(reg, size, Cfg::VecStd as u8);

        write_field_unchecked::<u32>(ptr, 0, cap);
        write_field_unchecked::<u32>(ptr, 4, 0);

        ptr
    }
}

#[inline(always)]
pub fn stdvec_init(reg: *mut Region, val: &[*mut SlHead]) -> *mut SlHead {
    let len = val.len() as u32;
    let ptr = stdvec_make(reg, len);
    unsafe { write_field_unchecked(ptr, 4, len) }

    for (i, p) in val.iter().enumerate() {
        unsafe { write_field_unchecked(ptr, 8 + (8 * i), *p) }
    }

    ptr
}

#[inline(always)]
pub fn string_make(reg: *mut Region, cap: u32) -> *mut SlHead {
    // cap, len, (byte * cap)
    unsafe {
        let size = vec_size(NUM_32_LEN as usize * 2, NUM_8_LEN as usize, cap as usize);
        let ptr = memmgt::alloc(reg, size, Cfg::VecStr as u8);

        write_field_unchecked::<u32>(ptr, 0, cap);
        write_field_unchecked::<u32>(ptr, 4, 0);

        ptr
    }
}

#[inline(always)]
pub fn string_init(reg: *mut Region, val: &str) -> *mut SlHead {
    let len = val.len();
    let ptr = string_make(reg, len as u32);

    unsafe {
        write_field_unchecked(ptr, 4, len as u32);

        let local = std::slice::from_raw_parts_mut(value_ptr(ptr).add(8), len);
        local.copy_from_slice(val.as_bytes());
    }

    ptr
}

#[inline(always)]
pub fn hashvec_make(reg: *mut Region, size: u32) -> *mut SlHead {
    // size, fill, (pointer * size)
    unsafe {
        let top_size = vec_size(NUM_32_LEN as usize * 2, PTR_LEN as usize, size as usize);
        let ptr = memmgt::alloc(reg, top_size, Cfg::VecHash as u8);

        write_field_unchecked::<u32>(ptr, 0, size); // size
        write_field_unchecked::<u32>(ptr, 4, 0); // fill

        for i in 0..size as usize {
            write_field_unchecked(ptr, 4 + 4 + (i * 8), ptr::null_mut());
        }

        ptr
    }
}

#[inline(always)]
pub fn proc_lambda_make(reg: *mut Region, argct: u16) -> *mut SlHead {
    // argct, pointer, (symbol * argct)
    unsafe {
        let size = proc_lambda_size(argct);
        let ptr = memmgt::alloc(reg, size, Cfg::ProcLambda as u8);

        write_field_unchecked::<u16>(ptr, 0, argct);
        write_field_unchecked(ptr, 2, ptr::null_mut());

        ptr
    }
}

#[inline(always)]
pub fn proc_native_make(reg: *mut Region, argct: u16) -> *mut SlHead {
    // argct, pointer
    unsafe {
        let size = proc_native_size();
        let ptr = memmgt::alloc(reg, size, Cfg::ProcNative as u8);

        write_field_unchecked::<u16>(ptr, 0, argct);
        write_field_unchecked(ptr, 2, ptr::null_mut());

        ptr
    }
}

// TODO: this implementation will **easily** cause memory leaks
// TODO: decrement the reference count of previous object
#[inline(always)]
pub fn ref_set(loc: *mut SlHead, dst: *mut SlHead) {
    coretypck!(loc ; Ref);
    core_write_field(loc, 0, dst)
}

#[inline(always)]
pub fn ref_get(loc: *mut SlHead) -> *mut SlHead {
    coretypck!(loc ; Ref);
    core_read_field(loc, 0)
}

#[inline(always)]
fn ref_empty_p(loc: *mut SlHead) -> bool {
    nil_p(ref_get(loc))
}

#[inline(always)]
pub fn sym_set_id(loc: *mut SlHead, id: u32) {
    coretypck!(loc ; Symbol);
    core_write_field(loc, 0, id)
}

#[inline(always)]
pub fn sym_get_id(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; Symbol);
    core_read_field(loc, 0)
}

#[inline(always)]
fn stdvec_set_len(loc: *mut SlHead, len: u32) {
    coretypck!(loc ; VecStd);
    core_write_field(loc, 4, len)
}

#[inline(always)]
pub fn stdvec_get_len(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; VecStd);
    core_read_field(loc, 4)
}

#[inline(always)]
fn stdvec_get_cap(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; VecStd);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn stdvec_idx(loc: *mut SlHead, idx: u32) -> *mut SlHead {
    coretypck!(loc ; VecStd);
    core_read_field(loc, 4 + 4 + (idx as usize * 8))
}

#[inline(always)]
pub fn stdvec_push(loc: *mut SlHead, item: *mut SlHead) {
    let (len, cap) = (stdvec_get_len(loc), stdvec_get_cap(loc));

    if len < cap {
        core_write_field(loc, 4 + 4 + (len as usize * 8), item);
        stdvec_set_len(loc, len + 1);
    } else {
        panic!("not enough space in vec");
    }
}

#[inline(always)]
fn string_get_len(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; VecStr);
    core_read_field(loc, NUM_32_LEN as usize)
}

#[inline(always)]
fn string_set_len(loc: *mut SlHead, len: u32) {
    coretypck!(loc ; VecStr);
    core_write_field(loc, NUM_32_LEN as usize, len)
}

#[inline(always)]
fn string_get_cap(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; VecStr);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn string_set(loc: *mut SlHead, val: &str) {
    let cap = string_get_cap(loc);
    let len = val.len() as u32;

    // TODO: copy using a purpose-designed function
    if len <= cap {
        let mut count = 0;
        for c in val.bytes() {
            core_write_field(loc, 4 + 4 + count as usize, c);
            count += 1;
        }
        string_set_len(loc, len);
    } else {
        panic!("not enough space in string");
    }
}

#[inline(always)]
pub fn string_get(loc: *mut SlHead) -> &'static str {
    let len = string_get_len(loc);

    unsafe {
        std::str::from_utf8_unchecked(std::slice::from_raw_parts(
            value_ptr(loc).offset(4 + 4) as *mut u8,
            len as usize,
        ))
    }
}

#[inline(always)]
pub fn hashvec_get_size(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; VecHash);
    core_read_field(loc, 0)
}

/// Returns true if both arguments are the same Sail object
///
/// TODO: symbol handling etc
#[inline(always)]
fn id(fst: *mut SlHead, lst: *mut SlHead) -> bool {
    if fst == lst {
        true
    } else {
        false
    }
}

/// Returns true if both arguments' values are equal
///
/// TODO: make eq and hash actually function for all types
#[inline(always)]
pub fn core_eq(fst: *mut SlHead, lst: *mut SlHead) -> bool {
    if id(fst, lst) {
        true
    } else {
        match core_type(fst) {
            Some(CoreType::Ref) => {
                if ref_empty_p(fst) {
                    nil_p(lst) || (core_type(lst).unwrap() == CoreType::Ref && ref_empty_p(lst))
                } else {
                    false
                }
            }
            Some(CoreType::Nil) => {
                nil_p(lst) || (core_type(lst).unwrap() == CoreType::Ref && ref_empty_p(lst))
            }
            Some(typ) if typ != core_type(lst).unwrap() => false,
            Some(CoreType::Symbol) => sym_get_id(fst) == sym_get_id(lst),
            Some(CoreType::VecStr) => string_get(fst) == string_get(lst),
            _ => false,
        }
    }
}

/// Computes a hash value for the provided object
#[inline(always)]
fn core_hash(loc: *mut SlHead) -> u32 {
    match core_type(loc).expect("not a core type") {
        CoreType::Symbol => sym_get_id(loc),
        CoreType::VecStr => str_hash(string_get(loc)),
        _ => 0,
    }
}

/// Provides a simple hash function for string slices
#[inline(always)]
fn str_hash(slice: &str) -> u32 {
    let mut out: u32 = 1;
    for c in slice.bytes() {
        out = out.wrapping_add(out << 5).wrapping_add(c as u32);
    }
    out
}

// TODO: automatically resize as needed (probably as an option) (need "fill" field in subhead)
// TODO: clean up shadowed entries sometime
pub fn hash_map_insert(reg: *mut Region, loc: *mut SlHead, key: *mut SlHead, val: *mut SlHead) {
    let entry = core_cons_copy(reg, key, val);

    let size = hashvec_get_size(loc);
    let hash = core_hash(key) % size;
    let idx = 4 + 4 + (hash as usize * PTR_LEN as usize);

    let next = core_read_field(loc, idx);

    if !nil_p(next) {
        set_next_list_elt(entry, next);
    }

    core_write_field(loc, idx, entry)
}

// fn alist_map_insert(reg: *mut Region, loc: *mut SlHead, key: *mut SlHead, val: *mut SlHead) {
//     let entry = core_cons_copy(reg, key, val);
//     let next = core_read_field(loc, 0);
//     if !nil_p(next) {
//         set_next_list_elt(entry, next);
//     }
//     core_write_field(loc, 0, entry)
// }

// fn hash_map_lookup(loc: *mut SlHead, key: *mut SlHead) -> *mut SlHead {
//     let size = hashvec_get_size(loc);
//     let hash = core_hash(key) % size;
//     let entry = core_read_field(loc, 4 + 4 + (hash as usize * PTR_LEN as usize));
//     alist_search(entry, key)
// }

// fn alist_map_lookup(loc: *mut SlHead, key: *mut SlHead) -> *mut SlHead {
//     let entry = core_read_field(loc, 0);
//     alist_search(entry, key)
// }

// fn alist_search(head: *mut SlHead, target: *mut SlHead) -> *mut SlHead {
//     let mut pos = head;
//     loop {
//         if nil_p(pos) {
//             return nil();
//         }
//         if core_eq(ref_get(pos), target) {
//             return pos;
//         }
//         pos = get_next_list_elt(pos);
//     }
// }

#[inline(always)]
pub fn proc_get_argct(loc: *mut SlHead) -> u16 {
    assert!({
        let typ = core_type(loc).unwrap();
        typ == CoreType::ProcLambda || typ == CoreType::ProcNative
    });
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn proc_lambda_set_arg(loc: *mut SlHead, idx: u16, arg: u32) {
    coretypck!(loc ; ProcLambda);
    core_write_field(
        loc,
        (NUM_16_LEN + PTR_LEN) as usize + (idx as usize * SYMBOL_LEN as usize),
        arg,
    )
}

#[inline(always)]
pub fn proc_lambda_get_arg(reg: *mut Region, loc: *mut SlHead, idx: u16) -> *mut SlHead {
    sym_init(reg, proc_lambda_get_arg_id(loc, idx))
}

#[inline(always)]
fn proc_lambda_get_arg_id(loc: *mut SlHead, idx: u16) -> u32 {
    coretypck!(loc ; ProcLambda);
    core_read_field(
        loc,
        (NUM_16_LEN + PTR_LEN) as usize + (idx as usize * SYMBOL_LEN as usize),
    )
}

#[inline(always)]
pub fn proc_lambda_set_body(loc: *mut SlHead, body: *mut SlHead) {
    coretypck!(loc ; ProcLambda);
    core_write_field(loc, NUM_16_LEN as usize, body)
}

#[inline(always)]
pub fn proc_lambda_get_body(loc: *mut SlHead) -> *mut SlHead {
    coretypck!(loc ; ProcLambda);
    core_read_field(loc, NUM_16_LEN as usize)
}

#[inline(always)]
pub fn proc_native_set_body(loc: *mut SlHead, fun: NativeFn) {
    coretypck!(loc ; ProcNative);
    let ptr = unsafe { mem::transmute::<NativeFn, u64>(fun) };
    core_write_field(loc, NUM_16_LEN as usize, ptr)
}

#[inline(always)]
pub fn proc_native_get_body(loc: *mut SlHead) -> NativeFn {
    coretypck!(loc ; ProcNative);
    let ptr = core_read_field(loc, NUM_16_LEN as usize);
    unsafe { mem::transmute::<u64, NativeFn>(ptr) }
}

/// Copies the value from a Sail object of a core type into a newly
/// allocated object
#[inline(always)]
pub fn core_copy_val(reg: *mut Region, src: *mut SlHead) -> *mut SlHead {
    let (siz, cfg) = (core_size(src), get_cfg_all(src));

    unsafe {
        let dst = memmgt::alloc(reg, siz, cfg);
        ptr::copy_nonoverlapping(value_ptr(src), value_ptr(dst), siz);
        dst
    }
}

/// Copies the values from a pair of Sail objects of core types into a
/// two element list structure
#[inline(always)]
fn core_cons_copy(reg: *mut Region, car: *mut SlHead, cdr: *mut SlHead) -> *mut SlHead {
    let new_cdr = core_copy_val(reg, cdr);
    let new_car = core_copy_val(reg, car);
    set_next_list_elt(new_car, new_cdr);

    ref_init(reg, new_car)
}

// **********************************************************
// * `car` and `cdr` CANNOT be provided in the Sail internals
// * they do not fit well with the implementation details
// * use `ref_get` and `get_next_list_elt` instead of these
// **********************************************************

// /// Returns the first element of the provided list
// pub fn car(loc: *mut SlHead) -> *mut SlHead {
//     if nil_p(loc) {
//         nil()
//     } else if coretypp!(loc ; Ref) {
//         ref_get(loc)
//     } else {
//         loc
//     }
// }

// /// Returns list of elements following the first element of the provided list
// pub fn cdr(loc: *mut SlHead) -> *mut SlHead {
//     if coretypp!(loc ; Ref) {
//         get_next_list_elt(ref_get(loc))
//     } else {
//         get_next_list_elt(loc)
//     }
// }

// TODO: deal somewhere with dynamic bindings, lexical bindings, argument bindings
// TODO: give env and symtab their own predicate types
// TODO: the core does not use maps except for env and symtab, so consolidate the code
// TODO: improve the env data structure (this is where most evaluation time is spent)

/// Creates an environment, which is a list of maps that should
/// function as a LIFO stack
#[inline(always)]
pub fn env_create(reg: *mut Region, size: u32) -> *mut SlHead {
    hashvec_make(reg, size)
}

/// Looks up the given symbol in the given environment, returning the
/// object it refers to
#[inline(always)]
pub fn env_lookup(env: *mut SlHead, sym: *mut SlHead) -> *mut SlHead {
    env_lookup_by_id(env, sym_get_id(sym))
}

/// Looks up the given symbol ID in the given environment, returning
/// the object it refers to
#[inline(always)]
pub fn env_lookup_by_id(env: *mut SlHead, sym_id: u32) -> *mut SlHead {
    let entry = env_lookup_entry(env, sym_id);
    if nil_p(entry) {
        entry
    } else {
        get_next_list_elt(entry)
    }
}

/// Looks up the given symbol ID in the given environment, returning
/// the entry it refers to (symbol and object)
fn env_lookup_entry(mut env: *mut SlHead, sym_id: u32) -> *mut SlHead {
    while !nil_p(env) {
        // A layer can be a hash table or an alist
        let entry = if coretypp!(env ; VecHash) {
            let size = hashvec_get_size(env);
            let hash = sym_id % size;
            core_read_field(env, 4 + 4 + (hash as usize * PTR_LEN as usize))
        } else if coretypp!(env ; Ref) {
            core_read_field(env, 0)
        } else {
            println!("{:?}", core_type(env).unwrap());
            panic!("incorrect layer in env")
        };

        let mut pos = entry;
        loop {
            if nil_p(pos) {
                break;
            }
            if sym_get_id(ref_get(pos)) == sym_id {
                return ref_get(pos);
            }
            pos = get_next_list_elt(pos);
        }

        env = get_next_list_elt(env);
    }

    nil()
}

// TODO: use dynamic map mode

/// Creates a new hashmap based environment layer
fn env_new_layer(reg: *mut Region, min_size: u32) -> *mut SlHead {
    hashvec_make(reg, min_size * 2)
}

/// Inserts the given symbol into the environment, referring to the
/// `val` object
pub fn env_layer_ins_entry(
    reg: *mut Region,
    layer: *mut SlHead,
    sym: *mut SlHead,
    val: *mut SlHead,
) {
    env_layer_ins_by_id(reg, layer, sym_get_id(sym), val)
}

/// Inserts a symbol with the given ID into the environment, referring
/// to the `val` object
pub fn env_layer_ins_by_id(reg: *mut Region, layer: *mut SlHead, sym_id: u32, val: *mut SlHead) {
    let entry = {
        let sym = sym_init(reg, sym_id);
        set_next_list_elt(sym, val);

        ref_init(reg, sym)
    };

    let offset = if coretypp!(layer ; VecHash) {
        8 + ((sym_id % hashvec_get_size(layer)) as usize * PTR_LEN as usize)
    } else if coretypp!(layer ; Ref) {
        0
    } else {
        panic!("incorrect layer in env")
    };

    let next = core_read_field(layer, offset);

    if !nil_p(next) {
        set_next_list_elt(entry, next);
    }

    core_write_field(layer, offset, entry)
}

/// Changes the object pointed to by the given symbol's ID in the
/// environment, if the entry already exists
///
/// Returns false if no mutation was performed, or true if it was
pub fn env_layer_mut_entry(env: *mut SlHead, sym: *mut SlHead, val: *mut SlHead) -> bool {
    env_layer_mut_by_id(env, sym_get_id(sym), val)
}

/// Changes the object pointed to by the given ID in the environment,
/// if the entry already exists
///
/// Returns false if no mutation was performed, or true if it was
#[inline(always)]
fn env_layer_mut_by_id(env: *mut SlHead, sym_id: u32, val: *mut SlHead) -> bool {
    let entry = env_lookup_entry(env, sym_id);
    if nil_p(entry) {
        false
    } else {
        set_next_list_elt(entry, val);
        true
    }
}

// TODO: many improvements / optimizations possible throughout env system

/// Creates a new association list based environment layer; this is
/// meant for storing arguments to Sail procedures
pub fn env_new_arg_layer(reg: *mut Region) -> *mut SlHead {
    ref_make(reg)
}

/// Gets an object from the given argument layer by index
///
/// TODO: this should be a vector or something else more suitable
#[inline(always)]
pub fn env_arg_layer_get(layer: *mut SlHead, idx: u16) -> *mut SlHead {
    let mut left = idx;
    let mut pos = core_read_field(layer, 0);
    while left > 0 {
        pos = get_next_list_elt(pos);
        left -= 1;
    }
    get_next_list_elt(ref_get(pos))
}

/// Inserts the given object into the given argument layer using the
/// given symbol's ID
pub fn env_arg_layer_ins(reg: *mut Region, layer: *mut SlHead, key: *mut SlHead, val: *mut SlHead) {
    coretypck!(layer ; Ref);

    let entry = {
        let sym = core_copy_val(reg, key);
        set_next_list_elt(sym, val);
        ref_init(reg, sym)
    };

    let mut next = core_read_field(layer, 0);
    if nil_p(next) {
        core_write_field(layer, 0, entry);
    } else {
        while !get_next_list_elt(next).is_null() {
            next = get_next_list_elt(next);
        }

        set_next_list_elt(next, entry);
    }
}

// pub fn env_push_layer(env: *mut SlHead, layer: *mut SlHead) {
//     let next = ref_get(env);
//     set_next_list_elt(layer, next);
//     ref_set(env, layer);
// }

// pub fn env_pop_layer(env: *mut SlHead) {
//     let next = get_next_list_elt(ref_get(env));
//     ref_set(env, next);
// }

/// Creates a symbol table, which maps symbol strings to symbol IDs
/// and vice versa
///
/// This should take the form of a bimap, a 1 to 1 association between strings and IDs.
/// Two maps, one for each direction, pointing to the same set of cons cells (id . string).
/// Must keep track of id to assign (counter) and reclaim unused slots if counter reaches max.
fn sym_tab_create(reg: *mut Region, size: u32) -> *mut SlHead {
    let tbl = stdvec_make(reg, 3);

    let id_to_str = hashvec_make(reg, size);
    let str_to_id = hashvec_make(reg, size);

    let id_count = sym_init(reg, 0);

    stdvec_push(tbl, id_to_str);
    stdvec_push(tbl, str_to_id);
    stdvec_push(tbl, id_count);

    tbl
}

/// Takes the symbol table and a string object to insert, returning
/// the symbol's unique ID
fn sym_tab_insert(reg: *mut Region, tbl: *mut SlHead, sym: *mut SlHead) -> u32 {
    let next_id = stdvec_idx(tbl, 2);
    let id_num = sym_get_id(next_id);

    // println!("unique symbol id: {}", id_num);

    let entry = {
        let id = core_copy_val(reg, next_id);
        set_next_list_elt(id, sym);
        id
    };

    let id_to_str = stdvec_idx(tbl, 0);
    let str_to_id = stdvec_idx(tbl, 1);

    let id_size = hashvec_get_size(id_to_str);
    let str_size = hashvec_get_size(str_to_id);

    let id_hash = id_num % id_size;
    let str_hash = core_hash(sym) % str_size;

    let id_idx = 4 + 4 + (id_hash as usize * PTR_LEN as usize);
    let str_idx = 4 + 4 + (str_hash as usize * PTR_LEN as usize);

    let mut id_pos = core_read_field(id_to_str, id_idx);
    let mut str_pos = core_read_field(str_to_id, str_idx);

    let id_entry = ref_init(reg, entry);
    let str_entry = ref_init(reg, entry);

    if nil_p(id_pos) {
        core_write_field(id_to_str, id_idx, id_entry)
    } else {
        while !nil_p(get_next_list_elt(id_pos)) {
            id_pos = get_next_list_elt(id_pos);
        }

        set_next_list_elt(id_pos, id_entry)
    }

    if nil_p(str_pos) {
        core_write_field(str_to_id, str_idx, str_entry)
    } else {
        while !nil_p(get_next_list_elt(str_pos)) {
            str_pos = get_next_list_elt(str_pos);
        }

        set_next_list_elt(str_pos, str_entry)
    }

    sym_set_id(next_id, id_num + 1);

    id_num
}

/// Retrieves and returns the string representation of the given symbol
///
/// Looks up normally, by car, and returns cdr
pub fn sym_tab_lookup_by_id(tbl: *mut SlHead, qry: *mut SlHead) -> *mut SlHead {
    sym_tab_lookup_id_num(tbl, sym_get_id(qry))

    // let map = stdvec_idx(tbl, 0);
    // let size = hashvec_get_size(map);
    // let hash = core_hash(qry) % size as u32;

    // let mut entry = core_read_field(map, 4 + 4 + (hash as usize * PTR_LEN as usize));

    // loop {
    //     if nil_p(entry) {
    //         return nil();
    //     }

    //     if core_eq(ref_get(entry), qry) {
    //         return get_next_list_elt(ref_get(entry));
    //     }

    //     entry = get_next_list_elt(entry);
    // }
}

/// Retrieves and returns the symbol referring to the given string
///
/// Must look up by cdr, and return car
fn sym_tab_lookup_by_str(tbl: *mut SlHead, qry: *mut SlHead) -> *mut SlHead {
    let map = stdvec_idx(tbl, 1);
    let size = hashvec_get_size(map);
    let hash = core_hash(qry) % size as u32;

    let mut entry = core_read_field(map, 4 + 4 + (hash as usize * PTR_LEN as usize));

    loop {
        if nil_p(entry) {
            return nil();
        }

        if core_eq(get_next_list_elt(ref_get(entry)), qry) {
            return ref_get(entry);
        }

        entry = get_next_list_elt(entry);
    }
}

/// Retrieves and returns the string representation corresponding to
/// the given symbol ID
///
/// Looks up by ID number directly
pub fn sym_tab_lookup_id_num(tbl: *mut SlHead, id: u32) -> *mut SlHead {
    let map = stdvec_idx(tbl, 0);
    let size = hashvec_get_size(map);
    let hash = id % size as u32;

    let mut entry = core_read_field(map, 4 + 4 + (hash as usize * PTR_LEN as usize));

    loop {
        if nil_p(entry) {
            return nil();
        }

        if sym_get_id(ref_get(entry)) == id {
            return get_next_list_elt(ref_get(entry));
        }

        entry = get_next_list_elt(entry);
    }
}

/// Returns a unique ID for any symbol string; inserts symbol into the
/// table if not already present
pub fn sym_tab_get_id(reg: *mut Region, tbl: *mut SlHead, sym: &str) -> u32 {
    let map = stdvec_idx(tbl, 1);
    let size = hashvec_get_size(map);
    let hash = str_hash(sym) % size;

    let mut entry = core_read_field(map, 4 + 4 + (hash as usize * PTR_LEN as usize));

    while !nil_p(entry) {
        if sym == string_get(get_next_list_elt(ref_get(entry))) {
            return sym_get_id(ref_get(entry));
        }
        entry = get_next_list_elt(entry);
    }

    let record = string_init(reg, sym);

    sym_tab_insert(reg, tbl, record)
}

/// Prepares a complete Sail runtime environment, including symbol
/// table and env
///
/// TODO: should regions know about their Sail environment?
/// TODO: insert all the core type symbols
pub fn prep_environment(reg: *mut Region) -> (*mut SlHead, *mut SlHead) {
    (sym_tab_create(reg, 255), env_create(reg, 255))
}

#[inline(always)]
fn u8_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_8_LEN as usize, Cfg::B1U8 as u8) }
}

#[inline(always)]
fn u8_init(reg: *mut Region, val: u8) -> *mut SlHead {
    let ptr = u8_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn u8_set(loc: *mut SlHead, val: u8) {
    coretypck!(loc ; U8);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn u8_get(loc: *mut SlHead) -> u8 {
    coretypck!(loc ; U8);
    core_read_field(loc, 0)
}

#[inline(always)]
fn u16_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_16_LEN as usize, Cfg::B2U16 as u8) }
}

#[inline(always)]
fn u16_init(reg: *mut Region, val: u16) -> *mut SlHead {
    let ptr = u16_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn u16_set(loc: *mut SlHead, val: u16) {
    coretypck!(loc ; U16);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn u16_get(loc: *mut SlHead) -> u16 {
    coretypck!(loc ; U16);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn u32_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_32_LEN as usize, Cfg::B4U32 as u8) }
}

#[inline(always)]
pub fn u32_init(reg: *mut Region, val: u32) -> *mut SlHead {
    let ptr = u32_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
pub fn u32_set(loc: *mut SlHead, val: u32) {
    coretypck!(loc ; U32);
    core_write_field(loc, 0, val)
}

#[inline(always)]
pub fn u32_get(loc: *mut SlHead) -> u32 {
    coretypck!(loc ; U32);
    core_read_field(loc, 0)
}

#[inline(always)]
fn u64_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_64_LEN as usize, Cfg::B8U64 as u8) }
}

#[inline(always)]
fn u64_init(reg: *mut Region, val: u64) -> *mut SlHead {
    let ptr = u64_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn u64_set(loc: *mut SlHead, val: u64) {
    coretypck!(loc ; U64);
    core_write_field(loc, 0, val)
}

#[inline(always)]
pub fn u64_get(loc: *mut SlHead) -> u64 {
    coretypck!(loc ; U64);
    core_read_field(loc, 0)
}

#[inline(always)]
fn u128_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_128_LEN as usize, Cfg::B16U128 as u8) }
}

#[inline(always)]
fn u128_init(reg: *mut Region, val: u128) -> *mut SlHead {
    let ptr = u128_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn u128_set(loc: *mut SlHead, val: u128) {
    coretypck!(loc ; U128);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn u128_get(loc: *mut SlHead) -> u128 {
    coretypck!(loc ; U128);
    core_read_field(loc, 0)
}

#[inline(always)]
fn i8_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_8_LEN as usize, Cfg::B1I8 as u8) }
}

#[inline(always)]
fn i8_init(reg: *mut Region, val: i8) -> *mut SlHead {
    let ptr = i8_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn i8_set(loc: *mut SlHead, val: i8) {
    coretypck!(loc ; I8);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn i8_get(loc: *mut SlHead) -> i8 {
    coretypck!(loc ; I8);
    core_read_field(loc, 0)
}

#[inline(always)]
fn i16_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_16_LEN as usize, Cfg::B2I16 as u8) }
}

#[inline(always)]
fn i16_init(reg: *mut Region, val: i16) -> *mut SlHead {
    let ptr = i16_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn i16_set(loc: *mut SlHead, val: i16) {
    coretypck!(loc ; I16);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn i16_get(loc: *mut SlHead) -> i16 {
    coretypck!(loc ; I16);
    core_read_field(loc, 0)
}

#[inline(always)]
fn i32_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_32_LEN as usize, Cfg::B4I32 as u8) }
}

#[inline(always)]
fn i32_init(reg: *mut Region, val: i32) -> *mut SlHead {
    let ptr = i32_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn i32_set(loc: *mut SlHead, val: i32) {
    coretypck!(loc ; I32);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn i32_get(loc: *mut SlHead) -> i32 {
    coretypck!(loc ; I32);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn i64_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_64_LEN as usize, Cfg::B8I64 as u8) }
}

#[inline(always)]
pub fn i64_init(reg: *mut Region, val: i64) -> *mut SlHead {
    let ptr = i64_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
pub fn i64_set(loc: *mut SlHead, val: i64) {
    coretypck!(loc ; I64);
    core_write_field(loc, 0, val)
}

#[inline(always)]
pub fn i64_get(loc: *mut SlHead) -> i64 {
    coretypck!(loc ; I64);
    core_read_field(loc, 0)
}

#[inline(always)]
fn i128_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_128_LEN as usize, Cfg::B16I128 as u8) }
}

#[inline(always)]
fn i128_init(reg: *mut Region, val: i128) -> *mut SlHead {
    let ptr = i128_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
fn i128_set(loc: *mut SlHead, val: i128) {
    coretypck!(loc ; I128);
    core_write_field(loc, 0, val)
}

#[inline(always)]
fn i128_get(loc: *mut SlHead) -> i128 {
    coretypck!(loc ; I128);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn f32_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_32_LEN as usize, Cfg::B4F32 as u8) }
}

#[inline(always)]
pub fn f32_init(reg: *mut Region, val: f32) -> *mut SlHead {
    let ptr = f32_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
pub fn f32_set(loc: *mut SlHead, val: f32) {
    coretypck!(loc ; F32);
    core_write_field(loc, 0, val)
}

#[inline(always)]
pub fn f32_get(loc: *mut SlHead) -> f32 {
    coretypck!(loc ; F32);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn f64_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, NUM_64_LEN as usize, Cfg::B8F64 as u8) }
}

#[inline(always)]
pub fn f64_init(reg: *mut Region, val: f64) -> *mut SlHead {
    let ptr = f64_make(reg);
    unsafe { write_field_unchecked(ptr, 0, val) };
    ptr
}

#[inline(always)]
pub fn f64_set(loc: *mut SlHead, val: f64) {
    coretypck!(loc ; F64);
    core_write_field(loc, 0, val)
}

#[inline(always)]
pub fn f64_get(loc: *mut SlHead) -> f64 {
    coretypck!(loc ; F64);
    core_read_field(loc, 0)
}

#[inline(always)]
pub fn bool_make(reg: *mut Region) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, 0, Cfg::B0BoolF as u8) }
}

#[inline(always)]
pub fn bool_init(reg: *mut Region, val: bool) -> *mut SlHead {
    unsafe { memmgt::alloc(reg, 0, if val { Cfg::B0BoolT } else { Cfg::B0BoolF } as u8) }
}

#[inline(always)]
pub fn bool_set(loc: *mut SlHead, val: bool) {
    coretypck!(loc ; Bool);
    unsafe {
        if val {
            ptr::write_unaligned(
                loc as *mut u8,
                ptr::read_unaligned(loc as *mut u8) | 0b00000100,
            )
        } else {
            ptr::write_unaligned(
                loc as *mut u8,
                ptr::read_unaligned(loc as *mut u8) & 0b11111011,
            )
        }
    }
}

#[inline(always)]
pub fn bool_get(loc: *mut SlHead) -> bool {
    coretypck!(loc ; Bool);
    unsafe { ptr::read_unaligned(loc as *mut u8) & 0b00000100 != 0 }
}