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