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
use fmt::Formatter;
use std::collections::BTreeSet;
use std::fmt;
use id_arena::Arena;
use crate::core::{
function::{Function, FunctionId, ReturnType},
target_datalayout::TargetDataLayout,
target_triple::TargetTriple,
};
#[allow(dead_code)]
pub struct Module {
target_triple: Option<TargetTriple>,
target_datalayout: Option<TargetDataLayout>,
function_arena: Arena<Function>,
functions: BTreeSet<FunctionId>,
}
impl fmt::Display for Module {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(layout) = &self.target_datalayout {
writeln!(f, "target datalayout = \"{}\"", layout)?;
}
if let Some(target_triple) = &self.target_triple {
writeln!(f, "target triple = \"{}\"", target_triple)?;
}
for func_id in self.functions.iter() {
let func = self.function_arena.get(*func_id).unwrap();
writeln!(f, "{}", func)?;
}
Ok(())
}
}
impl Module {
pub fn set_target_triple(&mut self, triple: TargetTriple) {
self.target_triple = Some(triple);
}
pub fn set_target_datalayout(&mut self, layout: TargetDataLayout) {
self.target_datalayout = Some(layout);
}
pub fn get_function_ref_as_mut(&mut self, func_id: FunctionId) -> &mut Function {
self.function_arena.get_mut(func_id).unwrap()
}
pub fn get_function_ref(&self, func_id: FunctionId) -> &Function {
self.function_arena.get(func_id).unwrap()
}
pub fn new_function(&mut self, func_name: &str, ret_type: ReturnType) -> FunctionId {
let func_id = self
.function_arena
.alloc(Function::new(func_name, ret_type));
assert!(self.functions.insert(func_id));
func_id
}
}
impl Default for Module {
fn default() -> Self {
Self {
target_triple: None,
target_datalayout: None,
function_arena: Arena::new(),
functions: BTreeSet::new(),
}
}
}