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
// Copyright (c) 2017-2019 Fabian Schuiki

//! Representation of LLHD functions, processes, and entitites.
//!
//! This module implements the intermediate representation around which the rest
//! of the framework is built.

use crate::{impl_table_key, ty::Type};

mod cfg;
mod dfg;
mod entity;
mod function;
mod inst;
mod layout;
mod module;
pub mod prelude;
mod process;
mod sig;
mod unit;

pub use self::cfg::*;
pub use self::dfg::*;
pub use self::entity::*;
pub use self::function::*;
pub use self::inst::*;
pub use self::layout::*;
pub use self::module::*;
pub use self::process::*;
pub use self::sig::*;
pub use self::unit::*;

/// The position where new instructions will be inserted into a `Function` or
/// `Process`.
#[derive(Clone, Copy)]
enum FunctionInsertPos {
    None,
    Append(Block),
    Prepend(Block),
    After(Inst),
    Before(Inst),
}

impl FunctionInsertPos {
    /// Insert an instruction and update the insertion postition.
    fn add_inst(&mut self, inst: Inst, layout: &mut FunctionLayout) {
        use FunctionInsertPos::*;
        match *self {
            None => panic!("no block selected to insert instruction"),
            Append(bb) => layout.append_inst(inst, bb),
            Prepend(bb) => {
                layout.prepend_inst(inst, bb);
                *self = After(inst);
            }
            After(other) => {
                layout.insert_inst_after(inst, other);
                *self = After(inst);
            }
            Before(other) => layout.insert_inst_before(inst, other),
        }
    }

    /// Update the insertion position in response to removing an instruction.
    fn remove_inst(&mut self, inst: Inst, layout: &FunctionLayout) {
        use FunctionInsertPos::*;
        match *self {
            // If we inserted after i, now insert before i's successor, or if i
            // was the last inst in the block, at the end of the block.
            After(i) if i == inst => {
                *self = layout
                    .next_inst(i)
                    .map(Before)
                    .unwrap_or(Append(layout.inst_block(i).unwrap()))
            }
            // If we inserted before i, now insert after i's predecessor, or if
            // i was the first inst in the block, at the beginning of the block.
            Before(i) if i == inst => {
                *self = layout
                    .prev_inst(i)
                    .map(After)
                    .unwrap_or(Prepend(layout.inst_block(i).unwrap()))
            }
            // Everything else we just keep as is.
            _ => (),
        }
    }
}

/// The position where new instructions will be inserted into an `Entity`.
#[derive(Clone, Copy)]
enum EntityInsertPos {
    Append,
    Prepend,
    After(Inst),
    Before(Inst),
}

impl EntityInsertPos {
    /// Insert an instruction and update the insertion postition.
    fn add_inst(&mut self, inst: Inst, layout: &mut InstLayout) {
        use EntityInsertPos::*;
        match *self {
            Append => layout.append_inst(inst),
            Prepend => {
                layout.prepend_inst(inst);
                *self = After(inst);
            }
            After(other) => {
                layout.insert_inst_after(inst, other);
                *self = After(inst);
            }
            Before(other) => layout.insert_inst_before(inst, other),
        }
    }

    /// Update the insertion position in response to removing an instruction.
    fn remove_inst(&mut self, inst: Inst, layout: &InstLayout) {
        use EntityInsertPos::*;
        match *self {
            // If we inserted after i, now insert before i's successor, or if i
            // was the last inst, at the end of the entity.
            After(i) if i == inst => *self = layout.next_inst(i).map(Before).unwrap_or(Append),
            // If we inserted before i, now insert after i's predecessor, or if
            // i was the first inst, at the beginning of the entity.
            Before(i) if i == inst => *self = layout.prev_inst(i).map(After).unwrap_or(Prepend),
            // Everything else we just keep as is.
            _ => (),
        }
    }
}

impl_table_key! {
    /// An instruction.
    struct Inst(u32) as "i";

    /// A value.
    struct Value(u32) as "v";

    /// A basic block.
    struct Block(u32) as "bb";

    /// An argument of a `Function`, `Process`, or `Entity`.
    struct Arg(u32) as "arg";

    /// An external `Function`, `Process` or `Entity`.
    struct ExtUnit(u32) as "ext";
}

