2 tc-pj.c -- Assemble code for Pico Java
3 Copyright 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Contributed by Steve Chamberlain of Transmeta <sac@pobox.com>. */
25 #include "safe-ctype.h"
26 #include "opcode/pj.h"
28 extern const pj_opc_info_t pj_opc_info
[512];
30 const char comment_chars
[] = "!/";
31 const char line_separator_chars
[] = ";";
32 const char line_comment_chars
[] = "/!#";
34 static int pending_reloc
;
35 static struct hash_control
*opcode_hash_control
;
41 static char *parse_exp_save_ilp
42 PARAMS ((char *, expressionS
*));
45 static void ipush_code
46 PARAMS ((pj_opc_info_t
*, char *));
47 static void fake_opcode
48 PARAMS ((const char *, void (*) (struct pj_opc_info_t
*, char *)));
50 PARAMS ((const char *, const char *));
54 int ignore ATTRIBUTE_UNUSED
;
56 target_big_endian
= 0;
61 int ignore ATTRIBUTE_UNUSED
;
63 target_big_endian
= 1;
66 const pseudo_typeS md_pseudo_table
[] = {
72 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
73 const char EXP_CHARS
[] = "eE";
79 if (strncmp (input_line_pointer
, "%hi16", 5) == 0)
82 as_bad (_("confusing relocation expressions"));
83 pending_reloc
= BFD_RELOC_PJ_CODE_HI16
;
84 input_line_pointer
+= 5;
87 if (strncmp (input_line_pointer
, "%lo16", 5) == 0)
90 as_bad (_("confusing relocation expressions"));
91 pending_reloc
= BFD_RELOC_PJ_CODE_LO16
;
92 input_line_pointer
+= 5;
97 /* Parse an expression and then restore the input line pointer. */
100 parse_exp_save_ilp (s
, op
)
104 char *save
= input_line_pointer
;
105 input_line_pointer
= s
;
107 s
= input_line_pointer
;
108 input_line_pointer
= save
;
112 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
113 reloc for a cons. We could use the definition there, except that
114 we want to handle magic pending reloc expressions specially. */
117 pj_cons_fix_new_pj (frag
, where
, nbytes
, exp
)
123 static int rv
[5][2] =
125 { BFD_RELOC_8
, BFD_RELOC_8
},
126 { BFD_RELOC_PJ_CODE_DIR16
, BFD_RELOC_16
},
128 { BFD_RELOC_PJ_CODE_DIR32
, BFD_RELOC_32
}};
130 fix_new_exp (frag
, where
, nbytes
, exp
, 0,
131 pending_reloc
? pending_reloc
132 : rv
[nbytes
][(now_seg
->flags
& SEC_CODE
) ? 0 : 1]);
137 /* Turn a reloc description character from the pj-opc.h table into
138 code which BFD can handle. */
147 return BFD_RELOC_8_PCREL
;
152 return BFD_RELOC_PJ_CODE_REL16
;
155 return BFD_RELOC_PJ_CODE_DIR16
;
157 return BFD_RELOC_PJ_CODE_REL32
;
159 return BFD_RELOC_PJ_CODE_DIR32
;
165 /* Handler for the ipush fake opcode,
166 turns ipush <foo> into sipush lo16<foo>, sethi hi16<foo>. */
169 ipush_code (opcode
, str
)
170 pj_opc_info_t
*opcode ATTRIBUTE_UNUSED
;
173 char *b
= frag_more (6);
178 parse_exp_save_ilp (str
+ 1, &arg
);
181 as_bad (_("can't have relocation for ipush"));
185 fix_new_exp (frag_now
, b
- frag_now
->fr_literal
+ 1, 2,
186 &arg
, 0, BFD_RELOC_PJ_CODE_DIR16
);
187 fix_new_exp (frag_now
, b
- frag_now
->fr_literal
+ 4, 2,
188 &arg
, 0, BFD_RELOC_PJ_CODE_HI16
);
191 /* Insert names into the opcode table which are really mini macros,
192 not opcodes. The fakeness is indicated with an opcode of -1. */
195 fake_opcode (name
, func
)
197 void (*func
) PARAMS ((struct pj_opc_info_t
*, char *));
199 pj_opc_info_t
*fake
= (pj_opc_info_t
*) xmalloc (sizeof (pj_opc_info_t
));
202 fake
->opcode_next
= -1;
204 hash_insert (opcode_hash_control
, name
, (char *) fake
);
207 /* Enter another entry into the opcode hash table so the same opcode
208 can have another name. */
215 hash_insert (opcode_hash_control
, new,
216 (char *) hash_find (opcode_hash_control
, old
));
219 /* This function is called once, at assembler startup time. It sets
220 up the hash table with all the opcodes in it, and also initializes
221 some aliases for compatibility with other assemblers. */
226 const pj_opc_info_t
*opcode
;
227 opcode_hash_control
= hash_new ();
229 /* Insert names into hash table. */
230 for (opcode
= pj_opc_info
; opcode
->u
.name
; opcode
++)
231 hash_insert (opcode_hash_control
, opcode
->u
.name
, (char *) opcode
);
233 /* Insert the only fake opcode. */
234 fake_opcode ("ipush", ipush_code
);
236 /* Add some aliases for opcode names. */
237 alias ("ifeq_s", "ifeq");
238 alias ("ifne_s", "ifne");
239 alias ("if_icmpge_s", "if_icmpge");
240 alias ("if_icmpne_s", "if_icmpne");
241 alias ("if_icmpeq_s", "if_icmpeq");
242 alias ("if_icmpgt_s", "if_icmpgt");
243 alias ("goto_s", "goto");
245 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, 0);
248 /* This is the guts of the machine-dependent assembler. STR points to
249 a machine dependent instruction. This function is supposed to emit
250 the frags/bytes it assembles to. */
259 pj_opc_info_t
*opcode
;
266 /* Drop leading whitespace. */
270 /* Find the op code end. */
273 *op_end
&& !is_end_of_line
[*op_end
& 0xff] && *op_end
!= ' ';
281 as_bad (_("can't find opcode "));
283 opcode
= (pj_opc_info_t
*) hash_find (opcode_hash_control
, op_start
);
288 as_bad (_("unknown opcode %s"), op_start
);
292 if (opcode
->opcode
== -1)
294 /* It's a fake opcode. Dig out the args and pretend that was
295 what we were passed. */
296 (*opcode
->u
.func
) (opcode
, op_end
);
302 output
= frag_more (opcode
->len
);
303 output
[idx
++] = opcode
->opcode
;
305 if (opcode
->opcode_next
!= -1)
306 output
[idx
++] = opcode
->opcode_next
;
308 for (an
= 0; opcode
->arg
[an
]; an
++)
312 if (*op_end
== ',' && an
!= 0)
316 as_bad ("expected expresssion");
318 op_end
= parse_exp_save_ilp (op_end
, &arg
);
320 fix_new_exp (frag_now
,
321 output
- frag_now
->fr_literal
+ idx
,
322 ASIZE (opcode
->arg
[an
]),
324 PCREL (opcode
->arg
[an
]),
325 pending_reloc
? pending_reloc
: c_to_r (opcode
->arg
[an
]));
327 idx
+= ASIZE (opcode
->arg
[an
]);
331 while (ISSPACE (*op_end
))
335 as_warn ("extra stuff on line ignored");
340 as_bad ("Something forgot to clean up\n");
344 /* Turn a string in input_line_pointer into a floating point constant
345 of type type, and store the appropriate bytes in *LITP. The number
346 of LITTLENUMS emitted is stored in *SIZEP . An error message is
347 returned, or NULL on OK. */
350 md_atof (type
, litP
, sizeP
)
356 LITTLENUM_TYPE words
[4];
372 return _("bad call to md_atof");
375 t
= atof_ieee (input_line_pointer
, type
, words
);
377 input_line_pointer
= t
;
381 if (!target_big_endian
)
383 for (i
= prec
- 1; i
>= 0; i
--)
385 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
391 for (i
= 0; i
< prec
; i
++)
393 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
401 const char *md_shortopts
= "";
403 struct option md_longopts
[] = {
405 #define OPTION_LITTLE (OPTION_MD_BASE)
406 #define OPTION_BIG (OPTION_LITTLE + 1)
408 {"little", no_argument
, NULL
, OPTION_LITTLE
},
409 {"big", no_argument
, NULL
, OPTION_BIG
},
410 {NULL
, no_argument
, NULL
, 0}
412 size_t md_longopts_size
= sizeof (md_longopts
);
415 md_parse_option (c
, arg
)
417 char *arg ATTRIBUTE_UNUSED
;
434 md_show_usage (stream
)
437 fprintf (stream
, _("\
439 -little generate little endian code\n\
440 -big generate big endian code\n"));
443 /* Apply a fixup to the object file. */
446 md_apply_fix3 (fixP
, valP
, seg
)
449 segT seg ATTRIBUTE_UNUSED
;
451 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
458 switch (fixP
->fx_r_type
)
460 case BFD_RELOC_VTABLE_INHERIT
:
461 case BFD_RELOC_VTABLE_ENTRY
:
465 case BFD_RELOC_PJ_CODE_REL16
:
466 if (val
< -0x8000 || val
>= 0x7fff)
467 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("pcrel too far"));
468 buf
[0] |= (val
>> 8) & 0xff;
472 case BFD_RELOC_PJ_CODE_HI16
:
475 fixP
->fx_addnumber
= val
& 0xffff;
478 case BFD_RELOC_PJ_CODE_DIR16
:
479 case BFD_RELOC_PJ_CODE_LO16
:
493 case BFD_RELOC_PJ_CODE_DIR32
:
501 if (target_big_endian
)
518 if (target_big_endian
)
534 if (max
!= 0 && (val
< min
|| val
> max
))
535 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("offset out of range"));
537 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
541 /* Put number into target byte order. Always put values in an
542 executable section into big endian order. */
545 md_number_to_chars (ptr
, use
, nbytes
)
550 if (target_big_endian
|| now_seg
->flags
& SEC_CODE
)
551 number_to_chars_bigendian (ptr
, use
, nbytes
);
553 number_to_chars_littleendian (ptr
, use
, nbytes
);
556 /* Translate internal representation of relocation info to BFD target
560 tc_gen_reloc (section
, fixp
)
561 asection
*section ATTRIBUTE_UNUSED
;
565 bfd_reloc_code_real_type r_type
;
567 rel
= (arelent
*) xmalloc (sizeof (arelent
));
568 rel
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
569 *rel
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
570 rel
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
572 r_type
= fixp
->fx_r_type
;
573 rel
->addend
= fixp
->fx_addnumber
;
574 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, r_type
);
576 if (rel
->howto
== NULL
)
578 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
579 _("Cannot represent relocation type %s"),
580 bfd_get_reloc_code_name (r_type
));
581 /* Set howto to a garbage value so that we can keep going. */
582 rel
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
583 assert (rel
->howto
!= NULL
);