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
use yaxpeax_arch::Arch;
use data::ValueLocations;

#[macro_use]
pub mod control_flow;
pub mod data_flow;
pub mod function_signatures;
pub mod memory_layout;
pub mod static_single_assignment;
pub mod xrefs;
pub mod evaluators;
pub mod value_range;

pub enum CompletionStatus {
    Incomplete,
    Complete,
}

pub struct ValueRes<V> {
    pub value: V,
    pub carry: V,
}

#[allow(dead_code)]
impl<V: Value> ValueRes<V> {
    pub(crate) fn from_zero() -> Self {
        ValueRes::literal(V::from_const(0))
    }
    pub(crate) fn literal(value: V) -> Self {
        ValueRes {
            value,
            carry: V::from_const(0),
        }
    }
    pub(crate) fn unknown() -> Self {
        ValueRes {
            value: V::unknown(),
            carry: V::unknown(),
        }
    }
    pub(crate) fn parts(self) -> (V, V) {
        (self.value, self.carry)
    }
    pub(crate) fn value(self) -> V {
        self.value
    }
    pub(crate) fn carry(self) -> V {
        self.carry
    }
    pub(crate) fn zero(&self) -> V {
        self.value.eq(&V::from_const(0))
    }
    pub(crate) fn sign(&self) -> V {
        self.value.lt(&V::from_const(0))
    }
}

/// interface to query a data flow graph (dfg). this interface is .... in flux.
///
/// TODOs in order of "how hard i think they are":
/// * it should be possible to look up a def site for a value
/// * it should be possible to iterate the use sites of a value
/// * perhaps it should be possible to insert new values to the dfg? optionally? this approaches
/// supporting general patching
/// * it should be possible to detach and move values
///
/// conceptually, these graphs have vertices at places where values are read or written, edges
/// from uses to some write, and a value associated with the write describing what subsequent reads
/// will see. these graphs describe the relation between values in a machine with
/// architecture-defined locations for values to exist. in many cases these graphs are operated on
/// in a manner consistent with the most atomic changes for a given architcture - typically an
/// instruction's execution. in an ideal world, this means `DFG` would have vertices at a pair
/// `(A::Address, A::Instruction, A::Location)`; "at a given address in memory, with a
/// corresponding instruction, the value at a specific architectural location is ___".
///
/// why is using an `(Address, Location)` pair, like `(0x1234, rdi)` not sufficient to uniquely
/// identify a location? because, dear reader, data at an address is not constant. if you decode
/// data at address `0x1234`, is that before or after relocations are applied? if that address is
/// known to be modified after loading, is the instruction there before or after the modification?
/// different answers to this temporal question mean the architectural locations referenced by the
/// corresponding instruction can be totally different!
///
/// so, really, a `DFG` describes the architectural state of a program at every discrete point of
/// change for any point in the program. an eventual TODO is to key on `(Address, Generation)`
/// where a "Generation" describes some series of memory edits. this is approximately supported in
/// SSA-based DFG construction, where `Memory` is a single architectural location that can be
/// versioned - perhaps "the program" may be inferred to a distinct memory region from
/// unknown-destination memorry accesses by default? in a way, a `DFG` might be self-describing if
/// at some location `(0x1234, Gen1)` the instruction modifies code memory by writing `(0x1236,
/// Gen2)`, where finding bytes to decode the next instruction would have to be a DFG query? this
/// suggests that in the most precise case, a DFG might be backed by a `MemoryRepr` with a series
/// of edits for each generation layered on top? it's not clear how this might interact with
/// disjoint memory regions that are versioned independently.
pub trait DFG<V: Value, A: Arch + ValueLocations, When=<A as Arch>::Address> where When: Copy {
    type Indirect: IndirectQuery<V>;

