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