1use crate::HashMap;
7use crate::entity::{PrimaryMap, SecondaryMap};
8use crate::ir::DebugTags;
9use crate::ir::{
10 self, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, DynamicStackSlots,
11 DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, JumpTable,
12 JumpTableData, Layout, ReplaceBuilder, SigRef, Signature, SourceLocs, StackSlot, StackSlotData,
13 StackSlots, Type,
14};
15use crate::isa::CallConv;
16use crate::write::{write_function, write_function_spec};
17#[cfg(feature = "enable-serde")]
18use alloc::string::String;
19use core::fmt;
20
21#[cfg(feature = "enable-serde")]
22use serde::de::{Deserializer, Error};
23#[cfg(feature = "enable-serde")]
24use serde::ser::Serializer;
25#[cfg(feature = "enable-serde")]
26use serde::{Deserialize, Serialize};
27
28use super::entities::UserExternalNameRef;
29use super::extname::UserFuncName;
30use super::{RelSourceLoc, SourceLoc, UserExternalName};
31
32#[derive(Default, Copy, Clone, Debug, PartialEq, Hash)]
35pub struct VersionMarker;
36
37#[cfg(feature = "enable-serde")]
38impl Serialize for VersionMarker {
39 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40 where
41 S: Serializer,
42 {
43 crate::VERSION.serialize(serializer)
44 }
45}
46
47#[cfg(feature = "enable-serde")]
48impl<'de> Deserialize<'de> for VersionMarker {
49 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
50 where
51 D: Deserializer<'de>,
52 {
53 let version = String::deserialize(deserializer)?;
54 if version != crate::VERSION {
55 return Err(D::Error::custom(&format!(
56 "Expected a clif ir function for version {}, found one for version {}",
57 crate::VERSION,
58 version,
59 )));
60 }
61 Ok(VersionMarker)
62 }
63}
64
65#[derive(Clone, PartialEq)]
68#[cfg_attr(
69 feature = "enable-serde",
70 derive(serde_derive::Serialize, serde_derive::Deserialize)
71)]
72pub struct FunctionParameters {
73 base_srcloc: Option<SourceLoc>,
76
77 user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,
79
80 user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,
82}
83
84impl FunctionParameters {
85 pub fn new() -> Self {
87 Self {
88 base_srcloc: None,
89 user_named_funcs: Default::default(),
90 user_ext_name_to_ref: Default::default(),
91 }
92 }
93
94 pub fn base_srcloc(&self) -> SourceLoc {
99 self.base_srcloc.unwrap_or_default()
100 }
101
102 pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {
104 match self.base_srcloc {
105 Some(val) => val,
106 None => {
107 self.base_srcloc = Some(srcloc);
108 srcloc
109 }
110 }
111 }
112
113 pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {
118 if let Some(reff) = self.user_ext_name_to_ref.get(&name) {
119 *reff
120 } else {
121 let reff = self.user_named_funcs.push(name.clone());
122 self.user_ext_name_to_ref.insert(name, reff);
123 reff
124 }
125 }
126
127 pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {
129 if let Some(prev_name) = self.user_named_funcs.get_mut(index) {
130 self.user_ext_name_to_ref.remove(prev_name);
131 *prev_name = name.clone();
132 self.user_ext_name_to_ref.insert(name, index);
133 }
134 }
135
136 pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {
138 &self.user_named_funcs
139 }
140
141 fn clear(&mut self) {
142 self.base_srcloc = None;
143 self.user_named_funcs.clear();
144 self.user_ext_name_to_ref.clear();
145 }
146}
147
148#[derive(Clone, PartialEq, Hash)]
153#[cfg_attr(
154 feature = "enable-serde",
155 derive(serde_derive::Serialize, serde_derive::Deserialize)
156)]
157pub struct FunctionStencil {
158 pub version_marker: VersionMarker,
163
164 pub signature: Signature,
166
167 pub sized_stack_slots: StackSlots,
169
170 pub dynamic_stack_slots: DynamicStackSlots,
172
173 pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
175
176 pub dfg: DataFlowGraph,
178
179 pub layout: Layout,
181
182 pub srclocs: SourceLocs,
187
188 pub debug_tags: DebugTags,
203
204 pub stack_limit: Option<ir::GlobalValue>,
210}
211
212impl FunctionStencil {
213 fn clear(&mut self) {
214 self.signature.clear(CallConv::Fast);
215 self.sized_stack_slots.clear();
216 self.dynamic_stack_slots.clear();
217 self.global_values.clear();
218 self.dfg.clear();
219 self.layout.clear();
220 self.srclocs.clear();
221 self.debug_tags.clear();
222 self.stack_limit = None;
223 }
224
225 pub fn replace(&mut self, inst: Inst) -> ReplaceBuilder<'_> {
230 ReplaceBuilder::new(&mut self.dfg, &mut self.layout, inst)
231 }
232
233 pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
235 self.dfg.jump_tables.push(data)
236 }
237
238 pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
241 self.sized_stack_slots.push(data)
242 }
243
244 pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
247 self.dynamic_stack_slots.push(data)
248 }
249
250 pub fn import_signature(&mut self, signature: Signature) -> SigRef {
252 self.dfg.signatures.push(signature)
253 }
254
255 pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
257 self.global_values.push(data)
258 }
259
260 pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
262 self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
263 }
264
265 pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
267 let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
268 self.get_dyn_scale(dyn_ty)
269 }
270
271 pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
273 self.dfg
274 .dynamic_types
275 .get(ty)
276 .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))
277 .concrete()
278 }
279
280 pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
284 let entry = self.layout.entry_block().expect("Function is empty");
285 self.signature
286 .special_param_index(purpose)
287 .map(|i| self.dfg.block_params(entry)[i])
288 }
289
290 pub fn collect_debug_info(&mut self) {
292 self.dfg.collect_debug_info();
293 }
294
295 pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
298 for dest in self.dfg.insts[inst]
299 .branch_destination_mut(&mut self.dfg.jump_tables, &mut self.dfg.exception_tables)
300 {
301 if dest.block(&self.dfg.value_lists) == old_dest {
302 dest.set_block(new_dest, &mut self.dfg.value_lists)
303 }
304 }
305 }
306
307 pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
311 let dfg = &self.dfg;
312 let inst_iter = self.layout.block_insts(block);
313
314 let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
316
317 if let Some(_branch) = inst_iter.next() {
318 if let Some(next) = inst_iter.next() {
319 return Err((next, "post-terminator instruction"));
320 }
321 }
322
323 Ok(())
324 }
325
326 pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {
328 self.layout.last_inst(block).into_iter().flat_map(|inst| {
329 self.dfg.insts[inst]
330 .branch_destination(&self.dfg.jump_tables, &self.dfg.exception_tables)
331 .iter()
332 .map(|block| block.block(&self.dfg.value_lists))
333 })
334 }
335
336 pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
345 debug_assert_eq!(
346 self.dfg.inst_results(dst).len(),
347 self.dfg.inst_results(src).len()
348 );
349 debug_assert!(
350 self.dfg
351 .inst_results(dst)
352 .iter()
353 .zip(self.dfg.inst_results(src))
354 .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))
355 );
356
357 self.dfg.insts[dst] = self.dfg.insts[src];
358 self.layout.remove_inst(src);
359 }
360
361 pub fn fixed_stack_size(&self) -> u32 {
365 self.sized_stack_slots.values().map(|ss| ss.size).sum()
366 }
367
368 pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
370 &self.srclocs
371 }
372}
373
374#[derive(Clone, PartialEq)]
377#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
378pub struct Function {
379 pub name: UserFuncName,
383
384 pub stencil: FunctionStencil,
387
388 pub params: FunctionParameters,
391}
392
393impl core::ops::Deref for Function {
394 type Target = FunctionStencil;
395
396 fn deref(&self) -> &Self::Target {
397 &self.stencil
398 }
399}
400
401impl core::ops::DerefMut for Function {
402 fn deref_mut(&mut self) -> &mut Self::Target {
403 &mut self.stencil
404 }
405}
406
407impl Function {
408 pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
410 Self {
411 name,
412 stencil: FunctionStencil {
413 version_marker: VersionMarker,
414 signature: sig,
415 sized_stack_slots: StackSlots::new(),
416 dynamic_stack_slots: DynamicStackSlots::new(),
417 global_values: PrimaryMap::new(),
418 dfg: DataFlowGraph::new(),
419 layout: Layout::new(),
420 srclocs: SecondaryMap::new(),
421 stack_limit: None,
422 debug_tags: DebugTags::default(),
423 },
424 params: FunctionParameters::new(),
425 }
426 }
427
428 pub fn clear(&mut self) {
430 self.stencil.clear();
431 self.params.clear();
432 self.name = UserFuncName::default();
433 }
434
435 pub fn new() -> Self {
437 Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
438 }
439
440 pub fn display(&self) -> DisplayFunction<'_> {
442 DisplayFunction(self)
443 }
444
445 pub fn display_spec(&self) -> DisplayFunctionSpec<'_> {
447 DisplayFunctionSpec(self)
448 }
449
450 pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
454 let base = self.params.ensure_base_srcloc(srcloc);
455 self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
456 }
457
458 pub fn srcloc(&self, inst: Inst) -> SourceLoc {
460 let base = self.params.base_srcloc();
461 self.stencil.srclocs[inst].expand(base)
462 }
463
464 pub fn declare_imported_user_function(
466 &mut self,
467 name: UserExternalName,
468 ) -> UserExternalNameRef {
469 self.params.ensure_user_func_name(name)
470 }
471
472 pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
474 self.stencil.dfg.ext_funcs.push(data)
475 }
476}
477
478pub struct DisplayFunction<'a>(&'a Function);
480
481impl<'a> fmt::Display for DisplayFunction<'a> {
482 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
483 write_function(fmt, self.0)
484 }
485}
486
487impl fmt::Display for Function {
488 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
489 write_function(fmt, self)
490 }
491}
492
493impl fmt::Debug for Function {
494 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
495 write_function(fmt, self)
496 }
497}
498
499pub struct DisplayFunctionSpec<'a>(&'a Function);
501
502impl<'a> fmt::Display for DisplayFunctionSpec<'a> {
503 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
504 write_function_spec(fmt, self.0)
505 }
506}
507
508impl<'a> fmt::Debug for DisplayFunctionSpec<'a> {
509 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
510 write_function_spec(fmt, self.0)
511 }
512}