This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gas / config / tc-z8k.c
1 /* tc-z8k.c -- Assemble code for the Zilog Z800N
2    Copyright (C) 1992 Free Software Foundation.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /*
21   Written By Steve Chamberlain
22   sac@cygnus.com
23   */
24 #define DEFINE_TABLE
25 #include <stdio.h>
26
27 #include "../opcodes/z8k-opc.h"
28
29 #include "as.h"
30 #include "bfd.h"
31 #include <ctype.h>
32
33 const char comment_chars[] =
34 {'!', 0};
35 const char line_separator_chars[] =
36 {';', 0};
37 const char line_comment_chars[] =
38 {'#', 0};
39
40 extern int machine;
41 extern int coff_flags;
42 int segmented_mode;
43 const int md_reloc_size;
44
45 /* This table describes all the machine specific pseudo-ops the assembler
46    has to support.  The fields are:
47    pseudo-op name without dot
48    function to call to execute this pseudo-op
49    Integer arg to pass to the function
50    */
51
52 void cons ();
53
54 void
55 s_segm ()
56 {
57   segmented_mode = 1;
58   machine = bfd_mach_z8001;
59   coff_flags = F_Z8001;
60 }
61
62 void
63 s_unseg ()
64 {
65   segmented_mode = 0;
66   machine = bfd_mach_z8002;
67   coff_flags = F_Z8002;
68 }
69
70 static
71 void
72 even ()
73 {
74   frag_align (1, 0);
75   record_alignment (now_seg, 1);
76 }
77
78 void obj_coff_section ();
79
80 int
81 tohex (c)
82      int c;
83 {
84   if (isdigit (c))
85     return c - '0';
86   if (islower (c))
87     return c - 'a' + 10;
88   return c - 'A' + 10;
89 }
90
91 void
92 sval ()
93 {
94
95   SKIP_WHITESPACE ();
96   if (*input_line_pointer == '\'')
97     {
98       int c;
99       input_line_pointer++;
100       c = *input_line_pointer++;
101       while (c != '\'')
102         {
103           if (c == '%')
104             {
105               c = (tohex (input_line_pointer[0]) << 4)
106                 | tohex (input_line_pointer[1]);
107               input_line_pointer += 2;
108             }
109           FRAG_APPEND_1_CHAR (c);
110           c = *input_line_pointer++;
111         }
112       demand_empty_rest_of_line ();
113     }
114
115 }
116 const pseudo_typeS md_pseudo_table[] =
117 {
118   {"int", cons, 2},
119   {"data.b", cons, 1},
120   {"data.w", cons, 2},
121   {"data.l", cons, 4},
122   {"form", listing_psize, 0},
123   {"heading", listing_title, 0},
124   {"import", s_ignore, 0},
125   {"page", listing_eject, 0},
126   {"program", s_ignore, 0},
127   {"z8001", s_segm, 0},
128   {"z8002", s_unseg, 0},
129
130
131   {"segm", s_segm, 0},
132   {"unsegm", s_unseg, 0},
133   {"unseg", s_unseg, 0},
134   {"name", s_app_file, 0},
135   {"global", s_globl, 0},
136   {"wval", cons, 2},
137   {"lval", cons, 4},
138   {"bval", cons, 1},
139   {"sval", sval, 0},
140   {"rsect", obj_coff_section, 0},
141   {"sect", obj_coff_section, 0},
142   {"block", s_space, 0},
143   {"even", even, 0},
144   {0, 0, 0}
145 };
146
147 const char EXP_CHARS[] = "eE";
148
149 /* Chars that mean this number is a floating point constant */
150 /* As in 0f12.456 */
151 /* or    0d1.2345e12 */
152 const char FLT_CHARS[] = "rRsSfFdDxXpP";
153
154 const relax_typeS md_relax_table[1];
155
156 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
157
158 void
159 md_begin ()
160 {
161   opcode_entry_type *opcode;
162   char *prev_name = "";
163   int idx = 0;
164
165   opcode_hash_control = hash_new ();
166
167   for (opcode = z8k_table; opcode->name; opcode++)
168     {
169       /* Only enter unique codes into the table */
170       char *src = opcode->name;
171
172       if (strcmp (opcode->name, prev_name))
173         {
174           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
175           idx++;
176         }
177       opcode->idx = idx;
178       prev_name = opcode->name;
179     }
180
181   /* default to z8002 */
182   s_unseg ();
183
184   /* insert the pseudo ops too */
185   for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
186     {
187       opcode_entry_type *fake_opcode;
188       fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
189       fake_opcode->name = md_pseudo_table[idx].poc_name,
190         fake_opcode->func = (void *) (md_pseudo_table + idx);
191       fake_opcode->opcode = 250;
192
193       hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
194
195     }
196 }
197
198 struct z8k_exp
199 {
200   char *e_beg;
201   char *e_end;
202   expressionS e_exp;
203 };
204 typedef struct z8k_op
205 {
206   char regsize;                 /* 'b','w','r','q' */
207   unsigned int reg;             /* 0..15 */
208
209   int mode;
210
211   unsigned int x_reg;           /* any other register associated with the mode */
212   expressionS exp;              /* any expression */
213 }
214
215 op_type;
216
217 static expressionS *da_operand;
218 static expressionS *imm_operand;
219
220 int reg[16];
221 int the_cc;
222
223 char *
224 DEFUN (whatreg, (reg, src),
225        int *reg AND
226        char *src)
227 {
228   if (isdigit (src[1]))
229     {
230       *reg = (src[0] - '0') * 10 + src[1] - '0';
231       return src + 2;
232     }
233   else
234     {
235       *reg = (src[0] - '0');
236       return src + 1;
237     }
238 }
239
240 /*
241   parse operands
242
243   rh0-rh7, rl0-rl7
244   r0-r15
245   rr0-rr14
246   rq0--rq12
247   WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
248   r0l,r0h,..r7l,r7h
249   @WREG
250   @WREG+
251   @-WREG
252   #const
253
254   */
255
256 /* try and parse a reg name, returns number of chars consumed */
257 char *
258 DEFUN (parse_reg, (src, mode, reg),
259        char *src AND
260        int *mode AND
261        unsigned int *reg)
262 {
263   char *res = 0;
264
265   if (src[0] == 's' && src[1] == 'p')
266     {
267       if (segmented_mode)
268         {
269           *mode = CLASS_REG_LONG;
270           *reg = 14;
271         }
272       else
273         {
274           *mode = CLASS_REG_WORD;
275           *reg = 15;
276         }
277       return src + 2;
278     }
279   if (src[0] == 'r')
280     {
281       if (src[1] == 'r')
282         {
283           *mode = CLASS_REG_LONG;
284           res = whatreg (reg, src + 2);
285         }
286       else if (src[1] == 'h')
287         {
288           *mode = CLASS_REG_BYTE;
289           res = whatreg (reg, src + 2);
290         }
291       else if (src[1] == 'l')
292         {
293           *mode = CLASS_REG_BYTE;
294           res = whatreg (reg, src + 2);
295           *reg += 8;
296         }
297       else if (src[1] == 'q')
298         {
299           *mode = CLASS_REG_QUAD;
300           res = whatreg (reg, src + 2);
301         }
302       else
303         {
304           *mode = CLASS_REG_WORD;
305           res = whatreg (reg, src + 1);
306         }
307     }
308   return res;
309
310 }
311
312 char *
313 DEFUN (parse_exp, (s, op),
314        char *s AND
315        expressionS * op)
316 {
317   char *save = input_line_pointer;
318   char *new;
319
320   input_line_pointer = s;
321   expression (op);
322   if (op->X_op == O_absent)
323     as_bad ("missing operand");
324   new = input_line_pointer;
325   input_line_pointer = save;
326   return new;
327 }
328
329 /* The many forms of operand:
330
331    <rb>
332    <r>
333    <rr>
334    <rq>
335    @r
336    #exp
337    exp
338    exp(r)
339    r(#exp)
340    r(r)
341
342
343
344    */
345
346 static
347 char *
348 DEFUN (checkfor, (ptr, what),
349        char *ptr AND
350        char what)
351 {
352   if (*ptr == what)
353     ptr++;
354   else
355     {
356       as_bad ("expected %c", what);
357     }
358   return ptr;
359 }
360
361 /* Make sure the mode supplied is the size of a word */
362 static void
363 DEFUN (regword, (mode, string),
364        int mode AND
365        char *string)
366 {
367   int ok;
368
369   ok = CLASS_REG_WORD;
370   if (ok != mode)
371     {
372       as_bad ("register is wrong size for a word %s", string);
373     }
374 }
375
376 /* Make sure the mode supplied is the size of an address */
377 static void
378 DEFUN (regaddr, (mode, string),
379        int mode AND
380        char *string)
381 {
382   int ok;
383
384   ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
385   if (ok != mode)
386     {
387       as_bad ("register is wrong size for address %s", string);
388     }
389 }
390
391 struct cc_names
392 {
393   int value;
394   char *name;
395
396 };
397
398 struct cc_names table[] =
399 {
400   0x0, "f",
401   0x1, "lt",
402   0x2, "le",
403   0x3, "ule",
404   0x4, "ov",
405   0x4, "pe",
406   0x5, "mi",
407   0x6, "eq",
408   0x6, "z",
409   0x7, "c",
410   0x7, "ult",
411   0x8, "t",
412   0x9, "ge",
413   0xa, "gt",
414   0xb, "ugt",
415   0xc, "nov",
416   0xc, "po",
417   0xd, "pl",
418   0xe, "ne",
419   0xe, "nz",
420   0xf, "nc",
421   0xf, "uge",
422   0, 0
423 };
424
425 static void
426 DEFUN (get_cc_operand, (ptr, mode, dst),
427        char **ptr AND
428        struct z8k_op *mode AND
429        unsigned int dst)
430 {
431   char *src = *ptr;
432   int r;
433   int i;
434
435   while (*src == ' ')
436     src++;
437
438   mode->mode = CLASS_CC;
439   for (i = 0; table[i].name; i++)
440     {
441       int j;
442
443       for (j = 0; table[i].name[j]; j++)
444         {
445           if (table[i].name[j] != src[j])
446             goto fail;
447         }
448       the_cc = table[i].value;
449       *ptr = src + j;
450       return;
451     fail:;
452     }
453   the_cc = 0x8;
454   return;
455 }
456
457 static void
458 get_operand (ptr, mode, dst)
459      char **ptr;
460      struct z8k_op *mode;
461      unsigned int dst;
462 {
463   char *src = *ptr;
464   char *end;
465   unsigned int num;
466   unsigned int len;
467   unsigned int size;
468
469   mode->mode = 0;
470
471   while (*src == ' ')
472     src++;
473   if (*src == '#')
474     {
475       mode->mode = CLASS_IMM;
476       imm_operand = &(mode->exp);
477       src = parse_exp (src + 1, &(mode->exp));
478     }
479   else if (*src == '@')
480     {
481       int d;
482
483       mode->mode = CLASS_IR;
484       src = parse_reg (src + 1, &d, &mode->reg);
485     }
486   else
487     {
488       int regn;
489
490       end = parse_reg (src, &mode->mode, &regn);
491
492       if (end)
493         {
494           int nw, nr;
495
496           src = end;
497           if (*src == '(')
498             {
499               src++;
500               end = parse_reg (src, &nw, &nr);
501               if (end)
502                 {
503                   /* Got Ra(Rb) */
504                   src = end;
505
506                   if (*src != ')')
507                     {
508                       as_bad ("Missing ) in ra(rb)");
509                     }
510                   else
511                     {
512                       src++;
513                     }
514
515                   regaddr (mode->mode, "ra(rb) ra");
516 /*                regword (mode->mode, "ra(rb) rb");*/
517                   mode->mode = CLASS_BX;
518                   mode->reg = regn;
519                   mode->x_reg = nr;
520                   reg[ARG_RX] = nr;
521                 }
522               else
523                 {
524                   /* Got Ra(disp) */
525                   if (*src == '#')
526                     src++;
527                   src = parse_exp (src, &(mode->exp));
528                   src = checkfor (src, ')');
529                   mode->mode = CLASS_BA;
530                   mode->reg = regn;
531                   mode->x_reg = 0;
532                   imm_operand = &(mode->exp);
533                 }
534             }
535           else
536             {
537               mode->reg = regn;
538               mode->x_reg = 0;
539             }
540         }
541       else
542         {
543           /* No initial reg */
544           src = parse_exp (src, &(mode->exp));
545           if (*src == '(')
546             {
547               src++;
548               end = parse_reg (src, &(mode->mode), &regn);
549               regword (mode->mode, "addr(Ra) ra");
550               mode->mode = CLASS_X;
551               mode->reg = regn;
552               mode->x_reg = 0;
553               da_operand = &(mode->exp);
554               src = checkfor (end, ')');
555             }
556           else
557             {
558               /* Just an address */
559               mode->mode = CLASS_DA;
560               mode->reg = 0;
561               mode->x_reg = 0;
562               da_operand = &(mode->exp);
563             }
564         }
565     }
566   *ptr = src;
567 }
568
569 static
570 char *
571 get_operands (opcode, op_end, operand)
572      opcode_entry_type *opcode;
573      char *op_end;
574      op_type *operand;
575 {
576   char *ptr = op_end;
577
578   switch (opcode->noperands)
579     {
580     case 0:
581       operand[0].mode = 0;
582       operand[1].mode = 0;
583       break;
584
585     case 1:
586       ptr++;
587       if (opcode->arg_info[0] == CLASS_CC)
588         {
589           get_cc_operand (&ptr, operand + 0, 0);
590         }
591       else
592         {
593
594           get_operand (&ptr, operand + 0, 0);
595         }
596       operand[1].mode = 0;
597       break;
598
599     case 2:
600       ptr++;
601       if (opcode->arg_info[0] == CLASS_CC)
602         {
603           get_cc_operand (&ptr, operand + 0, 0);
604         }
605       else
606         {
607
608           get_operand (&ptr, operand + 0, 0);
609         }
610       if (ptr == 0)
611         return;
612       if (*ptr == ',')
613         ptr++;
614       get_operand (&ptr, operand + 1, 1);
615       break;
616
617     case 3:
618       ptr++;
619       get_operand (&ptr, operand + 0, 0);
620       if (*ptr == ',')
621         ptr++;
622       get_operand (&ptr, operand + 1, 1);
623       if (*ptr == ',')
624         ptr++;
625       get_operand (&ptr, operand + 2, 2);
626       break;
627
628     case 4:
629       ptr++;
630       get_operand (&ptr, operand + 0, 0);
631       if (*ptr == ',')
632         ptr++;
633       get_operand (&ptr, operand + 1, 1);
634       if (*ptr == ',')
635         ptr++;
636       get_operand (&ptr, operand + 2, 2);
637       if (*ptr == ',')
638         ptr++;
639       get_cc_operand (&ptr, operand + 3, 3);
640       break;
641     default:
642       abort ();
643     }
644
645   return ptr;
646 }
647
648 /* Passed a pointer to a list of opcodes which use different
649    addressing modes, return the opcode which matches the opcodes
650    provided
651    */
652
653 static
654 opcode_entry_type *
655 DEFUN (get_specific, (opcode, operands),
656        opcode_entry_type * opcode AND
657        op_type * operands)
658
659 {
660   opcode_entry_type *this_try = opcode;
661   int found = 0;
662   unsigned int noperands = opcode->noperands;
663
664   unsigned int dispreg;
665   unsigned int this_index = opcode->idx;
666
667   while (this_index == opcode->idx && !found)
668     {
669       unsigned int i;
670
671       this_try = opcode++;
672       for (i = 0; i < noperands; i++)
673         {
674           int mode = operands[i].mode;
675
676           if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
677             {
678               /* it could be an pc rel operand, if this is a da mode and
679            we like disps, then insert it */
680
681               if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
682                 {
683                   /* This is the case */
684                   operands[i].mode = CLASS_DISP;
685                 }
686               else if (mode == CLASS_BA && this_try->arg_info[i])
687                 {
688                   /* Can't think of a way to turn what we've been given into
689              something that's ok */
690                   goto fail;
691                 }
692               else if (this_try->arg_info[i] & CLASS_PR)
693                 {
694                   if (mode == CLASS_REG_LONG && segmented_mode)
695                     {
696                       /* ok */
697                     }
698                   else if (mode == CLASS_REG_WORD && !segmented_mode)
699                     {
700                       /* ok */
701                     }
702                   else
703                     goto fail;
704                 }
705               else
706                 goto fail;
707             }
708           switch (mode & CLASS_MASK)
709             {
710             default:
711               break;
712             case CLASS_X:
713             case CLASS_IR:
714             case CLASS_BA:
715             case CLASS_BX:
716             case CLASS_DISP:
717             case CLASS_REG:
718             case CLASS_REG_WORD:
719             case CLASS_REG_BYTE:
720             case CLASS_REG_QUAD:
721             case CLASS_REG_LONG:
722             case CLASS_REGN0:
723               reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
724               break;
725             }
726         }
727
728       found = 1;
729     fail:;
730     }
731   if (found)
732     return this_try;
733   else
734     return 0;
735 }
736
737 static void
738 DEFUN (check_operand, (operand, width, string),
739        struct z8k_op *operand AND
740        unsigned int width AND
741        char *string)
742 {
743   if (operand->exp.X_add_symbol == 0
744       && operand->exp.X_op_symbol == 0)
745     {
746
747       /* No symbol involved, let's look at offset, it's dangerous if any of
748        the high bits are not 0 or ff's, find out by oring or anding with
749        the width and seeing if the answer is 0 or all fs*/
750       if ((operand->exp.X_add_number & ~width) != 0 &&
751           (operand->exp.X_add_number | width) != (~0))
752         {
753           as_warn ("operand %s0x%x out of range.", string, operand->exp.X_add_number);
754         }
755     }
756
757 }
758
759 static char buffer[20];
760
761 static void
762 DEFUN (newfix, (ptr, type, operand),
763        int ptr AND
764        int type AND
765        expressionS * operand)
766 {
767   if (operand->X_add_symbol
768       || operand->X_op_symbol
769       || operand->X_add_number)
770     {
771       fix_new_exp (frag_now,
772                    ptr,
773                    1,
774                    operand,
775                    0,
776                    type);
777     }
778 }
779
780 static char *
781 DEFUN (apply_fix, (ptr, type, operand, size),
782        char *ptr AND
783        int type AND
784        expressionS * operand AND
785        int size)
786 {
787   int n = operand->X_add_number;
788
789   operand->X_add_number = n;
790   newfix ((ptr - buffer) / 2, type, operand);
791 #if 1
792   switch (size)
793     {
794     case 8:                     /* 8 nibbles == 32 bits */
795       *ptr++ = n >> 28;
796       *ptr++ = n >> 24;
797       *ptr++ = n >> 20;
798       *ptr++ = n >> 16;
799     case 4:                     /* 4 niblles == 16 bits */
800       *ptr++ = n >> 12;
801       *ptr++ = n >> 8;
802     case 2:
803       *ptr++ = n >> 4;
804     case 1:
805       *ptr++ = n >> 0;
806       break;
807     }
808 #endif
809   return ptr;
810
811 }
812
813 /* Now we know what sort of opcodes it is, lets build the bytes -
814  */
815 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
816 static void
817 build_bytes (this_try, operand)
818      opcode_entry_type * this_try;
819      struct z8k_op *operand;
820 {
821   unsigned int i;
822
823   int length;
824   char *output;
825   char *output_ptr = buffer;
826   char part;
827   int c;
828   char high;
829   int nib;
830   int nibble;
831   unsigned int *class_ptr;
832
833   frag_wane (frag_now);
834   frag_new (0);
835
836   memset (buffer, 20, 0);
837   class_ptr = this_try->byte_info;
838 top:;
839
840   for (nibble = 0; c = *class_ptr++; nibble++)
841     {
842
843       switch (c & CLASS_MASK)
844         {
845         default:
846
847           abort ();
848         case CLASS_ADDRESS:
849           /* Direct address, we don't cope with the SS mode right now */
850           if (segmented_mode)
851             {
852               da_operand->X_add_number |= 0x80000000;
853               output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
854             }
855           else
856             {
857               output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
858             }
859           da_operand = 0;
860           break;
861         case CLASS_DISP8:
862           /* pc rel 8 bit */
863           output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
864           da_operand = 0;
865           break;
866
867         case CLASS_0DISP7:
868           /* pc rel 7 bit */
869           *output_ptr = 0;
870           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
871           da_operand = 0;
872           break;
873
874         case CLASS_1DISP7:
875           /* pc rel 7 bit */
876           *output_ptr = 0x80;
877           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
878           da_operand = 0;
879           break;
880
881         case CLASS_BIT_1OR2:
882           *output_ptr = c & 0xf;
883           if (imm_operand)
884             {
885               if (imm_operand->X_add_number == 2)
886                 {
887                   *output_ptr |= 2;
888                 }
889               else if (imm_operand->X_add_number != 1)
890                 {
891                   as_bad ("immediate must be 1 or 2");
892                 }
893             }
894           else
895             {
896               as_bad ("immediate 1 or 2 expected");
897             }
898           output_ptr++;
899           break;
900         case CLASS_CC:
901           *output_ptr++ = the_cc;
902           break;
903         case CLASS_BIT:
904           *output_ptr++ = c & 0xf;
905           break;
906         case CLASS_REGN0:
907           if (reg[c & 0xf] == 0)
908             {
909               as_bad ("can't use R0 here");
910             }
911         case CLASS_REG:
912         case CLASS_REG_BYTE:
913         case CLASS_REG_WORD:
914         case CLASS_REG_LONG:
915         case CLASS_REG_QUAD:
916           /* Insert bit mattern of
917          right reg */
918           *output_ptr++ = reg[c & 0xf];
919           break;
920         case CLASS_DISP:
921           output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
922           da_operand = 0;
923           break;
924
925         case CLASS_IMM:
926           {
927             nib = 0;
928             switch (c & ARG_MASK)
929               {
930               case ARG_IMM4:
931                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
932                 break;
933               case ARG_IMM4M1:
934                 imm_operand->X_add_number--;
935                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
936                 break;
937               case ARG_IMMNMINUS1:
938                 imm_operand->X_add_number--;
939                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
940                 break;
941               case ARG_NIM8:
942                 imm_operand->X_add_number = -imm_operand->X_add_number;
943               case ARG_IMM8:
944                 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
945                 break;
946               case ARG_IMM16:
947                 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
948                 break;
949
950               case ARG_IMM32:
951                 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
952                 break;
953
954               default:
955                 abort ();
956               }
957           }
958         }
959     }
960
961   /* Copy from the nibble buffer into the frag */
962
963   {
964     int length = (output_ptr - buffer) / 2;
965     char *src = buffer;
966     char *fragp = frag_more (length);
967
968     while (src < output_ptr)
969       {
970         *fragp = (src[0] << 4) | src[1];
971         src += 2;
972         fragp++;
973       }
974
975   }
976
977 }
978
979 /* This is the guts of the machine-dependent assembler.  STR points to a
980    machine dependent instruction.  This funciton is supposed to emit
981    the frags/bytes it assembles to.
982    */
983
984 void
985 DEFUN (md_assemble, (str),
986        char *str)
987 {
988   char *op_start;
989   char *op_end;
990   unsigned int i;
991   struct z8k_op operand[3];
992   opcode_entry_type *opcode;
993   opcode_entry_type *prev_opcode;
994
995   char *dot = 0;
996   char c;
997
998   /* Drop leading whitespace */
999   while (*str == ' ')
1000     str++;
1001
1002   /* find the op code end */
1003   for (op_start = op_end = str;
1004        *op_end != 0 && *op_end != ' ';
1005        op_end++)
1006     {
1007     }
1008
1009   ;
1010
1011   if (op_end == op_start)
1012     {
1013       as_bad ("can't find opcode ");
1014     }
1015   c = *op_end;
1016
1017   *op_end = 0;
1018
1019   opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1020                                             op_start);
1021
1022
1023   if (opcode == NULL)
1024     {
1025       as_bad ("unknown opcode");
1026       return;
1027     }
1028
1029   if (opcode->opcode == 250)
1030     {
1031       /* was really a pseudo op */
1032
1033       pseudo_typeS *p;
1034       char oc;
1035
1036       char *old = input_line_pointer;
1037       *op_end = c;
1038
1039
1040       input_line_pointer = op_end;
1041
1042       oc = *old;
1043       *old = '\n';
1044       while (*input_line_pointer == ' ')
1045         input_line_pointer++;
1046       p = (pseudo_typeS *) (opcode->func);
1047
1048       (p->poc_handler) (p->poc_val);
1049       input_line_pointer = old;
1050       *old = oc;
1051     }
1052   else
1053     {
1054       input_line_pointer = get_operands (opcode, op_end,
1055                                          operand);
1056       prev_opcode = opcode;
1057
1058       opcode = get_specific (opcode, operand);
1059
1060       if (opcode == 0)
1061         {
1062           /* Couldn't find an opcode which matched the operands */
1063           char *where = frag_more (2);
1064
1065           where[0] = 0x0;
1066           where[1] = 0x0;
1067
1068           as_bad ("Can't find opcode to match operands");
1069           return;
1070         }
1071
1072       build_bytes (opcode, operand);
1073     }
1074 }
1075
1076 void
1077 DEFUN (tc_crawl_symbol_chain, (headers),
1078        object_headers * headers)
1079 {
1080   printf ("call to tc_crawl_symbol_chain \n");
1081 }
1082
1083 symbolS *
1084 DEFUN (md_undefined_symbol, (name),
1085        char *name)
1086 {
1087   return 0;
1088 }
1089
1090 void
1091 DEFUN (tc_headers_hook, (headers),
1092        object_headers * headers)
1093 {
1094   printf ("call to tc_headers_hook \n");
1095 }
1096
1097 void
1098 DEFUN_VOID (md_end)
1099 {
1100 }
1101
1102 /* Various routines to kill one day */
1103 /* Equal to MAX_PRECISION in atof-ieee.c */
1104 #define MAX_LITTLENUMS 6
1105
1106 /* Turn a string in input_line_pointer into a floating point constant of type
1107    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1108    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
1109    */
1110 char *
1111 md_atof (type, litP, sizeP)
1112      char type;
1113      char *litP;
1114      int *sizeP;
1115 {
1116   int prec;
1117   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1118   LITTLENUM_TYPE *wordP;
1119   char *t;
1120   char *atof_ieee ();
1121
1122   switch (type)
1123     {
1124     case 'f':
1125     case 'F':
1126     case 's':
1127     case 'S':
1128       prec = 2;
1129       break;
1130
1131     case 'd':
1132     case 'D':
1133     case 'r':
1134     case 'R':
1135       prec = 4;
1136       break;
1137
1138     case 'x':
1139     case 'X':
1140       prec = 6;
1141       break;
1142
1143     case 'p':
1144     case 'P':
1145       prec = 6;
1146       break;
1147
1148     default:
1149       *sizeP = 0;
1150       return "Bad call to MD_ATOF()";
1151     }
1152   t = atof_ieee (input_line_pointer, type, words);
1153   if (t)
1154     input_line_pointer = t;
1155
1156   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1157   for (wordP = words; prec--;)
1158     {
1159       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1160       litP += sizeof (LITTLENUM_TYPE);
1161     }
1162   return 0;
1163 }
1164
1165 int
1166 md_parse_option (argP, cntP, vecP)
1167      char **argP;
1168      int *cntP;
1169      char ***vecP;
1170
1171 {
1172   if (!strcmp (*argP, "z8001"))
1173     {
1174       s_segm ();
1175     }
1176   else if (!strcmp (*argP, "z8002"))
1177     {
1178       s_unseg ();
1179     }
1180   else
1181     return 0;
1182   **argP = 0;
1183   return 1;
1184 }
1185
1186 int md_short_jump_size;
1187
1188 void
1189 tc_aout_fix_to_chars ()
1190 {
1191   printf ("call to tc_aout_fix_to_chars \n");
1192   abort ();
1193 }
1194
1195 void
1196 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1197      char *ptr;
1198      addressT from_addr;
1199      addressT to_addr;
1200      fragS *frag;
1201      symbolS *to_symbol;
1202 {
1203   as_fatal ("failed sanity check.");
1204 }
1205
1206 void
1207 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1208      char *ptr;
1209      addressT from_addr, to_addr;
1210      fragS *frag;
1211      symbolS *to_symbol;
1212 {
1213   as_fatal ("failed sanity check.");
1214 }
1215
1216 void
1217 md_convert_frag (headers, fragP)
1218      object_headers *headers;
1219      fragS *fragP;
1220
1221 {
1222   printf ("call to md_convert_frag \n");
1223   abort ();
1224 }
1225
1226 valueT
1227 DEFUN (md_section_align, (seg, size),
1228        segT seg AND
1229        valueT size)
1230 {
1231   return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1232
1233 }
1234
1235 void
1236 md_apply_fix (fixP, val)
1237      fixS *fixP;
1238      long val;
1239 {
1240   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1241
1242   switch (fixP->fx_r_type)
1243     {
1244     case R_IMM4L:
1245       buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1246       break;
1247
1248     case R_JR:
1249
1250       *buf++ = val;
1251       /*    if (val != 0) abort();*/
1252       break;
1253
1254     case R_DISP7:
1255
1256       *buf++ += val;
1257       /*    if (val != 0) abort();*/
1258       break;
1259
1260     case R_IMM8:
1261       buf[0] += val;
1262       break;
1263     case R_IMM16:
1264       *buf++ = (val >> 8);
1265       *buf++ = val;
1266       break;
1267     case R_IMM32:
1268       *buf++ = (val >> 24);
1269       *buf++ = (val >> 16);
1270       *buf++ = (val >> 8);
1271       *buf++ = val;
1272       break;
1273 #if 0
1274     case R_DA | R_SEG:
1275       *buf++ = (val >> 16);
1276       *buf++ = 0x00;
1277       *buf++ = (val >> 8);
1278       *buf++ = val;
1279       break;
1280 #endif
1281
1282     default:
1283       abort ();
1284
1285     }
1286 }
1287
1288 void
1289 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1290 {
1291 }
1292
1293 int md_long_jump_size;
1294 int
1295 md_estimate_size_before_relax (fragP, segment_type)
1296      register fragS *fragP;
1297      register segT segment_type;
1298 {
1299   printf ("call tomd_estimate_size_before_relax \n");
1300   abort ();
1301 }
1302
1303 /* Put number into target byte order */
1304
1305 void
1306 DEFUN (md_number_to_chars, (ptr, use, nbytes),
1307        char *ptr AND
1308        valueT use AND
1309        int nbytes)
1310 {
1311   switch (nbytes)
1312     {
1313     case 4:
1314       *ptr++ = (use >> 24) & 0xff;
1315     case 3:
1316       *ptr++ = (use >> 16) & 0xff;
1317     case 2:
1318       *ptr++ = (use >> 8) & 0xff;
1319     case 1:
1320       *ptr++ = (use >> 0) & 0xff;
1321       break;
1322     default:
1323       abort ();
1324     }
1325 }
1326 long
1327 md_pcrel_from (fixP)
1328      fixS *fixP;
1329 {
1330   abort ();
1331 }
1332
1333 void
1334 tc_coff_symbol_emit_hook ()
1335 {
1336 }
1337
1338 void
1339 tc_reloc_mangle (fix_ptr, intr, base)
1340      fixS *fix_ptr;
1341      struct internal_reloc *intr;
1342      bfd_vma base;
1343
1344 {
1345   symbolS *symbol_ptr;
1346
1347   symbol_ptr = fix_ptr->fx_addsy;
1348
1349   /* If this relocation is attached to a symbol then it's ok
1350      to output it */
1351   if (fix_ptr->fx_r_type == 0)
1352     {
1353       /* cons likes to create reloc32's whatever the size of the reloc..
1354      */
1355       switch (fix_ptr->fx_size)
1356         {
1357
1358         case 2:
1359           intr->r_type = R_IMM16;
1360           break;
1361         case 1:
1362           intr->r_type = R_IMM8;
1363           break;
1364         case 4:
1365           intr->r_type = R_IMM32;
1366           break;
1367         default:
1368           abort ();
1369
1370         }
1371
1372     }
1373   else
1374     {
1375       intr->r_type = fix_ptr->fx_r_type;
1376     }
1377
1378   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1379   intr->r_offset = fix_ptr->fx_offset;
1380
1381   if (symbol_ptr)
1382     intr->r_symndx = symbol_ptr->sy_number;
1383   else
1384     intr->r_symndx = -1;
1385
1386 }