ROSE  0.9.9.149
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
RegisterDictionary Class Reference

Description

Defines registers available for a particular architecture.

The dictionary maps each register name to a RegisterDescriptor. The RegisterDescriptor describes how a register name maps to (part of) a physical CPU register by providing a major number, minor number, bit offset, and number of bits. The major number is typically a register class number and the minor number is typically an offset within the class. For instance, for x86 the major numbers might indicate whether a register is a general purpose register, an 80-bit floating point register, a 128-bit MMX register, etc.

Users should not assume that the values of a RegisterDescriptor correspond to actual values found in the machine instructions that were disassembled. What they can assume is that two unrelated registers (such as "eax" and "ebx") do not overlap in the RegisterDescriptor address space (major, minor, offset, size). They can also assume that two related registers (such as "eax" and "rax", the 32- and 64-bit versions of a single CPU register) do, in fact, overlap in the RegisterDescriptor address space and that the overlap indicates how the registers are related.

Users should not assume that RegisterDescriptor entries from two separate dictionaries are compatible, although the dictionaries created by the ROSE library do use compatible descriptors across each family. Looking up the "eax" register in one dictionary may return a different descriptor than "eax" looked up in a different dictionary (but not in ROSE-created dictinaries). Components of the ROSE binary support that generate RegisterDescriptors provide a mechanism for obtaining (and possibly setting) the register dictionary. For instance, the Disassembler class has get_registers() and set_registers() methods.

Definition at line 32 of file Registers.h.

#include <Registers.h>

Classes

class  SortBySize
 Compares number of bits in register descriptors. More...
 

Public Types

typedef std::map< std::string, RegisterDescriptorEntries
 
typedef std::vector< RegisterDescriptorRegisterDescriptors
 

Public Member Functions

 RegisterDictionary (const std::string &name)
 
 RegisterDictionary (const RegisterDictionary &other)
 
const std::string & get_architecture_name () const
 Obtain the name of the dictionary. More...
 
void set_architecture_name (const std::string &name)
 Set the name of the dictionary. More...
 
void insert (const std::string &name, RegisterDescriptor)
 Insert a definition into the dictionary. More...
 
void insert (const std::string &name, unsigned majr, unsigned minr, unsigned offset, unsigned nbits)
 Insert a definition into the dictionary. More...
 
void insert (const RegisterDictionary *)
 Inserts definitions from another dictionary into this dictionary. More...
 
void insert (const RegisterDictionary &)
 Inserts definitions from another dictionary into this dictionary. More...
 
void resize (const std::string &name, unsigned new_nbits)
 Changes the size of a register. More...
 
const RegisterDescriptorlookup (const std::string &name) const
 Returns a descriptor for a given register name. More...
 
const std::string & lookup (RegisterDescriptor) const
 Returns a register name for a given descriptor. More...
 
const RegisterDescriptorexists (RegisterDescriptor) const
 Determine if a register descriptor exists. More...
 
RegisterDescriptor findLargestRegister (unsigned major, unsigned minor, size_t maxWidth=0) const
 Finds the first largest register with specified major and minor number. More...
 
Rose::BinaryAnalysis::RegisterParts getAllParts () const
 Returns all register parts. More...
 
RegisterDescriptors get_descriptors () const
 Returns the list of all register descriptors. More...
 
unsigned firstUnusedMajor () const
 Returns the first unused major register number. More...
 
unsigned firstUnusedMinor (unsigned majr) const
 Returns the first unused minor register number. More...
 
RegisterDescriptors get_largest_registers () const
 Returns a list of the largest non-overlapping registers. More...
 
RegisterDescriptors get_smallest_registers () const
 Returns a list of the smallest non-overlapping registers. More...
 
void print (std::ostream &) const
 Prints the contents of this register dictionary. More...
 
size_t size () const
 Return the number of entries in the dictionary. More...
 
const Entries & get_registers () const
 Returns the list of all register definitions in the dictionary.
 
Entries & get_registers ()
 Returns the list of all register definitions in the dictionary.
 

Static Public Member Functions

