Back to index

cell-binutils  2.17cvs20070401
Classes | Defines | Functions | Variables
itbl-ops.c File Reference
#include "as.h"
#include "itbl-ops.h"
#include <itbl-parse.h>

Go to the source code of this file.

Classes

struct  itbl_range
struct  itbl_field
struct  itbl_entry

Defines

#define ASSERT(x)
#define DBG(x)
#define min(a, b)   (a<b?a:b)

Functions

static unsigned long build_opcode (struct itbl_entry *e)
static e_type get_type (int yytype)
static e_processor get_processor (int yyproc)
static struct itbl_entry ** get_entries (e_processor processor, e_type type)
static struct itbl_entryfind_entry_byname (e_processor processor, e_type type, char *name)
static struct itbl_entryfind_entry_byval (e_processor processor, e_type type, unsigned long val, struct itbl_range *r)
static struct itbl_entryalloc_entry (e_processor processor, e_type type, char *name, unsigned long value)
static unsigned long apply_range (unsigned long value, struct itbl_range r)
static unsigned long extract_range (unsigned long value, struct itbl_range r)
static struct itbl_fieldalloc_field (e_type type, int sbit, int ebit, unsigned long flags)
int itbl_parse (char *insntbl)
struct itbl_entryitbl_add_reg (int yyprocessor, int yytype, char *regname, int regnum)
struct itbl_entryitbl_add_insn (int yyprocessor, char *name, unsigned long value, int sbit, int ebit, unsigned long flags)
struct itbl_fielditbl_add_operand (struct itbl_entry *e, int yytype, int sbit, int ebit, unsigned long flags)
static void append_insns_as_macros (void)
void itbl_init (void)
static char * form_args (struct itbl_entry *e)
int itbl_get_reg_val (char *name, unsigned long *pval)
char * itbl_get_name (e_processor processor, e_type type, unsigned long val)
int itbl_get_val (e_processor processor, e_type type, char *name, unsigned long *pval)
unsigned long itbl_assemble (char *name, char *s)
int itbl_disassemble (char *s, unsigned long insn)
char * itbl_get_field (char **S)

Variables

int itbl_have_entries = 0
static int itbl_num_opcodes = 0
static struct itbl_entryentries [e_nprocs][e_ntypes]

Class Documentation

struct itbl_range

Definition at line 116 of file itbl-ops.c.

Class Members
int ebit
int sbit
struct itbl_field

Definition at line 121 of file itbl-ops.c.

Collaboration diagram for itbl_field:
Class Members
unsigned long flags
struct itbl_field * next
e_type type
struct itbl_entry

Definition at line 133 of file itbl-ops.c.

Collaboration diagram for itbl_entry:
Class Members
struct itbl_field * fields
unsigned long flags
char * name
struct itbl_entry * next
e_processor processor
e_type type
unsigned long value

Define Documentation

#define ASSERT (   x)

Definition at line 103 of file itbl-ops.c.

#define DBG (   x)

Definition at line 104 of file itbl-ops.c.

#define min (   a,
  b 
)    (a<b?a:b)

Definition at line 108 of file itbl-ops.c.


Function Documentation

static struct itbl_entry * alloc_entry ( e_processor  processor,
e_type  type,
char *  name,
unsigned long  value 
) [static, read]

Definition at line 841 of file itbl-ops.c.

