Split breakpoint_from_pc to breakpoint_kind_from_pc and sw_breakpoint_from_kind
[external/binutils.git] / gdb / iq2000-tdep.c
1 /* Target-dependent code for the IQ2000 architecture, for GDB, the GNU
2    Debugger.
3
4    Copyright (C) 2000-2016 Free Software Foundation, Inc.
5
6    Contributed by Red Hat.
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 3 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, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "frame-base.h"
26 #include "frame-unwind.h"
27 #include "dwarf2-frame.h"
28 #include "gdbtypes.h"
29 #include "value.h"
30 #include "dis-asm.h"
31 #include "arch-utils.h"
32 #include "regcache.h"
33 #include "osabi.h"
34 #include "gdbcore.h"
35
36 enum gdb_regnum
37 {
38   E_R0_REGNUM,  E_R1_REGNUM,  E_R2_REGNUM,  E_R3_REGNUM, 
39   E_R4_REGNUM,  E_R5_REGNUM,  E_R6_REGNUM,  E_R7_REGNUM, 
40   E_R8_REGNUM,  E_R9_REGNUM,  E_R10_REGNUM, E_R11_REGNUM, 
41   E_R12_REGNUM, E_R13_REGNUM, E_R14_REGNUM, E_R15_REGNUM, 
42   E_R16_REGNUM, E_R17_REGNUM, E_R18_REGNUM, E_R19_REGNUM, 
43   E_R20_REGNUM, E_R21_REGNUM, E_R22_REGNUM, E_R23_REGNUM, 
44   E_R24_REGNUM, E_R25_REGNUM, E_R26_REGNUM, E_R27_REGNUM, 
45   E_R28_REGNUM, E_R29_REGNUM, E_R30_REGNUM, E_R31_REGNUM, 
46   E_PC_REGNUM, 
47   E_LR_REGNUM        = E_R31_REGNUM, /* Link register.  */
48   E_SP_REGNUM        = E_R29_REGNUM, /* Stack pointer.  */
49   E_FP_REGNUM        = E_R27_REGNUM, /* Frame pointer.  */
50   E_FN_RETURN_REGNUM = E_R2_REGNUM,  /* Function return value register.  */
51   E_1ST_ARGREG       = E_R4_REGNUM,  /* 1st  function arg register.  */
52   E_LAST_ARGREG      = E_R11_REGNUM, /* Last function arg register.  */
53   E_NUM_REGS         = E_PC_REGNUM + 1
54 };
55
56 /* Use an invalid address value as 'not available' marker.  */
57 enum { REG_UNAVAIL = (CORE_ADDR) -1 };
58
59 struct iq2000_frame_cache
60 {
61   /* Base address.  */
62   CORE_ADDR  base;
63   CORE_ADDR  pc;
64   LONGEST    framesize;
65   int        using_fp;
66   CORE_ADDR  saved_sp;
67   CORE_ADDR  saved_regs [E_NUM_REGS];
68 };
69
70 /* Harvard methods: */
71
72 static CORE_ADDR
73 insn_ptr_from_addr (CORE_ADDR addr)     /* CORE_ADDR to target pointer.  */
74 {
75   return addr & 0x7fffffffL;
76 }
77
78 static CORE_ADDR
79 insn_addr_from_ptr (CORE_ADDR ptr)      /* target_pointer to CORE_ADDR.  */
80 {
81   return (ptr & 0x7fffffffL) | 0x80000000L;
82 }
83
84 /* Function: pointer_to_address
85    Convert a target pointer to an address in host (CORE_ADDR) format.  */
86
87 static CORE_ADDR
88 iq2000_pointer_to_address (struct gdbarch *gdbarch,
89                            struct type * type, const gdb_byte * buf)
90 {
91   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
92   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
93   CORE_ADDR addr
94     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
95
96   if (target == TYPE_CODE_FUNC
97       || target == TYPE_CODE_METHOD
98       || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
99     addr = insn_addr_from_ptr (addr);
100
101   return addr;
102 }
103
104 /* Function: address_to_pointer
105    Convert a host-format address (CORE_ADDR) into a target pointer.  */
106
107 static void
108 iq2000_address_to_pointer (struct gdbarch *gdbarch,
109                            struct type *type, gdb_byte *buf, CORE_ADDR addr)
110 {
111   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
112   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
113
114   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
115     addr = insn_ptr_from_addr (addr);
116   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
117 }
118
119 /* Real register methods: */
120
121 /* Function: register_name
122    Returns the name of the iq2000 register number N.  */
123
124 static const char *
125 iq2000_register_name (struct gdbarch *gdbarch, int regnum)
126 {
127   static const char * names[E_NUM_REGS] =
128     {
129       "r0",  "r1",  "r2",  "r3",  "r4",
130       "r5",  "r6",  "r7",  "r8",  "r9",
131       "r10", "r11", "r12", "r13", "r14",
132       "r15", "r16", "r17", "r18", "r19",
133       "r20", "r21", "r22", "r23", "r24",
134       "r25", "r26", "r27", "r28", "r29",
135       "r30", "r31",
136       "pc"
137     };
138   if (regnum < 0 || regnum >= E_NUM_REGS)
139     return NULL;
140   return names[regnum];
141 }
142
143 /* Prologue analysis methods:  */
144
145 /* ADDIU insn (001001 rs(5) rt(5) imm(16)).  */
146 #define INSN_IS_ADDIU(X)        (((X) & 0xfc000000) == 0x24000000) 
147 #define ADDIU_REG_SRC(X)        (((X) & 0x03e00000) >> 21)
148 #define ADDIU_REG_TGT(X)        (((X) & 0x001f0000) >> 16)
149 #define ADDIU_IMMEDIATE(X)      ((signed short) ((X) & 0x0000ffff))
150
151 /* "MOVE" (OR) insn (000000 rs(5) rt(5) rd(5) 00000 100101).  */
152 #define INSN_IS_MOVE(X)         (((X) & 0xffe007ff) == 0x00000025)
153 #define MOVE_REG_SRC(X)         (((X) & 0x001f0000) >> 16)
154 #define MOVE_REG_TGT(X)         (((X) & 0x0000f800) >> 11)
155
156 /* STORE WORD insn (101011 rs(5) rt(5) offset(16)).  */
157 #define INSN_IS_STORE_WORD(X)   (((X) & 0xfc000000) == 0xac000000)
158 #define SW_REG_INDEX(X)         (((X) & 0x03e00000) >> 21)
159 #define SW_REG_SRC(X)           (((X) & 0x001f0000) >> 16)
160 #define SW_OFFSET(X)            ((signed short) ((X) & 0x0000ffff))
161
162 /* Function: find_last_line_symbol
163
164    Given an address range, first find a line symbol corresponding to
165    the starting address.  Then find the last line symbol within the 
166    range that has a line number less than or equal to the first line.
167
168    For optimized code with code motion, this finds the last address
169    for the lowest-numbered line within the address range.  */
170
171 static struct symtab_and_line
172 find_last_line_symbol (CORE_ADDR start, CORE_ADDR end, int notcurrent)
173 {
174   struct symtab_and_line sal = find_pc_line (start, notcurrent);
175   struct symtab_and_line best_sal = sal;
176
177   if (sal.pc == 0 || sal.line == 0 || sal.end == 0)
178     return sal;
179
180   do
181     {
182       if (sal.line && sal.line <= best_sal.line)
183         best_sal = sal;
184       sal = find_pc_line (sal.end, notcurrent);
185     }
186   while (sal.pc && sal.pc < end);
187
188   return best_sal;
189 }
190
191 /* Function: scan_prologue
192    Decode the instructions within the given address range.
193    Decide when we must have reached the end of the function prologue.
194    If a frame_info pointer is provided, fill in its prologue information.
195
196    Returns the address of the first instruction after the prologue.  */
197
198 static CORE_ADDR
199 iq2000_scan_prologue (struct gdbarch *gdbarch,
200                       CORE_ADDR scan_start,
201                       CORE_ADDR scan_end,
202                       struct frame_info *fi,
203                       struct iq2000_frame_cache *cache)
204 {
205   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
206   struct symtab_and_line sal;
207   CORE_ADDR pc;
208   CORE_ADDR loop_end;
209   int srcreg;
210   int tgtreg;
211   signed short offset;
212
213   if (scan_end == (CORE_ADDR) 0)
214     {
215       loop_end = scan_start + 100;
216       sal.end = sal.pc = 0;
217     }
218   else
219     {
220       loop_end = scan_end;
221       if (fi)
222         sal = find_last_line_symbol (scan_start, scan_end, 0);
223       else
224         sal.end = 0;    /* Avoid GCC false warning.  */
225     }
226
227   /* Saved registers:
228      We first have to save the saved register's offset, and 
229      only later do we compute its actual address.  Since the
230      offset can be zero, we must first initialize all the 
231      saved regs to minus one (so we can later distinguish 
232      between one that's not saved, and one that's saved at zero).  */
233   for (srcreg = 0; srcreg < E_NUM_REGS; srcreg ++)
234     cache->saved_regs[srcreg] = -1;
235   cache->using_fp = 0;
236   cache->framesize = 0;
237
238   for (pc = scan_start; pc < loop_end; pc += 4)
239     {
240       LONGEST insn = read_memory_unsigned_integer (pc, 4, byte_order);
241       /* Skip any instructions writing to (sp) or decrementing the
242          SP.  */
243       if ((insn & 0xffe00000) == 0xac200000)
244         {
245           /* sw using SP/%1 as base.  */
246           /* LEGACY -- from assembly-only port.  */
247           tgtreg = ((insn >> 16) & 0x1f);
248           if (tgtreg >= 0 && tgtreg < E_NUM_REGS)
249             cache->saved_regs[tgtreg] = -((signed short) (insn & 0xffff));
250
251           continue;
252         }
253
254       if ((insn & 0xffff8000) == 0x20218000)
255         {
256           /* addi %1, %1, -N == addi %sp, %sp, -N */
257           /* LEGACY -- from assembly-only port.  */
258           cache->framesize = -((signed short) (insn & 0xffff));
259           continue;
260         }
261
262       if (INSN_IS_ADDIU (insn))
263         {
264           srcreg = ADDIU_REG_SRC (insn);
265           tgtreg = ADDIU_REG_TGT (insn);
266           offset = ADDIU_IMMEDIATE (insn);
267           if (srcreg == E_SP_REGNUM && tgtreg == E_SP_REGNUM)
268             cache->framesize = -offset;
269           continue;
270         }
271
272       if (INSN_IS_STORE_WORD (insn))
273         {
274           srcreg = SW_REG_SRC (insn);
275           tgtreg = SW_REG_INDEX (insn);
276           offset = SW_OFFSET (insn);
277
278           if (tgtreg == E_SP_REGNUM || tgtreg == E_FP_REGNUM)
279             {
280               /* "push" to stack (via SP or FP reg).  */
281               if (cache->saved_regs[srcreg] == -1) /* Don't save twice.  */
282                 cache->saved_regs[srcreg] = offset;
283               continue;
284             }
285         }
286
287       if (INSN_IS_MOVE (insn))
288         {
289           srcreg = MOVE_REG_SRC (insn);
290           tgtreg = MOVE_REG_TGT (insn);
291
292           if (srcreg == E_SP_REGNUM && tgtreg == E_FP_REGNUM)
293             {
294               /* Copy sp to fp.  */
295               cache->using_fp = 1;
296               continue;
297             }
298         }
299
300       /* Unknown instruction encountered in frame.  Bail out?
301          1) If we have a subsequent line symbol, we can keep going.
302          2) If not, we need to bail out and quit scanning instructions.  */
303
304       if (fi && sal.end && (pc < sal.end)) /* Keep scanning.  */
305         continue;
306       else /* bail */
307         break;
308     }
309
310   return pc;
311 }
312
313 static void
314 iq2000_init_frame_cache (struct iq2000_frame_cache *cache)
315 {
316   int i;
317
318   cache->base = 0;
319   cache->framesize = 0;
320   cache->using_fp = 0;
321   cache->saved_sp = 0;
322   for (i = 0; i < E_NUM_REGS; i++)
323     cache->saved_regs[i] = -1;
324 }
325
326 /* Function: iq2000_skip_prologue
327    If the input address is in a function prologue, 
328    returns the address of the end of the prologue;
329    else returns the input address.
330
331    Note: the input address is likely to be the function start, 
332    since this function is mainly used for advancing a breakpoint
333    to the first line, or stepping to the first line when we have
334    stepped into a function call.  */
335
336 static CORE_ADDR
337 iq2000_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
338 {
339   CORE_ADDR func_addr = 0 , func_end = 0;
340
341   if (find_pc_partial_function (pc, NULL, & func_addr, & func_end))
342     {
343       struct symtab_and_line sal;
344       struct iq2000_frame_cache cache;
345
346       /* Found a function.  */
347       sal = find_pc_line (func_addr, 0);
348       if (sal.end && sal.end < func_end)
349         /* Found a line number, use it as end of prologue.  */
350         return sal.end;
351
352       /* No useable line symbol.  Use prologue parsing method.  */
353       iq2000_init_frame_cache (&cache);
354       return iq2000_scan_prologue (gdbarch, func_addr, func_end, NULL, &cache);
355     }
356
357   /* No function symbol -- just return the PC.  */
358   return (CORE_ADDR) pc;
359 }
360
361 static struct iq2000_frame_cache *
362 iq2000_frame_cache (struct frame_info *this_frame, void **this_cache)
363 {
364   struct gdbarch *gdbarch = get_frame_arch (this_frame);
365   struct iq2000_frame_cache *cache;
366   CORE_ADDR current_pc;
367   int i;
368
369   if (*this_cache)
370     return (struct iq2000_frame_cache *) *this_cache;
371
372   cache = FRAME_OBSTACK_ZALLOC (struct iq2000_frame_cache);
373   iq2000_init_frame_cache (cache);
374   *this_cache = cache;
375
376   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
377
378   current_pc = get_frame_pc (this_frame);
379   find_pc_partial_function (current_pc, NULL, &cache->pc, NULL);
380   if (cache->pc != 0)
381     iq2000_scan_prologue (gdbarch, cache->pc, current_pc, this_frame, cache);
382   if (!cache->using_fp)
383     cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
384
385   cache->saved_sp = cache->base + cache->framesize;
386
387   for (i = 0; i < E_NUM_REGS; i++)
388     if (cache->saved_regs[i] != -1)
389       cache->saved_regs[i] += cache->base;
390
391   return cache;
392 }
393
394 static struct value *
395 iq2000_frame_prev_register (struct frame_info *this_frame, void **this_cache,
396                             int regnum)
397 {
398   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
399                                                          this_cache);
400
401   if (regnum == E_SP_REGNUM && cache->saved_sp)
402     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
403
404   if (regnum == E_PC_REGNUM)
405     regnum = E_LR_REGNUM;
406
407   if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != -1)
408     return frame_unwind_got_memory (this_frame, regnum,
409                                     cache->saved_regs[regnum]);
410
411   return frame_unwind_got_register (this_frame, regnum, regnum);
412 }
413
414 static void
415 iq2000_frame_this_id (struct frame_info *this_frame, void **this_cache,
416                       struct frame_id *this_id)
417 {
418   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
419                                                          this_cache);
420
421   /* This marks the outermost frame.  */
422   if (cache->base == 0) 
423     return;
424
425   *this_id = frame_id_build (cache->saved_sp, cache->pc);
426 }
427
428 static const struct frame_unwind iq2000_frame_unwind = {
429   NORMAL_FRAME,
430   default_frame_unwind_stop_reason,
431   iq2000_frame_this_id,
432   iq2000_frame_prev_register,
433   NULL,
434   default_frame_sniffer
435 };
436
437 static CORE_ADDR
438 iq2000_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
439 {
440   return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
441 }   
442
443 static CORE_ADDR
444 iq2000_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
445 {
446   return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
447 }
448
449 static struct frame_id
450 iq2000_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
451 {
452   CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
453   return frame_id_build (sp, get_frame_pc (this_frame));
454 }
455
456 static CORE_ADDR
457 iq2000_frame_base_address (struct frame_info *this_frame, void **this_cache)
458 {
459   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
460                                                          this_cache);
461
462   return cache->base;
463 }
464   
465 static const struct frame_base iq2000_frame_base = {
466   &iq2000_frame_unwind,
467   iq2000_frame_base_address,
468   iq2000_frame_base_address, 
469   iq2000_frame_base_address
470 };
471
472 static int
473 iq2000_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
474 {
475   if ((*pcptr & 3) != 0)
476     error (_("breakpoint_from_pc: invalid breakpoint address 0x%lx"),
477            (long) *pcptr);
478
479   return 4;
480 }
481
482 static const gdb_byte *
483 iq2000_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
484 {
485   static const unsigned char big_breakpoint[] = { 0x00, 0x00, 0x00, 0x0d };
486   static const unsigned char little_breakpoint[] = { 0x0d, 0x00, 0x00, 0x00 };
487   *size = kind;
488
489   return (gdbarch_byte_order (gdbarch)
490           == BFD_ENDIAN_BIG) ? big_breakpoint : little_breakpoint;
491 }
492
493 GDBARCH_BREAKPOINT_FROM_PC (iq2000)
494
495 /* Target function return value methods: */
496
497 /* Function: store_return_value
498    Copy the function return value from VALBUF into the 
499    proper location for a function return.  */
500
501 static void
502 iq2000_store_return_value (struct type *type, struct regcache *regcache,
503                            const void *valbuf)
504 {
505   int len = TYPE_LENGTH (type);
506   int regno = E_FN_RETURN_REGNUM;
507
508   while (len > 0)
509     {
510       gdb_byte buf[4];
511       int size = len % 4 ?: 4;
512
513       memset (buf, 0, 4);
514       memcpy (buf + 4 - size, valbuf, size);
515       regcache_raw_write (regcache, regno++, buf);
516       len -= size;
517       valbuf = ((char *) valbuf) + size;
518     }
519 }
520
521 /* Function: use_struct_convention 
522    Returns non-zero if the given struct type will be returned using
523    a special convention, rather than the normal function return method.  */
524
525 static int
526 iq2000_use_struct_convention (struct type *type)
527 {
528   return ((TYPE_CODE (type) == TYPE_CODE_STRUCT)
529           || (TYPE_CODE (type) == TYPE_CODE_UNION))
530          && TYPE_LENGTH (type) > 8;
531 }
532
533 /* Function: extract_return_value
534    Copy the function's return value into VALBUF. 
535    This function is called only in the context of "target function calls",
536    ie. when the debugger forces a function to be called in the child, and
537    when the debugger forces a function to return prematurely via the
538    "return" command.  */
539
540 static void
541 iq2000_extract_return_value (struct type *type, struct regcache *regcache,
542                              gdb_byte *valbuf)
543 {
544   struct gdbarch *gdbarch = get_regcache_arch (regcache);
545   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
546
547   /* If the function's return value is 8 bytes or less, it is
548      returned in a register, and if larger than 8 bytes, it is 
549      returned in a stack location which is pointed to by the same
550      register.  */
551   int len = TYPE_LENGTH (type);
552
553   if (len <= (2 * 4))
554     {
555       int regno = E_FN_RETURN_REGNUM;
556
557       /* Return values of <= 8 bytes are returned in 
558          FN_RETURN_REGNUM.  */
559       while (len > 0)
560         {
561           ULONGEST tmp;
562           int size = len % 4 ?: 4;
563
564           /* By using store_unsigned_integer we avoid having to
565              do anything special for small big-endian values.  */
566           regcache_cooked_read_unsigned (regcache, regno++, &tmp);
567           store_unsigned_integer (valbuf, size, byte_order, tmp);
568           len -= size;
569           valbuf += size;
570         }
571     }
572   else
573     {
574       /* Return values > 8 bytes are returned in memory,
575          pointed to by FN_RETURN_REGNUM.  */
576       ULONGEST return_buffer;
577       regcache_cooked_read_unsigned (regcache, E_FN_RETURN_REGNUM,
578                                      &return_buffer);
579       read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
580     }
581 }
582
583 static enum return_value_convention
584 iq2000_return_value (struct gdbarch *gdbarch, struct value *function,
585                      struct type *type, struct regcache *regcache,
586                      gdb_byte *readbuf, const gdb_byte *writebuf)
587 {
588   if (iq2000_use_struct_convention (type))
589     return RETURN_VALUE_STRUCT_CONVENTION;
590   if (writebuf)
591     iq2000_store_return_value (type, regcache, writebuf);
592   else if (readbuf)
593     iq2000_extract_return_value (type, regcache, readbuf);
594   return RETURN_VALUE_REGISTER_CONVENTION;
595 }
596
597 /* Function: register_virtual_type
598    Returns the default type for register N.  */
599
600 static struct type *
601 iq2000_register_type (struct gdbarch *gdbarch, int regnum)
602 {
603   return builtin_type (gdbarch)->builtin_int32;
604 }
605
606 static CORE_ADDR
607 iq2000_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
608 {
609   /* This is the same frame alignment used by gcc.  */
610   return ((sp + 7) & ~7);
611 }
612
613 /* Convenience function to check 8-byte types for being a scalar type
614    or a struct with only one long long or double member.  */
615 static int
616 iq2000_pass_8bytetype_by_address (struct type *type)
617 {
618   struct type *ftype;
619
620   /* Skip typedefs.  */
621   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
622     type = TYPE_TARGET_TYPE (type);
623   /* Non-struct and non-union types are always passed by value.  */
624   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
625       && TYPE_CODE (type) != TYPE_CODE_UNION)
626     return 0;
627   /* Structs with more than 1 field are always passed by address.  */
628   if (TYPE_NFIELDS (type) != 1)
629     return 1;
630   /* Get field type.  */
631   ftype = (TYPE_FIELDS (type))[0].type;
632   /* The field type must have size 8, otherwise pass by address.  */
633   if (TYPE_LENGTH (ftype) != 8)
634     return 1;
635   /* Skip typedefs of field type.  */
636   while (TYPE_CODE (ftype) == TYPE_CODE_TYPEDEF)
637     ftype = TYPE_TARGET_TYPE (ftype);
638   /* If field is int or float, pass by value.  */
639   if (TYPE_CODE (ftype) == TYPE_CODE_FLT
640       || TYPE_CODE (ftype) == TYPE_CODE_INT)
641     return 0;
642   /* Everything else, pass by address.  */
643   return 1;
644 }
645
646 static CORE_ADDR
647 iq2000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
648                         struct regcache *regcache, CORE_ADDR bp_addr,
649                         int nargs, struct value **args, CORE_ADDR sp,
650                         int struct_return, CORE_ADDR struct_addr)
651 {
652   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
653   const bfd_byte *val;
654   bfd_byte buf[4];
655   struct type *type;
656   int i, argreg, typelen, slacklen;
657   int stackspace = 0;
658   /* Used to copy struct arguments into the stack.  */
659   CORE_ADDR struct_ptr;
660
661   /* First determine how much stack space we will need.  */
662   for (i = 0, argreg = E_1ST_ARGREG + (struct_return != 0); i < nargs; i++)
663     {
664       type = value_type (args[i]);
665       typelen = TYPE_LENGTH (type);
666       if (typelen <= 4)
667         {
668           /* Scalars of up to 4 bytes, 
669              structs of up to 4 bytes, and
670              pointers.  */
671           if (argreg <= E_LAST_ARGREG)
672             argreg++;
673           else
674             stackspace += 4;
675         }
676       else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type))
677         {
678           /* long long, 
679              double, and possibly
680              structs with a single field of long long or double.  */
681           if (argreg <= E_LAST_ARGREG - 1)
682             {
683               /* 8-byte arg goes into a register pair
684                  (must start with an even-numbered reg).  */
685               if (((argreg - E_1ST_ARGREG) % 2) != 0)
686                 argreg ++;
687               argreg += 2;
688             }
689           else
690             {
691               argreg = E_LAST_ARGREG + 1;       /* no more argregs.  */
692               /* 8-byte arg goes on stack, must be 8-byte aligned.  */
693               stackspace = ((stackspace + 7) & ~7);
694               stackspace += 8;
695             }
696         }
697       else
698         {
699           /* Structs are passed as pointer to a copy of the struct.
700              So we need room on the stack for a copy of the struct
701              plus for the argument pointer.  */
702           if (argreg <= E_LAST_ARGREG)
703             argreg++;
704           else
705             stackspace += 4;
706           /* Care for 8-byte alignment of structs saved on stack.  */
707           stackspace += ((typelen + 7) & ~7);
708         }
709     }
710
711   /* Now copy params, in ascending order, into their assigned location
712      (either in a register or on the stack).  */
713
714   sp -= (sp % 8);       /* align */
715   struct_ptr = sp;
716   sp -= stackspace;
717   sp -= (sp % 8);       /* align again */
718   stackspace = 0;
719
720   argreg = E_1ST_ARGREG;
721   if (struct_return)
722     {
723       /* A function that returns a struct will consume one argreg to do so.
724        */
725       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
726     }
727
728   for (i = 0; i < nargs; i++)
729     {
730       type = value_type (args[i]);
731       typelen = TYPE_LENGTH (type);
732       val = value_contents (args[i]);
733       if (typelen <= 4)
734         {
735           /* Char, short, int, float, pointer, and structs <= four bytes.  */
736           slacklen = (4 - (typelen % 4)) % 4;
737           memset (buf, 0, sizeof (buf));
738           memcpy (buf + slacklen, val, typelen);
739           if (argreg <= E_LAST_ARGREG)
740             {
741               /* Passed in a register.  */
742               regcache_raw_write (regcache, argreg++, buf);
743             }
744           else
745             {
746               /* Passed on the stack.  */
747               write_memory (sp + stackspace, buf, 4);
748               stackspace += 4;
749             }
750         }
751       else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type))
752         {
753           /* (long long), (double), or struct consisting of 
754              a single (long long) or (double).  */
755           if (argreg <= E_LAST_ARGREG - 1)
756             {
757               /* 8-byte arg goes into a register pair
758                  (must start with an even-numbered reg).  */
759               if (((argreg - E_1ST_ARGREG) % 2) != 0)
760                 argreg++;
761               regcache_raw_write (regcache, argreg++, val);
762               regcache_raw_write (regcache, argreg++, val + 4);
763             }
764           else
765             {
766               /* 8-byte arg goes on stack, must be 8-byte aligned.  */
767               argreg = E_LAST_ARGREG + 1;       /* no more argregs.  */
768               stackspace = ((stackspace + 7) & ~7);
769               write_memory (sp + stackspace, val, typelen);
770               stackspace += 8;
771             }
772         }
773       else
774         {
775           /* Store struct beginning at the upper end of the previously
776              computed stack space.  Then store the address of the struct
777              using the usual rules for a 4 byte value.  */
778           struct_ptr -= ((typelen + 7) & ~7);
779           write_memory (struct_ptr, val, typelen);
780           if (argreg <= E_LAST_ARGREG)
781             regcache_cooked_write_unsigned (regcache, argreg++, struct_ptr);
782           else
783             {
784               store_unsigned_integer (buf, 4, byte_order, struct_ptr);
785               write_memory (sp + stackspace, buf, 4);
786               stackspace += 4;
787             }
788         }
789     }
790
791   /* Store return address.  */
792   regcache_cooked_write_unsigned (regcache, E_LR_REGNUM, bp_addr);
793
794   /* Update stack pointer.  */
795   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
796
797   /* And that should do it.  Return the new stack pointer.  */
798   return sp;
799 }
800
801 /* Function: gdbarch_init
802    Initializer function for the iq2000 gdbarch vector.
803    Called by gdbarch.  Sets up the gdbarch vector(s) for this target.  */
804
805 static struct gdbarch *
806 iq2000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
807 {
808   struct gdbarch *gdbarch;
809
810   /* Look up list for candidates - only one.  */
811   arches = gdbarch_list_lookup_by_info (arches, &info);
812   if (arches != NULL)
813     return arches->gdbarch;
814
815   gdbarch = gdbarch_alloc (&info, NULL);
816
817   set_gdbarch_num_regs             (gdbarch, E_NUM_REGS);
818   set_gdbarch_num_pseudo_regs      (gdbarch, 0);
819   set_gdbarch_sp_regnum            (gdbarch, E_SP_REGNUM);
820   set_gdbarch_pc_regnum            (gdbarch, E_PC_REGNUM);
821   set_gdbarch_register_name        (gdbarch, iq2000_register_name);
822   set_gdbarch_address_to_pointer   (gdbarch, iq2000_address_to_pointer);
823   set_gdbarch_pointer_to_address   (gdbarch, iq2000_pointer_to_address);
824   set_gdbarch_ptr_bit              (gdbarch, 4 * TARGET_CHAR_BIT);
825   set_gdbarch_short_bit            (gdbarch, 2 * TARGET_CHAR_BIT);
826   set_gdbarch_int_bit              (gdbarch, 4 * TARGET_CHAR_BIT);
827   set_gdbarch_long_bit             (gdbarch, 4 * TARGET_CHAR_BIT);
828   set_gdbarch_long_long_bit        (gdbarch, 8 * TARGET_CHAR_BIT);
829   set_gdbarch_float_bit            (gdbarch, 4 * TARGET_CHAR_BIT);
830   set_gdbarch_double_bit           (gdbarch, 8 * TARGET_CHAR_BIT);
831   set_gdbarch_long_double_bit      (gdbarch, 8 * TARGET_CHAR_BIT);
832   set_gdbarch_float_format         (gdbarch, floatformats_ieee_single);
833   set_gdbarch_double_format        (gdbarch, floatformats_ieee_double);
834   set_gdbarch_long_double_format   (gdbarch, floatformats_ieee_double);
835   set_gdbarch_return_value         (gdbarch, iq2000_return_value);
836   SET_GDBARCH_BREAKPOINT_MANIPULATION (iq2000);
837   set_gdbarch_frame_args_skip      (gdbarch, 0);
838   set_gdbarch_skip_prologue        (gdbarch, iq2000_skip_prologue);
839   set_gdbarch_inner_than           (gdbarch, core_addr_lessthan);
840   set_gdbarch_print_insn           (gdbarch, print_insn_iq2000);
841   set_gdbarch_register_type (gdbarch, iq2000_register_type);
842   set_gdbarch_frame_align (gdbarch, iq2000_frame_align);
843   set_gdbarch_unwind_sp (gdbarch, iq2000_unwind_sp);
844   set_gdbarch_unwind_pc (gdbarch, iq2000_unwind_pc);
845   set_gdbarch_dummy_id (gdbarch, iq2000_dummy_id);
846   frame_base_set_default (gdbarch, &iq2000_frame_base);
847   set_gdbarch_push_dummy_call (gdbarch, iq2000_push_dummy_call);
848
849   gdbarch_init_osabi (info, gdbarch);
850
851   dwarf2_append_unwinders (gdbarch);
852   frame_unwind_append_unwinder (gdbarch, &iq2000_frame_unwind);
853
854   return gdbarch;
855 }
856
857 /* Function: _initialize_iq2000_tdep
858    Initializer function for the iq2000 module.
859    Called by gdb at start-up.  */
860
861 /* Provide a prototype to silence -Wmissing-prototypes.  */
862 extern initialize_file_ftype _initialize_iq2000_tdep;
863
864 void
865 _initialize_iq2000_tdep (void)
866 {
867   register_gdbarch_init (bfd_arch_iq2000, iq2000_gdbarch_init);
868 }