1use crate::binemit::CodeOffset;
7use crate::entity::{PrimaryMap, SecondaryMap};
8use crate::ir;
9use crate::ir::{DataFlowGraph, ExternalName, Layout, Signature};
10use crate::ir::{
11 Ebb, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Heap, HeapData, Inst, JumpTable,
12 JumpTableData, SigRef, StackSlot, StackSlotData, Table, TableData,
13};
14use crate::ir::{EbbOffsets, FrameLayout, InstEncodings, SourceLocs, StackSlots, ValueLocations};
15use crate::ir::{JumpTableOffsets, JumpTables};
16use crate::isa::{CallConv, EncInfo, Encoding, Legalize, TargetIsa};
17use crate::regalloc::{EntryRegDiversions, RegDiversions};
18use crate::value_label::ValueLabelsRanges;
19use crate::write::write_function;
20use core::fmt;
21
22#[cfg(feature = "basic-blocks")]
23use crate::ir::Opcode;
24
25#[derive(Clone)]
30pub struct Function {
31 pub name: ExternalName,
33
34 pub signature: Signature,
36
37 pub old_signature: Option<Signature>,
40
41 pub stack_slots: StackSlots,
43
44 pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
46
47 pub heaps: PrimaryMap<ir::Heap, ir::HeapData>,
49
50 pub tables: PrimaryMap<ir::Table, ir::TableData>,
52
53 pub jump_tables: JumpTables,
55
56 pub dfg: DataFlowGraph,
58
59 pub layout: Layout,
61
62 pub encodings: InstEncodings,
65
66 pub locations: ValueLocations,
68
69 pub entry_diversions: EntryRegDiversions,
74
75 pub offsets: EbbOffsets,
81
82 pub jt_offsets: JumpTableOffsets,
84
85 pub srclocs: SourceLocs,
90
91 pub prologue_end: Option<Inst>,
95
96 pub frame_layout: Option<FrameLayout>,
102}
103
104impl Function {
105 pub fn with_name_signature(name: ExternalName, sig: Signature) -> Self {
107 Self {
108 name,
109 signature: sig,
110 old_signature: None,
111 stack_slots: StackSlots::new(),
112 global_values: PrimaryMap::new(),
113 heaps: PrimaryMap::new(),
114 tables: PrimaryMap::new(),
115 jump_tables: PrimaryMap::new(),
116 dfg: DataFlowGraph::new(),
117 layout: Layout::new(),
118 encodings: SecondaryMap::new(),
119 locations: SecondaryMap::new(),
120 entry_diversions: EntryRegDiversions::new(),
121 offsets: SecondaryMap::new(),
122 jt_offsets: SecondaryMap::new(),
123 srclocs: SecondaryMap::new(),
124 prologue_end: None,
125 frame_layout: None,
126 }
127 }
128
129 pub fn clear(&mut self) {
131 self.signature.clear(CallConv::Fast);
132 self.stack_slots.clear();
133 self.global_values.clear();
134 self.heaps.clear();
135 self.tables.clear();
136 self.jump_tables.clear();
137 self.dfg.clear();
138 self.layout.clear();
139 self.encodings.clear();
140 self.locations.clear();
141 self.entry_diversions.clear();
142 self.offsets.clear();
143 self.jt_offsets.clear();
144 self.srclocs.clear();
145 self.prologue_end = None;
146 self.frame_layout = None;
147 }
148
149 pub fn new() -> Self {
151 Self::with_name_signature(ExternalName::default(), Signature::new(CallConv::Fast))
152 }
153
154 pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
156 self.jump_tables.push(data)
157 }
158
159 pub fn create_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
162 self.stack_slots.push(data)
163 }
164
165 pub fn import_signature(&mut self, signature: Signature) -> SigRef {
167 self.dfg.signatures.push(signature)
168 }
169
170 pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
172 self.dfg.ext_funcs.push(data)
173 }
174
175 pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
177 self.global_values.push(data)
178 }
179
180 pub fn create_heap(&mut self, data: HeapData) -> Heap {
182 self.heaps.push(data)
183 }
184
185 pub fn create_table(&mut self, data: TableData) -> Table {
187 self.tables.push(data)
188 }
189
190 pub fn display<'a, I: Into<Option<&'a dyn TargetIsa>>>(
192 &'a self,
193 isa: I,
194 ) -> DisplayFunction<'a> {
195 DisplayFunction(self, isa.into().into())
196 }
197
198 pub fn display_with<'a>(
200 &'a self,
201 annotations: DisplayFunctionAnnotations<'a>,
202 ) -> DisplayFunction<'a> {
203 DisplayFunction(self, annotations)
204 }
205
206 pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
210 let entry = self.layout.entry_block().expect("Function is empty");
211 self.signature
212 .special_param_index(purpose)
213 .map(|i| self.dfg.ebb_params(entry)[i])
214 }
215
216 pub fn inst_offsets<'a>(&'a self, ebb: Ebb, encinfo: &EncInfo) -> InstOffsetIter<'a> {
226 assert!(
227 !self.offsets.is_empty(),
228 "Code layout must be computed first"
229 );
230 let mut divert = RegDiversions::new();
231 divert.at_ebb(&self.entry_diversions, ebb);
232 InstOffsetIter {
233 encinfo: encinfo.clone(),
234 func: self,
235 divert,
236 encodings: &self.encodings,
237 offset: self.offsets[ebb],
238 iter: self.layout.ebb_insts(ebb),
239 }
240 }
241
242 pub fn update_encoding(&mut self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<(), Legalize> {
244 self.encode(inst, isa).map(|e| self.encodings[inst] = e)
245 }
246
247 pub fn encode(&self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<Encoding, Legalize> {
250 isa.encode(&self, &self.dfg[inst], self.dfg.ctrl_typevar(inst))
251 }
252
253 pub fn collect_debug_info(&mut self) {
255 self.dfg.collect_debug_info();
256 self.frame_layout = Some(FrameLayout::new());
257 }
258
259 pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Ebb) {
262 match self.dfg[inst].branch_destination_mut() {
263 None => (),
264 Some(inst_dest) => *inst_dest = new_dest,
265 }
266 }
267
268 #[cfg(feature = "basic-blocks")]
272 pub fn is_ebb_basic(&self, ebb: Ebb) -> Result<(), (Inst, &'static str)> {
273 let dfg = &self.dfg;
274 let inst_iter = self.layout.ebb_insts(ebb);
275
276 let mut inst_iter = inst_iter.skip_while(|&inst| !dfg[inst].opcode().is_branch());
278
279 if let Some(_branch) = inst_iter.next() {
282 if let Some(next) = inst_iter.next() {
283 match dfg[next].opcode() {
284 Opcode::Fallthrough | Opcode::Jump => (),
285 _ => return Err((next, "post-branch instruction not fallthrough or jump")),
286 }
287 }
288 }
289
290 Ok(())
291 }
292
293 pub fn is_leaf(&self) -> bool {
296 !self.dfg.signatures.is_empty()
299 }
300}
301
302#[derive(Default)]
304pub struct DisplayFunctionAnnotations<'a> {
305 pub isa: Option<&'a dyn TargetIsa>,
307
308 pub value_ranges: Option<&'a ValueLabelsRanges>,
310}
311
312impl<'a> From<Option<&'a dyn TargetIsa>> for DisplayFunctionAnnotations<'a> {
313 fn from(isa: Option<&'a dyn TargetIsa>) -> DisplayFunctionAnnotations {
314 DisplayFunctionAnnotations {
315 isa,
316 value_ranges: None,
317 }
318 }
319}
320
321pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
323
324impl<'a> fmt::Display for DisplayFunction<'a> {
325 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
326 write_function(fmt, self.0, &self.1)
327 }
328}
329
330impl fmt::Display for Function {
331 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
332 write_function(fmt, self, &DisplayFunctionAnnotations::default())
333 }
334}
335
336impl fmt::Debug for Function {
337 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
338 write_function(fmt, self, &DisplayFunctionAnnotations::default())
339 }
340}
341
342pub struct InstOffsetIter<'a> {
344 encinfo: EncInfo,
345 divert: RegDiversions,
346 func: &'a Function,
347 encodings: &'a InstEncodings,
348 offset: CodeOffset,
349 iter: ir::layout::Insts<'a>,
350}
351
352impl<'a> Iterator for InstOffsetIter<'a> {
353 type Item = (CodeOffset, ir::Inst, CodeOffset);
354
355 fn next(&mut self) -> Option<Self::Item> {
356 self.iter.next().map(|inst| {
357 self.divert.apply(&self.func.dfg[inst]);
358 let byte_size =
359 self.encinfo
360 .byte_size(self.encodings[inst], inst, &self.divert, self.func);
361 let offset = self.offset;
362 self.offset += byte_size;
363 (offset, inst, byte_size)
364 })
365 }
366}