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