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