+ else
+ as_bad (_("Bit field out of range"));
+}
+
+/* Convert iif to fragments. From this point we start to dribble with
+ functions in other files than this one.(Except hash.c) So, if it's
+ possible to make an iif for an other CPU, you don't need to know
+ what frags, relax, obstacks, etc is in order to port this
+ assembler. You only need to know if it's possible to reduce your
+ cpu-instruction to iif-format (takes some work) and adopt the other
+ md_? parts according to given instructions Note that iif was
+ invented for the clean ns32k`s architecture. */
+
+/* GAS for the ns32k has a problem. PC relative displacements are
+ relative to the address of the opcode, not the address of the
+ operand. We used to keep track of the offset between the operand
+ and the opcode in pcrel_adjust for each frag and each fix. However,
+ we get into trouble where there are two or more pc-relative
+ operands and the size of the first one can't be determined. Then in
+ the relax phase, the size of the first operand will change and
+ pcrel_adjust will no longer be correct. The current solution is
+ keep a pointer to the frag with the opcode in it and the offset in
+ that frag for each frag and each fix. Then, when needed, we can
+ always figure out how far it is between the opcode and the pcrel
+ object. See also md_pcrel_adjust and md_fix_pcrel_adjust. For
+ objects not part of an instruction, the pointer to the opcode frag
+ is always zero. */
+
+static void
+convert_iif (void)
+{
+ int i;
+ bit_fixS *j;
+ fragS *inst_frag;
+ unsigned int inst_offset;
+ char *inst_opcode;
+ char *memP;
+ int l;
+ int k;
+ char type;
+ char size = 0;
+
+ frag_grow (iif.instr_size); /* This is important. */
+ memP = frag_more (0);
+ inst_opcode = memP;
+ inst_offset = (memP - frag_now->fr_literal);
+ inst_frag = frag_now;
+
+ for (i = 0; i < IIF_ENTRIES; i++)
+ {
+ if ((type = iif.iifP[i].type))
+ {
+ /* The object exist, so handle it. */
+ switch (size = iif.iifP[i].size)
+ {
+ case 42:
+ size = 0;
+ /* It's a bitfix that operates on an existing object. */
+ if (iif.iifP[i].bit_fixP->fx_bit_base)
+ /* Expand fx_bit_base to point at opcode. */
+ iif.iifP[i].bit_fixP->fx_bit_base = (long) inst_opcode;
+ /* Fall through. */
+
+ case 8: /* bignum or doublefloat. */
+ case 1:
+ case 2:
+ case 3:
+ case 4:
+ /* The final size in objectmemory is known. */
+ memP = frag_more (size);
+ j = iif.iifP[i].bit_fixP;
+
+ switch (type)
+ {
+ case 1: /* The object is pure binary. */
+ if (j)
+ md_number_to_field (memP, exprP.X_add_number, j);
+
+ else if (iif.iifP[i].pcrel)
+ fix_new_ns32k (frag_now,
+ (long) (memP - frag_now->fr_literal),
+ size,
+ 0,
+ iif.iifP[i].object,
+ iif.iifP[i].pcrel,
+ iif.iifP[i].im_disp,
+ 0,
+ iif.iifP[i].bsr, /* Sequent hack. */
+ inst_frag, inst_offset);
+ else
+ {
+ /* Good, just put them bytes out. */
+ switch (iif.iifP[i].im_disp)
+ {
+ case 0:
+ md_number_to_chars (memP, iif.iifP[i].object, size);
+ break;
+ case 1:
+ md_number_to_disp (memP, iif.iifP[i].object, size);
+ break;
+ default:
+ as_fatal (_("iif convert internal pcrel/binary"));
+ }
+ }
+ break;
+
+ case 2:
+ /* The object is a pointer at an expression, so
+ unpack it, note that bignums may result from the
+ expression. */
+ evaluate_expr (&exprP, (char *) iif.iifP[i].object);
+ if (exprP.X_op == O_big || size == 8)
+ {
+ if ((k = exprP.X_add_number) > 0)
+ {
+ /* We have a bignum ie a quad. This can only
+ happens in a long suffixed instruction. */
+ if (k * 2 > size)
+ as_bad (_("Bignum too big for long"));
+
+ if (k == 3)
+ memP += 2;
+
+ for (l = 0; k > 0; k--, l += 2)
+ md_number_to_chars (memP + l,
+ generic_bignum[l >> 1],
+ sizeof (LITTLENUM_TYPE));
+ }
+ else
+ {
+ /* flonum. */
+ LITTLENUM_TYPE words[4];
+
+ switch (size)
+ {
+ case 4:
+ gen_to_words (words, 2, 8);
+ md_number_to_imm (memP, (long) words[0],
+ sizeof (LITTLENUM_TYPE));
+ md_number_to_imm (memP + sizeof (LITTLENUM_TYPE),
+ (long) words[1],
+ sizeof (LITTLENUM_TYPE));
+ break;
+ case 8:
+ gen_to_words (words, 4, 11);
+ md_number_to_imm (memP, (long) words[0],
+ sizeof (LITTLENUM_TYPE));
+ md_number_to_imm (memP + sizeof (LITTLENUM_TYPE),
+ (long) words[1],
+ sizeof (LITTLENUM_TYPE));
+ md_number_to_imm ((memP + 2
+ * sizeof (LITTLENUM_TYPE)),
+ (long) words[2],
+ sizeof (LITTLENUM_TYPE));
+ md_number_to_imm ((memP + 3
+ * sizeof (LITTLENUM_TYPE)),
+ (long) words[3],
+ sizeof (LITTLENUM_TYPE));
+ break;
+ }
+ }
+ break;
+ }
+ if (exprP.X_add_symbol ||
+ exprP.X_op_symbol ||
+ iif.iifP[i].pcrel)
+ {
+ /* The expression was undefined due to an
+ undefined label. Create a fix so we can fix
+ the object later. */
+ exprP.X_add_number += iif.iifP[i].object_adjust;
+ fix_new_ns32k_exp (frag_now,
+ (long) (memP - frag_now->fr_literal),
+ size,
+ &exprP,
+ iif.iifP[i].pcrel,
+ iif.iifP[i].im_disp,
+ j,
+ iif.iifP[i].bsr,
+ inst_frag, inst_offset);
+ }
+ else if (j)
+ md_number_to_field (memP, exprP.X_add_number, j);
+ else
+ {
+ /* Good, just put them bytes out. */
+ switch (iif.iifP[i].im_disp)
+ {
+ case 0:
+ md_number_to_imm (memP, exprP.X_add_number, size);
+ break;
+ case 1:
+ md_number_to_disp (memP, exprP.X_add_number, size);
+ break;
+ default:
+ as_fatal (_("iif convert internal pcrel/pointer"));
+ }
+ }
+ break;
+ default:
+ as_fatal (_("Internal logic error in iif.iifP[n].type"));
+ }
+ break;
+
+ case 0:
+ /* Too bad, the object may be undefined as far as its
+ final nsize in object memory is concerned. The size
+ of the object in objectmemory is not explicitly
+ given. If the object is defined its length can be
+ determined and a fix can replace the frag. */
+ {
+ evaluate_expr (&exprP, (char *) iif.iifP[i].object);
+
+ if ((exprP.X_add_symbol || exprP.X_op_symbol) &&
+ !iif.iifP[i].pcrel)
+ {
+ /* Size is unknown until link time so have to default. */
+ size = default_disp_size; /* Normally 4 bytes. */
+ memP = frag_more (size);
+ fix_new_ns32k_exp (frag_now,
+ (long) (memP - frag_now->fr_literal),
+ size,
+ &exprP,
+ 0, /* never iif.iifP[i].pcrel, */
+ 1, /* always iif.iifP[i].im_disp */
+ (bit_fixS *) 0, 0,
+ inst_frag,
+ inst_offset);
+ break; /* Exit this absolute hack. */
+ }
+
+ if (exprP.X_add_symbol || exprP.X_op_symbol)
+ {
+ /* Frag it. */
+ if (exprP.X_op_symbol)
+ /* We cant relax this case. */
+ as_fatal (_("Can't relax difference"));
+ else
+ {
+ /* Size is not important. This gets fixed by
+ relax, but we assume 0 in what follows. */
+ memP = frag_more (4); /* Max size. */
+ size = 0;
+
+ {
+ fragS *old_frag = frag_now;
+ frag_variant (rs_machine_dependent,
+ 4, /* Max size. */
+ 0, /* Size. */
+ IND (BRANCH, UNDEF), /* Expecting
+ the worst. */
+ exprP.X_add_symbol,
+ exprP.X_add_number,
+ inst_opcode);
+ frag_opcode_frag (old_frag) = inst_frag;
+ frag_opcode_offset (old_frag) = inst_offset;
+ frag_bsr (old_frag) = iif.iifP[i].bsr;
+ }
+ }
+ }
+ else
+ {
+ /* This duplicates code in md_number_to_disp. */
+ if (-64 <= exprP.X_add_number && exprP.X_add_number <= 63)
+ size = 1;
+ else
+ {
+ if (-8192 <= exprP.X_add_number
+ && exprP.X_add_number <= 8191)
+ size = 2;
+ else
+ {
+ if (-0x20000000 <= exprP.X_add_number
+ && exprP.X_add_number<=0x1fffffff)
+ size = 4;
+ else
+ {
+ as_bad (_("Displacement too large for :d"));
+ size = 4;
+ }
+ }
+ }
+
+ memP = frag_more (size);
+ md_number_to_disp (memP, exprP.X_add_number, size);
+ }
+ }
+ break;
+
+ default:
+ as_fatal (_("Internal logic error in iif.iifP[].type"));
+ }
+ }
+ }
+}
+\f
+void
+md_assemble (char *line)
+{
+ freeptr = freeptr_static;
+ parse (line, 0); /* Explode line to more fix form in iif. */
+ convert_iif (); /* Convert iif to frags, fix's etc. */
+#ifdef SHOW_NUM
+ printf (" \t\t\t%s\n", line);
+#endif
+}
+
+void
+md_begin (void)
+{
+ /* Build a hashtable of the instructions. */
+ const struct ns32k_opcode *ptr;
+ const char *status;
+ const struct ns32k_opcode *endop;
+
+ inst_hash_handle = hash_new ();
+
+ endop = ns32k_opcodes + sizeof (ns32k_opcodes) / sizeof (ns32k_opcodes[0]);
+ for (ptr = ns32k_opcodes; ptr < endop; ptr++)
+ {
+ if ((status = hash_insert (inst_hash_handle, ptr->name, (char *) ptr)))
+ /* Fatal. */
+ as_fatal (_("Can't hash %s: %s"), ptr->name, status);
+ }
+
+ /* Some private space please! */
+ freeptr_static = XNEWVEC (char, PRIVATE_SIZE);
+}
+
+/* Turn the string pointed to by litP into a floating point constant
+ of type TYPE, and emit the appropriate bytes. The number of
+ LITTLENUMS emitted is stored in *SIZEP. An error message is
+ returned, or NULL on OK. */
+
+const char *
+md_atof (int type, char *litP, int *sizeP)
+{
+ return ieee_md_atof (type, litP, sizeP, FALSE);
+}
+\f
+int
+md_pcrel_adjust (fragS *fragP)
+{
+ fragS *opcode_frag;
+ addressT opcode_address;
+ unsigned int offset;
+
+ opcode_frag = frag_opcode_frag (fragP);
+ if (opcode_frag == 0)
+ return 0;
+
+ offset = frag_opcode_offset (fragP);
+ opcode_address = offset + opcode_frag->fr_address;
+
+ return fragP->fr_address + fragP->fr_fix - opcode_address;
+}
+
+static int
+md_fix_pcrel_adjust (fixS *fixP)
+{
+ fragS *opcode_frag;
+ addressT opcode_address;
+ unsigned int offset;
+
+ opcode_frag = fix_opcode_frag (fixP);
+ if (opcode_frag == 0)
+ return 0;
+
+ offset = fix_opcode_offset (fixP);
+ opcode_address = offset + opcode_frag->fr_address;
+
+ return fixP->fx_where + fixP->fx_frag->fr_address - opcode_address;