Merge branch 'calli-3'

This commit is contained in:
David Anderson 2014-08-22 09:59:11 -07:00
commit 50f3ec8da2
3 changed files with 579 additions and 665 deletions

View File

@ -67,8 +67,9 @@ t_native *native_list = NULL;
int main(int argc, char *argv[])
{
if (pc_compile(argc,argv) == 0)
{
if (pc_compile(argc,argv) != 0)
return 1;
AMX_HEADER *hdr;
AMX_DBG_HDR *dbg = NULL;
int err;
@ -80,75 +81,55 @@ int main(int argc, char *argv[])
FILE *fp;
if (bin_file == NULL)
{
return 0;
}
hdr = (AMX_HEADER *)bin_file->base;
if ((spf=spfw_create(bin_file->name, NULL)) == NULL)
{
if ((spf=spfw_create(bin_file->name, NULL)) == NULL) {
pc_printf("Error creating binary file!\n");
memfile_destroy(bin_file);
return 0;
}
if ((err=setjmp(brkout))!=0)
{
goto write_error;
}
spfw_add_section(spf, ".code");
spfw_add_section(spf, ".data");
sections[FS_Publics] = (hdr->natives - hdr->publics) / hdr->defsize;
if (sections[FS_Publics])
{
spfw_add_section(spf, ".publics");
}
sections[FS_Pubvars] = (hdr->tags - hdr->pubvars) / hdr->defsize;
if (sections[FS_Pubvars])
{
spfw_add_section(spf, ".pubvars");
}
sections[FS_Natives] = (hdr->libraries - hdr->natives) / hdr->defsize;
if (sections[FS_Natives])
{
spfw_add_section(spf, ".natives");
}
sections[FS_Tags] = (hdr->nametable - hdr->tags) / hdr->defsize;
if (sections[FS_Tags])
{
spfw_add_section(spf, ".tags");
}
spfw_add_section(spf, ".names");
if (hdr->flags & AMX_FLAG_DEBUG)
{
if (hdr->flags & AMX_FLAG_DEBUG) {
dbg = (AMX_DBG_HDR *)((unsigned char *)hdr + hdr->size);
if (dbg->magic != AMX_DBG_MAGIC)
{
if (dbg->magic != AMX_DBG_MAGIC) {
pc_printf("Error reading AMX_DBG_HDR, debug data will not be written.");
} else {
dbgtab = memfile_creat("", 512);
dbgptr = (unsigned char *)dbg + sizeof(AMX_DBG_HDR);
if ((sections[FS_DbgNatives] = sections[FS_Natives]) > 0)
{
spfw_add_section(spf, ".dbg.natives");
}
if (dbg->files)
{
if (dbg->files) {
spfw_add_section(spf, ".dbg.files");
sections[FS_DbgFile] = dbg->files;
}
if (dbg->lines)
{
if (dbg->lines) {
spfw_add_section(spf, ".dbg.lines");
sections[FS_DbgLine] = dbg->lines;
}
if (dbg->symbols)
{
if (dbg->symbols) {
spfw_add_section(spf, ".dbg.symbols");
sections[FS_DbgSymbol] = dbg->symbols;
}
@ -165,8 +146,7 @@ int main(int argc, char *argv[])
* Begin writing each of our known tables out
*/
if (sections[FS_Code])
{
if (sections[FS_Code]) {
sp_file_code_t cod;
unsigned char *cbase;
@ -190,8 +170,7 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_Data])
{
if (sections[FS_Data]) {
sp_file_data_t dat;
unsigned char *dbase = (unsigned char *)hdr + hdr->dat;
@ -202,8 +181,7 @@ int main(int argc, char *argv[])
/* write header */
sfwrite(&dat, sizeof(dat), 1, spf);
if (dat.datasize)
{
if (dat.datasize) {
/* write data */
sfwrite(dbase, dat.datasize, 1, spf);
}
@ -211,8 +189,7 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_Publics])
{
if (sections[FS_Publics]) {
sp_file_publics_t *pbtbl;
AMX_FUNCSTUBNT *stub;
unsigned char *stubptr;
@ -221,8 +198,7 @@ int main(int argc, char *argv[])
pbtbl = (sp_file_publics_t *)malloc(sizeof(sp_file_publics_t) * publics);
stubptr = (unsigned char *)hdr + hdr->publics;
for (i=0; i<publics; i++)
{
for (i=0; i<publics; i++) {
stub = (AMX_FUNCSTUBNT *)stubptr;
pbtbl[i].address = stub->address;
pbtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
@ -230,16 +206,13 @@ int main(int argc, char *argv[])
stubptr += hdr->defsize;
}
if (publics)
{
sfwrite(pbtbl, sizeof(sp_file_publics_t), publics, spf);
}
free(pbtbl);
spfw_next_section(spf);
}
if (sections[FS_Pubvars])
{
if (sections[FS_Pubvars]) {
sp_file_pubvars_t *pbvars;
AMX_FUNCSTUBNT *stub;
unsigned char *stubptr;
@ -248,8 +221,7 @@ int main(int argc, char *argv[])
pbvars = (sp_file_pubvars_t *)malloc(sizeof(sp_file_pubvars_t) * pubvars);
stubptr = (unsigned char *)hdr + hdr->pubvars;
for (i=0; i<pubvars; i++)
{
for (i=0; i<pubvars; i++) {
stub = (AMX_FUNCSTUBNT *)stubptr;
pbvars[i].address = stub->address;
pbvars[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
@ -257,15 +229,12 @@ int main(int argc, char *argv[])
stubptr += hdr->defsize;
}
if (pubvars)
{
sfwrite(pbvars, sizeof(sp_file_pubvars_t), pubvars, spf);
}
free(pbvars);
spfw_next_section(spf);
}
if (sections[FS_Natives])
{
if (sections[FS_Natives]) {
sp_file_natives_t *nvtbl;
AMX_FUNCSTUBNT *stub;
unsigned char *stubptr;
@ -274,29 +243,25 @@ int main(int argc, char *argv[])
nvtbl = (sp_file_natives_t *)malloc(sizeof(sp_file_natives_t) * natives);
stubptr = (unsigned char *)hdr + hdr->natives;
for (i=0; i<natives; i++)
{
for (i=0; i<natives; i++) {
stub = (AMX_FUNCSTUBNT *)stubptr;
nvtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
stubptr += hdr->defsize;
}
if (natives)
{
if (natives) {
sfwrite(nvtbl, sizeof(sp_file_natives_t), natives, spf);
}
free(nvtbl);
spfw_next_section(spf);
}
if (sections[FS_Tags])
{
if (sections[FS_Tags]) {
uint32_t numTags = (uint32_t)sections[FS_Tags];
AMX_FUNCSTUBNT *stub;
sp_file_tag_t tag;
for (i=0; i<numTags; i++)
{
for (i=0; i<numTags; i++) {
stub = (AMX_FUNCSTUBNT *)((unsigned char *)hdr + hdr->tags + (i * hdr->defsize));
tag.tag_id = stub->address;
tag.name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
@ -305,8 +270,7 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_Nametable])
{
if (sections[FS_Nametable]) {
unsigned char *base;
uint32_t namelen;
@ -321,22 +285,19 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (hdr->flags & AMX_FLAG_DEBUG)
{
if (hdr->flags & AMX_FLAG_DEBUG) {
sp_fdbg_info_t info;
memset(&info, 0, sizeof(sp_fdbg_info_t));
if (sections[FS_Natives])
{
if (sections[FS_Natives]) {
uint16_t j;
uint32_t idx;
uint32_t name;
uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize;
sfwrite(&natives, sizeof(uint32_t), 1, spf);
for (idx=0; idx<natives; idx++)
{
for (idx=0; idx<natives; idx++) {
sfwrite(&idx, sizeof(uint32_t), 1, spf);
name = (uint32_t)memfile_tell(dbgtab);
memfile_write(dbgtab, native_list[idx].name, strlen(native_list[idx].name) + 1);
@ -345,8 +306,7 @@ int main(int argc, char *argv[])
sfwrite(&native_list[idx].num_args, sizeof(uint16_t), 1, spf);
/* Go through arguments */
for (j = 0; j < native_list[idx].num_args; j++)
{
for (j = 0; j < native_list[idx].num_args; j++) {
sfwrite(&native_list[idx].args[j].ident, sizeof(uint8_t), 1, spf);
sfwrite(&native_list[idx].args[j].tag, sizeof(int16_t), 1, spf);
sfwrite(&native_list[idx].args[j].dimcount, sizeof(uint16_t), 1, spf);
@ -355,8 +315,7 @@ int main(int argc, char *argv[])
memfile_write(dbgtab,
native_list[idx].args[j].name,
strlen(native_list[idx].args[j].name) + 1);
if (native_list[idx].args[j].dimcount)
{
if (native_list[idx].args[j].dimcount) {
sfwrite(native_list[idx].args[j].dims,
sizeof(sp_fdbg_arraydim_t),
native_list[idx].args[j].dimcount,
@ -370,14 +329,12 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_DbgFile])
{
if (sections[FS_DbgFile]) {
uint32_t idx;
sp_fdbg_file_t dbgfile;
AMX_DBG_FILE *_ptr;
uint32_t len;
for (idx=0; idx<sections[FS_DbgFile]; idx++)
{
for (idx=0; idx<sections[FS_DbgFile]; idx++) {
/* get entry info */
_ptr = (AMX_DBG_FILE *)dbgptr;
len = strlen(_ptr->name);
@ -393,13 +350,11 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_DbgLine])
{
if (sections[FS_DbgLine]) {
uint32_t idx;
AMX_DBG_LINE *line;
sp_fdbg_line_t dbgline;
for (idx=0; idx<sections[FS_DbgLine]; idx++)
{
for (idx=0; idx<sections[FS_DbgLine]; idx++) {
/* get entry info */
line = (AMX_DBG_LINE *)dbgptr;
/* store */
@ -413,8 +368,7 @@ int main(int argc, char *argv[])
spfw_next_section(spf);
}
if (sections[FS_DbgSymbol])
{
if (sections[FS_DbgSymbol]) {
uint32_t idx;
uint32_t dnum;
AMX_DBG_SYMBOL *sym;
@ -423,8 +377,7 @@ int main(int argc, char *argv[])
sp_fdbg_arraydim_t dbgdim;
uint32_t len;
for (idx=0; idx<sections[FS_DbgSymbol]; idx++)
{
for (idx=0; idx<sections[FS_DbgSymbol]; idx++) {
/* get entry info */
sym = (AMX_DBG_SYMBOL *)dbgptr;
/* store */
@ -463,8 +416,7 @@ int main(int argc, char *argv[])
sfwrite(&info, sizeof(sp_fdbg_info_t), 1, spf);
spfw_next_section(spf);
if (sections[FS_DbgStrings])
{
if (sections[FS_DbgStrings]) {
sfwrite(dbgtab->base, sizeof(char), dbgtab->usedoffs, spf);
spfw_next_section(spf);
}
@ -538,8 +490,7 @@ int main(int argc, char *argv[])
/**
* write file
*/
if ((fp=fopen(bin_file->name, "wb")) != NULL)
{
if ((fp=fopen(bin_file->name, "wb")) != NULL) {
fwrite(bin_file->base, bin_file->usedoffs, 1, fp);
fclose(fp);
} else {
@ -558,26 +509,19 @@ write_error:
memfile_destroy(bin_file);
memfile_destroy(dbgtab);
return 1;
}
return 1;
}
void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf)
{
if (spf->funcs.fnWrite(buf, size, count, spf->handle) != count)
{
longjmp(brkout, 1);
}
}
void sp_fdbg_ntv_start(int num_natives)
{
if (num_natives == 0)
{
return;
}
native_list = (t_native *)malloc(sizeof(t_native) * num_natives);
memset(native_list, 0, sizeof(t_native) * num_natives);
@ -593,19 +537,15 @@ void sp_fdbg_ntv_hook(int index, symbol *sym)
native = &native_list[index];
native->name = strdup(sym->name);
for (i = 0; i < sMAXARGS; i++)
{
for (i = 0; i < sMAXARGS; i++) {
if (sym->dim.arglist[i].ident == 0)
{
break;
}
native->num_args++;
native->args[i].tag = sym->dim.arglist[i].tags == NULL ? 0 : sym->dim.arglist[i].tags[0];
native->args[i].name = strdup(sym->dim.arglist[i].name);
native->args[i].ident = sym->dim.arglist[i].ident;
native->args[i].dimcount = sym->dim.arglist[i].numdim;
for (j = 0; j < native->args[i].dimcount; j++)
{
for (j = 0; j < native->args[i].dimcount; j++) {
native->args[i].dims[j].size = sym->dim.arglist[i].dim[j];
native->args[i].dims[j].tagid = sym->dim.arglist[i].idxtag[j];
}

View File

@ -27,15 +27,11 @@ sp_file_t *spfw_create(const char *name, sp_writefuncs_t *optfuncs)
sp_file_t *pFile;
if (!optfuncs)
{
optfuncs = &cstd_funcs;
}
file.handle = optfuncs->fnOpen(name);
if (!file.handle)
{
return NULL;
}
pFile = (sp_file_t *)malloc(sizeof(sp_file_t));
@ -74,9 +70,7 @@ uint8_t spfw_add_section(sp_file_t *spf, const char *name)
size_t namelen;
uint8_t s;
if (spf->state != -1)
{
return 0;
}
namelen = strlen(name) + 1;
@ -110,27 +104,19 @@ int spfw_finalize_header(sp_file_t *spf)
{
uint32_t size;
if (spf->state != -1)
{
return -1;
}
size = sizeof(sp_file_section_t) * spf->header.sections;
spf->header.stringtab = sizeof(spf->header) + size;
spf->header.dataoffs = spf->header.stringtab + spf->nametab_idx;
if (spf->funcs.fnWrite(&spf->header, sizeof(spf->header), 1, spf->handle) != 1)
{
return -1;
}
if (spf->funcs.fnWrite(spf->sections, sizeof(sp_file_section_t), spf->header.sections, spf->handle) !=
spf->header.sections)
{
return -1;
}
if (spf->funcs.fnWrite(spf->nametab, sizeof(char), spf->nametab_idx, spf->handle) != spf->nametab_idx)
{
return -1;
}
spf->curoffs = spf->funcs.fnGetPos(spf->handle);
spf->lastsection = spf->curoffs;
spf->state++;
@ -144,14 +130,10 @@ int spfw_next_section(sp_file_t *spf)
uint32_t rest[2];
if (spf->state < 0 || spf->state > spf->header.sections)
{
return -1;
}
if (spf->state == (int)spf->header.sections)
{
return 0;
}
s = (uint8_t)spf->state;
@ -161,9 +143,7 @@ int spfw_next_section(sp_file_t *spf)
rest[0] = spf->lastsection;
rest[1] = spf->curoffs - spf->lastsection;
if (spf->funcs.fnWrite(rest, sizeof(uint32_t), 2, spf->handle) != 2)
{
return -1;
}
spf->funcs.fnSetPos(spf->handle, spf->curoffs);
spf->lastsection = spf->curoffs;
@ -178,9 +158,7 @@ int spfw_finalize_all(sp_file_t *spf)
uint8_t offs;
if (spf->state < spf->header.sections)
{
return -1;
}
offs = offsetof(sp_file_hdr_t, imagesize);
spf->header.disksize = spf->funcs.fnGetPos(spf->handle);
@ -209,14 +187,10 @@ void mf_close(void *handle)
size_t mf_write(const void *buf, size_t size, size_t count, void *handle)
{
if (!count)
{
return 0;
}
if (memfile_write((memfile_t *)handle, buf, size*count))
{
return count;
}
return 0;
}