1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcepawn/jit/sp_vm_function.cpp Sun Jul 06 04:59:39 2008 +0000
1.3 @@ -0,0 +1,366 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourcePawn
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include <stdio.h>
1.36 +#include <string.h>
1.37 +#include "sp_vm_function.h"
1.38 +#include "sm_stringutil.h"
1.39 +
1.40 +/********************
1.41 +* FUNCTION CALLING *
1.42 +********************/
1.43 +
1.44 +void CFunction::Set(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id)
1.45 +{
1.46 + m_codeaddr = code_addr;
1.47 + m_pContext = plugin;
1.48 + m_curparam = 0;
1.49 + m_errorstate = SP_ERROR_NONE;
1.50 + m_Invalid = false;
1.51 + m_pCtx = plugin ? plugin->GetContext() : NULL;
1.52 + m_FnId = id;
1.53 +
1.54 + m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
1.55 +}
1.56 +
1.57 +bool CFunction::IsRunnable()
1.58 +{
1.59 + return ((m_pCtx->flags & SPFLAG_PLUGIN_PAUSED) != SPFLAG_PLUGIN_PAUSED);
1.60 +}
1.61 +
1.62 +int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
1.63 +{
1.64 + int ir, serial;
1.65 +
1.66 + if (!IsRunnable())
1.67 + {
1.68 + return SP_ERROR_NOT_RUNNABLE;
1.69 + }
1.70 +
1.71 + if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
1.72 + && m_pPublic != NULL)
1.73 + {
1.74 + serial = m_pCtx->profiler->OnCallbackBegin(m_pContext, m_pPublic);
1.75 + }
1.76 +
1.77 + while (num_params--)
1.78 + {
1.79 + m_pContext->PushCell(params[num_params]);
1.80 + }
1.81 +
1.82 + ir = m_pContext->Execute(m_codeaddr, result);
1.83 +
1.84 + if ((m_pCtx->prof_flags & SP_PROF_CALLBACKS) == SP_PROF_CALLBACKS
1.85 + && m_pPublic != NULL)
1.86 + {
1.87 + m_pCtx->profiler->OnCallbackEnd(serial);
1.88 + }
1.89 +
1.90 + return ir;
1.91 +}
1.92 +
1.93 +IPluginContext *CFunction::GetParentContext()
1.94 +{
1.95 + return m_pContext;
1.96 +}
1.97 +
1.98 +CFunction::CFunction(uint32_t code_addr, IPluginContext *plugin, funcid_t id, uint32_t pub_id) :
1.99 + m_codeaddr(code_addr), m_pContext(plugin), m_curparam(0),
1.100 + m_errorstate(SP_ERROR_NONE), m_FnId(id)
1.101 +{
1.102 + m_Invalid = false;
1.103 + if (plugin)
1.104 + {
1.105 + m_pCtx = plugin->GetContext();
1.106 + }
1.107 + m_pContext->GetPublicByIndex(pub_id, &m_pPublic);
1.108 +}
1.109 +
1.110 +int CFunction::PushCell(cell_t cell)
1.111 +{
1.112 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.113 + {
1.114 + return SetError(SP_ERROR_PARAMS_MAX);
1.115 + }
1.116 +
1.117 + m_info[m_curparam].marked = false;
1.118 + m_params[m_curparam] = cell;
1.119 + m_curparam++;
1.120 +
1.121 + return SP_ERROR_NONE;
1.122 +}
1.123 +
1.124 +int CFunction::PushCellByRef(cell_t *cell, int flags)
1.125 +{
1.126 + return PushArray(cell, 1, flags);
1.127 +}
1.128 +
1.129 +int CFunction::PushFloat(float number)
1.130 +{
1.131 + cell_t val = *(cell_t *)&number;
1.132 +
1.133 + return PushCell(val);
1.134 +}
1.135 +
1.136 +int CFunction::PushFloatByRef(float *number, int flags)
1.137 +{
1.138 + return PushCellByRef((cell_t *)number, flags);
1.139 +}
1.140 +
1.141 +int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
1.142 +{
1.143 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.144 + {
1.145 + return SetError(SP_ERROR_PARAMS_MAX);
1.146 + }
1.147 +
1.148 + ParamInfo *info = &m_info[m_curparam];
1.149 +
1.150 + info->flags = inarray ? copyback : 0;
1.151 + info->marked = true;
1.152 + info->size = cells;
1.153 + info->str.is_sz = false;
1.154 + info->orig_addr = inarray;
1.155 +
1.156 + m_curparam++;
1.157 +
1.158 + return SP_ERROR_NONE;
1.159 +}
1.160 +
1.161 +int CFunction::PushString(const char *string)
1.162 +{
1.163 + return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
1.164 +}
1.165 +
1.166 +int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
1.167 +{
1.168 + return _PushString(buffer, sz_flags, cp_flags, length);
1.169 +}
1.170 +
1.171 +int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
1.172 +{
1.173 + if (m_curparam >= SP_MAX_EXEC_PARAMS)
1.174 + {
1.175 + return SetError(SP_ERROR_PARAMS_MAX);
1.176 + }
1.177 +
1.178 + ParamInfo *info = &m_info[m_curparam];
1.179 +
1.180 + info->marked = true;
1.181 + info->orig_addr = (cell_t *)string;
1.182 + info->flags = cp_flags;
1.183 + info->size = len;
1.184 + info->str.sz_flags = sz_flags;
1.185 + info->str.is_sz = true;
1.186 +
1.187 + m_curparam++;
1.188 +
1.189 + return SP_ERROR_NONE;
1.190 +}
1.191 +
1.192 +void CFunction::Cancel()
1.193 +{
1.194 + if (!m_curparam)
1.195 + {
1.196 + return;
1.197 + }
1.198 +
1.199 + m_errorstate = SP_ERROR_NONE;
1.200 + m_curparam = 0;
1.201 +}
1.202 +
1.203 +int CFunction::Execute(cell_t *result)
1.204 +{
1.205 + int err = SP_ERROR_NONE;
1.206 +
1.207 + if (!IsRunnable())
1.208 + {
1.209 + m_errorstate = SP_ERROR_NOT_RUNNABLE;
1.210 + }
1.211 +
1.212 + if (m_errorstate != SP_ERROR_NONE)
1.213 + {
1.214 + err = m_errorstate;
1.215 + Cancel();
1.216 + return err;
1.217 + }
1.218 +
1.219 + //This is for re-entrancy!
1.220 + cell_t temp_params[SP_MAX_EXEC_PARAMS];
1.221 + ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
1.222 + unsigned int numparams = m_curparam;
1.223 + unsigned int i;
1.224 + bool docopies = true;
1.225 +
1.226 + if (numparams)
1.227 + {
1.228 + //Save the info locally, then reset it for re-entrant calls.
1.229 + memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
1.230 + }
1.231 + m_curparam = 0;
1.232 +
1.233 + /* Browse the parameters and build arrays */
1.234 + for (i=0; i<numparams; i++)
1.235 + {
1.236 + /* Is this marked as an array? */
1.237 + if (temp_info[i].marked)
1.238 + {
1.239 + if (!temp_info[i].str.is_sz)
1.240 + {
1.241 + /* Allocate a normal/generic array */
1.242 + if ((err=m_pContext->HeapAlloc(temp_info[i].size,
1.243 + &(temp_info[i].local_addr),
1.244 + &(temp_info[i].phys_addr)))
1.245 + != SP_ERROR_NONE)
1.246 + {
1.247 + break;
1.248 + }
1.249 + if (temp_info[i].orig_addr)
1.250 + {
1.251 + memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
1.252 + }
1.253 + }
1.254 + else
1.255 + {
1.256 + /* Calculate cells required for the string */
1.257 + size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
1.258 +
1.259 + /* Allocate the buffer */
1.260 + if ((err=m_pContext->HeapAlloc(cells,
1.261 + &(temp_info[i].local_addr),
1.262 + &(temp_info[i].phys_addr)))
1.263 + != SP_ERROR_NONE)
1.264 + {
1.265 + break;
1.266 + }
1.267 + /* Copy original string if necessary */
1.268 + if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
1.269 + {
1.270 + /* Cut off UTF-8 properly */
1.271 + if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
1.272 + {
1.273 + if ((err=m_pContext->StringToLocalUTF8(temp_info[i].local_addr,
1.274 + temp_info[i].size,
1.275 + (const char *)temp_info[i].orig_addr,
1.276 + NULL))
1.277 + != SP_ERROR_NONE)
1.278 + {
1.279 + break;
1.280 + }
1.281 + }
1.282 + /* Copy a binary blob */
1.283 + else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
1.284 + {
1.285 + memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
1.286 + }
1.287 + /* Copy ASCII characters */
1.288 + else
1.289 + {
1.290 + if ((err=m_pContext->StringToLocal(temp_info[i].local_addr,
1.291 + temp_info[i].size,
1.292 + (const char *)temp_info[i].orig_addr))
1.293 + != SP_ERROR_NONE)
1.294 + {
1.295 + break;
1.296 + }
1.297 + }
1.298 + }
1.299 + } /* End array/string calculation */
1.300 + /* Update the pushed parameter with the byref local address */
1.301 + temp_params[i] = temp_info[i].local_addr;
1.302 + }
1.303 + else
1.304 + {
1.305 + /* Just copy the value normally */
1.306 + temp_params[i] = m_params[i];
1.307 + }
1.308 + }
1.309 +
1.310 + /* Make the call if we can */
1.311 + if (err == SP_ERROR_NONE)
1.312 + {
1.313 + if ((err = CallFunction(temp_params, numparams, result)) != SP_ERROR_NONE)
1.314 + {
1.315 + docopies = false;
1.316 + }
1.317 + }
1.318 + else
1.319 + {
1.320 + docopies = false;
1.321 + }
1.322 +
1.323 + /* i should be equal to the last valid parameter + 1 */
1.324 + while (i--)
1.325 + {
1.326 + if (!temp_info[i].marked)
1.327 + {
1.328 + continue;
1.329 + }
1.330 +
1.331 + if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
1.332 + {
1.333 + if (temp_info[i].orig_addr)
1.334 + {
1.335 + if (temp_info[i].str.is_sz)
1.336 + {
1.337 + memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
1.338 +
1.339 + }
1.340 + else
1.341 + {
1.342 + if (temp_info[i].size == 1)
1.343 + {
1.344 + *temp_info[i].orig_addr = *(temp_info[i].phys_addr);
1.345 + }
1.346 + else
1.347 + {
1.348 + memcpy(temp_info[i].orig_addr,
1.349 + temp_info[i].phys_addr,
1.350 + temp_info[i].size * sizeof(cell_t));
1.351 + }
1.352 + }
1.353 + }
1.354 + }
1.355 +
1.356 + if ((err=m_pContext->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
1.357 + {
1.358 + return err;
1.359 + }
1.360 + }
1.361 +
1.362 + return err;
1.363 +}
1.364 +
1.365 +funcid_t CFunction::GetFunctionID()
1.366 +{
1.367 + return m_FnId;
1.368 +}
1.369 +