]>
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 <[email protected]>. */ |
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 { |
e0c6ed95 AM |
196 | /* 'b','w','r','q'. */ |
197 | char regsize; | |
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) | |
e0c6ed95 AM |
314 | as_warn (_("register rr%d, out of range."), regno); |
315 | } | |
252b5132 | 316 | else if (src[1] == 'h') |
e0c6ed95 | 317 | { |
28bab82b NC |
318 | if (src[2] < '0' || src[2] > '9') |
319 | return res; /* Assume no register name but a label starting with 'rh'. */ | |
e0c6ed95 AM |
320 | *mode = CLASS_REG_BYTE; |
321 | res = whatreg (reg, src + 2); | |
252b5132 RH |
322 | regno = *reg; |
323 | if (regno > 7) | |
e0c6ed95 AM |
324 | as_warn (_("register rh%d, out of range."), regno); |
325 | } | |
252b5132 | 326 | else if (src[1] == 'l') |
e0c6ed95 | 327 | { |
28bab82b NC |
328 | if (src[2] < '0' || src[2] > '9') |
329 | return res; /* Assume no register name but a label starting with 'rl'. */ | |
e0c6ed95 AM |
330 | *mode = CLASS_REG_BYTE; |
331 | res = whatreg (reg, src + 2); | |
252b5132 RH |
332 | regno = *reg; |
333 | if (regno > 7) | |
e0c6ed95 AM |
334 | as_warn (_("register rl%d, out of range."), regno); |
335 | *reg += 8; | |
336 | } | |
252b5132 | 337 | else if (src[1] == 'q') |
e0c6ed95 | 338 | { |
28bab82b NC |
339 | if (src[2] < '0' || src[2] > '9') |
340 | return res; /* Assume no register name but a label starting with 'rq'. */ | |
e0c6ed95 AM |
341 | *mode = CLASS_REG_QUAD; |
342 | res = whatreg (reg, src + 2); | |
252b5132 RH |
343 | regno = *reg; |
344 | if (regno > 12) | |
e0c6ed95 AM |
345 | as_warn (_("register rq%d, out of range."), regno); |
346 | } | |
252b5132 | 347 | else |
e0c6ed95 | 348 | { |
28bab82b NC |
349 | if (src[1] < '0' || src[1] > '9') |
350 | return res; /* Assume no register name but a label starting with 'r'. */ | |
e0c6ed95 AM |
351 | *mode = CLASS_REG_WORD; |
352 | res = whatreg (reg, src + 1); | |
252b5132 RH |
353 | regno = *reg; |
354 | if (regno > 15) | |
e0c6ed95 AM |
355 | as_warn (_("register r%d, out of range."), regno); |
356 | } | |
252b5132 RH |
357 | } |
358 | return res; | |
252b5132 RH |
359 | } |
360 | ||
78a33af2 | 361 | static char * |
c0fecd35 AM |
362 | parse_exp (s, op) |
363 | char *s; | |
364 | expressionS *op; | |
252b5132 RH |
365 | { |
366 | char *save = input_line_pointer; | |
367 | char *new; | |
368 | ||
369 | input_line_pointer = s; | |
370 | expression (op); | |
371 | if (op->X_op == O_absent) | |
372 | as_bad (_("missing operand")); | |
373 | new = input_line_pointer; | |
374 | input_line_pointer = save; | |
375 | return new; | |
376 | } | |
377 | ||
378 | /* The many forms of operand: | |
379 | ||
380 | <rb> | |
381 | <r> | |
382 | <rr> | |
383 | <rq> | |
384 | @r | |
385 | #exp | |
386 | exp | |
387 | exp(r) | |
388 | r(#exp) | |
389 | r(r) | |
252b5132 RH |
390 | */ |
391 | ||
e0c6ed95 | 392 | static char * |
c0fecd35 AM |
393 | checkfor (ptr, what) |
394 | char *ptr; | |
395 | char what; | |
252b5132 RH |
396 | { |
397 | if (*ptr == what) | |
398 | ptr++; | |
399 | else | |
e0c6ed95 AM |
400 | as_bad (_("expected %c"), what); |
401 | ||
252b5132 RH |
402 | return ptr; |
403 | } | |
404 | ||
e0c6ed95 AM |
405 | /* Make sure the mode supplied is the size of a word. */ |
406 | ||
252b5132 | 407 | static void |
c0fecd35 AM |
408 | regword (mode, string) |
409 | int mode; | |
410 | char *string; | |
252b5132 RH |
411 | { |
412 | int ok; | |
413 | ||
414 | ok = CLASS_REG_WORD; | |
415 | if (ok != mode) | |
416 | { | |
417 | as_bad (_("register is wrong size for a word %s"), string); | |
418 | } | |
419 | } | |
420 | ||
e0c6ed95 AM |
421 | /* Make sure the mode supplied is the size of an address. */ |
422 | ||
252b5132 | 423 | static void |
c0fecd35 AM |
424 | regaddr (mode, string) |
425 | int mode; | |
426 | char *string; | |
252b5132 RH |
427 | { |
428 | int ok; | |
429 | ||
430 | ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD; | |
431 | if (ok != mode) | |
432 | { | |
433 | as_bad (_("register is wrong size for address %s"), string); | |
434 | } | |
435 | } | |
436 | ||
19d63e5d | 437 | struct ctrl_names { |
e0c6ed95 AM |
438 | int value; |
439 | char *name; | |
252b5132 RH |
440 | }; |
441 | ||
e0c6ed95 | 442 | struct ctrl_names ctrl_table[] = { |
879db8be NC |
443 | { 0x2, "fcw" }, |
444 | { 0x3, "refresh" }, | |
445 | { 0x4, "psapseg" }, | |
446 | { 0x5, "psapoff" }, | |
447 | { 0x5, "psap" }, | |
448 | { 0x6, "nspseg" }, | |
449 | { 0x7, "nspoff" }, | |
450 | { 0x7, "nsp" }, | |
451 | { 0 , 0 } | |
252b5132 | 452 | }; |
e0c6ed95 | 453 | |
252b5132 | 454 | static void |
c0fecd35 AM |
455 | get_ctrl_operand (ptr, mode, dst) |
456 | char **ptr; | |
457 | struct z8k_op *mode; | |
879db8be | 458 | unsigned int dst ATTRIBUTE_UNUSED; |
252b5132 RH |
459 | { |
460 | char *src = *ptr; | |
252b5132 RH |
461 | int i; |
462 | ||
463 | while (*src == ' ') | |
464 | src++; | |
465 | ||
466 | mode->mode = CLASS_CTRL; | |
467 | for (i = 0; ctrl_table[i].name; i++) | |
468 | { | |
469 | int j; | |
470 | ||
471 | for (j = 0; ctrl_table[i].name[j]; j++) | |
e0c6ed95 AM |
472 | { |
473 | if (ctrl_table[i].name[j] != src[j]) | |
474 | goto fail; | |
475 | } | |
252b5132 RH |
476 | the_ctrl = ctrl_table[i].value; |
477 | *ptr = src + j; | |
478 | return; | |
e0c6ed95 AM |
479 | fail: |
480 | ; | |
252b5132 RH |
481 | } |
482 | the_ctrl = 0; | |
483 | return; | |
484 | } | |
485 | ||
19d63e5d | 486 | struct flag_names { |
252b5132 RH |
487 | int value; |
488 | char *name; | |
489 | ||
490 | }; | |
491 | ||
e0c6ed95 | 492 | struct flag_names flag_table[] = { |
879db8be NC |
493 | { 0x1, "p" }, |
494 | { 0x1, "v" }, | |
495 | { 0x2, "s" }, | |
496 | { 0x4, "z" }, | |
497 | { 0x8, "c" }, | |
498 | { 0x0, "+" }, | |
499 | { 0, 0 } | |
252b5132 RH |
500 | }; |
501 | ||
502 | static void | |
c0fecd35 AM |
503 | get_flags_operand (ptr, mode, dst) |
504 | char **ptr; | |
505 | struct z8k_op *mode; | |
879db8be | 506 | unsigned int dst ATTRIBUTE_UNUSED; |
252b5132 RH |
507 | { |
508 | char *src = *ptr; | |
252b5132 RH |
509 | int i; |
510 | int j; | |
511 | ||
512 | while (*src == ' ') | |
513 | src++; | |
514 | ||
515 | mode->mode = CLASS_FLAGS; | |
516 | the_flags = 0; | |
517 | for (j = 0; j <= 9; j++) | |
518 | { | |
e0c6ed95 | 519 | if (!src[j]) |
252b5132 | 520 | goto done; |
e0c6ed95 AM |
521 | for (i = 0; flag_table[i].name; i++) |
522 | { | |
523 | if (flag_table[i].name[0] == src[j]) | |
524 | { | |
525 | the_flags = the_flags | flag_table[i].value; | |
526 | goto match; | |
527 | } | |
528 | } | |
252b5132 RH |
529 | goto done; |
530 | match: | |
e0c6ed95 | 531 | ; |
252b5132 | 532 | } |
e0c6ed95 | 533 | done: |
252b5132 RH |
534 | *ptr = src + j; |
535 | return; | |
536 | } | |
537 | ||
19d63e5d | 538 | struct interrupt_names { |
252b5132 RH |
539 | int value; |
540 | char *name; | |
541 | ||
542 | }; | |
543 | ||
19d63e5d | 544 | struct interrupt_names intr_table[] = { |
879db8be NC |
545 | { 0x1, "nvi" }, |
546 | { 0x2, "vi" }, | |
547 | { 0x3, "both" }, | |
548 | { 0x3, "all" }, | |
549 | { 0, 0 } | |
252b5132 RH |
550 | }; |
551 | ||
552 | static void | |
c0fecd35 AM |
553 | get_interrupt_operand (ptr, mode, dst) |
554 | char **ptr; | |
555 | struct z8k_op *mode; | |
879db8be | 556 | unsigned int dst ATTRIBUTE_UNUSED; |
252b5132 RH |
557 | { |
558 | char *src = *ptr; | |
252b5132 RH |
559 | int i; |
560 | ||
561 | while (*src == ' ') | |
562 | src++; | |
563 | ||
564 | mode->mode = CLASS_IMM; | |
565 | for (i = 0; intr_table[i].name; i++) | |
566 | { | |
567 | int j; | |
568 | ||
569 | for (j = 0; intr_table[i].name[j]; j++) | |
e0c6ed95 AM |
570 | { |
571 | if (intr_table[i].name[j] != src[j]) | |
572 | goto fail; | |
573 | } | |
252b5132 RH |
574 | the_interrupt = intr_table[i].value; |
575 | *ptr = src + j; | |
576 | return; | |
e0c6ed95 AM |
577 | fail: |
578 | ; | |
252b5132 | 579 | } |
7f31df7c CG |
580 | /* No interrupt type specified, opcode won't do anything. */ |
581 | as_warn (_("opcode has no effect.")); | |
252b5132 RH |
582 | the_interrupt = 0x0; |
583 | return; | |
584 | } | |
585 | ||
19d63e5d | 586 | struct cc_names { |
252b5132 RH |
587 | int value; |
588 | char *name; | |
252b5132 RH |
589 | }; |
590 | ||
e0c6ed95 | 591 | struct cc_names table[] = { |
879db8be NC |
592 | { 0x0, "f" }, |
593 | { 0x1, "lt" }, | |
594 | { 0x2, "le" }, | |
595 | { 0x3, "ule" }, | |
596 | { 0x4, "ov" }, | |
597 | { 0x4, "pe" }, | |
598 | { 0x5, "mi" }, | |
599 | { 0x6, "eq" }, | |
600 | { 0x6, "z" }, | |
601 | { 0x7, "c" }, | |
602 | { 0x7, "ult" }, | |
603 | { 0x8, "t" }, | |
604 | { 0x9, "ge" }, | |
605 | { 0xa, "gt" }, | |
606 | { 0xb, "ugt" }, | |
607 | { 0xc, "nov" }, | |
608 | { 0xc, "po" }, | |
609 | { 0xd, "pl" }, | |
610 | { 0xe, "ne" }, | |
611 | { 0xe, "nz" }, | |
612 | { 0xf, "nc" }, | |
613 | { 0xf, "uge" }, | |
614 | { 0 , 0 } | |
252b5132 RH |
615 | }; |
616 | ||
617 | static void | |
c0fecd35 AM |
618 | get_cc_operand (ptr, mode, dst) |
619 | char **ptr; | |
620 | struct z8k_op *mode; | |
879db8be | 621 | unsigned int dst ATTRIBUTE_UNUSED; |
252b5132 RH |
622 | { |
623 | char *src = *ptr; | |
252b5132 RH |
624 | int i; |
625 | ||
626 | while (*src == ' ') | |
627 | src++; | |
628 | ||
629 | mode->mode = CLASS_CC; | |
630 | for (i = 0; table[i].name; i++) | |
631 | { | |
632 | int j; | |
633 | ||
634 | for (j = 0; table[i].name[j]; j++) | |
635 | { | |
636 | if (table[i].name[j] != src[j]) | |
637 | goto fail; | |
638 | } | |
639 | the_cc = table[i].value; | |
640 | *ptr = src + j; | |
641 | return; | |
e0c6ed95 AM |
642 | fail: |
643 | ; | |
252b5132 RH |
644 | } |
645 | the_cc = 0x8; | |
646 | } | |
647 | ||
648 | static void | |
649 | get_operand (ptr, mode, dst) | |
650 | char **ptr; | |
651 | struct z8k_op *mode; | |
879db8be | 652 | unsigned int dst ATTRIBUTE_UNUSED; |
252b5132 RH |
653 | { |
654 | char *src = *ptr; | |
655 | char *end; | |
252b5132 RH |
656 | |
657 | mode->mode = 0; | |
658 | ||
659 | while (*src == ' ') | |
660 | src++; | |
661 | if (*src == '#') | |
662 | { | |
663 | mode->mode = CLASS_IMM; | |
664 | imm_operand = &(mode->exp); | |
665 | src = parse_exp (src + 1, &(mode->exp)); | |
666 | } | |
667 | else if (*src == '@') | |
668 | { | |
669 | int d; | |
670 | ||
671 | mode->mode = CLASS_IR; | |
672 | src = parse_reg (src + 1, &d, &mode->reg); | |
673 | } | |
674 | else | |
675 | { | |
676 | int regn; | |
677 | ||
678 | end = parse_reg (src, &mode->mode, ®n); | |
679 | ||
680 | if (end) | |
681 | { | |
682 | int nw, nr; | |
683 | ||
684 | src = end; | |
685 | if (*src == '(') | |
686 | { | |
687 | src++; | |
688 | end = parse_reg (src, &nw, &nr); | |
689 | if (end) | |
690 | { | |
e0c6ed95 | 691 | /* Got Ra(Rb). */ |
252b5132 RH |
692 | src = end; |
693 | ||
694 | if (*src != ')') | |
e0c6ed95 | 695 | as_bad (_("Missing ) in ra(rb)")); |
252b5132 | 696 | else |
e0c6ed95 | 697 | src++; |
252b5132 RH |
698 | |
699 | regaddr (mode->mode, "ra(rb) ra"); | |
e0c6ed95 AM |
700 | #if 0 |
701 | regword (mode->mode, "ra(rb) rb"); | |
702 | #endif | |
252b5132 RH |
703 | mode->mode = CLASS_BX; |
704 | mode->reg = regn; | |
705 | mode->x_reg = nr; | |
706 | reg[ARG_RX] = nr; | |
707 | } | |
708 | else | |
709 | { | |
e0c6ed95 | 710 | /* Got Ra(disp). */ |
252b5132 RH |
711 | if (*src == '#') |
712 | src++; | |
713 | src = parse_exp (src, &(mode->exp)); | |
714 | src = checkfor (src, ')'); | |
715 | mode->mode = CLASS_BA; | |
716 | mode->reg = regn; | |
717 | mode->x_reg = 0; | |
718 | imm_operand = &(mode->exp); | |
719 | } | |
720 | } | |
721 | else | |
722 | { | |
723 | mode->reg = regn; | |
724 | mode->x_reg = 0; | |
725 | } | |
726 | } | |
727 | else | |
728 | { | |
e0c6ed95 | 729 | /* No initial reg. */ |
252b5132 RH |
730 | src = parse_exp (src, &(mode->exp)); |
731 | if (*src == '(') | |
732 | { | |
733 | src++; | |
734 | end = parse_reg (src, &(mode->mode), ®n); | |
735 | regword (mode->mode, "addr(Ra) ra"); | |
736 | mode->mode = CLASS_X; | |
737 | mode->reg = regn; | |
738 | mode->x_reg = 0; | |
739 | da_operand = &(mode->exp); | |
740 | src = checkfor (end, ')'); | |
741 | } | |
742 | else | |
743 | { | |
e0c6ed95 | 744 | /* Just an address. */ |
252b5132 RH |
745 | mode->mode = CLASS_DA; |
746 | mode->reg = 0; | |
747 | mode->x_reg = 0; | |
748 | da_operand = &(mode->exp); | |
749 | } | |
750 | } | |
751 | } | |
752 | *ptr = src; | |
753 | } | |
754 | ||
e0c6ed95 | 755 | static char * |
252b5132 | 756 | get_operands (opcode, op_end, operand) |
78a33af2 | 757 | const opcode_entry_type *opcode; |
252b5132 RH |
758 | char *op_end; |
759 | op_type *operand; | |
760 | { | |
761 | char *ptr = op_end; | |
e0c6ed95 AM |
762 | char *savptr; |
763 | ||
14899840 | 764 | ptr++; |
252b5132 RH |
765 | switch (opcode->noperands) |
766 | { | |
767 | case 0: | |
768 | operand[0].mode = 0; | |
769 | operand[1].mode = 0; | |
770 | break; | |
771 | ||
772 | case 1: | |
252b5132 | 773 | if (opcode->arg_info[0] == CLASS_CC) |
14899840 NC |
774 | get_cc_operand (&ptr, operand + 0, 0); |
775 | ||
252b5132 | 776 | else if (opcode->arg_info[0] == CLASS_FLAGS) |
14899840 NC |
777 | get_flags_operand (&ptr, operand + 0, 0); |
778 | ||
e0c6ed95 | 779 | else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2))) |
14899840 NC |
780 | get_interrupt_operand (&ptr, operand + 0, 0); |
781 | ||
252b5132 | 782 | else |
14899840 NC |
783 | get_operand (&ptr, operand + 0, 0); |
784 | ||
252b5132 RH |
785 | operand[1].mode = 0; |
786 | break; | |
787 | ||
788 | case 2: | |
252b5132 RH |
789 | savptr = ptr; |
790 | if (opcode->arg_info[0] == CLASS_CC) | |
14899840 NC |
791 | get_cc_operand (&ptr, operand + 0, 0); |
792 | ||
252b5132 | 793 | else if (opcode->arg_info[0] == CLASS_CTRL) |
e0c6ed95 AM |
794 | { |
795 | get_ctrl_operand (&ptr, operand + 0, 0); | |
14899840 | 796 | |
e0c6ed95 AM |
797 | if (the_ctrl == 0) |
798 | { | |
799 | ptr = savptr; | |
800 | get_operand (&ptr, operand + 0, 0); | |
14899840 | 801 | |
e0c6ed95 | 802 | if (ptr == 0) |
879db8be | 803 | return NULL; |
e0c6ed95 AM |
804 | if (*ptr == ',') |
805 | ptr++; | |
806 | get_ctrl_operand (&ptr, operand + 1, 1); | |
807 | return ptr; | |
808 | } | |
809 | } | |
252b5132 | 810 | else |
14899840 NC |
811 | get_operand (&ptr, operand + 0, 0); |
812 | ||
252b5132 | 813 | if (ptr == 0) |
879db8be | 814 | return NULL; |
252b5132 | 815 | if (*ptr == ',') |
e0c6ed95 | 816 | ptr++; |
252b5132 RH |
817 | get_operand (&ptr, operand + 1, 1); |
818 | break; | |
819 | ||
820 | case 3: | |
252b5132 RH |
821 | get_operand (&ptr, operand + 0, 0); |
822 | if (*ptr == ',') | |
823 | ptr++; | |
824 | get_operand (&ptr, operand + 1, 1); | |
825 | if (*ptr == ',') | |
826 | ptr++; | |
827 | get_operand (&ptr, operand + 2, 2); | |
828 | break; | |
829 | ||
830 | case 4: | |
252b5132 RH |
831 | get_operand (&ptr, operand + 0, 0); |
832 | if (*ptr == ',') | |
833 | ptr++; | |
834 | get_operand (&ptr, operand + 1, 1); | |
835 | if (*ptr == ',') | |
836 | ptr++; | |
837 | get_operand (&ptr, operand + 2, 2); | |
838 | if (*ptr == ',') | |
839 | ptr++; | |
840 | get_cc_operand (&ptr, operand + 3, 3); | |
841 | break; | |
e0c6ed95 | 842 | |
252b5132 RH |
843 | default: |
844 | abort (); | |
845 | } | |
846 | ||
847 | return ptr; | |
848 | } | |
849 | ||
850 | /* Passed a pointer to a list of opcodes which use different | |
e0c6ed95 AM |
851 | addressing modes. Return the opcode which matches the opcodes |
852 | provided. */ | |
252b5132 | 853 | |
e0c6ed95 | 854 | static opcode_entry_type * |
c0fecd35 AM |
855 | get_specific (opcode, operands) |
856 | opcode_entry_type *opcode; | |
857 | op_type *operands; | |
252b5132 RH |
858 | { |
859 | opcode_entry_type *this_try = opcode; | |
860 | int found = 0; | |
861 | unsigned int noperands = opcode->noperands; | |
862 | ||
879db8be | 863 | int this_index = opcode->idx; |
252b5132 RH |
864 | |
865 | while (this_index == opcode->idx && !found) | |
866 | { | |
867 | unsigned int i; | |
868 | ||
869 | this_try = opcode++; | |
870 | for (i = 0; i < noperands; i++) | |
871 | { | |
879db8be | 872 | unsigned int mode = operands[i].mode; |
252b5132 RH |
873 | |
874 | if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK)) | |
875 | { | |
c03099e6 | 876 | /* It could be a pc rel operand, if this is a da mode |
e0c6ed95 | 877 | and we like disps, then insert it. */ |
252b5132 RH |
878 | |
879 | if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP) | |
880 | { | |
e0c6ed95 | 881 | /* This is the case. */ |
252b5132 RH |
882 | operands[i].mode = CLASS_DISP; |
883 | } | |
884 | else if (mode == CLASS_BA && this_try->arg_info[i]) | |
885 | { | |
e0c6ed95 AM |
886 | /* Can't think of a way to turn what we've been |
887 | given into something that's OK. */ | |
252b5132 RH |
888 | goto fail; |
889 | } | |
890 | else if (this_try->arg_info[i] & CLASS_PR) | |
891 | { | |
892 | if (mode == CLASS_REG_LONG && segmented_mode) | |
893 | { | |
e0c6ed95 | 894 | /* OK. */ |
252b5132 RH |
895 | } |
896 | else if (mode == CLASS_REG_WORD && !segmented_mode) | |
897 | { | |
e0c6ed95 | 898 | /* OK. */ |
252b5132 RH |
899 | } |
900 | else | |
901 | goto fail; | |
902 | } | |
903 | else | |
904 | goto fail; | |
905 | } | |
906 | switch (mode & CLASS_MASK) | |
907 | { | |
908 | default: | |
909 | break; | |
910 | case CLASS_X: | |
911 | case CLASS_IR: | |
912 | case CLASS_BA: | |
913 | case CLASS_BX: | |
914 | case CLASS_DISP: | |
915 | case CLASS_REG: | |
916 | case CLASS_REG_WORD: | |
917 | case CLASS_REG_BYTE: | |
918 | case CLASS_REG_QUAD: | |
919 | case CLASS_REG_LONG: | |
920 | case CLASS_REGN0: | |
921 | reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg; | |
922 | break; | |
923 | } | |
924 | } | |
925 | ||
926 | found = 1; | |
e0c6ed95 AM |
927 | fail: |
928 | ; | |
252b5132 RH |
929 | } |
930 | if (found) | |
931 | return this_try; | |
932 | else | |
933 | return 0; | |
934 | } | |
935 | ||
879db8be | 936 | #if 0 /* Not used. */ |
252b5132 | 937 | static void |
c0fecd35 AM |
938 | check_operand (operand, width, string) |
939 | struct z8k_op *operand; | |
940 | unsigned int width; | |
941 | char *string; | |
252b5132 RH |
942 | { |
943 | if (operand->exp.X_add_symbol == 0 | |
944 | && operand->exp.X_op_symbol == 0) | |
945 | { | |
946 | ||
e0c6ed95 AM |
947 | /* No symbol involved, let's look at offset, it's dangerous if |
948 | any of the high bits are not 0 or ff's, find out by oring or | |
949 | anding with the width and seeing if the answer is 0 or all | |
950 | fs. */ | |
252b5132 RH |
951 | if ((operand->exp.X_add_number & ~width) != 0 && |
952 | (operand->exp.X_add_number | width) != (~0)) | |
953 | { | |
e0c6ed95 AM |
954 | as_warn (_("operand %s0x%x out of range."), |
955 | string, operand->exp.X_add_number); | |
252b5132 RH |
956 | } |
957 | } | |
958 | ||
959 | } | |
879db8be | 960 | #endif |
252b5132 RH |
961 | |
962 | static char buffer[20]; | |
963 | ||
964 | static void | |
7f31df7c | 965 | newfix (ptr, type, size, operand) |
c0fecd35 AM |
966 | int ptr; |
967 | int type; | |
7f31df7c | 968 | int size; /* nibbles. */ |
c0fecd35 | 969 | expressionS *operand; |
252b5132 RH |
970 | { |
971 | if (operand->X_add_symbol | |
972 | || operand->X_op_symbol | |
973 | || operand->X_add_number) | |
974 | { | |
975 | fix_new_exp (frag_now, | |
976 | ptr, | |
7f31df7c | 977 | size / 2, |
252b5132 RH |
978 | operand, |
979 | 0, | |
980 | type); | |
981 | } | |
982 | } | |
983 | ||
984 | static char * | |
c0fecd35 AM |
985 | apply_fix (ptr, type, operand, size) |
986 | char *ptr; | |
987 | int type; | |
988 | expressionS *operand; | |
7f31df7c | 989 | int size; /* nibbles. */ |
252b5132 | 990 | { |
7f31df7c | 991 | long n = operand->X_add_number; |
252b5132 | 992 | |
7f31df7c | 993 | newfix ((ptr - buffer) / 2, type, size + 1, operand); |
252b5132 RH |
994 | switch (size) |
995 | { | |
879db8be | 996 | case 8: /* 8 nibbles == 32 bits. */ |
252b5132 RH |
997 | *ptr++ = n >> 28; |
998 | *ptr++ = n >> 24; | |
999 | *ptr++ = n >> 20; | |
1000 | *ptr++ = n >> 16; | |
879db8be | 1001 | case 4: /* 4 nibbles == 16 bits. */ |
252b5132 RH |
1002 | *ptr++ = n >> 12; |
1003 | *ptr++ = n >> 8; | |
1004 | case 2: | |
1005 | *ptr++ = n >> 4; | |
1006 | case 1: | |
1007 | *ptr++ = n >> 0; | |
1008 | break; | |
1009 | } | |
252b5132 | 1010 | return ptr; |
252b5132 RH |
1011 | } |
1012 | ||
e0c6ed95 AM |
1013 | /* Now we know what sort of opcodes it is. Let's build the bytes. */ |
1014 | ||
252b5132 | 1015 | #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y; |
19d63e5d | 1016 | |
252b5132 RH |
1017 | static void |
1018 | build_bytes (this_try, operand) | |
e0c6ed95 | 1019 | opcode_entry_type *this_try; |
879db8be | 1020 | struct z8k_op *operand ATTRIBUTE_UNUSED; |
252b5132 | 1021 | { |
252b5132 | 1022 | char *output_ptr = buffer; |
252b5132 | 1023 | int c; |
252b5132 RH |
1024 | int nibble; |
1025 | unsigned int *class_ptr; | |
1026 | ||
1027 | frag_wane (frag_now); | |
1028 | frag_new (0); | |
1029 | ||
25d3fb58 | 1030 | memset (buffer, 0, sizeof (buffer)); |
252b5132 | 1031 | class_ptr = this_try->byte_info; |
252b5132 | 1032 | |
879db8be | 1033 | for (nibble = 0; (c = *class_ptr++); nibble++) |
252b5132 RH |
1034 | { |
1035 | ||
1036 | switch (c & CLASS_MASK) | |
1037 | { | |
1038 | default: | |
252b5132 | 1039 | abort (); |
e0c6ed95 | 1040 | |
252b5132 | 1041 | case CLASS_ADDRESS: |
e0c6ed95 | 1042 | /* Direct address, we don't cope with the SS mode right now. */ |
252b5132 RH |
1043 | if (segmented_mode) |
1044 | { | |
879db8be | 1045 | /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */ |
252b5132 RH |
1046 | output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8); |
1047 | } | |
1048 | else | |
1049 | { | |
1050 | output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4); | |
1051 | } | |
1052 | da_operand = 0; | |
1053 | break; | |
1054 | case CLASS_DISP8: | |
e0c6ed95 | 1055 | /* pc rel 8 bit */ |
252b5132 RH |
1056 | output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2); |
1057 | da_operand = 0; | |
1058 | break; | |
1059 | ||
1060 | case CLASS_0DISP7: | |
e0c6ed95 | 1061 | /* pc rel 7 bit */ |
252b5132 RH |
1062 | *output_ptr = 0; |
1063 | output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2); | |
1064 | da_operand = 0; | |
1065 | break; | |
1066 | ||
1067 | case CLASS_1DISP7: | |
e0c6ed95 | 1068 | /* pc rel 7 bit */ |
252b5132 RH |
1069 | *output_ptr = 0x80; |
1070 | output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2); | |
e0c6ed95 | 1071 | output_ptr[-2] = 0x8; |
252b5132 RH |
1072 | da_operand = 0; |
1073 | break; | |
1074 | ||
1075 | case CLASS_BIT_1OR2: | |
1076 | *output_ptr = c & 0xf; | |
1077 | if (imm_operand) | |
1078 | { | |
1079 | if (imm_operand->X_add_number == 2) | |
e0c6ed95 | 1080 | *output_ptr |= 2; |
252b5132 | 1081 | else if (imm_operand->X_add_number != 1) |
e0c6ed95 | 1082 | as_bad (_("immediate must be 1 or 2")); |
252b5132 RH |
1083 | } |
1084 | else | |
e0c6ed95 | 1085 | as_bad (_("immediate 1 or 2 expected")); |
252b5132 RH |
1086 | output_ptr++; |
1087 | break; | |
1088 | case CLASS_CC: | |
1089 | *output_ptr++ = the_cc; | |
1090 | break; | |
e0c6ed95 AM |
1091 | case CLASS_0CCC: |
1092 | *output_ptr++ = the_ctrl; | |
1093 | break; | |
1094 | case CLASS_1CCC: | |
1095 | *output_ptr++ = the_ctrl | 0x8; | |
1096 | break; | |
1097 | case CLASS_00II: | |
1098 | *output_ptr++ = (~the_interrupt & 0x3); | |
1099 | break; | |
1100 | case CLASS_01II: | |
1101 | *output_ptr++ = (~the_interrupt & 0x3) | 0x4; | |
1102 | break; | |
1103 | case CLASS_FLAGS: | |
1104 | *output_ptr++ = the_flags; | |
1105 | break; | |
3c25c5f6 | 1106 | case CLASS_IGNORE: |
252b5132 RH |
1107 | case CLASS_BIT: |
1108 | *output_ptr++ = c & 0xf; | |
1109 | break; | |
1110 | case CLASS_REGN0: | |
1111 | if (reg[c & 0xf] == 0) | |
e0c6ed95 AM |
1112 | as_bad (_("can't use R0 here")); |
1113 | /* Fall through. */ | |
252b5132 RH |
1114 | case CLASS_REG: |
1115 | case CLASS_REG_BYTE: | |
1116 | case CLASS_REG_WORD: | |
1117 | case CLASS_REG_LONG: | |
1118 | case CLASS_REG_QUAD: | |
e0c6ed95 | 1119 | /* Insert bit mattern of right reg. */ |
252b5132 RH |
1120 | *output_ptr++ = reg[c & 0xf]; |
1121 | break; | |
1122 | case CLASS_DISP: | |
6840198f NC |
1123 | switch (c & ARG_MASK) |
1124 | { | |
1125 | case ARG_DISP12: | |
1126 | output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4); | |
1127 | break; | |
1128 | case ARG_DISP16: | |
879db8be NC |
1129 | output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4); |
1130 | break; | |
1131 | default: | |
1132 | output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4); | |
1133 | } | |
252b5132 RH |
1134 | da_operand = 0; |
1135 | break; | |
1136 | ||
1137 | case CLASS_IMM: | |
1138 | { | |
252b5132 RH |
1139 | switch (c & ARG_MASK) |
1140 | { | |
3c25c5f6 | 1141 | case ARG_NIM4: |
7f31df7c CG |
1142 | if (imm_operand->X_add_number > 15) |
1143 | { | |
1144 | as_bad (_("immediate value out of range")); | |
1145 | } | |
3c25c5f6 | 1146 | imm_operand->X_add_number = -imm_operand->X_add_number; |
252b5132 RH |
1147 | output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1); |
1148 | break; | |
7f31df7c | 1149 | /*case ARG_IMMNMINUS1: not used. */ |
252b5132 RH |
1150 | case ARG_IMM4M1: |
1151 | imm_operand->X_add_number--; | |
7f31df7c CG |
1152 | /* Drop through. */ |
1153 | case ARG_IMM4: | |
1154 | if (imm_operand->X_add_number > 15) | |
1155 | { | |
1156 | as_bad (_("immediate value out of range")); | |
1157 | } | |
252b5132 RH |
1158 | output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1); |
1159 | break; | |
1160 | case ARG_NIM8: | |
1161 | imm_operand->X_add_number = -imm_operand->X_add_number; | |
7f31df7c | 1162 | /* Drop through. */ |
252b5132 RH |
1163 | case ARG_IMM8: |
1164 | output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2); | |
1165 | break; | |
1166 | case ARG_IMM16: | |
1167 | output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4); | |
1168 | break; | |
252b5132 RH |
1169 | case ARG_IMM32: |
1170 | output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8); | |
1171 | break; | |
252b5132 RH |
1172 | default: |
1173 | abort (); | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | } | |
1178 | ||
e0c6ed95 | 1179 | /* Copy from the nibble buffer into the frag. */ |
252b5132 RH |
1180 | { |
1181 | int length = (output_ptr - buffer) / 2; | |
1182 | char *src = buffer; | |
1183 | char *fragp = frag_more (length); | |
1184 | ||
1185 | while (src < output_ptr) | |
1186 | { | |
1187 | *fragp = (src[0] << 4) | src[1]; | |
1188 | src += 2; | |
1189 | fragp++; | |
1190 | } | |
252b5132 | 1191 | } |
252b5132 RH |
1192 | } |
1193 | ||
1194 | /* This is the guts of the machine-dependent assembler. STR points to a | |
1994a7c7 NC |
1195 | machine dependent instruction. This function is supposed to emit |
1196 | the frags/bytes it assembles to. */ | |
252b5132 RH |
1197 | |
1198 | void | |
c0fecd35 AM |
1199 | md_assemble (str) |
1200 | char *str; | |
252b5132 | 1201 | { |
879db8be | 1202 | char c; |
252b5132 RH |
1203 | char *op_start; |
1204 | char *op_end; | |
252b5132 RH |
1205 | struct z8k_op operand[3]; |
1206 | opcode_entry_type *opcode; | |
1207 | opcode_entry_type *prev_opcode; | |
1208 | ||
e0c6ed95 | 1209 | /* Drop leading whitespace. */ |
252b5132 RH |
1210 | while (*str == ' ') |
1211 | str++; | |
1212 | ||
e0c6ed95 | 1213 | /* Find the op code end. */ |
252b5132 RH |
1214 | for (op_start = op_end = str; |
1215 | *op_end != 0 && *op_end != ' '; | |
1216 | op_end++) | |
e0c6ed95 | 1217 | ; |
252b5132 RH |
1218 | |
1219 | if (op_end == op_start) | |
1220 | { | |
1221 | as_bad (_("can't find opcode ")); | |
1222 | } | |
1223 | c = *op_end; | |
1224 | ||
1225 | *op_end = 0; | |
1226 | ||
e0c6ed95 | 1227 | opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start); |
252b5132 RH |
1228 | |
1229 | if (opcode == NULL) | |
1230 | { | |
1231 | as_bad (_("unknown opcode")); | |
1232 | return; | |
1233 | } | |
1234 | ||
1235 | if (opcode->opcode == 250) | |
1236 | { | |
252b5132 RH |
1237 | pseudo_typeS *p; |
1238 | char oc; | |
252b5132 RH |
1239 | char *old = input_line_pointer; |
1240 | *op_end = c; | |
1241 | ||
3c25c5f6 NC |
1242 | /* Was really a pseudo op. */ |
1243 | ||
252b5132 RH |
1244 | input_line_pointer = op_end; |
1245 | ||
1246 | oc = *old; | |
1247 | *old = '\n'; | |
1248 | while (*input_line_pointer == ' ') | |
1249 | input_line_pointer++; | |
1250 | p = (pseudo_typeS *) (opcode->func); | |
1251 | ||
1252 | (p->poc_handler) (p->poc_val); | |
1253 | input_line_pointer = old; | |
1254 | *old = oc; | |
1255 | } | |
1256 | else | |
1257 | { | |
3c25c5f6 NC |
1258 | char *new_input_line_pointer; |
1259 | ||
1260 | new_input_line_pointer = get_operands (opcode, op_end, operand); | |
1261 | if (new_input_line_pointer) | |
1262 | input_line_pointer = new_input_line_pointer; | |
7f31df7c | 1263 | prev_opcode = opcode; /* XXX is this used ?? */ |
252b5132 RH |
1264 | |
1265 | opcode = get_specific (opcode, operand); | |
1266 | ||
1267 | if (opcode == 0) | |
1268 | { | |
e0c6ed95 | 1269 | /* Couldn't find an opcode which matched the operands. */ |
252b5132 RH |
1270 | char *where = frag_more (2); |
1271 | ||
1272 | where[0] = 0x0; | |
1273 | where[1] = 0x0; | |
1274 | ||
1275 | as_bad (_("Can't find opcode to match operands")); | |
1276 | return; | |
1277 | } | |
1278 | ||
1279 | build_bytes (opcode, operand); | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | void | |
c0fecd35 | 1284 | tc_crawl_symbol_chain (headers) |
879db8be | 1285 | object_headers *headers ATTRIBUTE_UNUSED; |
252b5132 RH |
1286 | { |
1287 | printf (_("call to tc_crawl_symbol_chain \n")); | |
1288 | } | |
1289 | ||
7f31df7c CG |
1290 | /* We have no need to default values of symbols. */ |
1291 | ||
252b5132 | 1292 | symbolS * |
c0fecd35 | 1293 | md_undefined_symbol (name) |
879db8be | 1294 | char *name ATTRIBUTE_UNUSED; |
252b5132 RH |
1295 | { |
1296 | return 0; | |
1297 | } | |
1298 | ||
1299 | void | |
c0fecd35 | 1300 | tc_headers_hook (headers) |
879db8be | 1301 | object_headers *headers ATTRIBUTE_UNUSED; |
252b5132 RH |
1302 | { |
1303 | printf (_("call to tc_headers_hook \n")); | |
1304 | } | |
1305 | ||
e0c6ed95 AM |
1306 | /* Various routines to kill one day. */ |
1307 | /* Equal to MAX_PRECISION in atof-ieee.c. */ | |
252b5132 RH |
1308 | #define MAX_LITTLENUMS 6 |
1309 | ||
e0c6ed95 AM |
1310 | /* Turn a string in input_line_pointer into a floating point constant |
1311 | of type TYPE, and store the appropriate bytes in *LITP. The number | |
1312 | of LITTLENUMS emitted is stored in *SIZEP. An error message is | |
1313 | returned, or NULL on OK. */ | |
1314 | ||
252b5132 RH |
1315 | char * |
1316 | md_atof (type, litP, sizeP) | |
1317 | char type; | |
1318 | char *litP; | |
1319 | int *sizeP; | |
1320 | { | |
1321 | int prec; | |
1322 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
1323 | LITTLENUM_TYPE *wordP; | |
1324 | char *t; | |
252b5132 RH |
1325 | |
1326 | switch (type) | |
1327 | { | |
1328 | case 'f': | |
1329 | case 'F': | |
1330 | case 's': | |
1331 | case 'S': | |
1332 | prec = 2; | |
1333 | break; | |
1334 | ||
1335 | case 'd': | |
1336 | case 'D': | |
1337 | case 'r': | |
1338 | case 'R': | |
1339 | prec = 4; | |
1340 | break; | |
1341 | ||
1342 | case 'x': | |
1343 | case 'X': | |
1344 | prec = 6; | |
1345 | break; | |
1346 | ||
1347 | case 'p': | |
1348 | case 'P': | |
1349 | prec = 6; | |
1350 | break; | |
1351 | ||
1352 | default: | |
1353 | *sizeP = 0; | |
1354 | return _("Bad call to MD_ATOF()"); | |
1355 | } | |
1356 | t = atof_ieee (input_line_pointer, type, words); | |
1357 | if (t) | |
1358 | input_line_pointer = t; | |
1359 | ||
1360 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
1361 | for (wordP = words; prec--;) | |
1362 | { | |
1363 | md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE)); | |
1364 | litP += sizeof (LITTLENUM_TYPE); | |
1365 | } | |
1366 | return 0; | |
1367 | } | |
1368 | \f | |
5a38dc70 | 1369 | const char *md_shortopts = "z:"; |
e0c6ed95 | 1370 | |
3c25c5f6 NC |
1371 | struct option md_longopts[] = |
1372 | { | |
7f31df7c CG |
1373 | #define OPTION_RELAX (OPTION_MD_BASE) |
1374 | {"linkrelax", no_argument, NULL, OPTION_RELAX}, | |
3c25c5f6 NC |
1375 | {NULL, no_argument, NULL, 0} |
1376 | }; | |
e0c6ed95 AM |
1377 | |
1378 | size_t md_longopts_size = sizeof (md_longopts); | |
252b5132 RH |
1379 | |
1380 | int | |
1381 | md_parse_option (c, arg) | |
1382 | int c; | |
1383 | char *arg; | |
1384 | { | |
1385 | switch (c) | |
1386 | { | |
1387 | case 'z': | |
1388 | if (!strcmp (arg, "8001")) | |
7f31df7c | 1389 | s_segm (1); |
252b5132 | 1390 | else if (!strcmp (arg, "8002")) |
7f31df7c | 1391 | s_segm (0); |
252b5132 RH |
1392 | else |
1393 | { | |
1394 | as_bad (_("invalid architecture -z%s"), arg); | |
1395 | return 0; | |
1396 | } | |
7f31df7c CG |
1397 | z8k_target_from_cmdline = 1; |
1398 | break; | |
1399 | ||
1400 | case OPTION_RELAX: | |
1401 | linkrelax = 1; | |
252b5132 RH |
1402 | break; |
1403 | ||
1404 | default: | |
1405 | return 0; | |
1406 | } | |
1407 | ||
1408 | return 1; | |
1409 | } | |
1410 | ||
1411 | void | |
1412 | md_show_usage (stream) | |
1413 | FILE *stream; | |
1414 | { | |
e0c6ed95 | 1415 | fprintf (stream, _("\ |
7f31df7c CG |
1416 | Z8K options:\n\ |
1417 | -z8001 generate segmented code\n\ | |
1418 | -z8002 generate unsegmented code\n\ | |
1419 | -linkrelax create linker relaxable code\n")); | |
252b5132 RH |
1420 | } |
1421 | \f | |
252b5132 RH |
1422 | void |
1423 | md_convert_frag (headers, seg, fragP) | |
879db8be NC |
1424 | object_headers *headers ATTRIBUTE_UNUSED; |
1425 | segT seg ATTRIBUTE_UNUSED; | |
1426 | fragS *fragP ATTRIBUTE_UNUSED; | |
252b5132 | 1427 | { |
7f31df7c | 1428 | printf (_("call to md_convert_frag\n")); |
252b5132 RH |
1429 | abort (); |
1430 | } | |
1431 | ||
1432 | valueT | |
c0fecd35 AM |
1433 | md_section_align (seg, size) |
1434 | segT seg; | |
1435 | valueT size; | |
252b5132 | 1436 | { |
e0c6ed95 AM |
1437 | return ((size + (1 << section_alignment[(int) seg]) - 1) |
1438 | & (-1 << section_alignment[(int) seg])); | |
252b5132 RH |
1439 | } |
1440 | ||
1441 | void | |
94f592af | 1442 | md_apply_fix3 (fixP, valP, segment) |
14899840 | 1443 | fixS * fixP; |
94f592af NC |
1444 | valueT * valP; |
1445 | segT segment ATTRIBUTE_UNUSED; | |
252b5132 | 1446 | { |
94f592af | 1447 | long val = * (long *) valP; |
252b5132 RH |
1448 | char *buf = fixP->fx_where + fixP->fx_frag->fr_literal; |
1449 | ||
1450 | switch (fixP->fx_r_type) | |
1451 | { | |
1452 | case R_IMM4L: | |
7f31df7c | 1453 | buf[0] = (buf[0] & 0xf0) | (val & 0xf); |
252b5132 RH |
1454 | break; |
1455 | ||
1456 | case R_JR: | |
7f31df7c CG |
1457 | val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size; |
1458 | if (val & 1) | |
1459 | as_bad (_("cannot branch to odd address")); | |
1460 | val /= 2; | |
1461 | if (val > 127 || val < -128) | |
1462 | as_bad (_("relative jump out of range")); | |
252b5132 | 1463 | *buf++ = val; |
7f31df7c | 1464 | fixP->fx_no_overflow = 1; |
252b5132 RH |
1465 | break; |
1466 | ||
1467 | case R_DISP7: | |
7f31df7c CG |
1468 | val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size; |
1469 | if (val & 1) | |
1470 | as_bad (_("cannot branch to odd address")); | |
1471 | val /= 2; | |
1472 | if (val > 0 || val < -128) | |
1473 | as_bad (_("relative jump out of range")); | |
1474 | *buf = (*buf & 0x80) | (val & 0x7f); | |
1475 | fixP->fx_no_overflow = 1; | |
1476 | break; | |
252b5132 | 1477 | |
7f31df7c CG |
1478 | case R_CALLR: |
1479 | if (val > 8191 || val < -8192) | |
1480 | as_bad (_("relative call out of range")); | |
1481 | val = -val; | |
1482 | *buf++ = (buf[0] & 0xf0) | ((val >> 8) & 0xf); | |
1483 | *buf++ = val & 0xff; | |
252b5132 RH |
1484 | break; |
1485 | ||
1486 | case R_IMM8: | |
7f31df7c | 1487 | *buf++ = val; |
252b5132 | 1488 | break; |
7f31df7c | 1489 | |
252b5132 RH |
1490 | case R_IMM16: |
1491 | *buf++ = (val >> 8); | |
1492 | *buf++ = val; | |
1493 | break; | |
7f31df7c | 1494 | |
252b5132 RH |
1495 | case R_IMM32: |
1496 | *buf++ = (val >> 24); | |
1497 | *buf++ = (val >> 16); | |
1498 | *buf++ = (val >> 8); | |
1499 | *buf++ = val; | |
1500 | break; | |
7f31df7c CG |
1501 | |
1502 | case R_REL16: | |
1503 | val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size; | |
1504 | if (val > 32767 || val < -32768) | |
1505 | as_bad (_("relative address out of range")); | |
1506 | *buf++ = (val >> 8); | |
1507 | *buf++ = val; | |
1508 | fixP->fx_no_overflow = 1; | |
1509 | break; | |
1510 | ||
252b5132 RH |
1511 | #if 0 |
1512 | case R_DA | R_SEG: | |
1513 | *buf++ = (val >> 16); | |
1514 | *buf++ = 0x00; | |
1515 | *buf++ = (val >> 8); | |
1516 | *buf++ = val; | |
1517 | break; | |
1518 | #endif | |
1519 | ||
1520 | case 0: | |
1521 | md_number_to_chars (buf, val, fixP->fx_size); | |
1522 | break; | |
1523 | ||
1524 | default: | |
7f31df7c | 1525 | printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type); |
252b5132 | 1526 | abort (); |
252b5132 | 1527 | } |
94f592af NC |
1528 | |
1529 | if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0) | |
1530 | fixP->fx_done = 1; | |
252b5132 RH |
1531 | } |
1532 | ||
1533 | int | |
1534 | md_estimate_size_before_relax (fragP, segment_type) | |
879db8be NC |
1535 | register fragS *fragP ATTRIBUTE_UNUSED; |
1536 | register segT segment_type ATTRIBUTE_UNUSED; | |
252b5132 | 1537 | { |
7f31df7c | 1538 | printf (_("call to md_estimate_size_before_relax\n")); |
252b5132 RH |
1539 | abort (); |
1540 | } | |
1541 | ||
e0c6ed95 | 1542 | /* Put number into target byte order. */ |
252b5132 RH |
1543 | |
1544 | void | |
c0fecd35 AM |
1545 | md_number_to_chars (ptr, use, nbytes) |
1546 | char *ptr; | |
1547 | valueT use; | |
1548 | int nbytes; | |
252b5132 RH |
1549 | { |
1550 | number_to_chars_bigendian (ptr, use, nbytes); | |
1551 | } | |
e0c6ed95 | 1552 | |
252b5132 RH |
1553 | long |
1554 | md_pcrel_from (fixP) | |
879db8be | 1555 | fixS *fixP ATTRIBUTE_UNUSED; |
252b5132 RH |
1556 | { |
1557 | abort (); | |
1558 | } | |
1559 | ||
1560 | void | |
1561 | tc_coff_symbol_emit_hook (s) | |
879db8be | 1562 | symbolS *s ATTRIBUTE_UNUSED; |
252b5132 RH |
1563 | { |
1564 | } | |
1565 | ||
1566 | void | |
1567 | tc_reloc_mangle (fix_ptr, intr, base) | |
1568 | fixS *fix_ptr; | |
1569 | struct internal_reloc *intr; | |
1570 | bfd_vma base; | |
1571 | ||
1572 | { | |
1573 | symbolS *symbol_ptr; | |
1574 | ||
e0c6ed95 AM |
1575 | if (fix_ptr->fx_addsy |
1576 | && fix_ptr->fx_subsy) | |
252b5132 RH |
1577 | { |
1578 | symbolS *add = fix_ptr->fx_addsy; | |
1579 | symbolS *sub = fix_ptr->fx_subsy; | |
e0c6ed95 AM |
1580 | |
1581 | if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub)) | |
1582 | as_bad (_("Can't subtract symbols in different sections %s %s"), | |
1583 | S_GET_NAME (add), S_GET_NAME (sub)); | |
1584 | else | |
252b5132 | 1585 | { |
e0c6ed95 AM |
1586 | int diff = S_GET_VALUE (add) - S_GET_VALUE (sub); |
1587 | ||
1588 | fix_ptr->fx_addsy = 0; | |
1589 | fix_ptr->fx_subsy = 0; | |
1590 | fix_ptr->fx_offset += diff; | |
252b5132 | 1591 | } |
252b5132 RH |
1592 | } |
1593 | symbol_ptr = fix_ptr->fx_addsy; | |
1594 | ||
1595 | /* If this relocation is attached to a symbol then it's ok | |
e0c6ed95 | 1596 | to output it. */ |
252b5132 RH |
1597 | if (fix_ptr->fx_r_type == 0) |
1598 | { | |
e0c6ed95 | 1599 | /* cons likes to create reloc32's whatever the size of the reloc. */ |
252b5132 RH |
1600 | switch (fix_ptr->fx_size) |
1601 | { | |
1602 | case 2: | |
1603 | intr->r_type = R_IMM16; | |
1604 | break; | |
1605 | case 1: | |
1606 | intr->r_type = R_IMM8; | |
1607 | break; | |
1608 | case 4: | |
1609 | intr->r_type = R_IMM32; | |
1610 | break; | |
1611 | default: | |
1612 | abort (); | |
1613 | } | |
252b5132 RH |
1614 | } |
1615 | else | |
e0c6ed95 | 1616 | intr->r_type = fix_ptr->fx_r_type; |
252b5132 RH |
1617 | |
1618 | intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base; | |
1619 | intr->r_offset = fix_ptr->fx_offset; | |
1620 | ||
1621 | if (symbol_ptr) | |
1622 | intr->r_symndx = symbol_ptr->sy_number; | |
1623 | else | |
1624 | intr->r_symndx = -1; | |
1625 | } |