sourcepawn/compiler/pawncc.c
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/sourcepawn/compiler/pawncc.c	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,501 +0,0 @@
     1.4 -#include <stdio.h>
     1.5 -#include <setjmp.h>
     1.6 -#include <assert.h>
     1.7 -#include <string.h>
     1.8 -#include "memfile.h"
     1.9 -#include "sp_file.h"
    1.10 -#include "amx.h"
    1.11 -#include "amxdbg.h"
    1.12 -#include "osdefs.h"
    1.13 -#include "zlib/zlib.h"
    1.14 -
    1.15 -enum FileSections
    1.16 -{
    1.17 -	FS_Code,			/* required */
    1.18 -	FS_Data,			/* required */
    1.19 -	FS_Publics,
    1.20 -	FS_Pubvars,
    1.21 -	FS_Natives,
    1.22 -	FS_Nametable,		/* required */
    1.23 -	FS_DbgFile,
    1.24 -	FS_DbgSymbol,
    1.25 -	FS_DbgLine,
    1.26 -	FS_DbgTags,
    1.27 -	FS_DbgAutomaton,
    1.28 -	FS_DbgState,
    1.29 -	FS_DbgStrings,
    1.30 -	FS_DbgInfo,
    1.31 -	FS_Tags,
    1.32 -	/* --- */
    1.33 -	FS_Number,
    1.34 -};
    1.35 -
    1.36 -int pc_printf(const char *message,...);
    1.37 -int pc_compile(int argc, char **argv);
    1.38 -void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf);
    1.39 -
    1.40 -memfile_t *bin_file = NULL;
    1.41 -jmp_buf brkout;
    1.42 -
    1.43 -int main(int argc, char *argv[])
    1.44 -{
    1.45 -#if defined _DEBUG
    1.46 -	getchar();
    1.47 -#endif
    1.48 -	if (pc_compile(argc,argv) == 0)
    1.49 -	{
    1.50 -		AMX_HEADER *hdr;
    1.51 -		AMX_DBG_HDR *dbg = NULL;
    1.52 -		int err;
    1.53 -		uint32_t i;
    1.54 -		sp_file_t *spf;
    1.55 -		memfile_t *dbgtab = NULL;		//dbgcrab
    1.56 -		unsigned char *dbgptr = NULL;
    1.57 -		uint32_t sections[FS_Number] = {1,1,0,0,0,1,0,0,0,0,0,0,0};
    1.58 -		FILE *fp;
    1.59 -
    1.60 -		if (bin_file == NULL)
    1.61 -		{
    1.62 -			return 0;
    1.63 -		}
    1.64 -
    1.65 -		hdr = (AMX_HEADER *)bin_file->base;
    1.66 -
    1.67 -		if ((spf=spfw_create(bin_file->name, NULL)) == NULL)
    1.68 -		{
    1.69 -			pc_printf("Error creating binary file!\n");
    1.70 -			memfile_destroy(bin_file);
    1.71 -			return 0;
    1.72 -		}
    1.73 -
    1.74 -		if ((err=setjmp(brkout))!=0)
    1.75 -		{
    1.76 -			goto write_error;
    1.77 -		}
    1.78 -
    1.79 -		spfw_add_section(spf, ".code");
    1.80 -		spfw_add_section(spf, ".data");
    1.81 -
    1.82 -		sections[FS_Publics] = (hdr->natives - hdr->publics) / hdr->defsize;
    1.83 -		if (sections[FS_Publics])
    1.84 -		{
    1.85 -			spfw_add_section(spf, ".publics");
    1.86 -		}
    1.87 -		sections[FS_Pubvars] = (hdr->tags - hdr->pubvars) / hdr->defsize;
    1.88 -		if (sections[FS_Pubvars])
    1.89 -		{
    1.90 -			spfw_add_section(spf, ".pubvars");
    1.91 -		}
    1.92 -		sections[FS_Natives] = (hdr->libraries - hdr->natives) / hdr->defsize;
    1.93 -		if (sections[FS_Natives])
    1.94 -		{
    1.95 -			spfw_add_section(spf, ".natives");
    1.96 -		}
    1.97 -		sections[FS_Tags] = (hdr->nametable - hdr->tags) / hdr->defsize;
    1.98 -		if (sections[FS_Tags])
    1.99 -		{
   1.100 -			spfw_add_section(spf, ".tags");
   1.101 -		}
   1.102 -
   1.103 -		spfw_add_section(spf, ".names");
   1.104 -
   1.105 -		if (hdr->flags & AMX_FLAG_DEBUG)
   1.106 -		{
   1.107 -			dbg = (AMX_DBG_HDR *)((unsigned char *)hdr + hdr->size);
   1.108 -			if (dbg->magic != AMX_DBG_MAGIC)
   1.109 -			{
   1.110 -				pc_printf("Error reading AMX_DBG_HDR, debug data will not be written.");
   1.111 -			} else {
   1.112 -				dbgtab = memfile_creat("", 512);
   1.113 -				dbgptr = (unsigned char *)dbg + sizeof(AMX_DBG_HDR);
   1.114 -				if (dbg->files)
   1.115 -				{
   1.116 -					spfw_add_section(spf, ".dbg.files");
   1.117 -					sections[FS_DbgFile] = dbg->files;
   1.118 -				}
   1.119 -				if (dbg->lines)
   1.120 -				{
   1.121 -					spfw_add_section(spf, ".dbg.lines");
   1.122 -					sections[FS_DbgLine] = dbg->lines;
   1.123 -				}
   1.124 -				if (dbg->symbols)
   1.125 -				{
   1.126 -					spfw_add_section(spf, ".dbg.symbols");
   1.127 -					sections[FS_DbgSymbol] = dbg->symbols;
   1.128 -				}
   1.129 -				sections[FS_DbgInfo] = 1;
   1.130 -				sections[FS_DbgStrings] = 1;
   1.131 -				spfw_add_section(spf, ".dbg.info");
   1.132 -				spfw_add_section(spf, ".dbg.strings");
   1.133 -			}
   1.134 -		}
   1.135 -
   1.136 -		spfw_finalize_header(spf);
   1.137 -		
   1.138 -		/** 
   1.139 -		 * Begin writing each of our known tables out
   1.140 -		 */
   1.141 -		
   1.142 -		if (sections[FS_Code])
   1.143 -		{
   1.144 -			sp_file_code_t cod;
   1.145 -			unsigned char *cbase;
   1.146 -
   1.147 -			cod.cellsize = sizeof(cell);
   1.148 -
   1.149 -			cod.codesize = hdr->dat - hdr->cod;
   1.150 -			cod.codeversion = hdr->amx_version;
   1.151 -			cod.flags = 0;
   1.152 -			if (hdr->flags & AMX_FLAG_DEBUG)
   1.153 -			{
   1.154 -				cod.flags |= SP_FLAG_DEBUG;
   1.155 -			}
   1.156 -			cod.code = sizeof(cod);
   1.157 -			cod.main = hdr->cip;
   1.158 -
   1.159 -			/* write the code */
   1.160 -			cbase = (unsigned char *)hdr + hdr->cod;
   1.161 -			sfwrite(&cod, sizeof(cod), 1, spf);
   1.162 -			sfwrite(cbase, cod.codesize, 1, spf);
   1.163 -
   1.164 -			spfw_next_section(spf);
   1.165 -		}
   1.166 -
   1.167 -		if (sections[FS_Data])
   1.168 -		{
   1.169 -			sp_file_data_t dat;
   1.170 -			unsigned char *dbase = (unsigned char *)hdr + hdr->dat;
   1.171 -
   1.172 -			dat.datasize = hdr->hea - hdr->dat;
   1.173 -			dat.memsize = hdr->stp;
   1.174 -			dat.data = sizeof(dat);
   1.175 -
   1.176 -			/* write header */
   1.177 -			sfwrite(&dat, sizeof(dat), 1, spf);
   1.178 -
   1.179 -			if (dat.datasize)
   1.180 -			{
   1.181 -				/* write data */
   1.182 -				sfwrite(dbase, dat.datasize, 1, spf);
   1.183 -			}
   1.184 -
   1.185 -			spfw_next_section(spf);
   1.186 -		}
   1.187 -
   1.188 -		if (sections[FS_Publics])
   1.189 -		{
   1.190 -			sp_file_publics_t *pbtbl;
   1.191 -			AMX_FUNCSTUBNT *stub;
   1.192 -			unsigned char *stubptr;
   1.193 -			uint32_t publics = sections[FS_Publics];
   1.194 -
   1.195 -			pbtbl = (sp_file_publics_t *)malloc(sizeof(sp_file_publics_t) * publics);
   1.196 -			stubptr = (unsigned char *)hdr + hdr->publics;
   1.197 -
   1.198 -			for (i=0; i<publics; i++)
   1.199 -			{
   1.200 -				stub = (AMX_FUNCSTUBNT *)stubptr;
   1.201 -				pbtbl[i].address = stub->address;
   1.202 -				pbtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
   1.203 -
   1.204 -				stubptr += hdr->defsize;
   1.205 -			}
   1.206 -			if (publics)
   1.207 -			{
   1.208 -				sfwrite(pbtbl, sizeof(sp_file_publics_t), publics, spf);
   1.209 -			}
   1.210 -			free(pbtbl);
   1.211 -
   1.212 -			spfw_next_section(spf);
   1.213 -		}
   1.214 -
   1.215 -		if (sections[FS_Pubvars])
   1.216 -		{
   1.217 -			sp_file_pubvars_t *pbvars;
   1.218 -			AMX_FUNCSTUBNT *stub;
   1.219 -			unsigned char *stubptr;
   1.220 -			uint32_t pubvars = sections[FS_Pubvars];
   1.221 -
   1.222 -			pbvars = (sp_file_pubvars_t *)malloc(sizeof(sp_file_pubvars_t) * pubvars);
   1.223 -			stubptr = (unsigned char *)hdr + hdr->pubvars;
   1.224 -
   1.225 -			for (i=0; i<pubvars; i++)
   1.226 -			{
   1.227 -				stub = (AMX_FUNCSTUBNT *)stubptr;
   1.228 -				pbvars[i].address = stub->address;
   1.229 -				pbvars[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
   1.230 -
   1.231 -				stubptr += hdr->defsize;
   1.232 -			}
   1.233 -			if (pubvars)
   1.234 -			{
   1.235 -				sfwrite(pbvars, sizeof(sp_file_pubvars_t), pubvars, spf);
   1.236 -			}
   1.237 -			free(pbvars);
   1.238 -			spfw_next_section(spf);
   1.239 -		}
   1.240 -
   1.241 -		if (sections[FS_Natives])
   1.242 -		{
   1.243 -			sp_file_natives_t *nvtbl;
   1.244 -			AMX_FUNCSTUBNT *stub;
   1.245 -			unsigned char *stubptr;
   1.246 -			uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize;
   1.247 -
   1.248 -			nvtbl = (sp_file_natives_t *)malloc(sizeof(sp_file_natives_t) * natives);
   1.249 -			stubptr = (unsigned char *)hdr + hdr->natives;
   1.250 -
   1.251 -			for (i=0; i<natives; i++)
   1.252 -			{
   1.253 -				stub = (AMX_FUNCSTUBNT *)stubptr;
   1.254 -				nvtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
   1.255 -
   1.256 -				stubptr += hdr->defsize;
   1.257 -			}
   1.258 -			if (natives)
   1.259 -			{
   1.260 -				sfwrite(nvtbl, sizeof(sp_file_natives_t), natives, spf);
   1.261 -			}
   1.262 -			free(nvtbl);
   1.263 -			spfw_next_section(spf);
   1.264 -		}
   1.265 -
   1.266 -		if (sections[FS_Tags])
   1.267 -		{
   1.268 -			uint32_t numTags = (uint32_t)sections[FS_Tags];
   1.269 -			AMX_FUNCSTUBNT *stub;
   1.270 -			sp_file_tag_t tag;
   1.271 -
   1.272 -			for (i=0; i<numTags; i++)
   1.273 -			{
   1.274 -				stub = (AMX_FUNCSTUBNT *)((unsigned char *)hdr + hdr->tags + (i * hdr->defsize));
   1.275 -				tag.tag_id = stub->address;
   1.276 -				tag.name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
   1.277 -				sfwrite(&tag, sizeof(sp_file_tag_t), 1, spf);
   1.278 -			}
   1.279 -			spfw_next_section(spf);
   1.280 -		}
   1.281 -
   1.282 -		if (sections[FS_Nametable])
   1.283 -		{
   1.284 -			unsigned char *base;
   1.285 -			uint32_t namelen;
   1.286 -
   1.287 -			/* write the entire block */
   1.288 -			base = (unsigned char *)hdr + hdr->nametable + sizeof(uint16_t);
   1.289 -			/**
   1.290 -			 * note - the name table will be padded to sizeof(cell) bytes.
   1.291 -			 * this may clip at most an extra three bytes in!
   1.292 -			 */
   1.293 -			namelen = hdr->cod - hdr->nametable;
   1.294 -			sfwrite(base, namelen, 1, spf);
   1.295 -			spfw_next_section(spf);
   1.296 -		}
   1.297 -
   1.298 -		if (hdr->flags & AMX_FLAG_DEBUG)
   1.299 -		{
   1.300 -			sp_fdbg_info_t info;
   1.301 -
   1.302 -			memset(&info, 0, sizeof(sp_fdbg_info_t));
   1.303 -
   1.304 -			if (sections[FS_DbgFile])
   1.305 -			{
   1.306 -				uint32_t idx;
   1.307 -				sp_fdbg_file_t dbgfile;
   1.308 -				AMX_DBG_FILE *_ptr;
   1.309 -				uint32_t len;
   1.310 -				for (idx=0; idx<sections[FS_DbgFile]; idx++)
   1.311 -				{
   1.312 -					/* get entry info */
   1.313 -					_ptr = (AMX_DBG_FILE *)dbgptr;
   1.314 -					len = strlen(_ptr->name);
   1.315 -					/* store */
   1.316 -					dbgfile.addr = _ptr->address;
   1.317 -					dbgfile.name = (uint32_t)memfile_tell(dbgtab);
   1.318 -					sfwrite(&dbgfile, sizeof(sp_fdbg_file_t), 1, spf);
   1.319 -					/* write to tab, then move to next */
   1.320 -					memfile_write(dbgtab, _ptr->name, len + 1);
   1.321 -					dbgptr += sizeof(AMX_DBG_FILE) + len;
   1.322 -					info.num_files++;
   1.323 -				}
   1.324 -				spfw_next_section(spf);
   1.325 -			}
   1.326 -
   1.327 -			if (sections[FS_DbgLine])
   1.328 -			{
   1.329 -				uint32_t idx;
   1.330 -				AMX_DBG_LINE *line;
   1.331 -				sp_fdbg_line_t dbgline;
   1.332 -				for (idx=0; idx<sections[FS_DbgLine]; idx++)
   1.333 -				{
   1.334 -					/* get entry info */
   1.335 -					line = (AMX_DBG_LINE *)dbgptr;
   1.336 -					/* store */
   1.337 -					dbgline.addr = (uint32_t)line->address;
   1.338 -					dbgline.line = (uint32_t)line->line;
   1.339 -					sfwrite(&dbgline, sizeof(sp_fdbg_line_t), 1, spf);
   1.340 -					/* move to next */
   1.341 -					dbgptr += sizeof(AMX_DBG_LINE);
   1.342 -					info.num_lines++;
   1.343 -				}
   1.344 -				spfw_next_section(spf);
   1.345 -			}
   1.346 -
   1.347 -			if (sections[FS_DbgSymbol])
   1.348 -			{
   1.349 -				uint32_t idx;
   1.350 -				uint32_t dnum;
   1.351 -				AMX_DBG_SYMBOL *sym;
   1.352 -				AMX_DBG_SYMDIM *dim;
   1.353 -				sp_fdbg_symbol_t dbgsym;
   1.354 -				sp_fdbg_arraydim_t dbgdim;
   1.355 -				uint32_t len;
   1.356 -
   1.357 -				for (idx=0; idx<sections[FS_DbgSymbol]; idx++)
   1.358 -				{
   1.359 -					/* get entry info */
   1.360 -					sym = (AMX_DBG_SYMBOL *)dbgptr;
   1.361 -					/* store */
   1.362 -					dbgsym.addr = *(int32_t *)&sym->address;
   1.363 -					dbgsym.tagid = sym->tag;
   1.364 -					dbgsym.codestart = (uint32_t)sym->codestart;
   1.365 -					dbgsym.codeend = (uint32_t)sym->codeend;
   1.366 -					dbgsym.dimcount = (uint16_t)sym->dim;
   1.367 -					dbgsym.vclass = (uint8_t)sym->vclass;
   1.368 -					dbgsym.ident = (uint8_t)sym->ident;
   1.369 -					dbgsym.name = (uint32_t)memfile_tell(dbgtab);
   1.370 -					sfwrite(&dbgsym, sizeof(sp_fdbg_symbol_t), 1, spf);
   1.371 -					/* write to tab */
   1.372 -					len = strlen(sym->name);
   1.373 -					memfile_write(dbgtab, sym->name, len + 1);
   1.374 -					/* move to next */
   1.375 -					dbgptr += sizeof(AMX_DBG_SYMBOL) + len;
   1.376 -					/* look for any dimensions */
   1.377 -					info.num_syms++;
   1.378 -					for (dnum=0; dnum<dbgsym.dimcount; dnum++)
   1.379 -					{
   1.380 -						/* get entry info */
   1.381 -						dim = (AMX_DBG_SYMDIM *)dbgptr;
   1.382 -						/* store */
   1.383 -						dbgdim.size = (uint32_t)dim->size;
   1.384 -						dbgdim.tagid = dim->tag;
   1.385 -						sfwrite(&dbgdim, sizeof(sp_fdbg_arraydim_t), 1, spf);
   1.386 -						/* move to next */
   1.387 -						dbgptr += sizeof(AMX_DBG_SYMDIM);
   1.388 -						info.num_arrays++;
   1.389 -					}
   1.390 -				}
   1.391 -				spfw_next_section(spf);
   1.392 -			}
   1.393 -
   1.394 -			sfwrite(&info, sizeof(sp_fdbg_info_t), 1, spf);
   1.395 -			spfw_next_section(spf);
   1.396 -
   1.397 -			if (sections[FS_DbgStrings])
   1.398 -			{
   1.399 -				sfwrite(dbgtab->base, sizeof(char), dbgtab->usedoffs, spf);
   1.400 -				spfw_next_section(spf);
   1.401 -			}
   1.402 -		}
   1.403 -
   1.404 -		spfw_finalize_all(spf);
   1.405 -
   1.406 -		/** 
   1.407 -		 * do compression
   1.408 -		 * new block for scoping only
   1.409 -		 */
   1.410 -		{
   1.411 -			memfile_t *pOrig = (memfile_t *)spf->handle;
   1.412 -			sp_file_hdr_t *pHdr;
   1.413 -			unsigned char *proper;
   1.414 -			size_t size;
   1.415 -			Bytef *zcmp;
   1.416 -			uLong disksize;
   1.417 -			size_t header_size;
   1.418 -			int err = Z_OK;
   1.419 -
   1.420 -			/* reuse this memory block! */
   1.421 -			memfile_reset(bin_file);
   1.422 -
   1.423 -			/* copy tip of header */
   1.424 -			memfile_write(bin_file, pOrig->base, sizeof(sp_file_hdr_t));
   1.425 -
   1.426 -			/* get pointer to header */
   1.427 -			pHdr = (sp_file_hdr_t *)bin_file->base;
   1.428 -
   1.429 -			/* copy the rest of the header */
   1.430 -			memfile_write(bin_file, 
   1.431 -							(unsigned char *)pOrig->base + sizeof(sp_file_hdr_t),
   1.432 -							pHdr->dataoffs - sizeof(sp_file_hdr_t));
   1.433 -
   1.434 -			header_size = pHdr->dataoffs;
   1.435 -			size = pHdr->imagesize - header_size;
   1.436 -			proper = (unsigned char *)pOrig->base + header_size;
   1.437 -
   1.438 -			/* get initial size estimate */
   1.439 -			disksize = compressBound(pHdr->imagesize);
   1.440 -			pHdr->disksize = (uint32_t)disksize;
   1.441 -			zcmp = (Bytef *)malloc(pHdr->disksize);
   1.442 -
   1.443 -			if ((err=compress2(zcmp, 
   1.444 -					&disksize, 
   1.445 -					(Bytef *)proper,
   1.446 -					(uLong)size,
   1.447 -					Z_BEST_COMPRESSION))
   1.448 -				!= Z_OK)
   1.449 -			{
   1.450 -				free(zcmp);
   1.451 -				pc_printf("Unable to compress (Z): error %d\n", err);
   1.452 -				pc_printf("Falling back to no compression.");
   1.453 -				memfile_write(bin_file, 
   1.454 -							proper,
   1.455 -							size);
   1.456 -			} else {
   1.457 -				pHdr->disksize = (uint32_t)disksize + header_size;
   1.458 -				pHdr->compression = SPFILE_COMPRESSION_GZ;
   1.459 -				memfile_write(bin_file,
   1.460 -							(unsigned char *)zcmp,
   1.461 -							disksize);
   1.462 -				free(zcmp);
   1.463 -			}
   1.464 -		}
   1.465 -
   1.466 -		spfw_destroy(spf);
   1.467 -		memfile_destroy(dbgtab);
   1.468 -
   1.469 -		/** 
   1.470 -		 * write file 
   1.471 -		 */
   1.472 -		if ((fp=fopen(bin_file->name, "wb")) != NULL)
   1.473 -		{
   1.474 -			fwrite(bin_file->base, bin_file->usedoffs, 1, fp);
   1.475 -			fclose(fp);
   1.476 -		} else {
   1.477 -			pc_printf("Unable to open %s for writing!", bin_file->name);
   1.478 -		}
   1.479 -
   1.480 -		memfile_destroy(bin_file);
   1.481 -
   1.482 -		return 0;
   1.483 -
   1.484 -write_error:
   1.485 -		pc_printf("Error writing to file: %s", bin_file->name);
   1.486 -		
   1.487 -		spfw_destroy(spf);
   1.488 -		unlink(bin_file->name);
   1.489 -		memfile_destroy(bin_file);
   1.490 -		memfile_destroy(dbgtab);
   1.491 -
   1.492 -		return 1;
   1.493 -	}
   1.494 -
   1.495 -	return 1;
   1.496 -}
   1.497 -
   1.498 -void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf)
   1.499 -{
   1.500 -	if (spf->funcs.fnWrite(buf, size, count, spf->handle) != count)
   1.501 -	{
   1.502 -		longjmp(brkout, 1);
   1.503 -	}
   1.504 -}