1use cranelift_entity::{EntityRef, PrimaryMap};
5use std::collections::HashMap;
6
7#[derive(Debug, PartialEq, Eq, Clone, Copy)]
9pub struct DefinedFuncIndex(usize);
10
11impl EntityRef for DefinedFuncIndex {
12 fn new(v: usize) -> Self {
13 Self(v)
14 }
15
16 fn index(self) -> usize {
17 self.0
18 }
19}
20
21#[derive(Debug, PartialEq, Eq, Clone, Copy)]
23pub struct SourceLoc(u32);
24
25impl SourceLoc {
26 pub fn new(v: u32) -> Self {
28 Self(v)
29 }
30
31 pub fn get(&self) -> u32 {
33 self.0
34 }
35
36 pub fn is_default(&self) -> bool {
38 self.0 == !0
39 }
40}
41
42#[derive(Debug, PartialEq, Eq, Clone)]
44pub struct CompiledFunctionData {
45 pub instructions: Vec<CompiledInstructionData>,
47 pub start_srcloc: SourceLoc,
49 pub end_srcloc: SourceLoc,
51 pub body_offset: usize,
53 pub body_len: usize,
55}
56
57#[derive(Debug, PartialEq, Eq, Clone)]
59pub struct CompiledInstructionData {
60 pub srcloc: SourceLoc,
62 pub code_len: usize,
64 pub code_offset: usize,
66}
67
68pub fn create_module_address_map<'a, I>(info: I) -> ModuleAddressMap
70where
71 I: Iterator<Item = &'a CompiledFunctionData>,
72{
73 let mut map = PrimaryMap::new();
74 for cfd in info {
75 map.push(cfd.clone());
76 }
77 map
78}
79
80pub type ModuleAddressMap = PrimaryMap<DefinedFuncIndex, CompiledFunctionData>;
82
83pub type RegUnit = u16;
85
86#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
88pub struct StackSlot(u32);
89
90impl StackSlot {
91 pub fn from_u32(x: u32) -> Self {
93 Self(x)
94 }
95
96 pub fn as_u32(self) -> u32 {
98 self.0
99 }
100}
101
102#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
104pub struct ValueLabel(u32);
105
106impl ValueLabel {
107 pub fn from_u32(x: u32) -> Self {
109 Self(x)
110 }
111
112 pub fn as_u32(self) -> u32 {
114 self.0
115 }
116}
117
118impl EntityRef for ValueLabel {
119 fn new(v: usize) -> Self {
120 Self(v as u32)
121 }
122
123 fn index(self) -> usize {
124 self.0 as usize
125 }
126}
127
128#[derive(Debug, Clone, Copy)]
130pub enum ValueLoc {
131 Unassigned,
132 Reg(RegUnit),
134 Stack(StackSlot),
136}
137
138#[derive(Debug, Clone)]
140pub struct ValueLocRange {
141 pub loc: ValueLoc,
143 pub start: u32,
145 pub end: u32,
147}
148
149pub fn build_values_ranges<'a, I>(vlri_iter: I) -> ValueLabelsRanges
151where
152 I: Iterator<Item = &'a ValueLabelsRangesInner>,
153{
154 let mut map = PrimaryMap::new();
155
156 for i in vlri_iter {
157 map.push(i.clone());
158 }
159
160 map
161}
162
163pub type ValueLabelsRanges = PrimaryMap<DefinedFuncIndex, ValueLabelsRangesInner>;
165pub type ValueLabelsRangesInner = HashMap<ValueLabel, Vec<ValueLocRange>>;
167
168pub fn get_vmctx_value_label() -> ValueLabel {
171 ValueLabel(0xffff_fffe)
173}
174
175pub struct ModuleVmctxInfo {
177 pub memory_offset: i64,
181 pub vmctx_size: i64,
183 pub stack_slot_offsets: PrimaryMap<DefinedFuncIndex, Vec<Option<i32>>>,
185}
186
187impl ModuleVmctxInfo {
188 pub fn new<'a, I>(memory_offset: i64, vmctx_size: i64, stack_slot_offsets: I) -> Self
189 where
190 I: Iterator<Item = &'a Vec<Option<i32>>>,
191 {
192 let mut map = PrimaryMap::new();
193 for o in stack_slot_offsets {
194 map.push(o.clone());
195 }
196 Self {
197 memory_offset,
198 vmctx_size,
199 stack_slot_offsets: map,
200 }
201 }
202}