gdb: fix vfork with multiple threads
[deliverable/binutils-gdb.git] / gdb / moxie-tdep.c
CommitLineData
d7066cce
AG
1/* Target-dependent code for Moxie.
2
3666a048 3 Copyright (C) 2009-2021 Free Software Foundation, Inc.
d7066cce
AG
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "frame.h"
22#include "frame-unwind.h"
23#include "frame-base.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "gdbcmd.h"
27#include "gdbcore.h"
d7066cce
AG
28#include "value.h"
29#include "inferior.h"
30#include "symfile.h"
31#include "objfiles.h"
32#include "osabi.h"
33#include "language.h"
34#include "arch-utils.h"
35#include "regcache.h"
36#include "trad-frame.h"
37#include "dis-asm.h"
451fa05e 38#include "record.h"
d02ed0bb 39#include "record-full.h"
d7066cce 40
d7066cce 41#include "moxie-tdep.h"
325fac50 42#include <algorithm>
d7066cce 43
d7066cce
AG
44/* Use an invalid address value as 'not available' marker. */
45enum { REG_UNAVAIL = (CORE_ADDR) -1 };
46
47struct moxie_frame_cache
48{
49 /* Base address. */
50 CORE_ADDR base;
51 CORE_ADDR pc;
52 LONGEST framesize;
53 CORE_ADDR saved_regs[MOXIE_NUM_REGS];
54 CORE_ADDR saved_sp;
55};
56
57/* Implement the "frame_align" gdbarch method. */
58
59static CORE_ADDR
60moxie_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
61{
62 /* Align to the size of an instruction (so that they can safely be
63 pushed onto the stack. */
64 return sp & ~1;
65}
66
04180708 67constexpr gdb_byte moxie_break_insn[] = { 0x35, 0x00 };
d7066cce 68
04180708 69typedef BP_MANIPULATION (moxie_break_insn) moxie_breakpoint;
d7066cce
AG
70
71/* Moxie register names. */
72
27087b7f 73static const char * const moxie_register_names[] = {
d7066cce
AG
74 "$fp", "$sp", "$r0", "$r1", "$r2",
75 "$r3", "$r4", "$r5", "$r6", "$r7",
76 "$r8", "$r9", "$r10", "$r11", "$r12",
77 "$r13", "$pc", "$cc" };
78
79/* Implement the "register_name" gdbarch method. */
80
81static const char *
82moxie_register_name (struct gdbarch *gdbarch, int reg_nr)
83{
84 if (reg_nr < 0)
85 return NULL;
86 if (reg_nr >= MOXIE_NUM_REGS)
87 return NULL;
88 return moxie_register_names[reg_nr];
89}
90
91/* Implement the "register_type" gdbarch method. */
92
93static struct type *
94moxie_register_type (struct gdbarch *gdbarch, int reg_nr)
95{
96 if (reg_nr == MOXIE_PC_REGNUM)
97 return builtin_type (gdbarch)->builtin_func_ptr;
98 else if (reg_nr == MOXIE_SP_REGNUM || reg_nr == MOXIE_FP_REGNUM)
99 return builtin_type (gdbarch)->builtin_data_ptr;
100 else
df4df182 101 return builtin_type (gdbarch)->builtin_int32;
d7066cce
AG
102}
103
104/* Write into appropriate registers a function return value
105 of type TYPE, given in virtual format. */
106
107static void
108moxie_store_return_value (struct type *type, struct regcache *regcache,
7c543f7b 109 const gdb_byte *valbuf)
d7066cce 110{
ac7936df 111 struct gdbarch *gdbarch = regcache->arch ();
e17a4113 112 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d7066cce
AG
113 CORE_ADDR regval;
114 int len = TYPE_LENGTH (type);
115
116 /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
e17a4113 117 regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
d7066cce
AG
118 regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
119 if (len > 4)
120 {
7c543f7b 121 regval = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
d7066cce
AG
122 regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
123 }
124}
125
126/* Decode the instructions within the given address range. Decide
127 when we must have reached the end of the function prologue. If a
128 frame_info pointer is provided, fill in its saved_regs etc.
129
130 Returns the address of the first instruction after the prologue. */
131
132static CORE_ADDR
133moxie_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
99f75275
AG
134 struct moxie_frame_cache *cache,
135 struct gdbarch *gdbarch)
d7066cce 136{
e17a4113 137 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d7066cce
AG
138 CORE_ADDR next_addr;
139 ULONGEST inst, inst2;
140 LONGEST offset;
141 int regnum;
142
143 /* Record where the jsra instruction saves the PC and FP. */
144 cache->saved_regs[MOXIE_PC_REGNUM] = -4;
145 cache->saved_regs[MOXIE_FP_REGNUM] = 0;
146 cache->framesize = 0;
147
148 if (start_addr >= end_addr)
149 return end_addr;
150
151 for (next_addr = start_addr; next_addr < end_addr; )
152 {
e17a4113 153 inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
d7066cce 154
5152ff90
AG
155 /* Match "push $sp $rN" where N is between 0 and 13 inclusive. */
156 if (inst >= 0x0612 && inst <= 0x061f)
d7066cce
AG
157 {
158 regnum = inst & 0x000f;
159 cache->framesize += 4;
160 cache->saved_regs[regnum] = cache->framesize;
161 next_addr += 2;
162 }
4ee73e90
AG
163 else
164 break;
ce0bf488 165 }
d7066cce 166
ce0bf488 167 inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
d7066cce 168
ce0bf488
AG
169 /* Optional stack allocation for args and local vars <= 4
170 byte. */
5152ff90 171 if (inst == 0x01e0) /* ldi.l $r12, X */
ce0bf488
AG
172 {
173 offset = read_memory_integer (next_addr + 2, 4, byte_order);
174 inst2 = read_memory_unsigned_integer (next_addr + 6, 2, byte_order);
175
5152ff90 176 if (inst2 == 0x291e) /* sub.l $sp, $r12 */
ce0bf488
AG
177 {
178 cache->framesize += offset;
179 }
180
181 return (next_addr + 8);
182 }
5152ff90 183 else if ((inst & 0xff00) == 0x9100) /* dec $sp, X */
ce0bf488
AG
184 {
185 cache->framesize += (inst & 0x00ff);
186 next_addr += 2;
d7066cce 187
ce0bf488
AG
188 while (next_addr < end_addr)
189 {
190 inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
5152ff90 191 if ((inst & 0xff00) != 0x9100) /* no more dec $sp, X */
ce0bf488
AG
192 break;
193 cache->framesize += (inst & 0x00ff);
194 next_addr += 2;
d7066cce 195 }
d7066cce
AG
196 }
197
198 return next_addr;
199}
200
201/* Find the end of function prologue. */
202
203static CORE_ADDR
204moxie_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
205{
206 CORE_ADDR func_addr = 0, func_end = 0;
2c02bd72 207 const char *func_name;
d7066cce
AG
208
209 /* See if we can determine the end of the prologue via the symbol table.
210 If so, then return either PC, or the PC after the prologue, whichever
211 is greater. */
212 if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
213 {
d80b854b
UW
214 CORE_ADDR post_prologue_pc
215 = skip_prologue_using_sal (gdbarch, func_addr);
d7066cce 216 if (post_prologue_pc != 0)
325fac50 217 return std::max (pc, post_prologue_pc);
d7066cce
AG
218 else
219 {
220 /* Can't determine prologue from the symbol table, need to examine
221 instructions. */
222 struct symtab_and_line sal;
223 struct symbol *sym;
224 struct moxie_frame_cache cache;
225 CORE_ADDR plg_end;
226
227 memset (&cache, 0, sizeof cache);
228
229 plg_end = moxie_analyze_prologue (func_addr,
99f75275 230 func_end, &cache, gdbarch);
d7066cce 231 /* Found a function. */
835a09d9 232 sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
d7066cce 233 /* Don't use line number debug info for assembly source
025bb325 234 files. */
c1b5c1eb 235 if (sym && sym->language () != language_asm)
d7066cce
AG
236 {
237 sal = find_pc_line (func_addr, 0);
238 if (sal.end && sal.end < func_end)
239 {
240 /* Found a line number, use it as end of
241 prologue. */
242 return sal.end;
243 }
244 }
245 /* No useable line symbol. Use result of prologue parsing
246 method. */
247 return plg_end;
248 }
249 }
250
251 /* No function symbol -- just return the PC. */
252 return (CORE_ADDR) pc;
253}
254
255struct moxie_unwind_cache
256{
257 /* The previous frame's inner most stack address. Used as this
258 frame ID's stack_addr. */
259 CORE_ADDR prev_sp;
260 /* The frame's base, optionally used by the high-level debug info. */
261 CORE_ADDR base;
262 int size;
263 /* How far the SP and r13 (FP) have been offset from the start of
264 the stack frame (as defined by the previous frame's stack
265 pointer). */
266 LONGEST sp_offset;
267 LONGEST r13_offset;
268 int uses_frame;
269 /* Table indicating the location of each and every register. */
098caef4 270 trad_frame_saved_reg *saved_regs;
d7066cce
AG
271};
272
6ed1ff02 273/* Read an unsigned integer from the inferior, and adjust
405feb71 274 endianness. */
6ed1ff02
AG
275static ULONGEST
276moxie_process_readu (CORE_ADDR addr, gdb_byte *buf,
277 int length, enum bfd_endian byte_order)
278{
279 if (target_read_memory (addr, buf, length))
280 {
281 if (record_debug)
282 printf_unfiltered (_("Process record: error reading memory at "
283 "addr 0x%s len = %d.\n"),
284 paddress (target_gdbarch (), addr), length);
285 return -1;
286 }
287
288 return extract_unsigned_integer (buf, length, byte_order);
289}
290
291
292/* Helper macro to extract the signed 10-bit offset from a 16-bit
293 branch instruction. */
294#define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
295
296/* Insert a single step breakpoint. */
297
a0ff9e1a 298static std::vector<CORE_ADDR>
f5ea389a 299moxie_software_single_step (struct regcache *regcache)
6ed1ff02 300{
ac7936df 301 struct gdbarch *gdbarch = regcache->arch ();
6ed1ff02
AG
302 CORE_ADDR addr;
303 gdb_byte buf[4];
304 uint16_t inst;
305 uint32_t tmpu32;
306 ULONGEST fp;
307 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
a0ff9e1a 308 std::vector<CORE_ADDR> next_pcs;
6ed1ff02 309
a8f34182 310 addr = regcache_read_pc (regcache);
6ed1ff02
AG
311
312 inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
313
314 /* Decode instruction. */
315 if (inst & (1 << 15))
316 {
317 if (inst & (1 << 14))
318 {
319 /* This is a Form 3 instruction. */
320 int opcode = (inst >> 10 & 0xf);
321
322 switch (opcode)
323 {
324 case 0x00: /* beq */
325 case 0x01: /* bne */
326 case 0x02: /* blt */
327 case 0x03: /* bgt */
328 case 0x04: /* bltu */
329 case 0x05: /* bgtu */
330 case 0x06: /* bge */
331 case 0x07: /* ble */
332 case 0x08: /* bgeu */
333 case 0x09: /* bleu */
334 /* Insert breaks on both branches, because we can't currently tell
335 which way things will go. */
a0ff9e1a
SM
336 next_pcs.push_back (addr + 2);
337 next_pcs.push_back (addr + 2 + INST2OFFSET(inst));
6ed1ff02
AG
338 break;
339 default:
340 {
341 /* Do nothing. */
342 break;
343 }
344 }
345 }
346 else
347 {
348 /* This is a Form 2 instruction. They are all 16 bits. */
a0ff9e1a 349 next_pcs.push_back (addr + 2);
6ed1ff02
AG
350 }
351 }
352 else
353 {
354 /* This is a Form 1 instruction. */
355 int opcode = inst >> 8;
356
357 switch (opcode)
358 {
359 /* 16-bit instructions. */
6441e6db 360 case 0x00: /* bad */
6ed1ff02
AG
361 case 0x02: /* mov (register-to-register) */
362 case 0x05: /* add.l */
363 case 0x06: /* push */
364 case 0x07: /* pop */
365 case 0x0a: /* ld.l (register indirect) */
366 case 0x0b: /* st.l */
367 case 0x0e: /* cmp */
6441e6db
AG
368 case 0x0f: /* nop */
369 case 0x10: /* sex.b */
370 case 0x11: /* sex.s */
371 case 0x12: /* zex.b */
372 case 0x13: /* zex.s */
373 case 0x14: /* umul.x */
374 case 0x15: /* mul.x */
6ed1ff02
AG
375 case 0x16:
376 case 0x17:
377 case 0x18:
378 case 0x1c: /* ld.b (register indirect) */
379 case 0x1e: /* st.b */
380 case 0x21: /* ld.s (register indirect) */
381 case 0x23: /* st.s */
382 case 0x26: /* and */
383 case 0x27: /* lshr */
384 case 0x28: /* ashl */
385 case 0x29: /* sub.l */
386 case 0x2a: /* neg */
387 case 0x2b: /* or */
388 case 0x2c: /* not */
389 case 0x2d: /* ashr */
390 case 0x2e: /* xor */
391 case 0x2f: /* mul.l */
392 case 0x31: /* div.l */
393 case 0x32: /* udiv.l */
394 case 0x33: /* mod.l */
395 case 0x34: /* umod.l */
a0ff9e1a 396 next_pcs.push_back (addr + 2);
6ed1ff02
AG
397 break;
398
6441e6db
AG
399 /* 32-bit instructions. */
400 case 0x0c: /* ldo.l */
401 case 0x0d: /* sto.l */
402 case 0x36: /* ldo.b */
403 case 0x37: /* sto.b */
404 case 0x38: /* ldo.s */
405 case 0x39: /* sto.s */
a0ff9e1a 406 next_pcs.push_back (addr + 4);
6441e6db
AG
407 break;
408
6ed1ff02
AG
409 /* 48-bit instructions. */
410 case 0x01: /* ldi.l (immediate) */
411 case 0x08: /* lda.l */
412 case 0x09: /* sta.l */
6ed1ff02
AG
413 case 0x1b: /* ldi.b (immediate) */
414 case 0x1d: /* lda.b */
415 case 0x1f: /* sta.b */
416 case 0x20: /* ldi.s (immediate) */
417 case 0x22: /* lda.s */
418 case 0x24: /* sta.s */
a0ff9e1a 419 next_pcs.push_back (addr + 6);
6ed1ff02
AG
420 break;
421
422 /* Control flow instructions. */
423 case 0x03: /* jsra */
424 case 0x1a: /* jmpa */
a0ff9e1a
SM
425 next_pcs.push_back (moxie_process_readu (addr + 2, buf, 4,
426 byte_order));
6ed1ff02
AG
427 break;
428
429 case 0x04: /* ret */
430 regcache_cooked_read_unsigned (regcache, MOXIE_FP_REGNUM, &fp);
a0ff9e1a 431 next_pcs.push_back (moxie_process_readu (fp + 4, buf, 4, byte_order));
6ed1ff02
AG
432 break;
433
434 case 0x19: /* jsr */
435 case 0x25: /* jmp */
0b883586 436 regcache->raw_read ((inst >> 4) & 0xf, (gdb_byte *) & tmpu32);
a0ff9e1a 437 next_pcs.push_back (tmpu32);
6ed1ff02
AG
438 break;
439
440 case 0x30: /* swi */
441 case 0x35: /* brk */
442 /* Unsupported, for now. */
443 break;
444 }
445 }
446
93f9a11f 447 return next_pcs;
6ed1ff02
AG
448}
449
d7066cce
AG
450/* Given a return value in `regbuf' with a type `valtype',
451 extract and copy its value into `valbuf'. */
452
453static void
454moxie_extract_return_value (struct type *type, struct regcache *regcache,
7c543f7b 455 gdb_byte *dst)
d7066cce 456{
ac7936df 457 struct gdbarch *gdbarch = regcache->arch ();
e17a4113 458 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d7066cce
AG
459 int len = TYPE_LENGTH (type);
460 ULONGEST tmp;
461
462 /* By using store_unsigned_integer we avoid having to do
463 anything special for small big-endian values. */
464 regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
7c543f7b 465 store_unsigned_integer (dst, (len > 4 ? len - 4 : len), byte_order, tmp);
d7066cce
AG
466
467 /* Ignore return values more than 8 bytes in size because the moxie
468 returns anything more than 8 bytes in the stack. */
469 if (len > 4)
470 {
471 regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
7c543f7b 472 store_unsigned_integer (dst + len - 4, 4, byte_order, tmp);
d7066cce
AG
473 }
474}
475
476/* Implement the "return_value" gdbarch method. */
477
478static enum return_value_convention
6a3a010b 479moxie_return_value (struct gdbarch *gdbarch, struct value *function,
d7066cce
AG
480 struct type *valtype, struct regcache *regcache,
481 gdb_byte *readbuf, const gdb_byte *writebuf)
482{
483 if (TYPE_LENGTH (valtype) > 8)
484 return RETURN_VALUE_STRUCT_CONVENTION;
485 else
486 {
487 if (readbuf != NULL)
488 moxie_extract_return_value (valtype, regcache, readbuf);
489 if (writebuf != NULL)
490 moxie_store_return_value (valtype, regcache, writebuf);
491 return RETURN_VALUE_REGISTER_CONVENTION;
492 }
493}
494
495/* Allocate and initialize a moxie_frame_cache object. */
496
497static struct moxie_frame_cache *
498moxie_alloc_frame_cache (void)
499{
500 struct moxie_frame_cache *cache;
501 int i;
502
503 cache = FRAME_OBSTACK_ZALLOC (struct moxie_frame_cache);
504
505 cache->base = 0;
506 cache->saved_sp = 0;
507 cache->pc = 0;
508 cache->framesize = 0;
509 for (i = 0; i < MOXIE_NUM_REGS; ++i)
510 cache->saved_regs[i] = REG_UNAVAIL;
511
512 return cache;
513}
514
515/* Populate a moxie_frame_cache object for this_frame. */
516
517static struct moxie_frame_cache *
518moxie_frame_cache (struct frame_info *this_frame, void **this_cache)
519{
520 struct moxie_frame_cache *cache;
521 CORE_ADDR current_pc;
522 int i;
523
524 if (*this_cache)
19ba03f4 525 return (struct moxie_frame_cache *) *this_cache;
d7066cce
AG
526
527 cache = moxie_alloc_frame_cache ();
528 *this_cache = cache;
529
530 cache->base = get_frame_register_unsigned (this_frame, MOXIE_FP_REGNUM);
531 if (cache->base == 0)
532 return cache;
533
534 cache->pc = get_frame_func (this_frame);
535 current_pc = get_frame_pc (this_frame);
536 if (cache->pc)
99f75275
AG
537 {
538 struct gdbarch *gdbarch = get_frame_arch (this_frame);
539 moxie_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
540 }
d7066cce
AG
541
542 cache->saved_sp = cache->base - cache->framesize;
543
544 for (i = 0; i < MOXIE_NUM_REGS; ++i)
545 if (cache->saved_regs[i] != REG_UNAVAIL)
546 cache->saved_regs[i] = cache->base - cache->saved_regs[i];
547
548 return cache;
549}
550
d7066cce
AG
551/* Given a GDB frame, determine the address of the calling function's
552 frame. This will be used to create a new GDB frame struct. */
553
554static void
555moxie_frame_this_id (struct frame_info *this_frame,
556 void **this_prologue_cache, struct frame_id *this_id)
557{
558 struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
559 this_prologue_cache);
560
561 /* This marks the outermost frame. */
562 if (cache->base == 0)
563 return;
564
565 *this_id = frame_id_build (cache->saved_sp, cache->pc);
566}
567
568/* Get the value of register regnum in the previous stack frame. */
569
570static struct value *
571moxie_frame_prev_register (struct frame_info *this_frame,
572 void **this_prologue_cache, int regnum)
573{
574 struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
575 this_prologue_cache);
576
577 gdb_assert (regnum >= 0);
578
579 if (regnum == MOXIE_SP_REGNUM && cache->saved_sp)
580 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
581
582 if (regnum < MOXIE_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
583 return frame_unwind_got_memory (this_frame, regnum,
584 cache->saved_regs[regnum]);
585
586 return frame_unwind_got_register (this_frame, regnum, regnum);
587}
588
589static const struct frame_unwind moxie_frame_unwind = {
a154d838 590 "moxie prologue",
d7066cce 591 NORMAL_FRAME,
8fbca658 592 default_frame_unwind_stop_reason,
d7066cce
AG
593 moxie_frame_this_id,
594 moxie_frame_prev_register,
595 NULL,
596 default_frame_sniffer
597};
598
599/* Return the base address of this_frame. */
600
601static CORE_ADDR
602moxie_frame_base_address (struct frame_info *this_frame, void **this_cache)
603{
604 struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
605 this_cache);
606
607 return cache->base;
608}
609
610static const struct frame_base moxie_frame_base = {
611 &moxie_frame_unwind,
612 moxie_frame_base_address,
613 moxie_frame_base_address,
614 moxie_frame_base_address
615};
616
451fa05e
AG
617/* Parse the current instruction and record the values of the registers and
618 memory that will be changed in current instruction to "record_arch_list".
025bb325 619 Return -1 if something wrong. */
451fa05e 620
693be288 621static int
451fa05e
AG
622moxie_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
623 CORE_ADDR addr)
624{
625 gdb_byte buf[4];
626 uint16_t inst;
627 uint32_t tmpu32;
628 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
629
630 if (record_debug > 1)
631 fprintf_unfiltered (gdb_stdlog, "Process record: moxie_process_record "
dda83cd7 632 "addr = 0x%s\n",
f5656ead 633 paddress (target_gdbarch (), addr));
451fa05e
AG
634
635 inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
636
637 /* Decode instruction. */
638 if (inst & (1 << 15))
639 {
640 if (inst & (1 << 14))
641 {
642 /* This is a Form 3 instruction. */
643 int opcode = (inst >> 10 & 0xf);
644
645 switch (opcode)
646 {
647 case 0x00: /* beq */
648 case 0x01: /* bne */
649 case 0x02: /* blt */
650 case 0x03: /* bgt */
651 case 0x04: /* bltu */
652 case 0x05: /* bgtu */
653 case 0x06: /* bge */
654 case 0x07: /* ble */
655 case 0x08: /* bgeu */
656 case 0x09: /* bleu */
657 /* Do nothing. */
658 break;
659 default:
660 {
661 /* Do nothing. */
662 break;
663 }
664 }
665 }
666 else
667 {
668 /* This is a Form 2 instruction. */
669 int opcode = (inst >> 12 & 0x3);
670 switch (opcode)
671 {
672 case 0x00: /* inc */
673 case 0x01: /* dec */
674 case 0x02: /* gsr */
675 {
676 int reg = (inst >> 8) & 0xf;
25ea693b 677 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
678 return -1;
679 }
680 break;
681 case 0x03: /* ssr */
682 {
683 /* Do nothing until GDB learns about moxie's special
684 registers. */
685 }
686 break;
687 default:
688 /* Do nothing. */
689 break;
690 }
691 }
692 }
693 else
694 {
695 /* This is a Form 1 instruction. */
696 int opcode = inst >> 8;
697
698 switch (opcode)
699 {
700 case 0x00: /* nop */
701 /* Do nothing. */
702 break;
703 case 0x01: /* ldi.l (immediate) */
704 case 0x02: /* mov (register-to-register) */
705 {
706 int reg = (inst >> 4) & 0xf;
25ea693b 707 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
708 return -1;
709 }
710 break;
711 case 0x03: /* jsra */
712 {
0b883586 713 regcache->raw_read (
451fa05e
AG
714 MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
715 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
716 4, byte_order);
25ea693b
MM
717 if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
718 || (record_full_arch_list_add_reg (regcache,
719 MOXIE_SP_REGNUM))
720 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
451fa05e
AG
721 return -1;
722 }
723 break;
724 case 0x04: /* ret */
725 {
25ea693b
MM
726 if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
727 || (record_full_arch_list_add_reg (regcache,
728 MOXIE_SP_REGNUM)))
451fa05e
AG
729 return -1;
730 }
731 break;
732 case 0x05: /* add.l */
733 {
734 int reg = (inst >> 4) & 0xf;
25ea693b 735 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
736 return -1;
737 }
738 break;
739 case 0x06: /* push */
740 {
741 int reg = (inst >> 4) & 0xf;
0b883586 742 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
743 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
744 4, byte_order);
25ea693b
MM
745 if (record_full_arch_list_add_reg (regcache, reg)
746 || record_full_arch_list_add_mem (tmpu32 - 4, 4))
451fa05e
AG
747 return -1;
748 }
749 break;
750 case 0x07: /* pop */
751 {
752 int a = (inst >> 4) & 0xf;
753 int b = inst & 0xf;
25ea693b
MM
754 if (record_full_arch_list_add_reg (regcache, a)
755 || record_full_arch_list_add_reg (regcache, b))
451fa05e
AG
756 return -1;
757 }
758 break;
759 case 0x08: /* lda.l */
760 {
761 int reg = (inst >> 4) & 0xf;
25ea693b 762 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
763 return -1;
764 }
765 break;
766 case 0x09: /* sta.l */
767 {
768 tmpu32 = (uint32_t) moxie_process_readu (addr+2, buf,
769 4, byte_order);
25ea693b 770 if (record_full_arch_list_add_mem (tmpu32, 4))
451fa05e
AG
771 return -1;
772 }
773 break;
774 case 0x0a: /* ld.l (register indirect) */
775 {
776 int reg = (inst >> 4) & 0xf;
25ea693b 777 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
778 return -1;
779 }
780 break;
781 case 0x0b: /* st.l */
782 {
783 int reg = (inst >> 4) & 0xf;
0b883586 784 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
785 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
786 4, byte_order);
25ea693b 787 if (record_full_arch_list_add_mem (tmpu32, 4))
451fa05e
AG
788 return -1;
789 }
790 break;
791 case 0x0c: /* ldo.l */
792 {
793 int reg = (inst >> 4) & 0xf;
25ea693b 794 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
795 return -1;
796 }
797 break;
798 case 0x0d: /* sto.l */
799 {
800 int reg = (inst >> 4) & 0xf;
6441e6db
AG
801 uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
802 byte_order)) << 16 ) >> 16;
0b883586 803 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
804 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
805 4, byte_order);
806 tmpu32 += offset;
25ea693b 807 if (record_full_arch_list_add_mem (tmpu32, 4))
451fa05e
AG
808 return -1;
809 }
810 break;
811 case 0x0e: /* cmp */
812 {
25ea693b 813 if (record_full_arch_list_add_reg (regcache, MOXIE_CC_REGNUM))
451fa05e
AG
814 return -1;
815 }
816 break;
6441e6db
AG
817 case 0x0f: /* nop */
818 {
819 /* Do nothing. */
820 break;
821 }
822 case 0x10: /* sex.b */
823 case 0x11: /* sex.s */
824 case 0x12: /* zex.b */
825 case 0x13: /* zex.s */
826 case 0x14: /* umul.x */
827 case 0x15: /* mul.x */
828 {
829 int reg = (inst >> 4) & 0xf;
830 if (record_full_arch_list_add_reg (regcache, reg))
831 return -1;
832 }
833 break;
451fa05e
AG
834 case 0x16:
835 case 0x17:
836 case 0x18:
837 {
838 /* Do nothing. */
839 break;
840 }
841 case 0x19: /* jsr */
842 {
0b883586 843 regcache->raw_read (
451fa05e
AG
844 MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
845 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
846 4, byte_order);
25ea693b
MM
847 if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
848 || (record_full_arch_list_add_reg (regcache,
849 MOXIE_SP_REGNUM))
850 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
451fa05e
AG
851 return -1;
852 }
853 break;
854 case 0x1a: /* jmpa */
855 {
856 /* Do nothing. */
857 }
858 break;
859 case 0x1b: /* ldi.b (immediate) */
860 case 0x1c: /* ld.b (register indirect) */
861 case 0x1d: /* lda.b */
862 {
863 int reg = (inst >> 4) & 0xf;
25ea693b 864 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
865 return -1;
866 }
867 break;
868 case 0x1e: /* st.b */
869 {
870 int reg = (inst >> 4) & 0xf;
0b883586 871 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
872 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
873 4, byte_order);
25ea693b 874 if (record_full_arch_list_add_mem (tmpu32, 1))
451fa05e
AG
875 return -1;
876 }
877 break;
878 case 0x1f: /* sta.b */
879 {
948f8e3d 880 tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
25ea693b 881 if (record_full_arch_list_add_mem (tmpu32, 1))
451fa05e
AG
882 return -1;
883 }
884 break;
885 case 0x20: /* ldi.s (immediate) */
886 case 0x21: /* ld.s (register indirect) */
887 case 0x22: /* lda.s */
888 {
889 int reg = (inst >> 4) & 0xf;
25ea693b 890 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
891 return -1;
892 }
893 break;
894 case 0x23: /* st.s */
895 {
896 int reg = (inst >> 4) & 0xf;
0b883586 897 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
898 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
899 4, byte_order);
25ea693b 900 if (record_full_arch_list_add_mem (tmpu32, 2))
451fa05e
AG
901 return -1;
902 }
903 break;
904 case 0x24: /* sta.s */
905 {
948f8e3d 906 tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
25ea693b 907 if (record_full_arch_list_add_mem (tmpu32, 2))
451fa05e
AG
908 return -1;
909 }
910 break;
911 case 0x25: /* jmp */
912 {
913 /* Do nothing. */
914 }
915 break;
916 case 0x26: /* and */
917 case 0x27: /* lshr */
918 case 0x28: /* ashl */
6441e6db 919 case 0x29: /* sub */
451fa05e
AG
920 case 0x2a: /* neg */
921 case 0x2b: /* or */
922 case 0x2c: /* not */
923 case 0x2d: /* ashr */
924 case 0x2e: /* xor */
6441e6db 925 case 0x2f: /* mul */
451fa05e
AG
926 {
927 int reg = (inst >> 4) & 0xf;
25ea693b 928 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
929 return -1;
930 }
931 break;
932 case 0x30: /* swi */
933 {
934 /* We currently implement support for libgloss'
935 system calls. */
936
948f8e3d 937 int inum = moxie_process_readu (addr+2, buf, 4, byte_order);
451fa05e
AG
938
939 switch (inum)
940 {
941 case 0x1: /* SYS_exit */
942 {
943 /* Do nothing. */
944 }
945 break;
946 case 0x2: /* SYS_open */
947 {
25ea693b 948 if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
451fa05e
AG
949 return -1;
950 }
951 break;
952 case 0x4: /* SYS_read */
953 {
954 uint32_t length, ptr;
955
956 /* Read buffer pointer is in $r1. */
0b883586 957 regcache->raw_read (3, (gdb_byte *) & ptr);
451fa05e
AG
958 ptr = extract_unsigned_integer ((gdb_byte *) & ptr,
959 4, byte_order);
960
025bb325 961 /* String length is at 0x12($fp). */
0b883586 962 regcache->raw_read (
451fa05e
AG
963 MOXIE_FP_REGNUM, (gdb_byte *) & tmpu32);
964 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
965 4, byte_order);
948f8e3d 966 length = moxie_process_readu (tmpu32+20, buf, 4, byte_order);
451fa05e 967
25ea693b 968 if (record_full_arch_list_add_mem (ptr, length))
451fa05e
AG
969 return -1;
970 }
971 break;
972 case 0x5: /* SYS_write */
973 {
25ea693b 974 if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
451fa05e
AG
975 return -1;
976 }
977 break;
978 default:
979 break;
980 }
981 }
982 break;
983 case 0x31: /* div.l */
984 case 0x32: /* udiv.l */
985 case 0x33: /* mod.l */
986 case 0x34: /* umod.l */
987 {
988 int reg = (inst >> 4) & 0xf;
25ea693b 989 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
990 return -1;
991 }
992 break;
993 case 0x35: /* brk */
994 /* Do nothing. */
995 break;
996 case 0x36: /* ldo.b */
997 {
998 int reg = (inst >> 4) & 0xf;
25ea693b 999 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
1000 return -1;
1001 }
1002 break;
1003 case 0x37: /* sto.b */
1004 {
1005 int reg = (inst >> 4) & 0xf;
6441e6db
AG
1006 uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
1007 byte_order)) << 16 ) >> 16;
0b883586 1008 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
1009 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
1010 4, byte_order);
1011 tmpu32 += offset;
25ea693b 1012 if (record_full_arch_list_add_mem (tmpu32, 1))
451fa05e
AG
1013 return -1;
1014 }
1015 break;
1016 case 0x38: /* ldo.s */
1017 {
1018 int reg = (inst >> 4) & 0xf;
25ea693b 1019 if (record_full_arch_list_add_reg (regcache, reg))
451fa05e
AG
1020 return -1;
1021 }
1022 break;
1023 case 0x39: /* sto.s */
1024 {
1025 int reg = (inst >> 4) & 0xf;
6441e6db
AG
1026 uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
1027 byte_order)) << 16 ) >> 16;
0b883586 1028 regcache->raw_read (reg, (gdb_byte *) & tmpu32);
451fa05e
AG
1029 tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
1030 4, byte_order);
1031 tmpu32 += offset;
25ea693b 1032 if (record_full_arch_list_add_mem (tmpu32, 2))
451fa05e
AG
1033 return -1;
1034 }
1035 break;
1036 default:
1037 /* Do nothing. */
1038 break;
1039 }
1040 }
1041
25ea693b 1042 if (record_full_arch_list_add_reg (regcache, MOXIE_PC_REGNUM))
451fa05e 1043 return -1;
25ea693b 1044 if (record_full_arch_list_add_end ())
451fa05e
AG
1045 return -1;
1046 return 0;
1047}
1048
d7066cce
AG
1049/* Allocate and initialize the moxie gdbarch object. */
1050
1051static struct gdbarch *
1052moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1053{
1054 struct gdbarch *gdbarch;
1055 struct gdbarch_tdep *tdep;
1056
1057 /* If there is already a candidate, use it. */
1058 arches = gdbarch_list_lookup_by_info (arches, &info);
1059 if (arches != NULL)
1060 return arches->gdbarch;
1061
1062 /* Allocate space for the new architecture. */
cdd238da 1063 tdep = XCNEW (struct gdbarch_tdep);
d7066cce
AG
1064 gdbarch = gdbarch_alloc (&info, tdep);
1065
53375380
PA
1066 set_gdbarch_wchar_bit (gdbarch, 32);
1067 set_gdbarch_wchar_signed (gdbarch, 0);
1068
d7066cce
AG
1069 set_gdbarch_num_regs (gdbarch, MOXIE_NUM_REGS);
1070 set_gdbarch_sp_regnum (gdbarch, MOXIE_SP_REGNUM);
451fa05e 1071 set_gdbarch_pc_regnum (gdbarch, MOXIE_PC_REGNUM);
d7066cce
AG
1072 set_gdbarch_register_name (gdbarch, moxie_register_name);
1073 set_gdbarch_register_type (gdbarch, moxie_register_type);
1074
1075 set_gdbarch_return_value (gdbarch, moxie_return_value);
1076
1077 set_gdbarch_skip_prologue (gdbarch, moxie_skip_prologue);
1078 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
04180708
YQ
1079 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1080 moxie_breakpoint::kind_from_pc);
1081 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1082 moxie_breakpoint::bp_from_kind);
d7066cce
AG
1083 set_gdbarch_frame_align (gdbarch, moxie_frame_align);
1084
1085 frame_base_set_default (gdbarch, &moxie_frame_base);
1086
d7066cce
AG
1087 /* Hook in ABI-specific overrides, if they have been registered. */
1088 gdbarch_init_osabi (info, gdbarch);
1089
1090 /* Hook in the default unwinders. */
1091 frame_unwind_append_unwinder (gdbarch, &moxie_frame_unwind);
1092
6ed1ff02
AG
1093 /* Single stepping. */
1094 set_gdbarch_software_single_step (gdbarch, moxie_software_single_step);
1095
d7066cce
AG
1096 /* Support simple overlay manager. */
1097 set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
1098
451fa05e
AG
1099 /* Support reverse debugging. */
1100 set_gdbarch_process_record (gdbarch, moxie_process_record);
1101
d7066cce
AG
1102 return gdbarch;
1103}
1104
1105/* Register this machine's init routine. */
1106
6c265988 1107void _initialize_moxie_tdep ();
d7066cce 1108void
6c265988 1109_initialize_moxie_tdep ()
d7066cce
AG
1110{
1111 register_gdbarch_init (bfd_arch_moxie, moxie_gdbarch_init);
1112}
This page took 1.298794 seconds and 4 git commands to generate.