static const RegisterDictionarydictionary_i8086 ()
 Intel 8086 registers. More...
 
static const RegisterDictionarydictionary_i8088 ()
 Intel 8088 registers. More...
 
static const RegisterDictionarydictionary_i286 ()
 Intel 80286 registers. More...
 
static const RegisterDictionarydictionary_i386 ()
 Intel 80386 registers. More...
 
static const RegisterDictionarydictionary_i386_387 ()
 Intel 80386 with 80387 math co-processor. More...
 
static const RegisterDictionarydictionary_i486 ()
 Intel 80486 registers. More...
 
static const RegisterDictionarydictionary_pentium ()
 Intel Pentium registers. More...
 
static const RegisterDictionarydictionary_pentiumiii ()
 Intel Pentium III registers. More...
 
static const RegisterDictionarydictionary_pentium4 ()
 Intel Pentium 4 registers. More...
 
static const RegisterDictionarydictionary_amd64 ()
 Amd64 registers. More...
 
static const RegisterDictionarydictionary_arm7 ()
 ARM7 registers. More...
 
static const RegisterDictionarydictionary_powerpc ()
 PowerPC registers. More...
 
static const RegisterDictionarydictionary_mips32 ()
 MIPS32 Release 1. More...
 
static const RegisterDictionarydictionary_mips32_altnames ()
 MIPS32 Release 1 with special registers. More...
 
static const RegisterDictionarydictionary_m68000 ()
 Motorola M68330 register names.
 
static const RegisterDictionarydictionary_m68000_altnames ()
 Motorola M68330 alternate registers. More...
 
static const RegisterDictionarydictionary_coldfire ()
 FreeScale ColdFire generic hardware registers. More...
 
static const RegisterDictionarydictionary_coldfire_emac ()
 Registers for FreeScale ColdFire CPUs with EMAC (extended multiply-accumulate) unit. More...
 
template<class Compare >
static RegisterDescriptors filter_nonoverlapping (RegisterDescriptors reglist, Compare order=SortBySize(), bool reconsider_parts=true)
 Returns the list of non-overlapping registers or register parts. More...
 
static const RegisterDictionarydictionary_for_isa (SgAsmExecutableFileFormat::InsSetArchitecture)
 Class method to choose an appropriate register dictionary for an instruction set architecture. More...
 
static const RegisterDictionarydictionary_for_isa (SgAsmInterpretation *)
 Class method to choose an appropriate register dictionary for an instruction set architecture. More...
 

Friends

std::ostream & operator<< (std::ostream &, const RegisterDictionary &)
 

Member Function Documentation

static const RegisterDictionary* RegisterDictionary::dictionary_i8086 ( )
static

Intel 8086 registers.

