mirror of
https://github.com/ksherlock/x65.git
synced 2024-12-29 10:30:32 +00:00
Compile issues fixed for xcode
- Additional stability checks
This commit is contained in:
parent
071d64de80
commit
c8ceffdbf6
170
x65.cpp
170
x65.cpp
@ -425,7 +425,7 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool insert(unsigned int pos, H key) {
|
bool insert(unsigned int pos, H key) {
|
||||||
if (insert(pos)) {
|
if (insert(pos) && keys) {
|
||||||
keys[pos] = key;
|
keys[pos] = key;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -843,8 +843,10 @@ public:
|
|||||||
|
|
||||||
// Clean up work allocations
|
// Clean up work allocations
|
||||||
void Asm::Cleanup() {
|
void Asm::Cleanup() {
|
||||||
for (std::vector<char*>::iterator i = loadedData.begin(); i!=loadedData.end(); ++i)
|
for (std::vector<char*>::iterator i = loadedData.begin(); i != loadedData.end(); ++i) {
|
||||||
free(*i);
|
if (char *data = *i)
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
map.clear();
|
map.clear();
|
||||||
labelPools.clear();
|
labelPools.clear();
|
||||||
loadedData.clear();
|
loadedData.clear();
|
||||||
@ -1591,7 +1593,7 @@ StatusCode Asm::EvalExpression(strref expression, const struct EvalContext &etx,
|
|||||||
char ops[MAX_EVAL_OPER]; // RPN expression
|
char ops[MAX_EVAL_OPER]; // RPN expression
|
||||||
int values[MAX_EVAL_VALUES]; // RPN values (in order of RPN EVOP_VAL operations)
|
int values[MAX_EVAL_VALUES]; // RPN values (in order of RPN EVOP_VAL operations)
|
||||||
short section_ids[MAX_EVAL_SECTIONS]; // local index of each referenced section
|
short section_ids[MAX_EVAL_SECTIONS]; // local index of each referenced section
|
||||||
short section_val[MAX_EVAL_VALUES]; // each value can be assigned to one section, or -1 if fixed
|
short section_val[MAX_EVAL_VALUES] = { 0 }; // each value can be assigned to one section, or -1 if fixed
|
||||||
short num_sections = 0; // number of sections in section_ids (normally 0 or 1, can be up to MAX_EVAL_SECTIONS)
|
short num_sections = 0; // number of sections in section_ids (normally 0 or 1, can be up to MAX_EVAL_SECTIONS)
|
||||||
values[0] = 0; // Initialize RPN if no expression
|
values[0] = 0; // Initialize RPN if no expression
|
||||||
{
|
{
|
||||||
@ -1672,7 +1674,7 @@ StatusCode Asm::EvalExpression(strref expression, const struct EvalContext &etx,
|
|||||||
int valIdx = 0;
|
int valIdx = 0;
|
||||||
int ri = 0; // RPN index (value)
|
int ri = 0; // RPN index (value)
|
||||||
int preByteVal = 0; // special case for relative reference to low byte / high byte
|
int preByteVal = 0; // special case for relative reference to low byte / high byte
|
||||||
short section_counts[MAX_EVAL_SECTIONS][MAX_EVAL_VALUES];
|
short section_counts[MAX_EVAL_SECTIONS][MAX_EVAL_VALUES] = { 0 };
|
||||||
for (int o = 0; o<numOps; o++) {
|
for (int o = 0; o<numOps; o++) {
|
||||||
EvalOperator op = (EvalOperator)ops[o];
|
EvalOperator op = (EvalOperator)ops[o];
|
||||||
if (op!=EVOP_VAL && op!=EVOP_LOB && op!=EVOP_HIB && ri<2)
|
if (op!=EVOP_VAL && op!=EVOP_LOB && op!=EVOP_HIB && ri<2)
|
||||||
@ -1728,11 +1730,15 @@ StatusCode Asm::EvalExpression(strref expression, const struct EvalContext &etx,
|
|||||||
section_counts[i][ri-1] |= section_counts[i][ri];
|
section_counts[i][ri-1] |= section_counts[i][ri];
|
||||||
values[ri-1] >>= values[ri]; break;
|
values[ri-1] >>= values[ri]; break;
|
||||||
case EVOP_LOB: // low byte
|
case EVOP_LOB: // low byte
|
||||||
preByteVal = values[ri - 1];
|
if (ri) {
|
||||||
values[ri-1] &= 0xff; break;
|
preByteVal = values[ri - 1];
|
||||||
|
values[ri-1] &= 0xff;
|
||||||
|
} break;
|
||||||
case EVOP_HIB:
|
case EVOP_HIB:
|
||||||
preByteVal = values[ri - 1];
|
if (ri) {
|
||||||
values[ri - 1] = (values[ri - 1] >> 8) & 0xff; break;
|
preByteVal = values[ri - 1];
|
||||||
|
values[ri - 1] = (values[ri - 1] >> 8) & 0xff;
|
||||||
|
} break;
|
||||||
default:
|
default:
|
||||||
return ERROR_EXPRESSION_OPERATION;
|
return ERROR_EXPRESSION_OPERATION;
|
||||||
break;
|
break;
|
||||||
@ -2212,8 +2218,8 @@ StatusCode Asm::AssignLabel(strref label, strref line, bool make_constant)
|
|||||||
{
|
{
|
||||||
line.trim_whitespace();
|
line.trim_whitespace();
|
||||||
int val = 0;
|
int val = 0;
|
||||||
StatusCode status = EvalExpression(line, struct EvalContext(
|
struct EvalContext etx(CurrSection().GetPC(), scope_address[scope_depth], -1, -1);
|
||||||
CurrSection().GetPC(), scope_address[scope_depth], -1, -1), val);
|
StatusCode status = EvalExpression(line, etx, val);
|
||||||
if (status != STATUS_NOT_READY && status != STATUS_OK)
|
if (status != STATUS_NOT_READY && status != STATUS_OK)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
@ -2574,7 +2580,6 @@ StatusCode Asm::ApplyDirective(AssemblerDirective dir, strref line, strref sourc
|
|||||||
strref file = line.between('"', '"');
|
strref file = line.between('"', '"');
|
||||||
if (!file) // MERLIN: No quotes around PUT filenames
|
if (!file) // MERLIN: No quotes around PUT filenames
|
||||||
file = line.split_range(filename_end_char_range);
|
file = line.split_range(filename_end_char_range);
|
||||||
size_t size = 0;
|
|
||||||
error = ReadObjectFile(file);
|
error = ReadObjectFile(file);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -3101,9 +3106,9 @@ StatusCode Asm::AddOpcode(strref line, int group, int index, strref source_file)
|
|||||||
Reloc::Type target_section_type = Reloc::NONE;
|
Reloc::Type target_section_type = Reloc::NONE;
|
||||||
bool evalLater = false;
|
bool evalLater = false;
|
||||||
if (expression) {
|
if (expression) {
|
||||||
error = EvalExpression(expression, struct EvalContext(CurrSection().GetPC(),
|
struct EvalContext etx(CurrSection().GetPC(), scope_address[scope_depth], -1,
|
||||||
scope_address[scope_depth], -1,
|
group==OPG_BRANCH ? SectionId() : -1);
|
||||||
group==OPG_BRANCH ? SectionId() : -1), value);
|
error = EvalExpression(expression, etx, value);
|
||||||
if (error == STATUS_NOT_READY) {
|
if (error == STATUS_NOT_READY) {
|
||||||
evalLater = true;
|
evalLater = true;
|
||||||
error = STATUS_OK;
|
error = STATUS_OK;
|
||||||
@ -3575,9 +3580,9 @@ struct ObjFileMapSymbol {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Simple string pool, converts strref strings to zero terminated strings and returns the offset to the string in the pool.
|
// Simple string pool, converts strref strings to zero terminated strings and returns the offset to the string in the pool.
|
||||||
static int _AddStrPool(strref str, pairArray<unsigned int, int> *pLookup, char **strPool, unsigned int &strPoolSize, unsigned int &strPoolCap)
|
static int _AddStrPool(const strref str, pairArray<unsigned int, int> *pLookup, char **strPool, unsigned int &strPoolSize, unsigned int &strPoolCap)
|
||||||
{
|
{
|
||||||
if (!str)
|
if (!str.get() || !str.get_len())
|
||||||
return -1; // empty string
|
return -1; // empty string
|
||||||
unsigned int hash = str.fnv1a();
|
unsigned int hash = str.fnv1a();
|
||||||
unsigned int index = FindLabelIndex(hash, pLookup->getKeys(), pLookup->count());
|
unsigned int index = FindLabelIndex(hash, pLookup->getKeys(), pLookup->count());
|
||||||
@ -3587,18 +3592,21 @@ static int _AddStrPool(strref str, pairArray<unsigned int, int> *pLookup, char *
|
|||||||
if ((strPoolSize + str.get_len() + 1) > strPoolCap) {
|
if ((strPoolSize + str.get_len() + 1) > strPoolCap) {
|
||||||
strPoolCap += 4096;
|
strPoolCap += 4096;
|
||||||
char *strPoolGrow = (char*)malloc(strPoolCap);
|
char *strPoolGrow = (char*)malloc(strPoolCap);
|
||||||
if (*strPool) {
|
if (*strPool && strPoolGrow) {
|
||||||
memcpy(strPoolGrow, *strPool, strPoolSize);
|
memcpy(strPoolGrow, *strPool, strPoolSize);
|
||||||
free(*strPool);
|
free(*strPool);
|
||||||
}
|
}
|
||||||
*strPool = strPoolGrow;
|
*strPool = strPoolGrow;
|
||||||
}
|
}
|
||||||
int ret = strPoolSize;
|
int ret = strPoolSize;
|
||||||
memcpy(*strPool + strPoolSize, str.get(), str.get_len());
|
if (*strPool) {
|
||||||
(*strPool + strPoolSize)[str.get_len()] = 0;
|
char *dest = *strPool + strPoolSize;
|
||||||
strPoolSize += str.get_len()+1;
|
memcpy(dest, str.get(), str.get_len());
|
||||||
pLookup->insert(index, hash);
|
dest[str.get_len()] = 0;
|
||||||
pLookup->getValues()[index] = ret;
|
strPoolSize += str.get_len()+1;
|
||||||
|
pLookup->insert(index, hash);
|
||||||
|
pLookup->getValues()[index] = ret;
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3606,7 +3614,7 @@ StatusCode Asm::WriteObjectFile(strref filename)
|
|||||||
{
|
{
|
||||||
if (FILE *f = fopen(strown<512>(filename).c_str(), "wb")) {
|
if (FILE *f = fopen(strown<512>(filename).c_str(), "wb")) {
|
||||||
struct ObjFileHeader hdr = { 0 };
|
struct ObjFileHeader hdr = { 0 };
|
||||||
hdr.id = 'o6';
|
hdr.id = 0x6f36;
|
||||||
hdr.sections = (short)allSections.size();
|
hdr.sections = (short)allSections.size();
|
||||||
hdr.relocs = 0;
|
hdr.relocs = 0;
|
||||||
hdr.bindata = 0;
|
hdr.bindata = 0;
|
||||||
@ -3637,81 +3645,91 @@ StatusCode Asm::WriteObjectFile(strref filename)
|
|||||||
int sect = 0, reloc = 0, labs = 0, late = 0, map_sym = 0;
|
int sect = 0, reloc = 0, labs = 0, late = 0, map_sym = 0;
|
||||||
|
|
||||||
// write out sections and relocs
|
// write out sections and relocs
|
||||||
for (std::vector<Section>::iterator si = allSections.begin(); si!=allSections.end(); ++si) {
|
if (hdr.sections) {
|
||||||
struct ObjFileSection &s = aSects[sect++];
|
for (std::vector<Section>::iterator si = allSections.begin(); si!=allSections.end(); ++si) {
|
||||||
s.name.offs = _AddStrPool(si->name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
struct ObjFileSection &s = aSects[sect++];
|
||||||
s.output_size = (short)si->size();
|
s.name.offs = _AddStrPool(si->name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
s.relocs = si->pRelocs ? (short)(si->pRelocs->size()) : 0;
|
s.output_size = (short)si->size();
|
||||||
s.start_address = si->start_address;
|
s.relocs = si->pRelocs ? (short)(si->pRelocs->size()) : 0;
|
||||||
s.flags =
|
s.start_address = si->start_address;
|
||||||
|
s.flags =
|
||||||
(si->IsDummySection() ? (1 << ObjFileSection::OFS_DUMMY) : 0) |
|
(si->IsDummySection() ? (1 << ObjFileSection::OFS_DUMMY) : 0) |
|
||||||
(si->IsMergedSection() ? (1 << ObjFileSection::OFS_MERGED) : 0) |
|
(si->IsMergedSection() ? (1 << ObjFileSection::OFS_MERGED) : 0) |
|
||||||
(si->address_assigned ? (1 << ObjFileSection::OFS_FIXED) : 0);
|
(si->address_assigned ? (1 << ObjFileSection::OFS_FIXED) : 0);
|
||||||
if (si->pRelocs && si->pRelocs->size()) {
|
if (si->pRelocs && si->pRelocs->size()) {
|
||||||
for (relocList::iterator ri = si->pRelocs->begin(); ri!=si->pRelocs->end(); ++ri) {
|
for (relocList::iterator ri = si->pRelocs->begin(); ri!=si->pRelocs->end(); ++ri) {
|
||||||
struct ObjFileReloc &r = aRelocs[reloc++];
|
struct ObjFileReloc &r = aRelocs[reloc++];
|
||||||
r.base_value = ri->base_value;
|
r.base_value = ri->base_value;
|
||||||
r.section_offset = ri->section_offset;
|
r.section_offset = ri->section_offset;
|
||||||
r.target_section = ri->target_section;
|
r.target_section = ri->target_section;
|
||||||
r.value_type = ri->value_type;
|
r.value_type = ri->value_type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out labels
|
// write out labels
|
||||||
for (unsigned int li = 0; li<labels.count(); li++) {
|
if (hdr.labels) {
|
||||||
Label &lo = labels.getValue(li);
|
for (unsigned int li = 0; li<labels.count(); li++) {
|
||||||
struct ObjFileLabel &l = aLabels[labs++];
|
Label &lo = labels.getValue(li);
|
||||||
l.name.offs = _AddStrPool(lo.label_name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
|
||||||
l.value = lo.value;
|
|
||||||
l.section = lo.section;
|
|
||||||
l.mapIndex = lo.mapIndex;
|
|
||||||
l.flags =
|
|
||||||
(lo.constant ? ObjFileLabel::OFL_CNST : 0) |
|
|
||||||
(lo.pc_relative ? ObjFileLabel::OFL_ADDR : 0) |
|
|
||||||
(lo.evaluated ? ObjFileLabel::OFL_EVAL : 0) |
|
|
||||||
(lo.external ? ObjFileLabel::OFL_XDEF : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// protected labels included from other object files
|
|
||||||
int file_index = 1;
|
|
||||||
for (std::vector<ExtLabels>::iterator el = externals.begin(); el != externals.end(); ++el) {
|
|
||||||
for (unsigned int li = 0; li < el->labels.count(); ++li) {
|
|
||||||
Label &lo = el->labels.getValue(li);
|
|
||||||
struct ObjFileLabel &l = aLabels[labs++];
|
struct ObjFileLabel &l = aLabels[labs++];
|
||||||
l.name.offs = _AddStrPool(lo.label_name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
l.name.offs = _AddStrPool(lo.label_name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
l.value = lo.value;
|
l.value = lo.value;
|
||||||
l.section = lo.section;
|
l.section = lo.section;
|
||||||
l.mapIndex = lo.mapIndex;
|
l.mapIndex = lo.mapIndex;
|
||||||
l.flags =
|
l.flags =
|
||||||
|
(lo.constant ? ObjFileLabel::OFL_CNST : 0) |
|
||||||
|
(lo.pc_relative ? ObjFileLabel::OFL_ADDR : 0) |
|
||||||
|
(lo.evaluated ? ObjFileLabel::OFL_EVAL : 0) |
|
||||||
|
(lo.external ? ObjFileLabel::OFL_XDEF : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// protected labels included from other object files
|
||||||
|
if (hdr.labels) {
|
||||||
|
int file_index = 1;
|
||||||
|
for (std::vector<ExtLabels>::iterator el = externals.begin(); el != externals.end(); ++el) {
|
||||||
|
for (unsigned int li = 0; li < el->labels.count(); ++li) {
|
||||||
|
Label &lo = el->labels.getValue(li);
|
||||||
|
struct ObjFileLabel &l = aLabels[labs++];
|
||||||
|
l.name.offs = _AddStrPool(lo.label_name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
|
l.value = lo.value;
|
||||||
|
l.section = lo.section;
|
||||||
|
l.mapIndex = lo.mapIndex;
|
||||||
|
l.flags =
|
||||||
(lo.constant ? ObjFileLabel::OFL_CNST : 0) |
|
(lo.constant ? ObjFileLabel::OFL_CNST : 0) |
|
||||||
(lo.pc_relative ? ObjFileLabel::OFL_ADDR : 0) |
|
(lo.pc_relative ? ObjFileLabel::OFL_ADDR : 0) |
|
||||||
(lo.evaluated ? ObjFileLabel::OFL_EVAL : 0) |
|
(lo.evaluated ? ObjFileLabel::OFL_EVAL : 0) |
|
||||||
file_index;
|
file_index;
|
||||||
|
}
|
||||||
|
file_index++;
|
||||||
}
|
}
|
||||||
file_index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out late evals
|
// write out late evals
|
||||||
for (std::vector<LateEval>::iterator lei = lateEval.begin(); lei != lateEval.end(); ++lei) {
|
if (aLateEvals) {
|
||||||
struct ObjFileLateEval &le = aLateEvals[late++];
|
for (std::vector<LateEval>::iterator lei = lateEval.begin(); lei != lateEval.end(); ++lei) {
|
||||||
le.label.offs = _AddStrPool(lei->label, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
struct ObjFileLateEval &le = aLateEvals[late++];
|
||||||
le.expression.offs = _AddStrPool(lei->expression, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
le.label.offs = _AddStrPool(lei->label, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
le.source_file.offs = _AddStrPool(lei->source_file, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
le.expression.offs = _AddStrPool(lei->expression, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
le.section = lei->section;
|
le.source_file.offs = _AddStrPool(lei->source_file, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
le.target = (short)lei->target;
|
le.section = lei->section;
|
||||||
le.address = lei->address;
|
le.target = (short)lei->target;
|
||||||
le.scope = lei->scope;
|
le.address = lei->address;
|
||||||
le.type = lei->type;
|
le.scope = lei->scope;
|
||||||
|
le.type = lei->type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out map symbols
|
// write out map symbols
|
||||||
for (MapSymbolArray::iterator mi = map.begin(); mi != map.end(); ++mi) {
|
if (aMapSyms) {
|
||||||
struct ObjFileMapSymbol &ms = aMapSyms[map_sym++];
|
for (MapSymbolArray::iterator mi = map.begin(); mi != map.end(); ++mi) {
|
||||||
ms.name.offs = _AddStrPool(mi->name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
struct ObjFileMapSymbol &ms = aMapSyms[map_sym++];
|
||||||
ms.value = mi->value;
|
ms.name.offs = _AddStrPool(mi->name, &stringArray, &stringPool, hdr.stringdata, stringPoolCap);
|
||||||
ms.local = mi->local;
|
ms.value = mi->value;
|
||||||
ms.resolved = mi->resolved;
|
ms.local = mi->local;
|
||||||
|
ms.resolved = mi->resolved;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out the file
|
// write out the file
|
||||||
@ -3758,7 +3776,7 @@ StatusCode Asm::ReadObjectFile(strref filename)
|
|||||||
hdr.relocs * sizeof(struct ObjFileReloc) + hdr.labels * sizeof(struct ObjFileLabel) +
|
hdr.relocs * sizeof(struct ObjFileReloc) + hdr.labels * sizeof(struct ObjFileLabel) +
|
||||||
hdr.late_evals * sizeof(struct ObjFileLateEval) +
|
hdr.late_evals * sizeof(struct ObjFileLateEval) +
|
||||||
hdr.map_symbols * sizeof(struct ObjFileMapSymbol) + hdr.stringdata + hdr.bindata;
|
hdr.map_symbols * sizeof(struct ObjFileMapSymbol) + hdr.stringdata + hdr.bindata;
|
||||||
if (hdr.id == 'o6' && sum == size) {
|
if (hdr.id == 0x6f36 && sum == size) {
|
||||||
struct ObjFileSection *aSect = (struct ObjFileSection*)(&hdr + 1);
|
struct ObjFileSection *aSect = (struct ObjFileSection*)(&hdr + 1);
|
||||||
struct ObjFileReloc *aReloc = (struct ObjFileReloc*)(aSect + hdr.sections);
|
struct ObjFileReloc *aReloc = (struct ObjFileReloc*)(aSect + hdr.sections);
|
||||||
struct ObjFileLabel *aLabels = (struct ObjFileLabel*)(aReloc + hdr.relocs);
|
struct ObjFileLabel *aLabels = (struct ObjFileLabel*)(aReloc + hdr.relocs);
|
||||||
@ -3778,10 +3796,8 @@ StatusCode Asm::ReadObjectFile(strref filename)
|
|||||||
// for now just append to existing assembler data
|
// for now just append to existing assembler data
|
||||||
|
|
||||||
// sections
|
// sections
|
||||||
int load_section = (int)allSections.size();
|
|
||||||
int reloc_idx = 0;
|
int reloc_idx = 0;
|
||||||
for (int si = 0; si < hdr.sections; si++) {
|
for (int si = 0; si < hdr.sections; si++) {
|
||||||
Section *s = nullptr;
|
|
||||||
short f = aSect[si].flags;
|
short f = aSect[si].flags;
|
||||||
aSctRmp[si] = (short)allSections.size();
|
aSctRmp[si] = (short)allSections.size();
|
||||||
if (f & (1 << ObjFileSection::OFS_MERGED))
|
if (f & (1 << ObjFileSection::OFS_MERGED))
|
||||||
|
Loading…
Reference in New Issue
Block a user