GDB (API)
/home/stan/gdb/src/gdb/avr-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for Atmel AVR, for GDB.
00002 
00003    Copyright (C) 1996-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 /* Contributed by Theodore A. Roth, troth@openavr.org */
00021 
00022 /* Portions of this file were taken from the original gdb-4.18 patch developed
00023    by Denis Chertykov, denisc@overta.ru */
00024 
00025 #include "defs.h"
00026 #include "frame.h"
00027 #include "frame-unwind.h"
00028 #include "frame-base.h"
00029 #include "trad-frame.h"
00030 #include "gdbcmd.h"
00031 #include "gdbcore.h"
00032 #include "gdbtypes.h"
00033 #include "inferior.h"
00034 #include "symfile.h"
00035 #include "arch-utils.h"
00036 #include "regcache.h"
00037 #include "gdb_string.h"
00038 #include "dis-asm.h"
00039 
00040 /* AVR Background:
00041 
00042    (AVR micros are pure Harvard Architecture processors.)
00043 
00044    The AVR family of microcontrollers have three distinctly different memory
00045    spaces: flash, sram and eeprom.  The flash is 16 bits wide and is used for
00046    the most part to store program instructions.  The sram is 8 bits wide and is
00047    used for the stack and the heap.  Some devices lack sram and some can have
00048    an additional external sram added on as a peripheral.
00049 
00050    The eeprom is 8 bits wide and is used to store data when the device is
00051    powered down.  Eeprom is not directly accessible, it can only be accessed
00052    via io-registers using a special algorithm.  Accessing eeprom via gdb's
00053    remote serial protocol ('m' or 'M' packets) looks difficult to do and is
00054    not included at this time.
00055 
00056    [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or
00057    written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''.  For this to
00058    work, the remote target must be able to handle eeprom accesses and perform
00059    the address translation.]
00060 
00061    All three memory spaces have physical addresses beginning at 0x0.  In
00062    addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit
00063    bytes instead of the 16 bit wide words used by the real device for the
00064    Program Counter.
00065 
00066    In order for remote targets to work correctly, extra bits must be added to
00067    addresses before they are send to the target or received from the target
00068    via the remote serial protocol.  The extra bits are the MSBs and are used to
00069    decode which memory space the address is referring to.  */
00070 
00071 /* Constants: prefixed with AVR_ to avoid name space clashes */
00072 
00073 enum
00074 {
00075   AVR_REG_W = 24,
00076   AVR_REG_X = 26,
00077   AVR_REG_Y = 28,
00078   AVR_FP_REGNUM = 28,
00079   AVR_REG_Z = 30,
00080 
00081   AVR_SREG_REGNUM = 32,
00082   AVR_SP_REGNUM = 33,
00083   AVR_PC_REGNUM = 34,
00084 
00085   AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
00086   AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
00087 
00088   /* Pseudo registers.  */
00089   AVR_PSEUDO_PC_REGNUM = 35,
00090   AVR_NUM_PSEUDO_REGS = 1,
00091 
00092   AVR_PC_REG_INDEX = 35,        /* index into array of registers */
00093 
00094   AVR_MAX_PROLOGUE_SIZE = 64,   /* bytes */
00095 
00096   /* Count of pushed registers.  From r2 to r17 (inclusively), r28, r29 */
00097   AVR_MAX_PUSHES = 18,
00098 
00099   /* Number of the last pushed register.  r17 for current avr-gcc */
00100   AVR_LAST_PUSHED_REGNUM = 17,
00101 
00102   AVR_ARG1_REGNUM = 24,         /* Single byte argument */
00103   AVR_ARGN_REGNUM = 25,         /* Multi byte argments */
00104 
00105   AVR_RET1_REGNUM = 24,         /* Single byte return value */
00106   AVR_RETN_REGNUM = 25,         /* Multi byte return value */
00107 
00108   /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8
00109      bits?  Do these have to match the bfd vma values?  It sure would make
00110      things easier in the future if they didn't need to match.
00111 
00112      Note: I chose these values so as to be consistent with bfd vma
00113      addresses.
00114 
00115      TRoth/2002-04-08: There is already a conflict with very large programs
00116      in the mega128.  The mega128 has 128K instruction bytes (64K words),
00117      thus the Most Significant Bit is 0x10000 which gets masked off my
00118      AVR_MEM_MASK.
00119 
00120      The problem manifests itself when trying to set a breakpoint in a
00121      function which resides in the upper half of the instruction space and
00122      thus requires a 17-bit address.
00123 
00124      For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK
00125      from 0x00ff0000 to 0x00f00000.  Eeprom is not accessible from gdb yet,
00126      but could be for some remote targets by just adding the correct offset
00127      to the address and letting the remote target handle the low-level
00128      details of actually accessing the eeprom.  */
00129 
00130   AVR_IMEM_START = 0x00000000,  /* INSN memory */
00131   AVR_SMEM_START = 0x00800000,  /* SRAM memory */
00132 #if 1
00133   /* No eeprom mask defined */
00134   AVR_MEM_MASK = 0x00f00000,    /* mask to determine memory space */
00135 #else
00136   AVR_EMEM_START = 0x00810000,  /* EEPROM memory */
00137   AVR_MEM_MASK = 0x00ff0000,    /* mask to determine memory space */
00138 #endif
00139 };
00140 
00141 /* Prologue types:
00142 
00143    NORMAL and CALL are the typical types (the -mcall-prologues gcc option
00144    causes the generation of the CALL type prologues).  */
00145 
00146 enum {
00147     AVR_PROLOGUE_NONE,              /* No prologue */
00148     AVR_PROLOGUE_NORMAL,
00149     AVR_PROLOGUE_CALL,              /* -mcall-prologues */
00150     AVR_PROLOGUE_MAIN,
00151     AVR_PROLOGUE_INTR,              /* interrupt handler */
00152     AVR_PROLOGUE_SIG,               /* signal handler */
00153 };
00154 
00155 /* Any function with a frame looks like this
00156    .......    <-SP POINTS HERE
00157    LOCALS1    <-FP POINTS HERE
00158    LOCALS0
00159    SAVED FP
00160    SAVED R3
00161    SAVED R2
00162    RET PC
00163    FIRST ARG
00164    SECOND ARG */
00165 
00166 struct avr_unwind_cache
00167 {
00168   /* The previous frame's inner most stack address.  Used as this
00169      frame ID's stack_addr.  */
00170   CORE_ADDR prev_sp;
00171   /* The frame's base, optionally used by the high-level debug info.  */
00172   CORE_ADDR base;
00173   int size;
00174   int prologue_type;
00175   /* Table indicating the location of each and every register.  */
00176   struct trad_frame_saved_reg *saved_regs;
00177 };
00178 
00179 struct gdbarch_tdep
00180 {
00181   /* Number of bytes stored to the stack by call instructions.
00182      2 bytes for avr1-5, 3 bytes for avr6.  */
00183   int call_length;
00184 
00185   /* Type for void.  */
00186   struct type *void_type;
00187   /* Type for a function returning void.  */
00188   struct type *func_void_type;
00189   /* Type for a pointer to a function.  Used for the type of PC.  */
00190   struct type *pc_type;
00191 };
00192 
00193 /* Lookup the name of a register given it's number.  */
00194 
00195 static const char *
00196 avr_register_name (struct gdbarch *gdbarch, int regnum)
00197 {
00198   static const char * const register_names[] = {
00199     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
00200     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
00201     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
00202     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
00203     "SREG", "SP", "PC2",
00204     "pc"
00205   };
00206   if (regnum < 0)
00207     return NULL;
00208   if (regnum >= (sizeof (register_names) / sizeof (*register_names)))
00209     return NULL;
00210   return register_names[regnum];
00211 }
00212 
00213 /* Return the GDB type object for the "standard" data type
00214    of data in register N.  */
00215 
00216 static struct type *
00217 avr_register_type (struct gdbarch *gdbarch, int reg_nr)
00218 {
00219   if (reg_nr == AVR_PC_REGNUM)
00220     return builtin_type (gdbarch)->builtin_uint32;
00221   if (reg_nr == AVR_PSEUDO_PC_REGNUM)
00222     return gdbarch_tdep (gdbarch)->pc_type;
00223   if (reg_nr == AVR_SP_REGNUM)
00224     return builtin_type (gdbarch)->builtin_data_ptr;
00225   return builtin_type (gdbarch)->builtin_uint8;
00226 }
00227 
00228 /* Instruction address checks and convertions.  */
00229 
00230 static CORE_ADDR
00231 avr_make_iaddr (CORE_ADDR x)
00232 {
00233   return ((x) | AVR_IMEM_START);
00234 }
00235 
00236 /* FIXME: TRoth: Really need to use a larger mask for instructions.  Some
00237    devices are already up to 128KBytes of flash space.
00238 
00239    TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined.  */
00240 
00241 static CORE_ADDR
00242 avr_convert_iaddr_to_raw (CORE_ADDR x)
00243 {
00244   return ((x) & 0xffffffff);
00245 }
00246 
00247 /* SRAM address checks and convertions.  */
00248 
00249 static CORE_ADDR
00250 avr_make_saddr (CORE_ADDR x)
00251 {
00252   /* Return 0 for NULL.  */
00253   if (x == 0)
00254     return 0;
00255 
00256   return ((x) | AVR_SMEM_START);
00257 }
00258 
00259 static CORE_ADDR
00260 avr_convert_saddr_to_raw (CORE_ADDR x)
00261 {
00262   return ((x) & 0xffffffff);
00263 }
00264 
00265 /* EEPROM address checks and convertions.  I don't know if these will ever
00266    actually be used, but I've added them just the same.  TRoth */
00267 
00268 /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
00269    programs in the mega128.  */
00270 
00271 /*  static CORE_ADDR */
00272 /*  avr_make_eaddr (CORE_ADDR x) */
00273 /*  { */
00274 /*    return ((x) | AVR_EMEM_START); */
00275 /*  } */
00276 
00277 /*  static int */
00278 /*  avr_eaddr_p (CORE_ADDR x) */
00279 /*  { */
00280 /*    return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
00281 /*  } */
00282 
00283 /*  static CORE_ADDR */
00284 /*  avr_convert_eaddr_to_raw (CORE_ADDR x) */
00285 /*  { */
00286 /*    return ((x) & 0xffffffff); */
00287 /*  } */
00288 
00289 /* Convert from address to pointer and vice-versa.  */
00290 
00291 static void
00292 avr_address_to_pointer (struct gdbarch *gdbarch,
00293                         struct type *type, gdb_byte *buf, CORE_ADDR addr)
00294 {
00295   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00296 
00297   /* Is it a code address?  */
00298   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
00299       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
00300     {
00301       store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
00302                               avr_convert_iaddr_to_raw (addr >> 1));
00303     }
00304   else
00305     {
00306       /* Strip off any upper segment bits.  */
00307       store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
00308                               avr_convert_saddr_to_raw (addr));
00309     }
00310 }
00311 
00312 static CORE_ADDR
00313 avr_pointer_to_address (struct gdbarch *gdbarch,
00314                         struct type *type, const gdb_byte *buf)
00315 {
00316   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00317   CORE_ADDR addr
00318     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
00319 
00320   /* Is it a code address?  */
00321   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
00322       || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
00323       || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
00324     return avr_make_iaddr (addr << 1);
00325   else
00326     return avr_make_saddr (addr);
00327 }
00328 
00329 static CORE_ADDR
00330 avr_integer_to_address (struct gdbarch *gdbarch,
00331                         struct type *type, const gdb_byte *buf)
00332 {
00333   ULONGEST addr = unpack_long (type, buf);
00334 
00335   return avr_make_saddr (addr);
00336 }
00337 
00338 static CORE_ADDR
00339 avr_read_pc (struct regcache *regcache)
00340 {
00341   ULONGEST pc;
00342   regcache_cooked_read_unsigned (regcache, AVR_PC_REGNUM, &pc);
00343   return avr_make_iaddr (pc);
00344 }
00345 
00346 static void
00347 avr_write_pc (struct regcache *regcache, CORE_ADDR val)
00348 {
00349   regcache_cooked_write_unsigned (regcache, AVR_PC_REGNUM,
00350                                   avr_convert_iaddr_to_raw (val));
00351 }
00352 
00353 static enum register_status
00354 avr_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
00355                           int regnum, gdb_byte *buf)
00356 {
00357   ULONGEST val;
00358   enum register_status status;
00359 
00360   switch (regnum)
00361     {
00362     case AVR_PSEUDO_PC_REGNUM:
00363       status = regcache_raw_read_unsigned (regcache, AVR_PC_REGNUM, &val);
00364       if (status != REG_VALID)
00365         return status;
00366       val >>= 1;
00367       store_unsigned_integer (buf, 4, gdbarch_byte_order (gdbarch), val);
00368       return status;
00369     default:
00370       internal_error (__FILE__, __LINE__, _("invalid regnum"));
00371     }
00372 }
00373 
00374 static void
00375 avr_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
00376                            int regnum, const gdb_byte *buf)
00377 {
00378   ULONGEST val;
00379 
00380   switch (regnum)
00381     {
00382     case AVR_PSEUDO_PC_REGNUM:
00383       val = extract_unsigned_integer (buf, 4, gdbarch_byte_order (gdbarch));
00384       val <<= 1;
00385       regcache_raw_write_unsigned (regcache, AVR_PC_REGNUM, val);
00386       break;
00387     default:
00388       internal_error (__FILE__, __LINE__, _("invalid regnum"));
00389     }
00390 }
00391 
00392 /* Function: avr_scan_prologue
00393 
00394    This function decodes an AVR function prologue to determine:
00395      1) the size of the stack frame
00396      2) which registers are saved on it
00397      3) the offsets of saved regs
00398    This information is stored in the avr_unwind_cache structure.
00399 
00400    Some devices lack the sbiw instruction, so on those replace this:
00401         sbiw    r28, XX
00402    with this:
00403         subi    r28,lo8(XX)
00404         sbci    r29,hi8(XX)
00405 
00406    A typical AVR function prologue with a frame pointer might look like this:
00407         push    rXX        ; saved regs
00408         ...
00409         push    r28
00410         push    r29
00411         in      r28,__SP_L__
00412         in      r29,__SP_H__
00413         sbiw    r28,<LOCALS_SIZE>
00414         in      __tmp_reg__,__SREG__
00415         cli
00416         out     __SP_H__,r29
00417         out     __SREG__,__tmp_reg__
00418         out     __SP_L__,r28
00419 
00420    A typical AVR function prologue without a frame pointer might look like
00421    this:
00422         push    rXX        ; saved regs
00423         ...
00424 
00425    A main function prologue looks like this:
00426         ldi     r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
00427         ldi     r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
00428         out     __SP_H__,r29
00429         out     __SP_L__,r28
00430 
00431    A signal handler prologue looks like this:
00432         push    __zero_reg__
00433         push    __tmp_reg__
00434         in      __tmp_reg__, __SREG__
00435         push    __tmp_reg__
00436         clr     __zero_reg__
00437         push    rXX             ; save registers r18:r27, r30:r31
00438         ...
00439         push    r28             ; save frame pointer
00440         push    r29
00441         in      r28, __SP_L__
00442         in      r29, __SP_H__
00443         sbiw    r28, <LOCALS_SIZE>
00444         out     __SP_H__, r29
00445         out     __SP_L__, r28
00446         
00447    A interrupt handler prologue looks like this:
00448         sei
00449         push    __zero_reg__
00450         push    __tmp_reg__
00451         in      __tmp_reg__, __SREG__
00452         push    __tmp_reg__
00453         clr     __zero_reg__
00454         push    rXX             ; save registers r18:r27, r30:r31
00455         ...
00456         push    r28             ; save frame pointer
00457         push    r29
00458         in      r28, __SP_L__
00459         in      r29, __SP_H__
00460         sbiw    r28, <LOCALS_SIZE>
00461         cli
00462         out     __SP_H__, r29
00463         sei     
00464         out     __SP_L__, r28
00465 
00466    A `-mcall-prologues' prologue looks like this (Note that the megas use a
00467    jmp instead of a rjmp, thus the prologue is one word larger since jmp is a
00468    32 bit insn and rjmp is a 16 bit insn):
00469         ldi     r26,lo8(<LOCALS_SIZE>)
00470         ldi     r27,hi8(<LOCALS_SIZE>)
00471         ldi     r30,pm_lo8(.L_foo_body)
00472         ldi     r31,pm_hi8(.L_foo_body)
00473         rjmp    __prologue_saves__+RRR
00474         .L_foo_body:  */
00475 
00476 /* Not really part of a prologue, but still need to scan for it, is when a
00477    function prologue moves values passed via registers as arguments to new
00478    registers.  In this case, all local variables live in registers, so there
00479    may be some register saves.  This is what it looks like:
00480         movw    rMM, rNN
00481         ...
00482 
00483    There could be multiple movw's.  If the target doesn't have a movw insn, it
00484    will use two mov insns.  This could be done after any of the above prologue
00485    types.  */
00486 
00487 static CORE_ADDR
00488 avr_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR pc_beg, CORE_ADDR pc_end,
00489                    struct avr_unwind_cache *info)
00490 {
00491   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00492   int i;
00493   unsigned short insn;
00494   int scan_stage = 0;
00495   struct minimal_symbol *msymbol;
00496   unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
00497   int vpc = 0;
00498   int len;
00499 
00500   len = pc_end - pc_beg;
00501   if (len > AVR_MAX_PROLOGUE_SIZE)
00502     len = AVR_MAX_PROLOGUE_SIZE;
00503 
00504   /* FIXME: TRoth/2003-06-11: This could be made more efficient by only
00505      reading in the bytes of the prologue.  The problem is that the figuring
00506      out where the end of the prologue is is a bit difficult.  The old code 
00507      tried to do that, but failed quite often.  */
00508   read_memory (pc_beg, prologue, len);
00509 
00510   /* Scanning main()'s prologue
00511      ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
00512      ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
00513      out __SP_H__,r29
00514      out __SP_L__,r28 */
00515 
00516   if (len >= 4)
00517     {
00518       CORE_ADDR locals;
00519       static const unsigned char img[] = {
00520         0xde, 0xbf,             /* out __SP_H__,r29 */
00521         0xcd, 0xbf              /* out __SP_L__,r28 */
00522       };
00523 
00524       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00525       /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
00526       if ((insn & 0xf0f0) == 0xe0c0)
00527         {
00528           locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
00529           insn = extract_unsigned_integer (&prologue[vpc + 2], 2, byte_order);
00530           /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
00531           if ((insn & 0xf0f0) == 0xe0d0)
00532             {
00533               locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
00534               if (vpc + 4 + sizeof (img) < len
00535                   && memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
00536                 {
00537                   info->prologue_type = AVR_PROLOGUE_MAIN;
00538                   info->base = locals;
00539                   return pc_beg + 4;
00540                 }
00541             }
00542         }
00543     }
00544 
00545   /* Scanning `-mcall-prologues' prologue
00546      Classic prologue is 10 bytes, mega prologue is a 12 bytes long */
00547 
00548   while (1)     /* Using a while to avoid many goto's */
00549     {
00550       int loc_size;
00551       int body_addr;
00552       unsigned num_pushes;
00553       int pc_offset = 0;
00554 
00555       /* At least the fifth instruction must have been executed to
00556          modify frame shape.  */
00557       if (len < 10)
00558         break;
00559 
00560       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00561       /* ldi r26,<LOCALS_SIZE> */
00562       if ((insn & 0xf0f0) != 0xe0a0)
00563         break;
00564       loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
00565       pc_offset += 2;
00566 
00567       insn = extract_unsigned_integer (&prologue[vpc + 2], 2, byte_order);
00568       /* ldi r27,<LOCALS_SIZE> / 256 */
00569       if ((insn & 0xf0f0) != 0xe0b0)
00570         break;
00571       loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
00572       pc_offset += 2;
00573 
00574       insn = extract_unsigned_integer (&prologue[vpc + 4], 2, byte_order);
00575       /* ldi r30,pm_lo8(.L_foo_body) */
00576       if ((insn & 0xf0f0) != 0xe0e0)
00577         break;
00578       body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
00579       pc_offset += 2;
00580 
00581       insn = extract_unsigned_integer (&prologue[vpc + 6], 2, byte_order);
00582       /* ldi r31,pm_hi8(.L_foo_body) */
00583       if ((insn & 0xf0f0) != 0xe0f0)
00584         break;
00585       body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
00586       pc_offset += 2;
00587 
00588       msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
00589       if (!msymbol)
00590         break;
00591 
00592       insn = extract_unsigned_integer (&prologue[vpc + 8], 2, byte_order);
00593       /* rjmp __prologue_saves__+RRR */
00594       if ((insn & 0xf000) == 0xc000)
00595         {
00596           /* Extract PC relative offset from RJMP */
00597           i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
00598           /* Convert offset to byte addressable mode */
00599           i *= 2;
00600           /* Destination address */
00601           i += pc_beg + 10;
00602 
00603           if (body_addr != (pc_beg + 10)/2)
00604             break;
00605 
00606           pc_offset += 2;
00607         }
00608       else if ((insn & 0xfe0e) == 0x940c)
00609         {
00610           /* Extract absolute PC address from JMP */
00611           i = (((insn & 0x1) | ((insn & 0x1f0) >> 3) << 16)
00612                | (extract_unsigned_integer (&prologue[vpc + 10], 2, byte_order)
00613                   & 0xffff));
00614           /* Convert address to byte addressable mode */
00615           i *= 2;
00616 
00617           if (body_addr != (pc_beg + 12)/2)
00618             break;
00619 
00620           pc_offset += 4;
00621         }
00622       else
00623         break;
00624 
00625       /* Resolve offset (in words) from __prologue_saves__ symbol.
00626          Which is a pushes count in `-mcall-prologues' mode */
00627       num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2;
00628 
00629       if (num_pushes > AVR_MAX_PUSHES)
00630         {
00631           fprintf_unfiltered (gdb_stderr, _("Num pushes too large: %d\n"),
00632                               num_pushes);
00633           num_pushes = 0;
00634         }
00635 
00636       if (num_pushes)
00637         {
00638           int from;
00639 
00640           info->saved_regs[AVR_FP_REGNUM + 1].addr = num_pushes;
00641           if (num_pushes >= 2)
00642             info->saved_regs[AVR_FP_REGNUM].addr = num_pushes - 1;
00643 
00644           i = 0;
00645           for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
00646                from <= AVR_LAST_PUSHED_REGNUM; ++from)
00647             info->saved_regs [from].addr = ++i;
00648         }
00649       info->size = loc_size + num_pushes;
00650       info->prologue_type = AVR_PROLOGUE_CALL;
00651 
00652       return pc_beg + pc_offset;
00653     }
00654 
00655   /* Scan for the beginning of the prologue for an interrupt or signal
00656      function.  Note that we have to set the prologue type here since the
00657      third stage of the prologue may not be present (e.g. no saved registered
00658      or changing of the SP register).  */
00659 
00660   if (1)
00661     {
00662       static const unsigned char img[] = {
00663         0x78, 0x94,             /* sei */
00664         0x1f, 0x92,             /* push r1 */
00665         0x0f, 0x92,             /* push r0 */
00666         0x0f, 0xb6,             /* in r0,0x3f SREG */
00667         0x0f, 0x92,             /* push r0 */
00668         0x11, 0x24              /* clr r1 */
00669       };
00670       if (len >= sizeof (img)
00671           && memcmp (prologue, img, sizeof (img)) == 0)
00672         {
00673           info->prologue_type = AVR_PROLOGUE_INTR;
00674           vpc += sizeof (img);
00675           info->saved_regs[AVR_SREG_REGNUM].addr = 3;
00676           info->saved_regs[0].addr = 2;
00677           info->saved_regs[1].addr = 1;
00678           info->size += 3;
00679         }
00680       else if (len >= sizeof (img) - 2
00681                && memcmp (img + 2, prologue, sizeof (img) - 2) == 0)
00682         {
00683           info->prologue_type = AVR_PROLOGUE_SIG;
00684           vpc += sizeof (img) - 2;
00685           info->saved_regs[AVR_SREG_REGNUM].addr = 3;
00686           info->saved_regs[0].addr = 2;
00687           info->saved_regs[1].addr = 1;
00688           info->size += 2;
00689         }
00690     }
00691 
00692   /* First stage of the prologue scanning.
00693      Scan pushes (saved registers) */
00694 
00695   for (; vpc < len; vpc += 2)
00696     {
00697       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00698       if ((insn & 0xfe0f) == 0x920f)    /* push rXX */
00699         {
00700           /* Bits 4-9 contain a mask for registers R0-R32.  */
00701           int regno = (insn & 0x1f0) >> 4;
00702           info->size++;
00703           info->saved_regs[regno].addr = info->size;
00704           scan_stage = 1;
00705         }
00706       else
00707         break;
00708     }
00709 
00710   gdb_assert (vpc < AVR_MAX_PROLOGUE_SIZE);
00711 
00712   /* Handle static small stack allocation using rcall or push.  */
00713 
00714   while (scan_stage == 1 && vpc < len)
00715     {
00716       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00717       if (insn == 0xd000)       /* rcall .+0 */
00718         {
00719           info->size += gdbarch_tdep (gdbarch)->call_length;
00720           vpc += 2;
00721         }
00722       else if (insn == 0x920f)  /* push r0 */
00723         {
00724           info->size += 1;
00725           vpc += 2;
00726         }
00727       else
00728         break;
00729     }
00730 
00731   /* Second stage of the prologue scanning.
00732      Scan:
00733      in r28,__SP_L__
00734      in r29,__SP_H__ */
00735 
00736   if (scan_stage == 1 && vpc < len)
00737     {
00738       static const unsigned char img[] = {
00739         0xcd, 0xb7,             /* in r28,__SP_L__ */
00740         0xde, 0xb7              /* in r29,__SP_H__ */
00741       };
00742 
00743       if (vpc + sizeof (img) < len
00744           && memcmp (prologue + vpc, img, sizeof (img)) == 0)
00745         {
00746           vpc += 4;
00747           scan_stage = 2;
00748         }
00749     }
00750 
00751   /* Third stage of the prologue scanning.  (Really two stages).
00752      Scan for:
00753      sbiw r28,XX or subi r28,lo8(XX)
00754                     sbci r29,hi8(XX)
00755      in __tmp_reg__,__SREG__
00756      cli
00757      out __SP_H__,r29
00758      out __SREG__,__tmp_reg__
00759      out __SP_L__,r28 */
00760 
00761   if (scan_stage == 2 && vpc < len)
00762     {
00763       int locals_size = 0;
00764       static const unsigned char img[] = {
00765         0x0f, 0xb6,             /* in r0,0x3f */
00766         0xf8, 0x94,             /* cli */
00767         0xde, 0xbf,             /* out 0x3e,r29 ; SPH */
00768         0x0f, 0xbe,             /* out 0x3f,r0  ; SREG */
00769         0xcd, 0xbf              /* out 0x3d,r28 ; SPL */
00770       };
00771       static const unsigned char img_sig[] = {
00772         0xde, 0xbf,             /* out 0x3e,r29 ; SPH */
00773         0xcd, 0xbf              /* out 0x3d,r28 ; SPL */
00774       };
00775       static const unsigned char img_int[] = {
00776         0xf8, 0x94,             /* cli */
00777         0xde, 0xbf,             /* out 0x3e,r29 ; SPH */
00778         0x78, 0x94,             /* sei */
00779         0xcd, 0xbf              /* out 0x3d,r28 ; SPL */
00780       };
00781 
00782       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00783       if ((insn & 0xff30) == 0x9720)    /* sbiw r28,XXX */
00784         {
00785           locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
00786           vpc += 2;
00787         }
00788       else if ((insn & 0xf0f0) == 0x50c0)       /* subi r28,lo8(XX) */
00789         {
00790           locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
00791           vpc += 2;
00792           insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00793           vpc += 2;
00794           locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4)) << 8;
00795         }
00796       else
00797         return pc_beg + vpc;
00798 
00799       /* Scan the last part of the prologue.  May not be present for interrupt
00800          or signal handler functions, which is why we set the prologue type
00801          when we saw the beginning of the prologue previously.  */
00802 
00803       if (vpc + sizeof (img_sig) < len
00804           && memcmp (prologue + vpc, img_sig, sizeof (img_sig)) == 0)
00805         {
00806           vpc += sizeof (img_sig);
00807         }
00808       else if (vpc + sizeof (img_int) < len 
00809                && memcmp (prologue + vpc, img_int, sizeof (img_int)) == 0)
00810         {
00811           vpc += sizeof (img_int);
00812         }
00813       if (vpc + sizeof (img) < len
00814           && memcmp (prologue + vpc, img, sizeof (img)) == 0)
00815         {
00816           info->prologue_type = AVR_PROLOGUE_NORMAL;
00817           vpc += sizeof (img);
00818         }
00819 
00820       info->size += locals_size;
00821 
00822       /* Fall through.  */
00823     }
00824 
00825   /* If we got this far, we could not scan the prologue, so just return the pc
00826      of the frame plus an adjustment for argument move insns.  */
00827 
00828   for (; vpc < len; vpc += 2)
00829     {
00830       insn = extract_unsigned_integer (&prologue[vpc], 2, byte_order);
00831       if ((insn & 0xff00) == 0x0100)    /* movw rXX, rYY */
00832         continue;
00833       else if ((insn & 0xfc00) == 0x2c00) /* mov rXX, rYY */
00834         continue;
00835       else
00836           break;
00837     }
00838     
00839   return pc_beg + vpc;
00840 }
00841 
00842 static CORE_ADDR
00843 avr_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00844 {
00845   CORE_ADDR func_addr, func_end;
00846   CORE_ADDR post_prologue_pc;
00847 
00848   /* See what the symbol table says */
00849 
00850   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
00851     return pc;
00852 
00853   post_prologue_pc = skip_prologue_using_sal (gdbarch, func_addr);
00854   if (post_prologue_pc != 0)
00855     return max (pc, post_prologue_pc);
00856 
00857   {
00858     CORE_ADDR prologue_end = pc;
00859     struct avr_unwind_cache info = {0};
00860     struct trad_frame_saved_reg saved_regs[AVR_NUM_REGS];
00861 
00862     info.saved_regs = saved_regs;
00863     
00864     /* Need to run the prologue scanner to figure out if the function has a
00865        prologue and possibly skip over moving arguments passed via registers
00866        to other registers.  */
00867     
00868     prologue_end = avr_scan_prologue (gdbarch, func_addr, func_end, &info);
00869     
00870     if (info.prologue_type != AVR_PROLOGUE_NONE)
00871       return prologue_end;
00872   }
00873 
00874   /* Either we didn't find the start of this function (nothing we can do),
00875      or there's no line info, or the line after the prologue is after
00876      the end of the function (there probably isn't a prologue).  */
00877 
00878   return pc;
00879 }
00880 
00881 /* Not all avr devices support the BREAK insn.  Those that don't should treat
00882    it as a NOP.  Thus, it should be ok.  Since the avr is currently a remote
00883    only target, this shouldn't be a problem (I hope).  TRoth/2003-05-14  */
00884 
00885 static const unsigned char *
00886 avr_breakpoint_from_pc (struct gdbarch *gdbarch,
00887                         CORE_ADDR *pcptr, int *lenptr)
00888 {
00889     static const unsigned char avr_break_insn [] = { 0x98, 0x95 };
00890     *lenptr = sizeof (avr_break_insn);
00891     return avr_break_insn;
00892 }
00893 
00894 /* Determine, for architecture GDBARCH, how a return value of TYPE
00895    should be returned.  If it is supposed to be returned in registers,
00896    and READBUF is non-zero, read the appropriate value from REGCACHE,
00897    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
00898    from WRITEBUF into REGCACHE.  */
00899 
00900 static enum return_value_convention
00901 avr_return_value (struct gdbarch *gdbarch, struct value *function,
00902                   struct type *valtype, struct regcache *regcache,
00903                   gdb_byte *readbuf, const gdb_byte *writebuf)
00904 {
00905   int i;
00906   /* Single byte are returned in r24.
00907      Otherwise, the MSB of the return value is always in r25, calculate which
00908      register holds the LSB.  */
00909   int lsb_reg;
00910 
00911   if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
00912        || TYPE_CODE (valtype) == TYPE_CODE_UNION
00913        || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
00914       && TYPE_LENGTH (valtype) > 8)
00915     return RETURN_VALUE_STRUCT_CONVENTION;
00916 
00917   if (TYPE_LENGTH (valtype) <= 2)
00918     lsb_reg = 24;
00919   else if (TYPE_LENGTH (valtype) <= 4)
00920     lsb_reg = 22;
00921   else if (TYPE_LENGTH (valtype) <= 8)
00922     lsb_reg = 18;
00923   else
00924     gdb_assert_not_reached ("unexpected type length");
00925 
00926   if (writebuf != NULL)
00927     {
00928       for (i = 0; i < TYPE_LENGTH (valtype); i++)
00929         regcache_cooked_write (regcache, lsb_reg + i, writebuf + i);
00930     }
00931 
00932   if (readbuf != NULL)
00933     {
00934       for (i = 0; i < TYPE_LENGTH (valtype); i++)
00935         regcache_cooked_read (regcache, lsb_reg + i, readbuf + i);
00936     }
00937 
00938   return RETURN_VALUE_REGISTER_CONVENTION;
00939 }
00940 
00941 
00942 /* Put here the code to store, into fi->saved_regs, the addresses of
00943    the saved registers of frame described by FRAME_INFO.  This
00944    includes special registers such as pc and fp saved in special ways
00945    in the stack frame.  sp is even more special: the address we return
00946    for it IS the sp for the next frame.  */
00947 
00948 static struct avr_unwind_cache *
00949 avr_frame_unwind_cache (struct frame_info *this_frame,
00950                         void **this_prologue_cache)
00951 {
00952   CORE_ADDR start_pc, current_pc;
00953   ULONGEST prev_sp;
00954   ULONGEST this_base;
00955   struct avr_unwind_cache *info;
00956   struct gdbarch *gdbarch;
00957   struct gdbarch_tdep *tdep;
00958   int i;
00959 
00960   if (*this_prologue_cache)
00961     return *this_prologue_cache;
00962 
00963   info = FRAME_OBSTACK_ZALLOC (struct avr_unwind_cache);
00964   *this_prologue_cache = info;
00965   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00966 
00967   info->size = 0;
00968   info->prologue_type = AVR_PROLOGUE_NONE;
00969 
00970   start_pc = get_frame_func (this_frame);
00971   current_pc = get_frame_pc (this_frame);
00972   if ((start_pc > 0) && (start_pc <= current_pc))
00973     avr_scan_prologue (get_frame_arch (this_frame),
00974                        start_pc, current_pc, info);
00975 
00976   if ((info->prologue_type != AVR_PROLOGUE_NONE)
00977       && (info->prologue_type != AVR_PROLOGUE_MAIN))
00978     {
00979       ULONGEST high_base;       /* High byte of FP */
00980 
00981       /* The SP was moved to the FP.  This indicates that a new frame
00982          was created.  Get THIS frame's FP value by unwinding it from
00983          the next frame.  */
00984       this_base = get_frame_register_unsigned (this_frame, AVR_FP_REGNUM);
00985       high_base = get_frame_register_unsigned (this_frame, AVR_FP_REGNUM + 1);
00986       this_base += (high_base << 8);
00987       
00988       /* The FP points at the last saved register.  Adjust the FP back
00989          to before the first saved register giving the SP.  */
00990       prev_sp = this_base + info->size; 
00991    }
00992   else
00993     {
00994       /* Assume that the FP is this frame's SP but with that pushed
00995          stack space added back.  */
00996       this_base = get_frame_register_unsigned (this_frame, AVR_SP_REGNUM);
00997       prev_sp = this_base + info->size;
00998     }
00999 
01000   /* Add 1 here to adjust for the post-decrement nature of the push
01001      instruction.*/
01002   info->prev_sp = avr_make_saddr (prev_sp + 1);
01003   info->base = avr_make_saddr (this_base);
01004 
01005   gdbarch = get_frame_arch (this_frame);
01006 
01007   /* Adjust all the saved registers so that they contain addresses and not
01008      offsets.  */
01009   for (i = 0; i < gdbarch_num_regs (gdbarch) - 1; i++)
01010     if (info->saved_regs[i].addr > 0)
01011       info->saved_regs[i].addr = info->prev_sp - info->saved_regs[i].addr;
01012 
01013   /* Except for the main and startup code, the return PC is always saved on
01014      the stack and is at the base of the frame.  */
01015 
01016   if (info->prologue_type != AVR_PROLOGUE_MAIN)
01017     info->saved_regs[AVR_PC_REGNUM].addr = info->prev_sp;
01018 
01019   /* The previous frame's SP needed to be computed.  Save the computed
01020      value.  */
01021   tdep = gdbarch_tdep (gdbarch);
01022   trad_frame_set_value (info->saved_regs, AVR_SP_REGNUM,
01023                         info->prev_sp - 1 + tdep->call_length);
01024 
01025   return info;
01026 }
01027 
01028 static CORE_ADDR
01029 avr_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01030 {
01031   ULONGEST pc;
01032 
01033   pc = frame_unwind_register_unsigned (next_frame, AVR_PC_REGNUM);
01034 
01035   return avr_make_iaddr (pc);
01036 }
01037 
01038 static CORE_ADDR
01039 avr_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
01040 {
01041   ULONGEST sp;
01042 
01043   sp = frame_unwind_register_unsigned (next_frame, AVR_SP_REGNUM);
01044 
01045   return avr_make_saddr (sp);
01046 }
01047 
01048 /* Given a GDB frame, determine the address of the calling function's
01049    frame.  This will be used to create a new GDB frame struct.  */
01050 
01051 static void
01052 avr_frame_this_id (struct frame_info *this_frame,
01053                    void **this_prologue_cache,
01054                    struct frame_id *this_id)
01055 {
01056   struct avr_unwind_cache *info
01057     = avr_frame_unwind_cache (this_frame, this_prologue_cache);
01058   CORE_ADDR base;
01059   CORE_ADDR func;
01060   struct frame_id id;
01061 
01062   /* The FUNC is easy.  */
01063   func = get_frame_func (this_frame);
01064 
01065   /* Hopefully the prologue analysis either correctly determined the
01066      frame's base (which is the SP from the previous frame), or set
01067      that base to "NULL".  */
01068   base = info->prev_sp;
01069   if (base == 0)
01070     return;
01071 
01072   id = frame_id_build (base, func);
01073   (*this_id) = id;
01074 }
01075 
01076 static struct value *
01077 avr_frame_prev_register (struct frame_info *this_frame,
01078                          void **this_prologue_cache, int regnum)
01079 {
01080   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01081   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01082   struct avr_unwind_cache *info
01083     = avr_frame_unwind_cache (this_frame, this_prologue_cache);
01084 
01085   if (regnum == AVR_PC_REGNUM || regnum == AVR_PSEUDO_PC_REGNUM)
01086     {
01087       if (trad_frame_addr_p (info->saved_regs, AVR_PC_REGNUM))
01088         {
01089           /* Reading the return PC from the PC register is slightly
01090              abnormal.  register_size(AVR_PC_REGNUM) says it is 4 bytes,
01091              but in reality, only two bytes (3 in upcoming mega256) are
01092              stored on the stack.
01093 
01094              Also, note that the value on the stack is an addr to a word
01095              not a byte, so we will need to multiply it by two at some
01096              point. 
01097 
01098              And to confuse matters even more, the return address stored
01099              on the stack is in big endian byte order, even though most
01100              everything else about the avr is little endian.  Ick!  */
01101           ULONGEST pc;
01102           int i;
01103           gdb_byte buf[3];
01104           struct gdbarch *gdbarch = get_frame_arch (this_frame);
01105           struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01106 
01107           read_memory (info->saved_regs[AVR_PC_REGNUM].addr,
01108                        buf, tdep->call_length);
01109 
01110           /* Extract the PC read from memory as a big-endian.  */
01111           pc = 0;
01112           for (i = 0; i < tdep->call_length; i++)
01113             pc = (pc << 8) | buf[i];
01114 
01115           if (regnum == AVR_PC_REGNUM)
01116             pc <<= 1;
01117 
01118           return frame_unwind_got_constant (this_frame, regnum, pc);
01119         }
01120 
01121       return frame_unwind_got_optimized (this_frame, regnum);
01122     }
01123 
01124   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
01125 }
01126 
01127 static const struct frame_unwind avr_frame_unwind = {
01128   NORMAL_FRAME,
01129   default_frame_unwind_stop_reason,
01130   avr_frame_this_id,
01131   avr_frame_prev_register,
01132   NULL,
01133   default_frame_sniffer
01134 };
01135 
01136 static CORE_ADDR
01137 avr_frame_base_address (struct frame_info *this_frame, void **this_cache)
01138 {
01139   struct avr_unwind_cache *info
01140     = avr_frame_unwind_cache (this_frame, this_cache);
01141 
01142   return info->base;
01143 }
01144 
01145 static const struct frame_base avr_frame_base = {
01146   &avr_frame_unwind,
01147   avr_frame_base_address,
01148   avr_frame_base_address,
01149   avr_frame_base_address
01150 };
01151 
01152 /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
01153    frame.  The frame ID's base needs to match the TOS value saved by
01154    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */
01155 
01156 static struct frame_id
01157 avr_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
01158 {
01159   ULONGEST base;
01160 
01161   base = get_frame_register_unsigned (this_frame, AVR_SP_REGNUM);
01162   return frame_id_build (avr_make_saddr (base), get_frame_pc (this_frame));
01163 }
01164 
01165 /* When arguments must be pushed onto the stack, they go on in reverse
01166    order.  The below implements a FILO (stack) to do this.  */
01167 
01168 struct stack_item
01169 {
01170   int len;
01171   struct stack_item *prev;
01172   void *data;
01173 };
01174 
01175 static struct stack_item *
01176 push_stack_item (struct stack_item *prev, const bfd_byte *contents, int len)
01177 {
01178   struct stack_item *si;
01179   si = xmalloc (sizeof (struct stack_item));
01180   si->data = xmalloc (len);
01181   si->len = len;
01182   si->prev = prev;
01183   memcpy (si->data, contents, len);
01184   return si;
01185 }
01186 
01187 static struct stack_item *pop_stack_item (struct stack_item *si);
01188 static struct stack_item *
01189 pop_stack_item (struct stack_item *si)
01190 {
01191   struct stack_item *dead = si;
01192   si = si->prev;
01193   xfree (dead->data);
01194   xfree (dead);
01195   return si;
01196 }
01197 
01198 /* Setup the function arguments for calling a function in the inferior.
01199 
01200    On the AVR architecture, there are 18 registers (R25 to R8) which are
01201    dedicated for passing function arguments.  Up to the first 18 arguments
01202    (depending on size) may go into these registers.  The rest go on the stack.
01203 
01204    All arguments are aligned to start in even-numbered registers (odd-sized
01205    arguments, including char, have one free register above them).  For example,
01206    an int in arg1 and a char in arg2 would be passed as such:
01207 
01208       arg1 -> r25:r24
01209       arg2 -> r22
01210 
01211    Arguments that are larger than 2 bytes will be split between two or more
01212    registers as available, but will NOT be split between a register and the
01213    stack.  Arguments that go onto the stack are pushed last arg first (this is
01214    similar to the d10v).  */
01215 
01216 /* NOTE: TRoth/2003-06-17: The rest of this comment is old looks to be
01217    inaccurate.
01218 
01219    An exceptional case exists for struct arguments (and possibly other
01220    aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
01221    not a multiple of WORDSIZE bytes.  In this case the argument is never split
01222    between the registers and the stack, but instead is copied in its entirety
01223    onto the stack, AND also copied into as many registers as there is room
01224    for.  In other words, space in registers permitting, two copies of the same
01225    argument are passed in.  As far as I can tell, only the one on the stack is
01226    used, although that may be a function of the level of compiler
01227    optimization.  I suspect this is a compiler bug.  Arguments of these odd
01228    sizes are left-justified within the word (as opposed to arguments smaller
01229    than WORDSIZE bytes, which are right-justified).
01230  
01231    If the function is to return an aggregate type such as a struct, the caller
01232    must allocate space into which the callee will copy the return value.  In
01233    this case, a pointer to the return value location is passed into the callee
01234    in register R0, which displaces one of the other arguments passed in via
01235    registers R0 to R2.  */
01236 
01237 static CORE_ADDR
01238 avr_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
01239                      struct regcache *regcache, CORE_ADDR bp_addr,
01240                      int nargs, struct value **args, CORE_ADDR sp,
01241                      int struct_return, CORE_ADDR struct_addr)
01242 {
01243   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01244   int i;
01245   gdb_byte buf[3];
01246   int call_length = gdbarch_tdep (gdbarch)->call_length;
01247   CORE_ADDR return_pc = avr_convert_iaddr_to_raw (bp_addr);
01248   int regnum = AVR_ARGN_REGNUM;
01249   struct stack_item *si = NULL;
01250 
01251   if (struct_return)
01252     {
01253       regcache_cooked_write_unsigned
01254         (regcache, regnum--, (struct_addr >> 8) & 0xff);
01255       regcache_cooked_write_unsigned
01256         (regcache, regnum--, struct_addr & 0xff);
01257       /* SP being post decremented, we need to reserve one byte so that the
01258          return address won't overwrite the result (or vice-versa).  */
01259       if (sp == struct_addr)
01260         sp--;
01261     }
01262 
01263   for (i = 0; i < nargs; i++)
01264     {
01265       int last_regnum;
01266       int j;
01267       struct value *arg = args[i];
01268       struct type *type = check_typedef (value_type (arg));
01269       const bfd_byte *contents = value_contents (arg);
01270       int len = TYPE_LENGTH (type);
01271 
01272       /* Calculate the potential last register needed.  */
01273       last_regnum = regnum - (len + (len & 1));
01274 
01275       /* If there are registers available, use them.  Once we start putting
01276          stuff on the stack, all subsequent args go on stack.  */
01277       if ((si == NULL) && (last_regnum >= 8))
01278         {
01279           ULONGEST val;
01280 
01281           /* Skip a register for odd length args.  */
01282           if (len & 1)
01283             regnum--;
01284 
01285           val = extract_unsigned_integer (contents, len, byte_order);
01286           for (j = 0; j < len; j++)
01287             regcache_cooked_write_unsigned
01288               (regcache, regnum--, val >> (8 * (len - j - 1)));
01289         }
01290       /* No registers available, push the args onto the stack.  */
01291       else
01292         {
01293           /* From here on, we don't care about regnum.  */
01294           si = push_stack_item (si, contents, len);
01295         }
01296     }
01297 
01298   /* Push args onto the stack.  */
01299   while (si)
01300     {
01301       sp -= si->len;
01302       /* Add 1 to sp here to account for post decr nature of pushes.  */
01303       write_memory (sp + 1, si->data, si->len);
01304       si = pop_stack_item (si);
01305     }
01306 
01307   /* Set the return address.  For the avr, the return address is the BP_ADDR.
01308      Need to push the return address onto the stack noting that it needs to be
01309      in big-endian order on the stack.  */
01310   for (i = 1; i <= call_length; i++)
01311     {
01312       buf[call_length - i] = return_pc & 0xff;
01313       return_pc >>= 8;
01314     }
01315 
01316   sp -= call_length;
01317   /* Use 'sp + 1' since pushes are post decr ops.  */
01318   write_memory (sp + 1, buf, call_length);
01319 
01320   /* Finally, update the SP register.  */
01321   regcache_cooked_write_unsigned (regcache, AVR_SP_REGNUM,
01322                                   avr_convert_saddr_to_raw (sp));
01323 
01324   /* Return SP value for the dummy frame, where the return address hasn't been
01325      pushed.  */
01326   return sp + call_length;
01327 }
01328 
01329 /* Unfortunately dwarf2 register for SP is 32.  */
01330 
01331 static int
01332 avr_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
01333 {
01334   if (reg >= 0 && reg < 32)
01335     return reg;
01336   if (reg == 32)
01337     return AVR_SP_REGNUM;
01338 
01339   warning (_("Unmapped DWARF Register #%d encountered."), reg);
01340 
01341   return -1;
01342 }
01343 
01344 /* Initialize the gdbarch structure for the AVR's.  */
01345 
01346 static struct gdbarch *
01347 avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
01348 {
01349   struct gdbarch *gdbarch;
01350   struct gdbarch_tdep *tdep;
01351   struct gdbarch_list *best_arch;
01352   int call_length;
01353 
01354   /* Avr-6 call instructions save 3 bytes.  */
01355   switch (info.bfd_arch_info->mach)
01356     {
01357     case bfd_mach_avr1:
01358     case bfd_mach_avr2:
01359     case bfd_mach_avr3:
01360     case bfd_mach_avr4:
01361     case bfd_mach_avr5:
01362     default:
01363       call_length = 2;
01364       break;
01365     case bfd_mach_avr6:
01366       call_length = 3;
01367       break;
01368     }
01369 
01370   /* If there is already a candidate, use it.  */
01371   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
01372        best_arch != NULL;
01373        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
01374     {
01375       if (gdbarch_tdep (best_arch->gdbarch)->call_length == call_length)
01376         return best_arch->gdbarch;
01377     }
01378 
01379   /* None found, create a new architecture from the information provided.  */
01380   tdep = XMALLOC (struct gdbarch_tdep);
01381   gdbarch = gdbarch_alloc (&info, tdep);
01382   
01383   tdep->call_length = call_length;
01384 
01385   /* Create a type for PC.  We can't use builtin types here, as they may not
01386      be defined.  */
01387   tdep->void_type = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void");
01388   tdep->func_void_type = make_function_type (tdep->void_type, NULL);
01389   tdep->pc_type = arch_type (gdbarch, TYPE_CODE_PTR, 4, NULL);
01390   TYPE_TARGET_TYPE (tdep->pc_type) = tdep->func_void_type;
01391   TYPE_UNSIGNED (tdep->pc_type) = 1;
01392 
01393   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
01394   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
01395   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01396   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
01397   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
01398   set_gdbarch_addr_bit (gdbarch, 32);
01399 
01400   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01401   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01402   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
01403 
01404   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
01405   set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
01406   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
01407 
01408   set_gdbarch_read_pc (gdbarch, avr_read_pc);
01409   set_gdbarch_write_pc (gdbarch, avr_write_pc);
01410 
01411   set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
01412 
01413   set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
01414   set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
01415 
01416   set_gdbarch_register_name (gdbarch, avr_register_name);
01417   set_gdbarch_register_type (gdbarch, avr_register_type);
01418 
01419   set_gdbarch_num_pseudo_regs (gdbarch, AVR_NUM_PSEUDO_REGS);
01420   set_gdbarch_pseudo_register_read (gdbarch, avr_pseudo_register_read);
01421   set_gdbarch_pseudo_register_write (gdbarch, avr_pseudo_register_write);
01422 
01423   set_gdbarch_return_value (gdbarch, avr_return_value);
01424   set_gdbarch_print_insn (gdbarch, print_insn_avr);
01425 
01426   set_gdbarch_push_dummy_call (gdbarch, avr_push_dummy_call);
01427 
01428   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, avr_dwarf_reg_to_regnum);
01429 
01430   set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
01431   set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
01432   set_gdbarch_integer_to_address (gdbarch, avr_integer_to_address);
01433 
01434   set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
01435   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
01436 
01437   set_gdbarch_breakpoint_from_pc (gdbarch, avr_breakpoint_from_pc);
01438 
01439   frame_unwind_append_unwinder (gdbarch, &avr_frame_unwind);
01440   frame_base_set_default (gdbarch, &avr_frame_base);
01441 
01442   set_gdbarch_dummy_id (gdbarch, avr_dummy_id);
01443 
01444   set_gdbarch_unwind_pc (gdbarch, avr_unwind_pc);
01445   set_gdbarch_unwind_sp (gdbarch, avr_unwind_sp);
01446 
01447   return gdbarch;
01448 }
01449 
01450 /* Send a query request to the avr remote target asking for values of the io
01451    registers.  If args parameter is not NULL, then the user has requested info
01452    on a specific io register [This still needs implemented and is ignored for
01453    now].  The query string should be one of these forms:
01454 
01455    "Ravr.io_reg" -> reply is "NN" number of io registers
01456 
01457    "Ravr.io_reg:addr,len" where addr is first register and len is number of
01458    registers to be read.  The reply should be "<NAME>,VV;" for each io register
01459    where, <NAME> is a string, and VV is the hex value of the register.
01460 
01461    All io registers are 8-bit.  */
01462 
01463 static void
01464 avr_io_reg_read_command (char *args, int from_tty)
01465 {
01466   LONGEST bufsiz = 0;
01467   gdb_byte *buf;
01468   const char *bufstr;
01469   char query[400];
01470   const char *p;
01471   unsigned int nreg = 0;
01472   unsigned int val;
01473   int i, j, k, step;
01474 
01475   /* Find out how many io registers the target has.  */
01476   bufsiz = target_read_alloc (&current_target, TARGET_OBJECT_AVR,
01477                               "avr.io_reg", &buf);
01478   bufstr = (const char *) buf;
01479 
01480   if (bufsiz <= 0)
01481     {
01482       fprintf_unfiltered (gdb_stderr,
01483                           _("ERR: info io_registers NOT supported "
01484                             "by current target\n"));
01485       return;
01486     }
01487 
01488   if (sscanf (bufstr, "%x", &nreg) != 1)
01489     {
01490       fprintf_unfiltered (gdb_stderr,
01491                           _("Error fetching number of io registers\n"));
01492       xfree (buf);
01493       return;
01494     }
01495 
01496   xfree (buf);
01497 
01498   reinitialize_more_filter ();
01499 
01500   printf_unfiltered (_("Target has %u io registers:\n\n"), nreg);
01501 
01502   /* only fetch up to 8 registers at a time to keep the buffer small */
01503   step = 8;
01504 
01505   for (i = 0; i < nreg; i += step)
01506     {
01507       /* how many registers this round? */
01508       j = step;
01509       if ((i+j) >= nreg)
01510         j = nreg - i;           /* last block is less than 8 registers */
01511 
01512       snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
01513       bufsiz = target_read_alloc (&current_target, TARGET_OBJECT_AVR,
01514                                   query, &buf);
01515 
01516       p = (const char *) buf;
01517       for (k = i; k < (i + j); k++)
01518         {
01519           if (sscanf (p, "%[^,],%x;", query, &val) == 2)
01520             {
01521               printf_filtered ("[%02x] %-15s : %02x\n", k, query, val);
01522               while ((*p != ';') && (*p != '\0'))
01523                 p++;
01524               p++;              /* skip over ';' */
01525               if (*p == '\0')
01526                 break;
01527             }
01528         }
01529 
01530       xfree (buf);
01531     }
01532 }
01533 
01534 extern initialize_file_ftype _initialize_avr_tdep; /* -Wmissing-prototypes */
01535 
01536 void
01537 _initialize_avr_tdep (void)
01538 {
01539   register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
01540 
01541   /* Add a new command to allow the user to query the avr remote target for
01542      the values of the io space registers in a saner way than just using
01543      `x/NNNb ADDR`.  */
01544 
01545   /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
01546      io_registers' to signify it is not available on other platforms.  */
01547 
01548   add_cmd ("io_registers", class_info, avr_io_reg_read_command,
01549            _("query remote avr target for io space register values"),
01550            &infolist);
01551 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines