1use crate::entity::{PrimaryMap, SecondaryMap};
7use crate::ir::{
8 self, pcc::Fact, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData,
9 DynamicStackSlots, DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst,
10 JumpTable, JumpTableData, Layout, MemoryType, MemoryTypeData, Opcode, SigRef, Signature,
11 SourceLocs, StackSlot, StackSlotData, StackSlots, Type,
12};
13use crate::isa::CallConv;
14use crate::write::write_function;
15use crate::HashMap;
16#[cfg(feature = "enable-serde")]
17use alloc::string::String;
18use core::fmt;
19
20#[cfg(feature = "enable-serde")]
21use serde::de::{Deserializer, Error};
22#[cfg(feature = "enable-serde")]
23use serde::ser::Serializer;
24#[cfg(feature = "enable-serde")]
25use serde::{Deserialize, Serialize};
26
27use super::entities::UserExternalNameRef;
28use super::extname::UserFuncName;
29use super::{RelSourceLoc, SourceLoc, UserExternalName};
30
31#[derive(Default, Copy, Clone, Debug, PartialEq, Hash)]
34pub struct VersionMarker;
35
36#[cfg(feature = "enable-serde")]
37impl Serialize for VersionMarker {
38 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
39 where
40 S: Serializer,
41 {
42 crate::VERSION.serialize(serializer)
43 }
44}
45
46#[cfg(feature = "enable-serde")]
47impl<'de> Deserialize<'de> for VersionMarker {
48 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
49 where
50 D: Deserializer<'de>,
51 {
52 let version = String::deserialize(deserializer)?;
53 if version != crate::VERSION {
54 return Err(D::Error::custom(&format!(
55 "Expected a clif ir function for version {}, found one for version {}",
56 crate::VERSION,
57 version,
58 )));
59 }
60 Ok(VersionMarker)
61 }
62}
63
64#[derive(Clone, PartialEq)]
67#[cfg_attr(
68 feature = "enable-serde",
69 derive(serde_derive::Serialize, serde_derive::Deserialize)
70)]
71pub struct FunctionParameters {
72 base_srcloc: Option<SourceLoc>,
75
76 user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,
78
79 user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,
81}
82
83impl FunctionParameters {
84 pub fn new() -> Self {
86 Self {
87 base_srcloc: None,
88 user_named_funcs: Default::default(),
89 user_ext_name_to_ref: Default::default(),
90 }
91 }
92
93 pub fn base_srcloc(&self) -> SourceLoc {
98 self.base_srcloc.unwrap_or_default()
99 }
100
101 pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {
103 match self.base_srcloc {
104 Some(val) => val,
105 None => {
106 self.base_srcloc = Some(srcloc);
107 srcloc
108 }
109 }
110 }
111
112 pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {
117 if let Some(reff) = self.user_ext_name_to_ref.get(&name) {
118 *reff
119 } else {
120 let reff = self.user_named_funcs.push(name.clone());
121 self.user_ext_name_to_ref.insert(name, reff);
122 reff
123 }
124 }
125
126 pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {
128 if let Some(prev_name) = self.user_named_funcs.get_mut(index) {
129 self.user_ext_name_to_ref.remove(prev_name);
130 *prev_name = name.clone();
131 self.user_ext_name_to_ref.insert(name, index);
132 }
133 }
134
135 pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {
137 &self.user_named_funcs
138 }
139
140 fn clear(&mut self) {
141 self.base_srcloc = None;
142 self.user_named_funcs.clear();
143 self.user_ext_name_to_ref.clear();
144 }
145}
146
147#[derive(Clone, PartialEq, Hash)]
152#[cfg_attr(
153 feature = "enable-serde",
154 derive(serde_derive::Serialize, serde_derive::Deserialize)
155)]
156pub struct FunctionStencil {
157 pub version_marker: VersionMarker,
162
163 pub signature: Signature,
165
166 pub sized_stack_slots: StackSlots,
168
169 pub dynamic_stack_slots: DynamicStackSlots,
171
172 pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
174
175 pub global_value_facts: SecondaryMap<ir::GlobalValue, Option<Fact>>,
177
178 pub memory_types: PrimaryMap<ir::MemoryType, ir::MemoryTypeData>,
180
181 pub dfg: DataFlowGraph,
183
184 pub layout: Layout,
186
187 pub srclocs: SourceLocs,
192
193 pub stack_limit: Option<ir::GlobalValue>,
199}
200
201impl FunctionStencil {
202 fn clear(&mut self) {
203 self.signature.clear(CallConv::Fast);
204 self.sized_stack_slots.clear();
205 self.dynamic_stack_slots.clear();
206 self.global_values.clear();
207 self.global_value_facts.clear();
208 self.memory_types.clear();
209 self.dfg.clear();
210 self.layout.clear();
211 self.srclocs.clear();
212 self.stack_limit = None;
213 }
214
215 pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
217 self.dfg.jump_tables.push(data)
218 }
219
220 pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
223 self.sized_stack_slots.push(data)
224 }
225
226 pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
229 self.dynamic_stack_slots.push(data)
230 }
231
232 pub fn import_signature(&mut self, signature: Signature) -> SigRef {
234 self.dfg.signatures.push(signature)
235 }
236
237 pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
239 self.global_values.push(data)
240 }
241
242 pub fn create_memory_type(&mut self, data: MemoryTypeData) -> MemoryType {
244 self.memory_types.push(data)
245 }
246
247 pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
249 self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
250 }
251
252 pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
254 let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
255 self.get_dyn_scale(dyn_ty)
256 }
257
258 pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
260 self.dfg
261 .dynamic_types
262 .get(ty)
263 .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))
264 .concrete()
265 }
266
267 pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
271 let entry = self.layout.entry_block().expect("Function is empty");
272 self.signature
273 .special_param_index(purpose)
274 .map(|i| self.dfg.block_params(entry)[i])
275 }
276
277 pub fn collect_debug_info(&mut self) {
279 self.dfg.collect_debug_info();
280 }
281
282 pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
285 for dest in self.dfg.insts[inst].branch_destination_mut(&mut self.dfg.jump_tables) {
286 if dest.block(&self.dfg.value_lists) == old_dest {
287 dest.set_block(new_dest, &mut self.dfg.value_lists)
288 }
289 }
290 }
291
292 pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
296 let dfg = &self.dfg;
297 let inst_iter = self.layout.block_insts(block);
298
299 let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
301
302 if let Some(_branch) = inst_iter.next() {
305 if let Some(next) = inst_iter.next() {
306 match dfg.insts[next].opcode() {
307 Opcode::Jump => (),
308 _ => return Err((next, "post-branch instruction not jump")),
309 }
310 }
311 }
312
313 Ok(())
314 }
315
316 pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {
318 self.layout.last_inst(block).into_iter().flat_map(|inst| {
319 self.dfg.insts[inst]
320 .branch_destination(&self.dfg.jump_tables)
321 .iter()
322 .map(|block| block.block(&self.dfg.value_lists))
323 })
324 }
325
326 pub fn is_leaf(&self) -> bool {
329 let has_signatures = !self.dfg.signatures.is_empty();
332
333 let has_tls = self.global_values.values().any(|gv| match gv {
337 GlobalValueData::Symbol { tls, .. } => *tls,
338 _ => false,
339 });
340
341 !has_signatures && !has_tls
342 }
343
344 pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
353 debug_assert_eq!(
354 self.dfg.inst_results(dst).len(),
355 self.dfg.inst_results(src).len()
356 );
357 debug_assert!(self
358 .dfg
359 .inst_results(dst)
360 .iter()
361 .zip(self.dfg.inst_results(src))
362 .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
363
364 self.dfg.insts[dst] = self.dfg.insts[src];
365 self.layout.remove_inst(src);
366 }
367
368 pub fn fixed_stack_size(&self) -> u32 {
372 self.sized_stack_slots.values().map(|ss| ss.size).sum()
373 }
374
375 pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
377 &self.srclocs
378 }
379}
380
381#[derive(Clone, PartialEq)]
384#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
385pub struct Function {
386 pub name: UserFuncName,
390
391 pub stencil: FunctionStencil,
394
395 pub params: FunctionParameters,
398}
399
400impl core::ops::Deref for Function {
401 type Target = FunctionStencil;
402
403 fn deref(&self) -> &Self::Target {
404 &self.stencil
405 }
406}
407
408impl core::ops::DerefMut for Function {
409 fn deref_mut(&mut self) -> &mut Self::Target {
410 &mut self.stencil
411 }
412}
413
414impl Function {
415 pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
417 Self {
418 name,
419 stencil: FunctionStencil {
420 version_marker: VersionMarker,
421 signature: sig,
422 sized_stack_slots: StackSlots::new(),
423 dynamic_stack_slots: DynamicStackSlots::new(),
424 global_values: PrimaryMap::new(),
425 global_value_facts: SecondaryMap::new(),
426 memory_types: PrimaryMap::new(),
427 dfg: DataFlowGraph::new(),
428 layout: Layout::new(),
429 srclocs: SecondaryMap::new(),
430 stack_limit: None,
431 },
432 params: FunctionParameters::new(),
433 }
434 }
435
436 pub fn clear(&mut self) {
438 self.stencil.clear();
439 self.params.clear();
440 self.name = UserFuncName::default();
441 }
442
443 pub fn new() -> Self {
445 Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
446 }
447
448 pub fn display(&self) -> DisplayFunction<'_> {
450 DisplayFunction(self)
451 }
452
453 pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
457 let base = self.params.ensure_base_srcloc(srcloc);
458 self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
459 }
460
461 pub fn srcloc(&self, inst: Inst) -> SourceLoc {
463 let base = self.params.base_srcloc();
464 self.stencil.srclocs[inst].expand(base)
465 }
466
467 pub fn declare_imported_user_function(
469 &mut self,
470 name: UserExternalName,
471 ) -> UserExternalNameRef {
472 self.params.ensure_user_func_name(name)
473 }
474
475 pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
477 self.stencil.dfg.ext_funcs.push(data)
478 }
479}
480
481pub struct DisplayFunction<'a>(&'a Function);
483
484impl<'a> fmt::Display for DisplayFunction<'a> {
485 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
486 write_function(fmt, self.0)
487 }
488}
489
490impl fmt::Display for Function {
491 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
492 write_function(fmt, self)
493 }
494}
495
496impl fmt::Debug for Function {
497 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
498 write_function(fmt, self)
499 }
500}