1 /* Target-dependent code for Zylin ZPU
2
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004 Free Software
4 Foundation, Inc.
5
6 Contributed by Stephane Carrez, stcarrez@nerim.fr
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "frame-unwind.h"
28 #include "frame-base.h"
29 #include "dwarf2-frame.h"
30 #include "trad-frame.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "gdb_string.h"
36 #include "value.h"
37 #include "inferior.h"
38 #include "dis-asm.h"
39 #include "symfile.h"
40 #include "objfiles.h"
41 #include "arch-utils.h"
42 #include "regcache.h"
43 #include "reggroups.h"
44
45 #include "target.h"
46 #include "opcode/zpu.h"
47 #include "elf/zpu.h"
48 #include "elf-bfd.h"
49
50 #define RETURN_ADDR_SIZE 4
51
52 #define HARD_R0_REGNUM 0
53 #define HARD_R1_REGNUM 1
54 #define HARD_R2_REGNUM 2
55 #define HARD_R3_REGNUM 3
56 #define HARD_R4_REGNUM 4
57 #define HARD_R5_REGNUM 5
58 #define HARD_R6_REGNUM 6
59 #define HARD_R7_REGNUM 7
60 #define HARD_SP_REGNUM 32
61 #define HARD_PC_REGNUM 33
62
63 #define ZPU_NUM_REGS (34)
64
65 #define ZPU_REG_SIZE (4)
66
67 struct insn_sequence;
68 struct gdbarch_tdep
69 {
70 /* ELF flags for ABI. */
71 int elf_flags;
72 };
73
74
75 struct zpu_unwind_cache
76 {
77 /* The previous frame's inner most stack address. Used as this
78 frame ID's stack_addr. */
79 CORE_ADDR prev_sp;
80 /* The frame's base, optionally used by the high-level debug info. */
81 CORE_ADDR base;
82 CORE_ADDR pc;
83 int argSize; /* argument size */
84 int frameSize; /* frame size */
85 int savedSize; /* size of saved registers */
86 CORE_ADDR return_pc;
87
88 /* Table indicating the location of each and every register. */
89 struct trad_frame_saved_reg *saved_regs;
90 };
91
92 /* Table of registers for ZPU. This includes the hard registers
93 and the soft registers used by GCC. */
94 static char *
95 zpu_register_names[] =
96 {
97 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
98 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
99 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
100 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
101 "sp", "pc"
102 };
103
104
105 static const char *
zpu_register_name(int reg_nr)106 zpu_register_name (int reg_nr)
107 {
108 if ((reg_nr <0)||(reg_nr>ZPU_NUM_REGS))
109 return NULL;
110
111 return zpu_register_names[reg_nr];
112 }
113
114 /* breakpoint is always the same as PC address w/0x00. */
115 static const unsigned char *
zpu_breakpoint_from_pc(CORE_ADDR * pcptr,int * lenptr)116 zpu_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
117 {
118 static unsigned char breakpoint[] = {0x00};
119
120 *lenptr = sizeof (breakpoint);
121 return breakpoint;
122 }
123
124 /* Return the GDB type object for the "standard" data type
125 of data in register N. */
126
127 static struct type *
zpu_register_type(struct gdbarch * gdbarch,int reg_nr)128 zpu_register_type (struct gdbarch *gdbarch, int reg_nr)
129 {
130 return builtin_type_uint32;
131 }
132
133 static void
zpu_store_return_value(struct type * type,struct regcache * regcache,const void * valbuf)134 zpu_store_return_value (struct type *type, struct regcache *regcache,
135 const void *valbuf)
136 {
137 error ("not implemented store return value..");
138 }
139
140
141 /* Given a return value in `regcache' with a type `type',
142 extract and copy its value into `valbuf'. */
143 static void
zpu_extract_return_value(struct type * type,struct regcache * regcache,void * valbuf)144 zpu_extract_return_value (struct type *type, struct regcache *regcache,
145 void *valbuf)
146 {
147 regcache_raw_read (regcache, HARD_R0_REGNUM, valbuf);
148 }
149
150 enum return_value_convention
zpu_return_value(struct gdbarch * gdbarch,struct type * valtype,struct regcache * regcache,void * readbuf,const void * writebuf)151 zpu_return_value (struct gdbarch *gdbarch, struct type *valtype,
152 struct regcache *regcache, void *readbuf,
153 const void *writebuf)
154 {
155 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
156 || TYPE_CODE (valtype) == TYPE_CODE_UNION
157 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY
158 || TYPE_LENGTH (valtype) > 4)
159 return RETURN_VALUE_STRUCT_CONVENTION;
160 else
161 {
162 if (readbuf != NULL)
163 zpu_extract_return_value (valtype, regcache, readbuf);
164 if (writebuf != NULL)
165 zpu_store_return_value (valtype, regcache, writebuf);
166 return RETURN_VALUE_REGISTER_CONVENTION;
167 }
168 }
169
170 static int
gdb_print_insn_zpu(bfd_vma memaddr,disassemble_info * info)171 gdb_print_insn_zpu (bfd_vma memaddr, disassemble_info *info)
172 {
173 return print_insn_zpu (memaddr, info);
174 }
175
176
177
178 #define PUSHPC 1
179 #define LOAD 4
180 #define STORE 5
181 #define POPPC 6
182 #define ADD 8
183 #define PUSHSP 9
184 #define POPSP 10
185
decodeImmediate(unsigned char t)186 static int decodeImmediate(unsigned char t)
187 {
188 return (((int)((signed char)(t<<1)))>>1);
189 }
190
191 /* skip copy of return value to register */
skip_store_retval(CORE_ADDR ip)192 CORE_ADDR skip_store_retval(CORE_ADDR ip)
193 {
194 unsigned char t;
195 CORE_ADDR scan=ip;
196
197 t=read_memory_integer (scan++, 1);
198 if (t==0x80)
199 {
200 t=read_memory_integer (scan++, 1);
201 if (t==LOAD)
202 {
203 t=read_memory_integer (scan++, 1);
204 if (((t&0x7f)>=0)&&((t&0x7f)<=12))
205 {
206 t=read_memory_integer (scan++, 1);
207 if (t==STORE)
208 {
209 return scan;
210 }
211 }
212 }
213 }
214 return ip;
215 }
216
217
scan_stack_change(CORE_ADDR * ip_core,int * const frameSize,int * const gotFrame)218 void scan_stack_change(CORE_ADDR *ip_core, int * const frameSize, int * const gotFrame)
219 {
220 unsigned char t;
221 *gotFrame=0;
222 t=read_memory_integer ((*ip_core)++, 1);
223 if (t==PUSHSP)
224 {
225 *gotFrame=1;
226
227 t=read_memory_integer ((*ip_core)++, 1);
228 if ((t&0x80)!=0)
229 {
230 /* amount of stack allocated is < 63 bytes
231 *
232 * allocate space on stack
233 436: 09 pushsp
234 437: f8 im -8
235 438: 08 add
236 439: 0a popsp
237 *
238 * */
239 *frameSize=decodeImmediate(t);
240
241 *gotFrame=1;
242
243 } else
244 {
245 CORE_ADDR frameSizeAddr;
246 /*
247 * allocate space on stack, but > 63 bytes
248 4: 09 pushsp
249 5: 01 pushpc
250 6: 97 im 23
251 7: 08 add
252 8: 04 load
253 9: 08 add
254 a: 0a popsp
255 */
256
257 frameSizeAddr=*ip_core;
258 t=read_memory_integer ((*ip_core)++, 1);
259 *gotFrame=0;
260
261 if (t==PUSHPC)
262 {
263 t=read_memory_integer ((*ip_core)++, 1);
264 if ((t&0x80)!=0)
265 {
266 frameSizeAddr+=decodeImmediate(t);
267
268 t=read_memory_integer ((*ip_core)++, 1);
269 if (t==ADD)
270 {
271 t=read_memory_integer ((*ip_core)++, 1);
272 if (t==LOAD)
273 {
274 *frameSize=read_memory_integer(frameSizeAddr, 4);
275
276 *gotFrame=1;
277 }
278 }
279 }
280 }
281 }
282
283
284 if (*gotFrame)
285 {
286 *gotFrame=0;
287
288 /*
289 * 9: 08 add
290 * a: 0a popsp
291 */
292 t=read_memory_integer ((*ip_core)++, 1);
293 if (t==ADD)
294 {
295 t=read_memory_integer ((*ip_core)++, 1);
296 if (t==POPSP)
297 {
298 *gotFrame=1;
299 }
300 }
301 }
302 }
303 }
304
305 /*
306 * Example prologue:
307 *
308 * save registers
309 430: 84 im 4
310 431: 04 load
311 432: 88 im 8
312 433: 04 load
313 434: 8c im 12
314 435: 04 load
315 */
316
317 /* fill in zpu_unwind_cache structure w/information about fn.
318 *
319 * primary return value returns first instruction after prologue
320 */
321 static CORE_ADDR
zpu_scan_prologue(CORE_ADDR ip,CORE_ADDR current_pc,struct zpu_unwind_cache * info,struct trad_frame_saved_reg * saved_regs)322 zpu_scan_prologue (
323 /* address of function to scan */
324 CORE_ADDR ip,
325 /* current pc inside function */
326 CORE_ADDR current_pc,
327 /* fill in this structure */
328 struct zpu_unwind_cache *info,
329 struct trad_frame_saved_reg *saved_regs)
330
331 {
332 int frameSize=0;
333 int gotFrame=1;
334 info->frameSize = 0;
335 if (ip > current_pc)
336 {
337 /* sanity check */
338 return current_pc;
339 }
340
341 if (ip == 0)
342 {
343 /* we're not inside a fn w/debug info. */
344 return ip;
345 }
346
347 CORE_ADDR ip_core=ip;
348 int i;
349
350 /* skip saving of registers... At most r1-r3 */
351 unsigned char t;
352 for (i=0; i<3; i++)
353 {
354 t=read_memory_integer (ip_core++, 1);
355 if (t==PUSHSP)
356 {
357 /* found all saved registers... */
358 break;
359 }
360
361 /* quick&dirty check on whether this is a saved register */
362 switch (t)
363 {
364 case 4+128:
365 case 8+128:
366 case 12+128:
367 break;
368
369 default:
370 return ip;
371 }
372
373 if (saved_regs!=NULL)
374 {
375 int reg=(t&0x7f)/4;
376 saved_regs[reg].addr=-i*4;
377 }
378
379 t=read_memory_integer (ip_core++, 1);
380 if (t!=LOAD)
381 {
382 return ip;
383 }
384 }
385
386 /* size of saved registers */
387 info->savedSize=i*4;
388
389 if (saved_regs!=NULL)
390 {
391 /* add size of registers... */
392 for (i=0; i<4; i++)
393 {
394 if (trad_frame_addr_p (info->saved_regs, i))
395 {
396 saved_regs[i].addr+=info->savedSize;
397 }
398 }
399 }
400
401 ip=ip_core; /* this is the first instruction as far as we know for now */
402
403
404 /* figure out how much stack that has been allocated */
405 if (i!=3)
406 {
407 /* step back an address */
408 ip_core--;
409 }
410
411
412 /* at this point we might be frameless, so only continue scanning
413 * as long as we see expected frame instructions and only commit frame
414 * size when we actually see the frame allocated.
415 */
416 scan_stack_change(&ip_core, &frameSize, &gotFrame);
417
418 if (gotFrame&&(frameSize<0))
419 {
420 info->frameSize=-frameSize;
421 ip=ip_core;
422 }
423
424 return ip;
425 }
426
zpu_skip_prologue(CORE_ADDR ip)427 CORE_ADDR zpu_skip_prologue (CORE_ADDR ip)
428 {
429 struct zpu_unwind_cache tmp_cache = { 0 };
430
431 return zpu_scan_prologue (ip, (CORE_ADDR) -1, &tmp_cache, NULL);
432 }
433
434 static CORE_ADDR
zpu_unwind_sp(struct gdbarch * gdbarch,struct frame_info * next_frame)435 zpu_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
436 {
437 ULONGEST sp;
438 frame_unwind_unsigned_register (next_frame, HARD_SP_REGNUM, &sp);
439 return sp;
440 }
441
442 static CORE_ADDR
zpu_unwind_pc(struct gdbarch * gdbarch,struct frame_info * next_frame)443 zpu_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
444 {
445 ULONGEST pc;
446
447 frame_unwind_unsigned_register (next_frame, gdbarch_pc_regnum (gdbarch),
448 &pc);
449 return pc;
450 }
451
452
453 /* examine stack frame, starting from next_frame */
454
455 struct zpu_unwind_cache *
zpu_frame_unwind_cache(struct frame_info * next_frame,void ** this_prologue_cache)456 zpu_frame_unwind_cache (struct frame_info *next_frame,
457 void **this_prologue_cache)
458 {
459 ULONGEST this_base;
460 struct zpu_unwind_cache *info;
461 CORE_ADDR current_pc;
462 int i;
463
464 if ((*this_prologue_cache))
465 return (*this_prologue_cache);
466
467 info = FRAME_OBSTACK_ZALLOC (struct zpu_unwind_cache);
468
469 /* NOTE! by setting the info pointer here, we avoid infinite recursion.
470 * Make sure that we do not use the 'info' structure before we have filled
471 * in the bits we are asking questions about...
472 */
473 (*this_prologue_cache) = info;
474
475 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
476
477 /* frame_func_unwind() returns 0 if no fn was found */
478 info->pc = frame_func_unwind (next_frame);
479
480 info->frameSize = 0;
481
482 /* figure out frame pointer.... */
483 frame_unwind_unsigned_register (next_frame, HARD_SP_REGNUM, &this_base);
484 if (this_base == 0)
485 {
486 info->base = 0;
487 return info;
488 }
489
490 current_pc = frame_pc_unwind (next_frame);
491 if (info->pc != 0)
492 zpu_scan_prologue (info->pc, current_pc, info, info->saved_regs);
493
494
495 /* PC is saved immediately before saved off registers */
496 info->saved_regs[HARD_PC_REGNUM].addr = this_base+info->frameSize+info->savedSize;
497
498 /* we need to scan the instructions at the return address to figure out the
499 * size of the arguments passed to the fn.
500 */
501 info->return_pc=read_memory_integer(info->saved_regs[HARD_PC_REGNUM].addr, 4);
502
503 CORE_ADDR scan=info->return_pc;
504 int gotIt;
505
506 scan=skip_store_retval(scan);
507
508 scan_stack_change(&scan, &info->argSize, &gotIt);
509 if (info->argSize<0)
510 {
511 /* don't know what the caller is doing, but it isn't popping arguments off
512 * the stack
513 */
514 info->argSize=0;
515 }
516
517
518 info->prev_sp = info->saved_regs[HARD_PC_REGNUM].addr + RETURN_ADDR_SIZE;
519
520 info->base = this_base;
521
522
523
524 /* The previous frame's SP needed to be computed. Save the computed
525 value. */
526 trad_frame_set_value (info->saved_regs, HARD_SP_REGNUM, info->prev_sp);
527
528 return info;
529 }
530
531 /* Given a GDB frame, determine the address of the calling function's
532 frame. This will be used to create a new GDB frame struct. */
533
534 static void
zpu_frame_this_id(struct frame_info * next_frame,void ** this_prologue_cache,struct frame_id * this_id)535 zpu_frame_this_id (struct frame_info *next_frame,
536 void **this_prologue_cache,
537 struct frame_id *this_id)
538 {
539 struct zpu_unwind_cache *info
540 = zpu_frame_unwind_cache (next_frame, this_prologue_cache);
541 CORE_ADDR func;
542 struct frame_id id;
543
544 func = frame_func_unwind (next_frame);
545
546 id = frame_id_build (info->base, func);
547 (*this_id) = id;
548 }
549
550 static void
zpu_frame_prev_register(struct frame_info * next_frame,void ** this_prologue_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * bufferp)551 zpu_frame_prev_register (struct frame_info *next_frame,
552 void **this_prologue_cache,
553 int regnum, int *optimizedp,
554 enum lval_type *lvalp, CORE_ADDR *addrp,
555 int *realnump, void *bufferp)
556 {
557 struct zpu_unwind_cache *info
558 = zpu_frame_unwind_cache (next_frame, this_prologue_cache);
559
560 trad_frame_prev_register (next_frame, info->saved_regs, regnum,
561 optimizedp, lvalp, addrp, realnump, bufferp);
562 }
563
564 static const struct frame_unwind zpu_frame_unwind = {
565 NORMAL_FRAME,
566 zpu_frame_this_id,
567 zpu_frame_prev_register
568 };
569
570 const struct frame_unwind *
zpu_frame_sniffer(struct frame_info * next_frame)571 zpu_frame_sniffer (struct frame_info *next_frame)
572 {
573 return &zpu_frame_unwind;
574 }
575
576 static CORE_ADDR
zpu_frame_base_address(struct frame_info * next_frame,void ** this_cache)577 zpu_frame_base_address (struct frame_info *next_frame, void **this_cache)
578 {
579 struct zpu_unwind_cache *info
580 = zpu_frame_unwind_cache (next_frame, this_cache);
581
582 return info->base;
583 }
584
585
586 static CORE_ADDR
zpu_frame_args_address(struct frame_info * next_frame,void ** this_cache)587 zpu_frame_args_address (struct frame_info *next_frame, void **this_cache)
588 {
589 CORE_ADDR addr;
590 struct zpu_unwind_cache *info
591 = zpu_frame_unwind_cache (next_frame, this_cache);
592
593 addr = info->base + info->savedSize + RETURN_ADDR_SIZE;
594
595 return addr;
596 }
597
598
599 static const struct frame_base zpu_frame_base = {
600 &zpu_frame_unwind,
601 zpu_frame_base_address,
602 zpu_frame_base_address,
603 zpu_frame_args_address
604 };
605
606
607 static struct gdbarch *
zpu_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)608 zpu_gdbarch_init (struct gdbarch_info info,
609 struct gdbarch_list *arches)
610 {
611 struct gdbarch *gdbarch;
612 struct gdbarch_tdep *tdep;
613 int elf_flags;
614
615 /* Extract the elf_flags if available. */
616 if (info.abfd != NULL
617 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
618 elf_flags = elf_elfheader (info.abfd)->e_flags;
619 else
620 elf_flags = 0;
621
622 /* try to find a pre-existing architecture */
623 for (arches = gdbarch_list_lookup_by_info (arches, &info);
624 arches != NULL;
625 arches = gdbarch_list_lookup_by_info (arches->next, &info))
626 {
627 if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
628 continue;
629
630 return arches->gdbarch;
631 }
632
633 /* Need a new architecture. Fill in a target specific vector. */
634 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
635 gdbarch = gdbarch_alloc (&info, tdep);
636 tdep->elf_flags = elf_flags;
637
638 switch (info.bfd_arch_info->arch)
639 {
640 case bfd_arch_zpu:
641 set_gdbarch_addr_bit (gdbarch, 32);
642 set_gdbarch_pc_regnum (gdbarch, HARD_PC_REGNUM);
643 set_gdbarch_num_regs (gdbarch, ZPU_NUM_REGS);
644 break;
645
646 default:
647 break;
648 }
649
650 set_gdbarch_short_bit (gdbarch, 16);
651 set_gdbarch_int_bit (gdbarch, 32);
652 set_gdbarch_float_bit (gdbarch, 32);
653 set_gdbarch_double_bit (gdbarch, 64 );
654 set_gdbarch_long_double_bit (gdbarch, 64);
655 set_gdbarch_long_bit (gdbarch, 32);
656 set_gdbarch_ptr_bit (gdbarch, 32);
657 set_gdbarch_long_long_bit (gdbarch, 64);
658
659 /* Characters are unsigned. */
660 set_gdbarch_char_signed (gdbarch, 0);
661
662 set_gdbarch_skip_prologue (gdbarch, zpu_skip_prologue);
663 set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
664
665 /* frame stuff */
666 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
667
668 set_gdbarch_unwind_pc (gdbarch, zpu_unwind_pc);
669 set_gdbarch_unwind_sp (gdbarch, zpu_unwind_sp);
670
671 /* register stuff */
672 set_gdbarch_sp_regnum (gdbarch, HARD_SP_REGNUM);
673 set_gdbarch_register_name (gdbarch, zpu_register_name);
674 set_gdbarch_register_type (gdbarch, zpu_register_type);
675
676 set_gdbarch_return_value (gdbarch, zpu_return_value);
677 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
678 set_gdbarch_breakpoint_from_pc (gdbarch, zpu_breakpoint_from_pc);
679 set_gdbarch_print_insn (gdbarch, gdb_print_insn_zpu);
680 return gdbarch;
681 }
682
683 extern initialize_file_ftype _initialize_zpu_tdep; /* -Wmissing-prototypes */
684
685 void
_initialize_zpu_tdep(void)686 _initialize_zpu_tdep (void)
687 {
688 register_gdbarch_init (bfd_arch_zpu, zpu_gdbarch_init);
689 }
690