The Intel 8086 has fourteen 16-bit registers. Four of them (AX, BX, CX, DX) are general registers (although each may have an additional purpose; for example only CX can be used as a counter with the loop instruction). Each can be accessed as two separate bytes (thus BX's high byte can be accessed as BH and low byte as BL). Four segment registers (CS, DS, SS and ES) are used to form a memory address. There are two pointer registers. SP points to the bottom of the stack and BP which is used to point at some other place in the stack or the memory(Offset). Two registers (SI and DI) are for array indexing. The FLAGS register contains flags such as carry flag, overflow flag and zero flag. Finally, the instruction pointer (IP) points to the next instruction that will be fetched from memory and then executed.

static const RegisterDictionary* RegisterDictionary::dictionary_i8088 ( )
static

Intel 8088 registers.

Intel 8088 has the same set of registers as Intel 8086.

static const RegisterDictionary* RegisterDictionary::dictionary_i286 ( )
static

Intel 80286 registers.

The 80286 has the same registers as the 8086 but adds two new flags to the "flags" register.

static const RegisterDictionary* RegisterDictionary::dictionary_i386 ( )
static

Intel 80386 registers.

The 80386 has the same registers as the 80286 but extends the general-purpose registers, base registers, index registers, instruction pointer, and flags register to 32 bits. Register names from the 80286 refer to the same offsets and sizes while the full 32 bits are accessed by names prefixed with "e" as in "eax" (the "e" means "extended"). Two new segment registers (FS and GS) were added and all segment registers remain 16 bits.

static const RegisterDictionary* RegisterDictionary::dictionary_i386_387 ( )
static

Intel 80386 with 80387 math co-processor.

static const RegisterDictionary* RegisterDictionary::dictionary_i486 ( )
static

Intel 80486 registers.

The 80486 has the same registers as the 80386 with '387 co-processor but adds a new flag to the "eflags" register.

static const RegisterDictionary* RegisterDictionary::dictionary_pentium ( )
static

Intel Pentium registers.

The Pentium has the same registers as the 80486 but adds a few flags to the "eflags" register and MMX registers.

static const RegisterDictionary* RegisterDictionary::dictionary_pentiumiii ( )
static

Intel Pentium III registers.

The Pentium III has the same register set as the Pentium but adds the xmm0 through xmm7 registers for the SSE instruction set.

static const RegisterDictionary* RegisterDictionary::dictionary_pentium4 ( )
static
static const RegisterDictionary* RegisterDictionary::dictionary_amd64 ( )
static

Amd64 registers.

The AMD64 architecture increases the size of the general purpose registers, base registers, index registers, instruction pointer, and flags register to 64-bits. Most register names from the Pentium architecture still exist and refer to 32-bit quantities, while the AMD64 adds new names that start with "r" rather than "e" (such as "rax" for the 64-bit register and "eax" for the 32 low-order bits of the same register). It also adds eight additional 64-bit general purpose registers named "r8" through "r15" along with "b", "w", and "d" suffixes for the low-order 8, 16, and 32 bits, respectively.

The only registers that are not retained are the control registers cr0-cr4, which are replaced by 64-bit registers of the same name, and debug registers dr0-dr7, which are also replaced by 64-bit registers of the same name.

static const RegisterDictionary* RegisterDictionary::dictionary_arm7 ( )
static

ARM7 registers.

The CPU has a total of 37 registers, each 32 bits wide: 31 general purpose registers named and six status registers named. At most 16 (8 in Thumb mode) general purpose registers are visible at a time depending on the mode of operation. They have names rN where N is an integer between 0 and 15, inclusive and are mapped onto a subset of the 31 physical general purpose registers. Register r13 and r14 are, by convention, a stack pointer and link register (the link register holds the return address for a function call). Register r15 is the instruction pointer. Also, at most two status registers are available at a time.

The major number of a RegisterDescriptor is used to indicate the type of register: 0=general purpose, 1=status. The minor number indicates the register number: 0-15 for general purpose, 0 or 1 for status.

static const RegisterDictionary* RegisterDictionary::dictionary_powerpc ( )
static

PowerPC registers.

static const RegisterDictionary* RegisterDictionary::dictionary_mips32 ( )
static

MIPS32 Release 1.

Release 1 of MIPS32 supports only a 32-bit FPU (support for 64-bit FPU was added in MIPS32 Release 2).

static const RegisterDictionary* RegisterDictionary::dictionary_mips32_altnames ( )
static

MIPS32 Release 1 with special registers.

This is the same dictionary as dictionary_mips32(), except additional names are supplied for the general purpose registers (e.g., "zero" for r0, "at" for r1, "gp" for r28, "sp" for r29, "fp" for r30, "ra" for r31, etc.). This is intended mostly for the AsmUnparser; any layer that looks up registers by name should probably use the standard names rather than relying on these alternate names.

static const RegisterDictionary* RegisterDictionary::dictionary_m68000_altnames ( )
static

Motorola M68330 alternate registers.

static const RegisterDictionary* RegisterDictionary::dictionary_coldfire ( )
static

FreeScale ColdFire generic hardware registers.

static const RegisterDictionary* RegisterDictionary::dictionary_coldfire_emac ( )
static

Registers for FreeScale ColdFire CPUs with EMAC (extended multiply-accumulate) unit.

static const RegisterDictionary* RegisterDictionary::dictionary_for_isa ( SgAsmExecutableFileFormat::InsSetArchitecture  )
static

Class method to choose an appropriate register dictionary for an instruction set architecture.

Returns the best available register dictionary for any architecture. Returns the null pointer if no dictionary is appropriate.

static const RegisterDictionary* RegisterDictionary::dictionary_for_isa ( SgAsmInterpretation )
static

Class method to choose an appropriate register dictionary for an instruction set architecture.

Returns the best available register dictionary for any architecture. Returns the null pointer if no dictionary is appropriate.

const std::string& RegisterDictionary::get_architecture_name ( ) const
inline

Obtain the name of the dictionary.

Definition at line 180 of file Registers.h.

void RegisterDictionary::set_architecture_name ( const std::string &  name)
inline

Set the name of the dictionary.

Dictionary names are generally architecture names. Dictionaries created by one of the built-in static methods of this class have the same name as the method that created it.

Definition at line 186 of file Registers.h.

void RegisterDictionary::insert ( const std::string &  name,
RegisterDescriptor   
)

Insert a definition into the dictionary.

If the name already exists in the dictionary then the new RegisterDescriptor will replace the one that already exists.

void RegisterDictionary::insert ( const std::string &  name,
unsigned  majr,
unsigned  minr,
unsigned  offset,
unsigned  nbits 
)

Insert a definition into the dictionary.

If the name already exists in the dictionary then the new RegisterDescriptor will replace the one that already exists.

void RegisterDictionary::insert ( const RegisterDictionary )

Inserts definitions from another dictionary into this dictionary.

Names in the other dictionary that are the same as names in this dictionary will replace the definitions in this dictionary.

void RegisterDictionary::insert ( const RegisterDictionary )

Inserts definitions from another dictionary into this dictionary.

Names in the other dictionary that are the same as names in this dictionary will replace the definitions in this dictionary.

void RegisterDictionary::resize ( const std::string &  name,
unsigned  new_nbits 
)

Changes the size of a register.

This is a common enough operation that we have a special method to do it. To change other properties of a register you would look up the register descriptor, change the property, then re-insert the register into the dictionary using the new descriptor. This method does exactly that.

const RegisterDescriptor* RegisterDictionary::lookup ( const std::string &  name) const

Returns a descriptor for a given register name.

Returns the null pointer if the name is not found. It is not possible to modify a descriptor in the dictionary because doing so would interfere with the dictionary's data structures for reverse lookups.

const std::string& RegisterDictionary::lookup ( RegisterDescriptor  ) const

Returns a register name for a given descriptor.

If more than one register has the same descriptor then the name added latest is returned. If no register is found then either return the empty string (default) or generate a generic name according to the optional supplied NameGenerator.

const RegisterDescriptor* RegisterDictionary::exists ( RegisterDescriptor  ) const

Determine if a register descriptor exists.

This is similar to the lookup method that takes a RegisterDescriptor argument, but instead of returning the name it returns a pointer to the RegisterDescriptor in the dictionary. Returns null if the specified register does not exist in this dictionary. Beware that this pointer is only valid until the dictionary is modified.

RegisterDescriptor RegisterDictionary::findLargestRegister ( unsigned  major,
unsigned  minor,
size_t  maxWidth = 0 
) const

Finds the first largest register with specified major and minor number.

Returns the first register with the largest width and having the specified major and minor numbers. Registers wider than maxWidth (if non-zero) are ignored. If no register is found with the major/minor number then an invalid (default-constructed) register is returned.

This function takes O(n) time where n is the number of registers defined.

Rose::BinaryAnalysis::RegisterParts RegisterDictionary::getAllParts ( ) const

Returns all register parts.

Returns all parts of all registers in this dictionary without any regard for register boundaries. For instance, if a diction contains the x86 AX and AL registers where AX is 16 bits and AL is its low-order eight bits, the return value will only contain the fact that the 16 bits corresponding to AX are stored, which also happens to contain the eight bits of AL, but it won't keep track that AX and AL were inserted separately. In other words, erasing AL from the returned container would also erase the low-order 8 bits of AX.

RegisterDescriptors RegisterDictionary::get_descriptors ( ) const

Returns the list of all register descriptors.

The returned list may have overlapping register descriptors. The return value is similar to get_registers() except only the RegisterDescriptor part is returned, not the names.

unsigned RegisterDictionary::firstUnusedMajor ( ) const

Returns the first unused major register number.

unsigned RegisterDictionary::firstUnusedMinor ( unsigned  majr) const

Returns the first unused minor register number.

template<class Compare >
RegisterDictionary::RegisterDescriptors RegisterDictionary::filter_nonoverlapping ( RegisterDescriptors  reglist,
Compare  order = SortBySize(),
bool  reconsider_parts = true 
)
static

Returns the list of non-overlapping registers or register parts.

The list of registers are processed in the reverse specified order and each register is added to the return value if its bits were not already added to the return value by a previous register. If a register under consideration is only partially represented in the return value at the time it is considered, then it is either split into smaller parts to be reconsidered later, or not reconsidered at all. Thus, when reconsider_parts is true, the return value might contain register descriptors that were not part of the input reglist, and which might not even have entries/names in the register dictionary (see get_largest_registers() for more explaination).

For example, to get a list of the largest non-overlapping registers one could do the following:

RegisterDictionary::SortBySize largest_to_smallest(RegisterDictionary::SortBySize::DESCENDING);
RegisterDictionary::RegisterDescriptors all_registers = ditionary.get_descriptors();
RegisterDictionary::RegisterDescriptors list = dictionary.filter_nonoverlapping(all_registers, largest_to_smallest, true);

Filtering largest to smallest and reconsidering parts is the default, so the example can be shortened to:

RegisterDictionary::RegisterDescriptors list = dictionary.filter_nonoverlapping(dictionary.get_descriptors());

In fact, it can be shortened even more since this common operation is encapsulated in get_largest_registers():

RegisterDictionary::RegisterDescriptors list = dictionary.get_largest_registers();

Definition at line 383 of file Registers.h.

References RangeMap< R, T >::begin(), RangeMap< R, T >::end(), RangeMap< R, T >::erase_ranges(), Range< T >::first(), RangeMap< R, T >::insert(), and Range< T >::size().

RegisterDescriptors RegisterDictionary::get_largest_registers ( ) const

Returns a list of the largest non-overlapping registers.

For instance, for a 32-bit x86 dictionary the return value will contain registers EAX, EBX, etc. but not AX, AH, AL, BX, BH, BL, etc. Note that some of the returned descriptors might not correspond to actual names in the dictionary; this can happen when the dictionary contains two registers that partially overlap, but are themselves not subsets of a larger register, as in:

|XXXXXXXXXXXX....| The "X" register occupies the first 12 bits of a 16-bit register
|....YYYYYYYYYYYY| The "Y" register occupies the last 12 bits of a 16-bit register

If the 16-bit register has no name, and the high- and low-order four-bit parts have no name, then the return value might consist of register "X" and the low four bits. Or it could be register "Y" and the high four bits.

RegisterDescriptors RegisterDictionary::get_smallest_registers ( ) const

Returns a list of the smallest non-overlapping registers.

For instance, for a 32-bit x86 dictionary the return value will contain AX, AH, AL, BX, BH, BL, etc. rather than EAX and EBX. It will also return the high 16-bit parts of EAX and EBX even though they're not represented with explicit definitions in the dictionary.

This is a one-liner in terms of filter_nonoverlapping. If you don't want the unnamed parts to appear in the return value (e.g., the high-order 16 bits of EAX), then call filter_nonoverlapping() like this:

SortBySize order(SortBySize::ASCENDING);
RegisterDescriptors smallest = dict.filter_nonoverlapping(dict.get_descriptors(), order, false);
void RegisterDictionary::print ( std::ostream &  ) const

Prints the contents of this register dictionary.

The first line of output contains the dictionary name. One additional line of output will be generated for each entry in the dictionary.

size_t RegisterDictionary::size ( ) const
inline

Return the number of entries in the dictionary.

Definition at line 349 of file Registers.h.


The documentation for this class was generated from the following file: