]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/xstormy16-tdep.c
2004-05-01 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / xstormy16-tdep.c
1 /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.
2
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "inferior.h"
25 #include "arch-utils.h"
26 #include "regcache.h"
27 #include "gdbcore.h"
28 #include "objfiles.h"
29 #include "dis-asm.h"
30
31 struct gdbarch_tdep
32 {
33 /* gdbarch target dependent data here. Currently unused for Xstormy16. */
34 };
35
36 /* Extra info which is saved in each frame_info. */
37 struct frame_extra_info
38 {
39 int framesize;
40 int frameless_p;
41 };
42
43 enum gdb_regnum
44 {
45 /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
46 Functions will return their values in register R2-R7 as they fit.
47 Otherwise a hidden pointer to an big enough area is given as argument
48 to the function in r2. Further arguments are beginning in r3 then.
49 R13 is used as frame pointer when GCC compiles w/o optimization
50 R14 is used as "PSW", displaying the CPU status.
51 R15 is used implicitely as stack pointer. */
52 E_R0_REGNUM,
53 E_R1_REGNUM,
54 E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
55 E_R3_REGNUM,
56 E_R4_REGNUM,
57 E_R5_REGNUM,
58 E_R6_REGNUM,
59 E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
60 E_R8_REGNUM,
61 E_R9_REGNUM,
62 E_R10_REGNUM,
63 E_R11_REGNUM,
64 E_R12_REGNUM,
65 E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
66 E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
67 E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
68 E_PC_REGNUM,
69 E_NUM_REGS
70 };
71
72 /* Size of instructions, registers, etc. */
73 enum
74 {
75 xstormy16_inst_size = 2,
76 xstormy16_reg_size = 2,
77 xstormy16_pc_size = 4
78 };
79
80 /* Size of return datatype which fits into the remaining return registers. */
81 #define E_MAX_RETTYPE_SIZE(regnum) ((E_LST_ARG_REGNUM - (regnum) + 1) \
82 * xstormy16_reg_size)
83
84 /* Size of return datatype which fits into all return registers. */
85 enum
86 {
87 E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
88 };
89
90
91 /* Size of all registers as a whole. */
92 enum
93 {
94 E_ALL_REGS_SIZE = (E_NUM_REGS - 1) * xstormy16_reg_size + xstormy16_pc_size
95 };
96
97 /* Function: xstormy16_register_name
98 Returns the name of the standard Xstormy16 register N. */
99
100 static const char *
101 xstormy16_register_name (int regnum)
102 {
103 static char *register_names[] = {
104 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
105 "r8", "r9", "r10", "r11", "r12", "r13",
106 "psw", "sp", "pc"
107 };
108
109 if (regnum < 0 ||
110 regnum >= sizeof (register_names) / sizeof (register_names[0]))
111 internal_error (__FILE__, __LINE__,
112 "xstormy16_register_name: illegal register number %d",
113 regnum);
114 else
115 return register_names[regnum];
116
117 }
118
119 /* Function: xstormy16_register_byte
120 Returns the byte position in the register cache for register N. */
121
122 static int
123 xstormy16_register_byte (int regnum)
124 {
125 if (regnum < 0 || regnum >= E_NUM_REGS)
126 internal_error (__FILE__, __LINE__,
127 "xstormy16_register_byte: illegal register number %d",
128 regnum);
129 else
130 /* All registers occupy 2 bytes in the regcache except for PC
131 which is the last one. Therefore the byte position is still
132 simply a multiple of 2. */
133 return regnum * xstormy16_reg_size;
134 }
135
136 /* Function: xstormy16_register_raw_size
137 Returns the number of bytes occupied by the register on the target. */
138
139 static int
140 xstormy16_register_raw_size (int regnum)
141 {
142 if (regnum < 0 || regnum >= E_NUM_REGS)
143 internal_error (__FILE__, __LINE__,
144 "xstormy16_register_raw_size: illegal register number %d",
145 regnum);
146 /* Only the PC has 4 Byte, all other registers 2 Byte. */
147 else if (regnum == E_PC_REGNUM)
148 return xstormy16_pc_size;
149 else
150 return xstormy16_reg_size;
151 }
152
153 /* Function: xstormy16_reg_virtual_type
154 Returns the default type for register N. */
155
156 static struct type *
157 xstormy16_reg_virtual_type (int regnum)
158 {
159 if (regnum < 0 || regnum >= E_NUM_REGS)
160 internal_error (__FILE__, __LINE__,
161 "xstormy16_register_virtual_type: illegal register number %d",
162 regnum);
163 else if (regnum == E_PC_REGNUM)
164 return builtin_type_uint32;
165 else
166 return builtin_type_uint16;
167 }
168
169 /* Function: xstormy16_get_saved_register
170 Find a register's saved value on the call stack.
171
172 Find register number REGNUM relative to FRAME and put its (raw,
173 target format) contents in *RAW_BUFFER.
174
175 Set *OPTIMIZED if the variable was optimized out (and thus can't be
176 fetched). Note that this is never set to anything other than zero
177 in this implementation.
178
179 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
180 whether the value was fetched from memory, from a register, or in a
181 strange and non-modifiable way (e.g. a frame pointer which was
182 calculated rather than fetched). We will use not_lval for values
183 fetched from generic dummy frames.
184
185 Set *ADDRP to the address, either in memory or as a
186 DEPRECATED_REGISTER_BYTE offset into the registers array. If the
187 value is stored in a dummy frame, set *ADDRP to zero.
188
189 The argument RAW_BUFFER must point to aligned memory.
190
191 The GET_SAVED_REGISTER architecture interface is entirely
192 redundant. New architectures should implement per-frame unwinders
193 (ref "frame-unwind.h"). */
194
195 static void
196 xstormy16_get_saved_register (char *raw_buffer, int *optimized,
197 CORE_ADDR *addrp,
198 struct frame_info *frame, int regnum,
199 enum lval_type *lval)
200 {
201 if (!target_has_registers)
202 error ("No registers.");
203
204 /* Normal systems don't optimize out things with register numbers. */
205 if (optimized != NULL)
206 *optimized = 0;
207
208 if (addrp) /* default assumption: not found in memory */
209 *addrp = 0;
210
211 /* Note: since the current frame's registers could only have been
212 saved by frames INTERIOR TO the current frame, we skip examining
213 the current frame itself: otherwise, we would be getting the
214 previous frame's registers which were saved by the current frame. */
215
216 if (frame != NULL)
217 {
218 for (frame = get_next_frame (frame);
219 get_frame_type (frame) != SENTINEL_FRAME;
220 frame = get_next_frame (frame))
221 {
222 if (get_frame_type (frame) == DUMMY_FRAME)
223 {
224 if (lval) /* found it in a CALL_DUMMY frame */
225 *lval = not_lval;
226 if (raw_buffer)
227 {
228 LONGEST val;
229 /* FIXME: cagney/2002-06-26: This should be via the
230 gdbarch_register_read() method so that it, on the
231 fly, constructs either a raw or pseudo register
232 from the raw register cache. */
233 val = deprecated_read_register_dummy (get_frame_pc (frame),
234 get_frame_base (frame),
235 regnum);
236 store_unsigned_integer (raw_buffer,
237 DEPRECATED_REGISTER_RAW_SIZE (regnum),
238 val);
239 }
240 return;
241 }
242
243 DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
244 if (deprecated_get_frame_saved_regs (frame) != NULL
245 && deprecated_get_frame_saved_regs (frame)[regnum] != 0)
246 {
247 if (lval) /* found it saved on the stack */
248 *lval = lval_memory;
249 if (regnum == SP_REGNUM)
250 {
251 if (raw_buffer) /* SP register treated specially */
252 /* NOTE: cagney/2003-05-09: In-line store_address()
253 with it's body - store_unsigned_integer(). */
254 store_unsigned_integer (raw_buffer,
255 DEPRECATED_REGISTER_RAW_SIZE (regnum),
256 deprecated_get_frame_saved_regs (frame)[regnum]);
257 }
258 else
259 {
260 if (addrp) /* any other register */
261 *addrp = deprecated_get_frame_saved_regs (frame)[regnum];
262 if (raw_buffer)
263 read_memory (deprecated_get_frame_saved_regs (frame)[regnum], raw_buffer,
264 DEPRECATED_REGISTER_RAW_SIZE (regnum));
265 }
266 return;
267 }
268 }
269 }
270
271 /* If we get thru the loop to this point, it means the register was
272 not saved in any frame. Return the actual live-register value. */
273
274 if (lval) /* found it in a live register */
275 *lval = lval_register;
276 if (addrp)
277 *addrp = DEPRECATED_REGISTER_BYTE (regnum);
278 if (raw_buffer)
279 deprecated_read_register_gen (regnum, raw_buffer);
280 }
281
282 /* Function: xstormy16_type_is_scalar
283 Makes the decision if a given type is a scalar types. Scalar
284 types are returned in the registers r2-r7 as they fit. */
285
286 static int
287 xstormy16_type_is_scalar (struct type *t)
288 {
289 return (TYPE_CODE(t) != TYPE_CODE_STRUCT
290 && TYPE_CODE(t) != TYPE_CODE_UNION
291 && TYPE_CODE(t) != TYPE_CODE_ARRAY);
292 }
293
294 /* Function: xstormy16_extract_return_value
295 Copy the function's return value into VALBUF.
296 This function is called only in the context of "target function calls",
297 ie. when the debugger forces a function to be called in the child, and
298 when the debugger forces a function to return prematurely via the
299 "return" command. */
300
301 static void
302 xstormy16_extract_return_value (struct type *type, char *regbuf, char *valbuf)
303 {
304 CORE_ADDR return_buffer;
305 int offset = 0;
306
307 if (xstormy16_type_is_scalar (type)
308 && TYPE_LENGTH (type) <= E_MAX_RETTYPE_SIZE_IN_REGS)
309 {
310 /* Scalar return values of <= 12 bytes are returned in
311 E_1ST_ARG_REGNUM to E_LST_ARG_REGNUM. */
312 memcpy (valbuf,
313 &regbuf[DEPRECATED_REGISTER_BYTE (E_1ST_ARG_REGNUM)] + offset,
314 TYPE_LENGTH (type));
315 }
316 else
317 {
318 /* Aggregates and return values > 12 bytes are returned in memory,
319 pointed to by R2. */
320 return_buffer =
321 extract_unsigned_integer (regbuf + DEPRECATED_REGISTER_BYTE (E_PTR_RET_REGNUM),
322 DEPRECATED_REGISTER_RAW_SIZE (E_PTR_RET_REGNUM));
323
324 read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
325 }
326 }
327
328 /* Function: xstormy16_push_arguments
329 Setup the function arguments for GDB to call a function in the inferior.
330 Called only in the context of a target function call from the debugger.
331 Returns the value of the SP register after the args are pushed.
332 */
333
334 static CORE_ADDR
335 xstormy16_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
336 int struct_return, CORE_ADDR struct_addr)
337 {
338 CORE_ADDR stack_dest = sp;
339 int argreg = E_1ST_ARG_REGNUM;
340 int i, j;
341 int typelen, slacklen;
342 char *val;
343
344 /* If struct_return is true, then the struct return address will
345 consume one argument-passing register. */
346 if (struct_return)
347 argreg++;
348
349 /* Arguments are passed in R2-R7 as they fit. If an argument doesn't
350 fit in the remaining registers we're switching over to the stack.
351 No argument is put on stack partially and as soon as we switched
352 over to stack no further argument is put in a register even if it
353 would fit in the remaining unused registers. */
354 for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
355 {
356 typelen = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
357 if (typelen > E_MAX_RETTYPE_SIZE (argreg))
358 break;
359
360 /* Put argument into registers wordwise. */
361 val = VALUE_CONTENTS (args[i]);
362 for (j = 0; j < typelen; j += xstormy16_reg_size)
363 write_register (argreg++,
364 extract_unsigned_integer (val + j,
365 typelen - j ==
366 1 ? 1 :
367 xstormy16_reg_size));
368 }
369
370 /* Align SP */
371 if (stack_dest & 1)
372 ++stack_dest;
373
374 /* Loop backwards through remaining arguments and push them on the stack,
375 wordaligned. */
376 for (j = nargs - 1; j >= i; j--)
377 {
378 typelen = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[j]));
379 slacklen = typelen & 1;
380 val = alloca (typelen + slacklen);
381 memcpy (val, VALUE_CONTENTS (args[j]), typelen);
382 memset (val + typelen, 0, slacklen);
383
384 /* Now write this data to the stack. The stack grows upwards. */
385 write_memory (stack_dest, val, typelen + slacklen);
386 stack_dest += typelen + slacklen;
387 }
388
389 /* And that should do it. Return the new stack pointer. */
390 return stack_dest;
391 }
392
393 /* Function: xstormy16_push_return_address (pc)
394 Setup the return address for GDB to call a function in the inferior.
395 Called only in the context of a target function call from the debugger.
396 Returns the value of the SP register when the operation is finished
397 (which may or may not be the same as before).
398 */
399
400 static CORE_ADDR
401 xstormy16_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
402 {
403 unsigned char buf[xstormy16_pc_size];
404
405 store_unsigned_integer (buf, xstormy16_pc_size, entry_point_address ());
406 write_memory (sp, buf, xstormy16_pc_size);
407 return sp + xstormy16_pc_size;
408 }
409
410 /* Function: xstormy16_pop_frame
411 Destroy the innermost (Top-Of-Stack) stack frame, restoring the
412 machine state that was in effect before the frame was created.
413 Used in the contexts of the "return" command, and of
414 target function calls from the debugger.
415 */
416
417 static void
418 xstormy16_pop_frame (void)
419 {
420 struct frame_info *fi = get_current_frame ();
421 int i;
422
423 if (fi == NULL)
424 return; /* paranoia */
425
426 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
427 get_frame_base (fi)))
428 {
429 deprecated_pop_dummy_frame ();
430 }
431 else
432 {
433 /* Restore the saved regs. */
434 for (i = 0; i < NUM_REGS; i++)
435 if (deprecated_get_frame_saved_regs (fi)[i])
436 {
437 if (i == SP_REGNUM)
438 write_register (i, deprecated_get_frame_saved_regs (fi)[i]);
439 else if (i == E_PC_REGNUM)
440 write_register (i, read_memory_integer (deprecated_get_frame_saved_regs (fi)[i],
441 xstormy16_pc_size));
442 else
443 write_register (i, read_memory_integer (deprecated_get_frame_saved_regs (fi)[i],
444 xstormy16_reg_size));
445 }
446 /* Restore the PC */
447 write_register (PC_REGNUM, DEPRECATED_FRAME_SAVED_PC (fi));
448 flush_cached_frames ();
449 }
450 return;
451 }
452
453 /* Function: xstormy16_store_struct_return
454 Copy the (struct) function return value to its destined location.
455 Called only in the context of a target function call from the debugger.
456 */
457
458 static void
459 xstormy16_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
460 {
461 write_register (E_PTR_RET_REGNUM, addr);
462 }
463
464 /* Function: xstormy16_store_return_value
465 Copy the function return value from VALBUF into the
466 proper location for a function return.
467 Called only in the context of the "return" command.
468 */
469
470 static void
471 xstormy16_store_return_value (struct type *type, char *valbuf)
472 {
473 CORE_ADDR return_buffer;
474 char buf[xstormy16_reg_size];
475
476 if (xstormy16_type_is_scalar (type) && TYPE_LENGTH (type) == 1)
477 {
478 /* Add leading zeros to the value. */
479 memset (buf, 0, xstormy16_reg_size);
480 memcpy (buf, valbuf, 1);
481 deprecated_write_register_gen (E_1ST_ARG_REGNUM, buf);
482 }
483 else if (xstormy16_type_is_scalar (type) &&
484 TYPE_LENGTH (type) <= E_MAX_RETTYPE_SIZE_IN_REGS)
485 deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (E_1ST_ARG_REGNUM),
486 valbuf, TYPE_LENGTH (type));
487 else
488 {
489 return_buffer = read_register (E_PTR_RET_REGNUM);
490 write_memory (return_buffer, valbuf, TYPE_LENGTH (type));
491 }
492 }
493
494 /* Function: xstormy16_extract_struct_value_address
495 Returns the address in which a function should return a struct value.
496 Used in the contexts of the "return" command, and of
497 target function calls from the debugger.
498 */
499
500 static CORE_ADDR
501 xstormy16_extract_struct_value_address (struct regcache *regcache)
502 {
503 /* FIXME: cagney/2004-01-17: Does the ABI guarantee that the return
504 address regster is preserved across function calls? Probably
505 not, making this function wrong. */
506 ULONGEST val;
507 regcache_raw_read_unsigned (regcache, E_PTR_RET_REGNUM, &val);
508 return val;
509 }
510
511 /* Function: xstormy16_use_struct_convention
512 Returns non-zero if the given struct type will be returned using
513 a special convention, rather than the normal function return method.
514 7sed in the contexts of the "return" command, and of
515 target function calls from the debugger.
516 */
517
518 static int
519 xstormy16_use_struct_convention (int gcc_p, struct type *type)
520 {
521 return !xstormy16_type_is_scalar (type)
522 || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
523 }
524
525 /* Function: frame_saved_register
526 Returns the value that regnum had in frame fi
527 (saved in fi or in one of its children).
528 */
529
530 static CORE_ADDR
531 xstormy16_frame_saved_register (struct frame_info *fi, int regnum)
532 {
533 int size = xstormy16_register_raw_size (regnum);
534 char *buf = (char *) alloca (size);
535
536 xstormy16_get_saved_register (buf, NULL, NULL, fi, regnum, NULL);
537 return (CORE_ADDR) extract_unsigned_integer (buf, size);
538 }
539
540 /* Function: xstormy16_scan_prologue
541 Decode the instructions within the given address range.
542 Decide when we must have reached the end of the function prologue.
543 If a frame_info pointer is provided, fill in its saved_regs etc.
544
545 Returns the address of the first instruction after the prologue.
546 */
547
548 static CORE_ADDR
549 xstormy16_scan_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
550 struct frame_info *fi, int *frameless)
551 {
552 CORE_ADDR sp = 0, fp = 0;
553 CORE_ADDR next_addr;
554 ULONGEST inst, inst2;
555 LONGEST offset;
556 int regnum;
557
558 if (frameless)
559 *frameless = 1;
560 if (fi)
561 {
562 /* In a call dummy, don't touch the frame. */
563 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
564 get_frame_base (fi)))
565 return start_addr;
566
567 /* Grab the frame-relative values of SP and FP, needed below.
568 The frame_saved_register function will find them on the
569 stack or in the registers as appropriate. */
570 sp = xstormy16_frame_saved_register (fi, E_SP_REGNUM);
571 fp = xstormy16_frame_saved_register (fi, E_FP_REGNUM);
572
573 /* Initialize framesize with size of PC put on stack by CALLF inst. */
574 get_frame_extra_info (fi)->framesize = xstormy16_pc_size;
575 }
576 for (next_addr = start_addr;
577 next_addr < end_addr; next_addr += xstormy16_inst_size)
578 {
579 inst = read_memory_unsigned_integer (next_addr, xstormy16_inst_size);
580 inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
581 xstormy16_inst_size);
582
583 if (inst >= 0x0082 && inst <= 0x008d) /* push r2 .. push r13 */
584 {
585 if (fi)
586 {
587 regnum = inst & 0x000f;
588 deprecated_get_frame_saved_regs (fi)[regnum] = get_frame_extra_info (fi)->framesize;
589 get_frame_extra_info (fi)->framesize += xstormy16_reg_size;
590 }
591 }
592
593 /* optional stack allocation for args and local vars <= 4 byte */
594 else if (inst == 0x301f || inst == 0x303f) /* inc r15, #0x1/#0x3 */
595 {
596 if (fi) /* Record the frame size. */
597 get_frame_extra_info (fi)->framesize += ((inst & 0x0030) >> 4) + 1;
598 }
599
600 /* optional stack allocation for args and local vars > 4 && < 16 byte */
601 else if ((inst & 0xff0f) == 0x510f) /* 51Hf add r15, #0xH */
602 {
603 if (fi) /* Record the frame size. */
604 get_frame_extra_info (fi)->framesize += (inst & 0x00f0) >> 4;
605 }
606
607 /* optional stack allocation for args and local vars >= 16 byte */
608 else if (inst == 0x314f && inst2 >= 0x0010) /* 314f HHHH add r15, #0xH */
609 {
610 if (fi) /* Record the frame size. */
611 get_frame_extra_info (fi)->framesize += inst2;
612 next_addr += xstormy16_inst_size;
613 }
614
615 else if (inst == 0x46fd) /* mov r13, r15 */
616 {
617 if (fi) /* Record that the frame pointer is in use. */
618 get_frame_extra_info (fi)->frameless_p = 0;
619 if (frameless)
620 *frameless = 0;
621 }
622
623 /* optional copying of args in r2-r7 to r10-r13 */
624 /* Probably only in optimized case but legal action for prologue */
625 else if ((inst & 0xff00) == 0x4600 /* 46SD mov rD, rS */
626 && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
627 && (inst & 0x000f) >= 0x00a0 && (inst & 0x000f) <= 0x000d)
628 ;
629
630 /* optional copying of args in r2-r7 to stack */
631 /* 72DS HHHH mov.b (rD, 0xHHHH), r(S-8) (bit3 always 1, bit2-0 = reg) */
632 /* 73DS HHHH mov.w (rD, 0xHHHH), r(S-8) */
633 else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
634 {
635 if (fi)
636 {
637 regnum = inst & 0x0007;
638 /* Only 12 of 16 bits of the argument are used for the
639 signed offset. */
640 offset = (LONGEST) (inst2 & 0x0fff);
641 if (offset & 0x0800)
642 offset -= 0x1000;
643
644 deprecated_get_frame_saved_regs (fi)[regnum] = get_frame_extra_info (fi)->framesize + offset;
645 }
646 next_addr += xstormy16_inst_size;
647 }
648
649 #if 0
650 /* 2001-08-10: Not part of the prologue anymore due to change in
651 ABI. r8 and r9 are not used for argument passing anymore. */
652
653 /* optional copying of r8, r9 to stack */
654 /* 46S7; 73Df HHHH mov.w r7,rS; mov.w (rD, 0xHHHH), r7 D=8,9; S=13,15 */
655 /* 46S7; 72df HHHH mov.w r7,rS; mov.b (rD, 0xHHHH), r7 D=8,9; S=13,15 */
656 else if ((inst & 0xffef) == 0x4687 && (inst2 & 0xfedf) == 0x72df)
657 {
658 next_addr += xstormy16_inst_size;
659 if (fi)
660 {
661 regnum = (inst & 0x00f0) >> 4;
662 inst = inst2;
663 inst2 = read_memory_unsigned_integer (next_addr
664 + xstormy16_inst_size,
665 xstormy16_inst_size);
666 /* Only 12 of 16 bits of the argument are used for the
667 signed offset. */
668 offset = (LONGEST) (inst2 & 0x0fff);
669 if (offset & 0x0800)
670 offset -= 0x1000;
671
672 fi->saved_regs[regnum] = fi->extra_info->framesize + offset;
673 }
674 next_addr += xstormy16_inst_size;
675 }
676 #endif
677
678 else /* Not a prologue instruction. */
679 break;
680 }
681
682 if (fi)
683 {
684 /* Special handling for the "saved" address of the SP:
685 The SP is of course never saved on the stack at all, so
686 by convention what we put here is simply the previous
687 _value_ of the SP (as opposed to an address where the
688 previous value would have been pushed). */
689 if (get_frame_extra_info (fi)->frameless_p)
690 {
691 deprecated_get_frame_saved_regs (fi)[E_SP_REGNUM] = sp - get_frame_extra_info (fi)->framesize;
692 deprecated_update_frame_base_hack (fi, sp);
693 }
694 else
695 {
696 deprecated_get_frame_saved_regs (fi)[E_SP_REGNUM] = fp - get_frame_extra_info (fi)->framesize;
697 deprecated_update_frame_base_hack (fi, fp);
698 }
699
700 /* So far only offsets to the beginning of the frame are
701 saved in the saved_regs. Now we now the relation between
702 sp, fp and framesize. We know the beginning of the frame
703 so we can translate the register offsets to real addresses. */
704 for (regnum = 0; regnum < E_SP_REGNUM; ++regnum)
705 if (deprecated_get_frame_saved_regs (fi)[regnum])
706 deprecated_get_frame_saved_regs (fi)[regnum] += deprecated_get_frame_saved_regs (fi)[E_SP_REGNUM];
707
708 /* Save address of PC on stack. */
709 deprecated_get_frame_saved_regs (fi)[E_PC_REGNUM] = deprecated_get_frame_saved_regs (fi)[E_SP_REGNUM];
710 }
711
712 return next_addr;
713 }
714
715 /* Function: xstormy16_skip_prologue
716 If the input address is in a function prologue,
717 returns the address of the end of the prologue;
718 else returns the input address.
719
720 Note: the input address is likely to be the function start,
721 since this function is mainly used for advancing a breakpoint
722 to the first line, or stepping to the first line when we have
723 stepped into a function call. */
724
725 static CORE_ADDR
726 xstormy16_skip_prologue (CORE_ADDR pc)
727 {
728 CORE_ADDR func_addr = 0, func_end = 0;
729 char *func_name;
730
731 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
732 {
733 struct symtab_and_line sal;
734 struct symbol *sym;
735
736 /* Don't trust line number debug info in frameless functions. */
737 int frameless = 1;
738 CORE_ADDR plg_end = xstormy16_scan_prologue (func_addr, func_end,
739 NULL, &frameless);
740 if (frameless)
741 return plg_end;
742
743 /* Found a function. */
744 sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL, NULL);
745 /* Don't use line number debug info for assembly source files. */
746 if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
747 {
748 sal = find_pc_line (func_addr, 0);
749 if (sal.end && sal.end < func_end)
750 {
751 /* Found a line number, use it as end of prologue. */
752 return sal.end;
753 }
754 }
755 /* No useable line symbol. Use result of prologue parsing method. */
756 return plg_end;
757 }
758
759 /* No function symbol -- just return the PC. */
760
761 return (CORE_ADDR) pc;
762 }
763
764 /* The epilogue is defined here as the area at the end of a function,
765 either on the `ret' instruction itself or after an instruction which
766 destroys the function's stack frame. */
767 static int
768 xstormy16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
769 {
770 CORE_ADDR addr, func_addr = 0, func_end = 0;
771
772 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
773 {
774 ULONGEST inst, inst2;
775 CORE_ADDR addr = func_end - xstormy16_inst_size;
776
777 /* The Xstormy16 epilogue is max. 14 bytes long. */
778 if (pc < func_end - 7 * xstormy16_inst_size)
779 return 0;
780
781 /* Check if we're on a `ret' instruction. Otherwise it's
782 too dangerous to proceed. */
783 inst = read_memory_unsigned_integer (addr, xstormy16_inst_size);
784 if (inst != 0x0003)
785 return 0;
786
787 while ((addr -= xstormy16_inst_size) >= func_addr)
788 {
789 inst = read_memory_unsigned_integer (addr, xstormy16_inst_size);
790 if (inst >= 0x009a && inst <= 0x009d) /* pop r10...r13 */
791 continue;
792 if (inst == 0x305f || inst == 0x307f) /* dec r15, #0x1/#0x3 */
793 break;
794 inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
795 xstormy16_inst_size);
796 if (inst2 == 0x314f && inst >= 0x8000) /* add r15, neg. value */
797 {
798 addr -= xstormy16_inst_size;
799 break;
800 }
801 return 0;
802 }
803 if (pc > addr)
804 return 1;
805 }
806 return 0;
807 }
808
809 /* Function: xstormy16_frame_init_saved_regs
810 Set up the 'saved_regs' array.
811 This is a data structure containing the addresses on the stack
812 where each register has been saved, for each stack frame.
813 Registers that have not been saved will have zero here.
814 The stack register is special: rather than the address where the
815 stack register has been saved, saved_regs[SP_REGNUM] will have the
816 actual value of the previous frame's stack register.
817
818 This function may be called in any context where the saved register
819 values may be needed (backtrace, frame_info, frame_register). On
820 many targets, it is called directly by init_extra_frame_info, in
821 part because the information may be needed immediately by
822 frame_chain. */
823
824 static void
825 xstormy16_frame_init_saved_regs (struct frame_info *fi)
826 {
827 CORE_ADDR func_addr, func_end;
828
829 if (!deprecated_get_frame_saved_regs (fi))
830 {
831 frame_saved_regs_zalloc (fi);
832
833 /* Find the beginning of this function, so we can analyze its
834 prologue. */
835 if (find_pc_partial_function (get_frame_pc (fi), NULL, &func_addr, &func_end))
836 xstormy16_scan_prologue (func_addr, get_frame_pc (fi), fi, NULL);
837 /* Else we're out of luck (can't debug completely stripped code).
838 FIXME. */
839 }
840 }
841
842 /* Function: xstormy16_frame_saved_pc
843 Returns the return address for the selected frame.
844 Called by frame_info, legacy_frame_chain_valid, and sometimes by
845 get_prev_frame. */
846
847 static CORE_ADDR
848 xstormy16_frame_saved_pc (struct frame_info *fi)
849 {
850 CORE_ADDR saved_pc;
851
852 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
853 get_frame_base (fi)))
854 {
855 saved_pc = deprecated_read_register_dummy (get_frame_pc (fi),
856 get_frame_base (fi),
857 E_PC_REGNUM);
858 }
859 else
860 {
861 saved_pc = read_memory_unsigned_integer (deprecated_get_frame_saved_regs (fi)[E_PC_REGNUM],
862 xstormy16_pc_size);
863 }
864
865 return saved_pc;
866 }
867
868 /* Function: xstormy16_init_extra_frame_info
869 This is the constructor function for the frame_info struct,
870 called whenever a new frame_info is created (from create_new_frame,
871 and from get_prev_frame).
872 */
873
874 static void
875 xstormy16_init_extra_frame_info (int fromleaf, struct frame_info *fi)
876 {
877 if (!get_frame_extra_info (fi))
878 {
879 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
880 get_frame_extra_info (fi)->framesize = 0;
881 get_frame_extra_info (fi)->frameless_p = 1; /* Default frameless, detect framed */
882
883 /* By default, the fi->frame is set to the value of the FP reg by gdb.
884 This may not always be right; we may be in a frameless function,
885 or we may be in the prologue, before the FP has been set up.
886 Unfortunately, we can't make this determination without first
887 calling scan_prologue, and we can't do that unles we know the
888 get_frame_pc (fi). */
889
890 if (!get_frame_pc (fi))
891 {
892 /* Sometimes we are called from get_prev_frame without
893 the PC being set up first. Long history, don't ask.
894 Fortunately this will never happen from the outermost
895 frame, so we should be able to get the saved pc from
896 the next frame. */
897 if (get_next_frame (fi))
898 deprecated_update_frame_pc_hack (fi, xstormy16_frame_saved_pc (get_next_frame (fi)));
899 }
900
901 /* Take care of the saved_regs right here (non-lazy). */
902 xstormy16_frame_init_saved_regs (fi);
903 }
904 }
905
906 /* Function: xstormy16_frame_chain
907 Returns a pointer to the stack frame of the calling function.
908 Called only from get_prev_frame. Needed for backtrace, "up", etc.
909 */
910
911 static CORE_ADDR
912 xstormy16_frame_chain (struct frame_info *fi)
913 {
914 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
915 get_frame_base (fi)))
916 {
917 /* Call dummy's frame is the same as caller's. */
918 return get_frame_base (fi);
919 }
920 else
921 {
922 /* Return computed offset from this frame's fp. */
923 return get_frame_base (fi) - get_frame_extra_info (fi)->framesize;
924 }
925 }
926
927 static int
928 xstormy16_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
929 {
930 return chain < 0x8000 && DEPRECATED_FRAME_SAVED_PC (thisframe) >= 0x8000 &&
931 (get_frame_extra_info (thisframe)->frameless_p ||
932 get_frame_base (thisframe) - get_frame_extra_info (thisframe)->framesize == chain);
933 }
934
935 /* Function: xstormy16_saved_pc_after_call Returns the previous PC
936 immediately after a function call. This function is meant to
937 bypass the regular frame_register() mechanism, ie. it is meant to
938 work even if the frame isn't complete. Called by
939 step_over_function, and sometimes by get_prev_frame. */
940
941 static CORE_ADDR
942 xstormy16_saved_pc_after_call (struct frame_info *ignore)
943 {
944 CORE_ADDR sp, pc, tmp;
945
946 sp = read_register (E_SP_REGNUM) - xstormy16_pc_size;
947 pc = read_memory_integer (sp, xstormy16_pc_size);
948
949 /* Skip over jump table entry if necessary. */
950 if ((tmp = SKIP_TRAMPOLINE_CODE (pc)))
951 pc = tmp;
952
953 return pc;
954 }
955
956 const static unsigned char *
957 xstormy16_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
958 {
959 static unsigned char breakpoint[] = { 0x06, 0x0 };
960 *lenptr = sizeof (breakpoint);
961 return breakpoint;
962 }
963
964 /* Given a pointer to a jump table entry, return the address
965 of the function it jumps to. Return 0 if not found. */
966 static CORE_ADDR
967 xstormy16_resolve_jmp_table_entry (CORE_ADDR faddr)
968 {
969 struct obj_section *faddr_sect = find_pc_section (faddr);
970
971 if (faddr_sect)
972 {
973 LONGEST inst, inst2, addr;
974 char buf[2 * xstormy16_inst_size];
975
976 /* Return faddr if it's not pointing into the jump table. */
977 if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
978 return faddr;
979
980 if (!target_read_memory (faddr, buf, sizeof buf))
981 {
982 inst = extract_unsigned_integer (buf, xstormy16_inst_size);
983 inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
984 xstormy16_inst_size);
985 addr = inst2 << 8 | (inst & 0xff);
986 return addr;
987 }
988 }
989 return 0;
990 }
991
992 /* Given a function's address, attempt to find (and return) the
993 address of the corresponding jump table entry. Return 0 if
994 not found. */
995 static CORE_ADDR
996 xstormy16_find_jmp_table_entry (CORE_ADDR faddr)
997 {
998 struct obj_section *faddr_sect = find_pc_section (faddr);
999
1000 if (faddr_sect)
1001 {
1002 struct obj_section *osect;
1003
1004 /* Return faddr if it's already a pointer to a jump table entry. */
1005 if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
1006 return faddr;
1007
1008 ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
1009 {
1010 if (!strcmp (osect->the_bfd_section->name, ".plt"))
1011 break;
1012 }
1013
1014 if (osect < faddr_sect->objfile->sections_end)
1015 {
1016 CORE_ADDR addr;
1017 for (addr = osect->addr;
1018 addr < osect->endaddr; addr += 2 * xstormy16_inst_size)
1019 {
1020 int status;
1021 LONGEST inst, inst2, faddr2;
1022 char buf[2 * xstormy16_inst_size];
1023
1024 if (target_read_memory (addr, buf, sizeof buf))
1025 return 0;
1026 inst = extract_unsigned_integer (buf, xstormy16_inst_size);
1027 inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
1028 xstormy16_inst_size);
1029 faddr2 = inst2 << 8 | (inst & 0xff);
1030 if (faddr == faddr2)
1031 return addr;
1032 }
1033 }
1034 }
1035 return 0;
1036 }
1037
1038 static CORE_ADDR
1039 xstormy16_skip_trampoline_code (CORE_ADDR pc)
1040 {
1041 int tmp = xstormy16_resolve_jmp_table_entry (pc);
1042
1043 if (tmp && tmp != pc)
1044 return tmp;
1045 return 0;
1046 }
1047
1048 static int
1049 xstormy16_in_solib_call_trampoline (CORE_ADDR pc, char *name)
1050 {
1051 return xstormy16_skip_trampoline_code (pc) != 0;
1052 }
1053
1054 static CORE_ADDR
1055 xstormy16_pointer_to_address (struct type *type, const void *buf)
1056 {
1057 enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
1058 CORE_ADDR addr = extract_unsigned_integer (buf, TYPE_LENGTH (type));
1059
1060 if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
1061 {
1062 CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (addr);
1063 if (addr2)
1064 addr = addr2;
1065 }
1066
1067 return addr;
1068 }
1069
1070 static void
1071 xstormy16_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
1072 {
1073 enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
1074
1075 if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
1076 {
1077 CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (addr);
1078 if (addr2)
1079 addr = addr2;
1080 }
1081 store_unsigned_integer (buf, TYPE_LENGTH (type), addr);
1082 }
1083
1084 static CORE_ADDR
1085 xstormy16_stack_align (CORE_ADDR addr)
1086 {
1087 if (addr & 1)
1088 ++addr;
1089 return addr;
1090 }
1091
1092 static void
1093 xstormy16_save_dummy_frame_tos (CORE_ADDR sp)
1094 {
1095 generic_save_dummy_frame_tos (sp - xstormy16_pc_size);
1096 }
1097
1098 /* Function: xstormy16_gdbarch_init
1099 Initializer function for the xstormy16 gdbarch vector.
1100 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1101
1102 static struct gdbarch *
1103 xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1104 {
1105 static LONGEST call_dummy_words[1] = { 0 };
1106 struct gdbarch_tdep *tdep = NULL;
1107 struct gdbarch *gdbarch;
1108
1109 /* find a candidate among the list of pre-declared architectures. */
1110 arches = gdbarch_list_lookup_by_info (arches, &info);
1111 if (arches != NULL)
1112 return (arches->gdbarch);
1113
1114 #if 0
1115 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1116 #endif
1117
1118 gdbarch = gdbarch_alloc (&info, 0);
1119
1120 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1121 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1122 set_gdbarch_deprecated_init_frame_pc (gdbarch, deprecated_init_frame_pc_default);
1123
1124 /*
1125 * Basic register fields and methods.
1126 */
1127
1128 set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
1129 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1130 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1131 set_gdbarch_deprecated_fp_regnum (gdbarch, E_FP_REGNUM);
1132 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1133 set_gdbarch_register_name (gdbarch, xstormy16_register_name);
1134 set_gdbarch_deprecated_register_size (gdbarch, xstormy16_reg_size);
1135 set_gdbarch_deprecated_register_bytes (gdbarch, E_ALL_REGS_SIZE);
1136 set_gdbarch_deprecated_register_byte (gdbarch, xstormy16_register_byte);
1137 set_gdbarch_deprecated_register_raw_size (gdbarch, xstormy16_register_raw_size);
1138 set_gdbarch_deprecated_max_register_raw_size (gdbarch, xstormy16_pc_size);
1139 set_gdbarch_deprecated_register_virtual_size (gdbarch, xstormy16_register_raw_size);
1140 set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 4);
1141 set_gdbarch_deprecated_register_virtual_type (gdbarch, xstormy16_reg_virtual_type);
1142
1143 /*
1144 * Frame Info
1145 */
1146 set_gdbarch_deprecated_init_extra_frame_info (gdbarch,
1147 xstormy16_init_extra_frame_info);
1148 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch,
1149 xstormy16_frame_init_saved_regs);
1150 set_gdbarch_deprecated_frame_chain (gdbarch, xstormy16_frame_chain);
1151 set_gdbarch_deprecated_get_saved_register (gdbarch, xstormy16_get_saved_register);
1152 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, xstormy16_saved_pc_after_call);
1153 set_gdbarch_deprecated_frame_saved_pc (gdbarch, xstormy16_frame_saved_pc);
1154 set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
1155 set_gdbarch_deprecated_frame_chain_valid (gdbarch, xstormy16_frame_chain_valid);
1156
1157 set_gdbarch_in_function_epilogue_p (gdbarch,
1158 xstormy16_in_function_epilogue_p);
1159
1160 /*
1161 * Miscelany
1162 */
1163 /* Stack grows up. */
1164 set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
1165
1166 /*
1167 * Call Dummies
1168 *
1169 * These values and methods are used when gdb calls a target function. */
1170 set_gdbarch_deprecated_push_return_address (gdbarch, xstormy16_push_return_address);
1171 set_gdbarch_deprecated_extract_return_value (gdbarch, xstormy16_extract_return_value);
1172 set_gdbarch_deprecated_push_arguments (gdbarch, xstormy16_push_arguments);
1173 set_gdbarch_deprecated_pop_frame (gdbarch, xstormy16_pop_frame);
1174 set_gdbarch_deprecated_store_struct_return (gdbarch, xstormy16_store_struct_return);
1175 set_gdbarch_deprecated_store_return_value (gdbarch, xstormy16_store_return_value);
1176 set_gdbarch_deprecated_extract_struct_value_address (gdbarch, xstormy16_extract_struct_value_address);
1177 set_gdbarch_use_struct_convention (gdbarch,
1178 xstormy16_use_struct_convention);
1179 set_gdbarch_deprecated_call_dummy_words (gdbarch, call_dummy_words);
1180 set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, 0);
1181 set_gdbarch_breakpoint_from_pc (gdbarch, xstormy16_breakpoint_from_pc);
1182
1183 set_gdbarch_char_signed (gdbarch, 0);
1184 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1185 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1186 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1187 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1188
1189 set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
1190 set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);
1191
1192 set_gdbarch_deprecated_stack_align (gdbarch, xstormy16_stack_align);
1193
1194 set_gdbarch_deprecated_save_dummy_frame_tos (gdbarch, xstormy16_save_dummy_frame_tos);
1195
1196 set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);
1197
1198 set_gdbarch_in_solib_call_trampoline (gdbarch,
1199 xstormy16_in_solib_call_trampoline);
1200
1201 /* Should be using push_dummy_call. */
1202 set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
1203
1204 set_gdbarch_print_insn (gdbarch, print_insn_xstormy16);
1205
1206 return gdbarch;
1207 }
1208
1209 /* Function: _initialize_xstormy16_tdep
1210 Initializer function for the Sanyo Xstormy16a module.
1211 Called by gdb at start-up. */
1212
1213 extern initialize_file_ftype _initialize_xstormy16_tdep; /* -Wmissing-prototypes */
1214
1215 void
1216 _initialize_xstormy16_tdep (void)
1217 {
1218 register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
1219 }