sourcepawn/jit/sp_vm_function.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 07 04:56:29 2008 +0000 (2008-07-07)
branchrefac-jit
changeset 2171 0885b66fcf81
parent 2169 d1e225d141fb
child 2188 24b170ea6ce7
permissions -rw-r--r--
refac-jit is almost done i think, next comes getting core compatible.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourcePawn
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdio.h>
    33 #include <string.h>
    34 #include "sp_vm_function.h"
    35 #include "BaseRuntime.h"
    36 
    37 /********************
    38 * FUNCTION CALLING *
    39 ********************/
    40 
    41 void CFunction::Set(uint32_t code_addr, BaseRuntime *runtime, funcid_t fnid, uint32_t pub_id)
    42 {
    43 	m_codeaddr = code_addr;
    44 	m_pRuntime = runtime;
    45 	m_curparam = 0;
    46 	m_errorstate = SP_ERROR_NONE;
    47 	m_Invalid = false;
    48 	m_FnId = fnid;
    49 }
    50 
    51 bool CFunction::IsRunnable()
    52 {
    53 	return m_pRuntime->IsPaused();
    54 }
    55 
    56 int CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result)
    57 {
    58 	return CallFunction(m_pRuntime->GetDefaultContext(), params, num_params, result);
    59 }
    60 
    61 int CFunction::CallFunction(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result)
    62 {
    63 	return pContext->Execute(this, params, num_params, result);
    64 }
    65 
    66 IPluginContext *CFunction::GetParentContext()
    67 {
    68 	return m_pRuntime->GetDefaultContext();
    69 }
    70 
    71 CFunction::CFunction(uint32_t code_addr, BaseRuntime *runtime, funcid_t id, uint32_t pub_id) : 
    72 	m_codeaddr(code_addr), m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id)
    73 {
    74 	m_Invalid = false;
    75 	m_pRuntime = runtime;
    76 }
    77 
    78 int CFunction::PushCell(cell_t cell)
    79 {
    80 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
    81 	{
    82 		return SetError(SP_ERROR_PARAMS_MAX);
    83 	}
    84 
    85 	m_info[m_curparam].marked = false;
    86 	m_params[m_curparam] = cell;
    87 	m_curparam++;
    88 
    89 	return SP_ERROR_NONE;
    90 }
    91 
    92 int CFunction::PushCellByRef(cell_t *cell, int flags)
    93 {
    94 	return PushArray(cell, 1, flags);
    95 }
    96 
    97 int CFunction::PushFloat(float number)
    98 {
    99 	cell_t val = *(cell_t *)&number;
   100 
   101 	return PushCell(val);
   102 }
   103 
   104 int CFunction::PushFloatByRef(float *number, int flags)
   105 {
   106 	return PushCellByRef((cell_t *)number, flags);
   107 }
   108 
   109 int CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback)
   110 {
   111 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   112 	{
   113 		return SetError(SP_ERROR_PARAMS_MAX);
   114 	}
   115 
   116 	ParamInfo *info = &m_info[m_curparam];
   117 
   118 	info->flags = inarray ? copyback : 0;
   119 	info->marked = true;
   120 	info->size = cells;
   121 	info->str.is_sz = false;
   122 	info->orig_addr = inarray;
   123 
   124 	m_curparam++;
   125 
   126 	return SP_ERROR_NONE;
   127 }
   128 
   129 int CFunction::PushString(const char *string)
   130 {
   131 	return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1);
   132 }
   133 
   134 int CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
   135 {
   136 	return _PushString(buffer, sz_flags, cp_flags, length);
   137 }
   138 
   139 int CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len)
   140 {
   141 	if (m_curparam >= SP_MAX_EXEC_PARAMS)
   142 	{
   143 		return SetError(SP_ERROR_PARAMS_MAX);
   144 	}
   145 
   146 	ParamInfo *info = &m_info[m_curparam];
   147 
   148 	info->marked = true;
   149 	info->orig_addr = (cell_t *)string;
   150 	info->flags = cp_flags;
   151 	info->size = len;
   152 	info->str.sz_flags = sz_flags;
   153 	info->str.is_sz = true;
   154 
   155 	m_curparam++;
   156 
   157 	return SP_ERROR_NONE;
   158 }
   159 
   160 void CFunction::Cancel()
   161 {
   162 	if (!m_curparam)
   163 	{
   164 		return;
   165 	}
   166 
   167 	m_errorstate = SP_ERROR_NONE;
   168 	m_curparam = 0;
   169 }
   170 
   171 int CFunction::Execute(cell_t *result)
   172 {
   173 	return Execute(m_pRuntime->GetDefaultContext(), result);
   174 }
   175 
   176 int CFunction::Execute(IPluginContext *ctx, cell_t *result)
   177 {
   178 	int err = SP_ERROR_NONE;
   179 
   180 	if (!IsRunnable())
   181 	{
   182 		m_errorstate = SP_ERROR_NOT_RUNNABLE;
   183 	}
   184 
   185 	if (m_errorstate != SP_ERROR_NONE)
   186 	{
   187 		err = m_errorstate;
   188 		Cancel();
   189 		return err;
   190 	}
   191 
   192 	//This is for re-entrancy!
   193 	cell_t temp_params[SP_MAX_EXEC_PARAMS];
   194 	ParamInfo temp_info[SP_MAX_EXEC_PARAMS];
   195 	unsigned int numparams = m_curparam;
   196 	unsigned int i;
   197 	bool docopies = true;
   198 
   199 	if (numparams)
   200 	{
   201 		//Save the info locally, then reset it for re-entrant calls.
   202 		memcpy(temp_info, m_info, numparams * sizeof(ParamInfo));
   203 	}
   204 	m_curparam = 0;
   205 
   206 	/* Browse the parameters and build arrays */
   207 	for (i=0; i<numparams; i++)
   208 	{
   209 		/* Is this marked as an array? */
   210 		if (temp_info[i].marked)
   211 		{
   212 			if (!temp_info[i].str.is_sz)
   213 			{
   214 				/* Allocate a normal/generic array */
   215 				if ((err=ctx->HeapAlloc(temp_info[i].size, 
   216 										   &(temp_info[i].local_addr),
   217 										   &(temp_info[i].phys_addr)))
   218 					!= SP_ERROR_NONE)
   219 				{
   220 					break;
   221 				}
   222 				if (temp_info[i].orig_addr)
   223 				{
   224 					memcpy(temp_info[i].phys_addr, temp_info[i].orig_addr, sizeof(cell_t) * temp_info[i].size);
   225 				}
   226 			}
   227 			else
   228 			{
   229 				/* Calculate cells required for the string */
   230 				size_t cells = (temp_info[i].size + sizeof(cell_t) - 1) / sizeof(cell_t);
   231 
   232 				/* Allocate the buffer */
   233 				if ((err=ctx->HeapAlloc(cells,
   234 										&(temp_info[i].local_addr),
   235 										&(temp_info[i].phys_addr)))
   236 					!= SP_ERROR_NONE)
   237 				{
   238 					break;
   239 				}
   240 				/* Copy original string if necessary */
   241 				if ((temp_info[i].str.sz_flags & SM_PARAM_STRING_COPY) && (temp_info[i].orig_addr != NULL))
   242 				{
   243 					/* Cut off UTF-8 properly */
   244 					if (temp_info[i].str.sz_flags & SM_PARAM_STRING_UTF8)
   245 					{
   246 						if ((err=ctx->StringToLocalUTF8(temp_info[i].local_addr, 
   247 															temp_info[i].size, 
   248 															(const char *)temp_info[i].orig_addr,
   249 															NULL))
   250 							!= SP_ERROR_NONE)
   251 						{
   252 							break;
   253 						}
   254 					}
   255 					/* Copy a binary blob */
   256 					else if (temp_info[i].str.sz_flags & SM_PARAM_STRING_BINARY)
   257 					{
   258 						memmove(temp_info[i].phys_addr, temp_info[i].orig_addr, temp_info[i].size);
   259 					}
   260 					/* Copy ASCII characters */
   261 					else
   262 					{
   263 						if ((err=ctx->StringToLocal(temp_info[i].local_addr,
   264 														temp_info[i].size,
   265 														(const char *)temp_info[i].orig_addr))
   266 							!= SP_ERROR_NONE)
   267 						{
   268 							break;
   269 						}
   270 					}
   271 				}
   272 			} /* End array/string calculation */
   273 			/* Update the pushed parameter with the byref local address */
   274 			temp_params[i] = temp_info[i].local_addr;
   275 		}
   276 		else
   277 		{
   278 			/* Just copy the value normally */
   279 			temp_params[i] = m_params[i];
   280 		}
   281 	}
   282 
   283 	/* Make the call if we can */
   284 	if (err == SP_ERROR_NONE)
   285 	{
   286 		if ((err = CallFunction(ctx, temp_params, numparams, result)) != SP_ERROR_NONE)
   287 		{
   288 			docopies = false;
   289 		}
   290 	}
   291 	else
   292 	{
   293 		docopies = false;
   294 	}
   295 
   296 	/* i should be equal to the last valid parameter + 1 */
   297 	while (i--)
   298 	{
   299 		if (!temp_info[i].marked)
   300 		{
   301 			continue;
   302 		}
   303 
   304 		if (docopies && (temp_info[i].flags & SM_PARAM_COPYBACK))
   305 		{
   306 			if (temp_info[i].orig_addr)
   307 			{
   308 				if (temp_info[i].str.is_sz)
   309 				{
   310 					memcpy(temp_info[i].orig_addr, temp_info[i].phys_addr, temp_info[i].size);
   311 				
   312 				}
   313 				else
   314 				{
   315 					if (temp_info[i].size == 1)
   316 					{
   317 						*temp_info[i].orig_addr = *(temp_info[i].phys_addr);
   318 					}
   319 					else
   320 					{
   321 						memcpy(temp_info[i].orig_addr, 
   322 								temp_info[i].phys_addr, 
   323 								temp_info[i].size * sizeof(cell_t));
   324 					}
   325 				}
   326 			}
   327 		}
   328 
   329 		if ((err=ctx->HeapPop(temp_info[i].local_addr)) != SP_ERROR_NONE)
   330 		{
   331 			return err;
   332 		}
   333 	}
   334 
   335 	return err;
   336 }
   337 
   338 funcid_t CFunction::GetFunctionID()
   339 {
   340 	return m_FnId;
   341 }
   342 
   343 int CFunction::SetError(int err)
   344 {
   345 	m_errorstate = err;
   346 
   347 	return err;
   348 }
   349 
   350 bool CFunction::IsInvalidated()
   351 {
   352 	return m_Invalid;
   353 }
   354 
   355