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