    fn read_loc(&self, when: When, loc: A::Location) -> V;
    fn read<T: ToDFGLoc<A::Location>>(&self, when: When, loc: &T) -> V {
        self.read_loc(when, loc.convert())
    }
    fn write_loc(&mut self, when: When, loc: A::Location, value: V);
    fn write<T: ToDFGLoc<A::Location>>(&mut self, when: When, loc: &T, value: V) {
        self.write_loc(when, loc.convert(), value)
    }
    // in an ideal world with a GAT-enabled rustc, `Indirect` would be spelled as
    // ```
    // type Indirect<'dfg>: IndirectQuery<'dfg>;
    // ```
    // and could retain a `&'dfg mut DFG` to do queries through. because we do not have GAT, that
    // associated type cannot be written, and we must obligate `Self::Indirect` and DFG impls to
    // holding `Rc<RefCell<IndirectionStruct>>` even though we know no user of a `DFG` interface
    // can get multiple refs, let alone mutable refs.
    fn indirect_loc(&self, _when: When, _loc: A::Location) -> Self::Indirect;
    fn indirect<T: ToDFGLoc<A::Location>>(&self, when: When, loc: &T) -> Self::Indirect {
        self.indirect_loc(when, loc.convert())
    }
    fn query_at(&self, when: When) -> DFGLocationQueryCursor<When, V, A, Self> {
        DFGLocationQueryCursor {
            dfg: self,
            addr: when,
            _a: std::marker::PhantomData,
            _v: std::marker::PhantomData,
        }
    }
    fn query_at_mut(&mut self, when: When) -> DFGLocationQueryCursorMut<When, V, A, Self> {
        DFGLocationQueryCursorMut {
            dfg: self,
            addr: when,
            _a: std::marker::PhantomData,
            _v: std::marker::PhantomData,
        }
    }
}

pub struct DFGLocationQueryCursor<'dfg, K: Copy + Sized, V: Value, A: Arch + ValueLocations, D: DFG<V, A, K> + ?Sized> {
    dfg: &'dfg D,
    addr: K,
    _a: std::marker::PhantomData<A>,
    _v: std::marker::PhantomData<V>,
}

pub struct DFGLocationQueryCursorMut<'dfg, K: Copy + Sized, V: Value, A: Arch + ValueLocations, D: DFG<V, A, K> + ?Sized> {
    dfg: &'dfg mut D,
    addr: K,
    _a: std::marker::PhantomData<A>,
    _v: std::marker::PhantomData<V>,
}

impl<'dfg, K: Copy, V: Value, A: Arch + ValueLocations, D: DFG<V, A, K> + ?Sized> DFGLocationQuery<V, A> for DFGLocationQueryCursor<'dfg, K, V, A, D> {
    type Indirect = D::Indirect;

    fn read_loc(&self, loc: A::Location) -> V {
        self.dfg.read_loc(self.addr, loc)
    }
    fn read<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> V {
        self.read_loc(loc.convert())
    }
    fn indirect_loc(&self, loc: A::Location) -> Self::Indirect {
        self.dfg.indirect_loc(self.addr, loc)
    }
    fn indirect<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> Self::Indirect {
        self.indirect_loc(loc.convert())
    }
}
impl<'dfg, K: Copy, V: Value, A: Arch + ValueLocations, D: DFG<V, A, K> + ?Sized> DFGLocationQuery<V, A> for DFGLocationQueryCursorMut<'dfg, K, V, A, D> {
    type Indirect = D::Indirect;

    fn read_loc(&self, loc: A::Location) -> V {
        self.dfg.read_loc(self.addr, loc)
    }
    fn read<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> V {
        self.read_loc(loc.convert())
    }
    fn indirect_loc(&self, loc: A::Location) -> Self::Indirect {
        self.dfg.indirect_loc(self.addr, loc)
    }
    fn indirect<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> Self::Indirect {
        self.indirect_loc(loc.convert())
    }
}
impl<'dfg, K: Copy, V: Value, A: Arch + ValueLocations, D: DFG<V, A, K> + ?Sized> DFGLocationQueryMut<V, A> for DFGLocationQueryCursorMut<'dfg, K, V, A, D> {
    fn write_loc(&mut self, loc: A::Location, value: V) {
        self.dfg.write_loc(self.addr, loc, value)
    }
    fn write<T: ToDFGLoc<A::Location>>(&mut self, loc: &T, value: V) {
        self.write_loc(loc.convert(), value)
    }
}

/// it's relatively common to want to query a dfg at one address, for multiple values/read-write
/// accesses. `DFGLocationQuery` is essentially a helper to curry a specific address for future
/// queries.
pub trait DFGLocationQuery<V: Value, A: Arch + ValueLocations> where Self: Sized {
    type Indirect: IndirectQuery<V>;

