Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* tc-w65.c -- Assemble code for the W65816 |
2132e3a3 AM |
2 | Copyright 1995, 1998, 2000, 2001, 2002, 2005 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
4 | |
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
8098403c | 22 | /* Written By Steve Chamberlain <sac@cygnus.com>. */ |
252b5132 RH |
23 | |
24 | #include <stdio.h> | |
25 | #include "as.h" | |
26 | #include "bfd.h" | |
27 | #include "subsegs.h" | |
28 | #define DEFINE_TABLE | |
29 | #include "../opcodes/w65-opc.h" | |
252b5132 | 30 | |
ea1562b3 | 31 | const char comment_chars[] = "!"; |
5a38dc70 | 32 | const char line_separator_chars[] = ";"; |
ea1562b3 | 33 | const char line_comment_chars[] = "!#"; |
252b5132 RH |
34 | |
35 | /* This table describes all the machine specific pseudo-ops the assembler | |
36 | has to support. The fields are: | |
8098403c | 37 | |
252b5132 RH |
38 | pseudo-op name without dot |
39 | function to call to execute this pseudo-op | |
ea1562b3 | 40 | Integer arg to pass to the function. */ |
252b5132 RH |
41 | |
42 | #define OP_BCC 0x90 | |
43 | #define OP_BCS 0xB0 | |
44 | #define OP_BEQ 0xF0 | |
45 | #define OP_BMI 0x30 | |
46 | #define OP_BNE 0xD0 | |
47 | #define OP_BPL 0x10 | |
48 | #define OP_BRA 0x80 | |
49 | #define OP_BRL 0x82 | |
50 | #define OP_BVC 0x50 | |
51 | #define OP_BVS 0x70 | |
52 | ||
ea1562b3 NC |
53 | static int M; /* M flag. */ |
54 | static int X; /* X flag. */ | |
6297c1fe | 55 | |
ea1562b3 NC |
56 | /* This function is called once, at assembler startup time. This |
57 | should set up all the tables, etc that the MD part of the assembler | |
58 | needs. */ | |
59 | ||
60 | static void | |
61 | s_longa (int xmode) | |
62 | { | |
63 | int *p = xmode ? &X : &M; | |
64 | ||
65 | while (*input_line_pointer == ' ') | |
66 | input_line_pointer++; | |
67 | if (strncmp (input_line_pointer, "on", 2) == 0) | |
68 | { | |
69 | input_line_pointer += 2; | |
70 | *p = 0; | |
71 | } | |
72 | else if (strncmp (input_line_pointer, "off", 3) == 0) | |
73 | { | |
74 | *p = 1; | |
75 | input_line_pointer += 3; | |
76 | } | |
77 | else | |
78 | as_bad (_("need on or off.")); | |
79 | demand_empty_rest_of_line (); | |
80 | } | |
19d63e5d | 81 | |
ea1562b3 NC |
82 | const pseudo_typeS md_pseudo_table[] = |
83 | { | |
252b5132 RH |
84 | {"int", cons, 2}, |
85 | {"word", cons, 2}, | |
86 | {"longa", s_longa, 0}, | |
87 | {"longi", s_longa, 1}, | |
88 | {0, 0, 0} | |
89 | }; | |
90 | ||
252b5132 RH |
91 | const char EXP_CHARS[] = "eE"; |
92 | ||
19d63e5d | 93 | /* Chars that mean this number is a floating point constant. */ |
252b5132 RH |
94 | /* As in 0f12.456 */ |
95 | /* or 0d1.2345e12 */ | |
96 | const char FLT_CHARS[] = "rRsSfFdDxXpP"; | |
97 | ||
19d63e5d KH |
98 | /* Opcode mnemonics */ |
99 | static struct hash_control *opcode_hash_control; | |
252b5132 | 100 | |
ea1562b3 NC |
101 | #define C(a, b) ENCODE_RELAX(a,b) |
102 | #define ENCODE_RELAX(what, length) (((what) << 2) + (length)) | |
252b5132 | 103 | |
ea1562b3 | 104 | #define GET_WHAT(x) (((x) >> 2)) |
252b5132 | 105 | |
ea1562b3 NC |
106 | #define BYTE_DISP 1 |
107 | #define WORD_DISP 2 | |
252b5132 RH |
108 | #define UNDEF_BYTE_DISP 0 |
109 | #define UNDEF_WORD_DISP 3 | |
252b5132 RH |
110 | #define COND_BRANCH 1 |
111 | #define UNCOND_BRANCH 2 | |
ea1562b3 | 112 | #define END 3 |
252b5132 | 113 | |
ea1562b3 NC |
114 | #define BYTE_F 127 /* How far we can branch forwards. */ |
115 | #define BYTE_B -126 /* How far we can branch backwards. */ | |
252b5132 RH |
116 | #define WORD_F 32767 |
117 | #define WORD_B 32768 | |
118 | ||
ea1562b3 NC |
119 | relax_typeS md_relax_table[C (END, 0)] = |
120 | { | |
e66457fb AM |
121 | { 0, 0, 0, 0 }, |
122 | { 0, 0, 0, 0 }, | |
123 | { 0, 0, 0, 0 }, | |
124 | { 0, 0, 0, 0 }, | |
125 | ||
126 | /* COND_BRANCH */ | |
127 | { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */ | |
128 | { BYTE_F, BYTE_B, 2, C (COND_BRANCH, WORD_DISP) }, /* BYTE_DISP */ | |
129 | { WORD_F, WORD_B, 5, 0 }, /* WORD_DISP */ | |
130 | { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */ | |
131 | ||
132 | /* UNCOND_BRANCH */ | |
133 | { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */ | |
134 | { BYTE_F, BYTE_B, 2, C (UNCOND_BRANCH, WORD_DISP) }, /* BYTE_DISP */ | |
135 | { WORD_F, WORD_B, 3, 0 }, /* WORD_DISP */ | |
136 | { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */ | |
e66457fb | 137 | }; |
252b5132 | 138 | |
252b5132 | 139 | void |
ea1562b3 | 140 | md_begin (void) |
252b5132 | 141 | { |
6297c1fe | 142 | const struct opinfo *opcode; |
252b5132 RH |
143 | char *prev_name = ""; |
144 | ||
145 | opcode_hash_control = hash_new (); | |
146 | ||
8098403c | 147 | /* Insert unique names into hash table. */ |
252b5132 RH |
148 | for (opcode = optable; opcode->name; opcode++) |
149 | { | |
150 | if (strcmp (prev_name, opcode->name)) | |
151 | { | |
152 | prev_name = opcode->name; | |
153 | hash_insert (opcode_hash_control, opcode->name, (char *) opcode); | |
154 | } | |
252b5132 RH |
155 | } |
156 | ||
252b5132 RH |
157 | flag_signed_overflow_ok = 1; |
158 | } | |
159 | ||
ea1562b3 NC |
160 | static expressionS immediate; /* Absolute expression. */ |
161 | static expressionS immediate1; /* Absolute expression. */ | |
252b5132 RH |
162 | int expr_size; |
163 | int expr_shift; | |
164 | int tc_cons_reloc; | |
8098403c | 165 | |
252b5132 | 166 | void |
ea1562b3 | 167 | w65_expression (expressionS *dest) |
252b5132 RH |
168 | { |
169 | expr_size = 0; | |
170 | expr_shift = 0; | |
171 | tc_cons_reloc = 0; | |
172 | while (*input_line_pointer == ' ') | |
173 | input_line_pointer++; | |
174 | ||
175 | if (*input_line_pointer == '<') | |
176 | { | |
177 | expr_size = 1; | |
178 | input_line_pointer++; | |
179 | } | |
180 | else if (*input_line_pointer == '>') | |
181 | { | |
182 | expr_shift = 1; | |
183 | input_line_pointer++; | |
184 | } | |
185 | else if (*input_line_pointer == '^') | |
186 | { | |
187 | expr_shift = 2; | |
188 | input_line_pointer++; | |
189 | } | |
190 | ||
191 | expr (0, dest); | |
192 | } | |
193 | ||
194 | int amode; | |
8098403c NC |
195 | |
196 | static char * | |
ea1562b3 | 197 | parse_exp (char *s) |
252b5132 RH |
198 | { |
199 | char *save; | |
200 | char *new; | |
201 | ||
202 | save = input_line_pointer; | |
203 | input_line_pointer = s; | |
6297c1fe | 204 | w65_expression (&immediate); |
252b5132 RH |
205 | if (immediate.X_op == O_absent) |
206 | as_bad (_("missing operand")); | |
207 | new = input_line_pointer; | |
208 | input_line_pointer = save; | |
209 | return new; | |
210 | } | |
211 | ||
8098403c | 212 | static char * |
ea1562b3 | 213 | get_operands (const struct opinfo *info, char *ptr) |
252b5132 | 214 | { |
ea1562b3 NC |
215 | int override_len = 0; |
216 | int bytes = 0; | |
8098403c | 217 | |
252b5132 RH |
218 | while (*ptr == ' ') |
219 | ptr++; | |
220 | ||
221 | if (ptr[0] == '#') | |
222 | { | |
223 | ptr++; | |
224 | switch (info->amode) | |
225 | { | |
226 | case ADDR_IMMTOI: | |
227 | bytes = X ? 1 : 2; | |
228 | amode = ADDR_IMMTOI; | |
229 | break; | |
230 | case ADDR_IMMTOA: | |
231 | bytes = M ? 1 : 2; | |
232 | amode = ADDR_IMMTOA; | |
233 | break; | |
234 | case ADDR_IMMCOP: | |
235 | bytes = 1; | |
236 | amode = ADDR_IMMCOP; | |
237 | break; | |
238 | case ADDR_DIR: | |
239 | bytes = 2; | |
240 | amode = ADDR_ABS; | |
241 | break; | |
242 | default: | |
243 | abort (); | |
244 | break; | |
245 | } | |
246 | ptr = parse_exp (ptr); | |
247 | } | |
248 | else if (ptr[0] == '!') | |
249 | { | |
250 | ptr = parse_exp (ptr + 1); | |
251 | if (ptr[0] == ',') | |
252 | { | |
253 | if (ptr[1] == 'y') | |
254 | { | |
255 | amode = ADDR_ABS_IDX_Y; | |
256 | bytes = 2; | |
257 | ptr += 2; | |
258 | } | |
259 | else if (ptr[1] == 'x') | |
260 | { | |
261 | amode = ADDR_ABS_IDX_X; | |
262 | bytes = 2; | |
263 | ptr += 2; | |
264 | } | |
265 | else | |
ea1562b3 | 266 | as_bad (_("syntax error after <exp")); |
252b5132 RH |
267 | } |
268 | else | |
269 | { | |
270 | amode = ADDR_ABS; | |
271 | bytes = 2; | |
272 | } | |
273 | } | |
274 | else if (ptr[0] == '>') | |
275 | { | |
276 | ptr = parse_exp (ptr + 1); | |
277 | if (ptr[0] == ',' && ptr[1] == 'x') | |
278 | { | |
279 | amode = ADDR_ABS_LONG_IDX_X; | |
280 | bytes = 3; | |
281 | ptr += 2; | |
282 | } | |
283 | else | |
284 | { | |
285 | amode = ADDR_ABS_LONG; | |
286 | bytes = 3; | |
287 | } | |
288 | } | |
289 | else if (ptr[0] == '<') | |
290 | { | |
291 | ptr = parse_exp (ptr + 1); | |
292 | if (ptr[0] == ',') | |
293 | { | |
294 | if (ptr[1] == 'y') | |
295 | { | |
296 | amode = ADDR_DIR_IDX_Y; | |
297 | ptr += 2; | |
298 | bytes = 2; | |
299 | } | |
300 | else if (ptr[1] == 'x') | |
301 | { | |
302 | amode = ADDR_DIR_IDX_X; | |
303 | ptr += 2; | |
304 | bytes = 2; | |
305 | } | |
306 | else | |
ea1562b3 | 307 | as_bad (_("syntax error after <exp")); |
252b5132 RH |
308 | } |
309 | else | |
310 | { | |
311 | amode = ADDR_DIR; | |
312 | bytes = 1; | |
313 | } | |
314 | } | |
315 | else if (ptr[0] == 'a') | |
ea1562b3 | 316 | amode = ADDR_ACC; |
252b5132 RH |
317 | else if (ptr[0] == '(') |
318 | { | |
319 | /* Look for (exp),y | |
320 | (<exp),y | |
321 | (exp,x) | |
322 | (<exp,x) | |
323 | (exp) | |
324 | (!exp) | |
325 | (exp) | |
326 | (<exp) | |
327 | (exp,x) | |
328 | (!exp,x) | |
329 | (exp,s) | |
330 | (exp,s),y */ | |
331 | ||
332 | ptr++; | |
333 | if (ptr[0] == '<') | |
334 | { | |
335 | override_len = 1; | |
336 | ptr++; | |
337 | } | |
338 | else if (ptr[0] == '!') | |
339 | { | |
340 | override_len = 2; | |
341 | ptr++; | |
342 | } | |
343 | else if (ptr[0] == '>') | |
344 | { | |
345 | override_len = 3; | |
346 | ptr++; | |
347 | } | |
348 | else | |
ea1562b3 NC |
349 | override_len = 0; |
350 | ||
252b5132 RH |
351 | ptr = parse_exp (ptr); |
352 | ||
353 | if (ptr[0] == ',') | |
354 | { | |
355 | ptr++; | |
356 | if (ptr[0] == 'x' && ptr[1] == ')') | |
357 | { | |
358 | ptr += 2; | |
359 | ||
360 | if (override_len == 1) | |
361 | { | |
362 | amode = ADDR_DIR_IDX_IND_X; | |
363 | bytes = 2; | |
364 | } | |
365 | else | |
366 | { | |
367 | amode = ADDR_ABS_IND_IDX; | |
368 | bytes = 2; | |
369 | } | |
370 | } | |
8098403c NC |
371 | else if (ptr[0] == 's' && ptr[1] == ')' |
372 | && ptr[2] == ',' && ptr[3] == 'y') | |
252b5132 RH |
373 | { |
374 | amode = ADDR_STACK_REL_INDX_IDX; | |
375 | bytes = 1; | |
376 | ptr += 4; | |
377 | } | |
378 | } | |
379 | else if (ptr[0] == ')') | |
380 | { | |
381 | if (ptr[1] == ',' && ptr[2] == 'y') | |
382 | { | |
383 | amode = ADDR_DIR_IND_IDX_Y; | |
384 | ptr += 3; | |
385 | bytes = 2; | |
386 | } | |
387 | else | |
388 | { | |
389 | if (override_len == 1) | |
390 | { | |
391 | amode = ADDR_DIR_IND; | |
392 | bytes = 1; | |
393 | } | |
394 | else | |
395 | { | |
396 | amode = ADDR_ABS_IND; | |
397 | bytes = 2; | |
398 | } | |
399 | ptr++; | |
252b5132 RH |
400 | } |
401 | } | |
252b5132 RH |
402 | } |
403 | else if (ptr[0] == '[') | |
404 | { | |
405 | ptr = parse_exp (ptr + 1); | |
ea1562b3 | 406 | |
252b5132 RH |
407 | if (ptr[0] == ']') |
408 | { | |
409 | ptr++; | |
410 | if (ptr[0] == ',' && ptr[1] == 'y') | |
411 | { | |
412 | bytes = 1; | |
413 | amode = ADDR_DIR_IND_IDX_Y_LONG; | |
414 | ptr += 2; | |
415 | } | |
416 | else | |
417 | { | |
8098403c | 418 | if (info->code == O_jmp) |
252b5132 | 419 | { |
8098403c NC |
420 | bytes = 2; |
421 | amode = ADDR_ABS_IND_LONG; | |
422 | } | |
423 | else | |
424 | { | |
425 | bytes = 1; | |
426 | amode = ADDR_DIR_IND_LONG; | |
427 | } | |
252b5132 RH |
428 | } |
429 | } | |
430 | } | |
431 | else | |
432 | { | |
6297c1fe | 433 | ptr = parse_exp (ptr); |
252b5132 RH |
434 | if (ptr[0] == ',') |
435 | { | |
436 | if (ptr[1] == 'y') | |
437 | { | |
438 | if (override_len == 1) | |
439 | { | |
440 | bytes = 1; | |
441 | amode = ADDR_DIR_IDX_Y; | |
442 | } | |
443 | else | |
444 | { | |
445 | amode = ADDR_ABS_IDX_Y; | |
446 | bytes = 2; | |
447 | } | |
448 | ptr += 2; | |
449 | } | |
450 | else if (ptr[1] == 'x') | |
451 | { | |
452 | if (override_len == 1) | |
453 | { | |
454 | amode = ADDR_DIR_IDX_X; | |
455 | bytes = 1; | |
456 | } | |
457 | else | |
458 | { | |
459 | amode = ADDR_ABS_IDX_X; | |
460 | bytes = 2; | |
461 | } | |
462 | ptr += 2; | |
463 | } | |
464 | else if (ptr[1] == 's') | |
465 | { | |
466 | bytes = 1; | |
467 | amode = ADDR_STACK_REL; | |
468 | ptr += 2; | |
469 | } | |
470 | else | |
471 | { | |
472 | bytes = 1; | |
473 | immediate1 = immediate; | |
474 | ptr = parse_exp (ptr + 1); | |
475 | amode = ADDR_BLOCK_MOVE; | |
476 | } | |
477 | } | |
478 | else | |
479 | { | |
480 | switch (info->amode) | |
481 | { | |
482 | case ADDR_PC_REL: | |
483 | amode = ADDR_PC_REL; | |
484 | bytes = 1; | |
485 | break; | |
486 | case ADDR_PC_REL_LONG: | |
487 | amode = ADDR_PC_REL_LONG; | |
488 | bytes = 2; | |
489 | break; | |
490 | default: | |
491 | if (override_len == 1) | |
492 | { | |
493 | amode = ADDR_DIR; | |
494 | bytes = 1; | |
495 | } | |
496 | else if (override_len == 3) | |
497 | { | |
498 | bytes = 3; | |
499 | amode = ADDR_ABS_LONG; | |
500 | } | |
501 | else | |
502 | { | |
503 | amode = ADDR_ABS; | |
504 | bytes = 2; | |
505 | } | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
510 | switch (bytes) | |
511 | { | |
512 | case 1: | |
513 | switch (expr_shift) | |
514 | { | |
515 | case 0: | |
516 | if (amode == ADDR_DIR) | |
8098403c | 517 | tc_cons_reloc = R_W65_DP; |
252b5132 | 518 | else |
8098403c | 519 | tc_cons_reloc = R_W65_ABS8; |
252b5132 RH |
520 | break; |
521 | case 1: | |
522 | tc_cons_reloc = R_W65_ABS8S8; | |
523 | break; | |
524 | case 2: | |
525 | tc_cons_reloc = R_W65_ABS8S16; | |
526 | break; | |
527 | } | |
528 | break; | |
529 | case 2: | |
530 | switch (expr_shift) | |
531 | { | |
532 | case 0: | |
533 | tc_cons_reloc = R_W65_ABS16; | |
534 | break; | |
535 | case 1: | |
536 | tc_cons_reloc = R_W65_ABS16S8; | |
537 | break; | |
538 | case 2: | |
539 | tc_cons_reloc = R_W65_ABS16S16; | |
540 | break; | |
541 | } | |
542 | } | |
543 | return ptr; | |
544 | } | |
545 | ||
546 | /* Passed a pointer to a list of opcodes which use different | |
547 | addressing modes, return the opcode which matches the opcodes | |
8098403c | 548 | provided. */ |
252b5132 | 549 | |
6297c1fe | 550 | static const struct opinfo * |
ea1562b3 | 551 | get_specific (const struct opinfo *opcode) |
252b5132 RH |
552 | { |
553 | int ocode = opcode->code; | |
554 | ||
555 | for (; opcode->code == ocode; opcode++) | |
ea1562b3 NC |
556 | if (opcode->amode == amode) |
557 | return opcode; | |
558 | ||
252b5132 RH |
559 | return 0; |
560 | } | |
561 | ||
1994a7c7 | 562 | /* Now we know what sort of opcodes it is, let's build the bytes. */ |
8098403c | 563 | |
252b5132 | 564 | static void |
ea1562b3 | 565 | build_Mytes (const struct opinfo *opcode) |
252b5132 RH |
566 | { |
567 | int size; | |
568 | int type; | |
569 | int pcrel; | |
570 | char *output; | |
571 | ||
572 | if (opcode->amode == ADDR_IMPLIED) | |
ea1562b3 NC |
573 | output = frag_more (1); |
574 | ||
252b5132 RH |
575 | else if (opcode->amode == ADDR_PC_REL) |
576 | { | |
577 | int type; | |
8098403c NC |
578 | |
579 | /* This is a relaxable insn, so we do some special handling. */ | |
252b5132 RH |
580 | type = opcode->val == OP_BRA ? UNCOND_BRANCH : COND_BRANCH; |
581 | output = frag_var (rs_machine_dependent, | |
582 | md_relax_table[C (type, WORD_DISP)].rlx_length, | |
583 | md_relax_table[C (type, BYTE_DISP)].rlx_length, | |
584 | C (type, UNDEF_BYTE_DISP), | |
585 | immediate.X_add_symbol, | |
586 | immediate.X_add_number, | |
587 | 0); | |
588 | } | |
589 | else | |
590 | { | |
591 | switch (opcode->amode) | |
592 | { | |
593 | GETINFO (size, type, pcrel); | |
6297c1fe AM |
594 | default: |
595 | abort (); | |
252b5132 RH |
596 | } |
597 | ||
8098403c NC |
598 | /* If something special was done in the expression modify the |
599 | reloc type. */ | |
252b5132 | 600 | if (tc_cons_reloc) |
8098403c | 601 | type = tc_cons_reloc; |
252b5132 | 602 | |
8098403c | 603 | /* 1 byte for the opcode + the bytes for the addrmode. */ |
252b5132 RH |
604 | output = frag_more (size + 1); |
605 | ||
606 | if (opcode->amode == ADDR_BLOCK_MOVE) | |
607 | { | |
8098403c | 608 | /* Two relocs for this one. */ |
252b5132 RH |
609 | fix_new_exp (frag_now, |
610 | output + 1 - frag_now->fr_literal, | |
611 | 1, | |
612 | &immediate, | |
613 | 0, | |
614 | R_W65_ABS8S16); | |
615 | ||
616 | fix_new_exp (frag_now, | |
617 | output + 2 - frag_now->fr_literal, | |
618 | 1, | |
619 | &immediate1, | |
620 | 0, | |
621 | R_W65_ABS8S16); | |
622 | } | |
623 | else if (type >= 0 | |
624 | && opcode->amode != ADDR_IMPLIED | |
625 | && opcode->amode != ADDR_ACC | |
626 | && opcode->amode != ADDR_STACK) | |
627 | { | |
628 | fix_new_exp (frag_now, | |
629 | output + 1 - frag_now->fr_literal, | |
630 | size, | |
631 | &immediate, | |
632 | pcrel, | |
633 | type); | |
634 | } | |
635 | } | |
636 | output[0] = opcode->val; | |
637 | } | |
638 | ||
8098403c NC |
639 | /* This is the guts of the machine-dependent assembler. STR points to |
640 | a machine dependent instruction. This function is supposed to emit | |
641 | the frags/bytes it assembles to. */ | |
252b5132 RH |
642 | |
643 | void | |
ea1562b3 | 644 | md_assemble (char *str) |
252b5132 | 645 | { |
6297c1fe | 646 | const struct opinfo *opcode; |
252b5132 | 647 | char name[20]; |
252b5132 RH |
648 | |
649 | /* Drop leading whitespace */ | |
650 | while (*str == ' ') | |
651 | str++; | |
652 | ||
ea1562b3 | 653 | /* All opcodes are three letters. */ |
252b5132 RH |
654 | name[0] = str[0]; |
655 | name[1] = str[1]; | |
656 | name[2] = str[2]; | |
657 | name[3] = 0; | |
658 | ||
659 | tc_cons_reloc = 0; | |
660 | str += 3; | |
661 | opcode = (struct opinfo *) hash_find (opcode_hash_control, name); | |
662 | ||
663 | if (opcode == NULL) | |
664 | { | |
665 | as_bad (_("unknown opcode")); | |
666 | return; | |
667 | } | |
668 | ||
669 | if (opcode->amode != ADDR_IMPLIED | |
670 | && opcode->amode != ADDR_STACK) | |
671 | { | |
672 | get_operands (opcode, str); | |
673 | opcode = get_specific (opcode); | |
674 | } | |
675 | ||
676 | if (opcode == 0) | |
677 | { | |
8098403c | 678 | /* Couldn't find an opcode which matched the operands. */ |
252b5132 RH |
679 | |
680 | char *where = frag_more (1); | |
681 | ||
682 | where[0] = 0x0; | |
683 | where[1] = 0x0; | |
684 | as_bad (_("invalid operands for opcode")); | |
685 | return; | |
686 | } | |
687 | ||
688 | build_Mytes (opcode); | |
689 | } | |
690 | ||
252b5132 | 691 | symbolS * |
ea1562b3 | 692 | md_undefined_symbol (char *name ATTRIBUTE_UNUSED) |
252b5132 RH |
693 | { |
694 | return 0; | |
695 | } | |
696 | ||
8098403c NC |
697 | /* Various routines to kill one day. */ |
698 | /* Equal to MAX_PRECISION in atof-ieee.c. */ | |
252b5132 RH |
699 | #define MAX_LITTLENUMS 6 |
700 | ||
8098403c NC |
701 | /* Turn a string in input_line_pointer into a floating point constant |
702 | of type TYPE, and store the appropriate bytes in *LITP. The number | |
703 | of LITTLENUMS emitted is stored in *SIZEP. An error message is | |
704 | returned, or NULL on OK. */ | |
705 | ||
252b5132 | 706 | char * |
ea1562b3 | 707 | md_atof (int type, char *litP, int *sizeP) |
252b5132 RH |
708 | { |
709 | int prec; | |
710 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
711 | LITTLENUM_TYPE *wordP; | |
712 | char *t; | |
252b5132 RH |
713 | |
714 | switch (type) | |
715 | { | |
716 | case 'f': | |
717 | case 'F': | |
718 | case 's': | |
719 | case 'S': | |
720 | prec = 2; | |
721 | break; | |
722 | ||
723 | case 'd': | |
724 | case 'D': | |
725 | case 'r': | |
726 | case 'R': | |
727 | prec = 4; | |
728 | break; | |
729 | ||
730 | case 'x': | |
731 | case 'X': | |
732 | prec = 6; | |
733 | break; | |
734 | ||
735 | case 'p': | |
736 | case 'P': | |
737 | prec = 6; | |
738 | break; | |
739 | ||
740 | default: | |
741 | *sizeP = 0; | |
742 | return _("Bad call to MD_NTOF()"); | |
743 | } | |
744 | t = atof_ieee (input_line_pointer, type, words); | |
745 | if (t) | |
746 | input_line_pointer = t; | |
747 | ||
748 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
749 | for (wordP = words + prec - 1; prec--;) | |
750 | { | |
751 | md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE)); | |
752 | litP += sizeof (LITTLENUM_TYPE); | |
753 | } | |
754 | return 0; | |
755 | } | |
756 | ||
757 | int | |
ea1562b3 | 758 | md_parse_option (int c ATTRIBUTE_UNUSED, char *a ATTRIBUTE_UNUSED) |
252b5132 | 759 | { |
a2199cc1 | 760 | return 0; |
252b5132 RH |
761 | } |
762 | ||
8098403c NC |
763 | /* Called after relaxing, change the frags so they know how big they |
764 | are. */ | |
765 | ||
252b5132 | 766 | void |
ea1562b3 NC |
767 | md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED, |
768 | segT seg ATTRIBUTE_UNUSED, | |
769 | fragS *fragP) | |
252b5132 RH |
770 | { |
771 | int disp_size = 0; | |
772 | int inst_size = 0; | |
8098403c NC |
773 | unsigned char *buffer = |
774 | (unsigned char *) (fragP->fr_fix + fragP->fr_literal); | |
252b5132 RH |
775 | |
776 | switch (fragP->fr_subtype) | |
777 | { | |
778 | case C (COND_BRANCH, BYTE_DISP): | |
779 | case C (UNCOND_BRANCH, BYTE_DISP): | |
780 | disp_size = 1; | |
781 | inst_size = 1; | |
782 | break; | |
783 | ||
8098403c | 784 | /* Conditional branches to a known 16 bit displacement. */ |
252b5132 RH |
785 | case C (COND_BRANCH, WORD_DISP): |
786 | switch (buffer[0]) | |
787 | { | |
788 | case OP_BCC: | |
789 | case OP_BCS: | |
790 | case OP_BEQ: | |
791 | case OP_BMI: | |
792 | case OP_BNE: | |
793 | case OP_BPL: | |
794 | case OP_BVS: | |
795 | case OP_BVC: | |
ea1562b3 | 796 | /* Invert the sense of the test. */ |
252b5132 | 797 | buffer[0] ^= 0x20; |
ea1562b3 | 798 | buffer[1] = 3; /* Jump over following brl. */ |
252b5132 RH |
799 | buffer[2] = OP_BRL; |
800 | buffer[3] = 0; | |
801 | buffer[4] = 0; | |
802 | disp_size = 2; | |
803 | inst_size = 3; | |
804 | break; | |
805 | default: | |
806 | abort (); | |
807 | } | |
808 | break; | |
809 | case C (UNCOND_BRANCH, WORD_DISP): | |
8098403c | 810 | /* Unconditional branches to a known 16 bit displacement. */ |
252b5132 RH |
811 | |
812 | switch (buffer[0]) | |
813 | { | |
814 | case OP_BRA: | |
815 | buffer[0] = OP_BRL; | |
816 | disp_size = 2; | |
817 | inst_size = 1; | |
818 | break; | |
819 | default: | |
820 | abort (); | |
821 | } | |
822 | break; | |
8098403c | 823 | /* Got to create a branch over a reloc here. */ |
252b5132 | 824 | case C (COND_BRANCH, UNDEF_WORD_DISP): |
ea1562b3 | 825 | buffer[0] ^= 0x20; /* Invert test. */ |
252b5132 RH |
826 | buffer[1] = 3; |
827 | buffer[2] = OP_BRL; | |
828 | buffer[3] = 0; | |
829 | buffer[4] = 0; | |
830 | fix_new (fragP, | |
831 | fragP->fr_fix + 3, | |
832 | 4, | |
833 | fragP->fr_symbol, | |
834 | fragP->fr_offset, | |
835 | 0, | |
836 | R_W65_PCR16); | |
837 | ||
838 | fragP->fr_fix += disp_size + inst_size; | |
839 | fragP->fr_var = 0; | |
840 | break; | |
841 | case C (UNCOND_BRANCH, UNDEF_WORD_DISP): | |
842 | buffer[0] = OP_BRL; | |
843 | buffer[1] = 0; | |
844 | buffer[2] = 0; | |
845 | fix_new (fragP, | |
846 | fragP->fr_fix + 1, | |
847 | 4, | |
848 | fragP->fr_symbol, | |
849 | fragP->fr_offset, | |
850 | 0, | |
851 | R_W65_PCR16); | |
852 | ||
853 | fragP->fr_fix += disp_size + inst_size; | |
854 | fragP->fr_var = 0; | |
855 | break; | |
856 | default: | |
857 | abort (); | |
858 | } | |
859 | if (inst_size) | |
860 | { | |
8098403c NC |
861 | /* Get the address of the end of the instruction. */ |
862 | int next_inst = (fragP->fr_fix + fragP->fr_address | |
863 | + disp_size + inst_size); | |
252b5132 RH |
864 | int targ_addr = (S_GET_VALUE (fragP->fr_symbol) + |
865 | fragP->fr_offset); | |
866 | int disp = targ_addr - next_inst; | |
867 | ||
2132e3a3 | 868 | md_number_to_chars ((char *) buffer + inst_size, disp, disp_size); |
252b5132 RH |
869 | fragP->fr_fix += disp_size + inst_size; |
870 | fragP->fr_var = 0; | |
871 | } | |
872 | } | |
873 | ||
252b5132 | 874 | valueT |
ea1562b3 | 875 | md_section_align (segT seg, valueT size) |
252b5132 RH |
876 | { |
877 | return ((size + (1 << section_alignment[(int) seg]) - 1) | |
878 | & (-1 << section_alignment[(int) seg])); | |
252b5132 RH |
879 | } |
880 | ||
881 | void | |
ea1562b3 | 882 | md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED) |
252b5132 | 883 | { |
94f592af | 884 | long val = * (long *) valP; |
252b5132 RH |
885 | char *buf = fixP->fx_where + fixP->fx_frag->fr_literal; |
886 | int addr = fixP->fx_frag->fr_address + fixP->fx_where; | |
887 | ||
888 | if (fixP->fx_r_type == 0) | |
889 | { | |
890 | if (fixP->fx_size == 1) | |
891 | fixP->fx_r_type = R_W65_ABS8; | |
892 | else | |
893 | fixP->fx_r_type = R_W65_ABS16; | |
894 | } | |
895 | ||
896 | switch (fixP->fx_r_type) | |
897 | { | |
898 | case R_W65_ABS8S16: | |
899 | val >>= 8; | |
900 | case R_W65_ABS8S8: | |
901 | val >>= 8; | |
902 | case R_W65_ABS8: | |
903 | *buf++ = val; | |
904 | break; | |
905 | case R_W65_ABS16S16: | |
906 | val >>= 8; | |
907 | case R_W65_ABS16S8: | |
908 | val >>= 8; | |
909 | case R_W65_ABS16: | |
910 | *buf++ = val >> 0; | |
911 | *buf++ = val >> 8; | |
912 | break; | |
913 | case R_W65_ABS24: | |
914 | *buf++ = val >> 0; | |
915 | *buf++ = val >> 8; | |
916 | *buf++ = val >> 16; | |
917 | break; | |
918 | case R_W65_PCR8: | |
919 | *buf++ = val - addr - 1; | |
920 | break; | |
921 | case R_W65_PCR16: | |
922 | val = val - addr - 1; | |
923 | *buf++ = val; | |
924 | *buf++ = val >> 8; | |
925 | break; | |
926 | case R_W65_DP: | |
927 | *buf++ = val; | |
928 | break; | |
929 | ||
930 | default: | |
931 | abort (); | |
932 | } | |
94f592af NC |
933 | |
934 | if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0) | |
935 | fixP->fx_done = 1; | |
252b5132 RH |
936 | } |
937 | ||
94f592af | 938 | /* Put number into target byte order. */ |
252b5132 RH |
939 | |
940 | void | |
ea1562b3 | 941 | md_number_to_chars (char *ptr, valueT use, int nbytes) |
252b5132 RH |
942 | { |
943 | number_to_chars_littleendian (ptr, use, nbytes); | |
944 | } | |
945 | ||
946 | long | |
ea1562b3 | 947 | md_pcrel_from (fixS *fixP) |
252b5132 RH |
948 | { |
949 | int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1; | |
950 | return gap; | |
951 | } | |
952 | ||
953 | void | |
ea1562b3 | 954 | tc_coff_symbol_emit_hook (symbolS *x ATTRIBUTE_UNUSED) |
252b5132 RH |
955 | { |
956 | } | |
957 | ||
958 | short | |
ea1562b3 | 959 | tc_coff_fix2rtype (fixS *fix_ptr) |
252b5132 RH |
960 | { |
961 | return fix_ptr->fx_r_type; | |
962 | } | |
963 | ||
964 | void | |
ea1562b3 NC |
965 | tc_reloc_mangle (fixS *fix_ptr, |
966 | struct internal_reloc *intr, | |
967 | bfd_vma base) | |
252b5132 RH |
968 | |
969 | { | |
970 | symbolS *symbol_ptr; | |
971 | ||
972 | symbol_ptr = fix_ptr->fx_addsy; | |
973 | ||
974 | /* If this relocation is attached to a symbol then it's ok | |
975 | to output it */ | |
976 | if (fix_ptr->fx_r_type == RELOC_32) | |
977 | { | |
ea1562b3 | 978 | /* Cons likes to create reloc32's whatever the size of the reloc. */ |
252b5132 RH |
979 | switch (fix_ptr->fx_size) |
980 | { | |
981 | case 2: | |
982 | intr->r_type = R_IMM16; | |
983 | break; | |
984 | case 1: | |
985 | intr->r_type = R_IMM8; | |
986 | break; | |
987 | default: | |
988 | abort (); | |
989 | } | |
990 | } | |
991 | else | |
992 | { | |
993 | if (fix_ptr->fx_size == 4) | |
994 | intr->r_type = R_W65_ABS24; | |
995 | else | |
996 | intr->r_type = fix_ptr->fx_r_type; | |
997 | } | |
998 | ||
999 | intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base; | |
1000 | intr->r_offset = fix_ptr->fx_offset; | |
1001 | ||
1002 | /* Turn the segment of the symbol into an offset. */ | |
1003 | if (symbol_ptr) | |
1004 | { | |
1005 | symbolS *dot; | |
1006 | ||
1007 | dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot; | |
1008 | if (dot) | |
1009 | { | |
1010 | intr->r_offset += S_GET_VALUE (symbol_ptr); | |
1011 | intr->r_symndx = dot->sy_number; | |
1012 | } | |
1013 | else | |
ea1562b3 | 1014 | intr->r_symndx = symbol_ptr->sy_number; |
252b5132 RH |
1015 | } |
1016 | else | |
ea1562b3 | 1017 | intr->r_symndx = -1; |
252b5132 RH |
1018 | } |
1019 | ||
1020 | int | |
ea1562b3 | 1021 | tc_coff_sizemachdep (fragS *frag) |
252b5132 RH |
1022 | { |
1023 | return md_relax_table[frag->fr_subtype].rlx_length; | |
1024 | } | |
1025 | ||
8098403c NC |
1026 | /* Called just before address relaxation, return the length by which a |
1027 | fragment must grow to reach it's destination. */ | |
252b5132 | 1028 | |
252b5132 | 1029 | int |
ea1562b3 | 1030 | md_estimate_size_before_relax (fragS *fragP, segT segment_type) |
252b5132 | 1031 | { |
e66457fb | 1032 | int what; |
252b5132 RH |
1033 | |
1034 | switch (fragP->fr_subtype) | |
1035 | { | |
1036 | default: | |
1037 | abort (); | |
93c2a809 | 1038 | |
252b5132 RH |
1039 | case C (COND_BRANCH, UNDEF_BYTE_DISP): |
1040 | case C (UNCOND_BRANCH, UNDEF_BYTE_DISP): | |
e66457fb | 1041 | what = GET_WHAT (fragP->fr_subtype); |
8098403c | 1042 | /* Used to be a branch to somewhere which was unknown. */ |
252b5132 | 1043 | if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type) |
ea1562b3 NC |
1044 | /* Got a symbol and it's defined in this segment, become byte |
1045 | sized - maybe it will fix up. */ | |
1046 | fragP->fr_subtype = C (what, BYTE_DISP); | |
252b5132 | 1047 | else |
ea1562b3 NC |
1048 | /* Its got a segment, but its not ours, so it will always be |
1049 | long. */ | |
1050 | fragP->fr_subtype = C (what, UNDEF_WORD_DISP); | |
93c2a809 AM |
1051 | break; |
1052 | ||
1053 | case C (COND_BRANCH, BYTE_DISP): | |
1054 | case C (COND_BRANCH, WORD_DISP): | |
e66457fb | 1055 | case C (COND_BRANCH, UNDEF_WORD_DISP): |
93c2a809 AM |
1056 | case C (UNCOND_BRANCH, BYTE_DISP): |
1057 | case C (UNCOND_BRANCH, WORD_DISP): | |
e66457fb | 1058 | case C (UNCOND_BRANCH, UNDEF_WORD_DISP): |
93c2a809 | 1059 | /* When relaxing a section for the second time, we don't need to |
e66457fb | 1060 | do anything besides return the current size. */ |
93c2a809 | 1061 | break; |
252b5132 | 1062 | } |
e66457fb AM |
1063 | |
1064 | fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length; | |
252b5132 RH |
1065 | return fragP->fr_var; |
1066 | } | |
1067 | ||
5a38dc70 | 1068 | const char *md_shortopts = ""; |
ea1562b3 NC |
1069 | struct option md_longopts[] = |
1070 | { | |
252b5132 RH |
1071 | #define OPTION_RELAX (OPTION_MD_BASE) |
1072 | {NULL, no_argument, NULL, 0} | |
1073 | }; | |
1074 | ||
1075 | void | |
ea1562b3 | 1076 | md_show_usage (FILE *stream ATTRIBUTE_UNUSED) |
252b5132 | 1077 | { |
252b5132 RH |
1078 | } |
1079 | ||
8098403c | 1080 | size_t md_longopts_size = sizeof (md_longopts); |