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

//! Representation of LLHD functions.

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

/// A function.
#[derive(Serialize, Deserialize)]
pub struct Function {
    pub name: UnitName,
    pub sig: Signature,
    pub dfg: DataFlowGraph,
    pub cfg: ControlFlowGraph,
    pub layout: FunctionLayout,
}

impl Function {
    /// Create a new function.
    pub fn new(name: UnitName, sig: Signature) -> Self {
        assert!(!sig.has_outputs());
        assert!(sig.has_return_type());
        let mut func = Self {
            name,
            sig,
            dfg: DataFlowGraph::new(),
            cfg: ControlFlowGraph::new(),
            layout: FunctionLayout::new(),
        };
        func.dfg.make_args_for_signature(&func.sig);
        func
    }
}

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

    fn get_function(&self) -> Option<&Function> {
        Some(self)
    }

    fn get_function_mut(&mut self) -> Option<&mut Function> {
        Some(self)
    }

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

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

    fn try_cfg(&self) -> Option<&ControlFlowGraph> {
        Some(&self.cfg)
    }

    fn try_cfg_mut(&mut self) -> Option<&mut ControlFlowGraph> {
        Some(&mut self.cfg)
    }

    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 {
        &self.layout
    }

    fn func_layout_mut(&mut self) -> &mut FunctionLayout {
        &mut self.layout
    }

    fn inst_layout(&self) -> &InstLayout {
        panic!("inst_layout() called on function");
    }

    fn inst_layout_mut(&mut self) -> &mut InstLayout {
        panic!("inst_layout_mut() called on function");
    }

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

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

/// Temporary object used to build a single `Function`.
pub struct FunctionBuilder<'u> {
    /// The function currently being built.
    pub func: &'u mut Function,
    /// The position where we are currently inserting instructions.
    pos: FunctionInsertPos,
}

impl<'u> FunctionBuilder<'u> {
    /// Create a new function builder.
    pub fn new(func: &'u mut Function) -> Self {
        Self {
            func,
            pos: FunctionInsertPos::None,
        }
    }
}

impl UnitBuilder for FunctionBuilder<'_> {
    type Unit = Function;

    fn unit(&self) -> &Function {
        self.func
    }

    fn unit_mut(&mut self) -> &mut Function {
        self.func
    }

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

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

    fn block(&mut self) -> Block {
        let bb = self.func.cfg.add_block();
        self.func.layout.append_block(bb);
        bb
    }

    fn remove_block(&mut self, bb: Block) {
        let insts: Vec<_> = self.func.layout.insts(bb).collect();
        self.func.layout.remove_block(bb);
        self.func.cfg_mut().remove_block(bb);
        for inst in insts {
            if self.func.dfg().has_result(inst) {
                let value = self.func.dfg().inst_result(inst);
                self.func.dfg_mut().replace_use(value, Value::invalid());
            }
            self.func.dfg_mut().remove_inst(inst);
        }
    }

    fn insert_at_end(&mut self) {
        panic!("insert_at_end() called on function")
    }

    fn insert_at_beginning(&mut self) {
        panic!("insert_at_beginning() called on function")
    }

    fn append_to(&mut self, bb: Block) {
        self.pos = FunctionInsertPos::Append(bb);
    }

    fn prepend_to(&mut self, bb: Block) {
        self.pos = FunctionInsertPos::Prepend(bb);
    }

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

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