sourcepawn/jit/engine2.cpp
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/sourcepawn/jit/engine2.cpp	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,278 +0,0 @@
     1.4 -#include <stdio.h>
     1.5 -#include <stdlib.h>
     1.6 -#include <string.h>
     1.7 -#include "engine2.h"
     1.8 -#include "x86/jit_x86.h"
     1.9 -#include "jit_version.h"
    1.10 -#include "zlib/zlib.h"
    1.11 -#include "BaseRuntime.h"
    1.12 -#include "sp_vm_engine.h"
    1.13 -
    1.14 -using namespace SourcePawn;
    1.15 -
    1.16 -SourcePawnEngine2::SourcePawnEngine2()
    1.17 -{
    1.18 -	m_Profiler = NULL;
    1.19 -}
    1.20 -
    1.21 -sp_plugin_t *_ReadPlugin(sp_file_hdr_t *hdr, uint8_t *base, sp_plugin_t *plugin, int *err)
    1.22 -{
    1.23 -	char *nameptr;
    1.24 -	uint8_t sectnum = 0;
    1.25 -	sp_file_section_t *secptr = (sp_file_section_t *)(base + sizeof(sp_file_hdr_t));
    1.26 -
    1.27 -	memset(plugin, 0, sizeof(sp_plugin_t));
    1.28 -
    1.29 -	plugin->base = base;
    1.30 -
    1.31 -	while (sectnum < hdr->sections)
    1.32 -	{
    1.33 -		nameptr = (char *)(base + hdr->stringtab + secptr->nameoffs);
    1.34 -
    1.35 -		if (!(plugin->pcode) && !strcmp(nameptr, ".code"))
    1.36 -		{
    1.37 -			sp_file_code_t *cod = (sp_file_code_t *)(base + secptr->dataoffs);
    1.38 -			plugin->pcode = base + secptr->dataoffs + cod->code;
    1.39 -			plugin->pcode_size = cod->codesize;
    1.40 -			plugin->flags = cod->flags;
    1.41 -		}
    1.42 -		else if (!(plugin->data) && !strcmp(nameptr, ".data"))
    1.43 -		{
    1.44 -			sp_file_data_t *dat = (sp_file_data_t *)(base + secptr->dataoffs);
    1.45 -			plugin->data = base + secptr->dataoffs + dat->data;
    1.46 -			plugin->data_size = dat->datasize;
    1.47 -			plugin->mem_size = dat->memsize;
    1.48 -			plugin->memory = new uint8_t[plugin->mem_size];
    1.49 -			memcpy(plugin->memory, plugin->data, plugin->data_size);
    1.50 -		}
    1.51 -		else if (!(plugin->info.publics) && !strcmp(nameptr, ".publics"))
    1.52 -		{
    1.53 -			plugin->info.publics_num = secptr->size / sizeof(sp_file_publics_t);
    1.54 -			plugin->info.publics = (sp_file_publics_t *)(base + secptr->dataoffs);
    1.55 -		}
    1.56 -		else if (!(plugin->info.pubvars) && !strcmp(nameptr, ".pubvars"))
    1.57 -		{
    1.58 -			plugin->info.pubvars_num = secptr->size / sizeof(sp_file_pubvars_t);
    1.59 -			plugin->info.pubvars = (sp_file_pubvars_t *)(base + secptr->dataoffs);
    1.60 -		}
    1.61 -		else if (!(plugin->info.natives) && !strcmp(nameptr, ".natives"))
    1.62 -		{
    1.63 -			plugin->info.natives_num = secptr->size / sizeof(sp_file_natives_t);
    1.64 -			plugin->info.natives = (sp_file_natives_t *)(base + secptr->dataoffs);
    1.65 -		}
    1.66 -		else if (!(plugin->info.stringbase) && !strcmp(nameptr, ".names"))
    1.67 -		{
    1.68 -			plugin->info.stringbase = (const char *)(base + secptr->dataoffs);
    1.69 -		}
    1.70 -		else if (!(plugin->debug.files) && !strcmp(nameptr, ".dbg.files"))
    1.71 -		{
    1.72 -			plugin->debug.files = (sp_fdbg_file_t *)(base + secptr->dataoffs);
    1.73 -		}
    1.74 -		else if (!(plugin->debug.lines) && !strcmp(nameptr, ".dbg.lines"))
    1.75 -		{
    1.76 -			plugin->debug.lines = (sp_fdbg_line_t *)(base + secptr->dataoffs);
    1.77 -		}
    1.78 -		else if (!(plugin->debug.symbols) && !strcmp(nameptr, ".dbg.symbols"))
    1.79 -		{
    1.80 -			plugin->debug.symbols = (sp_fdbg_symbol_t *)(base + secptr->dataoffs);
    1.81 -		}
    1.82 -		else if (!(plugin->debug.lines_num) && !strcmp(nameptr, ".dbg.info"))
    1.83 -		{
    1.84 -			sp_fdbg_info_t *inf = (sp_fdbg_info_t *)(base + secptr->dataoffs);
    1.85 -			plugin->debug.files_num = inf->num_files;
    1.86 -			plugin->debug.lines_num = inf->num_lines;
    1.87 -			plugin->debug.syms_num = inf->num_syms;
    1.88 -		}
    1.89 -		else if (!(plugin->debug.stringbase) && !strcmp(nameptr, ".dbg.strings"))
    1.90 -		{
    1.91 -			plugin->debug.stringbase = (const char *)(base + secptr->dataoffs);
    1.92 -		}
    1.93 -
    1.94 -		secptr++;
    1.95 -		sectnum++;
    1.96 -	}
    1.97 -
    1.98 -	if (!(plugin->pcode) || !(plugin->data) || !(plugin->info.stringbase))
    1.99 -	{
   1.100 -		goto return_error;
   1.101 -	}
   1.102 -
   1.103 -	if ((plugin->flags & SP_FLAG_DEBUG) && (!(plugin->debug.files) || !(plugin->debug.lines) || !(plugin->debug.symbols)))
   1.104 -	{
   1.105 -		goto return_error;
   1.106 -	}
   1.107 -
   1.108 -	if (err)
   1.109 -	{
   1.110 -		*err = SP_ERROR_NONE;
   1.111 -	}
   1.112 -
   1.113 -	return plugin;
   1.114 -
   1.115 -return_error:
   1.116 -	if (err)
   1.117 -	{
   1.118 -		*err = SP_ERROR_FILE_FORMAT;
   1.119 -	}
   1.120 -
   1.121 -	return NULL;
   1.122 -}
   1.123 -
   1.124 -IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
   1.125 -{
   1.126 -	sp_file_hdr_t hdr;
   1.127 -	sp_plugin_t *plugin;
   1.128 -	uint8_t *base;
   1.129 -	int z_result;
   1.130 -	int error;
   1.131 -	BaseRuntime *pRuntime;
   1.132 -
   1.133 -	FILE *fp = fopen(file, "rb");
   1.134 -
   1.135 -	if (!fp)
   1.136 -	{
   1.137 -		error = SP_ERROR_NOT_FOUND;
   1.138 -		goto return_error;
   1.139 -	}
   1.140 -
   1.141 -	/* Rewind for safety */
   1.142 -	fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
   1.143 -
   1.144 -	if (hdr.magic != SPFILE_MAGIC)
   1.145 -	{
   1.146 -		error = SP_ERROR_FILE_FORMAT;
   1.147 -		goto return_error;
   1.148 -	}
   1.149 -
   1.150 -	switch (hdr.compression)
   1.151 -	{
   1.152 -	case SPFILE_COMPRESSION_GZ:
   1.153 -		{
   1.154 -			uint32_t uncompsize = hdr.imagesize - hdr.dataoffs;
   1.155 -			uint32_t compsize = hdr.disksize - hdr.dataoffs;
   1.156 -			uint32_t sectsize = hdr.dataoffs - sizeof(sp_file_hdr_t);
   1.157 -			uLongf destlen = uncompsize;
   1.158 -
   1.159 -			char *tempbuf = (char *)malloc(compsize);
   1.160 -			void *uncompdata = malloc(uncompsize);
   1.161 -			void *sectheader = malloc(sectsize);
   1.162 -
   1.163 -			fread(sectheader, sectsize, 1, fp);
   1.164 -			fread(tempbuf, compsize, 1, fp);
   1.165 -
   1.166 -			z_result = uncompress((Bytef *)uncompdata, &destlen, (Bytef *)tempbuf, compsize);
   1.167 -			free(tempbuf);
   1.168 -			if (z_result != Z_OK)
   1.169 -			{
   1.170 -				free(sectheader);
   1.171 -				free(uncompdata);
   1.172 -				error = SP_ERROR_DECOMPRESSOR;
   1.173 -				goto return_error;
   1.174 -			}
   1.175 -
   1.176 -			base = (uint8_t *)malloc(hdr.imagesize);
   1.177 -			memcpy(base, &hdr, sizeof(sp_file_hdr_t));
   1.178 -			memcpy(base + sizeof(sp_file_hdr_t), sectheader, sectsize);
   1.179 -			free(sectheader);
   1.180 -			memcpy(base + hdr.dataoffs, uncompdata, uncompsize);
   1.181 -			free(uncompdata);
   1.182 -			break;
   1.183 -		}
   1.184 -	case SPFILE_COMPRESSION_NONE:
   1.185 -		{
   1.186 -			base = (uint8_t *)malloc(hdr.imagesize);
   1.187 -			rewind(fp);
   1.188 -			fread(base, hdr.imagesize, 1, fp);
   1.189 -			break;
   1.190 -		}
   1.191 -	default:
   1.192 -		{
   1.193 -			error = SP_ERROR_DECOMPRESSOR;
   1.194 -			goto return_error;
   1.195 -		}
   1.196 -	}
   1.197 -
   1.198 -	plugin = new sp_plugin_t;
   1.199 -
   1.200 -	memset(plugin, 0, sizeof(sp_plugin_t));
   1.201 -
   1.202 -	plugin->base_size = hdr.imagesize;
   1.203 -	if (!_ReadPlugin(&hdr, base, plugin, err))
   1.204 -	{
   1.205 -		delete plugin;
   1.206 -		free(base);
   1.207 -		return NULL;
   1.208 -	}
   1.209 -
   1.210 -	pRuntime = new BaseRuntime(plugin);
   1.211 -
   1.212 -	if (co == NULL)
   1.213 -	{
   1.214 -		co = g_Jit1.StartCompilation(pRuntime);
   1.215 -	}
   1.216 -
   1.217 -
   1.218 -	*err = pRuntime->ApplyCompilationOptions(co);
   1.219 -	if (*err != SP_ERROR_NONE)
   1.220 -	{
   1.221 -		delete pRuntime;
   1.222 -		return NULL;
   1.223 -	}
   1.224 -
   1.225 -	return pRuntime;
   1.226 -
   1.227 -return_error:
   1.228 -	*err = error;
   1.229 -
   1.230 -	return NULL;
   1.231 -}
   1.232 -
   1.233 -SPVM_NATIVE_FUNC SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
   1.234 -{
   1.235 -	return g_Jit1.CreateFakeNative(callback, pData);
   1.236 -}
   1.237 -
   1.238 -void SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
   1.239 -{
   1.240 -	g_Jit1.DestroyFakeNative(func);
   1.241 -}
   1.242 -
   1.243 -const char *SourcePawnEngine2::GetEngineName()
   1.244 -{
   1.245 -	return "SourcePawn 1.1, jit-x86";
   1.246 -}
   1.247 -
   1.248 -const char *SourcePawnEngine2::GetVersionString()
   1.249 -{
   1.250 -	return SVN_FULL_VERSION;
   1.251 -}
   1.252 -
   1.253 -IProfiler *SourcePawnEngine2::GetProfiler()
   1.254 -{
   1.255 -	return m_Profiler;
   1.256 -}
   1.257 -
   1.258 -void SourcePawnEngine2::SetProfiler(IProfiler *profiler)
   1.259 -{
   1.260 -	m_Profiler = profiler;
   1.261 -}
   1.262 -
   1.263 -IDebugListener *SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
   1.264 -{
   1.265 -	return g_engine1.SetDebugListener(listener);
   1.266 -}
   1.267 -
   1.268 -unsigned int SourcePawnEngine2::GetAPIVersion()
   1.269 -{
   1.270 -	return SOURCEPAWN_ENGINE2_API_VERSION;
   1.271 -}
   1.272 -
   1.273 -ICompilation *SourcePawnEngine2::StartCompilation()
   1.274 -{
   1.275 -	return g_Jit1.StartCompilation();
   1.276 -}
   1.277 -
   1.278 -const char *SourcePawnEngine2::GetErrorString(int err)
   1.279 -{
   1.280 -	return g_engine1.GetErrorString(err);
   1.281 -}