    fn read_loc(&self, loc: A::Location) -> V;
    fn read<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> V {
        self.read_loc(loc.convert())
    }
    fn indirect_loc(&self, loc: A::Location) -> Self::Indirect;
    fn indirect<T: ToDFGLoc<A::Location>>(&self, loc: &T) -> Self::Indirect {
        self.indirect_loc(loc.convert())
    }
}

pub trait DFGLocationQueryMut<V: Value, A: Arch + ValueLocations> where Self: Sized + DFGLocationQuery<V, A> {
    fn write_loc(&mut self, loc: A::Location, value: V);
    fn write<T: ToDFGLoc<A::Location>>(&mut self, loc: &T, value: V) {
        self.write_loc(loc.convert(), value)
    }
}

pub trait ToDFGLoc<U> {
    fn convert(&self) -> U;
}

impl<T: Clone> ToDFGLoc<T> for T {
    fn convert(&self) -> T {
        self.to_owned()
    }
}

impl<'a, T: Clone> ToDFGLoc<T> for &'a T {
    fn convert(&self) -> T {
        self.to_owned().to_owned()
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ValueIndex<'v, V: ?Sized> {
    pub base: &'v V,
    pub size: usize,
}

impl<'v, V: ?Sized + crate::analyses::static_single_assignment::DataDisplay<'v, 'static>> fmt::Display for ValueIndex<'v, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}, width: {}]", self.base.display(false, None), self.size)
    }
}

pub trait IntoValueIndex {
    fn byte(&self) -> ValueIndex<Self> {
        ValueIndex {
            base: self,
            size: 1,
        }
    }

    fn word(&self) -> ValueIndex<Self> {
        ValueIndex {
            base: self,
            size: 2,
        }
    }

    fn dword(&self) -> ValueIndex<Self> {
        ValueIndex {
            base: self,
            size: 4,
        }
    }

    fn qword(&self) -> ValueIndex<Self> {
        ValueIndex {
            base: self,
            size: 8,
        }
    }

    fn width(&self, size: usize) -> ValueIndex<Self> {
        ValueIndex {
            base: self,
            size,
        }
    }
}

impl <T: Value> IntoValueIndex for T {}

pub trait IndirectQuery<V> {
    /// attempt to load `address` indirectly through this value, indexed by `address`.
    ///
    /// ## panics
    ///
    /// * if `self` does not represent a value that can be indexed. for example, the value backing
    /// a register can probably not be indexed. the value backing main memory probably can be
    /// indexed.
    fn load(&self, _address: ValueIndex<V>) -> V;

    /// attempt to load `address` indirectly through this value, indexed by `address`.
    ///
    /// ## panics
    ///
    /// * if `self` does not represent a value that can be indexed. for example, the value backing
    /// a register can probably not be indexed. the value backing main memory probably can be
    /// indexed.
    fn store(&self, _address: ValueIndex<V>, _value: &V);

    /// check if `address` is a load known to this query cursor. get the referenced value if so,
    /// `None` if not.
    ///
    /// ## panics
    ///
    /// * if `self` does not represent a value that can be indexed. for example, the value backing
    /// a register can probably not be indexed. the value backing main memory probably can be
    /// indexed.
    fn try_get_load(&self, _address: ValueIndex<V>) -> Option<V>;

    /// check if `address` is a store known to this query cursor. `Some(())` if it is, `None` if not.
    ///
    /// ## panics
    ///
    /// * if `self` does not represent a value that can be indexed. for example, the value backing
    /// a register can probably not be indexed. the value backing main memory probably can be
    /// indexed.
    fn try_get_store(&self, _address: ValueIndex<V>) -> Option<()>;
}

pub struct OpaqueIndirection<V> {
    _marker: std::marker::PhantomData<V>,
}

impl<V> OpaqueIndirection<V> {
    pub fn inst() -> Self {
        OpaqueIndirection {
            _marker: std::marker::PhantomData
        }
    }
}


impl<V: Value> IndirectQuery<V> for OpaqueIndirection<V> {
    fn load(&self, _: ValueIndex<V>) -> V {
        V::unknown()
    }

    fn store(&self, _: ValueIndex<V>, _: &V) { }

    fn try_get_load(&self, _: ValueIndex<V>) -> Option<V> {
        None
    }

