Commit | Line | Data |
---|---|---|
8818c391 TR |
1 | /* Target-dependent code for Atmel AVR, for GDB. |
2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 | |
3 | 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 2 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, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | /* Contributed by Theodore A. Roth, troth@verinet.com */ | |
23 | ||
24 | /* Portions of this file were taken from the original gdb-4.18 patch developed | |
25 | by Denis Chertykov, denisc@overta.ru */ | |
26 | ||
27 | #include "defs.h" | |
28 | #include "gdbcmd.h" | |
29 | #include "gdbcore.h" | |
30 | #include "inferior.h" | |
31 | #include "symfile.h" | |
32 | #include "arch-utils.h" | |
33 | #include "regcache.h" | |
34 | ||
35 | /* AVR Background: | |
36 | ||
37 | (AVR micros are pure Harvard Architecture processors.) | |
38 | ||
39 | The AVR family of microcontrollers have three distinctly different memory | |
40 | spaces: flash, sram and eeprom. The flash is 16 bits wide and is used for | |
41 | the most part to store program instructions. The sram is 8 bits wide and is | |
42 | used for the stack and the heap. Some devices lack sram and some can have | |
43 | an additional external sram added on as a peripheral. | |
44 | ||
45 | The eeprom is 8 bits wide and is used to store data when the device is | |
46 | powered down. Eeprom is not directly accessible, it can only be accessed | |
47 | via io-registers using a special algorithm. Accessing eeprom via gdb's | |
48 | remote serial protocol ('m' or 'M' packets) looks difficult to do and is | |
49 | not included at this time. | |
50 | ||
51 | [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or | |
52 | written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''. For this to | |
53 | work, the remote target must be able to handle eeprom accesses and perform | |
54 | the address translation.] | |
55 | ||
56 | All three memory spaces have physical addresses beginning at 0x0. In | |
57 | addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit | |
58 | bytes instead of the 16 bit wide words used by the real device for the | |
59 | Program Counter. | |
60 | ||
61 | In order for remote targets to work correctly, extra bits must be added to | |
62 | addresses before they are send to the target or received from the target | |
63 | via the remote serial protocol. The extra bits are the MSBs and are used to | |
64 | decode which memory space the address is referring to. */ | |
65 | ||
66 | #undef XMALLOC | |
67 | #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE))) | |
68 | ||
69 | #undef EXTRACT_INSN | |
70 | #define EXTRACT_INSN(addr) extract_unsigned_integer(addr,2) | |
71 | ||
72 | /* Constants: prefixed with AVR_ to avoid name space clashes */ | |
73 | ||
74 | enum | |
2e5ff58c TR |
75 | { |
76 | AVR_REG_W = 24, | |
77 | AVR_REG_X = 26, | |
78 | AVR_REG_Y = 28, | |
79 | AVR_FP_REGNUM = 28, | |
80 | AVR_REG_Z = 30, | |
81 | ||
82 | AVR_SREG_REGNUM = 32, | |
83 | AVR_SP_REGNUM = 33, | |
84 | AVR_PC_REGNUM = 34, | |
85 | ||
86 | AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/, | |
87 | AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/, | |
88 | ||
89 | AVR_PC_REG_INDEX = 35, /* index into array of registers */ | |
90 | ||
91 | AVR_MAX_PROLOGUE_SIZE = 56, /* bytes */ | |
92 | ||
93 | /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */ | |
94 | AVR_MAX_PUSHES = 18, | |
95 | ||
96 | /* Number of the last pushed register. r17 for current avr-gcc */ | |
97 | AVR_LAST_PUSHED_REGNUM = 17, | |
98 | ||
99 | /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8 | |
100 | bits? Do these have to match the bfd vma values?. It sure would make | |
101 | things easier in the future if they didn't need to match. | |
102 | ||
103 | Note: I chose these values so as to be consistent with bfd vma | |
104 | addresses. | |
105 | ||
106 | TRoth/2002-04-08: There is already a conflict with very large programs | |
107 | in the mega128. The mega128 has 128K instruction bytes (64K words), | |
108 | thus the Most Significant Bit is 0x10000 which gets masked off my | |
109 | AVR_MEM_MASK. | |
110 | ||
111 | The problem manifests itself when trying to set a breakpoint in a | |
112 | function which resides in the upper half of the instruction space and | |
113 | thus requires a 17-bit address. | |
114 | ||
115 | For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK | |
116 | from 0x00ff0000 to 0x00f00000. Eeprom is not accessible from gdb yet, | |
117 | but could be for some remote targets by just adding the correct offset | |
118 | to the address and letting the remote target handle the low-level | |
119 | details of actually accessing the eeprom. */ | |
120 | ||
121 | AVR_IMEM_START = 0x00000000, /* INSN memory */ | |
122 | AVR_SMEM_START = 0x00800000, /* SRAM memory */ | |
8818c391 | 123 | #if 1 |
2e5ff58c TR |
124 | /* No eeprom mask defined */ |
125 | AVR_MEM_MASK = 0x00f00000, /* mask to determine memory space */ | |
8818c391 | 126 | #else |
2e5ff58c TR |
127 | AVR_EMEM_START = 0x00810000, /* EEPROM memory */ |
128 | AVR_MEM_MASK = 0x00ff0000, /* mask to determine memory space */ | |
8818c391 | 129 | #endif |
2e5ff58c | 130 | }; |
8818c391 TR |
131 | |
132 | /* Any function with a frame looks like this | |
133 | ....... <-SP POINTS HERE | |
134 | LOCALS1 <-FP POINTS HERE | |
135 | LOCALS0 | |
136 | SAVED FP | |
137 | SAVED R3 | |
138 | SAVED R2 | |
139 | RET PC | |
140 | FIRST ARG | |
141 | SECOND ARG */ | |
142 | ||
143 | struct frame_extra_info | |
2e5ff58c TR |
144 | { |
145 | CORE_ADDR return_pc; | |
146 | CORE_ADDR args_pointer; | |
147 | int locals_size; | |
148 | int framereg; | |
149 | int framesize; | |
150 | int is_main; | |
151 | }; | |
8818c391 TR |
152 | |
153 | struct gdbarch_tdep | |
2e5ff58c TR |
154 | { |
155 | /* FIXME: TRoth: is there anything to put here? */ | |
156 | int foo; | |
157 | }; | |
8818c391 TR |
158 | |
159 | /* Lookup the name of a register given it's number. */ | |
160 | ||
161 | static char * | |
162 | avr_register_name (int regnum) | |
163 | { | |
2e5ff58c TR |
164 | static char *register_names[] = { |
165 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", | |
166 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", | |
8818c391 TR |
167 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", |
168 | "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", | |
169 | "SREG", "SP", "PC" | |
170 | }; | |
171 | if (regnum < 0) | |
172 | return NULL; | |
173 | if (regnum >= (sizeof (register_names) / sizeof (*register_names))) | |
174 | return NULL; | |
175 | return register_names[regnum]; | |
176 | } | |
177 | ||
178 | /* Index within `registers' of the first byte of the space for | |
179 | register REGNUM. */ | |
180 | ||
181 | static int | |
182 | avr_register_byte (int regnum) | |
183 | { | |
184 | if (regnum < AVR_PC_REGNUM) | |
185 | return regnum; | |
186 | else | |
187 | return AVR_PC_REG_INDEX; | |
188 | } | |
189 | ||
190 | /* Number of bytes of storage in the actual machine representation for | |
191 | register REGNUM. */ | |
192 | ||
193 | static int | |
194 | avr_register_raw_size (int regnum) | |
195 | { | |
196 | switch (regnum) | |
197 | { | |
198 | case AVR_PC_REGNUM: | |
199 | return 4; | |
200 | case AVR_SP_REGNUM: | |
201 | case AVR_FP_REGNUM: | |
202 | return 2; | |
203 | default: | |
204 | return 1; | |
205 | } | |
206 | } | |
207 | ||
208 | /* Number of bytes of storage in the program's representation | |
209 | for register N. */ | |
210 | ||
211 | static int | |
212 | avr_register_virtual_size (int regnum) | |
213 | { | |
214 | return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum)); | |
215 | } | |
216 | ||
217 | /* Return the GDB type object for the "standard" data type | |
218 | of data in register N. */ | |
219 | ||
220 | static struct type * | |
221 | avr_register_virtual_type (int regnum) | |
222 | { | |
223 | switch (regnum) | |
224 | { | |
225 | case AVR_PC_REGNUM: | |
226 | return builtin_type_unsigned_long; | |
227 | case AVR_SP_REGNUM: | |
228 | return builtin_type_unsigned_short; | |
229 | default: | |
230 | return builtin_type_unsigned_char; | |
231 | } | |
232 | } | |
233 | ||
234 | /* Instruction address checks and convertions. */ | |
235 | ||
236 | static CORE_ADDR | |
237 | avr_make_iaddr (CORE_ADDR x) | |
238 | { | |
239 | return ((x) | AVR_IMEM_START); | |
240 | } | |
241 | ||
242 | static int | |
243 | avr_iaddr_p (CORE_ADDR x) | |
244 | { | |
245 | return (((x) & AVR_MEM_MASK) == AVR_IMEM_START); | |
246 | } | |
247 | ||
248 | /* FIXME: TRoth: Really need to use a larger mask for instructions. Some | |
249 | devices are already up to 128KBytes of flash space. | |
250 | ||
251 | TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */ | |
252 | ||
253 | static CORE_ADDR | |
254 | avr_convert_iaddr_to_raw (CORE_ADDR x) | |
255 | { | |
256 | return ((x) & 0xffffffff); | |
257 | } | |
258 | ||
259 | /* SRAM address checks and convertions. */ | |
260 | ||
261 | static CORE_ADDR | |
262 | avr_make_saddr (CORE_ADDR x) | |
263 | { | |
264 | return ((x) | AVR_SMEM_START); | |
265 | } | |
266 | ||
267 | static int | |
268 | avr_saddr_p (CORE_ADDR x) | |
269 | { | |
270 | return (((x) & AVR_MEM_MASK) == AVR_SMEM_START); | |
271 | } | |
272 | ||
273 | static CORE_ADDR | |
274 | avr_convert_saddr_to_raw (CORE_ADDR x) | |
275 | { | |
276 | return ((x) & 0xffffffff); | |
277 | } | |
278 | ||
279 | /* EEPROM address checks and convertions. I don't know if these will ever | |
280 | actually be used, but I've added them just the same. TRoth */ | |
281 | ||
282 | /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large | |
283 | programs in the mega128. */ | |
284 | ||
285 | /* static CORE_ADDR */ | |
286 | /* avr_make_eaddr (CORE_ADDR x) */ | |
287 | /* { */ | |
288 | /* return ((x) | AVR_EMEM_START); */ | |
289 | /* } */ | |
290 | ||
291 | /* static int */ | |
292 | /* avr_eaddr_p (CORE_ADDR x) */ | |
293 | /* { */ | |
294 | /* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */ | |
295 | /* } */ | |
296 | ||
297 | /* static CORE_ADDR */ | |
298 | /* avr_convert_eaddr_to_raw (CORE_ADDR x) */ | |
299 | /* { */ | |
300 | /* return ((x) & 0xffffffff); */ | |
301 | /* } */ | |
302 | ||
303 | /* Convert from address to pointer and vice-versa. */ | |
304 | ||
305 | static void | |
306 | avr_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr) | |
307 | { | |
308 | /* Is it a code address? */ | |
309 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC | |
310 | || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD) | |
311 | { | |
2e5ff58c TR |
312 | store_unsigned_integer (buf, TYPE_LENGTH (type), |
313 | avr_convert_iaddr_to_raw (addr)); | |
8818c391 TR |
314 | } |
315 | else | |
316 | { | |
317 | /* Strip off any upper segment bits. */ | |
2e5ff58c TR |
318 | store_unsigned_integer (buf, TYPE_LENGTH (type), |
319 | avr_convert_saddr_to_raw (addr)); | |
8818c391 TR |
320 | } |
321 | } | |
322 | ||
323 | static CORE_ADDR | |
324 | avr_pointer_to_address (struct type *type, void *buf) | |
325 | { | |
326 | CORE_ADDR addr = extract_address (buf, TYPE_LENGTH (type)); | |
327 | ||
2e5ff58c | 328 | if (TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type))) |
8818c391 | 329 | { |
2e5ff58c TR |
330 | fprintf_unfiltered (gdb_stderr, "CODE_SPACE ---->> ptr->addr: 0x%lx\n", |
331 | addr); | |
332 | fprintf_unfiltered (gdb_stderr, | |
333 | "+++ If you see this, please send me an email <troth@verinet.com>\n"); | |
8818c391 TR |
334 | } |
335 | ||
336 | /* Is it a code address? */ | |
337 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC | |
338 | || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD | |
2e5ff58c | 339 | || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type))) |
8818c391 TR |
340 | return avr_make_iaddr (addr); |
341 | else | |
342 | return avr_make_saddr (addr); | |
343 | } | |
344 | ||
345 | static CORE_ADDR | |
346 | avr_read_pc (ptid_t ptid) | |
347 | { | |
348 | ptid_t save_ptid; | |
349 | CORE_ADDR pc; | |
350 | CORE_ADDR retval; | |
351 | ||
352 | save_ptid = inferior_ptid; | |
353 | inferior_ptid = ptid; | |
354 | pc = (int) read_register (AVR_PC_REGNUM); | |
355 | inferior_ptid = save_ptid; | |
356 | retval = avr_make_iaddr (pc); | |
357 | return retval; | |
358 | } | |
359 | ||
360 | static void | |
361 | avr_write_pc (CORE_ADDR val, ptid_t ptid) | |
362 | { | |
363 | ptid_t save_ptid; | |
364 | ||
365 | save_ptid = inferior_ptid; | |
366 | inferior_ptid = ptid; | |
367 | write_register (AVR_PC_REGNUM, avr_convert_iaddr_to_raw (val)); | |
368 | inferior_ptid = save_ptid; | |
369 | } | |
370 | ||
371 | static CORE_ADDR | |
372 | avr_read_sp (void) | |
373 | { | |
374 | return (avr_make_saddr (read_register (AVR_SP_REGNUM))); | |
375 | } | |
376 | ||
377 | static void | |
378 | avr_write_sp (CORE_ADDR val) | |
379 | { | |
380 | write_register (AVR_SP_REGNUM, avr_convert_saddr_to_raw (val)); | |
381 | } | |
382 | ||
383 | static CORE_ADDR | |
384 | avr_read_fp (void) | |
385 | { | |
386 | return (avr_make_saddr (read_register (AVR_FP_REGNUM))); | |
387 | } | |
388 | ||
389 | /* Translate a GDB virtual ADDR/LEN into a format the remote target | |
390 | understands. Returns number of bytes that can be transfered | |
391 | starting at TARG_ADDR. Return ZERO if no bytes can be transfered | |
392 | (segmentation fault). | |
393 | ||
394 | TRoth/2002-04-08: Could this be used to check for dereferencing an invalid | |
395 | pointer? */ | |
396 | ||
397 | static void | |
398 | avr_remote_translate_xfer_address (CORE_ADDR memaddr, int nr_bytes, | |
2e5ff58c | 399 | CORE_ADDR *targ_addr, int *targ_len) |
8818c391 TR |
400 | { |
401 | long out_addr; | |
402 | long out_len; | |
403 | ||
404 | /* FIXME: TRoth: Do nothing for now. Will need to examine memaddr at this | |
405 | point and see if the high bit are set with the masks that we want. */ | |
406 | ||
407 | *targ_addr = memaddr; | |
408 | *targ_len = nr_bytes; | |
409 | } | |
410 | ||
411 | /* Function pointers obtained from the target are half of what gdb expects so | |
412 | multiply by 2. */ | |
413 | ||
414 | static CORE_ADDR | |
415 | avr_convert_from_func_ptr_addr (CORE_ADDR addr) | |
416 | { | |
417 | return addr * 2; | |
418 | } | |
419 | ||
420 | /* avr_scan_prologue is also used as the frame_init_saved_regs(). | |
421 | ||
422 | Put here the code to store, into fi->saved_regs, the addresses of | |
423 | the saved registers of frame described by FRAME_INFO. This | |
424 | includes special registers such as pc and fp saved in special ways | |
425 | in the stack frame. sp is even more special: the address we return | |
426 | for it IS the sp for the next frame. */ | |
427 | ||
428 | /* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info) | |
429 | This function decodes a AVR function prologue to determine: | |
430 | 1) the size of the stack frame | |
431 | 2) which registers are saved on it | |
432 | 3) the offsets of saved regs | |
433 | This information is stored in the "extra_info" field of the frame_info. | |
434 | ||
435 | A typical AVR function prologue might look like this: | |
436 | push rXX | |
437 | push r28 | |
438 | push r29 | |
439 | in r28,__SP_L__ | |
440 | in r29,__SP_H__ | |
441 | sbiw r28,<LOCALS_SIZE> | |
442 | in __tmp_reg__,__SREG__ | |
443 | cli | |
444 | out __SP_L__,r28 | |
445 | out __SREG__,__tmp_reg__ | |
446 | out __SP_H__,r29 | |
447 | ||
448 | A `-mcall-prologues' prologue look like this: | |
449 | ldi r26,<LOCALS_SIZE> | |
450 | ldi r27,<LOCALS_SIZE>/265 | |
451 | ldi r30,pm_lo8(.L_foo_body) | |
452 | ldi r31,pm_hi8(.L_foo_body) | |
453 | rjmp __prologue_saves__+RRR | |
454 | .L_foo_body: */ | |
455 | ||
456 | static void | |
457 | avr_scan_prologue (struct frame_info *fi) | |
458 | { | |
459 | CORE_ADDR prologue_start; | |
460 | CORE_ADDR prologue_end; | |
2e5ff58c TR |
461 | int i; |
462 | unsigned short insn; | |
463 | int regno; | |
464 | int scan_stage = 0; | |
465 | char *name; | |
8818c391 | 466 | struct minimal_symbol *msymbol; |
2e5ff58c | 467 | int prologue_len; |
8818c391 TR |
468 | unsigned char prologue[AVR_MAX_PROLOGUE_SIZE]; |
469 | int vpc = 0; | |
470 | ||
471 | fi->extra_info->framereg = AVR_SP_REGNUM; | |
2e5ff58c TR |
472 | |
473 | if (find_pc_partial_function | |
474 | (fi->pc, &name, &prologue_start, &prologue_end)) | |
8818c391 TR |
475 | { |
476 | struct symtab_and_line sal = find_pc_line (prologue_start, 0); | |
477 | ||
2e5ff58c TR |
478 | if (sal.line == 0) /* no line info, use current PC */ |
479 | prologue_end = fi->pc; | |
480 | else if (sal.end < prologue_end) /* next line begins after fn end */ | |
481 | prologue_end = sal.end; /* (probably means no prologue) */ | |
8818c391 TR |
482 | } |
483 | else | |
484 | /* We're in the boondocks: allow for */ | |
485 | /* 19 pushes, an add, and "mv fp,sp" */ | |
2e5ff58c | 486 | prologue_end = prologue_start + AVR_MAX_PROLOGUE_SIZE; |
8818c391 TR |
487 | |
488 | prologue_end = min (prologue_end, fi->pc); | |
489 | ||
490 | /* Search the prologue looking for instructions that set up the | |
491 | frame pointer, adjust the stack pointer, and save registers. */ | |
492 | ||
493 | fi->extra_info->framesize = 0; | |
494 | prologue_len = prologue_end - prologue_start; | |
495 | read_memory (prologue_start, prologue, prologue_len); | |
496 | ||
497 | /* Scanning main()'s prologue | |
498 | ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) | |
499 | ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) | |
500 | out __SP_H__,r29 | |
501 | out __SP_L__,r28 */ | |
502 | ||
503 | if (name && strcmp ("main", name) == 0 && prologue_len == 8) | |
504 | { | |
505 | CORE_ADDR locals; | |
2e5ff58c TR |
506 | unsigned char img[] = { |
507 | 0xde, 0xbf, /* out __SP_H__,r29 */ | |
508 | 0xcd, 0xbf /* out __SP_L__,r28 */ | |
8818c391 TR |
509 | }; |
510 | ||
511 | fi->extra_info->framereg = AVR_FP_REGNUM; | |
512 | insn = EXTRACT_INSN (&prologue[vpc]); | |
513 | /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */ | |
2e5ff58c TR |
514 | if ((insn & 0xf0f0) == 0xe0c0) |
515 | { | |
516 | locals = (insn & 0xf) | ((insn & 0x0f00) >> 4); | |
517 | insn = EXTRACT_INSN (&prologue[vpc + 2]); | |
518 | /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */ | |
519 | if ((insn & 0xf0f0) == 0xe0d0) | |
520 | { | |
521 | locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8; | |
522 | if (memcmp (prologue + vpc + 4, img, sizeof (img)) == 0) | |
523 | { | |
524 | fi->frame = locals; | |
525 | ||
526 | /* TRoth: Does -1 mean we're in main? */ | |
527 | fi->extra_info->is_main = 1; | |
528 | return; | |
529 | } | |
530 | } | |
531 | } | |
8818c391 | 532 | } |
2e5ff58c | 533 | |
8818c391 TR |
534 | /* Scanning `-mcall-prologues' prologue |
535 | FIXME: mega prologue have a 12 bytes long */ | |
536 | ||
2e5ff58c | 537 | while (prologue_len <= 12) /* I'm use while to avoit many goto's */ |
8818c391 TR |
538 | { |
539 | int loc_size; | |
540 | int body_addr; | |
541 | unsigned num_pushes; | |
2e5ff58c | 542 | |
8818c391 TR |
543 | insn = EXTRACT_INSN (&prologue[vpc]); |
544 | /* ldi r26,<LOCALS_SIZE> */ | |
2e5ff58c TR |
545 | if ((insn & 0xf0f0) != 0xe0a0) |
546 | break; | |
8818c391 | 547 | loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4); |
2e5ff58c | 548 | |
8818c391 TR |
549 | insn = EXTRACT_INSN (&prologue[vpc + 2]); |
550 | /* ldi r27,<LOCALS_SIZE> / 256 */ | |
551 | if ((insn & 0xf0f0) != 0xe0b0) | |
2e5ff58c | 552 | break; |
8818c391 | 553 | loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8; |
2e5ff58c | 554 | |
8818c391 TR |
555 | insn = EXTRACT_INSN (&prologue[vpc + 4]); |
556 | /* ldi r30,pm_lo8(.L_foo_body) */ | |
557 | if ((insn & 0xf0f0) != 0xe0e0) | |
2e5ff58c | 558 | break; |
8818c391 TR |
559 | body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4); |
560 | ||
561 | insn = EXTRACT_INSN (&prologue[vpc + 6]); | |
562 | /* ldi r31,pm_hi8(.L_foo_body) */ | |
563 | if ((insn & 0xf0f0) != 0xe0f0) | |
2e5ff58c | 564 | break; |
8818c391 TR |
565 | body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8; |
566 | ||
567 | if (body_addr != (prologue_start + 10) / 2) | |
2e5ff58c | 568 | break; |
8818c391 TR |
569 | |
570 | msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL); | |
571 | if (!msymbol) | |
2e5ff58c | 572 | break; |
8818c391 TR |
573 | |
574 | /* FIXME: prologue for mega have a JMP instead of RJMP */ | |
575 | insn = EXTRACT_INSN (&prologue[vpc + 8]); | |
576 | /* rjmp __prologue_saves__+RRR */ | |
577 | if ((insn & 0xf000) != 0xc000) | |
2e5ff58c TR |
578 | break; |
579 | ||
8818c391 TR |
580 | /* Extract PC relative offset from RJMP */ |
581 | i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0); | |
582 | /* Convert offset to byte addressable mode */ | |
583 | i *= 2; | |
584 | /* Destination address */ | |
585 | i += vpc + prologue_start + 10; | |
586 | /* Resovle offset (in words) from __prologue_saves__ symbol. | |
587 | Which is a pushes count in `-mcall-prologues' mode */ | |
588 | num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2; | |
589 | ||
590 | if (num_pushes > AVR_MAX_PUSHES) | |
2e5ff58c TR |
591 | num_pushes = 0; |
592 | ||
8818c391 | 593 | if (num_pushes) |
2e5ff58c TR |
594 | { |
595 | int from; | |
596 | fi->saved_regs[AVR_FP_REGNUM + 1] = num_pushes; | |
597 | if (num_pushes >= 2) | |
598 | fi->saved_regs[AVR_FP_REGNUM] = num_pushes - 1; | |
599 | i = 0; | |
600 | for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2); | |
601 | from <= AVR_LAST_PUSHED_REGNUM; ++from) | |
602 | fi->saved_regs[from] = ++i; | |
603 | } | |
8818c391 TR |
604 | fi->extra_info->locals_size = loc_size; |
605 | fi->extra_info->framesize = loc_size + num_pushes; | |
606 | fi->extra_info->framereg = AVR_FP_REGNUM; | |
607 | return; | |
608 | } | |
609 | ||
610 | /* Scan interrupt or signal function */ | |
611 | ||
612 | if (prologue_len >= 12) | |
613 | { | |
2e5ff58c TR |
614 | unsigned char img[] = { |
615 | 0x78, 0x94, /* sei */ | |
616 | 0x1f, 0x92, /* push r1 */ | |
617 | 0x0f, 0x92, /* push r0 */ | |
618 | 0x0f, 0xb6, /* in r0,0x3f SREG */ | |
619 | 0x0f, 0x92, /* push r0 */ | |
620 | 0x11, 0x24 /* clr r1 */ | |
8818c391 TR |
621 | }; |
622 | if (memcmp (prologue, img, sizeof (img)) == 0) | |
2e5ff58c TR |
623 | { |
624 | vpc += sizeof (img); | |
625 | fi->saved_regs[0] = 2; | |
626 | fi->saved_regs[1] = 1; | |
627 | fi->extra_info->framesize += 3; | |
628 | } | |
8818c391 | 629 | else if (memcmp (img + 1, prologue, sizeof (img) - 1) == 0) |
2e5ff58c TR |
630 | { |
631 | vpc += sizeof (img) - 1; | |
632 | fi->saved_regs[0] = 2; | |
633 | fi->saved_regs[1] = 1; | |
634 | fi->extra_info->framesize += 3; | |
635 | } | |
8818c391 TR |
636 | } |
637 | ||
638 | /* First stage of the prologue scanning. | |
639 | Scan pushes */ | |
640 | ||
641 | for (; vpc <= prologue_len; vpc += 2) | |
642 | { | |
643 | insn = EXTRACT_INSN (&prologue[vpc]); | |
2e5ff58c TR |
644 | if ((insn & 0xfe0f) == 0x920f) /* push rXX */ |
645 | { | |
646 | /* Bits 4-9 contain a mask for registers R0-R32. */ | |
647 | regno = (insn & 0x1f0) >> 4; | |
648 | ++fi->extra_info->framesize; | |
649 | fi->saved_regs[regno] = fi->extra_info->framesize; | |
650 | scan_stage = 1; | |
651 | } | |
8818c391 | 652 | else |
2e5ff58c | 653 | break; |
8818c391 TR |
654 | } |
655 | ||
656 | /* Second stage of the prologue scanning. | |
657 | Scan: | |
658 | in r28,__SP_L__ | |
659 | in r29,__SP_H__ */ | |
660 | ||
661 | if (scan_stage == 1 && vpc + 4 <= prologue_len) | |
662 | { | |
2e5ff58c TR |
663 | unsigned char img[] = { |
664 | 0xcd, 0xb7, /* in r28,__SP_L__ */ | |
665 | 0xde, 0xb7 /* in r29,__SP_H__ */ | |
8818c391 TR |
666 | }; |
667 | unsigned short insn1; | |
2e5ff58c | 668 | |
8818c391 | 669 | if (memcmp (prologue + vpc, img, sizeof (img)) == 0) |
2e5ff58c TR |
670 | { |
671 | vpc += 4; | |
672 | fi->extra_info->framereg = AVR_FP_REGNUM; | |
673 | scan_stage = 2; | |
674 | } | |
8818c391 TR |
675 | } |
676 | ||
677 | /* Third stage of the prologue scanning. (Really two stages) | |
678 | Scan for: | |
679 | sbiw r28,XX or subi r28,lo8(XX) | |
680 | sbci r29,hi8(XX) | |
681 | in __tmp_reg__,__SREG__ | |
682 | cli | |
683 | out __SP_L__,r28 | |
684 | out __SREG__,__tmp_reg__ | |
685 | out __SP_H__,r29 */ | |
686 | ||
687 | if (scan_stage == 2 && vpc + 12 <= prologue_len) | |
688 | { | |
689 | int locals_size = 0; | |
2e5ff58c TR |
690 | unsigned char img[] = { |
691 | 0x0f, 0xb6, /* in r0,0x3f */ | |
692 | 0xf8, 0x94, /* cli */ | |
693 | 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */ | |
694 | 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */ | |
695 | 0xde, 0xbf /* out 0x3e,r29 ; SPH */ | |
8818c391 | 696 | }; |
2e5ff58c TR |
697 | unsigned char img_sig[] = { |
698 | 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */ | |
699 | 0xde, 0xbf /* out 0x3e,r29 ; SPH */ | |
8818c391 | 700 | }; |
2e5ff58c TR |
701 | unsigned char img_int[] = { |
702 | 0xf8, 0x94, /* cli */ | |
703 | 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */ | |
704 | 0x78, 0x94, /* sei */ | |
705 | 0xde, 0xbf /* out 0x3e,r29 ; SPH */ | |
8818c391 | 706 | }; |
2e5ff58c | 707 | |
8818c391 TR |
708 | insn = EXTRACT_INSN (&prologue[vpc]); |
709 | vpc += 2; | |
2e5ff58c TR |
710 | if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */ |
711 | locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2); | |
712 | else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */ | |
713 | { | |
714 | locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4); | |
715 | insn = EXTRACT_INSN (&prologue[vpc]); | |
716 | vpc += 2; | |
717 | locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4) << 8); | |
718 | } | |
8818c391 | 719 | else |
2e5ff58c | 720 | return; |
8818c391 TR |
721 | fi->extra_info->locals_size = locals_size; |
722 | fi->extra_info->framesize += locals_size; | |
723 | } | |
724 | } | |
725 | ||
726 | /* This function actually figures out the frame address for a given pc and | |
727 | sp. This is tricky because we sometimes don't use an explicit | |
728 | frame pointer, and the previous stack pointer isn't necessarily recorded | |
729 | on the stack. The only reliable way to get this info is to | |
730 | examine the prologue. */ | |
731 | ||
732 | static void | |
733 | avr_init_extra_frame_info (int fromleaf, struct frame_info *fi) | |
734 | { | |
735 | int reg; | |
736 | ||
737 | if (fi->next) | |
738 | fi->pc = FRAME_SAVED_PC (fi->next); | |
739 | ||
740 | fi->extra_info = (struct frame_extra_info *) | |
741 | frame_obstack_alloc (sizeof (struct frame_extra_info)); | |
742 | frame_saved_regs_zalloc (fi); | |
743 | ||
744 | fi->extra_info->return_pc = 0; | |
745 | fi->extra_info->args_pointer = 0; | |
746 | fi->extra_info->locals_size = 0; | |
747 | fi->extra_info->framereg = 0; | |
748 | fi->extra_info->framesize = 0; | |
749 | fi->extra_info->is_main = 0; | |
2e5ff58c | 750 | |
8818c391 TR |
751 | avr_scan_prologue (fi); |
752 | ||
753 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) | |
754 | { | |
755 | /* We need to setup fi->frame here because run_stack_dummy gets it wrong | |
756 | by assuming it's always FP. */ | |
2e5ff58c | 757 | fi->frame = generic_read_register_dummy (fi->pc, fi->frame, fi->frame); |
8818c391 | 758 | } |
2e5ff58c | 759 | else if (!fi->next) /* this is the innermost frame? */ |
8818c391 | 760 | fi->frame = read_register (fi->extra_info->framereg); |
2e5ff58c | 761 | else if (fi->extra_info->is_main != 1) /* not the innermost frame, not `main' */ |
8818c391 TR |
762 | /* If we have an next frame, the callee saved it. */ |
763 | { | |
2e5ff58c | 764 | struct frame_info *next_fi = fi->next; |
8818c391 | 765 | if (fi->extra_info->framereg == AVR_SP_REGNUM) |
2e5ff58c TR |
766 | fi->frame = |
767 | next_fi->frame + 2 /* ret addr */ + next_fi->extra_info->framesize; | |
8818c391 TR |
768 | /* FIXME: I don't analyse va_args functions */ |
769 | else | |
2e5ff58c TR |
770 | { |
771 | CORE_ADDR fp = 0; | |
772 | CORE_ADDR fp1 = 0; | |
773 | unsigned int fp_low, fp_high; | |
774 | ||
775 | /* Scan all frames */ | |
776 | for (; next_fi; next_fi = next_fi->next) | |
777 | { | |
778 | /* look for saved AVR_FP_REGNUM */ | |
779 | if (next_fi->saved_regs[AVR_FP_REGNUM] && !fp) | |
780 | fp = next_fi->saved_regs[AVR_FP_REGNUM]; | |
781 | /* look for saved AVR_FP_REGNUM + 1 */ | |
782 | if (next_fi->saved_regs[AVR_FP_REGNUM + 1] && !fp1) | |
783 | fp1 = next_fi->saved_regs[AVR_FP_REGNUM + 1]; | |
784 | } | |
785 | fp_low = (fp ? read_memory_unsigned_integer (avr_make_saddr (fp), 1) | |
786 | : read_register (AVR_FP_REGNUM)) & 0xff; | |
787 | fp_high = | |
788 | (fp1 ? read_memory_unsigned_integer (avr_make_saddr (fp1), 1) : | |
789 | read_register (AVR_FP_REGNUM + 1)) & 0xff; | |
790 | fi->frame = fp_low | (fp_high << 8); | |
791 | } | |
8818c391 TR |
792 | } |
793 | ||
794 | /* TRoth: Do we want to do this if we are in main? I don't think we should | |
795 | since return_pc makes no sense when we are in main. */ | |
796 | ||
2e5ff58c | 797 | if ((fi->pc) && (fi->extra_info->is_main == 0)) /* We are not in CALL_DUMMY */ |
8818c391 TR |
798 | { |
799 | CORE_ADDR addr; | |
800 | int i; | |
2e5ff58c | 801 | |
8818c391 | 802 | addr = fi->frame + fi->extra_info->framesize + 1; |
2e5ff58c | 803 | |
8818c391 TR |
804 | /* Return address in stack in different endianness */ |
805 | ||
806 | fi->extra_info->return_pc = | |
2e5ff58c | 807 | read_memory_unsigned_integer (avr_make_saddr (addr), 1) << 8; |
8818c391 | 808 | fi->extra_info->return_pc |= |
2e5ff58c TR |
809 | read_memory_unsigned_integer (avr_make_saddr (addr + 1), 1); |
810 | ||
8818c391 TR |
811 | /* This return address in words, |
812 | must be converted to the bytes address */ | |
813 | fi->extra_info->return_pc *= 2; | |
814 | ||
815 | /* Resolve a pushed registers addresses */ | |
816 | for (i = 0; i < NUM_REGS; i++) | |
2e5ff58c TR |
817 | { |
818 | if (fi->saved_regs[i]) | |
819 | fi->saved_regs[i] = addr - fi->saved_regs[i]; | |
820 | } | |
8818c391 TR |
821 | } |
822 | } | |
823 | ||
824 | /* Restore the machine to the state it had before the current frame was | |
825 | created. Usually used either by the "RETURN" command, or by | |
826 | call_function_by_hand after the dummy_frame is finished. */ | |
827 | ||
828 | static void | |
829 | avr_pop_frame (void) | |
830 | { | |
831 | unsigned regnum; | |
832 | CORE_ADDR saddr; | |
833 | struct frame_info *frame = get_current_frame (); | |
834 | ||
2e5ff58c | 835 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
8818c391 | 836 | { |
2e5ff58c | 837 | generic_pop_dummy_frame (); |
8818c391 TR |
838 | } |
839 | else | |
840 | { | |
841 | /* TRoth: Why only loop over 8 registers? */ | |
842 | ||
843 | for (regnum = 0; regnum < 8; regnum++) | |
2e5ff58c TR |
844 | { |
845 | /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the | |
846 | actual value we want, not the address of the value we want. */ | |
847 | if (frame->saved_regs[regnum] && regnum != AVR_SP_REGNUM) | |
848 | { | |
849 | saddr = avr_make_saddr (frame->saved_regs[regnum]); | |
850 | write_register (regnum, | |
851 | read_memory_unsigned_integer (saddr, 1)); | |
852 | } | |
853 | else if (frame->saved_regs[regnum] && regnum == AVR_SP_REGNUM) | |
854 | write_register (regnum, frame->frame + 2); | |
855 | } | |
8818c391 TR |
856 | |
857 | /* Don't forget the update the PC too! */ | |
858 | write_pc (frame->extra_info->return_pc); | |
859 | } | |
860 | flush_cached_frames (); | |
861 | } | |
862 | ||
863 | /* Return the saved PC from this frame. */ | |
864 | ||
865 | static CORE_ADDR | |
866 | avr_frame_saved_pc (struct frame_info *frame) | |
867 | { | |
2e5ff58c TR |
868 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
869 | return generic_read_register_dummy (frame->pc, frame->frame, | |
870 | AVR_PC_REGNUM); | |
8818c391 TR |
871 | else |
872 | return frame->extra_info->return_pc; | |
873 | } | |
874 | ||
875 | static CORE_ADDR | |
876 | avr_saved_pc_after_call (struct frame_info *frame) | |
877 | { | |
878 | unsigned char m1, m2; | |
879 | unsigned int sp = read_register (AVR_SP_REGNUM); | |
880 | m1 = read_memory_unsigned_integer (avr_make_saddr (sp + 1), 1); | |
881 | m2 = read_memory_unsigned_integer (avr_make_saddr (sp + 2), 1); | |
882 | return (m2 | (m1 << 8)) * 2; | |
883 | } | |
884 | ||
885 | /* Figure out where in REGBUF the called function has left its return value. | |
886 | Copy that into VALBUF. */ | |
887 | ||
888 | static void | |
889 | avr_extract_return_value (struct type *type, char *regbuf, char *valbuf) | |
890 | { | |
891 | int wordsize, len; | |
892 | ||
893 | wordsize = 2; | |
894 | ||
2e5ff58c TR |
895 | len = TYPE_LENGTH (type); |
896 | ||
897 | switch (len) | |
898 | { | |
899 | case 1: /* (char) */ | |
900 | case 2: /* (short), (int) */ | |
901 | memcpy (valbuf, regbuf + REGISTER_BYTE (24), 2); | |
902 | break; | |
903 | case 4: /* (long), (float) */ | |
904 | memcpy (valbuf, regbuf + REGISTER_BYTE (22), 4); | |
905 | break; | |
906 | case 8: /* (double) (doesn't seem to happen, which is good, | |
907 | because this almost certainly isn't right. */ | |
908 | error ("I don't know how a double is returned."); | |
909 | break; | |
910 | } | |
8818c391 TR |
911 | } |
912 | ||
913 | /* Returns the return address for a dummy. */ | |
914 | ||
915 | static CORE_ADDR | |
916 | avr_call_dummy_address (void) | |
917 | { | |
918 | return entry_point_address (); | |
919 | } | |
920 | ||
921 | /* Place the appropriate value in the appropriate registers. | |
922 | Primarily used by the RETURN command. */ | |
923 | ||
924 | static void | |
925 | avr_store_return_value (struct type *type, char *valbuf) | |
926 | { | |
927 | int wordsize, len, regval; | |
2e5ff58c | 928 | |
8818c391 TR |
929 | wordsize = 2; |
930 | ||
2e5ff58c TR |
931 | len = TYPE_LENGTH (type); |
932 | switch (len) | |
933 | { | |
934 | case 1: /* char */ | |
935 | case 2: /* short, int */ | |
936 | regval = extract_address (valbuf, len); | |
937 | write_register (0, regval); | |
938 | break; | |
939 | case 4: /* long, float */ | |
940 | regval = extract_address (valbuf, len); | |
941 | write_register (0, regval >> 16); | |
942 | write_register (1, regval & 0xffff); | |
943 | break; | |
944 | case 8: /* presumeably double, but doesn't seem to happen */ | |
945 | error ("I don't know how to return a double."); | |
946 | break; | |
947 | } | |
8818c391 TR |
948 | } |
949 | ||
950 | /* Setup the return address for a dummy frame, as called by | |
951 | call_function_by_hand. Only necessary when you are using an empty | |
952 | CALL_DUMMY. */ | |
953 | ||
954 | static CORE_ADDR | |
955 | avr_push_return_address (CORE_ADDR pc, CORE_ADDR sp) | |
956 | { | |
957 | unsigned char buf[2]; | |
958 | int wordsize = 2; | |
959 | struct minimal_symbol *msymbol; | |
960 | CORE_ADDR mon_brk; | |
961 | ||
962 | fprintf_unfiltered (gdb_stderr, "avr_push_return_address() was called\n"); | |
963 | ||
964 | buf[0] = 0; | |
965 | buf[1] = 0; | |
966 | sp -= wordsize; | |
967 | write_memory (sp + 1, buf, 2); | |
968 | ||
969 | #if 0 | |
970 | /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a | |
971 | left-over from Denis' original patch which used avr-mon for the target | |
972 | instead of the generic remote target. */ | |
973 | if ((strcmp (target_shortname, "avr-mon") == 0) | |
974 | && (msymbol = lookup_minimal_symbol ("gdb_break", NULL, NULL))) | |
975 | { | |
976 | mon_brk = SYMBOL_VALUE_ADDRESS (msymbol); | |
977 | store_unsigned_integer (buf, wordsize, mon_brk / 2); | |
978 | sp -= wordsize; | |
979 | write_memory (sp + 1, buf + 1, 1); | |
980 | write_memory (sp + 2, buf, 1); | |
981 | } | |
982 | #endif | |
983 | return sp; | |
984 | } | |
985 | ||
986 | static CORE_ADDR | |
987 | avr_skip_prologue (CORE_ADDR pc) | |
988 | { | |
989 | CORE_ADDR func_addr, func_end; | |
990 | struct symtab_and_line sal; | |
991 | ||
992 | /* See what the symbol table says */ | |
993 | ||
994 | if (find_pc_partial_function (pc, NULL, &func_addr, &func_end)) | |
995 | { | |
996 | sal = find_pc_line (func_addr, 0); | |
997 | ||
2e5ff58c TR |
998 | if (sal.line != 0 && sal.end < func_end) |
999 | return sal.end; | |
8818c391 TR |
1000 | } |
1001 | ||
1002 | /* Either we didn't find the start of this function (nothing we can do), | |
1003 | or there's no line info, or the line after the prologue is after | |
1004 | the end of the function (there probably isn't a prologue). */ | |
1005 | ||
1006 | return pc; | |
1007 | } | |
1008 | ||
1009 | static CORE_ADDR | |
1010 | avr_frame_address (struct frame_info *fi) | |
1011 | { | |
1012 | return avr_make_saddr (fi->frame); | |
1013 | } | |
1014 | ||
1015 | /* Given a GDB frame, determine the address of the calling function's frame. | |
1016 | This will be used to create a new GDB frame struct, and then | |
1017 | INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. | |
1018 | ||
1019 | For us, the frame address is its stack pointer value, so we look up | |
1020 | the function prologue to determine the caller's sp value, and return it. */ | |
1021 | ||
1022 | static CORE_ADDR | |
1023 | avr_frame_chain (struct frame_info *frame) | |
1024 | { | |
1025 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) | |
1026 | { | |
1027 | /* initialize the return_pc now */ | |
1028 | frame->extra_info->return_pc = generic_read_register_dummy (frame->pc, | |
2e5ff58c TR |
1029 | frame-> |
1030 | frame, | |
1031 | AVR_PC_REGNUM); | |
8818c391 TR |
1032 | return frame->frame; |
1033 | } | |
1034 | return (frame->extra_info->is_main ? 0 | |
2e5ff58c | 1035 | : frame->frame + frame->extra_info->framesize + 2 /* ret addr */ ); |
8818c391 TR |
1036 | } |
1037 | ||
1038 | /* Store the address of the place in which to copy the structure the | |
1039 | subroutine will return. This is called from call_function. | |
1040 | ||
1041 | We store structs through a pointer passed in the first Argument | |
1042 | register. */ | |
1043 | ||
1044 | static void | |
1045 | avr_store_struct_return (CORE_ADDR addr, CORE_ADDR sp) | |
1046 | { | |
1047 | write_register (0, addr); | |
1048 | } | |
1049 | ||
1050 | /* Extract from an array REGBUF containing the (raw) register state | |
1051 | the address in which a function should return its structure value, | |
1052 | as a CORE_ADDR (or an expression that can be used as one). */ | |
1053 | ||
1054 | static CORE_ADDR | |
1055 | avr_extract_struct_value_address (char *regbuf) | |
1056 | { | |
1057 | return (extract_address ((regbuf) + REGISTER_BYTE (0), | |
2e5ff58c | 1058 | REGISTER_RAW_SIZE (0)) | AVR_SMEM_START); |
8818c391 TR |
1059 | } |
1060 | ||
1061 | /* Setup the function arguments for calling a function in the inferior. | |
1062 | ||
1063 | On the AVR architecture, there are 18 registers (R25 to R8) which are | |
1064 | dedicated for passing function arguments. Up to the first 18 arguments | |
1065 | (depending on size) may go into these registers. The rest go on the stack. | |
1066 | ||
1067 | Arguments that are larger than WORDSIZE bytes will be split between two or | |
1068 | more registers as available, but will NOT be split between a register and | |
1069 | the stack. | |
1070 | ||
1071 | An exceptional case exists for struct arguments (and possibly other | |
1072 | aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but | |
1073 | not a multiple of WORDSIZE bytes. In this case the argument is never split | |
1074 | between the registers and the stack, but instead is copied in its entirety | |
1075 | onto the stack, AND also copied into as many registers as there is room | |
1076 | for. In other words, space in registers permitting, two copies of the same | |
1077 | argument are passed in. As far as I can tell, only the one on the stack is | |
1078 | used, although that may be a function of the level of compiler | |
1079 | optimization. I suspect this is a compiler bug. Arguments of these odd | |
1080 | sizes are left-justified within the word (as opposed to arguments smaller | |
1081 | than WORDSIZE bytes, which are right-justified). | |
1082 | ||
1083 | If the function is to return an aggregate type such as a struct, the caller | |
1084 | must allocate space into which the callee will copy the return value. In | |
1085 | this case, a pointer to the return value location is passed into the callee | |
1086 | in register R0, which displaces one of the other arguments passed in via | |
1087 | registers R0 to R2. */ | |
1088 | ||
1089 | static CORE_ADDR | |
1090 | avr_push_arguments (int nargs, struct value **args, CORE_ADDR sp, | |
2e5ff58c | 1091 | int struct_return, CORE_ADDR struct_addr) |
8818c391 TR |
1092 | { |
1093 | int stack_alloc, stack_offset; | |
1094 | int wordsize; | |
1095 | int argreg; | |
1096 | int argnum; | |
1097 | struct type *type; | |
1098 | CORE_ADDR regval; | |
1099 | char *val; | |
1100 | char valbuf[4]; | |
1101 | int len; | |
1102 | ||
2e5ff58c | 1103 | wordsize = 1; |
8818c391 TR |
1104 | #if 0 |
1105 | /* Now make sure there's space on the stack */ | |
2e5ff58c TR |
1106 | for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++) |
1107 | stack_alloc += TYPE_LENGTH (VALUE_TYPE (args[argnum])); | |
1108 | sp -= stack_alloc; /* make room on stack for args */ | |
8818c391 TR |
1109 | /* we may over-allocate a little here, but that won't hurt anything */ |
1110 | #endif | |
1111 | argreg = 25; | |
2e5ff58c | 1112 | if (struct_return) /* "struct return" pointer takes up one argreg */ |
8818c391 TR |
1113 | { |
1114 | write_register (--argreg, struct_addr); | |
1115 | } | |
1116 | ||
1117 | /* Now load as many as possible of the first arguments into registers, and | |
1118 | push the rest onto the stack. There are 3N bytes in three registers | |
1119 | available. Loop thru args from first to last. */ | |
1120 | ||
1121 | for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++) | |
1122 | { | |
1123 | type = VALUE_TYPE (args[argnum]); | |
1124 | len = TYPE_LENGTH (type); | |
1125 | val = (char *) VALUE_CONTENTS (args[argnum]); | |
1126 | ||
1127 | /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because | |
1128 | some *&^%$ things get passed on the stack AND in the registers! */ | |
1129 | while (len > 0) | |
2e5ff58c TR |
1130 | { /* there's room in registers */ |
1131 | len -= wordsize; | |
1132 | regval = extract_address (val + len, wordsize); | |
1133 | write_register (argreg--, regval); | |
1134 | } | |
8818c391 TR |
1135 | } |
1136 | return sp; | |
1137 | } | |
1138 | ||
1139 | /* Initialize the gdbarch structure for the AVR's. */ | |
1140 | ||
1141 | static struct gdbarch * | |
2e5ff58c TR |
1142 | avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) |
1143 | { | |
8818c391 TR |
1144 | /* FIXME: TRoth/2002-02-18: I have no idea if avr_call_dummy_words[] should |
1145 | be bigger or not. Initial testing seems to show that `call my_func()` | |
1146 | works and backtrace from a breakpoint within the call looks correct. | |
1147 | Admittedly, I haven't tested with more than a very simple program. */ | |
2e5ff58c | 1148 | static LONGEST avr_call_dummy_words[] = { 0 }; |
8818c391 | 1149 | |
2e5ff58c TR |
1150 | struct gdbarch *gdbarch; |
1151 | struct gdbarch_tdep *tdep; | |
8818c391 TR |
1152 | |
1153 | /* Find a candidate among the list of pre-declared architectures. */ | |
1154 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
1155 | if (arches != NULL) | |
1156 | return arches->gdbarch; | |
1157 | ||
1158 | /* None found, create a new architecture from the information provided. */ | |
1159 | tdep = XMALLOC (struct gdbarch_tdep); | |
1160 | gdbarch = gdbarch_alloc (&info, tdep); | |
1161 | ||
1162 | /* If we ever need to differentiate the device types, do it here. */ | |
1163 | switch (info.bfd_arch_info->mach) | |
1164 | { | |
1165 | case bfd_mach_avr1: | |
1166 | case bfd_mach_avr2: | |
1167 | case bfd_mach_avr3: | |
1168 | case bfd_mach_avr4: | |
1169 | case bfd_mach_avr5: | |
1170 | break; | |
1171 | } | |
1172 | ||
1173 | set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT); | |
1174 | set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT); | |
1175 | set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
1176 | set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT); | |
1177 | set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT); | |
1178 | set_gdbarch_addr_bit (gdbarch, 32); | |
2e5ff58c | 1179 | set_gdbarch_bfd_vma_bit (gdbarch, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */ |
8818c391 TR |
1180 | |
1181 | set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
1182 | set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
1183 | set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT); | |
1184 | ||
1185 | set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little); | |
1186 | set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little); | |
1187 | set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_single_little); | |
1188 | ||
1189 | set_gdbarch_read_pc (gdbarch, avr_read_pc); | |
1190 | set_gdbarch_write_pc (gdbarch, avr_write_pc); | |
1191 | set_gdbarch_read_fp (gdbarch, avr_read_fp); | |
1192 | set_gdbarch_read_sp (gdbarch, avr_read_sp); | |
1193 | set_gdbarch_write_sp (gdbarch, avr_write_sp); | |
1194 | ||
1195 | set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS); | |
1196 | ||
1197 | set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM); | |
1198 | set_gdbarch_fp_regnum (gdbarch, AVR_FP_REGNUM); | |
1199 | set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM); | |
1200 | ||
1201 | set_gdbarch_register_name (gdbarch, avr_register_name); | |
1202 | set_gdbarch_register_size (gdbarch, 1); | |
1203 | set_gdbarch_register_bytes (gdbarch, AVR_NUM_REG_BYTES); | |
1204 | set_gdbarch_register_byte (gdbarch, avr_register_byte); | |
1205 | set_gdbarch_register_raw_size (gdbarch, avr_register_raw_size); | |
1206 | set_gdbarch_max_register_raw_size (gdbarch, 4); | |
1207 | set_gdbarch_register_virtual_size (gdbarch, avr_register_virtual_size); | |
1208 | set_gdbarch_max_register_virtual_size (gdbarch, 4); | |
1209 | set_gdbarch_register_virtual_type (gdbarch, avr_register_virtual_type); | |
1210 | ||
1211 | /* We might need to define our own here or define FRAME_INIT_SAVED_REGS */ | |
1212 | set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register); | |
1213 | ||
1214 | set_gdbarch_print_insn (gdbarch, print_insn_avr); | |
1215 | ||
1216 | set_gdbarch_use_generic_dummy_frames (gdbarch, 1); | |
1217 | set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT); | |
1218 | set_gdbarch_call_dummy_address (gdbarch, avr_call_dummy_address); | |
1219 | set_gdbarch_call_dummy_start_offset (gdbarch, 0); | |
1220 | set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1); | |
1221 | set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0); | |
1222 | set_gdbarch_call_dummy_length (gdbarch, 0); | |
1223 | set_gdbarch_pc_in_call_dummy (gdbarch, generic_pc_in_call_dummy); | |
1224 | set_gdbarch_call_dummy_p (gdbarch, 1); | |
1225 | set_gdbarch_call_dummy_words (gdbarch, avr_call_dummy_words); | |
1226 | set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0); | |
1227 | set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy); | |
1228 | ||
1229 | /* set_gdbarch_believe_pcc_promotion (gdbarch, 1); // TRoth: should this be set? */ | |
1230 | ||
1231 | set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer); | |
1232 | set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address); | |
26e9b323 | 1233 | set_gdbarch_deprecated_extract_return_value (gdbarch, avr_extract_return_value); |
8818c391 TR |
1234 | set_gdbarch_push_arguments (gdbarch, avr_push_arguments); |
1235 | set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame); | |
1236 | /* set_gdbarch_push_return_address (gdbarch, avr_push_return_address); */ | |
1237 | set_gdbarch_pop_frame (gdbarch, avr_pop_frame); | |
1238 | ||
1239 | set_gdbarch_store_return_value (gdbarch, avr_store_return_value); | |
1240 | ||
1241 | set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention); | |
1242 | set_gdbarch_store_struct_return (gdbarch, avr_store_struct_return); | |
26e9b323 AC |
1243 | set_gdbarch_deprecated_extract_struct_value_address |
1244 | (gdbarch, avr_extract_struct_value_address); | |
8818c391 TR |
1245 | |
1246 | set_gdbarch_frame_init_saved_regs (gdbarch, avr_scan_prologue); | |
1247 | set_gdbarch_init_extra_frame_info (gdbarch, avr_init_extra_frame_info); | |
1248 | set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue); | |
1249 | /* set_gdbarch_prologue_frameless_p (gdbarch, avr_prologue_frameless_p); */ | |
1250 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
1251 | ||
1252 | set_gdbarch_decr_pc_after_break (gdbarch, 0); | |
1253 | ||
1254 | set_gdbarch_function_start_offset (gdbarch, 0); | |
2e5ff58c TR |
1255 | set_gdbarch_remote_translate_xfer_address (gdbarch, |
1256 | avr_remote_translate_xfer_address); | |
8818c391 | 1257 | set_gdbarch_frame_args_skip (gdbarch, 0); |
2e5ff58c | 1258 | set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue); /* ??? */ |
8818c391 TR |
1259 | set_gdbarch_frame_chain (gdbarch, avr_frame_chain); |
1260 | set_gdbarch_frame_chain_valid (gdbarch, generic_func_frame_chain_valid); | |
1261 | set_gdbarch_frame_saved_pc (gdbarch, avr_frame_saved_pc); | |
1262 | set_gdbarch_frame_args_address (gdbarch, avr_frame_address); | |
1263 | set_gdbarch_frame_locals_address (gdbarch, avr_frame_address); | |
1264 | set_gdbarch_saved_pc_after_call (gdbarch, avr_saved_pc_after_call); | |
1265 | set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown); | |
1266 | ||
2e5ff58c TR |
1267 | set_gdbarch_convert_from_func_ptr_addr (gdbarch, |
1268 | avr_convert_from_func_ptr_addr); | |
8818c391 TR |
1269 | |
1270 | return gdbarch; | |
1271 | } | |
1272 | ||
1273 | /* Send a query request to the avr remote target asking for values of the io | |
1274 | registers. If args parameter is not NULL, then the user has requested info | |
1275 | on a specific io register [This still needs implemented and is ignored for | |
1276 | now]. The query string should be one of these forms: | |
1277 | ||
1278 | "Ravr.io_reg" -> reply is "NN" number of io registers | |
1279 | ||
1280 | "Ravr.io_reg:addr,len" where addr is first register and len is number of | |
1281 | registers to be read. The reply should be "<NAME>,VV;" for each io register | |
1282 | where, <NAME> is a string, and VV is the hex value of the register. | |
1283 | ||
1284 | All io registers are 8-bit. */ | |
1285 | ||
1286 | static void | |
1287 | avr_io_reg_read_command (char *args, int from_tty) | |
1288 | { | |
2e5ff58c TR |
1289 | int bufsiz = 0; |
1290 | char buf[400]; | |
1291 | char query[400]; | |
1292 | char *p; | |
1293 | unsigned int nreg = 0; | |
1294 | unsigned int val; | |
1295 | int i, j, k, step; | |
8818c391 TR |
1296 | |
1297 | /* fprintf_unfiltered (gdb_stderr, "DEBUG: avr_io_reg_read_command (\"%s\", %d)\n", */ | |
1298 | /* args, from_tty); */ | |
1299 | ||
2e5ff58c | 1300 | if (!current_target.to_query) |
8818c391 | 1301 | { |
2e5ff58c TR |
1302 | fprintf_unfiltered (gdb_stderr, |
1303 | "ERR: info io_registers NOT supported by current target\n"); | |
8818c391 TR |
1304 | return; |
1305 | } | |
1306 | ||
1307 | /* Just get the maximum buffer size. */ | |
1308 | target_query ((int) 'R', 0, 0, &bufsiz); | |
2e5ff58c TR |
1309 | if (bufsiz > sizeof (buf)) |
1310 | bufsiz = sizeof (buf); | |
8818c391 TR |
1311 | |
1312 | /* Find out how many io registers the target has. */ | |
1313 | strcpy (query, "avr.io_reg"); | |
2e5ff58c | 1314 | target_query ((int) 'R', query, buf, &bufsiz); |
8818c391 TR |
1315 | |
1316 | if (strncmp (buf, "", bufsiz) == 0) | |
1317 | { | |
2e5ff58c TR |
1318 | fprintf_unfiltered (gdb_stderr, |
1319 | "info io_registers NOT supported by target\n"); | |
8818c391 TR |
1320 | return; |
1321 | } | |
1322 | ||
2e5ff58c | 1323 | if (sscanf (buf, "%x", &nreg) != 1) |
8818c391 | 1324 | { |
2e5ff58c TR |
1325 | fprintf_unfiltered (gdb_stderr, |
1326 | "Error fetching number of io registers\n"); | |
8818c391 TR |
1327 | return; |
1328 | } | |
1329 | ||
2e5ff58c | 1330 | reinitialize_more_filter (); |
8818c391 TR |
1331 | |
1332 | printf_unfiltered ("Target has %u io registers:\n\n", nreg); | |
1333 | ||
1334 | /* only fetch up to 8 registers at a time to keep the buffer small */ | |
1335 | step = 8; | |
1336 | ||
2e5ff58c | 1337 | for (i = 0; i < nreg; i += step) |
8818c391 | 1338 | { |
2e5ff58c | 1339 | j = step - (nreg % step); /* how many registers this round? */ |
8818c391 | 1340 | |
2e5ff58c | 1341 | snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j); |
8818c391 TR |
1342 | target_query ((int) 'R', query, buf, &bufsiz); |
1343 | ||
1344 | p = buf; | |
2e5ff58c TR |
1345 | for (k = i; k < (i + j); k++) |
1346 | { | |
1347 | if (sscanf (p, "%[^,],%x;", query, &val) == 2) | |
1348 | { | |
1349 | printf_filtered ("[%02x] %-15s : %02x\n", k, query, val); | |
1350 | while ((*p != ';') && (*p != '\0')) | |
1351 | p++; | |
1352 | p++; /* skip over ';' */ | |
1353 | if (*p == '\0') | |
1354 | break; | |
1355 | } | |
1356 | } | |
8818c391 TR |
1357 | } |
1358 | } | |
1359 | ||
1360 | void | |
1361 | _initialize_avr_tdep (void) | |
1362 | { | |
1363 | register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init); | |
1364 | ||
1365 | /* Add a new command to allow the user to query the avr remote target for | |
1366 | the values of the io space registers in a saner way than just using | |
1367 | `x/NNNb ADDR`. */ | |
1368 | ||
1369 | /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr | |
1370 | io_registers' to signify it is not available on other platforms. */ | |
1371 | ||
1372 | add_cmd ("io_registers", class_info, avr_io_reg_read_command, | |
2e5ff58c | 1373 | "query remote avr target for io space register values", &infolist); |
8818c391 | 1374 | } |