m68hc11-tdep.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /* Target-dependent code for Motorola 68HC11 & 68HC12
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Stephane Carrez, stcarrez@nerim.fr
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "frame-unwind.h"
  18. #include "frame-base.h"
  19. #include "dwarf2/frame.h"
  20. #include "trad-frame.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "gdbcmd.h"
  24. #include "gdbcore.h"
  25. #include "value.h"
  26. #include "inferior.h"
  27. #include "dis-asm.h"
  28. #include "symfile.h"
  29. #include "objfiles.h"
  30. #include "arch-utils.h"
  31. #include "regcache.h"
  32. #include "reggroups.h"
  33. #include "gdbarch.h"
  34. #include "target.h"
  35. #include "opcode/m68hc11.h"
  36. #include "elf/m68hc11.h"
  37. #include "elf-bfd.h"
  38. /* Macros for setting and testing a bit in a minimal symbol.
  39. For 68HC11/68HC12 we have two flags that tell which return
  40. type the function is using. This is used for prologue and frame
  41. analysis to compute correct stack frame layout.
  42. The MSB of the minimal symbol's "info" field is used for this purpose.
  43. MSYMBOL_SET_RTC Actually sets the "RTC" bit.
  44. MSYMBOL_SET_RTI Actually sets the "RTI" bit.
  45. MSYMBOL_IS_RTC Tests the "RTC" bit in a minimal symbol.
  46. MSYMBOL_IS_RTI Tests the "RTC" bit in a minimal symbol. */
  47. #define MSYMBOL_SET_RTC(msym) \
  48. MSYMBOL_TARGET_FLAG_1 (msym) = 1
  49. #define MSYMBOL_SET_RTI(msym) \
  50. MSYMBOL_TARGET_FLAG_2 (msym) = 1
  51. #define MSYMBOL_IS_RTC(msym) \
  52. MSYMBOL_TARGET_FLAG_1 (msym)
  53. #define MSYMBOL_IS_RTI(msym) \
  54. MSYMBOL_TARGET_FLAG_2 (msym)
  55. enum insn_return_kind {
  56. RETURN_RTS,
  57. RETURN_RTC,
  58. RETURN_RTI
  59. };
  60. /* Register numbers of various important registers. */
  61. #define HARD_X_REGNUM 0
  62. #define HARD_D_REGNUM 1
  63. #define HARD_Y_REGNUM 2
  64. #define HARD_SP_REGNUM 3
  65. #define HARD_PC_REGNUM 4
  66. #define HARD_A_REGNUM 5
  67. #define HARD_B_REGNUM 6
  68. #define HARD_CCR_REGNUM 7
  69. /* 68HC12 page number register.
  70. Note: to keep a compatibility with gcc register naming, we must
  71. not have to rename FP and other soft registers. The page register
  72. is a real hard register and must therefore be counted by gdbarch_num_regs.
  73. For this it has the same number as Z register (which is not used). */
  74. #define HARD_PAGE_REGNUM 8
  75. #define M68HC11_LAST_HARD_REG (HARD_PAGE_REGNUM)
  76. /* Z is replaced by X or Y by gcc during machine reorg.
  77. ??? There is no way to get it and even know whether
  78. it's in X or Y or in ZS. */
  79. #define SOFT_Z_REGNUM 8
  80. /* Soft registers. These registers are special. There are treated
  81. like normal hard registers by gcc and gdb (ie, within dwarf2 info).
  82. They are physically located in memory. */
  83. #define SOFT_FP_REGNUM 9
  84. #define SOFT_TMP_REGNUM 10
  85. #define SOFT_ZS_REGNUM 11
  86. #define SOFT_XY_REGNUM 12
  87. #define SOFT_UNUSED_REGNUM 13
  88. #define SOFT_D1_REGNUM 14
  89. #define SOFT_D32_REGNUM (SOFT_D1_REGNUM+31)
  90. #define M68HC11_MAX_SOFT_REGS 32
  91. #define M68HC11_NUM_REGS (M68HC11_LAST_HARD_REG + 1)
  92. #define M68HC11_NUM_PSEUDO_REGS (M68HC11_MAX_SOFT_REGS+5)
  93. #define M68HC11_ALL_REGS (M68HC11_NUM_REGS+M68HC11_NUM_PSEUDO_REGS)
  94. #define M68HC11_REG_SIZE (2)
  95. #define M68HC12_NUM_REGS (9)
  96. #define M68HC12_NUM_PSEUDO_REGS ((M68HC11_MAX_SOFT_REGS+5)+1-1)
  97. #define M68HC12_HARD_PC_REGNUM (SOFT_D32_REGNUM+1)
  98. struct insn_sequence;
  99. struct m68gc11_gdbarch_tdep : gdbarch_tdep
  100. {
  101. /* Stack pointer correction value. For 68hc11, the stack pointer points
  102. to the next push location. An offset of 1 must be applied to obtain
  103. the address where the last value is saved. For 68hc12, the stack
  104. pointer points to the last value pushed. No offset is necessary. */
  105. int stack_correction = 0;
  106. /* Description of instructions in the prologue. */
  107. struct insn_sequence *prologue = nullptr;
  108. /* True if the page memory bank register is available
  109. and must be used. */
  110. int use_page_register = 0;
  111. /* ELF flags for ABI. */
  112. int elf_flags = 0;
  113. };
  114. static int
  115. stack_correction (gdbarch *arch)
  116. {
  117. m68gc11_gdbarch_tdep *tdep = (m68gc11_gdbarch_tdep *) gdbarch_tdep (arch);
  118. return tdep->stack_correction;
  119. }
  120. static int
  121. use_page_register (gdbarch *arch)
  122. {
  123. m68gc11_gdbarch_tdep *tdep = (m68gc11_gdbarch_tdep *) gdbarch_tdep (arch);
  124. return tdep->stack_correction;
  125. }
  126. struct m68hc11_unwind_cache
  127. {
  128. /* The previous frame's inner most stack address. Used as this
  129. frame ID's stack_addr. */
  130. CORE_ADDR prev_sp;
  131. /* The frame's base, optionally used by the high-level debug info. */
  132. CORE_ADDR base;
  133. CORE_ADDR pc;
  134. int size;
  135. int prologue_type;
  136. CORE_ADDR return_pc;
  137. CORE_ADDR sp_offset;
  138. int frameless;
  139. enum insn_return_kind return_kind;
  140. /* Table indicating the location of each and every register. */
  141. trad_frame_saved_reg *saved_regs;
  142. };
  143. /* Table of registers for 68HC11. This includes the hard registers
  144. and the soft registers used by GCC. */
  145. static const char *
  146. m68hc11_register_names[] =
  147. {
  148. "x", "d", "y", "sp", "pc", "a", "b",
  149. "ccr", "page", "frame","tmp", "zs", "xy", 0,
  150. "d1", "d2", "d3", "d4", "d5", "d6", "d7",
  151. "d8", "d9", "d10", "d11", "d12", "d13", "d14",
  152. "d15", "d16", "d17", "d18", "d19", "d20", "d21",
  153. "d22", "d23", "d24", "d25", "d26", "d27", "d28",
  154. "d29", "d30", "d31", "d32"
  155. };
  156. struct m68hc11_soft_reg
  157. {
  158. const char *name;
  159. CORE_ADDR addr;
  160. };
  161. static struct m68hc11_soft_reg soft_regs[M68HC11_ALL_REGS];
  162. #define M68HC11_FP_ADDR soft_regs[SOFT_FP_REGNUM].addr
  163. static int soft_min_addr;
  164. static int soft_max_addr;
  165. static int soft_reg_initialized = 0;
  166. /* Look in the symbol table for the address of a pseudo register
  167. in memory. If we don't find it, pretend the register is not used
  168. and not available. */
  169. static void
  170. m68hc11_get_register_info (struct m68hc11_soft_reg *reg, const char *name)
  171. {
  172. struct bound_minimal_symbol msymbol;
  173. msymbol = lookup_minimal_symbol (name, NULL, NULL);
  174. if (msymbol.minsym)
  175. {
  176. reg->addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  177. reg->name = xstrdup (name);
  178. /* Keep track of the address range for soft registers. */
  179. if (reg->addr < (CORE_ADDR) soft_min_addr)
  180. soft_min_addr = reg->addr;
  181. if (reg->addr > (CORE_ADDR) soft_max_addr)
  182. soft_max_addr = reg->addr;
  183. }
  184. else
  185. {
  186. reg->name = 0;
  187. reg->addr = 0;
  188. }
  189. }
  190. /* Initialize the table of soft register addresses according
  191. to the symbol table. */
  192. static void
  193. m68hc11_initialize_register_info (void)
  194. {
  195. int i;
  196. if (soft_reg_initialized)
  197. return;
  198. soft_min_addr = INT_MAX;
  199. soft_max_addr = 0;
  200. for (i = 0; i < M68HC11_ALL_REGS; i++)
  201. {
  202. soft_regs[i].name = 0;
  203. }
  204. m68hc11_get_register_info (&soft_regs[SOFT_FP_REGNUM], "_.frame");
  205. m68hc11_get_register_info (&soft_regs[SOFT_TMP_REGNUM], "_.tmp");
  206. m68hc11_get_register_info (&soft_regs[SOFT_ZS_REGNUM], "_.z");
  207. soft_regs[SOFT_Z_REGNUM] = soft_regs[SOFT_ZS_REGNUM];
  208. m68hc11_get_register_info (&soft_regs[SOFT_XY_REGNUM], "_.xy");
  209. for (i = SOFT_D1_REGNUM; i < M68HC11_MAX_SOFT_REGS; i++)
  210. {
  211. char buf[10];
  212. xsnprintf (buf, sizeof (buf), "_.d%d", i - SOFT_D1_REGNUM + 1);
  213. m68hc11_get_register_info (&soft_regs[i], buf);
  214. }
  215. if (soft_regs[SOFT_FP_REGNUM].name == 0)
  216. warning (_("No frame soft register found in the symbol table.\n"
  217. "Stack backtrace will not work."));
  218. soft_reg_initialized = 1;
  219. }
  220. /* Given an address in memory, return the soft register number if
  221. that address corresponds to a soft register. Returns -1 if not. */
  222. static int
  223. m68hc11_which_soft_register (CORE_ADDR addr)
  224. {
  225. int i;
  226. if (addr < soft_min_addr || addr > soft_max_addr)
  227. return -1;
  228. for (i = SOFT_FP_REGNUM; i < M68HC11_ALL_REGS; i++)
  229. {
  230. if (soft_regs[i].name && soft_regs[i].addr == addr)
  231. return i;
  232. }
  233. return -1;
  234. }
  235. /* Fetch a pseudo register. The 68hc11 soft registers are treated like
  236. pseudo registers. They are located in memory. Translate the register
  237. fetch into a memory read. */
  238. static enum register_status
  239. m68hc11_pseudo_register_read (struct gdbarch *gdbarch,
  240. readable_regcache *regcache,
  241. int regno, gdb_byte *buf)
  242. {
  243. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  244. /* The PC is a pseudo reg only for 68HC12 with the memory bank
  245. addressing mode. */
  246. if (regno == M68HC12_HARD_PC_REGNUM)
  247. {
  248. ULONGEST pc;
  249. const int regsize = 4;
  250. enum register_status status;
  251. status = regcache->cooked_read (HARD_PC_REGNUM, &pc);
  252. if (status != REG_VALID)
  253. return status;
  254. if (pc >= 0x8000 && pc < 0xc000)
  255. {
  256. ULONGEST page;
  257. regcache->cooked_read (HARD_PAGE_REGNUM, &page);
  258. pc -= 0x8000;
  259. pc += (page << 14);
  260. pc += 0x1000000;
  261. }
  262. store_unsigned_integer (buf, regsize, byte_order, pc);
  263. return REG_VALID;
  264. }
  265. m68hc11_initialize_register_info ();
  266. /* Fetch a soft register: translate into a memory read. */
  267. if (soft_regs[regno].name)
  268. {
  269. target_read_memory (soft_regs[regno].addr, buf, 2);
  270. }
  271. else
  272. {
  273. memset (buf, 0, 2);
  274. }
  275. return REG_VALID;
  276. }
  277. /* Store a pseudo register. Translate the register store
  278. into a memory write. */
  279. static void
  280. m68hc11_pseudo_register_write (struct gdbarch *gdbarch,
  281. struct regcache *regcache,
  282. int regno, const gdb_byte *buf)
  283. {
  284. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  285. /* The PC is a pseudo reg only for 68HC12 with the memory bank
  286. addressing mode. */
  287. if (regno == M68HC12_HARD_PC_REGNUM)
  288. {
  289. const int regsize = 4;
  290. gdb_byte *tmp = (gdb_byte *) alloca (regsize);
  291. CORE_ADDR pc;
  292. memcpy (tmp, buf, regsize);
  293. pc = extract_unsigned_integer (tmp, regsize, byte_order);
  294. if (pc >= 0x1000000)
  295. {
  296. pc -= 0x1000000;
  297. regcache_cooked_write_unsigned (regcache, HARD_PAGE_REGNUM,
  298. (pc >> 14) & 0x0ff);
  299. pc &= 0x03fff;
  300. regcache_cooked_write_unsigned (regcache, HARD_PC_REGNUM,
  301. pc + 0x8000);
  302. }
  303. else
  304. regcache_cooked_write_unsigned (regcache, HARD_PC_REGNUM, pc);
  305. return;
  306. }
  307. m68hc11_initialize_register_info ();
  308. /* Store a soft register: translate into a memory write. */
  309. if (soft_regs[regno].name)
  310. {
  311. const int regsize = 2;
  312. gdb_byte *tmp = (gdb_byte *) alloca (regsize);
  313. memcpy (tmp, buf, regsize);
  314. target_write_memory (soft_regs[regno].addr, tmp, regsize);
  315. }
  316. }
  317. static const char *
  318. m68hc11_register_name (struct gdbarch *gdbarch, int reg_nr)
  319. {
  320. if (reg_nr == M68HC12_HARD_PC_REGNUM && use_page_register (gdbarch))
  321. return "pc";
  322. if (reg_nr == HARD_PC_REGNUM && use_page_register (gdbarch))
  323. return "ppc";
  324. if (reg_nr < 0)
  325. return NULL;
  326. if (reg_nr >= M68HC11_ALL_REGS)
  327. return NULL;
  328. m68hc11_initialize_register_info ();
  329. /* If we don't know the address of a soft register, pretend it
  330. does not exist. */
  331. if (reg_nr > M68HC11_LAST_HARD_REG && soft_regs[reg_nr].name == 0)
  332. return NULL;
  333. return m68hc11_register_names[reg_nr];
  334. }
  335. constexpr gdb_byte m68hc11_break_insn[] = {0x0};
  336. typedef BP_MANIPULATION (m68hc11_break_insn) m68hc11_breakpoint;
  337. /* 68HC11 & 68HC12 prologue analysis. */
  338. #define MAX_CODES 12
  339. /* 68HC11 opcodes. */
  340. #undef M6811_OP_PAGE2
  341. #define M6811_OP_PAGE2 (0x18)
  342. #define M6811_OP_LDX (0xde)
  343. #define M6811_OP_LDX_EXT (0xfe)
  344. #define M6811_OP_PSHX (0x3c)
  345. #define M6811_OP_STS (0x9f)
  346. #define M6811_OP_STS_EXT (0xbf)
  347. #define M6811_OP_TSX (0x30)
  348. #define M6811_OP_XGDX (0x8f)
  349. #define M6811_OP_ADDD (0xc3)
  350. #define M6811_OP_TXS (0x35)
  351. #define M6811_OP_DES (0x34)
  352. /* 68HC12 opcodes. */
  353. #define M6812_OP_PAGE2 (0x18)
  354. #define M6812_OP_MOVW (0x01)
  355. #define M6812_PB_PSHW (0xae)
  356. #define M6812_OP_STS (0x5f)
  357. #define M6812_OP_STS_EXT (0x7f)
  358. #define M6812_OP_LEAS (0x1b)
  359. #define M6812_OP_PSHX (0x34)
  360. #define M6812_OP_PSHY (0x35)
  361. /* Operand extraction. */
  362. #define OP_DIRECT (0x100) /* 8-byte direct addressing. */
  363. #define OP_IMM_LOW (0x200) /* Low part of 16-bit constant/address. */
  364. #define OP_IMM_HIGH (0x300) /* High part of 16-bit constant/address. */
  365. #define OP_PBYTE (0x400) /* 68HC12 indexed operand. */
  366. /* Identification of the sequence. */
  367. enum m6811_seq_type
  368. {
  369. P_LAST = 0,
  370. P_SAVE_REG, /* Save a register on the stack. */
  371. P_SET_FRAME, /* Setup the frame pointer. */
  372. P_LOCAL_1, /* Allocate 1 byte for locals. */
  373. P_LOCAL_2, /* Allocate 2 bytes for locals. */
  374. P_LOCAL_N /* Allocate N bytes for locals. */
  375. };
  376. struct insn_sequence {
  377. enum m6811_seq_type type;
  378. unsigned length;
  379. unsigned short code[MAX_CODES];
  380. };
  381. /* Sequence of instructions in the 68HC11 function prologue. */
  382. static struct insn_sequence m6811_prologue[] = {
  383. /* Sequences to save a soft-register. */
  384. { P_SAVE_REG, 3, { M6811_OP_LDX, OP_DIRECT,
  385. M6811_OP_PSHX } },
  386. { P_SAVE_REG, 5, { M6811_OP_PAGE2, M6811_OP_LDX, OP_DIRECT,
  387. M6811_OP_PAGE2, M6811_OP_PSHX } },
  388. { P_SAVE_REG, 4, { M6811_OP_LDX_EXT, OP_IMM_HIGH, OP_IMM_LOW,
  389. M6811_OP_PSHX } },
  390. { P_SAVE_REG, 6, { M6811_OP_PAGE2, M6811_OP_LDX_EXT, OP_IMM_HIGH, OP_IMM_LOW,
  391. M6811_OP_PAGE2, M6811_OP_PSHX } },
  392. /* Sequences to allocate local variables. */
  393. { P_LOCAL_N, 7, { M6811_OP_TSX,
  394. M6811_OP_XGDX,
  395. M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
  396. M6811_OP_XGDX,
  397. M6811_OP_TXS } },
  398. { P_LOCAL_N, 11, { M6811_OP_PAGE2, M6811_OP_TSX,
  399. M6811_OP_PAGE2, M6811_OP_XGDX,
  400. M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
  401. M6811_OP_PAGE2, M6811_OP_XGDX,
  402. M6811_OP_PAGE2, M6811_OP_TXS } },
  403. { P_LOCAL_1, 1, { M6811_OP_DES } },
  404. { P_LOCAL_2, 1, { M6811_OP_PSHX } },
  405. { P_LOCAL_2, 2, { M6811_OP_PAGE2, M6811_OP_PSHX } },
  406. /* Initialize the frame pointer. */
  407. { P_SET_FRAME, 2, { M6811_OP_STS, OP_DIRECT } },
  408. { P_SET_FRAME, 3, { M6811_OP_STS_EXT, OP_IMM_HIGH, OP_IMM_LOW } },
  409. { P_LAST, 0, { 0 } }
  410. };
  411. /* Sequence of instructions in the 68HC12 function prologue. */
  412. static struct insn_sequence m6812_prologue[] = {
  413. { P_SAVE_REG, 5, { M6812_OP_PAGE2, M6812_OP_MOVW, M6812_PB_PSHW,
  414. OP_IMM_HIGH, OP_IMM_LOW } },
  415. { P_SET_FRAME, 2, { M6812_OP_STS, OP_DIRECT } },
  416. { P_SET_FRAME, 3, { M6812_OP_STS_EXT, OP_IMM_HIGH, OP_IMM_LOW } },
  417. { P_LOCAL_N, 2, { M6812_OP_LEAS, OP_PBYTE } },
  418. { P_LOCAL_2, 1, { M6812_OP_PSHX } },
  419. { P_LOCAL_2, 1, { M6812_OP_PSHY } },
  420. { P_LAST, 0 }
  421. };
  422. /* Analyze the sequence of instructions starting at the given address.
  423. Returns a pointer to the sequence when it is recognized and
  424. the optional value (constant/address) associated with it. */
  425. static struct insn_sequence *
  426. m68hc11_analyze_instruction (struct gdbarch *gdbarch,
  427. struct insn_sequence *seq, CORE_ADDR pc,
  428. CORE_ADDR *val)
  429. {
  430. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  431. unsigned char buffer[MAX_CODES];
  432. unsigned bufsize;
  433. unsigned j;
  434. CORE_ADDR cur_val;
  435. short v = 0;
  436. bufsize = 0;
  437. for (; seq->type != P_LAST; seq++)
  438. {
  439. cur_val = 0;
  440. for (j = 0; j < seq->length; j++)
  441. {
  442. if (bufsize < j + 1)
  443. {
  444. buffer[bufsize] = read_memory_unsigned_integer (pc + bufsize,
  445. 1, byte_order);
  446. bufsize++;
  447. }
  448. /* Continue while we match the opcode. */
  449. if (seq->code[j] == buffer[j])
  450. continue;
  451. if ((seq->code[j] & 0xf00) == 0)
  452. break;
  453. /* Extract a sequence parameter (address or constant). */
  454. switch (seq->code[j])
  455. {
  456. case OP_DIRECT:
  457. cur_val = (CORE_ADDR) buffer[j];
  458. break;
  459. case OP_IMM_HIGH:
  460. cur_val = cur_val & 0x0ff;
  461. cur_val |= (buffer[j] << 8);
  462. break;
  463. case OP_IMM_LOW:
  464. cur_val &= 0x0ff00;
  465. cur_val |= buffer[j];
  466. break;
  467. case OP_PBYTE:
  468. if ((buffer[j] & 0xE0) == 0x80)
  469. {
  470. v = buffer[j] & 0x1f;
  471. if (v & 0x10)
  472. v |= 0xfff0;
  473. }
  474. else if ((buffer[j] & 0xfe) == 0xf0)
  475. {
  476. v = read_memory_unsigned_integer (pc + j + 1, 1, byte_order);
  477. if (buffer[j] & 1)
  478. v |= 0xff00;
  479. }
  480. else if (buffer[j] == 0xf2)
  481. {
  482. v = read_memory_unsigned_integer (pc + j + 1, 2, byte_order);
  483. }
  484. cur_val = v;
  485. break;
  486. }
  487. }
  488. /* We have a full match. */
  489. if (j == seq->length)
  490. {
  491. *val = cur_val;
  492. return seq;
  493. }
  494. }
  495. return 0;
  496. }
  497. /* Return the instruction that the function at the PC is using. */
  498. static enum insn_return_kind
  499. m68hc11_get_return_insn (CORE_ADDR pc)
  500. {
  501. struct bound_minimal_symbol sym;
  502. /* A flag indicating that this is a STO_M68HC12_FAR or STO_M68HC12_INTERRUPT
  503. function is stored by elfread.c in the high bit of the info field.
  504. Use this to decide which instruction the function uses to return. */
  505. sym = lookup_minimal_symbol_by_pc (pc);
  506. if (sym.minsym == 0)
  507. return RETURN_RTS;
  508. if (MSYMBOL_IS_RTC (sym.minsym))
  509. return RETURN_RTC;
  510. else if (MSYMBOL_IS_RTI (sym.minsym))
  511. return RETURN_RTI;
  512. else
  513. return RETURN_RTS;
  514. }
  515. /* Analyze the function prologue to find some information
  516. about the function:
  517. - the PC of the first line (for m68hc11_skip_prologue)
  518. - the offset of the previous frame saved address (from current frame)
  519. - the soft registers which are pushed. */
  520. static CORE_ADDR
  521. m68hc11_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  522. CORE_ADDR current_pc, struct m68hc11_unwind_cache *info)
  523. {
  524. LONGEST save_addr;
  525. CORE_ADDR func_end;
  526. int size;
  527. int found_frame_point;
  528. int saved_reg;
  529. int done = 0;
  530. struct insn_sequence *seq_table;
  531. info->size = 0;
  532. info->sp_offset = 0;
  533. if (pc >= current_pc)
  534. return current_pc;
  535. size = 0;
  536. m68hc11_initialize_register_info ();
  537. if (pc == 0)
  538. {
  539. info->size = 0;
  540. return pc;
  541. }
  542. m68gc11_gdbarch_tdep *tdep = (m68gc11_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  543. seq_table = tdep->prologue;
  544. /* The 68hc11 stack is as follows:
  545. | |
  546. +-----------+
  547. | |
  548. | args |
  549. | |
  550. +-----------+
  551. | PC-return |
  552. +-----------+
  553. | Old frame |
  554. +-----------+
  555. | |
  556. | Locals |
  557. | |
  558. +-----------+ <--- current frame
  559. | |
  560. With most processors (like 68K) the previous frame can be computed
  561. easily because it is always at a fixed offset (see link/unlink).
  562. That is, locals are accessed with negative offsets, arguments are
  563. accessed with positive ones. Since 68hc11 only supports offsets
  564. in the range [0..255], the frame is defined at the bottom of
  565. locals (see picture).
  566. The purpose of the analysis made here is to find out the size
  567. of locals in this function. An alternative to this is to use
  568. DWARF2 info. This would be better but I don't know how to
  569. access dwarf2 debug from this function.
  570. Walk from the function entry point to the point where we save
  571. the frame. While walking instructions, compute the size of bytes
  572. which are pushed. This gives us the index to access the previous
  573. frame.
  574. We limit the search to 128 bytes so that the algorithm is bounded
  575. in case of random and wrong code. We also stop and abort if
  576. we find an instruction which is not supposed to appear in the
  577. prologue (as generated by gcc 2.95, 2.96). */
  578. func_end = pc + 128;
  579. found_frame_point = 0;
  580. info->size = 0;
  581. save_addr = 0;
  582. while (!done && pc + 2 < func_end)
  583. {
  584. struct insn_sequence *seq;
  585. CORE_ADDR val;
  586. seq = m68hc11_analyze_instruction (gdbarch, seq_table, pc, &val);
  587. if (seq == 0)
  588. break;
  589. /* If we are within the instruction group, we can't advance the
  590. pc nor the stack offset. Otherwise the caller's stack computed
  591. from the current stack can be wrong. */
  592. if (pc + seq->length > current_pc)
  593. break;
  594. pc = pc + seq->length;
  595. if (seq->type == P_SAVE_REG)
  596. {
  597. if (found_frame_point)
  598. {
  599. saved_reg = m68hc11_which_soft_register (val);
  600. if (saved_reg < 0)
  601. break;
  602. save_addr -= 2;
  603. if (info->saved_regs)
  604. info->saved_regs[saved_reg].set_addr (save_addr);
  605. }
  606. else
  607. {
  608. size += 2;
  609. }
  610. }
  611. else if (seq->type == P_SET_FRAME)
  612. {
  613. found_frame_point = 1;
  614. info->size = size;
  615. }
  616. else if (seq->type == P_LOCAL_1)
  617. {
  618. size += 1;
  619. }
  620. else if (seq->type == P_LOCAL_2)
  621. {
  622. size += 2;
  623. }
  624. else if (seq->type == P_LOCAL_N)
  625. {
  626. /* Stack pointer is decremented for the allocation. */
  627. if (val & 0x8000)
  628. size -= (int) (val) | 0xffff0000;
  629. else
  630. size -= val;
  631. }
  632. }
  633. if (found_frame_point == 0)
  634. info->sp_offset = size;
  635. else
  636. info->sp_offset = -1;
  637. return pc;
  638. }
  639. static CORE_ADDR
  640. m68hc11_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  641. {
  642. CORE_ADDR func_addr, func_end;
  643. struct symtab_and_line sal;
  644. struct m68hc11_unwind_cache tmp_cache = { 0 };
  645. /* If we have line debugging information, then the end of the
  646. prologue should be the first assembly instruction of the
  647. first source line. */
  648. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  649. {
  650. sal = find_pc_line (func_addr, 0);
  651. if (sal.end && sal.end < func_end)
  652. return sal.end;
  653. }
  654. pc = m68hc11_scan_prologue (gdbarch, pc, (CORE_ADDR) -1, &tmp_cache);
  655. return pc;
  656. }
  657. /* Put here the code to store, into fi->saved_regs, the addresses of
  658. the saved registers of frame described by FRAME_INFO. This
  659. includes special registers such as pc and fp saved in special ways
  660. in the stack frame. sp is even more special: the address we return
  661. for it IS the sp for the next frame. */
  662. static struct m68hc11_unwind_cache *
  663. m68hc11_frame_unwind_cache (struct frame_info *this_frame,
  664. void **this_prologue_cache)
  665. {
  666. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  667. ULONGEST prev_sp;
  668. ULONGEST this_base;
  669. struct m68hc11_unwind_cache *info;
  670. CORE_ADDR current_pc;
  671. int i;
  672. if ((*this_prologue_cache))
  673. return (struct m68hc11_unwind_cache *) (*this_prologue_cache);
  674. info = FRAME_OBSTACK_ZALLOC (struct m68hc11_unwind_cache);
  675. (*this_prologue_cache) = info;
  676. info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  677. info->pc = get_frame_func (this_frame);
  678. info->size = 0;
  679. info->return_kind = m68hc11_get_return_insn (info->pc);
  680. /* The SP was moved to the FP. This indicates that a new frame
  681. was created. Get THIS frame's FP value by unwinding it from
  682. the next frame. */
  683. this_base = get_frame_register_unsigned (this_frame, SOFT_FP_REGNUM);
  684. if (this_base == 0)
  685. {
  686. info->base = 0;
  687. return info;
  688. }
  689. current_pc = get_frame_pc (this_frame);
  690. if (info->pc != 0)
  691. m68hc11_scan_prologue (gdbarch, info->pc, current_pc, info);
  692. info->saved_regs[HARD_PC_REGNUM].set_addr (info->size);
  693. if (info->sp_offset != (CORE_ADDR) -1)
  694. {
  695. info->saved_regs[HARD_PC_REGNUM].set_addr (info->sp_offset);
  696. this_base = get_frame_register_unsigned (this_frame, HARD_SP_REGNUM);
  697. prev_sp = this_base + info->sp_offset + 2;
  698. this_base += stack_correction (gdbarch);
  699. }
  700. else
  701. {
  702. /* The FP points at the last saved register. Adjust the FP back
  703. to before the first saved register giving the SP. */
  704. prev_sp = this_base + info->size + 2;
  705. this_base += stack_correction (gdbarch);
  706. if (soft_regs[SOFT_FP_REGNUM].name)
  707. info->saved_regs[SOFT_FP_REGNUM].set_addr (info->size - 2);
  708. }
  709. if (info->return_kind == RETURN_RTC)
  710. {
  711. prev_sp += 1;
  712. info->saved_regs[HARD_PAGE_REGNUM].set_addr (info->size);
  713. info->saved_regs[HARD_PC_REGNUM].set_addr (info->size + 1);
  714. }
  715. else if (info->return_kind == RETURN_RTI)
  716. {
  717. prev_sp += 7;
  718. info->saved_regs[HARD_CCR_REGNUM].set_addr (info->size);
  719. info->saved_regs[HARD_D_REGNUM].set_addr (info->size + 1);
  720. info->saved_regs[HARD_X_REGNUM].set_addr (info->size + 3);
  721. info->saved_regs[HARD_Y_REGNUM].set_addr (info->size + 5);
  722. info->saved_regs[HARD_PC_REGNUM].set_addr (info->size + 7);
  723. }
  724. /* Add 1 here to adjust for the post-decrement nature of the push
  725. instruction. */
  726. info->prev_sp = prev_sp;
  727. info->base = this_base;
  728. /* Adjust all the saved registers so that they contain addresses and not
  729. offsets. */
  730. for (i = 0; i < gdbarch_num_cooked_regs (gdbarch); i++)
  731. if (info->saved_regs[i].is_addr ())
  732. {
  733. info->saved_regs[i].set_addr (info->saved_regs[i].addr () + this_base);
  734. }
  735. /* The previous frame's SP needed to be computed. Save the computed
  736. value. */
  737. info->saved_regs[HARD_SP_REGNUM].set_value (info->prev_sp);
  738. return info;
  739. }
  740. /* Given a GDB frame, determine the address of the calling function's
  741. frame. This will be used to create a new GDB frame struct. */
  742. static void
  743. m68hc11_frame_this_id (struct frame_info *this_frame,
  744. void **this_prologue_cache,
  745. struct frame_id *this_id)
  746. {
  747. struct m68hc11_unwind_cache *info
  748. = m68hc11_frame_unwind_cache (this_frame, this_prologue_cache);
  749. CORE_ADDR base;
  750. CORE_ADDR func;
  751. struct frame_id id;
  752. /* The FUNC is easy. */
  753. func = get_frame_func (this_frame);
  754. /* Hopefully the prologue analysis either correctly determined the
  755. frame's base (which is the SP from the previous frame), or set
  756. that base to "NULL". */
  757. base = info->prev_sp;
  758. if (base == 0)
  759. return;
  760. id = frame_id_build (base, func);
  761. (*this_id) = id;
  762. }
  763. static struct value *
  764. m68hc11_frame_prev_register (struct frame_info *this_frame,
  765. void **this_prologue_cache, int regnum)
  766. {
  767. struct value *value;
  768. struct m68hc11_unwind_cache *info
  769. = m68hc11_frame_unwind_cache (this_frame, this_prologue_cache);
  770. value = trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  771. /* Take into account the 68HC12 specific call (PC + page). */
  772. if (regnum == HARD_PC_REGNUM
  773. && info->return_kind == RETURN_RTC
  774. && use_page_register (get_frame_arch (this_frame)))
  775. {
  776. CORE_ADDR pc = value_as_long (value);
  777. if (pc >= 0x08000 && pc < 0x0c000)
  778. {
  779. CORE_ADDR page;
  780. release_value (value);
  781. value = trad_frame_get_prev_register (this_frame, info->saved_regs,
  782. HARD_PAGE_REGNUM);
  783. page = value_as_long (value);
  784. release_value (value);
  785. pc -= 0x08000;
  786. pc += ((page & 0x0ff) << 14);
  787. pc += 0x1000000;
  788. return frame_unwind_got_constant (this_frame, regnum, pc);
  789. }
  790. }
  791. return value;
  792. }
  793. static const struct frame_unwind m68hc11_frame_unwind = {
  794. "m68hc11 prologue",
  795. NORMAL_FRAME,
  796. default_frame_unwind_stop_reason,
  797. m68hc11_frame_this_id,
  798. m68hc11_frame_prev_register,
  799. NULL,
  800. default_frame_sniffer
  801. };
  802. static CORE_ADDR
  803. m68hc11_frame_base_address (struct frame_info *this_frame, void **this_cache)
  804. {
  805. struct m68hc11_unwind_cache *info
  806. = m68hc11_frame_unwind_cache (this_frame, this_cache);
  807. return info->base;
  808. }
  809. static CORE_ADDR
  810. m68hc11_frame_args_address (struct frame_info *this_frame, void **this_cache)
  811. {
  812. CORE_ADDR addr;
  813. struct m68hc11_unwind_cache *info
  814. = m68hc11_frame_unwind_cache (this_frame, this_cache);
  815. addr = info->base + info->size;
  816. if (info->return_kind == RETURN_RTC)
  817. addr += 1;
  818. else if (info->return_kind == RETURN_RTI)
  819. addr += 7;
  820. return addr;
  821. }
  822. static const struct frame_base m68hc11_frame_base = {
  823. &m68hc11_frame_unwind,
  824. m68hc11_frame_base_address,
  825. m68hc11_frame_base_address,
  826. m68hc11_frame_args_address
  827. };
  828. /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
  829. frame. The frame ID's base needs to match the TOS value saved by
  830. save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint. */
  831. static struct frame_id
  832. m68hc11_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  833. {
  834. ULONGEST tos;
  835. CORE_ADDR pc = get_frame_pc (this_frame);
  836. tos = get_frame_register_unsigned (this_frame, SOFT_FP_REGNUM);
  837. tos += 2;
  838. return frame_id_build (tos, pc);
  839. }
  840. /* Get and print the register from the given frame. */
  841. static void
  842. m68hc11_print_register (struct gdbarch *gdbarch, struct ui_file *file,
  843. struct frame_info *frame, int regno)
  844. {
  845. LONGEST rval;
  846. if (regno == HARD_PC_REGNUM || regno == HARD_SP_REGNUM
  847. || regno == SOFT_FP_REGNUM || regno == M68HC12_HARD_PC_REGNUM)
  848. rval = get_frame_register_unsigned (frame, regno);
  849. else
  850. rval = get_frame_register_signed (frame, regno);
  851. if (regno == HARD_A_REGNUM || regno == HARD_B_REGNUM
  852. || regno == HARD_CCR_REGNUM || regno == HARD_PAGE_REGNUM)
  853. {
  854. gdb_printf (file, "0x%02x ", (unsigned char) rval);
  855. if (regno != HARD_CCR_REGNUM)
  856. print_longest (file, 'd', 1, rval);
  857. }
  858. else
  859. {
  860. m68gc11_gdbarch_tdep *tdep
  861. = (m68gc11_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  862. if (regno == HARD_PC_REGNUM && tdep->use_page_register)
  863. {
  864. ULONGEST page;
  865. page = get_frame_register_unsigned (frame, HARD_PAGE_REGNUM);
  866. gdb_printf (file, "0x%02x:%04x ", (unsigned) page,
  867. (unsigned) rval);
  868. }
  869. else
  870. {
  871. gdb_printf (file, "0x%04x ", (unsigned) rval);
  872. if (regno != HARD_PC_REGNUM && regno != HARD_SP_REGNUM
  873. && regno != SOFT_FP_REGNUM && regno != M68HC12_HARD_PC_REGNUM)
  874. print_longest (file, 'd', 1, rval);
  875. }
  876. }
  877. if (regno == HARD_CCR_REGNUM)
  878. {
  879. /* CCR register */
  880. int C, Z, N, V;
  881. unsigned char l = rval & 0xff;
  882. gdb_printf (file, "%c%c%c%c%c%c%c%c ",
  883. l & M6811_S_BIT ? 'S' : '-',
  884. l & M6811_X_BIT ? 'X' : '-',
  885. l & M6811_H_BIT ? 'H' : '-',
  886. l & M6811_I_BIT ? 'I' : '-',
  887. l & M6811_N_BIT ? 'N' : '-',
  888. l & M6811_Z_BIT ? 'Z' : '-',
  889. l & M6811_V_BIT ? 'V' : '-',
  890. l & M6811_C_BIT ? 'C' : '-');
  891. N = (l & M6811_N_BIT) != 0;
  892. Z = (l & M6811_Z_BIT) != 0;
  893. V = (l & M6811_V_BIT) != 0;
  894. C = (l & M6811_C_BIT) != 0;
  895. /* Print flags following the h8300. */
  896. if ((C | Z) == 0)
  897. gdb_printf (file, "u> ");
  898. else if ((C | Z) == 1)
  899. gdb_printf (file, "u<= ");
  900. else if (C == 0)
  901. gdb_printf (file, "u< ");
  902. if (Z == 0)
  903. gdb_printf (file, "!= ");
  904. else
  905. gdb_printf (file, "== ");
  906. if ((N ^ V) == 0)
  907. gdb_printf (file, ">= ");
  908. else
  909. gdb_printf (file, "< ");
  910. if ((Z | (N ^ V)) == 0)
  911. gdb_printf (file, "> ");
  912. else
  913. gdb_printf (file, "<= ");
  914. }
  915. }
  916. /* Same as 'info reg' but prints the registers in a different way. */
  917. static void
  918. m68hc11_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  919. struct frame_info *frame, int regno, int cpregs)
  920. {
  921. if (regno >= 0)
  922. {
  923. const char *name = gdbarch_register_name (gdbarch, regno);
  924. if (!name || !*name)
  925. return;
  926. gdb_printf (file, "%-10s ", name);
  927. m68hc11_print_register (gdbarch, file, frame, regno);
  928. gdb_printf (file, "\n");
  929. }
  930. else
  931. {
  932. int i, nr;
  933. gdb_printf (file, "PC=");
  934. m68hc11_print_register (gdbarch, file, frame, HARD_PC_REGNUM);
  935. gdb_printf (file, " SP=");
  936. m68hc11_print_register (gdbarch, file, frame, HARD_SP_REGNUM);
  937. gdb_printf (file, " FP=");
  938. m68hc11_print_register (gdbarch, file, frame, SOFT_FP_REGNUM);
  939. gdb_printf (file, "\nCCR=");
  940. m68hc11_print_register (gdbarch, file, frame, HARD_CCR_REGNUM);
  941. gdb_printf (file, "\nD=");
  942. m68hc11_print_register (gdbarch, file, frame, HARD_D_REGNUM);
  943. gdb_printf (file, " X=");
  944. m68hc11_print_register (gdbarch, file, frame, HARD_X_REGNUM);
  945. gdb_printf (file, " Y=");
  946. m68hc11_print_register (gdbarch, file, frame, HARD_Y_REGNUM);
  947. m68gc11_gdbarch_tdep *tdep = (m68gc11_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  948. if (tdep->use_page_register)
  949. {
  950. gdb_printf (file, "\nPage=");
  951. m68hc11_print_register (gdbarch, file, frame, HARD_PAGE_REGNUM);
  952. }
  953. gdb_printf (file, "\n");
  954. nr = 0;
  955. for (i = SOFT_D1_REGNUM; i < M68HC11_ALL_REGS; i++)
  956. {
  957. /* Skip registers which are not defined in the symbol table. */
  958. if (soft_regs[i].name == 0)
  959. continue;
  960. gdb_printf (file, "D%d=", i - SOFT_D1_REGNUM + 1);
  961. m68hc11_print_register (gdbarch, file, frame, i);
  962. nr++;
  963. if ((nr % 8) == 7)
  964. gdb_printf (file, "\n");
  965. else
  966. gdb_printf (file, " ");
  967. }
  968. if (nr && (nr % 8) != 7)
  969. gdb_printf (file, "\n");
  970. }
  971. }
  972. static CORE_ADDR
  973. m68hc11_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  974. struct regcache *regcache, CORE_ADDR bp_addr,
  975. int nargs, struct value **args, CORE_ADDR sp,
  976. function_call_return_method return_method,
  977. CORE_ADDR struct_addr)
  978. {
  979. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  980. int argnum;
  981. int first_stack_argnum;
  982. struct type *type;
  983. const gdb_byte *val;
  984. gdb_byte buf[2];
  985. first_stack_argnum = 0;
  986. if (return_method == return_method_struct)
  987. regcache_cooked_write_unsigned (regcache, HARD_D_REGNUM, struct_addr);
  988. else if (nargs > 0)
  989. {
  990. type = value_type (args[0]);
  991. /* First argument is passed in D and X registers. */
  992. if (TYPE_LENGTH (type) <= 4)
  993. {
  994. ULONGEST v;
  995. v = extract_unsigned_integer (value_contents (args[0]).data (),
  996. TYPE_LENGTH (type), byte_order);
  997. first_stack_argnum = 1;
  998. regcache_cooked_write_unsigned (regcache, HARD_D_REGNUM, v);
  999. if (TYPE_LENGTH (type) > 2)
  1000. {
  1001. v >>= 16;
  1002. regcache_cooked_write_unsigned (regcache, HARD_X_REGNUM, v);
  1003. }
  1004. }
  1005. }
  1006. for (argnum = nargs - 1; argnum >= first_stack_argnum; argnum--)
  1007. {
  1008. type = value_type (args[argnum]);
  1009. if (TYPE_LENGTH (type) & 1)
  1010. {
  1011. static gdb_byte zero = 0;
  1012. sp--;
  1013. write_memory (sp, &zero, 1);
  1014. }
  1015. val = value_contents (args[argnum]).data ();
  1016. sp -= TYPE_LENGTH (type);
  1017. write_memory (sp, val, TYPE_LENGTH (type));
  1018. }
  1019. /* Store return address. */
  1020. sp -= 2;
  1021. store_unsigned_integer (buf, 2, byte_order, bp_addr);
  1022. write_memory (sp, buf, 2);
  1023. /* Finally, update the stack pointer... */
  1024. sp -= stack_correction (gdbarch);
  1025. regcache_cooked_write_unsigned (regcache, HARD_SP_REGNUM, sp);
  1026. /* ...and fake a frame pointer. */
  1027. regcache_cooked_write_unsigned (regcache, SOFT_FP_REGNUM, sp);
  1028. /* DWARF2/GCC uses the stack address *before* the function call as a
  1029. frame's CFA. */
  1030. return sp + 2;
  1031. }
  1032. /* Return the GDB type object for the "standard" data type
  1033. of data in register N. */
  1034. static struct type *
  1035. m68hc11_register_type (struct gdbarch *gdbarch, int reg_nr)
  1036. {
  1037. switch (reg_nr)
  1038. {
  1039. case HARD_PAGE_REGNUM:
  1040. case HARD_A_REGNUM:
  1041. case HARD_B_REGNUM:
  1042. case HARD_CCR_REGNUM:
  1043. return builtin_type (gdbarch)->builtin_uint8;
  1044. case M68HC12_HARD_PC_REGNUM:
  1045. return builtin_type (gdbarch)->builtin_uint32;
  1046. default:
  1047. return builtin_type (gdbarch)->builtin_uint16;
  1048. }
  1049. }
  1050. static void
  1051. m68hc11_store_return_value (struct type *type, struct regcache *regcache,
  1052. const gdb_byte *valbuf)
  1053. {
  1054. int len;
  1055. len = TYPE_LENGTH (type);
  1056. /* First argument is passed in D and X registers. */
  1057. if (len <= 2)
  1058. regcache->raw_write_part (HARD_D_REGNUM, 2 - len, len, valbuf);
  1059. else if (len <= 4)
  1060. {
  1061. regcache->raw_write_part (HARD_X_REGNUM, 4 - len, len - 2, valbuf);
  1062. regcache->raw_write (HARD_D_REGNUM, valbuf + (len - 2));
  1063. }
  1064. else
  1065. error (_("return of value > 4 is not supported."));
  1066. }
  1067. /* Given a return value in `regcache' with a type `type',
  1068. extract and copy its value into `valbuf'. */
  1069. static void
  1070. m68hc11_extract_return_value (struct type *type, struct regcache *regcache,
  1071. void *valbuf)
  1072. {
  1073. gdb_byte buf[M68HC11_REG_SIZE];
  1074. regcache->raw_read (HARD_D_REGNUM, buf);
  1075. switch (TYPE_LENGTH (type))
  1076. {
  1077. case 1:
  1078. memcpy (valbuf, buf + 1, 1);
  1079. break;
  1080. case 2:
  1081. memcpy (valbuf, buf, 2);
  1082. break;
  1083. case 3:
  1084. memcpy ((char*) valbuf + 1, buf, 2);
  1085. regcache->raw_read (HARD_X_REGNUM, buf);
  1086. memcpy (valbuf, buf + 1, 1);
  1087. break;
  1088. case 4:
  1089. memcpy ((char*) valbuf + 2, buf, 2);
  1090. regcache->raw_read (HARD_X_REGNUM, buf);
  1091. memcpy (valbuf, buf, 2);
  1092. break;
  1093. default:
  1094. error (_("bad size for return value"));
  1095. }
  1096. }
  1097. static enum return_value_convention
  1098. m68hc11_return_value (struct gdbarch *gdbarch, struct value *function,
  1099. struct type *valtype, struct regcache *regcache,
  1100. gdb_byte *readbuf, const gdb_byte *writebuf)
  1101. {
  1102. if (valtype->code () == TYPE_CODE_STRUCT
  1103. || valtype->code () == TYPE_CODE_UNION
  1104. || valtype->code () == TYPE_CODE_ARRAY
  1105. || TYPE_LENGTH (valtype) > 4)
  1106. return RETURN_VALUE_STRUCT_CONVENTION;
  1107. else
  1108. {
  1109. if (readbuf != NULL)
  1110. m68hc11_extract_return_value (valtype, regcache, readbuf);
  1111. if (writebuf != NULL)
  1112. m68hc11_store_return_value (valtype, regcache, writebuf);
  1113. return RETURN_VALUE_REGISTER_CONVENTION;
  1114. }
  1115. }
  1116. /* Test whether the ELF symbol corresponds to a function using rtc or
  1117. rti to return. */
  1118. static void
  1119. m68hc11_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
  1120. {
  1121. unsigned char flags;
  1122. flags = ((elf_symbol_type *)sym)->internal_elf_sym.st_other;
  1123. if (flags & STO_M68HC12_FAR)
  1124. MSYMBOL_SET_RTC (msym);
  1125. if (flags & STO_M68HC12_INTERRUPT)
  1126. MSYMBOL_SET_RTI (msym);
  1127. }
  1128. /* 68HC11/68HC12 register groups.
  1129. Identify real hard registers and soft registers used by gcc. */
  1130. static const reggroup *m68hc11_soft_reggroup;
  1131. static const reggroup *m68hc11_hard_reggroup;
  1132. static void
  1133. m68hc11_init_reggroups (void)
  1134. {
  1135. m68hc11_hard_reggroup = reggroup_new ("hard", USER_REGGROUP);
  1136. m68hc11_soft_reggroup = reggroup_new ("soft", USER_REGGROUP);
  1137. }
  1138. static void
  1139. m68hc11_add_reggroups (struct gdbarch *gdbarch)
  1140. {
  1141. reggroup_add (gdbarch, m68hc11_hard_reggroup);
  1142. reggroup_add (gdbarch, m68hc11_soft_reggroup);
  1143. }
  1144. static int
  1145. m68hc11_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  1146. const struct reggroup *group)
  1147. {
  1148. /* We must save the real hard register as well as gcc
  1149. soft registers including the frame pointer. */
  1150. if (group == save_reggroup || group == restore_reggroup)
  1151. {
  1152. return (regnum <= gdbarch_num_regs (gdbarch)
  1153. || ((regnum == SOFT_FP_REGNUM
  1154. || regnum == SOFT_TMP_REGNUM
  1155. || regnum == SOFT_ZS_REGNUM
  1156. || regnum == SOFT_XY_REGNUM)
  1157. && m68hc11_register_name (gdbarch, regnum)));
  1158. }
  1159. /* Group to identify gcc soft registers (d1..dN). */
  1160. if (group == m68hc11_soft_reggroup)
  1161. {
  1162. return regnum >= SOFT_D1_REGNUM
  1163. && m68hc11_register_name (gdbarch, regnum);
  1164. }
  1165. if (group == m68hc11_hard_reggroup)
  1166. {
  1167. return regnum == HARD_PC_REGNUM || regnum == HARD_SP_REGNUM
  1168. || regnum == HARD_X_REGNUM || regnum == HARD_D_REGNUM
  1169. || regnum == HARD_Y_REGNUM || regnum == HARD_CCR_REGNUM;
  1170. }
  1171. return default_register_reggroup_p (gdbarch, regnum, group);
  1172. }
  1173. static struct gdbarch *
  1174. m68hc11_gdbarch_init (struct gdbarch_info info,
  1175. struct gdbarch_list *arches)
  1176. {
  1177. struct gdbarch *gdbarch;
  1178. int elf_flags;
  1179. soft_reg_initialized = 0;
  1180. /* Extract the elf_flags if available. */
  1181. if (info.abfd != NULL
  1182. && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1183. elf_flags = elf_elfheader (info.abfd)->e_flags;
  1184. else
  1185. elf_flags = 0;
  1186. /* Try to find a pre-existing architecture. */
  1187. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1188. arches != NULL;
  1189. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1190. {
  1191. m68gc11_gdbarch_tdep *tdep
  1192. = (m68gc11_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  1193. if (tdep->elf_flags != elf_flags)
  1194. continue;
  1195. return arches->gdbarch;
  1196. }
  1197. /* Need a new architecture. Fill in a target specific vector. */
  1198. m68gc11_gdbarch_tdep *tdep = new m68gc11_gdbarch_tdep;
  1199. gdbarch = gdbarch_alloc (&info, tdep);
  1200. tdep->elf_flags = elf_flags;
  1201. switch (info.bfd_arch_info->arch)
  1202. {
  1203. case bfd_arch_m68hc11:
  1204. tdep->stack_correction = 1;
  1205. tdep->use_page_register = 0;
  1206. tdep->prologue = m6811_prologue;
  1207. set_gdbarch_addr_bit (gdbarch, 16);
  1208. set_gdbarch_num_pseudo_regs (gdbarch, M68HC11_NUM_PSEUDO_REGS);
  1209. set_gdbarch_pc_regnum (gdbarch, HARD_PC_REGNUM);
  1210. set_gdbarch_num_regs (gdbarch, M68HC11_NUM_REGS);
  1211. break;
  1212. case bfd_arch_m68hc12:
  1213. tdep->stack_correction = 0;
  1214. tdep->use_page_register = elf_flags & E_M68HC12_BANKS;
  1215. tdep->prologue = m6812_prologue;
  1216. set_gdbarch_addr_bit (gdbarch, elf_flags & E_M68HC12_BANKS ? 32 : 16);
  1217. set_gdbarch_num_pseudo_regs (gdbarch,
  1218. elf_flags & E_M68HC12_BANKS
  1219. ? M68HC12_NUM_PSEUDO_REGS
  1220. : M68HC11_NUM_PSEUDO_REGS);
  1221. set_gdbarch_pc_regnum (gdbarch, elf_flags & E_M68HC12_BANKS
  1222. ? M68HC12_HARD_PC_REGNUM : HARD_PC_REGNUM);
  1223. set_gdbarch_num_regs (gdbarch, elf_flags & E_M68HC12_BANKS
  1224. ? M68HC12_NUM_REGS : M68HC11_NUM_REGS);
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. /* Initially set everything according to the ABI.
  1230. Use 16-bit integers since it will be the case for most
  1231. programs. The size of these types should normally be set
  1232. according to the dwarf2 debug information. */
  1233. set_gdbarch_short_bit (gdbarch, 16);
  1234. set_gdbarch_int_bit (gdbarch, elf_flags & E_M68HC11_I32 ? 32 : 16);
  1235. set_gdbarch_float_bit (gdbarch, 32);
  1236. if (elf_flags & E_M68HC11_F64)
  1237. {
  1238. set_gdbarch_double_bit (gdbarch, 64);
  1239. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  1240. }
  1241. else
  1242. {
  1243. set_gdbarch_double_bit (gdbarch, 32);
  1244. set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1245. }
  1246. set_gdbarch_long_double_bit (gdbarch, 64);
  1247. set_gdbarch_long_bit (gdbarch, 32);
  1248. set_gdbarch_ptr_bit (gdbarch, 16);
  1249. set_gdbarch_long_long_bit (gdbarch, 64);
  1250. /* Characters are unsigned. */
  1251. set_gdbarch_char_signed (gdbarch, 0);
  1252. /* Set register info. */
  1253. set_gdbarch_fp0_regnum (gdbarch, -1);
  1254. set_gdbarch_sp_regnum (gdbarch, HARD_SP_REGNUM);
  1255. set_gdbarch_register_name (gdbarch, m68hc11_register_name);
  1256. set_gdbarch_register_type (gdbarch, m68hc11_register_type);
  1257. set_gdbarch_pseudo_register_read (gdbarch, m68hc11_pseudo_register_read);
  1258. set_gdbarch_pseudo_register_write (gdbarch, m68hc11_pseudo_register_write);
  1259. set_gdbarch_push_dummy_call (gdbarch, m68hc11_push_dummy_call);
  1260. set_gdbarch_return_value (gdbarch, m68hc11_return_value);
  1261. set_gdbarch_skip_prologue (gdbarch, m68hc11_skip_prologue);
  1262. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1263. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  1264. m68hc11_breakpoint::kind_from_pc);
  1265. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  1266. m68hc11_breakpoint::bp_from_kind);
  1267. m68hc11_add_reggroups (gdbarch);
  1268. set_gdbarch_register_reggroup_p (gdbarch, m68hc11_register_reggroup_p);
  1269. set_gdbarch_print_registers_info (gdbarch, m68hc11_print_registers_info);
  1270. /* Hook in the DWARF CFI frame unwinder. */
  1271. dwarf2_append_unwinders (gdbarch);
  1272. frame_unwind_append_unwinder (gdbarch, &m68hc11_frame_unwind);
  1273. frame_base_set_default (gdbarch, &m68hc11_frame_base);
  1274. /* Methods for saving / extracting a dummy frame's ID. The ID's
  1275. stack address must match the SP value returned by
  1276. PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
  1277. set_gdbarch_dummy_id (gdbarch, m68hc11_dummy_id);
  1278. /* Minsymbol frobbing. */
  1279. set_gdbarch_elf_make_msymbol_special (gdbarch,
  1280. m68hc11_elf_make_msymbol_special);
  1281. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  1282. return gdbarch;
  1283. }
  1284. void _initialize_m68hc11_tdep ();
  1285. void
  1286. _initialize_m68hc11_tdep ()
  1287. {
  1288. register_gdbarch_init (bfd_arch_m68hc11, m68hc11_gdbarch_init);
  1289. register_gdbarch_init (bfd_arch_m68hc12, m68hc11_gdbarch_init);
  1290. m68hc11_init_reggroups ();
  1291. }