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