2005-04-05 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / gas / config / tc-pj.c
blob5a71e4037f644289a9384b15f727df18e254a0f1
1 /*-
2 tc-pj.c -- Assemble code for Pico Java
3 Copyright 1999, 2000, 2001, 2002, 2003, 2005
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Contributed by Steve Chamberlain of Transmeta <sac@pobox.com>. */
25 #include "as.h"
26 #include "safe-ctype.h"
27 #include "opcode/pj.h"
29 extern const pj_opc_info_t pj_opc_info[512];
31 const char comment_chars[] = "!/";
32 const char line_separator_chars[] = ";";
33 const char line_comment_chars[] = "/!#";
35 static int pending_reloc;
36 static struct hash_control *opcode_hash_control;
38 static void
39 little (int ignore ATTRIBUTE_UNUSED)
41 target_big_endian = 0;
44 static void
45 big (int ignore ATTRIBUTE_UNUSED)
47 target_big_endian = 1;
50 const pseudo_typeS md_pseudo_table[] =
52 {"ml", little, 0},
53 {"mb", big, 0},
54 {0, 0, 0}
57 const char FLT_CHARS[] = "rRsSfFdDxXpP";
58 const char EXP_CHARS[] = "eE";
60 void
61 md_operand (expressionS *op)
63 if (strncmp (input_line_pointer, "%hi16", 5) == 0)
65 if (pending_reloc)
66 as_bad (_("confusing relocation expressions"));
67 pending_reloc = BFD_RELOC_PJ_CODE_HI16;
68 input_line_pointer += 5;
69 expression (op);
72 if (strncmp (input_line_pointer, "%lo16", 5) == 0)
74 if (pending_reloc)
75 as_bad (_("confusing relocation expressions"));
76 pending_reloc = BFD_RELOC_PJ_CODE_LO16;
77 input_line_pointer += 5;
78 expression (op);
82 /* Parse an expression and then restore the input line pointer. */
84 static char *
85 parse_exp_save_ilp (char *s, expressionS *op)
87 char *save = input_line_pointer;
89 input_line_pointer = s;
90 expression (op);
91 s = input_line_pointer;
92 input_line_pointer = save;
93 return s;
96 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
97 reloc for a cons. We could use the definition there, except that
98 we want to handle magic pending reloc expressions specially. */
100 void
101 pj_cons_fix_new_pj (fragS *frag, int where, int nbytes, expressionS *exp)
103 static int rv[5][2] =
104 { { 0, 0 },
105 { BFD_RELOC_8, BFD_RELOC_8 },
106 { BFD_RELOC_PJ_CODE_DIR16, BFD_RELOC_16 },
107 { 0, 0 },
108 { BFD_RELOC_PJ_CODE_DIR32, BFD_RELOC_32 }};
110 fix_new_exp (frag, where, nbytes, exp, 0,
111 pending_reloc ? pending_reloc
112 : rv[nbytes][(now_seg->flags & SEC_CODE) ? 0 : 1]);
114 pending_reloc = 0;
117 /* Turn a reloc description character from the pj-opc.h table into
118 code which BFD can handle. */
120 static int
121 c_to_r (int x)
123 switch (x)
125 case O_R8:
126 return BFD_RELOC_8_PCREL;
127 case O_U8:
128 case O_8:
129 return BFD_RELOC_8;
130 case O_R16:
131 return BFD_RELOC_PJ_CODE_REL16;
132 case O_U16:
133 case O_16:
134 return BFD_RELOC_PJ_CODE_DIR16;
135 case O_R32:
136 return BFD_RELOC_PJ_CODE_REL32;
137 case O_32:
138 return BFD_RELOC_PJ_CODE_DIR32;
140 abort ();
141 return 0;
144 /* Handler for the ipush fake opcode,
145 turns ipush <foo> into sipush lo16<foo>, sethi hi16<foo>. */
147 static void
148 ipush_code (pj_opc_info_t *opcode ATTRIBUTE_UNUSED, char *str)
150 char *b = frag_more (6);
151 expressionS arg;
153 b[0] = 0x11;
154 b[3] = 0xed;
155 parse_exp_save_ilp (str + 1, &arg);
156 if (pending_reloc)
158 as_bad (_("can't have relocation for ipush"));
159 pending_reloc = 0;
162 fix_new_exp (frag_now, b - frag_now->fr_literal + 1, 2,
163 &arg, 0, BFD_RELOC_PJ_CODE_DIR16);
164 fix_new_exp (frag_now, b - frag_now->fr_literal + 4, 2,
165 &arg, 0, BFD_RELOC_PJ_CODE_HI16);
168 /* Insert names into the opcode table which are really mini macros,
169 not opcodes. The fakeness is indicated with an opcode of -1. */
171 static void
172 fake_opcode (const char *name,
173 void (*func) (struct pj_opc_info_t *, char *))
175 pj_opc_info_t * fake = xmalloc (sizeof (pj_opc_info_t));
177 fake->opcode = -1;
178 fake->opcode_next = -1;
179 fake->u.func = func;
180 hash_insert (opcode_hash_control, name, (char *) fake);
183 /* Enter another entry into the opcode hash table so the same opcode
184 can have another name. */
186 static void
187 alias (const char *new, const char *old)
189 hash_insert (opcode_hash_control, new,
190 (char *) hash_find (opcode_hash_control, old));
193 /* This function is called once, at assembler startup time. It sets
194 up the hash table with all the opcodes in it, and also initializes
195 some aliases for compatibility with other assemblers. */
197 void
198 md_begin (void)
200 const pj_opc_info_t *opcode;
201 opcode_hash_control = hash_new ();
203 /* Insert names into hash table. */
204 for (opcode = pj_opc_info; opcode->u.name; opcode++)
205 hash_insert (opcode_hash_control, opcode->u.name, (char *) opcode);
207 /* Insert the only fake opcode. */
208 fake_opcode ("ipush", ipush_code);
210 /* Add some aliases for opcode names. */
211 alias ("ifeq_s", "ifeq");
212 alias ("ifne_s", "ifne");
213 alias ("if_icmpge_s", "if_icmpge");
214 alias ("if_icmpne_s", "if_icmpne");
215 alias ("if_icmpeq_s", "if_icmpeq");
216 alias ("if_icmpgt_s", "if_icmpgt");
217 alias ("goto_s", "goto");
219 bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
222 /* This is the guts of the machine-dependent assembler. STR points to
223 a machine dependent instruction. This function is supposed to emit
224 the frags/bytes it assembles to. */
226 void
227 md_assemble (char *str)
229 char *op_start;
230 char *op_end;
232 pj_opc_info_t *opcode;
233 char *output;
234 int idx = 0;
235 char pend;
237 int nlen = 0;
239 /* Drop leading whitespace. */
240 while (*str == ' ')
241 str++;
243 /* Find the op code end. */
244 op_start = str;
245 for (op_end = str;
246 *op_end && !is_end_of_line[*op_end & 0xff] && *op_end != ' ';
247 op_end++)
248 nlen++;
250 pend = *op_end;
251 *op_end = 0;
253 if (nlen == 0)
254 as_bad (_("can't find opcode "));
256 opcode = (pj_opc_info_t *) hash_find (opcode_hash_control, op_start);
257 *op_end = pend;
259 if (opcode == NULL)
261 as_bad (_("unknown opcode %s"), op_start);
262 return;
265 if (opcode->opcode == -1)
267 /* It's a fake opcode. Dig out the args and pretend that was
268 what we were passed. */
269 (*opcode->u.func) (opcode, op_end);
271 else
273 int an;
275 output = frag_more (opcode->len);
276 output[idx++] = opcode->opcode;
278 if (opcode->opcode_next != -1)
279 output[idx++] = opcode->opcode_next;
281 for (an = 0; opcode->arg[an]; an++)
283 expressionS arg;
285 if (*op_end == ',' && an != 0)
286 op_end++;
288 if (*op_end == 0)
289 as_bad ("expected expresssion");
291 op_end = parse_exp_save_ilp (op_end, &arg);
293 fix_new_exp (frag_now,
294 output - frag_now->fr_literal + idx,
295 ASIZE (opcode->arg[an]),
296 &arg,
297 PCREL (opcode->arg[an]),
298 pending_reloc ? pending_reloc : c_to_r (opcode->arg[an]));
300 idx += ASIZE (opcode->arg[an]);
301 pending_reloc = 0;
304 while (ISSPACE (*op_end))
305 op_end++;
307 if (*op_end != 0)
308 as_warn ("extra stuff on line ignored");
312 if (pending_reloc)
313 as_bad ("Something forgot to clean up\n");
317 /* Turn a string in input_line_pointer into a floating point constant
318 of type type, and store the appropriate bytes in *LITP. The number
319 of LITTLENUMS emitted is stored in *SIZEP . An error message is
320 returned, or NULL on OK. */
322 char *
323 md_atof (int type, char *litP, int *sizeP)
325 int prec;
326 LITTLENUM_TYPE words[4];
327 char *t;
328 int i;
330 switch (type)
332 case 'f':
333 prec = 2;
334 break;
336 case 'd':
337 prec = 4;
338 break;
340 default:
341 *sizeP = 0;
342 return _("bad call to md_atof");
345 t = atof_ieee (input_line_pointer, type, words);
346 if (t)
347 input_line_pointer = t;
349 *sizeP = prec * 2;
351 if (!target_big_endian)
353 for (i = prec - 1; i >= 0; i--)
355 md_number_to_chars (litP, (valueT) words[i], 2);
356 litP += 2;
359 else
361 for (i = 0; i < prec; i++)
363 md_number_to_chars (litP, (valueT) words[i], 2);
364 litP += 2;
368 return NULL;
371 const char *md_shortopts = "";
373 struct option md_longopts[] =
375 #define OPTION_LITTLE (OPTION_MD_BASE)
376 #define OPTION_BIG (OPTION_LITTLE + 1)
378 {"little", no_argument, NULL, OPTION_LITTLE},
379 {"big", no_argument, NULL, OPTION_BIG},
380 {NULL, no_argument, NULL, 0}
382 size_t md_longopts_size = sizeof (md_longopts);
385 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
387 switch (c)
389 case OPTION_LITTLE:
390 little (0);
391 break;
392 case OPTION_BIG:
393 big (0);
394 break;
395 default:
396 return 0;
398 return 1;
401 void
402 md_show_usage (FILE *stream)
404 fprintf (stream, _("\
405 PJ options:\n\
406 -little generate little endian code\n\
407 -big generate big endian code\n"));
410 /* Apply a fixup to the object file. */
412 void
413 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
415 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
416 long val = *valP;
417 long max, min;
418 int shift;
420 max = min = 0;
421 shift = 0;
422 switch (fixP->fx_r_type)
424 case BFD_RELOC_VTABLE_INHERIT:
425 case BFD_RELOC_VTABLE_ENTRY:
426 fixP->fx_done = 0;
427 return;
429 case BFD_RELOC_PJ_CODE_REL16:
430 if (val < -0x8000 || val >= 0x7fff)
431 as_bad_where (fixP->fx_file, fixP->fx_line, _("pcrel too far"));
432 buf[0] |= (val >> 8) & 0xff;
433 buf[1] = val & 0xff;
434 break;
436 case BFD_RELOC_PJ_CODE_HI16:
437 *buf++ = val >> 24;
438 *buf++ = val >> 16;
439 fixP->fx_addnumber = val & 0xffff;
440 break;
442 case BFD_RELOC_PJ_CODE_DIR16:
443 case BFD_RELOC_PJ_CODE_LO16:
444 *buf++ = val >> 8;
445 *buf++ = val >> 0;
447 max = 0xffff;
448 min = -0xffff;
449 break;
451 case BFD_RELOC_8:
452 max = 0xff;
453 min = -0xff;
454 *buf++ = val;
455 break;
457 case BFD_RELOC_PJ_CODE_DIR32:
458 *buf++ = val >> 24;
459 *buf++ = val >> 16;
460 *buf++ = val >> 8;
461 *buf++ = val >> 0;
462 break;
464 case BFD_RELOC_32:
465 if (target_big_endian)
467 *buf++ = val >> 24;
468 *buf++ = val >> 16;
469 *buf++ = val >> 8;
470 *buf++ = val >> 0;
472 else
474 *buf++ = val >> 0;
475 *buf++ = val >> 8;
476 *buf++ = val >> 16;
477 *buf++ = val >> 24;
479 break;
481 case BFD_RELOC_16:
482 if (target_big_endian)
484 *buf++ = val >> 8;
485 *buf++ = val >> 0;
487 else
489 *buf++ = val >> 0;
490 *buf++ = val >> 8;
492 break;
494 default:
495 abort ();
498 if (max != 0 && (val < min || val > max))
499 as_bad_where (fixP->fx_file, fixP->fx_line, _("offset out of range"));
501 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
502 fixP->fx_done = 1;
505 /* Put number into target byte order. Always put values in an
506 executable section into big endian order. */
508 void
509 md_number_to_chars (char *ptr, valueT use, int nbytes)
511 if (target_big_endian || now_seg->flags & SEC_CODE)
512 number_to_chars_bigendian (ptr, use, nbytes);
513 else
514 number_to_chars_littleendian (ptr, use, nbytes);
517 /* Translate internal representation of relocation info to BFD target
518 format. */
520 arelent *
521 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
523 arelent *rel;
524 bfd_reloc_code_real_type r_type;
526 rel = xmalloc (sizeof (arelent));
527 rel->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
528 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
529 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
531 r_type = fixp->fx_r_type;
532 rel->addend = fixp->fx_addnumber;
533 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
535 if (rel->howto == NULL)
537 as_bad_where (fixp->fx_file, fixp->fx_line,
538 _("Cannot represent relocation type %s"),
539 bfd_get_reloc_code_name (r_type));
540 /* Set howto to a garbage value so that we can keep going. */
541 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
542 assert (rel->howto != NULL);
545 return rel;