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