[BACK]Return to tc-vax.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / external / gpl3 / binutils.old / dist / gas / config

Annotation of src/external/gpl3/binutils.old/dist/gas/config/tc-vax.c, Revision 1.5

1.1       christos    1: /* tc-vax.c - vax-specific -
1.5     ! christos    2:    Copyright (C) 1987-2016 Free Software Foundation, Inc.
1.1       christos    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 3, 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 the Free
                     18:    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
                     19:    02110-1301, USA.  */
                     20:
                     21: #include "as.h"
                     22:
                     23: #include "vax-inst.h"
                     24: #include "obstack.h"           /* For FRAG_APPEND_1_CHAR macro in "frags.h" */
                     25: #include "dw2gencfi.h"
                     26: #include "subsegs.h"
                     27: #include "safe-ctype.h"
                     28:
                     29: #ifdef OBJ_ELF
                     30: #include "elf/vax.h"
                     31: #endif
                     32:
                     33: /* These chars start a comment anywhere in a source file (except inside
                     34:    another comment */
                     35: const char comment_chars[] = "#";
                     36:
                     37: /* These chars only start a comment at the beginning of a line.  */
                     38: /* Note that for the VAX the are the same as comment_chars above.  */
                     39: const char line_comment_chars[] = "#";
                     40:
                     41: const char line_separator_chars[] = ";";
                     42:
                     43: /* Chars that can be used to separate mant from exp in floating point nums.  */
                     44: const char EXP_CHARS[] = "eE";
                     45:
                     46: /* Chars that mean this number is a floating point constant
                     47:    as in 0f123.456
                     48:    or    0H1.234E-12 (see exp chars above).  */
                     49: const char FLT_CHARS[] = "dDfFgGhH";
                     50:
                     51: /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
                     52:    changed in read.c .  Ideally it shouldn't have to know about it at all,
                     53:    but nothing is ideal around here.  */
                     54:
                     55: /* Hold details of an operand expression.  */
                     56: static expressionS exp_of_operand[VIT_MAX_OPERANDS];
                     57: static segT seg_of_operand[VIT_MAX_OPERANDS];
                     58:
                     59: /* A vax instruction after decoding.  */
                     60: static struct vit v;
                     61:
                     62: /* Hold details of big operands.  */
                     63: LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER];
                     64: FLONUM_TYPE float_operand[VIT_MAX_OPERANDS];
                     65: /* Above is made to point into big_operand_bits by md_begin().  */
                     66:
                     67: #ifdef OBJ_ELF
                     68: #define GLOBAL_OFFSET_TABLE_NAME       "_GLOBAL_OFFSET_TABLE_"
                     69: #define PROCEDURE_LINKAGE_TABLE_NAME   "_PROCEDURE_LINKAGE_TABLE_"
                     70: symbolS *GOT_symbol;           /* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
                     71: symbolS *PLT_symbol;           /* Pre-defined "_PROCEDURE_LINKAGE_TABLE_".  */
                     72: #endif
                     73:
                     74: int flag_hash_long_names;      /* -+ */
                     75: int flag_one;                  /* -1 */
                     76: int flag_show_after_trunc;     /* -H */
                     77: int flag_no_hash_mixed_case;   /* -h NUM */
                     78: #ifdef OBJ_ELF
                     79: int flag_want_pic;             /* -k */
                     80: #endif
                     81: 
                     82: /* For VAX, relative addresses of "just the right length" are easy.
                     83:    The branch displacement is always the last operand, even in
                     84:    synthetic instructions.
                     85:    For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
                     86:
                     87:                    4       3       2       1       0        bit number
                     88:        ---/ /--+-------+-------+-------+-------+-------+
                     89:                |     what state ?      |  how long ?   |
                     90:        ---/ /--+-------+-------+-------+-------+-------+
                     91:
                     92:    The "how long" bits are 00=byte, 01=word, 10=long.
                     93:    This is a Un*x convention.
                     94:    Not all lengths are legit for a given value of (what state).
                     95:    The "how long" refers merely to the displacement length.
                     96:    The address usually has some constant bytes in it as well.
                     97:
                     98:  groups for VAX address relaxing.
                     99:
                    100:  1.    "foo" pc-relative.
                    101:  length of byte, word, long
                    102:
                    103:  2a.   J<cond> where <cond> is a simple flag test.
                    104:  length of byte, word, long.
                    105:  VAX opcodes are:      (Hex)
                    106:  bneq/bnequ    12
                    107:  beql/beqlu    13
                    108:  bgtr          14
                    109:  bleq          15
                    110:  bgeq          18
                    111:  blss          19
                    112:  bgtru         1a
                    113:  blequ         1b
                    114:  bvc           1c
                    115:  bvs           1d
                    116:  bgequ/bcc     1e
                    117:  blssu/bcs     1f
                    118:  Always, you complement 0th bit to reverse condition.
                    119:  Always, 1-byte opcode, then 1-byte displacement.
                    120:
                    121:  2b.   J<cond> where cond tests a memory bit.
                    122:  length of byte, word, long.
                    123:  Vax opcodes are:      (Hex)
                    124:  bbs           e0
                    125:  bbc           e1
                    126:  bbss          e2
                    127:  bbcs          e3
                    128:  bbsc          e4
                    129:  bbcc          e5
                    130:  Always, you complement 0th bit to reverse condition.
                    131:  Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
                    132:
                    133:  2c.   J<cond> where cond tests low-order memory bit
                    134:  length of byte,word,long.
                    135:  Vax opcodes are:      (Hex)
                    136:  blbs          e8
                    137:  blbc          e9
                    138:  Always, you complement 0th bit to reverse condition.
                    139:  Always, 1-byte opcode, longword-address, 1-byte displacement.
                    140:
                    141:  3.    Jbs/Jbr.
                    142:  length of byte,word,long.
                    143:  Vax opcodes are:      (Hex)
                    144:  bsbb          10
                    145:  brb           11
                    146:  These are like (2) but there is no condition to reverse.
                    147:  Always, 1 byte opcode, then displacement/absolute.
                    148:
                    149:  4a.   JacbX
                    150:  length of word, long.
                    151:  Vax opcodes are:      (Hex)
                    152:  acbw          3d
                    153:  acbf          4f
                    154:  acbd          6f
                    155:  abcb          9d
                    156:  acbl          f1
                    157:  acbg        4ffd
                    158:  acbh        6ffd
                    159:  Always, we cannot reverse the sense of the branch; we have a word
                    160:  displacement.
                    161:  The double-byte op-codes don't hurt: we never want to modify the
                    162:  opcode, so we don't care how many bytes are between the opcode and
                    163:  the operand.
                    164:
                    165:  4b.   JXobXXX
                    166:  length of long, long, byte.
                    167:  Vax opcodes are:      (Hex)
                    168:  aoblss                f2
                    169:  aobleq                f3
                    170:  sobgeq                f4
                    171:  sobgtr                f5
                    172:  Always, we cannot reverse the sense of the branch; we have a byte
                    173:  displacement.
                    174:
                    175:  The only time we need to modify the opcode is for class 2 instructions.
                    176:  After relax() we may complement the lowest order bit of such instruction
                    177:  to reverse sense of branch.
                    178:
                    179:  For class 2 instructions, we store context of "where is the opcode literal".
                    180:  We can change an opcode's lowest order bit without breaking anything else.
                    181:
                    182:  We sometimes store context in the operand literal. This way we can figure out
                    183:  after relax() what the original addressing mode was.  */
                    184: 
                    185: /* These displacements are relative to the start address of the
                    186:    displacement.  The first letter is Byte, Word.  2nd letter is
                    187:    Forward, Backward.  */
                    188: #define BF (1+ 127)
                    189: #define BB (1+-128)
                    190: #define WF (2+ 32767)
                    191: #define WB (2+-32768)
                    192: /* Dont need LF, LB because they always reach. [They are coded as 0.]  */
                    193:
                    194: #define C(a,b) ENCODE_RELAX(a,b)
                    195: /* This macro has no side-effects.  */
                    196: #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
                    197: #define RELAX_STATE(s) ((s) >> 2)
                    198: #define RELAX_LENGTH(s) ((s) & 3)
                    199:
                    200: const relax_typeS md_relax_table[] =
                    201: {
                    202:   {1, 1, 0, 0},                        /* error sentinel   0,0 */
                    203:   {1, 1, 0, 0},                        /* unused           0,1 */
                    204:   {1, 1, 0, 0},                        /* unused           0,2 */
                    205:   {1, 1, 0, 0},                        /* unused           0,3 */
                    206:
                    207:   {BF + 1, BB + 1, 2, C (1, 1)},/* B^"foo"         1,0 */
                    208:   {WF + 1, WB + 1, 3, C (1, 2)},/* W^"foo"         1,1 */
                    209:   {0, 0, 5, 0},                        /* L^"foo"          1,2 */
                    210:   {1, 1, 0, 0},                        /* unused           1,3 */
                    211:
                    212:   {BF, BB, 1, C (2, 1)},       /* b<cond> B^"foo"  2,0 */
                    213:   {WF + 2, WB + 2, 4, C (2, 2)},/* br.+? brw X     2,1 */
                    214:   {0, 0, 7, 0},                        /* br.+? jmp X      2,2 */
                    215:   {1, 1, 0, 0},                        /* unused           2,3 */
                    216:
                    217:   {BF, BB, 1, C (3, 1)},       /* brb B^foo        3,0 */
                    218:   {WF, WB, 2, C (3, 2)},       /* brw W^foo        3,1 */
                    219:   {0, 0, 5, 0},                        /* Jmp L^foo        3,2 */
                    220:   {1, 1, 0, 0},                        /* unused           3,3 */
                    221:
                    222:   {1, 1, 0, 0},                        /* unused           4,0 */
                    223:   {WF, WB, 2, C (4, 2)},       /* acb_ ^Wfoo       4,1 */
                    224:   {0, 0, 10, 0},               /* acb_,br,jmp L^foo4,2 */
                    225:   {1, 1, 0, 0},                        /* unused           4,3 */
                    226:
                    227:   {BF, BB, 1, C (5, 1)},       /* Xob___,,foo      5,0 */
                    228:   {WF + 4, WB + 4, 6, C (5, 2)},/* Xob.+2,brb.+3,brw5,1 */
                    229:   {0, 0, 9, 0},                        /* Xob.+2,brb.+6,jmp5,2 */
                    230:   {1, 1, 0, 0},                        /* unused           5,3 */
                    231: };
                    232:
                    233: #undef C
                    234: #undef BF
                    235: #undef BB
                    236: #undef WF
                    237: #undef WB
                    238:
                    239: void float_cons (int);
