diff options
Diffstat (limited to 'opcodes/arc-ext.c')
-rw-r--r-- | opcodes/arc-ext.c | 512 |
1 files changed, 327 insertions, 185 deletions
diff --git a/opcodes/arc-ext.c b/opcodes/arc-ext.c index d2d838e..2bb1bb1 100644 --- a/opcodes/arc-ext.c +++ b/opcodes/arc-ext.c @@ -21,16 +21,269 @@ #include <stdlib.h> #include <stdio.h> + #include "bfd.h" #include "arc-ext.h" #include "elf/arc.h" - #include "libiberty.h" #include "sysdep.h" -/* extension structure */ + +/******************************************************************************/ +/* */ +/* Outline: */ +/* This module provides support for extensions to the ARC processor */ +/* architecture. */ +/* */ +/******************************************************************************/ + + +/* -------------------------------------------------------------------------- */ +/* local constants */ +/* -------------------------------------------------------------------------- */ + +#define FIRST_EXTENSION_CORE_REGISTER 32 +#define LAST_EXTENSION_CORE_REGISTER 59 +#define FIRST_EXTENSION_CONDITION_CODE 0x10 +#define LAST_EXTENSION_CONDITION_CODE 0x1f + +#define NUM_EXT_CORE (LAST_EXTENSION_CORE_REGISTER - FIRST_EXTENSION_CORE_REGISTER + 1) +#define NUM_EXT_COND (LAST_EXTENSION_CONDITION_CODE - FIRST_EXTENSION_CONDITION_CODE + 1) +#define INST_HASH_BITS 6 +#define INST_HASH_SIZE (1 << INST_HASH_BITS) +#define INST_HASH_MASK (INST_HASH_SIZE - 1) + + +/* -------------------------------------------------------------------------- */ +/* local types */ +/* -------------------------------------------------------------------------- */ + +/* these types define the information stored in the table */ + +struct ExtInstruction +{ + char major; + char minor; + char flags; + char* name; + struct ExtInstruction* next; +}; + +struct ExtAuxRegister +{ + long address; + char* name; + struct ExtAuxRegister* next; +}; + +struct ExtCoreRegister +{ + short number; + enum ExtReadWrite rw; + char* name; +}; + +struct arcExtMap +{ + struct ExtAuxRegister* auxRegisters; + struct ExtInstruction* instructions[INST_HASH_SIZE]; + struct ExtCoreRegister coreRegisters[NUM_EXT_CORE]; + char* condCodes[NUM_EXT_COND]; +}; + + +/* -------------------------------------------------------------------------- */ +/* local data */ +/* -------------------------------------------------------------------------- */ + +/* extension table */ static struct arcExtMap arc_extension_map; + +/* -------------------------------------------------------------------------- */ +/* local macros */ +/* -------------------------------------------------------------------------- */ + +/* a hash function used to map instructions into the table */ +#define INST_HASH(MAJOR, MINOR) ((((MAJOR) << 3) ^ (MINOR)) & INST_HASH_MASK) + + +/* -------------------------------------------------------------------------- */ +/* local functions */ +/* -------------------------------------------------------------------------- */ + +static void create_map(unsigned char *block, unsigned long length) +{ + unsigned char *p = block; + +//printf("building ext map...\n"); + + while (p && p < (block + length)) + { + /* p[0] == length of record + p[1] == type of record + For instructions: + p[2] = opcode + p[3] = minor opcode (if opcode == 3) + p[4] = flags + p[5]+ = name + For core regs and condition codes: + p[2] = value + p[3]+ = name + For auxiliary regs: + p[2..5] = value + p[6]+ = name + (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5]) */ + + /* the sequence of records is temrinated by an "empty" record */ + if (p[0] == 0) + break; + +// printf("%d byte type %d record\n", p[0], p[1]); + + switch (p[1]) + { /* type */ + case EXT_INSTRUCTION: + { + struct ExtInstruction *insn = XNEW (struct ExtInstruction); + int major = p[2]; + int minor = p[3]; + struct ExtInstruction **bucket = + &arc_extension_map.instructions[INST_HASH (major, minor)]; + + insn->name = xstrdup ((char *) (p+5)); + insn->major = major; + insn->minor = minor; + insn->flags = p[4]; + insn->next = *bucket; + *bucket = insn; + break; + } + + case EXT_CORE_REGISTER: + { + unsigned char number = p[2]; + char* name = (char *) p+3; + + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].number = number; + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].rw = REG_READWRITE; + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].name = xstrdup (name); + break; + } + + case EXT_LONG_CORE_REGISTER: + { + unsigned char number = p[2]; + char* name = (char *) p+7; + enum ExtReadWrite rw = p[6]; + + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].number = number; + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].rw = rw; + arc_extension_map.coreRegisters[number - FIRST_EXTENSION_CORE_REGISTER].name = xstrdup (name); + } + + case EXT_COND_CODE: + { + char *cc_name = xstrdup ((char *) (p+3)); + + arc_extension_map.condCodes[p[2] - FIRST_EXTENSION_CONDITION_CODE] = cc_name; + break; + } + + case EXT_AUX_REGISTER: + { + /* trickier -- need to store linked list of these */ + struct ExtAuxRegister *newAuxRegister = XNEW (struct ExtAuxRegister); + char *aux_name = xstrdup ((char *) (p+6)); + + newAuxRegister->name = aux_name; + newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5]; + newAuxRegister->next = arc_extension_map.auxRegisters; + arc_extension_map.auxRegisters = newAuxRegister; + break; + } + + default: +// printf("type %d extension record skipped\n", p[1]); + break; + } + + p += p[0]; /* move on to next record */ + } + +//printf("ext map built\n"); +} + + +/* Free memory that has been allocated for the extensions. */ +static void destroy_map(void) +{ + struct ExtAuxRegister *r; + unsigned int i; + + /* free auxiliary registers */ + r = arc_extension_map.auxRegisters; + while (r) + { + /* N.B. after r has been freed, r->next is invalid! */ + struct ExtAuxRegister* next = r->next; + + free (r->name); + free (r); + r = next; + } + + /* free instructions */ + for (i = 0; i < INST_HASH_SIZE; i++) + { + struct ExtInstruction *insn = arc_extension_map.instructions[i]; + + while (insn) + { + /* N.B. after insn has been freed, insn->next is invalid! */ + struct ExtInstruction *next = insn->next; + + free (insn->name); + free (insn); + insn = next; + } + } + + /* free core registers */ + for (i = 0; i < NUM_EXT_CORE; i++) + { + if (arc_extension_map.coreRegisters[i].name) + free (arc_extension_map.coreRegisters[i].name); + } + + /* free condition codes */ + for (i = 0; i < NUM_EXT_COND; i++) + { + if (arc_extension_map.condCodes[i]) + free (arc_extension_map.condCodes[i]); + } + + memset (&arc_extension_map, 0, sizeof (arc_extension_map)); +} + + +static const char* ExtReadWrite_image(enum ExtReadWrite val) +{ + switch (val) + { + case REG_INVALID : return "INVALID"; + case REG_READ : return "RO"; + case REG_WRITE : return "WO"; + case REG_READWRITE: return "R/W"; + default : return "???"; + } +} + + +/* -------------------------------------------------------------------------- */ +/* externally visible functions */ +/* -------------------------------------------------------------------------- */ + /* Get the name of an extension instruction. */ const char * @@ -119,237 +372,126 @@ arcExtMap_instName (int opcode, int insn, int *flags) while (temp) { if ((temp->major == opcode) && (temp->minor == minor)) - { - *flags = temp->flags; - return temp->name; - } + { + *flags = temp->flags; + return temp->name; + } temp = temp->next; } return NULL; } + /* get the name of an extension core register */ const char * -arcExtMap_coreRegName (int value) +arcExtMap_coreRegName (int regnum) { - if (value < 32) - return 0; - return arc_extension_map.coreRegisters[value-32].name; + if (regnum < FIRST_EXTENSION_CORE_REGISTER || regnum > LAST_EXTENSION_CONDITION_CODE) + return NULL; + return arc_extension_map.coreRegisters[regnum - FIRST_EXTENSION_CORE_REGISTER].name; } + +/* get the access mode of an extension core register */ enum ExtReadWrite -arcExtMap_coreReadWrite (int value) +arcExtMap_coreReadWrite (int regnum) { - if (value < 32) + if (regnum < FIRST_EXTENSION_CORE_REGISTER || regnum > LAST_EXTENSION_CONDITION_CODE) return REG_INVALID; - return arc_extension_map.coreRegisters[value-32].rw; + return arc_extension_map.coreRegisters[regnum - FIRST_EXTENSION_CORE_REGISTER].rw; } -#if 0 -struct ExtAuxRegister * -arc_ExtMap_auxRegs () -{ - return arc_extension_map.auxRegisters; -} -#endif -/* Get the name of an extension condition code. */ +/* get the name of an extension condition code */ const char * -arcExtMap_condCodeName (int value) +arcExtMap_condCodeName (int code) { - if (value < 16) - return 0; - return arc_extension_map.condCodes[value-16]; + if (code < FIRST_EXTENSION_CONDITION_CODE || code > LAST_EXTENSION_CONDITION_CODE) + return NULL; + return arc_extension_map.condCodes[code - FIRST_EXTENSION_CONDITION_CODE]; } -/* Get the name of an extension aux register. */ +/* Get the name of an extension auxiliary register. */ const char * arcExtMap_auxRegName (long address) { - /* Walk the list of aux reg names and find the name. */ + /* Walk the list of auxiliary register names and find the name. */ struct ExtAuxRegister *r; for (r = arc_extension_map.auxRegisters; r; r = r->next) { if (r->address == address) - return (const char *)r->name; + return (const char *)r->name; } - return 0; + return NULL; } -#if 0 -/* Recursively free auxilliary register strcture pointers until - the list is empty. */ -static void -clean_aux_registers (struct ExtAuxRegister *r) + +/* Load extensions described in .arcextmap and .gnu.linkonce.arcextmap.* ELF + section. */ +void +build_ARC_extmap (bfd *text_bfd) { - if (r -> next) - { - clean_aux_registers (r->next); - free (r->name); - free (r->next); - r->next = NULL; - } - else - free (r->name); + asection *sect; + + /* the map is built each time gdb loads an executable file - so free any + * existing map, as the map defined by the new file may differ from the old + */ + destroy_map(); + + for (sect = text_bfd->sections; sect != NULL; sect = sect->next) + if (!strncmp (sect->name, + ".gnu.linkonce.arcextmap.", + sizeof (".gnu.linkonce.arcextmap.") - 1) + || !strcmp (sect->name,".arcextmap")) + { + bfd_size_type count = bfd_get_section_size (sect); + unsigned char* buffer = xmalloc (count); + + if (buffer) + { + if (bfd_get_section_contents (text_bfd, sect, buffer, 0, count)) + create_map(buffer, count); + free (buffer); + } + } } -/* Free memory that has been allocated for the extensions. */ -static void -cleanup_ext_map (void) +void dump_ARC_extmap (void) { - struct ExtAuxRegister *r; - struct ExtInstruction *insn; - int i; + struct ExtAuxRegister* r; + int i; - /* Clean aux reg structure. */ - r = arc_extension_map.auxRegisters; - if (r) - { - (clean_aux_registers (r)); - free (r); - } + r = arc_extension_map.auxRegisters; - /* Clean instructions. */ - for (i = INST_HASH_SIZE - 1; i >= 0; i--) + while (r) { - for (insn = arc_extension_map.instructions[i]; insn ; insn = insn->next) - { - free (insn->name); - free (insn); - } + printf("AUX : %s %ld\n", r->name, r->address); + r = r->next; } - /* Clean core reg struct. */ - for (i = 0; i < NUM_EXT_CORE; i++) + for (i = 0; i < INST_HASH_SIZE; i++) { - if (arc_extension_map.coreRegisters[i].name) - free (arc_extension_map.coreRegisters[i].name); - } + struct ExtInstruction *insn; - for (i = 0; i < NUM_EXT_COND; i++) { - if (arc_extension_map.condCodes[i]) - free (arc_extension_map.condCodes[i]); - } - - memset (&arc_extension_map, 0, sizeof (struct arcExtMap)); -} -#endif - -int -arcExtMap_add (void *base, unsigned long length) -{ - unsigned char *block = base; - unsigned char *p = block; + for (insn = arc_extension_map.instructions[i]; insn != NULL; insn = insn->next) + printf("INST: %d %d %x %s\n", insn->major, insn->minor, insn->flags, insn->name); + } - while (p && p < (block + length)) + for (i = 0; i < NUM_EXT_CORE; i++) { - /* p[0] == length of record - p[1] == type of record - For instructions: - p[2] = opcode - p[3] = minor opcode (if opcode == 3) - p[4] = flags - p[5]+ = name - For core regs and condition codes: - p[2] = value - p[3]+ = name - For aux regs: - p[2..5] = value - p[6]+ = name - (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5]) */ - if (p[0] == 0) - return -1; - - switch (p[1]) - { /* type */ - case EXT_INSTRUCTION: - { - char *insn_name = xstrdup ((char *) (p+5)); - struct ExtInstruction *insn = XNEW (struct ExtInstruction); - int major = p[2]; - int minor = p[3]; - struct ExtInstruction **bucket - = &arc_extension_map.instructions[INST_HASH (major, minor)]; - - insn->name = insn_name; - insn->major = major; - insn->minor = minor; - insn->flags = p[4]; - insn->next = *bucket; - *bucket = insn; - break; - } - case EXT_CORE_REGISTER: - { - unsigned char number = p[2]; - char *name = (char *) p+3; - - arc_extension_map.coreRegisters[number-32].number = number; - arc_extension_map.coreRegisters[number-32].rw = REG_READWRITE; - arc_extension_map.coreRegisters[number-32].name = xstrdup (name); - break; - } - case EXT_LONG_CORE_REGISTER: - { - unsigned char number = p[2]; - char *name = (char *) p+7; - enum ExtReadWrite rw = p[6]; - - arc_extension_map.coreRegisters[number-32].number = number; - arc_extension_map.coreRegisters[number-32].rw = rw; - arc_extension_map.coreRegisters[number-32].name = xstrdup (name); - } - case EXT_COND_CODE: - { - char *cc_name = xstrdup ((char *) (p+3)); - - arc_extension_map.condCodes[p[2]-16] = cc_name; - break; - } - case EXT_AUX_REGISTER: - { - /* trickier -- need to store linked list to these */ - struct ExtAuxRegister *newAuxRegister - = XNEW (struct ExtAuxRegister); - char *aux_name = xstrdup ((char *) (p+6)); + struct ExtCoreRegister reg = arc_extension_map.coreRegisters[i]; - newAuxRegister->name = aux_name; - newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5]; - newAuxRegister->next = arc_extension_map.auxRegisters; - arc_extension_map.auxRegisters = newAuxRegister; - break; - } - default: - return -1; - } - p += p[0]; /* move to next record */ + if (reg.name) + printf("CORE: %s %d %s\n", reg.name, reg.number, ExtReadWrite_image(reg.rw)); } - return 0; -} -/* Load extensions described in .arcextmap and .gnu.linkonce.arcextmap.* ELF - section. */ -void -build_ARC_extmap (bfd *text_bfd) -{ - char *arcExtMap; - bfd_size_type count; - asection *p; - - for (p = text_bfd->sections; p != NULL; p = p->next) - if (!strncmp (p->name, - ".gnu.linkonce.arcextmap.", - sizeof (".gnu.linkonce.arcextmap.")-1) - || !strcmp (p->name,".arcextmap")) - { - count = bfd_get_section_size (p); - arcExtMap = (char *) xmalloc (count); - if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count)) - arcExtMap_add ((PTR) arcExtMap, count); - free ((PTR) arcExtMap); - } + for (i = 0; i < NUM_EXT_COND; i++) + if (arc_extension_map.condCodes[i]) + printf("COND: %s\n", arc_extension_map.condCodes[i]); } + +/******************************************************************************/ |