1 /* tc-tic80.c -- Assemble for the TI TMS320C80 (MV)
2 Copyright (C) 1996 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
[] = "dD";
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 const pseudo_typeS md_pseudo_table
[] =
60 {"word", cons
, 4}, /* FIXME: Should this be machine independent? */
64 /* Opcode hash table. */
65 static struct hash_control
*tic80_hash
;
67 static struct tic80_opcode
* find_opcode
PARAMS ((struct tic80_opcode
*, expressionS
[]));
68 static void build_insn
PARAMS ((struct tic80_opcode
*, expressionS
*));
69 static int get_operands
PARAMS ((expressionS exp
[]));
70 static int const_overflow
PARAMS ((unsigned long num
, int bits
, int flags
));
74 md_estimate_size_before_relax (fragP
, segment_type
)
78 internal_error ("Relaxation is a luxury we can't afford");
82 /* We have no need to default values of symbols. */
86 md_undefined_symbol (name
)
92 /* Turn a string in input_line_pointer into a floating point constant of type
93 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
94 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
97 #define MAX_LITTLENUMS 4
100 md_atof (type
, litP
, sizeP
)
106 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
107 LITTLENUM_TYPE
*wordP
;
129 return "bad call to md_atof ()";
132 t
= atof_ieee (input_line_pointer
, type
, words
);
135 input_line_pointer
= t
;
138 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
140 for (wordP
= words
; prec
--;)
142 md_number_to_chars (litP
, (valueT
) (*wordP
++), sizeof (LITTLENUM_TYPE
));
143 litP
+= sizeof (LITTLENUM_TYPE
);
148 /* Check to see if the constant value in NUM will fit in a field of
149 width BITS if it has flags FLAGS. */
152 const_overflow (num
, bits
, flags
)
160 /* Only need to check fields less than 32 bits wide */
162 if (flags
& TIC80_OPERAND_SIGNED
)
164 max
= (1 << (bits
- 1)) - 1;
165 min
= - (1 << (bits
- 1));
166 retval
= ((long) num
> max
) || ((long) num
< min
);
170 max
= (1 << bits
) - 1;
172 retval
= (num
> max
) || (num
< min
);
177 /* get_operands() parses a string of operands and fills in a passed array of
180 Note that we use O_absent expressions to record additional information
181 about the previous non-O_absent expression, such as ":m" or ":s"
182 modifiers or register numbers enclosed in parens like "(r10)".
184 Returns the number of expressions that were placed in EXP.
192 char *p
= input_line_pointer
;
200 /* Skip leading whitespace */
201 while (*p
== ' ' || *p
== '\t' || *p
== ',')
206 /* Check to see if we have any operands left to parse */
207 if (*p
== 0 || *p
== '\n' || *p
== '\r')
212 /* Notice scaling or direct memory operand modifiers and save them in
213 an O_absent expression after the expression that they modify. */
218 exp
[numexp
].X_op
= O_absent
;
222 exp
[numexp
].X_add_number
= TIC80_OPERAND_M_SI
| TIC80_OPERAND_M_LI
;
227 exp
[numexp
].X_add_number
= TIC80_OPERAND_SCALED
;
231 as_bad ("':' not followed by 'm' or 's'");
237 /* Handle leading '(' on operands that use them, by recording that we
238 have entered a paren nesting level and then continuing. We complain
239 about multiple nesting. */
245 as_bad ("paren nesting");
251 /* Handle trailing ')' on operands that use them, by reducing the
252 nesting level and then continuing. We complain if there were too
257 /* Record that we have left a paren group and continue */
260 as_bad ("mismatched parenthesis");
266 /* Begin operand parsing at the current scan point. */
268 input_line_pointer
= p
;
269 expression (&exp
[numexp
]);
271 if (exp
[numexp
].X_op
== O_illegal
)
273 as_bad ("illegal operand");
275 else if (exp
[numexp
].X_op
== O_absent
)
277 as_bad ("missing operand");
281 p
= input_line_pointer
;
286 exp
[numexp
].X_op
= O_absent
;
287 exp
[numexp
++].X_add_number
= TIC80_OPERAND_PARENS
;
290 /* Mark the end of the valid operands with an illegal expression. */
291 exp
[numexp
].X_op
= O_illegal
;
296 /* find_opcode() gets a pointer to the entry in the opcode table that
297 matches the instruction being assembled, or returns NULL if no such match
300 First it parses all the operands and save them as expressions. Note that
301 we use O_absent expressions to record additional information about the
302 previous non-O_absent expression, such as ":m" or ":s" modifiers or
303 register numbers enclosed in parens like "(r10)".
305 It then looks at all opcodes with the same name and uses the operands to
306 choose the correct opcode. */
308 static struct tic80_opcode
*
309 find_opcode (opcode
, myops
)
310 struct tic80_opcode
*opcode
;
313 int numexp
; /* Number of expressions from parsing operands */
314 int expi
; /* Index of current expression to match */
315 int opi
; /* Index of current operand to match */
316 int match
= 0; /* Set to 1 when an operand match is found */
317 struct tic80_opcode
*opc
= opcode
; /* Pointer to current opcode table entry */
318 const struct tic80_opcode
*end
; /* Pointer to end of opcode table */
320 /* First parse all the operands so we only have to do it once. There may
321 be more expressions generated than there are operands. */
323 numexp
= get_operands (myops
);
325 /* For each opcode with the same name, try to match it against the parsed
328 end
= tic80_opcodes
+ tic80_num_opcodes
;
329 while (!match
&& (opc
< end
) && (strcmp (opc
-> name
, opcode
-> name
) == 0))
331 /* Start off assuming a match. If we find a mismatch, then this is
332 reset and the operand/expr matching loop terminates with match
333 equal to zero, which allows us to try the next opcode. */
337 /* For each expression, try to match it against the current operand
338 for the current opcode. Upon any mismatch, we abandon further
339 matching for the current opcode table entry. */
341 for (expi
= 0, opi
= -1; (expi
< numexp
) && match
; expi
++)
343 int bits
, flags
, X_op
, num
;
345 X_op
= myops
[expi
].X_op
;
346 num
= myops
[expi
].X_add_number
;
347 if (X_op
!= O_absent
)
349 /* The O_absent expressions apply to the previously seen
350 operand, so only increment the operand index when the
351 current expression needs to be matched against the next
355 flags
= tic80_operands
[opc
-> operands
[opi
]].flags
;
356 bits
= tic80_operands
[opc
-> operands
[opi
]].bits
;
361 /* Also check that registers that are supposed to be even actually
363 if (((flags
& TIC80_OPERAND_GPR
) != (num
& TIC80_OPERAND_GPR
)) ||
364 ((flags
& TIC80_OPERAND_FPA
) != (num
& TIC80_OPERAND_FPA
)) ||
365 ((flags
& TIC80_OPERAND_CR
) != (num
& TIC80_OPERAND_CR
)) ||
366 ((flags
& TIC80_OPERAND_EVEN
) && (num
& 1)) ||
367 const_overflow (num
& ~TIC80_OPERAND_MASK
, bits
, flags
))
373 if ((flags
& TIC80_OPERAND_ENDMASK
) && (num
== 32))
375 /* Endmask values of 0 and 32 give identical results */
378 if ((flags
& (TIC80_OPERAND_FPA
| TIC80_OPERAND_GPR
)) ||
379 const_overflow (num
, bits
, flags
))
397 case O_bit_inclusive_or
:
399 case O_bit_exclusive_or
:
413 internal_error_a ("unhandled expression type", X_op
);
422 return (match
? opc
: NULL
);
426 /* Now search the opcode table table for one with operands that
427 matches what we've got. */
432 for (i
= 0; opcode
-> operands
[i
]; i
++)
434 int flags
= tic80_operands
[opcode
->operands
[i
]].flags
;
435 int X_op
= myops
[i
].X_op
;
436 int num
= myops
[i
].X_add_number
;
444 if (flags
& (TIC80_OPERAND_GPR
| TIC80_OPERAND_FPA
| TIC80_OPERAND_CR
))
446 if ((X_op
!= O_register
) ||
447 ((flags
& TIC80_OPERAND_GPR
) != (num
& TIC80_OPERAND_GPR
)) ||
448 ((flags
& TIC80_OPERAND_FPA
) != (num
& TIC80_OPERAND_FPA
)) ||
449 ((flags
& TIC80_OPERAND_CR
) != (num
& TIC80_OPERAND_CR
)))
456 if (((flags
& TIC80_OPERAND_MINUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_MINUS
))) ||
457 ((flags
& TIC80_OPERAND_PLUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_PLUS
))) ||
458 ((flags
& TIC80_OPERAND_ATMINUS
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATMINUS
))) ||
459 ((flags
& TIC80_OPERAND_ATPAR
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATPAR
))) ||
460 ((flags
& TIC80_OPERAND_ATSIGN
) && ((X_op
!= O_absent
) || (num
!= TIC80_OPERAND_ATSIGN
))))
466 /* we're only done if the operands matched so far AND there
467 are no more to check */
468 if (match
&& myops
[i
].X_op
==0)
473 next_opcode
= opcode
+1;
474 if (next_opcode
->opcode
== 0)
476 if (strcmp(next_opcode
->name
, opcode
->name
))
478 opcode
= next_opcode
;
483 as_bad ("bad opcode or operands");
487 /* Check that all registers that are required to be even are. */
488 /* Also, if any operands were marked as registers, but were really symbols */
490 for (i
=0; opcode
->operands
[i
]; i
++)
492 if ((tic80_operands
[opcode
->operands
[i
]].flags
& TIC80_OPERAND_EVEN
) &&
493 (myops
[i
].X_add_number
& 1))
494 as_fatal ("Register number must be EVEN");
495 if (myops
[i
].X_op
== O_register
)
497 if (!(tic80_operands
[opcode
->operands
[i
]].flags
& TIC80_OPERAND_REG
))
499 myops
[i
].X_op
= O_symbol
;
500 myops
[i
].X_add_symbol
= symbol_find_or_make ((char *)myops
[i
].X_op_symbol
);
501 myops
[i
].X_add_number
= 0;
502 myops
[i
].X_op_symbol
= NULL
;
510 /* build_insn takes a pointer to the opcode entry in the opcode table
511 and the array of operand expressions and writes out the instruction. */
514 build_insn (opcode
, opers
)
515 struct tic80_opcode
*opcode
;
518 int expi
; /* Index of current expression to match */
519 int opi
; /* Index of current operand to match */
520 unsigned long insn
[2]; /* Instruction and long immediate (if any) */
521 int extended
= 0; /* Nonzero if instruction is 8 bytes */
522 char *f
; /* Temporary pointer to output location */
524 /* Start with the raw opcode bits from the opcode table. */
525 insn
[0] = opcode
-> opcode
;
527 /* For each operand expression, insert the appropriate bits into the
529 for (expi
= 0, opi
= -1; opers
[expi
].X_op
!= O_illegal
; expi
++)
531 int bits
, shift
, flags
, X_op
, num
;
533 X_op
= opers
[expi
].X_op
;
534 num
= opers
[expi
].X_add_number
;
535 if (X_op
!= O_absent
)
537 /* The O_absent expressions apply to the previously seen
538 operand, so only increment the operand index when the
539 current expression needs to be matched against the next
545 /* Found a modifier that applies to the previously
546 seen operand, so handle it. */
547 switch (opers
[expi
].X_add_number
)
549 case TIC80_OPERAND_M_SI
| TIC80_OPERAND_M_LI
:
550 internal_error_a ("unhandled operand modifier", opers
[expi
].X_add_number
);
552 case TIC80_OPERAND_SCALED
:
553 internal_error_a ("unhandled operand modifier", opers
[expi
].X_add_number
);
555 case TIC80_OPERAND_PARENS
:
556 internal_error_a ("unhandled operand modifier", opers
[expi
].X_add_number
);
559 internal_error_a ("unhandled operand modifier", opers
[expi
].X_add_number
);
563 flags
= tic80_operands
[opcode
-> operands
[opi
]].flags
;
564 bits
= tic80_operands
[opcode
-> operands
[opi
]].bits
;
565 shift
= tic80_operands
[opcode
-> operands
[opi
]].shift
;
570 num
&= ~TIC80_OPERAND_MASK
;
571 insn
[0] = insn
[0] | (num
<< shift
);
574 if ((flags
& TIC80_OPERAND_ENDMASK
) && (num
== 32))
576 /* Endmask values of 0 and 32 give identical results */
579 /* Mask off upper bits, just it case it is signed and is negative */
582 num
&= (1 << bits
) - 1;
583 insn
[0] = insn
[0] | (num
<< shift
);
604 case O_bit_inclusive_or
:
606 case O_bit_exclusive_or
:
620 internal_error_a ("unhandled expression", X_op
);
625 /* Write out the instruction, either 4 or 8 bytes. */
628 md_number_to_chars (f
, insn
[0], 4);
632 md_number_to_chars (f
, insn
[1], 4);
636 /* This is the main entry point for the machine-dependent assembler. Gas
637 calls this function for each input line which does not contain a
640 STR points to a NULL terminated machine dependent instruction. This
641 function is supposed to emit the frags/bytes it assembles to. */
648 unsigned char *input_line_save
;
649 struct tic80_opcode
*opcode
;
650 expressionS myops
[16];
653 /* Ensure there is something there to assemble. */
656 /* Drop any leading whitespace. */
657 while (isspace (*str
))
662 /* Isolate the mnemonic from the rest of the string by finding the first
663 whitespace character and zapping it to a null byte. */
664 for (scan
= str
; *scan
!= '\000' && !isspace (*scan
); scan
++) {;}
670 /* Try to find this mnemonic in the hash table */
671 if ((opcode
= (struct tic80_opcode
*) hash_find (tic80_hash
, str
)) == NULL
)
673 as_bad ("Invalid mnemonic: '%s'", str
);
678 while (isspace (*scan
))
683 input_line_save
= input_line_pointer
;
684 input_line_pointer
= str
;
686 opcode
= find_opcode (opcode
, myops
);
689 as_bad ("Invalid operands: '%s'", input_line_save
);
692 input_line_pointer
= input_line_save
;
693 build_insn (opcode
, myops
);
696 /* This function is called once at the start of assembly, after the command
697 line arguments have been parsed and all the machine independent
698 initializations have been completed.
700 It should set up all the tables, etc., that the machine dependent part of
701 the assembler will need. */
706 char *prev_name
= "";
707 register const struct tic80_opcode
*op
;
708 register const struct tic80_opcode
*op_end
;
709 const struct predefined_symbol
*pdsp
;
711 tic80_hash
= hash_new ();
713 /* Insert unique names into hash table. The TIc80 instruction set
714 has many identical opcode names that have different opcodes based
715 on the operands. This hash table then provides a quick index to
716 the first opcode with a particular name in the opcode table. */
718 op_end
= tic80_opcodes
+ tic80_num_opcodes
;
719 for (op
= tic80_opcodes
; op
< op_end
; op
++)
721 if (strcmp (prev_name
, op
-> name
) != 0)
723 prev_name
= (char *) op
-> name
;
724 hash_insert (tic80_hash
, op
-> name
, (char *) op
);
728 /* Insert the predefined symbols into the symbol table. We use symbol_create
729 rather than symbol_new so that these symbols don't end up in the object
730 files' symbol table. Note that the values of the predefined symbols include
731 some upper bits that distinguish the type of the symbol (register, bitnum,
732 condition code, etc) and these bits must be masked away before actually
733 inserting the values into the instruction stream. For registers we put
734 these bits in the symbol table since we use them later and there is no
735 question that they aren't part of the register number. For constants we
736 can't do that since the constant can be any value, so they are masked off
737 before putting them into the symbol table. */
740 while ((pdsp
= tic80_next_predefined_symbol (pdsp
)) != NULL
)
746 symtype
= PDS_VALUE (pdsp
) & TIC80_OPERAND_MASK
;
749 case TIC80_OPERAND_GPR
:
750 case TIC80_OPERAND_FPA
:
751 case TIC80_OPERAND_CR
:
752 segment
= reg_section
;
753 valu
= PDS_VALUE (pdsp
);
755 case TIC80_OPERAND_CC
:
756 case TIC80_OPERAND_BITNUM
:
757 segment
= absolute_section
;
758 valu
= PDS_VALUE (pdsp
) & ~TIC80_OPERAND_MASK
;
761 internal_error_a ("unhandled predefined symbol bits", symtype
);
764 symbol_table_insert (symbol_create (PDS_NAME (pdsp
), segment
, valu
,
765 &zero_address_frag
));
771 /* The assembler adds md_shortopts to the string passed to getopt. */
773 CONST
char *md_shortopts
= "";
775 /* The assembler adds md_longopts to the machine independent long options
776 that are passed to getopt. */
778 struct option md_longopts
[] = {
779 {NULL
, no_argument
, NULL
, 0}
782 size_t md_longopts_size
= sizeof(md_longopts
);
784 /* The md_parse_option function will be called whenever getopt returns an
785 unrecognized code, presumably indicating a special code value which
786 appears in md_longopts for machine specific command line options. */
789 md_parse_option (c
, arg
)
796 /* The md_show_usage function will be called whenever a usage message is
797 printed. It should print a description of the machine specific options
798 found in md_longopts. */
801 md_show_usage (stream
)
807 /* Attempt to simplify or even eliminate a fixup. The return value is
808 ignored; perhaps it was once meaningful, but now it is historical.
809 To indicate that a fixup has been eliminated, set fixP->fx_done.
813 md_apply_fix (fixP
, val
)
817 internal_error ("md_apply_fix() not implemented yet");
822 /* Functions concerning relocs. */
824 /* The location from which a PC relative jump should be calculated,
825 given a PC relative reloc. */
831 internal_error ("md_pcrel_from() not implemented yet");
836 * Called after relax() is finished.
837 * In: Address of frag.
838 * fr_type == rs_machine_dependent.
839 * fr_subtype is what the address relaxed to.
841 * Out: Any fixSs and constants are set up.
842 * Caller will turn frag into a ".space 0".
846 md_convert_frag (headers
, seg
, fragP
)
847 object_headers
*headers
;
851 internal_error ("md_convert_frag() not implemented yet");
858 tc_coff_symbol_emit_hook (ignore
)
866 tc_coff_fix2rtype (fixP
)
869 internal_error ("tc_coff_fix2rtype() not implemented yet");
873 #endif /* OBJ_COFF */
875 /* end of tc-tic80.c */