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