Commit | Line | Data |
---|---|---|
49d45b20 JB |
1 | /* Target-dependent code for FT32. |
2 | ||
3 | Copyright (C) 2009-2015 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 "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" | |
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" | |
38 | #include "record.h" | |
39 | ||
49d45b20 JB |
40 | #include "ft32-tdep.h" |
41 | #include "gdb/sim-ft32.h" | |
42 | ||
43 | #define RAM_BIAS 0x800000 /* Bias added to RAM addresses. */ | |
44 | ||
45 | /* Local functions. */ | |
46 | ||
47 | extern void _initialize_ft32_tdep (void); | |
48 | ||
49 | /* Use an invalid address -1 as 'not available' marker. */ | |
50 | enum { REG_UNAVAIL = (CORE_ADDR) (-1) }; | |
51 | ||
52 | struct ft32_frame_cache | |
53 | { | |
54 | /* Base address of the frame */ | |
55 | CORE_ADDR base; | |
56 | /* Function this frame belongs to */ | |
57 | CORE_ADDR pc; | |
58 | /* Total size of this frame */ | |
59 | LONGEST framesize; | |
60 | /* Saved registers in this frame */ | |
61 | CORE_ADDR saved_regs[FT32_NUM_REGS]; | |
62 | /* Saved SP in this frame */ | |
63 | CORE_ADDR saved_sp; | |
64 | /* Has the new frame been LINKed. */ | |
65 | bfd_boolean established; | |
66 | }; | |
67 | ||
68 | /* Implement the "frame_align" gdbarch method. */ | |
69 | ||
70 | static CORE_ADDR | |
71 | ft32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp) | |
72 | { | |
73 | /* Align to the size of an instruction (so that they can safely be | |
74 | pushed onto the stack. */ | |
75 | return sp & ~1; | |
76 | } | |
77 | ||
78 | /* Implement the "breakpoint_from_pc" gdbarch method. */ | |
79 | ||
80 | static const unsigned char * | |
81 | ft32_breakpoint_from_pc (struct gdbarch *gdbarch, | |
82 | CORE_ADDR *pcptr, int *lenptr) | |
83 | { | |
84 | static const gdb_byte breakpoint[] = { 0x02, 0x00, 0x34, 0x00 }; | |
85 | ||
86 | *lenptr = sizeof (breakpoint); | |
87 | return breakpoint; | |
88 | } | |
89 | ||
90 | /* FT32 register names. */ | |
91 | ||
92 | static const char *const ft32_register_names[] = | |
93 | { | |
94 | "fp", "sp", | |
95 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
96 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", | |
97 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", | |
98 | "r24", "r25", "r26", "r27", "r28", "cc", | |
99 | "pc" | |
100 | }; | |
101 | ||
102 | /* Implement the "register_name" gdbarch method. */ | |
103 | ||
104 | static const char * | |
105 | ft32_register_name (struct gdbarch *gdbarch, int reg_nr) | |
106 | { | |
107 | if (reg_nr < 0) | |
108 | return NULL; | |
109 | if (reg_nr >= FT32_NUM_REGS) | |
110 | return NULL; | |
111 | return ft32_register_names[reg_nr]; | |
112 | } | |
113 | ||
114 | /* Implement the "register_type" gdbarch method. */ | |
115 | ||
116 | static struct type * | |
117 | ft32_register_type (struct gdbarch *gdbarch, int reg_nr) | |
118 | { | |
119 | if (reg_nr == FT32_PC_REGNUM) | |
120 | return builtin_type (gdbarch)->builtin_func_ptr; | |
121 | else if (reg_nr == FT32_SP_REGNUM || reg_nr == FT32_FP_REGNUM) | |
122 | return builtin_type (gdbarch)->builtin_data_ptr; | |
123 | else | |
124 | return builtin_type (gdbarch)->builtin_int32; | |
125 | } | |
126 | ||
127 | /* Write into appropriate registers a function return value | |
128 | of type TYPE, given in virtual format. */ | |
129 | ||
130 | static void | |
131 | ft32_store_return_value (struct type *type, struct regcache *regcache, | |
132 | const gdb_byte *valbuf) | |
133 | { | |
134 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
135 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
136 | CORE_ADDR regval; | |
137 | int len = TYPE_LENGTH (type); | |
138 | ||
139 | /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */ | |
140 | regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order); | |
141 | regcache_cooked_write_unsigned (regcache, FT32_R0_REGNUM, regval); | |
142 | if (len > 4) | |
143 | { | |
144 | regval = extract_unsigned_integer (valbuf + 4, | |
145 | len - 4, byte_order); | |
146 | regcache_cooked_write_unsigned (regcache, FT32_R1_REGNUM, regval); | |
147 | } | |
148 | } | |
149 | ||
150 | /* Decode the instructions within the given address range. Decide | |
151 | when we must have reached the end of the function prologue. If a | |
152 | frame_info pointer is provided, fill in its saved_regs etc. | |
153 | ||
154 | Returns the address of the first instruction after the prologue. */ | |
155 | ||
156 | #define IS_PUSH(inst) (((inst) & 0xfff00000) == 0x84000000) | |
157 | #define PUSH_REG(inst) (FT32_R0_REGNUM + (((inst) >> 15) & 0x1f)) | |
158 | #define IS_LINK(inst) (((inst) & 0xffff0000) == 0x95d00000) | |
159 | #define LINK_SIZE(inst) ((inst) & 0xffff) | |
160 | ||
161 | static CORE_ADDR | |
162 | ft32_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr, | |
163 | struct ft32_frame_cache *cache, | |
164 | struct gdbarch *gdbarch) | |
165 | { | |
166 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
167 | CORE_ADDR next_addr; | |
168 | ULONGEST inst, inst2; | |
169 | LONGEST offset; | |
170 | int regnum; | |
171 | ||
172 | cache->saved_regs[FT32_PC_REGNUM] = 0; | |
173 | cache->framesize = 0; | |
174 | ||
175 | if (start_addr >= end_addr) | |
176 | return end_addr; | |
177 | ||
178 | cache->established = 0; | |
179 | for (next_addr = start_addr; next_addr < end_addr; ) | |
180 | { | |
181 | inst = read_memory_unsigned_integer (next_addr, 4, byte_order); | |
182 | ||
183 | if (IS_PUSH (inst)) | |
184 | { | |
185 | regnum = PUSH_REG (inst); | |
186 | cache->framesize += 4; | |
187 | cache->saved_regs[regnum] = cache->framesize; | |
188 | next_addr += 4; | |
189 | } | |
190 | else | |
191 | break; | |
192 | } | |
193 | for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++) | |
194 | { | |
195 | if (cache->saved_regs[regnum] != REG_UNAVAIL) | |
196 | cache->saved_regs[regnum] = cache->framesize - cache->saved_regs[regnum]; | |
197 | } | |
198 | cache->saved_regs[FT32_PC_REGNUM] = cache->framesize; | |
199 | ||
200 | /* It is a LINK? */ | |
201 | if (next_addr < end_addr) | |
202 | { | |
203 | inst = read_memory_unsigned_integer (next_addr, 4, byte_order); | |
204 | if (IS_LINK (inst)) | |
205 | { | |
206 | cache->established = 1; | |
207 | for (regnum = FT32_R0_REGNUM; regnum < FT32_PC_REGNUM; regnum++) | |
208 | { | |
209 | if (cache->saved_regs[regnum] != REG_UNAVAIL) | |
210 | cache->saved_regs[regnum] += 4; | |
211 | } | |
212 | cache->saved_regs[FT32_PC_REGNUM] = cache->framesize + 4; | |
213 | cache->saved_regs[FT32_FP_REGNUM] = 0; | |
214 | cache->framesize += LINK_SIZE (inst); | |
215 | next_addr += 4; | |
216 | } | |
217 | } | |
218 | ||
219 | return next_addr; | |
220 | } | |
221 | ||
222 | /* Find the end of function prologue. */ | |
223 | ||
224 | static CORE_ADDR | |
225 | ft32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) | |
226 | { | |
227 | CORE_ADDR func_addr = 0, func_end = 0; | |
228 | const char *func_name; | |
229 | ||
230 | /* See if we can determine the end of the prologue via the symbol table. | |
231 | If so, then return either PC, or the PC after the prologue, whichever | |
232 | is greater. */ | |
233 | if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end)) | |
234 | { | |
235 | CORE_ADDR post_prologue_pc | |
236 | = skip_prologue_using_sal (gdbarch, func_addr); | |
237 | if (post_prologue_pc != 0) | |
238 | return max (pc, post_prologue_pc); | |
239 | else | |
240 | { | |
241 | /* Can't determine prologue from the symbol table, need to examine | |
242 | instructions. */ | |
243 | struct symtab_and_line sal; | |
244 | struct symbol *sym; | |
245 | struct ft32_frame_cache cache; | |
246 | CORE_ADDR plg_end; | |
247 | ||
248 | memset (&cache, 0, sizeof cache); | |
249 | ||
250 | plg_end = ft32_analyze_prologue (func_addr, | |
251 | func_end, &cache, gdbarch); | |
252 | /* Found a function. */ | |
835a09d9 | 253 | sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol; |
49d45b20 JB |
254 | /* Don't use line number debug info for assembly source files. */ |
255 | if ((sym != NULL) && SYMBOL_LANGUAGE (sym) != language_asm) | |
256 | { | |
257 | sal = find_pc_line (func_addr, 0); | |
258 | if (sal.end && sal.end < func_end) | |
259 | { | |
260 | /* Found a line number, use it as end of prologue. */ | |
261 | return sal.end; | |
262 | } | |
263 | } | |
264 | /* No useable line symbol. Use result of prologue parsing method. */ | |
265 | return plg_end; | |
266 | } | |
267 | } | |
268 | ||
269 | /* No function symbol -- just return the PC. */ | |
270 | return pc; | |
271 | } | |
272 | ||
273 | /* Implement the "read_pc" gdbarch method. */ | |
274 | ||
275 | static CORE_ADDR | |
276 | ft32_read_pc (struct regcache *regcache) | |
277 | { | |
278 | ULONGEST pc; | |
279 | ||
280 | regcache_cooked_read_unsigned (regcache, FT32_PC_REGNUM, &pc); | |
281 | return pc; | |
282 | } | |
283 | ||
284 | /* Implement the "write_pc" gdbarch method. */ | |
285 | ||
286 | static void | |
287 | ft32_write_pc (struct regcache *regcache, CORE_ADDR val) | |
288 | { | |
289 | regcache_cooked_write_unsigned (regcache, FT32_PC_REGNUM, val); | |
290 | } | |
291 | ||
292 | /* Implement the "unwind_sp" gdbarch method. */ | |
293 | ||
294 | static CORE_ADDR | |
295 | ft32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
296 | { | |
297 | return frame_unwind_register_unsigned (next_frame, FT32_SP_REGNUM); | |
298 | } | |
299 | ||
300 | /* Given a return value in `regbuf' with a type `valtype', | |
301 | extract and copy its value into `valbuf'. */ | |
302 | ||
303 | static void | |
304 | ft32_extract_return_value (struct type *type, struct regcache *regcache, | |
305 | gdb_byte *dst) | |
306 | { | |
307 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
308 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
309 | bfd_byte *valbuf = dst; | |
310 | int len = TYPE_LENGTH (type); | |
311 | ULONGEST tmp; | |
312 | ||
313 | /* By using store_unsigned_integer we avoid having to do | |
314 | anything special for small big-endian values. */ | |
315 | regcache_cooked_read_unsigned (regcache, FT32_R0_REGNUM, &tmp); | |
316 | store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp); | |
317 | ||
318 | /* Ignore return values more than 8 bytes in size because the ft32 | |
319 | returns anything more than 8 bytes in the stack. */ | |
320 | if (len > 4) | |
321 | { | |
322 | regcache_cooked_read_unsigned (regcache, FT32_R1_REGNUM, &tmp); | |
323 | store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp); | |
324 | } | |
325 | } | |
326 | ||
327 | /* Implement the "return_value" gdbarch method. */ | |
328 | ||
329 | static enum return_value_convention | |
330 | ft32_return_value (struct gdbarch *gdbarch, struct value *function, | |
331 | struct type *valtype, struct regcache *regcache, | |
332 | gdb_byte *readbuf, const gdb_byte *writebuf) | |
333 | { | |
334 | if (TYPE_LENGTH (valtype) > 8) | |
335 | return RETURN_VALUE_STRUCT_CONVENTION; | |
336 | else | |
337 | { | |
338 | if (readbuf != NULL) | |
339 | ft32_extract_return_value (valtype, regcache, readbuf); | |
340 | if (writebuf != NULL) | |
341 | ft32_store_return_value (valtype, regcache, writebuf); | |
342 | return RETURN_VALUE_REGISTER_CONVENTION; | |
343 | } | |
344 | } | |
345 | ||
346 | /* Allocate and initialize a ft32_frame_cache object. */ | |
347 | ||
348 | static struct ft32_frame_cache * | |
349 | ft32_alloc_frame_cache (void) | |
350 | { | |
351 | struct ft32_frame_cache *cache; | |
352 | int i; | |
353 | ||
354 | cache = FRAME_OBSTACK_ZALLOC (struct ft32_frame_cache); | |
355 | ||
356 | for (i = 0; i < FT32_NUM_REGS; ++i) | |
357 | cache->saved_regs[i] = REG_UNAVAIL; | |
358 | ||
359 | return cache; | |
360 | } | |
361 | ||
362 | /* Populate a ft32_frame_cache object for this_frame. */ | |
363 | ||
364 | static struct ft32_frame_cache * | |
365 | ft32_frame_cache (struct frame_info *this_frame, void **this_cache) | |
366 | { | |
367 | struct ft32_frame_cache *cache; | |
368 | CORE_ADDR current_pc; | |
369 | int i; | |
370 | ||
371 | if (*this_cache) | |
372 | return *this_cache; | |
373 | ||
374 | cache = ft32_alloc_frame_cache (); | |
375 | *this_cache = cache; | |
376 | ||
377 | cache->base = get_frame_register_unsigned (this_frame, FT32_FP_REGNUM); | |
378 | if (cache->base == 0) | |
379 | return cache; | |
380 | ||
381 | cache->pc = get_frame_func (this_frame); | |
382 | current_pc = get_frame_pc (this_frame); | |
383 | if (cache->pc) | |
384 | { | |
385 | struct gdbarch *gdbarch = get_frame_arch (this_frame); | |
386 | ||
387 | ft32_analyze_prologue (cache->pc, current_pc, cache, gdbarch); | |
388 | if (!cache->established) | |
389 | cache->base = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM); | |
390 | } | |
391 | ||
392 | cache->saved_sp = cache->base - 4; | |
393 | ||
394 | for (i = 0; i < FT32_NUM_REGS; ++i) | |
395 | if (cache->saved_regs[i] != REG_UNAVAIL) | |
396 | cache->saved_regs[i] = cache->base + cache->saved_regs[i]; | |
397 | ||
398 | return cache; | |
399 | } | |
400 | ||
401 | /* Implement the "unwind_pc" gdbarch method. */ | |
402 | ||
403 | static CORE_ADDR | |
404 | ft32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
405 | { | |
406 | return frame_unwind_register_unsigned (next_frame, FT32_PC_REGNUM); | |
407 | } | |
408 | ||
409 | /* Given a GDB frame, determine the address of the calling function's | |
410 | frame. This will be used to create a new GDB frame struct. */ | |
411 | ||
412 | static void | |
413 | ft32_frame_this_id (struct frame_info *this_frame, | |
414 | void **this_prologue_cache, struct frame_id *this_id) | |
415 | { | |
416 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
417 | this_prologue_cache); | |
418 | ||
419 | /* This marks the outermost frame. */ | |
420 | if (cache->base == 0) | |
421 | return; | |
422 | ||
423 | *this_id = frame_id_build (cache->saved_sp, cache->pc); | |
424 | } | |
425 | ||
426 | /* Get the value of register regnum in the previous stack frame. */ | |
427 | ||
428 | static struct value * | |
429 | ft32_frame_prev_register (struct frame_info *this_frame, | |
430 | void **this_prologue_cache, int regnum) | |
431 | { | |
432 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
433 | this_prologue_cache); | |
434 | ||
435 | gdb_assert (regnum >= 0); | |
436 | ||
437 | if (regnum == FT32_SP_REGNUM && cache->saved_sp) | |
438 | return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp); | |
439 | ||
440 | if (regnum < FT32_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL) | |
441 | return frame_unwind_got_memory (this_frame, regnum, | |
442 | RAM_BIAS | cache->saved_regs[regnum]); | |
443 | ||
444 | return frame_unwind_got_register (this_frame, regnum, regnum); | |
445 | } | |
446 | ||
447 | static const struct frame_unwind ft32_frame_unwind = | |
448 | { | |
449 | NORMAL_FRAME, | |
450 | default_frame_unwind_stop_reason, | |
451 | ft32_frame_this_id, | |
452 | ft32_frame_prev_register, | |
453 | NULL, | |
454 | default_frame_sniffer | |
455 | }; | |
456 | ||
457 | /* Return the base address of this_frame. */ | |
458 | ||
459 | static CORE_ADDR | |
460 | ft32_frame_base_address (struct frame_info *this_frame, void **this_cache) | |
461 | { | |
462 | struct ft32_frame_cache *cache = ft32_frame_cache (this_frame, | |
463 | this_cache); | |
464 | ||
465 | return cache->base; | |
466 | } | |
467 | ||
468 | static const struct frame_base ft32_frame_base = | |
469 | { | |
470 | &ft32_frame_unwind, | |
471 | ft32_frame_base_address, | |
472 | ft32_frame_base_address, | |
473 | ft32_frame_base_address | |
474 | }; | |
475 | ||
476 | static struct frame_id | |
477 | ft32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame) | |
478 | { | |
479 | CORE_ADDR sp = get_frame_register_unsigned (this_frame, FT32_SP_REGNUM); | |
480 | ||
481 | return frame_id_build (sp, get_frame_pc (this_frame)); | |
482 | } | |
483 | ||
484 | /* Allocate and initialize the ft32 gdbarch object. */ | |
485 | ||
486 | static struct gdbarch * | |
487 | ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
488 | { | |
489 | struct gdbarch *gdbarch; | |
490 | struct gdbarch_tdep *tdep; | |
491 | ||
492 | /* If there is already a candidate, use it. */ | |
493 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
494 | if (arches != NULL) | |
495 | return arches->gdbarch; | |
496 | ||
497 | /* Allocate space for the new architecture. */ | |
498 | tdep = XNEW (struct gdbarch_tdep); | |
499 | gdbarch = gdbarch_alloc (&info, tdep); | |
500 | ||
501 | set_gdbarch_read_pc (gdbarch, ft32_read_pc); | |
502 | set_gdbarch_write_pc (gdbarch, ft32_write_pc); | |
503 | set_gdbarch_unwind_sp (gdbarch, ft32_unwind_sp); | |
504 | ||
505 | set_gdbarch_num_regs (gdbarch, FT32_NUM_REGS); | |
506 | set_gdbarch_sp_regnum (gdbarch, FT32_SP_REGNUM); | |
507 | set_gdbarch_pc_regnum (gdbarch, FT32_PC_REGNUM); | |
508 | set_gdbarch_register_name (gdbarch, ft32_register_name); | |
509 | set_gdbarch_register_type (gdbarch, ft32_register_type); | |
510 | ||
511 | set_gdbarch_return_value (gdbarch, ft32_return_value); | |
512 | ||
513 | set_gdbarch_skip_prologue (gdbarch, ft32_skip_prologue); | |
514 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
515 | set_gdbarch_breakpoint_from_pc (gdbarch, ft32_breakpoint_from_pc); | |
516 | set_gdbarch_frame_align (gdbarch, ft32_frame_align); | |
517 | ||
518 | frame_base_set_default (gdbarch, &ft32_frame_base); | |
519 | ||
520 | /* Methods for saving / extracting a dummy frame's ID. The ID's | |
521 | stack address must match the SP value returned by | |
522 | PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */ | |
523 | set_gdbarch_dummy_id (gdbarch, ft32_dummy_id); | |
524 | ||
525 | set_gdbarch_unwind_pc (gdbarch, ft32_unwind_pc); | |
526 | ||
527 | set_gdbarch_print_insn (gdbarch, print_insn_ft32); | |
528 | ||
529 | /* Hook in ABI-specific overrides, if they have been registered. */ | |
530 | gdbarch_init_osabi (info, gdbarch); | |
531 | ||
532 | /* Hook in the default unwinders. */ | |
533 | frame_unwind_append_unwinder (gdbarch, &ft32_frame_unwind); | |
534 | ||
535 | /* Support simple overlay manager. */ | |
536 | set_gdbarch_overlay_update (gdbarch, simple_overlay_update); | |
537 | ||
538 | return gdbarch; | |
539 | } | |
540 | ||
541 | /* Register this machine's init routine. */ | |
542 | ||
543 | void | |
544 | _initialize_ft32_tdep (void) | |
545 | { | |
546 | register_gdbarch_init (bfd_arch_ft32, ft32_gdbarch_init); | |
547 | } |