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