home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Atari / Gnu / gassrc04.zoo / i860.c < prev    next >
C/C++ Source or Header  |  1991-01-24  |  28KB  |  1,143 lines

  1. /* i860.c -- Assemble for the I860
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include "i860-opcode.h"
  25. #include "as.h"
  26. #include "frags.h"
  27. #include "struc-symbol.h"
  28. #include "flonum.h"
  29. #include "expr.h"
  30. #include "hash.h"
  31. #include "md.h"
  32. #include "i860.h"    /* position dependent redefine */
  33. #include "write.h"
  34. #include "read.h"
  35. #include "symbols.h"
  36.  
  37. void md_begin();
  38. void md_end();
  39. void md_number_to_chars();
  40. void md_assemble();
  41. char *md_atof();
  42. void md_convert_frag();
  43. void md_create_short_jump();
  44. void md_create_long_jump();
  45. int  md_estimate_size_before_relax();
  46. void md_number_to_imm();
  47. void md_number_to_disp();
  48. void md_number_to_field();
  49. void md_ri_to_chars();
  50. static void i860_ip();
  51. void emit_relocations();
  52.  
  53. const relax_typeS md_relax_table[] = { 0 };
  54.  
  55. /* handle of the OPCODE hash table */
  56. static struct hash_control *op_hash = NULL;
  57.  
  58. static void s_dual(), s_enddual();
  59. static void s_atmp();
  60.  
  61. const pseudo_typeS
  62. md_pseudo_table[] = {
  63.     { "dual",       s_dual,     4 },
  64.     { "enddual",    s_enddual,  4 },
  65.     { "atmp",        s_atmp,    4 },
  66.     { NULL,         0,          0 },
  67. };
  68.  
  69. int md_short_jump_size = 4;
  70. int md_long_jump_size = 4;
  71. int omagic  =  OMAGIC;  /* Magic number for header */
  72.  
  73. /* This array holds the chars that always start a comment.  If the
  74.     pre-processor is disabled, these aren't very useful */
  75. char comment_chars[] = "!/";    /* JF removed '|' from comment_chars */
  76.  
  77. /* This array holds the chars that only start a comment at the beginning of
  78.    a line.  If the line seems to have the form '# 123 filename'
  79.    .line and .file directives will appear in the pre-processed output */
  80. /* Note that input_file.c hand checks for '#' at the beginning of the
  81.    first line of the input file.  This is because the compiler outputs
  82.    #NO_APP at the beginning of its output. */
  83. /* Also note that '/*' will always start a comment */
  84. char line_comment_chars[] = "#/";
  85.  
  86. /* Chars that can be used to separate mant from exp in floating point nums */
  87. char EXP_CHARS[] = "eE";
  88.  
  89. /* Chars that mean this number is a floating point constant */
  90. /* As in 0f12.456 */
  91. /* or    0d1.2345e12 */
  92. char FLT_CHARS[] = "rRsSfFdDxXpP";
  93.  
  94. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  95.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  96.    but nothing is ideal around here.
  97.  */
  98. int size_reloc_info = sizeof(struct relocation_info);
  99.  
  100. static unsigned char octal[256];
  101. #define isoctal(c)  octal[c]
  102. static unsigned char toHex[256];
  103.  
  104. struct i860_it {
  105.     char    *error;
  106.     unsigned long opcode;
  107.     struct nlist *nlistp;
  108.     expressionS exp;
  109.     int pcrel;
  110.     enum expand_type expand;
  111.     enum highlow_type highlow;
  112.     enum reloc_type reloc;
  113. } the_insn;
  114.  
  115. #ifdef __STDC__
  116. static void print_insn(struct i860_it *insn);
  117. static int getExpression(char *str);
  118. #else
  119. static void print_insn();
  120. static int getExpression();
  121. #endif
  122. static char *expr_end;
  123. static char last_expand;    /* error if expansion after branch */
  124.  
  125. enum dual
  126. {
  127.     DUAL_OFF = 0, DUAL_ON, DUAL_DDOT, DUAL_ONDDOT,
  128. };
  129. static enum dual dual_mode = DUAL_OFF;    /* dual-instruction mode */
  130.  
  131. static void
  132. s_dual()    /* floating point instructions have dual set */
  133. {
  134.     dual_mode = DUAL_ON;
  135. }
  136.  
  137. static void
  138. s_enddual()    /* floating point instructions have dual set */
  139. {
  140.     dual_mode = DUAL_OFF;
  141. }
  142.  
  143. static int atmp = 31; /* temporary register for pseudo's */
  144.  
  145. static void
  146. s_atmp()
  147. {
  148.     register int temp;
  149.     if (strncmp(input_line_pointer, "sp", 2) == 0) {
  150.       input_line_pointer += 2;
  151.       atmp = 2;
  152.     }
  153.     else if (strncmp(input_line_pointer, "fp", 2) == 0) {
  154.       input_line_pointer += 2;
  155.       atmp = 3;
  156.     }
  157.     else if (strncmp(input_line_pointer, "r", 1) == 0) {
  158.       input_line_pointer += 1;
  159.       temp = get_absolute_expression();
  160.       if (temp >= 0 && temp <= 31)
  161.           atmp = temp;
  162.         else
  163.             as_warn("Unknown temporary pseudo register");
  164.     }
  165.     else {
  166.         as_warn("Unknown temporary pseudo register");
  167.     }
  168.     demand_empty_rest_of_line();
  169.     return;
  170. }
  171.  
  172. /* This function is called once, at assembler startup time.  It should
  173.    set up all the tables, etc. that the MD part of the assembler will need.  */
  174. void
  175. md_begin()
  176. {
  177.   register char *retval = NULL;
  178.   int lose = 0;
  179.   register unsigned int i = 0;
  180.  
  181.   op_hash = hash_new();
  182.   if (op_hash == NULL)
  183.     as_fatal("Virtual memory exhausted");
  184.  
  185.   while (i < NUMOPCODES)
  186.     {
  187.       const char *name = i860_opcodes[i].name;
  188.       retval = hash_insert(op_hash, name, &i860_opcodes[i]);
  189.       if(retval != NULL && *retval != '\0')
  190.     {
  191.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  192.            i860_opcodes[i].name, retval);
  193.       lose = 1;
  194.     }
  195.       do
  196.     {
  197.       if (i860_opcodes[i].match & i860_opcodes[i].lose)
  198.         {
  199.           fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
  200.                i860_opcodes[i].name, i860_opcodes[i].args);
  201.           lose = 1;
  202.         }
  203.       ++i;
  204.     } while (i < NUMOPCODES
  205.          && !strcmp(i860_opcodes[i].name, name));
  206.     }
  207.  
  208.   if (lose)
  209.     as_fatal ("Broken assembler.  No assembly attempted.");
  210.  
  211.   for (i = '0'; i < '8'; ++i)
  212.     octal[i] = 1;
  213.   for (i = '0'; i <= '9'; ++i)
  214.     toHex[i] = i - '0';
  215.   for (i = 'a'; i <= 'f'; ++i)
  216.     toHex[i] = i + 10 - 'a';
  217.   for (i = 'A'; i <= 'F'; ++i)
  218.     toHex[i] = i + 10 - 'A';
  219. }
  220.  
  221. void
  222. md_end()
  223. {
  224.     return;
  225. }
  226.  
  227. void
  228. md_assemble(str)
  229.     char *str;
  230. {
  231.     char *toP;
  232.     int rsd;
  233.     int no_opcodes = 1;
  234.     int i;
  235.     struct i860_it pseudo[3];
  236.  
  237.     assert(str);
  238.     i860_ip(str);
  239.  
  240.     /* check for expandable flag to produce pseudo-instructions */
  241.     if (the_insn.expand != 0 && the_insn.highlow == NO_SPEC) {
  242.     for (i = 0; i < 3; i++)
  243.         pseudo[i] = the_insn;
  244.  
  245.     switch (the_insn.expand) {
  246.  
  247.     case E_DELAY:
  248.         no_opcodes = 1;
  249.         break;
  250.  
  251.     case E_MOV:
  252.         if (the_insn.exp.X_add_symbol == NULL &&
  253.             the_insn.exp.X_subtract_symbol == NULL &&
  254.         (the_insn.exp.X_add_number < (1 << 15) &&
  255.          the_insn.exp.X_add_number >= -(1 << 15)))
  256.         break;
  257.         /* or l%const,r0,ireg_dest */
  258.         pseudo[0].opcode = (the_insn.opcode & 0x001f0000) | 0xe4000000;
  259.         pseudo[0].highlow = PAIR;
  260.         /* orh h%const,ireg_dest,ireg_dest */
  261.         pseudo[1].opcode = (the_insn.opcode & 0x03ffffff) | 0xec000000 |
  262.         ((the_insn.opcode & 0x001f0000) << 5);
  263.         pseudo[1].highlow = HIGH;
  264.         no_opcodes = 2;
  265.         break;
  266.  
  267.     case E_ADDR:
  268.         if (the_insn.exp.X_add_symbol == NULL &&
  269.             the_insn.exp.X_subtract_symbol == NULL)
  270.         break;
  271.         /* orh ha%addr_expr,r0,r31 */
  272.         pseudo[0].opcode = 0xec000000 | (atmp<<16);
  273.         pseudo[0].highlow = HIGHADJ;
  274.         pseudo[0].reloc = LOW0;    /* must overwrite */
  275.         /* l%addr_expr(r31),ireg_dest */
  276.         pseudo[1].opcode = (the_insn.opcode & ~0x003e0000) | (atmp << 21);
  277.         pseudo[1].highlow = PAIR;
  278.         no_opcodes = 2;
  279.         break;
  280.  
  281.     case E_U32:    /* 2nd version emulates Intel as, not doc. */
  282.         if (the_insn.exp.X_add_symbol == NULL &&
  283.             the_insn.exp.X_subtract_symbol == NULL &&
  284.         (the_insn.exp.X_add_number < (1 << 16) &&
  285.          the_insn.exp.X_add_number >= 0))
  286.         break;
  287.         /* $(opcode)h h%const,ireg_src2,ireg_dest
  288.         pseudo[0].opcode = (the_insn.opcode & 0xf3ffffff) | 0x0c000000; */
  289.         /* $(opcode)h h%const,ireg_src2,r31 */
  290.         pseudo[0].opcode = (the_insn.opcode & 0xf3e0ffff) | 0x0c000000 |
  291.         (atmp << 16);
  292.         pseudo[0].highlow = HIGH;
  293.         /* $(opcode) l%const,ireg_dest,ireg_dest
  294.         pseudo[1].opcode = (the_insn.opcode & 0xf01f0000) | 0x04000000 |
  295.         ((the_insn.opcode & 0x001f0000) << 5); */
  296.         /* $(opcode) l%const,r31,ireg_dest */
  297.         pseudo[1].opcode = (the_insn.opcode & 0xf01f0000) | 0x04000000 |
  298.         (atmp << 21);
  299.         pseudo[1].highlow = PAIR;
  300.         no_opcodes = 2;
  301.         break;
  302.  
  303.     case E_AND:    /* 2nd version emulates Intel as, not doc. */
  304.         if (the_insn.exp.X_add_symbol == NULL &&
  305.             the_insn.exp.X_subtract_symbol == NULL &&
  306.         (the_insn.exp.X_add_number < (1 << 16) &&
  307.          the_insn.exp.X_add_number >= 0))
  308.         break;
  309.         /* andnot h%const,ireg_src2,ireg_dest
  310.         pseudo[0].opcode = (the_insn.opcode & 0x03ffffff) | 0xd4000000; */
  311.         /* andnot h%const,ireg_src2,r31 */
  312.         pseudo[0].opcode = (the_insn.opcode & 0x03e0ffff) | 0xd4000000 |
  313.         (atmp << 16);
  314.         pseudo[0].highlow = HIGH;
  315.         pseudo[0].exp.X_add_number = -1 - the_insn.exp.X_add_number;
  316.         /* andnot l%const,ireg_dest,ireg_dest
  317.         pseudo[1].opcode = (the_insn.opcode & 0x001f0000) | 0xd4000000 |
  318.         ((the_insn.opcode & 0x001f0000) << 5); */
  319.         /* andnot l%const,r31,ireg_dest */
  320.         pseudo[1].opcode = (the_insn.opcode & 0x001f0000) | 0xd4000000 |
  321.         (atmp << 21);
  322.         pseudo[1].highlow = PAIR;
  323.         pseudo[1].exp.X_add_number = -1 - the_insn.exp.X_add_number;
  324.         no_opcodes = 2;
  325.         break;
  326.  
  327.     case E_S32:
  328.         if (the_insn.exp.X_add_symbol == NULL &&
  329.             the_insn.exp.X_subtract_symbol == NULL &&
  330.         (the_insn.exp.X_add_number < (1 << 15) &&
  331.          the_insn.exp.X_add_number >= -(1 << 15)))
  332.         break;
  333.         /* orh h%const,r0,r31 */
  334.         pseudo[0].opcode = 0xec000000 | (atmp << 16);
  335.         pseudo[0].highlow = HIGH;
  336.         /* or l%const,r31,r31 */
  337.         pseudo[1].opcode = 0xe4000000 | (atmp << 21) | (atmp << 16);
  338.         pseudo[1].highlow = PAIR;
  339.         /* r31,ireg_src2,ireg_dest */
  340.         pseudo[2].opcode = (the_insn.opcode & ~0x0400ffff) | (atmp << 11);
  341.         pseudo[2].reloc = NO_RELOC;
  342.         no_opcodes = 3;
  343.         break;
  344.  
  345.         default:
  346.         abort();
  347.     }
  348.  
  349.         the_insn = pseudo[0];
  350.     /* check for expanded opcode after branch or in dual */
  351.     if (no_opcodes > 1 && last_expand == TRUE)
  352.         as_warn("Expanded opcode after delayed branch: `%s'", str);
  353.     if (no_opcodes > 1 && dual_mode != DUAL_OFF)
  354.         as_warn("Expanded opcode in dual mode: `%s'", str);
  355.     }
  356.  
  357.     i = 0;
  358.     do {    /* always produce at least one opcode */
  359.         toP = frag_more(4);
  360.         /* put out the opcode */
  361.         md_number_to_chars(toP, the_insn.opcode, 4);
  362.  
  363.     /* check for expanded opcode after branch or in dual */
  364.     last_expand = the_insn.pcrel;
  365.  
  366.         /* put out the symbol-dependent stuff */
  367.         if (the_insn.reloc != NO_RELOC) {
  368.         fix_new(
  369.             frag_now,                           /* which frag */
  370.             (toP - frag_now->fr_literal), /* where */
  371.             4,                                  /* size */
  372.             the_insn.exp.X_add_symbol,
  373.             the_insn.exp.X_subtract_symbol,
  374.             the_insn.exp.X_add_number,
  375.             the_insn.pcrel,
  376.         /* merge bit fields into one argument */
  377.         (int)(((the_insn.highlow & 0x3) << 4) | (the_insn.reloc & 0xf))
  378.         );
  379.         }
  380.         the_insn = pseudo[++i];
  381.     } while (--no_opcodes > 0);
  382.  
  383. }
  384.  
  385. static void
  386. i860_ip(str)
  387.     char *str;
  388. {
  389.     char *s;
  390.     const char *args;
  391.     char c;
  392.     unsigned long i;
  393.     struct i860_opcode *insn;
  394.     char *argsStart;
  395.     unsigned long   opcode;
  396.     unsigned int mask;
  397.     int match = FALSE;
  398.     int comma = 0;
  399.  
  400.  
  401.     for (s = str; islower(*s) || *s == '.' || *s == '3'; ++s)
  402.     ;
  403.     switch (*s) {
  404.  
  405.     case '\0':
  406.     break;
  407.  
  408.     case ',':
  409.     comma = 1;
  410.  
  411.     /*FALLTHROUGH*/
  412.  
  413.     case ' ':
  414.     *s++ = '\0';
  415.     break;
  416.  
  417.     default:
  418.         as_warn("Unknown opcode: `%s'", str);
  419.         exit(1);
  420.     }
  421.  
  422.     if (strncmp(str, "d.", 2) == 0) {    /* check for d. opcode prefix */
  423.     if (dual_mode == DUAL_ON)
  424.         dual_mode = DUAL_ONDDOT;
  425.     else
  426.         dual_mode = DUAL_DDOT;
  427.     str += 2;
  428.     }
  429.  
  430.     if ((insn = (struct i860_opcode *) hash_find(op_hash, str)) == NULL) {
  431.     if (dual_mode == DUAL_DDOT || dual_mode == DUAL_ONDDOT)
  432.         str -= 2;
  433.     as_warn("Unknown opcode: `%s'", str);
  434.     return;
  435.     }
  436.     if (comma) {
  437.     *--s = ',';
  438.     }
  439.     argsStart = s;
  440.     for (;;) {
  441.     opcode = insn->match;
  442.     bzero(&the_insn, sizeof(the_insn));
  443.     the_insn.reloc = NO_RELOC;
  444.  
  445.     /*
  446.      * Build the opcode, checking as we go to make
  447.      * sure that the operands match
  448.      */
  449.     for (args = insn->args; ; ++args) {
  450.         switch (*args) {
  451.  
  452.         case '\0':  /* end of args */
  453.         if (*s == '\0') {
  454.             match = TRUE;
  455.         }
  456.         break;
  457.  
  458.         case '+':
  459.         case '(':   /* these must match exactly */
  460.         case ')':
  461.         case ',':
  462.         case ' ':
  463.         if (*s++ == *args)
  464.             continue;
  465.         break;
  466.  
  467.         case '#':   /* must be at least one digit */
  468.         if (isdigit(*s++)) {
  469.             while (isdigit(*s)) {
  470.             ++s;
  471.             }
  472.             continue;
  473.         }
  474.         break;
  475.  
  476.         case '1':   /* next operand must be a register */
  477.         case '2':
  478.         case 'd':
  479.         switch (*s) {
  480.  
  481.         case 'f':   /* frame pointer */
  482.             s++;
  483.             if (*s++ == 'p') {
  484.             mask = 0x3;
  485.             break;
  486.             }
  487.             goto error;
  488.  
  489.         case 's':   /* stack pointer */
  490.             s++;
  491.             if (*s++ == 'p') {
  492.             mask= 0x2;
  493.             break;
  494.             }
  495.             goto error;
  496.  
  497.         case 'r': /* any register */
  498.             s++;
  499.             if (!isdigit(c = *s++)) {
  500.             goto error;
  501.             }
  502.             if (isdigit(*s)) {
  503.                 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32) {
  504.                 goto error;
  505.             }
  506.             } else {
  507.             c -= '0';
  508.             }
  509.             mask= c;
  510.             break;
  511.  
  512.         default:    /* not this opcode */
  513.             goto error;
  514.         }
  515.         /*
  516.          * Got the register, now figure out where
  517.          * it goes in the opcode.
  518.          */
  519.         switch (*args) {
  520.  
  521.         case '1':
  522.             opcode |= mask << 11;
  523.             continue;
  524.  
  525.         case '2':
  526.             opcode |= mask << 21;
  527.             continue;
  528.  
  529.         case 'd':
  530.             opcode |= mask << 16;
  531.             continue;
  532.  
  533.         }
  534.         break;
  535.  
  536.         case 'e':    /* next operand is a floating point register */
  537.         case 'f':
  538.         case 'g':
  539.             if (*s++ == 'f' && isdigit(*s)) {
  540.             mask = *s++;
  541.             if (isdigit(*s)) {
  542.             mask = 10 * (mask - '0') + (*s++ - '0');
  543.             if (mask >= 32) {
  544.                 break;
  545.             }
  546.             } else {
  547.             mask -= '0';
  548.             }
  549.             switch (*args) {
  550.  
  551.             case 'e':
  552.             opcode |= mask << 11;
  553.             continue;
  554.  
  555.             case 'f':
  556.             opcode |= mask << 21;
  557.             continue;
  558.  
  559.             case 'g':
  560.             opcode |= mask << 16;
  561.             if (dual_mode != DUAL_OFF)
  562.                 opcode |= (1 << 9);    /* dual mode instruction */
  563.             if (dual_mode == DUAL_DDOT)
  564.                 dual_mode = DUAL_OFF;
  565.             if (dual_mode == DUAL_ONDDOT)
  566.                 dual_mode = DUAL_ON;
  567.             if ((opcode & (1 << 10)) && (mask == ((opcode >> 11) & 0x1f)))
  568.                 as_warn("Fsr1 equals fdest with Pipelining");
  569.             continue;
  570.             }
  571.         }
  572.         break;
  573.  
  574.         case 'c': /* next operand must be a control register */
  575.         if (strncmp(s, "fir", 3) == 0) {
  576.             opcode |= 0x0 << 21;
  577.             s += 3;
  578.             continue;
  579.         }
  580.         if (strncmp(s, "psr", 3) == 0) {
  581.             opcode |= 0x1 << 21;
  582.             s += 3;
  583.             continue;
  584.         }
  585.         if (strncmp(s, "dirbase", 7) == 0) {
  586.             opcode |= 0x2 << 21;
  587.             s += 7;
  588.             continue;
  589.         }
  590.         if (strncmp(s, "db", 2) == 0) {
  591.             opcode |= 0x3 << 21;
  592.             s += 2;
  593.             continue;
  594.         }
  595.         if (strncmp(s, "fsr", 3) == 0) {
  596.             opcode |= 0x4 << 21;
  597.             s += 3;
  598.             continue;
  599.         }
  600.         if (strncmp(s, "epsr", 4) == 0) {
  601.             opcode |= 0x5 << 21;
  602.             s += 4;
  603.             continue;
  604.         }
  605.         break;
  606.  
  607.         case '5':   /* 5 bit immediate in src1 */
  608.         bzero(&the_insn, sizeof(the_insn));
  609.         if ( !getExpression(s)) {
  610.             s = expr_end;
  611.                 if (the_insn.exp.X_add_number & ~0x1f)
  612.                 as_warn("5-bit immediate too large");
  613.                 opcode |= (the_insn.exp.X_add_number & 0x1f) << 11;
  614.             bzero(&the_insn, sizeof(the_insn));
  615.             the_insn.reloc = NO_RELOC;
  616.             continue;
  617.         }
  618.         break;
  619.  
  620.         case 'l':   /* 26 bit immediate, relative branch */
  621.         the_insn.reloc = BRADDR;
  622.         the_insn.pcrel = TRUE;
  623.         goto immediate;
  624.  
  625.         case 's':   /* 16 bit immediate, split relative branch */
  626.             /* upper 5 bits of offset in dest field */
  627.         the_insn.pcrel = TRUE;
  628.         the_insn.reloc = SPLIT0;
  629.         goto immediate;
  630.  
  631.         case 'S':   /* 16 bit immediate, split (st), aligned */
  632.         if (opcode & (1 << 28))
  633.             if (opcode & 0x1)
  634.             the_insn.reloc = SPLIT2;
  635.             else
  636.             the_insn.reloc = SPLIT1;
  637.         else
  638.             the_insn.reloc = SPLIT0;
  639.         goto immediate;
  640.  
  641.         case 'I':   /* 16 bit immediate, aligned */
  642.         if (opcode & (1 << 28))
  643.             if (opcode & 0x1)
  644.             the_insn.reloc = LOW2;
  645.             else
  646.             the_insn.reloc = LOW1;
  647.         else
  648.             the_insn.reloc = LOW0;
  649.         goto immediate;
  650.  
  651.         case 'i':   /* 16 bit immediate */
  652.         the_insn.reloc = LOW0;
  653.  
  654.         /*FALLTHROUGH*/
  655.  
  656.         immediate:
  657.         if(*s==' ')
  658.           s++;
  659.         if (strncmp(s, "ha%", 3) == 0) {
  660.             the_insn.highlow = HIGHADJ;
  661.             s += 3;
  662.         } else if (strncmp(s, "h%", 2) == 0) {
  663.             the_insn.highlow = HIGH;
  664.             s += 2;
  665.         } else if (strncmp(s, "l%", 2) == 0) {
  666.             the_insn.highlow = PAIR;
  667.             s += 2;
  668.         }
  669.         the_insn.expand = insn->expand; 
  670.  
  671.         /* Note that if the getExpression() fails, we will still have
  672.            created U entries in the symbol table for the 'symbols'
  673.            in the input string.  Try not to create U symbols for
  674.            registers, etc. */
  675.  
  676.         if ( !getExpression(s)) {
  677.             s = expr_end;
  678.             continue;
  679.         }
  680.         break;
  681.  
  682.         default:
  683.         abort();
  684.         }
  685.         break;
  686.     }
  687.     error:
  688.     if (match == FALSE)
  689.       {
  690.         /* Args don't match.  */
  691.         if (&insn[1] - i860_opcodes < NUMOPCODES
  692.         && !strcmp(insn->name, insn[1].name))
  693.           {
  694.         ++insn;
  695.         s = argsStart;
  696.         continue;
  697.           }
  698.         else
  699.           {
  700.         as_warn("Illegal operands");
  701.         return;
  702.           }
  703.       }
  704.     break;
  705.     }
  706.  
  707.     the_insn.opcode = opcode;
  708.     return;
  709. }
  710.  
  711. static int
  712. getExpression(str)
  713.     char *str;
  714. {
  715.     char *save_in;
  716.     segT seg;
  717.  
  718.     save_in = input_line_pointer;
  719.     input_line_pointer = str;
  720.     switch (seg = expression(&the_insn.exp)) {
  721.  
  722.     case SEG_ABSOLUTE:
  723.     case SEG_TEXT:
  724.     case SEG_DATA:
  725.     case SEG_BSS:
  726.     case SEG_UNKNOWN:
  727.     case SEG_DIFFERENCE:
  728.     case SEG_BIG:
  729.     case SEG_NONE:
  730.     break;
  731.  
  732.     default:
  733.     the_insn.error = "bad segment";
  734.     expr_end = input_line_pointer;
  735.     input_line_pointer=save_in;
  736.     return 1;
  737.     }
  738.     expr_end = input_line_pointer;
  739.     input_line_pointer = save_in;
  740.     return 0;
  741. }
  742.  
  743.  
  744. /*
  745.     This is identical to the md_atof in m68k.c.  I think this is right,
  746.     but I'm not sure.
  747.  
  748.    Turn a string in input_line_pointer into a floating point constant of type
  749.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  750.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  751.  */
  752.  
  753. /* Equal to MAX_PRECISION in atof-ieee.c */
  754. #define MAX_LITTLENUMS 6
  755.  
  756. char *
  757. md_atof(type,litP,sizeP)
  758.     char type;
  759.     char *litP;
  760.     int *sizeP;
  761. {
  762.     int    prec;
  763.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  764.     LITTLENUM_TYPE *wordP;
  765.     char    *t;
  766.     char    *atof_ieee();
  767.  
  768.     switch(type) {
  769.  
  770.     case 'f':
  771.     case 'F':
  772.     case 's':
  773.     case 'S':
  774.     prec = 2;
  775.     break;
  776.  
  777.     case 'd':
  778.     case 'D':
  779.     case 'r':
  780.     case 'R':
  781.     prec = 4;
  782.     break;
  783.  
  784.     case 'x':
  785.     case 'X':
  786.     prec = 6;
  787.     break;
  788.  
  789.     case 'p':
  790.     case 'P':
  791.     prec = 6;
  792.     break;
  793.  
  794.     default:
  795.     *sizeP=0;
  796.     return "Bad call to MD_ATOF()";
  797.     }
  798.     t=atof_ieee(input_line_pointer,type,words);
  799.     if(t)
  800.     input_line_pointer=t;
  801.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  802.     for(wordP=words;prec--;) {
  803.     md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  804.     litP+=sizeof(LITTLENUM_TYPE);
  805.     }
  806.     return "";    /* Someone should teach Dean about null pointers */
  807. }
  808.  
  809. /*
  810.  * Write out big-endian.
  811.  */
  812. void
  813. md_number_to_chars(buf,val,n)
  814.     char *buf;
  815.     long val;
  816.     int n;
  817. {
  818.     switch(n) {
  819.  
  820.     case 4:
  821.     *buf++ = val >> 24;
  822.     *buf++ = val >> 16;
  823.     case 2:
  824.     *buf++ = val >> 8;
  825.     case 1:
  826.     *buf = val;
  827.     break;
  828.  
  829.     default:
  830.     abort();
  831.     }
  832.     return;
  833. }
  834.  
  835. void
  836. md_number_to_imm(buf,val,n, fixP, seg_type)
  837.     char *buf;
  838.     long val;
  839.     int n;
  840.     fixS *fixP;
  841.     int seg_type;
  842. {
  843.     enum reloc_type reloc = fixP->fx_r_type & 0xf;
  844.     enum highlow_type highlow = (fixP->fx_r_type >> 4) & 0x3;
  845.  
  846.     assert(buf);
  847.     assert(n == 4);    /* always on i860 */
  848.  
  849.     switch(highlow)
  850.     {
  851.  
  852.     case HIGHADJ:    /* adjusts the high-order 16-bits */
  853.     if (val & (1 << 15))
  854.         val += (1 << 16);
  855.  
  856.         /*FALLTHROUGH*/
  857.  
  858.     case HIGH:    /* selects the high-order 16-bits */
  859.     val >>= 16;
  860.     break;
  861.  
  862.     case PAIR:    /* selects the low-order 16-bits */
  863.     val = val & 0xffff;
  864.     break;
  865.  
  866.     default:
  867.     break;
  868.     }
  869.  
  870.     switch(reloc)
  871.     {
  872.  
  873.     case BRADDR:    /* br,call,bc,bc.t,bnc,bnc.t w/26-bit immediate */
  874.     if (fixP->fx_pcrel != TRUE)
  875.         as_warn("26-bit branch w/o pc relative set: 0x%08x", val);
  876.     val >>= 2;    /* align pcrel offset, see manual */
  877.  
  878.     if (val >= (1 << 25) || val < -(1 << 25))    /* check for overflow */
  879.         as_warn("26-bit branch offset overflow: 0x%08x", val);
  880.     buf[0] = (buf[0] & 0xfc) | ((val >> 24) & 0x3);
  881.     buf[1] = val >> 16;
  882.     buf[2] = val >> 8;
  883.     buf[3] = val;
  884.     break;
  885.  
  886.     case SPLIT2:    /* 16 bit immediate, 4-byte aligned */
  887.     if (val & 0x3)
  888.         as_warn("16-bit immediate 4-byte alignment error: 0x%08x", val);
  889.     val &= ~0x3;    /* 4-byte align value */
  890.     /*FALLTHROUGH*/
  891.     case SPLIT1:    /* 16 bit immediate, 2-byte aligned */
  892.     if (val & 0x1)
  893.         as_warn("16-bit immediate 2-byte alignment error: 0x%08x", val);
  894.     val &= ~0x1;    /* 2-byte align value */
  895.     /*FALLTHROUGH*/
  896.     case SPLIT0:    /* st,bla,bte,btne w/16-bit immediate */
  897.     if (fixP->fx_pcrel == TRUE)
  898.         val >>= 2;    /* align pcrel offset, see manual */
  899.     /* check for bounds */
  900.     if (highlow != PAIR && (val >= (1 << 16) || val < -(1 << 15)))
  901.         as_warn("16-bit branch offset overflow: 0x%08x", val);
  902.     buf[1] = (buf[1] & ~0x1f) | ((val >> 11) & 0x1f);
  903.     buf[2] = (buf[2] & ~0x7) | ((val >> 8) & 0x7);
  904.     buf[3] |= val;    /* perserve bottom opcode bits */    
  905.     break;
  906.  
  907.     case LOW4:    /* fld,pfld,pst,flush 16-byte aligned */
  908.     if (val & 0xf)
  909.         as_warn("16-bit immediate 16-byte alignment error: 0x%08x", val);
  910.     val &= ~0xf;    /* 16-byte align value */
  911.     /*FALLTHROUGH*/
  912.     case LOW3:    /* fld,pfld,pst,flush 8-byte aligned */
  913.     if (val & 0x7)
  914.         as_warn("16-bit immediate 8-byte alignment error: 0x%08x", val);
  915.     val &= ~0x7;    /* 8-byte align value */
  916.     /*FALLTHROUGH*/
  917.     case LOW2:    /* 16 bit immediate, 4-byte aligned */
  918.     if (val & 0x3)
  919.         as_warn("16-bit immediate 4-byte alignment error: 0x%08x", val);
  920.     val &= ~0x3;    /* 4-byte align value */
  921.     /*FALLTHROUGH*/
  922.     case LOW1:    /* 16 bit immediate, 2-byte aligned */
  923.     if (val & 0x1)
  924.         as_warn("16-bit immediate 2-byte alignment error: 0x%08x", val);
  925.     val &= ~0x1;    /* 2-byte align value */
  926.     /*FALLTHROUGH*/
  927.     case LOW0:    /* 16 bit immediate, byte aligned */
  928.     /* check for bounds */
  929.     if (highlow != PAIR && (val >= (1 << 16) || val < -(1 << 15)))
  930.         as_warn("16-bit immediate overflow: 0x%08x", val);
  931.     buf[2] = val >> 8;
  932.     buf[3] |= val;    /* perserve bottom opcode bits */    
  933.     break;
  934.  
  935.     case NO_RELOC:
  936.     default:
  937.     as_warn("bad relocation type: 0x%02x", reloc);
  938.     break;
  939.     }
  940.     return;
  941. }
  942.  
  943. /* should never be called for i860 */
  944. void
  945. md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
  946.     char *ptr;
  947.     long from_addr, to_addr;
  948. {
  949.     fprintf(stderr, "i860_create_short_jmp\n");
  950.     abort();
  951. }
  952.  
  953. /* should never be called for i860 */
  954. void
  955. md_number_to_disp(buf,val,n)
  956.     char    *buf;
  957.     long    val;
  958. {
  959.     fprintf(stderr, "md_number_to_disp\n");
  960.     abort();
  961. }
  962.  
  963. /* should never be called for i860 */
  964. void
  965. md_number_to_field(buf,val,fix)
  966.     char *buf;
  967.     long val;
  968.     void *fix;
  969. {
  970.     fprintf(stderr, "i860_number_to_field\n");
  971.     abort();
  972. }
  973.  
  974. /* the bit-field entries in the relocation_info struct plays hell 
  975.    with the byte-order problems of cross-assembly.  So as a hack,
  976.    I added this mach. dependent ri twiddler.  Ugly, but it gets
  977.    you there. -KWK */
  978. /* on i860: first 4 bytes are normal unsigned long address, next three
  979.    bytes are index, most sig. byte first.  Byte 7 is broken up with
  980.    bit 7 as pcrel, bit 6 as extern, and the lower six bits as
  981.    relocation type (highlow 5-4).  Next 4 bytes are long int addend. */
  982. /* Thanx and a tip of the hat to Michael Bloom, mb@ttidca.tti.com */
  983. void
  984. md_ri_to_chars(ri_p, ri)
  985.      struct relocation_info *ri_p, ri;
  986. {
  987. #if 0
  988.   unsigned char the_bytes[sizeof(*ri_p)];
  989.   
  990.   /* this is easy */
  991.   md_number_to_chars(the_bytes, ri.r_address, sizeof(ri.r_address));
  992.   /* now the fun stuff */
  993.   the_bytes[4] = (ri.r_index >> 16) & 0x0ff;
  994.   the_bytes[5] = (ri.r_index >> 8) & 0x0ff;
  995.   the_bytes[6] = ri.r_index & 0x0ff;
  996.   the_bytes[7] = ((ri.r_extern << 7)  & 0x80) | (0 & 0x60) | (ri.r_type & 0x1F);
  997.   /* Also easy */
  998.   md_number_to_chars(&the_bytes[8], ri.r_addend, sizeof(ri.r_addend));
  999.   /* now put it back where you found it, Junior... */
  1000.   bcopy (the_bytes, (char *)ri_p, sizeof(*ri_p));
  1001. #endif
  1002. }
  1003.  
  1004. /* should never be called for i860 */
  1005. void
  1006. md_convert_frag(fragP)
  1007.     register fragS *fragP;
  1008. {
  1009.     fprintf(stderr, "i860_convert_frag\n");
  1010.     abort();
  1011. }
  1012.  
  1013. /* should never be called for i860 */
  1014. void
  1015. md_create_long_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1016.     char    *ptr;
  1017.     long    from_addr,
  1018.             to_addr;
  1019.     fragS    *frag;
  1020.     symbolS    *to_symbol;
  1021. {
  1022.     fprintf(stderr, "i860_create_long_jump\n");
  1023.     abort();
  1024. }
  1025.  
  1026. /* should never be called for i860 */
  1027. int
  1028. md_estimate_size_before_relax(fragP, segtype)
  1029.     register fragS *fragP;
  1030. {
  1031.     fprintf(stderr, "i860_estimate_size_before_relax\n");
  1032.     abort();
  1033.     return 0;
  1034. }
  1035.  
  1036. /* for debugging only, must match enum reloc_type */
  1037. static char *Reloc[] = {
  1038.     "NO_RELOC",
  1039.     "BRADDR", 
  1040.     "LOW0", 
  1041.     "LOW1", 
  1042.     "LOW2", 
  1043.     "LOW3", 
  1044.     "LOW4", 
  1045.     "SPLIT0", 
  1046.     "SPLIT1", 
  1047.     "SPLIT2", 
  1048.     "RELOC_32", 
  1049. };
  1050. static char *Highlow[] = {
  1051.     "NO_SPEC",
  1052.     "PAIR", 
  1053.     "HIGH", 
  1054.     "HIGHADJ", 
  1055. };
  1056. static void
  1057. print_insn(insn)
  1058.     struct i860_it *insn;
  1059. {
  1060.     if (insn->error) {
  1061.     fprintf(stderr, "ERROR: %s\n");
  1062.     }
  1063.     fprintf(stderr, "opcode=0x%08x\t", insn->opcode);
  1064.     fprintf(stderr, "expand=0x%08x\t", insn->expand);
  1065.     fprintf(stderr, "reloc = %s\t", Reloc[insn->reloc]);
  1066.     fprintf(stderr, "highlow = %s\n", Highlow[insn->highlow]);
  1067.     fprintf(stderr, "exp =  {\n");
  1068.     fprintf(stderr, "\t\tX_add_symbol = %s\n",
  1069.     insn->exp.X_add_symbol ?
  1070.     (insn->exp.X_add_symbol->sy_name ? 
  1071.     insn->exp.X_add_symbol->sy_name : "???") : "0");
  1072.     fprintf(stderr, "\t\tX_sub_symbol = %s\n",
  1073.     insn->exp.X_subtract_symbol ?
  1074.         (insn->exp.X_subtract_symbol->sy_name ? 
  1075.             insn->exp.X_subtract_symbol->sy_name : "???") : "0");
  1076.     fprintf(stderr, "\t\tX_add_number = %d\n",
  1077.     insn->exp.X_add_number);
  1078.     fprintf(stderr, "}\n");
  1079.     return;
  1080. }
  1081.  
  1082. int
  1083. md_parse_option(argP,cntP,vecP)
  1084.     char **argP;
  1085.     int *cntP;
  1086.     char ***vecP;
  1087. {
  1088.     return 1;
  1089. }
  1090.  
  1091. /*
  1092.  * I860 relocations are completely different, so it needs
  1093.  * this machine dependent routine to emit them.
  1094.  */
  1095. void
  1096. emit_relocations(fixP, segment_address_in_file)
  1097.     register fixS *fixP;
  1098.     relax_addressT segment_address_in_file;
  1099. {
  1100.     struct reloc_info_i860 ri;
  1101.     register symbolS *symbolP;
  1102.     extern char *next_object_file_charP;
  1103.     long add_number;
  1104.  
  1105.     bzero((char *) &ri, sizeof(ri));
  1106.     for (; fixP; fixP = fixP->fx_next) {
  1107.  
  1108.     if (fixP->fx_r_type & ~0x3f) {
  1109.         fprintf(stderr, "fixP->fx_r_type = %d\n", fixP->fx_r_type);
  1110.         abort();
  1111.     }
  1112.     ri.r_pcrel = fixP->fx_pcrel;
  1113.     ri.r_type = fixP->fx_r_type;
  1114.  
  1115.     if ((symbolP = fixP->fx_addsy) != NULL) {
  1116.         ri.r_address = fixP->fx_frag->fr_address +
  1117.             fixP->fx_where - segment_address_in_file;
  1118.         if ((symbolP->sy_type & N_TYPE) == N_UNDF) {
  1119.         ri.r_extern = 1;
  1120.         ri.r_symbolnum = symbolP->sy_number;
  1121.         } else {
  1122.         ri.r_extern = 0;
  1123.         ri.r_symbolnum = symbolP->sy_type & N_TYPE;
  1124.         }
  1125.         if (symbolP && symbolP->sy_frag) {
  1126.         ri.r_addend = symbolP->sy_frag->fr_address;
  1127.         }
  1128.         ri.r_type = fixP->fx_r_type;
  1129.         if (fixP->fx_pcrel) {
  1130.         /* preserve actual offset vs. pc + 4 */
  1131.         ri.r_addend -= (ri.r_address + 4);
  1132.         } else {
  1133.         ri.r_addend = fixP->fx_addnumber;
  1134.         }
  1135.  
  1136.         md_ri_to_chars((char *) &ri, ri);
  1137.         append(&next_object_file_charP, (char *)& ri, sizeof(ri));
  1138.     }
  1139.     }
  1140.     return;
  1141. }
  1142.  
  1143.