sourcepawn/jit/x86/jit_x86.h
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 831 1995c0dd5c04
child 1330 ab94316bb52b
permissions -rw-r--r--
Removed strange debugging code
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourcePawn (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ================================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #ifndef _INCLUDE_SOURCEPAWN_JIT_X86_H_
    16 #define _INCLUDE_SOURCEPAWN_JIT_X86_H_
    17 
    18 #include <sp_vm_types.h>
    19 #include <sp_vm_api.h>
    20 #include <jit_helpers.h>
    21 
    22 using namespace SourcePawn;
    23 
    24 #define JIT_INLINE_ERRORCHECKS		(1<<0)
    25 #define JIT_INLINE_NATIVES			(1<<1)
    26 #define STACK_MARGIN				64			//8 parameters of safety, I guess
    27 #define JIT_FUNCMAGIC				0x214D4148	//magic function offset
    28 
    29 #define JITVARS_TRACKER				0		//important: don't change this to avoid trouble
    30 #define JITVARS_FUNCINFO			1		//important: don't change this aWOAWOGJQG I LIKE HAM
    31 #define JITVARS_REBASE				2		//important: hi, i'm bail
    32 
    33 typedef struct tracker_s
    34 {
    35 	size_t size; 
    36 	ucell_t *pBase; 
    37 	ucell_t *pCur;
    38 } tracker_t;
    39 
    40 typedef struct funcinfo_s
    41 {
    42 	unsigned int magic;
    43 	unsigned int index;
    44 } funcinfo_t;
    45 
    46 typedef struct functracker_s
    47 {
    48 	unsigned int num_functions;
    49 	unsigned int code_size;
    50 } functracker_t;
    51 
    52 struct floattbl_t
    53 {
    54 	floattbl_t()
    55 	{
    56 		found = false;
    57 		index = 0;
    58 	}
    59 	bool found;
    60 	unsigned int index;
    61 };
    62 
    63 class CompData : public ICompilation
    64 {
    65 public:
    66 	CompData() : plugin(NULL), 
    67 		debug(false), inline_level(0), rebase(NULL),
    68 		error_set(SP_ERROR_NONE), func_idx(0)
    69 	{
    70 	};
    71 public:
    72 	sp_plugin_t *plugin;			/* plugin handle */
    73 	bool debug;						/* whether to compile debug mode */
    74 	int inline_level;				/* inline optimization level */
    75 	jitcode_t rebase;				/* relocation map */
    76 	int error_set;					/* error code to halt process */
    77 	unsigned int func_idx;			/* current function index */
    78 	jitoffs_t jit_return;			/* point in main call to return to */
    79 	jitoffs_t jit_verify_addr_eax;
    80 	jitoffs_t jit_verify_addr_edx;
    81 	jitoffs_t jit_break;			/* call to op.break */
    82 	jitoffs_t jit_sysreq_n;			/* call version of op.sysreq.n */
    83 	jitoffs_t jit_genarray;			/* call to genarray intrinsic */
    84 	jitoffs_t jit_error_bounds;
    85 	jitoffs_t jit_error_divzero;
    86 	jitoffs_t jit_error_stacklow;
    87 	jitoffs_t jit_error_stackmin;
    88 	jitoffs_t jit_error_memaccess;
    89 	jitoffs_t jit_error_heaplow;
    90 	jitoffs_t jit_error_heapmin;
    91 	jitoffs_t jit_error_array_too_big;
    92 	jitoffs_t jit_extern_error;		/* returning generic error */
    93 	jitoffs_t jit_sysreq_c;			/* old version! */
    94 	jitoffs_t jit_rounding_table;
    95 	floattbl_t *jit_float_table;
    96 	uint32_t codesize;				/* total codesize */
    97 };
    98 
    99 class JITX86 : public IVirtualMachine
   100 {
   101 public:
   102 	const char *GetVMName();
   103 	ICompilation *StartCompilation(sp_plugin_t *plugin);
   104 	bool SetCompilationOption(ICompilation *co, const char *key, const char *val);
   105 	sp_context_t *CompileToContext(ICompilation *co, int *err);
   106 	void AbortCompilation(ICompilation *co);
   107 	void FreeContext(sp_context_t *ctx);
   108 	int ContextExecute(sp_context_t *ctx, uint32_t code_idx, cell_t *result);
   109 	unsigned int GetAPIVersion();
   110 	bool FunctionLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
   111 	bool FunctionPLookup(const sp_context_t *ctx, uint32_t code_addr, unsigned int *result);
   112 	unsigned int FunctionCount(const sp_context_t *ctx);
   113 	const char *GetVersionString();
   114 	const char *GetCPUOptimizations();
   115 	SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData);
   116 	void DestroyFakeNative(SPVM_NATIVE_FUNC func);
   117 };
   118 
   119 cell_t NativeCallback(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
   120 cell_t NativeCallback_Debug(sp_context_t *ctx, ucell_t native_idx, cell_t *params);
   121 jitoffs_t RelocLookup(JitWriter *jit, cell_t pcode_offs, bool relative=false);
   122 
   123 #define AMX_REG_PRI		REG_EAX
   124 #define AMX_REG_ALT		REG_EDX
   125 #define AMX_REG_STK		REG_EDI
   126 #define AMX_REG_DAT		REG_EBP
   127 #define AMX_REG_TMP		REG_ECX
   128 #define AMX_REG_INFO	REG_ESI
   129 #define AMX_REG_FRM		REG_EBX
   130 
   131 #define AMX_INFO_FRM		AMX_REG_INFO	//not relocated
   132 #define AMX_INFO_FRAME		0				//(same thing as above) 
   133 #define AMX_INFO_HEAP		4				//not relocated
   134 #define AMX_INFO_RETVAL		8				//physical
   135 #define AMX_INFO_CONTEXT	12				//physical
   136 #define AMX_INFO_STACKTOP	16				//relocated
   137 
   138 extern ISourcePawnEngine *engine;
   139 
   140 #endif //_INCLUDE_SOURCEPAWN_JIT_X86_H_