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

//! Representation of LLHD processes.

use crate::{
    ir::{
        Block, ControlFlowGraph, DataFlowGraph, EntityInsertPos, FunctionLayout, Inst, InstData,
        InstLayout, Signature, Unit, UnitBuilder, UnitKind, UnitName,
    },
    ty::Type,
    verifier::Verifier,
};

/// An entity.
#[derive(Serialize, Deserialize)]
pub struct Entity {
    pub name: UnitName,
    pub sig: Signature,
    pub dfg: DataFlowGraph,
    pub layout: InstLayout,
}

impl Entity {
    /// Create a new entity.
    pub fn new(name: UnitName, sig: Signature) -> Self {
        assert!(!sig.has_return_type());
        let mut ent = Self {
            name,
            sig,
            dfg: DataFlowGraph::new(),
            layout: InstLayout::new(),
        };
        ent.dfg.make_args_for_signature(&ent.sig);
        ent
    }
}

impl Unit for Entity {
    fn kind(&self) -> UnitKind {
        UnitKind::Entity
    }

    fn get_entity(&self) -> Option<&Entity> {
        Some(self)
    }

    fn get_entity_mut(&mut self) -> Option<&mut Entity> {
        Some(self)
    }

    fn dfg(&self) -> &DataFlowGraph {
        &self.dfg
    }

    fn dfg_mut(&mut self) -> &mut DataFlowGraph {
        &mut self.dfg
    }

    fn try_cfg(&self) -> Option<&ControlFlowGraph> {
        None
    }

    fn try_cfg_mut(&mut self) -> Option<&mut ControlFlowGraph> {
        None
    }

    fn sig(&self) -> &Signature {
        &self.sig
    }

    fn sig_mut(&mut self) -> &mut Signature {
        &mut self.sig
    }

    fn name(&self) -> &UnitName {
        &self.name
    }

    fn name_mut(&mut self) -> &mut UnitName {
        &mut self.name
    }

    fn func_layout(&self) -> &FunctionLayout {
        panic!("func_layout() called on entity");
    }

    fn func_layout_mut(&mut self) -> &mut FunctionLayout {
        panic!("func_layout_mut() called on entity");
    }

    fn inst_layout(&self) -> &InstLayout {
        &self.layout
    }

    fn inst_layout_mut(&mut self) -> &mut InstLayout {
        &mut self.layout
    }

    fn dump_fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "entity {} {} {{\n", self.name, self.sig.dump(&self.dfg))?;
        for inst in self.layout.insts() {
            write!(f, "    {}\n", inst.dump(&self.dfg, None))?;
        }
        write!(f, "}}")?;
        Ok(())
    }

    fn verify(&self) {
        let mut verifier = Verifier::new();
        verifier.verify_entity(self);
        match verifier.finish() {
            Ok(()) => (),
            Err(errs) => {
                eprintln!("");
                eprintln!("Verified entity:");
                eprintln!("{}", self.dump());
                eprintln!("");
                eprintln!("Verification errors:");
                eprintln!("{}", errs);
                panic!("verification failed");
            }
        }
    }
}

/// Temporary object used to build a single `Entity`.
pub struct EntityBuilder<'u> {
    /// The entity currently being built.
    pub entity: &'u mut Entity,
    /// The position where we are currently inserting instructions.
    pos: EntityInsertPos,
}

impl<'u> EntityBuilder<'u> {
    /// Create a new entity builder.
    pub fn new(entity: &mut Entity) -> EntityBuilder {
        EntityBuilder {
            entity,
            pos: EntityInsertPos::Append,
        }
    }
}

impl UnitBuilder for EntityBuilder<'_> {
    type Unit = Entity;

    fn unit(&self) -> &Entity {
        self.entity
    }

    fn unit_mut(&mut self) -> &mut Entity {
        self.entity
    }

    fn build_inst(&mut self, data: InstData, ty: Type) -> Inst {
        let inst = self.entity.dfg.add_inst(data, ty);
        self.pos.add_inst(inst, &mut self.entity.layout);
        inst
    }

    fn remove_inst(&mut self, inst: Inst) {
        self.entity.dfg.remove_inst(inst);
        self.pos.remove_inst(inst, &self.entity.layout);
        self.entity.layout.remove_inst(inst);
    }

    fn block(&mut self) -> Block {
        panic!("block() called on entity");
    }

    fn remove_block(&mut self, _: Block) {
        panic!("remove_block() called on entity");
    }

    fn insert_at_end(&mut self) {
        self.pos = EntityInsertPos::Append;
    }

    fn insert_at_beginning(&mut self) {
        self.pos = EntityInsertPos::Prepend;
    }

    fn append_to(&mut self, _: Block) {
        panic!("append_to() called on entity");
    }

    fn prepend_to(&mut self, _: Block) {
        panic!("prepend_to() called on entity");
    }

    fn insert_after(&mut self, inst: Inst) {
        self.pos = EntityInsertPos::After(inst);
    }

    fn insert_before(&mut self, inst: Inst) {
        self.pos = EntityInsertPos::Before(inst);
    }
}