1.5     ! christos  240: int flonum_gen2vax (int, FLONUM_TYPE *, LITTLENUM_TYPE *);
1.1       christos  241:
                    242: const pseudo_typeS md_pseudo_table[] =
                    243: {
                    244:   {"dfloat", float_cons, 'd'},
                    245:   {"ffloat", float_cons, 'f'},
                    246:   {"gfloat", float_cons, 'g'},
                    247:   {"hfloat", float_cons, 'h'},
                    248:   {"d_floating", float_cons, 'd'},
                    249:   {"f_floating", float_cons, 'f'},
                    250:   {"g_floating", float_cons, 'g'},
                    251:   {"h_floating", float_cons, 'h'},
                    252:   {NULL, NULL, 0},
                    253: };
                    254:
                    255: #define STATE_PC_RELATIVE              (1)
                    256: #define STATE_CONDITIONAL_BRANCH       (2)
                    257: #define STATE_ALWAYS_BRANCH            (3)     /* includes BSB...  */
                    258: #define STATE_COMPLEX_BRANCH           (4)
                    259: #define STATE_COMPLEX_HOP              (5)
                    260:
                    261: #define STATE_BYTE                     (0)
                    262: #define STATE_WORD                     (1)
                    263: #define STATE_LONG                     (2)
                    264: #define STATE_UNDF                     (3)     /* Symbol undefined in pass1.  */
                    265:
                    266: #define min(a, b)      ((a) < (b) ? (a) : (b))
                    267: 
                    268: void
                    269: md_number_to_chars (char con[], valueT value, int nbytes)
                    270: {
                    271:   number_to_chars_littleendian (con, value, nbytes);
                    272: }
                    273:
                    274: /* Fix up some data or instructions after we find out the value of a symbol
                    275:    that they reference.  */
                    276:
                    277: void                           /* Knows about order of bytes in address.  */
                    278: md_apply_fix (fixS *fixP, valueT *valueP, segT seg ATTRIBUTE_UNUSED)
                    279: {
                    280:   valueT value = * valueP;
                    281:
1.3       christos  282:   if (fixP->fx_subsy != (symbolS *) NULL)
                    283:     as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
                    284:
                    285:   if (fixP->fx_addsy == NULL)
                    286:     fixP->fx_done = 1;
                    287:
                    288:   if (fixP->fx_done)
1.1       christos  289:     number_to_chars_littleendian (fixP->fx_where + fixP->fx_frag->fr_literal,
                    290:                                  value, fixP->fx_size);
1.3       christos  291:   else
                    292:     /* Initialise the part of an instruction frag covered by the
                    293:        relocation.  (Many occurrences of frag_more followed by fix_new
                    294:        lack any init of the frag.)  Since VAX uses RELA relocs the
                    295:        value we write into this field doesn't really matter.  */
                    296:     memset (fixP->fx_where + fixP->fx_frag->fr_literal, 0, fixP->fx_size);
1.1       christos  297: }
                    298:
                    299: /* Convert a number from VAX byte order (little endian)
                    300:    into host byte order.
                    301:    con         is the buffer to convert,
                    302:    nbytes      is the length of the given buffer.  */
                    303: static long
                    304: md_chars_to_number (unsigned char con[], int nbytes)
                    305: {
                    306:   long retval;
                    307:
                    308:   for (retval = 0, con += nbytes - 1; nbytes--; con--)
                    309:     {
                    310:       retval <<= BITS_PER_CHAR;
                    311:       retval |= *con;
                    312:     }
                    313:   return retval;
                    314: }
                    315:
                    316: /* Copy a bignum from in to out.
                    317:    If the output is shorter than the input, copy lower-order
                    318:    littlenums.  Return 0 or the number of significant littlenums
                    319:    dropped.  Assumes littlenum arrays are densely packed: no unused
                    320:    chars between the littlenums. Uses memcpy() to move littlenums, and
                    321:    wants to know length (in chars) of the input bignum.  */
                    322:
                    323: static int
                    324: bignum_copy (LITTLENUM_TYPE *in,
                    325:             int in_length,     /* in sizeof(littlenum)s */
                    326:             LITTLENUM_TYPE *out,
                    327:             int out_length     /* in sizeof(littlenum)s */)
                    328: {
                    329:   int significant_littlenums_dropped;
                    330:
                    331:   if (out_length < in_length)
                    332:     {
                    333:       LITTLENUM_TYPE *p;       /* -> most significant (non-zero) input
                    334:                                      littlenum.  */
                    335:
                    336:       memcpy ((void *) out, (void *) in,
                    337:              (unsigned int) out_length << LITTLENUM_SHIFT);
                    338:       for (p = in + in_length - 1; p >= in; --p)
                    339:        {
                    340:          if (*p)
                    341:            break;
                    342:        }
                    343:       significant_littlenums_dropped = p - in - in_length + 1;
                    344:
                    345:       if (significant_littlenums_dropped < 0)
                    346:        significant_littlenums_dropped = 0;
                    347:     }
                    348:   else
                    349:     {
                    350:       memcpy ((char *) out, (char *) in,
                    351:              (unsigned int) in_length << LITTLENUM_SHIFT);
                    352:
                    353:       if (out_length > in_length)
                    354:        memset ((char *) (out + in_length), '\0',
                    355:                (unsigned int) (out_length - in_length) << LITTLENUM_SHIFT);
                    356:
                    357:       significant_littlenums_dropped = 0;
                    358:     }
                    359:
                    360:   return significant_littlenums_dropped;
                    361: }
                    362: 
                    363: /* md_estimate_size_before_relax(), called just before relax().
                    364:    Any symbol that is now undefined will not become defined.
                    365:    Return the correct fr_subtype in the frag and the growth beyond
                    366:    fr_fix.  */
                    367: int
                    368: md_estimate_size_before_relax (fragS *fragP, segT segment)
                    369: {
                    370:   if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
                    371:     {
                    372:       if (S_GET_SEGMENT (fragP->fr_symbol) != segment
                    373: #ifdef OBJ_ELF
                    374:          || S_IS_WEAK (fragP->fr_symbol)
                    375:          || S_IS_EXTERNAL (fragP->fr_symbol)
                    376: #endif
                    377:          )
                    378:        {
                    379:          /* Non-relaxable cases.  */
                    380:          int reloc_type = NO_RELOC;
                    381:          char *p;
                    382:          int old_fr_fix;
                    383:
                    384:          old_fr_fix = fragP->fr_fix;
                    385:          p = fragP->fr_literal + old_fr_fix;
                    386: #ifdef OBJ_ELF
                    387:          /* If this is to an undefined symbol, then if it's an indirect
                    388:             reference indicate that is can mutated into a GLOB_DAT or
                    389:             JUMP_SLOT by the loader.  We restrict ourselves to no offset
                    390:             due to a limitation in the NetBSD linker.  */
                    391:
                    392:          if (GOT_symbol == NULL)
                    393:            GOT_symbol = symbol_find (GLOBAL_OFFSET_TABLE_NAME);
                    394:          if (PLT_symbol == NULL)
                    395:            PLT_symbol = symbol_find (PROCEDURE_LINKAGE_TABLE_NAME);
                    396:          if ((GOT_symbol == NULL || fragP->fr_symbol != GOT_symbol)
                    397:              && (PLT_symbol == NULL || fragP->fr_symbol != PLT_symbol)
                    398:              && fragP->fr_symbol != NULL
                    399:              && flag_want_pic
                    400: #ifdef OBJ_ELF
                    401:              && ELF_ST_VISIBILITY (S_GET_OTHER (fragP->fr_symbol)) != STV_HIDDEN
                    402: #endif
                    403:              && (!S_IS_DEFINED (fragP->fr_symbol)
                    404:                  || S_IS_WEAK (fragP->fr_symbol)
                    405:                  || S_IS_EXTERNAL (fragP->fr_symbol)))
                    406:            {
                    407:              /* Indirect references cannot go through the GOT or PLT,
                    408:                 let's hope they'll become local in the final link.  */
                    409:              if ((ELF_ST_VISIBILITY (S_GET_OTHER (fragP->fr_symbol))
                    410:                   != STV_DEFAULT)
                    411:                  || (p[0] & 0x10))
                    412:                reloc_type = BFD_RELOC_32_PCREL;
                    413:              else if (((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLS
                    414:                       || ((unsigned char *) fragP->fr_opcode)[0] == VAX_CALLG
                    415:                       || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JSB
                    416:                       || ((unsigned char *) fragP->fr_opcode)[0] == VAX_JMP
                    417:                       || S_IS_FUNCTION (fragP->fr_symbol))
                    418:                reloc_type = BFD_RELOC_32_PLT_PCREL;
                    419:              else
                    420:                reloc_type = BFD_RELOC_32_GOT_PCREL;
                    421:            }
                    422: #endif
                    423:          switch (RELAX_STATE (fragP->fr_subtype))
                    424:            {
                    425:            case STATE_PC_RELATIVE:
                    426:              p[0] |= VAX_PC_RELATIVE_MODE;     /* Preserve @ bit.  */
                    427:              fragP->fr_fix += 1 + 4;
                    428:              fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
                    429:                       fragP->fr_offset, 1, reloc_type);
                    430:              break;
                    431:
                    432:            case STATE_CONDITIONAL_BRANCH:
                    433:              *fragP->fr_opcode ^= 1;           /* Reverse sense of branch.  */
                    434:              p[0] = 6;
                    435:              p[1] = VAX_JMP;
                    436:              p[2] = VAX_PC_RELATIVE_MODE;      /* ...(PC) */
                    437:              fragP->fr_fix += 1 + 1 + 1 + 4;
                    438:              fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol,
                    439:                       fragP->fr_offset, 1, NO_RELOC);
                    440:              break;
                    441:
                    442:            case STATE_COMPLEX_BRANCH:
                    443:              p[0] = 2;
                    444:              p[1] = 0;
                    445:              p[2] = VAX_BRB;
                    446:              p[3] = 6;
                    447:              p[4] = VAX_JMP;
                    448:              p[5] = VAX_PC_RELATIVE_MODE;      /* ...(pc) */
                    449:              fragP->fr_fix += 2 + 2 + 1 + 1 + 4;
                    450:              fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol,
                    451:                       fragP->fr_offset, 1, NO_RELOC);
                    452:              break;
                    453:
                    454:            case STATE_COMPLEX_HOP:
                    455:              p[0] = 2;
                    456:              p[1] = VAX_BRB;
                    457:              p[2] = 6;
                    458:              p[3] = VAX_JMP;
                    459:              p[4] = VAX_PC_RELATIVE_MODE;      /* ...(pc) */
                    460:              fragP->fr_fix += 1 + 2 + 1 + 1 + 4;
                    461:              fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol,
                    462:                       fragP->fr_offset, 1, NO_RELOC);
                    463:              break;
                    464:
                    465:            case STATE_ALWAYS_BRANCH:
                    466:              *fragP->fr_opcode += VAX_WIDEN_LONG;
                    467:              p[0] = VAX_PC_RELATIVE_MODE;      /* ...(PC) */
                    468:              fragP->fr_fix += 1 + 4;
                    469:              fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol,
                    470:                       fragP->fr_offset, 1, NO_RELOC);
                    471:              break;
                    472:
                    473:            default:
                    474:              abort ();
                    475:            }
                    476:          frag_wane (fragP);
                    477:
                    478:          /* Return the growth in the fixed part of the frag.  */
                    479:          return fragP->fr_fix - old_fr_fix;
                    480:        }
                    481:
                    482:       /* Relaxable cases.  Set up the initial guess for the variable
                    483:         part of the frag.  */
                    484:       switch (RELAX_STATE (fragP->fr_subtype))
                    485:        {
                    486:        case STATE_PC_RELATIVE:
                    487:          fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
                    488:          break;
                    489:        case STATE_CONDITIONAL_BRANCH:
                    490:          fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
                    491:          break;
                    492:        case STATE_COMPLEX_BRANCH:
                    493:          fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD);
                    494:          break;
                    495:        case STATE_COMPLEX_HOP:
                    496:          fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE);
                    497:          break;
                    498:        case STATE_ALWAYS_BRANCH:
                    499:          fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE);
                    500:          break;
                    501:        }
                    502:     }
                    503:
                    504:   if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
                    505:     abort ();
                    506:
                    507:   /* Return the size of the variable part of the frag.  */
                    508:   return md_relax_table[fragP->fr_subtype].rlx_length;
                    509: }
                    510: 
                    511: /* Called after relax() is finished.
                    512:    In: Address of frag.
                    513:        fr_type == rs_machine_dependent.
                    514:        fr_subtype is what the address relaxed to.
                    515:
                    516:    Out:        Any fixSs and constants are set up.
                    517:        Caller will turn frag into a ".space 0".  */
                    518: void
                    519: md_convert_frag (bfd *headers ATTRIBUTE_UNUSED,
                    520:                 segT seg ATTRIBUTE_UNUSED,
                    521:                 fragS *fragP)
                    522: {
                    523:   char *addressP;              /* -> _var to change.  */
                    524:   char *opcodeP;               /* -> opcode char(s) to change.  */
                    525:   short int extension = 0;     /* Size of relaxed address.  */
                    526:   /* Added to fr_fix: incl. ALL var chars.  */
                    527:   symbolS *symbolP;
                    528:   long where;
                    529:
                    530:   know (fragP->fr_type == rs_machine_dependent);
                    531:   where = fragP->fr_fix;
                    532:   addressP = fragP->fr_literal + where;
                    533:   opcodeP = fragP->fr_opcode;
                    534:   symbolP = fragP->fr_symbol;
                    535:   know (symbolP);
                    536:
                    537:   switch (fragP->fr_subtype)
                    538:     {
                    539:     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
                    540:       know (*addressP == 0 || *addressP == 0x10);      /* '@' bit.  */
                    541:       addressP[0] |= 0xAF;     /* Byte displacement. */
                    542:       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
                    543:               fragP->fr_offset, 1, NO_RELOC);
                    544:       extension = 2;
                    545:       break;
                    546:
                    547:     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
                    548:       know (*addressP == 0 || *addressP == 0x10);      /* '@' bit.  */
                    549:       addressP[0] |= 0xCF;     /* Word displacement. */
                    550:       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
                    551:               fragP->fr_offset, 1, NO_RELOC);
                    552:       extension = 3;
                    553:       break;
                    554:
                    555:     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG):
                    556:       know (*addressP == 0 || *addressP == 0x10);      /* '@' bit.  */
                    557:       addressP[0] |= 0xEF;     /* Long word displacement. */
                    558:       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
                    559:               fragP->fr_offset, 1, NO_RELOC);
                    560:       extension = 5;
                    561:       break;
                    562:
                    563:     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
                    564:       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
                    565:               fragP->fr_offset, 1, NO_RELOC);
                    566:       extension = 1;
                    567:       break;
                    568:
                    569:     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
                    570:       opcodeP[0] ^= 1;         /* Reverse sense of test.  */
                    571:       addressP[0] = 3;
                    572:       addressP[1] = VAX_BRW;
                    573:       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
                    574:               fragP->fr_offset, 1, NO_RELOC);
                    575:       extension = 4;
                    576:       break;
                    577:
                    578:     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG):
                    579:       opcodeP[0] ^= 1;         /* Reverse sense of test.  */
                    580:       addressP[0] = 6;
                    581:       addressP[1] = VAX_JMP;
                    582:       addressP[2] = VAX_PC_RELATIVE_MODE;
                    583:       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
                    584:               fragP->fr_offset, 1, NO_RELOC);
                    585:       extension = 7;
                    586:       break;
                    587:
                    588:     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE):
                    589:       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
                    590:               fragP->fr_offset, 1, NO_RELOC);
                    591:       extension = 1;
                    592:       break;
                    593:
                    594:     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD):
                    595:       opcodeP[0] += VAX_WIDEN_WORD;    /* brb -> brw, bsbb -> bsbw */
                    596:       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
                    597:               1, NO_RELOC);
                    598:       extension = 2;
                    599:       break;
                    600:
                    601:     case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG):
                    602:       opcodeP[0] += VAX_WIDEN_LONG;    /* brb -> jmp, bsbb -> jsb */
                    603:       addressP[0] = VAX_PC_RELATIVE_MODE;
                    604:       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
                    605:               fragP->fr_offset, 1, NO_RELOC);
                    606:       extension = 5;
                    607:       break;
                    608:
                    609:     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD):
                    610:       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
                    611:               fragP->fr_offset, 1, NO_RELOC);
                    612:       extension = 2;
                    613:       break;
                    614:
                    615:     case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG):
                    616:       addressP[0] = 2;
                    617:       addressP[1] = 0;
                    618:       addressP[2] = VAX_BRB;
                    619:       addressP[3] = 6;
                    620:       addressP[4] = VAX_JMP;
                    621:       addressP[5] = VAX_PC_RELATIVE_MODE;
                    622:       fix_new (fragP, fragP->fr_fix + 6, 4, fragP->fr_symbol,
                    623:               fragP->fr_offset, 1, NO_RELOC);
                    624:       extension = 10;
                    625:       break;
                    626:
                    627:     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE):
                    628:       fix_new (fragP, fragP->fr_fix, 1, fragP->fr_symbol,
                    629:               fragP->fr_offset, 1, NO_RELOC);
                    630:       extension = 1;
                    631:       break;
                    632:
                    633:     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD):
                    634:       addressP[0] = 2;
                    635:       addressP[1] = VAX_BRB;
                    636:       addressP[2] = 3;
                    637:       addressP[3] = VAX_BRW;
                    638:       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
                    639:               fragP->fr_offset, 1, NO_RELOC);
                    640:       extension = 6;
                    641:       break;
                    642:
                    643:     case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG):
                    644:       addressP[0] = 2;
                    645:       addressP[1] = VAX_BRB;
                    646:       addressP[2] = 6;
                    647:       addressP[3] = VAX_JMP;
                    648:       addressP[4] = VAX_PC_RELATIVE_MODE;
                    649:       fix_new (fragP, fragP->fr_fix + 5, 4, fragP->fr_symbol,
                    650:               fragP->fr_offset, 1, NO_RELOC);
                    651:       extension = 9;
                    652:       break;
                    653:
                    654:     default:
                    655:       BAD_CASE (fragP->fr_subtype);
                    656:       break;
                    657:     }
                    658:   fragP->fr_fix += extension;
                    659: }
                    660:
                    661: /* Translate internal format of relocation info into target format.
                    662:
                    663:    On vax: first 4 bytes are normal unsigned long, next three bytes
                    664:    are symbolnum, least sig. byte first.  Last byte is broken up with
                    665:    the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
                    666:    bit 0 as pcrel.  */
                    667: #ifdef comment
                    668: void
                    669: md_ri_to_chars (char *the_bytes, struct reloc_info_generic ri)
                    670: {
                    671:   /* This is easy.  */
                    672:   md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address));
                    673:   /* Now the fun stuff.  */
                    674:   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
                    675:   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
                    676:   the_bytes[4] = ri.r_symbolnum & 0x0ff;
                    677:   the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06)
                    678:                  | ((ri.r_pcrel << 0) & 0x01)) & 0x0F;
                    679: }
                    680:
                    681: #endif /* comment */
                    682:
                    683: /*       BUGS, GRIPES,  APOLOGIA, etc.
                    684:
                    685:    The opcode table 'votstrs' needs to be sorted on opcode frequency.
                    686:    That is, AFTER we hash it with hash_...(), we want most-used opcodes
                    687:    to come out of the hash table faster.
                    688:
                    689:    I am sorry to inflict yet another VAX assembler on the world, but
                    690:    RMS says we must do everything from scratch, to prevent pin-heads
                    691:    restricting this software.
                    692:
                    693:    This is a vaguely modular set of routines in C to parse VAX
                    694:    assembly code using DEC mnemonics. It is NOT un*x specific.
                    695:
                    696:    The idea here is that the assembler has taken care of all:
                    697:      labels
                    698:      macros
                    699:      listing
                    700:      pseudo-ops
                    701:      line continuation
                    702:      comments
                    703:      condensing any whitespace down to exactly one space
                    704:    and all we have to do is parse 1 line into a vax instruction
                    705:    partially formed. We will accept a line, and deliver:
                    706:      an error message (hopefully empty)
                    707:      a skeleton VAX instruction (tree structure)
                    708:      textual pointers to all the operand expressions
                    709:      a warning message that notes a silly operand (hopefully empty)
                    710:
                    711:                E D I T   H I S T O R Y
                    712:
                    713:    17may86 Dean Elsner. Bug if line ends immediately after opcode.
                    714:    30apr86 Dean Elsner. New vip_op() uses arg block so change call.
                    715:     6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
                    716:     2jan86 Dean Elsner. Invent synthetic opcodes.
                    717:        Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
                    718:        which means this is not a real opcode, it is like a macro; it will
                    719:        be relax()ed into 1 or more instructions.
                    720:        Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
                    721:        like a regular branch instruction. Option added to vip_begin():
                    722:        exclude synthetic opcodes. Invent synthetic_votstrs[].
                    723:    31dec85 Dean Elsner. Invent vit_opcode_nbytes.
                    724:        Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
                    725:        so caller's don't have to know the difference between a 1-byte & a
                    726:        2-byte op-code. Still need vax_opcodeT concept, so we know how
                    727:        big an object must be to hold an op.code.
                    728:    30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
                    729:        because vax opcodes may be 16 bits. Our crufty C compiler was
                    730:        happily initialising 8-bit vot_codes with 16-bit numbers!
                    731:        (Wouldn't the 'phone company like to compress data so easily!)
                    732:    29dec85 Dean Elsner. New static table vax_operand_width_size[].
                    733:        Invented so we know hw many bytes a "I^#42" needs in its immediate
                    734:        operand. Revised struct vop in "vax-inst.h": explicitly include
                    735:        byte length of each operand, and it's letter-code datum type.
                    736:    17nov85 Dean Elsner. Name Change.
                    737:        Due to ar(1) truncating names, we learned the hard way that
                    738:        "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
                    739:        the archived object name. SO... we shortened the name of this
                    740:        source file, and changed the makefile.  */
                    741:
                    742: /* Handle of the OPCODE hash table.  */
                    743: static struct hash_control *op_hash;
                    744:
                    745: /* In: 1 character, from "bdfghloqpw" being the data-type of an operand
                    746:        of a vax instruction.
                    747:
                    748:    Out:        the length of an operand of that type, in bytes.
                    749:        Special branch operands types "-?!" have length 0.  */
                    750:
                    751: static const short int vax_operand_width_size[256] =
                    752: {
                    753:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    754:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    755:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    756:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    757:   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,     /* ..b.d.fgh...l..o  */
                    758:   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,     /* .q.....w........  */
                    759:   0, 0, 1, 0, 8, 0, 4, 8, 16, 0, 0, 0, 4, 0, 0,16,     /* ..b.d.fgh...l..o  */
                    760:   0, 8, 0, 0, 0, 0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0,     /* .q.....w........  */
                    761:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    762:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    763:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    764:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    765:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    766:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    767:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    768:   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
                    769: };
                    770: 
                    771: /* This perversion encodes all the vax opcodes as a bunch of strings.
                    772:    RMS says we should build our hash-table at run-time. Hmm.
                    773:    Please would someone arrange these in decreasing frequency of opcode?
                    774:    Because of the way hash_...() works, the most frequently used opcode
                    775:    should be textually first and so on.
                    776:
                    777:    Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
                    778:    So change 'vax.opcodes', then re-generate this table.  */
                    779:
                    780: #include "opcode/vax.h"
                    781: 
                    782: /* This is a table of optional op-codes. All of them represent
                    783:    'synthetic' instructions that seem popular.
                    784:
                    785:    Here we make some pseudo op-codes. Every code has a bit set to say
                    786:    it is synthetic. This lets you catch them if you want to
                    787:    ban these opcodes. They are mnemonics for "elastic" instructions
                    788:    that are supposed to assemble into the fewest bytes needed to do a
                    789:    branch, or to do a conditional branch, or whatever.
1.3       christos  790:
1.1       christos  791:    The opcode is in the usual place [low-order n*8 bits]. This means
                    792:    that if you mask off the bucky bits, the usual rules apply about
                    793:    how long the opcode is.
1.3       christos  794:
1.1       christos  795:    All VAX branch displacements come at the end of the instruction.
                    796:    For simple branches (1-byte opcode + 1-byte displacement) the last
                    797:    operand is coded 'b?' where the "data type" '?' is a clue that we
                    798:    may reverse the sense of the branch (complement lowest order bit)
                    799:    and branch around a jump. This is by far the most common case.
                    800:    That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
                    801:    a 0-byte op-code followed by 2 or more bytes of operand address.
1.3       christos  802:
1.1       christos  803:    If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
                    804:    case.
1.3       christos  805:
1.1       christos  806:    For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
                    807:    option before (2) we can directly JSB/JMP because there is no condition.
                    808:    These operands have 'b-' as their access/data type.
1.3       christos  809:
1.1       christos  810:    That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
                    811:    cases, we do the same idea. JACBxxx are all marked with a 'b!'
                    812:    JAOBxxx & JSOBxxx are marked with a 'b:'.  */
                    813: #if (VIT_OPCODE_SYNTHETIC != 0x80000000)
                    814: #error "You have just broken the encoding below, which assumes the sign bit means 'I am an imaginary instruction'."
                    815: #endif
                    816:
                    817: #if (VIT_OPCODE_SPECIAL != 0x40000000)
                    818: #error "You have just broken the encoding below, which assumes the 0x40 M bit means 'I am not to be "optimised" the way normal branches are'."
                    819: #endif
                    820:
                    821: static const struct vot
                    822:   synthetic_votstrs[] =
                    823: {
                    824:   {"jbsb",     {"b-", 0xC0000010}},            /* BSD 4.2 */
                    825: /* jsb used already */
                    826:   {"jbr",      {"b-", 0xC0000011}},            /* BSD 4.2 */
                    827:   {"jr",       {"b-", 0xC0000011}},            /* consistent */
                    828:   {"jneq",     {"b?", 0x80000012}},
                    829:   {"jnequ",    {"b?", 0x80000012}},
                    830:   {"jeql",     {"b?", 0x80000013}},
                    831:   {"jeqlu",    {"b?", 0x80000013}},
                    832:   {"jgtr",     {"b?", 0x80000014}},
                    833:   {"jleq",     {"b?", 0x80000015}},
                    834: /* un-used opcodes here */
                    835:   {"jgeq",     {"b?", 0x80000018}},
                    836:   {"jlss",     {"b?", 0x80000019}},
                    837:   {"jgtru",    {"b?", 0x8000001a}},
                    838:   {"jlequ",    {"b?", 0x8000001b}},
                    839:   {"jvc",      {"b?", 0x8000001c}},
                    840:   {"jvs",      {"b?", 0x8000001d}},
                    841:   {"jgequ",    {"b?", 0x8000001e}},
                    842:   {"jcc",      {"b?", 0x8000001e}},
                    843:   {"jlssu",    {"b?", 0x8000001f}},
                    844:   {"jcs",      {"b?", 0x8000001f}},
                    845:
                    846:   {"jacbw",    {"rwrwmwb!", 0xC000003d}},
                    847:   {"jacbf",    {"rfrfmfb!", 0xC000004f}},
                    848:   {"jacbd",    {"rdrdmdb!", 0xC000006f}},
                    849:   {"jacbb",    {"rbrbmbb!", 0xC000009d}},
                    850:   {"jacbl",    {"rlrlmlb!", 0xC00000f1}},
                    851:   {"jacbg",    {"rgrgmgb!", 0xC0004ffd}},
                    852:   {"jacbh",    {"rhrhmhb!", 0xC0006ffd}},
                    853:
                    854:   {"jbs",      {"rlvbb?", 0x800000e0}},
                    855:   {"jbc",      {"rlvbb?", 0x800000e1}},
                    856:   {"jbss",     {"rlvbb?", 0x800000e2}},
                    857:   {"jbcs",     {"rlvbb?", 0x800000e3}},
                    858:   {"jbsc",     {"rlvbb?", 0x800000e4}},
                    859:   {"jbcc",     {"rlvbb?", 0x800000e5}},
                    860:   {"jbssi",    {"rlvbb?", 0x800000e6}},
                    861:   {"jbcci",    {"rlvbb?", 0x800000e7}},
                    862:   {"jlbs",     {"rlb?", 0x800000e8}},
                    863:   {"jlbc",     {"rlb?", 0x800000e9}},
                    864:
                    865:   {"jaoblss",  {"rlmlb:", 0xC00000f2}},
                    866:   {"jaobleq",  {"rlmlb:", 0xC00000f3}},
                    867:   {"jsobgeq",  {"mlb:", 0xC00000f4}},
                    868:   {"jsobgtr",  {"mlb:", 0xC00000f5}},
                    869:
                    870: /* CASEx has no branch addresses in our conception of it.  */
                    871: /* You should use ".word ..." statements after the "case ...".  */
                    872:
                    873:   {"",         {"", 0}}        /* Empty is end sentinel.  */
                    874: };
                    875: 
                    876: /* Because this module is useful for both VMS and UN*X style assemblers
                    877:    and because of the variety of UN*X assemblers we must recognise
                    878:    the different conventions for assembler operand notation. For example
                    879:    VMS says "#42" for immediate mode, while most UN*X say "$42".
                    880:    We permit arbitrary sets of (single) characters to represent the
                    881:    3 concepts that DEC writes '#', '@', '^'.  */
                    882:
                    883: /* Character tests.  */
                    884: #define VIP_IMMEDIATE 01       /* Character is like DEC # */
                    885: #define VIP_INDIRECT  02       /* Char is like DEC @ */
                    886: #define VIP_DISPLEN   04       /* Char is like DEC ^ */
                    887:
                    888: #define IMMEDIATEP(c)  (vip_metacharacters [(c) & 0xff] & VIP_IMMEDIATE)
                    889: #define INDIRECTP(c)   (vip_metacharacters [(c) & 0xff] & VIP_INDIRECT)
                    890: #define DISPLENP(c)    (vip_metacharacters [(c) & 0xff] & VIP_DISPLEN)
                    891:
                    892: /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
                    893:    are ever called.  */
                    894:
                    895: #if defined(CONST_TABLE)
                    896: #define _ 0,
                    897: #define I VIP_IMMEDIATE,
                    898: #define S VIP_INDIRECT,
                    899: #define D VIP_DISPLEN,
                    900: static const char
                    901: vip_metacharacters[256] =
                    902: {
                    903:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /* ^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/
                    904:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /* ^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
                    905:   _ _ _ _ I _ _ _ _ _ S _ _ _ _ _      /* sp !  "  #  $  %  & '  (  )  *  +  ,  -  .  / */
                    906:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /*0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?*/
                    907:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /*@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O*/
                    908:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /*P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _*/
                    909:   D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /*`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o*/
                    910:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _      /*p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  ^?*/
                    911:
                    912:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    913:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    914:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    915:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    916:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    917:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    918:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    919:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                    920: };
                    921: #undef _
                    922: #undef I
                    923: #undef S
                    924: #undef D
                    925:
                    926: #else
                    927:
                    928: static char vip_metacharacters[256];
                    929:
                    930: static void
                    931: vip_op_1 (int bit, const char *syms)
                    932: {
                    933:   unsigned char t;
                    934:
                    935:   while ((t = *syms++) != 0)
                    936:     vip_metacharacters[t] |= bit;
                    937: }
                    938:
                    939: /* Can be called any time.  More arguments may appear in future.  */
                    940: static void
                    941: vip_op_defaults (const char *immediate, const char *indirect, const char *displen)
                    942: {
                    943:   vip_op_1 (VIP_IMMEDIATE, immediate);
                    944:   vip_op_1 (VIP_INDIRECT, indirect);
                    945:   vip_op_1 (VIP_DISPLEN, displen);
                    946: }
                    947:
                    948: #endif
                    949:
                    950: /* Call me once before you decode any lines.
                    951:    I decode votstrs into a hash table at op_hash (which I create).
                    952:    I return an error text or null.
                    953:    If you want, I will include the 'synthetic' jXXX instructions in the
                    954:    instruction table.
                    955:    You must nominate metacharacters for eg DEC's "#", "@", "^".  */
                    956:
                    957: static const char *
                    958: vip_begin (int synthetic_too,          /* 1 means include jXXX op-codes.  */
                    959:           const char *immediate,
                    960:           const char *indirect,
                    961:           const char *displen)
                    962: {
                    963:   const struct vot *vP;                /* scan votstrs */
                    964:   const char *retval = 0;      /* error text */
                    965:
                    966:   op_hash = hash_new ();
                    967:
                    968:   for (vP = votstrs; *vP->vot_name && !retval; vP++)
                    969:     retval = hash_insert (op_hash, vP->vot_name, (void *) &vP->vot_detail);
                    970:
                    971:   if (synthetic_too)
                    972:     for (vP = synthetic_votstrs; *vP->vot_name && !retval; vP++)
                    973:       retval = hash_insert (op_hash, vP->vot_name, (void *) &vP->vot_detail);
                    974:
                    975: #ifndef CONST_TABLE
                    976:   vip_op_defaults (immediate, indirect, displen);
                    977: #endif
                    978:
                    979:   return retval;
                    980: }
                    981:
                    982: /* Take 3 char.s, the last of which may be `\0` (non-existent)
                    983:    and return the VAX register number that they represent.
1.3       christos  984:
1.1       christos  985:    Return -1 if they don't form a register name. Good names return
                    986:    a number from 0:15 inclusive.
1.3       christos  987:
1.1       christos  988:    Case is not important in a name.
1.3       christos  989:
1.1       christos  990:    Register names understood are:
1.3       christos  991:
1.1       christos  992:        R0
                    993:        R1
                    994:        R2
                    995:        R3
                    996:        R4
                    997:        R5
                    998:        R6
                    999:        R7
                   1000:        R8
                   1001:        R9
                   1002:        R10
                   1003:        R11
                   1004:        R12     AP
                   1005:        R13     FP
                   1006:        R14     SP
                   1007:        R15     PC  */
                   1008:
                   1009: #define AP 12
                   1010: #define FP 13
                   1011: #define SP 14
                   1012: #define PC 15
                   1013:
                   1014: /* Returns the register number of something like '%r15' or 'ap', supplied
                   1015:    in four single chars. Returns -1 if the register isn't recognized,
                   1016:    0..15 otherwise.  */
                   1017: static int
                   1018: vax_reg_parse (char c1, char c2, char c3, char c4)
                   1019: {
                   1020:   int retval = -1;
                   1021:
                   1022: #ifdef OBJ_ELF
                   1023:   if (c1 != '%')       /* Register prefixes are mandatory for ELF.  */
                   1024:     return retval;
                   1025:   c1 = c2;
                   1026:   c2 = c3;
                   1027:   c3 = c4;
                   1028: #endif
                   1029: #ifdef OBJ_VMS
                   1030:   if (c4 != 0)         /* Register prefixes are not allowed under VMS.  */
                   1031:     return retval;
                   1032: #endif
                   1033: #ifdef OBJ_AOUT
                   1034:   if (c1 == '%')       /* Register prefixes are optional under a.out.  */
                   1035:     {
                   1036:       c1 = c2;
                   1037:       c2 = c3;
                   1038:       c3 = c4;
                   1039:     }
                   1040:   else if (c3 && c4)   /* Can't be 4 characters long.  */
                   1041:     return retval;
                   1042: #endif
                   1043:
                   1044:   c1 = TOLOWER (c1);
                   1045:   c2 = TOLOWER (c2);
                   1046:   if (ISDIGIT (c2) && c1 == 'r')
                   1047:     {
                   1048:       retval = c2 - '0';
                   1049:       if (ISDIGIT (c3))
                   1050:        {
                   1051:          retval = retval * 10 + c3 - '0';
                   1052:          retval = (retval > 15) ? -1 : retval;
                   1053:          /* clamp the register value to 1 hex digit */
                   1054:        }
                   1055:       else if (c3)
                   1056:        retval = -1;            /* c3 must be '\0' or a digit.  */
                   1057:     }
                   1058:   else if (c3)                 /* There are no three letter regs.  */
                   1059:     retval = -1;
                   1060:   else if (c2 == 'p')
                   1061:     {
                   1062:       switch (c1)
                   1063:        {
                   1064:        case 's':
                   1065:          retval = SP;
                   1066:          break;
                   1067:        case 'f':
                   1068:          retval = FP;
                   1069:          break;
                   1070:        case 'a':
                   1071:          retval = AP;
                   1072:          break;
                   1073:        default:
                   1074:          retval = -1;
                   1075:        }
                   1076:     }
                   1077:   else if (c1 == 'p' && c2 == 'c')
                   1078:     retval = PC;
                   1079:   else
                   1080:     retval = -1;
                   1081:   return retval;
                   1082: }
                   1083:
                   1084: #ifdef OBJ_AOUT
                   1085: #ifndef BFD_ASSEMBLER
                   1086: void
                   1087: tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
                   1088:      char *where;
                   1089:      fixS *fixP;
                   1090:      relax_addressT segment_address_in_file;
                   1091: {
                   1092:   /*
                   1093:    * In: length of relocation (or of address) in chars: 1, 2 or 4.
                   1094:    * Out: GNU LD relocation length code: 0, 1, or 2.
                   1095:    */
                   1096:
                   1097:   static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
                   1098:   int r_symbolnum;
                   1099:   int r_flags;
                   1100:
                   1101:   know (fixP->fx_addsy != NULL);
                   1102:
                   1103:   md_number_to_chars (where,
                   1104:        fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
                   1105:                      4);
                   1106:
                   1107:   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
                   1108:                 ? S_GET_TYPE (fixP->fx_addsy)
                   1109:                 : fixP->fx_addsy->sy_number);
                   1110:   r_flags = (fixP->fx_pcrel ? 1 : 0)
                   1111:       | (!S_IS_DEFINED (fixP->fx_addsy) ? 8 : 0)       /* extern */
                   1112:       | ((nbytes_r_length[fixP->fx_size] & 3) << 1);
                   1113:
                   1114: #if 0
                   1115:   r_flags |= ((!S_IS_DEFINED(fixP->fx_addsy)
                   1116:       && fixP->fx_pcrel
                   1117:       && fixP->fx_addsy != GOT_symbol
                   1118:       && fixP->fx_addsy != PLT_symbol
                   1119:       && flags_want_pic) ? 0x10 : 0);
                   1120: #endif
                   1121:
                   1122:   switch (fixP->fx_r_type) {
                   1123:        case NO_RELOC:
                   1124:                break;
                   1125:        case NO_RELOC2:
                   1126:                if (r_flags & 8)
                   1127:                    r_flags |= 0x80;            /* setting the copy bit */
                   1128:                                                /*   says we can convert */
                   1129:                                                /*   to gotslot if needed */
                   1130:                break;
                   1131:        case RELOC_32:
                   1132:                if (flag_want_pic && S_IS_EXTERNAL(fixP->fx_addsy)) {
                   1133:                        r_symbolnum = fixP->fx_addsy->sy_number;
                   1134:                        r_flags |= 8;           /* set extern bit */
                   1135:                }
                   1136:                break;
                   1137:        case RELOC_JMP_SLOT:
                   1138:                if (flag_want_pic) {
                   1139:                        r_flags |= 0x20;        /* set jmptable */
                   1140:                        r_flags &= ~0x08;       /* clear extern bit */
                   1141:                }
                   1142:                break;
                   1143:        case RELOC_JMP_TBL:
                   1144:                if (flag_want_pic) {
                   1145:                        r_flags |= 0x20;        /* set jmptable */
                   1146:                        r_flags |= 0x08;        /* set extern bit */
                   1147:                }
                   1148:                break;
                   1149:        case RELOC_GLOB_DAT:
                   1150:                if (flag_want_pic) {
                   1151:                        r_flags |= 0x10;        /* set baserel bit */
                   1152:                        r_symbolnum = fixP->fx_addsy->sy_number;
                   1153:                        if (S_IS_EXTERNAL(fixP->fx_addsy))
                   1154:                                r_flags |= 8;   /* set extern bit */
                   1155:                }
                   1156:                break;
                   1157:   }
                   1158:
                   1159:   where[4] = (r_symbolnum >>  0) & 0xff;
                   1160:   where[5] = (r_symbolnum >>  8) & 0xff;
                   1161:   where[6] = (r_symbolnum >> 16) & 0xff;
                   1162:   where[7] = r_flags;
                   1163: }
                   1164: #endif /* !BFD_ASSEMBLER */
                   1165: #endif /* OBJ_AOUT */
                   1166:
                   1167: /*
                   1168:  *       BUGS, GRIPES,  APOLOGIA, etc.
                   1169:  *
                   1170:  * The opcode table 'votstrs' needs to be sorted on opcode frequency.
                   1171:  * That is, AFTER we hash it with hash_...(), we want most-used opcodes
                   1172:  * to come out of the hash table faster.
                   1173:  *
                   1174:  * I am sorry to inflict yet another VAX assembler on the world, but
                   1175:  * RMS says we must do everything from scratch, to prevent pin-heads
                   1176:  * restricting this software.
                   1177:  */
                   1178:
                   1179: /*
                   1180:  * This is a vaguely modular set of routines in C to parse VAX
                   1181:  * assembly code using DEC mnemonics. It is NOT un*x specific.
                   1182:  *
                   1183:  * The idea here is that the assembler has taken care of all:
                   1184:  *   labels
                   1185:  *   macros
                   1186:  *   listing
                   1187:  *   pseudo-ops
                   1188:  *   line continuation
                   1189:  *   comments
                   1190:  *   condensing any whitespace down to exactly one space
                   1191:  * and all we have to do is parse 1 line into a vax instruction
                   1192:  * partially formed. We will accept a line, and deliver:
                   1193:  *   an error message (hopefully empty)
                   1194:  *   a skeleton VAX instruction (tree structure)
                   1195:  *   textual pointers to all the operand expressions
                   1196:  *   a warning message that notes a silly operand (hopefully empty)
                   1197:  */
                   1198: 
                   1199: /*
                   1200:  *             E D I T   H I S T O R Y
                   1201:  *
                   1202:  * 17may86 Dean Elsner. Bug if line ends immediately after opcode.
                   1203:  * 30apr86 Dean Elsner. New vip_op() uses arg block so change call.
                   1204:  *  6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
                   1205:  *  2jan86 Dean Elsner. Invent synthetic opcodes.
                   1206:  *     Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
                   1207:  *     which means this is not a real opcode, it is like a macro; it will
                   1208:  *     be relax()ed into 1 or more instructions.
                   1209:  *     Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
                   1210:  *     like a regular branch instruction. Option added to vip_begin():
                   1211:  *     exclude synthetic opcodes. Invent synthetic_votstrs[].
                   1212:  * 31dec85 Dean Elsner. Invent vit_opcode_nbytes.
                   1213:  *     Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
                   1214:  *     so caller's don't have to know the difference between a 1-byte & a
                   1215:  *     2-byte op-code. Still need vax_opcodeT concept, so we know how
                   1216:  *     big an object must be to hold an op.code.
                   1217:  * 30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
                   1218:  *     because vax opcodes may be 16 bits. Our crufty C compiler was
                   1219:  *     happily initialising 8-bit vot_codes with 16-bit numbers!
                   1220:  *     (Wouldn't the 'phone company like to compress data so easily!)
                   1221:  * 29dec85 Dean Elsner. New static table vax_operand_width_size[].
                   1222:  *     Invented so we know hw many bytes a "I^#42" needs in its immediate
                   1223:  *     operand. Revised struct vop in "vax-inst.h": explicitly include
                   1224:  *     byte length of each operand, and it's letter-code datum type.
                   1225:  * 17nov85 Dean Elsner. Name Change.
                   1226:  *     Due to ar(1) truncating names, we learned the hard way that
                   1227:  *     "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
                   1228:  *     the archived object name. SO... we shortened the name of this
                   1229:  *     source file, and changed the makefile.
                   1230:  */
                   1231:
                   1232: /* Parse a vax operand in DEC assembler notation.
                   1233:    For speed, expect a string of whitespace to be reduced to a single ' '.
                   1234:    This is the case for GNU AS, and is easy for other DEC-compatible
                   1235:    assemblers.
1.3       christos 1236:
1.1       christos 1237:    Knowledge about DEC VAX assembler operand notation lives here.
                   1238:    This doesn't even know what a register name is, except it believes
                   1239:    all register names are 2 or 3 characters, and lets vax_reg_parse() say
                   1240:    what number each name represents.
                   1241:    It does, however, know that PC, SP etc are special registers so it can
                   1242:    detect addressing modes that are silly for those registers.
1.3       christos 1243:
1.1       christos 1244:    Where possible, it delivers 1 fatal or 1 warning message if the operand
                   1245:    is suspect. Exactly what we test for is still evolving.
                   1246:
                   1247:    ---
                   1248:        Arg block.
1.3       christos 1249:
1.1       christos 1250:    There were a number of 'mismatched argument type' bugs to vip_op.
                   1251:    The most general solution is to typedef each (of many) arguments.
                   1252:    We used instead a typedef'd argument block. This is less modular
                   1253:    than using separate return pointers for each result, but runs faster
                   1254:    on most engines, and seems to keep programmers happy. It will have
                   1255:    to be done properly if we ever want to use vip_op as a general-purpose
                   1256:    module (it was designed to be).
1.3       christos 1257:
1.1       christos 1258:        G^
                   1259:
                   1260:    Doesn't support DEC "G^" format operands. These always take 5 bytes
                   1261:    to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
                   1262:    optimising to (say) a "B^" if you are lucky in the way you link.
                   1263:    When someone builds a linker smart enough to convert "G^" to "B^", "W^"
                   1264:    whenever possible, then we should implement it.
                   1265:    If there is some other use for "G^", feel free to code it in!
                   1266:
                   1267:        speed
1.3       christos 1268:
1.1       christos 1269:    If I nested if()s more, I could avoid testing (*err) which would save
                   1270:    time, space and page faults. I didn't nest all those if()s for clarity
                   1271:    and because I think the mode testing can be re-arranged 1st to test the
1.3       christos 1272:    commoner constructs 1st. Does anybody have statistics on this?
                   1273:
1.1       christos 1274:        error messages
1.3       christos 1275:
1.1       christos 1276:    In future, we should be able to 'compose' error messages in a scratch area
                   1277:    and give the user MUCH more informative error messages. Although this takes
                   1278:    a little more code at run-time, it will make this module much more self-
                   1279:    documenting. As an example of what sucks now: most error messages have
                   1280:    hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
                   1281:    the Un*x characters "$`*", that most users will expect from this AS.
                   1282:
                   1283:    ----
1.3       christos 1284:
1.1       christos 1285:    The input is a string, ending with '\0'.
1.3       christos 1286:
1.1       christos 1287:    We also require a 'hint' of what kind of operand is expected: so
                   1288:    we can remind caller not to write into literals for instance.
1.3       christos 1289:
1.1       christos 1290:    The output is a skeletal instruction.
1.3       christos 1291:
1.1       christos 1292:    The algorithm has two parts.
                   1293:    1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
                   1294:    2. express the @^#-()+[] as some parameters suited to further analysis.
1.3       christos 1295:
1.1       christos 1296:    2nd step is where we detect the googles of possible invalid combinations
                   1297:    a human (or compiler) might write. Note that if we do a half-way
                   1298:    decent assembler, we don't know how long to make (eg) displacement
                   1299:    fields when we first meet them (because they may not have defined values).
                   1300:    So we must wait until we know how many bits are needed for each address,
                   1301:    then we can know both length and opcodes of instructions.
                   1302:    For reason(s) above, we will pass to our caller a 'broken' instruction
                   1303:    of these major components, from which our caller can generate instructions:
                   1304:     -  displacement length      I^ S^ L^ B^ W^ unspecified
                   1305:     -  mode                     (many)
                   1306:     -  register                 R0-R15 or absent
                   1307:     -  index register           R0-R15 or absent
                   1308:     -  expression text          what we don't parse
                   1309:     -  error text(s)            why we couldn't understand the operand
                   1310:
                   1311:    ----
1.3       christos 1312:
1.1       christos 1313:    To decode output of this, test errtxt. If errtxt[0] == '\0', then
                   1314:    we had no errors that prevented parsing. Also, if we ever report
                   1315:    an internal bug, errtxt[0] is set non-zero. So one test tells you
                   1316:    if the other outputs are to be taken seriously.
                   1317:
                   1318:    ----
1.3       christos 1319:
1.1       christos 1320:    Dec defines the semantics of address modes (and values)
                   1321:    by a two-letter code, explained here.
1.3       christos 1322:
1.1       christos 1323:      letter 1:   access type
1.3       christos 1324:
1.1       christos 1325:        a         address calculation - no data access, registers forbidden
                   1326:        b         branch displacement
                   1327:        m         read - let go of bus - write back    "modify"
                   1328:        r         read
                   1329:        v         bit field address: like 'a' but registers are OK
                   1330:        w         write
                   1331:        space    no operator (eg ".long foo") [our convention]
1.3       christos 1332:
1.1       christos 1333:      letter 2:   data type (i.e. width, alignment)
1.3       christos 1334:
1.1       christos 1335:        b         byte
                   1336:        d         double precision floating point (D format)
                   1337:        f         single precision floating point (F format)
                   1338:        g         G format floating
                   1339:        h         H format floating
                   1340:        l         longword
                   1341:        o         octaword
                   1342:        q         quadword
                   1343:        w         word
                   1344:        ?        simple synthetic branch operand
                   1345:        -        unconditional synthetic JSB/JSR operand
                   1346:        !        complex synthetic branch operand
1.3       christos 1347:
1.1       christos 1348:    The '-?!' letter 2's are not for external consumption. They are used
                   1349:    for various assemblers. Generally, all unknown widths are assumed 0.
                   1350:    We don't limit your choice of width character.
1.3       christos 1351:
1.1       christos 1352:    DEC operands are hard work to parse. For example, '@' as the first
                   1353:    character means indirect (deferred) mode but elsewhere it is a shift
                   1354:    operator.
                   1355:    The long-winded explanation of how this is supposed to work is
                   1356:    cancelled. Read a DEC vax manual.
                   1357:    We try hard not to parse anything that MIGHT be part of the expression
                   1358:    buried in that syntax. For example if we see @...(Rn) we don't check
                   1359:    for '-' before the '(' because mode @-(Rn) does not exist.
1.3       christos 1360:
1.1       christos 1361:    After parsing we have:
1.3       christos 1362:
1.1       christos 1363:    at                     1 if leading '@' (or Un*x '*')
                   1364:    len                    takes one value from " bilsw". eg B^ -> 'b'.
                   1365:    hash                   1 if leading '#' (or Un*x '$')
                   1366:    expr_begin, expr_end   the expression we did not parse
                   1367:                           even though we don't interpret it, we make use
                   1368:                           of its presence or absence.
                   1369:    sign                   -1: -(Rn)    0: absent    +1: (Rn)+
                   1370:    paren                  1 if () are around register
                   1371:    reg                    major register number 0:15    -1 means absent
                   1372:    ndx                    index register number 0:15    -1 means absent
1.3       christos 1373:
1.1       christos 1374:    Again, I dare not explain it: just trace ALL the code!
                   1375:
                   1376:    Summary of vip_op outputs.
                   1377:
                   1378:   mode reg     len     ndx
                   1379:   (Rn) => @Rn
                   1380:   {@}Rn                        5+@     n       ' '     optional
                   1381:   branch operand               0       -1      ' '     -1
                   1382:   S^#foo                       0       -1      's'     -1
                   1383:   -(Rn)                        7       n       ' '     optional
                   1384:   {@}(Rn)+             8+@     n       ' '     optional
                   1385:   {@}#foo, no S^               8+@     PC      " i"    optional
                   1386:   {@}{q^}{(Rn)}                10+@+q  option  " bwl"  optional  */
                   1387:
                   1388: /* Dissect user-input 'optext' (which is something like "@B^foo@bar(AP)[FP]:")
                   1389:    using the vop in vopP. vopP's vop_access and vop_width. We fill _ndx, _reg,
                   1390:    _mode, _short, _warn, _error, _expr_begin, _expr_end and _nbytes.  */
                   1391:
                   1392: static void
                   1393: vip_op (char *optext, struct vop *vopP)
                   1394: {
                   1395:   /* Track operand text forward.  */
                   1396:   char *p;
                   1397:   /* Track operand text backward.  */
                   1398:   char *q;
                   1399:   /* 1 if leading '@' ('*') seen.  */
                   1400:   int at;
                   1401:   /* one of " bilsw" */
                   1402:   char len;
                   1403:   /* 1 if leading '#' ('$') seen.  */
                   1404:   int hash;
                   1405:   /* -1, 0 or +1.  */
                   1406:   int sign = 0;
                   1407:   /* 1 if () surround register.  */
                   1408:   int paren = 0;
                   1409:   /* Register number, -1:absent.  */
                   1410:   int reg = 0;
                   1411:   /* Index register number -1:absent.  */
                   1412:   int ndx = 0;
                   1413:   /* Report illegal operand, ""==OK.  */
                   1414:   /* " " is a FAKE error: means we won.  */
                   1415:   /* ANY err that begins with ' ' is a fake.  */
                   1416:   /* " " is converted to "" before return.  */
                   1417:   const char *err;
                   1418:   /* Warn about weird modes pf address.  */
                   1419:   const char *wrn;
                   1420:   /* Preserve q in case we backup.  */
                   1421:   char *oldq = NULL;
                   1422:   /* Build up 4-bit operand mode here.  */
                   1423:   /* Note: index mode is in ndx, this is.  */
                   1424:   /* The major mode of operand address.  */
                   1425:   int mode = 0;
                   1426:   /* Notice how we move wrong-arg-type bugs INSIDE this module: if we
                   1427:      get the types wrong below, we lose at compile time rather than at
                   1428:      lint or run time.  */
                   1429:   char access_mode;            /* vop_access.  */
                   1430:
                   1431:   access_mode = vopP->vop_access;
                   1432:   /* None of our code bugs (yet), no user text errors, no warnings
                   1433:      even.  */
                   1434:   err = wrn = 0;
                   1435:
                   1436:   p = optext;
                   1437:
                   1438:   if (*p == ' ')               /* Expect all whitespace reduced to ' '.  */
                   1439:     p++;                       /* skip over whitespace */
                   1440:
                   1441:   if ((at = INDIRECTP (*p)) != 0)
                   1442:     {                          /* 1 if *p=='@'(or '*' for Un*x) */
                   1443:       p++;                     /* at is determined */
                   1444:       if (*p == ' ')           /* Expect all whitespace reduced to ' '.  */
                   1445:        p++;                    /* skip over whitespace */
                   1446:     }
                   1447:
                   1448:   /* This code is subtle. It tries to detect all legal (letter)'^'
                   1449:      but it doesn't waste time explicitly testing for premature '\0' because
                   1450:      this case is rejected as a mismatch against either (letter) or '^'.  */
                   1451:   {
                   1452:     char c;
                   1453:
                   1454:     c = *p;
                   1455:     c = TOLOWER (c);
                   1456:     if (DISPLENP (p[1]) && strchr ("bilws", len = c))
                   1457:       p += 2;                  /* Skip (letter) '^'.  */
                   1458:     else                       /* No (letter) '^' seen.  */
                   1459:       len = ' ';               /* Len is determined.  */
                   1460:   }
                   1461:
                   1462:   if (*p == ' ')               /* Expect all whitespace reduced to ' '.  */
                   1463:     p++;
                   1464:
                   1465:   if ((hash = IMMEDIATEP (*p)) != 0)   /* 1 if *p=='#' ('$' for Un*x) */
                   1466:     p++;                       /* Hash is determined.  */
                   1467:
                   1468:   /* p points to what may be the beginning of an expression.
                   1469:      We have peeled off the front all that is peelable.
                   1470:      We know at, len, hash.
1.3       christos 1471:
1.1       christos 1472:      Lets point q at the end of the text and parse that (backwards).  */
                   1473:
                   1474:   for (q = p; *q; q++)
                   1475:     ;
                   1476:   q--;                         /* Now q points at last char of text.  */
                   1477:
                   1478:   if (*q == ' ' && q >= p)     /* Expect all whitespace reduced to ' '.  */
                   1479:     q--;
                   1480:
                   1481:   /* Reverse over whitespace, but don't.  */
                   1482:   /* Run back over *p.  */
                   1483:
                   1484:   /* As a matter of policy here, we look for [Rn], although both Rn and S^#
                   1485:      forbid [Rn]. This is because it is easy, and because only a sick
                   1486:      cyborg would have [...] trailing an expression in a VAX-like assembler.
                   1487:      A meticulous parser would first check for Rn followed by '(' or '['
                   1488:      and not parse a trailing ']' if it found another. We just ban expressions
                   1489:      ending in ']'.  */
                   1490:   if (*q == ']')
                   1491:     {
                   1492:       while (q >= p && *q != '[')
                   1493:        q--;
                   1494:       /* Either q<p or we got matching '['.  */
                   1495:       if (q < p)
                   1496:        err = _("no '[' to match ']'");
                   1497:       else
                   1498:        {
                   1499:          /* Confusers like "[]" will eventually lose with a bad register
                   1500:           * name error. So again we don't need to check for early '\0'.  */
                   1501:          if (q[3] == ']')
                   1502:            ndx = vax_reg_parse (q[1], q[2], 0, 0);
                   1503:          else if (q[4] == ']')
                   1504:            ndx = vax_reg_parse (q[1], q[2], q[3], 0);
                   1505:          else if (q[5] == ']')
                   1506:            ndx = vax_reg_parse (q[1], q[2], q[3], q[4]);
                   1507:          else
                   1508:            ndx = -1;
                   1509:          /* Since we saw a ']' we will demand a register name in the [].
                   1510:           * If luser hasn't given us one: be rude.  */
                   1511:          if (ndx < 0)
                   1512:            err = _("bad register in []");
                   1513:          else if (ndx == PC)
                   1514:            err = _("[PC] index banned");
                   1515:          else
                   1516:            /* Point q just before "[...]".  */
                   1517:            q--;
                   1518:        }
                   1519:     }
                   1520:   else
                   1521:     /* No ']', so no iNDeX register.  */
                   1522:     ndx = -1;
                   1523:
                   1524:   /* If err = "..." then we lost: run away.
                   1525:      Otherwise ndx == -1 if there was no "[...]".
                   1526:      Otherwise, ndx is index register number, and q points before "[...]".  */
                   1527:
                   1528:   if (*q == ' ' && q >= p)     /* Expect all whitespace reduced to ' '.  */
                   1529:     q--;
                   1530:   /* Reverse over whitespace, but don't.  */
                   1531:   /* Run back over *p.  */
                   1532:   if (!err || !*err)
                   1533:     {
                   1534:       /* no ()+ or -() seen yet */
                   1535:       sign = 0;
                   1536:
                   1537:       if (q > p + 3 && *q == '+' && q[-1] == ')')
                   1538:        {
                   1539:          sign = 1;             /* we saw a ")+" */
                   1540:          q--;                  /* q points to ')' */
                   1541:        }
                   1542:
                   1543:       if (*q == ')' && q > p + 2)
                   1544:        {
                   1545:          paren = 1;            /* assume we have "(...)" */
                   1546:          while (q >= p && *q != '(')
                   1547:            q--;
                   1548:          /* either q<p or we got matching '(' */
                   1549:          if (q < p)
                   1550:            err = _("no '(' to match ')'");
                   1551:          else
                   1552:            {
                   1553:              /* Confusers like "()" will eventually lose with a bad register
                   1554:                 name error. So again we don't need to check for early '\0'.  */
                   1555:              if (q[3] == ')')
                   1556:                reg = vax_reg_parse (q[1], q[2], 0, 0);
                   1557:              else if (q[4] == ')')
                   1558:                reg = vax_reg_parse (q[1], q[2], q[3], 0);
                   1559:              else if (q[5] == ')')
                   1560:                reg = vax_reg_parse (q[1], q[2], q[3], q[4]);
                   1561:              else
                   1562:                reg = -1;
                   1563:              /* Since we saw a ')' we will demand a register name in the ')'.
                   1564:                 This is nasty: why can't our hypothetical assembler permit
                   1565:                 parenthesised expressions? BECAUSE I AM LAZY! That is why.
                   1566:                 Abuse luser if we didn't spy a register name.  */
                   1567:              if (reg < 0)
                   1568:                {
                   1569:                  /* JF allow parenthesized expressions.  I hope this works.  */
                   1570:                  paren = 0;
                   1571:                  while (*q != ')')
                   1572:                    q++;
                   1573:                  /* err = "unknown register in ()"; */
                   1574:                }
                   1575:              else
                   1576:                q--;            /* point just before '(' of "(...)" */
                   1577:              /* If err == "..." then we lost. Run away.
                   1578:                 Otherwise if reg >= 0 then we saw (Rn).  */
                   1579:            }
                   1580:          /* If err == "..." then we lost.
                   1581:             Otherwise paren==1 and reg = register in "()".  */
                   1582:        }
                   1583:       else
                   1584:        paren = 0;
                   1585:       /* If err == "..." then we lost.
                   1586:          Otherwise, q points just before "(Rn)", if any.
                   1587:          If there was a "(...)" then paren==1, and reg is the register.  */
                   1588:
                   1589:       /* We should only seek '-' of "-(...)" if:
                   1590:            we saw "(...)"                    paren == 1
                   1591:            we have no errors so far          ! *err
                   1592:            we did not see '+' of "(...)+"    sign < 1
                   1593:          We don't check len. We want a specific error message later if
                   1594:          user tries "x^...-(Rn)". This is a feature not a bug.  */
                   1595:       if (!err || !*err)
                   1596:        {
                   1597:          if (paren && sign < 1)/* !sign is adequate test */
                   1598:            {
                   1599:              if (*q == '-')
                   1600:                {
                   1601:                  sign = -1;
                   1602:                  q--;
                   1603:                }
                   1604:            }
                   1605:          /* We have back-tracked over most
                   1606:             of the crud at the end of an operand.
                   1607:             Unless err, we know: sign, paren. If paren, we know reg.
                   1608:             The last case is of an expression "Rn".
                   1609:             This is worth hunting for if !err, !paren.
                   1610:             We wouldn't be here if err.
                   1611:             We remember to save q, in case we didn't want "Rn" anyway.  */
                   1612:          if (!paren)
                   1613:            {
                   1614:              if (*q == ' ' && q >= p)  /* Expect all whitespace reduced to ' '.  */
                   1615:                q--;
                   1616:              /* Reverse over whitespace, but don't.  */
                   1617:              /* Run back over *p.  */
                   1618:              /* Room for Rn or Rnn (include prefix) exactly?  */
                   1619:              if (q > p && q < p + 4)
                   1620:                reg = vax_reg_parse (p[0], p[1],
                   1621:                  q < p + 2 ? 0 : p[2],
                   1622:                  q < p + 3 ? 0 : p[3]);
                   1623:              else
                   1624:                reg = -1;       /* Always comes here if no register at all.  */
                   1625:              /* Here with a definitive reg value.  */
                   1626:              if (reg >= 0)
                   1627:                {
                   1628:                  oldq = q;
                   1629:                  q = p - 1;
                   1630:                }
                   1631:            }
                   1632:        }
                   1633:     }
                   1634:   /* have reg. -1:absent; else 0:15.  */
                   1635:
                   1636:   /* We have:  err, at, len, hash, ndx, sign, paren, reg.
                   1637:      Also, any remaining expression is from *p through *q inclusive.
                   1638:      Should there be no expression, q==p-1. So expression length = q-p+1.
                   1639:      This completes the first part: parsing the operand text.  */
                   1640: 
                   1641:   /* We now want to boil the data down, checking consistency on the way.
                   1642:      We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
                   1643:      We will deliver a 4-bit reg, and a 4-bit mode.  */
                   1644:
                   1645:   /* Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
1.3       christos 1646:
1.1       christos 1647:      in:  at   ?
                   1648:           len  ?
                   1649:           hash ?
                   1650:           p:q  ?
                   1651:           sign  ?
                   1652:           paren        ?
                   1653:           reg   ?
                   1654:           ndx   ?
1.3       christos 1655:
1.1       christos 1656:      out: mode  0
                   1657:           reg   -1
                   1658:           len  ' '
                   1659:           p:q  whatever was input
                   1660:           ndx  -1
                   1661:           err  " "              or error message, and other outputs trashed.  */
                   1662:   /* Branch operands have restricted forms.  */
                   1663:   if ((!err || !*err) && access_mode == 'b')
                   1664:     {
                   1665:       if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
                   1666:        err = _("invalid branch operand");
                   1667:       else
                   1668:        err = " ";
                   1669:     }
                   1670:
                   1671:   /* Since nobody seems to use it: comment this 'feature'(?) out for now.  */
                   1672: #ifdef NEVER
                   1673:   /* Case of stand-alone operand. e.g. ".long foo"
1.3       christos 1674:
1.1       christos 1675:      in:  at   ?
                   1676:           len  ?
                   1677:           hash ?
                   1678:           p:q  ?
                   1679:           sign  ?
                   1680:           paren        ?
                   1681:           reg   ?
                   1682:           ndx   ?
1.3       christos 1683:
1.1       christos 1684:      out: mode  0
                   1685:           reg   -1
                   1686:           len  ' '
                   1687:           p:q  whatever was input
                   1688:           ndx  -1
                   1689:           err  " "              or error message, and other outputs trashed.  */
                   1690:   if ((!err || !*err) && access_mode == ' ')
                   1691:     {
                   1692:       if (at)
                   1693:        err = _("address prohibits @");
                   1694:       else if (hash)
                   1695:        err = _("address prohibits #");
                   1696:       else if (sign)
                   1697:        {
                   1698:          if (sign < 0)
                   1699:            err = _("address prohibits -()");
                   1700:          else
                   1701:            err = _("address prohibits ()+");
                   1702:        }
                   1703:       else if (paren)
                   1704:        err = _("address prohibits ()");
                   1705:       else if (ndx >= 0)
                   1706:        err = _("address prohibits []");
                   1707:       else if (reg >= 0)
                   1708:        err = _("address prohibits register");
                   1709:       else if (len != ' ')
                   1710:        err = _("address prohibits displacement length specifier");
                   1711:       else
                   1712:        {
                   1713:          err = " ";    /* succeed */
                   1714:          mode = 0;
                   1715:        }
                   1716:     }
                   1717: #endif
                   1718:
                   1719:   /* Case of S^#.
1.3       christos 1720:
1.1       christos 1721:      in:  at       0
                   1722:           len      's'               definition
                   1723:           hash     1              demand
                   1724:           p:q                        demand not empty
                   1725:           sign     0                 by paren==0
                   1726:           paren    0             by "()" scan logic because "S^" seen
                   1727:           reg      -1                or nn by mistake
                   1728:           ndx      -1
1.3       christos 1729:
1.1       christos 1730:      out: mode     0
                   1731:           reg      -1
                   1732:           len      's'
                   1733:           exp
                   1734:           ndx      -1  */
                   1735:   if ((!err || !*err) && len == 's')
                   1736:     {
                   1737:       if (!hash || paren || at || ndx >= 0)
                   1738:        err = _("invalid operand of S^#");
                   1739:       else
                   1740:        {
                   1741:          if (reg >= 0)
                   1742:            {
                   1743:              /* Darn! we saw S^#Rnn ! put the Rnn back in
                   1744:                 expression. KLUDGE! Use oldq so we don't
                   1745:                 need to know exact length of reg name.  */
                   1746:              q = oldq;
                   1747:              reg = 0;
                   1748:            }
                   1749:          /* We have all the expression we will ever get.  */
                   1750:          if (p > q)
                   1751:            err = _("S^# needs expression");
                   1752:          else if (access_mode == 'r')
                   1753:            {
                   1754:              err = " ";        /* WIN! */
                   1755:              mode = 0;
                   1756:            }
                   1757:          else
                   1758:            err = _("S^# may only read-access");
                   1759:        }
                   1760:     }
1.3       christos 1761:
1.1       christos 1762:   /* Case of -(Rn), which is weird case.
1.3       christos 1763:
1.1       christos 1764:      in:  at       0
                   1765:           len      '
                   1766:           hash     0
                   1767:           p:q      q<p
                   1768:           sign     -1                by definition
                   1769:           paren    1              by definition
                   1770:           reg      present           by definition
                   1771:           ndx      optional
1.3       christos 1772:
1.1       christos 1773:      out: mode     7
                   1774:           reg      present
                   1775:           len      ' '
                   1776:           exp      ""                enforce empty expression
                   1777:           ndx      optional          warn if same as reg.  */
                   1778:   if ((!err || !*err) && sign < 0)
                   1779:     {
                   1780:       if (len != ' ' || hash || at || p <= q)
                   1781:        err = _("invalid operand of -()");
                   1782:       else
                   1783:        {
                   1784:          err = " ";            /* win */
                   1785:          mode = 7;
                   1786:          if (reg == PC)
                   1787:            wrn = _("-(PC) unpredictable");
                   1788:          else if (reg == ndx)
                   1789:            wrn = _("[]index same as -()register: unpredictable");
                   1790:        }
                   1791:     }
                   1792:
                   1793:   /* We convert "(Rn)" to "@Rn" for our convenience.
                   1794:      (I hope this is convenient: has someone got a better way to parse this?)
                   1795:      A side-effect of this is that "@Rn" is a valid operand.  */
                   1796:   if (paren && !sign && !hash && !at && len == ' ' && p > q)
                   1797:     {
                   1798:       at = 1;
                   1799:       paren = 0;
                   1800:     }
                   1801:
                   1802:   /* Case of (Rn)+, which is slightly different.
1.3       christos 1803:
1.1       christos 1804:      in:  at
                   1805:           len      ' '
                   1806:           hash     0
                   1807:           p:q      q<p
                   1808:           sign     +1                by definition
                   1809:           paren    1              by definition
                   1810:           reg      present           by definition
                   1811:           ndx      optional
1.3       christos 1812:
1.1       christos 1813:      out: mode     8+@
                   1814:           reg      present
                   1815:           len      ' '
                   1816:           exp      ""                enforce empty expression
                   1817:           ndx      optional          warn if same as reg.  */
                   1818:   if ((!err || !*err) && sign > 0)
                   1819:     {
                   1820:       if (len != ' ' || hash || p <= q)
                   1821:        err = _("invalid operand of ()+");
                   1822:       else
                   1823:        {
                   1824:          err = " ";            /* win */
                   1825:          mode = 8 + (at ? 1 : 0);
                   1826:          if (reg == PC)
                   1827:            wrn = _("(PC)+ unpredictable");
                   1828:          else if (reg == ndx)
                   1829:            wrn = _("[]index same as ()+register: unpredictable");
                   1830:        }
                   1831:     }
                   1832:
                   1833:   /* Case of #, without S^.
1.3       christos 1834:
1.1       christos 1835:      in:  at
                   1836:           len      ' ' or 'i'
                   1837:           hash     1              by definition
                   1838:           p:q
                   1839:           sign     0
                   1840:           paren    0
                   1841:           reg      absent
                   1842:           ndx      optional
1.3       christos 1843:
1.1       christos 1844:      out: mode     8+@
                   1845:           reg      PC
                   1846:           len      ' ' or 'i'
                   1847:           exp
                   1848:           ndx      optional.  */
                   1849:   if ((!err || !*err) && hash)
                   1850:     {
                   1851:       if (len != 'i' && len != ' ')
                   1852:        err = _("# conflicts length");
                   1853:       else if (paren)
                   1854:        err = _("# bars register");
                   1855:       else
                   1856:        {
                   1857:          if (reg >= 0)
                   1858:            {
                   1859:              /* Darn! we saw #Rnn! Put the Rnn back into the expression.
                   1860:                 By using oldq, we don't need to know how long Rnn was.
                   1861:                 KLUDGE!  */
                   1862:              q = oldq;
                   1863:              reg = -1;         /* No register any more.  */
                   1864:            }
                   1865:          err = " ";            /* Win.  */
                   1866:
                   1867:          /* JF a bugfix, I think!  */
                   1868:          if (at && access_mode == 'a')
                   1869:            vopP->vop_nbytes = 4;
                   1870:
                   1871:          mode = (at ? 9 : 8);
                   1872:          reg = PC;
                   1873:          if ((access_mode == 'm' || access_mode == 'w') && !at)
                   1874:            wrn = _("writing or modifying # is unpredictable");
                   1875:        }
                   1876:     }
                   1877:   /* If !*err, then       sign == 0
                   1878:                           hash == 0 */
                   1879:
                   1880:   /* Case of Rn. We separate this one because it has a few special
                   1881:      errors the remaining modes lack.
1.3       christos 1882:
1.1       christos 1883:      in:  at       optional
                   1884:           len      ' '
                   1885:           hash     0             by program logic
                   1886:           p:q      empty
                   1887:           sign     0                 by program logic
                   1888:           paren    0             by definition
                   1889:           reg      present           by definition
                   1890:           ndx      optional
1.3       christos 1891:
1.1       christos 1892:      out: mode     5+@
                   1893:           reg      present
                   1894:           len      ' '               enforce no length
                   1895:           exp      ""                enforce empty expression
                   1896:           ndx      optional          warn if same as reg.  */
                   1897:   if ((!err || !*err) && !paren && reg >= 0)
                   1898:     {
                   1899:       if (len != ' ')
                   1900:        err = _("length not needed");
                   1901:       else if (at)
                   1902:        {
                   1903:          err = " ";            /* win */
                   1904:          mode = 6;             /* @Rn */
                   1905:        }
                   1906:       else if (ndx >= 0)
                   1907:        err = _("can't []index a register, because it has no address");
                   1908:       else if (access_mode == 'a')
                   1909:        err = _("a register has no address");
                   1910:       else
                   1911:        {
                   1912:          /* Idea here is to detect from length of datum
                   1913:             and from register number if we will touch PC.
                   1914:             Warn if we do.
                   1915:             vop_nbytes is number of bytes in operand.
                   1916:             Compute highest byte affected, compare to PC0.  */
                   1917:          if ((vopP->vop_nbytes + reg * 4) > 60)
                   1918:            wrn = _("PC part of operand unpredictable");
                   1919:          err = " ";            /* win */
                   1920:          mode = 5;             /* Rn */
                   1921:        }
                   1922:     }
                   1923:   /* If !*err,        sign  == 0
                   1924:                       hash  == 0
                   1925:                       paren == 1  OR reg==-1  */
                   1926:
                   1927:   /* Rest of cases fit into one bunch.
1.3       christos 1928:
1.1       christos 1929:      in:  at       optional
                   1930:           len      ' ' or 'b' or 'w' or 'l'
                   1931:           hash     0             by program logic
                   1932:           p:q      expected          (empty is not an error)
                   1933:           sign     0                 by program logic
                   1934:           paren    optional
                   1935:           reg      optional
                   1936:           ndx      optional
1.3       christos 1937:
1.1       christos 1938:      out: mode     10 + @ + len
                   1939:           reg      optional
                   1940:           len      ' ' or 'b' or 'w' or 'l'
                   1941:           exp                        maybe empty
                   1942:           ndx      optional          warn if same as reg.  */
                   1943:   if (!err || !*err)
                   1944:     {
                   1945:       err = " ";               /* win (always) */
                   1946:       mode = 10 + (at ? 1 : 0);
                   1947:       switch (len)
                   1948:        {
                   1949:        case 'l':
                   1950:          mode += 2;
                   1951:        case 'w':
                   1952:          mode += 2;
                   1953:        case ' ':       /* Assumed B^ until our caller changes it.  */
                   1954:        case 'b':
                   1955:          break;
                   1956:        }
                   1957:     }
                   1958:
                   1959:   /* here with completely specified     mode
                   1960:                                        len
                   1961:                                        reg
                   1962:                                        expression   p,q
                   1963:                                        ndx.  */
                   1964:
                   1965:   if (*err == ' ')
                   1966:     err = 0;                   /* " " is no longer an error.  */
                   1967:
                   1968:   vopP->vop_mode = mode;
                   1969:   vopP->vop_reg = reg;
                   1970:   vopP->vop_short = len;
                   1971:   vopP->vop_expr_begin = p;
                   1972:   vopP->vop_expr_end = q;
                   1973:   vopP->vop_ndx = ndx;
                   1974:   vopP->vop_error = err;
                   1975:   vopP->vop_warn = wrn;
                   1976: }
                   1977:
                   1978: /* This converts a string into a vax instruction.
                   1979:    The string must be a bare single instruction in dec-vax (with BSD4 frobs)
                   1980:    format.
                   1981:    It provides some error messages: at most one fatal error message (which
                   1982:    stops the scan) and at most one warning message for each operand.
                   1983:    The vax instruction is returned in exploded form, since we have no
                   1984:    knowledge of how you parse (or evaluate) your expressions.
                   1985:    We do however strip off and decode addressing modes and operation
                   1986:    mnemonic.
1.3       christos 1987:
1.1       christos 1988:    The exploded instruction is returned to a struct vit of your choice.
                   1989:    #include "vax-inst.h" to know what a struct vit is.
1.3       christos 1990:
1.1       christos 1991:    This function's value is a string. If it is not "" then an internal
                   1992:    logic error was found: read this code to assign meaning to the string.
                   1993:    No argument string should generate such an error string:
                   1994:    it means a bug in our code, not in the user's text.
1.3       christos 1995:
1.1       christos 1996:    You MUST have called vip_begin() once before using this function.  */
                   1997:
                   1998: static void
                   1999: vip (struct vit *vitP,         /* We build an exploded instruction here.  */
                   2000:      char *instring)           /* Text of a vax instruction: we modify.  */
                   2001: {
                   2002:   /* How to bit-encode this opcode.  */
                   2003:   struct vot_wot *vwP;
                   2004:   /* 1/skip whitespace.2/scan vot_how */
                   2005:   char *p;
                   2006:   char *q;
                   2007:   /* counts number of operands seen */
                   2008:   unsigned char count;
                   2009:   /* scan operands in struct vit */
                   2010:   struct vop *operandp;
                   2011:   /* error over all operands */
                   2012:   const char *alloperr;
                   2013:   /* Remember char, (we clobber it with '\0' temporarily).  */
                   2014:   char c;
                   2015:   /* Op-code of this instruction.  */
                   2016:   vax_opcodeT oc;
                   2017:
                   2018:   if (*instring == ' ')
                   2019:     ++instring;
1.3       christos 2020:
1.1       christos 2021:   /* MUST end in end-of-string or exactly 1 space.  */
                   2022:   for (p = instring; *p && *p != ' '; p++)
                   2023:     ;
                   2024:
                   2025:   /* Scanned up to end of operation-code.  */
                   2026:   /* Operation-code is ended with whitespace.  */
                   2027:   if (p - instring == 0)
                   2028:     {
                   2029:       vitP->vit_error = _("No operator");
                   2030:       count = 0;
                   2031:       memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
                   2032:     }
                   2033:   else
                   2034:     {
                   2035:       c = *p;
                   2036:       *p = '\0';
                   2037:       /* Here with instring pointing to what better be an op-name, and p
                   2038:          pointing to character just past that.
                   2039:          We trust instring points to an op-name, with no whitespace.  */
                   2040:       vwP = (struct vot_wot *) hash_find (op_hash, instring);
                   2041:       /* Restore char after op-code.  */
                   2042:       *p = c;
                   2043:       if (vwP == 0)
                   2044:        {
                   2045:          vitP->vit_error = _("Unknown operator");
                   2046:          count = 0;
                   2047:          memset (vitP->vit_opcode, '\0', sizeof (vitP->vit_opcode));
                   2048:        }
                   2049:       else
                   2050:        {
                   2051:          /* We found a match! So let's pick up as many operands as the
                   2052:             instruction wants, and even gripe if there are too many.
                   2053:             We expect comma to separate each operand.
                   2054:             We let instring track the text, while p tracks a part of the
                   2055:             struct vot.  */
                   2056:          const char *howp;
                   2057:          /* The lines below know about 2-byte opcodes starting FD,FE or FF.
                   2058:             They also understand synthetic opcodes. Note:
                   2059:             we return 32 bits of opcode, including bucky bits, BUT
                   2060:             an opcode length is either 8 or 16 bits for vit_opcode_nbytes.  */
                   2061:          oc = vwP->vot_code;   /* The op-code.  */
                   2062:          vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
                   2063:          md_number_to_chars (vitP->vit_opcode, oc, 4);
                   2064:          count = 0;            /* No operands seen yet.  */
                   2065:          instring = p;         /* Point just past operation code.  */
                   2066:          alloperr = "";
                   2067:          for (howp = vwP->vot_how, operandp = vitP->vit_operand;
                   2068:               !(alloperr && *alloperr) && *howp;
                   2069:               operandp++, howp += 2)
                   2070:            {
                   2071:              /* Here to parse one operand. Leave instring pointing just
                   2072:                 past any one ',' that marks the end of this operand.  */
                   2073:              if (!howp[1])
                   2074:                as_fatal (_("odd number of bytes in operand description"));
                   2075:              else if (*instring)
                   2076:                {
                   2077:                  for (q = instring; (c = *q) && c != ','; q++)
                   2078:                    ;
                   2079:                  /* Q points to ',' or '\0' that ends argument. C is that
                   2080:                     character.  */
                   2081:                  *q = 0;
                   2082:                  operandp->vop_width = howp[1];
                   2083:                  operandp->vop_nbytes = vax_operand_width_size[(unsigned) howp[1]];
                   2084:                  operandp->vop_access = howp[0];
                   2085:                  vip_op (instring, operandp);
                   2086:                  *q = c;       /* Restore input text.  */
                   2087:                  if (operandp->vop_error)
                   2088:                    alloperr = _("Bad operand");
                   2089:                  instring = q + (c ? 1 : 0);   /* Next operand (if any).  */
                   2090:                  count++;      /*  Won another argument, may have an operr.  */
                   2091:                }
                   2092:              else
                   2093:                alloperr = _("Not enough operands");
                   2094:            }
                   2095:          if (!*alloperr)
                   2096:            {
                   2097:              if (*instring == ' ')
                   2098:                instring++;
                   2099:              if (*instring)
                   2100:                alloperr = _("Too many operands");
                   2101:            }
                   2102:          vitP->vit_error = alloperr;
                   2103:        }
                   2104:     }
                   2105:   vitP->vit_operands = count;
                   2106: }
                   2107: 
                   2108: #ifdef test
                   2109:
                   2110: /* Test program for above.  */
                   2111:
                   2112: struct vit myvit;              /* Build an exploded vax instruction here.  */
                   2113: char answer[100];              /* Human types a line of vax assembler here.  */
                   2114: char *mybug;                   /* "" or an internal logic diagnostic.  */
                   2115: int mycount;                   /* Number of operands.  */
                   2116: struct vop *myvop;             /* Scan operands from myvit.  */
                   2117: int mysynth;                   /* 1 means want synthetic opcodes.  */
                   2118: char my_immediate[200];
                   2119: char my_indirect[200];
                   2120: char my_displen[200];
                   2121:
                   2122: int
                   2123: main (void)
                   2124: {
                   2125:   char *p;
                   2126:
                   2127:   printf ("0 means no synthetic instructions.   ");
                   2128:   printf ("Value for vip_begin?  ");
                   2129:   gets (answer);
                   2130:   sscanf (answer, "%d", &mysynth);
                   2131:   printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not");
                   2132:   printf ("enter immediate symbols eg enter #   ");
                   2133:   gets (my_immediate);
                   2134:   printf ("enter indirect symbols  eg enter @   ");
                   2135:   gets (my_indirect);
                   2136:   printf ("enter displen symbols   eg enter ^   ");
                   2137:   gets (my_displen);
                   2138:
                   2139:   if (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))
                   2140:     error ("vip_begin=%s", p);
                   2141:
                   2142:   printf ("An empty input line will quit you from the vax instruction parser\n");
                   2143:   for (;;)
                   2144:     {
                   2145:       printf ("vax instruction: ");
                   2146:       fflush (stdout);
                   2147:       gets (answer);
                   2148:       if (!*answer)
                   2149:        break;          /* Out of for each input text loop.  */
                   2150:
                   2151:       vip (& myvit, answer);
                   2152:       if (*myvit.vit_error)
                   2153:        printf ("ERR:\"%s\"\n", myvit.vit_error);
                   2154:
                   2155:       printf ("opcode=");
                   2156:       for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode;
                   2157:           mycount;
                   2158:           mycount--, p++)
                   2159:        printf ("%02x ", *p & 0xFF);
                   2160:
                   2161:       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
                   2162:       for (myvop = myvit.vit_operand; mycount; mycount--, myvop++)
                   2163:        {
                   2164:          printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
                   2165:                  myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
                   2166:                  myvop->vop_short, myvop->vop_access, myvop->vop_width,
                   2167:                  myvop->vop_nbytes);
                   2168:          for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++)
                   2169:            putchar (*p);
                   2170:
                   2171:          printf ("\"\n");
                   2172:          if (myvop->vop_error)
                   2173:            printf ("  err:\"%s\"\n", myvop->vop_error);
                   2174:
                   2175:          if (myvop->vop_warn)
                   2176:            printf ("  wrn:\"%s\"\n", myvop->vop_warn);
                   2177:        }
                   2178:     }
                   2179:   vip_end ();
                   2180:   exit (EXIT_SUCCESS);
                   2181: }
                   2182:
                   2183: #endif
                   2184: 
                   2185: #ifdef TEST                    /* #Define to use this testbed.  */
                   2186:
                   2187: /* Follows a test program for this function.
                   2188:    We declare arrays non-local in case some of our tiny-minded machines
                   2189:    default to small stacks. Also, helps with some debuggers.  */
                   2190:
                   2191: char answer[100];              /* Human types into here.  */
                   2192: char *p;                       /*  */
                   2193: char *myerr;
                   2194: char *mywrn;
                   2195: char *mybug;
                   2196: char myaccess;
                   2197: char mywidth;
                   2198: char mymode;
                   2199: char myreg;
                   2200: char mylen;
                   2201: char *myleft;
                   2202: char *myright;
                   2203: char myndx;
                   2204: int my_operand_length;
                   2205: char my_immediate[200];
                   2206: char my_indirect[200];
                   2207: char my_displen[200];
                   2208:
                   2209: int
                   2210: main (void)
                   2211: {
                   2212:   printf ("enter immediate symbols eg enter #   ");
                   2213:   gets (my_immediate);
                   2214:   printf ("enter indirect symbols  eg enter @   ");
                   2215:   gets (my_indirect);
                   2216:   printf ("enter displen symbols   eg enter ^   ");
                   2217:   gets (my_displen);
                   2218:   vip_op_defaults (my_immediate, my_indirect, my_displen);
                   2219:
                   2220:   for (;;)
                   2221:     {
                   2222:       printf ("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
                   2223:       fflush (stdout);
                   2224:       gets (answer);
                   2225:       if (!answer[0])
                   2226:        exit (EXIT_SUCCESS);
                   2227:       myaccess = answer[0];
                   2228:       mywidth = answer[1];
                   2229:       switch (mywidth)
                   2230:        {
                   2231:        case 'b':
                   2232:          my_operand_length = 1;
                   2233:          break;
                   2234:        case 'd':
                   2235:          my_operand_length = 8;
                   2236:          break;
                   2237:        case 'f':
                   2238:          my_operand_length = 4;
                   2239:          break;
                   2240:        case 'g':
                   2241:          my_operand_length = 16;
                   2242:          break;
                   2243:        case 'h':
                   2244:          my_operand_length = 32;
                   2245:          break;
                   2246:        case 'l':
                   2247:          my_operand_length = 4;
                   2248:          break;
                   2249:        case 'o':
                   2250:          my_operand_length = 16;
                   2251:          break;
                   2252:        case 'q':
                   2253:          my_operand_length = 8;
                   2254:          break;
                   2255:        case 'w':
                   2256:          my_operand_length = 2;
                   2257:          break;
                   2258:        case '!':
                   2259:        case '?':
                   2260:        case '-':
                   2261:          my_operand_length = 0;
                   2262:          break;
                   2263:
                   2264:        default:
                   2265:          my_operand_length = 2;
                   2266:          printf ("I dn't understand access width %c\n", mywidth);
                   2267:          break;
                   2268:        }
                   2269:       printf ("VAX assembler instruction operand: ");
                   2270:       fflush (stdout);
                   2271:       gets (answer);
                   2272:       mybug = vip_op (answer, myaccess, mywidth, my_operand_length,
                   2273:                      &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
                   2274:                      &myerr, &mywrn);
                   2275:       if (*myerr)
                   2276:        {
                   2277:          printf ("error: \"%s\"\n", myerr);
                   2278:          if (*mybug)
                   2279:            printf (" bug: \"%s\"\n", mybug);
                   2280:        }
                   2281:       else
                   2282:        {
                   2283:          if (*mywrn)
                   2284:            printf ("warning: \"%s\"\n", mywrn);
                   2285:          mumble ("mode", mymode);
                   2286:          mumble ("register", myreg);
                   2287:          mumble ("index", myndx);
                   2288:          printf ("width:'%c'  ", mylen);
                   2289:          printf ("expression: \"");
                   2290:          while (myleft <= myright)
                   2291:            putchar (*myleft++);
                   2292:          printf ("\"\n");
                   2293:        }
                   2294:     }
                   2295: }
                   2296:
                   2297: void
                   2298: mumble (char *text, int value)
                   2299: {
                   2300:   printf ("%s:", text);
                   2301:   if (value >= 0)
                   2302:     printf ("%xx", value);
                   2303:   else
                   2304:     printf ("ABSENT");
                   2305:   printf ("  ");
                   2306: }
                   2307:
                   2308: #endif
                   2309:
                   2310: int md_short_jump_size = 3;
                   2311: int md_long_jump_size = 6;
                   2312:
                   2313: void
                   2314: md_create_short_jump (char *ptr,
                   2315:                      addressT from_addr,
                   2316:                      addressT to_addr ATTRIBUTE_UNUSED,
                   2317:                      fragS *frag ATTRIBUTE_UNUSED,
                   2318:                      symbolS *to_symbol ATTRIBUTE_UNUSED)
                   2319: {
                   2320:   valueT offset;
                   2321:
                   2322:   /* This former calculation was off by two:
                   2323:       offset = to_addr - (from_addr + 1);
                   2324:      We need to account for the one byte instruction and also its
                   2325:      two byte operand.  */
                   2326:   offset = to_addr - (from_addr + 1 + 2);
                   2327:   *ptr++ = VAX_BRW;            /* Branch with word (16 bit) offset.  */
                   2328:   md_number_to_chars (ptr, offset, 2);
                   2329: }
                   2330:
                   2331: void
                   2332: md_create_long_jump (char *ptr,
                   2333:                     addressT from_addr ATTRIBUTE_UNUSED,
                   2334:                     addressT to_addr,
                   2335:                     fragS *frag,
                   2336:                     symbolS *to_symbol)
                   2337: {
                   2338:   valueT offset;
                   2339:
                   2340:   offset = to_addr - S_GET_VALUE (to_symbol);
                   2341:   *ptr++ = VAX_JMP;            /* Arbitrary jump.  */
                   2342:   *ptr++ = VAX_ABSOLUTE_MODE;
                   2343:   md_number_to_chars (ptr, offset, 4);
                   2344:   fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (long) 0, 0, NO_RELOC);
                   2345: }
                   2346: 
                   2347: #ifdef OBJ_VMS
                   2348: const char *md_shortopts = "d:STt:V+1h:Hv::";
                   2349: #elif defined(OBJ_ELF)
                   2350: const char *md_shortopts = "d:STt:VkKQ:";
                   2351: #else
                   2352: const char *md_shortopts = "d:STt:V";
                   2353: #endif
                   2354: struct option md_longopts[] =
                   2355: {
                   2356: #ifdef OBJ_ELF
                   2357: #define OPTION_PIC (OPTION_MD_BASE)
                   2358:   { "pic", no_argument, NULL, OPTION_PIC },
                   2359: #endif
                   2360:   { NULL, no_argument, NULL, 0 }
                   2361: };
                   2362: size_t md_longopts_size = sizeof (md_longopts);
                   2363:
                   2364: int