{
  struct itbl_entry *e, **es;
  if (!name)
    return 0;
  e = (struct itbl_entry *) malloc (sizeof (struct itbl_entry));
  if (e)
    {
      memset (e, 0, sizeof (struct itbl_entry));
      e->name = (char *) malloc (sizeof (strlen (name)) + 1);
      if (e->name)
       strcpy (e->name, name);
      e->processor = processor;
      e->type = type;
      e->value = value;
      es = get_entries (e->processor, e->type);
      e->next = *es;
      *es = e;
    }
  return e;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct itbl_field * alloc_field ( e_type  type,
int  sbit,
int  ebit,
unsigned long  flags 
) [static, read]

Definition at line 867 of file itbl-ops.c.

{
  struct itbl_field *f;
  f = (struct itbl_field *) malloc (sizeof (struct itbl_field));
  if (f)
    {
      memset (f, 0, sizeof (struct itbl_field));
      f->type = type;
      f->range.sbit = sbit;
      f->range.ebit = ebit;
      f->flags = flags;
    }
  return f;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void append_insns_as_macros ( void  ) [static]

Definition at line 299 of file itbl-ops.c.

{
  struct ITBL_OPCODE_STRUCT *new_opcodes, *o;
  struct itbl_entry *e, **es;
  int n, id, size, new_size, new_num_opcodes;

  if (!itbl_have_entries)
    return;

  if (!itbl_num_opcodes)    /* no new instructions to add! */
    {
      return;
    }
  DBG (("previous num_opcodes=%d\n", ITBL_NUM_OPCODES));

  new_num_opcodes = ITBL_NUM_OPCODES + itbl_num_opcodes;
  ASSERT (new_num_opcodes >= itbl_num_opcodes);

  size = sizeof (struct ITBL_OPCODE_STRUCT) * ITBL_NUM_OPCODES;
  ASSERT (size >= 0);
  DBG (("I get=%d\n", size / sizeof (ITBL_OPCODES[0])));

  new_size = sizeof (struct ITBL_OPCODE_STRUCT) * new_num_opcodes;
  ASSERT (new_size > size);

  /* FIXME since ITBL_OPCODES culd be a static table,
              we can't realloc or delete the old memory.  */
  new_opcodes = (struct ITBL_OPCODE_STRUCT *) malloc (new_size);
  if (!new_opcodes)
    {
      printf (_("Unable to allocate memory for new instructions\n"));
      return;
    }
  if (size)                 /* copy preexisting opcodes table */
    memcpy (new_opcodes, ITBL_OPCODES, size);

  /* FIXME! some NUMOPCODES are calculated expressions.
              These need to be changed before itbls can be supported.  */

  id = ITBL_NUM_MACROS;            /* begin the next macro id after the last */
  o = &new_opcodes[ITBL_NUM_OPCODES];     /* append macro to opcodes list */
  for (n = e_p0; n < e_nprocs; n++)
    {
      es = get_entries (n, e_insn);
      for (e = *es; e; e = e->next)
       {
         /* name,    args,   mask,       match,  pinfo
               * {"li",      "t,i",  0x34000000, 0xffe00000, WR_t    },
               * {"li",      "t,I",  0,    (int) M_LI,   INSN_MACRO  },
               * Construct args from itbl_fields.
              */
         o->name = e->name;
         o->args = strdup (form_args (e));
         o->mask = apply_range (e->value, e->range);
         /* FIXME how to catch during assembly? */
         /* mask to identify this insn */
         o->match = apply_range (e->value, e->range);
         o->pinfo = 0;

#ifdef USE_MACROS
         o->mask = id++;    /* FIXME how to catch during assembly? */
         o->match = 0;             /* for macros, the insn_isa number */
         o->pinfo = INSN_MACRO;
#endif

         /* Don't add instructions which caused an error */
         if (o->args)
           o++;
         else
           new_num_opcodes--;
       }
    }
  ITBL_OPCODES = new_opcodes;
  ITBL_NUM_OPCODES = new_num_opcodes;

  /* FIXME
              At this point, we can free the entries, as they should have
              been added to the assembler's tables.
              Don't free name though, since name is being used by the new
              opcodes table.

              Eventually, we should also free the new opcodes table itself
              on exit.
       */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long apply_range ( unsigned long  value,
struct itbl_range  r 
) [static]

Definition at line 661 of file itbl-ops.c.

{
  unsigned long mask;
  unsigned long aval;
  int len = MAX_BITPOS - r.sbit;

  ASSERT (r.sbit >= r.ebit);
  ASSERT (MAX_BITPOS >= r.sbit);
  ASSERT (r.ebit >= 0);

  /* create mask by truncating 1s by shifting */
  mask = 0xffffffff << len;
  mask = mask >> len;
  mask = mask >> r.ebit;
  mask = mask << r.ebit;

  aval = (rval << r.ebit) & mask;
  return aval;
}

Here is the caller graph for this function:

static unsigned long build_opcode ( struct itbl_entry e) [static]

Definition at line 640 of file itbl-ops.c.

{
  unsigned long opcode;

  opcode = apply_range (e->value, e->range);
  opcode |= ITBL_ENCODE_PNUM (e->processor);
  return opcode;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long extract_range ( unsigned long  value,
struct itbl_range  r 
) [static]

Definition at line 685 of file itbl-ops.c.

{
  unsigned long mask;
  unsigned long rval;
  int len = MAX_BITPOS - r.sbit;

  /* create mask by truncating 1s by shifting */
  mask = 0xffffffff << len;
  mask = mask >> len;
  mask = mask >> r.ebit;
  mask = mask << r.ebit;

  rval = (aval & mask) >> r.ebit;
  return rval;
}

Here is the caller graph for this function:

static struct itbl_entry * find_entry_byname ( e_processor  processor,
e_type  type,
char *  name 
) [static, read]

Definition at line 735 of file itbl-ops.c.

{
  struct itbl_entry *e, **es;

  es = get_entries (processor, type);
  for (e = *es; e; e = e->next)    /* for each entry, ...  */
    {
      if (!strcmp (e->name, n))
       return e;
    }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct itbl_entry * find_entry_byval ( e_processor  processor,
e_type  type,
unsigned long  val,
struct itbl_range r 
) [static, read]

Definition at line 756 of file itbl-ops.c.

{
  struct itbl_entry *e, **es;
  unsigned long eval;

  es = get_entries (processor, type);
  for (e = *es; e; e = e->next)    /* for each entry, ...  */
    {
      if (processor != e->processor)
       continue;
      /* For insns, we might not know the range of the opcode,
        * so a range of 0 will allow this routine to match against
        * the range of the entry to be compared with.
        * This could cause ambiguities.
        * For operands, we get an extracted value and a range.
        */
      /* if range is 0, mask val against the range of the compared entry.  */
      if (r == 0)           /* if no range passed, must be whole 32-bits
                      * so create 32-bit value from entry's range */
       {
         eval = apply_range (e->value, e->range);
         val &= apply_range (0xffffffff, e->range);
       }
      else if ((r->sbit == e->range.sbit && r->ebit == e->range.ebit)
              || (e->range.sbit == 0 && e->range.ebit == 0))
       {
         eval = apply_range (e->value, *r);
         val = apply_range (val, *r);
       }
      else
       continue;
      if (val == eval)
       return e;
    }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char * form_args ( struct itbl_entry e) [static]

Definition at line 386 of file itbl-ops.c.

{
  static char s[31];
  char c = 0, *p = s;
  struct itbl_field *f;

  ASSERT (e);
  for (f = e->fields; f; f = f->next)
    {
      switch (f->type)
       {
       case e_dreg:
         c = 'd';
         break;
       case e_creg:
         c = 't';
         break;
       case e_greg:
         c = 's';
         break;
       case e_immed:
         c = 'i';
         break;
       case e_addr:
         c = 'a';
         break;
       default:
         c = 0;             /* ignore; unknown field type */
       }
      if (c)
       {
         if (p != s)
           *p++ = ',';
         *p++ = c;
       }
    }
  *p = 0;
  return s;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct itbl_entry ** get_entries ( e_processor  processor,
e_type  type 
) [static, read]

Definition at line 797 of file itbl-ops.c.

{
  return &entries[processor][type];
}

Here is the caller graph for this function:

static e_processor get_processor ( int  yyproc) [static]

Definition at line 805 of file itbl-ops.c.

{
  /* translate from yacc's processor to enum */
  if (yyproc >= e_p0 && yyproc < e_nprocs)
    return (e_processor) yyproc;
  return e_invproc;         /* error; invalid processor */
}

Here is the caller graph for this function:

static e_type get_type ( int  yytype) [static]

Definition at line 816 of file itbl-ops.c.

{
  switch (yytype)
    {
      /* translate from yacc's type to enum */
    case INSN:
      return e_insn;
    case DREG:
      return e_dreg;
    case CREG:
      return e_creg;
    case GREG:
      return e_greg;
    case ADDR:
      return e_addr;
    case IMMED:
      return e_immed;
    default:
      return e_invtype;            /* error; invalid type */
    }
}

Here is the caller graph for this function:

struct itbl_entry* itbl_add_insn ( int  yyprocessor,
char *  name,
unsigned long  value,
int  sbit,
int  ebit,
unsigned long  flags 
) [read]

Definition at line 208 of file itbl-ops.c.

{
  struct itbl_entry *e;
  e = alloc_entry (get_processor (yyprocessor), e_insn, name, value);
  if (e)
    {
      e->range.sbit = sbit;
      e->range.ebit = ebit;
      e->flags = flags;
      itbl_num_opcodes++;
    }
  return e;
}

Here is the call graph for this function:

struct itbl_field* itbl_add_operand ( struct itbl_entry e,
int  yytype,
int  sbit,
int  ebit,
unsigned long  flags 
) [read]

Definition at line 226 of file itbl-ops.c.

{
  struct itbl_field *f, **last_f;
  if (!e)
    return 0;
  /* Add to end of fields' list.  */
  f = alloc_field (get_type (yytype), sbit, ebit, flags);
  if (f)
    {
      last_f = &e->fields;
      while (*last_f)
       last_f = &(*last_f)->next;
      *last_f = f;
      f->next = 0;
    }
  return f;
}

Here is the call graph for this function:

struct itbl_entry* itbl_add_reg ( int  yyprocessor,
int  yytype,
char *  regname,
int  regnum 
) [read]

Definition at line 198 of file itbl-ops.c.

{
  return alloc_entry (get_processor (yyprocessor), get_type (yytype), regname,
                    (unsigned long) regnum);
}

Here is the call graph for this function:

unsigned long itbl_assemble ( char *  name,
char *  s 
)

Definition at line 479 of file itbl-ops.c.

{
  unsigned long opcode;
  struct itbl_entry *e = NULL;
  struct itbl_field *f;
  char *n;
  int processor;

  if (!name || !*name)
    return 0;               /* error!  must have an opcode name/expr */

  /* find entry in list of instructions for all processors */
  for (processor = 0; processor < e_nprocs; processor++)
    {
      e = find_entry_byname (processor, e_insn, name);
      if (e)
       break;
    }
  if (!e)
    return 0;               /* opcode not in table; invalid instruction */
  opcode = build_opcode (e);

  /* parse opcode's args (if any) */
  for (f = e->fields; f; f = f->next)     /* for each arg, ...  */
    {
      struct itbl_entry *r;
      unsigned long value;
      if (!s || !*s)
       return 0;            /* error - not enough operands */
      n = itbl_get_field (&s);
      /* n should be in form $n or 0xhhh (are symbol names valid?? */
      switch (f->type)
       {
       case e_dreg:
       case e_creg:
       case e_greg:
         /* Accept either a string name
                      * or '$' followed by the register number */
         if (*n == '$')
           {
             n++;
             value = strtol (n, 0, 10);
             /* FIXME! could have "0l"... then what?? */
             if (value == 0 && *n != '0')
              return 0;     /* error; invalid operand */
           }
         else
           {
             r = find_entry_byname (e->processor, f->type, n);
             if (r)
              value = r->value;
             else
              return 0;     /* error; invalid operand */
           }
         break;
       case e_addr:
         /* use assembler's symbol table to find symbol */
         /* FIXME!! Do we need this?
                            if so, what about relocs??
                            my_getExpression (&imm_expr, s);
                            return 0;     /-* error; invalid operand *-/
                            break;
                     */
         /* If not a symbol, fall thru to IMMED */
       case e_immed:
         if (*n == '0' && *(n + 1) == 'x')       /* hex begins 0x...  */
           {
             n += 2;
             value = strtol (n, 0, 16);
             /* FIXME! could have "0xl"... then what?? */
           }
         else
           {
             value = strtol (n, 0, 10);
             /* FIXME! could have "0l"... then what?? */
             if (value == 0 && *n != '0')
              return 0;     /* error; invalid operand */
           }
         break;
       default:
         return 0;          /* error; invalid field spec */
       }
      opcode |= apply_range (value, f->range);
    }
  if (s && *s)
    return 0;               /* error - too many operands */
  return opcode;            /* done! */
}

Here is the call graph for this function:

Here is the caller graph for this function:

int itbl_disassemble ( char *  s,
unsigned long  insn 
)

Definition at line 575 of file itbl-ops.c.

{
  e_processor processor;
  struct itbl_entry *e;
  struct itbl_field *f;

  if (!ITBL_IS_INSN (insn))
    return 0;               /* error */
  processor = get_processor (ITBL_DECODE_PNUM (insn));

  /* find entry in list */
  e = find_entry_byval (processor, e_insn, insn, 0);
  if (!e)
    return 0;               /* opcode not in table; invalid instruction */
  strcpy (s, e->name);

  /* Parse insn's args (if any).  */
  for (f = e->fields; f; f = f->next)     /* for each arg, ...  */
    {
      struct itbl_entry *r;
      unsigned long value;

      if (f == e->fields)   /* First operand is preceded by tab.  */
       strcat (s, "\t");
      else                  /* ','s separate following operands.  */
       strcat (s, ",");
      value = extract_range (insn, f->range);
      /* n should be in form $n or 0xhhh (are symbol names valid?? */
      switch (f->type)
       {
       case e_dreg:
       case e_creg:
       case e_greg:
         /* Accept either a string name
            or '$' followed by the register number.  */
         r = find_entry_byval (e->processor, f->type, value, &f->range);
         if (r)
           strcat (s, r->name);
         else
           sprintf (s, "%s$%lu", s, value);
         break;
       case e_addr:
         /* Use assembler's symbol table to find symbol.  */
         /* FIXME!! Do we need this?  If so, what about relocs??  */
         /* If not a symbol, fall through to IMMED.  */
       case e_immed:
         sprintf (s, "%s0x%lx", s, value);
         break;
       default:
         return 0;          /* error; invalid field spec */
       }
    }
  return 1;                 /* Done!  */
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* itbl_get_field ( char **  S)

Definition at line 707 of file itbl-ops.c.

{
  static char n[128];
  char *s;
  int len;

  s = *S;
  if (!s || !*s)
    return 0;
  /* FIXME: This is a weird set of delimiters.  */
  len = strcspn (s, " \t,()");
  ASSERT (128 > len + 1);
  strncpy (n, s, len);
  n[len] = 0;
  if (s[len] == '\0')
    s = 0;                  /* no more args */
  else
    s += len + 1;           /* advance to next arg */

  *S = s;
  return n;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* itbl_get_name ( e_processor  processor,
e_type  type,
unsigned long  val 
)

Definition at line 447 of file itbl-ops.c.

{
  struct itbl_entry *r;
  /* type depends on instruction passed */
  r = find_entry_byval (processor, type, val, 0);
  if (r)
    return r->name;
  else
    return 0;               /* error; invalid operand */
}

Here is the call graph for this function:

Here is the caller graph for this function:

int itbl_get_reg_val ( char *  name,
unsigned long pval 
)

Definition at line 430 of file itbl-ops.c.

{
  e_type t;
  e_processor p;

  for (p = e_p0; p < e_nprocs; p++)
    {
      for (t = e_regtype0; t < e_nregtypes; t++)
       {
         if (itbl_get_val (p, t, name, pval))
           return 1;
       }
    }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int itbl_get_val ( e_processor  processor,
e_type  type,
char *  name,
unsigned long pval 
)

Definition at line 461 of file itbl-ops.c.

{
  struct itbl_entry *r;
  /* type depends on instruction passed */
  r = find_entry_byname (processor, type, name);
  if (r == NULL)
    return 0;
  *pval = r->value;
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void itbl_init ( void  )

Definition at line 254 of file itbl-ops.c.

{
  struct itbl_entry *e, **es;
  e_processor procn;
  e_type type;

  if (!itbl_have_entries)
    return;

  /* Since register names don't have a prefix, put them in the symbol table so
     they can't be used as symbols.  This simplifies argument parsing as
     we can let gas parse registers for us.  */
  /* Use symbol_create instead of symbol_new so we don't try to
     output registers into the object file's symbol table.  */

  for (type = e_regtype0; type < e_nregtypes; type++)
    for (procn = e_p0; procn < e_nprocs; procn++)
      {
       es = get_entries (procn, type);
       for (e = *es; e; e = e->next)
         {
           symbol_table_insert (symbol_create (e->name, reg_section,
                                          e->value, &zero_address_frag));
         }
      }
  append_insns_as_macros ();
}

Here is the call graph for this function:

int itbl_parse ( char *  insntbl)

Definition at line 174 of file itbl-ops.c.

{
  extern FILE *yyin;
  extern int yyparse (void);

  yyin = fopen (insntbl, FOPEN_RT);
  if (yyin == 0)
    {
      printf ("Can't open processor instruction specification file \"%s\"\n",
             insntbl);
      return 1;
    }

  while (yyparse ())
    ;

  fclose (yyin);
  itbl_have_entries = 1;
  return 0;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 148 of file itbl-ops.c.

Definition at line 111 of file itbl-ops.c.

int itbl_num_opcodes = 0 [static]

Definition at line 146 of file itbl-ops.c.