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, MemoryType, MemoryTypeData, SigRef, Signature, SourceLocs, StackSlot,
13 StackSlotData, StackSlots, Type, pcc::Fact,
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 global_value_facts: SecondaryMap<ir::GlobalValue, Option<Fact>>,
178
179 pub memory_types: PrimaryMap<ir::MemoryType, ir::MemoryTypeData>,
181
182 pub dfg: DataFlowGraph,
184
185 pub layout: Layout,
187
188 pub srclocs: SourceLocs,
193
194 pub debug_tags: DebugTags,
209
210 pub stack_limit: Option<ir::GlobalValue>,
216}
217
218impl FunctionStencil {
219 fn clear(&mut self) {
220 self.signature.clear(CallConv::Fast);
221 self.sized_stack_slots.clear();
222 self.dynamic_stack_slots.clear();
223 self.global_values.clear();
224 self.global_value_facts.clear();
225 self.memory_types.clear();
226 self.dfg.clear();
227 self.layout.clear();
228 self.srclocs.clear();
229 self.debug_tags.clear();
230 self.stack_limit = None;
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 create_memory_type(&mut self, data: MemoryTypeData) -> MemoryType {
262 self.memory_types.push(data)
263 }
264
265 pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
267 self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
268 }
269
270 pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
272 let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
273 self.get_dyn_scale(dyn_ty)
274 }
275
276 pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
278 self.dfg
279 .dynamic_types
280 .get(ty)
281 .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))
282 .concrete()
283 }
284
285 pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
289 let entry = self.layout.entry_block().expect("Function is empty");
290 self.signature
291 .special_param_index(purpose)
292 .map(|i| self.dfg.block_params(entry)[i])
293 }
294
295 pub fn collect_debug_info(&mut self) {
297 self.dfg.collect_debug_info();
298 }
299
300 pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
303 for dest in self.dfg.insts[inst]
304 .branch_destination_mut(&mut self.dfg.jump_tables, &mut self.dfg.exception_tables)
305 {
306 if dest.block(&self.dfg.value_lists) == old_dest {
307 dest.set_block(new_dest, &mut self.dfg.value_lists)
308 }
309 }
310 }
311
312 pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
316 let dfg = &self.dfg;
317 let inst_iter = self.layout.block_insts(block);
318
319 let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
321
322 if let Some(_branch) = inst_iter.next() {
323 if let Some(next) = inst_iter.next() {
324 return Err((next, "post-terminator instruction"));
325 }
326 }
327
328 Ok(())
329 }
330
331 pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {
333 self.layout.last_inst(block).into_iter().flat_map(|inst| {
334 self.dfg.insts[inst]
335 .branch_destination(&self.dfg.jump_tables, &self.dfg.exception_tables)
336 .iter()
337 .map(|block| block.block(&self.dfg.value_lists))
338 })
339 }
340
341 pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
350 debug_assert_eq!(
351 self.dfg.inst_results(dst).len(),
352 self.dfg.inst_results(src).len()
353 );
354 debug_assert!(
355 self.dfg
356 .inst_results(dst)
357 .iter()
358 .zip(self.dfg.inst_results(src))
359 .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))
360 );
361
362 self.dfg.insts[dst] = self.dfg.insts[src];
363 self.layout.remove_inst(src);
364 }
365
366 pub fn fixed_stack_size(&self) -> u32 {
370 self.sized_stack_slots.values().map(|ss| ss.size).sum()
371 }
372
373 pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
375 &self.srclocs
376 }
377}
378
379#[derive(Clone, PartialEq)]
382#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
383pub struct Function {
384 pub name: UserFuncName,
388
389 pub stencil: FunctionStencil,
392
393 pub params: FunctionParameters,
396}
397
398impl core::ops::Deref for Function {
399 type Target = FunctionStencil;
400
401 fn deref(&self) -> &Self::Target {
402 &self.stencil
403 }
404}
405
406impl core::ops::DerefMut for Function {
407 fn deref_mut(&mut self) -> &mut Self::Target {
408 &mut self.stencil
409 }
410}
411
412impl Function {
413 pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
415 Self {
416 name,
417 stencil: FunctionStencil {
418 version_marker: VersionMarker,
419 signature: sig,
420 sized_stack_slots: StackSlots::new(),
421 dynamic_stack_slots: DynamicStackSlots::new(),
422 global_values: PrimaryMap::new(),
423 global_value_facts: SecondaryMap::new(),
424 memory_types: PrimaryMap::new(),
425 dfg: DataFlowGraph::new(),
426 layout: Layout::new(),
427 srclocs: SecondaryMap::new(),
428 stack_limit: None,
429 debug_tags: DebugTags::default(),
430 },
431 params: FunctionParameters::new(),
432 }
433 }
434
435 pub fn clear(&mut self) {
437 self.stencil.clear();
438 self.params.clear();
439 self.name = UserFuncName::default();
440 }
441
442 pub fn new() -> Self {
444 Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
445 }
446
447 pub fn display(&self) -> DisplayFunction<'_> {
449 DisplayFunction(self)
450 }
451
452 pub fn display_spec(&self) -> DisplayFunctionSpec<'_> {
454 DisplayFunctionSpec(self)
455 }
456
457 pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
461 let base = self.params.ensure_base_srcloc(srcloc);
462 self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
463 }
464
465 pub fn srcloc(&self, inst: Inst) -> SourceLoc {
467 let base = self.params.base_srcloc();
468 self.stencil.srclocs[inst].expand(base)
469 }
470
471 pub fn declare_imported_user_function(
473 &mut self,
474 name: UserExternalName,
475 ) -> UserExternalNameRef {
476 self.params.ensure_user_func_name(name)
477 }
478
479 pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
481 self.stencil.dfg.ext_funcs.push(data)
482 }
483}
484
485pub struct DisplayFunction<'a>(&'a Function);
487
488impl<'a> fmt::Display for DisplayFunction<'a> {
489 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
490 write_function(fmt, self.0)
491 }
492}
493
494impl fmt::Display for Function {
495 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
496 write_function(fmt, self)
497 }
498}
499
500impl fmt::Debug for Function {
501 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
502 write_function(fmt, self)
503 }
504}
505
506pub struct DisplayFunctionSpec<'a>(&'a Function);
508
509impl<'a> fmt::Display for DisplayFunctionSpec<'a> {
510 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
511 write_function_spec(fmt, self.0)
512 }
513}
514
515impl<'a> fmt::Debug for DisplayFunctionSpec<'a> {
516 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
517 write_function_spec(fmt, self.0)
518 }
519}