1.5     ! christos 2365: md_parse_option (int c, const char *arg)
1.1       christos 2366: {
                   2367:   switch (c)
                   2368:     {
                   2369:     case 'S':
                   2370:       as_warn (_("SYMBOL TABLE not implemented"));
                   2371:       break;
                   2372:
                   2373:     case 'T':
                   2374:       as_warn (_("TOKEN TRACE not implemented"));
                   2375:       break;
                   2376:
                   2377:     case 'd':
                   2378:       as_warn (_("Displacement length %s ignored!"), arg);
                   2379:       break;
                   2380:
                   2381:     case 't':
                   2382:       as_warn (_("I don't need or use temp. file \"%s\"."), arg);
                   2383:       break;
                   2384:
                   2385:     case 'V':
                   2386:       as_warn (_("I don't use an interpass file! -V ignored"));
                   2387:       break;
                   2388:
                   2389: #ifdef OBJ_VMS
                   2390:     case '+':                  /* For g++.  Hash any name > 31 chars long.  */
                   2391:       flag_hash_long_names = 1;
                   2392:       break;
                   2393:
                   2394:     case '1':                  /* For backward compatibility.  */
                   2395:       flag_one = 1;
                   2396:       break;
                   2397:
                   2398:     case 'H':                  /* Show new symbol after hash truncation.  */
                   2399:       flag_show_after_trunc = 1;
                   2400:       break;
                   2401:
                   2402:     case 'h':                  /* No hashing of mixed-case names.  */
                   2403:       {
                   2404:        extern char vms_name_mapping;
                   2405:        vms_name_mapping = atoi (arg);
                   2406:        flag_no_hash_mixed_case = 1;
                   2407:       }
                   2408:       break;
                   2409:
                   2410:     case 'v':
                   2411:       {
                   2412:        extern char *compiler_version_string;
                   2413:
                   2414:        if (!arg || !*arg || access (arg, 0) == 0)
                   2415:          return 0;             /* Have caller show the assembler version.  */
                   2416:        compiler_version_string = arg;
                   2417:       }
                   2418:       break;
                   2419: #endif
                   2420:
                   2421: #ifdef OBJ_ELF
                   2422:     case OPTION_PIC:
                   2423:     case 'k':
                   2424:       flag_want_pic = 1;
                   2425:       break;                   /* -pic, Position Independent Code.  */
                   2426:
                   2427:      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment
                   2428:        section should be emitted or not.  FIXME: Not implemented.  */
                   2429:     case 'Q':
                   2430:       break;
                   2431: #endif
                   2432:
                   2433:     default:
                   2434:       return 0;
                   2435:     }
                   2436:
                   2437:   return 1;
                   2438: }
                   2439:
                   2440: void
                   2441: md_show_usage (FILE *stream)
                   2442: {
                   2443:   fprintf (stream, _("\
                   2444: VAX options:\n\
                   2445: -d LENGTH              ignored\n\
                   2446: -J                     ignored\n\
                   2447: -S                     ignored\n\
                   2448: -t FILE                        ignored\n\
                   2449: -T                     ignored\n\
                   2450: -V                     ignored\n"));
                   2451: #ifdef OBJ_VMS
                   2452:   fprintf (stream, _("\
                   2453: VMS options:\n\
                   2454: -+                     hash encode names longer than 31 characters\n\
                   2455: -1                     `const' handling compatible with gcc 1.x\n\
                   2456: -H                     show new symbol after hash truncation\n\
                   2457: -h NUM                 don't hash mixed-case names, and adjust case:\n\
                   2458:                        0 = upper, 2 = lower, 3 = preserve case\n\
                   2459: -v\"VERSION\"          code being assembled was produced by compiler \"VERSION\"\n"));
                   2460: #endif
                   2461: }
                   2462: 
                   2463: /* We have no need to default values of symbols.  */
                   2464:
                   2465: symbolS *
                   2466: md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
                   2467: {
                   2468:   return NULL;
                   2469: }
                   2470:
                   2471: /* Round up a section size to the appropriate boundary.  */
                   2472: valueT
                   2473: md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
                   2474: {
                   2475:   /* Byte alignment is fine */
                   2476:   return size;
                   2477: }
                   2478:
                   2479: /* Exactly what point is a PC-relative offset relative TO?
                   2480:    On the vax, they're relative to the address of the offset, plus
                   2481:    its size. */
                   2482: long
                   2483: md_pcrel_from (fixS *fixP)
                   2484: {
                   2485:   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
                   2486: }
                   2487:
                   2488: arelent *
                   2489: tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
                   2490: {
                   2491:   arelent *reloc;
                   2492:   bfd_reloc_code_real_type code;
                   2493:
                   2494:   if (fixp->fx_tcbit)
                   2495:     abort ();
                   2496:
1.3       christos 2497:   if (fixp->fx_r_type != NO_RELOC)
1.1       christos 2498:     {
                   2499:       code = fixp->fx_r_type;
                   2500:
                   2501:       if (fixp->fx_pcrel)
                   2502:        {
                   2503:          switch (code)
                   2504:            {
                   2505:            case BFD_RELOC_8_PCREL:
                   2506:            case BFD_RELOC_16_PCREL:
                   2507:            case BFD_RELOC_32_PCREL:
                   2508: #ifdef OBJ_ELF
                   2509:            case BFD_RELOC_8_GOT_PCREL:
                   2510:            case BFD_RELOC_16_GOT_PCREL:
                   2511:            case BFD_RELOC_32_GOT_PCREL:
                   2512:            case BFD_RELOC_8_PLT_PCREL:
                   2513:            case BFD_RELOC_16_PLT_PCREL:
                   2514:            case BFD_RELOC_32_PLT_PCREL:
                   2515: #endif
                   2516:              break;
                   2517:            default:
                   2518:              as_bad_where (fixp->fx_file, fixp->fx_line,
                   2519:                            _("Cannot make %s relocation PC relative"),
                   2520:                            bfd_get_reloc_code_name (code));
                   2521:            }
                   2522:        }
                   2523:     }
                   2524:   else
                   2525:     {
                   2526: #define F(SZ,PCREL)            (((SZ) << 1) + (PCREL))
                   2527:       switch (F (fixp->fx_size, fixp->fx_pcrel))
                   2528:        {
                   2529: #define MAP(SZ,PCREL,TYPE)     case F(SZ,PCREL): code = (TYPE); break
                   2530:          MAP (1, 0, BFD_RELOC_8);
                   2531:          MAP (2, 0, BFD_RELOC_16);
                   2532:          MAP (4, 0, BFD_RELOC_32);
                   2533:          MAP (1, 1, BFD_RELOC_8_PCREL);
                   2534:          MAP (2, 1, BFD_RELOC_16_PCREL);
                   2535:          MAP (4, 1, BFD_RELOC_32_PCREL);
                   2536:        default:
                   2537:          abort ();
                   2538:        }
                   2539:     }
                   2540: #undef F
                   2541: #undef MAP
                   2542:
1.5     ! christos 2543:   reloc = XNEW (arelent);
        !          2544:   reloc->sym_ptr_ptr = XNEW (asymbol *);
1.1       christos 2545:   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
                   2546:   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
                   2547: #ifndef OBJ_ELF
                   2548:   if (fixp->fx_pcrel)
                   2549:     reloc->addend = fixp->fx_addnumber;
                   2550:   else
                   2551:     reloc->addend = 0;
                   2552: #else
                   2553:   reloc->addend = fixp->fx_offset;
                   2554: #endif
                   2555:
                   2556:   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
                   2557:   gas_assert (reloc->howto != 0);
                   2558:
                   2559:   return reloc;
                   2560: }
                   2561:
                   2562: /* vax:md_assemble() emit frags for 1 instruction given in textual form.  */
                   2563: void
                   2564: md_assemble (char *instruction_string)
                   2565: {
                   2566:   /* Non-zero if operand expression's segment is not known yet.  */
                   2567:   int is_undefined;
                   2568:   /* Non-zero if operand expression's segment is absolute.  */
                   2569:   int is_absolute;
                   2570:   int length_code;
                   2571:   char *p;
                   2572:   /* An operand. Scans all operands.  */
                   2573:   struct vop *operandP;
                   2574:   char *save_input_line_pointer;
                   2575:                        /* What used to live after an expression.  */
                   2576:   char c_save;
                   2577:   /* 1: instruction_string bad for all passes.  */
                   2578:   int goofed;
                   2579:   /* Points to slot just after last operand.  */
                   2580:   struct vop *end_operandP;
                   2581:   /* Points to expression values for this operand.  */
                   2582:   expressionS *expP;
                   2583:   segT *segP;
                   2584:
                   2585:   /* These refer to an instruction operand expression.  */
                   2586:   /* Target segment of the address.     */
                   2587:   segT to_seg;
                   2588:   valueT this_add_number;
                   2589:   /* Positive (minuend) symbol.  */
                   2590:   symbolS *this_add_symbol;
                   2591:   /* As a number.  */
                   2592:   long opcode_as_number;
                   2593:   /* Least significant byte 1st.  */
                   2594:   char *opcode_as_chars;
                   2595:   /* As an array of characters.  */
                   2596:   /* Least significant byte 1st */
                   2597:   char *opcode_low_byteP;
                   2598:   /* length (bytes) meant by vop_short.  */
                   2599:   int length;
                   2600:   /* 0, or 1 if '@' is in addressing mode.  */
                   2601:   int at;
                   2602:   /* From vop_nbytes: vax_operand_width (in bytes) */
                   2603:   int nbytes;
                   2604:   FLONUM_TYPE *floatP;
                   2605:   LITTLENUM_TYPE literal_float[8];
                   2606:   /* Big enough for any floating point literal.  */
                   2607:
                   2608:   vip (&v, instruction_string);
                   2609:
                   2610:   /* Now we try to find as many as_warn()s as we can. If we do any as_warn()s
                   2611:      then goofed=1. Notice that we don't make any frags yet.
                   2612:      Should goofed be 1, then this instruction will wedge in any pass,
                   2613:      and we can safely flush it, without causing interpass symbol phase
                   2614:      errors. That is, without changing label values in different passes.  */
                   2615:   if ((goofed = (*v.vit_error)) != 0)
                   2616:     {
                   2617:       as_fatal (_("Ignoring statement due to \"%s\""), v.vit_error);
                   2618:     }
                   2619:   /* We need to use expression() and friends, which require us to diddle
                   2620:      input_line_pointer. So we save it and restore it later.  */
                   2621:   save_input_line_pointer = input_line_pointer;
                   2622:   for (operandP = v.vit_operand,
                   2623:        expP = exp_of_operand,
                   2624:        segP = seg_of_operand,
                   2625:        floatP = float_operand,
                   2626:        end_operandP = v.vit_operand + v.vit_operands;
                   2627:
                   2628:        operandP < end_operandP;
                   2629:
                   2630:        operandP++, expP++, segP++, floatP++)
                   2631:     {
                   2632:       if (operandP->vop_error)
                   2633:        {
                   2634:          as_fatal (_("Aborting because statement has \"%s\""), operandP->vop_error);
                   2635:          goofed = 1;
                   2636:        }
                   2637:       else
                   2638:        {
                   2639:          /* Statement has no syntax goofs: let's sniff the expression.  */
                   2640:          int can_be_short = 0; /* 1 if a bignum can be reduced to a short literal.  */
                   2641:
                   2642:          input_line_pointer = operandP->vop_expr_begin;
                   2643:          c_save = operandP->vop_expr_end[1];
                   2644:          operandP->vop_expr_end[1] = '\0';
                   2645:          /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = 1.  */
                   2646:          *segP = expression (expP);
                   2647:          switch (expP->X_op)
                   2648:            {
                   2649:            case O_absent:
                   2650:              /* for BSD4.2 compatibility, missing expression is absolute 0 */
                   2651:              expP->X_op = O_constant;
                   2652:              expP->X_add_number = 0;
                   2653:              /* For SEG_ABSOLUTE, we shouldn't need to set X_op_symbol,
                   2654:                 X_add_symbol to any particular value.  But, we will program
                   2655:                 defensively. Since this situation occurs rarely so it costs
                   2656:                 us little to do, and stops Dean worrying about the origin of
                   2657:                 random bits in expressionS's.  */
                   2658:              expP->X_add_symbol = NULL;
                   2659:              expP->X_op_symbol = NULL;
                   2660:              break;
                   2661:
                   2662:            case O_symbol:
                   2663:            case O_constant:
                   2664:              break;
                   2665:
                   2666:            default:
                   2667:              /* Major bug. We can't handle the case of a
                   2668:                 SEG_OP expression in a VIT_OPCODE_SYNTHETIC
                   2669:                 variable-length instruction.
                   2670:                 We don't have a frag type that is smart enough to
                   2671:                 relax a SEG_OP, and so we just force all
                   2672:                 SEG_OPs to behave like SEG_PASS1s.
                   2673:                 Clearly, if there is a demand we can invent a new or
                   2674:                 modified frag type and then coding up a frag for this
                   2675:                 case will be easy. SEG_OP was invented for the
                   2676:                 .words after a CASE opcode, and was never intended for
                   2677:                 instruction operands.  */
                   2678:              need_pass_2 = 1;
                   2679:              as_fatal (_("Can't relocate expression"));
                   2680:              break;
                   2681:
                   2682:            case O_big:
                   2683:              /* Preserve the bits.  */
                   2684:              if (expP->X_add_number > 0)
                   2685:                {
                   2686:                  bignum_copy (generic_bignum, expP->X_add_number,
                   2687:                               floatP->low, SIZE_OF_LARGE_NUMBER);
                   2688:                }
                   2689:              else
                   2690:                {
                   2691:                  know (expP->X_add_number < 0);
                   2692:                  flonum_copy (&generic_floating_point_number,
                   2693:                               floatP);
                   2694:                  if (strchr ("s i", operandP->vop_short))
                   2695:                    {
                   2696:                      /* Could possibly become S^# */
                   2697:                      flonum_gen2vax (-expP->X_add_number, floatP, literal_float);
                   2698:                      switch (-expP->X_add_number)
                   2699:                        {
                   2700:                        case 'f':
                   2701:                          can_be_short =
                   2702:                            (literal_float[0] & 0xFC0F) == 0x4000
                   2703:                            && literal_float[1] == 0;
                   2704:                          break;
                   2705:
                   2706:                        case 'd':
                   2707:                          can_be_short =
                   2708:                            (literal_float[0] & 0xFC0F) == 0x4000
                   2709:                            && literal_float[1] == 0
                   2710:                            && literal_float[2] == 0
                   2711:                            && literal_float[3] == 0;
                   2712:                          break;
                   2713:
                   2714:                        case 'g':
                   2715:                          can_be_short =
                   2716:                            (literal_float[0] & 0xFF81) == 0x4000
                   2717:                            && literal_float[1] == 0
                   2718:                            && literal_float[2] == 0
                   2719:                            && literal_float[3] == 0;
                   2720:                          break;
                   2721:
                   2722:                        case 'h':
                   2723:                          can_be_short = ((literal_float[0] & 0xFFF8) == 0x4000
                   2724:                                          && (literal_float[1] & 0xE000) == 0
                   2725:                                          && literal_float[2] == 0
                   2726:                                          && literal_float[3] == 0
                   2727:                                          && literal_float[4] == 0
                   2728:                                          && literal_float[5] == 0
                   2729:                                          && literal_float[6] == 0
                   2730:                                          && literal_float[7] == 0);
                   2731:                          break;
                   2732:
                   2733:                        default:
                   2734:                          BAD_CASE (-expP->X_add_number);
                   2735:                          break;
                   2736:                        }
                   2737:                    }
                   2738:                }
                   2739:
                   2740:              if (operandP->vop_short == 's'
                   2741:                  || operandP->vop_short == 'i'
                   2742:                  || (operandP->vop_short == ' '
                   2743:                      && operandP->vop_reg == 0xF
                   2744:                      && (operandP->vop_mode & 0xE) == 0x8))
                   2745:                {
                   2746:                  /* Saw a '#'.  */
                   2747:                  if (operandP->vop_short == ' ')
                   2748:                    {
                   2749:                      /* We must chose S^ or I^.  */
                   2750:                      if (expP->X_add_number > 0)
                   2751:                        {
                   2752:                          /* Bignum: Short literal impossible.  */
                   2753:                          operandP->vop_short = 'i';
                   2754:                          operandP->vop_mode = 8;
                   2755:                          operandP->vop_reg = 0xF;      /* VAX PC.  */
                   2756:                        }
                   2757:                      else
                   2758:                        {
                   2759:                          /* Flonum: Try to do it.  */
                   2760:                          if (can_be_short)
                   2761:                            {
                   2762:                              operandP->vop_short = 's';
                   2763:                              operandP->vop_mode = 0;
                   2764:                              operandP->vop_ndx = -1;
                   2765:                              operandP->vop_reg = -1;
                   2766:                              expP->X_op = O_constant;
                   2767:                            }
                   2768:                          else
                   2769:                            {
                   2770:                              operandP->vop_short = 'i';
                   2771:                              operandP->vop_mode = 8;
                   2772:                              operandP->vop_reg = 0xF;  /* VAX PC */
                   2773:                            }
                   2774:                        }       /* bignum or flonum ? */
                   2775:                    }           /*  if #, but no S^ or I^ seen.  */
                   2776:                  /* No more ' ' case: either 's' or 'i'.  */
                   2777:                  if (operandP->vop_short == 's')
                   2778:                    {
                   2779:                      /* Wants to be a short literal.  */
                   2780:                      if (expP->X_add_number > 0)
                   2781:                        {
                   2782:                          as_warn (_("Bignum not permitted in short literal. Immediate mode assumed."));
                   2783:                          operandP->vop_short = 'i';
                   2784:                          operandP->vop_mode = 8;
                   2785:                          operandP->vop_reg = 0xF;      /* VAX PC.  */
                   2786:                        }
                   2787:                      else
                   2788:                        {
                   2789:                          if (!can_be_short)
                   2790:                            {
                   2791:                              as_warn (_("Can't do flonum short literal: immediate mode used."));
                   2792:                              operandP->vop_short = 'i';
                   2793:                              operandP->vop_mode = 8;
                   2794:                              operandP->vop_reg = 0xF;  /* VAX PC.  */
                   2795:                            }
                   2796:                          else
                   2797:                            {
                   2798:                              /* Encode short literal now.  */
                   2799:                              int temp = 0;
                   2800:
                   2801:                              switch (-expP->X_add_number)
                   2802:                                {
                   2803:                                case 'f':
                   2804:                                case 'd':
                   2805:                                  temp = literal_float[0] >> 4;
                   2806:                                  break;
                   2807:
                   2808:                                case 'g':
                   2809:                                  temp = literal_float[0] >> 1;
                   2810:                                  break;
                   2811:
                   2812:                                case 'h':
                   2813:                                  temp = ((literal_float[0] << 3) & 070)
                   2814:                                    | ((literal_float[1] >> 13) & 07);
                   2815:                                  break;
                   2816:
                   2817:                                default:
                   2818:                                  BAD_CASE (-expP->X_add_number);
                   2819:                                  break;
                   2820:                                }
                   2821:
                   2822:                              floatP->low[0] = temp & 077;
                   2823:                              floatP->low[1] = 0;
                   2824:                            }
                   2825:                        }
                   2826:                    }
                   2827:                  else
                   2828:                    {
                   2829:                      /* I^# seen: set it up if float.  */
                   2830:                      if (expP->X_add_number < 0)
                   2831:                        {
                   2832:                          memcpy (floatP->low, literal_float, sizeof (literal_float));
                   2833:                        }
                   2834:                    }           /* if S^# seen.  */
                   2835:                }
                   2836:              else
                   2837:                {
                   2838:                  as_warn (_("A bignum/flonum may not be a displacement: 0x%lx used"),
                   2839:                           (expP->X_add_number = 0x80000000L));
                   2840:                  /* Chosen so luser gets the most offset bits to patch later.  */
                   2841:                }
                   2842:              expP->X_add_number = floatP->low[0]
                   2843:                | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS);
                   2844:
                   2845:              /* For the O_big case we have:
                   2846:                 If vop_short == 's' then a short floating literal is in the
                   2847:                        lowest 6 bits of floatP -> low [0], which is
                   2848:                        big_operand_bits [---] [0].
                   2849:                 If vop_short == 'i' then the appropriate number of elements
                   2850:                        of big_operand_bits [---] [...] are set up with the correct
                   2851:                        bits.
                   2852:                 Also, just in case width is byte word or long, we copy the lowest
                   2853:                 32 bits of the number to X_add_number.  */
                   2854:              break;
                   2855:            }
                   2856:          if (input_line_pointer != operandP->vop_expr_end + 1)
                   2857:            {
                   2858:              as_fatal ("Junk at end of expression \"%s\"", input_line_pointer);
                   2859:              goofed = 1;
                   2860:            }
                   2861:          operandP->vop_expr_end[1] = c_save;
                   2862:        }
                   2863:     }
                   2864:
                   2865:   input_line_pointer = save_input_line_pointer;
                   2866:
                   2867:   if (need_pass_2 || goofed)
                   2868:     return;
                   2869:
                   2870:   dwarf2_emit_insn (0);
                   2871:   /* Emit op-code.  */
                   2872:   /* Remember where it is, in case we want to modify the op-code later.  */
                   2873:   opcode_low_byteP = frag_more (v.vit_opcode_nbytes);
                   2874:   memcpy (opcode_low_byteP, v.vit_opcode, v.vit_opcode_nbytes);
                   2875:   opcode_as_chars = v.vit_opcode;
                   2876:   opcode_as_number = md_chars_to_number ((unsigned char *) opcode_as_chars, 4);
                   2877:   for (operandP = v.vit_operand,
                   2878:        expP = exp_of_operand,
                   2879:        segP = seg_of_operand,
                   2880:        floatP = float_operand,
                   2881:        end_operandP = v.vit_operand + v.vit_operands;
                   2882:
                   2883:        operandP < end_operandP;
                   2884:
                   2885:        operandP++,
                   2886:        floatP++,
                   2887:        segP++,
                   2888:        expP++)
                   2889:     {
                   2890:       if (operandP->vop_ndx >= 0)
                   2891:        {
                   2892:          /* Indexed addressing byte.  */
                   2893:          /* Legality of indexed mode already checked: it is OK.  */
                   2894:          FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx);
                   2895:        }                       /* if(vop_ndx>=0) */
                   2896:
                   2897:       /* Here to make main operand frag(s).  */
                   2898:       this_add_number = expP->X_add_number;
                   2899:       this_add_symbol = expP->X_add_symbol;
                   2900:       to_seg = *segP;
                   2901:       is_undefined = (to_seg == undefined_section);
                   2902:       is_absolute = (to_seg == absolute_section);
                   2903:       at = operandP->vop_mode & 1;
                   2904:       length = (operandP->vop_short == 'b'
                   2905:                ? 1 : (operandP->vop_short == 'w'
                   2906:                       ? 2 : (operandP->vop_short == 'l'
                   2907:                              ? 4 : 0)));
                   2908:       nbytes = operandP->vop_nbytes;
                   2909:       if (operandP->vop_access == 'b')
                   2910:        {
                   2911:          if (to_seg == now_seg || is_undefined)
                   2912:            {
                   2913:              /* If is_undefined, then it might BECOME now_seg.  */
                   2914:              if (nbytes)
                   2915:                {
                   2916:                  p = frag_more (nbytes);
                   2917:                  fix_new (frag_now, p - frag_now->fr_literal, nbytes,
                   2918:                           this_add_symbol, this_add_number, 1, NO_RELOC);
                   2919:                }
                   2920:              else
                   2921:                {
                   2922:                  /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
                   2923:                  /* nbytes==0 */
                   2924:                  length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
                   2925:                  if (opcode_as_number & VIT_OPCODE_SPECIAL)
                   2926:                    {
                   2927:                      if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
                   2928:                        {
                   2929:                          /* br or jsb */
                   2930:                          frag_var (rs_machine_dependent, 5, 1,
                   2931:                            ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code),
                   2932:                                    this_add_symbol, this_add_number,
                   2933:                                    opcode_low_byteP);
                   2934:                        }
                   2935:                      else
                   2936:                        {
                   2937:                          if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
                   2938:                            {
                   2939:                              length_code = STATE_WORD;
                   2940:                              /* JF: There is no state_byte for this one! */
                   2941:                              frag_var (rs_machine_dependent, 10, 2,
                   2942:                                        ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code),
                   2943:                                        this_add_symbol, this_add_number,
                   2944:                                        opcode_low_byteP);
                   2945:                            }
                   2946:                          else
                   2947:                            {
                   2948:                              know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
                   2949:                              frag_var (rs_machine_dependent, 9, 1,
                   2950:                              ENCODE_RELAX (STATE_COMPLEX_HOP, length_code),
                   2951:                                        this_add_symbol, this_add_number,
                   2952:                                        opcode_low_byteP);
                   2953:                            }
                   2954:                        }
                   2955:                    }
                   2956:                  else
                   2957:                    {
                   2958:                      know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
                   2959:                      frag_var (rs_machine_dependent, 7, 1,
                   2960:                       ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
                   2961:                                this_add_symbol, this_add_number,
                   2962:                                opcode_low_byteP);
                   2963:                    }
                   2964:                }
                   2965:            }
                   2966:          else
                   2967:            {
                   2968:              /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
                   2969:              /* --- SEG FLOAT MAY APPEAR HERE ---  */
                   2970:              if (is_absolute)
                   2971:                {
                   2972:                  if (nbytes)
                   2973:                    {
                   2974:                      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
                   2975:                      p = frag_more (nbytes);
                   2976:                      /* Conventional relocation.  */
                   2977:                      fix_new (frag_now, p - frag_now->fr_literal, nbytes,
                   2978:                               section_symbol (absolute_section),
                   2979:                               this_add_number, 1, NO_RELOC);
                   2980:                    }
                   2981:                  else
                   2982:                    {
                   2983:                      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
                   2984:                      if (opcode_as_number & VIT_OPCODE_SPECIAL)
                   2985:                        {
                   2986:                          if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
                   2987:                            {
                   2988:                              /* br or jsb */
                   2989:                              *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
                   2990:                              know (opcode_as_chars[1] == 0);
                   2991:                              p = frag_more (5);
                   2992:                              p[0] = VAX_ABSOLUTE_MODE; /* @#...  */
                   2993:                              md_number_to_chars (p + 1, this_add_number, 4);
                   2994:                              /* Now (eg) JMP @#foo or JSB @#foo.  */
                   2995:                            }
                   2996:                          else
                   2997:                            {
                   2998:                              if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
                   2999:                                {
                   3000:                                  p = frag_more (10);
                   3001:                                  p[0] = 2;
                   3002:                                  p[1] = 0;
                   3003:                                  p[2] = VAX_BRB;
                   3004:                                  p[3] = 6;
                   3005:                                  p[4] = VAX_JMP;
                   3006:                                  p[5] = VAX_ABSOLUTE_MODE;     /* @#...  */
                   3007:                                  md_number_to_chars (p + 6, this_add_number, 4);
                   3008:                                  /* Now (eg)   ACBx    1f
                   3009:                                                BRB     2f
                   3010:                                        1:      JMP     @#foo
                   3011:                                        2:  */
                   3012:                                }
                   3013:                              else
                   3014:                                {
                   3015:                                  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
                   3016:                                  p = frag_more (9);
                   3017:                                  p[0] = 2;
                   3018:                                  p[1] = VAX_BRB;
                   3019:                                  p[2] = 6;
                   3020:                                  p[3] = VAX_JMP;
                   3021:                                   p[4] = VAX_ABSOLUTE_MODE;     /* @#...  */
                   3022:                                  md_number_to_chars (p + 5, this_add_number, 4);
                   3023:                                  /* Now (eg)   xOBxxx  1f
                   3024:                                                BRB     2f
                   3025:                                        1:      JMP     @#foo
                   3026:                                        2:  */
                   3027:                                }
                   3028:                            }
                   3029:                        }
                   3030:                      else
                   3031:                        {
                   3032:                          /* b<cond> */
                   3033:                          *opcode_low_byteP ^= 1;
                   3034:                          /* To reverse the condition in a VAX branch,
                   3035:                             complement the lowest order bit.  */
                   3036:                          p = frag_more (7);
                   3037:                          p[0] = 6;
                   3038:                          p[1] = VAX_JMP;
                   3039:                          p[2] = VAX_ABSOLUTE_MODE;     /* @#...  */
                   3040:                          md_number_to_chars (p + 3, this_add_number, 4);
                   3041:                          /* Now (eg)   BLEQ    1f
                   3042:                                        JMP     @#foo
                   3043:                                1:  */
                   3044:                        }
                   3045:                    }
                   3046:                }
                   3047:              else
                   3048:                {
                   3049:                  /* to_seg != now_seg && !is_undefinfed && !is_absolute */
                   3050:                  if (nbytes > 0)
                   3051:                    {
                   3052:                      /* Pc-relative. Conventional relocation.  */
                   3053:                      know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC));
                   3054:                      p = frag_more (nbytes);
                   3055:                      fix_new (frag_now, p - frag_now->fr_literal, nbytes,
                   3056:                               section_symbol (absolute_section),
                   3057:                               this_add_number, 1, NO_RELOC);
                   3058:                    }
                   3059:                  else
                   3060:                    {
                   3061:                      know (opcode_as_number & VIT_OPCODE_SYNTHETIC);
                   3062:                      if (opcode_as_number & VIT_OPCODE_SPECIAL)
                   3063:                        {
                   3064:                          if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP)
                   3065:                            {
                   3066:                              /* br or jsb */
                   3067:                              know (opcode_as_chars[1] == 0);
                   3068:                              *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
                   3069:                              p = frag_more (5);
                   3070:                              p[0] = VAX_PC_RELATIVE_MODE;
                   3071:                              fix_new (frag_now,
                   3072:                                       p + 1 - frag_now->fr_literal, 4,
                   3073:                                       this_add_symbol,
                   3074:                                       this_add_number, 1, NO_RELOC);
                   3075:                              /* Now eg JMP foo or JSB foo.  */
                   3076:                            }
                   3077:                          else
                   3078:                            {
                   3079:                              if (operandP->vop_width == VAX_WIDTH_WORD_JUMP)
                   3080:                                {
                   3081:                                  p = frag_more (10);
                   3082:                                  p[0] = 0;
                   3083:                                  p[1] = 2;
                   3084:                                  p[2] = VAX_BRB;
                   3085:                                  p[3] = 6;
                   3086:                                  p[4] = VAX_JMP;
                   3087:                                  p[5] = VAX_PC_RELATIVE_MODE;
                   3088:                                  fix_new (frag_now,
                   3089:                                           p + 6 - frag_now->fr_literal, 4,
                   3090:                                           this_add_symbol,
                   3091:                                           this_add_number, 1, NO_RELOC);
                   3092:                                  /* Now (eg)   ACBx    1f
                   3093:                                                BRB     2f
                   3094:                                        1:      JMP     foo
                   3095:                                        2:  */
                   3096:                                }
                   3097:                              else
                   3098:                                {
                   3099:                                  know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP);
                   3100:                                  p = frag_more (10);
                   3101:                                  p[0] = 2;
                   3102:                                  p[1] = VAX_BRB;
                   3103:                                  p[2] = 6;
                   3104:                                  p[3] = VAX_JMP;
                   3105:                                  p[4] = VAX_PC_RELATIVE_MODE;
                   3106:                                  fix_new (frag_now,
                   3107:                                           p + 5 - frag_now->fr_literal,
                   3108:                                           4, this_add_symbol,
                   3109:                                           this_add_number, 1, NO_RELOC);
                   3110:                                  /* Now (eg)   xOBxxx  1f
                   3111:                                                BRB     2f
                   3112:                                        1:      JMP     foo
                   3113:                                        2:  */
                   3114:                                }
                   3115:                            }
                   3116:                        }
                   3117:                      else
                   3118:                        {
                   3119:                          know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP);
                   3120:                          *opcode_low_byteP ^= 1;       /* Reverse branch condition.  */
                   3121:                          p = frag_more (7);
                   3122:                          p[0] = 6;
                   3123:                          p[1] = VAX_JMP;
                   3124:                          p[2] = VAX_PC_RELATIVE_MODE;
                   3125:                          fix_new (frag_now, p + 3 - frag_now->fr_literal,
                   3126:                                   4, this_add_symbol,
                   3127:                                   this_add_number, 1, NO_RELOC);
                   3128:                        }
                   3129:                    }
                   3130:                }
                   3131:            }
                   3132:        }
                   3133:       else
                   3134:        {
                   3135:          /* So it is ordinary operand.  */
                   3136:          know (operandP->vop_access != 'b');
                   3137:          /* ' ' target-independent: elsewhere.  */
                   3138:          know (operandP->vop_access != ' ');
                   3139:          know (operandP->vop_access == 'a'
                   3140:                || operandP->vop_access == 'm'
                   3141:                || operandP->vop_access == 'r'
                   3142:                || operandP->vop_access == 'v'
                   3143:                || operandP->vop_access == 'w');
                   3144:          if (operandP->vop_short == 's')
                   3145:            {
                   3146:              if (is_absolute)
                   3147:                {
                   3148:                  if (this_add_number >= 64)
                   3149:                    {
                   3150:                      as_warn (_("Short literal overflow(%ld.), immediate mode assumed."),
                   3151:                               (long) this_add_number);
                   3152:                      operandP->vop_short = 'i';
                   3153:                      operandP->vop_mode = 8;
                   3154:                      operandP->vop_reg = 0xF;
                   3155:                    }
                   3156:                }
                   3157:              else
                   3158:                {
                   3159:                  as_warn (_("Forced short literal to immediate mode. now_seg=%s to_seg=%s"),
                   3160:                           segment_name (now_seg), segment_name (to_seg));
                   3161:                  operandP->vop_short = 'i';
                   3162:                  operandP->vop_mode = 8;
                   3163:                  operandP->vop_reg = 0xF;
                   3164:                }
                   3165:            }
                   3166:          if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8
                   3167:                  || (operandP->vop_reg != 0xF && operandP->vop_mode < 10)))
                   3168:            {
                   3169:              /* One byte operand.  */
                   3170:              know (operandP->vop_mode > 3);
                   3171:              FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg);
                   3172:              /* All 1-bytes except S^# happen here.  */
                   3173:            }
                   3174:          else
                   3175:            {
                   3176:              /* {@}{q^}foo{(Rn)} or S^#foo */
                   3177:              if (operandP->vop_reg == -1 && operandP->vop_short != 's')
                   3178:                {
                   3179:                  /* "{@}{q^}foo" */
                   3180:                  if (to_seg == now_seg)
                   3181:                    {
                   3182:                      if (length == 0)
                   3183:                        {
                   3184:                          know (operandP->vop_short == ' ');
                   3185:                          length_code = STATE_BYTE;
                   3186: #ifdef OBJ_ELF
                   3187:                          if (S_IS_EXTERNAL (this_add_symbol)
                   3188:                              || S_IS_WEAK (this_add_symbol))
                   3189:                            length_code = STATE_UNDF;
                   3190: #endif
                   3191:                          p = frag_var (rs_machine_dependent, 10, 2,
                   3192:                               ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
                   3193:                                        this_add_symbol, this_add_number,
                   3194:                                        opcode_low_byteP);
                   3195:                          know (operandP->vop_mode == 10 + at);
                   3196:                          *p = at << 4;
                   3197:                          /* At is the only context we need to carry
                   3198:                             to other side of relax() process.  Must
                   3199:                             be in the correct bit position of VAX
                   3200:                             operand spec. byte.  */
                   3201:                        }
                   3202:                      else
                   3203:                        {
                   3204:                          know (length);
                   3205:                          know (operandP->vop_short != ' ');
                   3206:                          p = frag_more (length + 1);
                   3207:                          p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
                   3208:                          fix_new (frag_now, p + 1 - frag_now->fr_literal,
                   3209:                                   length, this_add_symbol,
                   3210:                                   this_add_number, 1, NO_RELOC);
                   3211:                        }
                   3212:                    }
                   3213:                  else
                   3214:                    {
                   3215:                      /* to_seg != now_seg */
                   3216:                      if (this_add_symbol == NULL)
                   3217:                        {
                   3218:                          know (is_absolute);
                   3219:                          /* Do @#foo: simpler relocation than foo-.(pc) anyway.  */
                   3220:                          p = frag_more (5);
                   3221:                          p[0] = VAX_ABSOLUTE_MODE;     /* @#...  */
                   3222:                          md_number_to_chars (p + 1, this_add_number, 4);
                   3223:                          if (length && length != 4)
                   3224:                            as_warn (_("Length specification ignored. Address mode 9F used"));
                   3225:                        }
                   3226:                      else
                   3227:                        {
                   3228:                          /* {@}{q^}other_seg */
                   3229:                          know ((length == 0 && operandP->vop_short == ' ')
                   3230:                             || (length > 0 && operandP->vop_short != ' '));
                   3231:                          if (is_undefined
                   3232: #ifdef OBJ_ELF
                   3233:                              || S_IS_WEAK(this_add_symbol)
                   3234:                              || S_IS_EXTERNAL(this_add_symbol)
                   3235: #endif
                   3236:                              )
                   3237:                            {
                   3238:                              switch (length)
                   3239:                                {
                   3240:                                default: length_code = STATE_UNDF; break;
                   3241:                                case 1: length_code = STATE_BYTE; break;
                   3242:                                case 2: length_code = STATE_WORD; break;
                   3243:                                case 4: length_code = STATE_LONG; break;
                   3244:                                }
                   3245:                              /* We have a SEG_UNKNOWN symbol. It might
                   3246:                                 turn out to be in the same segment as
                   3247:                                 the instruction, permitting relaxation.  */
                   3248:                              p = frag_var (rs_machine_dependent, 5, 2,
                   3249:                               ENCODE_RELAX (STATE_PC_RELATIVE, length_code),
                   3250:                                            this_add_symbol, this_add_number,
                   3251:                                            opcode_low_byteP);
                   3252:                              p[0] = at << 4;
                   3253:                            }
                   3254:                          else
                   3255:                            {
                   3256:                              if (length == 0)
                   3257:                                {
                   3258:                                  know (operandP->vop_short == ' ');
                   3259:                                  length = 4;   /* Longest possible.  */
                   3260:                                }
                   3261:                              p = frag_more (length + 1);
                   3262:                              p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
                   3263:                              md_number_to_chars (p + 1, this_add_number, length);
                   3264:                              fix_new (frag_now,
                   3265:                                       p + 1 - frag_now->fr_literal,
                   3266:                                       length, this_add_symbol,
                   3267:                                       this_add_number, 1, NO_RELOC);
                   3268:                            }
                   3269:                        }
                   3270:                    }
                   3271:                }
                   3272:              else
                   3273:                {
                   3274:                  /* {@}{q^}foo(Rn) or S^# or I^# or # */
                   3275:                  if (operandP->vop_mode < 0xA)
                   3276:                    {
                   3277:                      /* # or S^# or I^# */
                   3278:                      if (operandP->vop_access == 'v'
                   3279:                          || operandP->vop_access == 'a')
                   3280:                        {
                   3281:                          if (operandP->vop_access == 'v')
                   3282:                            as_warn (_("Invalid operand:  immediate value used as base address."));
                   3283:                          else
                   3284:                            as_warn (_("Invalid operand:  immediate value used as address."));
                   3285:                          /* gcc 2.6.3 is known to generate these in at least
                   3286:                             one case.  */
                   3287:                        }
                   3288:                      if (length == 0
                   3289:                          && is_absolute && (expP->X_op != O_big)
                   3290:                          && operandP->vop_mode == 8    /* No '@'.  */
                   3291:                          && this_add_number < 64)
                   3292:                        {
                   3293:                          operandP->vop_short = 's';
                   3294:                        }
                   3295:                      if (operandP->vop_short == 's')
                   3296:                        {
                   3297:                          FRAG_APPEND_1_CHAR (this_add_number);
                   3298:                        }
                   3299:                      else
                   3300:                        {
                   3301:                          /* I^#...  */
                   3302:                          know (nbytes);
                   3303:                          p = frag_more (nbytes + 1);
                   3304:                          know (operandP->vop_reg == 0xF);
                   3305: #ifdef OBJ_ELF
                   3306:                          if (flag_want_pic && operandP->vop_mode == 8
                   3307:                                && this_add_symbol != NULL)
                   3308:                            {
                   3309:                              as_warn (_("Symbol '%s' used as immediate operand in PIC mode."),
                   3310:                                       S_GET_NAME (this_add_symbol));
                   3311:                            }
                   3312: #endif
                   3313:                          p[0] = (operandP->vop_mode << 4) | 0xF;
                   3314:                          if ((is_absolute) && (expP->X_op != O_big))
                   3315:                            {
                   3316:                              /* If nbytes > 4, then we are scrod. We
                   3317:                                 don't know if the high order bytes
                   3318:                                 are to be 0xFF or 0x00.  BSD4.2 & RMS
                   3319:                                 say use 0x00. OK --- but this
                   3320:                                 assembler needs ANOTHER rewrite to
                   3321:                                 cope properly with this bug.  */
                   3322:                              md_number_to_chars (p + 1, this_add_number,
                   3323:                                                  min (sizeof (valueT),
                   3324:                                                       (size_t) nbytes));
                   3325:                              if ((size_t) nbytes > sizeof (valueT))
                   3326:                                memset (p + 1 + sizeof (valueT),
                   3327:                                        '\0', nbytes - sizeof (valueT));
                   3328:                            }
                   3329:                          else
                   3330:                            {
                   3331:                              if (expP->X_op == O_big)
                   3332:                                {
                   3333:                                  /* Problem here is to get the bytes
                   3334:                                     in the right order.  We stored
                   3335:                                     our constant as LITTLENUMs, not
                   3336:                                     bytes.  */
                   3337:                                  LITTLENUM_TYPE *lP;
                   3338:
                   3339:                                  lP = floatP->low;
                   3340:                                  if (nbytes & 1)
                   3341:                                    {
                   3342:                                      know (nbytes == 1);
                   3343:                                      p[1] = *lP;
                   3344:                                    }
                   3345:                                  else
                   3346:                                    {
                   3347:                                      for (p++; nbytes; nbytes -= 2, p += 2, lP++)
                   3348:                                        md_number_to_chars (p, *lP, 2);
                   3349:                                    }
                   3350:                                }
                   3351:                              else
                   3352:                                {
                   3353:                                  fix_new (frag_now, p + 1 - frag_now->fr_literal,
                   3354:                                           nbytes, this_add_symbol,
                   3355:                                           this_add_number, 0, NO_RELOC);
                   3356:                                }
                   3357:                            }
                   3358:                        }
                   3359:                    }
                   3360:                  else
                   3361:                    {
                   3362:                      /* {@}{q^}foo(Rn) */
                   3363:                      know ((length == 0 && operandP->vop_short == ' ')
                   3364:                            || (length > 0 && operandP->vop_short != ' '));
                   3365:                      if (length == 0)
                   3366:                        {
                   3367:                          if (is_absolute)
                   3368:                            {
                   3369:                              long test;
                   3370:
                   3371:                              test = this_add_number;
                   3372:
                   3373:                              if (test < 0)
                   3374:                                test = ~test;
                   3375:
                   3376:                              length = test & 0xffff8000 ? 4
                   3377:                                : test & 0xffffff80 ? 2
                   3378:                                : 1;
                   3379:                            }
                   3380:                          else
                   3381:                            {
                   3382:                              length = 4;
                   3383:                            }
                   3384:                        }
                   3385: #ifdef OBJ_ELF
                   3386:                      if (flag_want_pic && this_add_symbol != NULL)
                   3387:                        {
                   3388:                          as_warn (_("Symbol '%s' used as displacement in PIC mode."),
                   3389:                               S_GET_NAME (this_add_symbol));
                   3390:                        }
                   3391: #endif
                   3392:                      p = frag_more (1 + length);
                   3393:                      know (operandP->vop_reg != 0xf);
                   3394:                      know (operandP->vop_reg >= 0);
                   3395:                      p[0] = operandP->vop_reg
                   3396:                        | ((at | "?\12\14?\16"[length]) << 4);
                   3397:                      if (is_absolute)
                   3398:                        {
                   3399:                          md_number_to_chars (p + 1, this_add_number, length);
                   3400:                        }
                   3401:                      else
                   3402:                        {
                   3403:                          fix_new (frag_now, p + 1 - frag_now->fr_literal,
                   3404:                                   length, this_add_symbol,
                   3405:                                   this_add_number, 0, NO_RELOC);
                   3406:                        }
                   3407:                    }
                   3408:                }
                   3409:            }
                   3410:        }
                   3411:     }
                   3412: }
                   3413:
                   3414: void
                   3415: md_begin (void)
                   3416: {
                   3417:   const char *errtxt;
                   3418:   FLONUM_TYPE *fP;
                   3419:   int i;
                   3420:
                   3421:   if ((errtxt = vip_begin (1, "$", "*", "`")) != 0)
                   3422:     as_fatal (_("VIP_BEGIN error:%s"), errtxt);
                   3423:
                   3424:   for (i = 0, fP = float_operand;
                   3425:        fP < float_operand + VIT_MAX_OPERANDS;
                   3426:        i++, fP++)
                   3427:     {
                   3428:       fP->low = &big_operand_bits[i][0];
                   3429:       fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1];
                   3430:     }
                   3431: }
                   3432:
1.3       christos 3433: bfd_reloc_code_real_type
1.1       christos 3434: vax_cons (expressionS *exp, int size)
                   3435: {
                   3436:   char *save;
1.5     ! christos 3437:   const char *vax_cons_special_reloc;
1.1       christos 3438:
                   3439:   SKIP_WHITESPACE ();
                   3440:   vax_cons_special_reloc = NULL;
                   3441:   save = input_line_pointer;
                   3442:   if (input_line_pointer[0] == '%')
                   3443:     {
                   3444:       if (strncmp (input_line_pointer + 1, "pcrel", 5) == 0)
                   3445:        {
                   3446:          input_line_pointer += 6;
                   3447:          vax_cons_special_reloc = "pcrel";
                   3448:        }
                   3449:       if (vax_cons_special_reloc)
                   3450:        {
                   3451:          int bad = 0;
                   3452:
                   3453:          switch (size)
                   3454:            {
                   3455:            case 1:
                   3456:              if (*input_line_pointer != '8')
                   3457:                bad = 1;
                   3458:              input_line_pointer--;
                   3459:              break;
                   3460:            case 2:
                   3461:              if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
                   3462:                bad = 1;
                   3463:              break;
                   3464:            case 4:
                   3465:              if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
                   3466:                bad = 1;
                   3467:              break;
                   3468:            default:
                   3469:              bad = 1;
                   3470:              break;
                   3471:            }
                   3472:
                   3473:          if (bad)
                   3474:            {
                   3475:              as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
                   3476:                      vax_cons_special_reloc, size * 8, size);
                   3477:            }
                   3478:          else
                   3479:            {
                   3480:              input_line_pointer += 2;
                   3481:              if (*input_line_pointer != '(')
                   3482:                {
                   3483:                  as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
                   3484:                          vax_cons_special_reloc, size * 8);
                   3485:                  bad = 1;
                   3486:                }
                   3487:            }
                   3488:
                   3489:          if (bad)
                   3490:            {
                   3491:              input_line_pointer = save;
                   3492:              vax_cons_special_reloc = NULL;
                   3493:            }
                   3494:          else
                   3495:            {
                   3496:              int c;
                   3497:              char *end = ++input_line_pointer;
                   3498:              int npar = 0;
                   3499:
                   3500:              while (! is_end_of_line[(c = *end)])
                   3501:                {
                   3502:                  if (c == '(')
                   3503:                    npar++;
                   3504:                  else if (c == ')')
                   3505:                    {
                   3506:                      if (!npar)
                   3507:                        break;
                   3508:                      npar--;
                   3509:                    }
                   3510:                  end++;
                   3511:                }
                   3512:
                   3513:              if (c != ')')
                   3514:                as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
                   3515:                        vax_cons_special_reloc, size * 8);
                   3516:              else
                   3517:                {
                   3518:                  *end = '\0';
                   3519:                  expression (exp);
                   3520:                  *end = c;
                   3521:                  if (input_line_pointer != end)
                   3522:                    {
                   3523:                      as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
                   3524:                              vax_cons_special_reloc, size * 8);
                   3525:                    }
                   3526:                  else
                   3527:                    {
                   3528:                      input_line_pointer++;
                   3529:                      SKIP_WHITESPACE ();
                   3530:                      c = *input_line_pointer;
                   3531:                      if (! is_end_of_line[c] && c != ',')
                   3532:                        as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
                   3533:                                vax_cons_special_reloc, size * 8);
                   3534:                    }
                   3535:                }
                   3536:            }
                   3537:        }
                   3538:     }
                   3539:   if (vax_cons_special_reloc == NULL)
                   3540:     expression (exp);
