home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / config / tc-h8300.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  28KB  |  1,479 lines

  1. /* tc-h8300.c -- Assemble code for the Hitachi H8/300
  2.    Copyright (C) 1991, 1992 Free Software Foundation.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20.  
  21. /*
  22.   Written By Steve Chamberlain
  23.   sac@cygnus.com
  24.   */
  25.  
  26. #include <stdio.h>
  27. #include "as.h"
  28. #include "bfd.h"
  29. #define DEFINE_TABLE
  30. #define h8_opcodes ops
  31. #include "opcode/h8300.h"
  32. #include <ctype.h>
  33.  
  34. const char comment_chars[] =
  35. {';', 0};
  36. const char line_separator_chars[] =
  37. {0};
  38. const char line_comment_chars[] = "#";
  39.  
  40. /* This table describes all the machine specific pseudo-ops the assembler
  41.    has to support.  The fields are:
  42.    pseudo-op name without dot
  43.    function to call to execute this pseudo-op
  44.    Integer arg to pass to the function
  45.    */
  46.  
  47. void cons ();
  48.  
  49. int Hmode;
  50. #define PSIZE (Hmode ? L_32 : L_16)
  51. #define DMODE (L_16)
  52. #define DSYMMODE (Hmode ? L_24 : L_16)
  53. int bsize = L_8;        /* default branch displacement */
  54.  
  55.  
  56. void
  57. h8300hmode ()
  58. {
  59.   Hmode = 1;
  60. }
  61.  
  62. void
  63. sbranch (size)
  64.      int size;
  65. {
  66.   bsize = size;
  67. }
  68.  
  69. static void pint ()
  70. {
  71.   cons (Hmode ? 4 : 2);
  72. }
  73.  
  74. const pseudo_typeS md_pseudo_table[] =
  75. {
  76.  
  77.   {"h8300h", h8300hmode, 0},
  78.   {"sbranch", sbranch, L_8},
  79.   {"lbranch", sbranch, L_16},
  80.  
  81.   {"int", pint, 0},
  82.   {"data.b", cons, 1},
  83.   {"data.w", cons, 2},
  84.   {"data.l", cons, 4},
  85.   {"form", listing_psize, 0},
  86.   {"heading", listing_title, 0},
  87.   {"import", s_ignore, 0},
  88.   {"page", listing_eject, 0},
  89.   {"program", s_ignore, 0},
  90.   {0, 0, 0}
  91. };
  92.  
  93. const int md_reloc_size;
  94.  
  95. const char EXP_CHARS[] = "eE";
  96.  
  97. /* Chars that mean this number is a floating point constant */
  98. /* As in 0f12.456 */
  99. /* or    0d1.2345e12 */
  100. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  101.  
  102. static struct hash_control *opcode_hash_control;    /* Opcode mnemonics */
  103.  
  104. /*
  105.   This function is called once, at assembler startup time.  This should
  106.   set up all the tables, etc that the MD part of the assembler needs
  107.   */
  108.  
  109.  
  110. void
  111. md_begin ()
  112. {
  113.   struct h8_opcode *opcode;
  114.   char prev_buffer[100];
  115.   int idx = 0;
  116.  
  117.   opcode_hash_control = hash_new ();
  118.   prev_buffer[0] = 0;
  119.  
  120.   for (opcode = h8_opcodes; opcode->name; opcode++)
  121.     {
  122.       /* Strip off any . part when inserting the opcode and only enter
  123.          unique codes into the hash table
  124.          */
  125.       char *src = opcode->name;
  126.       unsigned int len = strlen (src);
  127.       char *dst = malloc (len + 1);
  128.       char *buffer = dst;
  129.  
  130.       opcode->size = 0;
  131.       while (*src)
  132.     {
  133.       if (*src == '.')
  134.         {
  135.           src++;
  136.           opcode->size = *src;
  137.           break;
  138.         }
  139.       *dst++ = *src++;
  140.     }
  141.       *dst++ = 0;
  142.       if (strcmp (buffer, prev_buffer))
  143.     {
  144.       hash_insert (opcode_hash_control, buffer, (char *) opcode);
  145.       strcpy (prev_buffer, buffer);
  146.       idx++;
  147.     }
  148.       opcode->idx = idx;
  149.  
  150.  
  151.       /* Find the number of operands */
  152.       opcode->noperands = 0;
  153.       while (opcode->args.nib[opcode->noperands] != E)
  154.     opcode->noperands++;
  155.       /* Find the length of the opcode in bytes */
  156.       opcode->length = 0;
  157.       while (opcode->data.nib[opcode->length * 2] != E)
  158.     opcode->length++;
  159.     }
  160.  
  161.   linkrelax = 1;
  162. }
  163.  
  164.  
  165. struct h8_exp
  166. {
  167.   char *e_beg;
  168.   char *e_end;
  169.   expressionS e_exp;
  170. };
  171. int dispreg;
  172. int opsize;            /* Set when a register size is seen */
  173.  
  174.  
  175. struct h8_op
  176. {
  177.   op_type mode;
  178.   unsigned reg;
  179.   expressionS exp;
  180. };
  181.  
  182. /*
  183.   parse operands
  184.   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
  185.   r0l,r0h,..r7l,r7h
  186.   @WREG
  187.   @WREG+
  188.   @-WREG
  189.   #const
  190.  
  191.   */
  192.  
  193. /* try and parse a reg name, returns number of chars consumed */
  194. int
  195. parse_reg (src, mode, reg, direction)
  196.      char *src;
  197.      op_type *mode;
  198.      unsigned int *reg;
  199.      int direction;
  200.  
  201. {
  202.   if (src[0] == 's' && src[1] == 'p')
  203.     {
  204.       *mode = PSIZE | REG | direction;
  205.       *reg = 7;
  206.       return 2;
  207.     }
  208.   if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
  209.     {
  210.       *mode = CCR;
  211.       *reg = 0;
  212.       return 3;
  213.     }
  214.   if (src[0] == 'f' && src[1] == 'p')
  215.     {
  216.       *mode = PSIZE | REG | direction;
  217.       *reg = 6;
  218.       return 2;
  219.     }
  220.   if (src[0] == 'e'
  221.       && src[1] == 'r'
  222.       && src[2] >= '0' && src[2] <= '7')
  223.     {
  224.       *mode = L_32 | REG | direction;
  225.       *reg = src[2] - '0';
  226.       if (!Hmode)
  227.     as_warn ("Reg not valid for H8/300");
  228.  
  229.       return 3;
  230.     }
  231.   if (src[0] == 'e'
  232.       && src[1] >= '0' && src[1] <= '7')
  233.     {
  234.       *mode = L_16 | REG | direction;
  235.       *reg = src[1] - '0' + 8;
  236.       if (!Hmode)
  237.     as_warn ("Reg not valid for H8/300");
  238.       return 2;
  239.     }
  240.  
  241.   if (src[0] == 'r')
  242.     {
  243.       if (src[1] >= '0' && src[1] <= '7')
  244.     {
  245.       if (src[2] == 'l')
  246.         {
  247.           *mode = L_8 | REG | direction;
  248.           *reg = (src[1] - '0') + 8;
  249.           return 3;
  250.         }
  251.       if (src[2] == 'h')
  252.         {
  253.           *mode = L_8 | REG | direction;
  254.           *reg = (src[1] - '0');
  255.           return 3;
  256.         }
  257.       *mode = L_16 | REG | direction;
  258.       *reg = (src[1] - '0');
  259.       return 2;
  260.     }
  261.     }
  262.   return 0;
  263. }
  264.  
  265. char *
  266. parse_exp (s, op)
  267.      char *s;
  268.      expressionS * op;
  269. {
  270.   char *save = input_line_pointer;
  271.   char *new;
  272.  
  273.   input_line_pointer = s;
  274.   expression (op);
  275.   if (op->X_op == O_absent)
  276.     as_bad ("missing operand");
  277.   new = input_line_pointer;
  278.   input_line_pointer = save;
  279.   return new;
  280. }
  281.  
  282. static char *
  283. skip_colonthing (ptr, exp, mode)
  284.      char *ptr;
  285.      expressionS *exp;
  286.      int *mode;
  287. {
  288.   if (*ptr == ':')
  289.     {
  290.       ptr++;
  291.       *mode &= ~SIZE;
  292.       if (*ptr == '8')
  293.     {
  294.       ptr++;
  295.       /* ff fill any 8 bit quantity */
  296.      /* exp->X_add_number -= 0x100;*/
  297.       *mode |= L_8;
  298.     }
  299.       else
  300.     {
  301.       if (*ptr == '2')
  302.         {
  303.           *mode |= L_24;
  304.         }
  305.       else if (*ptr == '3')
  306.         {
  307.           *mode |= L_32;
  308.         }
  309.       else if (*ptr == '1')
  310.         {
  311.           *mode |= L_16;
  312.         }
  313.       while (isdigit (*ptr))
  314.         ptr++;
  315.     }
  316.     }
  317.   return ptr;
  318. }
  319.  
  320. /* The many forms of operand:
  321.  
  322.    Rn            Register direct
  323.    @Rn            Register indirect
  324.    @(exp[:16], Rn)    Register indirect with displacement
  325.    @Rn+
  326.    @-Rn
  327.    @aa:8            absolute 8 bit
  328.    @aa:16            absolute 16 bit
  329.    @aa            absolute 16 bit
  330.  
  331.    #xx[:size]        immediate data
  332.    @(exp:[8], pc)        pc rel
  333.    @@aa[:8]        memory indirect
  334.  
  335.    */
  336.  
  337. char *
  338. colonmod24 (op, src)
  339.      struct h8_op *op;
  340.      char *src;
  341.  
  342. {
  343.   int mode = 0;
  344.   src = skip_colonthing (src, &op->exp, &mode);
  345.  
  346.   if (!mode)
  347.     {
  348.       /* Choose a default mode */
  349.       if (op->exp.X_add_number < -32768
  350.       || op->exp.X_add_number > 32767)
  351.     {
  352.       if (Hmode)
  353.         mode = L_24;
  354.       else
  355.         mode = L_16;
  356.     }
  357.       else if (op->exp.X_add_symbol
  358.            || op->exp.X_op_symbol)
  359.     mode = DSYMMODE;
  360.       else
  361.     mode = DMODE;
  362.     }
  363.   op->mode |= mode;
  364.   return src;
  365.  
  366. }
  367.  
  368.  
  369. static void
  370. get_operand (ptr, op, dst, direction)
  371.      char **ptr;
  372.      struct h8_op *op;
  373.      unsigned int dst;
  374.      int direction;
  375. {
  376.   char *src = *ptr;
  377.   op_type mode;
  378.   unsigned int num;
  379.   unsigned int len;
  380.  
  381.   op->mode = E;
  382.  
  383.  
  384.   len = parse_reg (src, &op->mode, &op->reg, direction);
  385.   if (len)
  386.     {
  387.       *ptr = src + len;
  388.       return;
  389.     }
  390.  
  391.   if (*src == '@')
  392.     {
  393.       src++;
  394.       if (*src == '@')
  395.     {
  396.       src++;
  397.       src = parse_exp (src, &op->exp);
  398.  
  399.       src = skip_colonthing (src, &op->exp, &op->mode);
  400.  
  401.       *ptr = src;
  402.  
  403.       op->mode = MEMIND;
  404.       return;
  405.  
  406.     }
  407.  
  408.  
  409.       if (*src == '-')
  410.     {
  411.       src++;
  412.       len = parse_reg (src, &mode, &num, direction);
  413.       if (len == 0)
  414.         {
  415.           /* Oops, not a reg after all, must be ordinary exp */
  416.           src--;
  417.           /* must be a symbol */
  418.           op->mode = ABS | PSIZE | direction;
  419.           *ptr = skip_colonthing (parse_exp (src, &op->exp),
  420.                       &op->exp, &op->mode);
  421.  
  422.           return;
  423.  
  424.  
  425.         }
  426.  
  427.  
  428.       if ((mode & SIZE) != PSIZE)
  429.         as_bad ("Wrong size pointer register for architecture.");
  430.       op->mode = RDDEC;
  431.       op->reg = num;
  432.       *ptr = src + len;
  433.       return;
  434.     }
  435.       if (*src == '(')
  436.     {
  437.       /* Disp */
  438.       src++;
  439.  
  440.       /* Start off assuming a 16 bit offset */
  441.  
  442.  
  443.       src = parse_exp (src, &op->exp);
  444.  
  445.       src = colonmod24 (op, src);
  446.  
  447.       if (*src == ')')
  448.         {
  449.           src++;
  450.           op->mode |= ABS | direction;
  451.           *ptr = src;
  452.           return;
  453.         }
  454.  
  455.       if (*src != ',')
  456.         {
  457.           as_bad ("expected @(exp, reg16)");
  458.           return;
  459.  
  460.         }
  461.       src++;
  462.  
  463.       len = parse_reg (src, &mode, &op->reg, direction);
  464.       if (len == 0 || !(mode & REG))
  465.         {
  466.           as_bad ("expected @(exp, reg16)");
  467.           return;
  468.         }
  469.       op->mode |= DISP | direction;
  470.       dispreg = op->reg;
  471.       src += len;
  472.       src = skip_colonthing (src, &op->exp, &op->mode);
  473.  
  474.       if (*src != ')' && '(')
  475.         {
  476.           as_bad ("expected @(exp, reg16)");
  477.           return;
  478.         }
  479.       *ptr = src + 1;
  480.  
  481.       return;
  482.     }
  483.       len = parse_reg (src, &mode, &num, direction);
  484.  
  485.       if (len)
  486.     {
  487.       src += len;
  488.       if (*src == '+')
  489.         {
  490.           src++;
  491.           if ((mode & SIZE) != PSIZE)
  492.         as_bad ("Wrong size pointer register for architecture.");
  493.           op->mode = RSINC;
  494.           op->reg = num;
  495.           *ptr = src;
  496.           return;
  497.         }
  498.       if ((mode & SIZE) != PSIZE)
  499.         as_bad ("Wrong size pointer register for architecture.");
  500.  
  501.       op->mode = direction | IND | PSIZE;
  502.       op->reg = num;
  503.       *ptr = src;
  504.  
  505.       return;
  506.     }
  507.       else
  508.     {
  509.       /* must be a symbol */
  510.  
  511.       op->mode = ABS | direction;
  512.       src = parse_exp (src, &op->exp);
  513.  
  514.       *ptr = colonmod24 (op, src);
  515.  
  516.       return;
  517.     }
  518.     }
  519.  
  520.  
  521.   if (*src == '#')
  522.     {
  523.       src++;
  524.       op->mode = IMM;
  525.       src = parse_exp (src, &op->exp);
  526.       *ptr = skip_colonthing (src, &op->exp, &op->mode);
  527.  
  528.       return;
  529.     }
  530.   else
  531.     {
  532.       src = parse_exp (src, &op->exp);
  533.       /* Trailing ':' size ? */
  534.       if (*src == ':')
  535.     {
  536.       if (src[1] == '1' && src[2] == '6')
  537.         {
  538.           op->mode = PCREL | L_16;
  539.           src += 3;
  540.         }
  541.       else if (src[1] == '8')
  542.         {
  543.           op->mode = PCREL | L_8;
  544.           src += 2;
  545.         }
  546.       else
  547.         {
  548.           as_bad ("expect :8 or :16 here");
  549.         }
  550.     }
  551.       else
  552.     {
  553.       op->mode = PCREL | bsize;
  554.     }
  555.       *ptr = src;
  556.     }
  557. }
  558.  
  559.  
  560. static
  561. char *
  562. get_operands (noperands, op_end, operand)
  563.      unsigned int noperands;
  564.      char *op_end;
  565.      struct h8_op *operand;
  566. {
  567.   char *ptr = op_end;
  568.  
  569.   switch (noperands)
  570.     {
  571.     case 0:
  572.       operand[0].mode = 0;
  573.       operand[1].mode = 0;
  574.       break;
  575.  
  576.     case 1:
  577.       ptr++;
  578.       get_operand (&ptr, operand + 0, 0, SRC);
  579.       if (*ptr == ',')
  580.     {
  581.       ptr++;
  582.       get_operand (&ptr, operand + 1, 1, DST);
  583.     }
  584.       else
  585.     {
  586.       operand[1].mode = 0;
  587.     }
  588.  
  589.       break;
  590.     case 2:
  591.       ptr++;
  592.       get_operand (&ptr, operand + 0, 0, SRC);
  593.       if (*ptr == ',')
  594.     ptr++;
  595.       get_operand (&ptr, operand + 1, 1, DST);
  596.       break;
  597.  
  598.     default:
  599.       abort ();
  600.     }
  601.  
  602.  
  603.   return ptr;
  604. }
  605.  
  606. /* Passed a pointer to a list of opcodes which use different
  607.    addressing modes, return the opcode which matches the opcodes
  608.    provided
  609.    */
  610. static
  611. struct h8_opcode *
  612. get_specific (opcode, operands)
  613.      struct h8_opcode *opcode;
  614.      struct h8_op *operands;
  615. {
  616.   struct h8_opcode *this_try = opcode;
  617.   int found = 0;
  618.  
  619.   unsigned int this_index = opcode->idx;
  620.  
  621.   /* There's only one ldm/stm and it's easier to just
  622.      get out quick for them.  */
  623.   if (strcmp (opcode->name, "stm.l") == 0
  624.       || strcmp (opcode->name, "ldm.l") == 0)
  625.     return this_try;
  626.  
  627.   while (this_index == opcode->idx && !found)
  628.     {
  629.       unsigned int i;
  630.       found = 1;
  631.  
  632.       this_try = opcode++;
  633.       for (i = 0; i < this_try->noperands && found; i++)
  634.     {
  635.       op_type op = this_try->args.nib[i];
  636.       int x = operands[i].mode;
  637.  
  638.       if ((op & (DISP | REG)) == (DISP | REG)
  639.           && ((x & (DISP | REG)) == (DISP | REG)))
  640.         {
  641.           dispreg = operands[i].reg;
  642.         }
  643.       else if (op & REG)
  644.         {
  645.           if (!(x & REG))
  646.         found = 0;
  647.  
  648.           if (x & L_P)
  649.         {
  650.           x = (x & ~L_P) | (Hmode ? L_32 : L_16);
  651.         }
  652.           if (op & L_P)
  653.         {
  654.           op = (op & ~L_P) | (Hmode ? L_32 : L_16);
  655.         }
  656.  
  657.           opsize = op & SIZE;
  658.  
  659.           /* The size of the reg is v important */
  660.           if ((op & SIZE) != (x & SIZE))
  661.         found = 0;
  662.         }
  663.       else if ((op & ABSJMP) && (x & ABS))
  664.         {
  665.           operands[i].mode &= ~ABS;
  666.           operands[i].mode |= ABSJMP;
  667.           /* But it may not be 24 bits long */
  668.           if (!Hmode)
  669.         {
  670.           operands[i].mode &= ~SIZE;
  671.           operands[i].mode |= L_16;
  672.         }
  673.  
  674.  
  675.         }
  676.       else if ((op & (KBIT | DBIT)) && (x & IMM))
  677.         {
  678.           /* This is ok if the immediate value is sensible */
  679.  
  680.         }
  681.       else if (op & PCREL)
  682.         {
  683.  
  684.           /* The size of the displacement is important */
  685.           if ((op & SIZE) != (x & SIZE))
  686.         found = 0;
  687.  
  688.         }
  689.       else if ((op & (DISP | IMM | ABS))
  690.            && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
  691.         {
  692.           /* Promote a L_24 to L_32 if it makes us match.  */
  693.           if ((x & L_24) && (op & L_32))
  694.         {
  695.           x &= ~L_24;
  696.           x |= L_32;
  697.         }
  698.           /* Promote an L8 to L_16 if it makes us match.  */
  699.           if (op & ABS && op & L_8 && op & DISP) 
  700.         {
  701.           if (x & L_16)
  702.             found= 1;
  703.         }
  704.           else if ((x & SIZE) != 0
  705.                && ((op & SIZE) != (x & SIZE)))
  706.         found = 0;
  707.         }
  708.       else if ((op & MODE) != (x & MODE))
  709.         {
  710.           found = 0;
  711.         }
  712.  
  713.     }
  714.     }
  715.   if (found)
  716.     return this_try;
  717.   else
  718.     return 0;
  719. }
  720.  
  721. static void
  722. check_operand (operand, width, string)
  723.      struct h8_op *operand;
  724.      unsigned int width;
  725.      char *string;
  726. {
  727.   if (operand->exp.X_add_symbol == 0
  728.       && operand->exp.X_op_symbol == 0)
  729.     {
  730.  
  731.       /* No symbol involved, let's look at offset, it's dangerous if any of
  732.      the high bits are not 0 or ff's, find out by oring or anding with
  733.      the width and seeing if the answer is 0 or all fs*/
  734.       
  735.       if ((operand->exp.X_add_number & ~width) != 0 &&
  736.       (operand->exp.X_add_number | width) != (~0))
  737.     {
  738.       if (width == 255 
  739.           && (operand->exp.X_add_number & 0xff00) == 0xff00)
  740.         {
  741.           /* Just ignore this one - which happens when trying to
  742.          fit a 16 bit address truncated into an 8 bit address
  743.          of something like bset.  */
  744.         }
  745.       else 
  746.         {
  747.           as_warn ("operand %s0x%lx out of range.", string,
  748.                (unsigned long) operand->exp.X_add_number);
  749.         }
  750.     }
  751.     }
  752.  
  753. }
  754.  
  755. /* RELAXMODE has one of 3 values:
  756.  
  757.    0 Output a "normal" reloc, no relaxing possible for this insn/reloc
  758.  
  759.    1 Output a relaxable 24bit absolute mov.w address relocation
  760.      (may relax into a 16bit absolute address).
  761.  
  762.    2 Output a relaxable 16/24 absolute mov.b address relocation
  763.      (may relax into an 8bit absolute address).  */
  764.  
  765. static void
  766. do_a_fix_imm (offset, operand, relaxmode)
  767.      int offset;
  768.      struct h8_op *operand;
  769.      int relaxmode;
  770. {
  771.   int idx;
  772.   int size;
  773.   int where;
  774.  
  775.  
  776.   char *t = operand->mode & IMM ? "#" : "@";
  777.  
  778.   if (operand->exp.X_add_symbol == 0)
  779.     {
  780.       char *bytes = frag_now->fr_literal + offset;
  781.       switch (operand->mode & SIZE)
  782.     {
  783.     case L_2:
  784.       check_operand (operand, 0x3, t);
  785.       bytes[0] |= (operand->exp.X_add_number) << 4;
  786.       break;
  787.     case L_3:
  788.       check_operand (operand, 0x7, t);
  789.       bytes[0] |= (operand->exp.X_add_number) << 4;
  790.       break;
  791.     case L_8:
  792.       check_operand (operand, 0xff, t);
  793.       bytes[0] = operand->exp.X_add_number;
  794.       break;
  795.     case L_16:
  796.       check_operand (operand, 0xffff, t);
  797.       bytes[0] = operand->exp.X_add_number >> 8;
  798.       bytes[1] = operand->exp.X_add_number >> 0;
  799.       break;
  800.     case L_24:
  801.       check_operand (operand, 0xffffff, t);
  802.       bytes[0] = operand->exp.X_add_number >> 16;
  803.       bytes[1] = operand->exp.X_add_number >> 8;
  804.       bytes[2] = operand->exp.X_add_number >> 0;
  805.       break;
  806.  
  807.     case L_32:
  808.       /* This should be done with bfd */
  809.       bytes[0] = operand->exp.X_add_number >> 24;
  810.       bytes[1] = operand->exp.X_add_number >> 16;
  811.       bytes[2] = operand->exp.X_add_number >> 8;
  812.       bytes[3] = operand->exp.X_add_number >> 0;
  813.       break;
  814.     }
  815.  
  816.     }
  817.   else
  818.     {
  819.       switch (operand->mode & SIZE)
  820.     {
  821.  
  822.     case L_24:
  823.     case L_32:
  824.       size = 4;
  825.       where = (operand->mode & SIZE) == L_24 ? -1 : 0;
  826.       if (relaxmode == 2)
  827.         idx = R_MOV24B1;
  828.       else if (relaxmode == 1)
  829.         idx = R_MOVL1;
  830.       else
  831.         idx = R_RELLONG;
  832.       break;
  833.     default:
  834.       as_bad("Can't work out size of operand.\n");
  835.     case L_16:
  836.       size = 2;
  837.       where = 0;
  838.       if (relaxmode == 2)
  839.         idx = R_MOV16B1;
  840.       else
  841.         idx = R_RELWORD;
  842.       operand->exp.X_add_number = (short)operand->exp.X_add_number;
  843.       break;
  844.     case L_8:
  845.       size = 1;
  846.       where = 0;
  847.       idx = R_RELBYTE;
  848.       operand->exp.X_add_number = (char)operand->exp.X_add_number;
  849.     }
  850.  
  851.       fix_new_exp (frag_now,
  852.            offset + where,
  853.            size,
  854.            &operand->exp,
  855.            0,
  856.            idx);
  857.     }
  858.  
  859. }
  860.  
  861. /* Now we know what sort of opcodes it is, lets build the bytes -
  862.  */
  863. static void
  864. build_bytes (this_try, operand)
  865.      struct h8_opcode *this_try;
  866.      struct h8_op *operand;
  867. {
  868.   unsigned int i;
  869.  
  870.   char *output = frag_more (this_try->length);
  871.   op_type *nibble_ptr = this_try->data.nib;
  872.   op_type c;
  873.   unsigned int nibble_count = 0;
  874.   int absat;
  875.   int immat;
  876.   int nib;
  877.   int movb = 0;
  878.   char asnibbles[30];
  879.   char *p = asnibbles;
  880.  
  881.   if (!(this_try->inbase || Hmode))
  882.     as_warn ("Opcode `%s' not available in H8/300 mode", this_try->name);
  883.  
  884.   while (*nibble_ptr != E)
  885.     {
  886.       int d;
  887.       c = *nibble_ptr++;
  888.  
  889.       d = (c & (DST | SRC_IN_DST)) != 0;
  890.  
  891.       if (c < 16)
  892.     {
  893.       nib = c;
  894.     }
  895.       else
  896.     {
  897.  
  898.       if (c & (REG | IND | INC | DEC))
  899.         {
  900.           nib = operand[d].reg;
  901.         }
  902.       else if ((c & DISPREG) == (DISPREG))
  903.         {
  904.           nib = dispreg;
  905.         }
  906.       else if (c &  ABS )
  907.         {
  908.           operand[d].mode = c;
  909.           absat = nibble_count / 2;
  910.           nib = 0;
  911.         }
  912.       else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
  913.         {
  914.           operand[d].mode = c;
  915.           immat = nibble_count / 2;
  916.           nib = 0;
  917.         }
  918.       else if (c & IGNORE)
  919.         {
  920.           nib = 0;
  921.         }
  922.       else if (c & DBIT)
  923.         {
  924.           switch (operand[0].exp.X_add_number)
  925.         {
  926.         case 1:
  927.           nib = c;
  928.           break;
  929.         case 2:
  930.           nib = 0x8 | c;
  931.           break;
  932.         default:
  933.           as_bad ("Need #1 or #2 here");
  934.         }
  935.         }
  936.       else if (c & KBIT)
  937.         {
  938.           switch (operand[0].exp.X_add_number)
  939.         {
  940.         case 1:
  941.           nib = 0;
  942.           break;
  943.         case 2:
  944.           nib = 8;
  945.           break;
  946.         case 4:
  947.           if (!Hmode)
  948.             as_warn ("#4 not valid on H8/300.");
  949.           nib = 9;
  950.           break;
  951.  
  952.         default:
  953.           as_bad ("Need #1 or #2 here");
  954.           break;
  955.         }
  956.           /* stop it making a fix */
  957.           operand[0].mode = 0;
  958.         }
  959.  
  960.       if (c & MEMRELAX)
  961.         {
  962.           operand[d].mode |= MEMRELAX;
  963.         }
  964.  
  965.       if (c & B31)
  966.         {
  967.           nib |= 0x8;
  968.         }
  969.  
  970.     }
  971.       nibble_count++;
  972.  
  973.       *p++ = nib;
  974.     }
  975.  
  976.  
  977.   for (i = 0; i < this_try->length; i++)
  978.     {
  979.       output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
  980.     }
  981.  
  982.   /* Note if this is a movb instruction -- there's a special relaxation
  983.      which only applies to them.  */
  984.   if (strcmp (this_try->name, "mov.b") == 0)
  985.     movb = 1;
  986.  
  987.   /* output any fixes */
  988.   for (i = 0; i < 2; i++)
  989.     {
  990.       int x = operand[i].mode;
  991.  
  992.       if (x & (IMM | DISP))
  993.     {
  994.       do_a_fix_imm (output - frag_now->fr_literal + immat,
  995.             operand + i, x & MEMRELAX != 0);
  996.     }
  997.       else if (x & ABS)
  998.     {
  999.       do_a_fix_imm (output - frag_now->fr_literal + absat,
  1000.             operand + i, x & MEMRELAX ? movb + 1 : 0);
  1001.     }
  1002.       else if (x & PCREL)
  1003.     {
  1004.       int size16 = x & L_16;
  1005.       int where = size16 ? 2 : 1;
  1006.       int size = size16 ? 2 : 1;
  1007.       int type = size16 ? R_PCRWORD : R_PCRBYTE;
  1008.  
  1009.       check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
  1010.  
  1011.       if (operand[i].exp.X_add_number & 1)
  1012.         {
  1013.           as_warn ("branch operand has odd offset (%lx)\n",
  1014.                (unsigned long) operand->exp.X_add_number);
  1015.         }
  1016.  
  1017.       operand[i].exp.X_add_number =
  1018.         (char) (operand[i].exp.X_add_number - 1);
  1019.       fix_new_exp (frag_now,
  1020.                output - frag_now->fr_literal + where,
  1021.                size,
  1022.                &operand[i].exp,
  1023.                1,
  1024.                type);
  1025.     }
  1026.       else if (x & MEMIND)
  1027.     {
  1028.  
  1029.       check_operand (operand + i, 0xff, "@@");
  1030.       fix_new_exp (frag_now,
  1031.                output - frag_now->fr_literal + 1,
  1032.                1,
  1033.                &operand[i].exp,
  1034.                0,
  1035.                R_MEM_INDIRECT);
  1036.     }
  1037.       else if (x & ABSJMP)
  1038.     {
  1039.       /* This jmp may be a jump or a branch */
  1040.  
  1041.       check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
  1042.       if (operand[i].exp.X_add_number & 1)
  1043.         {
  1044.           as_warn ("branch operand has odd offset (%lx)\n",
  1045.                (unsigned long) operand->exp.X_add_number);
  1046.         }
  1047.       if (!Hmode)
  1048.         operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
  1049.       fix_new_exp (frag_now,
  1050.                output - frag_now->fr_literal,
  1051.                4,
  1052.                &operand[i].exp,
  1053.                0,
  1054.                R_JMPL1);
  1055.     }
  1056.     }
  1057.  
  1058. }
  1059.  
  1060. /*
  1061.   try and give an intelligent error message for common and simple to
  1062.   detect errors
  1063.   */
  1064.  
  1065. static void
  1066. clever_message (opcode, operand)
  1067.      struct h8_opcode *opcode;
  1068.      struct h8_op *operand;
  1069. {
  1070.   /* Find out if there was more than one possible opccode */
  1071.  
  1072.   if ((opcode + 1)->idx != opcode->idx)
  1073.     {
  1074.       unsigned int argn;
  1075.  
  1076.       /* Only one opcode of this flavour, try and guess which operand
  1077.          didn't match */
  1078.       for (argn = 0; argn < opcode->noperands; argn++)
  1079.     {
  1080.       switch (opcode->args.nib[argn])
  1081.         {
  1082.         case RD16:
  1083.           if (operand[argn].mode != RD16)
  1084.         {
  1085.           as_bad ("destination operand must be 16 bit register");
  1086.           return;
  1087.  
  1088.         }
  1089.           break;
  1090.  
  1091.         case RS8:
  1092.  
  1093.           if (operand[argn].mode != RS8)
  1094.         {
  1095.           as_bad ("source operand must be 8 bit register");
  1096.           return;
  1097.         }
  1098.           break;
  1099.  
  1100.         case ABS16DST:
  1101.           if (operand[argn].mode != ABS16DST)
  1102.         {
  1103.           as_bad ("destination operand must be 16bit absolute address");
  1104.           return;
  1105.         }
  1106.           break;
  1107.         case RD8:
  1108.           if (operand[argn].mode != RD8)
  1109.         {
  1110.           as_bad ("destination operand must be 8 bit register");
  1111.           return;
  1112.         }
  1113.           break;
  1114.  
  1115.  
  1116.         case ABS16SRC:
  1117.           if (operand[argn].mode != ABS16SRC)
  1118.         {
  1119.           as_bad ("source operand must be 16bit absolute address");
  1120.           return;
  1121.         }
  1122.           break;
  1123.  
  1124.         }
  1125.     }
  1126.     }
  1127.   as_bad ("invalid operands");
  1128. }
  1129.  
  1130. /* This is the guts of the machine-dependent assembler.  STR points to a
  1131.    machine dependent instruction.  This funciton is supposed to emit
  1132.    the frags/bytes it assembles to.
  1133.    */
  1134.  
  1135.  
  1136.  
  1137. void
  1138. md_assemble (str)
  1139.      char *str;
  1140. {
  1141.   char *op_start;
  1142.   char *op_end;
  1143.   struct h8_op operand[2];
  1144.   struct h8_opcode *opcode;
  1145.   struct h8_opcode *prev_opcode;
  1146.  
  1147.   char *dot = 0;
  1148.   char c;
  1149.  
  1150.   /* Drop leading whitespace */
  1151.   while (*str == ' ')
  1152.     str++;
  1153.  
  1154.   /* find the op code end */
  1155.   for (op_start = op_end = str;
  1156.        *op_end != 0 && *op_end != ' ';
  1157.        op_end++)
  1158.     {
  1159.       if (*op_end == '.')
  1160.     {
  1161.       dot = op_end + 1;
  1162.       *op_end = 0;
  1163.       op_end += 2;
  1164.       break;
  1165.     }
  1166.     }
  1167.  
  1168.   ;
  1169.  
  1170.   if (op_end == op_start)
  1171.     {
  1172.       as_bad ("can't find opcode ");
  1173.     }
  1174.   c = *op_end;
  1175.  
  1176.   *op_end = 0;
  1177.  
  1178.   opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
  1179.                        op_start);
  1180.  
  1181.   if (opcode == NULL)
  1182.     {
  1183.       as_bad ("unknown opcode");
  1184.       return;
  1185.     }
  1186.  
  1187.   /* We use to set input_line_pointer to the result of get_operands,
  1188.      but that is wrong.  Our caller assumes we don't change it.  */
  1189.  
  1190.   (void) get_operands (opcode->noperands, op_end, operand);
  1191.   *op_end = c;
  1192.   prev_opcode = opcode;
  1193.  
  1194.   opcode = get_specific (opcode, operand);
  1195.  
  1196.   if (opcode == 0)
  1197.     {
  1198.       /* Couldn't find an opcode which matched the operands */
  1199.       char *where = frag_more (2);
  1200.  
  1201.       where[0] = 0x0;
  1202.       where[1] = 0x0;
  1203.       clever_message (prev_opcode, operand);
  1204.  
  1205.       return;
  1206.     }
  1207.   if (opcode->size && dot)
  1208.     {
  1209.       if (opcode->size != *dot)
  1210.     {
  1211.       as_warn ("mismatch between opcode size and operand size");
  1212.     }
  1213.     }
  1214.  
  1215.   build_bytes (opcode, operand);
  1216.  
  1217. }
  1218.  
  1219. void
  1220. tc_crawl_symbol_chain (headers)
  1221.      object_headers * headers;
  1222. {
  1223.   printf ("call to tc_crawl_symbol_chain \n");
  1224. }
  1225.  
  1226. symbolS *
  1227. md_undefined_symbol (name)
  1228.      char *name;
  1229. {
  1230.   return 0;
  1231. }
  1232.  
  1233. void
  1234. tc_headers_hook (headers)
  1235.      object_headers * headers;
  1236. {
  1237.   printf ("call to tc_headers_hook \n");
  1238. }
  1239.  
  1240. /* Various routines to kill one day */
  1241. /* Equal to MAX_PRECISION in atof-ieee.c */
  1242. #define MAX_LITTLENUMS 6
  1243.  
  1244. /* Turn a string in input_line_pointer into a floating point constant of type
  1245.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  1246.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  1247.    */
  1248. char *
  1249. md_atof (type, litP, sizeP)
  1250.      char type;
  1251.      char *litP;
  1252.      int *sizeP;
  1253. {
  1254.   int prec;
  1255.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  1256.   LITTLENUM_TYPE *wordP;
  1257.   char *t;
  1258.   char *atof_ieee ();
  1259.  
  1260.   switch (type)
  1261.     {
  1262.     case 'f':
  1263.     case 'F':
  1264.     case 's':
  1265.     case 'S':
  1266.       prec = 2;
  1267.       break;
  1268.  
  1269.     case 'd':
  1270.     case 'D':
  1271.     case 'r':
  1272.     case 'R':
  1273.       prec = 4;
  1274.       break;
  1275.  
  1276.     case 'x':
  1277.     case 'X':
  1278.       prec = 6;
  1279.       break;
  1280.  
  1281.     case 'p':
  1282.     case 'P':
  1283.       prec = 6;
  1284.       break;
  1285.  
  1286.     default:
  1287.       *sizeP = 0;
  1288.       return "Bad call to MD_ATOF()";
  1289.     }
  1290.   t = atof_ieee (input_line_pointer, type, words);
  1291.   if (t)
  1292.     input_line_pointer = t;
  1293.  
  1294.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  1295.   for (wordP = words; prec--;)
  1296.     {
  1297.       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
  1298.       litP += sizeof (LITTLENUM_TYPE);
  1299.     }
  1300.   return 0;
  1301. }
  1302.  
  1303. CONST char *md_shortopts = "";
  1304. struct option md_longopts[] = {
  1305.   {NULL, no_argument, NULL, 0}
  1306. };
  1307. size_t md_longopts_size = sizeof(md_longopts);
  1308.  
  1309. int
  1310. md_parse_option (c, arg)
  1311.      int c;
  1312.      char *arg;
  1313. {
  1314.   return 0;
  1315. }
  1316.  
  1317. void
  1318. md_show_usage (stream)
  1319.      FILE *stream;
  1320. {
  1321. }
  1322.  
  1323. int md_short_jump_size;
  1324.  
  1325. void
  1326. tc_aout_fix_to_chars ()
  1327. {
  1328.   printf ("call to tc_aout_fix_to_chars \n");
  1329.   abort ();
  1330. }
  1331.  
  1332. void
  1333. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1334.      char *ptr;
  1335.      addressT from_addr;
  1336.      addressT to_addr;
  1337.      fragS *frag;
  1338.      symbolS *to_symbol;
  1339. {
  1340.   as_fatal ("failed sanity check.");
  1341. }
  1342.  
  1343. void
  1344. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  1345.      char *ptr;
  1346.      addressT from_addr, to_addr;
  1347.      fragS *frag;
  1348.      symbolS *to_symbol;
  1349. {
  1350.   as_fatal ("failed sanity check.");
  1351. }
  1352.  
  1353. void
  1354. md_convert_frag (headers, seg, fragP)
  1355.      object_headers *headers;
  1356.      segT seg;
  1357.      fragS *fragP;
  1358. {
  1359.   printf ("call to md_convert_frag \n");
  1360.   abort ();
  1361. }
  1362.  
  1363. valueT 
  1364. md_section_align (seg, size)
  1365.      segT seg;
  1366.      valueT size;
  1367. {
  1368.   return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
  1369.  
  1370. }
  1371.  
  1372. void
  1373. md_apply_fix (fixP, val)
  1374.      fixS *fixP;
  1375.      long val;
  1376. {
  1377.   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
  1378.  
  1379.   switch (fixP->fx_size)
  1380.     {
  1381.     case 1:
  1382.       *buf++ = val;
  1383.       break;
  1384.     case 2:
  1385.       *buf++ = (val >> 8);
  1386.       *buf++ = val;
  1387.       break;
  1388.     case 4:
  1389.       *buf++ = (val >> 24);
  1390.       *buf++ = (val >> 16);
  1391.       *buf++ = (val >> 8);
  1392.       *buf++ = val;
  1393.       break;
  1394.     default:
  1395.       abort ();
  1396.     }
  1397. }
  1398.  
  1399. int md_long_jump_size;
  1400.  
  1401. int
  1402. md_estimate_size_before_relax (fragP, segment_type)
  1403.      register fragS *fragP;
  1404.      register segT segment_type;
  1405. {
  1406.   printf ("call tomd_estimate_size_before_relax \n");
  1407.   abort ();
  1408. }
  1409.  
  1410. /* Put number into target byte order */
  1411.  
  1412. void
  1413. md_number_to_chars (ptr, use, nbytes)
  1414.      char *ptr;
  1415.      valueT use;
  1416.      int nbytes;
  1417. {
  1418.   number_to_chars_bigendian (ptr, use, nbytes);
  1419. }
  1420. long
  1421. md_pcrel_from (fixP)
  1422.      fixS *fixP;
  1423. {
  1424.   abort ();
  1425. }
  1426.  
  1427.  
  1428. void
  1429. tc_reloc_mangle (fix_ptr, intr, base)
  1430.      fixS *fix_ptr;
  1431.      struct internal_reloc *intr;
  1432.      bfd_vma base;
  1433.  
  1434. {
  1435.   symbolS *symbol_ptr;
  1436.  
  1437.   symbol_ptr = fix_ptr->fx_addsy;
  1438.  
  1439.   /* If this relocation is attached to a symbol then it's ok
  1440.      to output it */
  1441.   if (fix_ptr->fx_r_type == TC_CONS_RELOC)
  1442.     {
  1443.       /* cons likes to create reloc32's whatever the size of the reloc..
  1444.        */
  1445.       switch (fix_ptr->fx_size)
  1446.     {
  1447.     case 4:
  1448.       intr->r_type = R_RELLONG;
  1449.       break;
  1450.     case 2:
  1451.       intr->r_type = R_RELWORD;
  1452.       break;
  1453.     case 1:
  1454.       intr->r_type = R_RELBYTE;
  1455.       break;
  1456.     default:
  1457.       abort ();
  1458.  
  1459.     }
  1460.  
  1461.     }
  1462.   else
  1463.     {
  1464.       intr->r_type = fix_ptr->fx_r_type;
  1465.     }
  1466.  
  1467.   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
  1468.   intr->r_offset = fix_ptr->fx_offset;
  1469.  
  1470.   if (symbol_ptr)
  1471.     intr->r_symndx = symbol_ptr->sy_number;
  1472.   else
  1473.     intr->r_symndx = -1;
  1474.  
  1475.  
  1476. }
  1477.  
  1478. /* end of tc-h8300.c */
  1479.