+
+#if GDB_SELF_TEST
+
+namespace selftests {
+
+/* Unit test to function execute_cfa_program. */
+
+static void
+execute_cfa_program_test (struct gdbarch *gdbarch)
+{
+ struct dwarf2_fde fde;
+ struct dwarf2_cie cie;
+
+ memset (&fde, 0, sizeof fde);
+ memset (&cie, 0, sizeof cie);
+
+ cie.data_alignment_factor = -4;
+ cie.code_alignment_factor = 2;
+ fde.cie = &cie;
+
+ dwarf2_frame_state fs (0, fde.cie);
+
+ gdb_byte insns[] =
+ {
+ DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
+ DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
+ DW_CFA_remember_state,
+ DW_CFA_restore_state,
+ };
+
+ const gdb_byte *insn_end = insns + sizeof (insns);
+ const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
+ 0, &fs);
+
+ SELF_CHECK (out == insn_end);
+ SELF_CHECK (fs.pc == 0);
+
+ /* The instructions above only use r1 and r2, but the register numbers
+ used are adjusted by dwarf2_frame_adjust_regnum. */
+ auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
+ auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
+
+ SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
+
+ SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
+ SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
+
+ for (auto i = 0; i < fs.regs.reg.size (); i++)
+ if (i != r2)
+ SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
+
+ SELF_CHECK (fs.regs.cfa_reg == 1);
+ SELF_CHECK (fs.regs.cfa_offset == 4);
+ SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
+ SELF_CHECK (fs.regs.cfa_exp == NULL);
+ SELF_CHECK (fs.regs.prev == NULL);
+}
+
+} // namespace selftests
+#endif /* GDB_SELF_TEST */
+