Back to index

cell-binutils  2.17cvs20070401
tc-avr.c
Go to the documentation of this file.
00001 /* tc-avr.c -- Assembler code for the ATMEL AVR
00002 
00003    Copyright 1999, 2000, 2001, 2002, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Contributed by Denis Chertykov <denisc@overta.ru>
00006 
00007    This file is part of GAS, the GNU Assembler.
00008 
00009    GAS is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2, or (at your option)
00012    any later version.
00013 
00014    GAS is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with GAS; see the file COPYING.  If not, write to
00021    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
00022    Boston, MA 02110-1301, USA.  */
00023 
00024 #include "as.h"
00025 #include "safe-ctype.h"
00026 #include "subsegs.h"
00027 
00028 struct avr_opcodes_s
00029 {
00030   char *        name;
00031   char *        constraints;
00032   int           insn_size;         /* In words.  */
00033   int           isa;
00034   unsigned int  bin_opcode;
00035 };
00036 
00037 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
00038 {#NAME, CONSTR, SIZE, ISA, BIN},
00039 
00040 struct avr_opcodes_s avr_opcodes[] =
00041 {
00042   #include "opcode/avr.h"
00043   {NULL, NULL, 0, 0, 0}
00044 };
00045 
00046 const char comment_chars[] = ";";
00047 const char line_comment_chars[] = "#";
00048 const char line_separator_chars[] = "$";
00049 
00050 const char *md_shortopts = "m:";
00051 struct mcu_type_s
00052 {
00053   char *name;
00054   int isa;
00055   int mach;
00056 };
00057 
00058 /* XXX - devices that don't seem to exist (renamed, replaced with larger
00059    ones, or planned but never produced), left here for compatibility.
00060    TODO: hide them in show_mcu_list output?  */
00061 
00062 static struct mcu_type_s mcu_types[] =
00063 {
00064   {"avr1",       AVR_ISA_TINY1,   bfd_mach_avr1},
00065   {"avr2",       AVR_ISA_TINY2,   bfd_mach_avr2},
00066   {"avr3",       AVR_ISA_M103,    bfd_mach_avr3},
00067   {"avr4",       AVR_ISA_M8,      bfd_mach_avr4},
00068   {"avr5",       AVR_ISA_ALL,     bfd_mach_avr5},
00069   {"avr6",       AVR_ISA_ALL,     bfd_mach_avr6},
00070   {"at90s1200",  AVR_ISA_1200,    bfd_mach_avr1},
00071   {"attiny10",   AVR_ISA_TINY1,   bfd_mach_avr1}, /* XXX -> tn11 */
00072   {"attiny11",   AVR_ISA_TINY1,   bfd_mach_avr1},
00073   {"attiny12",   AVR_ISA_TINY1,   bfd_mach_avr1},
00074   {"attiny15",   AVR_ISA_TINY1,   bfd_mach_avr1},
00075   {"attiny28",   AVR_ISA_TINY1,   bfd_mach_avr1},
00076   {"at90s2313",  AVR_ISA_2xxx,    bfd_mach_avr2},
00077   {"at90s2323",  AVR_ISA_2xxx,    bfd_mach_avr2},
00078   {"at90s2333",  AVR_ISA_2xxx,    bfd_mach_avr2}, /* XXX -> 4433 */
00079   {"at90s2343",  AVR_ISA_2xxx,    bfd_mach_avr2},
00080   {"attiny22",   AVR_ISA_2xxx,    bfd_mach_avr2}, /* XXX -> 2343 */
00081   {"attiny26",   AVR_ISA_2xxx,    bfd_mach_avr2},
00082   {"at90s4433",  AVR_ISA_2xxx,    bfd_mach_avr2},
00083   {"at90s4414",  AVR_ISA_2xxx,    bfd_mach_avr2}, /* XXX -> 8515 */
00084   {"at90s4434",  AVR_ISA_2xxx,    bfd_mach_avr2}, /* XXX -> 8535 */
00085   {"at90s8515",  AVR_ISA_2xxx,    bfd_mach_avr2},
00086   {"at90s8535",  AVR_ISA_2xxx,    bfd_mach_avr2},
00087   {"at90c8534",  AVR_ISA_2xxx,    bfd_mach_avr2},
00088   {"at86rf401",  AVR_ISA_2xxx,    bfd_mach_avr2},
00089   {"attiny13",   AVR_ISA_TINY2,   bfd_mach_avr2},
00090   {"attiny2313", AVR_ISA_TINY2,   bfd_mach_avr2},
00091   {"attiny261",  AVR_ISA_TINY2,   bfd_mach_avr2},
00092   {"attiny461",  AVR_ISA_TINY2,   bfd_mach_avr2},
00093   {"attiny861",  AVR_ISA_TINY2,   bfd_mach_avr2},
00094   {"attiny24",   AVR_ISA_TINY2,   bfd_mach_avr2},
00095   {"attiny44",   AVR_ISA_TINY2,   bfd_mach_avr2},
00096   {"attiny84",   AVR_ISA_TINY2,   bfd_mach_avr2},
00097   {"attiny25",   AVR_ISA_TINY2,   bfd_mach_avr2},
00098   {"attiny45",   AVR_ISA_TINY2,   bfd_mach_avr2},
00099   {"attiny85",   AVR_ISA_TINY2,   bfd_mach_avr2},
00100   {"atmega603",  AVR_ISA_M603,    bfd_mach_avr3}, /* XXX -> m103 */
00101   {"atmega103",  AVR_ISA_M103,    bfd_mach_avr3},
00102   {"at43usb320", AVR_ISA_M103,    bfd_mach_avr3},
00103   {"at43usb355", AVR_ISA_M603,    bfd_mach_avr3},
00104   {"at76c711",   AVR_ISA_M603,    bfd_mach_avr3},
00105   {"atmega48",   AVR_ISA_PWMx,    bfd_mach_avr4},
00106   {"atmega8",    AVR_ISA_M8,      bfd_mach_avr4},
00107   {"atmega83",   AVR_ISA_M8,      bfd_mach_avr4}, /* XXX -> m8535 */
00108   {"atmega85",   AVR_ISA_M8,      bfd_mach_avr4}, /* XXX -> m8 */
00109   {"atmega88",   AVR_ISA_PWMx,    bfd_mach_avr4},
00110   {"atmega8515", AVR_ISA_M8,      bfd_mach_avr4},
00111   {"atmega8535", AVR_ISA_M8,      bfd_mach_avr4},
00112   {"at90pwm2",   AVR_ISA_PWMx,    bfd_mach_avr4},
00113   {"at90pwm3",   AVR_ISA_PWMx,    bfd_mach_avr4},
00114   {"atmega16",   AVR_ISA_M323,    bfd_mach_avr5},
00115   {"atmega161",  AVR_ISA_M161,    bfd_mach_avr5},
00116   {"atmega162",  AVR_ISA_M323,    bfd_mach_avr5},
00117   {"atmega163",  AVR_ISA_M161,    bfd_mach_avr5},
00118   {"atmega164p", AVR_ISA_M323,    bfd_mach_avr5},
00119   {"atmega165",  AVR_ISA_M323,    bfd_mach_avr5},
00120   {"atmega165p", AVR_ISA_M323,    bfd_mach_avr5},
00121   {"atmega168",  AVR_ISA_M323,    bfd_mach_avr5},
00122   {"atmega169",  AVR_ISA_M323,    bfd_mach_avr5},
00123   {"atmega169p", AVR_ISA_M323,    bfd_mach_avr5},
00124   {"atmega32",   AVR_ISA_M323,    bfd_mach_avr5},
00125   {"atmega323",  AVR_ISA_M323,    bfd_mach_avr5},
00126   {"atmega324p", AVR_ISA_M323,    bfd_mach_avr5},
00127   {"atmega325",  AVR_ISA_M323,    bfd_mach_avr5},
00128   {"atmega329",  AVR_ISA_M323,    bfd_mach_avr5},
00129   {"atmega3250", AVR_ISA_M323,    bfd_mach_avr5},
00130   {"atmega3290", AVR_ISA_M323,    bfd_mach_avr5},
00131   {"atmega406",  AVR_ISA_M323,    bfd_mach_avr5},
00132   {"atmega64",   AVR_ISA_M323,    bfd_mach_avr5},
00133   {"atmega640",  AVR_ISA_M323,    bfd_mach_avr5},
00134   {"atmega644",  AVR_ISA_M323,    bfd_mach_avr5},
00135   {"atmega644p", AVR_ISA_M323,    bfd_mach_avr5},
00136   {"atmega128",  AVR_ISA_M128,    bfd_mach_avr5},
00137   {"atmega1280", AVR_ISA_M128,    bfd_mach_avr5},
00138   {"atmega1281", AVR_ISA_M128,    bfd_mach_avr5},
00139   {"atmega645",  AVR_ISA_M323,    bfd_mach_avr5},
00140   {"atmega649",  AVR_ISA_M323,    bfd_mach_avr5},
00141   {"atmega6450", AVR_ISA_M323,    bfd_mach_avr5},
00142   {"atmega6490", AVR_ISA_M323,    bfd_mach_avr5},
00143   {"at90can32" , AVR_ISA_M323,    bfd_mach_avr5},
00144   {"at90can64" , AVR_ISA_M323,    bfd_mach_avr5},
00145   {"at90can128", AVR_ISA_M128,    bfd_mach_avr5},
00146   {"at90usb646", AVR_ISA_M323,    bfd_mach_avr5},
00147   {"at90usb647", AVR_ISA_M323,    bfd_mach_avr5},
00148   {"at90usb1286",AVR_ISA_M128,    bfd_mach_avr5},
00149   {"at90usb1287",AVR_ISA_M128,    bfd_mach_avr5},
00150   {"at94k",      AVR_ISA_94K,     bfd_mach_avr5},
00151   {"atmega2560", AVR_ISA_ALL,     bfd_mach_avr6},
00152   {"atmega2561", AVR_ISA_ALL,     bfd_mach_avr6},
00153   {NULL, 0, 0}
00154 };
00155 
00156 /* Current MCU type.  */
00157 static struct mcu_type_s   default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
00158 static struct mcu_type_s * avr_mcu = & default_mcu;
00159 
00160 /* AVR target-specific switches.  */
00161 struct avr_opt_s
00162 {
00163   int all_opcodes;  /* -mall-opcodes: accept all known AVR opcodes.  */
00164   int no_skip_bug;  /* -mno-skip-bug: no warnings for skipping 2-word insns.  */
00165   int no_wrap;      /* -mno-wrap: reject rjmp/rcall with 8K wrap-around.  */
00166 };
00167 
00168 static struct avr_opt_s avr_opt = { 0, 0, 0 };
00169 
00170 const char EXP_CHARS[] = "eE";
00171 const char FLT_CHARS[] = "dD";
00172 
00173 static void avr_set_arch (int);
00174 
00175 /* The target specific pseudo-ops which we support.  */
00176 const pseudo_typeS md_pseudo_table[] =
00177 {
00178   {"arch", avr_set_arch,    0},
00179   { NULL,     NULL,         0}
00180 };
00181 
00182 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
00183 
00184 #define EXP_MOD_NAME(i)       exp_mod[i].name
00185 #define EXP_MOD_RELOC(i)      exp_mod[i].reloc
00186 #define EXP_MOD_NEG_RELOC(i)  exp_mod[i].neg_reloc
00187 #define HAVE_PM_P(i)          exp_mod[i].have_pm
00188 
00189 struct exp_mod_s
00190 {
00191   char *                    name;
00192   bfd_reloc_code_real_type  reloc;
00193   bfd_reloc_code_real_type  neg_reloc;
00194   int                       have_pm;
00195 };
00196 
00197 static struct exp_mod_s exp_mod[] =
00198 {
00199   {"hh8",    BFD_RELOC_AVR_HH8_LDI,    BFD_RELOC_AVR_HH8_LDI_NEG,    1},
00200   {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
00201   {"hi8",    BFD_RELOC_AVR_HI8_LDI,    BFD_RELOC_AVR_HI8_LDI_NEG,    1},
00202   {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
00203   {"lo8",    BFD_RELOC_AVR_LO8_LDI,    BFD_RELOC_AVR_LO8_LDI_NEG,    1},
00204   {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
00205   {"hlo8",   BFD_RELOC_AVR_HH8_LDI,    BFD_RELOC_AVR_HH8_LDI_NEG,    0},
00206   {"hhi8",   BFD_RELOC_AVR_MS8_LDI,    BFD_RELOC_AVR_MS8_LDI_NEG,    0},
00207 };
00208 
00209 /* A union used to store indicies into the exp_mod[] array
00210    in a hash table which expects void * data types.  */
00211 typedef union
00212 {
00213   void * ptr;
00214   int    index;
00215 } mod_index;
00216 
00217 /* Opcode hash table.  */
00218 static struct hash_control *avr_hash;
00219 
00220 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx).  */
00221 static struct hash_control *avr_mod_hash;
00222 
00223 #define OPTION_MMCU 'm'
00224 enum options
00225 {
00226   OPTION_ALL_OPCODES = OPTION_MD_BASE + 1,
00227   OPTION_NO_SKIP_BUG,
00228   OPTION_NO_WRAP
00229 };
00230 
00231 struct option md_longopts[] =
00232 {
00233   { "mmcu",   required_argument, NULL, OPTION_MMCU        },
00234   { "mall-opcodes", no_argument, NULL, OPTION_ALL_OPCODES },
00235   { "mno-skip-bug", no_argument, NULL, OPTION_NO_SKIP_BUG },
00236   { "mno-wrap",     no_argument, NULL, OPTION_NO_WRAP     },
00237   { NULL, no_argument, NULL, 0 }
00238 };
00239 
00240 size_t md_longopts_size = sizeof (md_longopts);
00241 
00242 /* Display nicely formatted list of known MCU names.  */
00243 
00244 static void
00245 show_mcu_list (FILE *stream)
00246 {
00247   int i, x;
00248 
00249   fprintf (stream, _("Known MCU names:"));
00250   x = 1000;
00251 
00252   for (i = 0; mcu_types[i].name; i++)
00253     {
00254       int len = strlen (mcu_types[i].name);
00255 
00256       x += len + 1;
00257 
00258       if (x < 75)
00259        fprintf (stream, " %s", mcu_types[i].name);
00260       else
00261        {
00262          fprintf (stream, "\n  %s", mcu_types[i].name);
00263          x = len + 2;
00264        }
00265     }
00266 
00267   fprintf (stream, "\n");
00268 }
00269 
00270 static inline char *
00271 skip_space (char *s)
00272 {
00273   while (*s == ' ' || *s == '\t')
00274     ++s;
00275   return s;
00276 }
00277 
00278 /* Extract one word from FROM and copy it to TO.  */
00279 
00280 static char *
00281 extract_word (char *from, char *to, int limit)
00282 {
00283   char *op_start;
00284   char *op_end;
00285   int size = 0;
00286 
00287   /* Drop leading whitespace.  */
00288   from = skip_space (from);
00289   *to = 0;
00290 
00291   /* Find the op code end.  */
00292   for (op_start = op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
00293     {
00294       to[size++] = *op_end++;
00295       if (size + 1 >= limit)
00296        break;
00297     }
00298 
00299   to[size] = 0;
00300   return op_end;
00301 }
00302 
00303 int
00304 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
00305                             asection *seg ATTRIBUTE_UNUSED)
00306 {
00307   abort ();
00308   return 0;
00309 }
00310 
00311 void
00312 md_show_usage (FILE *stream)
00313 {
00314   fprintf (stream,
00315       _("AVR options:\n"
00316        "  -mmcu=[avr-name] select microcontroller variant\n"
00317        "                   [avr-name] can be:\n"
00318        "                   avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
00319        "                   avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
00320        "                   avr3 - ATmega103, ATmega603\n"
00321        "                   avr4 - ATmega83, ATmega85\n"
00322        "                   avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
00323        "                   or immediate microcontroller name.\n"));
00324   fprintf (stream,
00325       _("  -mall-opcodes    accept all AVR opcodes, even if not supported by MCU\n"
00326        "  -mno-skip-bug    disable warnings for skipping two-word instructions\n"
00327        "                   (default for avr4, avr5)\n"
00328        "  -mno-wrap        reject rjmp/rcall instructions with 8K wrap-around\n"
00329        "                   (default for avr3, avr5)\n"));
00330   show_mcu_list (stream);
00331 }
00332 
00333 static void
00334 avr_set_arch (int dummy ATTRIBUTE_UNUSED)
00335 {
00336   char str[20];
00337 
00338   input_line_pointer = extract_word (input_line_pointer, str, 20);
00339   md_parse_option (OPTION_MMCU, str);
00340   bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
00341 }
00342 
00343 int
00344 md_parse_option (int c, char *arg)
00345 {
00346   switch (c)
00347     {
00348     case OPTION_MMCU:
00349       {
00350        int i;
00351        char *s = alloca (strlen (arg) + 1);
00352 
00353        {
00354          char *t = s;
00355          char *arg1 = arg;
00356 
00357          do
00358            *t = TOLOWER (*arg1++);
00359          while (*t++);
00360        }
00361 
00362        for (i = 0; mcu_types[i].name; ++i)
00363          if (strcmp (mcu_types[i].name, s) == 0)
00364            break;
00365 
00366        if (!mcu_types[i].name)
00367          {
00368            show_mcu_list (stderr);
00369            as_fatal (_("unknown MCU: %s\n"), arg);
00370          }
00371 
00372        /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
00373           type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
00374           as .arch ... in the asm output at the same time.  */
00375        if (avr_mcu == &default_mcu || avr_mcu->mach == mcu_types[i].mach)
00376          avr_mcu = &mcu_types[i];
00377        else
00378          as_fatal (_("redefinition of mcu type `%s' to `%s'"),
00379                   avr_mcu->name, mcu_types[i].name);
00380        return 1;
00381       }
00382     case OPTION_ALL_OPCODES:
00383       avr_opt.all_opcodes = 1;
00384       return 1;
00385     case OPTION_NO_SKIP_BUG:
00386       avr_opt.no_skip_bug = 1;
00387       return 1;
00388     case OPTION_NO_WRAP:
00389       avr_opt.no_wrap = 1;
00390       return 1;
00391     }
00392 
00393   return 0;
00394 }
00395 
00396 symbolS *
00397 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
00398 {
00399   return NULL;
00400 }
00401 
00402 /* Turn a string in input_line_pointer into a floating point constant
00403    of type TYPE, and store the appropriate bytes in *LITP.  The number
00404    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
00405    returned, or NULL on OK.  */
00406 
00407 char *
00408 md_atof (int type, char *litP, int *sizeP)
00409 {
00410   int prec;
00411   LITTLENUM_TYPE words[4];
00412   LITTLENUM_TYPE *wordP;
00413   char *t;
00414 
00415   switch (type)
00416     {
00417     case 'f':
00418       prec = 2;
00419       break;
00420     case 'd':
00421       prec = 4;
00422       break;
00423     default:
00424       *sizeP = 0;
00425       return _("bad call to md_atof");
00426     }
00427 
00428   t = atof_ieee (input_line_pointer, type, words);
00429   if (t)
00430     input_line_pointer = t;
00431 
00432   *sizeP = prec * sizeof (LITTLENUM_TYPE);
00433 
00434   /* This loop outputs the LITTLENUMs in REVERSE order.  */
00435   for (wordP = words + prec - 1; prec--;)
00436     {
00437       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
00438       litP += sizeof (LITTLENUM_TYPE);
00439     }
00440 
00441   return NULL;
00442 }
00443 
00444 void
00445 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
00446                asection *sec ATTRIBUTE_UNUSED,
00447                fragS *fragP ATTRIBUTE_UNUSED)
00448 {
00449   abort ();
00450 }
00451 
00452 void
00453 md_begin (void)
00454 {
00455   unsigned int i;
00456   struct avr_opcodes_s *opcode;
00457 
00458   avr_hash = hash_new ();
00459 
00460   /* Insert unique names into hash table.  This hash table then provides a
00461      quick index to the first opcode with a particular name in the opcode
00462      table.  */
00463   for (opcode = avr_opcodes; opcode->name; opcode++)
00464     hash_insert (avr_hash, opcode->name, (char *) opcode);
00465 
00466   avr_mod_hash = hash_new ();
00467 
00468   for (i = 0; i < ARRAY_SIZE (exp_mod); ++i)
00469     {
00470       mod_index m;
00471 
00472       m.index = i + 10;
00473       hash_insert (avr_mod_hash, EXP_MOD_NAME (i), m.ptr);
00474     }
00475 
00476   bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
00477 }
00478 
00479 /* Resolve STR as a constant expression and return the result.
00480    If result greater than MAX then error.  */
00481 
00482 static unsigned int
00483 avr_get_constant (char *str, int max)
00484 {
00485   expressionS ex;
00486 
00487   str = skip_space (str);
00488   input_line_pointer = str;
00489   expression (& ex);
00490 
00491   if (ex.X_op != O_constant)
00492     as_bad (_("constant value required"));
00493 
00494   if (ex.X_add_number > max || ex.X_add_number < 0)
00495     as_bad (_("number must be less than %d"), max + 1);
00496 
00497   return ex.X_add_number;
00498 }
00499 
00500 /* Parse for ldd/std offset.  */
00501 
00502 static void
00503 avr_offset_expression (expressionS *exp)
00504 {
00505   char *str = input_line_pointer;
00506   char *tmp;
00507   char op[8];
00508 
00509   tmp = str;
00510   str = extract_word (str, op, sizeof (op));
00511 
00512   input_line_pointer = tmp;
00513   expression (exp);
00514 
00515   /* Warn about expressions that fail to use lo8 ().  */
00516   if (exp->X_op == O_constant)
00517     {
00518       int x = exp->X_add_number;
00519 
00520       if (x < -255 || x > 255)
00521        as_warn (_("constant out of 8-bit range: %d"), x);
00522     }
00523 }
00524 
00525 /* Parse ordinary expression.  */
00526 
00527 static char *
00528 parse_exp (char *s, expressionS *op)
00529 {
00530   input_line_pointer = s;
00531   expression (op);
00532   if (op->X_op == O_absent)
00533     as_bad (_("missing operand"));
00534   return input_line_pointer;
00535 }
00536 
00537 /* Parse special expressions (needed for LDI command):
00538    xx8 (address)
00539    xx8 (-address)
00540    pm_xx8 (address)
00541    pm_xx8 (-address)
00542    where xx is: hh, hi, lo.  */
00543 
00544 static bfd_reloc_code_real_type
00545 avr_ldi_expression (expressionS *exp)
00546 {
00547   char *str = input_line_pointer;
00548   char *tmp;
00549   char op[8];
00550   int mod;
00551   int linker_stubs_should_be_generated = 0;
00552 
00553   tmp = str;
00554 
00555   str = extract_word (str, op, sizeof (op));
00556 
00557   if (op[0])
00558     {
00559       mod_index m;
00560 
00561       m.ptr = hash_find (avr_mod_hash, op);
00562       mod = m.index;
00563 
00564       if (mod)
00565        {
00566          int closes = 0;
00567 
00568          mod -= 10;
00569          str = skip_space (str);
00570 
00571          if (*str == '(')
00572            {
00573              bfd_reloc_code_real_type  reloc_to_return;
00574              int neg_p = 0;
00575 
00576              ++str;
00577 
00578              if (strncmp ("pm(", str, 3) == 0
00579                   || strncmp ("gs(",str,3) == 0
00580                   || strncmp ("-(gs(",str,5) == 0
00581                 || strncmp ("-(pm(", str, 5) == 0)
00582               {
00583                 if (HAVE_PM_P (mod))
00584                   {
00585                     ++mod;
00586                     ++closes;
00587                   }
00588                 else
00589                   as_bad (_("illegal expression"));
00590 
00591                   if (str[0] == 'g' || str[2] == 'g')
00592                     linker_stubs_should_be_generated = 1;
00593 
00594                 if (*str == '-')
00595                   {
00596                     neg_p = 1;
00597                     ++closes;
00598                     str += 5;
00599                   }
00600                 else
00601                   str += 3;
00602               }
00603 
00604              if (*str == '-' && *(str + 1) == '(')
00605               {
00606                 neg_p ^= 1;
00607                 ++closes;
00608                 str += 2;
00609               }
00610 
00611              input_line_pointer = str;
00612              expression (exp);
00613 
00614              do
00615               {
00616                 if (*input_line_pointer != ')')
00617                   {
00618                     as_bad (_("`)' required"));
00619                     break;
00620                   }
00621                 input_line_pointer++;
00622               }
00623              while (closes--);
00624 
00625              reloc_to_return =
00626               neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
00627              if (linker_stubs_should_be_generated)
00628               {
00629                 switch (reloc_to_return)
00630                   {
00631                   case BFD_RELOC_AVR_LO8_LDI_PM:
00632                     reloc_to_return = BFD_RELOC_AVR_LO8_LDI_GS;
00633                     break;
00634                   case BFD_RELOC_AVR_HI8_LDI_PM:
00635                     reloc_to_return = BFD_RELOC_AVR_HI8_LDI_GS;
00636                     break;
00637 
00638                   default:
00639                     as_warn (_("expression dangerous with linker stubs"));
00640                   }
00641               }
00642              return reloc_to_return;
00643            }
00644        }
00645     }
00646 
00647   input_line_pointer = tmp;
00648   expression (exp);
00649 
00650   /* Warn about expressions that fail to use lo8 ().  */
00651   if (exp->X_op == O_constant)
00652     {
00653       int x = exp->X_add_number;
00654 
00655       if (x < -255 || x > 255)
00656        as_warn (_("constant out of 8-bit range: %d"), x);
00657     }
00658 
00659   return BFD_RELOC_AVR_LDI;
00660 }
00661 
00662 /* Parse one instruction operand.
00663    Return operand bitmask.  Also fixups can be generated.  */
00664 
00665 static unsigned int
00666 avr_operand (struct avr_opcodes_s *opcode,
00667             int where,
00668             char *op,
00669             char **line)
00670 {
00671   expressionS op_expr;
00672   unsigned int op_mask = 0;
00673   char *str = skip_space (*line);
00674 
00675   switch (*op)
00676     {
00677       /* Any register operand.  */
00678     case 'w':
00679     case 'd':
00680     case 'r':
00681     case 'a':
00682     case 'v':
00683       if (*str == 'r' || *str == 'R')
00684        {
00685          char r_name[20];
00686 
00687          str = extract_word (str, r_name, sizeof (r_name));
00688          op_mask = 0xff;
00689          if (ISDIGIT (r_name[1]))
00690            {
00691              if (r_name[2] == '\0')
00692               op_mask = r_name[1] - '0';
00693              else if (r_name[1] != '0'
00694                      && ISDIGIT (r_name[2])
00695                      && r_name[3] == '\0')
00696               op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
00697            }
00698        }
00699       else
00700        {
00701          op_mask = avr_get_constant (str, 31);
00702          str = input_line_pointer;
00703        }
00704 
00705       if (op_mask <= 31)
00706        {
00707          switch (*op)
00708            {
00709            case 'a':
00710              if (op_mask < 16 || op_mask > 23)
00711               as_bad (_("register r16-r23 required"));
00712              op_mask -= 16;
00713              break;
00714 
00715            case 'd':
00716              if (op_mask < 16)
00717               as_bad (_("register number above 15 required"));
00718              op_mask -= 16;
00719              break;
00720 
00721            case 'v':
00722              if (op_mask & 1)
00723               as_bad (_("even register number required"));
00724              op_mask >>= 1;
00725              break;
00726 
00727            case 'w':
00728              if ((op_mask & 1) || op_mask < 24)
00729               as_bad (_("register r24, r26, r28 or r30 required"));
00730              op_mask = (op_mask - 24) >> 1;
00731              break;
00732            }
00733          break;
00734        }
00735       as_bad (_("register name or number from 0 to 31 required"));
00736       break;
00737 
00738     case 'e':
00739       {
00740        char c;
00741 
00742        if (*str == '-')
00743          {
00744            str = skip_space (str + 1);
00745            op_mask = 0x1002;
00746          }
00747        c = TOLOWER (*str);
00748        if (c == 'x')
00749          op_mask |= 0x100c;
00750        else if (c == 'y')
00751          op_mask |= 0x8;
00752        else if (c != 'z')
00753          as_bad (_("pointer register (X, Y or Z) required"));
00754 
00755        str = skip_space (str + 1);
00756        if (*str == '+')
00757          {
00758            ++str;
00759            if (op_mask & 2)
00760              as_bad (_("cannot both predecrement and postincrement"));
00761            op_mask |= 0x1001;
00762          }
00763 
00764        /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
00765           registers, no predecrement, no postincrement.  */
00766        if (!avr_opt.all_opcodes && (op_mask & 0x100F)
00767            && !(avr_mcu->isa & AVR_ISA_SRAM))
00768          as_bad (_("addressing mode not supported"));
00769       }
00770       break;
00771 
00772     case 'z':
00773       if (*str == '-')
00774        as_bad (_("can't predecrement"));
00775 
00776       if (! (*str == 'z' || *str == 'Z'))
00777        as_bad (_("pointer register Z required"));
00778 
00779       str = skip_space (str + 1);
00780 
00781       if (*str == '+')
00782        {
00783          ++str;
00784          op_mask |= 1;
00785        }
00786       break;
00787 
00788     case 'b':
00789       {
00790        char c = TOLOWER (*str++);
00791 
00792        if (c == 'y')
00793          op_mask |= 0x8;
00794        else if (c != 'z')
00795          as_bad (_("pointer register (Y or Z) required"));
00796        str = skip_space (str);
00797        if (*str++ == '+')
00798          {
00799            input_line_pointer = str;
00800            avr_offset_expression (& op_expr);
00801            str = input_line_pointer;
00802            fix_new_exp (frag_now, where, 3,
00803                       &op_expr, FALSE, BFD_RELOC_AVR_6);
00804          }
00805       }
00806       break;
00807 
00808     case 'h':
00809       str = parse_exp (str, &op_expr);
00810       fix_new_exp (frag_now, where, opcode->insn_size * 2,
00811                  &op_expr, FALSE, BFD_RELOC_AVR_CALL);
00812       break;
00813 
00814     case 'L':
00815       str = parse_exp (str, &op_expr);
00816       fix_new_exp (frag_now, where, opcode->insn_size * 2,
00817                  &op_expr, TRUE, BFD_RELOC_AVR_13_PCREL);
00818       break;
00819 
00820     case 'l':
00821       str = parse_exp (str, &op_expr);
00822       fix_new_exp (frag_now, where, opcode->insn_size * 2,
00823                  &op_expr, TRUE, BFD_RELOC_AVR_7_PCREL);
00824       break;
00825 
00826     case 'i':
00827       str = parse_exp (str, &op_expr);
00828       fix_new_exp (frag_now, where + 2, opcode->insn_size * 2,
00829                  &op_expr, FALSE, BFD_RELOC_16);
00830       break;
00831 
00832     case 'M':
00833       {
00834        bfd_reloc_code_real_type r_type;
00835 
00836        input_line_pointer = str;
00837        r_type = avr_ldi_expression (&op_expr);
00838        str = input_line_pointer;
00839        fix_new_exp (frag_now, where, 3,
00840                    &op_expr, FALSE, r_type);
00841       }
00842       break;
00843 
00844     case 'n':
00845       {
00846        unsigned int x;
00847 
00848        x = ~avr_get_constant (str, 255);
00849        str = input_line_pointer;
00850        op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
00851       }
00852       break;
00853 
00854     case 'K':
00855       input_line_pointer = str;
00856       avr_offset_expression (& op_expr);
00857       str = input_line_pointer;
00858       fix_new_exp (frag_now, where, 3,
00859                  & op_expr, FALSE, BFD_RELOC_AVR_6_ADIW);
00860       break;
00861 
00862     case 'S':
00863     case 's':
00864       {
00865        unsigned int x;
00866 
00867        x = avr_get_constant (str, 7);
00868        str = input_line_pointer;
00869        if (*op == 'S')
00870          x <<= 4;
00871        op_mask |= x;
00872       }
00873       break;
00874 
00875     case 'P':
00876       {
00877        unsigned int x;
00878 
00879        x = avr_get_constant (str, 63);
00880        str = input_line_pointer;
00881        op_mask |= (x & 0xf) | ((x & 0x30) << 5);
00882       }
00883       break;
00884 
00885     case 'p':
00886       {
00887        unsigned int x;
00888 
00889        x = avr_get_constant (str, 31);
00890        str = input_line_pointer;
00891        op_mask |= x << 3;
00892       }
00893       break;
00894 
00895     case '?':
00896       break;
00897 
00898     default:
00899       as_bad (_("unknown constraint `%c'"), *op);
00900     }
00901 
00902   *line = str;
00903   return op_mask;
00904 }
00905 
00906 /* Parse instruction operands.
00907    Return binary opcode.  */
00908 
00909 static unsigned int
00910 avr_operands (struct avr_opcodes_s *opcode, char **line)
00911 {
00912   char *op = opcode->constraints;
00913   unsigned int bin = opcode->bin_opcode;
00914   char *frag = frag_more (opcode->insn_size * 2);
00915   char *str = *line;
00916   int where = frag - frag_now->fr_literal;
00917   static unsigned int prev = 0;  /* Previous opcode.  */
00918 
00919   /* Opcode have operands.  */
00920   if (*op)
00921     {
00922       unsigned int reg1 = 0;
00923       unsigned int reg2 = 0;
00924       int reg1_present = 0;
00925       int reg2_present = 0;
00926 
00927       /* Parse first operand.  */
00928       if (REGISTER_P (*op))
00929        reg1_present = 1;
00930       reg1 = avr_operand (opcode, where, op, &str);
00931       ++op;
00932 
00933       /* Parse second operand.  */
00934       if (*op)
00935        {
00936          if (*op == ',')
00937            ++op;
00938 
00939          if (*op == '=')
00940            {
00941              reg2 = reg1;
00942              reg2_present = 1;
00943            }
00944          else
00945            {
00946              if (REGISTER_P (*op))
00947               reg2_present = 1;
00948 
00949              str = skip_space (str);
00950              if (*str++ != ',')
00951               as_bad (_("`,' required"));
00952              str = skip_space (str);
00953 
00954              reg2 = avr_operand (opcode, where, op, &str);
00955            }
00956 
00957          if (reg1_present && reg2_present)
00958            reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
00959          else if (reg2_present)
00960            reg2 <<= 4;
00961        }
00962       if (reg1_present)
00963        reg1 <<= 4;
00964       bin |= reg1 | reg2;
00965     }
00966 
00967   /* Detect undefined combinations (like ld r31,Z+).  */
00968   if (!avr_opt.all_opcodes && AVR_UNDEF_P (bin))
00969     as_warn (_("undefined combination of operands"));
00970 
00971   if (opcode->insn_size == 2)
00972     {
00973       /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
00974          (AVR core bug, fixed in the newer devices).  */
00975       if (!(avr_opt.no_skip_bug ||
00976             (avr_mcu->isa & (AVR_ISA_MUL | AVR_ISA_MOVW)))
00977          && AVR_SKIP_P (prev))
00978        as_warn (_("skipping two-word instruction"));
00979 
00980       bfd_putl32 ((bfd_vma) bin, frag);
00981     }
00982   else
00983     bfd_putl16 ((bfd_vma) bin, frag);
00984 
00985   prev = bin;
00986   *line = str;
00987   return bin;
00988 }
00989 
00990 /* GAS will call this function for each section at the end of the assembly,
00991    to permit the CPU backend to adjust the alignment of a section.  */
00992 
00993 valueT
00994 md_section_align (asection *seg, valueT addr)
00995 {
00996   int align = bfd_get_section_alignment (stdoutput, seg);
00997   return ((addr + (1 << align) - 1) & (-1 << align));
00998 }
00999 
01000 /* If you define this macro, it should return the offset between the
01001    address of a PC relative fixup and the position from which the PC
01002    relative adjustment should be made.  On many processors, the base
01003    of a PC relative instruction is the next instruction, so this
01004    macro would return the length of an instruction.  */
01005 
01006 long
01007 md_pcrel_from_section (fixS *fixp, segT sec)
01008 {
01009   if (fixp->fx_addsy != (symbolS *) NULL
01010       && (!S_IS_DEFINED (fixp->fx_addsy)
01011          || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
01012     return 0;
01013 
01014   return fixp->fx_frag->fr_address + fixp->fx_where;
01015 }
01016 
01017 /* GAS will call this for each fixup.  It should store the correct
01018    value in the object file.  */
01019 
01020 void
01021 md_apply_fix (fixS *fixP, valueT * valP, segT seg)
01022 {
01023   unsigned char *where;
01024   unsigned long insn;
01025   long value = *valP;
01026 
01027   if (fixP->fx_addsy == (symbolS *) NULL)
01028     fixP->fx_done = 1;
01029 
01030   else if (fixP->fx_pcrel)
01031     {
01032       segT s = S_GET_SEGMENT (fixP->fx_addsy);
01033 
01034       if (s == seg || s == absolute_section)
01035        {
01036          value += S_GET_VALUE (fixP->fx_addsy);
01037          fixP->fx_done = 1;
01038        }
01039     }
01040 
01041   /* We don't actually support subtracting a symbol.  */
01042   if (fixP->fx_subsy != (symbolS *) NULL)
01043     as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
01044 
01045   switch (fixP->fx_r_type)
01046     {
01047     default:
01048       fixP->fx_no_overflow = 1;
01049       break;
01050     case BFD_RELOC_AVR_7_PCREL:
01051     case BFD_RELOC_AVR_13_PCREL:
01052     case BFD_RELOC_32:
01053     case BFD_RELOC_16:
01054     case BFD_RELOC_AVR_CALL:
01055       break;
01056     }
01057 
01058   if (fixP->fx_done)
01059     {
01060       /* Fetch the instruction, insert the fully resolved operand
01061         value, and stuff the instruction back again.  */
01062       where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
01063       insn = bfd_getl16 (where);
01064 
01065       switch (fixP->fx_r_type)
01066        {
01067        case BFD_RELOC_AVR_7_PCREL:
01068          if (value & 1)
01069            as_bad_where (fixP->fx_file, fixP->fx_line,
01070                        _("odd address operand: %ld"), value);
01071 
01072          /* Instruction addresses are always right-shifted by 1.  */
01073          value >>= 1;
01074          --value;                  /* Correct PC.  */
01075 
01076          if (value < -64 || value > 63)
01077            as_bad_where (fixP->fx_file, fixP->fx_line,
01078                        _("operand out of range: %ld"), value);
01079          value = (value << 3) & 0x3f8;
01080          bfd_putl16 ((bfd_vma) (value | insn), where);
01081          break;
01082 
01083        case BFD_RELOC_AVR_13_PCREL:
01084          if (value & 1)
01085            as_bad_where (fixP->fx_file, fixP->fx_line,
01086                        _("odd address operand: %ld"), value);
01087 
01088          /* Instruction addresses are always right-shifted by 1.  */
01089          value >>= 1;
01090          --value;                  /* Correct PC.  */
01091 
01092          if (value < -2048 || value > 2047)
01093            {
01094              /* No wrap for devices with >8K of program memory.  */
01095              if ((avr_mcu->isa & AVR_ISA_MEGA) || avr_opt.no_wrap)
01096               as_bad_where (fixP->fx_file, fixP->fx_line,
01097                            _("operand out of range: %ld"), value);
01098            }
01099 
01100          value &= 0xfff;
01101          bfd_putl16 ((bfd_vma) (value | insn), where);
01102          break;
01103 
01104        case BFD_RELOC_32:
01105          bfd_putl16 ((bfd_vma) value, where);
01106          break;
01107 
01108        case BFD_RELOC_16:
01109          bfd_putl16 ((bfd_vma) value, where);
01110          break;
01111 
01112        case BFD_RELOC_AVR_16_PM:
01113          bfd_putl16 ((bfd_vma) (value >> 1), where);
01114          break;
01115 
01116        case BFD_RELOC_AVR_LDI:
01117          if (value > 255)
01118            as_bad_where (fixP->fx_file, fixP->fx_line,
01119                        _("operand out of range: %ld"), value);
01120          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
01121          break;
01122 
01123        case BFD_RELOC_AVR_6:
01124          if ((value > 63) || (value < 0))
01125            as_bad_where (fixP->fx_file, fixP->fx_line,
01126                        _("operand out of range: %ld"), value);
01127          bfd_putl16 ((bfd_vma) insn | ((value & 7) | ((value & (3 << 3)) << 7) | ((value & (1 << 5)) << 8)), where);
01128          break;
01129 
01130        case BFD_RELOC_AVR_6_ADIW:
01131          if ((value > 63) || (value < 0))
01132            as_bad_where (fixP->fx_file, fixP->fx_line,
01133                        _("operand out of range: %ld"), value);
01134          bfd_putl16 ((bfd_vma) insn | (value & 0xf) | ((value & 0x30) << 2), where);
01135          break;
01136 
01137        case BFD_RELOC_AVR_LO8_LDI:
01138          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
01139          break;
01140 
01141        case BFD_RELOC_AVR_HI8_LDI:
01142          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
01143          break;
01144 
01145        case BFD_RELOC_AVR_MS8_LDI:
01146          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
01147          break;
01148 
01149        case BFD_RELOC_AVR_HH8_LDI:
01150          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
01151          break;
01152 
01153        case BFD_RELOC_AVR_LO8_LDI_NEG:
01154          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
01155          break;
01156 
01157        case BFD_RELOC_AVR_HI8_LDI_NEG:
01158          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
01159          break;
01160 
01161        case BFD_RELOC_AVR_MS8_LDI_NEG:
01162          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
01163          break;
01164 
01165        case BFD_RELOC_AVR_HH8_LDI_NEG:
01166          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
01167          break;
01168 
01169        case BFD_RELOC_AVR_LO8_LDI_PM:
01170          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
01171          break;
01172 
01173        case BFD_RELOC_AVR_HI8_LDI_PM:
01174          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
01175          break;
01176 
01177        case BFD_RELOC_AVR_HH8_LDI_PM:
01178          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
01179          break;
01180 
01181        case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
01182          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
01183          break;
01184 
01185        case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
01186          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
01187          break;
01188 
01189        case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
01190          bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
01191          break;
01192 
01193        case BFD_RELOC_AVR_CALL:
01194          {
01195            unsigned long x;
01196 
01197            x = bfd_getl16 (where);
01198            if (value & 1)
01199              as_bad_where (fixP->fx_file, fixP->fx_line,
01200                          _("odd address operand: %ld"), value);
01201            value >>= 1;
01202            x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
01203            bfd_putl16 ((bfd_vma) x, where);
01204            bfd_putl16 ((bfd_vma) (value & 0xffff), where + 2);
01205          }
01206          break;
01207 
01208        default:
01209          as_fatal (_("line %d: unknown relocation type: 0x%x"),
01210                   fixP->fx_line, fixP->fx_r_type);
01211          break;
01212        }
01213     }
01214   else
01215     {
01216       switch (fixP->fx_r_type)
01217        {
01218        case -BFD_RELOC_AVR_HI8_LDI_NEG:
01219        case -BFD_RELOC_AVR_HI8_LDI:
01220        case -BFD_RELOC_AVR_LO8_LDI_NEG:
01221        case -BFD_RELOC_AVR_LO8_LDI:
01222          as_bad_where (fixP->fx_file, fixP->fx_line,
01223                      _("only constant expression allowed"));
01224          fixP->fx_done = 1;
01225          break;
01226        default:
01227          break;
01228        }
01229     }
01230 }
01231 
01232 /* GAS will call this to generate a reloc, passing the resulting reloc
01233    to `bfd_install_relocation'.  This currently works poorly, as
01234    `bfd_install_relocation' often does the wrong thing, and instances of
01235    `tc_gen_reloc' have been written to work around the problems, which
01236    in turns makes it difficult to fix `bfd_install_relocation'.  */
01237 
01238 /* If while processing a fixup, a reloc really needs to be created
01239    then it is done here.  */
01240 
01241 arelent *
01242 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED,
01243              fixS *fixp)
01244 {
01245   arelent *reloc;
01246 
01247   if (fixp->fx_addsy && fixp->fx_subsy)
01248     {
01249       long value = 0;
01250 
01251       if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
01252           || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
01253         {
01254           as_bad_where (fixp->fx_file, fixp->fx_line,
01255               "Difference of symbols in different sections is not supported");
01256           return NULL;
01257         }
01258 
01259       /* We are dealing with two symbols defined in the same section.
01260          Let us fix-up them here.  */
01261       value += S_GET_VALUE (fixp->fx_addsy);
01262       value -= S_GET_VALUE (fixp->fx_subsy);
01263 
01264       /* When fx_addsy and fx_subsy both are zero, md_apply_fix
01265          only takes it's second operands for the fixup value.  */
01266       fixp->fx_addsy = NULL;
01267       fixp->fx_subsy = NULL;
01268       md_apply_fix (fixp, (valueT *) &value, NULL);
01269 
01270       return NULL;
01271     }
01272 
01273   reloc = xmalloc (sizeof (arelent));
01274 
01275   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
01276   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
01277 
01278   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
01279   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
01280   if (reloc->howto == (reloc_howto_type *) NULL)
01281     {
01282       as_bad_where (fixp->fx_file, fixp->fx_line,
01283                   _("reloc %d not supported by object file format"),
01284                   (int) fixp->fx_r_type);
01285       return NULL;
01286     }
01287 
01288   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
01289       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
01290     reloc->address = fixp->fx_offset;
01291 
01292   reloc->addend = fixp->fx_offset;
01293 
01294   return reloc;
01295 }
01296 
01297 void
01298 md_assemble (char *str)
01299 {
01300   struct avr_opcodes_s *opcode;
01301   char op[11];
01302 
01303   str = skip_space (extract_word (str, op, sizeof (op)));
01304 
01305   if (!op[0])
01306     as_bad (_("can't find opcode "));
01307 
01308   opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
01309 
01310   if (opcode == NULL)
01311     {
01312       as_bad (_("unknown opcode `%s'"), op);
01313       return;
01314     }
01315 
01316   /* Special case for opcodes with optional operands (lpm, elpm) -
01317      version with operands exists in avr_opcodes[] in the next entry.  */
01318 
01319   if (*str && *opcode->constraints == '?')
01320     ++opcode;
01321 
01322   if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa)
01323     as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
01324 
01325   /* We used to set input_line_pointer to the result of get_operands,
01326      but that is wrong.  Our caller assumes we don't change it.  */
01327   {
01328     char *t = input_line_pointer;
01329 
01330     avr_operands (opcode, &str);
01331     if (*skip_space (str))
01332       as_bad (_("garbage at end of line"));
01333     input_line_pointer = t;
01334   }
01335 }
01336 
01337 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
01338    `avr_cons_fix_new'.  */
01339 static int exp_mod_pm = 0;
01340 
01341 /* Parse special CONS expression: pm (expression)
01342    or alternatively: gs (expression).
01343    These are used for addressing program memory.
01344    Relocation: BFD_RELOC_AVR_16_PM.  */
01345 
01346 void
01347 avr_parse_cons_expression (expressionS *exp, int nbytes)
01348 {
01349   char *tmp;
01350 
01351   exp_mod_pm = 0;
01352 
01353   tmp = input_line_pointer = skip_space (input_line_pointer);
01354 
01355   if (nbytes == 2)
01356     {
01357       char *pm_name1 = "pm";
01358       char *pm_name2 = "gs";
01359       int len = strlen (pm_name1);
01360       /* len must be the same for both pm identifiers.  */
01361 
01362       if (strncasecmp (input_line_pointer, pm_name1, len) == 0
01363           || strncasecmp (input_line_pointer, pm_name2, len) == 0)
01364        {
01365          input_line_pointer = skip_space (input_line_pointer + len);
01366 
01367          if (*input_line_pointer == '(')
01368            {
01369              input_line_pointer = skip_space (input_line_pointer + 1);
01370              exp_mod_pm = 1;
01371              expression (exp);
01372 
01373              if (*input_line_pointer == ')')
01374               ++input_line_pointer;
01375              else
01376               {
01377                 as_bad (_("`)' required"));
01378                 exp_mod_pm = 0;
01379               }
01380 
01381              return;
01382            }
01383 
01384          input_line_pointer = tmp;
01385        }
01386     }
01387 
01388   expression (exp);
01389 }
01390 
01391 void
01392 avr_cons_fix_new (fragS *frag,
01393                 int where,
01394                 int nbytes,
01395                 expressionS *exp)
01396 {
01397   if (exp_mod_pm == 0)
01398     {
01399       if (nbytes == 2)
01400        fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_16);
01401       else if (nbytes == 4)
01402        fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_32);
01403       else
01404        as_bad (_("illegal %srelocation size: %d"), "", nbytes);
01405     }
01406   else
01407     {
01408       if (nbytes == 2)
01409        fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_AVR_16_PM);
01410       else
01411        as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes);
01412       exp_mod_pm = 0;
01413     }
01414 }