summaryrefslogtreecommitdiff
path: root/tools/standalone_miri/main.cpp
blob: 8823cf44ee01b99417f6d6846b5ff91e78bbe89c (plain)
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
//
//
//
#include <iostream>
#include "module_tree.hpp"
#include "value.hpp"
#include <algorithm>
#include <iomanip>
#include "debug.hpp"

struct ProgramOptions
{
    ::std::string   infile;

    int parse(int argc, const char* argv[]);
};

Value MIRI_Invoke(ModuleTree& modtree, ::HIR::Path path, ::std::vector<Value> args);
Value MIRI_Invoke_Extern(const ::std::string& link_name, const ::std::string& abi, ::std::vector<Value> args);
Value MIRI_Invoke_Intrinsic(const ModuleTree& modtree, const ::std::string& name, const ::HIR::PathParams& ty_params, ::std::vector<Value> args);

int main(int argc, const char* argv[])
{
    ProgramOptions  opts;

    if( opts.parse(argc, argv) )
    {
        return 1;
    }

    auto tree = ModuleTree {};

    tree.load_file(opts.infile);

    auto val_argc = Value( ::HIR::TypeRef{RawType::I32} );
    ::HIR::TypeRef  argv_ty { RawType::I8 };
    argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
    argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
    auto val_argv = Value(argv_ty);
    val_argc.write_bytes(0, "\0\0\0", 4);
    val_argv.write_bytes(0, "\0\0\0\0\0\0\0", argv_ty.get_size());

    ::std::vector<Value>    args;
    args.push_back(::std::move(val_argc));
    args.push_back(::std::move(val_argv));
    auto rv = MIRI_Invoke( tree, tree.find_lang_item("start"), ::std::move(args) );
    ::std::cout << rv << ::std::endl;

    return 0;
}
class PrimitiveValue
{
public:
    virtual ~PrimitiveValue() {}

    virtual bool add(const PrimitiveValue& v) = 0;
    virtual bool subtract(const PrimitiveValue& v) = 0;
    virtual bool multiply(const PrimitiveValue& v) = 0;
    virtual bool divide(const PrimitiveValue& v) = 0;
    virtual bool modulo(const PrimitiveValue& v) = 0;
    virtual void write_to_value(Value& tgt, size_t ofs) const = 0;

    template<typename T>
    const T& check(const char* opname) const
    {
        const auto* xp = dynamic_cast<const T*>(this);
        LOG_ASSERT(xp, "Attempting to " << opname << " mismatched types, expected " << typeid(T).name() << " got " << typeid(*this).name());
        return *xp;
    }
};
template<typename T>
struct PrimitiveUInt:
    public PrimitiveValue
{
    typedef PrimitiveUInt<T>    Self;
    T   v;

    PrimitiveUInt(T v): v(v) {}
    ~PrimitiveUInt() override {}

    bool add(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("add");
        T newv = this->v + xp->v;
        bool did_overflow = newv < this->v;
        this->v = newv;
        return !did_overflow;
    }
    bool subtract(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("subtract");
        T newv = this->v - xp->v;
        bool did_overflow = newv > this->v;
        this->v = newv;
        return !did_overflow;
    }
    bool multiply(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("multiply");
        T newv = this->v * xp->v;
        bool did_overflow = newv < this->v && newv < xp->v;
        this->v = newv;
        return !did_overflow;
    }
    bool divide(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("divide");
        if(xp->v == 0)  return false;
        T newv = this->v / xp->v;
        this->v = newv;
        return true;
    }
    bool modulo(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("modulo");
        if(xp->v == 0)  return false;
        T newv = this->v % xp->v;
        this->v = newv;
        return true;
    }
};
struct PrimitiveU64: public PrimitiveUInt<uint64_t>
{
    PrimitiveU64(uint64_t v): PrimitiveUInt(v) {}
    void write_to_value(Value& tgt, size_t ofs) const override {
        tgt.write_u64(ofs, this->v);
    }
};
struct PrimitiveU32: public PrimitiveUInt<uint32_t>
{
    PrimitiveU32(uint32_t v): PrimitiveUInt(v) {}
    void write_to_value(Value& tgt, size_t ofs) const override {
        tgt.write_u32(ofs, this->v);
    }
};
template<typename T>
struct PrimitiveSInt:
    public PrimitiveValue
{
    typedef PrimitiveSInt<T>    Self;
    T   v;

    PrimitiveSInt(T v): v(v) {}
    ~PrimitiveSInt() override {}

    // TODO: Make this correct.
    bool add(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("add");
        T newv = this->v + xp->v;
        bool did_overflow = newv < this->v;
        this->v = newv;
        return !did_overflow;
    }
    bool subtract(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("subtract");
        T newv = this->v - xp->v;
        bool did_overflow = newv > this->v;
        this->v = newv;
        return !did_overflow;
    }
    bool multiply(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("multiply");
        T newv = this->v * xp->v;
        bool did_overflow = newv < this->v && newv < xp->v;
        this->v = newv;
        return !did_overflow;
    }
    bool divide(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("divide");
        if(xp->v == 0)  return false;
        T newv = this->v / xp->v;
        this->v = newv;
        return true;
    }
    bool modulo(const PrimitiveValue& x) override {
        const auto* xp = &x.check<Self>("modulo");
        if(xp->v == 0)  return false;
        T newv = this->v % xp->v;
        this->v = newv;
        return true;
    }
};
struct PrimitiveI64: public PrimitiveSInt<int64_t>
{
    PrimitiveI64(int64_t v): PrimitiveSInt(v) {}
    void write_to_value(Value& tgt, size_t ofs) const override {
        tgt.write_i64(ofs, this->v);
    }
};
struct PrimitiveI32: public PrimitiveSInt<int32_t>
{
    PrimitiveI32(int32_t v): PrimitiveSInt(v) {}
    void write_to_value(Value& tgt, size_t ofs) const override {
        tgt.write_i32(ofs, this->v);
    }
};

class PrimitiveValueVirt
{
    uint64_t    buf[3]; // Allows i128 plus a vtable pointer
    PrimitiveValueVirt() {}
public:
    // HACK: No copy/move constructors, assumes that contained data is always POD
    ~PrimitiveValueVirt() {
        reinterpret_cast<PrimitiveValue*>(&this->buf)->~PrimitiveValue();
    }
    PrimitiveValue& get() { return *reinterpret_cast<PrimitiveValue*>(&this->buf); }
    const PrimitiveValue& get() const { return *reinterpret_cast<const PrimitiveValue*>(&this->buf); }

    static PrimitiveValueVirt from_value(const ::HIR::TypeRef& t, const ValueRef& v) {
        PrimitiveValueVirt  rv;
        LOG_ASSERT(t.wrappers.empty(), "PrimitiveValueVirt::from_value: " << t);
        switch(t.inner_type)
        {
        case RawType::U32:
            new(&rv.buf) PrimitiveU32(v.read_u32(0));
            break;
        case RawType::U64:
            new(&rv.buf) PrimitiveU64(v.read_u64(0));
            break;
        case RawType::USize:
            if( POINTER_SIZE == 8 )
                new(&rv.buf) PrimitiveU64(v.read_u64(0));
            else
                new(&rv.buf) PrimitiveU32(v.read_u32(0));
            break;

        case RawType::I32:
            new(&rv.buf) PrimitiveI32(v.read_i32(0));
            break;
        case RawType::I64:
            new(&rv.buf) PrimitiveI64(v.read_i64(0));
            break;
        case RawType::ISize:
            if( POINTER_SIZE == 8 )
                new(&rv.buf) PrimitiveI64(v.read_i64(0));
            else
                new(&rv.buf) PrimitiveI32(v.read_i32(0));
            break;
        default:
            LOG_TODO("PrimitiveValueVirt::from_value: " << t);
        }
        return rv;
    }
};

struct Ops {
    template<typename T>
    static int do_compare(T l, T r) {
        if( l == r ) {
            return 0;
        }
        else if( !(l != r) ) {
            // Special return value for NaN w/ NaN
            return 2;
        }
        else if( l < r ) {
            return -1;
        }
        else {
            return 1;
        }
    }
    static uint64_t do_unsigned(uint64_t l, uint64_t r, ::MIR::eBinOp op) {
        switch(op)
        {
        case ::MIR::eBinOp::ADD:    return l + r;
        case ::MIR::eBinOp::SUB:    return l - r;
        case ::MIR::eBinOp::MUL:    return l * r;
        case ::MIR::eBinOp::DIV:    return l / r;
        case ::MIR::eBinOp::MOD:    return l % r;
        default:
            LOG_BUG("Unexpected operation in Ops::do_unsigned");
        }
    }
};

Value MIRI_Invoke(ModuleTree& modtree, ::HIR::Path path, ::std::vector<Value> args)
{
    TRACE_FUNCTION_R(path, "");

    LOG_DEBUG(path);
    const auto& fcn = modtree.get_function(path);
    for(size_t i = 0; i < args.size(); i ++)
    {
        LOG_DEBUG("- Argument(" << i << ") = " << args[i]);
    }

    if( fcn.external.link_name != "" )
    {
        // External function!
        return MIRI_Invoke_Extern(fcn.external.link_name, fcn.external.link_abi, ::std::move(args));
    }

    struct State
    {
        ModuleTree& modtree;
        const Function& fcn;
        Value   ret;
        ::std::vector<Value>    args;
        ::std::vector<Value>    locals;
        ::std::vector<bool>     drop_flags;

        State(ModuleTree& modtree, const Function& fcn, ::std::vector<Value> args):
            modtree(modtree),
            fcn(fcn),
            ret(fcn.ret_ty == RawType::Unreachable ? ::HIR::TypeRef() : fcn.ret_ty),
            args(::std::move(args)),
            drop_flags(fcn.m_mir.drop_flags)
        {
            locals.reserve(fcn.m_mir.locals.size());
            for(const auto& ty : fcn.m_mir.locals)
            {
                if( ty == RawType::Unreachable ) {
                    // HACK: Locals can be !, but they can NEVER be accessed
                    locals.push_back(Value());
                }
                else {
                    locals.push_back(Value(ty));
                }
            }
        }

        ValueRef get_value_and_type(const ::MIR::LValue& lv, ::HIR::TypeRef& ty)
        {
            switch(lv.tag())
            {
            case ::MIR::LValue::TAGDEAD:    throw "";
            TU_ARM(lv, Return, _e) {
                ty = fcn.ret_ty;
                return ValueRef(ret, 0, ret.size());
                } break;
            TU_ARM(lv, Local, e) {
                ty = fcn.m_mir.locals.at(e);
                return ValueRef(locals.at(e), 0, locals.at(e).size());
                } break;
            TU_ARM(lv, Argument, e) {
                ty = fcn.args.at(e.idx);
                return ValueRef(args.at(e.idx), 0, args.at(e.idx).size());
                } break;
            TU_ARM(lv, Static, e) {
                // TODO: Type!
                return ValueRef(modtree.get_static(e), 0, modtree.get_static(e).size());
                } break;
            TU_ARM(lv, Index, e) {
                auto idx = get_value_ref(*e.idx).read_usize(0);
                ::HIR::TypeRef  array_ty;
                auto base_val = get_value_and_type(*e.val, array_ty);
                if( array_ty.wrappers.empty() )
                    throw "ERROR";
                if( array_ty.wrappers.front().type == TypeWrapper::Ty::Array )
                {
                    ty = array_ty.get_inner();
                    base_val.m_offset += ty.get_size() * idx;
                    return base_val;
                }
                else if( array_ty.wrappers.front().type == TypeWrapper::Ty::Slice )
                {
                    throw "TODO";
                }
                else
                {
                    throw "ERROR";
                }
                } break;
            TU_ARM(lv, Field, e) {
                ::HIR::TypeRef  composite_ty;
                auto base_val = get_value_and_type(*e.val, composite_ty);
                LOG_DEBUG("Field - " << composite_ty);
                size_t inner_ofs;
                ty = composite_ty.get_field(e.field_index, inner_ofs);
                base_val.m_offset += inner_ofs;
                return base_val;
                }
            TU_ARM(lv, Downcast, e) {
                ::HIR::TypeRef  composite_ty;
                auto base_val = get_value_and_type(*e.val, composite_ty);
                LOG_DEBUG("Downcast - " << composite_ty);

                size_t inner_ofs;
                ty = composite_ty.get_field(e.variant_index, inner_ofs);
                base_val.m_offset += inner_ofs;
                return base_val;
                }
            TU_ARM(lv, Deref, e) {
                ::HIR::TypeRef  ptr_ty;
                auto val = get_value_and_type(*e.val, ptr_ty);
                ty = ptr_ty.get_inner();
                // TODO: Slices and slice-like types have the same logic.
                if( ty == RawType::Str || (!ty.wrappers.empty() && ty.wrappers.front().type == TypeWrapper::Ty::Slice) )
                {
                    LOG_ASSERT(val.m_size == 2*POINTER_SIZE, "Deref of " << ty << " that isn't a fat-pointer sized value");
                    // There MUST be a relocation at this point with a valid allocation.
                    auto& val_alloc = val.m_alloc ? val.m_alloc : val.m_value->allocation;
                    LOG_ASSERT(val_alloc, "Deref of a value with no allocation (hence no relocations)");
                    LOG_TRACE("Deref " << val_alloc.alloc());
                    auto alloc = val_alloc.alloc().get_relocation(val.m_offset);
                    LOG_ASSERT(alloc, "Deref of a value with no relocation");
                    size_t ofs = val.read_usize(0);
                    size_t size = val.read_usize(POINTER_SIZE);
                    return ValueRef(::std::move(alloc), ofs, size);
                }
                // TODO: Trait objects and trait-object likes
                else
                {
                    LOG_ASSERT(val.m_size == POINTER_SIZE, "Deref of a value that isn't a pointer-sized value");
                    // There MUST be a relocation at this point with a valid allocation.
                    auto& val_alloc = val.m_alloc ? val.m_alloc : val.m_value->allocation;
                    LOG_ASSERT(val_alloc, "Deref of a value with no allocation (hence no relocations)");
                    LOG_TRACE("Deref " << val_alloc.alloc());
                    auto alloc = val_alloc.alloc().get_relocation(val.m_offset);
                    LOG_ASSERT(alloc, "Deref of a value with no relocation");
                    size_t ofs = val.read_usize(0);
                    return ValueRef(::std::move(alloc), ofs, ty.get_size());
                }
                } break;
            }
            throw "";
        }
        ValueRef get_value_ref(const ::MIR::LValue& lv)
        {
            ::HIR::TypeRef  tmp;
            return get_value_and_type(lv, tmp);
        }

        ::HIR::TypeRef get_lvalue_ty(const ::MIR::LValue& lv)
        {
            ::HIR::TypeRef  ty;
            get_value_and_type(lv, ty);
            return ty;
        }

        Value read_lvalue_with_ty(const ::MIR::LValue& lv, ::HIR::TypeRef& ty)
        {
            auto base_value = get_value_and_type(lv, ty);

            return base_value.read_value(0, ty.get_size());
        }
        Value read_lvalue(const ::MIR::LValue& lv)
        {
            ::HIR::TypeRef  ty;
            return read_lvalue_with_ty(lv, ty);
        }
        void write_lvalue(const ::MIR::LValue& lv, Value val)
        {
            //LOG_DEBUG(lv << " = " << val);
            ::HIR::TypeRef  ty;
            auto base_value = get_value_and_type(lv, ty);

            if(base_value.m_alloc) {
                base_value.m_alloc.alloc().write_value(base_value.m_offset, ::std::move(val));
            }
            else {
                base_value.m_value->write_value(base_value.m_offset, ::std::move(val));
            }
        }

        Value const_to_value(const ::MIR::Constant& c, ::HIR::TypeRef& ty)
        {
            switch(c.tag())
            {
            case ::MIR::Constant::TAGDEAD:  throw "";
            TU_ARM(c, Int, ce) {
                ty = ::HIR::TypeRef(ce.t);
                Value val = Value(ty);
                val.write_bytes(0, &ce.v, ::std::min(ty.get_size(), sizeof(ce.v)));  // TODO: Endian
                // TODO: If the write was clipped, sign-extend
                return val;
                } break;
            TU_ARM(c, Uint, ce) {
                ty = ::HIR::TypeRef(ce.t);
                Value val = Value(ty);
                val.write_bytes(0, &ce.v, ::std::min(ty.get_size(), sizeof(ce.v)));  // TODO: Endian
                return val;
                } break;
            TU_ARM(c, Bool, ce) {
                Value val = Value(::HIR::TypeRef { RawType::Bool });
                val.write_bytes(0, &ce.v, 1);
                return val;
                } break;
            TU_ARM(c, Float, ce) {
                ty = ::HIR::TypeRef(ce.t);
                Value val = Value(ty);
                if( ce.t.raw_type == RawType::F64 ) {
                    val.write_bytes(0, &ce.v, ::std::min(ty.get_size(), sizeof(ce.v)));  // TODO: Endian/format?
                }
                else if( ce.t.raw_type == RawType::F32 ) {
                    float v = static_cast<float>(ce.v);
                    val.write_bytes(0, &v, ::std::min(ty.get_size(), sizeof(v)));  // TODO: Endian/format?
                }
                else {
                    throw ::std::runtime_error("BUG: Invalid type in Constant::Float");
                }
                return val;
                } break;
            TU_ARM(c, Const, ce) {
                LOG_BUG("Constant::Const in mmir");
                } break;
            TU_ARM(c, Bytes, ce) {
                LOG_TODO("Constant::Bytes");
                } break;
            TU_ARM(c, StaticString, ce) {
                ty = ::HIR::TypeRef(RawType::Str);
                ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Borrow, 0 });
                Value val = Value(ty);
                val.write_usize(0, 0);
                val.write_usize(POINTER_SIZE, ce.size());
                val.allocation.alloc().relocations.push_back(Relocation { 0, AllocationPtr::new_string(&ce) });
                LOG_DEBUG(c << " = " << val);
                //return Value::new_dataptr(ce.data());
                return val;
                } break;
            TU_ARM(c, ItemAddr, ce) {
                // Create a value with a special backing allocation of zero size that references the specified item.
                if( const auto* fn = modtree.get_function_opt(ce) ) {
                    return Value::new_fnptr(ce);
                }
                LOG_TODO("Constant::ItemAddr - statics?");
                } break;
            }
            throw "";
        }
        Value const_to_value(const ::MIR::Constant& c)
        {
            ::HIR::TypeRef  ty;
            return const_to_value(c, ty);
        }
        Value param_to_value(const ::MIR::Param& p, ::HIR::TypeRef& ty)
        {
            switch(p.tag())
            {
            case ::MIR::Param::TAGDEAD: throw "";
            TU_ARM(p, Constant, pe)
                return const_to_value(pe, ty);
            TU_ARM(p, LValue, pe)
                return read_lvalue_with_ty(pe, ty);
            }
            throw "";
        }
        Value param_to_value(const ::MIR::Param& p)
        {
            ::HIR::TypeRef  ty;
            return param_to_value(p, ty);
        }

        ValueRef get_value_ref_param(const ::MIR::Param& p, Value& tmp, ::HIR::TypeRef& ty)
        {
            switch(p.tag())
            {
            case ::MIR::Param::TAGDEAD: throw "";
            TU_ARM(p, Constant, pe)
                tmp = const_to_value(pe, ty);
                return ValueRef(tmp, 0, ty.get_size());
            TU_ARM(p, LValue, pe)
                return get_value_and_type(pe, ty);
            }
            throw "";
        }
    } state { modtree, fcn, ::std::move(args) };

    size_t bb_idx = 0;
    for(;;)
    {
        const auto& bb = fcn.m_mir.blocks.at(bb_idx);

        for(const auto& stmt : bb.statements)
        {
            LOG_DEBUG("BB" << bb_idx << "/" << (&stmt - bb.statements.data()) << ": " << stmt);
            switch(stmt.tag())
            {
            case ::MIR::Statement::TAGDEAD: throw "";
            TU_ARM(stmt, Assign, se) {
                Value   new_val;
                switch(se.src.tag())
                {
                case ::MIR::RValue::TAGDEAD: throw "";
                TU_ARM(se.src, Use, re) {
                    new_val = state.read_lvalue(re);
                    } break;
                TU_ARM(se.src, Constant, re) {
                    new_val = state.const_to_value(re);
                    } break;
                TU_ARM(se.src, Borrow, re) {
                    ::HIR::TypeRef  src_ty;
                    ValueRef src_base_value = state.get_value_and_type(re.val, src_ty);
                    auto alloc = src_base_value.m_alloc;
                    if( !alloc )
                    {
                        if( !src_base_value.m_value->allocation )
                        {
                            src_base_value.m_value->create_allocation();
                        }
                        alloc = AllocationPtr(src_base_value.m_value->allocation);
                    }
                    if( alloc.is_alloc() )
                        LOG_DEBUG("- alloc=" << alloc << " (" << alloc.alloc() << ")");
                    else
                        LOG_DEBUG("- alloc=" << alloc);
                    size_t ofs = src_base_value.m_offset;
                    bool is_slice_like = src_ty.has_slice_meta();
                    src_ty.wrappers.insert(src_ty.wrappers.begin(), TypeWrapper { TypeWrapper::Ty::Borrow, static_cast<size_t>(re.type) });

                    new_val = Value(src_ty);
                    // ^ Pointer value
                    new_val.write_usize(0, ofs);
                    if( is_slice_like )
                    {
                        new_val.write_usize(POINTER_SIZE, src_base_value.m_size);
                    }
                    // - Add the relocation after writing the value (writing clears the relocations)
                    new_val.allocation.alloc().relocations.push_back(Relocation { 0, ::std::move(alloc) });
                    } break;
                TU_ARM(se.src, Cast, re) {
                    // Determine the type of cast, is it a reinterpret or is it a value transform?
                    // - Float <-> integer is a transform, anything else should be a reinterpret.
                    ::HIR::TypeRef  src_ty;
                    auto src_value = state.get_value_and_type(re.val, src_ty);

                    new_val = Value(re.type);
                    if( re.type == src_ty )
                    {
                        // No-op cast
                        new_val = src_value.read_value(0, re.type.get_size());
                    }
                    else if( !re.type.wrappers.empty() )
                    {
                        // Destination can only be a raw pointer
                        if( re.type.wrappers.at(0).type != TypeWrapper::Ty::Pointer ) {
                            throw "ERROR";
                        }
                        if( !src_ty.wrappers.empty() )
                        {
                            // Source can be either
                            if( src_ty.wrappers.at(0).type != TypeWrapper::Ty::Pointer
                                && src_ty.wrappers.at(0).type != TypeWrapper::Ty::Borrow ) {
                                throw "ERROR";
                            }

                            if( src_ty.get_size() > re.type.get_size() ) {
                                // TODO: How to casting fat to thin?
                                //LOG_TODO("Handle casting fat to thin, " << src_ty << " -> " << re.type);
                                new_val = src_value.read_value(0, re.type.get_size());
                            }
                            else 
                            {
                                new_val = src_value.read_value(0, re.type.get_size());
                            }
                        }
                        else
                        {
                            if( src_ty == RawType::Function )
                            {
                            }
                            else if( src_ty == RawType::USize )
                            {
                            }
                            else
                            {
                                ::std::cerr << "ERROR: Trying to pointer (" << re.type <<" ) from invalid type (" << src_ty << ")\n";
                                throw "ERROR";
                            }
                            new_val = src_value.read_value(0, re.type.get_size());
                        }
                    }
                    else if( !src_ty.wrappers.empty() )
                    {
                        // TODO: top wrapper MUST be a pointer
                        if( src_ty.wrappers.at(0).type != TypeWrapper::Ty::Pointer
                            && src_ty.wrappers.at(0).type != TypeWrapper::Ty::Borrow ) {
                            throw "ERROR";
                        }
                        // TODO: MUST be a thin pointer?

                        // TODO: MUST be an integer (usize only?)
                        if( re.type != RawType::USize && re.type != RawType::ISize ) {
                            LOG_ERROR("Casting from a pointer to non-usize - " << re.type << " to " << src_ty);
                            throw "ERROR";
                        }
                        new_val = src_value.read_value(0, re.type.get_size());
                    }
                    else
                    {
                        // TODO: What happens if there'a cast of something with a relocation?
                        switch(re.type.inner_type)
                        {
                        case RawType::Unreachable:  throw "BUG";
                        case RawType::Composite:    throw "ERROR";
                        case RawType::TraitObject:    throw "ERROR";
                        case RawType::Function:    throw "ERROR";
                        case RawType::Str:    throw "ERROR";
                        case RawType::Unit:   throw "ERROR";
                        case RawType::F32: {
                            float dst_val = 0.0;
                            // Can be an integer, or F64 (pointer is impossible atm)
                            switch(src_ty.inner_type)
                            {
                            case RawType::Unreachable:  throw "BUG";
                            case RawType::Composite:    throw "ERROR";
                            case RawType::TraitObject:  throw "ERROR";
                            case RawType::Function:     throw "ERROR";
                            case RawType::Char: throw "ERROR";
                            case RawType::Str:  throw "ERROR";
                            case RawType::Unit: throw "ERROR";
                            case RawType::Bool: throw "ERROR";
                            case RawType::F32:  throw "BUG";
                            case RawType::F64:  dst_val = static_cast<float>( src_value.read_f64(0) ); break;
                            case RawType::USize:    throw "TODO";// /*dst_val = src_value.read_usize();*/   break;
                            case RawType::ISize:    throw "TODO";// /*dst_val = src_value.read_isize();*/   break;
                            case RawType::U8:   dst_val = static_cast<float>( src_value.read_u8 (0) );  break;
                            case RawType::I8:   dst_val = static_cast<float>( src_value.read_i8 (0) );  break;
                            case RawType::U16:  dst_val = static_cast<float>( src_value.read_u16(0) );  break;
                            case RawType::I16:  dst_val = static_cast<float>( src_value.read_i16(0) );  break;
                            case RawType::U32:  dst_val = static_cast<float>( src_value.read_u32(0) );  break;
                            case RawType::I32:  dst_val = static_cast<float>( src_value.read_i32(0) );  break;
                            case RawType::U64:  dst_val = static_cast<float>( src_value.read_u64(0) );  break;
                            case RawType::I64:  dst_val = static_cast<float>( src_value.read_i64(0) );  break;
                            case RawType::U128: throw "TODO";// /*dst_val = src_value.read_u128();*/ break;
                            case RawType::I128: throw "TODO";// /*dst_val = src_value.read_i128();*/ break;
                            }
                            new_val.write_f32(0, dst_val);
                            } break;
                        case RawType::F64: {
                            double dst_val = 0.0;
                            // Can be an integer, or F32 (pointer is impossible atm)
                            switch(src_ty.inner_type)
                            {
                            case RawType::Unreachable:  throw "BUG";
                            case RawType::Composite:    throw "ERROR";
                            case RawType::TraitObject:  throw "ERROR";
                            case RawType::Function:     throw "ERROR";
                            case RawType::Char: throw "ERROR";
                            case RawType::Str:  throw "ERROR";
                            case RawType::Unit: throw "ERROR";
                            case RawType::Bool: throw "ERROR";
                            case RawType::F64:  throw "BUG";
                            case RawType::F32:  dst_val = static_cast<double>( src_value.read_f32(0) ); break;
                            case RawType::USize:    dst_val = static_cast<double>( src_value.read_usize(0) );   break;
                            case RawType::ISize:    dst_val = static_cast<double>( src_value.read_isize(0) );   break;
                            case RawType::U8:   dst_val = static_cast<double>( src_value.read_u8 (0) );  break;
                            case RawType::I8:   dst_val = static_cast<double>( src_value.read_i8 (0) );  break;
                            case RawType::U16:  dst_val = static_cast<double>( src_value.read_u16(0) );  break;
                            case RawType::I16:  dst_val = static_cast<double>( src_value.read_i16(0) );  break;
                            case RawType::U32:  dst_val = static_cast<double>( src_value.read_u32(0) );  break;
                            case RawType::I32:  dst_val = static_cast<double>( src_value.read_i32(0) );  break;
                            case RawType::U64:  dst_val = static_cast<double>( src_value.read_u64(0) );  break;
                            case RawType::I64:  dst_val = static_cast<double>( src_value.read_i64(0) );  break;
                            case RawType::U128: throw "TODO"; /*dst_val = src_value.read_u128();*/ break;
                            case RawType::I128: throw "TODO"; /*dst_val = src_value.read_i128();*/ break;
                            }
                            new_val.write_f64(0, dst_val);
                            } break;
                        case RawType::Bool:
                            LOG_TODO("Cast to " << re.type);
                        case RawType::Char:
                            LOG_TODO("Cast to " << re.type);
                        case RawType::USize:
                        case RawType::U8:
                        case RawType::U16:
                        case RawType::U32:
                        case RawType::U64:
                        case RawType::ISize:
                        case RawType::I8:
                        case RawType::I16:
                        case RawType::I32:
                        case RawType::I64:
                            {
                            uint64_t dst_val = 0;
                            // Can be an integer, or F32 (pointer is impossible atm)
                            switch(src_ty.inner_type)
                            {
                            case RawType::Unreachable:  throw "BUG";
                            case RawType::Composite:    throw "ERROR";
                            case RawType::TraitObject:  throw "ERROR";
                            case RawType::Function:
                                LOG_ASSERT(re.type.inner_type == RawType::USize, "");
                                new_val = src_value.read_value(0, re.type.get_size());
                                break;
                            case RawType::Char: LOG_TODO("Cast char to integer (only u32)");
                            case RawType::Str:  throw "ERROR";
                            case RawType::Unit: throw "ERROR";
                            case RawType::Bool: throw "ERROR";
                            case RawType::F64:  throw "BUG";
                            case RawType::F32:
                                dst_val = static_cast<uint64_t>( src_value.read_f32(0) );
                                if(0)
                            case RawType::USize:
                                dst_val = static_cast<uint64_t>( src_value.read_usize(0) );
                                if(0)
                            case RawType::ISize:
                                dst_val = static_cast<uint64_t>( src_value.read_isize(0) );
                                if(0)
                            case RawType::U8:
                                dst_val = static_cast<uint64_t>( src_value.read_u8 (0) );
                                if(0)
                            case RawType::I8:
                                dst_val = static_cast<uint64_t>( src_value.read_i8 (0) );
                                if(0)
                            case RawType::U16:
                                dst_val = static_cast<uint64_t>( src_value.read_u16(0) );
                                if(0)
                            case RawType::I16:
                                dst_val = static_cast<uint64_t>( src_value.read_i16(0) );
                                if(0)
                            case RawType::U32:
                                dst_val = static_cast<uint64_t>( src_value.read_u32(0) );
                                if(0)
                            case RawType::I32:
                                dst_val = static_cast<uint64_t>( src_value.read_i32(0) );
                                if(0)
                            case RawType::U64:
                                dst_val = static_cast<uint64_t>( src_value.read_u64(0) );
                                if(0)
                            case RawType::I64:
                                dst_val = static_cast<uint64_t>( src_value.read_i64(0) );

                                switch(re.type.inner_type)
                                {
                                case RawType::USize:
                                    new_val.write_usize(0, dst_val);
                                    break;
                                case RawType::U8:
                                    new_val.write_u8(0, static_cast<uint8_t>(dst_val));
                                    break;
                                case RawType::U16:
                                    new_val.write_u16(0, static_cast<uint16_t>(dst_val));
                                    break;
                                case RawType::U32:
                                    new_val.write_u32(0, static_cast<uint32_t>(dst_val));
                                    break;
                                case RawType::U64:
                                    new_val.write_u64(0, dst_val);
                                    break;
                                case RawType::ISize:
                                    new_val.write_usize(0, static_cast<int64_t>(dst_val));
                                    break;
                                case RawType::I8:
                                    new_val.write_i8(0, static_cast<int8_t>(dst_val));
                                    break;
                                case RawType::I16:
                                    new_val.write_i16(0, static_cast<int16_t>(dst_val));
                                    break;
                                case RawType::I32:
                                    new_val.write_i32(0, static_cast<int32_t>(dst_val));
                                    break;
                                case RawType::I64:
                                    new_val.write_i64(0, static_cast<int64_t>(dst_val));
                                    break;
                                default:
                                    throw "";
                                }
                                break;
                            case RawType::U128: throw "TODO"; /*dst_val = src_value.read_u128();*/ break;
                            case RawType::I128: throw "TODO"; /*dst_val = src_value.read_i128();*/ break;
                            }
                            } break;
                        case RawType::U128:
                        case RawType::I128:
                            LOG_TODO("Cast to " << re.type);
                        }
                    }
                    } break;
                TU_ARM(se.src, BinOp, re) {
                    ::HIR::TypeRef  ty_l, ty_r;
                    Value   tmp_l, tmp_r;
                    auto v_l = state.get_value_ref_param(re.val_l, tmp_l, ty_l);
                    auto v_r = state.get_value_ref_param(re.val_r, tmp_r, ty_r);
                    LOG_DEBUG(v_l << " ? " << v_r);

                    switch(re.op)
                    {
                    case ::MIR::eBinOp::BIT_SHL:
                    case ::MIR::eBinOp::BIT_SHR:
                        LOG_TODO("BinOp SHL/SHR - can have mismatched types - " << se.src);
                    case ::MIR::eBinOp::EQ:
                    case ::MIR::eBinOp::NE:
                    case ::MIR::eBinOp::GT:
                    case ::MIR::eBinOp::GE:
                    case ::MIR::eBinOp::LT:
                    case ::MIR::eBinOp::LE: {
                        LOG_ASSERT(ty_l == ty_r, "BinOp type mismatch - " << ty_l << " != " << ty_r);
                        int res = 0;
                        // TODO: Handle comparison of the relocations too

                        const auto& alloc_l = v_l.m_value ? v_l.m_value->allocation : v_l.m_alloc;
                        const auto& alloc_r = v_r.m_value ? v_r.m_value->allocation : v_r.m_alloc;
                        auto reloc_l = alloc_l ? alloc_l.alloc().get_relocation(0) : AllocationPtr();
                        auto reloc_r = alloc_r ? alloc_r.alloc().get_relocation(0) : AllocationPtr();

                        if( reloc_l != reloc_r )
                        {
                            res = (reloc_l < reloc_r ? -1 : 1);
                        }
                        LOG_DEBUG("res=" << res << ", " << reloc_l << " ? " << reloc_r);

                        if( ty_l.wrappers.empty() )
                        {
                            switch(ty_l.inner_type)
                            {
                            case RawType::U64:
                                res = res != 0 ? res : Ops::do_compare(v_l.read_u64(0), v_r.read_u64(0));
                                break;
                            case RawType::U32:
                                res = res != 0 ? res : Ops::do_compare(v_l.read_u32(0), v_r.read_u32(0));
                                break;
                            case RawType::U16:
                                res = res != 0 ? res : Ops::do_compare(v_l.read_u16(0), v_r.read_u16(0));
                                break;
                            case RawType::U8:
                                res = res != 0 ? res : Ops::do_compare(v_l.read_u8(0), v_r.read_u8(0));
                                break;
                            case RawType::USize:
                                res = res != 0 ? res : Ops::do_compare(v_l.read_usize(0), v_r.read_usize(0));
                                break;
                            default:
                                LOG_TODO("BinOp comparisons - " << se.src << " w/ " << ty_l);
                            }
                        }
                        else if( ty_l.wrappers.front().type == TypeWrapper::Ty::Pointer )
                        {
                            // TODO: Technically only EQ/NE are valid.

                            res = res != 0 ? res : Ops::do_compare(v_l.read_usize(0), v_r.read_usize(0));

                            // Compare fat metadata.
                            if( res == 0 && v_l.m_size > POINTER_SIZE )
                            {
                                reloc_l = alloc_l ? alloc_l.alloc().get_relocation(POINTER_SIZE) : AllocationPtr();
                                reloc_r = alloc_r ? alloc_r.alloc().get_relocation(POINTER_SIZE) : AllocationPtr();

                                if( res == 0 && reloc_l != reloc_r )
                                {
                                    res = (reloc_l < reloc_r ? -1 : 1);
                                }
                                res = res != 0 ? res : Ops::do_compare(v_l.read_usize(POINTER_SIZE), v_r.read_usize(POINTER_SIZE));
                            }
                        }
                        else
                        {
                            LOG_TODO("BinOp comparisons - " << se.src << " w/ " << ty_l);
                        }
                        bool res_bool;
                        switch(re.op)
                        {
                        case ::MIR::eBinOp::EQ: res_bool = (res == 0);  break;
                        case ::MIR::eBinOp::NE: res_bool = (res != 0);  break;
                        case ::MIR::eBinOp::GT: res_bool = (res == 1);  break;
                        case ::MIR::eBinOp::GE: res_bool = (res == 1 || res == 0);  break;
                        case ::MIR::eBinOp::LT: res_bool = (res == -1); break;
                        case ::MIR::eBinOp::LE: res_bool = (res == -1 || res == 0); break;
                            break;
                        default:
                            LOG_BUG("Unknown comparison");
                        }
                        new_val = Value(::HIR::TypeRef(RawType::Bool));
                        new_val.write_u8(0, res_bool ? 1 : 0);
                        } break;
                    default:
                        LOG_ASSERT(ty_l == ty_r, "BinOp type mismatch - " << ty_l << " != " << ty_r);
                        auto val_l = PrimitiveValueVirt::from_value(ty_l, v_l);
                        auto val_r = PrimitiveValueVirt::from_value(ty_r, v_r);
                        switch(re.op)
                        {
                        case ::MIR::eBinOp::ADD:    val_l.get().add( val_r.get() ); break;
                        case ::MIR::eBinOp::SUB:    val_l.get().subtract( val_r.get() ); break;
                        case ::MIR::eBinOp::MUL:    val_l.get().multiply( val_r.get() ); break;
                        case ::MIR::eBinOp::DIV:    val_l.get().divide( val_r.get() ); break;
                        case ::MIR::eBinOp::MOD:    val_l.get().modulo( val_r.get() ); break;
                        default:
                            throw "";
                        }
                        new_val = Value(ty_l);
                        val_l.get().write_to_value(new_val, 0);
                        break;
                    }
                    } break;
                TU_ARM(se.src, UniOp, re) {
                    ::HIR::TypeRef  ty;
                    auto v = state.get_value_and_type(re.val, ty);
                    LOG_ASSERT(ty.wrappers.empty(), "UniOp on wrapped type - " << ty);
                    new_val = Value(ty);
                    switch(re.op)
                    {
                    case ::MIR::eUniOp::INV:
                        switch(ty.inner_type)
                        {
                        case RawType::U128:
                            LOG_TODO("UniOp::INV U128");
                        case RawType::U64:
                            new_val.write_u64( 0, ~v.read_u64(0) );
                            break;
                        case RawType::U32:
                            new_val.write_u32( 0, ~v.read_u32(0) );
                            break;
                        case RawType::U16:
                            new_val.write_u16( 0, ~v.read_u16(0) );
                            break;
                        case RawType::U8:
                            new_val.write_u8 ( 0, ~v.read_u8 (0) );
                            break;
                        case RawType::USize:
                            new_val.write_usize( 0, ~v.read_usize(0) );
                            break;
                        case RawType::Bool:
                            new_val.write_u8 ( 0, v.read_u8 (0) == 0 );
                            break;
                        default:
                            LOG_TODO("UniOp::INV - w/ type " << ty);
                        }
                        break;
                    case ::MIR::eUniOp::NEG:
                        switch(ty.inner_type)
                        {
                        case RawType::I128:
                            LOG_TODO("UniOp::NEG I128");
                        case RawType::I64:
                            new_val.write_i64( 0, -v.read_i64(0) );
                            break;
                        case RawType::I32:
                            new_val.write_i32( 0, -v.read_i32(0) );
                            break;
                        case RawType::I16:
                            new_val.write_i16( 0, -v.read_i16(0) );
                            break;
                        case RawType::I8:
                            new_val.write_i8 ( 0, -v.read_i8 (0) );
                            break;
                        case RawType::ISize:
                            new_val.write_isize( 0, -v.read_isize(0) );
                            break;
                        default:
                            LOG_TODO("UniOp::INV - w/ type " << ty);
                        }
                        break;
                    }
                    } break;
                TU_ARM(se.src, DstMeta, re) {
                    LOG_TODO(stmt);
                    } break;
                TU_ARM(se.src, DstPtr, re) {
                    LOG_TODO(stmt);
                    } break;
                TU_ARM(se.src, MakeDst, re) {
                    LOG_TODO(stmt);
                    } break;
                TU_ARM(se.src, Tuple, re) {
                    ::HIR::TypeRef  dst_ty;
                    state.get_value_and_type(se.dst, dst_ty);
                    new_val = Value(dst_ty);

                    for(size_t i = 0; i < re.vals.size(); i++)
                    {
                        auto fld_ofs = dst_ty.composite_type->fields.at(i).first;
                        new_val.write_value(fld_ofs, state.param_to_value(re.vals[i]));
                    }
                    } break;
                TU_ARM(se.src, Array, re) {
                    ::HIR::TypeRef  dst_ty;
                    state.get_value_and_type(se.dst, dst_ty);
                    new_val = Value(dst_ty);
                    // TODO: Assert that type is an array
                    auto inner_ty = dst_ty.get_inner();
                    size_t stride = inner_ty.get_size();

                    size_t ofs = 0;
                    for(const auto& v : re.vals)
                    {
                        new_val.write_value(ofs, state.param_to_value(v));
                        ofs += stride;
                    }
                    } break;
                TU_ARM(se.src, SizedArray, re) {
                    ::HIR::TypeRef  dst_ty;
                    state.get_value_and_type(se.dst, dst_ty);
                    new_val = Value(dst_ty);
                    // TODO: Assert that type is an array
                    auto inner_ty = dst_ty.get_inner();
                    size_t stride = inner_ty.get_size();

                    size_t ofs = 0;
                    for(size_t i = 0; i < re.count; i++)
                    {
                        new_val.write_value(ofs, state.param_to_value(re.val));
                        ofs += stride;
                    }
                    } break;
                TU_ARM(se.src, Variant, re) {
                    // 1. Get the composite by path.
                    const auto& data_ty = state.modtree.get_composite(re.path);
                    auto dst_ty = ::HIR::TypeRef(&data_ty);
                    new_val = Value(dst_ty);
                    // Three cases:
                    // - Unions (no tag)
                    // - Data enums (tag and data)
                    // - Value enums (no data)
                    const auto& var = data_ty.variants.at(re.index);
                    if( var.data_field != SIZE_MAX )
                    {
                        const auto& fld = data_ty.fields.at(re.index);

                        new_val.write_value(fld.first, state.param_to_value(re.val));
                    }
                    if( var.base_field != SIZE_MAX )
                    {
                        ::HIR::TypeRef  tag_ty;
                        size_t tag_ofs = dst_ty.get_field_ofs(var.base_field, var.field_path, tag_ty);
                        LOG_ASSERT(tag_ty.get_size() == var.tag_data.size(), "");
                        new_val.write_bytes(tag_ofs, var.tag_data.data(), var.tag_data.size());
                    }
                    else
                    {
                        // Union, no tag
                    }
                    } break;
                TU_ARM(se.src, Struct, re) {
                    const auto& data_ty = state.modtree.get_composite(re.path);

                    ::HIR::TypeRef  dst_ty;
                    state.get_value_and_type(se.dst, dst_ty);
                    new_val = Value(dst_ty);
                    LOG_ASSERT(dst_ty.composite_type == &data_ty, "Destination type of RValue::Struct isn't the same as the input");

                    for(size_t i = 0; i < re.vals.size(); i++)
                    {
                        auto fld_ofs = data_ty.fields.at(i).first;
                        new_val.write_value(fld_ofs, state.param_to_value(re.vals[i]));
                    }
                    } break;
                }
                LOG_DEBUG("- " << new_val);
                state.write_lvalue(se.dst, ::std::move(new_val));
                } break;
            case ::MIR::Statement::TAG_Asm:
                LOG_TODO(stmt);
                break;
            TU_ARM(stmt, Drop, se) {
                if( se.flag_idx == ~0u || state.drop_flags.at(se.flag_idx) )
                {
                    auto drop_value = [](ValueRef v, const ::HIR::TypeRef& ty) {
                        if( ty.wrappers.empty() )
                        {
                            if( ty.inner_type == RawType::Composite )
                            {
                                if( ty.composite_type->drop_glue != ::HIR::Path() )
                                {
                                    LOG_TODO("Drop - " << ty);
                                }
                                else
                                {
                                    // No drop glue
                                }
                            }
                            else if( ty.inner_type == RawType::TraitObject )
                            {
                                LOG_TODO("Drop - " << ty);
                            }
                            else
                            {
                                // No destructor
                            }
                        }
                        else if( ty.wrappers[0].type == TypeWrapper::Ty::Borrow )
                        {
                            if( ty.wrappers[0].size == static_cast<size_t>(::HIR::BorrowType::Move) )
                            {
                                LOG_TODO("Drop - " << ty << " - dereference and go to inner");
                                // TODO: Clear validity on the entire inner value.
                            }
                            else
                            {
                                // No destructor
                            }
                        }
                        // TODO: Arrays
                        else
                        {
                            LOG_TODO("Drop - " << ty);
                        }

                        };

                    ::HIR::TypeRef  ty;
                    auto v = state.get_value_and_type(se.slot, ty);
                    drop_value(v, ty);
                    // TODO: Clear validity on the entire inner value.
                }
                } break;
            TU_ARM(stmt, SetDropFlag, se) {
                bool val = (se.other == ~0 ? false : state.drop_flags.at(se.other)) != se.new_val;
                LOG_DEBUG("- " << val);
                state.drop_flags.at(se.idx) = val;
                } break;
            case ::MIR::Statement::TAG_ScopeEnd:
                LOG_TODO(stmt);
                break;
            }
        }

        LOG_DEBUG("BB" << bb_idx << "/TERM: " << bb.terminator);
        switch(bb.terminator.tag())
        {
        case ::MIR::Terminator::TAGDEAD:    throw "";
        TU_ARM(bb.terminator, Incomplete, _te)
            LOG_TODO("Terminator::Incomplete hit");
        TU_ARM(bb.terminator, Diverge, _te)
            LOG_TODO("Terminator::Diverge hit");
        TU_ARM(bb.terminator, Panic, _te)
            LOG_TODO("Terminator::Panic");
        TU_ARM(bb.terminator, Goto, te)
            bb_idx = te;
            continue;
        TU_ARM(bb.terminator, Return, _te)
            return state.ret;
        TU_ARM(bb.terminator, If, te) {
            uint8_t v = state.get_value_ref(te.cond).read_u8(0);
            LOG_ASSERT(v == 0 || v == 1, "");
            bb_idx = v ? te.bb0 : te.bb1;
            } continue;
        TU_ARM(bb.terminator, Switch, te) {
            ::HIR::TypeRef ty;
            auto v = state.get_value_and_type(te.val, ty);
            LOG_ASSERT(ty.wrappers.size() == 0, "" << ty);
            LOG_ASSERT(ty.inner_type == RawType::Composite, "" << ty);

            // TODO: Convert the variant list into something that makes it easier to switch on.
            size_t found_target = SIZE_MAX;
            size_t default_target = SIZE_MAX;
            for(size_t i = 0; i < ty.composite_type->variants.size(); i ++)
            {
                const auto& var = ty.composite_type->variants[i];
                if( var.tag_data.size() == 0 )
                {
                    // Save as the default, error for multiple defaults
                    if( default_target != SIZE_MAX )
                    {
                        LOG_FATAL("Two variants with no tag in Switch");
                    }
                    default_target = i;
                }
                else
                {
                    // Get offset, read the value.
                    ::HIR::TypeRef  tag_ty;
                    size_t tag_ofs = ty.get_field_ofs(var.base_field, var.field_path, tag_ty);
                    // Read the value bytes
                    ::std::vector<char> tmp( var.tag_data.size() );
                    v.read_bytes(tag_ofs, const_cast<char*>(tmp.data()), tmp.size());
                    if( ::std::memcmp(tmp.data(), var.tag_data.data(), tmp.size()) == 0 )
                    {
                        found_target = i;
                        break ;
                    }
                }
            }

            if( found_target == SIZE_MAX )
            {
                found_target = default_target;
            }
            if( found_target == SIZE_MAX )
            {
                LOG_FATAL("Terminator::Switch on " << ty << " didn't find a variant");
            }
            bb_idx = te.targets.at(found_target);
            } continue;
        TU_ARM(bb.terminator, SwitchValue, _te)
            LOG_TODO("Terminator::SwitchValue");
        TU_ARM(bb.terminator, Call, te) {
            ::std::vector<Value>    sub_args; sub_args.reserve(te.args.size());
            for(const auto& a : te.args)
            {
                sub_args.push_back( state.param_to_value(a) );
            }
            if( te.fcn.is_Intrinsic() )
            {
                const auto& fe = te.fcn.as_Intrinsic();
                state.write_lvalue(te.ret_val, MIRI_Invoke_Intrinsic(modtree, fe.name, fe.params, ::std::move(sub_args)));
            }
            else
            {
                const ::HIR::Path* fcn_p;
                if( te.fcn.is_Path() ) {
                    fcn_p = &te.fcn.as_Path();
                }
                else {
                    ::HIR::TypeRef ty;
                    auto v = state.get_value_and_type(te.fcn.as_Value(), ty);
                    // TODO: Assert type
                    // TODO: Assert offset/content.
                    assert(v.read_usize(v.m_offset) == 0);
                    auto& alloc_ptr = v.m_alloc ? v.m_alloc : v.m_value->allocation;
                    LOG_ASSERT(alloc_ptr, "");
                    auto& fcn_alloc_ptr = alloc_ptr.alloc().get_relocation(v.m_offset);
                    LOG_ASSERT(fcn_alloc_ptr, "");
                    fcn_p = &fcn_alloc_ptr.fcn();
                }

                LOG_DEBUG("Call " << *fcn_p);
                state.write_lvalue(te.ret_val, MIRI_Invoke(modtree, *fcn_p, ::std::move(sub_args)));
            }
            bb_idx = te.ret_block;
            } continue;
        }
        throw "";
    }

    throw "";
}
Value MIRI_Invoke_Extern(const ::std::string& link_name, const ::std::string& abi, ::std::vector<Value> args)
{
    // WinAPI functions used by libstd
    if( link_name == "AddVectoredExceptionHandler" )
    {
        LOG_DEBUG("Call `AddVectoredExceptionHandler` - Ignoring and returning non-null");
        auto rv = Value(::HIR::TypeRef(RawType::USize));
        rv.write_usize(0, 1);
        return rv;
    }
    // Allocators!
    else if( link_name == "__rust_allocate" )
    {
        auto size = args.at(0).read_usize(0);
        auto align = args.at(1).read_usize(0);
        LOG_DEBUG("__rust_allocate(size=" << size << ", align=" << align << ")");
        ::HIR::TypeRef  rty { RawType::Unit };
        rty.wrappers.push_back({ TypeWrapper::Ty::Pointer, 0 });
        Value rv = Value(rty);
        // TODO: Use the alignment when making an allocation.
        rv.allocation.alloc().relocations.push_back({ 0,  Allocation::new_alloc(size) });
        return rv;
    }
    else if( link_name == "__rust_reallocate" )
    {
        LOG_ASSERT(args.at(0).allocation, "__rust_reallocate first argument doesn't have an allocation");
        auto alloc_ptr = args.at(0).allocation.alloc().get_relocation(0);
        auto ptr_ofs = args.at(0).read_usize(0);
        LOG_ASSERT(ptr_ofs == 0, "__rust_reallocate with offset pointer");
        auto oldsize = args.at(1).read_usize(0);
        auto newsize = args.at(2).read_usize(0);
        auto align = args.at(3).read_usize(0);
        LOG_DEBUG("__rust_reallocate(ptr=" << alloc_ptr << ", oldsize=" << oldsize << ", newsize=" << newsize << ", align=" << align << ")");

        LOG_ASSERT(alloc_ptr, "__rust_reallocate with no backing allocation attached to pointer");
        LOG_ASSERT(alloc_ptr.is_alloc(), "__rust_reallocate with no backing allocation attached to pointer");
        auto& alloc = alloc_ptr.alloc();
        // TODO: Check old size and alignment against allocation.
        alloc.data.resize(newsize);
        // TODO: Should this instead make a new allocation to catch use-after-free?
        return ::std::move(args.at(0));
    }
    else
    {
        LOG_TODO("Call external function " << link_name);
    }
}
Value MIRI_Invoke_Intrinsic(const ModuleTree& modtree, const ::std::string& name, const ::HIR::PathParams& ty_params, ::std::vector<Value> args)
{
    Value rv;
    TRACE_FUNCTION_R(name, rv);
    for(const auto& a : args)
        LOG_DEBUG("#" << (&a - args.data()) << ": " << a);
    if( name == "atomic_store" )
    {
        auto& ptr_val = args.at(0);
        auto& data_val = args.at(1);

        LOG_ASSERT(ptr_val.size() == POINTER_SIZE, "atomic_store of a value that isn't a pointer-sized value");

        // There MUST be a relocation at this point with a valid allocation.
        LOG_ASSERT(ptr_val.allocation, "Deref of a value with no allocation (hence no relocations)");
        LOG_TRACE("Deref " << ptr_val.allocation.alloc());
        auto alloc = ptr_val.allocation.alloc().get_relocation(0);
        LOG_ASSERT(alloc, "Deref of a value with no relocation");

        // TODO: Atomic side of this?
        size_t ofs = ptr_val.read_usize(0);
        const auto& ty = ty_params.tys.at(0);
        alloc.alloc().write_value(ofs, ::std::move(data_val));
    }
    else if( name == "atomic_load" )
    {
        auto& ptr_val = args.at(0);
        LOG_ASSERT(ptr_val.size() == POINTER_SIZE, "atomic_store of a value that isn't a pointer-sized value");

        // There MUST be a relocation at this point with a valid allocation.
        LOG_ASSERT(ptr_val.allocation, "Deref of a value with no allocation (hence no relocations)");
        LOG_TRACE("Deref " << ptr_val.allocation.alloc());
        auto alloc = ptr_val.allocation.alloc().get_relocation(0);
        LOG_ASSERT(alloc, "Deref of a value with no relocation");

        // TODO: Atomic side of this?
        size_t ofs = ptr_val.read_usize(0);
        const auto& ty = ty_params.tys.at(0);
        rv = alloc.alloc().read_value(ofs, ty.get_size());
    }
    else if( name == "transmute" )
    {
        // Transmute requires the same size, so just copying the value works
        rv = ::std::move(args.at(0));
    }
    else if( name == "assume" )
    {
        // Assume is a no-op which returns unit
    }
    else if( name == "offset" )
    {
        auto ptr_val = ::std::move(args.at(0));
        auto& ofs_val = args.at(1);

        auto r = ptr_val.allocation.alloc().get_relocation(0);
        auto orig_ofs = ptr_val.read_usize(0);
        auto delta_ofs = ptr_val.read_usize(0);
        auto new_ofs = orig_ofs + delta_ofs;
        if(POINTER_SIZE != 8) {
            new_ofs &= 0xFFFFFFFF;
        }


        ptr_val.write_usize(0, new_ofs);
        ptr_val.allocation.alloc().relocations.push_back({ 0, r });
        return ptr_val;
    }
    // effectively ptr::write
    else if( name == "move_val_init" )
    {
        auto& ptr_val = args.at(0);
        auto& data_val = args.at(1);

        LOG_ASSERT(ptr_val.size() == POINTER_SIZE, "move_val_init of an address that isn't a pointer-sized value");

        // There MUST be a relocation at this point with a valid allocation.
        LOG_ASSERT(ptr_val.allocation, "Deref of a value with no allocation (hence no relocations)");
        LOG_TRACE("Deref " << ptr_val.allocation.alloc());
        auto alloc = ptr_val.allocation.alloc().get_relocation(0);
        LOG_ASSERT(alloc, "Deref of a value with no relocation");

        size_t ofs = ptr_val.read_usize(0);
        const auto& ty = ty_params.tys.at(0);
        alloc.alloc().write_value(ofs, ::std::move(data_val));
    }
    // ----------------------------------------------------------------
    // Checked arithmatic
    else if( name == "add_with_overflow" )
    {
        const auto& ty = ty_params.tys.at(0);

        auto lhs = PrimitiveValueVirt::from_value(ty, args.at(0));
        auto rhs = PrimitiveValueVirt::from_value(ty, args.at(1));
        bool didnt_overflow = lhs.get().add( rhs.get() );

        // Get return type - a tuple of `(T, bool,)`
        ::HIR::GenericPath  gp;
        gp.m_params.tys.push_back(ty);
        gp.m_params.tys.push_back(::HIR::TypeRef { RawType::Bool });
        const auto& dty = modtree.get_composite(gp);

        rv = Value(::HIR::TypeRef(&dty));
        lhs.get().write_to_value(rv, dty.fields[0].first);
        rv.write_u8( dty.fields[1].first, didnt_overflow ? 0 : 1 ); // Returns true if overflow happened
    }
    else if( name == "sub_with_overflow" )
    {
        const auto& ty = ty_params.tys.at(0);

        auto lhs = PrimitiveValueVirt::from_value(ty, args.at(0));
        auto rhs = PrimitiveValueVirt::from_value(ty, args.at(1));
        bool didnt_overflow = lhs.get().subtract( rhs.get() );

        // Get return type - a tuple of `(T, bool,)`
        ::HIR::GenericPath  gp;
        gp.m_params.tys.push_back(ty);
        gp.m_params.tys.push_back(::HIR::TypeRef { RawType::Bool });
        const auto& dty = modtree.get_composite(gp);

        rv = Value(::HIR::TypeRef(&dty));
        lhs.get().write_to_value(rv, dty.fields[0].first);
        rv.write_u8( dty.fields[1].first, didnt_overflow ? 0 : 1 ); // Returns true if overflow happened
    }
    else if( name == "mul_with_overflow" )
    {
        const auto& ty = ty_params.tys.at(0);
    
        auto lhs = PrimitiveValueVirt::from_value(ty, args.at(0));
        auto rhs = PrimitiveValueVirt::from_value(ty, args.at(1));
        bool didnt_overflow = lhs.get().multiply( rhs.get() );

        // Get return type - a tuple of `(T, bool,)`
        ::HIR::GenericPath  gp;
        gp.m_params.tys.push_back(ty);
        gp.m_params.tys.push_back(::HIR::TypeRef { RawType::Bool });
        const auto& dty = modtree.get_composite(gp);

        rv = Value(::HIR::TypeRef(&dty));
        lhs.get().write_to_value(rv, dty.fields[0].first);
        rv.write_u8( dty.fields[1].first, didnt_overflow ? 0 : 1 ); // Returns true if overflow happened
    }
    // Overflowing artithmatic
    else if( name == "overflowing_sub" )
    {
        const auto& ty = ty_params.tys.at(0);

        auto lhs = PrimitiveValueVirt::from_value(ty, args.at(0));
        auto rhs = PrimitiveValueVirt::from_value(ty, args.at(1));
        lhs.get().subtract( rhs.get() );

        rv = Value(ty);
        lhs.get().write_to_value(rv, 0);
    }
    // ----------------------------------------------------------------
    else
    {
        LOG_TODO("Call intrinsic \"" << name << "\"");
    }
    return rv;
}

int ProgramOptions::parse(int argc, const char* argv[])
{
    bool all_free = false;
    for(int argidx = 1; argidx < argc; argidx ++)
    {
        const char* arg = argv[argidx]; 
        if( arg[0] != '-' || all_free )
        {
            // Free
            if( this->infile == "" )
            {
                this->infile = arg;
            }
            else
            {
                // TODO: Too many free arguments
            }
        }
        else if( arg[1] != '-' )
        {
            // Short
        }
        else if( arg[2] != '\0' )
        {
            // Long
        }
        else
        {
            all_free = true;
        }
    }
    return 0;
}