impl Value {
    /// A placeholder for invalid values.
    ///
    /// This is used for unused instruction arguments.
    fn invalid() -> Self {
        Value(std::u32::MAX)
    }

    /// Check if this is a placeholder for invalid values.
    pub fn is_invalid(&self) -> bool {
        self.0 == std::u32::MAX
    }
}

impl Block {
    /// A placeholder for invalid blocks.
    ///
    /// This is used for unused instruction arguments.
    fn invalid() -> Self {
        Block(std::u32::MAX)
    }

    /// Check if this is a placeholder for invalid blocks.
    pub fn is_invalid(&self) -> bool {
        self.0 == std::u32::MAX
    }
}

/// Internal table storage for values.
#[derive(Debug, Serialize, Deserialize)]
pub enum ValueData {
    /// The value is the result of an instruction.
    Inst { ty: Type, inst: Inst },
    /// The value is an argument of the `Function`, `Process`, or `Entity`.
    Arg { ty: Type, arg: Arg },
    /// The value is a placeholder. Used during PHI node construction.
    Placeholder { ty: Type },
}

impl ValueData {
    /// Check if the value is a placeholder.
    pub fn is_placeholder(&self) -> bool {
        match self {
            ValueData::Placeholder { .. } => true,
            _ => false,
        }
    }
}

/// Internal table storage for blocks.
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockData {
    /// The name of the block.
    pub name: Option<String>,
}

/// Another unit referenced within a `Function`, `Process`, or `Entity`.
///
/// The linker will hook up external units to the actual counterparts as
/// appropriate.
#[derive(Debug, Serialize, Deserialize)]
pub struct ExtUnitData {
    /// The name of the referenced unit.
    pub name: UnitName,
    /// The signature of the referenced unit.
    pub sig: Signature,
}

/// Any one of the table keys in this module.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnyObject {
    Inst(Inst),
    Value(Value),
    Block(Block),
    Arg(Arg),
}

impl From<Inst> for AnyObject {
    fn from(x: Inst) -> Self {
        AnyObject::Inst(x)
    }
}

impl From<Value> for AnyObject {
    fn from(x: Value) -> Self {
        AnyObject::Value(x)
    }
}

impl From<Block> for AnyObject {
    fn from(x: Block) -> Self {
        AnyObject::Block(x)
    }
}

impl From<Arg> for AnyObject {
    fn from(x: Arg) -> Self {
        AnyObject::Arg(x)
    }
}

impl std::fmt::Display for AnyObject {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            AnyObject::Inst(x) => write!(f, "{}", x),
            AnyObject::Value(x) => write!(f, "{}", x),
            AnyObject::Block(x) => write!(f, "{}", x),
            AnyObject::Arg(x) => write!(f, "{}", x),
        }
    }
}

impl Value {
    /// Dump the value in human-readable form.
    pub fn dump(self, dfg: &DataFlowGraph) -> ValueDumper {
        ValueDumper(self, dfg)
    }
}

/// Temporary object to dump a `Value` in human-readable form for debugging.
pub struct ValueDumper<'a>(Value, &'a DataFlowGraph);

impl std::fmt::Display for ValueDumper<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.0.is_invalid() {
            write!(f, "%<invalid>")
        } else if let Some(name) = self.1.get_name(self.0) {
            write!(f, "%{}", name)
        } else if let Some(index) = self.1.get_anonymous_hint(self.0) {
            write!(f, "%{}", index)
        } else {
            write!(f, "%{}", self.0)
        }
    }
}

impl Block {
    /// Dump the basic block in human-readable form.
    pub fn dump(self, cfg: &ControlFlowGraph) -> BlockDumper {
        BlockDumper(self, cfg)
    }
}

/// Temporary object to dump a `Block` in human-readable form for debugging.
pub struct BlockDumper<'a>(Block, &'a ControlFlowGraph);

impl std::fmt::Display for BlockDumper<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.0.is_invalid() {
            write!(f, "%<invalid>")
        } else if let Some(name) = self.1.get_name(self.0) {
            write!(f, "%{}", name)
        } else if let Some(index) = self.1.get_anonymous_hint(self.0) {
            write!(f, "%{}", index)
        } else {
            write!(f, "%{}", self.0)
        }
    }
}