sourcepawn/jit/BaseRuntime.cpp
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/sourcepawn/jit/BaseRuntime.cpp	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,357 +0,0 @@
     1.4 -#include <stdio.h>
     1.5 -#include <stdlib.h>
     1.6 -#include <string.h>
     1.7 -#include "BaseRuntime.h"
     1.8 -#include "sp_vm_engine.h"
     1.9 -#include "x86/jit_x86.h"
    1.10 -#include "sp_vm_basecontext.h"
    1.11 -#include "engine2.h"
    1.12 -
    1.13 -using namespace SourcePawn;
    1.14 -
    1.15 -int GlobalDebugBreak(BaseContext *ctx, uint32_t frm, uint32_t cip)
    1.16 -{
    1.17 -	g_engine1.RunTracer(ctx, frm, cip);
    1.18 -
    1.19 -	return SP_ERROR_NONE;
    1.20 -}
    1.21 -
    1.22 -BaseRuntime::BaseRuntime(sp_plugin_t *pl) : m_Debug(pl), m_pPlugin(pl)
    1.23 -{
    1.24 -	m_pCtx = new BaseContext(this);
    1.25 -
    1.26 -	if (m_pPlugin->info.publics_num > 0)
    1.27 -	{
    1.28 -		m_PubFuncs = new CFunction *[m_pPlugin->info.publics_num];
    1.29 -		memset(m_PubFuncs, 0, sizeof(CFunction *) * m_pPlugin->info.publics_num);
    1.30 -	}
    1.31 -	else
    1.32 -	{
    1.33 -		m_PubFuncs = NULL;
    1.34 -	}
    1.35 -
    1.36 -	m_pPlugin->dbreak = GlobalDebugBreak;
    1.37 -	m_pPlugin->profiler = g_engine2.GetProfiler();
    1.38 -}
    1.39 -
    1.40 -BaseRuntime::~BaseRuntime()
    1.41 -{
    1.42 -	for (uint32_t i = 0; i < m_pPlugin->info.publics_num; i++)
    1.43 -	{
    1.44 -		delete m_PubFuncs[i];
    1.45 -		m_PubFuncs[i] = NULL;
    1.46 -	}
    1.47 -	delete [] m_PubFuncs;
    1.48 -
    1.49 -	ClearCompile();
    1.50 -
    1.51 -	free(m_pPlugin->base);
    1.52 -	delete [] m_pPlugin->memory;
    1.53 -	delete m_pPlugin;
    1.54 -}
    1.55 -
    1.56 -void BaseRuntime::ClearCompile()
    1.57 -{
    1.58 -	g_Jit1.FreeContextVars(m_pCtx->GetCtx());
    1.59 -	g_Jit1.FreePluginVars(m_pPlugin);
    1.60 -}
    1.61 -
    1.62 -int BaseRuntime::FindNativeByName(const char *name, uint32_t *index)
    1.63 -{
    1.64 -	int high;
    1.65 -
    1.66 -	high = m_pPlugin->info.natives_num - 1;
    1.67 -
    1.68 -	for (uint32_t i=0; i<m_pPlugin->info.natives_num; i++)
    1.69 -	{
    1.70 -		if (strcmp(m_pPlugin->natives[i].name, name) == 0)
    1.71 -		{
    1.72 -			if (index)
    1.73 -			{
    1.74 -				*index = i;
    1.75 -			}
    1.76 -			return SP_ERROR_NONE;
    1.77 -		}
    1.78 -	}
    1.79 -
    1.80 -	return SP_ERROR_NOT_FOUND;
    1.81 -}
    1.82 -
    1.83 -int BaseRuntime::GetNativeByIndex(uint32_t index, sp_native_t **native)
    1.84 -{
    1.85 -	if (index >= m_pPlugin->info.natives_num)
    1.86 -	{
    1.87 -		return SP_ERROR_INDEX;
    1.88 -	}
    1.89 -
    1.90 -	if (native)
    1.91 -	{
    1.92 -		*native = &(m_pPlugin->natives[index]);
    1.93 -	}
    1.94 -
    1.95 -	return SP_ERROR_NONE;
    1.96 -}
    1.97 -
    1.98 -
    1.99 -uint32_t BaseRuntime::GetNativesNum()
   1.100 -{
   1.101 -	return m_pPlugin->info.natives_num;
   1.102 -}
   1.103 -
   1.104 -int BaseRuntime::FindPublicByName(const char *name, uint32_t *index)
   1.105 -{
   1.106 -	int diff, high, low;
   1.107 -	uint32_t mid;
   1.108 -
   1.109 -	high = m_pPlugin->info.publics_num - 1;
   1.110 -	low = 0;
   1.111 -
   1.112 -	while (low <= high)
   1.113 -	{
   1.114 -		mid = (low + high) / 2;
   1.115 -		diff = strcmp(m_pPlugin->publics[mid].name, name);
   1.116 -		if (diff == 0)
   1.117 -		{
   1.118 -			if (index)
   1.119 -			{
   1.120 -				*index = mid;
   1.121 -			}
   1.122 -			return SP_ERROR_NONE;
   1.123 -		} else if (diff < 0) {
   1.124 -			low = mid + 1;
   1.125 -		} else {
   1.126 -			high = mid - 1;
   1.127 -		}
   1.128 -	}
   1.129 -
   1.130 -	return SP_ERROR_NOT_FOUND;
   1.131 -}
   1.132 -
   1.133 -int BaseRuntime::GetPublicByIndex(uint32_t index, sp_public_t **pblic)
   1.134 -{
   1.135 -	if (index >= m_pPlugin->info.publics_num)
   1.136 -	{
   1.137 -		return SP_ERROR_INDEX;
   1.138 -	}
   1.139 -
   1.140 -	if (pblic)
   1.141 -	{
   1.142 -		*pblic = &(m_pPlugin->publics[index]);
   1.143 -	}
   1.144 -
   1.145 -	return SP_ERROR_NONE;
   1.146 -}
   1.147 -
   1.148 -uint32_t BaseRuntime::GetPublicsNum()
   1.149 -{
   1.150 -	return m_pPlugin->info.publics_num;
   1.151 -}
   1.152 -
   1.153 -int BaseRuntime::GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar)
   1.154 -{
   1.155 -	if (index >= m_pPlugin->info.pubvars_num)
   1.156 -	{
   1.157 -		return SP_ERROR_INDEX;
   1.158 -	}
   1.159 -
   1.160 -	if (pubvar)
   1.161 -	{
   1.162 -		*pubvar = &(m_pPlugin->pubvars[index]);
   1.163 -	}
   1.164 -
   1.165 -	return SP_ERROR_NONE;
   1.166 -}
   1.167 -
   1.168 -int BaseRuntime::FindPubvarByName(const char *name, uint32_t *index)
   1.169 -{
   1.170 -	int diff, high, low;
   1.171 -	uint32_t mid;
   1.172 -
   1.173 -	high = m_pPlugin->info.pubvars_num - 1;
   1.174 -	low = 0;
   1.175 -
   1.176 -	while (low <= high)
   1.177 -	{
   1.178 -		mid = (low + high) / 2;
   1.179 -		diff = strcmp(m_pPlugin->pubvars[mid].name, name);
   1.180 -		if (diff == 0)
   1.181 -		{
   1.182 -			if (index)
   1.183 -			{
   1.184 -				*index = mid;
   1.185 -			}
   1.186 -			return SP_ERROR_NONE;
   1.187 -		} else if (diff < 0) {
   1.188 -			low = mid + 1;
   1.189 -		} else {
   1.190 -			high = mid - 1;
   1.191 -		}
   1.192 -	}
   1.193 -
   1.194 -	return SP_ERROR_NOT_FOUND;
   1.195 -}
   1.196 -
   1.197 -int BaseRuntime::GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr)
   1.198 -{
   1.199 -	if (index >= m_pPlugin->info.pubvars_num)
   1.200 -	{
   1.201 -		return SP_ERROR_INDEX;
   1.202 -	}
   1.203 -
   1.204 -	*local_addr = m_pPlugin->info.pubvars[index].address;
   1.205 -	*phys_addr = m_pPlugin->pubvars[index].offs;
   1.206 -
   1.207 -	return SP_ERROR_NONE;
   1.208 -}
   1.209 -
   1.210 -uint32_t BaseRuntime::GetPubVarsNum()
   1.211 -{
   1.212 -	return m_pPlugin->info.pubvars_num;
   1.213 -}
   1.214 -
   1.215 -IPluginContext *BaseRuntime::GetDefaultContext()
   1.216 -{
   1.217 -	return m_pCtx;
   1.218 -}
   1.219 -
   1.220 -IPluginDebugInfo *BaseRuntime::GetDebugInfo()
   1.221 -{
   1.222 -	if (!IsDebugging())
   1.223 -	{
   1.224 -		return NULL;
   1.225 -	}
   1.226 -
   1.227 -	return &m_Debug;
   1.228 -}
   1.229 -
   1.230 -void BaseRuntime::RefreshFunctionCache()
   1.231 -{
   1.232 -	if (m_PubFuncs != NULL)
   1.233 -	{
   1.234 -		sp_public_t *pub;
   1.235 -		for (uint32_t i = 0; i < m_pPlugin->info.publics_num; i++)
   1.236 -		{
   1.237 -			if (m_PubFuncs[i] == NULL)
   1.238 -			{
   1.239 -				continue;
   1.240 -			}
   1.241 -			if (GetPublicByIndex(i, &pub) != SP_ERROR_NONE)
   1.242 -			{
   1.243 -				continue;
   1.244 -			}
   1.245 -			m_PubFuncs[i]->Set(pub->code_offs, this, (i << 1) | 1, i);
   1.246 -		}
   1.247 -	}
   1.248 -
   1.249 -}
   1.250 -
   1.251 -IPluginFunction *BaseRuntime::GetFunctionById(funcid_t func_id)
   1.252 -{
   1.253 -	CFunction *pFunc = NULL;
   1.254 -
   1.255 -	if (func_id & 1)
   1.256 -	{
   1.257 -		func_id >>= 1;
   1.258 -		if (func_id >= m_pPlugin->info.publics_num)
   1.259 -		{
   1.260 -			return NULL;
   1.261 -		}
   1.262 -		pFunc = m_PubFuncs[func_id];
   1.263 -		if (!pFunc)
   1.264 -		{
   1.265 -			m_PubFuncs[func_id] = new CFunction(m_pPlugin->publics[func_id].code_offs, 
   1.266 -				this, 
   1.267 -				(func_id << 1) | 1,
   1.268 -				func_id);
   1.269 -			pFunc = m_PubFuncs[func_id];
   1.270 -		}
   1.271 -		else if (pFunc->IsInvalidated())
   1.272 -		{
   1.273 -			pFunc->Set(m_pPlugin->publics[func_id].code_offs, 
   1.274 -				this,
   1.275 -				(func_id << 1) | 1,
   1.276 -				func_id);
   1.277 -		}
   1.278 -	}
   1.279 -
   1.280 -	return pFunc;
   1.281 -}
   1.282 -
   1.283 -IPluginFunction *BaseRuntime::GetFunctionByName(const char *public_name)
   1.284 -{
   1.285 -	uint32_t index;
   1.286 -
   1.287 -	if (FindPublicByName(public_name, &index) != SP_ERROR_NONE)
   1.288 -	{
   1.289 -		return NULL;
   1.290 -	}
   1.291 -
   1.292 -	CFunction *pFunc = m_PubFuncs[index];
   1.293 -	if (!pFunc)
   1.294 -	{
   1.295 -		sp_public_t *pub = NULL;
   1.296 -		GetPublicByIndex(index, &pub);
   1.297 -		if (pub)
   1.298 -		{
   1.299 -			m_PubFuncs[index] = new CFunction(pub->code_offs, this, (index << 1) | 1, index);
   1.300 -		}
   1.301 -		pFunc = m_PubFuncs[index];
   1.302 -	}
   1.303 -	else if (pFunc->IsInvalidated())
   1.304 -	{
   1.305 -		sp_public_t *pub = NULL;
   1.306 -		GetPublicByIndex(index, &pub);
   1.307 -		if (pub)
   1.308 -		{
   1.309 -			pFunc->Set(pub->code_offs, this, (index << 1) | 1, index);
   1.310 -		}
   1.311 -		else
   1.312 -		{
   1.313 -			pFunc = NULL;
   1.314 -		}
   1.315 -	}
   1.316 -
   1.317 -	return pFunc;
   1.318 -}
   1.319 -
   1.320 -bool BaseRuntime::IsDebugging()
   1.321 -{
   1.322 -	return ((m_pPlugin->run_flags & SPFLAG_PLUGIN_DEBUG) == SPFLAG_PLUGIN_DEBUG);
   1.323 -}
   1.324 -
   1.325 -void BaseRuntime::SetPauseState(bool paused)
   1.326 -{
   1.327 -	if (paused)
   1.328 -	{
   1.329 -		m_pPlugin->run_flags |= SPFLAG_PLUGIN_PAUSED;
   1.330 -	}
   1.331 -	else
   1.332 -	{
   1.333 -		m_pPlugin->run_flags &= ~SPFLAG_PLUGIN_PAUSED;
   1.334 -	}
   1.335 -}
   1.336 -
   1.337 -bool BaseRuntime::IsPaused()
   1.338 -{
   1.339 -	return ((m_pPlugin->run_flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED);
   1.340 -}
   1.341 -
   1.342 -size_t BaseRuntime::GetMemUsage()
   1.343 -{
   1.344 -	size_t mem = 0;
   1.345 -
   1.346 -	mem += sizeof(this);
   1.347 -	mem += sizeof(sp_plugin_t);
   1.348 -	mem += sizeof(BaseContext);
   1.349 -	mem += m_pPlugin->base_size;
   1.350 -	mem += m_pPlugin->jit_codesize;
   1.351 -	mem += m_pPlugin->jit_memsize;
   1.352 -
   1.353 -	return mem;
   1.354 -}
   1.355 -
   1.356 -BaseContext *BaseRuntime::GetBaseContext()
   1.357 -{
   1.358 -	return m_pCtx;
   1.359 -}
   1.360 -