Some code reformatting in SymbolMap

This commit is contained in:
Henrik Rydgård 2013-12-02 15:30:03 +01:00
parent 16369dde47
commit cfbd1b07e8
2 changed files with 89 additions and 157 deletions

View File

@ -31,8 +31,7 @@
SymbolMap symbolMap;
void SymbolMap::SortSymbols()
{
void SymbolMap::SortSymbols() {
lock_guard guard(lock_);
AssignFunctionIndices();
@ -45,8 +44,7 @@ void SymbolMap::Clear() {
data.clear();
}
bool SymbolMap::LoadSymbolMap(const char *filename)
{
bool SymbolMap::LoadSymbolMap(const char *filename) {
lock_guard guard(lock_);
Clear();
FILE *f = File::OpenCFile(filename, "r");
@ -60,13 +58,12 @@ bool SymbolMap::LoadSymbolMap(const char *filename)
bool started=false;
while (!feof(f))
{
while (!feof(f)) {
char line[512], temp[256] = {0};
char *p = fgets(line,512,f);
if(p == NULL)
char *p = fgets(line, 512, f);
if (p == NULL)
break;
// Chop any newlines off.
for (size_t i = strlen(line) - 1; i > 0; i--) {
if (line[i] == '\r' || line[i] == '\n') {
@ -141,14 +138,12 @@ void SymbolMap::SaveSymbolMap(const char *filename) const
if (!f)
return;
fprintf(f,".text\n");
for (auto it = functions.begin(), end = functions.end(); it != end; ++it)
{
for (auto it = functions.begin(), end = functions.end(); it != end; ++it) {
const FunctionEntry& e = it->second;
fprintf(f,"%08x %08x %08x %i %s\n",it->first,e.size,it->first,ST_FUNCTION,GetLabelName(it->first));
}
for (auto it = data.begin(), end = data.end(); it != end; ++it)
{
for (auto it = data.begin(), end = data.end(); it != end; ++it) {
const DataEntry& e = it->second;
fprintf(f,"%08x %08x %08x %i %s\n",it->first,e.size,it->first,ST_DATA,GetLabelName(it->first));
}
@ -162,52 +157,47 @@ bool SymbolMap::LoadNocashSym(const char *filename)
if (!f)
return false;
while (!feof(f))
{
while (!feof(f)) {
char line[256], value[256] = {0};
char *p = fgets(line,256,f);
if(p == NULL)
char *p = fgets(line, 256, f);
if (p == NULL)
break;
u32 address;
if (sscanf(line,"%08X %s",&address,value) != 2) continue;
if (address == 0 && strcmp(value,"0") == 0) continue;
if (sscanf(line, "%08X %s", &address, value) != 2)
continue;
if (address == 0 && strcmp(value, "0") == 0)
continue;
if (value[0] == '.') // data directives
{
char* s = strchr(value,':');
if (s != NULL)
{
if (value[0] == '.') {
// data directives
char* s = strchr(value, ':');
if (s != NULL) {
*s = 0;
u32 size = 0;
if (sscanf(s+1,"%04X",&size) != 1) continue;
if (sscanf(s + 1, "%04X", &size) != 1)
continue;
if (strcasecmp(value,".byt") == 0)
{
AddData(address,size,DATATYPE_BYTE);
} else if (strcasecmp(value,".wrd") == 0)
{
AddData(address,size,DATATYPE_HALFWORD);
} else if (strcasecmp(value,".dbl") == 0)
{
AddData(address,size,DATATYPE_WORD);
} else if (strcasecmp(value,".asc") == 0)
{
AddData(address,size,DATATYPE_ASCII);
if (strcasecmp(value, ".byt") == 0) {
AddData(address, size, DATATYPE_BYTE);
} else if (strcasecmp(value, ".wrd") == 0) {
AddData(address, size, DATATYPE_HALFWORD);
} else if (strcasecmp(value, ".dbl") == 0) {
AddData(address, size, DATATYPE_WORD);
} else if (strcasecmp(value, ".asc") == 0) {
AddData(address, size, DATATYPE_ASCII);
}
}
} else { // labels
int size = 1;
char* seperator = strchr(value,',');
if (seperator != NULL)
{
if (seperator != NULL) {
*seperator = 0;
sscanf(seperator+1,"%08X",&size);
}
if (size != 1)
{
if (size != 1) {
AddFunction(value,address,size);
} else {
AddLabel(value,address);
@ -219,16 +209,11 @@ bool SymbolMap::LoadNocashSym(const char *filename)
return true;
}
SymbolType SymbolMap::GetSymbolType(u32 address) const
{
SymbolType SymbolMap::GetSymbolType(u32 address) const {
if (functions.find(address) != functions.end())
return ST_FUNCTION;
if (data.find(address) != data.end())
return ST_DATA;
return ST_NONE;
}
@ -273,8 +258,7 @@ bool SymbolMap::GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask)
}
// if both exist, return the function
if (info != NULL)
{
if (info != NULL) {
info->type = ST_FUNCTION;
info->address = functionAddress;
info->size = GetFunctionSize(functionAddress);
@ -283,33 +267,29 @@ bool SymbolMap::GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask)
return true;
}
u32 SymbolMap::GetNextSymbolAddress(u32 address, SymbolType symmask)
{
u32 SymbolMap::GetNextSymbolAddress(u32 address, SymbolType symmask) {
const auto functionEntry = symmask & ST_FUNCTION ? functions.upper_bound(address) : functions.end();
const auto dataEntry = symmask & ST_DATA ? data.upper_bound(address) : data.end();
if (functionEntry == functions.end() && dataEntry == data.end())
return INVALID_ADDRESS;
u32 funcAddress = (functionEntry != functions.end()) ? functionEntry->first : 0xFFFFFFFF;
u32 dataAddress = (dataEntry != data.end()) ? dataEntry->first : 0xFFFFFFFF;
if (funcAddress <= dataAddress)
return funcAddress;
else
return dataAddress;
}
static char descriptionTemp[256];
const char *SymbolMap::GetDescription(unsigned int address) const
{
const char *SymbolMap::GetDescription(unsigned int address) const {
const char* labelName = NULL;
u32 funcStart = GetFunctionStart(address);
if (funcStart != INVALID_ADDRESS)
{
if (funcStart != INVALID_ADDRESS) {
labelName = GetLabelName(funcStart);
} else {
u32 dataStart = GetDataStart(address);
@ -324,14 +304,11 @@ const char *SymbolMap::GetDescription(unsigned int address) const
return descriptionTemp;
}
std::vector<SymbolEntry> SymbolMap::GetAllSymbols(SymbolType symmask)
{
std::vector<SymbolEntry> SymbolMap::GetAllSymbols(SymbolType symmask) {
std::vector<SymbolEntry> result;
if (symmask & ST_FUNCTION)
{
for (auto it = functions.begin(); it != functions.end(); it++)
{
if (symmask & ST_FUNCTION) {
for (auto it = functions.begin(); it != functions.end(); it++) {
SymbolEntry entry;
entry.address = it->first;
entry.size = GetFunctionSize(entry.address);
@ -341,11 +318,9 @@ std::vector<SymbolEntry> SymbolMap::GetAllSymbols(SymbolType symmask)
result.push_back(entry);
}
}
if (symmask & ST_DATA)
{
for (auto it = data.begin(); it != data.end(); it++)
{
if (symmask & ST_DATA) {
for (auto it = data.begin(); it != data.end(); it++) {
SymbolEntry entry;
entry.address = it->first;
entry.size = GetDataSize(entry.address);
@ -359,9 +334,7 @@ std::vector<SymbolEntry> SymbolMap::GetAllSymbols(SymbolType symmask)
return result;
}
void SymbolMap::AddFunction(const char* name, u32 address, u32 size)
{
void SymbolMap::AddFunction(const char* name, u32 address, u32 size) {
lock_guard guard(lock_);
FunctionEntry func;
@ -373,30 +346,23 @@ void SymbolMap::AddFunction(const char* name, u32 address, u32 size)
AddLabel(name,address);
}
u32 SymbolMap::GetFunctionStart(u32 address) const
{
u32 SymbolMap::GetFunctionStart(u32 address) const {
auto it = functions.upper_bound(address);
if (it == functions.end())
{
if (it == functions.end()) {
// check last element
auto rit = functions.rbegin();
if (rit != functions.rend())
{
if (rit != functions.rend()) {
u32 start = rit->first;
u32 size = rit->second.size;
if (start <= address && start+size > address)
return start;
}
// otherwise there's no function that contains this address
return INVALID_ADDRESS;
}
if (it != functions.begin())
{
if (it != functions.begin()) {
it--;
u32 start = it->first;
u32 size = it->second.size;
if (start <= address && start+size > address)
@ -406,8 +372,7 @@ u32 SymbolMap::GetFunctionStart(u32 address) const
return INVALID_ADDRESS;
}
u32 SymbolMap::GetFunctionSize(u32 startAddress) const
{
u32 SymbolMap::GetFunctionSize(u32 startAddress) const {
auto it = functions.find(startAddress);
if (it == functions.end())
return INVALID_ADDRESS;
@ -415,8 +380,7 @@ u32 SymbolMap::GetFunctionSize(u32 startAddress) const
return it->second.size;
}
int SymbolMap::GetFunctionNum(u32 address) const
{
int SymbolMap::GetFunctionNum(u32 address) const {
u32 start = GetFunctionStart(address);
if (start == INVALID_ADDRESS)
return INVALID_ADDRESS;
@ -428,17 +392,14 @@ int SymbolMap::GetFunctionNum(u32 address) const
return it->second.index;
}
void SymbolMap::AssignFunctionIndices()
{
void SymbolMap::AssignFunctionIndices() {
int index = 0;
for (auto it = functions.begin(); it != functions.end(); it++)
{
for (auto it = functions.begin(); it != functions.end(); it++) {
it->second.index = index++;
}
}
bool SymbolMap::SetFunctionSize(u32 startAddress, u32 newSize)
{
bool SymbolMap::SetFunctionSize(u32 startAddress, u32 newSize) {
lock_guard guard(lock_);
auto it = functions.find(startAddress);
@ -451,8 +412,7 @@ bool SymbolMap::SetFunctionSize(u32 startAddress, u32 newSize)
return true;
}
bool SymbolMap::RemoveFunction(u32 startAddress, bool removeName)
{
bool SymbolMap::RemoveFunction(u32 startAddress, bool removeName) {
lock_guard guard(lock_);
auto it = functions.find(startAddress);
@ -460,8 +420,7 @@ bool SymbolMap::RemoveFunction(u32 startAddress, bool removeName)
return false;
functions.erase(it);
if (removeName)
{
if (removeName) {
auto labelIt = labels.find(startAddress);
if (labelIt != labels.end())
labels.erase(labelIt);
@ -495,8 +454,7 @@ void SymbolMap::SetLabelName(const char* name, u32 address) {
}
}
const char* SymbolMap::GetLabelName(u32 address) const
{
const char* SymbolMap::GetLabelName(u32 address) const {
auto it = labels.find(address);
if (it == labels.end())
return NULL;
@ -504,12 +462,9 @@ const char* SymbolMap::GetLabelName(u32 address) const
return it->second.name;
}
bool SymbolMap::GetLabelValue(const char* name, u32& dest)
{
for (auto it = labels.begin(); it != labels.end(); it++)
{
if (strcasecmp(name,it->second.name) == 0)
{
bool SymbolMap::GetLabelValue(const char* name, u32& dest) {
for (auto it = labels.begin(); it != labels.end(); it++) {
if (strcasecmp(name,it->second.name) == 0) {
dest = it->first;
return true;
}
@ -518,19 +473,14 @@ bool SymbolMap::GetLabelValue(const char* name, u32& dest)
return false;
}
void SymbolMap::AddData(u32 address, u32 size, DataType type)
{
void SymbolMap::AddData(u32 address, u32 size, DataType type) {
DataEntry entry;
entry.size = size;
entry.type = type;
data[address] = entry;
}
u32 SymbolMap::GetDataStart(u32 address) const
{
u32 SymbolMap::GetDataStart(u32 address) const {
auto it = data.upper_bound(address);
if (it == data.end())
{
@ -544,15 +494,12 @@ u32 SymbolMap::GetDataStart(u32 address) const
if (start <= address && start+size > address)
return start;
}
// otherwise there's no data that contains this address
return INVALID_ADDRESS;
}
if (it != data.begin())
{
if (it != data.begin()) {
it--;
u32 start = it->first;
u32 size = it->second.size;
if (start <= address && start+size > address)
@ -562,30 +509,23 @@ u32 SymbolMap::GetDataStart(u32 address) const
return INVALID_ADDRESS;
}
u32 SymbolMap::GetDataSize(u32 startAddress) const
{
u32 SymbolMap::GetDataSize(u32 startAddress) const {
auto it = data.find(startAddress);
if (it == data.end())
return INVALID_ADDRESS;
return it->second.size;
}
DataType SymbolMap::GetDataType(u32 startAddress) const
{
DataType SymbolMap::GetDataType(u32 startAddress) const {
auto it = data.find(startAddress);
if (it == data.end())
return DATATYPE_NONE;
return it->second.type;
}
#ifdef _WIN32
struct DefaultSymbol
{
struct DefaultSymbol {
u32 address;
const char* name;
};
@ -598,30 +538,25 @@ static const DefaultSymbol defaultSymbols[]= {
{ 0x00010000, "Scratchpad" },
};
void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const
{
void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const {
wchar_t temp[256];
lock_guard guard(lock_);
SendMessage(listbox, WM_SETREDRAW, FALSE, 0);
ListBox_ResetContent(listbox);
switch (symType)
{
switch (symType) {
case ST_FUNCTION:
{
SendMessage(listbox, LB_INITSTORAGE, (WPARAM)functions.size(), (LPARAM)functions.size() * 30);
for (auto it = functions.begin(), end = functions.end(); it != end; ++it)
{
for (auto it = functions.begin(), end = functions.end(); it != end; ++it) {
const FunctionEntry& entry = it->second;
const char* name = GetLabelName(it->first);
if (name != NULL)
wsprintf(temp, L"%S", name);
else
wsprintf(temp, L"0x%08X", it->first);
int index = ListBox_AddString(listbox,temp);
ListBox_SetItemData(listbox,index,it->first);
}
@ -633,15 +568,13 @@ void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const
int count = ARRAYSIZE(defaultSymbols)+(int)data.size();
SendMessage(listbox, LB_INITSTORAGE, (WPARAM)count, (LPARAM)count * 30);
for (int i = 0; i < ARRAYSIZE(defaultSymbols); i++)
{
for (int i = 0; i < ARRAYSIZE(defaultSymbols); i++) {
wsprintf(temp, L"0x%08X (%S)", defaultSymbols[i].address, defaultSymbols[i].name);
int index = ListBox_AddString(listbox,temp);
ListBox_SetItemData(listbox,index,defaultSymbols[i].address);
}
for (auto it = data.begin(), end = data.end(); it != end; ++it)
{
for (auto it = data.begin(), end = data.end(); it != end; ++it) {
const DataEntry& entry = it->second;
const char* name = GetLabelName(it->first);

View File

@ -24,10 +24,10 @@
#include <map>
enum SymbolType {
ST_NONE=0,
ST_FUNCTION=1,
ST_DATA=2,
ST_ALL=3
ST_NONE = 0,
ST_FUNCTION = 1,
ST_DATA = 2,
ST_ALL = 3,
};
struct SymbolInfo {
@ -36,14 +36,15 @@ struct SymbolInfo {
u32 size;
};
struct SymbolEntry
{
struct SymbolEntry {
std::string name;
u32 address;
u32 size;
};
enum DataType { DATATYPE_NONE, DATATYPE_BYTE, DATATYPE_HALFWORD, DATATYPE_WORD, DATATYPE_ASCII };
enum DataType {
DATATYPE_NONE, DATATYPE_BYTE, DATATYPE_HALFWORD, DATATYPE_WORD, DATATYPE_ASCII
};
#ifdef _WIN32
struct HWND__;
@ -59,7 +60,7 @@ public:
bool LoadSymbolMap(const char *filename);
void SaveSymbolMap(const char *filename) const;
bool LoadNocashSym(const char *ilename);
SymbolType GetSymbolType(u32 address) const;
bool GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask = ST_FUNCTION) const;
u32 GetNextSymbolAddress(u32 address, SymbolType symmask);
@ -91,28 +92,26 @@ public:
private:
void AssignFunctionIndices();
struct FunctionEntry
{
struct FunctionEntry {
u32 size;
int index;
};
struct LabelEntry
{
struct LabelEntry {
char name[128];
};
struct DataEntry
{
struct DataEntry {
DataType type;
u32 size;
};
std::map<u32,FunctionEntry> functions;
std::map<u32,LabelEntry> labels;
std::map<u32,DataEntry> data;
std::map<u32, FunctionEntry> functions;
std::map<u32, LabelEntry> labels;
std::map<u32, DataEntry> data;
mutable recursive_mutex lock_;
};
extern SymbolMap symbolMap;