diff --git a/sourcepawn/compiler/pawncc.c b/sourcepawn/compiler/pawncc.c index af1d9842..6750b45c 100644 --- a/sourcepawn/compiler/pawncc.c +++ b/sourcepawn/compiler/pawncc.c @@ -16,24 +16,24 @@ enum FileSections { - FS_Code, /* required */ - FS_Data, /* required */ - FS_Publics, - FS_Pubvars, - FS_Natives, - FS_Nametable, /* required */ - FS_DbgFile, - FS_DbgSymbol, - FS_DbgLine, - FS_DbgTags, - FS_DbgNatives, - FS_DbgAutomaton, - FS_DbgState, - FS_DbgStrings, - FS_DbgInfo, - FS_Tags, - /* --- */ - FS_Number, + FS_Code, /* required */ + FS_Data, /* required */ + FS_Publics, + FS_Pubvars, + FS_Natives, + FS_Nametable, /* required */ + FS_DbgFile, + FS_DbgSymbol, + FS_DbgLine, + FS_DbgTags, + FS_DbgNatives, + FS_DbgAutomaton, + FS_DbgState, + FS_DbgStrings, + FS_DbgInfo, + FS_Tags, + /* --- */ + FS_Number, }; int pc_printf(const char *message,...); @@ -43,573 +43,513 @@ void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf); memfile_t *bin_file = NULL; jmp_buf brkout; -#define sARGS_MAX 32 /* number of arguments a function can have, max */ +#define sARGS_MAX 32 /* number of arguments a function can have, max */ #define sDIMEN_MAX 4 /* maximum number of array dimensions */ typedef struct t_arg_s { - uint8_t ident; - int16_t tag; - char *name; - uint16_t dimcount; - sp_fdbg_arraydim_t dims[sDIMEN_MAX]; + uint8_t ident; + int16_t tag; + char *name; + uint16_t dimcount; + sp_fdbg_arraydim_t dims[sDIMEN_MAX]; } t_arg; typedef struct t_native_s { - char *name; - int16_t ret_tag; - uint16_t num_args; - t_arg args[sARGS_MAX]; + char *name; + int16_t ret_tag; + uint16_t num_args; + t_arg args[sARGS_MAX]; } t_native; t_native *native_list = NULL; int main(int argc, char *argv[]) { - if (pc_compile(argc,argv) == 0) - { - AMX_HEADER *hdr; - AMX_DBG_HDR *dbg = NULL; - int err; - uint32_t i; - sp_file_t *spf; - memfile_t *dbgtab = NULL; //dbgcrab - unsigned char *dbgptr = NULL; - uint32_t sections[FS_Number] = {1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0}; - FILE *fp; + if (pc_compile(argc,argv) != 0) + return 1; - if (bin_file == NULL) - { - return 0; - } + AMX_HEADER *hdr; + AMX_DBG_HDR *dbg = NULL; + int err; + uint32_t i; + sp_file_t *spf; + memfile_t *dbgtab = NULL; //dbgcrab + unsigned char *dbgptr = NULL; + uint32_t sections[FS_Number] = {1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0}; + FILE *fp; - hdr = (AMX_HEADER *)bin_file->base; + if (bin_file == NULL) + return 0; - if ((spf=spfw_create(bin_file->name, NULL)) == NULL) - { - pc_printf("Error creating binary file!\n"); - memfile_destroy(bin_file); - return 0; - } + hdr = (AMX_HEADER *)bin_file->base; - if ((err=setjmp(brkout))!=0) - { - goto write_error; - } + if ((spf=spfw_create(bin_file->name, NULL)) == NULL) { + pc_printf("Error creating binary file!\n"); + memfile_destroy(bin_file); + return 0; + } - spfw_add_section(spf, ".code"); - spfw_add_section(spf, ".data"); + if ((err=setjmp(brkout))!=0) + goto write_error; - 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, ".code"); + spfw_add_section(spf, ".data"); - spfw_add_section(spf, ".names"); + 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"); - if (hdr->flags & AMX_FLAG_DEBUG) - { - dbg = (AMX_DBG_HDR *)((unsigned char *)hdr + hdr->size); - 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) - { - spfw_add_section(spf, ".dbg.files"); - sections[FS_DbgFile] = dbg->files; - } - if (dbg->lines) - { - spfw_add_section(spf, ".dbg.lines"); - sections[FS_DbgLine] = dbg->lines; - } - if (dbg->symbols) - { - spfw_add_section(spf, ".dbg.symbols"); - sections[FS_DbgSymbol] = dbg->symbols; - } - sections[FS_DbgInfo] = 1; - sections[FS_DbgStrings] = 1; - spfw_add_section(spf, ".dbg.info"); - spfw_add_section(spf, ".dbg.strings"); - } - } + spfw_add_section(spf, ".names"); - spfw_finalize_header(spf); - - /** - * Begin writing each of our known tables out - */ - - if (sections[FS_Code]) - { - sp_file_code_t cod; - unsigned char *cbase; + if (hdr->flags & AMX_FLAG_DEBUG) { + dbg = (AMX_DBG_HDR *)((unsigned char *)hdr + hdr->size); + 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) { + spfw_add_section(spf, ".dbg.files"); + sections[FS_DbgFile] = dbg->files; + } + if (dbg->lines) { + spfw_add_section(spf, ".dbg.lines"); + sections[FS_DbgLine] = dbg->lines; + } + if (dbg->symbols) { + spfw_add_section(spf, ".dbg.symbols"); + sections[FS_DbgSymbol] = dbg->symbols; + } + sections[FS_DbgInfo] = 1; + sections[FS_DbgStrings] = 1; + spfw_add_section(spf, ".dbg.info"); + spfw_add_section(spf, ".dbg.strings"); + } + } - cod.cellsize = sizeof(cell); + spfw_finalize_header(spf); + + /** + * Begin writing each of our known tables out + */ + + if (sections[FS_Code]) { + sp_file_code_t cod; + unsigned char *cbase; - cod.codesize = hdr->dat - hdr->cod; - cod.codeversion = hdr->amx_version; - cod.flags = 0; - if (hdr->flags & AMX_FLAG_DEBUG) - { - cod.flags |= SP_FLAG_DEBUG; - } - cod.code = sizeof(cod); - cod.main = hdr->cip; + cod.cellsize = sizeof(cell); - /* write the code */ - cbase = (unsigned char *)hdr + hdr->cod; - sfwrite(&cod, sizeof(cod), 1, spf); - sfwrite(cbase, cod.codesize, 1, spf); + cod.codesize = hdr->dat - hdr->cod; + cod.codeversion = hdr->amx_version; + cod.flags = 0; + if (hdr->flags & AMX_FLAG_DEBUG) + { + cod.flags |= SP_FLAG_DEBUG; + } + cod.code = sizeof(cod); + cod.main = hdr->cip; - spfw_next_section(spf); - } + /* write the code */ + cbase = (unsigned char *)hdr + hdr->cod; + sfwrite(&cod, sizeof(cod), 1, spf); + sfwrite(cbase, cod.codesize, 1, spf); - if (sections[FS_Data]) - { - sp_file_data_t dat; - unsigned char *dbase = (unsigned char *)hdr + hdr->dat; + spfw_next_section(spf); + } - dat.datasize = hdr->hea - hdr->dat; - dat.memsize = hdr->stp; - dat.data = sizeof(dat); + if (sections[FS_Data]) { + sp_file_data_t dat; + unsigned char *dbase = (unsigned char *)hdr + hdr->dat; - /* write header */ - sfwrite(&dat, sizeof(dat), 1, spf); + dat.datasize = hdr->hea - hdr->dat; + dat.memsize = hdr->stp; + dat.data = sizeof(dat); - if (dat.datasize) - { - /* write data */ - sfwrite(dbase, dat.datasize, 1, spf); - } + /* write header */ + sfwrite(&dat, sizeof(dat), 1, spf); - spfw_next_section(spf); - } + if (dat.datasize) { + /* write data */ + sfwrite(dbase, dat.datasize, 1, spf); + } - if (sections[FS_Publics]) - { - sp_file_publics_t *pbtbl; - AMX_FUNCSTUBNT *stub; - unsigned char *stubptr; - uint32_t publics = sections[FS_Publics]; + spfw_next_section(spf); + } - pbtbl = (sp_file_publics_t *)malloc(sizeof(sp_file_publics_t) * publics); - stubptr = (unsigned char *)hdr + hdr->publics; + if (sections[FS_Publics]) { + sp_file_publics_t *pbtbl; + AMX_FUNCSTUBNT *stub; + unsigned char *stubptr; + uint32_t publics = sections[FS_Publics]; - for (i=0; iaddress; - pbtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); + pbtbl = (sp_file_publics_t *)malloc(sizeof(sp_file_publics_t) * publics); + stubptr = (unsigned char *)hdr + hdr->publics; - stubptr += hdr->defsize; - } - if (publics) - { - sfwrite(pbtbl, sizeof(sp_file_publics_t), publics, spf); - } - free(pbtbl); + for (i=0; iaddress; + pbtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); - spfw_next_section(spf); - } + stubptr += hdr->defsize; + } + if (publics) + sfwrite(pbtbl, sizeof(sp_file_publics_t), publics, spf); + free(pbtbl); - if (sections[FS_Pubvars]) - { - sp_file_pubvars_t *pbvars; - AMX_FUNCSTUBNT *stub; - unsigned char *stubptr; - uint32_t pubvars = sections[FS_Pubvars]; + spfw_next_section(spf); + } - pbvars = (sp_file_pubvars_t *)malloc(sizeof(sp_file_pubvars_t) * pubvars); - stubptr = (unsigned char *)hdr + hdr->pubvars; + if (sections[FS_Pubvars]) { + sp_file_pubvars_t *pbvars; + AMX_FUNCSTUBNT *stub; + unsigned char *stubptr; + uint32_t pubvars = sections[FS_Pubvars]; - for (i=0; iaddress; - pbvars[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); + pbvars = (sp_file_pubvars_t *)malloc(sizeof(sp_file_pubvars_t) * pubvars); + stubptr = (unsigned char *)hdr + hdr->pubvars; - stubptr += hdr->defsize; - } - if (pubvars) - { - sfwrite(pbvars, sizeof(sp_file_pubvars_t), pubvars, spf); - } - free(pbvars); - spfw_next_section(spf); - } + for (i=0; iaddress; + pbvars[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); - if (sections[FS_Natives]) - { - sp_file_natives_t *nvtbl; - AMX_FUNCSTUBNT *stub; - unsigned char *stubptr; - uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize; + stubptr += hdr->defsize; + } + if (pubvars) + sfwrite(pbvars, sizeof(sp_file_pubvars_t), pubvars, spf); + free(pbvars); + spfw_next_section(spf); + } - nvtbl = (sp_file_natives_t *)malloc(sizeof(sp_file_natives_t) * natives); - stubptr = (unsigned char *)hdr + hdr->natives; + if (sections[FS_Natives]) { + sp_file_natives_t *nvtbl; + AMX_FUNCSTUBNT *stub; + unsigned char *stubptr; + uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize; - for (i=0; inameofs - (hdr->nametable + sizeof(uint16_t)); + nvtbl = (sp_file_natives_t *)malloc(sizeof(sp_file_natives_t) * natives); + stubptr = (unsigned char *)hdr + hdr->natives; - stubptr += hdr->defsize; - } - if (natives) - { - sfwrite(nvtbl, sizeof(sp_file_natives_t), natives, spf); - } - free(nvtbl); - spfw_next_section(spf); - } + for (i=0; inameofs - (hdr->nametable + sizeof(uint16_t)); - if (sections[FS_Tags]) - { - uint32_t numTags = (uint32_t)sections[FS_Tags]; - AMX_FUNCSTUBNT *stub; - sp_file_tag_t tag; + stubptr += hdr->defsize; + } + if (natives) { + sfwrite(nvtbl, sizeof(sp_file_natives_t), natives, spf); + } + free(nvtbl); + spfw_next_section(spf); + } - for (i=0; itags + (i * hdr->defsize)); - tag.tag_id = stub->address; - tag.name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); - sfwrite(&tag, sizeof(sp_file_tag_t), 1, spf); - } - spfw_next_section(spf); - } + if (sections[FS_Tags]) { + uint32_t numTags = (uint32_t)sections[FS_Tags]; + AMX_FUNCSTUBNT *stub; + sp_file_tag_t tag; - if (sections[FS_Nametable]) - { - unsigned char *base; - uint32_t namelen; + for (i=0; itags + (i * hdr->defsize)); + tag.tag_id = stub->address; + tag.name = stub->nameofs - (hdr->nametable + sizeof(uint16_t)); + sfwrite(&tag, sizeof(sp_file_tag_t), 1, spf); + } + spfw_next_section(spf); + } - /* write the entire block */ - base = (unsigned char *)hdr + hdr->nametable + sizeof(uint16_t); - /** - * note - the name table will be padded to sizeof(cell) bytes. - * this may clip at most an extra three bytes in! - */ - namelen = hdr->cod - hdr->nametable; - sfwrite(base, namelen, 1, spf); - spfw_next_section(spf); - } + if (sections[FS_Nametable]) { + unsigned char *base; + uint32_t namelen; - if (hdr->flags & AMX_FLAG_DEBUG) - { - sp_fdbg_info_t info; + /* write the entire block */ + base = (unsigned char *)hdr + hdr->nametable + sizeof(uint16_t); + /** + * note - the name table will be padded to sizeof(cell) bytes. + * this may clip at most an extra three bytes in! + */ + namelen = hdr->cod - hdr->nametable; + sfwrite(base, namelen, 1, spf); + spfw_next_section(spf); + } - memset(&info, 0, sizeof(sp_fdbg_info_t)); + if (hdr->flags & AMX_FLAG_DEBUG) { + sp_fdbg_info_t info; - if (sections[FS_Natives]) - { - uint16_t j; - uint32_t idx; - uint32_t name; - uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize; + memset(&info, 0, sizeof(sp_fdbg_info_t)); - sfwrite(&natives, sizeof(uint32_t), 1, spf); - for (idx=0; idxlibraries - hdr->natives) / hdr->defsize; - /* Go through arguments */ - 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); - name = (uint32_t)memfile_tell(dbgtab); - sfwrite(&name, sizeof(uint32_t), 1, spf); - memfile_write(dbgtab, - native_list[idx].args[j].name, - strlen(native_list[idx].args[j].name) + 1); - 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, - spf); - } - free(native_list[idx].args[j].name); - } - free(native_list[idx].name); - } - free(native_list); - spfw_next_section(spf); - } + sfwrite(&natives, sizeof(uint32_t), 1, spf); + for (idx=0; idxname); - /* store */ - dbgfile.addr = _ptr->address; - dbgfile.name = (uint32_t)memfile_tell(dbgtab); - sfwrite(&dbgfile, sizeof(sp_fdbg_file_t), 1, spf); - /* write to tab, then move to next */ - memfile_write(dbgtab, _ptr->name, len + 1); - dbgptr += sizeof(AMX_DBG_FILE) + len; - info.num_files++; - } - spfw_next_section(spf); - } + /* Go through arguments */ + 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); + name = (uint32_t)memfile_tell(dbgtab); + sfwrite(&name, sizeof(uint32_t), 1, spf); + memfile_write(dbgtab, + native_list[idx].args[j].name, + strlen(native_list[idx].args[j].name) + 1); + 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, + spf); + } + free(native_list[idx].args[j].name); + } + free(native_list[idx].name); + } + free(native_list); + spfw_next_section(spf); + } - if (sections[FS_DbgLine]) - { - uint32_t idx; - AMX_DBG_LINE *line; - sp_fdbg_line_t dbgline; - for (idx=0; idxaddress; - dbgline.line = (uint32_t)line->line; - sfwrite(&dbgline, sizeof(sp_fdbg_line_t), 1, spf); - /* move to next */ - dbgptr += sizeof(AMX_DBG_LINE); - info.num_lines++; - } - spfw_next_section(spf); - } + if (sections[FS_DbgFile]) { + uint32_t idx; + sp_fdbg_file_t dbgfile; + AMX_DBG_FILE *_ptr; + uint32_t len; + for (idx=0; idxname); + /* store */ + dbgfile.addr = _ptr->address; + dbgfile.name = (uint32_t)memfile_tell(dbgtab); + sfwrite(&dbgfile, sizeof(sp_fdbg_file_t), 1, spf); + /* write to tab, then move to next */ + memfile_write(dbgtab, _ptr->name, len + 1); + dbgptr += sizeof(AMX_DBG_FILE) + len; + info.num_files++; + } + spfw_next_section(spf); + } - if (sections[FS_DbgSymbol]) - { - uint32_t idx; - uint32_t dnum; - AMX_DBG_SYMBOL *sym; - AMX_DBG_SYMDIM *dim; - sp_fdbg_symbol_t dbgsym; - sp_fdbg_arraydim_t dbgdim; - uint32_t len; + if (sections[FS_DbgLine]) { + uint32_t idx; + AMX_DBG_LINE *line; + sp_fdbg_line_t dbgline; + for (idx=0; idxaddress; + dbgline.line = (uint32_t)line->line; + sfwrite(&dbgline, sizeof(sp_fdbg_line_t), 1, spf); + /* move to next */ + dbgptr += sizeof(AMX_DBG_LINE); + info.num_lines++; + } + spfw_next_section(spf); + } - for (idx=0; idxaddress; - dbgsym.tagid = sym->tag; - dbgsym.codestart = (uint32_t)sym->codestart; - dbgsym.codeend = (uint32_t)sym->codeend; - dbgsym.dimcount = (uint16_t)sym->dim; - dbgsym.vclass = (uint8_t)sym->vclass; - dbgsym.ident = (uint8_t)sym->ident; - dbgsym.name = (uint32_t)memfile_tell(dbgtab); - sfwrite(&dbgsym, sizeof(sp_fdbg_symbol_t), 1, spf); - /* write to tab */ - len = strlen(sym->name); - memfile_write(dbgtab, sym->name, len + 1); - /* move to next */ - dbgptr += sizeof(AMX_DBG_SYMBOL) + len; - /* look for any dimensions */ - info.num_syms++; - for (dnum=0; dnumsize; - dbgdim.tagid = dim->tag; - sfwrite(&dbgdim, sizeof(sp_fdbg_arraydim_t), 1, spf); - /* move to next */ - dbgptr += sizeof(AMX_DBG_SYMDIM); - info.num_arrays++; - } - } - spfw_next_section(spf); - } + if (sections[FS_DbgSymbol]) { + uint32_t idx; + uint32_t dnum; + AMX_DBG_SYMBOL *sym; + AMX_DBG_SYMDIM *dim; + sp_fdbg_symbol_t dbgsym; + sp_fdbg_arraydim_t dbgdim; + uint32_t len; - sfwrite(&info, sizeof(sp_fdbg_info_t), 1, spf); - spfw_next_section(spf); + for (idx=0; idxaddress; + dbgsym.tagid = sym->tag; + dbgsym.codestart = (uint32_t)sym->codestart; + dbgsym.codeend = (uint32_t)sym->codeend; + dbgsym.dimcount = (uint16_t)sym->dim; + dbgsym.vclass = (uint8_t)sym->vclass; + dbgsym.ident = (uint8_t)sym->ident; + dbgsym.name = (uint32_t)memfile_tell(dbgtab); + sfwrite(&dbgsym, sizeof(sp_fdbg_symbol_t), 1, spf); + /* write to tab */ + len = strlen(sym->name); + memfile_write(dbgtab, sym->name, len + 1); + /* move to next */ + dbgptr += sizeof(AMX_DBG_SYMBOL) + len; + /* look for any dimensions */ + info.num_syms++; + for (dnum=0; dnumsize; + dbgdim.tagid = dim->tag; + sfwrite(&dbgdim, sizeof(sp_fdbg_arraydim_t), 1, spf); + /* move to next */ + dbgptr += sizeof(AMX_DBG_SYMDIM); + info.num_arrays++; + } + } + spfw_next_section(spf); + } - if (sections[FS_DbgStrings]) - { - sfwrite(dbgtab->base, sizeof(char), dbgtab->usedoffs, spf); - spfw_next_section(spf); - } - } + sfwrite(&info, sizeof(sp_fdbg_info_t), 1, spf); + spfw_next_section(spf); - spfw_finalize_all(spf); + if (sections[FS_DbgStrings]) { + sfwrite(dbgtab->base, sizeof(char), dbgtab->usedoffs, spf); + spfw_next_section(spf); + } + } - /** - * do compression - * new block for scoping only - */ - { - memfile_t *pOrig = (memfile_t *)spf->handle; - sp_file_hdr_t *pHdr; - unsigned char *proper; - size_t size; - Bytef *zcmp; - uLong disksize; - size_t header_size; - int err = Z_OK; + spfw_finalize_all(spf); - /* reuse this memory block! */ - memfile_reset(bin_file); + /** + * do compression + * new block for scoping only + */ + { + memfile_t *pOrig = (memfile_t *)spf->handle; + sp_file_hdr_t *pHdr; + unsigned char *proper; + size_t size; + Bytef *zcmp; + uLong disksize; + size_t header_size; + int err = Z_OK; - /* copy tip of header */ - memfile_write(bin_file, pOrig->base, sizeof(sp_file_hdr_t)); + /* reuse this memory block! */ + memfile_reset(bin_file); - /* get pointer to header */ - pHdr = (sp_file_hdr_t *)bin_file->base; + /* copy tip of header */ + memfile_write(bin_file, pOrig->base, sizeof(sp_file_hdr_t)); - /* copy the rest of the header */ - memfile_write(bin_file, - (unsigned char *)pOrig->base + sizeof(sp_file_hdr_t), - pHdr->dataoffs - sizeof(sp_file_hdr_t)); + /* get pointer to header */ + pHdr = (sp_file_hdr_t *)bin_file->base; - header_size = pHdr->dataoffs; - size = pHdr->imagesize - header_size; - proper = (unsigned char *)pOrig->base + header_size; + /* copy the rest of the header */ + memfile_write(bin_file, + (unsigned char *)pOrig->base + sizeof(sp_file_hdr_t), + pHdr->dataoffs - sizeof(sp_file_hdr_t)); - /* get initial size estimate */ - disksize = compressBound(pHdr->imagesize); - pHdr->disksize = (uint32_t)disksize; - zcmp = (Bytef *)malloc(pHdr->disksize); + header_size = pHdr->dataoffs; + size = pHdr->imagesize - header_size; + proper = (unsigned char *)pOrig->base + header_size; - if ((err=compress2(zcmp, - &disksize, - (Bytef *)proper, - (uLong)size, - Z_BEST_COMPRESSION)) - != Z_OK) - { - free(zcmp); - pc_printf("Unable to compress (Z): error %d\n", err); - pc_printf("Falling back to no compression."); - memfile_write(bin_file, - proper, - size); - } else { - pHdr->disksize = (uint32_t)disksize + header_size; - pHdr->compression = SPFILE_COMPRESSION_GZ; - memfile_write(bin_file, - (unsigned char *)zcmp, - disksize); - free(zcmp); - } - } + /* get initial size estimate */ + disksize = compressBound(pHdr->imagesize); + pHdr->disksize = (uint32_t)disksize; + zcmp = (Bytef *)malloc(pHdr->disksize); - spfw_destroy(spf); - memfile_destroy(dbgtab); + if ((err=compress2(zcmp, + &disksize, + (Bytef *)proper, + (uLong)size, + Z_BEST_COMPRESSION)) + != Z_OK) + { + free(zcmp); + pc_printf("Unable to compress (Z): error %d\n", err); + pc_printf("Falling back to no compression."); + memfile_write(bin_file, + proper, + size); + } else { + pHdr->disksize = (uint32_t)disksize + header_size; + pHdr->compression = SPFILE_COMPRESSION_GZ; + memfile_write(bin_file, + (unsigned char *)zcmp, + disksize); + free(zcmp); + } + } - /** - * write file - */ - if ((fp=fopen(bin_file->name, "wb")) != NULL) - { - fwrite(bin_file->base, bin_file->usedoffs, 1, fp); - fclose(fp); - } else { - pc_printf("Unable to open %s for writing!", bin_file->name); - } + spfw_destroy(spf); + memfile_destroy(dbgtab); - memfile_destroy(bin_file); + /** + * write file + */ + if ((fp=fopen(bin_file->name, "wb")) != NULL) { + fwrite(bin_file->base, bin_file->usedoffs, 1, fp); + fclose(fp); + } else { + pc_printf("Unable to open %s for writing!", bin_file->name); + } - return 0; + memfile_destroy(bin_file); + + return 0; write_error: - pc_printf("Error writing to file: %s", bin_file->name); - - spfw_destroy(spf); - unlink(bin_file->name); - memfile_destroy(bin_file); - memfile_destroy(dbgtab); + pc_printf("Error writing to file: %s", bin_file->name); + + spfw_destroy(spf); + unlink(bin_file->name); + memfile_destroy(bin_file); + memfile_destroy(dbgtab); - return 1; - } - - 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); - } + 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; - } + if (num_natives == 0) + return; - native_list = (t_native *)malloc(sizeof(t_native) * num_natives); - memset(native_list, 0, sizeof(t_native) * num_natives); + native_list = (t_native *)malloc(sizeof(t_native) * num_natives); + memset(native_list, 0, sizeof(t_native) * num_natives); } #include "sc.h" void sp_fdbg_ntv_hook(int index, symbol *sym) { - int i, j; - t_native *native; + int i, j; + t_native *native; - native = &native_list[index]; - native->name = strdup(sym->name); - - 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++) - { - native->args[i].dims[j].size = sym->dim.arglist[i].dim[j]; - native->args[i].dims[j].tagid = sym->dim.arglist[i].idxtag[j]; - } - } + native = &native_list[index]; + native->name = strdup(sym->name); + + 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++) { + native->args[i].dims[j].size = sym->dim.arglist[i].dim[j]; + native->args[i].dims[j].tagid = sym->dim.arglist[i].idxtag[j]; + } + } - native->ret_tag = sym->tag; + native->ret_tag = sym->tag; } diff --git a/sourcepawn/compiler/sp_file.c b/sourcepawn/compiler/sp_file.c index e5d6addb..6a6b2c1d 100644 --- a/sourcepawn/compiler/sp_file.c +++ b/sourcepawn/compiler/sp_file.c @@ -13,183 +13,161 @@ int mf_setpos(void *handle, size_t pos); sp_writefuncs_t cstd_funcs = { - mf_open, - mf_close, - mf_write, - mf_read, - mf_getpos, - mf_setpos + mf_open, + mf_close, + mf_write, + mf_read, + mf_getpos, + mf_setpos }; sp_file_t *spfw_create(const char *name, sp_writefuncs_t *optfuncs) { - sp_file_t file; - sp_file_t *pFile; + sp_file_t file; + sp_file_t *pFile; - if (!optfuncs) - { - optfuncs = &cstd_funcs; - } + if (!optfuncs) + optfuncs = &cstd_funcs; - file.handle = optfuncs->fnOpen(name); - if (!file.handle) - { - return NULL; - } + file.handle = optfuncs->fnOpen(name); + if (!file.handle) + return NULL; - pFile = (sp_file_t *)malloc(sizeof(sp_file_t)); + pFile = (sp_file_t *)malloc(sizeof(sp_file_t)); - pFile->handle = file.handle; - memcpy(&pFile->funcs, optfuncs, sizeof(sp_writefuncs_t)); - pFile->curoffs = 0; - pFile->header.magic = SPFILE_MAGIC; - pFile->header.sections = 0; - pFile->header.stringtab = 0; - pFile->header.version = SPFILE_VERSION; - pFile->header.imagesize = 0; - pFile->header.disksize = 0; - pFile->header.compression = SPFILE_COMPRESSION_NONE; - pFile->header.dataoffs = 0; - pFile->lastsection = 0; - pFile->offsets = NULL; - pFile->sections = NULL; - pFile->state = -1; - pFile->nametab = NULL; - pFile->nametab_idx = 0; + pFile->handle = file.handle; + memcpy(&pFile->funcs, optfuncs, sizeof(sp_writefuncs_t)); + pFile->curoffs = 0; + pFile->header.magic = SPFILE_MAGIC; + pFile->header.sections = 0; + pFile->header.stringtab = 0; + pFile->header.version = SPFILE_VERSION; + pFile->header.imagesize = 0; + pFile->header.disksize = 0; + pFile->header.compression = SPFILE_COMPRESSION_NONE; + pFile->header.dataoffs = 0; + pFile->lastsection = 0; + pFile->offsets = NULL; + pFile->sections = NULL; + pFile->state = -1; + pFile->nametab = NULL; + pFile->nametab_idx = 0; - return pFile; + return pFile; } void spfw_destroy(sp_file_t *spf) { - free(spf->sections); - free(spf->nametab); - free(spf->offsets); - spf->funcs.fnClose(spf->handle); - free(spf); + free(spf->sections); + free(spf->nametab); + free(spf->offsets); + spf->funcs.fnClose(spf->handle); + free(spf); } uint8_t spfw_add_section(sp_file_t *spf, const char *name) { - size_t namelen; - uint8_t s; - if (spf->state != -1) - { - return 0; - } + size_t namelen; + uint8_t s; + if (spf->state != -1) + return 0; - namelen = strlen(name) + 1; + namelen = strlen(name) + 1; - if (spf->header.sections == 0) - { - /** allocate for first section */ - spf->sections = (sp_file_section_t *)malloc(sizeof(sp_file_section_t)); - spf->offsets = (size_t *)malloc(sizeof(size_t)); - spf->nametab = (char *)malloc(namelen); - } else { - uint16_t num = spf->header.sections + 1; - spf->sections = (sp_file_section_t *)realloc(spf->sections, sizeof(sp_file_section_t) * num); - spf->offsets = (size_t *)realloc(spf->offsets, sizeof(size_t) * num); - spf->nametab = (char *)realloc(spf->nametab, spf->nametab_idx + namelen); - } + if (spf->header.sections == 0) + { + /** allocate for first section */ + spf->sections = (sp_file_section_t *)malloc(sizeof(sp_file_section_t)); + spf->offsets = (size_t *)malloc(sizeof(size_t)); + spf->nametab = (char *)malloc(namelen); + } else { + uint16_t num = spf->header.sections + 1; + spf->sections = (sp_file_section_t *)realloc(spf->sections, sizeof(sp_file_section_t) * num); + spf->offsets = (size_t *)realloc(spf->offsets, sizeof(size_t) * num); + spf->nametab = (char *)realloc(spf->nametab, spf->nametab_idx + namelen); + } - s = spf->header.sections; + s = spf->header.sections; - spf->sections[s].nameoffs = spf->nametab_idx; - /** - * "fix" offset will be the second uint2 slot, which is after the previous sections after the header. - */ - spf->offsets[s] = sizeof(spf->header) + (sizeof(sp_file_section_t) * spf->header.sections) + sizeof(uint32_t); - strcpy(&spf->nametab[spf->nametab_idx], name); - spf->nametab_idx += namelen; + spf->sections[s].nameoffs = spf->nametab_idx; + /** + * "fix" offset will be the second uint2 slot, which is after the previous sections after the header. + */ + spf->offsets[s] = sizeof(spf->header) + (sizeof(sp_file_section_t) * spf->header.sections) + sizeof(uint32_t); + strcpy(&spf->nametab[spf->nametab_idx], name); + spf->nametab_idx += namelen; - return ++spf->header.sections; + return ++spf->header.sections; } int spfw_finalize_header(sp_file_t *spf) { - uint32_t size; - if (spf->state != -1) - { - return -1; - } + uint32_t size; + if (spf->state != -1) + return -1; - size = sizeof(sp_file_section_t) * spf->header.sections; + 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++; + 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++; - return 0; + return 0; } int spfw_next_section(sp_file_t *spf) { - uint8_t s; - uint32_t rest[2]; + uint8_t s; + uint32_t rest[2]; - if (spf->state < 0 || spf->state > spf->header.sections) - { - return -1; - } + if (spf->state < 0 || spf->state > spf->header.sections) + return -1; - if (spf->state == (int)spf->header.sections) - { - return 0; - } + if (spf->state == (int)spf->header.sections) + return 0; - s = (uint8_t)spf->state; + s = (uint8_t)spf->state; - spf->curoffs = spf->funcs.fnGetPos(spf->handle); - spf->funcs.fnSetPos(spf->handle, spf->offsets[s]); - - 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->curoffs = spf->funcs.fnGetPos(spf->handle); + spf->funcs.fnSetPos(spf->handle, spf->offsets[s]); + + 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; + spf->funcs.fnSetPos(spf->handle, spf->curoffs); + spf->lastsection = spf->curoffs; - spf->state++; + spf->state++; - return 1; + return 1; } int spfw_finalize_all(sp_file_t *spf) { - uint8_t offs; + uint8_t offs; - if (spf->state < spf->header.sections) - { - return -1; - } + if (spf->state < spf->header.sections) + return -1; - offs = offsetof(sp_file_hdr_t, imagesize); - spf->header.disksize = spf->funcs.fnGetPos(spf->handle); - spf->header.imagesize = spf->funcs.fnGetPos(spf->handle); - spf->funcs.fnSetPos(spf->handle, offs); - spf->funcs.fnWrite(&spf->header.imagesize, sizeof(uint32_t), 1, spf->handle); - spf->funcs.fnSetPos(spf->handle, spf->header.imagesize); + offs = offsetof(sp_file_hdr_t, imagesize); + spf->header.disksize = spf->funcs.fnGetPos(spf->handle); + spf->header.imagesize = spf->funcs.fnGetPos(spf->handle); + spf->funcs.fnSetPos(spf->handle, offs); + spf->funcs.fnWrite(&spf->header.imagesize, sizeof(uint32_t), 1, spf->handle); + spf->funcs.fnSetPos(spf->handle, spf->header.imagesize); - return 1; + return 1; } /** @@ -198,43 +176,39 @@ int spfw_finalize_all(sp_file_t *spf) void *mf_open(const char *name) { - return memfile_creat(name, 1024); + return memfile_creat(name, 1024); } void mf_close(void *handle) { - memfile_destroy((memfile_t *)handle); + memfile_destroy((memfile_t *)handle); } size_t mf_write(const void *buf, size_t size, size_t count, void *handle) { - if (!count) - { - return 0; - } + if (!count) + return 0; - if (memfile_write((memfile_t *)handle, buf, size*count)) - { - return count; - } + if (memfile_write((memfile_t *)handle, buf, size*count)) + return count; - return 0; + return 0; } size_t mf_read(void *buf, size_t size, size_t count, void *handle) { - return memfile_read((memfile_t *)handle, buf, size*count) / count; + return memfile_read((memfile_t *)handle, buf, size*count) / count; } size_t mf_getpos(void *handle) { - return (long)memfile_tell((memfile_t *)handle); + return (long)memfile_tell((memfile_t *)handle); } int mf_setpos(void *handle, size_t pos) { - memfile_seek((memfile_t *)handle, (long)pos); - return 1; + memfile_seek((memfile_t *)handle, (long)pos); + return 1; } @@ -246,31 +220,31 @@ int mf_setpos(void *handle, size_t pos) void *fp_open(const char *name) { - return fopen(name, "wb"); + return fopen(name, "wb"); } void fp_close(void *handle) { - fclose((FILE *)handle); + fclose((FILE *)handle); } size_t fp_write(const void *buf, size_t size, size_t count, void *handle) { - return fwrite(buf, size, count, (FILE *)handle); + return fwrite(buf, size, count, (FILE *)handle); } size_t fp_read(void *buf, size_t size, size_t count, void *handle) { - return fread(buf, size, count, (FILE *)handle); + return fread(buf, size, count, (FILE *)handle); } size_t fp_getpos(void *handle) { - return (size_t)ftell((FILE *)handle); + return (size_t)ftell((FILE *)handle); } int fp_setpos(void *handle, size_t pos) { - return fseek((FILE *)handle, (long)pos, SEEK_SET); + return fseek((FILE *)handle, (long)pos, SEEK_SET); } #endif diff --git a/sourcepawn/compiler/sp_file.h b/sourcepawn/compiler/sp_file.h index f2d39786..2a9edcbe 100644 --- a/sourcepawn/compiler/sp_file.h +++ b/sourcepawn/compiler/sp_file.h @@ -8,30 +8,30 @@ */ typedef struct sp_writefuncs_s { - void *(*fnOpen)(const char *); /* filename, returns handle */ - void (*fnClose)(void *); /* handle */ - /* buffer, size, count, handle, returns count written */ - size_t (*fnWrite)(const void *, size_t, size_t, void *); - /* buffer, size, count, handle, returns count read */ - size_t (*fnRead)(void *, size_t, size_t, void *); - /* returns current position from start */ - size_t (*fnGetPos)(void *); - /* sets current position from start, return 0 for success, nonzero for error */ - int (*fnSetPos)(void *, size_t); + void *(*fnOpen)(const char *); /* filename, returns handle */ + void (*fnClose)(void *); /* handle */ + /* buffer, size, count, handle, returns count written */ + size_t (*fnWrite)(const void *, size_t, size_t, void *); + /* buffer, size, count, handle, returns count read */ + size_t (*fnRead)(void *, size_t, size_t, void *); + /* returns current position from start */ + size_t (*fnGetPos)(void *); + /* sets current position from start, return 0 for success, nonzero for error */ + int (*fnSetPos)(void *, size_t); } sp_writefuncs_t; typedef struct sp_file_s { - sp_file_hdr_t header; - sp_file_section_t *sections; - size_t *offsets; - sp_writefuncs_t funcs; - size_t lastsection; - size_t curoffs; - void *handle; - int state; - char *nametab; - size_t nametab_idx; + sp_file_hdr_t header; + sp_file_section_t *sections; + size_t *offsets; + sp_writefuncs_t funcs; + size_t lastsection; + size_t curoffs; + void *handle; + int state; + char *nametab; + size_t nametab_idx; } sp_file_t; /**