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