1 /* tc-tic80.c -- Assemble for the TI TMS320C80 (MV)
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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
22 #include "opcode/tic80.h"
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)
30 /* Generic assembler global variables which must be defined by all targets. */
32 /* Characters which always start a comment. */
33 const char comment_chars
[] = ";";
35 /* Characters which start a comment at the beginning of a line. */
36 const char line_comment_chars
[] = ";*#";
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
41 const char line_separator_chars
[] = "";
43 /* Characters which are used to indicate an exponent in a floating
45 const char EXP_CHARS
[] = "eE";
47 /* Characters which mean that a number is a floating point constant,
49 const char FLT_CHARS
[] = "fF";
51 /* This table describes all the machine specific pseudo-ops the assembler
52 has to support. The fields are:
54 pseudo-op name without dot
55 function to call to execute this pseudo-op
56 integer arg to pass to the function */
58 extern void obj_coff_section ();
60 const pseudo_typeS md_pseudo_table
[] =
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 */
70 /* Opcode hash table. */
71 static struct hash_control
*tic80_hash
;
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
));
80 md_estimate_size_before_relax (fragP
, segment_type
)
84 internal_error ("Relaxation is a luxury we can't afford");
88 /* We have no need to default values of symbols. */
92 md_undefined_symbol (name
)
98 /* Turn a string in input_line_pointer into a floating point constant of type
99 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
100 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
103 #define MAX_LITTLENUMS 4
106 md_atof (type
, litP
, sizeP
)
112 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
113 LITTLENUM_TYPE
*wordP
;
135 return "bad call to md_atof ()";
138 t
= atof_ieee (input_line_pointer
, type
, words
);
141 input_line_pointer
= t
;
144 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
146 for (wordP
= words
; prec
--;)
148 md_number_to_chars (litP
, (valueT
) (*wordP
++), sizeof (LITTLENUM_TYPE
));
149 litP
+= sizeof (LITTLENUM_TYPE
);
154 /* Check to see if the constant value in NUM will fit in a field of
155 width BITS if it has flags FLAGS. */
158 const_overflow (num
, bits
, flags
)
166 /* Only need to check fields less than 32 bits wide */
168 if (flags
& TIC80_OPERAND_SIGNED
)
170 max
= (1 << (bits
- 1)) - 1;
171 min
= - (1 << (bits
- 1));
172 retval
= ((long) num
> max
) || ((long) num
< min
);
176 max
= (1 << bits
) - 1;
178 retval
= (num
> max
) || (num
< min
);
183 /* get_operands() parses a string of operands and fills in a passed array of
186 Note that we use O_absent expressions to record additional information
187 about the previous non-O_absent expression, such as ":m" or ":s"
188 modifiers or register numbers enclosed in parens like "(r10)".
190 Returns the number of expressions that were placed in EXP.
198 char *p
= input_line_pointer
;
206 /* Skip leading whitespace */
207 while (*p
== ' ' || *p
== '\t' || *p
== ',')
212 /* Check to see if we have any operands left to parse */
213 if (*p
== 0 || *p
== '\n' || *p
== '\r')
218 /* Notice scaling or direct memory operand modifiers and save them in
219 an O_absent expression after the expression that they modify. */
224 exp
[numexp
].X_op
= O_absent
;
228 /* This is a ":m" modifier */
229 exp
[numexp
].X_add_number
= TIC80_OPERAND_M_SI
| TIC80_OPERAND_M_LI
;
234 /* This is a ":s" modifier */
235 exp
[numexp
].X_add_number
= TIC80_OPERAND_SCALED
;
239 as_bad ("':' not followed by 'm' or 's'");
245 /* Handle leading '(' on operands that use them, by recording that we
246 have entered a paren nesting level and then continuing. We complain
247 about multiple nesting. */
253 as_bad ("paren nesting");
259 /* Handle trailing ')' on operands that use them, by reducing the
260 nesting level and then continuing. We complain if there were too
265 /* Record that we have left a paren group and continue */
268 as_bad ("mismatched parenthesis");
274 /* Begin operand parsing at the current scan point. */
276 input_line_pointer
= p
;
277 expression (&exp
[numexp
]);
279 if (exp
[numexp
].X_op
== O_illegal
)
281 as_bad ("illegal operand");
283 else if (exp
[numexp
].X_op
== O_absent
)
285 as_bad ("missing operand");
289 p
= input_line_pointer
;
294 exp
[numexp
].X_op
= O_absent
;
295 exp
[numexp
++].X_add_number
= TIC80_OPERAND_PARENS
;
298 /* Mark the end of the valid operands with an illegal expression. */
299 exp
[numexp
].X_op
= O_illegal
;
304 /* find_opcode() gets a pointer to the entry in the opcode table that
305 matches the instruction being assembled, or returns NULL if no such match
308 First it parses all the operands and save them as expressions. Note that
309 we use O_absent expressions to record additional information about the
310 previous non-O_absent expression, such as ":m" or ":s" modifiers or
311 register numbers enclosed in parens like "(r10)".
313 It then looks at all opcodes with the same name and uses the operands to
314 choose the correct opcode. */
316 static struct tic80_opcode
*
317 find_opcode (opcode
, myops
)
318 struct tic80_opcode
*opcode
;
321 int numexp
; /* Number of expressions from parsing operands */
322 int expi
; /* Index of current expression to match */
323 int opi
; /* Index of current operand to match */
324 int match
= 0; /* Set to 1 when an operand match is found */
325 struct tic80_opcode
*opc
= opcode
; /* Pointer to current opcode table entry */
326 const struct tic80_opcode
*end
; /* Pointer to end of opcode table */
328 /* First parse all the operands so we only have to do it once. There may
329 be more expressions generated than there are operands. */
331 numexp
= get_operands (myops
);
333 /* For each opcode with the same name, try to match it against the parsed
336 end
= tic80_opcodes
+ tic80_num_opcodes
;
337 while (!match
&& (opc
< end
) && (strcmp (opc
-> name
, opcode
-> name
) == 0))
339 /* Start off assuming a match. If we find a mismatch, then this is
340 reset and the operand/expr matching loop terminates with match
341 equal to zero, which allows us to try the next opcode. */
345 /* For each expression, try to match it against the current operand
346 for the current opcode. Upon any mismatch, we abandon further
347 matching for the current opcode table entry. */
349 for (expi
= 0, opi
= -1; (expi
< numexp
) && match
; expi
++)
351 int bits
, flags
, X_op
, num
;
353 X_op
= myops
[expi
].X_op
;
354 num
= myops
[expi
].X_add_number
;
356 /* The O_absent expressions apply to the same operand as the most
357 recent non O_absent expression. So only increment the operand
358 index when the current expression is not one of these special
361 if (X_op
!= O_absent
)
366 flags
= tic80_operands
[opc
-> operands
[opi
]].flags
;
367 bits
= tic80_operands
[opc
-> operands
[opi
]].bits
;
372 /* Also check that registers that are supposed to be even actually
374 if (((flags
& TIC80_OPERAND_GPR
) != (num
& TIC80_OPERAND_GPR
)) ||
375 ((flags
& TIC80_OPERAND_FPA
) != (num
& TIC80_OPERAND_FPA
)) ||
376 ((flags
& TIC80_OPERAND_CR
) != (num
& TIC80_OPERAND_CR
)) ||
377 ((flags
& TIC80_OPERAND_EVEN
) && (num
& 1)) ||
378 const_overflow (num
& ~TIC80_OPERAND_MASK
, bits
, flags
))
384 if ((flags
& TIC80_OPERAND_ENDMASK
) && (num
== 32))
386 /* Endmask values of 0 and 32 give identical results */
389 if ((flags
& (TIC80_OPERAND_FPA
| TIC80_OPERAND_GPR
)) ||
390 const_overflow (num
, bits
, flags
))
396 if ((bits
< 32) && (flags
& TIC80_OPERAND_PCREL
))
398 /* For now we only allow PC relative relocations in the
399 short immediate fields, like the TI assembler.
400 FIXME: Should be able to choose "best-fit". */
402 else if ((bits
== 32) /* && (flags & TIC80_OPERAND_BASEREL) */)
404 /* For now we only allow base relative relocations in
405 the long immediate fields, like the TI assembler.
406 FIXME: Should be able to choose "best-fit". */
410 /* Symbols that don't match one of the above cases are
411 rejected as an operand. */
416 /* If this is an O_absent expression, then it may be an expression that
417 supplies additional information about the operand, such as ":m" or
418 ":s" modifiers. Check to see that the operand matches this requirement. */
419 if (!((num
& TIC80_OPERAND_M_SI
) && (flags
& TIC80_OPERAND_M_SI
) ||
420 (num
& TIC80_OPERAND_M_LI
) && (flags
& TIC80_OPERAND_M_LI
) ||
421 (num
& TIC80_OPERAND_SCALED
) && (flags
& TIC80_OPERAND_SCALED
)))
427 if ((num
> 0) || !(flags
& TIC80_OPERAND_FLOAT
))
442 case O_bit_inclusive_or
:
444 case O_bit_exclusive_or
:
458 internal_error_a ("unhandled expression type", X_op
);
467 return (match
? opc
: NULL
);
471 /* Now search the opcode table table for one with operands that
472 matches what we've got. */
477 for (i
= 0; opcode
-> operands
[i
]; i
++)
479 int flags
= tic80_operands
[opcode
->operands
[i
]].flags
;
480 int X_op
= myops
[i
].X_op
;
481 int num
= myops
[i
].X_add_number
;
489 if (flags
& (TIC80_OPERAND_GPR
| TIC80_OPERAND_FPA
| TIC80_OPERAND_CR
))
491 if ((X_op
!= O_register
) ||
492 ((flags
& TIC80_OPERAND_GPR
) != (num
& TIC80_OPERAND_GPR
)) ||
493 ((flags
& TIC80_OPERAND_FPA
) != (num
& TIC80_OPERAND_FPA
)) ||
494 ((flags
& TIC80_OPERAND_CR
) != (num
& TIC80_OPERAND_CR
)))
501 if (((flags
& TIC80_OPERAND_MINUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_MINUS
))) ||
502 ((flags
& TIC80_OPERAND_PLUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_PLUS
))) ||
503 ((flags
& TIC80_OPERAND_ATMINUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATMINUS
))) ||
504 ((flags
& TIC80_OPERAND_ATPAR
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATPAR
))) ||
505 ((flags
& TIC80_OPERAND_ATSIGN
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATSIGN
))))
511 /* we're only done if the operands matched so far AND there
512 are no more to check */
513 if (match
&& myops
[i
].X_op
==0)
518 next_opcode
= opcode
+1;
519 if (next_opcode
->opcode
== 0)
521 if (strcmp(next_opcode
->name
, opcode
->name
))
523 opcode
= next_opcode
;
528 as_bad ("bad opcode or operands");
532 /* Check that all registers that are required to be even are. */
533 /* Also, if any operands were marked as registers, but were really symbols */
535 for (i
=0; opcode
->operands
[i
]; i
++)
537 if ((tic80_operands
[opcode
->operands
[i
]].flags
& TIC80_OPERAND_EVEN
) &&
538 (myops
[i
].X_add_number
& 1))
539 as_fatal ("Register number must be EVEN");
540 if (myops
[i
].X_op
== O_register
)
542 if (!(tic80_operands
[opcode
->operands
[i
]].flags
& TIC80_OPERAND_REG
))
544 myops
[i
].X_op
= O_symbol
;
545 myops
[i
].X_add_symbol
= symbol_find_or_make ((char *)myops
[i
].X_op_symbol
);
546 myops
[i
].X_add_number
= 0;
547 myops
[i
].X_op_symbol
= NULL
;
555 /* build_insn takes a pointer to the opcode entry in the opcode table
556 and the array of operand expressions and writes out the instruction.
558 Note that the opcode word and extended word may be written to different
559 frags, with the opcode at the end of one frag and the extension at the
560 beginning of the next. */
563 build_insn (opcode
, opers
)
564 struct tic80_opcode
*opcode
;
567 int expi
; /* Index of current expression to match */
568 int opi
; /* Index of current operand to match */
569 unsigned long insn
[2]; /* Instruction and long immediate (if any) */
570 char *f
; /* Pointer to frag location for insn[0] */
571 fragS
*ffrag
; /* Frag containing location f */
572 char *fx
= NULL
; /* Pointer to frag location for insn[1] */
573 fragS
*fxfrag
; /* Frag containing location fx */
575 /* Start with the raw opcode bits from the opcode table. */
576 insn
[0] = opcode
-> opcode
;
578 /* We are going to insert at least one 32 bit opcode so get the
584 /* For each operand expression, insert the appropriate bits into the
586 for (expi
= 0, opi
= -1; opers
[expi
].X_op
!= O_illegal
; expi
++)
588 int bits
, shift
, flags
, X_op
, num
;
590 X_op
= opers
[expi
].X_op
;
591 num
= opers
[expi
].X_add_number
;
593 /* The O_absent expressions apply to the same operand as the most
594 recent non O_absent expression. So only increment the operand
595 index when the current expression is not one of these special
598 if (X_op
!= O_absent
)
603 flags
= tic80_operands
[opcode
-> operands
[opi
]].flags
;
604 bits
= tic80_operands
[opcode
-> operands
[opi
]].bits
;
605 shift
= tic80_operands
[opcode
-> operands
[opi
]].shift
;
610 num
&= ~TIC80_OPERAND_MASK
;
611 insn
[0] = insn
[0] | (num
<< shift
);
614 if ((flags
& TIC80_OPERAND_ENDMASK
) && (num
== 32))
616 /* Endmask values of 0 and 32 give identical results */
619 else if ((flags
& TIC80_OPERAND_BITNUM
))
621 /* BITNUM values are stored in one's complement form */
624 /* Mask off upper bits, just it case it is signed and is negative */
627 num
&= (1 << bits
) - 1;
628 insn
[0] = insn
[0] | (num
<< shift
);
643 if (flags
& TIC80_OPERAND_PCREL
)
646 fx
- (fxfrag
-> fr_literal
),
655 fx
- (fxfrag
-> fr_literal
),
662 else if (flags
& TIC80_OPERAND_PCREL
)
665 f
- (ffrag
-> fr_literal
),
666 4, /* FIXME! how is this used? */
673 internal_error ("symbol reloc that is not PC relative or 32 bits");
677 /* Each O_absent expression can indicate exactly one possible modifier. */
678 if ((num
& TIC80_OPERAND_M_SI
) && (flags
& TIC80_OPERAND_M_SI
))
680 insn
[0] = insn
[0] | (1 << 17);
682 else if ((num
& TIC80_OPERAND_M_LI
) && (flags
& TIC80_OPERAND_M_LI
))
684 insn
[0] = insn
[0] | (1 << 15);
686 else if ((num
& TIC80_OPERAND_SCALED
) && (flags
& TIC80_OPERAND_SCALED
))
688 insn
[0] = insn
[0] | (1 << 11);
690 else if ((num
& TIC80_OPERAND_PARENS
) && (flags
& TIC80_OPERAND_PARENS
))
692 /* No code to generate, just accept and discard this expression */
696 internal_error_a ("unhandled operand modifier", opers
[expi
].X_add_number
);
704 long exponent_bits
= 8L;
705 LITTLENUM_TYPE words
[2];
706 /* Value is still in generic_floating_point_number */
707 gen_to_words (words
, precision
, exponent_bits
);
708 insn
[1] = (words
[0] << 16) | words
[1];
721 case O_bit_inclusive_or
:
723 case O_bit_exclusive_or
:
737 internal_error_a ("unhandled expression", X_op
);
742 /* Write out the instruction, either 4 or 8 bytes. */
744 md_number_to_chars (f
, insn
[0], 4);
747 md_number_to_chars (fx
, insn
[1], 4);
751 /* This is the main entry point for the machine-dependent assembler. Gas
752 calls this function for each input line which does not contain a
755 STR points to a NULL terminated machine dependent instruction. This
756 function is supposed to emit the frags/bytes it assembles to. */
763 unsigned char *input_line_save
;
764 struct tic80_opcode
*opcode
;
765 expressionS myops
[16];
768 /* Ensure there is something there to assemble. */
771 /* Drop any leading whitespace. */
772 while (isspace (*str
))
777 /* Isolate the mnemonic from the rest of the string by finding the first
778 whitespace character and zapping it to a null byte. */
779 for (scan
= str
; *scan
!= '\000' && !isspace (*scan
); scan
++) {;}
785 /* Try to find this mnemonic in the hash table */
786 if ((opcode
= (struct tic80_opcode
*) hash_find (tic80_hash
, str
)) == NULL
)
788 as_bad ("Invalid mnemonic: '%s'", str
);
793 while (isspace (*scan
))
798 input_line_save
= input_line_pointer
;
799 input_line_pointer
= str
;
801 opcode
= find_opcode (opcode
, myops
);
804 as_bad ("Invalid operands: '%s'", input_line_save
);
807 input_line_pointer
= input_line_save
;
808 build_insn (opcode
, myops
);
811 /* This function is called once at the start of assembly, after the command
812 line arguments have been parsed and all the machine independent
813 initializations have been completed.
815 It should set up all the tables, etc., that the machine dependent part of
816 the assembler will need. */
821 char *prev_name
= "";
822 register const struct tic80_opcode
*op
;
823 register const struct tic80_opcode
*op_end
;
824 const struct predefined_symbol
*pdsp
;
825 extern int coff_flags
; /* Defined in obj-coff.c */
827 /* Set F_AR32WR in coff_flags, which will end up in the file header
830 coff_flags
|= F_AR32WR
; /* TIc80 is 32 bit little endian */
832 /* Insert unique names into hash table. The TIc80 instruction set
833 has many identical opcode names that have different opcodes based
834 on the operands. This hash table then provides a quick index to
835 the first opcode with a particular name in the opcode table. */
837 tic80_hash
= hash_new ();
838 op_end
= tic80_opcodes
+ tic80_num_opcodes
;
839 for (op
= tic80_opcodes
; op
< op_end
; op
++)
841 if (strcmp (prev_name
, op
-> name
) != 0)
843 prev_name
= (char *) op
-> name
;
844 hash_insert (tic80_hash
, op
-> name
, (char *) op
);
848 /* Insert the predefined symbols into the symbol table. We use symbol_create
849 rather than symbol_new so that these symbols don't end up in the object
850 files' symbol table. Note that the values of the predefined symbols include
851 some upper bits that distinguish the type of the symbol (register, bitnum,
852 condition code, etc) and these bits must be masked away before actually
853 inserting the values into the instruction stream. For registers we put
854 these bits in the symbol table since we use them later and there is no
855 question that they aren't part of the register number. For constants we
856 can't do that since the constant can be any value, so they are masked off
857 before putting them into the symbol table. */
860 while ((pdsp
= tic80_next_predefined_symbol (pdsp
)) != NULL
)
866 symtype
= PDS_VALUE (pdsp
) & TIC80_OPERAND_MASK
;
869 case TIC80_OPERAND_GPR
:
870 case TIC80_OPERAND_FPA
:
871 case TIC80_OPERAND_CR
:
872 segment
= reg_section
;
873 valu
= PDS_VALUE (pdsp
);
875 case TIC80_OPERAND_CC
:
876 case TIC80_OPERAND_BITNUM
:
877 segment
= absolute_section
;
878 valu
= PDS_VALUE (pdsp
) & ~TIC80_OPERAND_MASK
;
881 internal_error_a ("unhandled predefined symbol bits", symtype
);
884 symbol_table_insert (symbol_create (PDS_NAME (pdsp
), segment
, valu
,
885 &zero_address_frag
));
891 /* The assembler adds md_shortopts to the string passed to getopt. */
893 CONST
char *md_shortopts
= "";
895 /* The assembler adds md_longopts to the machine independent long options
896 that are passed to getopt. */
898 struct option md_longopts
[] = {
899 {NULL
, no_argument
, NULL
, 0}
902 size_t md_longopts_size
= sizeof(md_longopts
);
904 /* The md_parse_option function will be called whenever getopt returns an
905 unrecognized code, presumably indicating a special code value which
906 appears in md_longopts for machine specific command line options. */
909 md_parse_option (c
, arg
)
916 /* The md_show_usage function will be called whenever a usage message is
917 printed. It should print a description of the machine specific options
918 found in md_longopts. */
921 md_show_usage (stream
)
927 /* Attempt to simplify or even eliminate a fixup. The return value is
928 ignored; perhaps it was once meaningful, but now it is historical.
929 To indicate that a fixup has been eliminated, set fixP->fx_done.
933 md_apply_fix (fixP
, val
)
937 char *dest
= fixP
-> fx_frag
-> fr_literal
+ fixP
-> fx_where
;
940 switch (fixP
-> fx_r_type
)
943 md_number_to_chars (dest
, (valueT
) val
, 4);
947 val
+= 1; /* Target address computed from inst start */
948 md_number_to_chars (dest
, (valueT
) val
, 4);
951 overflow
= (val
< -65536L) || (val
> 65532L);
954 as_bad_where (fixP
-> fx_file
, fixP
-> fx_line
,
955 "PC offset 0x%lx outside range 0x%lx-0x%lx",
956 val
, -65536L, 65532L);
961 *dest
++ = val
& 0xFF;
963 *dest
= (*dest
& 0x80) | (val
& 0x7F);
967 md_number_to_chars (dest
, (valueT
) val
, fixP
-> fx_size
);
970 internal_error_a ("unhandled relocation type in fixup", fixP
-> fx_r_type
);
976 /* Functions concerning relocs. */
978 /* The location from which a PC relative jump should be calculated,
979 given a PC relative reloc.
981 For the TIc80, this is the address of the 32 bit opcode containing
982 the PC relative field. */
988 return (fixP
-> fx_frag
-> fr_address
+ fixP
-> fx_where
) ;
992 * Called after relax() is finished.
993 * In: Address of frag.
994 * fr_type == rs_machine_dependent.
995 * fr_subtype is what the address relaxed to.
997 * Out: Any fixSs and constants are set up.
998 * Caller will turn frag into a ".space 0".
1002 md_convert_frag (headers
, seg
, fragP
)
1003 object_headers
*headers
;
1007 internal_error ("md_convert_frag() not implemented yet");
1014 tc_coff_symbol_emit_hook (ignore
)
1019 #if defined OBJ_COFF
1022 tc_coff_fix2rtype (fixP
)
1025 return (fixP
-> fx_r_type
);
1028 #endif /* OBJ_COFF */
1030 /* end of tc-tic80.c */