Skip to content

Commit

Permalink
Constify backend data (capstone-engine#1040)
Browse files Browse the repository at this point in the history
* Constify string literals

Use -Wwrite-strings to force string literals to be of
type "const char[]", then fix up all warning fallout.

* Constify common infrastructure

Step one in allowing backend data to be readonly.
Minimal changes to backends for now; just set all pointers
in common structs that aren't modified to const.

* Constify AArch64 backend

Section size changes within libcapstone.so are

-.rodata               602587
-.data.rel.ro          228416
-.data                1003746
+.rodata               769051
+.data.rel.ro          241120
+.data                 824578

* Constify ARM backend

Section size changes within libcapstone.so are

-.rodata               769051
-.data.rel.ro          241120
-.data                 824578
+.rodata               959835
+.data.rel.ro          245120
+.data                 629506

* Constify Mips backend

Section size changes within libcapstone.so are

-.rodata               959835
-.data.rel.ro          245120
-.data                 629506
+.rodata              1069851
+.data.rel.ro          256416
+.data                 508194

* Constify PowerPC backend

Section size changes within libcapstone.so are

-.rodata              1069851
-.data.rel.ro          256416
-.data                 508194
+.rodata              1142715
+.data.rel.ro          272224
+.data                 419490

* Constify Sparc backend

Section size changes within libcapstone.so are

-.rodata              1142715
-.data.rel.ro          272224
-.data                 419490
+.rodata              1175227
+.data.rel.ro          277536
+.data                 381666

* Constify SystemZ backend

Section size changes within libcapstone.so are

-.rodata              1175227
-.data.rel.ro          277536
-.data                 381666
+.rodata              1221883
+.data.rel.ro          278016
+.data                 334498

* Constify X86 backend

Section size changes within libcapstone.so are

-.rodata              1221883
-.data.rel.ro          278016
-.data                 334498
+.rodata              1533531
+.data.rel.ro          281184
+.data                  19714

* Constify XCore backend

Section size changes within libcapstone.so are

-.rodata              1533531
-.data.rel.ro          281184
-.data                  19714
+.rodata              1553026
+.data.rel.ro          281280
+.data                     40
  • Loading branch information
rth7680 authored and aquynh committed Oct 22, 2017
1 parent 8998a3a commit 22ead3e
Show file tree
Hide file tree
Showing 65 changed files with 1,565 additions and 1,565 deletions.
4 changes: 2 additions & 2 deletions MCInstrDesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

/// isPredicate - Set if this is one of the operands that made up of
/// the predicate operand that controls an isPredicable() instruction.
bool MCOperandInfo_isPredicate(MCOperandInfo *m)
bool MCOperandInfo_isPredicate(const MCOperandInfo *m)
{
return m->Flags & (1 << MCOI_Predicate);
}

/// isOptionalDef - Set if this operand is a optional def.
///
bool MCOperandInfo_isOptionalDef(MCOperandInfo *m)
bool MCOperandInfo_isOptionalDef(const MCOperandInfo *m)
{
return m->Flags & (1 << MCOI_OptionalDef);
}
6 changes: 3 additions & 3 deletions MCInstrDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ typedef struct MCInstrDesc {
uint64_t TSFlags; // Target Specific Flag values
char ImplicitUses; // Registers implicitly read by this instr
char ImplicitDefs; // Registers implicitly defined by this instr
MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
const MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
uint64_t DeprecatedFeatureMask;// Feature bits that this is deprecated on, if any
// A complex method to determine is a certain is deprecated or not, and return
// the reason for deprecation.
//bool (*ComplexDeprecationInfo)(MCInst &, MCSubtargetInfo &, std::string &);
unsigned char ComplexDeprecationInfo; // dummy field, just to satisfy initializer
} MCInstrDesc;

bool MCOperandInfo_isPredicate(MCOperandInfo *m);
bool MCOperandInfo_isPredicate(const MCOperandInfo *m);

bool MCOperandInfo_isOptionalDef(MCOperandInfo *m);
bool MCOperandInfo_isOptionalDef(const MCOperandInfo *m);

#endif
26 changes: 13 additions & 13 deletions MCRegisterInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
/// defined below.
typedef struct DiffListIterator {
uint16_t Val;
MCPhysReg *List;
const MCPhysReg *List;
} DiffListIterator;

void MCRegisterInfo_InitMCRegisterInfo(MCRegisterInfo *RI,
MCRegisterDesc *D, unsigned NR,
const MCRegisterDesc *D, unsigned NR,
unsigned RA, unsigned PC,
MCRegisterClass *C, unsigned NC,
const MCRegisterClass *C, unsigned NC,
uint16_t (*RURoots)[2], unsigned NRU,
MCPhysReg *DL,
char *Strings,
uint16_t *SubIndices, unsigned NumIndices,
uint16_t *RET)
const MCPhysReg *DL,
const char *Strings,
const uint16_t *SubIndices, unsigned NumIndices,
const uint16_t *RET)
{
RI->Desc = D;
RI->NumRegs = NR;
Expand All @@ -50,7 +50,7 @@ void MCRegisterInfo_InitMCRegisterInfo(MCRegisterInfo *RI,
RI->RegEncodingTable = RET;
}

static void DiffListIterator_init(DiffListIterator *d, MCPhysReg InitVal, MCPhysReg *DiffList)
static void DiffListIterator_init(DiffListIterator *d, MCPhysReg InitVal, const MCPhysReg *DiffList)
{
d->Val = InitVal;
d->List = DiffList;
Expand Down Expand Up @@ -83,7 +83,7 @@ static bool DiffListIterator_isValid(DiffListIterator *d)
return (d->List != 0);
}

unsigned MCRegisterInfo_getMatchingSuperReg(MCRegisterInfo *RI, unsigned Reg, unsigned SubIdx, MCRegisterClass *RC)
unsigned MCRegisterInfo_getMatchingSuperReg(const MCRegisterInfo *RI, unsigned Reg, unsigned SubIdx, const MCRegisterClass *RC)
{
DiffListIterator iter;

Expand All @@ -105,10 +105,10 @@ unsigned MCRegisterInfo_getMatchingSuperReg(MCRegisterInfo *RI, unsigned Reg, un
return 0;
}

unsigned MCRegisterInfo_getSubReg(MCRegisterInfo *RI, unsigned Reg, unsigned Idx)
unsigned MCRegisterInfo_getSubReg(const MCRegisterInfo *RI, unsigned Reg, unsigned Idx)
{
DiffListIterator iter;
uint16_t *SRI = RI->SubRegIndices + RI->Desc[Reg].SubRegIndices;
const uint16_t *SRI = RI->SubRegIndices + RI->Desc[Reg].SubRegIndices;

DiffListIterator_init(&iter, (MCPhysReg)Reg, RI->DiffLists + RI->Desc[Reg].SubRegs);
DiffListIterator_next(&iter);
Expand All @@ -123,15 +123,15 @@ unsigned MCRegisterInfo_getSubReg(MCRegisterInfo *RI, unsigned Reg, unsigned Idx
return 0;
}

MCRegisterClass* MCRegisterInfo_getRegClass(MCRegisterInfo *RI, unsigned i)
const MCRegisterClass* MCRegisterInfo_getRegClass(const MCRegisterInfo *RI, unsigned i)
{
//assert(i < getNumRegClasses() && "Register Class ID out of range");
if (i >= RI->NumClasses)
return 0;
return &(RI->Classes[i]);
}

bool MCRegisterClass_contains(MCRegisterClass *c, unsigned Reg)
bool MCRegisterClass_contains(const MCRegisterClass *c, unsigned Reg)
{
unsigned InByte = Reg % 8;
unsigned Byte = Reg / 8;
Expand Down
38 changes: 19 additions & 19 deletions MCRegisterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
/// An unsigned integer type large enough to represent all physical registers,
/// but not necessarily virtual registers.
typedef uint16_t MCPhysReg;
typedef MCPhysReg* iterator;
typedef const MCPhysReg* iterator;

typedef struct MCRegisterClass {
char *Name;
const char *Name;
iterator RegsBegin;
uint8_t *RegSet;
const uint8_t *RegSet;
uint16_t RegsSize;
uint16_t RegSetSize;
uint16_t ID;
Expand Down Expand Up @@ -75,42 +75,42 @@ typedef struct MCRegisterDesc {
/// virtual methods.
///
typedef struct MCRegisterInfo {
MCRegisterDesc *Desc; // Pointer to the descriptor array
const MCRegisterDesc *Desc; // Pointer to the descriptor array
unsigned NumRegs; // Number of entries in the array
unsigned RAReg; // Return address register
unsigned PCReg; // Program counter register
MCRegisterClass *Classes; // Pointer to the regclass array
const MCRegisterClass *Classes; // Pointer to the regclass array
unsigned NumClasses; // Number of entries in the array
unsigned NumRegUnits; // Number of regunits.
uint16_t (*RegUnitRoots)[2]; // Pointer to regunit root table.
MCPhysReg *DiffLists; // Pointer to the difflists array
char *RegStrings; // Pointer to the string table.
uint16_t *SubRegIndices; // Pointer to the subreg lookup
const MCPhysReg *DiffLists; // Pointer to the difflists array
const char *RegStrings; // Pointer to the string table.
const uint16_t *SubRegIndices; // Pointer to the subreg lookup
// array.
unsigned NumSubRegIndices; // Number of subreg indices.
uint16_t *RegEncodingTable; // Pointer to array of register
const uint16_t *RegEncodingTable; // Pointer to array of register
// encodings.
} MCRegisterInfo;

void MCRegisterInfo_InitMCRegisterInfo(MCRegisterInfo *RI,
MCRegisterDesc *D, unsigned NR, unsigned RA,
const MCRegisterDesc *D, unsigned NR, unsigned RA,
unsigned PC,
MCRegisterClass *C, unsigned NC,
const MCRegisterClass *C, unsigned NC,
uint16_t (*RURoots)[2],
unsigned NRU,
MCPhysReg *DL,
char *Strings,
uint16_t *SubIndices,
const MCPhysReg *DL,
const char *Strings,
const uint16_t *SubIndices,
unsigned NumIndices,
uint16_t *RET);
const uint16_t *RET);


unsigned MCRegisterInfo_getMatchingSuperReg(MCRegisterInfo *RI, unsigned Reg, unsigned SubIdx, MCRegisterClass *RC);
unsigned MCRegisterInfo_getMatchingSuperReg(const MCRegisterInfo *RI, unsigned Reg, unsigned SubIdx, const MCRegisterClass *RC);

unsigned MCRegisterInfo_getSubReg(MCRegisterInfo *RI, unsigned Reg, unsigned Idx);
unsigned MCRegisterInfo_getSubReg(const MCRegisterInfo *RI, unsigned Reg, unsigned Idx);

MCRegisterClass* MCRegisterInfo_getRegClass(MCRegisterInfo *RI, unsigned i);
const MCRegisterClass* MCRegisterInfo_getRegClass(const MCRegisterInfo *RI, unsigned i);

bool MCRegisterClass_contains(MCRegisterClass *c, unsigned Reg);
bool MCRegisterClass_contains(const MCRegisterClass *c, unsigned Reg);

#endif
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ifneq (,$(findstring yes,$(CAPSTONE_X86_ATT_DISABLE)))
CFLAGS += -DCAPSTONE_X86_ATT_DISABLE
endif

CFLAGS += -fPIC -Wall -Iinclude
CFLAGS += -fPIC -Wall -Wwrite-strings -Iinclude

ifeq ($(CAPSTONE_USE_SYS_DYN_MEM),yes)
CFLAGS += -DCAPSTONE_USE_SYS_DYN_MEM
Expand Down
2 changes: 1 addition & 1 deletion SStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void SStream_Init(SStream *ss)
ss->buffer[0] = '\0';
}

void SStream_concat0(SStream *ss, char *s)
void SStream_concat0(SStream *ss, const char *s)
{
#ifndef CAPSTONE_DIET
unsigned int len = (unsigned int) strlen(s);
Expand Down
2 changes: 1 addition & 1 deletion SStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void SStream_Init(SStream *ss);

void SStream_concat(SStream *ss, const char *fmt, ...);

void SStream_concat0(SStream *ss, char *s);
void SStream_concat0(SStream *ss, const char *s);

void printInt64Bang(SStream *O, int64_t val);

Expand Down
Loading

0 comments on commit 22ead3e

Please sign in to comment.