2012-05-18 Sergio Durigan Junior <sergiodj@redhat.com>
[deliverable/binutils-gdb.git] / gdb / lm32-tdep.c
CommitLineData
c28c63d8
JB
1/* Target-dependent code for Lattice Mico32 processor, for GDB.
2 Contributed by Jon Beniston <jon@beniston.com>
3
0b302171 4 Copyright (C) 2009-2012 Free Software Foundation, Inc.
c28c63d8
JB
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22#include "frame.h"
23#include "frame-unwind.h"
24#include "frame-base.h"
25#include "inferior.h"
26#include "dis-asm.h"
27#include "symfile.h"
28#include "remote.h"
29#include "gdbcore.h"
30#include "gdb/sim-lm32.h"
31#include "gdb/callback.h"
32#include "gdb/remote-sim.h"
33#include "sim-regno.h"
34#include "arch-utils.h"
35#include "regcache.h"
36#include "trad-frame.h"
37#include "reggroups.h"
38#include "opcodes/lm32-desc.h"
39
40#include "gdb_string.h"
41
42/* Macros to extract fields from an instruction. */
43#define LM32_OPCODE(insn) ((insn >> 26) & 0x3f)
44#define LM32_REG0(insn) ((insn >> 21) & 0x1f)
45#define LM32_REG1(insn) ((insn >> 16) & 0x1f)
46#define LM32_REG2(insn) ((insn >> 11) & 0x1f)
47#define LM32_IMM16(insn) ((((long)insn & 0xffff) << 16) >> 16)
48
49struct gdbarch_tdep
50{
1777feb0 51 /* gdbarch target dependent data here. Currently unused for LM32. */
c28c63d8
JB
52};
53
54struct lm32_frame_cache
55{
56 /* The frame's base. Used when constructing a frame ID. */
57 CORE_ADDR base;
58 CORE_ADDR pc;
59 /* Size of frame. */
60 int size;
61 /* Table indicating the location of each and every register. */
62 struct trad_frame_saved_reg *saved_regs;
63};
64
65/* Add the available register groups. */
66
67static void
68lm32_add_reggroups (struct gdbarch *gdbarch)
69{
70 reggroup_add (gdbarch, general_reggroup);
71 reggroup_add (gdbarch, all_reggroup);
72 reggroup_add (gdbarch, system_reggroup);
73}
74
75/* Return whether a given register is in a given group. */
76
77static int
78lm32_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
79 struct reggroup *group)
80{
81 if (group == general_reggroup)
82 return ((regnum >= SIM_LM32_R0_REGNUM) && (regnum <= SIM_LM32_RA_REGNUM))
83 || (regnum == SIM_LM32_PC_REGNUM);
84 else if (group == system_reggroup)
85 return ((regnum >= SIM_LM32_EA_REGNUM) && (regnum <= SIM_LM32_BA_REGNUM))
86 || ((regnum >= SIM_LM32_EID_REGNUM) && (regnum <= SIM_LM32_IP_REGNUM));
87 return default_register_reggroup_p (gdbarch, regnum, group);
88}
89
90/* Return a name that corresponds to the given register number. */
91
92static const char *
93lm32_register_name (struct gdbarch *gdbarch, int reg_nr)
94{
95 static char *register_names[] = {
96 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
97 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
98 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
99 "r24", "r25", "gp", "fp", "sp", "ra", "ea", "ba",
100 "PC", "EID", "EBA", "DEBA", "IE", "IM", "IP"
101 };
102
103 if ((reg_nr < 0) || (reg_nr >= ARRAY_SIZE (register_names)))
104 return NULL;
105 else
106 return register_names[reg_nr];
107}
108
109/* Return type of register. */
110
111static struct type *
112lm32_register_type (struct gdbarch *gdbarch, int reg_nr)
113{
df4df182 114 return builtin_type (gdbarch)->builtin_int32;
c28c63d8
JB
115}
116
117/* Return non-zero if a register can't be written. */
118
119static int
120lm32_cannot_store_register (struct gdbarch *gdbarch, int regno)
121{
122 return (regno == SIM_LM32_R0_REGNUM) || (regno == SIM_LM32_EID_REGNUM);
123}
124
125/* Analyze a function's prologue. */
126
127static CORE_ADDR
e17a4113
UW
128lm32_analyze_prologue (struct gdbarch *gdbarch,
129 CORE_ADDR pc, CORE_ADDR limit,
c28c63d8
JB
130 struct lm32_frame_cache *info)
131{
e17a4113 132 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c28c63d8
JB
133 unsigned long instruction;
134
135 /* Keep reading though instructions, until we come across an instruction
136 that isn't likely to be part of the prologue. */
137 info->size = 0;
138 for (; pc < limit; pc += 4)
139 {
140
141 /* Read an instruction. */
e17a4113 142 instruction = read_memory_integer (pc, 4, byte_order);
c28c63d8
JB
143
144 if ((LM32_OPCODE (instruction) == OP_SW)
145 && (LM32_REG0 (instruction) == SIM_LM32_SP_REGNUM))
146 {
1777feb0 147 /* Any stack displaced store is likely part of the prologue.
c28c63d8
JB
148 Record that the register is being saved, and the offset
149 into the stack. */
150 info->saved_regs[LM32_REG1 (instruction)].addr =
151 LM32_IMM16 (instruction);
152 }
153 else if ((LM32_OPCODE (instruction) == OP_ADDI)
154 && (LM32_REG1 (instruction) == SIM_LM32_SP_REGNUM))
155 {
1777feb0 156 /* An add to the SP is likely to be part of the prologue.
c28c63d8
JB
157 Adjust stack size by whatever the instruction adds to the sp. */
158 info->size -= LM32_IMM16 (instruction);
159 }
160 else if ( /* add fp,fp,sp */
161 ((LM32_OPCODE (instruction) == OP_ADD)
162 && (LM32_REG2 (instruction) == SIM_LM32_FP_REGNUM)
163 && (LM32_REG0 (instruction) == SIM_LM32_FP_REGNUM)
164 && (LM32_REG1 (instruction) == SIM_LM32_SP_REGNUM))
165 /* mv fp,imm */
166 || ((LM32_OPCODE (instruction) == OP_ADDI)
167 && (LM32_REG1 (instruction) == SIM_LM32_FP_REGNUM)
168 && (LM32_REG0 (instruction) == SIM_LM32_R0_REGNUM)))
169 {
170 /* Likely to be in the prologue for functions that require
171 a frame pointer. */
172 }
173 else
174 {
1777feb0
MS
175 /* Any other instruction is likely not to be part of the
176 prologue. */
c28c63d8
JB
177 break;
178 }
179 }
180
181 return pc;
182}
183
184/* Return PC of first non prologue instruction, for the function at the
185 specified address. */
186
187static CORE_ADDR
188lm32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
189{
190 CORE_ADDR func_addr, limit_pc;
c28c63d8
JB
191 struct lm32_frame_cache frame_info;
192 struct trad_frame_saved_reg saved_regs[SIM_LM32_NUM_REGS];
193
194 /* See if we can determine the end of the prologue via the symbol table.
195 If so, then return either PC, or the PC after the prologue, whichever
196 is greater. */
197 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
198 {
d80b854b
UW
199 CORE_ADDR post_prologue_pc
200 = skip_prologue_using_sal (gdbarch, func_addr);
c28c63d8
JB
201 if (post_prologue_pc != 0)
202 return max (pc, post_prologue_pc);
203 }
204
205 /* Can't determine prologue from the symbol table, need to examine
206 instructions. */
207
208 /* Find an upper limit on the function prologue using the debug
209 information. If the debug information could not be used to provide
210 that bound, then use an arbitrary large number as the upper bound. */
d80b854b 211 limit_pc = skip_prologue_using_sal (gdbarch, pc);
c28c63d8
JB
212 if (limit_pc == 0)
213 limit_pc = pc + 100; /* Magic. */
214
215 frame_info.saved_regs = saved_regs;
e17a4113 216 return lm32_analyze_prologue (gdbarch, pc, limit_pc, &frame_info);
c28c63d8
JB
217}
218
219/* Create a breakpoint instruction. */
220
221static const gdb_byte *
222lm32_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
223 int *lenptr)
224{
225 static const gdb_byte breakpoint[4] = { OP_RAISE << 2, 0, 0, 2 };
226
227 *lenptr = sizeof (breakpoint);
228 return breakpoint;
229}
230
231/* Setup registers and stack for faking a call to a function in the
232 inferior. */
233
234static CORE_ADDR
235lm32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
236 struct regcache *regcache, CORE_ADDR bp_addr,
237 int nargs, struct value **args, CORE_ADDR sp,
238 int struct_return, CORE_ADDR struct_addr)
239{
e17a4113 240 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c28c63d8
JB
241 int first_arg_reg = SIM_LM32_R1_REGNUM;
242 int num_arg_regs = 8;
243 int i;
244
245 /* Set the return address. */
246 regcache_cooked_write_signed (regcache, SIM_LM32_RA_REGNUM, bp_addr);
247
248 /* If we're returning a large struct, a pointer to the address to
249 store it at is passed as a first hidden parameter. */
250 if (struct_return)
251 {
252 regcache_cooked_write_unsigned (regcache, first_arg_reg, struct_addr);
253 first_arg_reg++;
254 num_arg_regs--;
255 sp -= 4;
256 }
257
258 /* Setup parameters. */
259 for (i = 0; i < nargs; i++)
260 {
261 struct value *arg = args[i];
262 struct type *arg_type = check_typedef (value_type (arg));
263 gdb_byte *contents;
264 int len;
c28c63d8
JB
265 ULONGEST val;
266
267 /* Promote small integer types to int. */
268 switch (TYPE_CODE (arg_type))
269 {
270 case TYPE_CODE_INT:
271 case TYPE_CODE_BOOL:
272 case TYPE_CODE_CHAR:
273 case TYPE_CODE_RANGE:
274 case TYPE_CODE_ENUM:
275 if (TYPE_LENGTH (arg_type) < 4)
276 {
df4df182 277 arg_type = builtin_type (gdbarch)->builtin_int32;
c28c63d8
JB
278 arg = value_cast (arg_type, arg);
279 }
280 break;
281 }
282
283 /* FIXME: Handle structures. */
284
285 contents = (gdb_byte *) value_contents (arg);
286 len = TYPE_LENGTH (arg_type);
e17a4113 287 val = extract_unsigned_integer (contents, len, byte_order);
c28c63d8
JB
288
289 /* First num_arg_regs parameters are passed by registers,
290 and the rest are passed on the stack. */
291 if (i < num_arg_regs)
292 regcache_cooked_write_unsigned (regcache, first_arg_reg + i, val);
293 else
294 {
295 write_memory (sp, (void *) &val, len);
296 sp -= 4;
297 }
298 }
299
300 /* Update stack pointer. */
301 regcache_cooked_write_signed (regcache, SIM_LM32_SP_REGNUM, sp);
302
303 /* Return adjusted stack pointer. */
304 return sp;
305}
306
307/* Extract return value after calling a function in the inferior. */
308
309static void
310lm32_extract_return_value (struct type *type, struct regcache *regcache,
311 gdb_byte *valbuf)
312{
e17a4113
UW
313 struct gdbarch *gdbarch = get_regcache_arch (regcache);
314 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c28c63d8
JB
315 ULONGEST l;
316 CORE_ADDR return_buffer;
317
318 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
319 && TYPE_CODE (type) != TYPE_CODE_UNION
320 && TYPE_CODE (type) != TYPE_CODE_ARRAY && TYPE_LENGTH (type) <= 4)
321 {
322 /* Return value is returned in a single register. */
323 regcache_cooked_read_unsigned (regcache, SIM_LM32_R1_REGNUM, &l);
e17a4113 324 store_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order, l);
c28c63d8
JB
325 }
326 else if ((TYPE_CODE (type) == TYPE_CODE_INT) && (TYPE_LENGTH (type) == 8))
327 {
328 /* 64-bit values are returned in a register pair. */
329 regcache_cooked_read_unsigned (regcache, SIM_LM32_R1_REGNUM, &l);
330 memcpy (valbuf, &l, 4);
331 regcache_cooked_read_unsigned (regcache, SIM_LM32_R2_REGNUM, &l);
332 memcpy (valbuf + 4, &l, 4);
333 }
334 else
335 {
1777feb0
MS
336 /* Aggregate types greater than a single register are returned
337 in memory. FIXME: Unless they are only 2 regs?. */
c28c63d8
JB
338 regcache_cooked_read_unsigned (regcache, SIM_LM32_R1_REGNUM, &l);
339 return_buffer = l;
340 read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
341 }
342}
343
344/* Write into appropriate registers a function return value of type
345 TYPE, given in virtual format. */
346static void
347lm32_store_return_value (struct type *type, struct regcache *regcache,
348 const gdb_byte *valbuf)
349{
e17a4113
UW
350 struct gdbarch *gdbarch = get_regcache_arch (regcache);
351 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c28c63d8
JB
352 ULONGEST val;
353 int len = TYPE_LENGTH (type);
354
355 if (len <= 4)
356 {
e17a4113 357 val = extract_unsigned_integer (valbuf, len, byte_order);
c28c63d8
JB
358 regcache_cooked_write_unsigned (regcache, SIM_LM32_R1_REGNUM, val);
359 }
360 else if (len <= 8)
361 {
e17a4113 362 val = extract_unsigned_integer (valbuf, 4, byte_order);
c28c63d8 363 regcache_cooked_write_unsigned (regcache, SIM_LM32_R1_REGNUM, val);
e17a4113 364 val = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
c28c63d8
JB
365 regcache_cooked_write_unsigned (regcache, SIM_LM32_R2_REGNUM, val);
366 }
367 else
368 error (_("lm32_store_return_value: type length too large."));
369}
370
371/* Determine whether a functions return value is in a register or memory. */
372static enum return_value_convention
6a3a010b 373lm32_return_value (struct gdbarch *gdbarch, struct value *function,
c28c63d8
JB
374 struct type *valtype, struct regcache *regcache,
375 gdb_byte *readbuf, const gdb_byte *writebuf)
376{
377 enum type_code code = TYPE_CODE (valtype);
378
379 if (code == TYPE_CODE_STRUCT
380 || code == TYPE_CODE_UNION
381 || code == TYPE_CODE_ARRAY || TYPE_LENGTH (valtype) > 8)
382 return RETURN_VALUE_STRUCT_CONVENTION;
383
384 if (readbuf)
385 lm32_extract_return_value (valtype, regcache, readbuf);
386 if (writebuf)
387 lm32_store_return_value (valtype, regcache, writebuf);
388
389 return RETURN_VALUE_REGISTER_CONVENTION;
390}
391
392static CORE_ADDR
393lm32_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
394{
395 return frame_unwind_register_unsigned (next_frame, SIM_LM32_PC_REGNUM);
396}
397
398static CORE_ADDR
399lm32_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
400{
401 return frame_unwind_register_unsigned (next_frame, SIM_LM32_SP_REGNUM);
402}
403
404static struct frame_id
405lm32_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
406{
407 CORE_ADDR sp = get_frame_register_unsigned (this_frame, SIM_LM32_SP_REGNUM);
408
409 return frame_id_build (sp, get_frame_pc (this_frame));
410}
411
412/* Put here the code to store, into fi->saved_regs, the addresses of
413 the saved registers of frame described by FRAME_INFO. This
414 includes special registers such as pc and fp saved in special ways
415 in the stack frame. sp is even more special: the address we return
416 for it IS the sp for the next frame. */
417
418static struct lm32_frame_cache *
419lm32_frame_cache (struct frame_info *this_frame, void **this_prologue_cache)
420{
c28c63d8
JB
421 CORE_ADDR current_pc;
422 ULONGEST prev_sp;
423 ULONGEST this_base;
424 struct lm32_frame_cache *info;
c28c63d8 425 int i;
c28c63d8
JB
426
427 if ((*this_prologue_cache))
428 return (*this_prologue_cache);
429
430 info = FRAME_OBSTACK_ZALLOC (struct lm32_frame_cache);
431 (*this_prologue_cache) = info;
432 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
433
434 info->pc = get_frame_func (this_frame);
435 current_pc = get_frame_pc (this_frame);
e17a4113
UW
436 lm32_analyze_prologue (get_frame_arch (this_frame),
437 info->pc, current_pc, info);
c28c63d8
JB
438
439 /* Compute the frame's base, and the previous frame's SP. */
440 this_base = get_frame_register_unsigned (this_frame, SIM_LM32_SP_REGNUM);
441 prev_sp = this_base + info->size;
442 info->base = this_base;
443
444 /* Convert callee save offsets into addresses. */
445 for (i = 0; i < gdbarch_num_regs (get_frame_arch (this_frame)) - 1; i++)
446 {
447 if (trad_frame_addr_p (info->saved_regs, i))
448 info->saved_regs[i].addr = this_base + info->saved_regs[i].addr;
449 }
450
451 /* The call instruction moves the caller's PC in the callee's RA register.
452 Since this is an unwind, do the reverse. Copy the location of RA register
453 into PC (the address / regnum) so that a request for PC will be
454 converted into a request for the RA register. */
455 info->saved_regs[SIM_LM32_PC_REGNUM] = info->saved_regs[SIM_LM32_RA_REGNUM];
456
1777feb0
MS
457 /* The previous frame's SP needed to be computed. Save the computed
458 value. */
c28c63d8
JB
459 trad_frame_set_value (info->saved_regs, SIM_LM32_SP_REGNUM, prev_sp);
460
461 return info;
462}
463
464static void
465lm32_frame_this_id (struct frame_info *this_frame, void **this_cache,
466 struct frame_id *this_id)
467{
468 struct lm32_frame_cache *cache = lm32_frame_cache (this_frame, this_cache);
469
470 /* This marks the outermost frame. */
471 if (cache->base == 0)
472 return;
473
474 (*this_id) = frame_id_build (cache->base, cache->pc);
475}
476
477static struct value *
478lm32_frame_prev_register (struct frame_info *this_frame,
479 void **this_prologue_cache, int regnum)
480{
481 struct lm32_frame_cache *info;
482
483 info = lm32_frame_cache (this_frame, this_prologue_cache);
484 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
485}
486
487static const struct frame_unwind lm32_frame_unwind = {
488 NORMAL_FRAME,
8fbca658 489 default_frame_unwind_stop_reason,
c28c63d8
JB
490 lm32_frame_this_id,
491 lm32_frame_prev_register,
492 NULL,
493 default_frame_sniffer
494};
495
496static CORE_ADDR
497lm32_frame_base_address (struct frame_info *this_frame, void **this_cache)
498{
499 struct lm32_frame_cache *info = lm32_frame_cache (this_frame, this_cache);
500
501 return info->base;
502}
503
504static const struct frame_base lm32_frame_base = {
505 &lm32_frame_unwind,
506 lm32_frame_base_address,
507 lm32_frame_base_address,
508 lm32_frame_base_address
509};
510
511static CORE_ADDR
512lm32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
513{
514 /* Align to the size of an instruction (so that they can safely be
515 pushed onto the stack. */
516 return sp & ~3;
517}
518
519static struct gdbarch *
520lm32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
521{
522 struct gdbarch *gdbarch;
523 struct gdbarch_tdep *tdep;
524
525 /* If there is already a candidate, use it. */
526 arches = gdbarch_list_lookup_by_info (arches, &info);
527 if (arches != NULL)
528 return arches->gdbarch;
529
530 /* None found, create a new architecture from the information provided. */
531 tdep = XMALLOC (struct gdbarch_tdep);
532 gdbarch = gdbarch_alloc (&info, tdep);
533
534 /* Type sizes. */
535 set_gdbarch_short_bit (gdbarch, 16);
536 set_gdbarch_int_bit (gdbarch, 32);
537 set_gdbarch_long_bit (gdbarch, 32);
538 set_gdbarch_long_long_bit (gdbarch, 64);
539 set_gdbarch_float_bit (gdbarch, 32);
540 set_gdbarch_double_bit (gdbarch, 64);
541 set_gdbarch_long_double_bit (gdbarch, 64);
542 set_gdbarch_ptr_bit (gdbarch, 32);
543
544 /* Register info. */
545 set_gdbarch_num_regs (gdbarch, SIM_LM32_NUM_REGS);
546 set_gdbarch_sp_regnum (gdbarch, SIM_LM32_SP_REGNUM);
547 set_gdbarch_pc_regnum (gdbarch, SIM_LM32_PC_REGNUM);
548 set_gdbarch_register_name (gdbarch, lm32_register_name);
549 set_gdbarch_register_type (gdbarch, lm32_register_type);
550 set_gdbarch_cannot_store_register (gdbarch, lm32_cannot_store_register);
551
552 /* Frame info. */
553 set_gdbarch_skip_prologue (gdbarch, lm32_skip_prologue);
554 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
555 set_gdbarch_decr_pc_after_break (gdbarch, 0);
556 set_gdbarch_frame_args_skip (gdbarch, 0);
557
558 /* Frame unwinding. */
559 set_gdbarch_frame_align (gdbarch, lm32_frame_align);
560 frame_base_set_default (gdbarch, &lm32_frame_base);
561 set_gdbarch_unwind_pc (gdbarch, lm32_unwind_pc);
562 set_gdbarch_unwind_sp (gdbarch, lm32_unwind_sp);
563 set_gdbarch_dummy_id (gdbarch, lm32_dummy_id);
564 frame_unwind_append_unwinder (gdbarch, &lm32_frame_unwind);
565
566 /* Breakpoints. */
567 set_gdbarch_breakpoint_from_pc (gdbarch, lm32_breakpoint_from_pc);
568 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
569
570 /* Calling functions in the inferior. */
571 set_gdbarch_push_dummy_call (gdbarch, lm32_push_dummy_call);
572 set_gdbarch_return_value (gdbarch, lm32_return_value);
573
574 /* Instruction disassembler. */
575 set_gdbarch_print_insn (gdbarch, print_insn_lm32);
576
577 lm32_add_reggroups (gdbarch);
578 set_gdbarch_register_reggroup_p (gdbarch, lm32_register_reggroup_p);
579
580 return gdbarch;
581}
582
693be288
JK
583/* -Wmissing-prototypes */
584extern initialize_file_ftype _initialize_lm32_tdep;
585
c28c63d8
JB
586void
587_initialize_lm32_tdep (void)
588{
589 register_gdbarch_init (bfd_arch_lm32, lm32_gdbarch_init);
590}
This page took 0.47578 seconds and 4 git commands to generate.