|
1 | | -use super::*; |
2 | | -use crate::isa::aarch64::inst::{args::PairAMode, imms::Imm12, regs, ALUOp, Inst}; |
3 | | -use crate::isa::unwind::input::{UnwindCode, UnwindInfo}; |
4 | | -use crate::machinst::UnwindInfoContext; |
5 | | -use crate::result::CodegenResult; |
6 | | -use alloc::vec::Vec; |
7 | | -use regalloc::Reg; |
8 | | - |
9 | 1 | #[cfg(feature = "unwind")] |
10 | 2 | pub(crate) mod systemv; |
11 | | - |
12 | | -pub struct AArch64UnwindInfo; |
13 | | - |
14 | | -impl UnwindInfoGenerator<Inst> for AArch64UnwindInfo { |
15 | | - fn create_unwind_info( |
16 | | - context: UnwindInfoContext<Inst>, |
17 | | - ) -> CodegenResult<Option<UnwindInfo<Reg>>> { |
18 | | - let word_size = 8u8; |
19 | | - let pair_size = word_size * 2; |
20 | | - let mut codes = Vec::new(); |
21 | | - |
22 | | - for i in context.prologue.clone() { |
23 | | - let i = i as usize; |
24 | | - let inst = &context.insts[i]; |
25 | | - let offset = context.insts_layout[i]; |
26 | | - |
27 | | - match inst { |
28 | | - Inst::StoreP64 { |
29 | | - rt, |
30 | | - rt2, |
31 | | - mem: PairAMode::PreIndexed(rn, imm7), |
32 | | - .. |
33 | | - } if *rt == regs::fp_reg() |
34 | | - && *rt2 == regs::link_reg() |
35 | | - && *rn == regs::writable_stack_reg() |
36 | | - && imm7.value == -(pair_size as i16) => |
37 | | - { |
38 | | - // stp fp (x29), lr (x30), [sp, #-16]! |
39 | | - codes.push(( |
40 | | - offset, |
41 | | - UnwindCode::StackAlloc { |
42 | | - size: pair_size as u32, |
43 | | - }, |
44 | | - )); |
45 | | - codes.push(( |
46 | | - offset, |
47 | | - UnwindCode::SaveRegister { |
48 | | - reg: *rt, |
49 | | - stack_offset: 0, |
50 | | - }, |
51 | | - )); |
52 | | - codes.push(( |
53 | | - offset, |
54 | | - UnwindCode::SaveRegister { |
55 | | - reg: *rt2, |
56 | | - stack_offset: word_size as u32, |
57 | | - }, |
58 | | - )); |
59 | | - } |
60 | | - Inst::StoreP64 { |
61 | | - rt, |
62 | | - rt2, |
63 | | - mem: PairAMode::PreIndexed(rn, imm7), |
64 | | - .. |
65 | | - } if rn.to_reg() == regs::stack_reg() && imm7.value % (pair_size as i16) == 0 => { |
66 | | - // stp r1, r2, [sp, #(i * #16)] |
67 | | - let stack_offset = imm7.value as u32; |
68 | | - codes.push(( |
69 | | - offset, |
70 | | - UnwindCode::SaveRegister { |
71 | | - reg: *rt, |
72 | | - stack_offset, |
73 | | - }, |
74 | | - )); |
75 | | - if *rt2 != regs::zero_reg() { |
76 | | - codes.push(( |
77 | | - offset, |
78 | | - UnwindCode::SaveRegister { |
79 | | - reg: *rt2, |
80 | | - stack_offset: stack_offset + word_size as u32, |
81 | | - }, |
82 | | - )); |
83 | | - } |
84 | | - } |
85 | | - Inst::AluRRImm12 { |
86 | | - alu_op: ALUOp::Add64, |
87 | | - rd, |
88 | | - rn, |
89 | | - imm12: |
90 | | - Imm12 { |
91 | | - bits: 0, |
92 | | - shift12: false, |
93 | | - }, |
94 | | - } if *rd == regs::writable_fp_reg() && *rn == regs::stack_reg() => { |
95 | | - // mov fp (x29), sp. |
96 | | - codes.push((offset, UnwindCode::SetFramePointer { reg: rd.to_reg() })); |
97 | | - } |
98 | | - Inst::VirtualSPOffsetAdj { offset: adj } if offset > 0 => { |
99 | | - codes.push((offset, UnwindCode::StackAlloc { size: *adj as u32 })); |
100 | | - } |
101 | | - _ => {} |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - // TODO epilogues |
106 | | - |
107 | | - let prologue_size = if context.prologue.len() == 0 { |
108 | | - 0 |
109 | | - } else { |
110 | | - context.insts_layout[context.prologue.end as usize - 1] |
111 | | - }; |
112 | | - |
113 | | - Ok(Some(UnwindInfo { |
114 | | - prologue_size, |
115 | | - prologue_unwind_codes: codes, |
116 | | - epilogues_unwind_codes: vec![], |
117 | | - function_size: context.len, |
118 | | - word_size, |
119 | | - initial_sp_offset: 0, |
120 | | - })) |
121 | | - } |
122 | | -} |
123 | | - |
124 | | -#[cfg(test)] |
125 | | -mod tests { |
126 | | - use super::*; |
127 | | - use crate::cursor::{Cursor, FuncCursor}; |
128 | | - use crate::ir::{ExternalName, Function, InstBuilder, Signature, StackSlotData, StackSlotKind}; |
129 | | - use crate::isa::{lookup, CallConv}; |
130 | | - use crate::settings::{builder, Flags}; |
131 | | - use crate::Context; |
132 | | - use std::str::FromStr; |
133 | | - use target_lexicon::triple; |
134 | | - |
135 | | - #[test] |
136 | | - fn test_simple_func() { |
137 | | - let isa = lookup(triple!("aarch64")) |
138 | | - .expect("expect aarch64 ISA") |
139 | | - .finish(Flags::new(builder())); |
140 | | - |
141 | | - let mut context = Context::for_function(create_function( |
142 | | - CallConv::SystemV, |
143 | | - Some(StackSlotData::new(StackSlotKind::ExplicitSlot, 64)), |
144 | | - )); |
145 | | - |
146 | | - context.compile(&*isa).expect("expected compilation"); |
147 | | - |
148 | | - let result = context.mach_compile_result.unwrap(); |
149 | | - let unwind_info = result.unwind_info.unwrap(); |
150 | | - |
151 | | - assert_eq!( |
152 | | - unwind_info, |
153 | | - UnwindInfo { |
154 | | - prologue_size: 12, |
155 | | - prologue_unwind_codes: vec![ |
156 | | - (4, UnwindCode::StackAlloc { size: 16 }), |
157 | | - ( |
158 | | - 4, |
159 | | - UnwindCode::SaveRegister { |
160 | | - reg: regs::fp_reg(), |
161 | | - stack_offset: 0 |
162 | | - } |
163 | | - ), |
164 | | - ( |
165 | | - 4, |
166 | | - UnwindCode::SaveRegister { |
167 | | - reg: regs::link_reg(), |
168 | | - stack_offset: 8 |
169 | | - } |
170 | | - ), |
171 | | - ( |
172 | | - 8, |
173 | | - UnwindCode::SetFramePointer { |
174 | | - reg: regs::fp_reg() |
175 | | - } |
176 | | - ) |
177 | | - ], |
178 | | - epilogues_unwind_codes: vec![], |
179 | | - function_size: 24, |
180 | | - word_size: 8, |
181 | | - initial_sp_offset: 0, |
182 | | - } |
183 | | - ); |
184 | | - } |
185 | | - |
186 | | - fn create_function(call_conv: CallConv, stack_slot: Option<StackSlotData>) -> Function { |
187 | | - let mut func = |
188 | | - Function::with_name_signature(ExternalName::user(0, 0), Signature::new(call_conv)); |
189 | | - |
190 | | - let block0 = func.dfg.make_block(); |
191 | | - let mut pos = FuncCursor::new(&mut func); |
192 | | - pos.insert_block(block0); |
193 | | - pos.ins().return_(&[]); |
194 | | - |
195 | | - if let Some(stack_slot) = stack_slot { |
196 | | - func.stack_slots.push(stack_slot); |
197 | | - } |
198 | | - |
199 | | - func |
200 | | - } |
201 | | -} |
0 commit comments