]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cris-tdep.c
2003-06-11 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / cris-tdep.c
CommitLineData
29134980 1/* Target dependent code for CRIS, for GDB, the GNU debugger.
51603483 2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
29134980
OF
3 Contributed by Axis Communications AB.
4 Written by Hendrik Ruijter, Stefan Andersson, and Orjan Friberg.
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "frame.h"
24#include "symtab.h"
25#include "inferior.h"
26#include "gdbtypes.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "target.h"
30#include "value.h"
31#include "opcode/cris.h"
32#include "arch-utils.h"
33#include "regcache.h"
34
35/* To get entry_point_address. */
36#include "symfile.h"
37
dbbff683
OF
38#include "solib.h" /* Support for shared libraries. */
39#include "solib-svr4.h" /* For struct link_map_offsets. */
9bbe19fb 40#include "gdb_string.h"
dbbff683
OF
41
42
29134980
OF
43enum cris_num_regs
44{
45 /* There are no floating point registers. Used in gdbserver low-linux.c. */
46 NUM_FREGS = 0,
47
48 /* There are 16 general registers. */
49 NUM_GENREGS = 16,
50
51 /* There are 16 special registers. */
52 NUM_SPECREGS = 16
53};
54
55/* Register numbers of various important registers.
0ba6dca9 56 DEPRECATED_FP_REGNUM Contains address of executing stack frame.
29134980
OF
57 STR_REGNUM Contains the address of structure return values.
58 RET_REGNUM Contains the return value when shorter than or equal to 32 bits
59 ARG1_REGNUM Contains the first parameter to a function.
60 ARG2_REGNUM Contains the second parameter to a function.
61 ARG3_REGNUM Contains the third parameter to a function.
62 ARG4_REGNUM Contains the fourth parameter to a function. Rest on stack.
63 SP_REGNUM Contains address of top of stack.
64 PC_REGNUM Contains address of next instruction.
65 SRP_REGNUM Subroutine return pointer register.
66 BRP_REGNUM Breakpoint return pointer register. */
67
0ba6dca9
AC
68/* DEPRECATED_FP_REGNUM = 8, SP_REGNUM = 14, and PC_REGNUM = 15 have
69 been incorporated into the multi-arch framework. */
29134980
OF
70
71enum cris_regnums
72{
73 /* Enums with respect to the general registers, valid for all
74 CRIS versions. */
75 STR_REGNUM = 9,
76 RET_REGNUM = 10,
77 ARG1_REGNUM = 10,
78 ARG2_REGNUM = 11,
79 ARG3_REGNUM = 12,
80 ARG4_REGNUM = 13,
81
82 /* Enums with respect to the special registers, some of which may not be
83 applicable to all CRIS versions. */
84 P0_REGNUM = 16,
85 VR_REGNUM = 17,
86 P2_REGNUM = 18,
87 P3_REGNUM = 19,
88 P4_REGNUM = 20,
89 CCR_REGNUM = 21,
90 MOF_REGNUM = 23,
91 P8_REGNUM = 24,
92 IBR_REGNUM = 25,
93 IRP_REGNUM = 26,
94 SRP_REGNUM = 27,
95 BAR_REGNUM = 28,
2a9ecef2 96 DCCR_REGNUM = 29,
29134980
OF
97 BRP_REGNUM = 30,
98 USP_REGNUM = 31
99};
100
101extern const struct cris_spec_reg cris_spec_regs[];
102
103/* CRIS version, set via the user command 'set cris-version'. Affects
104 register names and sizes.*/
105static int usr_cmd_cris_version;
106
107/* Indicates whether to trust the above variable. */
108static int usr_cmd_cris_version_valid = 0;
109
110/* CRIS mode, set via the user command 'set cris-mode'. Affects availability
111 of some registers. */
112static const char *usr_cmd_cris_mode;
113
114/* Indicates whether to trust the above variable. */
115static int usr_cmd_cris_mode_valid = 0;
116
117static const char CRIS_MODE_USER[] = "CRIS_MODE_USER";
118static const char CRIS_MODE_SUPERVISOR[] = "CRIS_MODE_SUPERVISOR";
119static const char *cris_mode_enums[] =
120{
121 CRIS_MODE_USER,
122 CRIS_MODE_SUPERVISOR,
123 0
124};
125
126/* CRIS ABI, set via the user command 'set cris-abi'.
127 There are two flavours:
128 1. Original ABI with 32-bit doubles, where arguments <= 4 bytes are
129 passed by value.
130 2. New ABI with 64-bit doubles, where arguments <= 8 bytes are passed by
131 value. */
132static const char *usr_cmd_cris_abi;
133
134/* Indicates whether to trust the above variable. */
135static int usr_cmd_cris_abi_valid = 0;
136
137/* These variables are strings instead of enums to make them usable as
138 parameters to add_set_enum_cmd. */
139static const char CRIS_ABI_ORIGINAL[] = "CRIS_ABI_ORIGINAL";
140static const char CRIS_ABI_V2[] = "CRIS_ABI_V2";
141static const char CRIS_ABI_SYMBOL[] = ".$CRIS_ABI_V2";
142static const char *cris_abi_enums[] =
143{
144 CRIS_ABI_ORIGINAL,
145 CRIS_ABI_V2,
146 0
147};
148
149/* CRIS architecture specific information. */
150struct gdbarch_tdep
151{
152 int cris_version;
153 const char *cris_mode;
154 const char *cris_abi;
155};
156
157/* Functions for accessing target dependent data. */
158
159static int
160cris_version (void)
161{
162 return (gdbarch_tdep (current_gdbarch)->cris_version);
163}
164
165static const char *
166cris_mode (void)
167{
168 return (gdbarch_tdep (current_gdbarch)->cris_mode);
169}
170
171static const char *
172cris_abi (void)
173{
174 return (gdbarch_tdep (current_gdbarch)->cris_abi);
175}
176
177/* For saving call-clobbered contents in R9 when returning structs. */
178static CORE_ADDR struct_return_address;
179
180struct frame_extra_info
181{
182 CORE_ADDR return_pc;
183 int leaf_function;
184};
185
186/* The instruction environment needed to find single-step breakpoints. */
187typedef
188struct instruction_environment
189{
190 unsigned long reg[NUM_GENREGS];
191 unsigned long preg[NUM_SPECREGS];
192 unsigned long branch_break_address;
193 unsigned long delay_slot_pc;
194 unsigned long prefix_value;
195 int branch_found;
196 int prefix_found;
197 int invalid;
198 int slot_needed;
199 int delay_slot_pc_active;
200 int xflag_found;
201 int disable_interrupt;
202} inst_env_type;
203
204/* Save old breakpoints in order to restore the state before a single_step.
205 At most, two breakpoints will have to be remembered. */
206typedef
207char binsn_quantum[BREAKPOINT_MAX];
208static binsn_quantum break_mem[2];
209static CORE_ADDR next_pc = 0;
210static CORE_ADDR branch_target_address = 0;
211static unsigned char branch_break_inserted = 0;
212
213/* Machine-dependencies in CRIS for opcodes. */
214
215/* Instruction sizes. */
216enum cris_instruction_sizes
217{
218 INST_BYTE_SIZE = 0,
219 INST_WORD_SIZE = 1,
220 INST_DWORD_SIZE = 2
221};
222
223/* Addressing modes. */
224enum cris_addressing_modes
225{
226 REGISTER_MODE = 1,
227 INDIRECT_MODE = 2,
228 AUTOINC_MODE = 3
229};
230
231/* Prefix addressing modes. */
232enum cris_prefix_addressing_modes
233{
234 PREFIX_INDEX_MODE = 2,
235 PREFIX_ASSIGN_MODE = 3,
236
237 /* Handle immediate byte offset addressing mode prefix format. */
238 PREFIX_OFFSET_MODE = 2
239};
240
241/* Masks for opcodes. */
242enum cris_opcode_masks
243{
244 BRANCH_SIGNED_SHORT_OFFSET_MASK = 0x1,
245 SIGNED_EXTEND_BIT_MASK = 0x2,
246 SIGNED_BYTE_MASK = 0x80,
247 SIGNED_BYTE_EXTEND_MASK = 0xFFFFFF00,
248 SIGNED_WORD_MASK = 0x8000,
249 SIGNED_WORD_EXTEND_MASK = 0xFFFF0000,
250 SIGNED_DWORD_MASK = 0x80000000,
251 SIGNED_QUICK_VALUE_MASK = 0x20,
252 SIGNED_QUICK_VALUE_EXTEND_MASK = 0xFFFFFFC0
253};
254
255/* Functions for opcodes. The general form of the ETRAX 16-bit instruction:
256 Bit 15 - 12 Operand2
257 11 - 10 Mode
258 9 - 6 Opcode
259 5 - 4 Size
260 3 - 0 Operand1 */
261
262static int
263cris_get_operand2 (unsigned short insn)
264{
265 return ((insn & 0xF000) >> 12);
266}
267
268static int
269cris_get_mode (unsigned short insn)
270{
271 return ((insn & 0x0C00) >> 10);
272}
273
274static int
275cris_get_opcode (unsigned short insn)
276{
277 return ((insn & 0x03C0) >> 6);
278}
279
280static int
281cris_get_size (unsigned short insn)
282{
283 return ((insn & 0x0030) >> 4);
284}
285
286static int
287cris_get_operand1 (unsigned short insn)
288{
289 return (insn & 0x000F);
290}
291
292/* Additional functions in order to handle opcodes. */
293
294static int
295cris_get_wide_opcode (unsigned short insn)
296{
297 return ((insn & 0x03E0) >> 5);
298}
299
300static int
301cris_get_short_size (unsigned short insn)
302{
303 return ((insn & 0x0010) >> 4);
304}
305
306static int
307cris_get_quick_value (unsigned short insn)
308{
309 return (insn & 0x003F);
310}
311
312static int
313cris_get_bdap_quick_offset (unsigned short insn)
314{
315 return (insn & 0x00FF);
316}
317
318static int
319cris_get_branch_short_offset (unsigned short insn)
320{
321 return (insn & 0x00FF);
322}
323
324static int
325cris_get_asr_shift_steps (unsigned long value)
326{
327 return (value & 0x3F);
328}
329
330static int
331cris_get_asr_quick_shift_steps (unsigned short insn)
332{
333 return (insn & 0x1F);
334}
335
336static int
337cris_get_clear_size (unsigned short insn)
338{
339 return ((insn) & 0xC000);
340}
341
342static int
343cris_is_signed_extend_bit_on (unsigned short insn)
344{
345 return (((insn) & 0x20) == 0x20);
346}
347
348static int
349cris_is_xflag_bit_on (unsigned short insn)
350{
351 return (((insn) & 0x1000) == 0x1000);
352}
353
354static void
355cris_set_size_to_dword (unsigned short *insn)
356{
357 *insn &= 0xFFCF;
358 *insn |= 0x20;
359}
360
8535cb38 361static signed char
29134980
OF
362cris_get_signed_offset (unsigned short insn)
363{
8535cb38 364 return ((signed char) (insn & 0x00FF));
29134980
OF
365}
366
367/* Calls an op function given the op-type, working on the insn and the
368 inst_env. */
369static void cris_gdb_func (enum cris_op_type, unsigned short, inst_env_type *);
370
371static CORE_ADDR cris_skip_prologue_main (CORE_ADDR pc, int frameless_p);
372
373static struct gdbarch *cris_gdbarch_init (struct gdbarch_info,
374 struct gdbarch_list *);
375
376static int cris_delayed_get_disassembler (bfd_vma, disassemble_info *);
377
378static void cris_dump_tdep (struct gdbarch *, struct ui_file *);
379
380static void cris_version_update (char *ignore_args, int from_tty,
381 struct cmd_list_element *c);
382
383static void cris_mode_update (char *ignore_args, int from_tty,
384 struct cmd_list_element *c);
385
386static void cris_abi_update (char *ignore_args, int from_tty,
387 struct cmd_list_element *c);
388
389static CORE_ADDR bfd_lookup_symbol (bfd *, const char *);
390
391/* Frames information. The definition of the struct frame_info is
392
393 CORE_ADDR frame
394 CORE_ADDR pc
5a203e44 395 enum frame_type type;
29134980
OF
396 CORE_ADDR return_pc
397 int leaf_function
398
399 If the compilation option -fno-omit-frame-pointer is present the
400 variable frame will be set to the content of R8 which is the frame
401 pointer register.
402
403 The variable pc contains the address where execution is performed
404 in the present frame. The innermost frame contains the current content
405 of the register PC. All other frames contain the content of the
406 register PC in the next frame.
407
5a203e44
AC
408 The variable `type' indicates the frame's type: normal, SIGTRAMP
409 (associated with a signal handler), dummy (associated with a dummy
410 frame).
29134980
OF
411
412 The variable return_pc contains the address where execution should be
413 resumed when the present frame has finished, the return address.
414
415 The variable leaf_function is 1 if the return address is in the register
416 SRP, and 0 if it is on the stack.
417
418 Prologue instructions C-code.
419 The prologue may consist of (-fno-omit-frame-pointer)
420 1) 2)
421 push srp
422 push r8 push r8
423 move.d sp,r8 move.d sp,r8
424 subq X,sp subq X,sp
425 movem rY,[sp] movem rY,[sp]
426 move.S rZ,[r8-U] move.S rZ,[r8-U]
427
428 where 1 is a non-terminal function, and 2 is a leaf-function.
429
430 Note that this assumption is extremely brittle, and will break at the
431 slightest change in GCC's prologue.
432
433 If local variables are declared or register contents are saved on stack
434 the subq-instruction will be present with X as the number of bytes
435 needed for storage. The reshuffle with respect to r8 may be performed
436 with any size S (b, w, d) and any of the general registers Z={0..13}.
437 The offset U should be representable by a signed 8-bit value in all cases.
438 Thus, the prefix word is assumed to be immediate byte offset mode followed
439 by another word containing the instruction.
440
441 Degenerate cases:
442 3)
443 push r8
444 move.d sp,r8
445 move.d r8,sp
446 pop r8
447
448 Prologue instructions C++-code.
449 Case 1) and 2) in the C-code may be followed by
450
451 move.d r10,rS ; this
452 move.d r11,rT ; P1
453 move.d r12,rU ; P2
454 move.d r13,rV ; P3
455 move.S [r8+U],rZ ; P4
456
457 if any of the call parameters are stored. The host expects these
458 instructions to be executed in order to get the call parameters right. */
459
460/* Examine the prologue of a function. The variable ip is the address of
461 the first instruction of the prologue. The variable limit is the address
462 of the first instruction after the prologue. The variable fi contains the
463 information in struct frame_info. The variable frameless_p controls whether
464 the entire prologue is examined (0) or just enough instructions to
465 determine that it is a prologue (1). */
466
a78f21af 467static CORE_ADDR
29134980
OF
468cris_examine (CORE_ADDR ip, CORE_ADDR limit, struct frame_info *fi,
469 int frameless_p)
470{
471 /* Present instruction. */
472 unsigned short insn;
473
474 /* Next instruction, lookahead. */
475 unsigned short insn_next;
476 int regno;
477
478 /* Is there a push fp? */
479 int have_fp;
480
481 /* Number of byte on stack used for local variables and movem. */
482 int val;
483
484 /* Highest register number in a movem. */
485 int regsave;
486
487 /* move.d r<source_register>,rS */
488 short source_register;
489
490 /* This frame is with respect to a leaf until a push srp is found. */
da50a4b7 491 get_frame_extra_info (fi)->leaf_function = 1;
29134980
OF
492
493 /* This frame is without the FP until a push fp is found. */
494 have_fp = 0;
495
496 /* Assume nothing on stack. */
497 val = 0;
498 regsave = -1;
499
500 /* No information about register contents so far. */
501
502 /* We only want to know the end of the prologue when fi->saved_regs == 0.
503 When the saved registers are allocated full information is required. */
b2fb4676 504 if (get_frame_saved_regs (fi))
29134980
OF
505 {
506 for (regno = 0; regno < NUM_REGS; regno++)
b2fb4676 507 get_frame_saved_regs (fi)[regno] = 0;
29134980
OF
508 }
509
510 /* Find the prologue instructions. */
511 do
512 {
513 insn = read_memory_unsigned_integer (ip, sizeof (short));
514 ip += sizeof (short);
515 if (insn == 0xE1FC)
516 {
517 /* push <reg> 32 bit instruction */
518 insn_next = read_memory_unsigned_integer (ip, sizeof (short));
519 ip += sizeof (short);
520 regno = cris_get_operand2 (insn_next);
dbbff683
OF
521
522 /* This check, meant to recognize srp, used to be regno ==
523 (SRP_REGNUM - NUM_GENREGS), but that covers r11 also. */
524 if (insn_next == 0xBE7E)
29134980
OF
525 {
526 if (frameless_p)
527 {
528 return ip;
529 }
da50a4b7 530 get_frame_extra_info (fi)->leaf_function = 0;
29134980 531 }
0ba6dca9 532 else if (regno == DEPRECATED_FP_REGNUM)
29134980
OF
533 {
534 have_fp = 1;
535 }
536 }
537 else if (insn == 0x866E)
538 {
539 /* move.d sp,r8 */
540 if (frameless_p)
541 {
542 return ip;
543 }
544 continue;
545 }
546 else if (cris_get_operand2 (insn) == SP_REGNUM
547 && cris_get_mode (insn) == 0x0000
548 && cris_get_opcode (insn) == 0x000A)
549 {
550 /* subq <val>,sp */
551 val = cris_get_quick_value (insn);
552 }
553 else if (cris_get_mode (insn) == 0x0002
554 && cris_get_opcode (insn) == 0x000F
555 && cris_get_size (insn) == 0x0003
556 && cris_get_operand1 (insn) == SP_REGNUM)
557 {
558 /* movem r<regsave>,[sp] */
559 if (frameless_p)
560 {
561 return ip;
562 }
563 regsave = cris_get_operand2 (insn);
564 }
565 else if (cris_get_operand2 (insn) == SP_REGNUM
566 && ((insn & 0x0F00) >> 8) == 0x0001
567 && (cris_get_signed_offset (insn) < 0))
568 {
569 /* Immediate byte offset addressing prefix word with sp as base
570 register. Used for CRIS v8 i.e. ETRAX 100 and newer if <val>
571 is between 64 and 128.
572 movem r<regsave>,[sp=sp-<val>] */
573 val = -cris_get_signed_offset (insn);
574 insn_next = read_memory_unsigned_integer (ip, sizeof (short));
575 ip += sizeof (short);
576 if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
577 && cris_get_opcode (insn_next) == 0x000F
578 && cris_get_size (insn_next) == 0x0003
579 && cris_get_operand1 (insn_next) == SP_REGNUM)
580 {
581 if (frameless_p)
582 {
583 return ip;
584 }
585 regsave = cris_get_operand2 (insn_next);
586 }
587 else
588 {
589 /* The prologue ended before the limit was reached. */
590 ip -= 2 * sizeof (short);
591 break;
592 }
593 }
594 else if (cris_get_mode (insn) == 0x0001
595 && cris_get_opcode (insn) == 0x0009
596 && cris_get_size (insn) == 0x0002)
597 {
598 /* move.d r<10..13>,r<0..15> */
599 if (frameless_p)
600 {
601 return ip;
602 }
603 source_register = cris_get_operand1 (insn);
2a9ecef2
OF
604
605 /* FIXME? In the glibc solibs, the prologue might contain something
606 like (this example taken from relocate_doit):
607 move.d $pc,$r0
608 sub.d 0xfffef426,$r0
609 which isn't covered by the source_register check below. Question
610 is whether to add a check for this combo, or make better use of
611 the limit variable instead. */
29134980
OF
612 if (source_register < ARG1_REGNUM || source_register > ARG4_REGNUM)
613 {
614 /* The prologue ended before the limit was reached. */
615 ip -= sizeof (short);
616 break;
617 }
618 }
0ba6dca9 619 else if (cris_get_operand2 (insn) == DEPRECATED_FP_REGNUM
29134980
OF
620 /* The size is a fixed-size. */
621 && ((insn & 0x0F00) >> 8) == 0x0001
622 /* A negative offset. */
623 && (cris_get_signed_offset (insn) < 0))
624 {
625 /* move.S rZ,[r8-U] (?) */
626 insn_next = read_memory_unsigned_integer (ip, sizeof (short));
627 ip += sizeof (short);
628 regno = cris_get_operand2 (insn_next);
629 if ((regno >= 0 && regno < SP_REGNUM)
630 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
631 && cris_get_opcode (insn_next) == 0x000F)
632 {
633 /* move.S rZ,[r8-U] */
634 continue;
635 }
636 else
637 {
638 /* The prologue ended before the limit was reached. */
639 ip -= 2 * sizeof (short);
640 break;
641 }
642 }
0ba6dca9 643 else if (cris_get_operand2 (insn) == DEPRECATED_FP_REGNUM
29134980
OF
644 /* The size is a fixed-size. */
645 && ((insn & 0x0F00) >> 8) == 0x0001
646 /* A positive offset. */
647 && (cris_get_signed_offset (insn) > 0))
648 {
649 /* move.S [r8+U],rZ (?) */
650 insn_next = read_memory_unsigned_integer (ip, sizeof (short));
651 ip += sizeof (short);
652 regno = cris_get_operand2 (insn_next);
653 if ((regno >= 0 && regno < SP_REGNUM)
654 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
655 && cris_get_opcode (insn_next) == 0x0009
656 && cris_get_operand1 (insn_next) == regno)
657 {
658 /* move.S [r8+U],rZ */
659 continue;
660 }
661 else
662 {
663 /* The prologue ended before the limit was reached. */
664 ip -= 2 * sizeof (short);
665 break;
666 }
667 }
668 else
669 {
670 /* The prologue ended before the limit was reached. */
671 ip -= sizeof (short);
672 break;
673 }
674 }
675 while (ip < limit);
676
677 /* We only want to know the end of the prologue when
678 fi->saved_regs == 0. */
b2fb4676 679 if (!get_frame_saved_regs (fi))
29134980
OF
680 return ip;
681
682 if (have_fp)
683 {
0ba6dca9 684 get_frame_saved_regs (fi)[DEPRECATED_FP_REGNUM] = get_frame_base (fi);
29134980
OF
685
686 /* Calculate the addresses. */
687 for (regno = regsave; regno >= 0; regno--)
688 {
b2fb4676 689 get_frame_saved_regs (fi)[regno] = get_frame_base (fi) - val;
29134980
OF
690 val -= 4;
691 }
da50a4b7 692 if (get_frame_extra_info (fi)->leaf_function)
29134980
OF
693 {
694 /* Set the register SP to contain the stack pointer of
695 the caller. */
b2fb4676 696 get_frame_saved_regs (fi)[SP_REGNUM] = get_frame_base (fi) + 4;
29134980
OF
697 }
698 else
699 {
700 /* Set the register SP to contain the stack pointer of
701 the caller. */
b2fb4676 702 get_frame_saved_regs (fi)[SP_REGNUM] = get_frame_base (fi) + 8;
29134980
OF
703
704 /* Set the register SRP to contain the return address of
705 the caller. */
b2fb4676 706 get_frame_saved_regs (fi)[SRP_REGNUM] = get_frame_base (fi) + 4;
29134980
OF
707 }
708 }
709 return ip;
710}
711
712/* Advance pc beyond any function entry prologue instructions at pc
713 to reach some "real" code. */
714
a78f21af 715static CORE_ADDR
29134980
OF
716cris_skip_prologue (CORE_ADDR pc)
717{
718 return cris_skip_prologue_main (pc, 0);
719}
720
721/* As cris_skip_prologue, but stops as soon as it knows that the function
722 has a frame. Its result is equal to its input pc if the function is
723 frameless, unequal otherwise. */
724
a78f21af 725static CORE_ADDR
29134980
OF
726cris_skip_prologue_frameless_p (CORE_ADDR pc)
727{
728 return cris_skip_prologue_main (pc, 1);
729}
730
731/* Given a PC value corresponding to the start of a function, return the PC
732 of the first instruction after the function prologue. */
733
a78f21af 734static CORE_ADDR
29134980
OF
735cris_skip_prologue_main (CORE_ADDR pc, int frameless_p)
736{
f6c609c4
AC
737 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
738 struct frame_info *fi;
29134980
OF
739 struct symtab_and_line sal = find_pc_line (pc, 0);
740 int best_limit;
741 CORE_ADDR pc_after_prologue;
742
f6c609c4
AC
743 /* frame_info now contains dynamic memory. Since fi is a dummy
744 here, I don't bother allocating memory for saved_regs. */
745 fi = deprecated_frame_xmalloc_with_cleanup (0, sizeof (struct frame_extra_info));
29134980
OF
746
747 /* If there is no symbol information then sal.end == 0, and we end up
748 examining only the first instruction in the function prologue.
749 Exaggerating the limit seems to be harmless. */
750 if (sal.end > 0)
751 best_limit = sal.end;
752 else
753 best_limit = pc + 100;
754
f6c609c4
AC
755 pc_after_prologue = cris_examine (pc, best_limit, fi, frameless_p);
756 do_cleanups (old_chain);
29134980
OF
757 return pc_after_prologue;
758}
759
760/* Use the program counter to determine the contents and size of a breakpoint
761 instruction. It returns a pointer to a string of bytes that encode a
762 breakpoint instruction, stores the length of the string to *lenptr, and
763 adjusts pcptr (if necessary) to point to the actual memory location where
764 the breakpoint should be inserted. */
765
a78f21af 766static const unsigned char *
29134980
OF
767cris_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
768{
769 static unsigned char break_insn[] = {0x38, 0xe9};
770 *lenptr = 2;
771
772 return break_insn;
773}
774
775/* Returns the register SRP (subroutine return pointer) which must contain
776 the content of the register PC after a function call. */
777
a208b0cb
AC
778static CORE_ADDR
779cris_saved_pc_after_call (struct frame_info *frame)
29134980
OF
780{
781 return read_register (SRP_REGNUM);
782}
783
784/* Returns 1 if spec_reg is applicable to the current gdbarch's CRIS version,
785 0 otherwise. */
786
a78f21af 787static int
29134980
OF
788cris_spec_reg_applicable (struct cris_spec_reg spec_reg)
789{
790 int version = cris_version ();
791
792 switch (spec_reg.applicable_version)
793 {
794 case cris_ver_version_all:
795 return 1;
796 case cris_ver_warning:
797 /* Indeterminate/obsolete. */
798 return 0;
799 case cris_ver_sim:
800 /* Simulator only. */
801 return 0;
802 case cris_ver_v0_3:
803 return (version >= 0 && version <= 3);
804 case cris_ver_v3p:
805 return (version >= 3);
806 case cris_ver_v8:
807 return (version == 8 || version == 9);
808 case cris_ver_v8p:
809 return (version >= 8);
810 case cris_ver_v10p:
811 return (version >= 10);
812 default:
813 /* Invalid cris version. */
814 return 0;
815 }
816}
817
818/* Returns the register size in unit byte. Returns 0 for an unimplemented
819 register, -1 for an invalid register. */
820
a78f21af 821static int
29134980
OF
822cris_register_size (int regno)
823{
824 int i;
825 int spec_regno;
826
827 if (regno >= 0 && regno < NUM_GENREGS)
828 {
829 /* General registers (R0 - R15) are 32 bits. */
830 return 4;
831 }
832 else if (regno >= NUM_GENREGS && regno < NUM_REGS)
833 {
834 /* Special register (R16 - R31). cris_spec_regs is zero-based.
835 Adjust regno accordingly. */
836 spec_regno = regno - NUM_GENREGS;
837
838 /* The entries in cris_spec_regs are stored in register number order,
839 which means we can shortcut into the array when searching it. */
840 for (i = spec_regno; cris_spec_regs[i].name != NULL; i++)
841 {
842 if (cris_spec_regs[i].number == spec_regno
843 && cris_spec_reg_applicable (cris_spec_regs[i]))
844 /* Go with the first applicable register. */
845 return cris_spec_regs[i].reg_size;
846 }
847 /* Special register not applicable to this CRIS version. */
848 return 0;
849 }
850 else
851 {
852 /* Invalid register. */
853 return -1;
854 }
855}
856
857/* Nonzero if regno should not be fetched from the target. This is the case
858 for unimplemented (size 0) and non-existant registers. */
859
a78f21af 860static int
29134980
OF
861cris_cannot_fetch_register (int regno)
862{
863 return ((regno < 0 || regno >= NUM_REGS)
864 || (cris_register_size (regno) == 0));
865}
866
867/* Nonzero if regno should not be written to the target, for various
868 reasons. */
869
a78f21af 870static int
29134980
OF
871cris_cannot_store_register (int regno)
872{
873 /* There are three kinds of registers we refuse to write to.
874 1. Those that not implemented.
875 2. Those that are read-only (depends on the processor mode).
876 3. Those registers to which a write has no effect.
877 */
878
879 if (regno < 0 || regno >= NUM_REGS || cris_register_size (regno) == 0)
880 /* Not implemented. */
881 return 1;
882
883 else if (regno == VR_REGNUM)
884 /* Read-only. */
885 return 1;
886
887 else if (regno == P0_REGNUM || regno == P4_REGNUM || regno == P8_REGNUM)
888 /* Writing has no effect. */
889 return 1;
890
891 else if (cris_mode () == CRIS_MODE_USER)
892 {
893 if (regno == IBR_REGNUM || regno == BAR_REGNUM || regno == BRP_REGNUM
894 || regno == IRP_REGNUM)
895 /* Read-only in user mode. */
896 return 1;
897 }
898
899 return 0;
900}
901
902/* Returns the register offset for the first byte of register regno's space
903 in the saved register state. Returns -1 for an invalid or unimplemented
904 register. */
905
a78f21af 906static int
29134980
OF
907cris_register_offset (int regno)
908{
909 int i;
910 int reg_size;
911 int offset = 0;
912
913 if (regno >= 0 && regno < NUM_REGS)
914 {
915 /* FIXME: The offsets should be cached and calculated only once,
916 when the architecture being debugged has changed. */
917 for (i = 0; i < regno; i++)
918 offset += cris_register_size (i);
919
920 return offset;
921 }
922 else
923 {
924 /* Invalid register. */
925 return -1;
926 }
927}
928
929/* Return the GDB type (defined in gdbtypes.c) for the "standard" data type
930 of data in register regno. */
931
a78f21af 932static struct type *
29134980
OF
933cris_register_virtual_type (int regno)
934{
935 if (regno == SP_REGNUM || regno == PC_REGNUM
936 || (regno > P8_REGNUM && regno < USP_REGNUM))
937 {
938 /* SP, PC, IBR, IRP, SRP, BAR, DCCR, BRP */
939 return lookup_pointer_type (builtin_type_void);
940 }
941 else if (regno == P8_REGNUM || regno == USP_REGNUM
942 || (regno >= 0 && regno < SP_REGNUM))
943 {
944 /* R0 - R13, P8, P15 */
945 return builtin_type_unsigned_long;
946 }
947 else if (regno > P3_REGNUM && regno < P8_REGNUM)
948 {
949 /* P4, CCR, DCR0, DCR1 */
950 return builtin_type_unsigned_short;
951 }
952 else if (regno > PC_REGNUM && regno < P4_REGNUM)
953 {
954 /* P0, P1, P2, P3 */
955 return builtin_type_unsigned_char;
956 }
957 else
958 {
959 /* Invalid register. */
960 return builtin_type_void;
961 }
962}
963
964/* Stores a function return value of type type, where valbuf is the address
965 of the value to be stored. */
966
967/* In the original CRIS ABI, R10 is used to store return values. */
968
a78f21af 969static void
29134980
OF
970cris_abi_original_store_return_value (struct type *type, char *valbuf)
971{
972 int len = TYPE_LENGTH (type);
973
b1e29e33 974 if (len <= DEPRECATED_REGISTER_SIZE)
73937e03 975 deprecated_write_register_bytes (REGISTER_BYTE (RET_REGNUM), valbuf, len);
29134980
OF
976 else
977 internal_error (__FILE__, __LINE__, "cris_abi_original_store_return_value: type length too large.");
978}
979
980/* In the CRIS ABI V2, R10 and R11 are used to store return values. */
981
a78f21af 982static void
29134980
OF
983cris_abi_v2_store_return_value (struct type *type, char *valbuf)
984{
985 int len = TYPE_LENGTH (type);
986
b1e29e33 987 if (len <= 2 * DEPRECATED_REGISTER_SIZE)
29134980
OF
988 {
989 /* Note that this works since R10 and R11 are consecutive registers. */
73937e03
AC
990 deprecated_write_register_bytes (REGISTER_BYTE (RET_REGNUM), valbuf,
991 len);
29134980
OF
992 }
993 else
994 internal_error (__FILE__, __LINE__, "cris_abi_v2_store_return_value: type length too large.");
995}
996
997/* Return the name of register regno as a string. Return NULL for an invalid or
998 unimplemented register. */
999
a78f21af 1000static const char *
29134980
OF
1001cris_register_name (int regno)
1002{
1003 static char *cris_genreg_names[] =
1004 { "r0", "r1", "r2", "r3", \
1005 "r4", "r5", "r6", "r7", \
1006 "r8", "r9", "r10", "r11", \
1007 "r12", "r13", "sp", "pc" };
1008
1009 int i;
1010 int spec_regno;
1011
1012 if (regno >= 0 && regno < NUM_GENREGS)
1013 {
1014 /* General register. */
1015 return cris_genreg_names[regno];
1016 }
1017 else if (regno >= NUM_GENREGS && regno < NUM_REGS)
1018 {
1019 /* Special register (R16 - R31). cris_spec_regs is zero-based.
1020 Adjust regno accordingly. */
1021 spec_regno = regno - NUM_GENREGS;
1022
1023 /* The entries in cris_spec_regs are stored in register number order,
1024 which means we can shortcut into the array when searching it. */
1025 for (i = spec_regno; cris_spec_regs[i].name != NULL; i++)
1026 {
1027 if (cris_spec_regs[i].number == spec_regno
1028 && cris_spec_reg_applicable (cris_spec_regs[i]))
1029 /* Go with the first applicable register. */
1030 return cris_spec_regs[i].name;
1031 }
1032 /* Special register not applicable to this CRIS version. */
1033 return NULL;
1034 }
1035 else
1036 {
1037 /* Invalid register. */
1038 return NULL;
1039 }
1040}
1041
a78f21af 1042static int
29134980
OF
1043cris_register_bytes_ok (long bytes)
1044{
b8b527c5 1045 return (bytes == DEPRECATED_REGISTER_BYTES);
29134980
OF
1046}
1047
1048/* Extract from an array regbuf containing the raw register state a function
1049 return value of type type, and copy that, in virtual format, into
1050 valbuf. */
1051
1052/* In the original CRIS ABI, R10 is used to return values. */
1053
a78f21af 1054static void
29134980
OF
1055cris_abi_original_extract_return_value (struct type *type, char *regbuf,
1056 char *valbuf)
1057{
1058 int len = TYPE_LENGTH (type);
1059
b1e29e33 1060 if (len <= DEPRECATED_REGISTER_SIZE)
29134980
OF
1061 memcpy (valbuf, regbuf + REGISTER_BYTE (RET_REGNUM), len);
1062 else
1063 internal_error (__FILE__, __LINE__, "cris_abi_original_extract_return_value: type length too large");
1064}
1065
1066/* In the CRIS ABI V2, R10 and R11 are used to store return values. */
1067
a78f21af 1068static void
29134980
OF
1069cris_abi_v2_extract_return_value (struct type *type, char *regbuf,
1070 char *valbuf)
1071{
1072 int len = TYPE_LENGTH (type);
1073
b1e29e33 1074 if (len <= 2 * DEPRECATED_REGISTER_SIZE)
29134980
OF
1075 memcpy (valbuf, regbuf + REGISTER_BYTE (RET_REGNUM), len);
1076 else
1077 internal_error (__FILE__, __LINE__, "cris_abi_v2_extract_return_value: type length too large");
1078}
1079
1080/* Store the address of the place in which to copy the structure the
1081 subroutine will return. In the CRIS ABI, R9 is used in order to pass
1082 the address of the allocated area where a structure return value must
1083 be stored. R9 is call-clobbered, which means we must save it here for
1084 later use. */
1085
a78f21af 1086static void
29134980
OF
1087cris_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1088{
1089 write_register (STR_REGNUM, addr);
1090 struct_return_address = addr;
1091}
1092
1093/* Extract from regbuf the address where a function should return a
1094 structure value. It's not there in the CRIS ABI, so we must do it another
1095 way. */
1096
a78f21af 1097static CORE_ADDR
29134980
OF
1098cris_extract_struct_value_address (char *regbuf)
1099{
1100 return struct_return_address;
1101}
1102
1103/* Returns 1 if a value of the given type being returned from a function
1104 must have space allocated for it on the stack. gcc_p is true if the
1105 function being considered is known to have been compiled by GCC.
1106 In the CRIS ABI, structure return values are passed to the called
1107 function by reference in register R9 to a caller-allocated area, so
1108 this is always true. */
1109
a78f21af 1110static int
29134980
OF
1111cris_use_struct_convention (int gcc_p, struct type *type)
1112{
1113 return 1;
1114}
1115
1116/* Returns 1 if the given type will be passed by pointer rather than
1117 directly. */
1118
1119/* In the original CRIS ABI, arguments shorter than or equal to 32 bits are
1120 passed by value. */
1121
a78f21af 1122static int
29134980
OF
1123cris_abi_original_reg_struct_has_addr (int gcc_p, struct type *type)
1124{
1125 return (TYPE_LENGTH (type) > 4);
1126}
1127
1128/* In the CRIS ABI V2, arguments shorter than or equal to 64 bits are passed
1129 by value. */
1130
a78f21af 1131static int
29134980
OF
1132cris_abi_v2_reg_struct_has_addr (int gcc_p, struct type *type)
1133{
1134 return (TYPE_LENGTH (type) > 8);
1135}
1136
1137/* Returns 1 if the function invocation represented by fi does not have a
1138 stack frame associated with it. Otherwise return 0. */
1139
a78f21af 1140static int
29134980
OF
1141cris_frameless_function_invocation (struct frame_info *fi)
1142{
5a203e44 1143 if ((get_frame_type (fi) == SIGTRAMP_FRAME))
29134980
OF
1144 return 0;
1145 else
1146 return frameless_look_for_prologue (fi);
1147}
1148
f30ee0bc
AC
1149/* See frame.h. Determines the address of all registers in the
1150 current stack frame storing each in frame->saved_regs. Space for
1151 frame->saved_regs shall be allocated by
1152 DEPRECATED_FRAME_INIT_SAVED_REGS using frame_saved_regs_zalloc. */
29134980 1153
a78f21af 1154static void
29134980
OF
1155cris_frame_init_saved_regs (struct frame_info *fi)
1156{
1157 CORE_ADDR ip;
1158 struct symtab_and_line sal;
1159 int best_limit;
1e2330ba
AC
1160 char *dummy_regs = deprecated_generic_find_dummy_frame (get_frame_pc (fi),
1161 get_frame_base (fi));
29134980
OF
1162
1163 /* Examine the entire prologue. */
1164 register int frameless_p = 0;
1165
1166 /* Has this frame's registers already been initialized? */
b2fb4676 1167 if (get_frame_saved_regs (fi))
29134980
OF
1168 return;
1169
1170 frame_saved_regs_zalloc (fi);
1171
1172 if (dummy_regs)
1173 {
1174 /* I don't see this ever happening, considering the context in which
1175 cris_frame_init_saved_regs is called (always when we're not in
1176 a dummy frame). */
7b5849cc 1177 memcpy (get_frame_saved_regs (fi), dummy_regs, SIZEOF_FRAME_SAVED_REGS);
29134980
OF
1178 }
1179 else
1180 {
be41e9f4 1181 ip = get_frame_func (fi);
29134980
OF
1182 sal = find_pc_line (ip, 0);
1183
1184 /* If there is no symbol information then sal.end == 0, and we end up
1185 examining only the first instruction in the function prologue.
1186 Exaggerating the limit seems to be harmless. */
1187 if (sal.end > 0)
1188 best_limit = sal.end;
1189 else
1190 best_limit = ip + 100;
1191
1192 cris_examine (ip, best_limit, fi, frameless_p);
1193 }
1194}
1195
1196/* Initialises the extra frame information at the creation of a new frame.
1197 The inparameter fromleaf is 0 when the call is from create_new_frame.
1198 When the call is from get_prev_frame_info, fromleaf is determined by
1199 cris_frameless_function_invocation. */
1200
a78f21af 1201static void
29134980
OF
1202cris_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1203{
11c02a10 1204 if (get_next_frame (fi))
29134980
OF
1205 {
1206 /* Called from get_prev_frame. */
8bedc050 1207 deprecated_update_frame_pc_hack (fi, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi)));
29134980
OF
1208 }
1209
a00a19e9 1210 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
29134980 1211
da50a4b7
AC
1212 get_frame_extra_info (fi)->return_pc = 0;
1213 get_frame_extra_info (fi)->leaf_function = 0;
29134980 1214
1e2330ba
AC
1215 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi),
1216 get_frame_base (fi),
1217 get_frame_base (fi)))
29134980 1218 {
04714b91
AC
1219 /* We need to setup fi->frame here because call_function_by_hand
1220 gets it wrong by assuming it's always FP. */
1e2330ba 1221 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), get_frame_base (fi), SP_REGNUM));
da50a4b7 1222 get_frame_extra_info (fi)->return_pc =
1e2330ba
AC
1223 deprecated_read_register_dummy (get_frame_pc (fi),
1224 get_frame_base (fi), PC_REGNUM);
29134980
OF
1225
1226 /* FIXME: Is this necessarily true? */
da50a4b7 1227 get_frame_extra_info (fi)->leaf_function = 0;
29134980
OF
1228 }
1229 else
1230 {
1231 cris_frame_init_saved_regs (fi);
1232
1233 /* Check fromleaf/frameless_function_invocation. (FIXME) */
1234
b2fb4676 1235 if (get_frame_saved_regs (fi)[SRP_REGNUM] != 0)
29134980
OF
1236 {
1237 /* SRP was saved on the stack; non-leaf function. */
da50a4b7 1238 get_frame_extra_info (fi)->return_pc =
b2fb4676 1239 read_memory_integer (get_frame_saved_regs (fi)[SRP_REGNUM],
29134980
OF
1240 REGISTER_RAW_SIZE (SRP_REGNUM));
1241 }
1242 else
1243 {
1244 /* SRP is still in a register; leaf function. */
da50a4b7 1245 get_frame_extra_info (fi)->return_pc = read_register (SRP_REGNUM);
29134980 1246 /* FIXME: Should leaf_function be set to 1 here? */
da50a4b7 1247 get_frame_extra_info (fi)->leaf_function = 1;
29134980
OF
1248 }
1249 }
1250}
1251
1252/* Return the content of the frame pointer in the present frame. In other
1253 words, determine the address of the calling function's frame. */
1254
a78f21af 1255static CORE_ADDR
29134980
OF
1256cris_frame_chain (struct frame_info *fi)
1257{
1e2330ba
AC
1258 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi),
1259 get_frame_base (fi),
1260 get_frame_base (fi)))
29134980 1261 {
1e2330ba 1262 return get_frame_base (fi);
29134980 1263 }
50abf9e5 1264 else if (!inside_entry_file (get_frame_pc (fi)))
29134980 1265 {
c193f6ac 1266 return read_memory_unsigned_integer (get_frame_base (fi), 4);
29134980
OF
1267 }
1268 else
1269 {
1270 return 0;
1271 }
1272}
1273
1274/* Return the saved PC (which equals the return address) of this frame. */
1275
a78f21af 1276static CORE_ADDR
29134980
OF
1277cris_frame_saved_pc (struct frame_info *fi)
1278{
da50a4b7 1279 return get_frame_extra_info (fi)->return_pc;
29134980
OF
1280}
1281
29134980
OF
1282/* Setup the function arguments for calling a function in the inferior. */
1283
a78f21af 1284static CORE_ADDR
29134980
OF
1285cris_abi_original_push_arguments (int nargs, struct value **args,
1286 CORE_ADDR sp, int struct_return,
1287 CORE_ADDR struct_addr)
1288{
1289 int stack_alloc;
1290 int stack_offset;
1291 int argreg;
1292 int argnum;
1293 struct type *type;
1294 int len;
1295 CORE_ADDR regval;
1296 char *val;
1297
1298 /* Data and parameters reside in different areas on the stack.
1299 Both frame pointers grow toward higher addresses. */
1300 CORE_ADDR fp_params;
1301 CORE_ADDR fp_data;
1302
1303 /* Are we returning a value using a structure return or a normal value
1304 return? struct_addr is the address of the reserved space for the return
1305 structure to be written on the stack. */
1306 if (struct_return)
1307 {
1308 write_register (STR_REGNUM, struct_addr);
1309 }
1310
1311 /* Make sure there's space on the stack. Allocate space for data and a
1312 parameter to refer to that data. */
1313 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
b1e29e33 1314 stack_alloc += (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + DEPRECATED_REGISTER_SIZE);
29134980
OF
1315 sp -= stack_alloc;
1316 /* We may over-allocate a little here, but that won't hurt anything. */
1317
1318 /* Initialize stack frame pointers. */
1319 fp_params = sp;
b1e29e33 1320 fp_data = sp + (nargs * DEPRECATED_REGISTER_SIZE);
29134980
OF
1321
1322 /* Now load as many as possible of the first arguments into
1323 registers, and push the rest onto the stack. */
1324 argreg = ARG1_REGNUM;
1325 stack_offset = 0;
1326
1327 for (argnum = 0; argnum < nargs; argnum++)
1328 {
1329 type = VALUE_TYPE (args[argnum]);
1330 len = TYPE_LENGTH (type);
1331 val = (char *) VALUE_CONTENTS (args[argnum]);
1332
b1e29e33 1333 if (len <= DEPRECATED_REGISTER_SIZE && argreg <= ARG4_REGNUM)
29134980
OF
1334 {
1335 /* Data fits in a register; put it in the first available
1336 register. */
1337 write_register (argreg, *(unsigned long *) val);
1338 argreg++;
1339 }
b1e29e33 1340 else if (len > DEPRECATED_REGISTER_SIZE && argreg <= ARG4_REGNUM)
29134980
OF
1341 {
1342 /* Data does not fit in register; pass it on the stack and
1343 put its address in the first available register. */
1344 write_memory (fp_data, val, len);
1345 write_register (argreg, fp_data);
1346 fp_data += len;
1347 argreg++;
1348 }
b1e29e33 1349 else if (len > DEPRECATED_REGISTER_SIZE)
29134980
OF
1350 {
1351 /* Data does not fit in register; put both data and
1352 parameter on the stack. */
1353 write_memory (fp_data, val, len);
b1e29e33 1354 write_memory (fp_params, (char *) (&fp_data), DEPRECATED_REGISTER_SIZE);
29134980 1355 fp_data += len;
b1e29e33 1356 fp_params += DEPRECATED_REGISTER_SIZE;
29134980
OF
1357 }
1358 else
1359 {
1360 /* Data fits in a register, but we are out of registers;
1361 put the parameter on the stack. */
b1e29e33
AC
1362 write_memory (fp_params, val, DEPRECATED_REGISTER_SIZE);
1363 fp_params += DEPRECATED_REGISTER_SIZE;
29134980
OF
1364 }
1365 }
1366
1367 return sp;
1368}
1369
a78f21af 1370static CORE_ADDR
29134980
OF
1371cris_abi_v2_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1372 int struct_return, CORE_ADDR struct_addr)
1373{
1374 int stack_alloc;
1375 int stack_offset;
1376 int argreg;
1377 int argnum;
1378
1379 CORE_ADDR regval;
1380
1381 /* The function's arguments and memory allocated by gdb for the arguments to
1382 point at reside in separate areas on the stack.
1383 Both frame pointers grow toward higher addresses. */
1384 CORE_ADDR fp_arg;
1385 CORE_ADDR fp_mem;
1386
1387 /* Are we returning a value using a structure return or a normal value
1388 return? struct_addr is the address of the reserved space for the return
1389 structure to be written on the stack. */
1390 if (struct_return)
1391 {
1392 write_register (STR_REGNUM, struct_addr);
1393 }
1394
1395 /* Allocate enough to keep things word-aligned on both parts of the
1396 stack. */
1397 stack_alloc = 0;
1398 for (argnum = 0; argnum < nargs; argnum++)
1399 {
1400 int len;
1401 int reg_demand;
1402
1403 len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
b1e29e33 1404 reg_demand = (len / DEPRECATED_REGISTER_SIZE) + (len % DEPRECATED_REGISTER_SIZE != 0 ? 1 : 0);
29134980 1405
b1e29e33
AC
1406 /* reg_demand * DEPRECATED_REGISTER_SIZE is the amount of memory
1407 we might need to allocate for this argument. 2 *
1408 DEPRECATED_REGISTER_SIZE is the amount of stack space we
1409 might need to pass the argument itself (either by value or by
29134980 1410 reference). */
b1e29e33 1411 stack_alloc += (reg_demand * DEPRECATED_REGISTER_SIZE + 2 * DEPRECATED_REGISTER_SIZE);
29134980
OF
1412 }
1413 sp -= stack_alloc;
1414 /* We may over-allocate a little here, but that won't hurt anything. */
1415
1416 /* Initialize frame pointers. */
1417 fp_arg = sp;
b1e29e33 1418 fp_mem = sp + (nargs * (2 * DEPRECATED_REGISTER_SIZE));
29134980
OF
1419
1420 /* Now load as many as possible of the first arguments into registers,
1421 and push the rest onto the stack. */
1422 argreg = ARG1_REGNUM;
1423 stack_offset = 0;
1424
1425 for (argnum = 0; argnum < nargs; argnum++)
1426 {
1427 int len;
1428 char *val;
1429 int reg_demand;
1430 int i;
1431
1432 len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1433 val = (char *) VALUE_CONTENTS (args[argnum]);
1434
1435 /* How may registers worth of storage do we need for this argument? */
b1e29e33 1436 reg_demand = (len / DEPRECATED_REGISTER_SIZE) + (len % DEPRECATED_REGISTER_SIZE != 0 ? 1 : 0);
29134980 1437
b1e29e33 1438 if (len <= (2 * DEPRECATED_REGISTER_SIZE)
29134980
OF
1439 && (argreg + reg_demand - 1 <= ARG4_REGNUM))
1440 {
1441 /* Data passed by value. Fits in available register(s). */
1442 for (i = 0; i < reg_demand; i++)
1443 {
1444 write_register (argreg, *(unsigned long *) val);
1445 argreg++;
b1e29e33 1446 val += DEPRECATED_REGISTER_SIZE;
29134980
OF
1447 }
1448 }
b1e29e33 1449 else if (len <= (2 * DEPRECATED_REGISTER_SIZE) && argreg <= ARG4_REGNUM)
29134980
OF
1450 {
1451 /* Data passed by value. Does not fit in available register(s).
1452 Use the register(s) first, then the stack. */
1453 for (i = 0; i < reg_demand; i++)
1454 {
1455 if (argreg <= ARG4_REGNUM)
1456 {
1457 write_register (argreg, *(unsigned long *) val);
1458 argreg++;
b1e29e33 1459 val += DEPRECATED_REGISTER_SIZE;
29134980
OF
1460 }
1461 else
1462 {
b1e29e33
AC
1463 /* I guess this memory write could write the
1464 remaining data all at once instead of in
1465 DEPRECATED_REGISTER_SIZE chunks. */
1466 write_memory (fp_arg, val, DEPRECATED_REGISTER_SIZE);
1467 fp_arg += DEPRECATED_REGISTER_SIZE;
1468 val += DEPRECATED_REGISTER_SIZE;
29134980
OF
1469 }
1470 }
1471 }
b1e29e33 1472 else if (len > (2 * DEPRECATED_REGISTER_SIZE))
29134980
OF
1473 {
1474 /* Data passed by reference. Put it on the stack. */
1475 write_memory (fp_mem, val, len);
b1e29e33 1476 write_memory (fp_arg, (char *) (&fp_mem), DEPRECATED_REGISTER_SIZE);
29134980
OF
1477
1478 /* fp_mem need not be word-aligned since it's just a chunk of
1479 memory being pointed at. That is, += len would do. */
b1e29e33
AC
1480 fp_mem += reg_demand * DEPRECATED_REGISTER_SIZE;
1481 fp_arg += DEPRECATED_REGISTER_SIZE;
29134980
OF
1482 }
1483 else
1484 {
1485 /* Data passed by value. No available registers. Put it on
1486 the stack. */
1487 write_memory (fp_arg, val, len);
1488
1489 /* fp_arg must be word-aligned (i.e., don't += len) to match
1490 the function prologue. */
b1e29e33 1491 fp_arg += reg_demand * DEPRECATED_REGISTER_SIZE;
29134980
OF
1492 }
1493 }
1494
1495 return sp;
1496}
1497
1498/* Never put the return address on the stack. The register SRP is pushed
1499 by the called function unless it is a leaf-function. Due to the BRP
1500 register the PC will change when continue is sent. */
1501
a78f21af 1502static CORE_ADDR
29134980
OF
1503cris_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
1504{
1505 write_register (SRP_REGNUM, CALL_DUMMY_ADDRESS ());
1506 return sp;
1507}
1508
1509/* Restore the machine to the state it had before the current frame
1510 was created. Discard the innermost frame from the stack and restore
1511 all saved registers. */
1512
a78f21af 1513static void
5ae5f592 1514cris_pop_frame (void)
29134980
OF
1515{
1516 register struct frame_info *fi = get_current_frame ();
1517 register int regno;
1518 register int stack_offset = 0;
1519
1e2330ba
AC
1520 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi),
1521 get_frame_base (fi),
1522 get_frame_base (fi)))
29134980
OF
1523 {
1524 /* This happens when we hit a breakpoint set at the entry point,
1525 when returning from a dummy frame. */
1526 generic_pop_dummy_frame ();
1527 }
1528 else
1529 {
1530 cris_frame_init_saved_regs (fi);
1531
1532 /* For each register, the address of where it was saved on entry to
1533 the frame now lies in fi->saved_regs[regno], or zero if it was not
1534 saved. This includes special registers such as PC and FP saved in
1535 special ways in the stack frame. The SP_REGNUM is even more
1536 special, the address here is the SP for the next frame, not the
1537 address where the SP was saved. */
1538
1539 /* Restore general registers R0 - R7. They were pushed on the stack
1540 after SP was saved. */
0ba6dca9 1541 for (regno = 0; regno < DEPRECATED_FP_REGNUM; regno++)
29134980 1542 {
b2fb4676 1543 if (get_frame_saved_regs (fi)[regno])
29134980
OF
1544 {
1545 write_register (regno,
b2fb4676 1546 read_memory_integer (get_frame_saved_regs (fi)[regno], 4));
29134980
OF
1547 }
1548 }
1549
0ba6dca9 1550 if (get_frame_saved_regs (fi)[DEPRECATED_FP_REGNUM])
29134980
OF
1551 {
1552 /* Pop the frame pointer (R8). It was pushed before SP
1553 was saved. */
0ba6dca9
AC
1554 write_register (DEPRECATED_FP_REGNUM,
1555 read_memory_integer (get_frame_saved_regs (fi)[DEPRECATED_FP_REGNUM], 4));
29134980
OF
1556 stack_offset += 4;
1557
1558 /* Not a leaf function. */
b2fb4676 1559 if (get_frame_saved_regs (fi)[SRP_REGNUM])
29134980
OF
1560 {
1561 /* SRP was pushed before SP was saved. */
1562 stack_offset += 4;
1563 }
1564
1565 /* Restore the SP and adjust for R8 and (possibly) SRP. */
0ba6dca9 1566 write_register (SP_REGNUM, get_frame_saved_regs (fi)[DEPRECATED_FP_REGNUM] + stack_offset);
29134980
OF
1567 }
1568 else
1569 {
1570 /* Currently, we can't get the correct info into fi->saved_regs
1571 without a frame pointer. */
1572 }
1573
1574 /* Restore the PC. */
da50a4b7 1575 write_register (PC_REGNUM, get_frame_extra_info (fi)->return_pc);
29134980
OF
1576 }
1577 flush_cached_frames ();
1578}
1579
1580/* Calculates a value that measures how good inst_args constraints an
1581 instruction. It stems from cris_constraint, found in cris-dis.c. */
1582
1583static int
1584constraint (unsigned int insn, const signed char *inst_args,
1585 inst_env_type *inst_env)
1586{
1587 int retval = 0;
1588 int tmp, i;
1589
1590 const char *s = inst_args;
1591
1592 for (; *s; s++)
1593 switch (*s)
1594 {
1595 case 'm':
1596 if ((insn & 0x30) == 0x30)
1597 return -1;
1598 break;
1599
1600 case 'S':
1601 /* A prefix operand. */
1602 if (inst_env->prefix_found)
1603 break;
1604 else
1605 return -1;
1606
1607 case 'B':
1608 /* A "push" prefix. (This check was REMOVED by san 970921.) Check for
1609 valid "push" size. In case of special register, it may be != 4. */
1610 if (inst_env->prefix_found)
1611 break;
1612 else
1613 return -1;
1614
1615 case 'D':
1616 retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
1617 if (!retval)
1618 return -1;
1619 else
1620 retval += 4;
1621 break;
1622
1623 case 'P':
1624 tmp = (insn >> 0xC) & 0xF;
fa4e4598
OF
1625
1626 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1627 {
1628 /* Since we match four bits, we will give a value of
1629 4 - 1 = 3 in a match. If there is a corresponding
1630 exact match of a special register in another pattern, it
1631 will get a value of 4, which will be higher. This should
1632 be correct in that an exact pattern would match better that
1633 a general pattern.
1634 Note that there is a reason for not returning zero; the
1635 pattern for "clear" is partly matched in the bit-pattern
1636 (the two lower bits must be zero), while the bit-pattern
1637 for a move from a special register is matched in the
1638 register constraint.
1639 This also means we will will have a race condition if
1640 there is a partly match in three bits in the bit pattern. */
1641 if (tmp == cris_spec_regs[i].number)
1642 {
1643 retval += 3;
1644 break;
1645 }
1646 }
1647
1648 if (cris_spec_regs[i].name == NULL)
29134980
OF
1649 return -1;
1650 break;
1651 }
1652 return retval;
1653}
1654
1655/* Returns the number of bits set in the variable value. */
1656
1657static int
1658number_of_bits (unsigned int value)
1659{
1660 int number_of_bits = 0;
1661
1662 while (value != 0)
1663 {
1664 number_of_bits += 1;
1665 value &= (value - 1);
1666 }
1667 return number_of_bits;
1668}
1669
1670/* Finds the address that should contain the single step breakpoint(s).
1671 It stems from code in cris-dis.c. */
1672
1673static int
1674find_cris_op (unsigned short insn, inst_env_type *inst_env)
1675{
1676 int i;
1677 int max_level_of_match = -1;
1678 int max_matched = -1;
1679 int level_of_match;
1680
1681 for (i = 0; cris_opcodes[i].name != NULL; i++)
1682 {
1683 if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match)
1684 && ((cris_opcodes[i].lose & insn) == 0))
1685 {
1686 level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
1687 if (level_of_match >= 0)
1688 {
1689 level_of_match +=
1690 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
1691 if (level_of_match > max_level_of_match)
1692 {
1693 max_matched = i;
1694 max_level_of_match = level_of_match;
1695 if (level_of_match == 16)
1696 {
1697 /* All bits matched, cannot find better. */
1698 break;
1699 }
1700 }
1701 }
1702 }
1703 }
1704 return max_matched;
1705}
1706
1707/* Attempts to find single-step breakpoints. Returns -1 on failure which is
1708 actually an internal error. */
1709
1710static int
1711find_step_target (inst_env_type *inst_env)
1712{
1713 int i;
1714 int offset;
1715 unsigned short insn;
1716
1717 /* Create a local register image and set the initial state. */
1718 for (i = 0; i < NUM_GENREGS; i++)
1719 {
1720 inst_env->reg[i] = (unsigned long) read_register (i);
1721 }
1722 offset = NUM_GENREGS;
1723 for (i = 0; i < NUM_SPECREGS; i++)
1724 {
1725 inst_env->preg[i] = (unsigned long) read_register (offset + i);
1726 }
1727 inst_env->branch_found = 0;
1728 inst_env->slot_needed = 0;
1729 inst_env->delay_slot_pc_active = 0;
1730 inst_env->prefix_found = 0;
1731 inst_env->invalid = 0;
1732 inst_env->xflag_found = 0;
1733 inst_env->disable_interrupt = 0;
1734
1735 /* Look for a step target. */
1736 do
1737 {
1738 /* Read an instruction from the client. */
1739 insn = read_memory_unsigned_integer (inst_env->reg[PC_REGNUM], 2);
1740
1741 /* If the instruction is not in a delay slot the new content of the
1742 PC is [PC] + 2. If the instruction is in a delay slot it is not
1743 that simple. Since a instruction in a delay slot cannot change
1744 the content of the PC, it does not matter what value PC will have.
1745 Just make sure it is a valid instruction. */
1746 if (!inst_env->delay_slot_pc_active)
1747 {
1748 inst_env->reg[PC_REGNUM] += 2;
1749 }
1750 else
1751 {
1752 inst_env->delay_slot_pc_active = 0;
1753 inst_env->reg[PC_REGNUM] = inst_env->delay_slot_pc;
1754 }
1755 /* Analyse the present instruction. */
1756 i = find_cris_op (insn, inst_env);
1757 if (i == -1)
1758 {
1759 inst_env->invalid = 1;
1760 }
1761 else
1762 {
1763 cris_gdb_func (cris_opcodes[i].op, insn, inst_env);
1764 }
1765 } while (!inst_env->invalid
1766 && (inst_env->prefix_found || inst_env->xflag_found
1767 || inst_env->slot_needed));
1768 return i;
1769}
1770
1771/* There is no hardware single-step support. The function find_step_target
1772 digs through the opcodes in order to find all possible targets.
1773 Either one ordinary target or two targets for branches may be found. */
1774
a78f21af 1775static void
29134980
OF
1776cris_software_single_step (enum target_signal ignore, int insert_breakpoints)
1777{
1778 inst_env_type inst_env;
1779
1780 if (insert_breakpoints)
1781 {
1782 /* Analyse the present instruction environment and insert
1783 breakpoints. */
1784 int status = find_step_target (&inst_env);
1785 if (status == -1)
1786 {
1787 /* Could not find a target. FIXME: Should do something. */
1788 }
1789 else
1790 {
1791 /* Insert at most two breakpoints. One for the next PC content
1792 and possibly another one for a branch, jump, etc. */
1793 next_pc = (CORE_ADDR) inst_env.reg[PC_REGNUM];
1794 target_insert_breakpoint (next_pc, break_mem[0]);
1795 if (inst_env.branch_found
1796 && (CORE_ADDR) inst_env.branch_break_address != next_pc)
1797 {
1798 branch_target_address =
1799 (CORE_ADDR) inst_env.branch_break_address;
1800 target_insert_breakpoint (branch_target_address, break_mem[1]);
1801 branch_break_inserted = 1;
1802 }
1803 }
1804 }
1805 else
1806 {
1807 /* Remove breakpoints. */
1808 target_remove_breakpoint (next_pc, break_mem[0]);
1809 if (branch_break_inserted)
1810 {
1811 target_remove_breakpoint (branch_target_address, break_mem[1]);
1812 branch_break_inserted = 0;
1813 }
1814 }
1815}
1816
1817/* Calculates the prefix value for quick offset addressing mode. */
1818
a78f21af 1819static void
29134980
OF
1820quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
1821{
1822 /* It's invalid to be in a delay slot. You can't have a prefix to this
1823 instruction (not 100% sure). */
1824 if (inst_env->slot_needed || inst_env->prefix_found)
1825 {
1826 inst_env->invalid = 1;
1827 return;
1828 }
1829
1830 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
1831 inst_env->prefix_value += cris_get_bdap_quick_offset (inst);
1832
1833 /* A prefix doesn't change the xflag_found. But the rest of the flags
1834 need updating. */
1835 inst_env->slot_needed = 0;
1836 inst_env->prefix_found = 1;
1837}
1838
1839/* Updates the autoincrement register. The size of the increment is derived
1840 from the size of the operation. The PC is always kept aligned on even
1841 word addresses. */
1842
a78f21af 1843static void
29134980
OF
1844process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
1845{
1846 if (size == INST_BYTE_SIZE)
1847 {
1848 inst_env->reg[cris_get_operand1 (inst)] += 1;
1849
1850 /* The PC must be word aligned, so increase the PC with one
1851 word even if the size is byte. */
1852 if (cris_get_operand1 (inst) == REG_PC)
1853 {
1854 inst_env->reg[REG_PC] += 1;
1855 }
1856 }
1857 else if (size == INST_WORD_SIZE)
1858 {
1859 inst_env->reg[cris_get_operand1 (inst)] += 2;
1860 }
1861 else if (size == INST_DWORD_SIZE)
1862 {
1863 inst_env->reg[cris_get_operand1 (inst)] += 4;
1864 }
1865 else
1866 {
1867 /* Invalid size. */
1868 inst_env->invalid = 1;
1869 }
1870}
1871
2a9ecef2
OF
1872/* Just a forward declaration. */
1873
a78f21af
AC
1874static unsigned long get_data_from_address (unsigned short *inst,
1875 CORE_ADDR address);
2a9ecef2 1876
29134980
OF
1877/* Calculates the prefix value for the general case of offset addressing
1878 mode. */
1879
a78f21af 1880static void
29134980
OF
1881bdap_prefix (unsigned short inst, inst_env_type *inst_env)
1882{
1883
1884 long offset;
1885
1886 /* It's invalid to be in a delay slot. */
1887 if (inst_env->slot_needed || inst_env->prefix_found)
1888 {
1889 inst_env->invalid = 1;
1890 return;
1891 }
1892
fa4e4598
OF
1893 /* The calculation of prefix_value used to be after process_autoincrement,
1894 but that fails for an instruction such as jsr [$r0+12] which is encoded
1895 as 5f0d 0c00 30b9 when compiled with -fpic. Since PC is operand1 it
1896 mustn't be incremented until we have read it and what it points at. */
29134980
OF
1897 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
1898
1899 /* The offset is an indirection of the contents of the operand1 register. */
1900 inst_env->prefix_value +=
2a9ecef2
OF
1901 get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)]);
1902
fa4e4598
OF
1903 if (cris_get_mode (inst) == AUTOINC_MODE)
1904 {
1905 process_autoincrement (cris_get_size (inst), inst, inst_env);
1906 }
1907
29134980
OF
1908 /* A prefix doesn't change the xflag_found. But the rest of the flags
1909 need updating. */
1910 inst_env->slot_needed = 0;
1911 inst_env->prefix_found = 1;
1912}
1913
1914/* Calculates the prefix value for the index addressing mode. */
1915
a78f21af 1916static void
29134980
OF
1917biap_prefix (unsigned short inst, inst_env_type *inst_env)
1918{
1919 /* It's invalid to be in a delay slot. I can't see that it's possible to
1920 have a prefix to this instruction. So I will treat this as invalid. */
1921 if (inst_env->slot_needed || inst_env->prefix_found)
1922 {
1923 inst_env->invalid = 1;
1924 return;
1925 }
1926
1927 inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];
1928
1929 /* The offset is the operand2 value shifted the size of the instruction
1930 to the left. */
1931 inst_env->prefix_value +=
1932 inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);
1933
1934 /* If the PC is operand1 (base) the address used is the address after
1935 the main instruction, i.e. address + 2 (the PC is already compensated
1936 for the prefix operation). */
1937 if (cris_get_operand1 (inst) == REG_PC)
1938 {
1939 inst_env->prefix_value += 2;
1940 }
1941
1942 /* A prefix doesn't change the xflag_found. But the rest of the flags
1943 need updating. */
1944 inst_env->slot_needed = 0;
1945 inst_env->xflag_found = 0;
1946 inst_env->prefix_found = 1;
1947}
1948
1949/* Calculates the prefix value for the double indirect addressing mode. */
1950
a78f21af 1951static void
29134980
OF
1952dip_prefix (unsigned short inst, inst_env_type *inst_env)
1953{
1954
1955 CORE_ADDR address;
1956
1957 /* It's invalid to be in a delay slot. */
1958 if (inst_env->slot_needed || inst_env->prefix_found)
1959 {
1960 inst_env->invalid = 1;
1961 return;
1962 }
1963
1964 /* The prefix value is one dereference of the contents of the operand1
1965 register. */
1966 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
1967 inst_env->prefix_value = read_memory_unsigned_integer (address, 4);
1968
1969 /* Check if the mode is autoincrement. */
1970 if (cris_get_mode (inst) == AUTOINC_MODE)
1971 {
1972 inst_env->reg[cris_get_operand1 (inst)] += 4;
1973 }
1974
1975 /* A prefix doesn't change the xflag_found. But the rest of the flags
1976 need updating. */
1977 inst_env->slot_needed = 0;
1978 inst_env->xflag_found = 0;
1979 inst_env->prefix_found = 1;
1980}
1981
1982/* Finds the destination for a branch with 8-bits offset. */
1983
a78f21af 1984static void
29134980
OF
1985eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
1986{
1987
1988 short offset;
1989
1990 /* If we have a prefix or are in a delay slot it's bad. */
1991 if (inst_env->slot_needed || inst_env->prefix_found)
1992 {
1993 inst_env->invalid = 1;
1994 return;
1995 }
1996
1997 /* We have a branch, find out where the branch will land. */
1998 offset = cris_get_branch_short_offset (inst);
1999
2000 /* Check if the offset is signed. */
2001 if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
2002 {
2003 offset |= 0xFF00;
2004 }
2005
2006 /* The offset ends with the sign bit, set it to zero. The address
2007 should always be word aligned. */
2008 offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;
2009
2010 inst_env->branch_found = 1;
2011 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2012
2013 inst_env->slot_needed = 1;
2014 inst_env->prefix_found = 0;
2015 inst_env->xflag_found = 0;
2016 inst_env->disable_interrupt = 1;
2017}
2018
2019/* Finds the destination for a branch with 16-bits offset. */
2020
a78f21af 2021static void
29134980
OF
2022sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2023{
2024 short offset;
2025
2026 /* If we have a prefix or is in a delay slot it's bad. */
2027 if (inst_env->slot_needed || inst_env->prefix_found)
2028 {
2029 inst_env->invalid = 1;
2030 return;
2031 }
2032
2033 /* We have a branch, find out the offset for the branch. */
2034 offset = read_memory_integer (inst_env->reg[REG_PC], 2);
2035
2036 /* The instruction is one word longer than normal, so add one word
2037 to the PC. */
2038 inst_env->reg[REG_PC] += 2;
2039
2040 inst_env->branch_found = 1;
2041 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2042
2043
2044 inst_env->slot_needed = 1;
2045 inst_env->prefix_found = 0;
2046 inst_env->xflag_found = 0;
2047 inst_env->disable_interrupt = 1;
2048}
2049
2050/* Handles the ABS instruction. */
2051
a78f21af 2052static void
29134980
OF
2053abs_op (unsigned short inst, inst_env_type *inst_env)
2054{
2055
2056 long value;
2057
2058 /* ABS can't have a prefix, so it's bad if it does. */
2059 if (inst_env->prefix_found)
2060 {
2061 inst_env->invalid = 1;
2062 return;
2063 }
2064
2065 /* Check if the operation affects the PC. */
2066 if (cris_get_operand2 (inst) == REG_PC)
2067 {
2068
2069 /* It's invalid to change to the PC if we are in a delay slot. */
2070 if (inst_env->slot_needed)
2071 {
2072 inst_env->invalid = 1;
2073 return;
2074 }
2075
2076 value = (long) inst_env->reg[REG_PC];
2077
2078 /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK. */
2079 if (value != SIGNED_DWORD_MASK)
2080 {
2081 value = -value;
2082 inst_env->reg[REG_PC] = (long) value;
2083 }
2084 }
2085
2086 inst_env->slot_needed = 0;
2087 inst_env->prefix_found = 0;
2088 inst_env->xflag_found = 0;
2089 inst_env->disable_interrupt = 0;
2090}
2091
2092/* Handles the ADDI instruction. */
2093
a78f21af 2094static void
29134980
OF
2095addi_op (unsigned short inst, inst_env_type *inst_env)
2096{
2097 /* It's invalid to have the PC as base register. And ADDI can't have
2098 a prefix. */
2099 if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
2100 {
2101 inst_env->invalid = 1;
2102 return;
2103 }
2104
2105 inst_env->slot_needed = 0;
2106 inst_env->prefix_found = 0;
2107 inst_env->xflag_found = 0;
2108 inst_env->disable_interrupt = 0;
2109}
2110
2111/* Handles the ASR instruction. */
2112
a78f21af 2113static void
29134980
OF
2114asr_op (unsigned short inst, inst_env_type *inst_env)
2115{
2116 int shift_steps;
2117 unsigned long value;
2118 unsigned long signed_extend_mask = 0;
2119
2120 /* ASR can't have a prefix, so check that it doesn't. */
2121 if (inst_env->prefix_found)
2122 {
2123 inst_env->invalid = 1;
2124 return;
2125 }
2126
2127 /* Check if the PC is the target register. */
2128 if (cris_get_operand2 (inst) == REG_PC)
2129 {
2130 /* It's invalid to change the PC in a delay slot. */
2131 if (inst_env->slot_needed)
2132 {
2133 inst_env->invalid = 1;
2134 return;
2135 }
2136 /* Get the number of bits to shift. */
2137 shift_steps = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
2138 value = inst_env->reg[REG_PC];
2139
2140 /* Find out how many bits the operation should apply to. */
2141 if (cris_get_size (inst) == INST_BYTE_SIZE)
2142 {
2143 if (value & SIGNED_BYTE_MASK)
2144 {
2145 signed_extend_mask = 0xFF;
2146 signed_extend_mask = signed_extend_mask >> shift_steps;
2147 signed_extend_mask = ~signed_extend_mask;
2148 }
2149 value = value >> shift_steps;
2150 value |= signed_extend_mask;
2151 value &= 0xFF;
2152 inst_env->reg[REG_PC] &= 0xFFFFFF00;
2153 inst_env->reg[REG_PC] |= value;
2154 }
2155 else if (cris_get_size (inst) == INST_WORD_SIZE)
2156 {
2157 if (value & SIGNED_WORD_MASK)
2158 {
2159 signed_extend_mask = 0xFFFF;
2160 signed_extend_mask = signed_extend_mask >> shift_steps;
2161 signed_extend_mask = ~signed_extend_mask;
2162 }
2163 value = value >> shift_steps;
2164 value |= signed_extend_mask;
2165 value &= 0xFFFF;
2166 inst_env->reg[REG_PC] &= 0xFFFF0000;
2167 inst_env->reg[REG_PC] |= value;
2168 }
2169 else if (cris_get_size (inst) == INST_DWORD_SIZE)
2170 {
2171 if (value & SIGNED_DWORD_MASK)
2172 {
2173 signed_extend_mask = 0xFFFFFFFF;
2174 signed_extend_mask = signed_extend_mask >> shift_steps;
2175 signed_extend_mask = ~signed_extend_mask;
2176 }
2177 value = value >> shift_steps;
2178 value |= signed_extend_mask;
2179 inst_env->reg[REG_PC] = value;
2180 }
2181 }
2182 inst_env->slot_needed = 0;
2183 inst_env->prefix_found = 0;
2184 inst_env->xflag_found = 0;
2185 inst_env->disable_interrupt = 0;
2186}
2187
2188/* Handles the ASRQ instruction. */
2189
a78f21af 2190static void
29134980
OF
2191asrq_op (unsigned short inst, inst_env_type *inst_env)
2192{
2193
2194 int shift_steps;
2195 unsigned long value;
2196 unsigned long signed_extend_mask = 0;
2197
2198 /* ASRQ can't have a prefix, so check that it doesn't. */
2199 if (inst_env->prefix_found)
2200 {
2201 inst_env->invalid = 1;
2202 return;
2203 }
2204
2205 /* Check if the PC is the target register. */
2206 if (cris_get_operand2 (inst) == REG_PC)
2207 {
2208
2209 /* It's invalid to change the PC in a delay slot. */
2210 if (inst_env->slot_needed)
2211 {
2212 inst_env->invalid = 1;
2213 return;
2214 }
2215 /* The shift size is given as a 5 bit quick value, i.e. we don't
2216 want the the sign bit of the quick value. */
2217 shift_steps = cris_get_asr_shift_steps (inst);
2218 value = inst_env->reg[REG_PC];
2219 if (value & SIGNED_DWORD_MASK)
2220 {
2221 signed_extend_mask = 0xFFFFFFFF;
2222 signed_extend_mask = signed_extend_mask >> shift_steps;
2223 signed_extend_mask = ~signed_extend_mask;
2224 }
2225 value = value >> shift_steps;
2226 value |= signed_extend_mask;
2227 inst_env->reg[REG_PC] = value;
2228 }
2229 inst_env->slot_needed = 0;
2230 inst_env->prefix_found = 0;
2231 inst_env->xflag_found = 0;
2232 inst_env->disable_interrupt = 0;
2233}
2234
2235/* Handles the AX, EI and SETF instruction. */
2236
a78f21af 2237static void
29134980
OF
2238ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
2239{
2240 if (inst_env->prefix_found)
2241 {
2242 inst_env->invalid = 1;
2243 return;
2244 }
2245 /* Check if the instruction is setting the X flag. */
2246 if (cris_is_xflag_bit_on (inst))
2247 {
2248 inst_env->xflag_found = 1;
2249 }
2250 else
2251 {
2252 inst_env->xflag_found = 0;
2253 }
2254 inst_env->slot_needed = 0;
2255 inst_env->prefix_found = 0;
2256 inst_env->disable_interrupt = 1;
2257}
2258
2259/* Checks if the instruction is in assign mode. If so, it updates the assign
2260 register. Note that check_assign assumes that the caller has checked that
2261 there is a prefix to this instruction. The mode check depends on this. */
2262
a78f21af 2263static void
29134980
OF
2264check_assign (unsigned short inst, inst_env_type *inst_env)
2265{
2266 /* Check if it's an assign addressing mode. */
2267 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2268 {
2269 /* Assign the prefix value to operand 1. */
2270 inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
2271 }
2272}
2273
2274/* Handles the 2-operand BOUND instruction. */
2275
a78f21af 2276static void
29134980
OF
2277two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2278{
2279 /* It's invalid to have the PC as the index operand. */
2280 if (cris_get_operand2 (inst) == REG_PC)
2281 {
2282 inst_env->invalid = 1;
2283 return;
2284 }
2285 /* Check if we have a prefix. */
2286 if (inst_env->prefix_found)
2287 {
2288 check_assign (inst, inst_env);
2289 }
2290 /* Check if this is an autoincrement mode. */
2291 else if (cris_get_mode (inst) == AUTOINC_MODE)
2292 {
2293 /* It's invalid to change the PC in a delay slot. */
2294 if (inst_env->slot_needed)
2295 {
2296 inst_env->invalid = 1;
2297 return;
2298 }
2299 process_autoincrement (cris_get_size (inst), inst, inst_env);
2300 }
2301 inst_env->slot_needed = 0;
2302 inst_env->prefix_found = 0;
2303 inst_env->xflag_found = 0;
2304 inst_env->disable_interrupt = 0;
2305}
2306
2307/* Handles the 3-operand BOUND instruction. */
2308
a78f21af 2309static void
29134980
OF
2310three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2311{
2312 /* It's an error if we haven't got a prefix. And it's also an error
2313 if the PC is the destination register. */
2314 if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
2315 {
2316 inst_env->invalid = 1;
2317 return;
2318 }
2319 inst_env->slot_needed = 0;
2320 inst_env->prefix_found = 0;
2321 inst_env->xflag_found = 0;
2322 inst_env->disable_interrupt = 0;
2323}
2324
2325/* Clears the status flags in inst_env. */
2326
a78f21af 2327static void
29134980
OF
2328btst_nop_op (unsigned short inst, inst_env_type *inst_env)
2329{
2330 /* It's an error if we have got a prefix. */
2331 if (inst_env->prefix_found)
2332 {
2333 inst_env->invalid = 1;
2334 return;
2335 }
2336
2337 inst_env->slot_needed = 0;
2338 inst_env->prefix_found = 0;
2339 inst_env->xflag_found = 0;
2340 inst_env->disable_interrupt = 0;
2341}
2342
2343/* Clears the status flags in inst_env. */
2344
a78f21af 2345static void
29134980
OF
2346clearf_di_op (unsigned short inst, inst_env_type *inst_env)
2347{
2348 /* It's an error if we have got a prefix. */
2349 if (inst_env->prefix_found)
2350 {
2351 inst_env->invalid = 1;
2352 return;
2353 }
2354
2355 inst_env->slot_needed = 0;
2356 inst_env->prefix_found = 0;
2357 inst_env->xflag_found = 0;
2358 inst_env->disable_interrupt = 1;
2359}
2360
2361/* Handles the CLEAR instruction if it's in register mode. */
2362
a78f21af 2363static void
29134980
OF
2364reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
2365{
2366 /* Check if the target is the PC. */
2367 if (cris_get_operand2 (inst) == REG_PC)
2368 {
2369 /* The instruction will clear the instruction's size bits. */
2370 int clear_size = cris_get_clear_size (inst);
2371 if (clear_size == INST_BYTE_SIZE)
2372 {
2373 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
2374 }
2375 if (clear_size == INST_WORD_SIZE)
2376 {
2377 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
2378 }
2379 if (clear_size == INST_DWORD_SIZE)
2380 {
2381 inst_env->delay_slot_pc = 0x0;
2382 }
2383 /* The jump will be delayed with one delay slot. So we need a delay
2384 slot. */
2385 inst_env->slot_needed = 1;
2386 inst_env->delay_slot_pc_active = 1;
2387 }
2388 else
2389 {
2390 /* The PC will not change => no delay slot. */
2391 inst_env->slot_needed = 0;
2392 }
2393 inst_env->prefix_found = 0;
2394 inst_env->xflag_found = 0;
2395 inst_env->disable_interrupt = 0;
2396}
2397
2398/* Handles the TEST instruction if it's in register mode. */
2399
a78f21af 2400static void
29134980
OF
2401reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
2402{
2403 /* It's an error if we have got a prefix. */
2404 if (inst_env->prefix_found)
2405 {
2406 inst_env->invalid = 1;
2407 return;
2408 }
2409 inst_env->slot_needed = 0;
2410 inst_env->prefix_found = 0;
2411 inst_env->xflag_found = 0;
2412 inst_env->disable_interrupt = 0;
2413
2414}
2415
2416/* Handles the CLEAR and TEST instruction if the instruction isn't
2417 in register mode. */
2418
a78f21af 2419static void
29134980
OF
2420none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
2421{
2422 /* Check if we are in a prefix mode. */
2423 if (inst_env->prefix_found)
2424 {
2425 /* The only way the PC can change is if this instruction is in
2426 assign addressing mode. */
2427 check_assign (inst, inst_env);
2428 }
2429 /* Indirect mode can't change the PC so just check if the mode is
2430 autoincrement. */
2431 else if (cris_get_mode (inst) == AUTOINC_MODE)
2432 {
2433 process_autoincrement (cris_get_size (inst), inst, inst_env);
2434 }
2435 inst_env->slot_needed = 0;
2436 inst_env->prefix_found = 0;
2437 inst_env->xflag_found = 0;
2438 inst_env->disable_interrupt = 0;
2439}
2440
2441/* Checks that the PC isn't the destination register or the instructions has
2442 a prefix. */
2443
a78f21af 2444static void
29134980
OF
2445dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
2446{
2447 /* It's invalid to have the PC as the destination. The instruction can't
2448 have a prefix. */
2449 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2450 {
2451 inst_env->invalid = 1;
2452 return;
2453 }
2454
2455 inst_env->slot_needed = 0;
2456 inst_env->prefix_found = 0;
2457 inst_env->xflag_found = 0;
2458 inst_env->disable_interrupt = 0;
2459}
2460
2461/* Checks that the instruction doesn't have a prefix. */
2462
a78f21af 2463static void
29134980
OF
2464break_op (unsigned short inst, inst_env_type *inst_env)
2465{
2466 /* The instruction can't have a prefix. */
2467 if (inst_env->prefix_found)
2468 {
2469 inst_env->invalid = 1;
2470 return;
2471 }
2472
2473 inst_env->slot_needed = 0;
2474 inst_env->prefix_found = 0;
2475 inst_env->xflag_found = 0;
2476 inst_env->disable_interrupt = 1;
2477}
2478
2479/* Checks that the PC isn't the destination register and that the instruction
2480 doesn't have a prefix. */
2481
a78f21af 2482static void
29134980
OF
2483scc_op (unsigned short inst, inst_env_type *inst_env)
2484{
2485 /* It's invalid to have the PC as the destination. The instruction can't
2486 have a prefix. */
2487 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2488 {
2489 inst_env->invalid = 1;
2490 return;
2491 }
2492
2493 inst_env->slot_needed = 0;
2494 inst_env->prefix_found = 0;
2495 inst_env->xflag_found = 0;
2496 inst_env->disable_interrupt = 1;
2497}
2498
2499/* Handles the register mode JUMP instruction. */
2500
a78f21af 2501static void
29134980
OF
2502reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2503{
2504 /* It's invalid to do a JUMP in a delay slot. The mode is register, so
2505 you can't have a prefix. */
2506 if ((inst_env->slot_needed) || (inst_env->prefix_found))
2507 {
2508 inst_env->invalid = 1;
2509 return;
2510 }
2511
2512 /* Just change the PC. */
2513 inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
2514 inst_env->slot_needed = 0;
2515 inst_env->prefix_found = 0;
2516 inst_env->xflag_found = 0;
2517 inst_env->disable_interrupt = 1;
2518}
2519
2520/* Handles the JUMP instruction for all modes except register. */
2521
a78f21af
AC
2522static void
2523none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
29134980
OF
2524{
2525 unsigned long newpc;
2526 CORE_ADDR address;
2527
2528 /* It's invalid to do a JUMP in a delay slot. */
2529 if (inst_env->slot_needed)
2530 {
2531 inst_env->invalid = 1;
2532 }
2533 else
2534 {
2535 /* Check if we have a prefix. */
2536 if (inst_env->prefix_found)
2537 {
2538 check_assign (inst, inst_env);
2539
2540 /* Get the new value for the the PC. */
2541 newpc =
2542 read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
2543 4);
2544 }
2545 else
2546 {
2547 /* Get the new value for the PC. */
2548 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2549 newpc = read_memory_unsigned_integer (address, 4);
2550
2551 /* Check if we should increment a register. */
2552 if (cris_get_mode (inst) == AUTOINC_MODE)
2553 {
2554 inst_env->reg[cris_get_operand1 (inst)] += 4;
2555 }
2556 }
2557 inst_env->reg[REG_PC] = newpc;
2558 }
2559 inst_env->slot_needed = 0;
2560 inst_env->prefix_found = 0;
2561 inst_env->xflag_found = 0;
2562 inst_env->disable_interrupt = 1;
2563}
2564
2565/* Handles moves to special registers (aka P-register) for all modes. */
2566
a78f21af 2567static void
29134980
OF
2568move_to_preg_op (unsigned short inst, inst_env_type *inst_env)
2569{
2570 if (inst_env->prefix_found)
2571 {
2572 /* The instruction has a prefix that means we are only interested if
2573 the instruction is in assign mode. */
2574 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2575 {
2576 /* The prefix handles the problem if we are in a delay slot. */
2577 if (cris_get_operand1 (inst) == REG_PC)
2578 {
2579 /* Just take care of the assign. */
2580 check_assign (inst, inst_env);
2581 }
2582 }
2583 }
2584 else if (cris_get_mode (inst) == AUTOINC_MODE)
2585 {
2586 /* The instruction doesn't have a prefix, the only case left that we
2587 are interested in is the autoincrement mode. */
2588 if (cris_get_operand1 (inst) == REG_PC)
2589 {
2590 /* If the PC is to be incremented it's invalid to be in a
2591 delay slot. */
2592 if (inst_env->slot_needed)
2593 {
2594 inst_env->invalid = 1;
2595 return;
2596 }
2a9ecef2
OF
2597
2598 /* The increment depends on the size of the special register. */
2599 if (cris_register_size (cris_get_operand2 (inst)) == 1)
29134980
OF
2600 {
2601 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2602 }
2a9ecef2 2603 else if (cris_register_size (cris_get_operand2 (inst)) == 2)
29134980
OF
2604 {
2605 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2606 }
2607 else
2608 {
2609 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2610 }
2611 }
2612 }
2613 inst_env->slot_needed = 0;
2614 inst_env->prefix_found = 0;
2615 inst_env->xflag_found = 0;
2616 inst_env->disable_interrupt = 1;
2617}
2618
2619/* Handles moves from special registers (aka P-register) for all modes
2620 except register. */
2621
a78f21af 2622static void
29134980
OF
2623none_reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2624{
2625 if (inst_env->prefix_found)
2626 {
2627 /* The instruction has a prefix that means we are only interested if
2628 the instruction is in assign mode. */
2629 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2630 {
2631 /* The prefix handles the problem if we are in a delay slot. */
2632 if (cris_get_operand1 (inst) == REG_PC)
2633 {
2634 /* Just take care of the assign. */
2635 check_assign (inst, inst_env);
2636 }
2637 }
2638 }
2639 /* The instruction doesn't have a prefix, the only case left that we
2640 are interested in is the autoincrement mode. */
2641 else if (cris_get_mode (inst) == AUTOINC_MODE)
2642 {
2643 if (cris_get_operand1 (inst) == REG_PC)
2644 {
2645 /* If the PC is to be incremented it's invalid to be in a
2646 delay slot. */
2647 if (inst_env->slot_needed)
2648 {
2649 inst_env->invalid = 1;
2650 return;
2651 }
2a9ecef2
OF
2652
2653 /* The increment depends on the size of the special register. */
2654 if (cris_register_size (cris_get_operand2 (inst)) == 1)
29134980
OF
2655 {
2656 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2657 }
2a9ecef2 2658 else if (cris_register_size (cris_get_operand2 (inst)) == 2)
29134980
OF
2659 {
2660 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2661 }
2662 else
2663 {
2664 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2665 }
2666 }
2667 }
2668 inst_env->slot_needed = 0;
2669 inst_env->prefix_found = 0;
2670 inst_env->xflag_found = 0;
2671 inst_env->disable_interrupt = 1;
2672}
2673
2674/* Handles moves from special registers (aka P-register) when the mode
2675 is register. */
2676
a78f21af 2677static void
29134980
OF
2678reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2679{
2680 /* Register mode move from special register can't have a prefix. */
2681 if (inst_env->prefix_found)
2682 {
2683 inst_env->invalid = 1;
2684 return;
2685 }
2686
2687 if (cris_get_operand1 (inst) == REG_PC)
2688 {
2689 /* It's invalid to change the PC in a delay slot. */
2690 if (inst_env->slot_needed)
2691 {
2692 inst_env->invalid = 1;
2693 return;
2694 }
2695 /* The destination is the PC, the jump will have a delay slot. */
2696 inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
2697 inst_env->slot_needed = 1;
2698 inst_env->delay_slot_pc_active = 1;
2699 }
2700 else
2701 {
2702 /* If the destination isn't PC, there will be no jump. */
2703 inst_env->slot_needed = 0;
2704 }
2705 inst_env->prefix_found = 0;
2706 inst_env->xflag_found = 0;
2707 inst_env->disable_interrupt = 1;
2708}
2709
2710/* Handles the MOVEM from memory to general register instruction. */
2711
a78f21af 2712static void
29134980
OF
2713move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
2714{
2715 if (inst_env->prefix_found)
2716 {
2717 /* The prefix handles the problem if we are in a delay slot. Is the
2718 MOVEM instruction going to change the PC? */
2719 if (cris_get_operand2 (inst) >= REG_PC)
2720 {
2721 inst_env->reg[REG_PC] =
2722 read_memory_unsigned_integer (inst_env->prefix_value, 4);
2723 }
2724 /* The assign value is the value after the increment. Normally, the
2725 assign value is the value before the increment. */
2726 if ((cris_get_operand1 (inst) == REG_PC)
2727 && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2728 {
2729 inst_env->reg[REG_PC] = inst_env->prefix_value;
2730 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2731 }
2732 }
2733 else
2734 {
2735 /* Is the MOVEM instruction going to change the PC? */
2736 if (cris_get_operand2 (inst) == REG_PC)
2737 {
2738 /* It's invalid to change the PC in a delay slot. */
2739 if (inst_env->slot_needed)
2740 {
2741 inst_env->invalid = 1;
2742 return;
2743 }
2744 inst_env->reg[REG_PC] =
2745 read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)],
2746 4);
2747 }
2748 /* The increment is not depending on the size, instead it's depending
2749 on the number of registers loaded from memory. */
2750 if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
2751 {
2752 /* It's invalid to change the PC in a delay slot. */
2753 if (inst_env->slot_needed)
2754 {
2755 inst_env->invalid = 1;
2756 return;
2757 }
2758 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2759 }
2760 }
2761 inst_env->slot_needed = 0;
2762 inst_env->prefix_found = 0;
2763 inst_env->xflag_found = 0;
2764 inst_env->disable_interrupt = 0;
2765}
2766
2767/* Handles the MOVEM to memory from general register instruction. */
2768
a78f21af 2769static void
29134980
OF
2770move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
2771{
2772 if (inst_env->prefix_found)
2773 {
2774 /* The assign value is the value after the increment. Normally, the
2775 assign value is the value before the increment. */
2776 if ((cris_get_operand1 (inst) == REG_PC) &&
2777 (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2778 {
2779 /* The prefix handles the problem if we are in a delay slot. */
2780 inst_env->reg[REG_PC] = inst_env->prefix_value;
2781 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2782 }
2783 }
2784 else
2785 {
2786 /* The increment is not depending on the size, instead it's depending
2787 on the number of registers loaded to memory. */
2788 if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
2789 {
2790 /* It's invalid to change the PC in a delay slot. */
2791 if (inst_env->slot_needed)
2792 {
2793 inst_env->invalid = 1;
2794 return;
2795 }
2796 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2797 }
2798 }
2799 inst_env->slot_needed = 0;
2800 inst_env->prefix_found = 0;
2801 inst_env->xflag_found = 0;
2802 inst_env->disable_interrupt = 0;
2803}
2804
2805/* Handles the pop instruction to a general register.
2806 POP is a assembler macro for MOVE.D [SP+], Rd. */
2807
a78f21af 2808static void
29134980
OF
2809reg_pop_op (unsigned short inst, inst_env_type *inst_env)
2810{
2811 /* POP can't have a prefix. */
2812 if (inst_env->prefix_found)
2813 {
2814 inst_env->invalid = 1;
2815 return;
2816 }
2817 if (cris_get_operand2 (inst) == REG_PC)
2818 {
2819 /* It's invalid to change the PC in a delay slot. */
2820 if (inst_env->slot_needed)
2821 {
2822 inst_env->invalid = 1;
2823 return;
2824 }
2825 inst_env->reg[REG_PC] =
2826 read_memory_unsigned_integer (inst_env->reg[REG_SP], 4);
2827 }
2828 inst_env->slot_needed = 0;
2829 inst_env->prefix_found = 0;
2830 inst_env->xflag_found = 0;
2831 inst_env->disable_interrupt = 0;
2832}
2833
2834/* Handles moves from register to memory. */
2835
a78f21af 2836static void
29134980
OF
2837move_reg_to_mem_index_inc_op (unsigned short inst, inst_env_type *inst_env)
2838{
2839 /* Check if we have a prefix. */
2840 if (inst_env->prefix_found)
2841 {
2842 /* The only thing that can change the PC is an assign. */
2843 check_assign (inst, inst_env);
2844 }
2845 else if ((cris_get_operand1 (inst) == REG_PC)
2846 && (cris_get_mode (inst) == AUTOINC_MODE))
2847 {
2848 /* It's invalid to change the PC in a delay slot. */
2849 if (inst_env->slot_needed)
2850 {
2851 inst_env->invalid = 1;
2852 return;
2853 }
2854 process_autoincrement (cris_get_size (inst), inst, inst_env);
2855 }
2856 inst_env->slot_needed = 0;
2857 inst_env->prefix_found = 0;
2858 inst_env->xflag_found = 0;
2859 inst_env->disable_interrupt = 0;
2860}
2861
2862/* Handles the intructions that's not yet implemented, by setting
2863 inst_env->invalid to true. */
2864
a78f21af 2865static void
29134980
OF
2866not_implemented_op (unsigned short inst, inst_env_type *inst_env)
2867{
2868 inst_env->invalid = 1;
2869}
2870
2871/* Handles the XOR instruction. */
2872
a78f21af 2873static void
29134980
OF
2874xor_op (unsigned short inst, inst_env_type *inst_env)
2875{
2876 /* XOR can't have a prefix. */
2877 if (inst_env->prefix_found)
2878 {
2879 inst_env->invalid = 1;
2880 return;
2881 }
2882
2883 /* Check if the PC is the target. */
2884 if (cris_get_operand2 (inst) == REG_PC)
2885 {
2886 /* It's invalid to change the PC in a delay slot. */
2887 if (inst_env->slot_needed)
2888 {
2889 inst_env->invalid = 1;
2890 return;
2891 }
2892 inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
2893 }
2894 inst_env->slot_needed = 0;
2895 inst_env->prefix_found = 0;
2896 inst_env->xflag_found = 0;
2897 inst_env->disable_interrupt = 0;
2898}
2899
2900/* Handles the MULS instruction. */
2901
a78f21af 2902static void
29134980
OF
2903muls_op (unsigned short inst, inst_env_type *inst_env)
2904{
2905 /* MULS/U can't have a prefix. */
2906 if (inst_env->prefix_found)
2907 {
2908 inst_env->invalid = 1;
2909 return;
2910 }
2911
2912 /* Consider it invalid if the PC is the target. */
2913 if (cris_get_operand2 (inst) == REG_PC)
2914 {
2915 inst_env->invalid = 1;
2916 return;
2917 }
2918 inst_env->slot_needed = 0;
2919 inst_env->prefix_found = 0;
2920 inst_env->xflag_found = 0;
2921 inst_env->disable_interrupt = 0;
2922}
2923
2924/* Handles the MULU instruction. */
2925
a78f21af 2926static void
29134980
OF
2927mulu_op (unsigned short inst, inst_env_type *inst_env)
2928{
2929 /* MULS/U can't have a prefix. */
2930 if (inst_env->prefix_found)
2931 {
2932 inst_env->invalid = 1;
2933 return;
2934 }
2935
2936 /* Consider it invalid if the PC is the target. */
2937 if (cris_get_operand2 (inst) == REG_PC)
2938 {
2939 inst_env->invalid = 1;
2940 return;
2941 }
2942 inst_env->slot_needed = 0;
2943 inst_env->prefix_found = 0;
2944 inst_env->xflag_found = 0;
2945 inst_env->disable_interrupt = 0;
2946}
2947
2948/* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE.
2949 The MOVE instruction is the move from source to register. */
2950
a78f21af 2951static void
29134980
OF
2952add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env,
2953 unsigned long source1, unsigned long source2)
2954{
2955 unsigned long pc_mask;
2956 unsigned long operation_mask;
2957
2958 /* Find out how many bits the operation should apply to. */
2959 if (cris_get_size (inst) == INST_BYTE_SIZE)
2960 {
2961 pc_mask = 0xFFFFFF00;
2962 operation_mask = 0xFF;
2963 }
2964 else if (cris_get_size (inst) == INST_WORD_SIZE)
2965 {
2966 pc_mask = 0xFFFF0000;
2967 operation_mask = 0xFFFF;
2968 }
2969 else if (cris_get_size (inst) == INST_DWORD_SIZE)
2970 {
2971 pc_mask = 0x0;
2972 operation_mask = 0xFFFFFFFF;
2973 }
2974 else
2975 {
2976 /* The size is out of range. */
2977 inst_env->invalid = 1;
2978 return;
2979 }
2980
2981 /* The instruction just works on uw_operation_mask bits. */
2982 source2 &= operation_mask;
2983 source1 &= operation_mask;
2984
2985 /* Now calculate the result. The opcode's 3 first bits separates
2986 the different actions. */
2987 switch (cris_get_opcode (inst) & 7)
2988 {
2989 case 0: /* add */
2990 source1 += source2;
2991 break;
2992
2993 case 1: /* move */
2994 source1 = source2;
2995 break;
2996
2997 case 2: /* subtract */
2998 source1 -= source2;
2999 break;
3000
3001 case 3: /* compare */
3002 break;
3003
3004 case 4: /* and */
3005 source1 &= source2;
3006 break;
3007
3008 case 5: /* or */
3009 source1 |= source2;
3010 break;
3011
3012 default:
3013 inst_env->invalid = 1;
3014 return;
3015
3016 break;
3017 }
3018
3019 /* Make sure that the result doesn't contain more than the instruction
3020 size bits. */
3021 source2 &= operation_mask;
3022
3023 /* Calculate the new breakpoint address. */
3024 inst_env->reg[REG_PC] &= pc_mask;
3025 inst_env->reg[REG_PC] |= source1;
3026
3027}
3028
3029/* Extends the value from either byte or word size to a dword. If the mode
3030 is zero extend then the value is extended with zero. If instead the mode
3031 is signed extend the sign bit of the value is taken into consideration. */
3032
a78f21af 3033static unsigned long
29134980
OF
3034do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
3035{
3036 /* The size can be either byte or word, check which one it is.
3037 Don't check the highest bit, it's indicating if it's a zero
3038 or sign extend. */
3039 if (cris_get_size (*inst) & INST_WORD_SIZE)
3040 {
3041 /* Word size. */
3042 value &= 0xFFFF;
3043
3044 /* Check if the instruction is signed extend. If so, check if value has
3045 the sign bit on. */
3046 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
3047 {
3048 value |= SIGNED_WORD_EXTEND_MASK;
3049 }
3050 }
3051 else
3052 {
3053 /* Byte size. */
3054 value &= 0xFF;
3055
3056 /* Check if the instruction is signed extend. If so, check if value has
3057 the sign bit on. */
3058 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
3059 {
3060 value |= SIGNED_BYTE_EXTEND_MASK;
3061 }
3062 }
3063 /* The size should now be dword. */
3064 cris_set_size_to_dword (inst);
3065 return value;
3066}
3067
3068/* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
3069 instruction. The MOVE instruction is the move from source to register. */
3070
a78f21af 3071static void
29134980
OF
3072reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3073 inst_env_type *inst_env)
3074{
3075 unsigned long operand1;
3076 unsigned long operand2;
3077
3078 /* It's invalid to have a prefix to the instruction. This is a register
3079 mode instruction and can't have a prefix. */
3080 if (inst_env->prefix_found)
3081 {
3082 inst_env->invalid = 1;
3083 return;
3084 }
3085 /* Check if the instruction has PC as its target. */
3086 if (cris_get_operand2 (inst) == REG_PC)
3087 {
3088 if (inst_env->slot_needed)
3089 {
3090 inst_env->invalid = 1;
3091 return;
3092 }
3093 /* The instruction has the PC as its target register. */
7ab98e9e 3094 operand1 = inst_env->reg[cris_get_operand1 (inst)];
29134980
OF
3095 operand2 = inst_env->reg[REG_PC];
3096
3097 /* Check if it's a extend, signed or zero instruction. */
3098 if (cris_get_opcode (inst) < 4)
3099 {
3100 operand1 = do_sign_or_zero_extend (operand1, &inst);
3101 }
3102 /* Calculate the PC value after the instruction, i.e. where the
3103 breakpoint should be. The order of the udw_operands is vital. */
3104 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3105 }
3106 inst_env->slot_needed = 0;
3107 inst_env->prefix_found = 0;
3108 inst_env->xflag_found = 0;
3109 inst_env->disable_interrupt = 0;
3110}
3111
3112/* Returns the data contained at address. The size of the data is derived from
3113 the size of the operation. If the instruction is a zero or signed
3114 extend instruction, the size field is changed in instruction. */
3115
a78f21af 3116static unsigned long
29134980
OF
3117get_data_from_address (unsigned short *inst, CORE_ADDR address)
3118{
3119 int size = cris_get_size (*inst);
3120 unsigned long value;
3121
3122 /* If it's an extend instruction we don't want the signed extend bit,
3123 because it influences the size. */
3124 if (cris_get_opcode (*inst) < 4)
3125 {
3126 size &= ~SIGNED_EXTEND_BIT_MASK;
3127 }
3128 /* Is there a need for checking the size? Size should contain the number of
3129 bytes to read. */
3130 size = 1 << size;
3131 value = read_memory_unsigned_integer (address, size);
3132
3133 /* Check if it's an extend, signed or zero instruction. */
3134 if (cris_get_opcode (*inst) < 4)
3135 {
3136 value = do_sign_or_zero_extend (value, inst);
3137 }
3138 return value;
3139}
3140
3141/* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3142 instructions. The MOVE instruction is the move from source to register. */
3143
a78f21af 3144static void
29134980
OF
3145handle_prefix_assign_mode_for_aritm_op (unsigned short inst,
3146 inst_env_type *inst_env)
3147{
3148 unsigned long operand2;
3149 unsigned long operand3;
3150
3151 check_assign (inst, inst_env);
3152 if (cris_get_operand2 (inst) == REG_PC)
3153 {
3154 operand2 = inst_env->reg[REG_PC];
3155
3156 /* Get the value of the third operand. */
3157 operand3 = get_data_from_address (&inst, inst_env->prefix_value);
3158
3159 /* Calculate the PC value after the instruction, i.e. where the
3160 breakpoint should be. The order of the udw_operands is vital. */
3161 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3162 }
3163 inst_env->slot_needed = 0;
3164 inst_env->prefix_found = 0;
3165 inst_env->xflag_found = 0;
3166 inst_env->disable_interrupt = 0;
3167}
3168
3169/* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
3170 OR instructions. Note that for this to work as expected, the calling
3171 function must have made sure that there is a prefix to this instruction. */
3172
a78f21af 3173static void
29134980
OF
3174three_operand_add_sub_cmp_and_or_op (unsigned short inst,
3175 inst_env_type *inst_env)
3176{
3177 unsigned long operand2;
3178 unsigned long operand3;
3179
3180 if (cris_get_operand1 (inst) == REG_PC)
3181 {
3182 /* The PC will be changed by the instruction. */
3183 operand2 = inst_env->reg[cris_get_operand2 (inst)];
3184
3185 /* Get the value of the third operand. */
3186 operand3 = get_data_from_address (&inst, inst_env->prefix_value);
3187
3188 /* Calculate the PC value after the instruction, i.e. where the
3189 breakpoint should be. */
3190 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3191 }
3192 inst_env->slot_needed = 0;
3193 inst_env->prefix_found = 0;
3194 inst_env->xflag_found = 0;
3195 inst_env->disable_interrupt = 0;
3196}
3197
3198/* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3199 instructions. The MOVE instruction is the move from source to register. */
3200
a78f21af 3201static void
29134980
OF
3202handle_prefix_index_mode_for_aritm_op (unsigned short inst,
3203 inst_env_type *inst_env)
3204{
3205 if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
3206 {
3207 /* If the instruction is MOVE it's invalid. If the instruction is ADD,
3208 SUB, AND or OR something weird is going on (if everything works these
3209 instructions should end up in the three operand version). */
3210 inst_env->invalid = 1;
3211 return;
3212 }
3213 else
3214 {
3215 /* three_operand_add_sub_cmp_and_or does the same as we should do here
3216 so use it. */
3217 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3218 }
3219 inst_env->slot_needed = 0;
3220 inst_env->prefix_found = 0;
3221 inst_env->xflag_found = 0;
3222 inst_env->disable_interrupt = 0;
3223}
3224
3225/* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
3226 CMP, AND OR and MOVE instruction. The MOVE instruction is the move from
3227 source to register. */
3228
a78f21af 3229static void
29134980
OF
3230handle_inc_and_index_mode_for_aritm_op (unsigned short inst,
3231 inst_env_type *inst_env)
3232{
3233 unsigned long operand1;
3234 unsigned long operand2;
3235 unsigned long operand3;
3236 int size;
3237
3238 /* The instruction is either an indirect or autoincrement addressing mode.
3239 Check if the destination register is the PC. */
3240 if (cris_get_operand2 (inst) == REG_PC)
3241 {
3242 /* Must be done here, get_data_from_address may change the size
3243 field. */
3244 size = cris_get_size (inst);
3245 operand2 = inst_env->reg[REG_PC];
3246
3247 /* Get the value of the third operand, i.e. the indirect operand. */
3248 operand1 = inst_env->reg[cris_get_operand1 (inst)];
3249 operand3 = get_data_from_address (&inst, operand1);
3250
3251 /* Calculate the PC value after the instruction, i.e. where the
3252 breakpoint should be. The order of the udw_operands is vital. */
3253 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3254 }
3255 /* If this is an autoincrement addressing mode, check if the increment
3256 changes the PC. */
3257 if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
3258 {
3259 /* Get the size field. */
3260 size = cris_get_size (inst);
3261
3262 /* If it's an extend instruction we don't want the signed extend bit,
3263 because it influences the size. */
3264 if (cris_get_opcode (inst) < 4)
3265 {
3266 size &= ~SIGNED_EXTEND_BIT_MASK;
3267 }
3268 process_autoincrement (size, inst, inst_env);
3269 }
3270 inst_env->slot_needed = 0;
3271 inst_env->prefix_found = 0;
3272 inst_env->xflag_found = 0;
3273 inst_env->disable_interrupt = 0;
3274}
3275
3276/* Handles the two-operand addressing mode, all modes except register, for
3277 the ADD, SUB CMP, AND and OR instruction. */
3278
a78f21af 3279static void
29134980
OF
3280none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3281 inst_env_type *inst_env)
3282{
3283 if (inst_env->prefix_found)
3284 {
3285 if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
3286 {
3287 handle_prefix_index_mode_for_aritm_op (inst, inst_env);
3288 }
3289 else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
3290 {
3291 handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
3292 }
3293 else
3294 {
3295 /* The mode is invalid for a prefixed base instruction. */
3296 inst_env->invalid = 1;
3297 return;
3298 }
3299 }
3300 else
3301 {
3302 handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
3303 }
3304}
3305
3306/* Handles the quick addressing mode for the ADD and SUB instruction. */
3307
a78f21af 3308static void
29134980
OF
3309quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
3310{
3311 unsigned long operand1;
3312 unsigned long operand2;
3313
3314 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3315 instruction and can't have a prefix. */
3316 if (inst_env->prefix_found)
3317 {
3318 inst_env->invalid = 1;
3319 return;
3320 }
3321
3322 /* Check if the instruction has PC as its target. */
3323 if (cris_get_operand2 (inst) == REG_PC)
3324 {
3325 if (inst_env->slot_needed)
3326 {
3327 inst_env->invalid = 1;
3328 return;
3329 }
3330 operand1 = cris_get_quick_value (inst);
3331 operand2 = inst_env->reg[REG_PC];
3332
3333 /* The size should now be dword. */
3334 cris_set_size_to_dword (&inst);
3335
3336 /* Calculate the PC value after the instruction, i.e. where the
3337 breakpoint should be. */
3338 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3339 }
3340 inst_env->slot_needed = 0;
3341 inst_env->prefix_found = 0;
3342 inst_env->xflag_found = 0;
3343 inst_env->disable_interrupt = 0;
3344}
3345
3346/* Handles the quick addressing mode for the CMP, AND and OR instruction. */
3347
a78f21af 3348static void
29134980
OF
3349quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
3350{
3351 unsigned long operand1;
3352 unsigned long operand2;
3353
3354 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3355 instruction and can't have a prefix. */
3356 if (inst_env->prefix_found)
3357 {
3358 inst_env->invalid = 1;
3359 return;
3360 }
3361 /* Check if the instruction has PC as its target. */
3362 if (cris_get_operand2 (inst) == REG_PC)
3363 {
3364 if (inst_env->slot_needed)
3365 {
3366 inst_env->invalid = 1;
3367 return;
3368 }
3369 /* The instruction has the PC as its target register. */
3370 operand1 = cris_get_quick_value (inst);
3371 operand2 = inst_env->reg[REG_PC];
3372
3373 /* The quick value is signed, so check if we must do a signed extend. */
3374 if (operand1 & SIGNED_QUICK_VALUE_MASK)
3375 {
3376 /* sign extend */
3377 operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
3378 }
3379 /* The size should now be dword. */
3380 cris_set_size_to_dword (&inst);
3381
3382 /* Calculate the PC value after the instruction, i.e. where the
3383 breakpoint should be. */
3384 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3385 }
3386 inst_env->slot_needed = 0;
3387 inst_env->prefix_found = 0;
3388 inst_env->xflag_found = 0;
3389 inst_env->disable_interrupt = 0;
3390}
3391
3392/* Translate op_type to a function and call it. */
3393
a78f21af
AC
3394static void
3395cris_gdb_func (enum cris_op_type op_type, unsigned short inst,
3396 inst_env_type *inst_env)
29134980
OF
3397{
3398 switch (op_type)
3399 {
3400 case cris_not_implemented_op:
3401 not_implemented_op (inst, inst_env);
3402 break;
3403
3404 case cris_abs_op:
3405 abs_op (inst, inst_env);
3406 break;
3407
3408 case cris_addi_op:
3409 addi_op (inst, inst_env);
3410 break;
3411
3412 case cris_asr_op:
3413 asr_op (inst, inst_env);
3414 break;
3415
3416 case cris_asrq_op:
3417 asrq_op (inst, inst_env);
3418 break;
3419
3420 case cris_ax_ei_setf_op:
3421 ax_ei_setf_op (inst, inst_env);
3422 break;
3423
3424 case cris_bdap_prefix:
3425 bdap_prefix (inst, inst_env);
3426 break;
3427
3428 case cris_biap_prefix:
3429 biap_prefix (inst, inst_env);
3430 break;
3431
3432 case cris_break_op:
3433 break_op (inst, inst_env);
3434 break;
3435
3436 case cris_btst_nop_op:
3437 btst_nop_op (inst, inst_env);
3438 break;
3439
3440 case cris_clearf_di_op:
3441 clearf_di_op (inst, inst_env);
3442 break;
3443
3444 case cris_dip_prefix:
3445 dip_prefix (inst, inst_env);
3446 break;
3447
3448 case cris_dstep_logshift_mstep_neg_not_op:
3449 dstep_logshift_mstep_neg_not_op (inst, inst_env);
3450 break;
3451
3452 case cris_eight_bit_offset_branch_op:
3453 eight_bit_offset_branch_op (inst, inst_env);
3454 break;
3455
3456 case cris_move_mem_to_reg_movem_op:
3457 move_mem_to_reg_movem_op (inst, inst_env);
3458 break;
3459
3460 case cris_move_reg_to_mem_movem_op:
3461 move_reg_to_mem_movem_op (inst, inst_env);
3462 break;
3463
3464 case cris_move_to_preg_op:
3465 move_to_preg_op (inst, inst_env);
3466 break;
3467
3468 case cris_muls_op:
3469 muls_op (inst, inst_env);
3470 break;
3471
3472 case cris_mulu_op:
3473 mulu_op (inst, inst_env);
3474 break;
3475
3476 case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
3477 none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3478 break;
3479
3480 case cris_none_reg_mode_clear_test_op:
3481 none_reg_mode_clear_test_op (inst, inst_env);
3482 break;
3483
3484 case cris_none_reg_mode_jump_op:
3485 none_reg_mode_jump_op (inst, inst_env);
3486 break;
3487
3488 case cris_none_reg_mode_move_from_preg_op:
3489 none_reg_mode_move_from_preg_op (inst, inst_env);
3490 break;
3491
3492 case cris_quick_mode_add_sub_op:
3493 quick_mode_add_sub_op (inst, inst_env);
3494 break;
3495
3496 case cris_quick_mode_and_cmp_move_or_op:
3497 quick_mode_and_cmp_move_or_op (inst, inst_env);
3498 break;
3499
3500 case cris_quick_mode_bdap_prefix:
3501 quick_mode_bdap_prefix (inst, inst_env);
3502 break;
3503
3504 case cris_reg_mode_add_sub_cmp_and_or_move_op:
3505 reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3506 break;
3507
3508 case cris_reg_mode_clear_op:
3509 reg_mode_clear_op (inst, inst_env);
3510 break;
3511
3512 case cris_reg_mode_jump_op:
3513 reg_mode_jump_op (inst, inst_env);
3514 break;
3515
3516 case cris_reg_mode_move_from_preg_op:
3517 reg_mode_move_from_preg_op (inst, inst_env);
3518 break;
3519
3520 case cris_reg_mode_test_op:
3521 reg_mode_test_op (inst, inst_env);
3522 break;
3523
3524 case cris_scc_op:
3525 scc_op (inst, inst_env);
3526 break;
3527
3528 case cris_sixteen_bit_offset_branch_op:
3529 sixteen_bit_offset_branch_op (inst, inst_env);
3530 break;
3531
3532 case cris_three_operand_add_sub_cmp_and_or_op:
3533 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3534 break;
3535
3536 case cris_three_operand_bound_op:
3537 three_operand_bound_op (inst, inst_env);
3538 break;
3539
3540 case cris_two_operand_bound_op:
3541 two_operand_bound_op (inst, inst_env);
3542 break;
3543
3544 case cris_xor_op:
3545 xor_op (inst, inst_env);
3546 break;
3547 }
3548}
3549
3550/* This wrapper is to avoid cris_get_assembler being called before
3551 exec_bfd has been set. */
3552
3553static int
3554cris_delayed_get_disassembler (bfd_vma addr, disassemble_info *info)
3555{
d7a27068 3556 deprecated_tm_print_insn = cris_get_disassembler (exec_bfd);
2bf0cb65 3557 return TARGET_PRINT_INSN (addr, info);
29134980
OF
3558}
3559
dbbff683
OF
3560/* Copied from <asm/elf.h>. */
3561typedef unsigned long elf_greg_t;
3562
3563/* Same as user_regs_struct struct in <asm/user.h>. */
3564typedef elf_greg_t elf_gregset_t[35];
3565
3566/* Unpack an elf_gregset_t into GDB's register cache. */
3567
a78f21af 3568static void
dbbff683
OF
3569supply_gregset (elf_gregset_t *gregsetp)
3570{
3571 int i;
3572 elf_greg_t *regp = *gregsetp;
3573 static char zerobuf[4] = {0};
3574
3575 /* The kernel dumps all 32 registers as unsigned longs, but supply_register
3576 knows about the actual size of each register so that's no problem. */
3577 for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
3578 {
3579 supply_register (i, (char *)&regp[i]);
3580 }
3581}
3582
3583/* Use a local version of this function to get the correct types for
3584 regsets, until multi-arch core support is ready. */
3585
3586static void
3587fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
3588 int which, CORE_ADDR reg_addr)
3589{
3590 elf_gregset_t gregset;
3591
3592 switch (which)
3593 {
3594 case 0:
3595 if (core_reg_size != sizeof (gregset))
3596 {
3597 warning ("wrong size gregset struct in core file");
3598 }
3599 else
3600 {
3601 memcpy (&gregset, core_reg_sect, sizeof (gregset));
3602 supply_gregset (&gregset);
3603 }
3604
3605 default:
3606 /* We've covered all the kinds of registers we know about here,
3607 so this must be something we wouldn't know what to do with
3608 anyway. Just ignore it. */
3609 break;
3610 }
3611}
3612
3613static struct core_fns cris_elf_core_fns =
3614{
3615 bfd_target_elf_flavour, /* core_flavour */
3616 default_check_format, /* check_format */
3617 default_core_sniffer, /* core_sniffer */
3618 fetch_core_registers, /* core_read_registers */
3619 NULL /* next */
3620};
3621
3622/* Fetch (and possibly build) an appropriate link_map_offsets
8605d56e
AC
3623 structure for native GNU/Linux CRIS targets using the struct
3624 offsets defined in link.h (but without actual reference to that
3625 file).
dbbff683 3626
8605d56e
AC
3627 This makes it possible to access GNU/Linux CRIS shared libraries
3628 from a GDB that was not built on an GNU/Linux CRIS host (for cross
3629 debugging).
dbbff683
OF
3630
3631 See gdb/solib-svr4.h for an explanation of these fields. */
3632
a78f21af 3633static struct link_map_offsets *
dbbff683
OF
3634cris_linux_svr4_fetch_link_map_offsets (void)
3635{
3636 static struct link_map_offsets lmo;
3637 static struct link_map_offsets *lmp = NULL;
3638
3639 if (lmp == NULL)
3640 {
3641 lmp = &lmo;
3642
3643 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
3644 this is all we need. */
3645 lmo.r_map_offset = 4;
3646 lmo.r_map_size = 4;
3647
3648 lmo.link_map_size = 20;
3649
3650 lmo.l_addr_offset = 0;
3651 lmo.l_addr_size = 4;
3652
3653 lmo.l_name_offset = 4;
3654 lmo.l_name_size = 4;
3655
3656 lmo.l_next_offset = 12;
3657 lmo.l_next_size = 4;
3658
3659 lmo.l_prev_offset = 16;
3660 lmo.l_prev_size = 4;
3661 }
3662
3663 return lmp;
3664}
3665
3666static void
3667cris_fpless_backtrace (char *noargs, int from_tty)
3668{
3669 /* Points at the instruction after the jsr (except when in innermost frame
3670 where it points at the original pc). */
3671 CORE_ADDR pc = 0;
3672
3673 /* Temporary variable, used for parsing from the start of the function that
3674 the pc is in, up to the pc. */
3675 CORE_ADDR tmp_pc = 0;
3676 CORE_ADDR sp = 0;
3677
3678 /* Information about current frame. */
3679 struct symtab_and_line sal;
3680 char* func_name;
3681
3682 /* Present instruction. */
3683 unsigned short insn;
3684
3685 /* Next instruction, lookahead. */
3686 unsigned short insn_next;
3687
3688 /* This is to store the offset between sp at start of function and until we
3689 reach push srp (if any). */
3690 int sp_add_later = 0;
3691 int push_srp_found = 0;
3692
3693 int val = 0;
3694
3695 /* Frame counter. */
3696 int frame = 0;
3697
3698 /* For the innermost frame, we want to look at srp in case it's a leaf
3699 function (since there's no push srp in that case). */
3700 int innermost_frame = 1;
3701
4caf0990
AC
3702 deprecated_read_register_gen (PC_REGNUM, (char *) &pc);
3703 deprecated_read_register_gen (SP_REGNUM, (char *) &sp);
dbbff683
OF
3704
3705 /* We make an explicit return when we can't find an outer frame. */
3706 while (1)
3707 {
3708 /* Get file name and line number. */
3709 sal = find_pc_line (pc, 0);
3710
3711 /* Get function name. */
3712 find_pc_partial_function (pc, &func_name, (CORE_ADDR *) NULL,
3713 (CORE_ADDR *) NULL);
3714
3715 /* Print information about current frame. */
3716 printf_unfiltered ("#%i 0x%08lx in %s", frame++, pc, func_name);
3717 if (sal.symtab)
3718 {
3719 printf_unfiltered (" at %s:%i", sal.symtab->filename, sal.line);
3720 }
3721 printf_unfiltered ("\n");
3722
3723 /* Get the start address of this function. */
3724 tmp_pc = get_pc_function_start (pc);
3725
3726 /* Mini parser, only meant to find push sp and sub ...,sp from the start
3727 of the function, up to the pc. */
3728 while (tmp_pc < pc)
3729 {
3730 insn = read_memory_unsigned_integer (tmp_pc, sizeof (short));
3731 tmp_pc += sizeof (short);
3732 if (insn == 0xE1FC)
3733 {
3734 /* push <reg> 32 bit instruction */
3735 insn_next = read_memory_unsigned_integer (tmp_pc,
3736 sizeof (short));
3737 tmp_pc += sizeof (short);
3738
3739 /* Recognize srp. */
3740 if (insn_next == 0xBE7E)
3741 {
3742 /* For subsequent (not this one though) push or sub which
3743 affects sp, adjust sp immediately. */
3744 push_srp_found = 1;
3745
3746 /* Note: this will break if we ever encounter a
3747 push vr (1 byte) or push ccr (2 bytes). */
3748 sp_add_later += 4;
3749 }
3750 else
3751 {
3752 /* Some other register was pushed. */
3753 if (push_srp_found)
3754 {
3755 sp += 4;
3756 }
3757 else
3758 {
3759 sp_add_later += 4;
3760 }
3761 }
3762 }
3763 else if (cris_get_operand2 (insn) == SP_REGNUM
3764 && cris_get_mode (insn) == 0x0000
3765 && cris_get_opcode (insn) == 0x000A)
3766 {
3767 /* subq <val>,sp */
3768 val = cris_get_quick_value (insn);
3769
3770 if (push_srp_found)
3771 {
3772 sp += val;
3773 }
3774 else
3775 {
3776 sp_add_later += val;
3777 }
3778
3779 }
3780 else if (cris_get_operand2 (insn) == SP_REGNUM
3781 /* Autoincrement addressing mode. */
3782 && cris_get_mode (insn) == 0x0003
3783 /* Opcode. */
3784 && ((insn) & 0x03E0) >> 5 == 0x0004)
3785 {
3786 /* subu <val>,sp */
3787 val = get_data_from_address (&insn, tmp_pc);
3788
3789 if (push_srp_found)
3790 {
3791 sp += val;
3792 }
3793 else
3794 {
3795 sp_add_later += val;
3796 }
3797 }
3798 else if (cris_get_operand2 (insn) == SP_REGNUM
3799 && ((insn & 0x0F00) >> 8) == 0x0001
3800 && (cris_get_signed_offset (insn) < 0))
3801 {
3802 /* Immediate byte offset addressing prefix word with sp as base
3803 register. Used for CRIS v8 i.e. ETRAX 100 and newer if <val>
3804 is between 64 and 128.
3805 movem r<regsave>,[sp=sp-<val>] */
3806 val = -cris_get_signed_offset (insn);
3807 insn_next = read_memory_unsigned_integer (tmp_pc,
3808 sizeof (short));
3809 tmp_pc += sizeof (short);
3810
3811 if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
3812 && cris_get_opcode (insn_next) == 0x000F
3813 && cris_get_size (insn_next) == 0x0003
3814 && cris_get_operand1 (insn_next) == SP_REGNUM)
3815 {
3816 if (push_srp_found)
3817 {
3818 sp += val;
3819 }
3820 else
3821 {
3822 sp_add_later += val;
3823 }
3824 }
3825 }
3826 }
3827
3828 if (push_srp_found)
3829 {
3830 /* Reset flag. */
3831 push_srp_found = 0;
3832
3833 /* sp should now point at where srp is stored on the stack. Update
3834 the pc to the srp. */
3835 pc = read_memory_unsigned_integer (sp, 4);
3836 }
3837 else if (innermost_frame)
3838 {
3839 /* We couldn't find a push srp in the prologue, so this must be
3840 a leaf function, and thus we use the srp register directly.
3841 This should happen at most once, for the innermost function. */
4caf0990 3842 deprecated_read_register_gen (SRP_REGNUM, (char *) &pc);
dbbff683
OF
3843 }
3844 else
3845 {
3846 /* Couldn't find an outer frame. */
3847 return;
3848 }
3849
3850 /* Reset flag. (In case the innermost frame wasn't a leaf, we don't
3851 want to look at the srp register later either). */
3852 innermost_frame = 0;
3853
3854 /* Now, add the offset for everything up to, and including push srp,
3855 that was held back during the prologue parsing. */
3856 sp += sp_add_later;
3857 sp_add_later = 0;
3858 }
3859}
3860
a78f21af
AC
3861extern initialize_file_ftype _initialize_cris_tdep; /* -Wmissing-prototypes */
3862
29134980
OF
3863void
3864_initialize_cris_tdep (void)
3865{
3866 struct cmd_list_element *c;
3867
3868 gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);
3869
3870 /* Used in disassembly. */
d7a27068 3871 deprecated_tm_print_insn = cris_delayed_get_disassembler;
29134980
OF
3872
3873 /* CRIS-specific user-commands. */
3874 c = add_set_cmd ("cris-version", class_support, var_integer,
3875 (char *) &usr_cmd_cris_version,
3876 "Set the current CRIS version.", &setlist);
9f60d481 3877 set_cmd_sfunc (c, cris_version_update);
29134980
OF
3878 add_show_from_set (c, &showlist);
3879
3880 c = add_set_enum_cmd ("cris-mode", class_support, cris_mode_enums,
3881 &usr_cmd_cris_mode,
3882 "Set the current CRIS mode.", &setlist);
9f60d481 3883 set_cmd_sfunc (c, cris_mode_update);
29134980
OF
3884 add_show_from_set (c, &showlist);
3885
3886 c = add_set_enum_cmd ("cris-abi", class_support, cris_abi_enums,
3887 &usr_cmd_cris_abi,
3888 "Set the current CRIS ABI version.", &setlist);
9f60d481 3889 set_cmd_sfunc (c, cris_abi_update);
29134980 3890 add_show_from_set (c, &showlist);
dbbff683
OF
3891
3892 c = add_cmd ("cris-fpless-backtrace", class_support, cris_fpless_backtrace,
3893 "Display call chain using the subroutine return pointer.\n"
3894 "Note that this displays the address after the jump to the "
3895 "subroutine.", &cmdlist);
3896
3897 add_core_fns (&cris_elf_core_fns);
3898
29134980
OF
3899}
3900
3901/* Prints out all target specific values. */
3902
3903static void
3904cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3905{
3906 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
3907 if (tdep != NULL)
3908 {
3909 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_version = %i\n",
3910 tdep->cris_version);
3911 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
3912 tdep->cris_mode);
3913 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_abi = %s\n",
3914 tdep->cris_abi);
3915
3916 }
3917}
3918
3919static void
3920cris_version_update (char *ignore_args, int from_tty,
3921 struct cmd_list_element *c)
3922{
3923 struct gdbarch_info info;
3924
1868c04e
AC
3925 /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3926 the set command passed as a parameter. The clone operation will
3927 include (BUG?) any ``set'' command callback, if present.
3928 Commands like ``info set'' call all the ``show'' command
3929 callbacks. Unfortunatly, for ``show'' commands cloned from
3930 ``set'', this includes callbacks belonging to ``set'' commands.
3931 Making this worse, this only occures if add_show_from_set() is
3932 called after add_cmd_sfunc() (BUG?). */
3933
29134980 3934 /* From here on, trust the user's CRIS version setting. */
1868c04e 3935 if (cmd_type (c) == set_cmd)
29134980
OF
3936 {
3937 usr_cmd_cris_version_valid = 1;
3938
3939 /* Update the current architecture, if needed. */
fb6ecb0f 3940 gdbarch_info_init (&info);
29134980
OF
3941 if (!gdbarch_update_p (info))
3942 internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3943 }
3944}
3945
3946static void
3947cris_mode_update (char *ignore_args, int from_tty,
3948 struct cmd_list_element *c)
3949{
3950 struct gdbarch_info info;
3951
1868c04e
AC
3952 /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3953 the set command passed as a parameter. The clone operation will
3954 include (BUG?) any ``set'' command callback, if present.
3955 Commands like ``info set'' call all the ``show'' command
3956 callbacks. Unfortunatly, for ``show'' commands cloned from
3957 ``set'', this includes callbacks belonging to ``set'' commands.
3958 Making this worse, this only occures if add_show_from_set() is
3959 called after add_cmd_sfunc() (BUG?). */
3960
29134980 3961 /* From here on, trust the user's CRIS mode setting. */
1868c04e 3962 if (cmd_type (c) == set_cmd)
29134980
OF
3963 {
3964 usr_cmd_cris_mode_valid = 1;
3965
3966 /* Update the current architecture, if needed. */
fb6ecb0f 3967 gdbarch_info_init (&info);
29134980
OF
3968 if (!gdbarch_update_p (info))
3969 internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3970 }
3971}
3972
3973static void
3974cris_abi_update (char *ignore_args, int from_tty,
3975 struct cmd_list_element *c)
3976{
3977 struct gdbarch_info info;
3978
1868c04e
AC
3979 /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3980 the set command passed as a parameter. The clone operation will
3981 include (BUG?) any ``set'' command callback, if present.
3982 Commands like ``info set'' call all the ``show'' command
3983 callbacks. Unfortunatly, for ``show'' commands cloned from
3984 ``set'', this includes callbacks belonging to ``set'' commands.
3985 Making this worse, this only occures if add_show_from_set() is
3986 called after add_cmd_sfunc() (BUG?). */
3987
29134980 3988 /* From here on, trust the user's CRIS ABI setting. */
1868c04e 3989 if (cmd_type (c) == set_cmd)
29134980
OF
3990 {
3991 usr_cmd_cris_abi_valid = 1;
3992
3993 /* Update the current architecture, if needed. */
fb6ecb0f 3994 gdbarch_info_init (&info);
29134980
OF
3995 if (!gdbarch_update_p (info))
3996 internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3997 }
3998}
3999
4000/* Copied from pa64solib.c, with a couple of minor changes. */
4001
4002static CORE_ADDR
4003bfd_lookup_symbol (bfd *abfd, const char *symname)
4004{
4005 unsigned int storage_needed;
4006 asymbol *sym;
4007 asymbol **symbol_table;
4008 unsigned int number_of_symbols;
4009 unsigned int i;
4010 struct cleanup *back_to;
4011 CORE_ADDR symaddr = 0;
4012
4013 storage_needed = bfd_get_symtab_upper_bound (abfd);
4014
4015 if (storage_needed > 0)
4016 {
4017 symbol_table = (asymbol **) xmalloc (storage_needed);
4efb68b1 4018 back_to = make_cleanup (free, symbol_table);
29134980
OF
4019 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
4020
4021 for (i = 0; i < number_of_symbols; i++)
4022 {
4023 sym = *symbol_table++;
4024 if (!strcmp (sym->name, symname))
4025 {
4026 /* Bfd symbols are section relative. */
4027 symaddr = sym->value + sym->section->vma;
4028 break;
4029 }
4030 }
4031 do_cleanups (back_to);
4032 }
4033 return (symaddr);
4034}
4035
4036static struct gdbarch *
4037cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
4038{
4039 struct gdbarch *gdbarch;
4040 struct gdbarch_tdep *tdep;
4041 int cris_version;
4042 const char *cris_mode;
4043 const char *cris_abi;
4044 CORE_ADDR cris_abi_sym = 0;
4045 int register_bytes;
4046
4047 if (usr_cmd_cris_version_valid)
4048 {
4049 /* Trust the user's CRIS version setting. */
4050 cris_version = usr_cmd_cris_version;
4051 }
4052 else
4053 {
4054 /* Assume it's CRIS version 10. */
4055 cris_version = 10;
4056 }
4057
4058 if (usr_cmd_cris_mode_valid)
4059 {
4060 /* Trust the user's CRIS mode setting. */
4061 cris_mode = usr_cmd_cris_mode;
4062 }
4063 else if (cris_version == 10)
4064 {
4065 /* Assume CRIS version 10 is in user mode. */
4066 cris_mode = CRIS_MODE_USER;
4067 }
4068 else
4069 {
4070 /* Strictly speaking, older CRIS version don't have a supervisor mode,
4071 but we regard its only mode as supervisor mode. */
4072 cris_mode = CRIS_MODE_SUPERVISOR;
4073 }
4074
4075 if (usr_cmd_cris_abi_valid)
4076 {
4077 /* Trust the user's ABI setting. */
4078 cris_abi = usr_cmd_cris_abi;
4079 }
4080 else if (info.abfd)
4081 {
4082 if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
4083 {
4084 /* An elf target uses the new ABI. */
4085 cris_abi = CRIS_ABI_V2;
4086 }
4087 else if (bfd_get_flavour (info.abfd) == bfd_target_aout_flavour)
4088 {
4089 /* An a.out target may use either ABI. Look for hints in the
4090 symbol table. */
4091 cris_abi_sym = bfd_lookup_symbol (info.abfd, CRIS_ABI_SYMBOL);
4092 cris_abi = cris_abi_sym ? CRIS_ABI_V2 : CRIS_ABI_ORIGINAL;
4093 }
4094 else
4095 {
4096 /* Unknown bfd flavour. Assume it's the new ABI. */
4097 cris_abi = CRIS_ABI_V2;
4098 }
4099 }
af566d9f 4100 else if (arches != NULL)
29134980 4101 {
af566d9f
AC
4102 /* No bfd available. Stick with the ABI from the most recently
4103 selected architecture of this same family (the head of arches
4104 always points to this). (This is to avoid changing the ABI
4105 when the user updates the architecture with the 'set
4106 cris-version' command.) */
4107 cris_abi = gdbarch_tdep (arches->gdbarch)->cris_abi;
29134980
OF
4108 }
4109 else
4110 {
af566d9f
AC
4111 /* No bfd, and no previously selected architecture available.
4112 Assume it's the new ABI. */
29134980
OF
4113 cris_abi = CRIS_ABI_V2;
4114 }
4115
4116 /* Make the current settings visible to the user. */
4117 usr_cmd_cris_version = cris_version;
4118 usr_cmd_cris_mode = cris_mode;
4119 usr_cmd_cris_abi = cris_abi;
4120
4121 /* Find a candidate among the list of pre-declared architectures. Both
4122 CRIS version and ABI must match. */
4123 for (arches = gdbarch_list_lookup_by_info (arches, &info);
4124 arches != NULL;
4125 arches = gdbarch_list_lookup_by_info (arches->next, &info))
4126 {
4127 if ((gdbarch_tdep (arches->gdbarch)->cris_version == cris_version)
4128 && (gdbarch_tdep (arches->gdbarch)->cris_mode == cris_mode)
4129 && (gdbarch_tdep (arches->gdbarch)->cris_abi == cris_abi))
4130 return arches->gdbarch;
4131 }
4132
4133 /* No matching architecture was found. Create a new one. */
4134 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
4135 gdbarch = gdbarch_alloc (&info, tdep);
4136
a5afb99f
AC
4137 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
4138 ready to unwind the PC first (see frame.c:get_prev_frame()). */
4139 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
4140
29134980
OF
4141 tdep->cris_version = cris_version;
4142 tdep->cris_mode = cris_mode;
4143 tdep->cris_abi = cris_abi;
4144
4145 /* INIT shall ensure that the INFO.BYTE_ORDER is non-zero. */
4146 switch (info.byte_order)
4147 {
778eb05e 4148 case BFD_ENDIAN_LITTLE:
29134980
OF
4149 /* Ok. */
4150 break;
4151
d7449b42 4152 case BFD_ENDIAN_BIG:
29134980
OF
4153 internal_error (__FILE__, __LINE__, "cris_gdbarch_init: big endian byte order in info");
4154 break;
4155
4156 default:
4157 internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown byte order in info");
4158 }
4159
4160 /* Initialize the ABI dependent things. */
4161 if (tdep->cris_abi == CRIS_ABI_ORIGINAL)
4162 {
4163 set_gdbarch_double_bit (gdbarch, 32);
b81774d8 4164 set_gdbarch_deprecated_push_arguments (gdbarch, cris_abi_original_push_arguments);
ebba8386 4165 set_gdbarch_deprecated_store_return_value (gdbarch,
29134980 4166 cris_abi_original_store_return_value);
26e9b323 4167 set_gdbarch_deprecated_extract_return_value
29134980
OF
4168 (gdbarch, cris_abi_original_extract_return_value);
4169 set_gdbarch_reg_struct_has_addr
4170 (gdbarch, cris_abi_original_reg_struct_has_addr);
4171 }
4172 else if (tdep->cris_abi == CRIS_ABI_V2)
4173 {
4174 set_gdbarch_double_bit (gdbarch, 64);
b81774d8 4175 set_gdbarch_deprecated_push_arguments (gdbarch, cris_abi_v2_push_arguments);
ebba8386 4176 set_gdbarch_deprecated_store_return_value (gdbarch, cris_abi_v2_store_return_value);
26e9b323
AC
4177 set_gdbarch_deprecated_extract_return_value
4178 (gdbarch, cris_abi_v2_extract_return_value);
29134980
OF
4179 set_gdbarch_reg_struct_has_addr (gdbarch,
4180 cris_abi_v2_reg_struct_has_addr);
4181 }
4182 else
4183 internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown CRIS ABI");
4184
4185 /* The default definition of a long double is 2 * TARGET_DOUBLE_BIT,
4186 which means we have to set this explicitly. */
4187 set_gdbarch_long_double_bit (gdbarch, 64);
4188
29134980
OF
4189 /* There are 32 registers (some of which may not be implemented). */
4190 set_gdbarch_num_regs (gdbarch, 32);
4191 set_gdbarch_sp_regnum (gdbarch, 14);
0ba6dca9 4192 set_gdbarch_deprecated_fp_regnum (gdbarch, 8);
29134980
OF
4193 set_gdbarch_pc_regnum (gdbarch, 15);
4194
4195 set_gdbarch_register_name (gdbarch, cris_register_name);
4196
4197 /* Length of ordinary registers used in push_word and a few other places.
4198 REGISTER_RAW_SIZE is the real way to know how big a register is. */
b1e29e33 4199 set_gdbarch_deprecated_register_size (gdbarch, 4);
29134980
OF
4200
4201 /* NEW */
4202 set_gdbarch_register_bytes_ok (gdbarch, cris_register_bytes_ok);
4203 set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
4204
4205
4206 set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
4207 set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);
4208
4209
4210 /* The total amount of space needed to store (in an array called registers)
4211 GDB's copy of the machine's register state. Note: We can not use
4212 cris_register_size at this point, since it relies on current_gdbarch
4213 being set. */
4214 switch (tdep->cris_version)
4215 {
4216 case 0:
4217 case 1:
4218 case 2:
4219 case 3:
4220 /* Support for these may be added later. */
4221 internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unsupported CRIS version");
4222 break;
4223
4224 case 8:
4225 case 9:
4226 /* CRIS v8 and v9, a.k.a. ETRAX 100. General registers R0 - R15
4227 (32 bits), special registers P0 - P1 (8 bits), P4 - P5 (16 bits),
4228 and P8 - P14 (32 bits). */
4229 register_bytes = (16 * 4) + (2 * 1) + (2 * 2) + (7 * 4);
4230 break;
4231
4232 case 10:
4233 case 11:
4234 /* CRIS v10 and v11, a.k.a. ETRAX 100LX. In addition to ETRAX 100,
4235 P7 (32 bits), and P15 (32 bits) have been implemented. */
4236 register_bytes = (16 * 4) + (2 * 1) + (2 * 2) + (9 * 4);
4237 break;
4238
4239 default:
4240 internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown CRIS version");
4241 }
4242
b8b527c5 4243 set_gdbarch_deprecated_register_bytes (gdbarch, register_bytes);
29134980
OF
4244
4245 /* Returns the register offset for the first byte of register regno's space
4246 in the saved register state. */
4247 set_gdbarch_register_byte (gdbarch, cris_register_offset);
4248
4249 /* The length of the registers in the actual machine representation. */
4250 set_gdbarch_register_raw_size (gdbarch, cris_register_size);
4251
4252 /* The largest value REGISTER_RAW_SIZE can have. */
a0ed5532 4253 set_gdbarch_deprecated_max_register_raw_size (gdbarch, 32);
29134980
OF
4254
4255 /* The length of the registers in the program's representation. */
4256 set_gdbarch_register_virtual_size (gdbarch, cris_register_size);
4257
4258 /* The largest value REGISTER_VIRTUAL_SIZE can have. */
a0ed5532 4259 set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 32);
29134980
OF
4260
4261 set_gdbarch_register_virtual_type (gdbarch, cris_register_virtual_type);
4262
4263 /* Use generic dummy frames. */
29134980 4264
29134980 4265 /* Read all about dummy frames in blockframe.c. */
ae45cd16 4266 set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_at_entry_point);
29134980
OF
4267
4268 /* Defined to 1 to indicate that the target supports inferior function
4269 calls. */
b1e29e33
AC
4270 set_gdbarch_deprecated_call_dummy_words (gdbarch, 0);
4271 set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, 0);
29134980 4272
129c1cd6 4273 set_gdbarch_deprecated_get_saved_register (gdbarch, deprecated_generic_get_saved_register);
29134980 4274
28f617b3 4275 set_gdbarch_deprecated_push_return_address (gdbarch, cris_push_return_address);
749b82f6 4276 set_gdbarch_deprecated_pop_frame (gdbarch, cris_pop_frame);
29134980 4277
4183d812 4278 set_gdbarch_deprecated_store_struct_return (gdbarch, cris_store_struct_return);
26e9b323
AC
4279 set_gdbarch_deprecated_extract_struct_value_address
4280 (gdbarch, cris_extract_struct_value_address);
29134980
OF
4281 set_gdbarch_use_struct_convention (gdbarch, cris_use_struct_convention);
4282
f30ee0bc 4283 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, cris_frame_init_saved_regs);
e9582e71 4284 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, cris_init_extra_frame_info);
29134980
OF
4285 set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);
4286 set_gdbarch_prologue_frameless_p (gdbarch, generic_prologue_frameless_p);
4287
4288 /* The stack grows downward. */
4289 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4290
4291 set_gdbarch_breakpoint_from_pc (gdbarch, cris_breakpoint_from_pc);
4292
4293 /* The PC must not be decremented after a breakpoint. (The breakpoint
4294 handler takes care of that.) */
4295 set_gdbarch_decr_pc_after_break (gdbarch, 0);
4296
4297 /* Offset from address of function to start of its code. */
4298 set_gdbarch_function_start_offset (gdbarch, 0);
4299
4300 /* The number of bytes at the start of arglist that are not really args,
4301 0 in the CRIS ABI. */
4302 set_gdbarch_frame_args_skip (gdbarch, 0);
4303 set_gdbarch_frameless_function_invocation
4304 (gdbarch, cris_frameless_function_invocation);
618ce49f 4305 set_gdbarch_deprecated_frame_chain (gdbarch, cris_frame_chain);
29134980 4306
8bedc050 4307 set_gdbarch_deprecated_frame_saved_pc (gdbarch, cris_frame_saved_pc);
6913c89a 4308 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, cris_saved_pc_after_call);
29134980 4309
29134980
OF
4310 /* Helpful for backtracing and returning in a call dummy. */
4311 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
4312
6c0e89ed 4313 /* Should be using push_dummy_call. */
b46e02f6 4314 set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
6c0e89ed 4315
dbbff683
OF
4316 /* Use target_specific function to define link map offsets. */
4317 set_solib_svr4_fetch_link_map_offsets
4318 (gdbarch, cris_linux_svr4_fetch_link_map_offsets);
4319
29134980
OF
4320 return gdbarch;
4321}