aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 7a69a0684bc14ad798b1cc8ba957453538cf4851 (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
extern crate num_traits;
extern crate termion;

use std::str::FromStr;

use std::fmt::{Debug, Display, Formatter};

use std::ops::{Add, Sub, AddAssign, SubAssign};

use std::rc::Rc;

use num_traits::identities;
use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub};

use termion::color;

                                                             // This is pretty wonk..
pub trait AddressDisplay {
    fn stringy(&self) -> String;
}

pub trait Address where Self:
    Debug + Display + AddressDisplay +
    Copy + Clone + Sized +
    Ord + Eq + PartialEq + Bounded +
    Add<Output=Self> + Sub<Output=Self> +
    AddAssign + SubAssign +
    WrappingAdd + WrappingSub +
    CheckedAdd + CheckedSub +
    FromStr +
    identities::One + identities::Zero {
    fn to_linear(&self) -> usize;

}
/*
impl <T> Address for T where T: Sized + Ord + Add<Output=Self> + From<u16> + Into<usize> {
    fn to_linear(&self) -> usize { *self.into() }
}
*/

impl AddressDisplay for usize {
    fn stringy(&self) -> String {
        format!("{:#x}", self)
    }
}

impl AddressDisplay for u64 {
    fn stringy(&self) -> String {
        format!("{:#x}", self)
    }
}

impl AddressDisplay for u32 {
    fn stringy(&self) -> String {
        format!("{:#x}", self)
    }
}

impl AddressDisplay for u16 {
    fn stringy(&self) -> String {
        format!("{:#x}", self)
    }
}

impl Address for u16 {
    fn to_linear(&self) -> usize { *self as usize }
}

impl Address for u32 {
    fn to_linear(&self) -> usize { *self as usize }
}

impl Address for u64 {
    fn to_linear(&self) -> usize { *self as usize }
}

impl Address for usize {
    fn to_linear(&self) -> usize { *self }
}

pub trait Decodable where Self: Sized {
    fn decode<T: IntoIterator<Item=u8>>(bytes: T) -> Option<Self>;
    fn decode_into<T: IntoIterator<Item=u8>>(&mut self, bytes: T) -> Option<()>;
}

pub trait Arch {
    type Address: Address + Debug;
    type Instruction: Decodable + LengthedInstruction<Unit=Self::Address> + Debug;
    type Operand;
}

pub trait LengthedInstruction {
    type Unit;
    fn len(&self) -> Self::Unit;
    fn min_size() -> Self::Unit;
}

pub struct ColorSettings {
    arithmetic: color::Fg<&'static color::Color>,
    stack: color::Fg<&'static color::Color>,
    nop: color::Fg<&'static color::Color>,
    stop: color::Fg<&'static color::Color>,
    control: color::Fg<&'static color::Color>,
    data: color::Fg<&'static color::Color>,
    comparison: color::Fg<&'static color::Color>,
    invalid: color::Fg<&'static color::Color>,
    platform: color::Fg<&'static color::Color>,
    misc: color::Fg<&'static color::Color>
}

impl Default for ColorSettings {
    fn default() -> ColorSettings {
        ColorSettings {
            arithmetic: color::Fg(&color::LightYellow),
            stack: color::Fg(&color::Magenta),
            nop: color::Fg(&color::Blue),
            stop: color::Fg(&color::LightRed),
            control: color::Fg(&color::Green),
            data: color::Fg(&color::LightMagenta),
            comparison: color::Fg(&color::Yellow),
            invalid: color::Fg(&color::Red),
            platform: color::Fg(&color::Cyan),
            misc: color::Fg(&color::LightCyan),
        }
    }
}

impl ColorSettings {
    pub fn arithmetic_op(&self) -> &color::Fg<&'static color::Color> { &self.arithmetic }
    pub fn stack_op(&self) -> &color::Fg<&'static color::Color> { &self.stack }
    pub fn nop_op(&self) -> &color::Fg<&'static color::Color> { &self.nop }
    pub fn stop_op(&self) -> &color::Fg<&'static color::Color> { &self.stop }
    pub fn control_flow_op(&self) -> &color::Fg<&'static color::Color> { &self.control }
    pub fn data_op(&self) -> &color::Fg<&'static color::Color> { &self.data }
    pub fn comparison_op(&self) -> &color::Fg<&'static color::Color> { &self.comparison }
    pub fn invalid_op(&self) -> &color::Fg<&'static color::Color> { &self.invalid }
}

/*
 * can this be a derivable trait or something?
 */
/*
impl <T: Colorize> Display for T {
    fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
        self.colorize(None, fmt)
    }
}
*/

pub trait Colorize<T: std::fmt::Write> {
    fn colorize(&self, colors: Option<&ColorSettings>, out: &mut T) -> std::fmt::Result;
}

/*
 * and make this auto-derive from a ShowContextual impl?
 */
/*
impl <T, U> Colorize for T where T: ShowContextual<Ctx=U> {
    fn colorize(&self, colors: Option<&ColorSettings>, fmt: &mut Formatter) -> std::fmt::Result {
        self.contextualize(colors, None, fmt)
    }
}
*/

pub trait ShowContextual<Addr, Ctx: ?Sized, T: std::fmt::Write> {
    fn contextualize(&self, colors: Option<&ColorSettings>, address: Addr, context: Option<&Ctx>, out: &mut T) -> std::fmt::Result;
}

/*
impl <C: ?Sized, T: std::fmt::Write, U: Colorize<T>> ShowContextual<C, T> for U {
    fn contextualize(&self, colors: Option<&ColorSettings>, context: Option<&C>, out: &mut T) -> std::fmt::Result {
        self.colorize(colors, out)
    }
}
*/