1 /* tc-tic80.c -- Assemble for the TI TMS320C80 (MV)
2    Copyright 1996, 1997, 2000, 2001, 2002 Free Software Foundation, Inc.
3 
4    This file is part of GAS, the GNU Assembler.
5 
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 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 the Free
18    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20 
21 #include "as.h"
22 #include "safe-ctype.h"
23 #include "opcode/tic80.h"
24 
25 #define internal_error(what) \
26   as_fatal (_("internal error:%s:%d: %s\n"), __FILE__, __LINE__, what)
27 
28 #define internal_error_a(what,arg) \
29   as_fatal (_("internal error:%s:%d: %s %ld\n"), __FILE__, __LINE__, what, arg)
30 
31 /* Generic assembler global variables which must be defined by all
32    targets.  */
33 
34 /* Characters which always start a comment.  */
35 const char comment_chars[] = ";";
36 
37 /* Characters which start a comment at the beginning of a line.  */
38 const char line_comment_chars[] = ";*#";
39 
40 /* Characters which may be used to separate multiple commands on a single
41    line. The semicolon is such a character by default and should not be
42    explicitly listed.  */
43 const char line_separator_chars[] = "";
44 
45 /* Characters which are used to indicate an exponent in a floating
46    point number.  */
47 const char EXP_CHARS[] = "eE";
48 
49 /* Characters which mean that a number is a floating point constant,
50    as in 0f1.0.  */
51 const char FLT_CHARS[] = "fF";
52 
53 /* This table describes all the machine specific pseudo-ops the assembler
54    has to support.  The fields are:
55 
56    pseudo-op name without dot
57    function to call to execute this pseudo-op
58    integer arg to pass to the function  */
59 
60 const pseudo_typeS md_pseudo_table[] = {
61   { "align",	s_align_bytes,		4 },	/* Do byte alignment, default is a 4 byte boundary  */
62   { "word",	cons,			4 },	/* FIXME: Should this be machine independent?  */
63   { "bss",	s_lcomm_bytes,		1 },
64   { "sect",	obj_coff_section,	0},	/* For compatibility with TI tools  */
65   { "section",	obj_coff_section,	0},	/* Standard COFF .section pseudo-op  */
66   { NULL,	NULL,			0 }
67 };
68 
69 /* Opcode hash table.  */
70 static struct hash_control *tic80_hash;
71 
72 static struct tic80_opcode * find_opcode PARAMS ((struct tic80_opcode *, expressionS []));
73 static void build_insn PARAMS ((struct tic80_opcode *, expressionS *));
74 static int get_operands PARAMS ((expressionS exp[]));
75 static int const_overflow PARAMS ((unsigned long num, int bits, int flags));
76 
77 /* Replace short PC relative instructions with long form when
78    necessary.  Currently this is off by default or when given the
79    -no-relax option.  Turning it on by using the -relax option forces
80    all PC relative instructions to use the long form, which is why it
81    is currently not the default.  */
82 static int tic80_relax = 0;
83 
84 int
85 md_estimate_size_before_relax (fragP, segment_type)
86      fragS *fragP ATTRIBUTE_UNUSED;
87      segT segment_type ATTRIBUTE_UNUSED;
88 {
89   internal_error (_("Relaxation is a luxury we can't afford"));
90   return (-1);
91 }
92 
93 /* We have no need to default values of symbols.  */
94 
95 symbolS *
96 md_undefined_symbol (name)
97      char *name ATTRIBUTE_UNUSED;
98 {
99   return 0;
100 }
101 
102 /* Turn a string in input_line_pointer into a floating point constant
103    of type TYPE, and store the appropriate bytes in *LITP.  The number
104    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
105    returned, or NULL on OK.  */
106 
107 #define MAX_LITTLENUMS 4
108 
109 char *
110 md_atof (type, litP, sizeP)
111      int type;
112      char *litP;
113      int *sizeP;
114 {
115   int prec;
116   LITTLENUM_TYPE words[MAX_LITTLENUMS];
117   LITTLENUM_TYPE *wordP;
118   char *t;
119 
120   switch (type)
121     {
122     case 'f':
123     case 'F':
124     case 's':
125     case 'S':
126       prec = 2;
127       break;
128 
129     case 'd':
130     case 'D':
131     case 'r':
132     case 'R':
133       prec = 4;
134       break;
135 
136     default:
137       *sizeP = 0;
138       return _("bad call to md_atof ()");
139     }
140 
141   t = atof_ieee (input_line_pointer, type, words);
142   if (t)
143     {
144       input_line_pointer = t;
145     }
146 
147   *sizeP = prec * sizeof (LITTLENUM_TYPE);
148 
149   for (wordP = words; prec--;)
150     {
151       md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
152       litP += sizeof (LITTLENUM_TYPE);
153     }
154   return (NULL);
155 }
156 
157 /* Check to see if the constant value in NUM will fit in a field of
158    width BITS if it has flags FLAGS.  */
159 
160 static int
161 const_overflow (num, bits, flags)
162      unsigned long num;
163      int bits;
164      int flags;
165 {
166   long min, max;
167   int retval = 0;
168 
169   /* Only need to check fields less than 32 bits wide.  */
170   if (bits >= 32)
171     return retval;
172 
173   if (flags & TIC80_OPERAND_SIGNED)
174     {
175       max = (1 << (bits - 1)) - 1;
176       min = - (1 << (bits - 1));
177       retval = (long) num > max || (long) num < min;
178     }
179   else
180     {
181       max = (1 << bits) - 1;
182       retval = num > (unsigned long) max;
183     }
184   return retval;
185 }
186 
187 /* get_operands () parses a string of operands and fills in a passed
188    array of expressions in EXP.
189 
190    Note that we use O_absent expressions to record additional information
191    about the previous non-O_absent expression, such as ":m" or ":s"
192    modifiers or register numbers enclosed in parens like "(r10)".
193 
194    Returns the number of expressions that were placed in EXP.  */
195 
196 static int
197 get_operands (exp)
198      expressionS exp[];
199 {
200   char *p = input_line_pointer;
201   int numexp = 0;
202   int parens = 0;
203 
204   while (*p)
205     {
206       /* Skip leading whitespace.  */
207       while (*p == ' ' || *p == '\t' || *p == ',')
208 	p++;
209 
210       /* Check to see if we have any operands left to parse.  */
211       if (*p == 0 || *p == '\n' || *p == '\r')
212 	break;
213 
214       /* Notice scaling or direct memory operand modifiers and save them in
215 	 an O_absent expression after the expression that they modify.  */
216 
217       if (*p == ':')
218 	{
219 	  p++;
220 	  exp[numexp].X_op = O_absent;
221 	  if (*p == 'm')
222 	    {
223 	      p++;
224 	      /* This is a ":m" modifier.  */
225 	      exp[numexp].X_add_number = TIC80_OPERAND_M_SI | TIC80_OPERAND_M_LI;
226 	    }
227 	  else if (*p == 's')
228 	    {
229 	      p++;
230 	      /* This is a ":s" modifier.  */
231 	      exp[numexp].X_add_number = TIC80_OPERAND_SCALED;
232 	    }
233 	  else
234 	    {
235 	      as_bad (_("':' not followed by 'm' or 's'"));
236 	    }
237 	  numexp++;
238 	  continue;
239 	}
240 
241       /* Handle leading '(' on operands that use them, by recording that we
242 	 have entered a paren nesting level and then continuing.  We complain
243 	 about multiple nesting.  */
244 
245       if (*p == '(')
246 	{
247 	  if (++parens != 1)
248 	    as_bad (_("paren nesting"));
249 
250 	  p++;
251 	  continue;
252 	}
253 
254       /* Handle trailing ')' on operands that use them, by reducing the
255 	 nesting level and then continuing.  We complain if there were too
256 	 many closures.  */
257 
258       if (*p == ')')
259 	{
260 	  /* Record that we have left a paren group and continue.  */
261 	  if (--parens < 0)
262 	    as_bad (_("mismatched parenthesis"));
263 
264 	  p++;
265 	  continue;
266 	}
267 
268       /* Begin operand parsing at the current scan point.  */
269 
270       input_line_pointer = p;
271       expression (&exp[numexp]);
272 
273       if (exp[numexp].X_op == O_illegal)
274 	{
275 	  as_bad (_("illegal operand"));
276 	}
277       else if (exp[numexp].X_op == O_absent)
278 	{
279 	  as_bad (_("missing operand"));
280 	}
281 
282       numexp++;
283       p = input_line_pointer;
284     }
285 
286   if (parens)
287     {
288       exp[numexp].X_op = O_absent;
289       exp[numexp++].X_add_number = TIC80_OPERAND_PARENS;
290     }
291 
292   /* Mark the end of the valid operands with an illegal expression.  */
293   exp[numexp].X_op = O_illegal;
294 
295   return (numexp);
296 }
297 
298 /* find_opcode() gets a pointer to the entry in the opcode table that
299    matches the instruction being assembled, or returns NULL if no such match
300    is found.
301 
302    First it parses all the operands and save them as expressions.  Note that
303    we use O_absent expressions to record additional information about the
304    previous non-O_absent expression, such as ":m" or ":s" modifiers or
305    register numbers enclosed in parens like "(r10)".
306 
307    It then looks at all opcodes with the same name and uses the operands to
308    choose the correct opcode.  */
309 
310 static struct tic80_opcode *
311 find_opcode (opcode, myops)
312      struct tic80_opcode *opcode;
313      expressionS myops[];
314 {
315   int numexp;				/* Number of expressions from parsing operands */
316   int expi;				/* Index of current expression to match */
317   int opi;				/* Index of current operand to match */
318   int match = 0;			/* Set to 1 when an operand match is found */
319   struct tic80_opcode *opc = opcode;	/* Pointer to current opcode table entry */
320   const struct tic80_opcode *end;	/* Pointer to end of opcode table */
321 
322   /* First parse all the operands so we only have to do it once.  There may
323      be more expressions generated than there are operands.  */
324 
325   numexp = get_operands (myops);
326 
327   /* For each opcode with the same name, try to match it against the parsed
328      operands.  */
329 
330   end = tic80_opcodes + tic80_num_opcodes;
331   while (!match && (opc < end) && (strcmp (opc->name, opcode->name) == 0))
332     {
333       /* Start off assuming a match.  If we find a mismatch, then this is
334 	 reset and the operand/expr matching loop terminates with match
335 	 equal to zero, which allows us to try the next opcode.  */
336 
337       match = 1;
338 
339       /* For each expression, try to match it against the current operand
340 	 for the current opcode.  Upon any mismatch, we abandon further
341 	 matching for the current opcode table entry.  */
342 
343       for (expi = 0, opi = -1; (expi < numexp) && match; expi++)
344 	{
345 	  int bits, flags, X_op, num;
346 
347 	  X_op = myops[expi].X_op;
348 	  num = myops[expi].X_add_number;
349 
350 	  /* The O_absent expressions apply to the same operand as the most
351 	     recent non O_absent expression.  So only increment the operand
352 	     index when the current expression is not one of these special
353 	     expressions.  */
354 
355 	  if (X_op != O_absent)
356 	    {
357 	      opi++;
358 	    }
359 
360 	  flags = tic80_operands[opc->operands[opi]].flags;
361 	  bits  = tic80_operands[opc->operands[opi]].bits;
362 
363 	  switch (X_op)
364 	    {
365 	    case O_register:
366 	      /* Also check that registers that are supposed to be
367 		 even actually are even.  */
368 	      if (((flags & TIC80_OPERAND_GPR) != (num & TIC80_OPERAND_GPR)) ||
369 		  ((flags & TIC80_OPERAND_FPA) != (num & TIC80_OPERAND_FPA)) ||
370 		  ((flags & TIC80_OPERAND_CR) != (num & TIC80_OPERAND_CR)) ||
371 		  ((flags & TIC80_OPERAND_EVEN) && (num & 1)) ||
372 		  const_overflow (num & ~TIC80_OPERAND_MASK, bits, flags))
373 		{
374 		  match = 0;
375 		}
376 	      break;
377 	    case O_constant:
378 	      if ((flags & TIC80_OPERAND_ENDMASK) && (num == 32))
379 		{
380 		  /* Endmask values of 0 and 32 give identical
381                      results.  */
382 		  num = 0;
383 		}
384 	      if ((flags & (TIC80_OPERAND_FPA | TIC80_OPERAND_GPR)) ||
385 		  const_overflow (num, bits, flags))
386 		{
387 		  match = 0;
388 		}
389 	      break;
390 	    case O_symbol:
391 	      if ((bits < 32) && (flags & TIC80_OPERAND_PCREL)
392 		  && !tic80_relax)
393 		{
394 		  /* The default is to prefer the short form of PC
395 		     relative relocations.  This is the only form that
396 		     the TI assembler supports.  If the -relax option
397 		     is given, we never use the short forms.
398 		     FIXME: Should be able to choose "best-fit".  */
399 		}
400 	      else if ((bits == 32)
401 #if 0
402 		       && (flags & TIC80_OPERAND_BASEREL)
403 #endif
404 		       )
405 		{
406 		  /* The default is to prefer the long form of base
407 		     relative relocations.  This is the only form that
408 		     the TI assembler supports.  If the -no-relax
409 		     option is given, we always use the long form of
410 		     PC relative relocations.
411 		     FIXME: Should be able to choose "best-fit".  */
412 		}
413 	      else
414 		{
415 		  /* Symbols that don't match one of the above cases are
416 		     rejected as an operand.  */
417 		  match = 0;
418 		}
419 	      break;
420 	    case O_absent:
421 	      /* If this is an O_absent expression, then it may be an
422 		 expression that supplies additional information about
423 		 the operand, such as ":m" or ":s" modifiers. Check to
424 		 see that the operand matches this requirement.  */
425 	      if (!((num & flags & TIC80_OPERAND_M_SI)
426 		    || (num & flags & TIC80_OPERAND_M_LI)
427 		    || (num & flags & TIC80_OPERAND_SCALED)))
428 		{
429 		  match = 0;
430 		}
431 	      break;
432 	    case O_big:
433 	      if ((num > 0) || !(flags & TIC80_OPERAND_FLOAT))
434 		{
435 		  match = 0;
436 		}
437 	      break;
438 	    case O_illegal:
439 	    case O_symbol_rva:
440 	    case O_uminus:
441 	    case O_bit_not:
442 	    case O_logical_not:
443 	    case O_multiply:
444 	    case O_divide:
445 	    case O_modulus:
446 	    case O_left_shift:
447 	    case O_right_shift:
448 	    case O_bit_inclusive_or:
449 	    case O_bit_or_not:
450 	    case O_bit_exclusive_or:
451 	    case O_bit_and:
452 	    case O_add:
453 	    case O_subtract:
454 	    case O_eq:
455 	    case O_ne:
456 	    case O_lt:
457 	    case O_le:
458 	    case O_ge:
459 	    case O_gt:
460 	    case O_logical_and:
461 	    case O_logical_or:
462 	    case O_max:
463 	    default:
464 	      internal_error_a (_("unhandled expression type"), (long) X_op);
465 	    }
466 	}
467       if (!match)
468 	opc++;
469     }
470 
471   return (match ? opc : NULL);
472 
473 #if 0
474   /* Now search the opcode table table for one with operands that
475      matches what we've got.  */
476 
477   while (!match)
478     {
479       match = 1;
480       for (i = 0; opcode->operands[i]; i++)
481 	{
482 	  int flags = tic80_operands[opcode->operands[i]].flags;
483 	  int X_op = myops[i].X_op;
484 	  int num = myops[i].X_add_number;
485 
486 	  if (X_op == 0)
487 	    {
488 	      match = 0;
489 	      break;
490 	    }
491 
492 	  if (flags
493 	      & (TIC80_OPERAND_GPR | TIC80_OPERAND_FPA | TIC80_OPERAND_CR))
494 	    {
495 	      if ((X_op != O_register) ||
496 		  ((flags & TIC80_OPERAND_GPR) != (num & TIC80_OPERAND_GPR)) ||
497 		  ((flags & TIC80_OPERAND_FPA) != (num & TIC80_OPERAND_FPA)) ||
498 		  ((flags & TIC80_OPERAND_CR) != (num & TIC80_OPERAND_CR)))
499 		{
500 		  match = 0;
501 		  break;
502 		}
503 	    }
504 
505 	  if (((flags & TIC80_OPERAND_MINUS) && ((X_op != O_absent) || (num != TIC80_OPERAND_MINUS))) ||
506 	      ((flags & TIC80_OPERAND_PLUS) && ((X_op != O_absent) || (num != TIC80_OPERAND_PLUS))) ||
507 	      ((flags & TIC80_OPERAND_ATMINUS) && ((X_op != O_absent) || (num != TIC80_OPERAND_ATMINUS))) ||
508 	      ((flags & TIC80_OPERAND_ATPAR) && ((X_op != O_absent) || (num != TIC80_OPERAND_ATPAR))) ||
509 	      ((flags & TIC80_OPERAND_ATSIGN) && ((X_op != O_absent) || (num != TIC80_OPERAND_ATSIGN))))
510 	    {
511 	      match = 0;
512 	      break;
513 	    }
514 	}
515       /* We're only done if the operands matched so far AND there
516 	 are no more to check.  */
517       if (match && myops[i].X_op == 0)
518 	break;
519       else
520 	match = 0;
521 
522       next_opcode = opcode + 1;
523       if (next_opcode->opcode == 0)
524 	break;
525       if (strcmp (next_opcode->name, opcode->name))
526 	break;
527       opcode = next_opcode;
528     }
529 
530   if (!match)
531     {
532       as_bad (_("bad opcode or operands"));
533       return (0);
534     }
535 
536   /* Check that all registers that are required to be even are.
537      Also, if any operands were marked as registers, but were really
538      symbols, fix that here.  */
539   for (i = 0; opcode->operands[i]; i++)
540     {
541       if ((tic80_operands[opcode->operands[i]].flags & TIC80_OPERAND_EVEN)
542 	  && (myops[i].X_add_number & 1))
543 	as_fatal (_("Register number must be EVEN"));
544       if (myops[i].X_op == O_register)
545 	{
546 	  if (!(tic80_operands[opcode->operands[i]].flags & TIC80_OPERAND_REG))
547 	    {
548 	      myops[i].X_op = O_symbol;
549 	      myops[i].X_add_symbol =
550 		symbol_find_or_make ((char *) myops[i].X_op_symbol);
551 	      myops[i].X_add_number = 0;
552 	      myops[i].X_op_symbol = NULL;
553 	    }
554 	}
555     }
556 #endif
557 }
558 
559 /* build_insn takes a pointer to the opcode entry in the opcode table
560    and the array of operand expressions and writes out the instruction.
561 
562    Note that the opcode word and extended word may be written to different
563    frags, with the opcode at the end of one frag and the extension at the
564    beginning of the next.  */
565 
566 static void
567 build_insn (opcode, opers)
568      struct tic80_opcode *opcode;
569      expressionS *opers;
570 {
571   int expi;				/* Index of current expression to match */
572   int opi;				/* Index of current operand to match */
573   unsigned long insn[2];		/* Instruction and long immediate (if any) */
574   char *f;				/* Pointer to frag location for insn[0] */
575   fragS *ffrag;				/* Frag containing location f */
576   char *fx = NULL;			/* Pointer to frag location for insn[1] */
577   fragS *fxfrag;			/* Frag containing location fx */
578 
579   /* Start with the raw opcode bits from the opcode table.  */
580   insn[0] = opcode->opcode;
581 
582   /* We are going to insert at least one 32 bit opcode so get the
583      frag now.  */
584 
585   f = frag_more (4);
586   ffrag = frag_now;
587 
588   /* For each operand expression, insert the appropriate bits into the
589      instruction.  */
590   for (expi = 0, opi = -1; opers[expi].X_op != O_illegal; expi++)
591     {
592       int bits, shift, flags, X_op, num;
593 
594       X_op = opers[expi].X_op;
595       num = opers[expi].X_add_number;
596 
597       /* The O_absent expressions apply to the same operand as the most
598 	 recent non O_absent expression.  So only increment the operand
599 	 index when the current expression is not one of these special
600 	 expressions.  */
601 
602       if (X_op != O_absent)
603 	{
604 	  opi++;
605 	}
606 
607       flags = tic80_operands[opcode->operands[opi]].flags;
608       bits  = tic80_operands[opcode->operands[opi]].bits;
609       shift = tic80_operands[opcode->operands[opi]].shift;
610 
611       switch (X_op)
612 	{
613 	case O_register:
614 	  num &= ~TIC80_OPERAND_MASK;
615 	  insn[0] = insn[0] | (num << shift);
616 	  break;
617 	case O_constant:
618 	  if ((flags & TIC80_OPERAND_ENDMASK) && (num == 32))
619 	    {
620 	      /* Endmask values of 0 and 32 give identical results.  */
621 	      num = 0;
622 	    }
623 	  else if ((flags & TIC80_OPERAND_BITNUM))
624 	    {
625 	      /* BITNUM values are stored in one's complement form.  */
626 	      num = (~num & 0x1F);
627 	    }
628 	  /* Mask off upper bits, just it case it is signed and is
629 	     negative.  */
630 	  if (bits < 32)
631 	    {
632 	      num &= (1 << bits) - 1;
633 	      insn[0] = insn[0] | (num << shift);
634 	    }
635 	  else
636 	    {
637 	      fx = frag_more (4);
638 	      fxfrag = frag_now;
639 	      insn[1] = num;
640 	    }
641 	  break;
642 	case O_symbol:
643 	  if (bits == 32)
644 	    {
645 	      fx = frag_more (4);
646 	      fxfrag = frag_now;
647 	      insn[1] = 0;
648 	      if (flags & TIC80_OPERAND_PCREL)
649 		{
650 		  fix_new_exp (fxfrag,
651 			       fx - (fxfrag->fr_literal),
652 			       4,
653 			       &opers[expi],
654 			       1,
655 			       R_MPPCR);
656 		}
657 	      else
658 		{
659 		  fix_new_exp (fxfrag,
660 			       fx - (fxfrag->fr_literal),
661 			       4,
662 			       &opers[expi],
663 			       0,
664 			       R_RELLONGX);
665 		}
666 	    }
667 	  else if (flags & TIC80_OPERAND_PCREL)
668 	    {
669 	      fix_new_exp (ffrag,
670 			   f - (ffrag->fr_literal),
671 			   4,			/* FIXME! how is this used?  */
672 			   &opers[expi],
673 			   1,
674 			   R_MPPCR15W);
675 	    }
676 	  else
677 	    {
678 	      internal_error (_("symbol reloc that is not PC relative or 32 bits"));
679 	    }
680 	  break;
681 	case O_absent:
682 	  /* Each O_absent expression can indicate exactly one
683              possible modifier.  */
684 	  if ((num & TIC80_OPERAND_M_SI)
685 	      && (flags & TIC80_OPERAND_M_SI))
686 	    {
687 	      insn[0] = insn[0] | (1 << 17);
688 	    }
689 	  else if ((num & TIC80_OPERAND_M_LI)
690 		   && (flags & TIC80_OPERAND_M_LI))
691 	    {
692 	      insn[0] = insn[0] | (1 << 15);
693 	    }
694 	  else if ((num & TIC80_OPERAND_SCALED)
695 		   && (flags & TIC80_OPERAND_SCALED))
696 	    {
697 	      insn[0] = insn[0] | (1 << 11);
698 	    }
699 	  else if ((num & TIC80_OPERAND_PARENS)
700 		   && (flags & TIC80_OPERAND_PARENS))
701 	    {
702 	      /* No code to generate, just accept and discard this
703                  expression.  */
704 	    }
705 	  else
706 	    {
707 	      internal_error_a (_("unhandled operand modifier"),
708 				(long) opers[expi].X_add_number);
709 	    }
710 	  break;
711 	case O_big:
712 	  fx = frag_more (4);
713 	  fxfrag = frag_now;
714 	  {
715 	    int precision = 2;
716 	    long exponent_bits = 8L;
717 	    LITTLENUM_TYPE words[2];
718 	    /* Value is still in generic_floating_point_number.  */
719 	    gen_to_words (words, precision, exponent_bits);
720 	    insn[1] = (words[0] << 16) | words[1];
721 	  }
722 	  break;
723 	case O_illegal:
724 	case O_symbol_rva:
725 	case O_uminus:
726 	case O_bit_not:
727 	case O_logical_not:
728 	case O_multiply:
729 	case O_divide:
730 	case O_modulus:
731 	case O_left_shift:
732 	case O_right_shift:
733 	case O_bit_inclusive_or:
734 	case O_bit_or_not:
735 	case O_bit_exclusive_or:
736 	case O_bit_and:
737 	case O_add:
738 	case O_subtract:
739 	case O_eq:
740 	case O_ne:
741 	case O_lt:
742 	case O_le:
743 	case O_ge:
744 	case O_gt:
745 	case O_logical_and:
746 	case O_logical_or:
747 	case O_max:
748 	default:
749 	  internal_error_a (_("unhandled expression"), (long) X_op);
750 	  break;
751 	}
752     }
753 
754   /* Write out the instruction, either 4 or 8 bytes.  */
755 
756   md_number_to_chars (f, insn[0], 4);
757   if (fx != NULL)
758     {
759       md_number_to_chars (fx, insn[1], 4);
760     }
761 }
762 
763 /* This is the main entry point for the machine-dependent assembler.  Gas
764    calls this function for each input line which does not contain a
765    pseudoop.
766 
767   STR points to a NULL terminated machine dependent instruction.  This
768   function is supposed to emit the frags/bytes it assembles to.  */
769 
770 void
771 md_assemble (str)
772      char *str;
773 {
774   char *scan;
775   unsigned char *input_line_save;
776   struct tic80_opcode *opcode;
777   expressionS myops[16];
778 
779   /* Ensure there is something there to assemble.  */
780   assert (str);
781 
782   /* Drop any leading whitespace.  */
783   while (ISSPACE (*str))
784     str++;
785 
786   /* Isolate the mnemonic from the rest of the string by finding the first
787      whitespace character and zapping it to a null byte.  */
788   for (scan = str; *scan != '\000' && !ISSPACE (*scan); scan++)
789     ;
790 
791   if (*scan != '\000')
792     *scan++ = '\000';
793 
794   /* Try to find this mnemonic in the hash table.  */
795   if ((opcode = (struct tic80_opcode *) hash_find (tic80_hash, str)) == NULL)
796     {
797       as_bad (_("Invalid mnemonic: '%s'"), str);
798       return;
799     }
800 
801   str = scan;
802   while (ISSPACE (*scan))
803     scan++;
804 
805   input_line_save = input_line_pointer;
806   input_line_pointer = str;
807 
808   opcode = find_opcode (opcode, myops);
809   if (opcode == NULL)
810     as_bad (_("Invalid operands: '%s'"), input_line_save);
811 
812   input_line_pointer = input_line_save;
813   build_insn (opcode, myops);
814 }
815 
816 /* This function is called once at the start of assembly, after the command
817    line arguments have been parsed and all the machine independent
818    initializations have been completed.
819 
820    It should set up all the tables, etc., that the machine dependent part of
821    the assembler will need.  */
822 
823 void
824 md_begin ()
825 {
826   char *prev_name = "";
827   register const struct tic80_opcode *op;
828   register const struct tic80_opcode *op_end;
829   const struct predefined_symbol *pdsp;
830   extern int coff_flags;			/* Defined in obj-coff.c  */
831 
832   /* Set F_AR32WR in coff_flags, which will end up in the file header
833      f_flags field.  */
834 
835   coff_flags |= F_AR32WR;	/* TIc80 is 32 bit little endian.  */
836 
837   /* Insert unique names into hash table.  The TIc80 instruction set
838      has many identical opcode names that have different opcodes based
839      on the operands.  This hash table then provides a quick index to
840      the first opcode with a particular name in the opcode table.  */
841 
842   tic80_hash = hash_new ();
843   op_end = tic80_opcodes + tic80_num_opcodes;
844   for (op = tic80_opcodes; op < op_end; op++)
845     {
846       if (strcmp (prev_name, op->name) != 0)
847 	{
848 	  prev_name = (char *) op->name;
849 	  hash_insert (tic80_hash, op->name, (char *) op);
850 	}
851     }
852 
853   /* Insert the predefined symbols into the symbol table.  We use
854      symbol_create rather than symbol_new so that these symbols don't
855      end up in the object files' symbol table.  Note that the values
856      of the predefined symbols include some upper bits that
857      distinguish the type of the symbol (register, bitnum, condition
858      code, etc) and these bits must be masked away before actually
859      inserting the values into the instruction stream.  For registers
860      we put these bits in the symbol table since we use them later and
861      there is no question that they aren't part of the register
862      number.  For constants we can't do that since the constant can be
863      any value, so they are masked off before putting them into the
864      symbol table.  */
865 
866   pdsp = NULL;
867   while ((pdsp = tic80_next_predefined_symbol (pdsp)) != NULL)
868     {
869       segT segment;
870       valueT valu;
871       int symtype;
872 
873       symtype = PDS_VALUE (pdsp) & TIC80_OPERAND_MASK;
874       switch (symtype)
875 	{
876 	case TIC80_OPERAND_GPR:
877 	case TIC80_OPERAND_FPA:
878 	case TIC80_OPERAND_CR:
879 	  segment = reg_section;
880 	  valu = PDS_VALUE (pdsp);
881 	  break;
882 	case TIC80_OPERAND_CC:
883 	case TIC80_OPERAND_BITNUM:
884 	  segment = absolute_section;
885 	  valu = PDS_VALUE (pdsp) & ~TIC80_OPERAND_MASK;
886 	  break;
887 	default:
888 	  internal_error_a (_("unhandled predefined symbol bits"),
889 			    (long) symtype);
890 	  break;
891 	}
892       symbol_table_insert (symbol_create (PDS_NAME (pdsp), segment, valu,
893 					  &zero_address_frag));
894     }
895 }
896 
897 /* The assembler adds md_shortopts to the string passed to getopt.  */
898 
899 const char *md_shortopts = "";
900 
901 /* The assembler adds md_longopts to the machine independent long options
902    that are passed to getopt.  */
903 
904 struct option md_longopts[] = {
905 
906 #define OPTION_RELAX	(OPTION_MD_BASE)
907   {"relax", no_argument, NULL, OPTION_RELAX},
908 
909 #define OPTION_NO_RELAX	(OPTION_RELAX + 1)
910   {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
911 
912   {NULL, no_argument, NULL, 0}
913 };
914 
915 size_t md_longopts_size = sizeof (md_longopts);
916 
917 /* The md_parse_option function will be called whenever getopt returns an
918    unrecognized code, presumably indicating a special code value which
919    appears in md_longopts for machine specific command line options.  */
920 
921 int
922 md_parse_option (c, arg)
923      int c;
924      char *arg ATTRIBUTE_UNUSED;
925 {
926   switch (c)
927     {
928     case OPTION_RELAX:
929       tic80_relax = 1;
930       break;
931     case OPTION_NO_RELAX:
932       tic80_relax = 0;
933       break;
934     default:
935       return (0);
936     }
937   return (1);
938 }
939 
940 /* The md_show_usage function will be called whenever a usage message is
941    printed.  It should print a description of the machine specific options
942    found in md_longopts.  */
943 
944 void
945 md_show_usage (stream)
946      FILE *stream;
947 {
948   fprintf (stream, "\
949 TIc80 options:\n\
950 -relax			alter PC relative branch instructions to use long form when needed\n\
951 -no-relax		always use short PC relative branch instructions, error on overflow\n");
952 }
953 
954 /* Attempt to simplify or even eliminate a fixup.  The return value is
955    ignored; perhaps it was once meaningful, but now it is historical.
956    To indicate that a fixup has been eliminated, set fixP->fx_done.  */
957 
958 void
959 md_apply_fix3 (fixP, valP, seg)
960      fixS *fixP;
961      valueT * valP;
962      segT seg ATTRIBUTE_UNUSED;
963 {
964   long val = * (long *) valP;
965   char *dest = fixP->fx_frag->fr_literal + fixP->fx_where;
966   int overflow;
967 
968   switch (fixP->fx_r_type)
969     {
970     case R_RELLONGX:
971       md_number_to_chars (dest, (valueT) val, 4);
972       break;
973     case R_MPPCR:
974       val >>= 2;
975       val += 1;	  /* Target address computed from inst start */
976       md_number_to_chars (dest, (valueT) val, 4);
977       break;
978     case R_MPPCR15W:
979       overflow = (val < -65536L) || (val > 65532L);
980       if (overflow)
981 	{
982 	  as_bad_where (fixP->fx_file, fixP->fx_line,
983 			_("PC offset 0x%lx outside range 0x%lx-0x%lx"),
984 			val, -65536L, 65532L);
985 	}
986       else
987 	{
988 	  val >>= 2;
989 	  *dest++ = val & 0xFF;
990 	  val >>= 8;
991 	  *dest = (*dest & 0x80) | (val & 0x7F);
992 	}
993       break;
994     case R_ABS:
995       md_number_to_chars (dest, (valueT) val, fixP->fx_size);
996       break;
997     default:
998       internal_error_a (_("unhandled relocation type in fixup"),
999 			(long) fixP->fx_r_type);
1000       break;
1001     }
1002 
1003   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1004     fixP->fx_done = 1;
1005 }
1006 
1007 /* Functions concerning relocs.  */
1008 
1009 /* The location from which a PC relative jump should be calculated,
1010    given a PC relative reloc.
1011 
1012    For the TIc80, this is the address of the 32 bit opcode containing
1013    the PC relative field.  */
1014 
1015 long
1016 md_pcrel_from (fixP)
1017      fixS *fixP;
1018 {
1019   return (fixP->fx_frag->fr_address + fixP->fx_where);
1020 }
1021 
1022 /* Called after relax() is finished.
1023  * In:	Address of frag.
1024  *	fr_type == rs_machine_dependent.
1025  *	fr_subtype is what the address relaxed to.
1026  *
1027  * Out:	Any fixSs and constants are set up.
1028  *	Caller will turn frag into a ".space 0".
1029  */
1030 
1031 void
1032 md_convert_frag (headers, seg, fragP)
1033      object_headers *headers ATTRIBUTE_UNUSED;
1034      segT seg ATTRIBUTE_UNUSED;
1035      fragS *fragP ATTRIBUTE_UNUSED;
1036 {
1037   internal_error (_("md_convert_frag() not implemented yet"));
1038   abort ();
1039 }
1040 
1041 void
1042 tc_coff_symbol_emit_hook (ignore)
1043      symbolS *ignore ATTRIBUTE_UNUSED;
1044 {
1045 }
1046 
1047 #if defined OBJ_COFF
1048 
1049 short
1050 tc_coff_fix2rtype (fixP)
1051      fixS *fixP;
1052 {
1053   return (fixP->fx_r_type);
1054 }
1055 
1056 #endif /* OBJ_COFF */
1057