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