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