vicis_core/ir/module/
mod.rs

1pub mod attributes;
2pub mod global_variable;
3pub mod linkage;
4pub mod metadata;
5pub mod name;
6pub mod parser;
7pub mod preemption_specifier;
8pub mod unnamed_addr;
9pub mod visibility;
10
11pub use parser::parse as parse_assembly;
12
13use super::{
14    function::{Function, FunctionId, Parameter},
15    types::{Type, Types},
16};
17use attributes::Attribute;
18use global_variable::GlobalVariable;
19use id_arena::{Arena, Id};
20use metadata::Metadata;
21use name::Name;
22use rustc_hash::FxHashMap;
23use std::fmt;
24
25#[derive(Debug, Clone)]
26pub struct Target {
27    triple: String,
28    datalayout: String,
29}
30
31pub struct Module {
32    pub(crate) name: String,
33    pub(crate) source_filename: String,
34    pub(crate) target: Target,
35    pub(crate) functions: Arena<Function>,
36    pub(crate) attributes: FxHashMap<u32, Vec<Attribute>>,
37    pub(crate) global_variables: FxHashMap<Name, GlobalVariable>,
38    pub types: Types,
39    pub metas: FxHashMap<Name, Metadata>,
40}
41
42impl Default for Module {
43    fn default() -> Self {
44        Self {
45            name: "".to_string(),
46            source_filename: "".to_string(),
47            target: Target::new(),
48            functions: Arena::new(),
49            attributes: FxHashMap::default(),
50            global_variables: FxHashMap::default(),
51            types: Types::new(),
52            metas: FxHashMap::default(),
53        }
54    }
55}
56
57impl Module {
58    pub fn new() -> Self {
59        Self::default()
60    }
61
62    pub fn name(&self) -> &String {
63        &self.name
64    }
65
66    pub fn source_filename(&self) -> &String {
67        &self.source_filename
68    }
69
70    pub fn target(&self) -> &Target {
71        &self.target
72    }
73
74    pub fn functions(&self) -> &Arena<Function> {
75        &self.functions
76    }
77
78    pub fn functions_mut(&mut self) -> &mut Arena<Function> {
79        &mut self.functions
80    }
81
82    pub fn attributes(&self) -> &FxHashMap<u32, Vec<Attribute>> {
83        &self.attributes
84    }
85
86    pub fn global_variables(&self) -> &FxHashMap<Name, GlobalVariable> {
87        &self.global_variables
88    }
89
90    pub fn add_function(&mut self, f: Function) -> Id<Function> {
91        self.functions.alloc(f)
92    }
93
94    pub fn create_function<T: AsRef<str>>(
95        &mut self,
96        name: T,
97        result_ty: Type,
98        params: Vec<Parameter>,
99        is_var_arg: bool,
100    ) -> Id<Function> {
101        self.functions.alloc(Function::new(
102            name,
103            result_ty,
104            params,
105            is_var_arg,
106            self.types.clone(),
107        ))
108    }
109
110    pub fn find_function_by_name<T: AsRef<str>>(&self, name: T) -> Option<FunctionId> {
111        for (id, func) in &self.functions {
112            if func.name() == name.as_ref() {
113                return Some(id);
114            }
115        }
116        None
117    }
118}
119
120impl Default for Target {
121    fn default() -> Self {
122        Self {
123            triple: "".to_string(),
124            datalayout: "".to_string(),
125        }
126    }
127}
128
129impl Target {
130    pub fn new() -> Self {
131        Self::default()
132    }
133
134    pub fn triple(&self) -> &str {
135        self.triple.as_str()
136    }
137
138    pub fn datalayout(&self) -> &str {
139        self.datalayout.as_str()
140    }
141}
142
143impl fmt::Debug for Module {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        writeln!(f, "source_filename = \"{}\"", self.source_filename)?;
146        writeln!(f, "target datalayout = \"{}\"", self.target.datalayout)?;
147        writeln!(f, "target triple = \"{}\"", self.target.triple)?;
148        writeln!(f)?;
149        write!(f, "{:?}", self.types)?;
150        for gv in self.global_variables.values() {
151            writeln!(f, "{}", gv.to_string(&self.types))?;
152        }
153        writeln!(f)?;
154        for (_, func) in &self.functions {
155            writeln!(f, "{:?}", func)?;
156        }
157        for (id, attrs) in &self.attributes {
158            write!(f, "attributes #{} = {{ ", id)?;
159            for attr in attrs {
160                write!(f, "{:?} ", attr)?;
161            }
162            writeln!(f, "}}")?
163        }
164        for (n, meta) in &self.metas {
165            writeln!(f, "!{} = {:?}", n, meta)?;
166        }
167        Ok(())
168    }
169}