cranelift_codegen/isa/x64/inst/unwind/
systemv.rs1use crate::isa::unwind::systemv::RegisterMappingError;
4use crate::machinst::{Reg, RegClass};
5use gimli::{write::CommonInformationEntry, Encoding, Format, Register, X86_64};
6
7pub fn create_cie() -> CommonInformationEntry {
9 use gimli::write::CallFrameInstruction;
10
11 let mut entry = CommonInformationEntry::new(
12 Encoding {
13 address_size: 8,
14 format: Format::Dwarf32,
15 version: 1,
16 },
17 1, -8, X86_64::RA,
20 );
21
22 entry.add_instruction(CallFrameInstruction::Cfa(X86_64::RSP, 8));
25
26 entry.add_instruction(CallFrameInstruction::Offset(X86_64::RA, -8));
28
29 entry
30}
31
32pub fn map_reg(reg: Reg) -> Result<Register, RegisterMappingError> {
34 const X86_GP_REG_MAP: [gimli::Register; 16] = [
36 X86_64::RAX,
37 X86_64::RCX,
38 X86_64::RDX,
39 X86_64::RBX,
40 X86_64::RSP,
41 X86_64::RBP,
42 X86_64::RSI,
43 X86_64::RDI,
44 X86_64::R8,
45 X86_64::R9,
46 X86_64::R10,
47 X86_64::R11,
48 X86_64::R12,
49 X86_64::R13,
50 X86_64::R14,
51 X86_64::R15,
52 ];
53 const X86_XMM_REG_MAP: [gimli::Register; 16] = [
54 X86_64::XMM0,
55 X86_64::XMM1,
56 X86_64::XMM2,
57 X86_64::XMM3,
58 X86_64::XMM4,
59 X86_64::XMM5,
60 X86_64::XMM6,
61 X86_64::XMM7,
62 X86_64::XMM8,
63 X86_64::XMM9,
64 X86_64::XMM10,
65 X86_64::XMM11,
66 X86_64::XMM12,
67 X86_64::XMM13,
68 X86_64::XMM14,
69 X86_64::XMM15,
70 ];
71
72 match reg.class() {
73 RegClass::Int => {
74 Ok(X86_GP_REG_MAP[reg.to_real_reg().unwrap().hw_enc() as usize])
77 }
78 RegClass::Float => Ok(X86_XMM_REG_MAP[reg.to_real_reg().unwrap().hw_enc() as usize]),
79 RegClass::Vector => unreachable!(),
80 }
81}
82
83pub(crate) struct RegisterMapper;
84
85impl crate::isa::unwind::systemv::RegisterMapper<Reg> for RegisterMapper {
86 fn map(&self, reg: Reg) -> Result<u16, RegisterMappingError> {
87 Ok(map_reg(reg)?.0)
88 }
89 fn fp(&self) -> Option<u16> {
90 Some(X86_64::RBP.0)
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use crate::cursor::{Cursor, FuncCursor};
97 use crate::ir::{
98 types, AbiParam, Function, InstBuilder, Signature, StackSlotData, StackSlotKind,
99 };
100 use crate::isa::{lookup, CallConv};
101 use crate::settings::{builder, Flags};
102 use crate::Context;
103 use gimli::write::Address;
104 use target_lexicon::triple;
105
106 #[test]
107 fn test_simple_func() {
108 let isa = lookup(triple!("x86_64"))
109 .expect("expect x86 ISA")
110 .finish(Flags::new(builder()))
111 .expect("expect backend creation to succeed");
112
113 let mut context = Context::for_function(create_function(
114 CallConv::SystemV,
115 Some(StackSlotData::new(StackSlotKind::ExplicitSlot, 64, 0)),
116 ));
117
118 let code = context
119 .compile(&*isa, &mut Default::default())
120 .expect("expected compilation");
121
122 let fde = match code
123 .create_unwind_info(isa.as_ref())
124 .expect("can create unwind info")
125 {
126 Some(crate::isa::unwind::UnwindInfo::SystemV(info)) => {
127 info.to_fde(Address::Constant(1234))
128 }
129 _ => panic!("expected unwind information"),
130 };
131
132 assert_eq!(format!("{fde:?}"), "FrameDescriptionEntry { address: Constant(1234), length: 17, lsda: None, instructions: [(1, CfaOffset(16)), (1, Offset(Register(6), -16)), (4, CfaRegister(Register(6)))] }");
133 }
134
135 fn create_function(call_conv: CallConv, stack_slot: Option<StackSlotData>) -> Function {
136 let mut func = Function::with_name_signature(Default::default(), Signature::new(call_conv));
137
138 let block0 = func.dfg.make_block();
139 let mut pos = FuncCursor::new(&mut func);
140 pos.insert_block(block0);
141 pos.ins().return_(&[]);
142
143 if let Some(stack_slot) = stack_slot {
144 func.sized_stack_slots.push(stack_slot);
145 }
146
147 func
148 }
149
150 #[test]
151 fn test_multi_return_func() {
152 let isa = lookup(triple!("x86_64"))
153 .expect("expect x86 ISA")
154 .finish(Flags::new(builder()))
155 .expect("expect backend creation to succeed");
156
157 let mut context = Context::for_function(create_multi_return_function(CallConv::SystemV));
158
159 let code = context
160 .compile(&*isa, &mut Default::default())
161 .expect("expected compilation");
162
163 let fde = match code
164 .create_unwind_info(isa.as_ref())
165 .expect("can create unwind info")
166 {
167 Some(crate::isa::unwind::UnwindInfo::SystemV(info)) => {
168 info.to_fde(Address::Constant(4321))
169 }
170 _ => panic!("expected unwind information"),
171 };
172
173 assert_eq!(format!("{fde:?}"), "FrameDescriptionEntry { address: Constant(4321), length: 22, lsda: None, instructions: [(1, CfaOffset(16)), (1, Offset(Register(6), -16)), (4, CfaRegister(Register(6)))] }");
174 }
175
176 fn create_multi_return_function(call_conv: CallConv) -> Function {
177 let mut sig = Signature::new(call_conv);
178 sig.params.push(AbiParam::new(types::I32));
179 let mut func = Function::with_name_signature(Default::default(), sig);
180
181 let block0 = func.dfg.make_block();
182 let v0 = func.dfg.append_block_param(block0, types::I32);
183 let block1 = func.dfg.make_block();
184 let block2 = func.dfg.make_block();
185
186 let mut pos = FuncCursor::new(&mut func);
187 pos.insert_block(block0);
188 pos.ins().brif(v0, block2, &[], block1, &[]);
189
190 pos.insert_block(block1);
191 pos.ins().return_(&[]);
192
193 pos.insert_block(block2);
194 pos.ins().return_(&[]);
195
196 func
197 }
198}