cranelift_interpreter/
environment.rs1use cranelift_codegen::ir::{FuncRef, Function};
3use cranelift_entity::{entity_impl, PrimaryMap};
4use std::collections::HashMap;
5
6#[derive(Default, Clone)]
8pub struct FunctionStore<'a> {
9 functions: PrimaryMap<FuncIndex, &'a Function>,
10 function_names: HashMap<String, FuncIndex>,
11}
12
13#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
15pub struct FuncIndex(u32);
16entity_impl!(FuncIndex, "fn");
17
18impl<'a> From<&'a Function> for FunctionStore<'a> {
20 fn from(function: &'a Function) -> Self {
21 let mut store = FunctionStore::default();
22 store.add(function.name.to_string(), function);
23 store
24 }
25}
26
27impl<'a> FunctionStore<'a> {
28 pub fn add(&mut self, name: String, function: &'a Function) {
30 assert!(!self.function_names.contains_key(&name));
31 let index = self.functions.push(function);
32 self.function_names.insert(name, index);
33 }
34
35 pub fn index_of(&self, name: &str) -> Option<FuncIndex> {
37 self.function_names.get(name).cloned()
38 }
39
40 pub fn get_by_index(&self, index: FuncIndex) -> Option<&'a Function> {
42 self.functions.get(index).cloned()
43 }
44
45 pub fn get_by_name(&self, name: &str) -> Option<&'a Function> {
47 let index = self.index_of(name)?;
48 self.get_by_index(index)
49 }
50
51 pub fn get_from_func_ref(
54 &self,
55 func_ref: FuncRef,
56 function: &Function,
57 ) -> Option<&'a Function> {
58 self.get_by_name(&get_function_name(func_ref, function))
59 }
60}
61
62fn get_function_name(func_ref: FuncRef, function: &Function) -> String {
65 function
66 .stencil
67 .dfg
68 .ext_funcs
69 .get(func_ref)
70 .expect("function to exist")
71 .name
72 .display(Some(&function.params))
73 .to_string()
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use cranelift_codegen::ir::{Signature, UserFuncName};
80 use cranelift_codegen::isa::CallConv;
81
82 #[test]
83 fn addition() {
84 let mut env = FunctionStore::default();
85 let a = "a";
86 let f = Function::new();
87
88 env.add(a.to_string(), &f);
89 assert!(env.get_by_name(a).is_some());
90 }
91
92 #[test]
93 fn nonexistence() {
94 let env = FunctionStore::default();
95 assert!(env.get_by_name("a").is_none());
96 }
97
98 #[test]
99 fn from() {
100 let name = UserFuncName::testcase("test");
101 let signature = Signature::new(CallConv::Fast);
102 let func = &Function::with_name_signature(name, signature);
103 let env: FunctionStore = func.into();
104 assert_eq!(env.index_of("%test"), Some(FuncIndex::from_u32(0)));
105 }
106}