    fn try_get_store(&self, _: ValueIndex<V>) -> Option<()> {
        None
    }
}

pub trait Value: Sized {
//    type Indirect: IndirectQuery<Self>; // associated type defaults are unstable, but should be `= OpaqueIndirection<V>`

    fn unknown() -> Self;

    fn from_const(_c: i64) -> Self {
        Self::unknown()
    }

    fn from_set(xs: &[Self]) -> Self;

    fn to_const(&self) -> Option<i64>;

    fn as_bool(&self) -> Option<bool> {
        self.ne(&Value::from_const(0)).to_const().map(|x| x != 0)
    }

    fn add(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn sub(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn mul(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn or(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn and(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn xor(&self, _other: &Self) -> ValueRes<Self> {
        ValueRes::unknown()
    }

    fn modulo(&self, other: &Self) -> Self {
        match (self.to_const(), other.to_const()) {
            (Some(x), Some(y)) => {
                Value::from_const(x % y)
            }
            _ => {
                Self::unknown()
            }
        }
    }

    fn ne(&self, _other: &Self) -> Self {
        Self::unknown()
    }

    fn le(&self, _other: &Self) -> Self {
        Self::unknown()
    }

    fn lt(&self, other: &Self) -> Self {
        self.le(other).and(&self.eq(other).not()).value()
    }

    fn gte(&self, other: &Self) -> Self {
        self.lt(other).not()
    }

    fn eq(&self, _other: &Self) -> Self {
        Self::unknown()
    }

    fn not(&self) -> Self {
        Self::unknown()
    }

    fn sxt(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn zxt(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn shr(&self, _amt: &Self) -> Self {
        Self::unknown()
    }

    fn sar(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn shl(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn sal(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn rcl(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn rcr(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn rol(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn ror(&self, _width: &Self) -> Self {
        Self::unknown()
    }
}

use SSAValues;
use std::rc::Rc;
use analyses::static_single_assignment::{DataDisplay, DFGRef};
use crate::ColorSettings;
use data::types::TypeSpec;

#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Item<Leaf> {
    pub ty: Option<TypeSpec>,
    pub value: Expression<Leaf>,
}

pub struct ItemDisplay<'data, 'colors, Leaf> where Leaf: DataDisplay<'data, 'colors> {
    data: &'data Rc<Item<Leaf>>,
    detailed: bool,
    colors: Option<&'colors ColorSettings>,
}

impl<'data, 'colors, Leaf: 'data + DataDisplay<'data, 'colors>> DataDisplay<'data, 'colors> for Rc<Item<Leaf>> {
    type Displayer = ItemDisplay<'data, 'colors, Leaf>;
    fn display(&'data self, detailed: bool, colors: Option<&'colors ColorSettings>) -> Self::Displayer {
        ItemDisplay {
            data: self,
            detailed,
            colors
        }
    }
}

impl<'data, 'colors, Leaf: 'data + DataDisplay<'data, 'colors>> fmt::Display for ItemDisplay<'data, 'colors, Leaf> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(ty) = self.data.ty.as_ref() {
            write!(f, "({}: {:?})", self.data.value.display(self.detailed, self.colors), ty)
        } else {
            write!(f, "{}", self.data.value.display(self.detailed, self.colors))
        }
    }
}

impl<Leaf: fmt::Display> fmt::Display for Item<Leaf> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.value, f)
    }
}

impl<A: SSAValues> Item<ValueOrImmediate<A>> where A::Data: Eq + fmt::Display {
    pub fn immediate(v: i64) -> Rc<Self> {
        Self::untyped(Expression::value(ValueOrImmediate::Immediate(v)))
    }
}

use analyses::memory_layout::Underlying;
impl<A: SSAValues> Item<ValueOrImmediate<A>> where A::Data: Underlying<Arch=A> + Eq + fmt::Display {
    pub fn dealiased(&self) -> Rc<Self> {
        let ty = self.ty.clone();
        let value = self.value.dealiased();
        Rc::new(Item { ty, value })
    }
}

impl<A: SSAValues> Item<ValueOrImmediate<A>> where A::Data: Eq + fmt::Display{
}

impl<Leaf> Item<Leaf> {
    pub fn untyped(value: Expression<Leaf>) -> Rc<Self> {
        Rc::new(Item {
            ty: None,
            value,
        })
    }

    pub fn opaque(ty: TypeSpec) -> Rc<Self> {
        Rc::new(Item {
            ty: Some(ty),
            value: Expression::unknown()
        })
    }

    pub fn unknown() -> Rc<Self> {
        Self::untyped(Expression::unknown())
    }

    pub fn value(leaf: Leaf) -> Rc<Self> {
        Self::untyped(Expression::value(leaf))
    }

    pub fn loadb(self: &Rc<Self>) -> Rc<Self> {
        self.load(1)
    }

    pub fn loadw(self: &Rc<Self>) -> Rc<Self> {
        self.load(2)
    }

    pub fn loadd(self: &Rc<Self>) -> Rc<Self> {
        self.load(4)
    }

    pub fn loadq(self: &Rc<Self>) -> Rc<Self> {
        self.load(8)
    }

    pub fn load(self: &Rc<Self>, size: u8) -> Rc<Self> {
        Self::untyped(Expression::Load { address: Rc::clone(self), size })
    }

    pub fn add(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Add { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn sub(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Sub { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn mul(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Mul { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn or(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Or { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn and(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::And { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn xor(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Xor { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn shl(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Shl { value: Rc::clone(self), amount: Rc::clone(other) })
    }

    pub fn shr(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Self::untyped(Expression::Shr { value: Rc::clone(self), amount: Rc::clone(other) })
    }
}

/// a very literal construction of `Value` operations into an expression tree. if `Leaf` is only
/// concrete values, this is suitable for computation. if `Leaf` can be abstract variables, this is
/// becomes a symbolic expression tree. realistically these expressions are often a mix of the two;
/// constants from immediates and variables from the rest of the program.
#[derive(Debug, Eq, Hash, PartialEq)]
pub enum Expression<Leaf> {
    Unknown,
    Value(Leaf),
    Load { address: Rc<Item<Leaf>>, size: u8 },
    // TODO: should there be a corresponding "store"?
    // Store { address: Rc<Item<Leaf>>, size: u8, value: Rc<Item<Leaf>> },
    Add { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    Sub { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    Mul { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    Or { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    And { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    Xor { left: Rc<Item<Leaf>>, right: Rc<Item<Leaf>> },
    Shl { value: Rc<Item<Leaf>>, amount: Rc<Item<Leaf>> },
    Shr { value: Rc<Item<Leaf>>, amount: Rc<Item<Leaf>> },
}

pub struct ExpressionDisplay<'data, 'colors, Leaf: DataDisplay<'data, 'colors>> {
    data: &'data Expression<Leaf>,
    detailed: bool,
    colors: Option<&'colors ColorSettings>,
}

impl<'data, 'colors, Leaf: 'data + DataDisplay<'data, 'colors>> DataDisplay<'data, 'colors> for Expression<Leaf> {
    type Displayer = ExpressionDisplay<'data, 'colors, Leaf>;
    fn display(&'data self, detailed: bool, colors: Option<&'colors ColorSettings>) -> Self::Displayer {
        ExpressionDisplay {
            data: self,
            detailed,
            colors
        }
    }
}

impl<'data, 'colors, Leaf: 'data + DataDisplay<'data, 'colors>> fmt::Display for ExpressionDisplay<'data, 'colors, Leaf> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.data {
            Expression::Unknown => {
                write!(f, "<unknown>")
            }
            Expression::Value(l) => {
                write!(f, "{}", l.display(self.detailed, self.colors))
            }
            Expression::Load { address, size } => {
                write!(f, "[{}:{}]", address.display(self.detailed, self.colors), size)
            }
            Expression::Add { left, right } => {
                write!(f, "({} + {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::Sub { left, right } => {
                write!(f, "({} - {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::Mul { left, right } => {
                write!(f, "({} * {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::Or { left, right } => {
                write!(f, "({} | {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::And { left, right } => {
                write!(f, "({} & {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::Xor { left, right } => {
                write!(f, "({} ^ {})", left.display(self.detailed, self.colors), right.display(self.detailed, self.colors))
            }
            Expression::Shl { value, amount } => {
                write!(f, "({} << {})", value.display(self.detailed, self.colors), amount.display(self.detailed, self.colors))
            }
            Expression::Shr { value, amount } => {
                write!(f, "({} >> {})", value.display(self.detailed, self.colors), amount.display(self.detailed, self.colors))
            }
        }
    }
}

impl<Leaf: fmt::Display> fmt::Display for Expression<Leaf> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Expression::Unknown => {
                write!(f, "<unknown>")
            }
            Expression::Value(l) => {
                write!(f, "{}", l)
            }
            Expression::Load { address, size } => {
                write!(f, "[{}:{}]", address, size)
            }
            Expression::Add { left, right } => {
                write!(f, "({} + {})", left, right)
            }
            Expression::Sub { left, right } => {
                write!(f, "({} - {})", left, right)
            }
            Expression::Mul { left, right } => {
                write!(f, "({} * {})", left, right)
            }
            Expression::Or { left, right } => {
                write!(f, "({} | {})", left, right)
            }
            Expression::And { left, right } => {
                write!(f, "({} & {})", left, right)
            }
            Expression::Xor { left, right } => {
                write!(f, "({} ^ {})", left, right)
            }
            Expression::Shl { value, amount } => {
                write!(f, "({} << {})", value, amount)
            }
            Expression::Shr { value, amount } => {
                write!(f, "({} >> {})", value, amount)
            }
        }
    }
}

impl<T: SSAValues> Expression<ValueOrImmediate<T>> where T::Data: Eq + fmt::Display {
    pub fn as_immediate(&self) -> Option<i64> {
        if let Expression::Value(ValueOrImmediate::Immediate(i)) = self {
            Some(*i)
        } else {
            None
        }
    }
}

impl<A: SSAValues> Expression<ValueOrImmediate<A>> where A::Data: Underlying<Arch=A> + Eq + fmt::Display {
    pub fn dealiased(&self) -> Self {
        match self {
            Expression::Unknown => Expression::Unknown,
            Expression::Value(ValueOrImmediate::Immediate(i)) => Expression::Value(ValueOrImmediate::Immediate(*i)),
            Expression::Value(ValueOrImmediate::Value(v)) => {
                if let Some(underlying) = v.borrow().data.as_ref().and_then(|x| x.underlying()) {
                    Expression::Value(ValueOrImmediate::Value(underlying))
                } else {
                    Expression::Value(ValueOrImmediate::Value(Rc::clone(v)))
                }
            }
            Expression::Load { address, size } => Expression::Load { address: address.dealiased(), size: *size },
            Expression::Add { left, right } => Expression::Add {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::Sub { left, right } => Expression::Sub {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::Mul { left, right } => Expression::Mul {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::Or { left, right } => Expression::Or {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::And { left, right } => Expression::And {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::Xor { left, right } => Expression::Xor {
                left: left.dealiased(), right: right.dealiased(),
            },
            Expression::Shl { value, amount } => Expression::Shl {
                value: value.dealiased(), amount: amount.dealiased(),
            },
            Expression::Shr { value, amount } => Expression::Shr {
                value: value.dealiased(), amount: amount.dealiased(),
            },
        }
    }
}

impl<Leaf> Expression<Leaf> {
    pub fn unknown() -> Self {
        Self::Unknown
    }

    pub fn value(leaf: Leaf) -> Self {
        Self::Value(leaf)
    }

    /*
    pub fn add(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Add { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn sub(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Sub { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn mul(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Mul { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn or(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Or { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn and(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::And { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn xor(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Xor { left: Rc::clone(self), right: Rc::clone(other) })
    }

    pub fn shl(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Shl { value: Rc::clone(self), amount: Rc::clone(other) })
    }

    pub fn shr(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
        Rc::new(Expression::Shr { value: Rc::clone(self), amount: Rc::clone(other) })
    }
    */
}

use std::hash::Hash;
use std::hash::Hasher;
pub enum ValueOrImmediate<A: SSAValues> where A::Data: Eq + fmt::Display {
    Immediate(i64),
    Value(DFGRef<A>),
}

impl<A: SSAValues> ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    pub fn immediate(v: i64) -> Expression<Self> {
        Expression::value(ValueOrImmediate::Immediate(v))
    }

    fn name(&self) -> String {
        match self {
            ValueOrImmediate::Immediate(v) => {
                format!("{}", v)
            }
            ValueOrImmediate::Value(v) => {
                let version_string = match v.borrow().version {
                    Some(v) => { v.to_string() },
                    None => "input".to_string()
                };
                if let Some(_data) = v.borrow().data.as_ref() {
                    format!("{:?}_{}", v.borrow().location, version_string)
                } else {
                    format!("{:?}_{}", v.borrow().location, version_string)
                }
            }
        }
    }
}

use std::fmt;
impl<A: SSAValues> fmt::Debug for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ValueOrImmediate::Immediate(v) => {
                write!(f, "Immediate({})", v)
            }
            ValueOrImmediate::Value(v) => {
                write!(f, "Value({:?})", v)
            }
        }
    }
}

impl<A: SSAValues> fmt::Display for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ValueOrImmediate::Immediate(v) => {
                write!(f, "{}", v)
            }
            ValueOrImmediate::Value(v) => {
                let version_string = match v.borrow().version {
                    Some(v) => { v.to_string() },
                    None => "input".to_string()
                };
                if let Some(data) = v.borrow().data.as_ref() {
                    write!(f, "{} ({:?}_{})", data, v.borrow().location, version_string)
                } else {
                    write!(f, "<unknown value> ({:?}_{})", v.borrow().location, version_string)
                }
            }
        }
    }
}

pub struct LeafDisplay<'data, 'colors, A: SSAValues> where A::Data: Eq + fmt::Display {
    data: &'data ValueOrImmediate<A>,
    detailed: bool,
    colors: Option<&'colors ColorSettings>,
}

impl<'data, 'colors, A: 'data + SSAValues> DataDisplay<'data, 'colors> for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    type Displayer = LeafDisplay<'data, 'colors, A>;
    fn display(&'data self, detailed: bool, colors: Option<&'colors ColorSettings>) -> Self::Displayer {
        LeafDisplay {
            data: self,
            detailed,
            colors
        }
    }
}

impl<'data, 'colors, A: SSAValues> fmt::Display for LeafDisplay<'data, 'colors, A> where A::Data: Eq + fmt::Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.data {
            ValueOrImmediate::Immediate(i) => {
                // TODO: signed hex
                write!(f, "{:#x}", i)
            }
            ValueOrImmediate::Value(v) => {
                fmt::Display::fmt(&v.borrow().display(self.detailed, self.colors), f)
            }
        }
    }
}


impl<A: SSAValues> Eq for ValueOrImmediate<A> where A::Data: Eq + fmt::Display { }
impl<A: SSAValues> PartialEq for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (ValueOrImmediate::Immediate(l), ValueOrImmediate::Immediate(r)) => {
                l == r
            }
            (ValueOrImmediate::Value(l), ValueOrImmediate::Value(r)) => {
                Rc::ptr_eq(l, r)
            }
            _ => {
                false
            }
        }
    }
}

impl<A: SSAValues> Clone for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    fn clone(&self) -> Self {
        match self {
            ValueOrImmediate::Immediate(v) => {
                ValueOrImmediate::Immediate(*v)
            }
            ValueOrImmediate::Value(v) => {
                ValueOrImmediate::Value(Rc::clone(v))
            }
        }
    }
}

impl<A: SSAValues> Hash for ValueOrImmediate<A> where A::Data: Eq + fmt::Display {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            ValueOrImmediate::Immediate(v) => {
                state.write_u8(1);
                state.write_i64(*v);
            }
            ValueOrImmediate::Value(value) => {
                state.write_u8(2);
                (crate::analyses::static_single_assignment::HashedValue { value: Rc::clone(value) }).hash(state);
            }
        }
    }
}

use analyses::static_single_assignment::{DFGRebase, DefSource, HashedValue, SSA};
impl<A: SSAValues + Arch> DFGRebase<A> for Rc<Item<ValueOrImmediate<A>>> where A::Data: Hash + Eq + fmt::Display, A::Location: DFGRebase<A> {
    fn rebase_references(&self, old_dfg: &SSA<A>, new_dfg: &SSA<A>) -> Self {
        match &self.value {
            Expression::Unknown => Item::unknown(),
            Expression::Value(leaf) => {
                match leaf {
                    ValueOrImmediate::Immediate(v) => {
                        Item::untyped(Expression::Value(ValueOrImmediate::Immediate(*v)))
                    }
                    ValueOrImmediate::Value(v) => {
                        if !new_dfg.defs.contains_key(&HashedValue { value: Rc::clone(v) }) {
                            let (old_def_addr, old_def_source) = old_dfg.get_def_site(Rc::clone(v));
                            let new_use = match old_def_source {
                                DefSource::Instruction => {
                                    new_dfg.get_use(old_def_addr, v.borrow().location.rebase_references(old_dfg, new_dfg))
                                        .value
                                },
                                DefSource::External => {
                                    new_dfg.instruction_values.values()
                                        .find_map(|values| {
                                            values.iter().find_map(|((loc, dir), dfg_ref)| {
                                                if dir == &crate::data::Direction::Read && loc == &v.borrow().location && v.borrow().version.is_none() {
                                                    Some(Rc::clone(dfg_ref))
                                                } else {
                                                    None
                                                }
                                            })
                                        })
                                        .expect(&format!("corresponding external def exists for location {:?}", v.borrow().location))
                                }
                                DefSource::Between(addr) => {
                                    Rc::clone(new_dfg.control_dependent_values
                                        .get(&old_def_addr).expect("old def addr is valid")
                                        .get(&addr).expect("between's prior addr is valid")
                                        .get(&(v.borrow().location.rebase_references(old_dfg, new_dfg), crate::data::Direction::Write))
                                        .expect("corresponding def exists in new dfg"))
                                }
                                other => {
                                    panic!("aaaa {}", other);
                                }
                            };
                            Item::untyped(Expression::Value(ValueOrImmediate::Value(new_use)))
                        } else {
                            Item::untyped(Expression::Value(ValueOrImmediate::Value(Rc::clone(v))))
                        }
                    }
                }
            }
            Expression::Load { address, size } => {
                Item::load(&address.rebase_references(old_dfg, new_dfg), *size)
            },
            Expression::Add { left, right } => {
                Item::add(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Sub { left, right } => {
                Item::sub(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Mul { left, right } => {
                Item::mul(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Or { left, right } => {
                Item::or(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::And { left, right } => {
                Item::and(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Xor { left, right } => {
                Item::xor(
                    &left.rebase_references(old_dfg, new_dfg),
                    &right.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Shl { value, amount } => {
                Item::shl(
                    &value.rebase_references(old_dfg, new_dfg),
                    &amount.rebase_references(old_dfg, new_dfg),
                )
            },
            Expression::Shr { value, amount } => {
                Item::shr(
                    &value.rebase_references(old_dfg, new_dfg),
                    &amount.rebase_references(old_dfg, new_dfg),
                )
            },
        }
    }
}

impl<A: SSAValues> Value for Rc<Item<ValueOrImmediate<A>>> where A::Data: Hash + Eq + fmt::Display {
    // type Indirect = OpaqueIndirection<Self>;

    fn unknown() -> Self {
        Item::untyped(Expression::Unknown)
    }

    fn from_const(c: i64) -> Self {
        Item::untyped(Expression::Value(ValueOrImmediate::Immediate(c)))
    }

    fn from_set(_xs: &[Self]) -> Self {
        // TODO: tag the information loss here
        Self::unknown()
    }

    fn to_const(&self) -> Option<i64> {
        if let Expression::Value(ValueOrImmediate::Immediate(c)) = self.as_ref().value {
            Some(c)
        } else {
            None
        }
    }

    fn as_bool(&self) -> Option<bool> {
        self.to_const().map(|x| x != 0)
    }

    fn add(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::Add { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn sub(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::Sub { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn mul(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::Mul { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn or(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::Or { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn and(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::And { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn xor(&self, other: &Self) -> ValueRes<Self> {
        ValueRes {
            value: Item::untyped(Expression::Xor { left: Rc::clone(self), right: Rc::clone(other) }),
            carry: Self::unknown()
        }
    }

    fn sxt(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn zxt(&self, _width: &Self) -> Self {
        Self::unknown()
    }

    fn shr(&self, amount: &Self) -> Self {
        Item::untyped(Expression::Shr { value: Rc::clone(self), amount: Rc::clone(amount) })
    }

    fn shl(&self, amount: &Self) -> Self {
        Item::untyped(Expression::Shl { value: Rc::clone(self), amount: Rc::clone(amount) })
    }
}