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