Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* tc-z8k.c -- Assemble code for the Zilog Z800n |
7f31df7c | 2 | Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003 |
f7e42eb4 | 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 | ||
e0c6ed95 AM |
22 | /* Written By Steve Chamberlain <sac@cygnus.com>. */ |
23 | ||
252b5132 RH |
24 | #define DEFINE_TABLE |
25 | #include <stdio.h> | |
26 | ||
252b5132 RH |
27 | #include "as.h" |
28 | #include "bfd.h" | |
3882b010 | 29 | #include "safe-ctype.h" |
a5d2034a | 30 | #include "opcodes/z8k-opc.h" |
252b5132 | 31 | |
63a0b638 AM |
32 | const char comment_chars[] = "!"; |
33 | const char line_comment_chars[] = "#"; | |
34 | const char line_separator_chars[] = ";"; | |
252b5132 RH |
35 | |
36 | extern int machine; | |
37 | extern int coff_flags; | |
38 | int segmented_mode; | |
39 | const int md_reloc_size; | |
40 | ||
7f31df7c CG |
41 | /* This is non-zero if target was set from the command line. */ |
42 | static int z8k_target_from_cmdline; | |
43 | ||
78a33af2 | 44 | static void |
464800ca | 45 | s_segm (int segm) |
252b5132 | 46 | { |
7f31df7c CG |
47 | if (segm) |
48 | { | |
d5bf5799 CG |
49 | segmented_mode = 1; |
50 | machine = bfd_mach_z8001; | |
51 | coff_flags = F_Z8001; | |
7f31df7c CG |
52 | } |
53 | else | |
54 | { | |
d5bf5799 CG |
55 | segmented_mode = 0; |
56 | machine = bfd_mach_z8002; | |
57 | coff_flags = F_Z8002; | |
7f31df7c | 58 | } |
252b5132 RH |
59 | } |
60 | ||
e0c6ed95 | 61 | static void |
464800ca | 62 | even (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
63 | { |
64 | frag_align (1, 0, 0); | |
65 | record_alignment (now_seg, 1); | |
66 | } | |
67 | ||
78a33af2 | 68 | static int |
464800ca | 69 | tohex (int c) |
252b5132 | 70 | { |
3882b010 | 71 | if (ISDIGIT (c)) |
252b5132 | 72 | return c - '0'; |
3882b010 | 73 | if (ISLOWER (c)) |
252b5132 RH |
74 | return c - 'a' + 10; |
75 | return c - 'A' + 10; | |
76 | } | |
77 | ||
78a33af2 | 78 | static void |
464800ca | 79 | sval (int ignore ATTRIBUTE_UNUSED) |
252b5132 | 80 | { |
252b5132 RH |
81 | SKIP_WHITESPACE (); |
82 | if (*input_line_pointer == '\'') | |
83 | { | |
84 | int c; | |
85 | input_line_pointer++; | |
86 | c = *input_line_pointer++; | |
87 | while (c != '\'') | |
88 | { | |
89 | if (c == '%') | |
90 | { | |
91 | c = (tohex (input_line_pointer[0]) << 4) | |
92 | | tohex (input_line_pointer[1]); | |
93 | input_line_pointer += 2; | |
94 | } | |
95 | FRAG_APPEND_1_CHAR (c); | |
96 | c = *input_line_pointer++; | |
97 | } | |
98 | demand_empty_rest_of_line (); | |
99 | } | |
252b5132 | 100 | } |
e0c6ed95 AM |
101 | |
102 | /* This table describes all the machine specific pseudo-ops the assembler | |
103 | has to support. The fields are: | |
104 | pseudo-op name without dot | |
105 | function to call to execute this pseudo-op | |
106 | Integer arg to pass to the function | |
107 | */ | |
108 | ||
109 | const pseudo_typeS md_pseudo_table[] = { | |
110 | {"int" , cons , 2}, | |
111 | {"data.b" , cons , 1}, | |
112 | {"data.w" , cons , 2}, | |
113 | {"data.l" , cons , 4}, | |
114 | {"form" , listing_psize , 0}, | |
115 | {"heading", listing_title , 0}, | |
116 | {"import" , s_ignore , 0}, | |
117 | {"page" , listing_eject , 0}, | |
118 | {"program", s_ignore , 0}, | |
7f31df7c CG |
119 | {"z8001" , s_segm , 1}, |
120 | {"z8002" , s_segm , 0}, | |
e0c6ed95 | 121 | |
7f31df7c CG |
122 | {"segm" , s_segm , 1}, |
123 | {"unsegm" , s_segm , 0}, | |
124 | {"unseg" , s_segm , 0}, | |
e0c6ed95 AM |
125 | {"name" , s_app_file , 0}, |
126 | {"global" , s_globl , 0}, | |
127 | {"wval" , cons , 2}, | |
128 | {"lval" , cons , 4}, | |
129 | {"bval" , cons , 1}, | |
130 | {"sval" , sval , 0}, | |
131 | {"rsect" , obj_coff_section, 0}, | |
132 | {"sect" , obj_coff_section, 0}, | |
133 | {"block" , s_space , 0}, | |
134 | {"even" , even , 0}, | |
135 | {0 , 0 , 0} | |
252b5132 RH |
136 | }; |
137 | ||
138 | const char EXP_CHARS[] = "eE"; | |
139 | ||
e0c6ed95 AM |
140 | /* Chars that mean this number is a floating point constant. |
141 | As in 0f12.456 | |
142 | or 0d1.2345e12 */ | |
252b5132 RH |
143 | const char FLT_CHARS[] = "rRsSfFdDxXpP"; |
144 | ||
e0c6ed95 AM |
145 | /* Opcode mnemonics. */ |
146 | static struct hash_control *opcode_hash_control; | |
252b5132 RH |
147 | |
148 | void | |
464800ca | 149 | md_begin (void) |
252b5132 | 150 | { |
78a33af2 AM |
151 | const opcode_entry_type *opcode; |
152 | int idx = -1; | |
252b5132 RH |
153 | |
154 | opcode_hash_control = hash_new (); | |
155 | ||
156 | for (opcode = z8k_table; opcode->name; opcode++) | |
157 | { | |
e0c6ed95 | 158 | /* Only enter unique codes into the table. */ |
78a33af2 AM |
159 | if (idx != opcode->idx) |
160 | hash_insert (opcode_hash_control, opcode->name, (char *) opcode); | |
161 | idx = opcode->idx; | |
252b5132 RH |
162 | } |
163 | ||
e0c6ed95 | 164 | /* Default to z8002. */ |
7f31df7c CG |
165 | if (! z8k_target_from_cmdline) |
166 | s_segm (0); | |
252b5132 | 167 | |
e0c6ed95 | 168 | /* Insert the pseudo ops, too. */ |
252b5132 RH |
169 | for (idx = 0; md_pseudo_table[idx].poc_name; idx++) |
170 | { | |
171 | opcode_entry_type *fake_opcode; | |
172 | fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type)); | |
879db8be NC |
173 | fake_opcode->name = md_pseudo_table[idx].poc_name; |
174 | fake_opcode->func = (void *) (md_pseudo_table + idx); | |
252b5132 RH |
175 | fake_opcode->opcode = 250; |
176 | hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode); | |
177 | } | |
252b5132 RH |
178 | } |
179 | ||
19d63e5d | 180 | struct z8k_exp { |
252b5132 RH |
181 | char *e_beg; |
182 | char *e_end; | |
183 | expressionS e_exp; | |
184 | }; | |
e0c6ed95 | 185 | |
19d63e5d | 186 | typedef struct z8k_op { |
f590b86e CG |
187 | /* CLASS_REG_xxx. */ |
188 | int regsize; | |
e0c6ed95 AM |
189 | |
190 | /* 0 .. 15. */ | |
191 | unsigned int reg; | |
252b5132 RH |
192 | |
193 | int mode; | |
194 | ||
e0c6ed95 AM |
195 | /* Any other register associated with the mode. */ |
196 | unsigned int x_reg; | |
197 | ||
198 | /* Any expression. */ | |
199 | expressionS exp; | |
19d63e5d | 200 | } op_type; |
252b5132 RH |
201 | |
202 | static expressionS *da_operand; | |
203 | static expressionS *imm_operand; | |
204 | ||
464800ca CG |
205 | static int reg[16]; |
206 | static int the_cc; | |
207 | static int the_ctrl; | |
208 | static int the_flags; | |
209 | static int the_interrupt; | |
78a33af2 AM |
210 | |
211 | static char * | |
464800ca | 212 | whatreg (int *reg, char *src) |
252b5132 | 213 | { |
3882b010 | 214 | if (ISDIGIT (src[1])) |
252b5132 RH |
215 | { |
216 | *reg = (src[0] - '0') * 10 + src[1] - '0'; | |
217 | return src + 2; | |
218 | } | |
219 | else | |
220 | { | |
221 | *reg = (src[0] - '0'); | |
222 | return src + 1; | |
223 | } | |
224 | } | |
225 | ||
e0c6ed95 | 226 | /* Parse operands |
252b5132 | 227 | |
e0c6ed95 AM |
228 | rh0-rh7, rl0-rl7 |
229 | r0-r15 | |
230 | rr0-rr14 | |
231 | rq0--rq12 | |
232 | WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp | |
233 | r0l,r0h,..r7l,r7h | |
234 | @WREG | |
235 | @WREG+ | |
236 | @-WREG | |
237 | #const | |
238 | */ | |
252b5132 | 239 | |
bc0d738a NC |
240 | /* Try to parse a reg name. Return a pointer to the first character |
241 | in SRC after the reg name. */ | |
242 | ||
78a33af2 | 243 | static char * |
464800ca | 244 | parse_reg (char *src, int *mode, unsigned int *reg) |
252b5132 RH |
245 | { |
246 | char *res = 0; | |
247 | char regno; | |
248 | ||
f69532ae CG |
249 | /* Check for stack pointer "sp" alias. */ |
250 | if ((src[0] == 's' || src[0] == 'S') | |
251 | && (src[1] == 'p' || src[1] == 'P') | |
252 | && (src[2] == 0 || src[2] == ',')) | |
252b5132 RH |
253 | { |
254 | if (segmented_mode) | |
e0c6ed95 AM |
255 | { |
256 | *mode = CLASS_REG_LONG; | |
257 | *reg = 14; | |
258 | } | |
252b5132 | 259 | else |
e0c6ed95 AM |
260 | { |
261 | *mode = CLASS_REG_WORD; | |
262 | *reg = 15; | |
263 | } | |
252b5132 RH |
264 | return src + 2; |
265 | } | |
f69532ae CG |
266 | |
267 | if (src[0] == 'r' || src[0] == 'R') | |
252b5132 | 268 | { |
f69532ae | 269 | if (src[1] == 'r' || src[1] == 'R') |
e0c6ed95 | 270 | { |
28bab82b NC |
271 | if (src[2] < '0' || src[2] > '9') |
272 | return res; /* Assume no register name but a label starting with 'rr'. */ | |
e0c6ed95 AM |
273 | *mode = CLASS_REG_LONG; |
274 | res = whatreg (reg, src + 2); | |
252b5132 RH |
275 | regno = *reg; |
276 | if (regno > 14) | |
f590b86e CG |
277 | as_bad (_("register rr%d out of range"), regno); |
278 | if (regno & 1) | |
279 | as_bad (_("register rr%d does not exist"), regno); | |
e0c6ed95 | 280 | } |
f69532ae | 281 | else if (src[1] == 'h' || src[1] == 'H') |
e0c6ed95 | 282 | { |
28bab82b NC |
283 | if (src[2] < '0' || src[2] > '9') |
284 | return res; /* Assume no register name but a label starting with 'rh'. */ | |
e0c6ed95 AM |
285 | *mode = CLASS_REG_BYTE; |
286 | res = whatreg (reg, src + 2); | |
252b5132 RH |
287 | regno = *reg; |
288 | if (regno > 7) | |
f590b86e | 289 | as_bad (_("register rh%d out of range"), regno); |
e0c6ed95 | 290 | } |
f69532ae | 291 | else if (src[1] == 'l' || src[1] == 'L') |
e0c6ed95 | 292 | { |
28bab82b NC |
293 | if (src[2] < '0' || src[2] > '9') |
294 | return res; /* Assume no register name but a label starting with 'rl'. */ | |
e0c6ed95 AM |
295 | *mode = CLASS_REG_BYTE; |
296 | res = whatreg (reg, src + 2); | |
252b5132 RH |
297 | regno = *reg; |
298 | if (regno > 7) | |
f590b86e | 299 | as_bad (_("register rl%d out of range"), regno); |
e0c6ed95 AM |
300 | *reg += 8; |
301 | } | |
f69532ae | 302 | else if (src[1] == 'q' || src[1] == 'Q') |
e0c6ed95 | 303 | { |
28bab82b NC |
304 | if (src[2] < '0' || src[2] > '9') |
305 | return res; /* Assume no register name but a label starting with 'rq'. */ | |
e0c6ed95 AM |
306 | *mode = CLASS_REG_QUAD; |
307 | res = whatreg (reg, src + 2); | |
252b5132 RH |
308 | regno = *reg; |
309 | if (regno > 12) | |
f590b86e CG |
310 | as_bad (_("register rq%d out of range"), regno); |
311 | if (regno & 3) | |
312 | as_bad (_("register rq%d does not exist"), regno); | |
e0c6ed95 | 313 | } |
252b5132 | 314 | else |
e0c6ed95 | 315 | { |
28bab82b NC |
316 | if (src[1] < '0' || src[1] > '9') |
317 | return res; /* Assume no register name but a label starting with 'r'. */ | |
e0c6ed95 AM |
318 | *mode = CLASS_REG_WORD; |
319 | res = whatreg (reg, src + 1); | |
252b5132 RH |
320 | regno = *reg; |
321 | if (regno > 15) | |
f590b86e | 322 | as_bad (_("register r%d out of range"), regno); |
e0c6ed95 | 323 | } |
252b5132 RH |
324 | } |
325 | return res; | |
252b5132 RH |
326 | } |
327 | ||
78a33af2 | 328 | static char * |
464800ca | 329 | parse_exp (char *s, expressionS *op) |
252b5132 RH |
330 | { |
331 | char *save = input_line_pointer; | |
332 | char *new; | |
333 | ||
334 | input_line_pointer = s; | |
335 | expression (op); | |
336 | if (op->X_op == O_absent) | |
337 | as_bad (_("missing operand")); | |
338 | new = input_line_pointer; | |
339 | input_line_pointer = save; | |
340 | return new; | |
341 | } | |
342 | ||
343 | /* The many forms of operand: | |
344 | ||
345 | <rb> | |
346 | <r> | |
347 | <rr> | |
348 | <rq> | |
349 | @r | |
350 | #exp | |
351 | exp | |
352 | exp(r) | |
353 | r(#exp) | |
354 | r(r) | |
252b5132 RH |
355 | */ |
356 | ||
e0c6ed95 | 357 | static char * |
464800ca | 358 | checkfor (char *ptr, char what) |
252b5132 RH |
359 | { |
360 | if (*ptr == what) | |
361 | ptr++; | |
362 | else | |
e0c6ed95 AM |
363 | as_bad (_("expected %c"), what); |
364 | ||
252b5132 RH |
365 | return ptr; |
366 | } | |
367 | ||
e0c6ed95 AM |
368 | /* Make sure the mode supplied is the size of a word. */ |
369 | ||
252b5132 | 370 | static void |
464800ca | 371 | regword (int mode, char *string) |
252b5132 RH |
372 | { |
373 | int ok; | |
374 | ||
375 | ok = CLASS_REG_WORD; | |
376 | if (ok != mode) | |
377 | { | |
378 | as_bad (_("register is wrong size for a word %s"), string); | |
379 | } | |
380 | } | |
381 | ||
e0c6ed95 AM |
382 | /* Make sure the mode supplied is the size of an address. */ |
383 | ||
252b5132 | 384 | static void |
464800ca | 385 | regaddr (int mode, char *string) |
252b5132 RH |
386 | { |
387 | int ok; | |
388 | ||
389 | ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD; | |
390 | if (ok != mode) | |
391 | { | |
392 | as_bad (_("register is wrong size for address %s"), string); | |
393 | } | |
394 | } | |
395 | ||
19d63e5d | 396 | struct ctrl_names { |
e0c6ed95 AM |
397 | int value; |
398 | char *name; | |
252b5132 RH |
399 | }; |
400 | ||
464800ca | 401 | static struct ctrl_names ctrl_table[] = { |
879db8be NC |
402 | { 0x2, "fcw" }, |
403 | { 0x3, "refresh" }, | |
404 | { 0x4, "psapseg" }, | |
405 | { 0x5, "psapoff" }, | |
406 | { 0x5, "psap" }, | |
407 | { 0x6, "nspseg" }, | |
408 | { 0x7, "nspoff" }, | |
409 | { 0x7, "nsp" }, | |
410 | { 0 , 0 } | |
252b5132 | 411 | }; |
e0c6ed95 | 412 | |
252b5132 | 413 | static void |
464800ca | 414 | get_ctrl_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED) |
252b5132 RH |
415 | { |
416 | char *src = *ptr; | |
f69532ae | 417 | int i, l; |
252b5132 RH |
418 | |
419 | while (*src == ' ') | |
420 | src++; | |
421 | ||
422 | mode->mode = CLASS_CTRL; | |
423 | for (i = 0; ctrl_table[i].name; i++) | |
424 | { | |
f69532ae CG |
425 | l = strlen (ctrl_table[i].name); |
426 | if (! strncasecmp (ctrl_table[i].name, src, l)) | |
427 | { | |
428 | the_ctrl = ctrl_table[i].value; | |
429 | if (*(src + l) && *(src + l) != ',') | |
430 | break; | |
431 | *ptr = src + l; /* Valid control name found: "consume" it. */ | |
432 | return; | |
433 | } | |
252b5132 RH |
434 | } |
435 | the_ctrl = 0; | |
436 | return; | |
437 | } | |
438 | ||
19d63e5d | 439 | struct flag_names { |
252b5132 RH |
440 | int value; |
441 | char *name; | |
252b5132 RH |
442 | }; |
443 | ||
464800ca | 444 | static struct flag_names flag_table[] = { |
879db8be NC |
445 | { 0x1, "p" }, |
446 | { 0x1, "v" }, | |
447 | { 0x2, "s" }, | |
448 | { 0x4, "z" }, | |
449 | { 0x8, "c" }, | |
450 | { 0x0, "+" }, | |
451 | { 0, 0 } | |
252b5132 RH |
452 | }; |
453 | ||
454 | static void | |
464800ca | 455 | get_flags_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED) |
252b5132 RH |
456 | { |
457 | char *src = *ptr; | |
252b5132 RH |
458 | int i; |
459 | int j; | |
460 | ||
461 | while (*src == ' ') | |
462 | src++; | |
463 | ||
464 | mode->mode = CLASS_FLAGS; | |
465 | the_flags = 0; | |
466 | for (j = 0; j <= 9; j++) | |
467 | { | |
e0c6ed95 | 468 | if (!src[j]) |
252b5132 | 469 | goto done; |
e0c6ed95 AM |
470 | for (i = 0; flag_table[i].name; i++) |
471 | { | |
472 | if (flag_table[i].name[0] == src[j]) | |
473 | { | |
474 | the_flags = the_flags | flag_table[i].value; | |
475 | goto match; | |
476 | } | |
477 | } | |
252b5132 RH |
478 | goto done; |
479 | match: | |
e0c6ed95 | 480 | ; |
252b5132 | 481 | } |
e0c6ed95 | 482 | done: |
252b5132 RH |
483 | *ptr = src + j; |
484 | return; | |
485 | } | |
486 | ||
19d63e5d | 487 | struct interrupt_names { |
252b5132 RH |
488 | int value; |
489 | char *name; | |
252b5132 RH |
490 | }; |
491 | ||
464800ca | 492 | static struct interrupt_names intr_table[] = { |
879db8be NC |
493 | { 0x1, "nvi" }, |
494 | { 0x2, "vi" }, | |
495 | { 0x3, "both" }, | |
496 | { 0x3, "all" }, | |
497 | { 0, 0 } | |
252b5132 RH |
498 | }; |
499 | ||
500 | static void | |
464800ca | 501 | get_interrupt_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED) |
252b5132 RH |
502 | { |
503 | char *src = *ptr; | |
252b5132 RH |
504 | int i; |
505 | ||
506 | while (*src == ' ') | |
507 | src++; | |
508 | ||
509 | mode->mode = CLASS_IMM; | |
510 | for (i = 0; intr_table[i].name; i++) | |
511 | { | |
512 | int j; | |
513 | ||
514 | for (j = 0; intr_table[i].name[j]; j++) | |
e0c6ed95 AM |
515 | { |
516 | if (intr_table[i].name[j] != src[j]) | |
517 | goto fail; | |
518 | } | |
252b5132 RH |
519 | the_interrupt = intr_table[i].value; |
520 | *ptr = src + j; | |
521 | return; | |
e0c6ed95 AM |
522 | fail: |
523 | ; | |
252b5132 | 524 | } |
7f31df7c | 525 | /* No interrupt type specified, opcode won't do anything. */ |
f590b86e | 526 | as_warn (_("opcode has no effect")); |
252b5132 RH |
527 | the_interrupt = 0x0; |
528 | return; | |
529 | } | |
530 | ||
19d63e5d | 531 | struct cc_names { |
252b5132 RH |
532 | int value; |
533 | char *name; | |
252b5132 RH |
534 | }; |
535 | ||
464800ca | 536 | static struct cc_names table[] = { |
879db8be NC |
537 | { 0x0, "f" }, |
538 | { 0x1, "lt" }, | |
539 | { 0x2, "le" }, | |
540 | { 0x3, "ule" }, | |
d5bf5799 | 541 | { 0x4, "ov/pe" }, |
879db8be | 542 | { 0x4, "ov" }, |
d5bf5799 | 543 | { 0x4, "pe/ov" }, |
879db8be NC |
544 | { 0x4, "pe" }, |
545 | { 0x5, "mi" }, | |
546 | { 0x6, "eq" }, | |
547 | { 0x6, "z" }, | |
d5bf5799 | 548 | { 0x7, "c/ult" }, |
879db8be | 549 | { 0x7, "c" }, |
d5bf5799 | 550 | { 0x7, "ult/c" }, |
879db8be NC |
551 | { 0x7, "ult" }, |
552 | { 0x8, "t" }, | |
553 | { 0x9, "ge" }, | |
554 | { 0xa, "gt" }, | |
555 | { 0xb, "ugt" }, | |
d5bf5799 | 556 | { 0xc, "nov/po" }, |
879db8be | 557 | { 0xc, "nov" }, |
d5bf5799 | 558 | { 0xc, "po/nov" }, |
879db8be NC |
559 | { 0xc, "po" }, |
560 | { 0xd, "pl" }, | |
561 | { 0xe, "ne" }, | |
562 | { 0xe, "nz" }, | |
d5bf5799 | 563 | { 0xf, "nc/uge" }, |
879db8be | 564 | { 0xf, "nc" }, |
d5bf5799 | 565 | { 0xf, "uge/nc" }, |
879db8be NC |
566 | { 0xf, "uge" }, |
567 | { 0 , 0 } | |
252b5132 RH |
568 | }; |
569 | ||
570 | static void | |
464800ca | 571 | get_cc_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED) |
252b5132 RH |
572 | { |
573 | char *src = *ptr; | |
d5bf5799 | 574 | int i, l; |
252b5132 RH |
575 | |
576 | while (*src == ' ') | |
577 | src++; | |
578 | ||
579 | mode->mode = CLASS_CC; | |
580 | for (i = 0; table[i].name; i++) | |
581 | { | |
d5bf5799 CG |
582 | l = strlen (table[i].name); |
583 | if (! strncasecmp (table[i].name, src, l)) | |
584 | { | |
585 | the_cc = table[i].value; | |
586 | if (*(src + l) && *(src + l) != ',') | |
587 | break; | |
588 | *ptr = src + l; /* Valid cc found: "consume" it. */ | |
589 | return; | |
590 | } | |
252b5132 | 591 | } |
d5bf5799 | 592 | the_cc = 0x8; /* Not recognizing the cc defaults to t. (Assuming no cc present.) */ |
252b5132 RH |
593 | } |
594 | ||
595 | static void | |
464800ca | 596 | get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED) |
252b5132 RH |
597 | { |
598 | char *src = *ptr; | |
599 | char *end; | |
252b5132 RH |
600 | |
601 | mode->mode = 0; | |
602 | ||
603 | while (*src == ' ') | |
604 | src++; | |
605 | if (*src == '#') | |
606 | { | |
607 | mode->mode = CLASS_IMM; | |
608 | imm_operand = &(mode->exp); | |
609 | src = parse_exp (src + 1, &(mode->exp)); | |
610 | } | |
611 | else if (*src == '@') | |
612 | { | |
252b5132 | 613 | mode->mode = CLASS_IR; |
f590b86e | 614 | src = parse_reg (src + 1, &mode->regsize, &mode->reg); |
252b5132 RH |
615 | } |
616 | else | |
617 | { | |
618 | int regn; | |
619 | ||
620 | end = parse_reg (src, &mode->mode, ®n); | |
621 | ||
622 | if (end) | |
623 | { | |
624 | int nw, nr; | |
625 | ||
626 | src = end; | |
627 | if (*src == '(') | |
628 | { | |
629 | src++; | |
630 | end = parse_reg (src, &nw, &nr); | |
631 | if (end) | |
632 | { | |
e0c6ed95 | 633 | /* Got Ra(Rb). */ |
252b5132 RH |
634 | src = end; |
635 | ||
636 | if (*src != ')') | |
e0c6ed95 | 637 | as_bad (_("Missing ) in ra(rb)")); |
252b5132 | 638 | else |
e0c6ed95 | 639 | src++; |
252b5132 RH |
640 | |
641 | regaddr (mode->mode, "ra(rb) ra"); | |
e0c6ed95 AM |
642 | #if 0 |
643 | regword (mode->mode, "ra(rb) rb"); | |
644 | #endif | |
252b5132 RH |
645 | mode->mode = CLASS_BX; |
646 | mode->reg = regn; | |
647 | mode->x_reg = nr; | |
648 | reg[ARG_RX] = nr; | |
649 | } | |
650 | else | |
651 | { | |
e0c6ed95 | 652 | /* Got Ra(disp). */ |
252b5132 RH |
653 | if (*src == '#') |
654 | src++; | |
655 | src = parse_exp (src, &(mode->exp)); | |
656 | src = checkfor (src, ')'); | |
657 | mode->mode = CLASS_BA; | |
658 | mode->reg = regn; | |
659 | mode->x_reg = 0; | |
660 | imm_operand = &(mode->exp); | |
661 | } | |
662 | } | |
663 | else | |
664 | { | |
665 | mode->reg = regn; | |
666 | mode->x_reg = 0; | |
667 | } | |
668 | } | |
669 | else | |
670 | { | |
e0c6ed95 | 671 | /* No initial reg. */ |
252b5132 RH |
672 | src = parse_exp (src, &(mode->exp)); |
673 | if (*src == '(') | |
674 | { | |
675 | src++; | |
676 | end = parse_reg (src, &(mode->mode), ®n); | |
677 | regword (mode->mode, "addr(Ra) ra"); | |
678 | mode->mode = CLASS_X; | |
679 | mode->reg = regn; | |
680 | mode->x_reg = 0; | |
681 | da_operand = &(mode->exp); | |
682 | src = checkfor (end, ')'); | |
683 | } | |
684 | else | |
685 | { | |
e0c6ed95 | 686 | /* Just an address. */ |
252b5132 RH |
687 | mode->mode = CLASS_DA; |
688 | mode->reg = 0; | |
689 | mode->x_reg = 0; | |
690 | da_operand = &(mode->exp); | |
691 | } | |
692 | } | |
693 | } | |
694 | *ptr = src; | |
695 | } | |
696 | ||
e0c6ed95 | 697 | static char * |
464800ca | 698 | get_operands (const opcode_entry_type *opcode, char *op_end, op_type *operand) |
252b5132 RH |
699 | { |
700 | char *ptr = op_end; | |
e0c6ed95 AM |
701 | char *savptr; |
702 | ||
252b5132 RH |
703 | switch (opcode->noperands) |
704 | { | |
705 | case 0: | |
706 | operand[0].mode = 0; | |
707 | operand[1].mode = 0; | |
d5bf5799 CG |
708 | while (*ptr == ' ') |
709 | ptr++; | |
252b5132 RH |
710 | break; |
711 | ||
712 | case 1: | |
252b5132 | 713 | if (opcode->arg_info[0] == CLASS_CC) |
d5bf5799 CG |
714 | { |
715 | get_cc_operand (&ptr, operand + 0, 0); | |
716 | while (*ptr == ' ') | |
717 | ptr++; | |
718 | if (*ptr && ! is_end_of_line[(unsigned char) *ptr]) | |
719 | { | |
720 | as_bad (_("invalid condition code '%s'"), ptr); | |
721 | while (*ptr && ! is_end_of_line[(unsigned char) *ptr]) | |
722 | ptr++; /* Consume rest of line. */ | |
723 | } | |
724 | } | |
252b5132 | 725 | else if (opcode->arg_info[0] == CLASS_FLAGS) |
14899840 | 726 | get_flags_operand (&ptr, operand + 0, 0); |
e0c6ed95 | 727 | else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2))) |
14899840 | 728 | get_interrupt_operand (&ptr, operand + 0, 0); |
252b5132 | 729 | else |
14899840 NC |
730 | get_operand (&ptr, operand + 0, 0); |
731 | ||
252b5132 RH |
732 | operand[1].mode = 0; |
733 | break; | |
734 | ||
735 | case 2: | |
252b5132 RH |
736 | savptr = ptr; |
737 | if (opcode->arg_info[0] == CLASS_CC) | |
d5bf5799 CG |
738 | { |
739 | get_cc_operand (&ptr, operand + 0, 0); | |
740 | while (*ptr == ' ') | |
741 | ptr++; | |
742 | if (*ptr != ',' && strchr (ptr + 1, ',')) | |
743 | { | |
744 | savptr = ptr; | |
745 | while (*ptr != ',') | |
746 | ptr++; | |
747 | *ptr = 0; | |
748 | ptr++; | |
749 | as_bad (_("invalid condition code '%s'"), savptr); | |
750 | } | |
751 | } | |
252b5132 | 752 | else if (opcode->arg_info[0] == CLASS_CTRL) |
e0c6ed95 AM |
753 | { |
754 | get_ctrl_operand (&ptr, operand + 0, 0); | |
14899840 | 755 | |
e0c6ed95 AM |
756 | if (the_ctrl == 0) |
757 | { | |
758 | ptr = savptr; | |
759 | get_operand (&ptr, operand + 0, 0); | |
14899840 | 760 | |
e0c6ed95 | 761 | if (ptr == 0) |
879db8be | 762 | return NULL; |
e0c6ed95 AM |
763 | if (*ptr == ',') |
764 | ptr++; | |
765 | get_ctrl_operand (&ptr, operand + 1, 1); | |
766 | return ptr; | |
767 | } | |
768 | } | |
252b5132 | 769 | else |
14899840 NC |
770 | get_operand (&ptr, operand + 0, 0); |
771 | ||
252b5132 | 772 | if (ptr == 0) |
879db8be | 773 | return NULL; |
252b5132 | 774 | if (*ptr == ',') |
e0c6ed95 | 775 | ptr++; |
252b5132 RH |
776 | get_operand (&ptr, operand + 1, 1); |
777 | break; | |
778 | ||
779 | case 3: | |
252b5132 RH |
780 | get_operand (&ptr, operand + 0, 0); |
781 | if (*ptr == ',') | |
782 | ptr++; | |
783 | get_operand (&ptr, operand + 1, 1); | |
784 | if (*ptr == ',') | |
785 | ptr++; | |
786 | get_operand (&ptr, operand + 2, 2); | |
787 | break; | |
788 | ||
789 | case 4: | |
252b5132 RH |
790 | get_operand (&ptr, operand + 0, 0); |
791 | if (*ptr == ',') | |
792 | ptr++; | |
793 | get_operand (&ptr, operand + 1, 1); | |
794 | if (*ptr == ',') | |
795 | ptr++; | |
796 | get_operand (&ptr, operand + 2, 2); | |
797 | if (*ptr == ',') | |
798 | ptr++; | |
799 | get_cc_operand (&ptr, operand + 3, 3); | |
800 | break; | |
e0c6ed95 | 801 | |
252b5132 RH |
802 | default: |
803 | abort (); | |
804 | } | |
805 | ||
806 | return ptr; | |
807 | } | |
808 | ||
809 | /* Passed a pointer to a list of opcodes which use different | |
e0c6ed95 AM |
810 | addressing modes. Return the opcode which matches the opcodes |
811 | provided. */ | |
252b5132 | 812 | |
e0c6ed95 | 813 | static opcode_entry_type * |
464800ca | 814 | get_specific (opcode_entry_type *opcode, op_type *operands) |
252b5132 RH |
815 | { |
816 | opcode_entry_type *this_try = opcode; | |
817 | int found = 0; | |
818 | unsigned int noperands = opcode->noperands; | |
819 | ||
879db8be | 820 | int this_index = opcode->idx; |
252b5132 RH |
821 | |
822 | while (this_index == opcode->idx && !found) | |
823 | { | |
824 | unsigned int i; | |
825 | ||
826 | this_try = opcode++; | |
827 | for (i = 0; i < noperands; i++) | |
828 | { | |
879db8be | 829 | unsigned int mode = operands[i].mode; |
252b5132 | 830 | |
f590b86e CG |
831 | if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO)) |
832 | { | |
833 | mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO; | |
834 | } | |
835 | ||
252b5132 RH |
836 | if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK)) |
837 | { | |
c03099e6 | 838 | /* It could be a pc rel operand, if this is a da mode |
e0c6ed95 | 839 | and we like disps, then insert it. */ |
252b5132 RH |
840 | |
841 | if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP) | |
842 | { | |
e0c6ed95 | 843 | /* This is the case. */ |
252b5132 RH |
844 | operands[i].mode = CLASS_DISP; |
845 | } | |
846 | else if (mode == CLASS_BA && this_try->arg_info[i]) | |
847 | { | |
e0c6ed95 AM |
848 | /* Can't think of a way to turn what we've been |
849 | given into something that's OK. */ | |
252b5132 RH |
850 | goto fail; |
851 | } | |
852 | else if (this_try->arg_info[i] & CLASS_PR) | |
853 | { | |
854 | if (mode == CLASS_REG_LONG && segmented_mode) | |
855 | { | |
e0c6ed95 | 856 | /* OK. */ |
252b5132 RH |
857 | } |
858 | else if (mode == CLASS_REG_WORD && !segmented_mode) | |
859 | { | |
e0c6ed95 | 860 | /* OK. */ |
252b5132 RH |
861 | } |
862 | else | |
863 | goto fail; | |
864 | } | |
865 | else | |
866 | goto fail; | |
867 | } | |
868 | switch (mode & CLASS_MASK) | |
869 | { | |
870 | default: | |
871 | break; | |
f590b86e CG |
872 | case CLASS_IRO: |
873 | if (operands[i].regsize != CLASS_REG_WORD) | |
874 | as_bad (_("invalid indirect register size")); | |
875 | reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg; | |
876 | break; | |
252b5132 | 877 | case CLASS_IR: |
f590b86e CG |
878 | if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG) |
879 | || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD)) | |
880 | as_bad (_("invalid indirect register size")); | |
881 | reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg; | |
882 | break; | |
883 | case CLASS_X: | |
252b5132 RH |
884 | case CLASS_BA: |
885 | case CLASS_BX: | |
886 | case CLASS_DISP: | |
887 | case CLASS_REG: | |
888 | case CLASS_REG_WORD: | |
889 | case CLASS_REG_BYTE: | |
890 | case CLASS_REG_QUAD: | |
891 | case CLASS_REG_LONG: | |
892 | case CLASS_REGN0: | |
893 | reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg; | |
894 | break; | |
895 | } | |
896 | } | |
897 | ||
898 | found = 1; | |
e0c6ed95 AM |
899 | fail: |
900 | ; | |
252b5132 RH |
901 | } |
902 | if (found) | |
903 | return this_try; | |
904 | else | |
905 | return 0; | |
906 | } | |
907 | ||
252b5132 RH |
908 | static char buffer[20]; |
909 | ||
910 | static void | |
464800ca | 911 | newfix (int ptr, int type, int size, expressionS *operand) |
252b5132 | 912 | { |
a72d6b4e CG |
913 | int is_pcrel = 0; |
914 | ||
464800ca CG |
915 | /* size is in nibbles. */ |
916 | ||
252b5132 RH |
917 | if (operand->X_add_symbol |
918 | || operand->X_op_symbol | |
919 | || operand->X_add_number) | |
920 | { | |
a72d6b4e CG |
921 | switch(type) |
922 | { | |
923 | case R_JR: | |
924 | case R_DISP7: | |
925 | case R_CALLR: | |
926 | is_pcrel = 1; | |
927 | } | |
252b5132 RH |
928 | fix_new_exp (frag_now, |
929 | ptr, | |
7f31df7c | 930 | size / 2, |
252b5132 | 931 | operand, |
a72d6b4e | 932 | is_pcrel, |
252b5132 RH |
933 | type); |
934 | } | |
935 | } | |
936 | ||
937 | static char * | |
464800ca | 938 | apply_fix (char *ptr, int type, expressionS *operand, int size) |
252b5132 | 939 | { |
7f31df7c | 940 | long n = operand->X_add_number; |
252b5132 | 941 | |
464800ca CG |
942 | /* size is in nibbles. */ |
943 | ||
7f31df7c | 944 | newfix ((ptr - buffer) / 2, type, size + 1, operand); |
252b5132 RH |
945 | switch (size) |
946 | { | |
879db8be | 947 | case 8: /* 8 nibbles == 32 bits. */ |
252b5132 RH |
948 | *ptr++ = n >> 28; |
949 | *ptr++ = n >> 24; | |
950 | *ptr++ = n >> 20; | |
951 | *ptr++ = n >> 16; | |
879db8be | 952 | case 4: /* 4 nibbles == 16 bits. */ |
252b5132 RH |
953 | *ptr++ = n >> 12; |
954 | *ptr++ = n >> 8; | |
955 | case 2: | |
956 | *ptr++ = n >> 4; | |
957 | case 1: | |
958 | *ptr++ = n >> 0; | |
959 | break; | |
960 | } | |
252b5132 | 961 | return ptr; |
252b5132 RH |
962 | } |
963 | ||
e0c6ed95 AM |
964 | /* Now we know what sort of opcodes it is. Let's build the bytes. */ |
965 | ||
252b5132 | 966 | #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y; |
19d63e5d | 967 | |
252b5132 | 968 | static void |
464800ca | 969 | build_bytes (opcode_entry_type *this_try, struct z8k_op *operand ATTRIBUTE_UNUSED) |
252b5132 | 970 | { |
252b5132 | 971 | char *output_ptr = buffer; |
252b5132 | 972 | int c; |
252b5132 RH |
973 | int nibble; |
974 | unsigned int *class_ptr; | |
975 | ||
976 | frag_wane (frag_now); | |
977 | frag_new (0); | |
978 | ||
25d3fb58 | 979 | memset (buffer, 0, sizeof (buffer)); |
252b5132 | 980 | class_ptr = this_try->byte_info; |
252b5132 | 981 | |
879db8be | 982 | for (nibble = 0; (c = *class_ptr++); nibble++) |
252b5132 RH |
983 | { |
984 | ||
985 | switch (c & CLASS_MASK) | |
986 | { | |
987 | default: | |
252b5132 | 988 | abort (); |
e0c6ed95 | 989 | |
252b5132 | 990 | case CLASS_ADDRESS: |
e0c6ed95 | 991 | /* Direct address, we don't cope with the SS mode right now. */ |
252b5132 RH |
992 | if (segmented_mode) |
993 | { | |
879db8be | 994 | /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */ |
252b5132 RH |
995 | output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8); |
996 | } | |
997 | else | |
998 | { | |
999 | output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4); | |
1000 | } | |
1001 | da_operand = 0; | |
1002 | break; | |
1003 | case CLASS_DISP8: | |
e0c6ed95 | 1004 | /* pc rel 8 bit */ |
252b5132 RH |
1005 | output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2); |
1006 | da_operand = 0; | |
1007 | break; | |
1008 | ||
1009 | case CLASS_0DISP7: | |
e0c6ed95 | 1010 | /* pc rel 7 bit */ |
252b5132 RH |
1011 | *output_ptr = 0; |
1012 | output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2); | |
1013 | da_operand = 0; | |
1014 | break; | |
1015 | ||
1016 | case CLASS_1DISP7: | |
e0c6ed95 | 1017 | /* pc rel 7 bit */ |
252b5132 RH |
1018 | *output_ptr = 0x80; |
1019 | output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2); | |
e0c6ed95 | 1020 | output_ptr[-2] = 0x8; |
252b5132 RH |
1021 | da_operand = 0; |
1022 | break; | |
1023 | ||
1024 | case CLASS_BIT_1OR2: | |
1025 | *output_ptr = c & 0xf; | |
1026 | if (imm_operand) | |
1027 | { | |
1028 | if (imm_operand->X_add_number == 2) | |
e0c6ed95 | 1029 | *output_ptr |= 2; |
252b5132 | 1030 | else if (imm_operand->X_add_number != 1) |
e0c6ed95 | 1031 | as_bad (_("immediate must be 1 or 2")); |
252b5132 RH |
1032 | } |
1033 | else | |
e0c6ed95 | 1034 | as_bad (_("immediate 1 or 2 expected")); |
252b5132 RH |
1035 | output_ptr++; |
1036 | break; | |
1037 | case CLASS_CC: | |
1038 | *output_ptr++ = the_cc; | |
1039 | break; | |
e0c6ed95 AM |
1040 | case CLASS_0CCC: |
1041 | *output_ptr++ = the_ctrl; | |
1042 | break; | |
1043 | case CLASS_1CCC: | |
1044 | *output_ptr++ = the_ctrl | 0x8; | |
1045 | break; | |
1046 | case CLASS_00II: | |
1047 | *output_ptr++ = (~the_interrupt & 0x3); | |
1048 | break; | |
1049 | case CLASS_01II: | |
1050 | *output_ptr++ = (~the_interrupt & 0x3) | 0x4; | |
1051 | break; | |
1052 | case CLASS_FLAGS: | |
1053 | *output_ptr++ = the_flags; | |
1054 | break; | |
3c25c5f6 | 1055 | case CLASS_IGNORE: |
252b5132 RH |
1056 | case CLASS_BIT: |
1057 | *output_ptr++ = c & 0xf; | |
1058 | break; | |
1059 | case CLASS_REGN0: | |
1060 | if (reg[c & 0xf] == 0) | |
e0c6ed95 AM |
1061 | as_bad (_("can't use R0 here")); |
1062 | /* Fall through. */ | |
252b5132 RH |
1063 | case CLASS_REG: |
1064 | case CLASS_REG_BYTE: | |
1065 | case CLASS_REG_WORD: | |
1066 | case CLASS_REG_LONG: | |
1067 | case CLASS_REG_QUAD: | |
e0c6ed95 | 1068 | /* Insert bit mattern of right reg. */ |
252b5132 RH |
1069 | *output_ptr++ = reg[c & 0xf]; |
1070 | break; | |
1071 | case CLASS_DISP: | |
6840198f NC |
1072 | switch (c & ARG_MASK) |
1073 | { | |
1074 | case ARG_DISP12: | |
1075 | output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4); | |
1076 | break; | |
1077 | case ARG_DISP16: | |
879db8be NC |
1078 | output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4); |
1079 | break; | |
1080 | default: | |
1081 | output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4); | |
1082 | } | |
252b5132 RH |
1083 | da_operand = 0; |
1084 | break; | |
1085 | ||
1086 | case CLASS_IMM: | |
1087 | { | |
252b5132 RH |
1088 | switch (c & ARG_MASK) |
1089 | { | |
3c25c5f6 | 1090 | case ARG_NIM4: |
7f31df7c CG |
1091 | if (imm_operand->X_add_number > 15) |
1092 | { | |
1093 | as_bad (_("immediate value out of range")); | |
1094 | } | |
3c25c5f6 | 1095 | imm_operand->X_add_number = -imm_operand->X_add_number; |
252b5132 RH |
1096 | output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1); |
1097 | break; | |
7f31df7c | 1098 | /*case ARG_IMMNMINUS1: not used. */ |
252b5132 RH |
1099 | case ARG_IMM4M1: |
1100 | imm_operand->X_add_number--; | |
7f31df7c CG |
1101 | /* Drop through. */ |
1102 | case ARG_IMM4: | |
1103 | if (imm_operand->X_add_number > 15) | |
1104 | { | |
1105 | as_bad (_("immediate value out of range")); | |
1106 | } | |
252b5132 RH |
1107 | output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1); |
1108 | break; | |
1109 | case ARG_NIM8: | |
1110 | imm_operand->X_add_number = -imm_operand->X_add_number; | |
7f31df7c | 1111 | /* Drop through. */ |
252b5132 RH |
1112 | case ARG_IMM8: |
1113 | output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2); | |
1114 | break; | |
1115 | case ARG_IMM16: | |
1116 | output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4); | |
1117 | break; | |
252b5132 RH |
1118 | case ARG_IMM32: |
1119 | output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8); | |
1120 | break; | |
252b5132 RH |
1121 | default: |
1122 | abort (); | |
1123 | } | |
1124 | } | |
1125 | } | |
1126 | } | |
1127 | ||
e0c6ed95 | 1128 | /* Copy from the nibble buffer into the frag. */ |
252b5132 RH |
1129 | { |
1130 | int length = (output_ptr - buffer) / 2; | |
1131 | char *src = buffer; | |
1132 | char *fragp = frag_more (length); | |
1133 | ||
1134 | while (src < output_ptr) | |
1135 | { | |
1136 | *fragp = (src[0] << 4) | src[1]; | |
1137 | src += 2; | |
1138 | fragp++; | |
1139 | } | |
252b5132 | 1140 | } |
252b5132 RH |
1141 | } |
1142 | ||
1143 | /* This is the guts of the machine-dependent assembler. STR points to a | |
1994a7c7 NC |
1144 | machine dependent instruction. This function is supposed to emit |
1145 | the frags/bytes it assembles to. */ | |
252b5132 RH |
1146 | |
1147 | void | |
464800ca | 1148 | md_assemble (char *str) |
252b5132 | 1149 | { |
879db8be | 1150 | char c; |
252b5132 RH |
1151 | char *op_start; |
1152 | char *op_end; | |
252b5132 RH |
1153 | struct z8k_op operand[3]; |
1154 | opcode_entry_type *opcode; | |
252b5132 | 1155 | |
e0c6ed95 | 1156 | /* Drop leading whitespace. */ |
252b5132 RH |
1157 | while (*str == ' ') |
1158 | str++; | |
1159 | ||
e0c6ed95 | 1160 | /* Find the op code end. */ |
252b5132 | 1161 | for (op_start = op_end = str; |
d5bf5799 | 1162 | *op_end != 0 && *op_end != ' ' && ! is_end_of_line[(unsigned char) *op_end]; |
252b5132 | 1163 | op_end++) |
e0c6ed95 | 1164 | ; |
252b5132 RH |
1165 | |
1166 | if (op_end == op_start) | |
1167 | { | |
1168 | as_bad (_("can't find opcode ")); | |
1169 | } | |
1170 | c = *op_end; | |
1171 | ||
d5bf5799 | 1172 | *op_end = 0; /* Zero-terminate op code string for hash_find() call. */ |
252b5132 | 1173 | |
e0c6ed95 | 1174 | opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start); |
252b5132 RH |
1175 | |
1176 | if (opcode == NULL) | |
1177 | { | |
1178 | as_bad (_("unknown opcode")); | |
1179 | return; | |
1180 | } | |
1181 | ||
d5bf5799 CG |
1182 | *op_end = c; /* Restore original string. */ |
1183 | ||
252b5132 RH |
1184 | if (opcode->opcode == 250) |
1185 | { | |
252b5132 RH |
1186 | pseudo_typeS *p; |
1187 | char oc; | |
252b5132 | 1188 | char *old = input_line_pointer; |
252b5132 | 1189 | |
3c25c5f6 NC |
1190 | /* Was really a pseudo op. */ |
1191 | ||
252b5132 RH |
1192 | input_line_pointer = op_end; |
1193 | ||
1194 | oc = *old; | |
1195 | *old = '\n'; | |
1196 | while (*input_line_pointer == ' ') | |
1197 | input_line_pointer++; | |
1198 | p = (pseudo_typeS *) (opcode->func); | |
1199 | ||
1200 | (p->poc_handler) (p->poc_val); | |
1201 | input_line_pointer = old; | |
1202 | *old = oc; | |
1203 | } | |
1204 | else | |
1205 | { | |
3c25c5f6 NC |
1206 | char *new_input_line_pointer; |
1207 | ||
1208 | new_input_line_pointer = get_operands (opcode, op_end, operand); | |
1209 | if (new_input_line_pointer) | |
1210 | input_line_pointer = new_input_line_pointer; | |
252b5132 RH |
1211 | |
1212 | opcode = get_specific (opcode, operand); | |
1213 | ||
1214 | if (opcode == 0) | |
1215 | { | |
e0c6ed95 | 1216 | /* Couldn't find an opcode which matched the operands. */ |
252b5132 RH |
1217 | char *where = frag_more (2); |
1218 | ||
1219 | where[0] = 0x0; | |
1220 | where[1] = 0x0; | |
1221 | ||
1222 | as_bad (_("Can't find opcode to match operands")); | |
1223 | return; | |
1224 | } | |
1225 | ||
1226 | build_bytes (opcode, operand); | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | void | |
464800ca | 1231 | tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED) |
252b5132 RH |
1232 | { |
1233 | printf (_("call to tc_crawl_symbol_chain \n")); | |
1234 | } | |
1235 | ||
7f31df7c CG |
1236 | /* We have no need to default values of symbols. */ |
1237 | ||
252b5132 | 1238 | symbolS * |
464800ca | 1239 | md_undefined_symbol (char *name ATTRIBUTE_UNUSED) |
252b5132 RH |
1240 | { |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | void | |
464800ca | 1245 | tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED) |
252b5132 RH |
1246 | { |
1247 | printf (_("call to tc_headers_hook \n")); | |
1248 | } | |
1249 | ||
e0c6ed95 AM |
1250 | /* Various routines to kill one day. */ |
1251 | /* Equal to MAX_PRECISION in atof-ieee.c. */ | |
252b5132 RH |
1252 | #define MAX_LITTLENUMS 6 |
1253 | ||
e0c6ed95 AM |
1254 | /* Turn a string in input_line_pointer into a floating point constant |
1255 | of type TYPE, and store the appropriate bytes in *LITP. The number | |
1256 | of LITTLENUMS emitted is stored in *SIZEP. An error message is | |
1257 | returned, or NULL on OK. */ | |
1258 | ||
252b5132 | 1259 | char * |
464800ca | 1260 | md_atof (int type, char *litP, int *sizeP) |
252b5132 RH |
1261 | { |
1262 | int prec; | |
1263 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
1264 | LITTLENUM_TYPE *wordP; | |
1265 | char *t; | |
252b5132 RH |
1266 | |
1267 | switch (type) | |
1268 | { | |
1269 | case 'f': | |
1270 | case 'F': | |
1271 | case 's': | |
1272 | case 'S': | |
1273 | prec = 2; | |
1274 | break; | |
1275 | ||
1276 | case 'd': | |
1277 | case 'D': | |
1278 | case 'r': | |
1279 | case 'R': | |
1280 | prec = 4; | |
1281 | break; | |
1282 | ||
1283 | case 'x': | |
1284 | case 'X': | |
1285 | prec = 6; | |
1286 | break; | |
1287 | ||
1288 | case 'p': | |
1289 | case 'P': | |
1290 | prec = 6; | |
1291 | break; | |
1292 | ||
1293 | default: | |
1294 | *sizeP = 0; | |
1295 | return _("Bad call to MD_ATOF()"); | |
1296 | } | |
1297 | t = atof_ieee (input_line_pointer, type, words); | |
1298 | if (t) | |
1299 | input_line_pointer = t; | |
1300 | ||
1301 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
1302 | for (wordP = words; prec--;) | |
1303 | { | |
1304 | md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE)); | |
1305 | litP += sizeof (LITTLENUM_TYPE); | |
1306 | } | |
1307 | return 0; | |
1308 | } | |
1309 | \f | |
5a38dc70 | 1310 | const char *md_shortopts = "z:"; |
e0c6ed95 | 1311 | |
3c25c5f6 NC |
1312 | struct option md_longopts[] = |
1313 | { | |
7f31df7c CG |
1314 | #define OPTION_RELAX (OPTION_MD_BASE) |
1315 | {"linkrelax", no_argument, NULL, OPTION_RELAX}, | |
3c25c5f6 NC |
1316 | {NULL, no_argument, NULL, 0} |
1317 | }; | |
e0c6ed95 AM |
1318 | |
1319 | size_t md_longopts_size = sizeof (md_longopts); | |
252b5132 RH |
1320 | |
1321 | int | |
464800ca | 1322 | md_parse_option (int c, char *arg) |
252b5132 RH |
1323 | { |
1324 | switch (c) | |
1325 | { | |
1326 | case 'z': | |
1327 | if (!strcmp (arg, "8001")) | |
7f31df7c | 1328 | s_segm (1); |
252b5132 | 1329 | else if (!strcmp (arg, "8002")) |
7f31df7c | 1330 | s_segm (0); |
252b5132 RH |
1331 | else |
1332 | { | |
1333 | as_bad (_("invalid architecture -z%s"), arg); | |
1334 | return 0; | |
1335 | } | |
7f31df7c CG |
1336 | z8k_target_from_cmdline = 1; |
1337 | break; | |
1338 | ||
1339 | case OPTION_RELAX: | |
1340 | linkrelax = 1; | |
252b5132 RH |
1341 | break; |
1342 | ||
1343 | default: | |
1344 | return 0; | |
1345 | } | |
1346 | ||
1347 | return 1; | |
1348 | } | |
1349 | ||
1350 | void | |
464800ca | 1351 | md_show_usage (FILE *stream) |
252b5132 | 1352 | { |
e0c6ed95 | 1353 | fprintf (stream, _("\ |
7f31df7c CG |
1354 | Z8K options:\n\ |
1355 | -z8001 generate segmented code\n\ | |
1356 | -z8002 generate unsegmented code\n\ | |
1357 | -linkrelax create linker relaxable code\n")); | |
252b5132 RH |
1358 | } |
1359 | \f | |
252b5132 | 1360 | void |
464800ca CG |
1361 | md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED, |
1362 | segT seg ATTRIBUTE_UNUSED, | |
1363 | fragS *fragP ATTRIBUTE_UNUSED) | |
252b5132 | 1364 | { |
7f31df7c | 1365 | printf (_("call to md_convert_frag\n")); |
252b5132 RH |
1366 | abort (); |
1367 | } | |
1368 | ||
1369 | valueT | |
464800ca | 1370 | md_section_align (segT seg, valueT size) |
252b5132 | 1371 | { |
e0c6ed95 AM |
1372 | return ((size + (1 << section_alignment[(int) seg]) - 1) |
1373 | & (-1 << section_alignment[(int) seg])); | |
252b5132 RH |
1374 | } |
1375 | ||
a72d6b4e CG |
1376 | /* Attempt to simplify or eliminate a fixup. To indicate that a fixup |
1377 | has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL, | |
1378 | we will have to generate a reloc entry. */ | |
252b5132 | 1379 | void |
464800ca | 1380 | md_apply_fix3 (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED) |
252b5132 | 1381 | { |
94f592af | 1382 | long val = * (long *) valP; |
252b5132 RH |
1383 | char *buf = fixP->fx_where + fixP->fx_frag->fr_literal; |
1384 | ||
1385 | switch (fixP->fx_r_type) | |
1386 | { | |
1387 | case R_IMM4L: | |
7f31df7c | 1388 | buf[0] = (buf[0] & 0xf0) | (val & 0xf); |
252b5132 RH |
1389 | break; |
1390 | ||
1391 | case R_JR: | |
a72d6b4e CG |
1392 | if (fixP->fx_addsy) |
1393 | { | |
1394 | fixP->fx_no_overflow = 1; | |
1395 | fixP->fx_done = 0; | |
1396 | } | |
1397 | else | |
1398 | { | |
7f31df7c CG |
1399 | if (val & 1) |
1400 | as_bad (_("cannot branch to odd address")); | |
1401 | val /= 2; | |
1402 | if (val > 127 || val < -128) | |
a72d6b4e | 1403 | as_warn (_("relative jump out of range")); |
252b5132 | 1404 | *buf++ = val; |
7f31df7c | 1405 | fixP->fx_no_overflow = 1; |
a72d6b4e CG |
1406 | fixP->fx_done = 1; |
1407 | } | |
252b5132 RH |
1408 | break; |
1409 | ||
1410 | case R_DISP7: | |
a72d6b4e CG |
1411 | if (fixP->fx_addsy) |
1412 | { | |
1413 | fixP->fx_no_overflow = 1; | |
1414 | fixP->fx_done = 0; | |
1415 | } | |
1416 | else | |
1417 | { | |
d5bf5799 CG |
1418 | if (val & 1) |
1419 | as_bad (_("cannot branch to odd address")); | |
1420 | val /= 2; | |
a72d6b4e | 1421 | if (val > 0 || val < -127) |
d5bf5799 | 1422 | as_bad (_("relative jump out of range")); |
a72d6b4e | 1423 | *buf = (*buf & 0x80) | (-val & 0x7f); |
d5bf5799 | 1424 | fixP->fx_no_overflow = 1; |
a72d6b4e CG |
1425 | fixP->fx_done = 1; |
1426 | } | |
7f31df7c | 1427 | break; |
252b5132 | 1428 | |
7f31df7c | 1429 | case R_CALLR: |
a72d6b4e CG |
1430 | if (fixP->fx_addsy) |
1431 | { | |
1432 | fixP->fx_no_overflow = 1; | |
1433 | fixP->fx_done = 0; | |
1434 | } | |
1435 | else | |
1436 | { | |
1437 | if (val & 1) | |
1438 | as_bad (_("cannot branch to odd address")); | |
1439 | if (val > 4096 || val < -4095) | |
d5bf5799 | 1440 | as_bad (_("relative call out of range")); |
a72d6b4e | 1441 | val = -val / 2; |
d5bf5799 CG |
1442 | *buf = (*buf & 0xf0) | ((val >> 8) & 0xf); |
1443 | buf++; | |
1444 | *buf++ = val & 0xff; | |
a72d6b4e CG |
1445 | fixP->fx_no_overflow = 1; |
1446 | fixP->fx_done = 1; | |
1447 | } | |
252b5132 RH |
1448 | break; |
1449 | ||
1450 | case R_IMM8: | |
7f31df7c | 1451 | *buf++ = val; |
252b5132 | 1452 | break; |
7f31df7c | 1453 | |
252b5132 RH |
1454 | case R_IMM16: |
1455 | *buf++ = (val >> 8); | |
1456 | *buf++ = val; | |
1457 | break; | |
7f31df7c | 1458 | |
252b5132 RH |
1459 | case R_IMM32: |
1460 | *buf++ = (val >> 24); | |
1461 | *buf++ = (val >> 16); | |
1462 | *buf++ = (val >> 8); | |
1463 | *buf++ = val; | |
1464 | break; | |
7f31df7c CG |
1465 | |
1466 | case R_REL16: | |
1467 | val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size; | |
1468 | if (val > 32767 || val < -32768) | |
1469 | as_bad (_("relative address out of range")); | |
1470 | *buf++ = (val >> 8); | |
1471 | *buf++ = val; | |
1472 | fixP->fx_no_overflow = 1; | |
1473 | break; | |
1474 | ||
252b5132 RH |
1475 | #if 0 |
1476 | case R_DA | R_SEG: | |
1477 | *buf++ = (val >> 16); | |
1478 | *buf++ = 0x00; | |
1479 | *buf++ = (val >> 8); | |
1480 | *buf++ = val; | |
1481 | break; | |
1482 | #endif | |
1483 | ||
1484 | case 0: | |
1485 | md_number_to_chars (buf, val, fixP->fx_size); | |
1486 | break; | |
1487 | ||
1488 | default: | |
7f31df7c | 1489 | printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type); |
252b5132 | 1490 | abort (); |
252b5132 | 1491 | } |
94f592af NC |
1492 | |
1493 | if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0) | |
1494 | fixP->fx_done = 1; | |
252b5132 RH |
1495 | } |
1496 | ||
1497 | int | |
464800ca CG |
1498 | md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED, |
1499 | segT segment_type ATTRIBUTE_UNUSED) | |
252b5132 | 1500 | { |
7f31df7c | 1501 | printf (_("call to md_estimate_size_before_relax\n")); |
252b5132 RH |
1502 | abort (); |
1503 | } | |
1504 | ||
e0c6ed95 | 1505 | /* Put number into target byte order. */ |
252b5132 RH |
1506 | |
1507 | void | |
464800ca | 1508 | md_number_to_chars (char *ptr, valueT use, int nbytes) |
252b5132 RH |
1509 | { |
1510 | number_to_chars_bigendian (ptr, use, nbytes); | |
1511 | } | |
e0c6ed95 | 1512 | |
a72d6b4e CG |
1513 | /* On the Z8000, a PC-relative offset is relative to the address of the |
1514 | instruction plus its size. */ | |
252b5132 | 1515 | long |
464800ca | 1516 | md_pcrel_from (fixS *fixP) |
252b5132 | 1517 | { |
a72d6b4e | 1518 | return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address; |
252b5132 RH |
1519 | } |
1520 | ||
1521 | void | |
464800ca | 1522 | tc_coff_symbol_emit_hook (symbolS *s ATTRIBUTE_UNUSED) |
252b5132 RH |
1523 | { |
1524 | } | |
1525 | ||
1526 | void | |
464800ca | 1527 | tc_reloc_mangle (fixS *fix_ptr, struct internal_reloc *intr, bfd_vma base) |
252b5132 RH |
1528 | { |
1529 | symbolS *symbol_ptr; | |
1530 | ||
e0c6ed95 AM |
1531 | if (fix_ptr->fx_addsy |
1532 | && fix_ptr->fx_subsy) | |
252b5132 RH |
1533 | { |
1534 | symbolS *add = fix_ptr->fx_addsy; | |
1535 | symbolS *sub = fix_ptr->fx_subsy; | |
e0c6ed95 AM |
1536 | |
1537 | if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub)) | |
1538 | as_bad (_("Can't subtract symbols in different sections %s %s"), | |
1539 | S_GET_NAME (add), S_GET_NAME (sub)); | |
1540 | else | |
252b5132 | 1541 | { |
e0c6ed95 AM |
1542 | int diff = S_GET_VALUE (add) - S_GET_VALUE (sub); |
1543 | ||
1544 | fix_ptr->fx_addsy = 0; | |
1545 | fix_ptr->fx_subsy = 0; | |
1546 | fix_ptr->fx_offset += diff; | |
252b5132 | 1547 | } |
252b5132 RH |
1548 | } |
1549 | symbol_ptr = fix_ptr->fx_addsy; | |
1550 | ||
1551 | /* If this relocation is attached to a symbol then it's ok | |
e0c6ed95 | 1552 | to output it. */ |
252b5132 RH |
1553 | if (fix_ptr->fx_r_type == 0) |
1554 | { | |
e0c6ed95 | 1555 | /* cons likes to create reloc32's whatever the size of the reloc. */ |
252b5132 RH |
1556 | switch (fix_ptr->fx_size) |
1557 | { | |
1558 | case 2: | |
1559 | intr->r_type = R_IMM16; | |
1560 | break; | |
1561 | case 1: | |
1562 | intr->r_type = R_IMM8; | |
1563 | break; | |
1564 | case 4: | |
1565 | intr->r_type = R_IMM32; | |
1566 | break; | |
1567 | default: | |
1568 | abort (); | |
1569 | } | |
252b5132 RH |
1570 | } |
1571 | else | |
e0c6ed95 | 1572 | intr->r_type = fix_ptr->fx_r_type; |
252b5132 RH |
1573 | |
1574 | intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base; | |
1575 | intr->r_offset = fix_ptr->fx_offset; | |
1576 | ||
1577 | if (symbol_ptr) | |
1578 | intr->r_symndx = symbol_ptr->sy_number; | |
1579 | else | |
1580 | intr->r_symndx = -1; | |
1581 | } |