xstormy16-tdep.c revision 1.5 1 1.1 christos /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.
2 1.1 christos
3 1.3 christos Copyright (C) 2001-2015 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "frame.h"
22 1.1 christos #include "frame-base.h"
23 1.1 christos #include "frame-unwind.h"
24 1.1 christos #include "dwarf2-frame.h"
25 1.1 christos #include "symtab.h"
26 1.1 christos #include "gdbtypes.h"
27 1.1 christos #include "gdbcmd.h"
28 1.1 christos #include "gdbcore.h"
29 1.1 christos #include "value.h"
30 1.1 christos #include "dis-asm.h"
31 1.1 christos #include "inferior.h"
32 1.1 christos #include "arch-utils.h"
33 1.1 christos #include "floatformat.h"
34 1.1 christos #include "regcache.h"
35 1.1 christos #include "doublest.h"
36 1.1 christos #include "osabi.h"
37 1.1 christos #include "objfiles.h"
38 1.1 christos
39 1.1 christos enum gdb_regnum
40 1.1 christos {
41 1.1 christos /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
42 1.1 christos Functions will return their values in register R2-R7 as they fit.
43 1.1 christos Otherwise a hidden pointer to an big enough area is given as argument
44 1.1 christos to the function in r2. Further arguments are beginning in r3 then.
45 1.1 christos R13 is used as frame pointer when GCC compiles w/o optimization
46 1.1 christos R14 is used as "PSW", displaying the CPU status.
47 1.1 christos R15 is used implicitely as stack pointer. */
48 1.1 christos E_R0_REGNUM,
49 1.1 christos E_R1_REGNUM,
50 1.1 christos E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
51 1.1 christos E_R3_REGNUM,
52 1.1 christos E_R4_REGNUM,
53 1.1 christos E_R5_REGNUM,
54 1.1 christos E_R6_REGNUM,
55 1.1 christos E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
56 1.1 christos E_R8_REGNUM,
57 1.1 christos E_R9_REGNUM,
58 1.1 christos E_R10_REGNUM,
59 1.1 christos E_R11_REGNUM,
60 1.1 christos E_R12_REGNUM,
61 1.1 christos E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
62 1.1 christos E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
63 1.1 christos E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
64 1.1 christos E_PC_REGNUM,
65 1.1 christos E_NUM_REGS
66 1.1 christos };
67 1.1 christos
68 1.1 christos /* Use an invalid address value as 'not available' marker. */
69 1.1 christos enum { REG_UNAVAIL = (CORE_ADDR) -1 };
70 1.1 christos
71 1.1 christos struct xstormy16_frame_cache
72 1.1 christos {
73 1.1 christos /* Base address. */
74 1.1 christos CORE_ADDR base;
75 1.1 christos CORE_ADDR pc;
76 1.1 christos LONGEST framesize;
77 1.1 christos int uses_fp;
78 1.1 christos CORE_ADDR saved_regs[E_NUM_REGS];
79 1.1 christos CORE_ADDR saved_sp;
80 1.1 christos };
81 1.1 christos
82 1.1 christos /* Size of instructions, registers, etc. */
83 1.1 christos enum
84 1.1 christos {
85 1.1 christos xstormy16_inst_size = 2,
86 1.1 christos xstormy16_reg_size = 2,
87 1.1 christos xstormy16_pc_size = 4
88 1.1 christos };
89 1.1 christos
90 1.1 christos /* Size of return datatype which fits into the remaining return registers. */
91 1.1 christos #define E_MAX_RETTYPE_SIZE(regnum) ((E_LST_ARG_REGNUM - (regnum) + 1) \
92 1.1 christos * xstormy16_reg_size)
93 1.1 christos
94 1.1 christos /* Size of return datatype which fits into all return registers. */
95 1.1 christos enum
96 1.1 christos {
97 1.1 christos E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
98 1.1 christos };
99 1.1 christos
100 1.1 christos /* Function: xstormy16_register_name
101 1.1 christos Returns the name of the standard Xstormy16 register N. */
102 1.1 christos
103 1.1 christos static const char *
104 1.1 christos xstormy16_register_name (struct gdbarch *gdbarch, int regnum)
105 1.1 christos {
106 1.1 christos static char *register_names[] = {
107 1.1 christos "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
108 1.1 christos "r8", "r9", "r10", "r11", "r12", "r13",
109 1.1 christos "psw", "sp", "pc"
110 1.1 christos };
111 1.1 christos
112 1.1 christos if (regnum < 0 || regnum >= E_NUM_REGS)
113 1.1 christos internal_error (__FILE__, __LINE__,
114 1.1 christos _("xstormy16_register_name: illegal register number %d"),
115 1.1 christos regnum);
116 1.1 christos else
117 1.1 christos return register_names[regnum];
118 1.1 christos
119 1.1 christos }
120 1.1 christos
121 1.1 christos static struct type *
122 1.1 christos xstormy16_register_type (struct gdbarch *gdbarch, int regnum)
123 1.1 christos {
124 1.1 christos if (regnum == E_PC_REGNUM)
125 1.1 christos return builtin_type (gdbarch)->builtin_uint32;
126 1.1 christos else
127 1.1 christos return builtin_type (gdbarch)->builtin_uint16;
128 1.1 christos }
129 1.1 christos
130 1.1 christos /* Function: xstormy16_type_is_scalar
131 1.1 christos Makes the decision if a given type is a scalar types. Scalar
132 1.1 christos types are returned in the registers r2-r7 as they fit. */
133 1.1 christos
134 1.1 christos static int
135 1.1 christos xstormy16_type_is_scalar (struct type *t)
136 1.1 christos {
137 1.1 christos return (TYPE_CODE(t) != TYPE_CODE_STRUCT
138 1.1 christos && TYPE_CODE(t) != TYPE_CODE_UNION
139 1.1 christos && TYPE_CODE(t) != TYPE_CODE_ARRAY);
140 1.1 christos }
141 1.1 christos
142 1.1 christos /* Function: xstormy16_use_struct_convention
143 1.1 christos Returns non-zero if the given struct type will be returned using
144 1.1 christos a special convention, rather than the normal function return method.
145 1.1 christos 7sed in the contexts of the "return" command, and of
146 1.1 christos target function calls from the debugger. */
147 1.1 christos
148 1.1 christos static int
149 1.1 christos xstormy16_use_struct_convention (struct type *type)
150 1.1 christos {
151 1.1 christos return !xstormy16_type_is_scalar (type)
152 1.1 christos || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
153 1.1 christos }
154 1.1 christos
155 1.1 christos /* Function: xstormy16_extract_return_value
156 1.1 christos Find a function's return value in the appropriate registers (in
157 1.1 christos regbuf), and copy it into valbuf. */
158 1.1 christos
159 1.1 christos static void
160 1.1 christos xstormy16_extract_return_value (struct type *type, struct regcache *regcache,
161 1.1 christos gdb_byte *valbuf)
162 1.1 christos {
163 1.1 christos int len = TYPE_LENGTH (type);
164 1.1 christos int i, regnum = E_1ST_ARG_REGNUM;
165 1.1 christos
166 1.1 christos for (i = 0; i < len; i += xstormy16_reg_size)
167 1.1 christos regcache_raw_read (regcache, regnum++, valbuf + i);
168 1.1 christos }
169 1.1 christos
170 1.1 christos /* Function: xstormy16_store_return_value
171 1.1 christos Copy the function return value from VALBUF into the
172 1.1 christos proper location for a function return.
173 1.1 christos Called only in the context of the "return" command. */
174 1.1 christos
175 1.1 christos static void
176 1.1 christos xstormy16_store_return_value (struct type *type, struct regcache *regcache,
177 1.1 christos const gdb_byte *valbuf)
178 1.1 christos {
179 1.1 christos if (TYPE_LENGTH (type) == 1)
180 1.1 christos {
181 1.1 christos /* Add leading zeros to the value. */
182 1.1 christos gdb_byte buf[xstormy16_reg_size];
183 1.1 christos memset (buf, 0, xstormy16_reg_size);
184 1.1 christos memcpy (buf, valbuf, 1);
185 1.1 christos regcache_raw_write (regcache, E_1ST_ARG_REGNUM, buf);
186 1.1 christos }
187 1.1 christos else
188 1.1 christos {
189 1.1 christos int len = TYPE_LENGTH (type);
190 1.1 christos int i, regnum = E_1ST_ARG_REGNUM;
191 1.1 christos
192 1.1 christos for (i = 0; i < len; i += xstormy16_reg_size)
193 1.1 christos regcache_raw_write (regcache, regnum++, valbuf + i);
194 1.1 christos }
195 1.1 christos }
196 1.1 christos
197 1.1 christos static enum return_value_convention
198 1.1 christos xstormy16_return_value (struct gdbarch *gdbarch, struct value *function,
199 1.1 christos struct type *type, struct regcache *regcache,
200 1.1 christos gdb_byte *readbuf, const gdb_byte *writebuf)
201 1.1 christos {
202 1.1 christos if (xstormy16_use_struct_convention (type))
203 1.1 christos return RETURN_VALUE_STRUCT_CONVENTION;
204 1.1 christos if (writebuf)
205 1.1 christos xstormy16_store_return_value (type, regcache, writebuf);
206 1.1 christos else if (readbuf)
207 1.1 christos xstormy16_extract_return_value (type, regcache, readbuf);
208 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
209 1.1 christos }
210 1.1 christos
211 1.1 christos static CORE_ADDR
212 1.1 christos xstormy16_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
213 1.1 christos {
214 1.1 christos if (addr & 1)
215 1.1 christos ++addr;
216 1.1 christos return addr;
217 1.1 christos }
218 1.1 christos
219 1.1 christos /* Function: xstormy16_push_dummy_call
220 1.1 christos Setup the function arguments for GDB to call a function in the inferior.
221 1.1 christos Called only in the context of a target function call from the debugger.
222 1.1 christos Returns the value of the SP register after the args are pushed. */
223 1.1 christos
224 1.1 christos static CORE_ADDR
225 1.1 christos xstormy16_push_dummy_call (struct gdbarch *gdbarch,
226 1.1 christos struct value *function,
227 1.1 christos struct regcache *regcache,
228 1.1 christos CORE_ADDR bp_addr, int nargs,
229 1.1 christos struct value **args,
230 1.1 christos CORE_ADDR sp, int struct_return,
231 1.1 christos CORE_ADDR struct_addr)
232 1.1 christos {
233 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
234 1.1 christos CORE_ADDR stack_dest = sp;
235 1.1 christos int argreg = E_1ST_ARG_REGNUM;
236 1.1 christos int i, j;
237 1.1 christos int typelen, slacklen;
238 1.1 christos const gdb_byte *val;
239 1.1 christos gdb_byte buf[xstormy16_pc_size];
240 1.1 christos
241 1.1 christos /* If struct_return is true, then the struct return address will
242 1.1 christos consume one argument-passing register. */
243 1.1 christos if (struct_return)
244 1.1 christos {
245 1.1 christos regcache_cooked_write_unsigned (regcache, E_PTR_RET_REGNUM, struct_addr);
246 1.1 christos argreg++;
247 1.1 christos }
248 1.1 christos
249 1.1 christos /* Arguments are passed in R2-R7 as they fit. If an argument doesn't
250 1.1 christos fit in the remaining registers we're switching over to the stack.
251 1.1 christos No argument is put on stack partially and as soon as we switched
252 1.1 christos over to stack no further argument is put in a register even if it
253 1.1 christos would fit in the remaining unused registers. */
254 1.1 christos for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
255 1.1 christos {
256 1.1 christos typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
257 1.1 christos if (typelen > E_MAX_RETTYPE_SIZE (argreg))
258 1.1 christos break;
259 1.1 christos
260 1.1 christos /* Put argument into registers wordwise. */
261 1.1 christos val = value_contents (args[i]);
262 1.1 christos for (j = 0; j < typelen; j += xstormy16_reg_size)
263 1.1 christos {
264 1.1 christos ULONGEST regval;
265 1.1 christos int size = (typelen - j == 1) ? 1 : xstormy16_reg_size;
266 1.1 christos
267 1.1 christos regval = extract_unsigned_integer (val + j, size, byte_order);
268 1.1 christos regcache_cooked_write_unsigned (regcache, argreg++, regval);
269 1.1 christos }
270 1.1 christos }
271 1.1 christos
272 1.1 christos /* Align SP */
273 1.1 christos stack_dest = xstormy16_frame_align (gdbarch, stack_dest);
274 1.1 christos
275 1.1 christos /* Loop backwards through remaining arguments and push them on the stack,
276 1.1 christos wordaligned. */
277 1.1 christos for (j = nargs - 1; j >= i; j--)
278 1.1 christos {
279 1.1 christos gdb_byte *val;
280 1.1 christos struct cleanup *back_to;
281 1.1 christos const gdb_byte *bytes = value_contents (args[j]);
282 1.1 christos
283 1.1 christos typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
284 1.1 christos slacklen = typelen & 1;
285 1.1 christos val = xmalloc (typelen + slacklen);
286 1.1 christos back_to = make_cleanup (xfree, val);
287 1.1 christos memcpy (val, bytes, typelen);
288 1.1 christos memset (val + typelen, 0, slacklen);
289 1.1 christos
290 1.1 christos /* Now write this data to the stack. The stack grows upwards. */
291 1.1 christos write_memory (stack_dest, val, typelen + slacklen);
292 1.1 christos stack_dest += typelen + slacklen;
293 1.1 christos do_cleanups (back_to);
294 1.1 christos }
295 1.1 christos
296 1.1 christos store_unsigned_integer (buf, xstormy16_pc_size, byte_order, bp_addr);
297 1.1 christos write_memory (stack_dest, buf, xstormy16_pc_size);
298 1.1 christos stack_dest += xstormy16_pc_size;
299 1.1 christos
300 1.1 christos /* Update stack pointer. */
301 1.1 christos regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, stack_dest);
302 1.1 christos
303 1.1 christos /* Return the new stack pointer minus the return address slot since
304 1.1 christos that's what DWARF2/GCC uses as the frame's CFA. */
305 1.1 christos return stack_dest - xstormy16_pc_size;
306 1.1 christos }
307 1.1 christos
308 1.1 christos /* Function: xstormy16_scan_prologue
309 1.1 christos Decode the instructions within the given address range.
310 1.1 christos Decide when we must have reached the end of the function prologue.
311 1.1 christos If a frame_info pointer is provided, fill in its saved_regs etc.
312 1.1 christos
313 1.1 christos Returns the address of the first instruction after the prologue. */
314 1.1 christos
315 1.1 christos static CORE_ADDR
316 1.1 christos xstormy16_analyze_prologue (struct gdbarch *gdbarch,
317 1.1 christos CORE_ADDR start_addr, CORE_ADDR end_addr,
318 1.1 christos struct xstormy16_frame_cache *cache,
319 1.1 christos struct frame_info *this_frame)
320 1.1 christos {
321 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
322 1.1 christos CORE_ADDR next_addr;
323 1.1 christos ULONGEST inst, inst2;
324 1.1 christos LONGEST offset;
325 1.1 christos int regnum;
326 1.1 christos
327 1.1 christos /* Initialize framesize with size of PC put on stack by CALLF inst. */
328 1.1 christos cache->saved_regs[E_PC_REGNUM] = 0;
329 1.1 christos cache->framesize = xstormy16_pc_size;
330 1.1 christos
331 1.1 christos if (start_addr >= end_addr)
332 1.1 christos return end_addr;
333 1.1 christos
334 1.1 christos for (next_addr = start_addr;
335 1.1 christos next_addr < end_addr; next_addr += xstormy16_inst_size)
336 1.1 christos {
337 1.1 christos inst = read_memory_unsigned_integer (next_addr,
338 1.1 christos xstormy16_inst_size, byte_order);
339 1.1 christos inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
340 1.1 christos xstormy16_inst_size, byte_order);
341 1.1 christos
342 1.1 christos if (inst >= 0x0082 && inst <= 0x008d) /* push r2 .. push r13 */
343 1.1 christos {
344 1.1 christos regnum = inst & 0x000f;
345 1.1 christos cache->saved_regs[regnum] = cache->framesize;
346 1.1 christos cache->framesize += xstormy16_reg_size;
347 1.1 christos }
348 1.1 christos
349 1.1 christos /* Optional stack allocation for args and local vars <= 4 byte. */
350 1.1 christos else if (inst == 0x301f || inst == 0x303f) /* inc r15, #0x1/#0x3 */
351 1.1 christos {
352 1.1 christos cache->framesize += ((inst & 0x0030) >> 4) + 1;
353 1.1 christos }
354 1.1 christos
355 1.1 christos /* optional stack allocation for args and local vars > 4 && < 16 byte */
356 1.1 christos else if ((inst & 0xff0f) == 0x510f) /* 51Hf add r15, #0xH */
357 1.1 christos {
358 1.1 christos cache->framesize += (inst & 0x00f0) >> 4;
359 1.1 christos }
360 1.1 christos
361 1.1 christos /* Optional stack allocation for args and local vars >= 16 byte. */
362 1.1 christos else if (inst == 0x314f && inst2 >= 0x0010) /* 314f HHHH add r15, #0xH */
363 1.1 christos {
364 1.1 christos cache->framesize += inst2;
365 1.1 christos next_addr += xstormy16_inst_size;
366 1.1 christos }
367 1.1 christos
368 1.1 christos else if (inst == 0x46fd) /* mov r13, r15 */
369 1.1 christos {
370 1.1 christos cache->uses_fp = 1;
371 1.1 christos }
372 1.1 christos
373 1.1 christos /* optional copying of args in r2-r7 to r10-r13. */
374 1.1 christos /* Probably only in optimized case but legal action for prologue. */
375 1.1 christos else if ((inst & 0xff00) == 0x4600 /* 46SD mov rD, rS */
376 1.1 christos && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
377 1.3 christos && (inst & 0x000f) >= 0x000a && (inst & 0x000f) <= 0x000d)
378 1.1 christos ;
379 1.1 christos
380 1.1 christos /* Optional copying of args in r2-r7 to stack. */
381 1.1 christos /* 72DS HHHH mov.b (rD, 0xHHHH), r(S-8)
382 1.1 christos (bit3 always 1, bit2-0 = reg) */
383 1.1 christos /* 73DS HHHH mov.w (rD, 0xHHHH), r(S-8) */
384 1.1 christos else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
385 1.1 christos {
386 1.1 christos regnum = inst & 0x0007;
387 1.1 christos /* Only 12 of 16 bits of the argument are used for the
388 1.1 christos signed offset. */
389 1.1 christos offset = (LONGEST) (inst2 & 0x0fff);
390 1.1 christos if (offset & 0x0800)
391 1.1 christos offset -= 0x1000;
392 1.1 christos
393 1.1 christos cache->saved_regs[regnum] = cache->framesize + offset;
394 1.1 christos next_addr += xstormy16_inst_size;
395 1.1 christos }
396 1.1 christos
397 1.1 christos else /* Not a prologue instruction. */
398 1.1 christos break;
399 1.1 christos }
400 1.1 christos
401 1.1 christos return next_addr;
402 1.1 christos }
403 1.1 christos
404 1.1 christos /* Function: xstormy16_skip_prologue
405 1.1 christos If the input address is in a function prologue,
406 1.1 christos returns the address of the end of the prologue;
407 1.1 christos else returns the input address.
408 1.1 christos
409 1.1 christos Note: the input address is likely to be the function start,
410 1.1 christos since this function is mainly used for advancing a breakpoint
411 1.1 christos to the first line, or stepping to the first line when we have
412 1.1 christos stepped into a function call. */
413 1.1 christos
414 1.1 christos static CORE_ADDR
415 1.1 christos xstormy16_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
416 1.1 christos {
417 1.1 christos CORE_ADDR func_addr = 0, func_end = 0;
418 1.1 christos const char *func_name;
419 1.1 christos
420 1.1 christos if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
421 1.1 christos {
422 1.1 christos struct symtab_and_line sal;
423 1.1 christos struct symbol *sym;
424 1.1 christos struct xstormy16_frame_cache cache;
425 1.1 christos CORE_ADDR plg_end;
426 1.1 christos
427 1.1 christos memset (&cache, 0, sizeof cache);
428 1.1 christos
429 1.1 christos /* Don't trust line number debug info in frameless functions. */
430 1.1 christos plg_end = xstormy16_analyze_prologue (gdbarch, func_addr, func_end,
431 1.1 christos &cache, NULL);
432 1.1 christos if (!cache.uses_fp)
433 1.1 christos return plg_end;
434 1.1 christos
435 1.1 christos /* Found a function. */
436 1.1 christos sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL);
437 1.1 christos /* Don't use line number debug info for assembly source files. */
438 1.1 christos if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
439 1.1 christos {
440 1.1 christos sal = find_pc_line (func_addr, 0);
441 1.1 christos if (sal.end && sal.end < func_end)
442 1.1 christos {
443 1.1 christos /* Found a line number, use it as end of prologue. */
444 1.1 christos return sal.end;
445 1.1 christos }
446 1.1 christos }
447 1.1 christos /* No useable line symbol. Use result of prologue parsing method. */
448 1.1 christos return plg_end;
449 1.1 christos }
450 1.1 christos
451 1.1 christos /* No function symbol -- just return the PC. */
452 1.1 christos
453 1.1 christos return (CORE_ADDR) pc;
454 1.1 christos }
455 1.1 christos
456 1.5 christos /* Implement the stack_frame_destroyed_p gdbarch method.
457 1.5 christos
458 1.5 christos The epilogue is defined here as the area at the end of a function,
459 1.1 christos either on the `ret' instruction itself or after an instruction which
460 1.1 christos destroys the function's stack frame. */
461 1.5 christos
462 1.1 christos static int
463 1.5 christos xstormy16_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
464 1.1 christos {
465 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
466 1.1 christos CORE_ADDR func_addr = 0, func_end = 0;
467 1.1 christos
468 1.1 christos if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
469 1.1 christos {
470 1.1 christos ULONGEST inst, inst2;
471 1.1 christos CORE_ADDR addr = func_end - xstormy16_inst_size;
472 1.1 christos
473 1.1 christos /* The Xstormy16 epilogue is max. 14 bytes long. */
474 1.1 christos if (pc < func_end - 7 * xstormy16_inst_size)
475 1.1 christos return 0;
476 1.1 christos
477 1.1 christos /* Check if we're on a `ret' instruction. Otherwise it's
478 1.1 christos too dangerous to proceed. */
479 1.1 christos inst = read_memory_unsigned_integer (addr,
480 1.1 christos xstormy16_inst_size, byte_order);
481 1.1 christos if (inst != 0x0003)
482 1.1 christos return 0;
483 1.1 christos
484 1.1 christos while ((addr -= xstormy16_inst_size) >= func_addr)
485 1.1 christos {
486 1.1 christos inst = read_memory_unsigned_integer (addr,
487 1.1 christos xstormy16_inst_size,
488 1.1 christos byte_order);
489 1.1 christos if (inst >= 0x009a && inst <= 0x009d) /* pop r10...r13 */
490 1.1 christos continue;
491 1.1 christos if (inst == 0x305f || inst == 0x307f) /* dec r15, #0x1/#0x3 */
492 1.1 christos break;
493 1.1 christos inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
494 1.1 christos xstormy16_inst_size,
495 1.1 christos byte_order);
496 1.1 christos if (inst2 == 0x314f && inst >= 0x8000) /* add r15, neg. value */
497 1.1 christos {
498 1.1 christos addr -= xstormy16_inst_size;
499 1.1 christos break;
500 1.1 christos }
501 1.1 christos return 0;
502 1.1 christos }
503 1.1 christos if (pc > addr)
504 1.1 christos return 1;
505 1.1 christos }
506 1.1 christos return 0;
507 1.1 christos }
508 1.1 christos
509 1.1 christos static const unsigned char *
510 1.1 christos xstormy16_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
511 1.1 christos int *lenptr)
512 1.1 christos {
513 1.1 christos static unsigned char breakpoint[] = { 0x06, 0x0 };
514 1.1 christos *lenptr = sizeof (breakpoint);
515 1.1 christos return breakpoint;
516 1.1 christos }
517 1.1 christos
518 1.1 christos /* Given a pointer to a jump table entry, return the address
519 1.1 christos of the function it jumps to. Return 0 if not found. */
520 1.1 christos static CORE_ADDR
521 1.1 christos xstormy16_resolve_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
522 1.1 christos {
523 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
524 1.1 christos struct obj_section *faddr_sect = find_pc_section (faddr);
525 1.1 christos
526 1.1 christos if (faddr_sect)
527 1.1 christos {
528 1.1 christos LONGEST inst, inst2, addr;
529 1.1 christos gdb_byte buf[2 * xstormy16_inst_size];
530 1.1 christos
531 1.1 christos /* Return faddr if it's not pointing into the jump table. */
532 1.1 christos if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
533 1.1 christos return faddr;
534 1.1 christos
535 1.1 christos if (!target_read_memory (faddr, buf, sizeof buf))
536 1.1 christos {
537 1.1 christos inst = extract_unsigned_integer (buf,
538 1.1 christos xstormy16_inst_size, byte_order);
539 1.1 christos inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
540 1.1 christos xstormy16_inst_size, byte_order);
541 1.1 christos addr = inst2 << 8 | (inst & 0xff);
542 1.1 christos return addr;
543 1.1 christos }
544 1.1 christos }
545 1.1 christos return 0;
546 1.1 christos }
547 1.1 christos
548 1.1 christos /* Given a function's address, attempt to find (and return) the
549 1.1 christos address of the corresponding jump table entry. Return 0 if
550 1.1 christos not found. */
551 1.1 christos static CORE_ADDR
552 1.1 christos xstormy16_find_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
553 1.1 christos {
554 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
555 1.1 christos struct obj_section *faddr_sect = find_pc_section (faddr);
556 1.1 christos
557 1.1 christos if (faddr_sect)
558 1.1 christos {
559 1.1 christos struct obj_section *osect;
560 1.1 christos
561 1.1 christos /* Return faddr if it's already a pointer to a jump table entry. */
562 1.1 christos if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
563 1.1 christos return faddr;
564 1.1 christos
565 1.1 christos ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
566 1.1 christos {
567 1.1 christos if (!strcmp (osect->the_bfd_section->name, ".plt"))
568 1.1 christos break;
569 1.1 christos }
570 1.1 christos
571 1.1 christos if (osect < faddr_sect->objfile->sections_end)
572 1.1 christos {
573 1.1 christos CORE_ADDR addr, endaddr;
574 1.1 christos
575 1.1 christos addr = obj_section_addr (osect);
576 1.1 christos endaddr = obj_section_endaddr (osect);
577 1.1 christos
578 1.1 christos for (; addr < endaddr; addr += 2 * xstormy16_inst_size)
579 1.1 christos {
580 1.1 christos LONGEST inst, inst2, faddr2;
581 1.1 christos gdb_byte buf[2 * xstormy16_inst_size];
582 1.1 christos
583 1.1 christos if (target_read_memory (addr, buf, sizeof buf))
584 1.1 christos return 0;
585 1.1 christos inst = extract_unsigned_integer (buf,
586 1.1 christos xstormy16_inst_size,
587 1.1 christos byte_order);
588 1.1 christos inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
589 1.1 christos xstormy16_inst_size,
590 1.1 christos byte_order);
591 1.1 christos faddr2 = inst2 << 8 | (inst & 0xff);
592 1.1 christos if (faddr == faddr2)
593 1.1 christos return addr;
594 1.1 christos }
595 1.1 christos }
596 1.1 christos }
597 1.1 christos return 0;
598 1.1 christos }
599 1.1 christos
600 1.1 christos static CORE_ADDR
601 1.1 christos xstormy16_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
602 1.1 christos {
603 1.1 christos struct gdbarch *gdbarch = get_frame_arch (frame);
604 1.1 christos CORE_ADDR tmp = xstormy16_resolve_jmp_table_entry (gdbarch, pc);
605 1.1 christos
606 1.1 christos if (tmp && tmp != pc)
607 1.1 christos return tmp;
608 1.1 christos return 0;
609 1.1 christos }
610 1.1 christos
611 1.1 christos /* Function pointers are 16 bit. The address space is 24 bit, using
612 1.1 christos 32 bit addresses. Pointers to functions on the XStormy16 are implemented
613 1.1 christos by using 16 bit pointers, which are either direct pointers in case the
614 1.1 christos function begins below 0x10000, or indirect pointers into a jump table.
615 1.1 christos The next two functions convert 16 bit pointers into 24 (32) bit addresses
616 1.1 christos and vice versa. */
617 1.1 christos
618 1.1 christos static CORE_ADDR
619 1.1 christos xstormy16_pointer_to_address (struct gdbarch *gdbarch,
620 1.1 christos struct type *type, const gdb_byte *buf)
621 1.1 christos {
622 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
623 1.1 christos enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
624 1.1 christos CORE_ADDR addr
625 1.1 christos = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
626 1.1 christos
627 1.1 christos if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
628 1.1 christos {
629 1.1 christos CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (gdbarch, addr);
630 1.1 christos if (addr2)
631 1.1 christos addr = addr2;
632 1.1 christos }
633 1.1 christos
634 1.1 christos return addr;
635 1.1 christos }
636 1.1 christos
637 1.1 christos static void
638 1.1 christos xstormy16_address_to_pointer (struct gdbarch *gdbarch,
639 1.1 christos struct type *type, gdb_byte *buf, CORE_ADDR addr)
640 1.1 christos {
641 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
642 1.1 christos enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
643 1.1 christos
644 1.1 christos if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
645 1.1 christos {
646 1.1 christos CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (gdbarch, addr);
647 1.1 christos if (addr2)
648 1.1 christos addr = addr2;
649 1.1 christos }
650 1.1 christos store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
651 1.1 christos }
652 1.1 christos
653 1.1 christos static struct xstormy16_frame_cache *
654 1.1 christos xstormy16_alloc_frame_cache (void)
655 1.1 christos {
656 1.1 christos struct xstormy16_frame_cache *cache;
657 1.1 christos int i;
658 1.1 christos
659 1.1 christos cache = FRAME_OBSTACK_ZALLOC (struct xstormy16_frame_cache);
660 1.1 christos
661 1.1 christos cache->base = 0;
662 1.1 christos cache->saved_sp = 0;
663 1.1 christos cache->pc = 0;
664 1.1 christos cache->uses_fp = 0;
665 1.1 christos cache->framesize = 0;
666 1.1 christos for (i = 0; i < E_NUM_REGS; ++i)
667 1.1 christos cache->saved_regs[i] = REG_UNAVAIL;
668 1.1 christos
669 1.1 christos return cache;
670 1.1 christos }
671 1.1 christos
672 1.1 christos static struct xstormy16_frame_cache *
673 1.1 christos xstormy16_frame_cache (struct frame_info *this_frame, void **this_cache)
674 1.1 christos {
675 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
676 1.1 christos struct xstormy16_frame_cache *cache;
677 1.1 christos CORE_ADDR current_pc;
678 1.1 christos int i;
679 1.1 christos
680 1.1 christos if (*this_cache)
681 1.1 christos return *this_cache;
682 1.1 christos
683 1.1 christos cache = xstormy16_alloc_frame_cache ();
684 1.1 christos *this_cache = cache;
685 1.1 christos
686 1.1 christos cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
687 1.1 christos if (cache->base == 0)
688 1.1 christos return cache;
689 1.1 christos
690 1.1 christos cache->pc = get_frame_func (this_frame);
691 1.1 christos current_pc = get_frame_pc (this_frame);
692 1.1 christos if (cache->pc)
693 1.1 christos xstormy16_analyze_prologue (gdbarch, cache->pc, current_pc,
694 1.1 christos cache, this_frame);
695 1.1 christos
696 1.1 christos if (!cache->uses_fp)
697 1.1 christos cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
698 1.1 christos
699 1.1 christos cache->saved_sp = cache->base - cache->framesize;
700 1.1 christos
701 1.1 christos for (i = 0; i < E_NUM_REGS; ++i)
702 1.1 christos if (cache->saved_regs[i] != REG_UNAVAIL)
703 1.1 christos cache->saved_regs[i] += cache->saved_sp;
704 1.1 christos
705 1.1 christos return cache;
706 1.1 christos }
707 1.1 christos
708 1.1 christos static struct value *
709 1.1 christos xstormy16_frame_prev_register (struct frame_info *this_frame,
710 1.1 christos void **this_cache, int regnum)
711 1.1 christos {
712 1.1 christos struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
713 1.1 christos this_cache);
714 1.1 christos gdb_assert (regnum >= 0);
715 1.1 christos
716 1.1 christos if (regnum == E_SP_REGNUM && cache->saved_sp)
717 1.1 christos return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
718 1.1 christos
719 1.1 christos if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
720 1.1 christos return frame_unwind_got_memory (this_frame, regnum,
721 1.1 christos cache->saved_regs[regnum]);
722 1.1 christos
723 1.1 christos return frame_unwind_got_register (this_frame, regnum, regnum);
724 1.1 christos }
725 1.1 christos
726 1.1 christos static void
727 1.1 christos xstormy16_frame_this_id (struct frame_info *this_frame, void **this_cache,
728 1.1 christos struct frame_id *this_id)
729 1.1 christos {
730 1.1 christos struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
731 1.1 christos this_cache);
732 1.1 christos
733 1.1 christos /* This marks the outermost frame. */
734 1.1 christos if (cache->base == 0)
735 1.1 christos return;
736 1.1 christos
737 1.1 christos *this_id = frame_id_build (cache->saved_sp, cache->pc);
738 1.1 christos }
739 1.1 christos
740 1.1 christos static CORE_ADDR
741 1.1 christos xstormy16_frame_base_address (struct frame_info *this_frame, void **this_cache)
742 1.1 christos {
743 1.1 christos struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
744 1.1 christos this_cache);
745 1.1 christos return cache->base;
746 1.1 christos }
747 1.1 christos
748 1.1 christos static const struct frame_unwind xstormy16_frame_unwind = {
749 1.1 christos NORMAL_FRAME,
750 1.1 christos default_frame_unwind_stop_reason,
751 1.1 christos xstormy16_frame_this_id,
752 1.1 christos xstormy16_frame_prev_register,
753 1.1 christos NULL,
754 1.1 christos default_frame_sniffer
755 1.1 christos };
756 1.1 christos
757 1.1 christos static const struct frame_base xstormy16_frame_base = {
758 1.1 christos &xstormy16_frame_unwind,
759 1.1 christos xstormy16_frame_base_address,
760 1.1 christos xstormy16_frame_base_address,
761 1.1 christos xstormy16_frame_base_address
762 1.1 christos };
763 1.1 christos
764 1.1 christos static CORE_ADDR
765 1.1 christos xstormy16_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
766 1.1 christos {
767 1.1 christos return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
768 1.1 christos }
769 1.1 christos
770 1.1 christos static CORE_ADDR
771 1.1 christos xstormy16_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
772 1.1 christos {
773 1.1 christos return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
774 1.1 christos }
775 1.1 christos
776 1.1 christos static struct frame_id
777 1.1 christos xstormy16_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
778 1.1 christos {
779 1.1 christos CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
780 1.1 christos return frame_id_build (sp, get_frame_pc (this_frame));
781 1.1 christos }
782 1.1 christos
783 1.1 christos
784 1.1 christos /* Function: xstormy16_gdbarch_init
785 1.1 christos Initializer function for the xstormy16 gdbarch vector.
786 1.1 christos Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
787 1.1 christos
788 1.1 christos static struct gdbarch *
789 1.1 christos xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
790 1.1 christos {
791 1.1 christos struct gdbarch *gdbarch;
792 1.1 christos
793 1.1 christos /* find a candidate among the list of pre-declared architectures. */
794 1.1 christos arches = gdbarch_list_lookup_by_info (arches, &info);
795 1.1 christos if (arches != NULL)
796 1.1 christos return (arches->gdbarch);
797 1.1 christos
798 1.1 christos gdbarch = gdbarch_alloc (&info, NULL);
799 1.1 christos
800 1.1 christos /*
801 1.1 christos * Basic register fields and methods, datatype sizes and stuff.
802 1.1 christos */
803 1.1 christos
804 1.1 christos set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
805 1.1 christos set_gdbarch_num_pseudo_regs (gdbarch, 0);
806 1.1 christos set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
807 1.1 christos set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
808 1.1 christos set_gdbarch_register_name (gdbarch, xstormy16_register_name);
809 1.1 christos set_gdbarch_register_type (gdbarch, xstormy16_register_type);
810 1.1 christos
811 1.1 christos set_gdbarch_char_signed (gdbarch, 0);
812 1.1 christos set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
813 1.1 christos set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
814 1.1 christos set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
815 1.1 christos set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
816 1.1 christos
817 1.1 christos set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
818 1.1 christos set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
819 1.1 christos set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
820 1.1 christos
821 1.1 christos set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
822 1.1 christos set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
823 1.1 christos set_gdbarch_dwarf2_addr_size (gdbarch, 4);
824 1.1 christos
825 1.1 christos set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
826 1.1 christos set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);
827 1.1 christos
828 1.1 christos /* Stack grows up. */
829 1.1 christos set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
830 1.1 christos
831 1.1 christos /*
832 1.1 christos * Frame Info
833 1.1 christos */
834 1.1 christos set_gdbarch_unwind_sp (gdbarch, xstormy16_unwind_sp);
835 1.1 christos set_gdbarch_unwind_pc (gdbarch, xstormy16_unwind_pc);
836 1.1 christos set_gdbarch_dummy_id (gdbarch, xstormy16_dummy_id);
837 1.1 christos set_gdbarch_frame_align (gdbarch, xstormy16_frame_align);
838 1.1 christos frame_base_set_default (gdbarch, &xstormy16_frame_base);
839 1.1 christos
840 1.1 christos set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
841 1.5 christos set_gdbarch_stack_frame_destroyed_p (gdbarch,
842 1.5 christos xstormy16_stack_frame_destroyed_p);
843 1.1 christos
844 1.1 christos /* These values and methods are used when gdb calls a target function. */
845 1.1 christos set_gdbarch_push_dummy_call (gdbarch, xstormy16_push_dummy_call);
846 1.1 christos set_gdbarch_breakpoint_from_pc (gdbarch, xstormy16_breakpoint_from_pc);
847 1.1 christos set_gdbarch_return_value (gdbarch, xstormy16_return_value);
848 1.1 christos
849 1.1 christos set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);
850 1.1 christos
851 1.1 christos set_gdbarch_print_insn (gdbarch, print_insn_xstormy16);
852 1.1 christos
853 1.1 christos gdbarch_init_osabi (info, gdbarch);
854 1.1 christos
855 1.1 christos dwarf2_append_unwinders (gdbarch);
856 1.1 christos frame_unwind_append_unwinder (gdbarch, &xstormy16_frame_unwind);
857 1.1 christos
858 1.1 christos return gdbarch;
859 1.1 christos }
860 1.1 christos
861 1.1 christos /* Function: _initialize_xstormy16_tdep
862 1.1 christos Initializer function for the Sanyo Xstormy16a module.
863 1.1 christos Called by gdb at start-up. */
864 1.1 christos
865 1.1 christos /* -Wmissing-prototypes */
866 1.1 christos extern initialize_file_ftype _initialize_xstormy16_tdep;
867 1.1 christos
868 1.1 christos void
869 1.1 christos _initialize_xstormy16_tdep (void)
870 1.1 christos {
871 1.1 christos register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
872 1.1 christos }
873