gdb: adjust gdbarch_tdep calls in nat files
[binutils-gdb.git] / gdb / m32c-tdep.c
1 /* Renesas M32C target-dependent code for GDB, the GNU debugger.
2
3 Copyright (C) 2004-2021 Free Software Foundation, Inc.
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 "gdb/sim-m32c.h"
22 #include "gdbtypes.h"
23 #include "regcache.h"
24 #include "arch-utils.h"
25 #include "frame.h"
26 #include "frame-unwind.h"
27 #include "symtab.h"
28 #include "gdbcore.h"
29 #include "value.h"
30 #include "reggroups.h"
31 #include "prologue-value.h"
32 #include "objfiles.h"
33
34 \f
35 /* The m32c tdep structure. */
36
37 static struct reggroup *m32c_dma_reggroup;
38
39 /* The type of a function that moves the value of REG between CACHE or
40 BUF --- in either direction. */
41 typedef enum register_status (m32c_write_reg_t) (struct m32c_reg *reg,
42 struct regcache *cache,
43 const gdb_byte *buf);
44
45 typedef enum register_status (m32c_read_reg_t) (struct m32c_reg *reg,
46 readable_regcache *cache,
47 gdb_byte *buf);
48
49 struct m32c_reg
50 {
51 /* The name of this register. */
52 const char *name;
53
54 /* Its type. */
55 struct type *type;
56
57 /* The architecture this register belongs to. */
58 struct gdbarch *arch;
59
60 /* Its GDB register number. */
61 int num;
62
63 /* Its sim register number. */
64 int sim_num;
65
66 /* Its DWARF register number, or -1 if it doesn't have one. */
67 int dwarf_num;
68
69 /* Register group memberships. */
70 unsigned int general_p : 1;
71 unsigned int dma_p : 1;
72 unsigned int system_p : 1;
73 unsigned int save_restore_p : 1;
74
75 /* Functions to read its value from a regcache, and write its value
76 to a regcache. */
77 m32c_read_reg_t *read;
78 m32c_write_reg_t *write;
79
80 /* Data for READ and WRITE functions. The exact meaning depends on
81 the specific functions selected; see the comments for those
82 functions. */
83 struct m32c_reg *rx, *ry;
84 int n;
85 };
86
87
88 /* An overestimate of the number of raw and pseudoregisters we will
89 have. The exact answer depends on the variant of the architecture
90 at hand, but we can use this to declare statically allocated
91 arrays, and bump it up when needed. */
92 #define M32C_MAX_NUM_REGS (75)
93
94 /* The largest assigned DWARF register number. */
95 #define M32C_MAX_DWARF_REGNUM (40)
96
97
98 struct m32c_gdbarch_tdep : gdbarch_tdep
99 {
100 /* All the registers for this variant, indexed by GDB register
101 number, and the number of registers present. */
102 struct m32c_reg regs[M32C_MAX_NUM_REGS] {};
103
104 /* The number of valid registers. */
105 int num_regs = 0;
106
107 /* Interesting registers. These are pointers into REGS. */
108 struct m32c_reg *pc = nullptr, *flg = nullptr;
109 struct m32c_reg *r0 = nullptr, *r1 = nullptr, *r2 = nullptr, *r3 = nullptr,
110 *a0 = nullptr, *a1 = nullptr;
111 struct m32c_reg *r2r0 = nullptr, *r3r2r1r0 = nullptr, *r3r1r2r0 = nullptr;
112 struct m32c_reg *sb = nullptr, *fb = nullptr, *sp = nullptr;
113
114 /* A table indexed by DWARF register numbers, pointing into
115 REGS. */
116 struct m32c_reg *dwarf_regs[M32C_MAX_DWARF_REGNUM + 1] {};
117
118 /* Types for this architecture. We can't use the builtin_type_foo
119 types, because they're not initialized when building a gdbarch
120 structure. */
121 struct type *voyd = nullptr, *ptr_voyd = nullptr, *func_voyd = nullptr;
122 struct type *uint8 = nullptr, *uint16 = nullptr;
123 struct type *int8 = nullptr, *int16 = nullptr, *int32 = nullptr,
124 *int64 = nullptr;
125
126 /* The types for data address and code address registers. */
127 struct type *data_addr_reg_type = nullptr, *code_addr_reg_type = nullptr;
128
129 /* The number of bytes a return address pushed by a 'jsr' instruction
130 occupies on the stack. */
131 int ret_addr_bytes = 0;
132
133 /* The number of bytes an address register occupies on the stack
134 when saved by an 'enter' or 'pushm' instruction. */
135 int push_addr_bytes = 0;
136 };
137
138 \f
139 /* Types. */
140
141 static void
142 make_types (struct gdbarch *arch)
143 {
144 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
145 unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
146 int data_addr_reg_bits, code_addr_reg_bits;
147 char type_name[50];
148
149 #if 0
150 /* This is used to clip CORE_ADDR values, so this value is
151 appropriate both on the m32c, where pointers are 32 bits long,
152 and on the m16c, where pointers are sixteen bits long, but there
153 may be code above the 64k boundary. */
154 set_gdbarch_addr_bit (arch, 24);
155 #else
156 /* GCC uses 32 bits for addrs in the dwarf info, even though
157 only 16/24 bits are used. Setting addr_bit to 24 causes
158 errors in reading the dwarf addresses. */
159 set_gdbarch_addr_bit (arch, 32);
160 #endif
161
162 set_gdbarch_int_bit (arch, 16);
163 switch (mach)
164 {
165 case bfd_mach_m16c:
166 data_addr_reg_bits = 16;
167 code_addr_reg_bits = 24;
168 set_gdbarch_ptr_bit (arch, 16);
169 tdep->ret_addr_bytes = 3;
170 tdep->push_addr_bytes = 2;
171 break;
172
173 case bfd_mach_m32c:
174 data_addr_reg_bits = 24;
175 code_addr_reg_bits = 24;
176 set_gdbarch_ptr_bit (arch, 32);
177 tdep->ret_addr_bytes = 4;
178 tdep->push_addr_bytes = 4;
179 break;
180
181 default:
182 gdb_assert_not_reached ("unexpected mach");
183 }
184
185 /* The builtin_type_mumble variables are sometimes uninitialized when
186 this is called, so we avoid using them. */
187 tdep->voyd = arch_type (arch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
188 tdep->ptr_voyd
189 = arch_pointer_type (arch, gdbarch_ptr_bit (arch), NULL, tdep->voyd);
190 tdep->func_voyd = lookup_function_type (tdep->voyd);
191
192 xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
193 gdbarch_bfd_arch_info (arch)->printable_name);
194 tdep->data_addr_reg_type
195 = arch_pointer_type (arch, data_addr_reg_bits, type_name, tdep->voyd);
196
197 xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
198 gdbarch_bfd_arch_info (arch)->printable_name);
199 tdep->code_addr_reg_type
200 = arch_pointer_type (arch, code_addr_reg_bits, type_name, tdep->func_voyd);
201
202 tdep->uint8 = arch_integer_type (arch, 8, 1, "uint8_t");
203 tdep->uint16 = arch_integer_type (arch, 16, 1, "uint16_t");
204 tdep->int8 = arch_integer_type (arch, 8, 0, "int8_t");
205 tdep->int16 = arch_integer_type (arch, 16, 0, "int16_t");
206 tdep->int32 = arch_integer_type (arch, 32, 0, "int32_t");
207 tdep->int64 = arch_integer_type (arch, 64, 0, "int64_t");
208 }
209
210
211 \f
212 /* Register set. */
213
214 static const char *
215 m32c_register_name (struct gdbarch *gdbarch, int num)
216 {
217 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
218 return tdep->regs[num].name;
219 }
220
221
222 static struct type *
223 m32c_register_type (struct gdbarch *arch, int reg_nr)
224 {
225 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
226 return tdep->regs[reg_nr].type;
227 }
228
229
230 static int
231 m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
232 {
233 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
234 return tdep->regs[reg_nr].sim_num;
235 }
236
237
238 static int
239 m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
240 {
241 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
242 if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
243 && tdep->dwarf_regs[reg_nr])
244 return tdep->dwarf_regs[reg_nr]->num;
245 else
246 /* The DWARF CFI code expects to see -1 for invalid register
247 numbers. */
248 return -1;
249 }
250
251
252 static int
253 m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
254 struct reggroup *group)
255 {
256 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
257 struct m32c_reg *reg = &tdep->regs[regnum];
258
259 /* The anonymous raw registers aren't in any groups. */
260 if (! reg->name)
261 return 0;
262
263 if (group == all_reggroup)
264 return 1;
265
266 if (group == general_reggroup
267 && reg->general_p)
268 return 1;
269
270 if (group == m32c_dma_reggroup
271 && reg->dma_p)
272 return 1;
273
274 if (group == system_reggroup
275 && reg->system_p)
276 return 1;
277
278 /* Since the m32c DWARF register numbers refer to cooked registers, not
279 raw registers, and frame_pop depends on the save and restore groups
280 containing registers the DWARF CFI will actually mention, our save
281 and restore groups are cooked registers, not raw registers. (This is
282 why we can't use the default reggroup function.) */
283 if ((group == save_reggroup
284 || group == restore_reggroup)
285 && reg->save_restore_p)
286 return 1;
287
288 return 0;
289 }
290
291
292 /* Register move functions. We declare them here using
293 m32c_{read,write}_reg_t to check the types. */
294 static m32c_read_reg_t m32c_raw_read;
295 static m32c_read_reg_t m32c_banked_read;
296 static m32c_read_reg_t m32c_sb_read;
297 static m32c_read_reg_t m32c_part_read;
298 static m32c_read_reg_t m32c_cat_read;
299 static m32c_read_reg_t m32c_r3r2r1r0_read;
300
301 static m32c_write_reg_t m32c_raw_write;
302 static m32c_write_reg_t m32c_banked_write;
303 static m32c_write_reg_t m32c_sb_write;
304 static m32c_write_reg_t m32c_part_write;
305 static m32c_write_reg_t m32c_cat_write;
306 static m32c_write_reg_t m32c_r3r2r1r0_write;
307
308 /* Copy the value of the raw register REG from CACHE to BUF. */
309 static enum register_status
310 m32c_raw_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
311 {
312 return cache->raw_read (reg->num, buf);
313 }
314
315
316 /* Copy the value of the raw register REG from BUF to CACHE. */
317 static enum register_status
318 m32c_raw_write (struct m32c_reg *reg, struct regcache *cache,
319 const gdb_byte *buf)
320 {
321 cache->raw_write (reg->num, buf);
322
323 return REG_VALID;
324 }
325
326
327 /* Return the value of the 'flg' register in CACHE. */
328 static int
329 m32c_read_flg (readable_regcache *cache)
330 {
331 gdbarch *arch = cache->arch ();
332 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
333 ULONGEST flg;
334
335 cache->raw_read (tdep->flg->num, &flg);
336 return flg & 0xffff;
337 }
338
339
340 /* Evaluate the real register number of a banked register. */
341 static struct m32c_reg *
342 m32c_banked_register (struct m32c_reg *reg, readable_regcache *cache)
343 {
344 return ((m32c_read_flg (cache) & reg->n) ? reg->ry : reg->rx);
345 }
346
347
348 /* Move the value of a banked register from CACHE to BUF.
349 If the value of the 'flg' register in CACHE has any of the bits
350 masked in REG->n set, then read REG->ry. Otherwise, read
351 REG->rx. */
352 static enum register_status
353 m32c_banked_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
354 {
355 struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
356 return cache->raw_read (bank_reg->num, buf);
357 }
358
359
360 /* Move the value of a banked register from BUF to CACHE.
361 If the value of the 'flg' register in CACHE has any of the bits
362 masked in REG->n set, then write REG->ry. Otherwise, write
363 REG->rx. */
364 static enum register_status
365 m32c_banked_write (struct m32c_reg *reg, struct regcache *cache,
366 const gdb_byte *buf)
367 {
368 struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
369 cache->raw_write (bank_reg->num, buf);
370
371 return REG_VALID;
372 }
373
374
375 /* Move the value of SB from CACHE to BUF. On bfd_mach_m32c, SB is a
376 banked register; on bfd_mach_m16c, it's not. */
377 static enum register_status
378 m32c_sb_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
379 {
380 if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
381 return m32c_raw_read (reg->rx, cache, buf);
382 else
383 return m32c_banked_read (reg, cache, buf);
384 }
385
386
387 /* Move the value of SB from BUF to CACHE. On bfd_mach_m32c, SB is a
388 banked register; on bfd_mach_m16c, it's not. */
389 static enum register_status
390 m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, const gdb_byte *buf)
391 {
392 if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
393 m32c_raw_write (reg->rx, cache, buf);
394 else
395 m32c_banked_write (reg, cache, buf);
396
397 return REG_VALID;
398 }
399
400
401 /* Assuming REG uses m32c_part_read and m32c_part_write, set *OFFSET_P
402 and *LEN_P to the offset and length, in bytes, of the part REG
403 occupies in its underlying register. The offset is from the
404 lower-addressed end, regardless of the architecture's endianness.
405 (The M32C family is always little-endian, but let's keep those
406 assumptions out of here.) */
407 static void
408 m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
409 {
410 /* The length of the containing register, of which REG is one part. */
411 int containing_len = TYPE_LENGTH (reg->rx->type);
412
413 /* The length of one "element" in our imaginary array. */
414 int elt_len = TYPE_LENGTH (reg->type);
415
416 /* The offset of REG's "element" from the least significant end of
417 the containing register. */
418 int elt_offset = reg->n * elt_len;
419
420 /* If we extend off the end, trim the length of the element. */
421 if (elt_offset + elt_len > containing_len)
422 {
423 elt_len = containing_len - elt_offset;
424 /* We shouldn't be declaring partial registers that go off the
425 end of their containing registers. */
426 gdb_assert (elt_len > 0);
427 }
428
429 /* Flip the offset around if we're big-endian. */
430 if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
431 elt_offset = TYPE_LENGTH (reg->rx->type) - elt_offset - elt_len;
432
433 *offset_p = elt_offset;
434 *len_p = elt_len;
435 }
436
437
438 /* Move the value of a partial register (r0h, intbl, etc.) from CACHE
439 to BUF. Treating the value of the register REG->rx as an array of
440 REG->type values, where higher indices refer to more significant
441 bits, read the value of the REG->n'th element. */
442 static enum register_status
443 m32c_part_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
444 {
445 int offset, len;
446
447 memset (buf, 0, TYPE_LENGTH (reg->type));
448 m32c_find_part (reg, &offset, &len);
449 return cache->cooked_read_part (reg->rx->num, offset, len, buf);
450 }
451
452
453 /* Move the value of a banked register from BUF to CACHE.
454 Treating the value of the register REG->rx as an array of REG->type
455 values, where higher indices refer to more significant bits, write
456 the value of the REG->n'th element. */
457 static enum register_status
458 m32c_part_write (struct m32c_reg *reg, struct regcache *cache,
459 const gdb_byte *buf)
460 {
461 int offset, len;
462
463 m32c_find_part (reg, &offset, &len);
464 cache->cooked_write_part (reg->rx->num, offset, len, buf);
465
466 return REG_VALID;
467 }
468
469
470 /* Move the value of REG from CACHE to BUF. REG's value is the
471 concatenation of the values of the registers REG->rx and REG->ry,
472 with REG->rx contributing the more significant bits. */
473 static enum register_status
474 m32c_cat_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
475 {
476 int high_bytes = TYPE_LENGTH (reg->rx->type);
477 int low_bytes = TYPE_LENGTH (reg->ry->type);
478 enum register_status status;
479
480 gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
481
482 if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
483 {
484 status = cache->cooked_read (reg->rx->num, buf);
485 if (status == REG_VALID)
486 status = cache->cooked_read (reg->ry->num, buf + high_bytes);
487 }
488 else
489 {
490 status = cache->cooked_read (reg->rx->num, buf + low_bytes);
491 if (status == REG_VALID)
492 status = cache->cooked_read (reg->ry->num, buf);
493 }
494 return status;
495 }
496
497
498 /* Move the value of REG from CACHE to BUF. REG's value is the
499 concatenation of the values of the registers REG->rx and REG->ry,
500 with REG->rx contributing the more significant bits. */
501 static enum register_status
502 m32c_cat_write (struct m32c_reg *reg, struct regcache *cache,
503 const gdb_byte *buf)
504 {
505 int high_bytes = TYPE_LENGTH (reg->rx->type);
506 int low_bytes = TYPE_LENGTH (reg->ry->type);
507
508 gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
509
510 if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
511 {
512 cache->cooked_write (reg->rx->num, buf);
513 cache->cooked_write (reg->ry->num, buf + high_bytes);
514 }
515 else
516 {
517 cache->cooked_write (reg->rx->num, buf + low_bytes);
518 cache->cooked_write (reg->ry->num, buf);
519 }
520
521 return REG_VALID;
522 }
523
524
525 /* Copy the value of the raw register REG from CACHE to BUF. REG is
526 the concatenation (from most significant to least) of r3, r2, r1,
527 and r0. */
528 static enum register_status
529 m32c_r3r2r1r0_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
530 {
531 gdbarch *arch = reg->arch;
532 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
533 int len = TYPE_LENGTH (tdep->r0->type);
534 enum register_status status;
535
536 if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
537 {
538 status = cache->cooked_read (tdep->r0->num, buf + len * 3);
539 if (status == REG_VALID)
540 status = cache->cooked_read (tdep->r1->num, buf + len * 2);
541 if (status == REG_VALID)
542 status = cache->cooked_read (tdep->r2->num, buf + len * 1);
543 if (status == REG_VALID)
544 status = cache->cooked_read (tdep->r3->num, buf);
545 }
546 else
547 {
548 status = cache->cooked_read (tdep->r0->num, buf);
549 if (status == REG_VALID)
550 status = cache->cooked_read (tdep->r1->num, buf + len * 1);
551 if (status == REG_VALID)
552 status = cache->cooked_read (tdep->r2->num, buf + len * 2);
553 if (status == REG_VALID)
554 status = cache->cooked_read (tdep->r3->num, buf + len * 3);
555 }
556
557 return status;
558 }
559
560
561 /* Copy the value of the raw register REG from BUF to CACHE. REG is
562 the concatenation (from most significant to least) of r3, r2, r1,
563 and r0. */
564 static enum register_status
565 m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache,
566 const gdb_byte *buf)
567 {
568 gdbarch *arch = reg->arch;
569 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
570 int len = TYPE_LENGTH (tdep->r0->type);
571
572 if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
573 {
574 cache->cooked_write (tdep->r0->num, buf + len * 3);
575 cache->cooked_write (tdep->r1->num, buf + len * 2);
576 cache->cooked_write (tdep->r2->num, buf + len * 1);
577 cache->cooked_write (tdep->r3->num, buf);
578 }
579 else
580 {
581 cache->cooked_write (tdep->r0->num, buf);
582 cache->cooked_write (tdep->r1->num, buf + len * 1);
583 cache->cooked_write (tdep->r2->num, buf + len * 2);
584 cache->cooked_write (tdep->r3->num, buf + len * 3);
585 }
586
587 return REG_VALID;
588 }
589
590
591 static enum register_status
592 m32c_pseudo_register_read (struct gdbarch *arch,
593 readable_regcache *cache,
594 int cookednum,
595 gdb_byte *buf)
596 {
597 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
598 struct m32c_reg *reg;
599
600 gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
601 gdb_assert (arch == cache->arch ());
602 gdb_assert (arch == tdep->regs[cookednum].arch);
603 reg = &tdep->regs[cookednum];
604
605 return reg->read (reg, cache, buf);
606 }
607
608
609 static void
610 m32c_pseudo_register_write (struct gdbarch *arch,
611 struct regcache *cache,
612 int cookednum,
613 const gdb_byte *buf)
614 {
615 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
616 struct m32c_reg *reg;
617
618 gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
619 gdb_assert (arch == cache->arch ());
620 gdb_assert (arch == tdep->regs[cookednum].arch);
621 reg = &tdep->regs[cookednum];
622
623 reg->write (reg, cache, buf);
624 }
625
626
627 /* Add a register with the given fields to the end of ARCH's table.
628 Return a pointer to the newly added register. */
629 static struct m32c_reg *
630 add_reg (struct gdbarch *arch,
631 const char *name,
632 struct type *type,
633 int sim_num,
634 m32c_read_reg_t *read,
635 m32c_write_reg_t *write,
636 struct m32c_reg *rx,
637 struct m32c_reg *ry,
638 int n)
639 {
640 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
641 struct m32c_reg *r = &tdep->regs[tdep->num_regs];
642
643 gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);
644
645 r->name = name;
646 r->type = type;
647 r->arch = arch;
648 r->num = tdep->num_regs;
649 r->sim_num = sim_num;
650 r->dwarf_num = -1;
651 r->general_p = 0;
652 r->dma_p = 0;
653 r->system_p = 0;
654 r->save_restore_p = 0;
655 r->read = read;
656 r->write = write;
657 r->rx = rx;
658 r->ry = ry;
659 r->n = n;
660
661 tdep->num_regs++;
662
663 return r;
664 }
665
666
667 /* Record NUM as REG's DWARF register number. */
668 static void
669 set_dwarf_regnum (struct m32c_reg *reg, int num)
670 {
671 gdb_assert (num < M32C_MAX_NUM_REGS);
672
673 /* Update the reg->DWARF mapping. Only count the first number
674 assigned to this register. */
675 if (reg->dwarf_num == -1)
676 reg->dwarf_num = num;
677
678 /* Update the DWARF->reg mapping. */
679 gdbarch *arch = reg->arch;
680 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
681 tdep->dwarf_regs[num] = reg;
682 }
683
684
685 /* Mark REG as a general-purpose register, and return it. */
686 static struct m32c_reg *
687 mark_general (struct m32c_reg *reg)
688 {
689 reg->general_p = 1;
690 return reg;
691 }
692
693
694 /* Mark REG as a DMA register. */
695 static void
696 mark_dma (struct m32c_reg *reg)
697 {
698 reg->dma_p = 1;
699 }
700
701
702 /* Mark REG as a SYSTEM register, and return it. */
703 static struct m32c_reg *
704 mark_system (struct m32c_reg *reg)
705 {
706 reg->system_p = 1;
707 return reg;
708 }
709
710
711 /* Mark REG as a save-restore register, and return it. */
712 static struct m32c_reg *
713 mark_save_restore (struct m32c_reg *reg)
714 {
715 reg->save_restore_p = 1;
716 return reg;
717 }
718
719
720 #define FLAGBIT_B 0x0010
721 #define FLAGBIT_U 0x0080
722
723 /* Handy macros for declaring registers. These all evaluate to
724 pointers to the register declared. Macros that define two
725 registers evaluate to a pointer to the first. */
726
727 /* A raw register named NAME, with type TYPE and sim number SIM_NUM. */
728 #define R(name, type, sim_num) \
729 (add_reg (arch, (name), (type), (sim_num), \
730 m32c_raw_read, m32c_raw_write, NULL, NULL, 0))
731
732 /* The simulator register number for a raw register named NAME. */
733 #define SIM(name) (m32c_sim_reg_ ## name)
734
735 /* A raw unsigned 16-bit data register named NAME.
736 NAME should be an identifier, not a string. */
737 #define R16U(name) \
738 (R(#name, tdep->uint16, SIM (name)))
739
740 /* A raw data address register named NAME.
741 NAME should be an identifier, not a string. */
742 #define RA(name) \
743 (R(#name, tdep->data_addr_reg_type, SIM (name)))
744
745 /* A raw code address register named NAME. NAME should
746 be an identifier, not a string. */
747 #define RC(name) \
748 (R(#name, tdep->code_addr_reg_type, SIM (name)))
749
750 /* A pair of raw registers named NAME0 and NAME1, with type TYPE.
751 NAME should be an identifier, not a string. */
752 #define RP(name, type) \
753 (R(#name "0", (type), SIM (name ## 0)), \
754 R(#name "1", (type), SIM (name ## 1)) - 1)
755
756 /* A raw banked general-purpose data register named NAME.
757 NAME should be an identifier, not a string. */
758 #define RBD(name) \
759 (R(NULL, tdep->int16, SIM (name ## _bank0)), \
760 R(NULL, tdep->int16, SIM (name ## _bank1)) - 1)
761
762 /* A raw banked data address register named NAME.
763 NAME should be an identifier, not a string. */
764 #define RBA(name) \
765 (R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank0)), \
766 R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank1)) - 1)
767
768 /* A cooked register named NAME referring to a raw banked register
769 from the bank selected by the current value of FLG. RAW_PAIR
770 should be a pointer to the first register in the banked pair.
771 NAME must be an identifier, not a string. */
772 #define CB(name, raw_pair) \
773 (add_reg (arch, #name, (raw_pair)->type, 0, \
774 m32c_banked_read, m32c_banked_write, \
775 (raw_pair), (raw_pair + 1), FLAGBIT_B))
776
777 /* A pair of registers named NAMEH and NAMEL, of type TYPE, that
778 access the top and bottom halves of the register pointed to by
779 NAME. NAME should be an identifier. */
780 #define CHL(name, type) \
781 (add_reg (arch, #name "h", (type), 0, \
782 m32c_part_read, m32c_part_write, name, NULL, 1), \
783 add_reg (arch, #name "l", (type), 0, \
784 m32c_part_read, m32c_part_write, name, NULL, 0) - 1)
785
786 /* A register constructed by concatenating the two registers HIGH and
787 LOW, whose name is HIGHLOW and whose type is TYPE. */
788 #define CCAT(high, low, type) \
789 (add_reg (arch, #high #low, (type), 0, \
790 m32c_cat_read, m32c_cat_write, (high), (low), 0))
791
792 /* Abbreviations for marking register group membership. */
793 #define G(reg) (mark_general (reg))
794 #define S(reg) (mark_system (reg))
795 #define DMA(reg) (mark_dma (reg))
796
797
798 /* Construct the register set for ARCH. */
799 static void
800 make_regs (struct gdbarch *arch)
801 {
802 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
803 int mach = gdbarch_bfd_arch_info (arch)->mach;
804 int num_raw_regs;
805 int num_cooked_regs;
806
807 struct m32c_reg *r0;
808 struct m32c_reg *r1;
809 struct m32c_reg *r2;
810 struct m32c_reg *r3;
811 struct m32c_reg *a0;
812 struct m32c_reg *a1;
813 struct m32c_reg *fb;
814 struct m32c_reg *sb;
815 struct m32c_reg *sp;
816 struct m32c_reg *r0hl;
817 struct m32c_reg *r1hl;
818 struct m32c_reg *r2r0;
819 struct m32c_reg *r3r1;
820 struct m32c_reg *r3r1r2r0;
821 struct m32c_reg *r3r2r1r0;
822 struct m32c_reg *a1a0;
823
824 struct m32c_reg *raw_r0_pair = RBD (r0);
825 struct m32c_reg *raw_r1_pair = RBD (r1);
826 struct m32c_reg *raw_r2_pair = RBD (r2);
827 struct m32c_reg *raw_r3_pair = RBD (r3);
828 struct m32c_reg *raw_a0_pair = RBA (a0);
829 struct m32c_reg *raw_a1_pair = RBA (a1);
830 struct m32c_reg *raw_fb_pair = RBA (fb);
831
832 /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
833 We always declare both raw registers, and deal with the distinction
834 in the pseudoregister. */
835 struct m32c_reg *raw_sb_pair = RBA (sb);
836
837 struct m32c_reg *usp = S (RA (usp));
838 struct m32c_reg *isp = S (RA (isp));
839 struct m32c_reg *intb = S (RC (intb));
840 struct m32c_reg *pc = G (RC (pc));
841 struct m32c_reg *flg = G (R16U (flg));
842
843 if (mach == bfd_mach_m32c)
844 {
845 S (R16U (svf));
846 S (RC (svp));
847 S (RC (vct));
848
849 DMA (RP (dmd, tdep->uint8));
850 DMA (RP (dct, tdep->uint16));
851 DMA (RP (drc, tdep->uint16));
852 DMA (RP (dma, tdep->data_addr_reg_type));
853 DMA (RP (dsa, tdep->data_addr_reg_type));
854 DMA (RP (dra, tdep->data_addr_reg_type));
855 }
856
857 num_raw_regs = tdep->num_regs;
858
859 r0 = G (CB (r0, raw_r0_pair));
860 r1 = G (CB (r1, raw_r1_pair));
861 r2 = G (CB (r2, raw_r2_pair));
862 r3 = G (CB (r3, raw_r3_pair));
863 a0 = G (CB (a0, raw_a0_pair));
864 a1 = G (CB (a1, raw_a1_pair));
865 fb = G (CB (fb, raw_fb_pair));
866
867 /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
868 Specify custom read/write functions that do the right thing. */
869 sb = G (add_reg (arch, "sb", raw_sb_pair->type, 0,
870 m32c_sb_read, m32c_sb_write,
871 raw_sb_pair, raw_sb_pair + 1, 0));
872
873 /* The current sp is either usp or isp, depending on the value of
874 the FLG register's U bit. */
875 sp = G (add_reg (arch, "sp", usp->type, 0,
876 m32c_banked_read, m32c_banked_write,
877 isp, usp, FLAGBIT_U));
878
879 r0hl = CHL (r0, tdep->int8);
880 r1hl = CHL (r1, tdep->int8);
881 CHL (r2, tdep->int8);
882 CHL (r3, tdep->int8);
883 CHL (intb, tdep->int16);
884
885 r2r0 = CCAT (r2, r0, tdep->int32);
886 r3r1 = CCAT (r3, r1, tdep->int32);
887 r3r1r2r0 = CCAT (r3r1, r2r0, tdep->int64);
888
889 r3r2r1r0
890 = add_reg (arch, "r3r2r1r0", tdep->int64, 0,
891 m32c_r3r2r1r0_read, m32c_r3r2r1r0_write, NULL, NULL, 0);
892
893 if (mach == bfd_mach_m16c)
894 a1a0 = CCAT (a1, a0, tdep->int32);
895 else
896 a1a0 = NULL;
897
898 num_cooked_regs = tdep->num_regs - num_raw_regs;
899
900 tdep->pc = pc;
901 tdep->flg = flg;
902 tdep->r0 = r0;
903 tdep->r1 = r1;
904 tdep->r2 = r2;
905 tdep->r3 = r3;
906 tdep->r2r0 = r2r0;
907 tdep->r3r2r1r0 = r3r2r1r0;
908 tdep->r3r1r2r0 = r3r1r2r0;
909 tdep->a0 = a0;
910 tdep->a1 = a1;
911 tdep->sb = sb;
912 tdep->fb = fb;
913 tdep->sp = sp;
914
915 /* Set up the DWARF register table. */
916 memset (tdep->dwarf_regs, 0, sizeof (tdep->dwarf_regs));
917 set_dwarf_regnum (r0hl + 1, 0x01);
918 set_dwarf_regnum (r0hl + 0, 0x02);
919 set_dwarf_regnum (r1hl + 1, 0x03);
920 set_dwarf_regnum (r1hl + 0, 0x04);
921 set_dwarf_regnum (r0, 0x05);
922 set_dwarf_regnum (r1, 0x06);
923 set_dwarf_regnum (r2, 0x07);
924 set_dwarf_regnum (r3, 0x08);
925 set_dwarf_regnum (a0, 0x09);
926 set_dwarf_regnum (a1, 0x0a);
927 set_dwarf_regnum (fb, 0x0b);
928 set_dwarf_regnum (sp, 0x0c);
929 set_dwarf_regnum (pc, 0x0d); /* GCC's invention */
930 set_dwarf_regnum (sb, 0x13);
931 set_dwarf_regnum (r2r0, 0x15);
932 set_dwarf_regnum (r3r1, 0x16);
933 if (a1a0)
934 set_dwarf_regnum (a1a0, 0x17);
935
936 /* Enumerate the save/restore register group.
937
938 The regcache_save and regcache_restore functions apply their read
939 function to each register in this group.
940
941 Since frame_pop supplies frame_unwind_register as its read
942 function, the registers meaningful to the Dwarf unwinder need to
943 be in this group.
944
945 On the other hand, when we make inferior calls, save_inferior_status
946 and restore_inferior_status use them to preserve the current register
947 values across the inferior call. For this, you'd kind of like to
948 preserve all the raw registers, to protect the interrupted code from
949 any sort of bank switching the callee might have done. But we handle
950 those cases so badly anyway --- for example, it matters whether we
951 restore FLG before or after we restore the general-purpose registers,
952 but there's no way to express that --- that it isn't worth worrying
953 about.
954
955 We omit control registers like inthl: if you call a function that
956 changes those, it's probably because you wanted that change to be
957 visible to the interrupted code. */
958 mark_save_restore (r0);
959 mark_save_restore (r1);
960 mark_save_restore (r2);
961 mark_save_restore (r3);
962 mark_save_restore (a0);
963 mark_save_restore (a1);
964 mark_save_restore (sb);
965 mark_save_restore (fb);
966 mark_save_restore (sp);
967 mark_save_restore (pc);
968 mark_save_restore (flg);
969
970 set_gdbarch_num_regs (arch, num_raw_regs);
971 set_gdbarch_num_pseudo_regs (arch, num_cooked_regs);
972 set_gdbarch_pc_regnum (arch, pc->num);
973 set_gdbarch_sp_regnum (arch, sp->num);
974 set_gdbarch_register_name (arch, m32c_register_name);
975 set_gdbarch_register_type (arch, m32c_register_type);
976 set_gdbarch_pseudo_register_read (arch, m32c_pseudo_register_read);
977 set_gdbarch_pseudo_register_write (arch, m32c_pseudo_register_write);
978 set_gdbarch_register_sim_regno (arch, m32c_register_sim_regno);
979 set_gdbarch_stab_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
980 set_gdbarch_dwarf2_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
981 set_gdbarch_register_reggroup_p (arch, m32c_register_reggroup_p);
982
983 reggroup_add (arch, general_reggroup);
984 reggroup_add (arch, all_reggroup);
985 reggroup_add (arch, save_reggroup);
986 reggroup_add (arch, restore_reggroup);
987 reggroup_add (arch, system_reggroup);
988 reggroup_add (arch, m32c_dma_reggroup);
989 }
990
991
992 \f
993 /* Breakpoints. */
994 constexpr gdb_byte m32c_break_insn[] = { 0x00 }; /* brk */
995
996 typedef BP_MANIPULATION (m32c_break_insn) m32c_breakpoint;
997
998 \f
999 /* Prologue analysis. */
1000
1001 enum m32c_prologue_kind
1002 {
1003 /* This function uses a frame pointer. */
1004 prologue_with_frame_ptr,
1005
1006 /* This function has no frame pointer. */
1007 prologue_sans_frame_ptr,
1008
1009 /* This function sets up the stack, so its frame is the first
1010 frame on the stack. */
1011 prologue_first_frame
1012 };
1013
1014 struct m32c_prologue
1015 {
1016 /* For consistency with the DWARF 2 .debug_frame info generated by
1017 GCC, a frame's CFA is the address immediately after the saved
1018 return address. */
1019
1020 /* The architecture for which we generated this prologue info. */
1021 struct gdbarch *arch;
1022
1023 enum m32c_prologue_kind kind;
1024
1025 /* If KIND is prologue_with_frame_ptr, this is the offset from the
1026 CFA to where the frame pointer points. This is always zero or
1027 negative. */
1028 LONGEST frame_ptr_offset;
1029
1030 /* If KIND is prologue_sans_frame_ptr, the offset from the CFA to
1031 the stack pointer --- always zero or negative.
1032
1033 Calling this a "size" is a bit misleading, but given that the
1034 stack grows downwards, using offsets for everything keeps one
1035 from going completely sign-crazy: you never change anything's
1036 sign for an ADD instruction; always change the second operand's
1037 sign for a SUB instruction; and everything takes care of
1038 itself.
1039
1040 Functions that use alloca don't have a constant frame size. But
1041 they always have frame pointers, so we must use that to find the
1042 CFA (and perhaps to unwind the stack pointer). */
1043 LONGEST frame_size;
1044
1045 /* The address of the first instruction at which the frame has been
1046 set up and the arguments are where the debug info says they are
1047 --- as best as we can tell. */
1048 CORE_ADDR prologue_end;
1049
1050 /* reg_offset[R] is the offset from the CFA at which register R is
1051 saved, or 1 if register R has not been saved. (Real values are
1052 always zero or negative.) */
1053 LONGEST reg_offset[M32C_MAX_NUM_REGS];
1054 };
1055
1056
1057 /* The longest I've seen, anyway. */
1058 #define M32C_MAX_INSN_LEN (9)
1059
1060 /* Processor state, for the prologue analyzer. */
1061 struct m32c_pv_state
1062 {
1063 struct gdbarch *arch;
1064 pv_t r0, r1, r2, r3;
1065 pv_t a0, a1;
1066 pv_t sb, fb, sp;
1067 pv_t pc;
1068 struct pv_area *stack;
1069
1070 /* Bytes from the current PC, the address they were read from,
1071 and the address of the next unconsumed byte. */
1072 gdb_byte insn[M32C_MAX_INSN_LEN];
1073 CORE_ADDR scan_pc, next_addr;
1074 };
1075
1076
1077 /* Push VALUE on STATE's stack, occupying SIZE bytes. Return zero if
1078 all went well, or non-zero if simulating the action would trash our
1079 state. */
1080 static int
1081 m32c_pv_push (struct m32c_pv_state *state, pv_t value, int size)
1082 {
1083 if (state->stack->store_would_trash (state->sp))
1084 return 1;
1085
1086 state->sp = pv_add_constant (state->sp, -size);
1087 state->stack->store (state->sp, size, value);
1088
1089 return 0;
1090 }
1091
1092
1093 enum srcdest_kind
1094 {
1095 srcdest_reg,
1096 srcdest_partial_reg,
1097 srcdest_mem
1098 };
1099
1100 /* A source or destination location for an m16c or m32c
1101 instruction. */
1102 struct srcdest
1103 {
1104 /* If srcdest_reg, the location is a register pointed to by REG.
1105 If srcdest_partial_reg, the location is part of a register pointed
1106 to by REG. We don't try to handle this too well.
1107 If srcdest_mem, the location is memory whose address is ADDR. */
1108 enum srcdest_kind kind;
1109 pv_t *reg, addr;
1110 };
1111
1112
1113 /* Return the SIZE-byte value at LOC in STATE. */
1114 static pv_t
1115 m32c_srcdest_fetch (struct m32c_pv_state *state, struct srcdest loc, int size)
1116 {
1117 if (loc.kind == srcdest_mem)
1118 return state->stack->fetch (loc.addr, size);
1119 else if (loc.kind == srcdest_partial_reg)
1120 return pv_unknown ();
1121 else
1122 return *loc.reg;
1123 }
1124
1125
1126 /* Write VALUE, a SIZE-byte value, to LOC in STATE. Return zero if
1127 all went well, or non-zero if simulating the store would trash our
1128 state. */
1129 static int
1130 m32c_srcdest_store (struct m32c_pv_state *state, struct srcdest loc,
1131 pv_t value, int size)
1132 {
1133 if (loc.kind == srcdest_mem)
1134 {
1135 if (state->stack->store_would_trash (loc.addr))
1136 return 1;
1137 state->stack->store (loc.addr, size, value);
1138 }
1139 else if (loc.kind == srcdest_partial_reg)
1140 *loc.reg = pv_unknown ();
1141 else
1142 *loc.reg = value;
1143
1144 return 0;
1145 }
1146
1147
1148 static int
1149 m32c_sign_ext (int v, int bits)
1150 {
1151 int mask = 1 << (bits - 1);
1152 return (v ^ mask) - mask;
1153 }
1154
1155 static unsigned int
1156 m32c_next_byte (struct m32c_pv_state *st)
1157 {
1158 gdb_assert (st->next_addr - st->scan_pc < sizeof (st->insn));
1159 return st->insn[st->next_addr++ - st->scan_pc];
1160 }
1161
1162 static int
1163 m32c_udisp8 (struct m32c_pv_state *st)
1164 {
1165 return m32c_next_byte (st);
1166 }
1167
1168
1169 static int
1170 m32c_sdisp8 (struct m32c_pv_state *st)
1171 {
1172 return m32c_sign_ext (m32c_next_byte (st), 8);
1173 }
1174
1175
1176 static int
1177 m32c_udisp16 (struct m32c_pv_state *st)
1178 {
1179 int low = m32c_next_byte (st);
1180 int high = m32c_next_byte (st);
1181
1182 return low + (high << 8);
1183 }
1184
1185
1186 static int
1187 m32c_sdisp16 (struct m32c_pv_state *st)
1188 {
1189 int low = m32c_next_byte (st);
1190 int high = m32c_next_byte (st);
1191
1192 return m32c_sign_ext (low + (high << 8), 16);
1193 }
1194
1195
1196 static int
1197 m32c_udisp24 (struct m32c_pv_state *st)
1198 {
1199 int low = m32c_next_byte (st);
1200 int mid = m32c_next_byte (st);
1201 int high = m32c_next_byte (st);
1202
1203 return low + (mid << 8) + (high << 16);
1204 }
1205
1206
1207 /* Extract the 'source' field from an m32c MOV.size:G-format instruction. */
1208 static int
1209 m32c_get_src23 (unsigned char *i)
1210 {
1211 return (((i[0] & 0x70) >> 2)
1212 | ((i[1] & 0x30) >> 4));
1213 }
1214
1215
1216 /* Extract the 'dest' field from an m32c MOV.size:G-format instruction. */
1217 static int
1218 m32c_get_dest23 (unsigned char *i)
1219 {
1220 return (((i[0] & 0x0e) << 1)
1221 | ((i[1] & 0xc0) >> 6));
1222 }
1223
1224
1225 static struct srcdest
1226 m32c_decode_srcdest4 (struct m32c_pv_state *st,
1227 int code, int size)
1228 {
1229 struct srcdest sd;
1230
1231 if (code < 6)
1232 sd.kind = (size == 2 ? srcdest_reg : srcdest_partial_reg);
1233 else
1234 sd.kind = srcdest_mem;
1235
1236 sd.addr = pv_unknown ();
1237 sd.reg = 0;
1238
1239 switch (code)
1240 {
1241 case 0x0: sd.reg = &st->r0; break;
1242 case 0x1: sd.reg = (size == 1 ? &st->r0 : &st->r1); break;
1243 case 0x2: sd.reg = (size == 1 ? &st->r1 : &st->r2); break;
1244 case 0x3: sd.reg = (size == 1 ? &st->r1 : &st->r3); break;
1245
1246 case 0x4: sd.reg = &st->a0; break;
1247 case 0x5: sd.reg = &st->a1; break;
1248
1249 case 0x6: sd.addr = st->a0; break;
1250 case 0x7: sd.addr = st->a1; break;
1251
1252 case 0x8: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
1253 case 0x9: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
1254 case 0xa: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
1255 case 0xb: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
1256
1257 case 0xc: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
1258 case 0xd: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
1259 case 0xe: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
1260 case 0xf: sd.addr = pv_constant (m32c_udisp16 (st)); break;
1261
1262 default:
1263 gdb_assert_not_reached ("unexpected srcdest4");
1264 }
1265
1266 return sd;
1267 }
1268
1269
1270 static struct srcdest
1271 m32c_decode_sd23 (struct m32c_pv_state *st, int code, int size, int ind)
1272 {
1273 struct srcdest sd;
1274
1275 sd.addr = pv_unknown ();
1276 sd.reg = 0;
1277
1278 switch (code)
1279 {
1280 case 0x12:
1281 case 0x13:
1282 case 0x10:
1283 case 0x11:
1284 sd.kind = (size == 1) ? srcdest_partial_reg : srcdest_reg;
1285 break;
1286
1287 case 0x02:
1288 case 0x03:
1289 sd.kind = (size == 4) ? srcdest_reg : srcdest_partial_reg;
1290 break;
1291
1292 default:
1293 sd.kind = srcdest_mem;
1294 break;
1295
1296 }
1297
1298 switch (code)
1299 {
1300 case 0x12: sd.reg = &st->r0; break;
1301 case 0x13: sd.reg = &st->r1; break;
1302 case 0x10: sd.reg = ((size == 1) ? &st->r0 : &st->r2); break;
1303 case 0x11: sd.reg = ((size == 1) ? &st->r1 : &st->r3); break;
1304 case 0x02: sd.reg = &st->a0; break;
1305 case 0x03: sd.reg = &st->a1; break;
1306
1307 case 0x00: sd.addr = st->a0; break;
1308 case 0x01: sd.addr = st->a1; break;
1309 case 0x04: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
1310 case 0x05: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
1311 case 0x06: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
1312 case 0x07: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
1313 case 0x08: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
1314 case 0x09: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
1315 case 0x0a: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
1316 case 0x0b: sd.addr = pv_add_constant (st->fb, m32c_sdisp16 (st)); break;
1317 case 0x0c: sd.addr = pv_add_constant (st->a0, m32c_udisp24 (st)); break;
1318 case 0x0d: sd.addr = pv_add_constant (st->a1, m32c_udisp24 (st)); break;
1319 case 0x0f: sd.addr = pv_constant (m32c_udisp16 (st)); break;
1320 case 0x0e: sd.addr = pv_constant (m32c_udisp24 (st)); break;
1321 default:
1322 gdb_assert_not_reached ("unexpected sd23");
1323 }
1324
1325 if (ind)
1326 {
1327 sd.addr = m32c_srcdest_fetch (st, sd, 4);
1328 sd.kind = srcdest_mem;
1329 }
1330
1331 return sd;
1332 }
1333
1334
1335 /* The r16c and r32c machines have instructions with similar
1336 semantics, but completely different machine language encodings. So
1337 we break out the semantics into their own functions, and leave
1338 machine-specific decoding in m32c_analyze_prologue.
1339
1340 The following functions all expect their arguments already decoded,
1341 and they all return zero if analysis should continue past this
1342 instruction, or non-zero if analysis should stop. */
1343
1344
1345 /* Simulate an 'enter SIZE' instruction in STATE. */
1346 static int
1347 m32c_pv_enter (struct m32c_pv_state *state, int size)
1348 {
1349 /* If simulating this store would require us to forget
1350 everything we know about the stack frame in the name of
1351 accuracy, it would be better to just quit now. */
1352 if (state->stack->store_would_trash (state->sp))
1353 return 1;
1354
1355 gdbarch *arch = state->arch;
1356 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1357 if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
1358 return 1;
1359
1360 state->fb = state->sp;
1361 state->sp = pv_add_constant (state->sp, -size);
1362
1363 return 0;
1364 }
1365
1366
1367 static int
1368 m32c_pv_pushm_one (struct m32c_pv_state *state, pv_t reg,
1369 int bit, int src, int size)
1370 {
1371 if (bit & src)
1372 {
1373 if (m32c_pv_push (state, reg, size))
1374 return 1;
1375 }
1376
1377 return 0;
1378 }
1379
1380
1381 /* Simulate a 'pushm SRC' instruction in STATE. */
1382 static int
1383 m32c_pv_pushm (struct m32c_pv_state *state, int src)
1384 {
1385 gdbarch *arch = state->arch;
1386 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1387
1388 /* The bits in SRC indicating which registers to save are:
1389 r0 r1 r2 r3 a0 a1 sb fb */
1390 return
1391 ( m32c_pv_pushm_one (state, state->fb, 0x01, src, tdep->push_addr_bytes)
1392 || m32c_pv_pushm_one (state, state->sb, 0x02, src, tdep->push_addr_bytes)
1393 || m32c_pv_pushm_one (state, state->a1, 0x04, src, tdep->push_addr_bytes)
1394 || m32c_pv_pushm_one (state, state->a0, 0x08, src, tdep->push_addr_bytes)
1395 || m32c_pv_pushm_one (state, state->r3, 0x10, src, 2)
1396 || m32c_pv_pushm_one (state, state->r2, 0x20, src, 2)
1397 || m32c_pv_pushm_one (state, state->r1, 0x40, src, 2)
1398 || m32c_pv_pushm_one (state, state->r0, 0x80, src, 2));
1399 }
1400
1401 /* Return non-zero if VALUE is the first incoming argument register. */
1402
1403 static int
1404 m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
1405 {
1406 gdbarch *arch = state->arch;
1407 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1408
1409 return (value.kind == pvk_register
1410 && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
1411 ? (value.reg == tdep->r1->num)
1412 : (value.reg == tdep->r0->num))
1413 && value.k == 0);
1414 }
1415
1416 /* Return non-zero if VALUE is an incoming argument register. */
1417
1418 static int
1419 m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
1420 {
1421 gdbarch *arch = state->arch;
1422 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1423
1424 return (value.kind == pvk_register
1425 && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
1426 ? (value.reg == tdep->r1->num || value.reg == tdep->r2->num)
1427 : (value.reg == tdep->r0->num))
1428 && value.k == 0);
1429 }
1430
1431 /* Return non-zero if a store of VALUE to LOC is probably spilling an
1432 argument register to its stack slot in STATE. Such instructions
1433 should be included in the prologue, if possible.
1434
1435 The store is a spill if:
1436 - the value being stored is the original value of an argument register;
1437 - the value has not already been stored somewhere in STACK; and
1438 - LOC is a stack slot (e.g., a memory location whose address is
1439 relative to the original value of the SP). */
1440
1441 static int
1442 m32c_is_arg_spill (struct m32c_pv_state *st,
1443 struct srcdest loc,
1444 pv_t value)
1445 {
1446 gdbarch *arch = st->arch;
1447 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1448
1449 return (m32c_is_arg_reg (st, value)
1450 && loc.kind == srcdest_mem
1451 && pv_is_register (loc.addr, tdep->sp->num)
1452 && ! st->stack->find_reg (st->arch, value.reg, 0));
1453 }
1454
1455 /* Return non-zero if a store of VALUE to LOC is probably
1456 copying the struct return address into an address register
1457 for immediate use. This is basically a "spill" into the
1458 address register, instead of onto the stack.
1459
1460 The prerequisites are:
1461 - value being stored is original value of the FIRST arg register;
1462 - value has not already been stored on stack; and
1463 - LOC is an address register (a0 or a1). */
1464
1465 static int
1466 m32c_is_struct_return (struct m32c_pv_state *st,
1467 struct srcdest loc,
1468 pv_t value)
1469 {
1470 gdbarch *arch = st->arch;
1471 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1472
1473 return (m32c_is_1st_arg_reg (st, value)
1474 && !st->stack->find_reg (st->arch, value.reg, 0)
1475 && loc.kind == srcdest_reg
1476 && (pv_is_register (*loc.reg, tdep->a0->num)
1477 || pv_is_register (*loc.reg, tdep->a1->num)));
1478 }
1479
1480 /* Return non-zero if a 'pushm' saving the registers indicated by SRC
1481 was a register save:
1482 - all the named registers should have their original values, and
1483 - the stack pointer should be at a constant offset from the
1484 original stack pointer. */
1485 static int
1486 m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
1487 {
1488 gdbarch *arch = st->arch;
1489 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1490
1491 /* The bits in SRC indicating which registers to save are:
1492 r0 r1 r2 r3 a0 a1 sb fb */
1493 return
1494 (pv_is_register (st->sp, tdep->sp->num)
1495 && (! (src & 0x01) || pv_is_register_k (st->fb, tdep->fb->num, 0))
1496 && (! (src & 0x02) || pv_is_register_k (st->sb, tdep->sb->num, 0))
1497 && (! (src & 0x04) || pv_is_register_k (st->a1, tdep->a1->num, 0))
1498 && (! (src & 0x08) || pv_is_register_k (st->a0, tdep->a0->num, 0))
1499 && (! (src & 0x10) || pv_is_register_k (st->r3, tdep->r3->num, 0))
1500 && (! (src & 0x20) || pv_is_register_k (st->r2, tdep->r2->num, 0))
1501 && (! (src & 0x40) || pv_is_register_k (st->r1, tdep->r1->num, 0))
1502 && (! (src & 0x80) || pv_is_register_k (st->r0, tdep->r0->num, 0)));
1503 }
1504
1505
1506 /* Function for finding saved registers in a 'struct pv_area'; we pass
1507 this to pv_area::scan.
1508
1509 If VALUE is a saved register, ADDR says it was saved at a constant
1510 offset from the frame base, and SIZE indicates that the whole
1511 register was saved, record its offset in RESULT_UNTYPED. */
1512 static void
1513 check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
1514 {
1515 struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
1516 struct gdbarch *arch = prologue->arch;
1517 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1518
1519 /* Is this the unchanged value of some register being saved on the
1520 stack? */
1521 if (value.kind == pvk_register
1522 && value.k == 0
1523 && pv_is_register (addr, tdep->sp->num))
1524 {
1525 /* Some registers require special handling: they're saved as a
1526 larger value than the register itself. */
1527 CORE_ADDR saved_size = register_size (arch, value.reg);
1528
1529 if (value.reg == tdep->pc->num)
1530 saved_size = tdep->ret_addr_bytes;
1531 else if (register_type (arch, value.reg)
1532 == tdep->data_addr_reg_type)
1533 saved_size = tdep->push_addr_bytes;
1534
1535 if (size == saved_size)
1536 {
1537 /* Find which end of the saved value corresponds to our
1538 register. */
1539 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
1540 prologue->reg_offset[value.reg]
1541 = (addr.k + saved_size - register_size (arch, value.reg));
1542 else
1543 prologue->reg_offset[value.reg] = addr.k;
1544 }
1545 }
1546 }
1547
1548
1549 /* Analyze the function prologue for ARCH at START, going no further
1550 than LIMIT, and place a description of what we found in
1551 PROLOGUE. */
1552 static void
1553 m32c_analyze_prologue (struct gdbarch *arch,
1554 CORE_ADDR start, CORE_ADDR limit,
1555 struct m32c_prologue *prologue)
1556 {
1557 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1558 unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
1559 CORE_ADDR after_last_frame_related_insn;
1560 struct m32c_pv_state st;
1561
1562 st.arch = arch;
1563 st.r0 = pv_register (tdep->r0->num, 0);
1564 st.r1 = pv_register (tdep->r1->num, 0);
1565 st.r2 = pv_register (tdep->r2->num, 0);
1566 st.r3 = pv_register (tdep->r3->num, 0);
1567 st.a0 = pv_register (tdep->a0->num, 0);
1568 st.a1 = pv_register (tdep->a1->num, 0);
1569 st.sb = pv_register (tdep->sb->num, 0);
1570 st.fb = pv_register (tdep->fb->num, 0);
1571 st.sp = pv_register (tdep->sp->num, 0);
1572 st.pc = pv_register (tdep->pc->num, 0);
1573 pv_area stack (tdep->sp->num, gdbarch_addr_bit (arch));
1574 st.stack = &stack;
1575
1576 /* Record that the call instruction has saved the return address on
1577 the stack. */
1578 m32c_pv_push (&st, st.pc, tdep->ret_addr_bytes);
1579
1580 memset (prologue, 0, sizeof (*prologue));
1581 prologue->arch = arch;
1582 {
1583 int i;
1584 for (i = 0; i < M32C_MAX_NUM_REGS; i++)
1585 prologue->reg_offset[i] = 1;
1586 }
1587
1588 st.scan_pc = after_last_frame_related_insn = start;
1589
1590 while (st.scan_pc < limit)
1591 {
1592 pv_t pre_insn_fb = st.fb;
1593 pv_t pre_insn_sp = st.sp;
1594
1595 /* In theory we could get in trouble by trying to read ahead
1596 here, when we only know we're expecting one byte. In
1597 practice I doubt anyone will care, and it makes the rest of
1598 the code easier. */
1599 if (target_read_memory (st.scan_pc, st.insn, sizeof (st.insn)))
1600 /* If we can't fetch the instruction from memory, stop here
1601 and hope for the best. */
1602 break;
1603 st.next_addr = st.scan_pc;
1604
1605 /* The assembly instructions are written as they appear in the
1606 section of the processor manuals that describe the
1607 instruction encodings.
1608
1609 When a single assembly language instruction has several
1610 different machine-language encodings, the manual
1611 distinguishes them by a number in parens, before the
1612 mnemonic. Those numbers are included, as well.
1613
1614 The srcdest decoding instructions have the same names as the
1615 analogous functions in the simulator. */
1616 if (mach == bfd_mach_m16c)
1617 {
1618 /* (1) ENTER #imm8 */
1619 if (st.insn[0] == 0x7c && st.insn[1] == 0xf2)
1620 {
1621 if (m32c_pv_enter (&st, st.insn[2]))
1622 break;
1623 st.next_addr += 3;
1624 }
1625 /* (1) PUSHM src */
1626 else if (st.insn[0] == 0xec)
1627 {
1628 int src = st.insn[1];
1629 if (m32c_pv_pushm (&st, src))
1630 break;
1631 st.next_addr += 2;
1632
1633 if (m32c_pushm_is_reg_save (&st, src))
1634 after_last_frame_related_insn = st.next_addr;
1635 }
1636
1637 /* (6) MOV.size:G src, dest */
1638 else if ((st.insn[0] & 0xfe) == 0x72)
1639 {
1640 int size = (st.insn[0] & 0x01) ? 2 : 1;
1641 struct srcdest src;
1642 struct srcdest dest;
1643 pv_t src_value;
1644 st.next_addr += 2;
1645
1646 src
1647 = m32c_decode_srcdest4 (&st, (st.insn[1] >> 4) & 0xf, size);
1648 dest
1649 = m32c_decode_srcdest4 (&st, st.insn[1] & 0xf, size);
1650 src_value = m32c_srcdest_fetch (&st, src, size);
1651
1652 if (m32c_is_arg_spill (&st, dest, src_value))
1653 after_last_frame_related_insn = st.next_addr;
1654 else if (m32c_is_struct_return (&st, dest, src_value))
1655 after_last_frame_related_insn = st.next_addr;
1656
1657 if (m32c_srcdest_store (&st, dest, src_value, size))
1658 break;
1659 }
1660
1661 /* (1) LDC #IMM16, sp */
1662 else if (st.insn[0] == 0xeb
1663 && st.insn[1] == 0x50)
1664 {
1665 st.next_addr += 2;
1666 st.sp = pv_constant (m32c_udisp16 (&st));
1667 }
1668
1669 else
1670 /* We've hit some instruction we don't know how to simulate.
1671 Strictly speaking, we should set every value we're
1672 tracking to "unknown". But we'll be optimistic, assume
1673 that we have enough information already, and stop
1674 analysis here. */
1675 break;
1676 }
1677 else
1678 {
1679 int src_indirect = 0;
1680 int dest_indirect = 0;
1681 int i = 0;
1682
1683 gdb_assert (mach == bfd_mach_m32c);
1684
1685 /* Check for prefix bytes indicating indirect addressing. */
1686 if (st.insn[0] == 0x41)
1687 {
1688 src_indirect = 1;
1689 i++;
1690 }
1691 else if (st.insn[0] == 0x09)
1692 {
1693 dest_indirect = 1;
1694 i++;
1695 }
1696 else if (st.insn[0] == 0x49)
1697 {
1698 src_indirect = dest_indirect = 1;
1699 i++;
1700 }
1701
1702 /* (1) ENTER #imm8 */
1703 if (st.insn[i] == 0xec)
1704 {
1705 if (m32c_pv_enter (&st, st.insn[i + 1]))
1706 break;
1707 st.next_addr += 2;
1708 }
1709
1710 /* (1) PUSHM src */
1711 else if (st.insn[i] == 0x8f)
1712 {
1713 int src = st.insn[i + 1];
1714 if (m32c_pv_pushm (&st, src))
1715 break;
1716 st.next_addr += 2;
1717
1718 if (m32c_pushm_is_reg_save (&st, src))
1719 after_last_frame_related_insn = st.next_addr;
1720 }
1721
1722 /* (7) MOV.size:G src, dest */
1723 else if ((st.insn[i] & 0x80) == 0x80
1724 && (st.insn[i + 1] & 0x0f) == 0x0b
1725 && m32c_get_src23 (&st.insn[i]) < 20
1726 && m32c_get_dest23 (&st.insn[i]) < 20)
1727 {
1728 struct srcdest src;
1729 struct srcdest dest;
1730 pv_t src_value;
1731 int bw = st.insn[i] & 0x01;
1732 int size = bw ? 2 : 1;
1733 st.next_addr += 2;
1734
1735 src
1736 = m32c_decode_sd23 (&st, m32c_get_src23 (&st.insn[i]),
1737 size, src_indirect);
1738 dest
1739 = m32c_decode_sd23 (&st, m32c_get_dest23 (&st.insn[i]),
1740 size, dest_indirect);
1741 src_value = m32c_srcdest_fetch (&st, src, size);
1742
1743 if (m32c_is_arg_spill (&st, dest, src_value))
1744 after_last_frame_related_insn = st.next_addr;
1745
1746 if (m32c_srcdest_store (&st, dest, src_value, size))
1747 break;
1748 }
1749 /* (2) LDC #IMM24, sp */
1750 else if (st.insn[i] == 0xd5
1751 && st.insn[i + 1] == 0x29)
1752 {
1753 st.next_addr += 2;
1754 st.sp = pv_constant (m32c_udisp24 (&st));
1755 }
1756 else
1757 /* We've hit some instruction we don't know how to simulate.
1758 Strictly speaking, we should set every value we're
1759 tracking to "unknown". But we'll be optimistic, assume
1760 that we have enough information already, and stop
1761 analysis here. */
1762 break;
1763 }
1764
1765 /* If this instruction changed the FB or decreased the SP (i.e.,
1766 allocated more stack space), then this may be a good place to
1767 declare the prologue finished. However, there are some
1768 exceptions:
1769
1770 - If the instruction just changed the FB back to its original
1771 value, then that's probably a restore instruction. The
1772 prologue should definitely end before that.
1773
1774 - If the instruction increased the value of the SP (that is,
1775 shrunk the frame), then it's probably part of a frame
1776 teardown sequence, and the prologue should end before
1777 that. */
1778
1779 if (! pv_is_identical (st.fb, pre_insn_fb))
1780 {
1781 if (! pv_is_register_k (st.fb, tdep->fb->num, 0))
1782 after_last_frame_related_insn = st.next_addr;
1783 }
1784 else if (! pv_is_identical (st.sp, pre_insn_sp))
1785 {
1786 /* The comparison of the constants looks odd, there, because
1787 .k is unsigned. All it really means is that the SP is
1788 lower than it was before the instruction. */
1789 if ( pv_is_register (pre_insn_sp, tdep->sp->num)
1790 && pv_is_register (st.sp, tdep->sp->num)
1791 && ((pre_insn_sp.k - st.sp.k) < (st.sp.k - pre_insn_sp.k)))
1792 after_last_frame_related_insn = st.next_addr;
1793 }
1794
1795 st.scan_pc = st.next_addr;
1796 }
1797
1798 /* Did we load a constant value into the stack pointer? */
1799 if (pv_is_constant (st.sp))
1800 prologue->kind = prologue_first_frame;
1801
1802 /* Alternatively, did we initialize the frame pointer? Remember
1803 that the CFA is the address after the return address. */
1804 if (pv_is_register (st.fb, tdep->sp->num))
1805 {
1806 prologue->kind = prologue_with_frame_ptr;
1807 prologue->frame_ptr_offset = st.fb.k;
1808 }
1809
1810 /* Is the frame size a known constant? Remember that frame_size is
1811 actually the offset from the CFA to the SP (i.e., a negative
1812 value). */
1813 else if (pv_is_register (st.sp, tdep->sp->num))
1814 {
1815 prologue->kind = prologue_sans_frame_ptr;
1816 prologue->frame_size = st.sp.k;
1817 }
1818
1819 /* We haven't been able to make sense of this function's frame. Treat
1820 it as the first frame. */
1821 else
1822 prologue->kind = prologue_first_frame;
1823
1824 /* Record where all the registers were saved. */
1825 st.stack->scan (check_for_saved, (void *) prologue);
1826
1827 prologue->prologue_end = after_last_frame_related_insn;
1828 }
1829
1830
1831 static CORE_ADDR
1832 m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
1833 {
1834 const char *name;
1835 CORE_ADDR func_addr, func_end, sal_end;
1836 struct m32c_prologue p;
1837
1838 /* Try to find the extent of the function that contains IP. */
1839 if (! find_pc_partial_function (ip, &name, &func_addr, &func_end))
1840 return ip;
1841
1842 /* Find end by prologue analysis. */
1843 m32c_analyze_prologue (gdbarch, ip, func_end, &p);
1844 /* Find end by line info. */
1845 sal_end = skip_prologue_using_sal (gdbarch, ip);
1846 /* Return whichever is lower. */
1847 if (sal_end != 0 && sal_end != ip && sal_end < p.prologue_end)
1848 return sal_end;
1849 else
1850 return p.prologue_end;
1851 }
1852
1853
1854 \f
1855 /* Stack unwinding. */
1856
1857 static struct m32c_prologue *
1858 m32c_analyze_frame_prologue (struct frame_info *this_frame,
1859 void **this_prologue_cache)
1860 {
1861 if (! *this_prologue_cache)
1862 {
1863 CORE_ADDR func_start = get_frame_func (this_frame);
1864 CORE_ADDR stop_addr = get_frame_pc (this_frame);
1865
1866 /* If we couldn't find any function containing the PC, then
1867 just initialize the prologue cache, but don't do anything. */
1868 if (! func_start)
1869 stop_addr = func_start;
1870
1871 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct m32c_prologue);
1872 m32c_analyze_prologue (get_frame_arch (this_frame),
1873 func_start, stop_addr,
1874 (struct m32c_prologue *) *this_prologue_cache);
1875 }
1876
1877 return (struct m32c_prologue *) *this_prologue_cache;
1878 }
1879
1880
1881 static CORE_ADDR
1882 m32c_frame_base (struct frame_info *this_frame,
1883 void **this_prologue_cache)
1884 {
1885 struct m32c_prologue *p
1886 = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
1887 gdbarch *arch = get_frame_arch (this_frame);
1888 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1889
1890 /* In functions that use alloca, the distance between the stack
1891 pointer and the frame base varies dynamically, so we can't use
1892 the SP plus static information like prologue analysis to find the
1893 frame base. However, such functions must have a frame pointer,
1894 to be able to restore the SP on exit. So whenever we do have a
1895 frame pointer, use that to find the base. */
1896 switch (p->kind)
1897 {
1898 case prologue_with_frame_ptr:
1899 {
1900 CORE_ADDR fb
1901 = get_frame_register_unsigned (this_frame, tdep->fb->num);
1902 return fb - p->frame_ptr_offset;
1903 }
1904
1905 case prologue_sans_frame_ptr:
1906 {
1907 CORE_ADDR sp
1908 = get_frame_register_unsigned (this_frame, tdep->sp->num);
1909 return sp - p->frame_size;
1910 }
1911
1912 case prologue_first_frame:
1913 return 0;
1914
1915 default:
1916 gdb_assert_not_reached ("unexpected prologue kind");
1917 }
1918 }
1919
1920
1921 static void
1922 m32c_this_id (struct frame_info *this_frame,
1923 void **this_prologue_cache,
1924 struct frame_id *this_id)
1925 {
1926 CORE_ADDR base = m32c_frame_base (this_frame, this_prologue_cache);
1927
1928 if (base)
1929 *this_id = frame_id_build (base, get_frame_func (this_frame));
1930 /* Otherwise, leave it unset, and that will terminate the backtrace. */
1931 }
1932
1933
1934 static struct value *
1935 m32c_prev_register (struct frame_info *this_frame,
1936 void **this_prologue_cache, int regnum)
1937 {
1938 gdbarch *arch = get_frame_arch (this_frame);
1939 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
1940 struct m32c_prologue *p
1941 = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
1942 CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
1943
1944 if (regnum == tdep->sp->num)
1945 return frame_unwind_got_constant (this_frame, regnum, frame_base);
1946
1947 /* If prologue analysis says we saved this register somewhere,
1948 return a description of the stack slot holding it. */
1949 if (p->reg_offset[regnum] != 1)
1950 return frame_unwind_got_memory (this_frame, regnum,
1951 frame_base + p->reg_offset[regnum]);
1952
1953 /* Otherwise, presume we haven't changed the value of this
1954 register, and get it from the next frame. */
1955 return frame_unwind_got_register (this_frame, regnum, regnum);
1956 }
1957
1958
1959 static const struct frame_unwind m32c_unwind = {
1960 "m32c prologue",
1961 NORMAL_FRAME,
1962 default_frame_unwind_stop_reason,
1963 m32c_this_id,
1964 m32c_prev_register,
1965 NULL,
1966 default_frame_sniffer
1967 };
1968
1969 \f
1970 /* Inferior calls. */
1971
1972 /* The calling conventions, according to GCC:
1973
1974 r8c, m16c
1975 ---------
1976 First arg may be passed in r1l or r1 if it (1) fits (QImode or
1977 HImode), (2) is named, and (3) is an integer or pointer type (no
1978 structs, floats, etc). Otherwise, it's passed on the stack.
1979
1980 Second arg may be passed in r2, same restrictions (but not QImode),
1981 even if the first arg is passed on the stack.
1982
1983 Third and further args are passed on the stack. No padding is
1984 used, stack "alignment" is 8 bits.
1985
1986 m32cm, m32c
1987 -----------
1988
1989 First arg may be passed in r0l or r0, same restrictions as above.
1990
1991 Second and further args are passed on the stack. Padding is used
1992 after QImode parameters (i.e. lower-addressed byte is the value,
1993 higher-addressed byte is the padding), stack "alignment" is 16
1994 bits. */
1995
1996
1997 /* Return true if TYPE is a type that can be passed in registers. (We
1998 ignore the size, and pay attention only to the type code;
1999 acceptable sizes depends on which register is being considered to
2000 hold it.) */
2001 static int
2002 m32c_reg_arg_type (struct type *type)
2003 {
2004 enum type_code code = type->code ();
2005
2006 return (code == TYPE_CODE_INT
2007 || code == TYPE_CODE_ENUM
2008 || code == TYPE_CODE_PTR
2009 || TYPE_IS_REFERENCE (type)
2010 || code == TYPE_CODE_BOOL
2011 || code == TYPE_CODE_CHAR);
2012 }
2013
2014
2015 static CORE_ADDR
2016 m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2017 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
2018 struct value **args, CORE_ADDR sp,
2019 function_call_return_method return_method,
2020 CORE_ADDR struct_addr)
2021 {
2022 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2023 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2024 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
2025 CORE_ADDR cfa;
2026 int i;
2027
2028 /* The number of arguments given in this function's prototype, or
2029 zero if it has a non-prototyped function type. The m32c ABI
2030 passes arguments mentioned in the prototype differently from
2031 those in the ellipsis of a varargs function, or from those passed
2032 to a non-prototyped function. */
2033 int num_prototyped_args = 0;
2034
2035 {
2036 struct type *func_type = value_type (function);
2037
2038 /* Dereference function pointer types. */
2039 if (func_type->code () == TYPE_CODE_PTR)
2040 func_type = TYPE_TARGET_TYPE (func_type);
2041
2042 gdb_assert (func_type->code () == TYPE_CODE_FUNC ||
2043 func_type->code () == TYPE_CODE_METHOD);
2044
2045 #if 0
2046 /* The ABI description in gcc/config/m32c/m32c.abi says that
2047 we need to handle prototyped and non-prototyped functions
2048 separately, but the code in GCC doesn't actually do so. */
2049 if (TYPE_PROTOTYPED (func_type))
2050 #endif
2051 num_prototyped_args = func_type->num_fields ();
2052 }
2053
2054 /* First, if the function returns an aggregate by value, push a
2055 pointer to a buffer for it. This doesn't affect the way
2056 subsequent arguments are allocated to registers. */
2057 if (return_method == return_method_struct)
2058 {
2059 int ptr_len = TYPE_LENGTH (tdep->ptr_voyd);
2060 sp -= ptr_len;
2061 write_memory_unsigned_integer (sp, ptr_len, byte_order, struct_addr);
2062 }
2063
2064 /* Push the arguments. */
2065 for (i = nargs - 1; i >= 0; i--)
2066 {
2067 struct value *arg = args[i];
2068 const gdb_byte *arg_bits = value_contents (arg).data ();
2069 struct type *arg_type = value_type (arg);
2070 ULONGEST arg_size = TYPE_LENGTH (arg_type);
2071
2072 /* Can it go in r1 or r1l (for m16c) or r0 or r0l (for m32c)? */
2073 if (i == 0
2074 && arg_size <= 2
2075 && i < num_prototyped_args
2076 && m32c_reg_arg_type (arg_type))
2077 {
2078 /* Extract and re-store as an integer as a terse way to make
2079 sure it ends up in the least significant end of r1. (GDB
2080 should avoid assuming endianness, even on uni-endian
2081 processors.) */
2082 ULONGEST u = extract_unsigned_integer (arg_bits, arg_size,
2083 byte_order);
2084 struct m32c_reg *reg = (mach == bfd_mach_m16c) ? tdep->r1 : tdep->r0;
2085 regcache_cooked_write_unsigned (regcache, reg->num, u);
2086 }
2087
2088 /* Can it go in r2? */
2089 else if (mach == bfd_mach_m16c
2090 && i == 1
2091 && arg_size == 2
2092 && i < num_prototyped_args
2093 && m32c_reg_arg_type (arg_type))
2094 regcache->cooked_write (tdep->r2->num, arg_bits);
2095
2096 /* Everything else goes on the stack. */
2097 else
2098 {
2099 sp -= arg_size;
2100
2101 /* Align the stack. */
2102 if (mach == bfd_mach_m32c)
2103 sp &= ~1;
2104
2105 write_memory (sp, arg_bits, arg_size);
2106 }
2107 }
2108
2109 /* This is the CFA we use to identify the dummy frame. */
2110 cfa = sp;
2111
2112 /* Push the return address. */
2113 sp -= tdep->ret_addr_bytes;
2114 write_memory_unsigned_integer (sp, tdep->ret_addr_bytes, byte_order,
2115 bp_addr);
2116
2117 /* Update the stack pointer. */
2118 regcache_cooked_write_unsigned (regcache, tdep->sp->num, sp);
2119
2120 /* We need to borrow an odd trick from the i386 target here.
2121
2122 The value we return from this function gets used as the stack
2123 address (the CFA) for the dummy frame's ID. The obvious thing is
2124 to return the new TOS. However, that points at the return
2125 address, saved on the stack, which is inconsistent with the CFA's
2126 described by GCC's DWARF 2 .debug_frame information: DWARF 2
2127 .debug_frame info uses the address immediately after the saved
2128 return address. So you end up with a dummy frame whose CFA
2129 points at the return address, but the frame for the function
2130 being called has a CFA pointing after the return address: the
2131 younger CFA is *greater than* the older CFA. The sanity checks
2132 in frame.c don't like that.
2133
2134 So we try to be consistent with the CFA's used by DWARF 2.
2135 Having a dummy frame and a real frame with the *same* CFA is
2136 tolerable. */
2137 return cfa;
2138 }
2139
2140
2141 \f
2142 /* Return values. */
2143
2144 /* Return value conventions, according to GCC:
2145
2146 r8c, m16c
2147 ---------
2148
2149 QImode in r0l
2150 HImode in r0
2151 SImode in r2r0
2152 near pointer in r0
2153 far pointer in r2r0
2154
2155 Aggregate values (regardless of size) are returned by pushing a
2156 pointer to a temporary area on the stack after the args are pushed.
2157 The function fills in this area with the value. Note that this
2158 pointer on the stack does not affect how register arguments, if any,
2159 are configured.
2160
2161 m32cm, m32c
2162 -----------
2163 Same. */
2164
2165 /* Return non-zero if values of type TYPE are returned by storing them
2166 in a buffer whose address is passed on the stack, ahead of the
2167 other arguments. */
2168 static int
2169 m32c_return_by_passed_buf (struct type *type)
2170 {
2171 enum type_code code = type->code ();
2172
2173 return (code == TYPE_CODE_STRUCT
2174 || code == TYPE_CODE_UNION);
2175 }
2176
2177 static enum return_value_convention
2178 m32c_return_value (struct gdbarch *gdbarch,
2179 struct value *function,
2180 struct type *valtype,
2181 struct regcache *regcache,
2182 gdb_byte *readbuf,
2183 const gdb_byte *writebuf)
2184 {
2185 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2186 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2187 enum return_value_convention conv;
2188 ULONGEST valtype_len = TYPE_LENGTH (valtype);
2189
2190 if (m32c_return_by_passed_buf (valtype))
2191 conv = RETURN_VALUE_STRUCT_CONVENTION;
2192 else
2193 conv = RETURN_VALUE_REGISTER_CONVENTION;
2194
2195 if (readbuf)
2196 {
2197 /* We should never be called to find values being returned by
2198 RETURN_VALUE_STRUCT_CONVENTION. Those can't be located,
2199 unless we made the call ourselves. */
2200 gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
2201
2202 gdb_assert (valtype_len <= 8);
2203
2204 /* Anything that fits in r0 is returned there. */
2205 if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
2206 {
2207 ULONGEST u;
2208 regcache_cooked_read_unsigned (regcache, tdep->r0->num, &u);
2209 store_unsigned_integer (readbuf, valtype_len, byte_order, u);
2210 }
2211 else
2212 {
2213 /* Everything else is passed in mem0, using as many bytes as
2214 needed. This is not what the Renesas tools do, but it's
2215 what GCC does at the moment. */
2216 struct bound_minimal_symbol mem0
2217 = lookup_minimal_symbol ("mem0", NULL, NULL);
2218
2219 if (! mem0.minsym)
2220 error (_("The return value is stored in memory at 'mem0', "
2221 "but GDB cannot find\n"
2222 "its address."));
2223 read_memory (BMSYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
2224 }
2225 }
2226
2227 if (writebuf)
2228 {
2229 /* We should never be called to store values to be returned
2230 using RETURN_VALUE_STRUCT_CONVENTION. We have no way of
2231 finding the buffer, unless we made the call ourselves. */
2232 gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
2233
2234 gdb_assert (valtype_len <= 8);
2235
2236 /* Anything that fits in r0 is returned there. */
2237 if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
2238 {
2239 ULONGEST u = extract_unsigned_integer (writebuf, valtype_len,
2240 byte_order);
2241 regcache_cooked_write_unsigned (regcache, tdep->r0->num, u);
2242 }
2243 else
2244 {
2245 /* Everything else is passed in mem0, using as many bytes as
2246 needed. This is not what the Renesas tools do, but it's
2247 what GCC does at the moment. */
2248 struct bound_minimal_symbol mem0
2249 = lookup_minimal_symbol ("mem0", NULL, NULL);
2250
2251 if (! mem0.minsym)
2252 error (_("The return value is stored in memory at 'mem0', "
2253 "but GDB cannot find\n"
2254 " its address."));
2255 write_memory (BMSYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
2256 }
2257 }
2258
2259 return conv;
2260 }
2261
2262
2263 \f
2264 /* Trampolines. */
2265
2266 /* The m16c and m32c use a trampoline function for indirect function
2267 calls. An indirect call looks like this:
2268
2269 ... push arguments ...
2270 ... push target function address ...
2271 jsr.a m32c_jsri16
2272
2273 The code for m32c_jsri16 looks like this:
2274
2275 m32c_jsri16:
2276
2277 # Save return address.
2278 pop.w m32c_jsri_ret
2279 pop.b m32c_jsri_ret+2
2280
2281 # Store target function address.
2282 pop.w m32c_jsri_addr
2283
2284 # Re-push return address.
2285 push.b m32c_jsri_ret+2
2286 push.w m32c_jsri_ret
2287
2288 # Call the target function.
2289 jmpi.a m32c_jsri_addr
2290
2291 Without further information, GDB will treat calls to m32c_jsri16
2292 like calls to any other function. Since m32c_jsri16 doesn't have
2293 debugging information, that normally means that GDB sets a step-
2294 resume breakpoint and lets the program continue --- which is not
2295 what the user wanted. (Giving the trampoline debugging info
2296 doesn't help: the user expects the program to stop in the function
2297 their program is calling, not in some trampoline code they've never
2298 seen before.)
2299
2300 The gdbarch_skip_trampoline_code method tells GDB how to step
2301 through such trampoline functions transparently to the user. When
2302 given the address of a trampoline function's first instruction,
2303 gdbarch_skip_trampoline_code should return the address of the first
2304 instruction of the function really being called. If GDB decides it
2305 wants to step into that function, it will set a breakpoint there
2306 and silently continue to it.
2307
2308 We recognize the trampoline by name, and extract the target address
2309 directly from the stack. This isn't great, but recognizing by its
2310 code sequence seems more fragile. */
2311
2312 static CORE_ADDR
2313 m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
2314 {
2315 struct gdbarch *gdbarch = get_frame_arch (frame);
2316 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2317 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2318
2319 /* It would be nicer to simply look up the addresses of known
2320 trampolines once, and then compare stop_pc with them. However,
2321 we'd need to ensure that that cached address got invalidated when
2322 someone loaded a new executable, and I'm not quite sure of the
2323 best way to do that. find_pc_partial_function does do some
2324 caching, so we'll see how this goes. */
2325 const char *name;
2326 CORE_ADDR start, end;
2327
2328 if (find_pc_partial_function (stop_pc, &name, &start, &end))
2329 {
2330 /* Are we stopped at the beginning of the trampoline function? */
2331 if (strcmp (name, "m32c_jsri16") == 0
2332 && stop_pc == start)
2333 {
2334 /* Get the stack pointer. The return address is at the top,
2335 and the target function's address is just below that. We
2336 know it's a two-byte address, since the trampoline is
2337 m32c_jsri*16*. */
2338 CORE_ADDR sp = get_frame_sp (get_current_frame ());
2339 CORE_ADDR target
2340 = read_memory_unsigned_integer (sp + tdep->ret_addr_bytes,
2341 2, byte_order);
2342
2343 /* What we have now is the address of a jump instruction.
2344 What we need is the destination of that jump.
2345 The opcode is 1 byte, and the destination is the next 3 bytes. */
2346
2347 target = read_memory_unsigned_integer (target + 1, 3, byte_order);
2348 return target;
2349 }
2350 }
2351
2352 return 0;
2353 }
2354
2355
2356 /* Address/pointer conversions. */
2357
2358 /* On the m16c, there is a 24-bit address space, but only a very few
2359 instructions can generate addresses larger than 0xffff: jumps,
2360 jumps to subroutines, and the lde/std (load/store extended)
2361 instructions.
2362
2363 Since GCC can only support one size of pointer, we can't have
2364 distinct 'near' and 'far' pointer types; we have to pick one size
2365 for everything. If we wanted to use 24-bit pointers, then GCC
2366 would have to use lde and ste for all memory references, which
2367 would be terrible for performance and code size. So the GNU
2368 toolchain uses 16-bit pointers for everything, and gives up the
2369 ability to have pointers point outside the first 64k of memory.
2370
2371 However, as a special hack, we let the linker place functions at
2372 addresses above 0xffff, as long as it also places a trampoline in
2373 the low 64k for every function whose address is taken. Each
2374 trampoline consists of a single jmp.a instruction that jumps to the
2375 function's real entry point. Pointers to functions can be 16 bits
2376 long, even though the functions themselves are at higher addresses:
2377 the pointers refer to the trampolines, not the functions.
2378
2379 This complicates things for GDB, however: given the address of a
2380 function (from debug info or linker symbols, say) which could be
2381 anywhere in the 24-bit address space, how can we find an
2382 appropriate 16-bit value to use as a pointer to it?
2383
2384 If the linker has not generated a trampoline for the function,
2385 we're out of luck. Well, I guess we could malloc some space and
2386 write a jmp.a instruction to it, but I'm not going to get into that
2387 at the moment.
2388
2389 If the linker has generated a trampoline for the function, then it
2390 also emitted a symbol for the trampoline: if the function's linker
2391 symbol is named NAME, then the function's trampoline's linker
2392 symbol is named NAME.plt.
2393
2394 So, given a code address:
2395 - We try to find a linker symbol at that address.
2396 - If we find such a symbol named NAME, we look for a linker symbol
2397 named NAME.plt.
2398 - If we find such a symbol, we assume it is a trampoline, and use
2399 its address as the pointer value.
2400
2401 And, given a function pointer:
2402 - We try to find a linker symbol at that address named NAME.plt.
2403 - If we find such a symbol, we look for a linker symbol named NAME.
2404 - If we find that, we provide that as the function's address.
2405 - If any of the above steps fail, we return the original address
2406 unchanged; it might really be a function in the low 64k.
2407
2408 See? You *knew* there was a reason you wanted to be a computer
2409 programmer! :) */
2410
2411 static void
2412 m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
2413 struct type *type, gdb_byte *buf, CORE_ADDR addr)
2414 {
2415 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2416 enum type_code target_code;
2417 gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
2418
2419 target_code = TYPE_TARGET_TYPE (type)->code ();
2420
2421 if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
2422 {
2423 const char *func_name;
2424 char *tramp_name;
2425 struct bound_minimal_symbol tramp_msym;
2426
2427 /* Try to find a linker symbol at this address. */
2428 struct bound_minimal_symbol func_msym
2429 = lookup_minimal_symbol_by_pc (addr);
2430
2431 if (! func_msym.minsym)
2432 error (_("Cannot convert code address %s to function pointer:\n"
2433 "couldn't find a symbol at that address, to find trampoline."),
2434 paddress (gdbarch, addr));
2435
2436 func_name = func_msym.minsym->linkage_name ();
2437 tramp_name = (char *) xmalloc (strlen (func_name) + 5);
2438 strcpy (tramp_name, func_name);
2439 strcat (tramp_name, ".plt");
2440
2441 /* Try to find a linker symbol for the trampoline. */
2442 tramp_msym = lookup_minimal_symbol (tramp_name, NULL, NULL);
2443
2444 /* We've either got another copy of the name now, or don't need
2445 the name any more. */
2446 xfree (tramp_name);
2447
2448 if (! tramp_msym.minsym)
2449 {
2450 CORE_ADDR ptrval;
2451
2452 /* No PLT entry found. Mask off the upper bits of the address
2453 to make a pointer. As noted in the warning to the user
2454 below, this value might be useful if converted back into
2455 an address by GDB, but will otherwise, almost certainly,
2456 be garbage.
2457
2458 Using this masked result does seem to be useful
2459 in gdb.cp/cplusfuncs.exp in which ~40 FAILs turn into
2460 PASSes. These results appear to be correct as well.
2461
2462 We print a warning here so that the user can make a
2463 determination about whether the result is useful or not. */
2464 ptrval = addr & 0xffff;
2465
2466 warning (_("Cannot convert code address %s to function pointer:\n"
2467 "couldn't find trampoline named '%s.plt'.\n"
2468 "Returning pointer value %s instead; this may produce\n"
2469 "a useful result if converted back into an address by GDB,\n"
2470 "but will most likely not be useful otherwise."),
2471 paddress (gdbarch, addr), func_name,
2472 paddress (gdbarch, ptrval));
2473
2474 addr = ptrval;
2475
2476 }
2477 else
2478 {
2479 /* The trampoline's address is our pointer. */
2480 addr = BMSYMBOL_VALUE_ADDRESS (tramp_msym);
2481 }
2482 }
2483
2484 store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
2485 }
2486
2487
2488 static CORE_ADDR
2489 m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
2490 struct type *type, const gdb_byte *buf)
2491 {
2492 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2493 CORE_ADDR ptr;
2494 enum type_code target_code;
2495
2496 gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
2497
2498 ptr = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
2499
2500 target_code = TYPE_TARGET_TYPE (type)->code ();
2501
2502 if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
2503 {
2504 /* See if there is a minimal symbol at that address whose name is
2505 "NAME.plt". */
2506 struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);
2507
2508 if (ptr_msym.minsym)
2509 {
2510 const char *ptr_msym_name = ptr_msym.minsym->linkage_name ();
2511 int len = strlen (ptr_msym_name);
2512
2513 if (len > 4
2514 && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
2515 {
2516 struct bound_minimal_symbol func_msym;
2517 /* We have a .plt symbol; try to find the symbol for the
2518 corresponding function.
2519
2520 Since the trampoline contains a jump instruction, we
2521 could also just extract the jump's target address. I
2522 don't see much advantage one way or the other. */
2523 char *func_name = (char *) xmalloc (len - 4 + 1);
2524 memcpy (func_name, ptr_msym_name, len - 4);
2525 func_name[len - 4] = '\0';
2526 func_msym
2527 = lookup_minimal_symbol (func_name, NULL, NULL);
2528
2529 /* If we do have such a symbol, return its value as the
2530 function's true address. */
2531 if (func_msym.minsym)
2532 ptr = BMSYMBOL_VALUE_ADDRESS (func_msym);
2533 }
2534 }
2535 else
2536 {
2537 int aspace;
2538
2539 for (aspace = 1; aspace <= 15; aspace++)
2540 {
2541 ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);
2542
2543 if (ptr_msym.minsym)
2544 ptr |= aspace << 16;
2545 }
2546 }
2547 }
2548
2549 return ptr;
2550 }
2551
2552 static void
2553 m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
2554 int *frame_regnum,
2555 LONGEST *frame_offset)
2556 {
2557 const char *name;
2558 CORE_ADDR func_addr, func_end;
2559 struct m32c_prologue p;
2560
2561 struct regcache *regcache = get_current_regcache ();
2562 m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2563
2564 if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
2565 internal_error (__FILE__, __LINE__,
2566 _("No virtual frame pointer available"));
2567
2568 m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
2569 switch (p.kind)
2570 {
2571 case prologue_with_frame_ptr:
2572 *frame_regnum = m32c_banked_register (tdep->fb, regcache)->num;
2573 *frame_offset = p.frame_ptr_offset;
2574 break;
2575 case prologue_sans_frame_ptr:
2576 *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
2577 *frame_offset = p.frame_size;
2578 break;
2579 default:
2580 *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
2581 *frame_offset = 0;
2582 break;
2583 }
2584 /* Sanity check */
2585 if (*frame_regnum > gdbarch_num_regs (gdbarch))
2586 internal_error (__FILE__, __LINE__,
2587 _("No virtual frame pointer available"));
2588 }
2589
2590 \f
2591 /* Initialization. */
2592
2593 static struct gdbarch *
2594 m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2595 {
2596 struct gdbarch *gdbarch;
2597 unsigned long mach = info.bfd_arch_info->mach;
2598
2599 /* Find a candidate among the list of architectures we've created
2600 already. */
2601 for (arches = gdbarch_list_lookup_by_info (arches, &info);
2602 arches != NULL;
2603 arches = gdbarch_list_lookup_by_info (arches->next, &info))
2604 return arches->gdbarch;
2605
2606 m32c_gdbarch_tdep *tdep = new m32c_gdbarch_tdep;
2607 gdbarch = gdbarch_alloc (&info, tdep);
2608
2609 /* Essential types. */
2610 make_types (gdbarch);
2611
2612 /* Address/pointer conversions. */
2613 if (mach == bfd_mach_m16c)
2614 {
2615 set_gdbarch_address_to_pointer (gdbarch, m32c_m16c_address_to_pointer);
2616 set_gdbarch_pointer_to_address (gdbarch, m32c_m16c_pointer_to_address);
2617 }
2618
2619 /* Register set. */
2620 make_regs (gdbarch);
2621
2622 /* Breakpoints. */
2623 set_gdbarch_breakpoint_kind_from_pc (gdbarch, m32c_breakpoint::kind_from_pc);
2624 set_gdbarch_sw_breakpoint_from_kind (gdbarch, m32c_breakpoint::bp_from_kind);
2625
2626 /* Prologue analysis and unwinding. */
2627 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2628 set_gdbarch_skip_prologue (gdbarch, m32c_skip_prologue);
2629 #if 0
2630 /* I'm dropping the dwarf2 sniffer because it has a few problems.
2631 They may be in the dwarf2 cfi code in GDB, or they may be in
2632 the debug info emitted by the upstream toolchain. I don't
2633 know which, but I do know that the prologue analyzer works better.
2634 MVS 04/13/06 */
2635 dwarf2_append_sniffers (gdbarch);
2636 #endif
2637 frame_unwind_append_unwinder (gdbarch, &m32c_unwind);
2638
2639 /* Inferior calls. */
2640 set_gdbarch_push_dummy_call (gdbarch, m32c_push_dummy_call);
2641 set_gdbarch_return_value (gdbarch, m32c_return_value);
2642
2643 /* Trampolines. */
2644 set_gdbarch_skip_trampoline_code (gdbarch, m32c_skip_trampoline_code);
2645
2646 set_gdbarch_virtual_frame_pointer (gdbarch, m32c_virtual_frame_pointer);
2647
2648 /* m32c function boundary addresses are not necessarily even.
2649 Therefore, the `vbit', which indicates a pointer to a virtual
2650 member function, is stored in the delta field, rather than as
2651 the low bit of a function pointer address.
2652
2653 In order to verify this, see the definition of
2654 TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
2655 definition of FUNCTION_BOUNDARY in gcc/config/m32c/m32c.h. */
2656 set_gdbarch_vbit_in_delta (gdbarch, 1);
2657
2658 return gdbarch;
2659 }
2660
2661 void _initialize_m32c_tdep ();
2662 void
2663 _initialize_m32c_tdep ()
2664 {
2665 register_gdbarch_init (bfd_arch_m32c, m32c_gdbarch_init);
2666
2667 m32c_dma_reggroup = reggroup_new ("dma", USER_REGGROUP);
2668 }