1.3       christos 3541:   else
                   3542:     switch (size)
                   3543:       {
                   3544:       case 1: return BFD_RELOC_8_PCREL;
                   3545:       case 2: return BFD_RELOC_16_PCREL;
                   3546:       case 4: return BFD_RELOC_32_PCREL;
                   3547:       }
                   3548:   return NO_RELOC;
1.1       christos 3549: }
                   3550:
                   3551: /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
                   3552:    reloc for a cons.  */
                   3553:
                   3554: void
1.3       christos 3555: vax_cons_fix_new (fragS *frag, int where, unsigned int nbytes, expressionS *exp,
                   3556:                  bfd_reloc_code_real_type r)
1.1       christos 3557: {
1.3       christos 3558:   if (r == NO_RELOC)
                   3559:     r = (nbytes == 1 ? BFD_RELOC_8
                   3560:         : nbytes == 2 ? BFD_RELOC_16
                   3561:         : BFD_RELOC_32);
1.1       christos 3562:
                   3563:   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
                   3564: }
                   3565:
1.5     ! christos 3566: const char *
1.1       christos 3567: md_atof (int type, char * litP, int * sizeP)
                   3568: {
                   3569:   return vax_md_atof (type, litP, sizeP);
                   3570: }
                   3571:
                   3572: void
                   3573: vax_cfi_frame_initial_instructions (void)
                   3574: {
                   3575:   cfi_add_CFA_def_cfa (14, 0);
                   3576: }
                   3577:
                   3578: int
                   3579: tc_vax_regname_to_dw2regnum (char *regname)
                   3580: {
                   3581:   unsigned int i;
                   3582:   static const struct { char *name; int dw2regnum; } regnames[] =
                   3583:     {
                   3584:       { "r0",   0 }, { "r1",  1 }, { "r2",   2 }, { "r3",   3 },
                   3585:       { "r4",   4 }, { "r5",  5 }, { "r6",   6 }, { "r7",   7 },
                   3586:       { "r8",   8 }, { "r9",  9 }, { "r10", 10 }, { "r11", 11 },
                   3587:       { "ap",  12 }, { "fp", 13 }, { "sp",  14 }, { "pc",  15 },
                   3588:       { "psw", 16 },
                   3589:     };
                   3590:
                   3591:   for (i = 0; i < ARRAY_SIZE (regnames); ++i)
                   3592:     if (strcmp (regnames[i].name, regname) == 0)
                   3593:       return regnames[i].dw2regnum;
                   3594:
                   3595:   return -1;
                   3596: }
                   3597:
                   3598: void
                   3599: vax_cfi_emit_pcrel_expr (expressionS *expP, unsigned int nbytes)
                   3600: {
                   3601:   expP->X_add_number += nbytes;
                   3602:   emit_expr (expP, nbytes);
                   3603: }

CVSweb <webmaster@jp.NetBSD.org>