start-sanitize-d10v
[external/binutils.git] / gas / config / tc-d10v.c
1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
2
3    Copyright (C) 1996 Free Software Foundation.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdio.h>
23 #include <ctype.h>
24 #include "as.h"
25 #include "subsegs.h"     
26 #include "opcode/d10v.h"
27 #include "elf/ppc.h"
28
29 const char comment_chars[] = "#;";
30 const char line_comment_chars[] = "#";
31 const char line_separator_chars[] = "";
32 const char *md_shortopts = "";
33 const char EXP_CHARS[] = "eE";
34 const char FLT_CHARS[] = "dD";
35
36
37 /* fixups */
38 #define MAX_INSN_FIXUPS (5)
39 struct d10v_fixup
40 {
41   expressionS exp;
42   bfd_reloc_code_real_type reloc;
43 };
44
45 typedef struct _fixups
46 {
47   int fc;
48   struct d10v_fixup fix[MAX_INSN_FIXUPS];
49   struct _fixups *next;
50 } Fixups;
51
52 static Fixups FixUps[2];
53 static Fixups *fixups;
54
55 /* local functions */
56 static int reg_name_search PARAMS ((char *name));
57 static int register_name PARAMS ((expressionS *expressionP));
58 static int postfix PARAMS ((char *p));
59 static bfd_reloc_code_real_type get_reloc PARAMS ((struct d10v_operand *op));
60 static int get_operands PARAMS ((expressionS exp[]));
61 static unsigned long build_insn PARAMS ((struct d10v_opcode *opcode, expressionS *opers));
62 static void write_long PARAMS ((struct d10v_opcode *opcode, unsigned long insn, Fixups *fx));
63 static void write_1_short PARAMS ((struct d10v_opcode *opcode, unsigned long insn, Fixups *fx));
64 static int write_2_short PARAMS ((struct d10v_opcode *opcode1, unsigned long insn1, 
65                                   struct d10v_opcode *opcode2, unsigned long insn2, int exec_type, Fixups *fx));
66 static unsigned long do_assemble PARAMS ((char *str, struct d10v_opcode **opcode));
67 static unsigned long d10v_insert_operand PARAMS (( unsigned long insn, int op_type,
68                                                    offsetT value, int left));
69
70
71 struct option md_longopts[] = {
72   {NULL, no_argument, NULL, 0}
73 };
74 size_t md_longopts_size = sizeof(md_longopts);       
75
76 /* The target specific pseudo-ops which we support.  */
77 const pseudo_typeS md_pseudo_table[] =
78 {
79   { NULL,       NULL,           0 }
80 };
81
82 /* Opcode hash table.  */
83 static struct hash_control *d10v_hash;
84
85 /* reg_name_search does a binary search of the pre_defined_registers
86    array to see if "name" is a valid regiter name.  Returns the register
87    number from the array on success, or -1 on failure. */
88
89 static int
90 reg_name_search (name)
91      char *name;
92 {
93   int middle, low, high;
94   int cmp;
95
96   low = 0;
97   high = reg_name_cnt() - 1;
98
99   do
100     {
101       middle = (low + high) / 2;
102       cmp = strcasecmp (name, pre_defined_registers[middle].name);
103       if (cmp < 0)
104         high = middle - 1;
105       else if (cmp > 0)
106         low = middle + 1;
107       else 
108           return pre_defined_registers[middle].value;
109     }
110   while (low <= high);
111   return -1;
112 }
113
114 /* register_name() checks the string at input_line_pointer
115    to see if it is a valid register name */
116
117 static int
118 register_name (expressionP)
119      expressionS *expressionP;
120 {
121   int reg_number;
122   char c, *p = input_line_pointer;
123   
124   while (*p && *p!='\n' && *p!='\r' && *p !=',' && *p!=' ' && *p!=')')
125     p++;
126
127   c = *p;
128   if (c)
129     *p++ = 0;
130
131   /* look to see if it's in the register table */
132   reg_number = reg_name_search (input_line_pointer);
133   if (reg_number >= 0) 
134     {
135       expressionP->X_op = O_register;
136       /* temporarily store a pointer to the string here */
137       expressionP->X_op_symbol = (struct symbol *)input_line_pointer;
138       expressionP->X_add_number = reg_number;
139       input_line_pointer = p;
140       return 1;
141     }
142   if (c)
143     *(p-1) = c;
144   return 0;
145 }
146
147 void
148 md_show_usage (stream)
149   FILE *stream;
150 {
151   fprintf(stream, "D10V options:\n\
152 none yet\n");
153
154
155 int
156 md_parse_option (c, arg)
157      int c;
158      char *arg;
159 {
160   return 0;
161 }
162
163 symbolS *
164 md_undefined_symbol (name)
165   char *name;
166 {
167   return 0;
168 }
169
170 char *
171 md_atof (type, litp, sizep)
172   int type;
173 char *litp;
174 int *sizep;
175 {
176   return "";
177 }
178
179 void
180 md_convert_frag (abfd, sec, fragP)
181   bfd *abfd;
182   asection *sec;
183   fragS *fragP;
184 {
185   printf ("call to md_convert_frag \n");
186   abort ();
187 }
188
189 valueT
190 md_section_align (seg, addr)
191      asection *seg;
192      valueT addr;
193 {
194   int align = bfd_get_section_alignment (stdoutput, seg);
195   return ((addr + (1 << align) - 1) & (-1 << align));
196 }
197
198
199 void
200 md_begin ()
201 {
202   char *prev_name = "";
203   struct d10v_opcode *opcode;
204   d10v_hash = hash_new();
205
206   /* Insert unique names into hash table.  The D10v instruction set
207      has many identical opcode names that have different opcodes based
208      on the operands.  This hash table then provides a quick index to
209      the first opcode with a particular name in the opcode table.  */
210
211   for (opcode = (struct d10v_opcode *)d10v_opcodes; opcode->name; opcode++)
212     {
213       if (strcmp (prev_name, opcode->name))
214         {
215           prev_name = (char *)opcode->name;
216           hash_insert (d10v_hash, opcode->name, (char *) opcode);
217         }
218     }
219
220   fixups = &FixUps[0];
221   FixUps[0].next = &FixUps[1];
222   FixUps[1].next = &FixUps[0];
223 }
224
225
226 /* this function removes the postincrement or postdecrement
227    operator ( '+' or '-' ) from an expression */
228
229 static int postfix (p) 
230      char *p;
231 {
232   while (*p != '-' && *p != '+') 
233     {
234       if (*p==0 || *p=='\n' || *p=='\r') 
235         break;
236       p++;
237     }
238
239   if (*p == '-') 
240     {
241       *p = ' ';
242       return (-1);
243     }
244   if (*p == '+') 
245     {
246       *p = ' ';
247       return (1);
248     }
249
250   return (0);
251 }
252
253
254 static bfd_reloc_code_real_type 
255 get_reloc (op) 
256      struct d10v_operand *op;
257 {
258   int bits = op->bits;
259
260   /*  printf("get_reloc:  bits=%d  address=%d\n",bits,op->flags & OPERAND_ADDR);   */
261   if (bits <= 4) 
262     return (0);
263       
264   if (op->flags & OPERAND_ADDR) 
265     {
266       if (bits == 8)
267         return (BFD_RELOC_D10V_10_PCREL_R);
268       else
269           return (BFD_RELOC_D10V_18_PCREL);
270     }
271
272   return (BFD_RELOC_16);
273 }
274
275 /* get_operands parses a string of operands and returns
276    an array of expressions */
277
278 static int
279 get_operands (exp) 
280      expressionS exp[];
281 {
282   char *p = input_line_pointer;
283   int numops = 0;
284   int post = 0;
285
286   while (*p)  
287     {
288       while (*p == ' ' || *p == '\t' || *p == ',') 
289         p++;
290       if (*p==0 || *p=='\n' || *p=='\r') 
291         break;
292       
293       if (*p == '@') 
294         {
295           p++;
296           exp[numops].X_op = O_absent;
297           if (*p == '(') 
298             {
299               p++;
300               exp[numops].X_add_number = OPERAND_ATPAR;
301             }
302           else if (*p == '-') 
303             {
304               p++;
305               exp[numops].X_add_number = OPERAND_ATMINUS;
306             }
307           else
308             {
309               exp[numops].X_add_number = OPERAND_ATSIGN;
310               post = postfix (p);
311             }
312           numops++;
313           continue;
314         }
315
316       if (*p == ')') 
317         {
318           /* just skip the trailing paren */
319           p++;
320           continue;
321         }
322
323       input_line_pointer = p;
324
325
326       /* check to see if it might be a register name */
327       if (!register_name (&exp[numops]))
328         {
329           /* parse as an expression */
330           expression (&exp[numops]);
331         }
332
333       if (exp[numops].X_op == O_illegal) 
334         as_bad ("illegal operand");
335       else if (exp[numops].X_op == O_absent) 
336         as_bad ("missing operand");
337
338       numops++;
339       p = input_line_pointer;
340     }
341
342   switch (post) 
343     {
344     case -1:    /* postdecrement mode */
345       exp[numops].X_op = O_absent;
346       exp[numops++].X_add_number = OPERAND_MINUS;
347       break;
348     case 1:     /* postincrement mode */
349       exp[numops].X_op = O_absent;
350       exp[numops++].X_add_number = OPERAND_PLUS;
351       break;
352     }
353
354   exp[numops].X_op = 0;
355   return (numops);
356 }
357
358 static unsigned long
359 d10v_insert_operand (insn, op_type, value, left) 
360      unsigned long insn;
361      int op_type;
362      offsetT value;
363      int left;
364 {
365   int shift, bits;
366
367   shift = d10v_operands[op_type].shift;
368   if (left)
369     shift += 15;
370
371   bits = d10v_operands[op_type].bits;
372   /* truncate to the proper number of bits */
373   /* FIXME: overflow checking here? */
374   value &= 0x7FFFFFFF >> (31 - bits);
375   insn |= (value << shift);
376
377   return insn;
378 }
379
380
381 /* build_insn takes a pointer to the opcode entry in the opcode table
382    and the array of operand expressions and returns the instruction */
383
384 static unsigned long
385 build_insn (opcode, opers) 
386      struct d10v_opcode *opcode;
387      expressionS *opers;
388 {
389   int i, bits, shift, flags;
390   unsigned long insn;
391   unsigned int number;
392   insn = opcode->opcode;
393
394   for (i=0;opcode->operands[i];i++) 
395     {
396       flags = d10v_operands[opcode->operands[i]].flags;
397       bits = d10v_operands[opcode->operands[i]].bits;
398       shift = d10v_operands[opcode->operands[i]].shift;
399       number = opers[i].X_add_number;
400
401       if (flags & OPERAND_REG) 
402         {
403           number &= REGISTER_MASK;
404           if (opcode->format == LONG_L)
405             shift += 15;
406         }
407
408       if (opers[i].X_op != O_register && opers[i].X_op != O_constant) 
409         {
410           /* now create a fixup */
411
412           /*
413           printf("need a fixup: ");
414           print_expr_1(stdout,&opers[i]);
415           printf("\n");
416           */
417
418           if (fixups->fc >= MAX_INSN_FIXUPS)
419             as_fatal ("too many fixups");
420           fixups->fix[fixups->fc].exp = opers[i];
421
422           /* put the operand number here for now.  We can look up
423              the reloc type and/or fixup the instruction in md_apply_fix() */
424           fixups->fix[fixups->fc].reloc = opcode->operands[i];
425           (fixups->fc)++;
426         }
427
428       /* truncate to the proper number of bits */
429       /* FIXME: overflow checking here? */
430       number &= 0x7FFFFFFF >> (31 - bits);
431       insn = insn | (number << shift);
432     }
433   return insn;
434 }
435
436 /* write out a long form instruction */
437 static void
438 write_long (opcode, insn, fx) 
439      struct d10v_opcode *opcode;
440      unsigned long insn;
441      Fixups *fx;
442 {
443   int i;
444   char *f = frag_more(4);
445
446   insn |= FM11;
447   /* printf("INSN: %08x\n",insn); */
448   number_to_chars_bigendian (f, insn, 4);
449
450   for (i=0; i < fx->fc; i++) 
451     {
452       if (get_reloc((struct d10v_operand *)&d10v_operands[fx->fix[i].reloc]))
453         { 
454           /*
455           printf("fix_new_exp: where:%x size:4\n    ",f - frag_now->fr_literal);
456           print_expr_1(stdout,&(fx->fix[i].exp));
457           printf("\n");
458           */
459
460           fix_new_exp (frag_now,
461                        f - frag_now->fr_literal, 
462                        4,
463                        &(fx->fix[i].exp),
464                        1,
465                        fx->fix[i].reloc);
466         }
467     }
468   fx->fc = 0;
469 }
470
471
472 /* write out a short form instruction by itself */
473 static void
474 write_1_short (opcode, insn, fx) 
475      struct d10v_opcode *opcode;
476      unsigned long insn;
477      Fixups *fx;
478 {
479   char *f = frag_more(4);
480   int i;
481
482   insn |= FM00 | (NOP << 15);
483   /*  printf("INSN: %08x\n",insn);  */
484   number_to_chars_bigendian (f, insn, 4);
485   for (i=0; i < fx->fc; i++) 
486     {
487       if (get_reloc((struct d10v_operand *)&d10v_operands[fx->fix[i].reloc])) 
488         { 
489           /*
490           printf("fix_new_exp: where:%x size:4\n    ",f - frag_now->fr_literal);
491           print_expr_1(stdout,&(fx->fix[i].exp));
492           printf("\n");
493           */
494
495           fix_new_exp (frag_now,
496                        f - frag_now->fr_literal, 
497                        4,
498                        &(fx->fix[i].exp),
499                        1,
500                        fx->fix[i].reloc);
501         }
502     }
503   fx->fc = 0;
504 }
505
506 /* write out a short form instruction if possible */
507 /* return number of instructions not written out */
508 static int
509 write_2_short (opcode1, insn1, opcode2, insn2, exec_type, fx) 
510      struct d10v_opcode *opcode1, *opcode2;
511      unsigned long insn1, insn2;
512      int exec_type;
513      Fixups *fx;
514 {
515   unsigned long insn;
516   char *f;
517   int i,j;
518
519   if(opcode1->exec_type == BRANCH_LINK)
520     {
521       /* subroutines must be called from 32-bit boundaries */
522       /* so the return address will be correct */
523       write_1_short (opcode1, insn1, fx->next);
524       return (1);
525     }
526
527   switch (exec_type) 
528     {
529     case 0:
530       if (opcode1->unit == IU) 
531         {
532           /* reverse sequential */
533           insn = FM10 | (insn2 << 15) | insn1;
534         }
535       else
536         {
537           insn = FM01 | (insn1 << 15) | insn2;
538           fx = fx->next;  
539         }
540       break;
541     case 1:     /* parallel */
542           insn = FM00 | (insn1 << 15) | insn2;  
543           fx = fx->next;
544       break;
545     case 2:     /* sequential */
546           insn = FM01 | (insn1 << 15) | insn2;  
547           fx = fx->next;
548       break;
549     case 3:     /* reverse sequential */
550           insn = FM10 | (insn1 << 15) | insn2;  
551       break;
552     default:
553       as_fatal("unknown execution type passed to write_2_short()");
554     }
555
556   /*  printf("INSN: %08x\n",insn); */
557   f = frag_more(4);
558   number_to_chars_bigendian (f, insn, 4);
559
560 for (j=0; j<2; j++) 
561   {
562     bfd_reloc_code_real_type reloc;
563     for (i=0; i < fx->fc; i++) 
564       {
565         reloc = get_reloc((struct d10v_operand *)&d10v_operands[fx->fix[i].reloc]);
566         if (reloc)
567           {
568             if ( (reloc == BFD_RELOC_D10V_10_PCREL_R) && (j == 0) )
569               fx->fix[i].reloc |= 1024;
570             
571             /*
572             printf("fix_new_exp: where:%x reloc:%d\n    ",f - frag_now->fr_literal,fx->fix[i].reloc);
573             print_expr_1(stdout,&(fx->fix[i].exp));
574             printf("\n");
575             */
576             fix_new_exp (frag_now,
577                          f - frag_now->fr_literal, 
578                          4,
579                          &(fx->fix[i].exp),
580                          1,
581                          fx->fix[i].reloc);
582           }
583       }
584     fx->fc = 0;
585     fx = fx->next;
586   }
587
588   return (0);
589 }
590
591
592 /* This is the main entry point for the machine-dependent assembler.  str points to a
593    machine-dependent instruction.  This function is supposed to emit the frags/bytes 
594    it assembles to.  For the D10V, it mostly handles the special VLIW parsing and packing
595    and leaves the difficult stuff to do_assemble().
596  */
597
598 static unsigned long prev_insn;
599 static struct d10v_opcode *prev_opcode = 0;
600 static subsegT prev_subseg;
601 static segT prev_seg;
602
603 void
604 md_assemble (str)
605      char *str;
606 {
607   struct d10v_opcode *opcode;
608   unsigned long insn;
609   int t=0;
610   char *str2;
611
612   /*  printf("md_assemble: str=%s\n",str); */
613
614   /* look for the special multiple instruction seperators */
615   str2 = strstr (str, "||");
616   if (str2) 
617     t = 1;
618   else
619     {
620       str2 = strstr (str, "->");
621       if (str2) 
622         t = 2;
623       else
624         {
625           str2 = strstr (str, "<-");
626           if (str2) 
627             t = 3;
628         }
629     }
630
631
632   /* str2 points to the seperator, if one */
633   if (str2) 
634     {
635       *str2 = 0;
636
637       /* if two instructions are present and we already have one saved
638          then first write it out */
639       if (prev_opcode) 
640           write_1_short (prev_opcode, prev_insn, fixups->next);
641       
642       /* assemble first instruction and save it */
643       prev_insn = do_assemble (str, &prev_opcode);
644       fixups = fixups->next;
645       str = str2 + 2;
646     }
647
648   insn = do_assemble (str, &opcode);
649
650   /* if this is a long instruction, write it and any previous short instruction */
651   if (opcode->format & LONG_OPCODE) 
652     {
653       if (t) 
654         as_fatal("Unable to mix instructions as specified");
655       if (prev_opcode) 
656         {
657           write_1_short (prev_opcode, prev_insn, fixups->next);
658           prev_opcode = NULL;
659         }
660       write_long (opcode, insn, fixups);
661       prev_opcode = NULL;
662       return;
663     }
664   
665   if (prev_opcode && (write_2_short (prev_opcode, prev_insn, opcode, insn, t, fixups) == 0)) 
666     {
667       /* no instructions saved */
668       prev_opcode = NULL;
669     }
670   else
671     {
672       if (t) 
673         as_fatal("Unable to mix instructions as specified");
674       /* save off last instruction so it may be packed on next pass */
675       prev_opcode = opcode;
676       prev_insn = insn;
677       prev_seg = now_seg;
678       prev_subseg = now_subseg;
679       fixups = fixups->next;
680     }
681 }
682
683
684 static unsigned long
685 do_assemble (str, opcode) 
686      char *str;
687      struct d10v_opcode **opcode;
688 {
689   struct d10v_opcode *next_opcode;
690   unsigned char *op_start, *save;
691   unsigned char *op_end;
692   char name[20];
693   int nlen = 0, i, match, numops;
694   expressionS myops[6];
695   unsigned long insn;
696
697   /* printf("do_assemble: str=%s\n",str); */
698
699   /* Drop leading whitespace */
700   while (*str == ' ')
701     str++;
702
703   /* find the opcode end */
704   for (op_start = op_end = (unsigned char *) (str);
705        *op_end
706        && nlen < 20
707        && !is_end_of_line[*op_end] && *op_end != ' ';
708        op_end++)
709     {
710       name[nlen] = op_start[nlen];
711       nlen++;
712     }
713   name[nlen] = 0;
714
715   if (nlen == 0)
716       as_bad ("can't find opcode ");
717   
718   /* find the first opcode with the proper name */
719   *opcode = (struct d10v_opcode *)hash_find (d10v_hash, name);
720   if (*opcode == NULL)
721     {
722       as_bad ("unknown opcode");
723       return;
724     }
725
726   save = input_line_pointer;
727   input_line_pointer = op_end;
728
729   /* get all the operands and save them as expressions */
730   numops = get_operands (myops);
731
732   /* now search the opcode table table for one with operands */
733   /* that match what we've got */
734   do
735     {
736       match = 1;
737       for (i = 0; (*opcode)->operands[i]; i++) 
738         {
739           int flags = d10v_operands[(*opcode)->operands[i]].flags;
740
741           if (myops[i].X_op==0) 
742             {
743               match=0;
744               break;
745             }
746
747           if (flags & OPERAND_REG) 
748             {
749               if ((myops[i].X_op != O_register) ||
750                   ((flags & OPERAND_ACC) != (myops[i].X_add_number & OPERAND_ACC)) ||
751                   ((flags & OPERAND_FLAG) != (myops[i].X_add_number & OPERAND_FLAG)) ||
752                   ((flags & OPERAND_CONTROL) != (myops[i].X_add_number & OPERAND_CONTROL)))
753                 {
754                   match=0;
755                   break;
756                 }         
757             }
758
759           if (((flags & OPERAND_MINUS) && ((myops[i].X_op != O_absent) || (myops[i].X_add_number != OPERAND_MINUS))) ||
760               ((flags & OPERAND_PLUS) && ((myops[i].X_op != O_absent) || (myops[i].X_add_number != OPERAND_PLUS))) ||
761               ((flags & OPERAND_ATMINUS) && ((myops[i].X_op != O_absent) || (myops[i].X_add_number != OPERAND_ATMINUS))) ||
762               ((flags & OPERAND_ATPAR) && ((myops[i].X_op != O_absent) || (myops[i].X_add_number != OPERAND_ATPAR))) ||
763               ((flags & OPERAND_ATSIGN) && ((myops[i].X_op != O_absent) || (myops[i].X_add_number != OPERAND_ATSIGN)))) 
764             {
765               match=0;
766               break;
767             }
768         }
769
770       /* we're only done if the operands matched AND there
771          are no more to check */
772       if (match && myops[i].X_op==0) 
773         break;
774
775       next_opcode = (*opcode)+1;
776       if (next_opcode->opcode == 0) 
777         break;
778       if (strcmp(next_opcode->name, (*opcode)->name))
779           break;
780       (*opcode) = next_opcode;
781     } while (!match);
782
783   if (!match)  
784     {
785       as_bad ("bad opcode or operands");
786       return (0);
787     }
788
789   /* Check that all registers that are required to be even are. */
790   /* Also, if any operands were marked as registers, but were really symbols */
791   /* fix that here. */
792   for (i=0; (*opcode)->operands[i]; i++) 
793     {
794       if ((d10v_operands[(*opcode)->operands[i]].flags & OPERAND_EVEN) &&
795           (myops[i].X_add_number & 1)) 
796         as_fatal("Register number must be EVEN");
797       if (myops[i].X_op == O_register)
798         {
799           if (!(d10v_operands[(*opcode)->operands[i]].flags & OPERAND_REG)) 
800             {
801               myops[i].X_op = O_symbol;
802               myops[i].X_add_symbol = symbol_find_or_make ((char *)myops[i].X_op_symbol);
803               myops[i].X_add_number = 0;
804               myops[i].X_op_symbol = NULL;
805               /* FIXME create a fixup */
806             }
807         }
808     }
809   
810   input_line_pointer = save;
811
812   /* at this point, we have "opcode" pointing to the opcode entry in the
813      d10v opcode table, with myops filled out with the operands. */
814   insn = build_insn ((*opcode), myops); 
815   /*  printf("sub-insn = %lx\n",insn); */
816
817   return (insn);
818 }
819
820
821 /* if while processing a fixup, a reloc really needs to be created */
822 /* then it is done here */
823                  
824 arelent *
825 tc_gen_reloc (seg, fixp)
826      asection *seg;
827      fixS *fixp;
828 {
829   arelent *reloc;
830   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
831   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
832   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
833   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
834   if (reloc->howto == (reloc_howto_type *) NULL)
835     {
836       as_bad_where (fixp->fx_file, fixp->fx_line,
837                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
838       return NULL;
839     }
840   reloc->addend = fixp->fx_addnumber;
841   /*  printf("tc_gen_reloc: addr=%x  addend=%x\n", reloc->address, reloc->addend); */
842   return reloc;
843 }
844
845 int
846 md_estimate_size_before_relax (fragp, seg)
847      fragS *fragp;
848      asection *seg;
849 {
850   abort ();
851   return 0;
852
853
854 long
855 md_pcrel_from_section (fixp, sec)
856      fixS *fixp;
857      segT sec;
858 {
859     return 0;
860     /*  return fixp->fx_frag->fr_address + fixp->fx_where; */
861 }
862
863 int
864 md_apply_fix3 (fixp, valuep, seg)
865      fixS *fixp;
866      valueT *valuep;
867      segT seg;
868 {
869   valueT value;
870   char *where;
871   unsigned long insn;
872   int op_type;
873   int left=0;
874
875   if (fixp->fx_addsy == (symbolS *) NULL)
876     {
877       value = *valuep;
878       fixp->fx_done = 1;
879     }
880   else if (fixp->fx_pcrel)
881     value = *valuep;
882   else
883     {
884       value = fixp->fx_offset;
885       if (fixp->fx_subsy != (symbolS *) NULL)
886         {
887           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
888             value -= S_GET_VALUE (fixp->fx_subsy);
889           else
890             {
891               /* We don't actually support subtracting a symbol.  */
892               as_bad_where (fixp->fx_file, fixp->fx_line,
893                             "expression too complex");
894             }
895         }
896     }
897   
898   /* printf("md_apply_fix: value=0x%x  type=%d\n",  value, fixp->fx_r_type);  */
899
900   op_type = fixp->fx_r_type;
901   if (op_type & 1024)
902     {
903       op_type -= 1024;
904       fixp->fx_r_type = BFD_RELOC_D10V_10_PCREL_L;
905       left = 1;
906     }
907   else
908     fixp->fx_r_type = get_reloc((struct d10v_operand *)&d10v_operands[op_type]); 
909
910   /* Fetch the instruction, insert the fully resolved operand
911      value, and stuff the instruction back again.  */
912   where = fixp->fx_frag->fr_literal + fixp->fx_where;
913   insn = bfd_getb32 ((unsigned char *) where);
914   /* printf("   insn=%x  value=%x\n",insn,value);  */
915
916   insn = d10v_insert_operand (insn, op_type, (offsetT) value, left);
917   
918   /* printf("   new insn=%x\n",insn); */
919   
920   bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
921   
922   if (fixp->fx_done)
923     return 1;
924
925   fixp->fx_addnumber = value;
926   return 1;
927 }
928
929
930 /* d10v_cleanup() is called after the assembler has finished parsing the input 
931    file or after a label is defined.  Because the D10V assembler sometimes saves short 
932    instructions to see if it can package them with the next instruction, there may
933    be a short instruction that still needs written.  */
934 int
935 d10v_cleanup()
936 {
937   segT seg;
938   subsegT subseg;
939
940   if (prev_opcode) 
941     {
942       seg = now_seg;
943       subseg = now_subseg;
944       subseg_set (prev_seg, prev_subseg);
945       write_1_short (prev_opcode, prev_insn, fixups);
946       subseg_set (seg, subseg);
947       prev_opcode = NULL;
948     }
949   return 1;
950 }