#include "as.h"
#include "safe-ctype.h"
#include "dwarf2dbg.h"
#include "opcode/crx.h"
#include "elf/crx.h"
#include <limits.h>
#define WORD_SIZE 16
#define WORD_SHIFT 16
#define REG_SIZE 4
#define INSN_MAX_SIZE 3
#define MAX_REGS_IN_MASK16 8
#define ESC_16 0xE
#define ESC_32 0xF
#define streq(a, b) (strcmp (a, b) == 0)
#define strneq(a, b, c) (strncmp (a, b, c) == 0)
#define SET_BITS_MASK(offs,n_bits) ((((1 << (n_bits)) - 1) << (offs)))
#define CLEAR_BITS_MASK(offs,n_bits) (~(((1 << (n_bits)) - 1) << (offs)))
#define GET_ACTUAL_TYPE \
for (i = 0; i < insn->nargs; i++) \
atyp_act[i] = getarg_type (instruction->operands[i].op_type)
#define GET_ACTUAL_SIZE \
for (i = 0; i < insn->nargs; i++) \
bits_act[i] = getbits (instruction->operands[i].op_type)
#define NO_OPERANDS_INST(OP) \
(streq (OP, "di") || streq (OP, "nop") \
|| streq (OP, "retx") || streq (OP, "ei") \
|| streq (OP, "wait") || streq (OP, "eiwait"))
#define CRX_PRINT(BYTE, NUM, SHIFT) output_opcode[BYTE] |= (NUM << SHIFT)
static struct hash_control *crx_inst_hash;
static struct hash_control *reg_hash;
static struct hash_control *copreg_hash;
const inst *instruction;
long output_opcode[2];
int relocatable;
int size_was_set;
int signflag;
int cst4flag;
char ins_parse[MAX_INST_LEN];
int post_inc_mode;
int processing_arg_number;
const char comment_chars[] = "#";
const char line_comment_chars[] = "#";
const char line_separator_chars[] = ";";
const char EXP_CHARS[] = "eE";
const char FLT_CHARS[] = "f'";
const char *md_shortopts = "";
struct option md_longopts[] = {
{NULL, no_argument, NULL, 0}
};
size_t md_longopts_size = sizeof (md_longopts);
const pseudo_typeS md_pseudo_table[] =
{
{"align", s_align_bytes, 0},
{0, 0, 0}
};
const relax_typeS md_relax_table[] =
{
{0xfa, -0x100, 2, 1},
{0xfffe, -0x10000, 4, 2},
{0xfffffffe, -0xfffffffe, 6, 0},
{0xfffe, -0x10000, 4, 4},
{0xfffffffe, -0xfffffffe, 6, 0},
{0xfe, -0x100, 4, 6},
{0xfffffe, -0x1000000, 6, 0}
};
static void reset_vars (char *, ins *);
static reg get_register (char *);
static copreg get_copregister (char *);
static void get_number_of_bits (ins *, int);
static argtype getarg_type (operand_type);
static int getbits (operand_type);
static int get_number_of_operands (void);
static void get_operandtype (char *, int, ins *);
static int gettrap (char *);
static void handle_pi_insn (char *);
static int get_cinv_parameters (char *);
static unsigned long getconstant (unsigned long, int);
static int getreg_image (reg);
static void parse_operands (ins *, char *);
static void parse_insn (ins *, char *);
static void print_operand (int, int, argument *);
static void print_constant (int, int, argument *);
static int exponent2scale (int);
static void mask_const (unsigned long *, int);
static void mask_reg (int, unsigned short *);
static int process_label_constant (char *, ins *, int);
static void set_indexmode_parameters (char *, ins *, int);
static void set_cons_rparams (char *, ins *, int);
static char * preprocess_reglist (char *, int *);
static int assemble_insn (char *, ins *);
static void print_insn (ins *);
static int
getbits (operand_type op)
{
if (op < MAX_OPRD)
return crx_optab[op].bit_size;
else
return 0;
}
static argtype
getarg_type (operand_type op)
{
if (op < MAX_OPRD)
return crx_optab[op].arg_type;
else
return nullargs;
}
static reg
get_register (char *reg_name)
{
const reg_entry *reg;
reg = (const reg_entry *) hash_find (reg_hash, reg_name);
if (reg != NULL)
return reg->value.reg_val;
else
return nullregister;
}
static copreg
get_copregister (char *copreg_name)
{
const reg_entry *copreg;
copreg = (const reg_entry *) hash_find (copreg_hash, copreg_name);
if (copreg != NULL)
return copreg->value.copreg_val;
else
return nullcopregister;
}
static void
mask_const (unsigned long int *t, int size)
{
*t &= (((LONGLONG)1 << size) - 1);
}
valueT
md_section_align (segT seg, valueT val)
{
if (seg == text_section)
return (val + 1) & ~1;
return val;
}
void
md_operand (expressionS * exp)
{
char c = *input_line_pointer;
switch (c)
{
case '*':
input_line_pointer++;
expression (exp);
break;
default:
break;
}
}
static void
reset_vars (char *op, ins *crx_ins)
{
unsigned int i;
processing_arg_number = relocatable = size_was_set
= signflag = post_inc_mode = cst4flag = 0;
memset (&output_opcode, '\0', sizeof (output_opcode));
for (i = 0; i < MAX_OPERANDS; i++)
crx_ins->arg[i].signflag = 0;
strcpy (ins_parse, op);
}
arelent *
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS * fixP)
{
arelent * reloc;
reloc = xmalloc (sizeof (arelent));
reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
*reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
reloc->addend = fixP->fx_offset;
if (fixP->fx_subsy != NULL)
as_bad_where (fixP->fx_file, fixP->fx_line,
_("can't resolve `%s' {%s section} - `%s' {%s section}"),
fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
segment_name (fixP->fx_addsy
? S_GET_SEGMENT (fixP->fx_addsy)
: absolute_section),
S_GET_NAME (fixP->fx_subsy),
segment_name (S_GET_SEGMENT (fixP->fx_addsy)));
assert ((int) fixP->fx_r_type > 0);
reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
if (reloc->howto == (reloc_howto_type *) NULL)
{
as_bad_where (fixP->fx_file, fixP->fx_line,
_("internal error: reloc %d (`%s') not supported by object file format"),
fixP->fx_r_type,
bfd_get_reloc_code_name (fixP->fx_r_type));
return NULL;
}
assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
return reloc;
}
int
md_estimate_size_before_relax (fragS *fragp, asection *seg)
{
relax_substateT subtype;
relax_substateT rlx_state[] = {0, 2,
3, 4,
5, 6};
for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
{
if (fragp->fr_subtype == rlx_state[subtype]
&& (!S_IS_DEFINED (fragp->fr_symbol)
|| seg != S_GET_SEGMENT (fragp->fr_symbol)))
{
fragp->fr_subtype = rlx_state[subtype + 1];
break;
}
}
if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
abort ();
return md_relax_table[fragp->fr_subtype].rlx_length;
}
void
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, fragS *fragP)
{
char *opcode = fragP->fr_literal + fragP->fr_fix;
bfd_reloc_code_real_type reloc;
subseg_change (sec, 0);
switch (fragP->fr_subtype)
{
case 0:
reloc = BFD_RELOC_CRX_REL8;
break;
case 1:
*opcode = 0x7e;
reloc = BFD_RELOC_CRX_REL16;
break;
case 2:
*opcode = 0x7f;
reloc = BFD_RELOC_CRX_REL32;
break;
case 3:
reloc = BFD_RELOC_CRX_REL16;
break;
case 4:
*++opcode = 0x31;
reloc = BFD_RELOC_CRX_REL32;
break;
case 5:
reloc = BFD_RELOC_CRX_REL8_CMP;
break;
case 6:
*++opcode = 0x31;
reloc = BFD_RELOC_CRX_REL24;
break;
default:
abort ();
break;
}
fix_new (fragP, fragP->fr_fix,
bfd_get_reloc_size (bfd_reloc_type_lookup (stdoutput, reloc)),
fragP->fr_symbol, fragP->fr_offset, 1, reloc);
fragP->fr_var = 0;
fragP->fr_fix += md_relax_table[fragP->fr_subtype].rlx_length;
}
int
md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
{
return 0;
}
void
md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
{
return;
}
char *
md_atof (int type, char *litP, int *sizeP)
{
int prec;
LITTLENUM_TYPE words[4];
char *t;
int i;
switch (type)
{
case 'f':
prec = 2;
break;
case 'd':
prec = 4;
break;
default:
*sizeP = 0;
return _("bad call to md_atof");
}
t = atof_ieee (input_line_pointer, type, words);
if (t)
input_line_pointer = t;
*sizeP = prec * 2;
if (! target_big_endian)
{
for (i = prec - 1; i >= 0; i--)
{
md_number_to_chars (litP, (valueT) words[i], 2);
litP += 2;
}
}
else
{
for (i = 0; i < prec; i++)
{
md_number_to_chars (litP, (valueT) words[i], 2);
litP += 2;
}
}
return NULL;
}
void
md_apply_fix3 (fixS *fixP, valueT *valP, segT seg)
{
valueT val = * valP;
char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
fixP->fx_offset = 0;
switch (fixP->fx_r_type)
{
case BFD_RELOC_CRX_NUM8:
bfd_put_8 (stdoutput, (unsigned char) val, buf);
break;
case BFD_RELOC_CRX_NUM16:
bfd_put_16 (stdoutput, val, buf);
break;
case BFD_RELOC_CRX_NUM32:
bfd_put_32 (stdoutput, val, buf);
break;
default:
abort ();
break;
}
fixP->fx_done = 0;
if (fixP->fx_addsy == NULL
&& fixP->fx_pcrel == 0)
fixP->fx_done = 1;
if (fixP->fx_pcrel == 1
&& fixP->fx_addsy != NULL
&& S_GET_SEGMENT (fixP->fx_addsy) == seg)
fixP->fx_done = 1;
}
long
md_pcrel_from (fixS *fixp)
{
return fixp->fx_frag->fr_address + fixp->fx_where;
}
void
md_begin (void)
{
const char *hashret = NULL;
int i = 0;
crx_inst_hash = hash_new ();
if (crx_inst_hash == NULL)
as_fatal (_("Virtual memory exhausted"));
while (crx_instruction[i].mnemonic != NULL)
{
const char *mnemonic = crx_instruction[i].mnemonic;
hashret = hash_insert (crx_inst_hash, mnemonic,
(PTR) &crx_instruction[i]);
if (hashret != NULL && *hashret != '\0')
as_fatal (_("Can't hash `%s': %s\n"), crx_instruction[i].mnemonic,
*hashret == 0 ? _("(unknown reason)") : hashret);
do
{
++i;
}
while (crx_instruction[i].mnemonic != NULL
&& streq (crx_instruction[i].mnemonic, mnemonic));
}
reg_hash = hash_new ();
{
const reg_entry *regtab;
for (regtab = crx_regtab;
regtab < (crx_regtab + NUMREGS); regtab++)
{
hashret = hash_insert (reg_hash, regtab->name, (PTR) regtab);
if (hashret)
as_fatal (_("Internal Error: Can't hash %s: %s"),
regtab->name,
hashret);
}
}
copreg_hash = hash_new ();
{
const reg_entry *copregtab;
for (copregtab = crx_copregtab; copregtab < (crx_copregtab + NUMCOPREGS);
copregtab++)
{
hashret = hash_insert (copreg_hash, copregtab->name, (PTR) copregtab);
if (hashret)
as_fatal (_("Internal Error: Can't hash %s: %s"),
copregtab->name,
hashret);
}
}
linkrelax = 1;
}
static void
get_number_of_bits (ins * crx_ins, int op_num)
{
int cnt_bits = 0;
unsigned long int temp = crx_ins->arg[op_num].constant;
const cst4_entry *cst4_op;
if (size_was_set)
return;
while (temp > 0)
{
temp >>= 1;
cnt_bits++;
}
if (IS_INSN_TYPE (ARITH_INS) && !relocatable && !signflag)
{
if (cnt_bits == 16)
{
crx_ins->arg[op_num].size = 17;
return;
}
}
if (IS_INSN_TYPE (LD_STOR_INS)
|| IS_INSN_TYPE (LD_STOR_INS_INC)
|| IS_INSN_TYPE (STOR_IMM_INS)
|| IS_INSN_TYPE (CSTBIT_INS))
{
if (!signflag && crx_ins->arg[op_num].type == arg_icr)
{
if (cnt_bits == 6)
{
crx_ins->arg[op_num].size = 7;
return;
}
if (cnt_bits == 22)
as_bad (_("Offset out of range in Instruction `%s'"), ins_parse);
}
}
if (IS_INSN_TYPE (LD_STOR_INS))
{
if (!signflag && crx_ins->arg[op_num].type == arg_cr)
{
if (cnt_bits == 16)
{
crx_ins->arg[op_num].size = 17;
return;
}
if (cnt_bits == 32)
as_bad (_("Offset out of range in Instruction `%s'"), ins_parse);
}
}
if (IS_INSN_TYPE (CSTBIT_INS)
|| IS_INSN_TYPE (LD_STOR_INS_INC)
|| IS_INSN_TYPE (STOR_IMM_INS))
{
if (!signflag && crx_ins->arg[op_num].type == arg_cr)
{
if (cnt_bits == 12)
{
crx_ins->arg[op_num].size = 13;
if (IS_INSN_TYPE (LD_STOR_INS_INC))
as_bad (_("Offset out of range in Instruction `%s'"), ins_parse);
return;
}
if (IS_INSN_TYPE (CSTBIT_INS) || IS_INSN_TYPE (STOR_IMM_INS))
{
if (cnt_bits == 28)
as_bad (_("Offset out of range in Instruction `%s'"), ins_parse);
}
}
}
if (signflag && !relocatable
&& ((IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS))
|| ((IS_INSN_TYPE (CMPBR_INS) && op_num == 0))))
{
for (cst4_op = cst4_map; cst4_op < (cst4_map + cst4_maps); cst4_op++)
{
if (crx_ins->arg[op_num].constant == (unsigned int)(-cst4_op->value))
{
crx_ins->arg[op_num].size = 4;
crx_ins->arg[op_num].constant = cst4_op->binary;
crx_ins->arg[op_num].signflag = 0;
return;
}
}
}
if (signflag && IS_INSN_TYPE (ARITH_BYTE_INS))
{
if (!relocatable)
{
if (crx_ins->arg[op_num].constant <= 0xffff)
crx_ins->arg[op_num].size = 16;
else
crx_ins->arg[op_num].size = 18;
}
else
crx_ins->arg[op_num].size = 16;
return;
}
if (signflag && IS_INSN_TYPE (ARITH_INS))
{
if (crx_ins->arg[op_num].constant <= 0xffff && !relocatable)
{
crx_ins->arg[op_num].size = 16;
return;
}
if (crx_ins->arg[op_num].constant < 0xffffffff || relocatable)
{
crx_ins->arg[op_num].size = 32;
return;
}
crx_ins->arg[op_num].size = 33;
return;
}
if (signflag && !relocatable)
return;
if (!relocatable)
crx_ins->arg[op_num].size = cnt_bits;
if (IS_INSN_TYPE (ARITH_INS) && !signflag)
{
if (cnt_bits > 32)
as_bad (_("Cannot represent Immediate in %d bits in Instruction `%s'"),
cnt_bits, ins_parse);
}
else if (IS_INSN_TYPE (ARITH_BYTE_INS) && !signflag)
{
if (cnt_bits > 16)
as_bad (_("Cannot represent Immediate in %d bits in Instruction `%s'"),
cnt_bits, ins_parse);
}
}
static int
process_label_constant (char *str, ins * crx_ins, int number)
{
char *save;
unsigned long int temp, cnt;
const cst4_entry *cst4_op;
int is_cst4=0;
int constant_val = 0;
int cmp_br_type_flag = 0, i;
int br_type_flag = 0;
save = input_line_pointer;
signflag = 0;
if (str[0] == '-')
{
signflag = 1;
str++;
}
else if (str[0] == '+')
str++;
if (strstr (str, ":s") != NULL && (IS_INSN_TYPE (CMPBR_INS)
|| IS_INSN_TYPE (COP_BRANCH_INS)))
cmp_br_type_flag = 8;
if (strstr (str, ":l") != NULL && (IS_INSN_TYPE (CMPBR_INS)
|| IS_INSN_TYPE (COP_BRANCH_INS)))
cmp_br_type_flag = 24;
if (IS_INSN_TYPE (BRANCH_INS))
{
if (strstr (str, ":s") != NULL)
br_type_flag = 8;
else if (strstr (str, ":m") != NULL)
br_type_flag = 16;
else if (strstr (str, ":l") != NULL)
br_type_flag = 32;
}
if (cmp_br_type_flag != 0 || br_type_flag != 0)
{
i = 0;
while (str[i] != ':')
{
i++;
}
str[i] = '\0';
}
input_line_pointer = str;
expression (&crx_ins->exp);
switch (crx_ins->exp.X_op)
{
case O_constant:
crx_ins->arg[number].constant = crx_ins->exp.X_add_number;
constant_val = crx_ins->exp.X_add_number;
if ((IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
&& number == 2)
{
LONGLONG temp64 = 0;
char ptr[20];
char temp_str[30];
unsigned int jump_value = 0;
int BR_MASK = 0, BR_SIZE = 0;
temp_str[0] = '\0';
if (signflag)
{
temp_str[0] = '-';
temp_str[1] = '\0';
}
strncat (temp_str, str, strlen (str));
temp64 = strtol (temp_str, (char **) &ptr,0);
if ((temp64 == LONG_MAX) || (temp64 == LONG_MIN))
as_bad (_("Overflow in displacement in Instruction `%s'"),
ins_parse);
if (!(((temp64 & UPPER31_MASK) == UPPER31_MASK)
|| ((temp64 & UPPER31_MASK) == 0x0)))
as_bad (_("Overflow in displacement in Instruction `%s'"),
ins_parse);
if (temp64 % 2 != 0)
as_bad (_("Odd Offset in displacement in Instruction `%s'"),
ins_parse);
jump_value = (unsigned int)temp64 & 0xFFFFFFFF;
if (((jump_value & 0xFFFFFF00) == 0xFFFFFF00)
|| ((jump_value & 0xFFFFFF00) == 0x0))
{
BR_MASK = 0xFF;
BR_SIZE = 8;
}
else
if (((jump_value & 0xFF000000) == 0xFF000000)
|| ((jump_value & 0xFF000000) == 0x0))
{
BR_MASK = 0xFFFFFF;
BR_SIZE = 24;
}
jump_value = jump_value >> 1;
crx_ins->arg[number].constant = jump_value & BR_MASK;
crx_ins->arg[number].size = BR_SIZE;
size_was_set = 1;
crx_ins->arg[number].signflag = signflag;
input_line_pointer = save;
return crx_ins->exp.X_op;
}
if (IS_INSN_TYPE (BRANCH_INS)
|| IS_INSN_MNEMONIC ("bal")
|| IS_INSN_TYPE (DCR_BRANCH_INS))
{
LONGLONG temp64 = 0;
char ptr[20];
char temp_str[30];
unsigned int jump_value = 0;
int BR_MASK = 0, BR_SIZE = 0;
temp_str[0] = '\0';
if (signflag)
{
temp_str[0] = '-';
temp_str[1] = '\0';
}
strncat (temp_str, str, strlen (str));
temp64 = strtol (temp_str, (char **) &ptr,0);
if ((temp64 == LONG_MAX) || (temp64 == LONG_MIN))
as_bad (_("Overflow in displacement in Instruction `%s'"),
ins_parse);
if (!(((temp64 & UPPER31_MASK) == UPPER31_MASK)
|| ((temp64 & UPPER31_MASK) == 0x0)))
as_bad (_("Overflow in displacement in Instruction `%s'"),
ins_parse);
if (temp64 % 2 != 0)
as_bad (_("Odd Offset in displacement in Instruction `%s'"),
ins_parse);
jump_value = (unsigned int)temp64 & 0xFFFFFFFF;
if (!IS_INSN_MNEMONIC ("bal") && !IS_INSN_TYPE (DCR_BRANCH_INS)
&& (((jump_value & 0xFFFFFF00) == 0xFFFFFF00)
|| ((jump_value & 0xFFFFFF00) == 0x0)))
{
BR_MASK = 0xFF;
BR_SIZE = 8;
}
else
if (((jump_value & 0xFFFF0000) == 0xFFFF0000)
|| ((jump_value & 0xFFFF0000) == 0x0))
{
BR_MASK = 0xFFFF;
BR_SIZE = 16;
}
else
{
BR_MASK = 0xFFFFFFFF;
BR_SIZE = 32;
}
jump_value = jump_value >> 1;
crx_ins->arg[number].constant = jump_value & BR_MASK;
crx_ins->arg[number].size = BR_SIZE;
size_was_set = 1;
crx_ins->arg[number].signflag = signflag;
input_line_pointer = save;
return crx_ins->exp.X_op;
}
if (constant_val < 0 && signflag != 1
&& !IS_INSN_TYPE (LD_STOR_INS) && !IS_INSN_TYPE (LD_STOR_INS_INC)
&& !IS_INSN_TYPE (CSTBIT_INS) && !IS_INSN_TYPE (STOR_IMM_INS)
&& !IS_INSN_TYPE (BRANCH_INS) && !IS_INSN_MNEMONIC ("bal"))
{
crx_ins->arg[number].constant =
~(crx_ins->arg[number].constant) + 1;
signflag = 1;
}
if (constant_val < 0 && signflag != 1
&& (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (LD_STOR_INS_INC)
|| IS_INSN_TYPE (CSTBIT_INS) || IS_INSN_TYPE (STOR_IMM_INS)))
{
if (crx_ins->arg[number].type == arg_cr
|| crx_ins->arg[number].type == arg_icr)
{
crx_ins->arg[number].constant =
~(crx_ins->arg[number].constant) + 1;
signflag = 1;
}
}
if (signflag)
{
if ((crx_ins->arg[number].constant != 1
&& crx_ins->arg[number].constant != 4)
|| (!IS_INSN_TYPE (ARITH_INS)
&& !IS_INSN_TYPE (ARITH_BYTE_INS)
&& !IS_INSN_TYPE (CMPBR_INS)))
{
cnt = 0;
temp = crx_ins->arg[number].constant - 1;
while (temp > 0)
{
temp >>= 1;
cnt++;
}
crx_ins->arg[number].size = cnt + 1;
crx_ins->arg[number].constant =
~(crx_ins->arg[number].constant) + 1;
if (IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS))
{
char ptr[30];
LONGLONG temp64;
temp64 = strtoul (str, (char **) &ptr, 0);
if (cnt < 4)
crx_ins->arg[number].size = 5;
if (IS_INSN_TYPE (ARITH_INS))
{
if (crx_ins->arg[number].size > 32
|| (temp64 == ULONG_MAX))
{
if (crx_ins->arg[number].size > 32)
as_bad (_("In Instruction `%s': Immediate size is \
%lu bits cannot be accomodated"),
ins_parse, cnt + 1);
if (temp64 == ULONG_MAX)
as_bad (_("Value given more than 32 bits in \
Instruction `%s'"), ins_parse);
}
}
if (IS_INSN_TYPE (ARITH_BYTE_INS))
{
if (crx_ins->arg[number].size > 16
|| !((temp64 & 0xFFFF0000) == 0xFFFF0000
|| (temp64 & 0xFFFF0000) == 0x0))
{
if (crx_ins->arg[number].size > 16)
as_bad (_("In Instruction `%s': Immediate size is \
%lu bits cannot be accomodated"),
ins_parse, cnt + 1);
if (!((temp64 & 0xFFFF0000) == 0xFFFF0000
|| (temp64 & 0xFFFF0000) == 0x0))
as_bad (_("Value given more than 16 bits in \
Instruction `%s'"), ins_parse);
}
}
}
if (IS_INSN_TYPE (LD_STOR_INS) && crx_ins->arg[number].type == arg_cr
&& !post_inc_mode)
{
if (crx_ins->arg[number].size <= 4)
crx_ins->arg[number].size = 5;
}
if ((IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
&& number == 2)
{
if (crx_ins->arg[number].size != 32)
crx_ins->arg[number].constant =
crx_ins->arg[number].constant >> 1;
}
mask_const (&crx_ins->arg[number].constant,
(int) crx_ins->arg[number].size);
}
}
else
{
if (((IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
&& number == 2)
|| IS_INSN_TYPE (BRANCH_NEQ_INS))
{
if (IS_INSN_TYPE (BRANCH_NEQ_INS))
{
if (crx_ins->arg[number].constant == 0)
as_bad (_("Instruction `%s' has Zero offset"), ins_parse);
}
if (crx_ins->arg[number].constant % 2 != 0)
as_bad (_("Instruction `%s' has odd offset"), ins_parse);
if (IS_INSN_TYPE (BRANCH_NEQ_INS))
{
if (crx_ins->arg[number].constant > 32
|| crx_ins->arg[number].constant < 2)
as_bad (_("Instruction `%s' has illegal offset (%ld)"),
ins_parse, crx_ins->arg[number].constant);
crx_ins->arg[number].constant -= 2;
}
crx_ins->arg[number].constant =
crx_ins->arg[number].constant >> 1;
get_number_of_bits (crx_ins, number);
}
if (IS_INSN_TYPE (CMPBR_INS) && number == 0)
{
for (cst4_op = cst4_map; cst4_op < (cst4_map + cst4_maps); cst4_op++)
{
if (crx_ins->arg[number].constant == (unsigned int)cst4_op->value)
{
crx_ins->arg[number].constant = cst4_op->binary;
is_cst4 = 1;
break;
}
}
if (!is_cst4)
as_bad (_("Instruction `%s' has invalid imm value as an \
operand"), ins_parse);
}
}
break;
case O_symbol:
case O_subtract:
crx_ins->arg[number].constant = 0;
relocatable = 1;
switch (crx_ins->arg[number].type)
{
case arg_cr:
if (IS_INSN_TYPE (LD_STOR_INS_INC))
crx_ins->rtype = BFD_RELOC_CRX_REGREL12;
else if (IS_INSN_TYPE (CSTBIT_INS)
|| IS_INSN_TYPE (STOR_IMM_INS))
crx_ins->rtype = BFD_RELOC_CRX_REGREL28;
else
crx_ins->rtype = BFD_RELOC_CRX_REGREL32;
break;
case arg_icr:
crx_ins->rtype = BFD_RELOC_CRX_REGREL22;
break;
case arg_c:
if (IS_INSN_MNEMONIC ("bal") || IS_INSN_TYPE (DCR_BRANCH_INS))
crx_ins->rtype = BFD_RELOC_CRX_REL16;
else if (IS_INSN_TYPE (BRANCH_INS))
{
crx_ins->rtype = BFD_RELOC_CRX_REL8;
switch (br_type_flag)
{
default:
break;
case 8:
crx_ins->rtype = BFD_RELOC_CRX_REL8;
break;
case 16:
crx_ins->rtype = BFD_RELOC_CRX_REL16;
break;
case 32:
crx_ins->rtype = BFD_RELOC_CRX_REL32;
break;
}
}
else if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (STOR_IMM_INS)
|| IS_INSN_TYPE (CSTBIT_INS))
crx_ins->rtype = BFD_RELOC_CRX_ABS32;
else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
crx_ins->rtype = BFD_RELOC_CRX_REL4;
else if (IS_INSN_TYPE (CMPBR_INS) || IS_INSN_TYPE (COP_BRANCH_INS))
{
if (cmp_br_type_flag == 24)
crx_ins->rtype = BFD_RELOC_CRX_REL24;
else
crx_ins->rtype = BFD_RELOC_CRX_REL8_CMP;
}
break;
case arg_ic:
case arg_dc:
if (IS_INSN_TYPE (ARITH_INS))
crx_ins->rtype = BFD_RELOC_CRX_IMM32;
else if (IS_INSN_TYPE (ARITH_BYTE_INS))
crx_ins->rtype = BFD_RELOC_CRX_IMM16;
break;
default:
break;
}
crx_ins->arg[number].size = (bfd_reloc_type_lookup (stdoutput, crx_ins->rtype))->bitsize;
break;
default:
break;
}
input_line_pointer = save;
crx_ins->arg[number].signflag = signflag;
return crx_ins->exp.X_op;
}
static int
exponent2scale (int val)
{
int exponent;
if (val == 0)
return 0;
for (exponent = 0; (val != 1); val >>= 1, exponent++)
;
return exponent;
}
static void
set_indexmode_parameters (char *operand, ins * crx_ins, int op_num)
{
char address_str[30];
char scale_str[MAX_OPERANDS];
int scale_cnt = 0;
char reg_name[MAX_REGNAME_LEN];
char regindex_name[MAX_REGNAME_LEN];
int i = 0;
int reg_counter = 0, addr_cnt = 0, temp_int_val = 0;
switch (crx_ins->arg[op_num].type)
{
case arg_icr:
while (operand[i] != '(')
{
address_str[addr_cnt++] = operand[i];
i++;
}
address_str[addr_cnt] = '\0';
process_label_constant (address_str, crx_ins, op_num);
i++;
reg_counter = 0;
while (operand[i] != ',' && operand[i] != ' ')
{
reg_name[reg_counter++] = operand[i];
i++;
}
reg_name[reg_counter] = '\0';
if ((crx_ins->arg[op_num].r = get_register (reg_name)) == nullregister)
as_bad (_("Illegal register `%s' in Instruction `%s'"),
reg_name, ins_parse);
i++;
while (operand[i] == ' ')
i++;
reg_counter = 0;
while (operand[i] != ')' && operand[i] != ',')
{
regindex_name[reg_counter++] = operand[i];
i++;
}
regindex_name[reg_counter] = '\0';
reg_counter = 0;
if ((crx_ins->arg[op_num].i_r = get_register (regindex_name))
== nullregister)
as_bad (_("Illegal register `%s' in Instruction `%s'"),
regindex_name, ins_parse);
while (operand[i] == ' ')
i++;
if (operand[i] == ')')
crx_ins->arg[op_num].scale = 0;
else
{
if (operand[i] == ',')
i++;
while (operand[i] != ' ' && operand[i] != ')')
{
scale_str[scale_cnt++] = operand[i];
i++;
}
scale_str[scale_cnt] = '\0';
if (strstr (scale_str, "0x") != NULL
|| strstr (scale_str, "0X") != NULL)
{
sscanf (scale_str, "%x", &temp_int_val);
memset (&scale_str, '\0', sizeof (scale_str));
sprintf (scale_str, "%d", temp_int_val);
}
temp_int_val = atoi (scale_str);
if (temp_int_val != 1 && temp_int_val != 2
&& temp_int_val != 4 && temp_int_val != 8)
as_bad (_("Illegal Scale - `%s'"), scale_str);
crx_ins->arg[op_num].scale = exponent2scale (temp_int_val);
}
break;
default:
break;
}
}
static void
set_cons_rparams (char *operand, ins * crx_ins, int op_num)
{
int i = 0, reg_count = 0;
char reg_name[MAX_REGNAME_LEN];
int change_flag = 0;
if (crx_ins->arg[op_num].type == arg_dc)
change_flag = 1;
switch (crx_ins->arg[op_num].type)
{
case arg_sc:
case arg_dc:
i++;
case arg_c:
process_label_constant (operand + i, crx_ins, op_num);
crx_ins->arg[op_num].type = arg_c;
break;
case arg_dcr:
operand++;
case arg_cr:
while (operand[i] != '(')
i++;
operand[i] = '\0';
process_label_constant (operand, crx_ins, op_num);
operand[i] = '(';
i++;
reg_count = 0;
while (operand[i] != ')')
{
reg_name[reg_count] = operand[i];
i++;
reg_count++;
}
reg_name[reg_count] = '\0';
if ((crx_ins->arg[op_num].r = get_register (reg_name)) == nullregister)
as_bad (_("Illegal register `%s' in Instruction `%s'"),
reg_name, ins_parse);
crx_ins->arg[op_num].type = arg_cr;
if (strstr (operand + i, "+") != NULL)
post_inc_mode = 1;
break;
default:
break;
}
if (change_flag == 1)
crx_ins->arg[op_num].type = arg_ic;
}
static void
get_operandtype (char *operand, int number, ins * crx_ins)
{
int ret_val;
char temp_operand[30];
switch (operand[0])
{
case 'r':
case 'c':
case 'i':
case 'u':
case 's':
case 'p':
case 'l':
case 'h':
ret_val = get_register (operand);
if (ret_val != nullregister)
{
crx_ins->arg[number].type = arg_r;
crx_ins->arg[number].r = ret_val;
crx_ins->arg[number].size = REG_SIZE;
}
else
{
ret_val = get_copregister (operand);
if (ret_val != nullcopregister)
{
crx_ins->arg[number].type = arg_copr;
if (ret_val >= cs0)
crx_ins->arg[number].type = arg_copsr;
crx_ins->arg[number].cr = ret_val;
crx_ins->arg[number].size = REG_SIZE;
}
else
{
if (strchr (operand, '(') != NULL)
{
if (strchr (operand, ',') != NULL
&& (strchr (operand, ',') > strchr (operand, '(')))
{
crx_ins->arg[number].type = arg_icr;
crx_ins->arg[number].constant = 0;
set_indexmode_parameters (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
return;
}
else
crx_ins->arg[number].type = arg_cr;
}
else
crx_ins->arg[number].type = arg_c;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
}
}
break;
case '$':
if (strchr (operand, '(') != NULL)
crx_ins->arg[number].type = arg_dcr;
else
crx_ins->arg[number].type = arg_dc;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
break;
case '(':
strcpy (temp_operand, "0");
strcat (temp_operand, operand);
if (strchr (temp_operand, ',') != NULL
&& (strchr (temp_operand, ',') > strchr (temp_operand, '(')))
{
crx_ins->arg[number].type = arg_icr;
crx_ins->arg[number].constant = 0;
set_indexmode_parameters (temp_operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
return;
}
else
{
crx_ins->arg[number].type = arg_cr;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
if ((! strneq (instruction->mnemonic, "load", 4))
&& (! strneq (instruction->mnemonic, "stor", 4)))
{
crx_ins->arg[number].type = arg_rbase;
crx_ins->arg[number].size = REG_SIZE;
}
return;
}
break;
case '*':
crx_ins->arg[number].type = arg_sc;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
break;
case '+':
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (strchr (operand, '(') != NULL)
{
if (strchr (operand, ',') != NULL
&& (strchr (operand, ',') > strchr (operand, '(')))
{
crx_ins->arg[number].type = arg_icr;
crx_ins->arg[number].constant = 0;
set_indexmode_parameters (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
return;
}
else
crx_ins->arg[number].type = arg_cr;
}
else
crx_ins->arg[number].type = arg_c;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
break;
default:
if (strchr (operand, '(') != NULL)
{
if (strchr (operand, ',') != NULL
&& (strchr (operand, ',') > strchr (operand, '(')))
{
crx_ins->arg[number].type = arg_icr;
crx_ins->arg[number].constant = 0;
set_indexmode_parameters (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
return;
}
else
crx_ins->arg[number].type = arg_cr;
}
else
crx_ins->arg[number].type = arg_c;
crx_ins->arg[number].constant = 0;
set_cons_rparams (operand, crx_ins, number);
get_number_of_bits (crx_ins, number);
break;
}
}
static void
parse_operands (ins * crx_ins, char *operands)
{
char *operandS;
char *operandH, *operandT;
int allocated = 0;
char *operand[MAX_OPERANDS];
int op_num = 0;
int bracket_flag = 0;
int sq_bracket_flag = 0;
operandS = operandH = operandT = (INST_HAS_REG_LIST) ?
preprocess_reglist (operands, &allocated) : operands;
while (*operandT != '\0')
{
if (*operandT == ',' && bracket_flag != 1 && sq_bracket_flag != 1)
{
*operandT++ = '\0';
operand[op_num++] = strdup (operandH);
operandH = operandT;
continue;
}
if (*operandT == ' ')
as_bad (_("Illegal operands (whitespace): `%s'"), ins_parse);
if (*operandT == '(')
bracket_flag = 1;
else if (*operandT == '[')
sq_bracket_flag = 1;
if (*operandT == ')')
{
if (bracket_flag)
bracket_flag = 0;
else
as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
}
else if (*operandT == ']')
{
if (sq_bracket_flag)
sq_bracket_flag = 0;
else
as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
}
if (bracket_flag == 1 && *operandT == ')')
bracket_flag = 0;
else if (sq_bracket_flag == 1 && *operandT == ']')
sq_bracket_flag = 0;
operandT++;
}
operand[op_num++] = strdup (operandH);
crx_ins->nargs = op_num;
if (bracket_flag || sq_bracket_flag)
as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
for (op_num = 0; op_num < crx_ins->nargs; op_num++)
{
get_operandtype (operand[op_num], op_num, crx_ins);
free (operand[op_num]);
}
if (allocated)
free (operandS);
}
static int
gettrap (char *s)
{
const trap_entry *trap;
for (trap = crx_traps; trap < (crx_traps + NUMTRAPS); trap++)
if (streq (trap->name, s))
return trap->entry;
as_bad (_("Unknown exception: `%s'"), s);
return 0;
}
static void
handle_pi_insn (char *operands)
{
if (strstr (operands, ")+") != NULL)
while (! IS_INSN_TYPE (LD_STOR_INS_INC))
instruction++;
}
static void
parse_insn (ins *insn, char *operands)
{
if (IS_INSN_MNEMONIC ("excp") || IS_INSN_MNEMONIC ("cinv"))
{
insn->nargs = 1;
insn->arg[0].type = arg_ic;
insn->arg[0].size = 4;
insn->arg[0].constant = IS_INSN_MNEMONIC ("excp") ?
gettrap (operands) : get_cinv_parameters (operands);
return;
}
if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (STOR_IMM_INS))
handle_pi_insn (operands);
if (operands != NULL)
parse_operands (insn, operands);
}
static int
get_cinv_parameters (char * operand)
{
char *p = operand;
int d_used = 0, i_used = 0, u_used = 0;
while (*++p != ']')
{
if (*p == ',' || *p == ' ')
continue;
if (*p == 'd')
d_used = 1;
else if (*p == 'i')
i_used = 1;
else if (*p == 'u')
u_used = 1;
else
as_bad (_("Illegal `cinv' parameter: `%c'"), *p);
}
return ((d_used ? 4 : 0)
+ (i_used ? 2 : 0)
+ (u_used ? 1 : 0));
}
static int
getreg_image (reg r)
{
const reg_entry *reg;
char *reg_name;
int special_register_flag = 0;
int movpr_flag = 0;
if (IS_INSN_MNEMONIC ("mtpr") || IS_INSN_MNEMONIC ("mfpr"))
movpr_flag = 1;
if (((IS_INSN_MNEMONIC ("mtpr")) && (processing_arg_number == 1))
|| ((IS_INSN_MNEMONIC ("mfpr")) && (processing_arg_number == 0)) )
special_register_flag = 1;
if (r < MAX_REG)
reg = &crx_regtab[r];
else if (r < MAX_COPREG)
reg = &crx_copregtab[r-MAX_REG];
else
{
as_bad (_("Unknown register: `%d'"), r);
return 0;
}
reg_name = reg->name;
#define IMAGE_ERR \
as_bad (_("Illegal register (`%s') in Instruction: `%s'"), \
reg_name, ins_parse); \
break;
switch (reg->type)
{
case CRX_U_REGTYPE:
case CRX_CFG_REGTYPE:
case CRX_MTPR_REGTYPE:
if (movpr_flag && special_register_flag)
return reg->image;
else
IMAGE_ERR;
case CRX_R_REGTYPE:
case CRX_C_REGTYPE:
case CRX_CS_REGTYPE:
if (!(movpr_flag && special_register_flag))
return reg->image;
else
IMAGE_ERR;
default:
IMAGE_ERR;
}
return 0;
}
static unsigned long int
getconstant (unsigned long int x, int nbits)
{
int cnt = 0;
unsigned long int temp = x;
while (temp > 0)
{
temp >>= 1;
cnt++;
}
if (cnt > nbits)
as_bad (_("Value `%ld' truncated to fit `%d' bits in instruction `%s'"),
x, cnt, ins_parse);
else
{
if (signflag)
x |= SET_BITS_MASK (cnt, nbits - cnt);
else
x &= CLEAR_BITS_MASK (cnt, nbits - cnt);
}
return (x & ((((1 << (nbits - 1)) - 1) << 1) | 1));
}
static void
print_constant (int nbits, int shift, argument *arg)
{
unsigned long mask = 0;
long constant = getconstant (arg->constant, nbits);
switch (nbits)
{
case 32:
case 28:
case 24:
case 22:
mask = (1 << (nbits - 16)) - 1;
CRX_PRINT (0, (constant >> WORD_SHIFT) & mask, 0);
CRX_PRINT (1, (constant & 0xFFFF), WORD_SHIFT);
break;
case 16:
case 12:
if (arg->type == arg_cr)
{
CRX_PRINT (0, constant, 0);
break;
}
if (instruction->size > 2)
CRX_PRINT (1, constant, WORD_SHIFT);
else
CRX_PRINT (0, constant, shift);
break;
default:
CRX_PRINT (0, constant, shift);
break;
}
}
static void
print_operand (int nbits, int shift, argument *arg)
{
switch (arg->type)
{
case arg_r:
CRX_PRINT (0, getreg_image (arg->r), shift);
break;
case arg_copr:
if (arg->cr < c0 || arg->cr > c15)
as_bad (_("Illegal Co-processor register in Instruction `%s' "),
ins_parse);
CRX_PRINT (0, getreg_image (arg->cr), shift);
break;
case arg_copsr:
if (arg->cr < cs0 || arg->cr > cs15)
as_bad (_("Illegal Co-processor special register in Instruction `%s' "),
ins_parse);
CRX_PRINT (0, getreg_image (arg->cr), shift);
break;
case arg_ic:
print_constant (nbits, shift, arg);
break;
case arg_icr:
CRX_PRINT (0, getreg_image (arg->r), 12);
CRX_PRINT (0, getreg_image (arg->i_r), 8);
CRX_PRINT (0, arg->scale, 6);
print_constant (nbits, shift, arg);
break;
case arg_rbase:
CRX_PRINT (0, getreg_image (arg->r), shift);
break;
case arg_cr:
if ((instruction->flags & CST4MAP) && cst4flag)
output_opcode[0] |= (getconstant (arg->constant, nbits)
<< (shift + REG_SIZE));
else
print_constant (nbits, shift, arg);
CRX_PRINT (0, getreg_image (arg->r), shift);
break;
case arg_c:
print_constant (nbits, shift, arg);
break;
default:
break;
}
}
static int
get_number_of_operands (void)
{
int i;
for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
;
return i;
}
static int
assemble_insn (char *mnemonic, ins *insn)
{
argtype atyp[MAX_OPERANDS];
argtype atyp_act[MAX_OPERANDS];
int bits[MAX_OPERANDS];
int bits_act[MAX_OPERANDS];
int shift_act[MAX_OPERANDS];
int match = 0;
int done_flag = 0;
int cst4maptype = 0;
int changed_already = 0;
unsigned int temp_value = 0;
int instrtype, i;
unsigned long int *cons;
const cst4_entry *cst4_op;
if (insn->nargs == 0)
{
output_opcode[0] = BIN (instruction->match, instruction->match_bits);
return 1;
}
while (get_number_of_operands () != insn->nargs
&& IS_INSN_MNEMONIC (mnemonic))
instruction++;
if (!IS_INSN_MNEMONIC (mnemonic))
return 0;
for (i = 0; i < insn->nargs; i++)
{
atyp[i] = insn->arg[i].type;
bits[i] = insn->arg[i].size;
}
GET_ACTUAL_TYPE;
GET_ACTUAL_SIZE;
while (match != 1
&& instruction->mnemonic != NULL
&& IS_INSN_MNEMONIC (mnemonic))
{
for (i = 0; i < insn->nargs; i++)
{
if (atyp_act[i] == atyp[i])
done_flag = 1;
else
{
done_flag = 0;
break;
}
}
if (done_flag)
{
if (post_inc_mode == 1 || IS_INSN_TYPE (LD_STOR_INS_INC))
done_flag = (post_inc_mode == IS_INSN_TYPE (LD_STOR_INS_INC));
}
if (done_flag == 0)
{
instruction++;
GET_ACTUAL_TYPE;
GET_ACTUAL_SIZE;
continue;
}
else
{
for (i = 0; i < insn->nargs; i++)
{
if (bits[i] > bits_act[i])
{
done_flag = 0;
instruction++;
GET_ACTUAL_TYPE;
GET_ACTUAL_SIZE;
break;
}
}
}
if (done_flag == 1)
{
match = 1;
break;
}
}
if (match == 0)
return 0;
else
{
if (!signflag)
{
if (IS_INSN_TYPE (LD_STOR_INS) && !relocatable)
{
instrtype = instruction->flags & REVERSE_MATCH ? 0 : 1;
cons = &insn->arg[instrtype].constant;
cst4maptype = instruction->flags & CST4MAP;
switch (cst4maptype)
{
case DISPUB4:
if (*cons == 14 || *cons == 15)
{
instruction++;
GET_ACTUAL_SIZE;
}
break;
case DISPUW4:
if (*cons <= 15 && *cons % 2 != 0)
{
instruction++;
GET_ACTUAL_SIZE;
}
else if (*cons > 15 && *cons < 27 && *cons % 2 == 0)
{
instruction--;
GET_ACTUAL_SIZE;
}
if (*cons < 27 && *cons % 2 == 0)
*cons /= 2;
break;
case DISPUD4:
if (*cons <= 15 && *cons % 4 != 0)
{
instruction++;
GET_ACTUAL_SIZE;
}
else if (*cons > 15 && *cons < 53 && *cons % 4 == 0)
{
instruction--;
GET_ACTUAL_SIZE;
}
if (*cons < 53 && *cons % 4 == 0)
*cons /= 4;
break;
default:
break;
}
}
if ((IS_INSN_TYPE (ARITH_BYTE_INS) || IS_INSN_TYPE (ARITH_INS))
&& !relocatable)
{
if ((instruction->operands[0].op_type == cst4
|| instruction->operands[0].op_type == i16)
&& (instruction->operands[1].op_type == regr))
{
if (insn->arg[0].constant == ESC_16
|| insn->arg[0].constant == ESC_32)
{
instruction++;
GET_ACTUAL_SIZE;
}
else
{
for (cst4_op = cst4_map; cst4_op < (cst4_map + cst4_maps);
cst4_op++)
{
if (insn->arg[0].constant == (unsigned int)cst4_op->binary
&& cst4_op->binary != cst4_op->value)
{
instruction++;
GET_ACTUAL_SIZE;
}
else if (insn->arg[0].constant == (unsigned int)cst4_op->value
&& insn->arg[0].constant >= 16)
{
instruction--;
insn->arg[0].constant = cst4_op->binary;
GET_ACTUAL_SIZE;
}
}
}
}
if (IS_INSN_MNEMONIC ("addub"))
{
if ((instruction->operands[0].op_type == cst4)
&& instruction->operands[1].op_type == regr)
{
if (insn->arg[0].constant == 0 && insn->arg[1].r == r0)
instruction++;
}
}
}
if (IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (STOR_IMM_INS)
|| IS_INSN_TYPE (LD_STOR_INS_INC))
{
instrtype = instruction->flags & REVERSE_MATCH ? 0 : 1;
if (instruction->operands[instrtype].op_type == rbase)
instruction++;
}
if (IS_INSN_TYPE (LD_STOR_INS_INC))
{
if (!((strneq (instruction->mnemonic, "stor", 4))
&& (insn->arg[0].type != arg_r)))
if (insn->arg[0].r == insn->arg[1].r)
as_bad (_("Invalid instruction : `%s' Source and Destination register \
same in Post INC mode"), ins_parse);
}
if (IS_INSN_TYPE (CSTBIT_INS) && !relocatable)
{
if (instruction->operands[1].op_type == rbase_dispu12)
{
if (insn->arg[1].constant == 0)
{
instruction--;
GET_ACTUAL_SIZE;
}
}
}
if ((IS_INSN_TYPE (LD_STOR_INS) || IS_INSN_TYPE (CSTBIT_INS)
|| IS_INSN_TYPE (STOR_IMM_INS)
|| IS_INSN_TYPE (LD_STOR_INS_INC)) & !relocatable)
{
instrtype = instruction->flags & REVERSE_MATCH ? 0 : 1;
changed_already = 0;
if (instruction->operands[instrtype].op_type == abs32)
{
if ((insn->arg[instrtype].constant & 0xFFFF0000) == 0xFFFF0000)
{
instruction--;
insn->arg[instrtype].constant =
insn->arg[instrtype].constant & 0xFFFF;
insn->arg[instrtype].size = 16;
changed_already = 1;
GET_ACTUAL_SIZE;
}
}
if (instruction->operands[instrtype].op_type == abs16
&& changed_already != 1)
{
instruction++;
insn->arg[instrtype].constant =
insn->arg[instrtype].constant & 0xFFFF;
insn->arg[instrtype].size = 32;
GET_ACTUAL_SIZE;
}
changed_already = 0;
}
if (IS_INSN_TYPE (BRANCH_INS) && !relocatable)
{
if (insn->arg[0].constant == 0x7e || insn->arg[0].constant == 0x7f)
{
instruction++;
GET_ACTUAL_SIZE;
}
}
}
for (i = 0; i < insn->nargs; i++)
{
if (instruction->operands[i].op_type == cst4
|| instruction->operands[i].op_type == rbase_cst4)
cst4flag = 1;
}
output_opcode[0] = BIN (instruction->match, instruction->match_bits);
if (IS_INSN_TYPE (COP_BRANCH_INS))
{
temp_value = insn->arg[0].constant;
insn->arg[0].constant = insn->arg[1].constant;
insn->arg[1].constant = temp_value;
}
for (i = 0; i < insn->nargs; i++)
{
shift_act[i] = instruction->operands[i].shift;
signflag = insn->arg[i].signflag;
processing_arg_number = i;
print_operand (bits_act[i], shift_act[i], &insn->arg[i]);
}
}
return 1;
}
static void
mask_reg (int r, unsigned short int *mask)
{
if ((reg)r > (reg)sp)
{
as_bad (_("Invalid Register in Register List"));
return;
}
*mask |= (1 << r);
}
static char *
preprocess_reglist (char *param, int *allocated)
{
char reg_name[MAX_REGNAME_LEN];
char *regP;
int reg_counter = 0;
unsigned short int mask = 0;
char *new_param;
char *paramP = param;
char maskstring[10];
reg r;
copreg cr;
if (strchr (paramP, '{') == NULL)
return param;
if (strchr (paramP, '}') == NULL)
as_fatal (_("Missing matching brackets : `%s'"), ins_parse);
while (*paramP++ != '{');
new_param = (char *)xcalloc (MAX_INST_LEN, sizeof (char));
*allocated = 1;
strncpy (new_param, param, paramP - param - 1);
while (*paramP != '}')
{
regP = paramP;
memset (®_name, '\0', sizeof (reg_name));
while (ISALNUM (*paramP))
paramP++;
strncpy (reg_name, regP, paramP - regP);
if (IS_INSN_TYPE (COP_REG_INS))
{
if ((cr = get_copregister (reg_name)) == nullcopregister)
as_bad (_("Illegal register `%s' in cop-register list"), reg_name);
mask_reg (getreg_image (cr - c0), &mask);
}
else
{
if ((r = get_register (reg_name)) == nullregister)
as_bad (_("Illegal register `%s' in register list"), reg_name);
mask_reg (getreg_image (r), &mask);
}
if (++reg_counter > MAX_REGS_IN_MASK16)
as_bad (_("Maximum %d bits may be set in `mask16' operand"),
MAX_REGS_IN_MASK16);
while (!ISALNUM (*paramP) && *paramP != '}')
paramP++;
}
if (*++paramP != '\0')
as_warn (_("rest of line ignored; first ignored character is `%c'"),
*paramP);
if (mask == 0)
as_bad (_("Illegal `mask16' operand, operation is undefined - `%s'"),
ins_parse);
sprintf (maskstring, "$0x%x", mask);
strcat (new_param, maskstring);
return new_param;
}
void
print_insn (ins *insn)
{
unsigned int i, j, insn_size;
char *this_frag;
unsigned short words[4];
for (i = 0, j = 0; i < 2; i++)
{
words[j++] = (output_opcode[i] >> 16) & 0xFFFF;
words[j++] = output_opcode[i] & 0xFFFF;
}
if ((instruction->flags & RELAXABLE) && relocatable)
{
int relax_subtype;
insn_size = INSN_MAX_SIZE;
if (IS_INSN_TYPE (BRANCH_INS))
relax_subtype = 0;
else if (IS_INSN_TYPE (DCR_BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
relax_subtype = 3;
else if (IS_INSN_TYPE (CMPBR_INS))
relax_subtype = 5;
else
abort ();
this_frag = frag_var (rs_machine_dependent, insn_size * 2,
4, relax_subtype,
insn->exp.X_add_symbol,
insn->exp.X_add_number,
0);
}
else
{
insn_size = instruction->size;
this_frag = frag_more (insn_size * 2);
if ((relocatable) && (insn->rtype != BFD_RELOC_NONE))
{
reloc_howto_type *reloc_howto;
int size;
reloc_howto = bfd_reloc_type_lookup (stdoutput, insn->rtype);
if (!reloc_howto)
abort ();
size = bfd_get_reloc_size (reloc_howto);
if (size < 1 || size > 4)
abort ();
fix_new_exp (frag_now, this_frag - frag_now->fr_literal,
size, &insn->exp, reloc_howto->pc_relative,
insn->rtype);
}
}
for (i = 0; i < insn_size; i++)
{
md_number_to_chars (this_frag, (valueT) words[i], 2);
this_frag += 2;
}
}
void
md_assemble (char *op)
{
ins crx_ins;
char *param;
char c;
reset_vars (op, &crx_ins);
for (param = op; *param != 0 && !ISSPACE (*param); param++)
;
c = *param;
*param++ = '\0';
instruction = (const inst *) hash_find (crx_inst_hash, op);
if (instruction == NULL)
{
as_bad (_("Unknown opcode: `%s'"), op);
return;
}
dwarf2_emit_insn (0);
if (NO_OPERANDS_INST (op))
crx_ins.nargs = 0;
else
parse_insn (&crx_ins, param);
if (assemble_insn (op, &crx_ins) == 0)
{
as_bad (_("Illegal operands in instruction : `%s'"), ins_parse);
return;
}
print_insn (&crx_ins);
}